Skip to content

Commit

Permalink
optimize form
Browse files Browse the repository at this point in the history
  • Loading branch information
nggit committed Nov 29, 2023
1 parent fa6452c commit 2ad42a3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion tests/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ async def get_lock(**server):
async def post_form(**server):
request = server['request']

await request.form()
await request.form(limit=8192)

data = []

Expand Down
12 changes: 12 additions & 0 deletions tests/test_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ def test_post_form_ok_11(self):
self.assertEqual(read_chunked(body),
b'username=myuser&password=mypass')

def test_post_form_limit(self):
header, body = getcontents(host=HTTP_HOST,
port=HTTP_PORT,
method='POST',
url='/submitform',
version='1.1',
data='d' * 8193)

self.assertEqual(header[:header.find(b'\r\n')],
b'HTTP/1.1 500 Internal Server Error')
self.assertEqual(body, b'Internal Server Error')

def test_post_upload_ok_10(self):
header, body = getcontents(
host=HTTP_HOST,
Expand Down
5 changes: 4 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ def getcontents(
method='GET',
url='/',
version='1.1',
headers=[],
headers=None,
data='',
raw=b''
):
if raw == b'':
if not headers:
headers = []

content_length = len(data)

if content_length > 0:
Expand Down
8 changes: 4 additions & 4 deletions tremolo/lib/http_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,10 @@ def cookies(self):
if b'cookie' in self.headers:
if isinstance(self.headers[b'cookie'], list):
self.params['cookies'] = parse_qs(
b'; '.join(self.headers[b'cookie'])
b'&'.join(self.headers[b'cookie'])
.replace(b'; ', b'&').replace(b';', b'&')
.decode('latin-1'),
max_num_fields=100
max_num_fields=100 * len(self.headers[b'cookie'])
)
else:
self.params['cookies'] = parse_qs(
Expand All @@ -290,7 +290,7 @@ def cookies(self):
async def form(self, limit=8 * 1048576, max_fields=100):
try:
return self.params['post']
except KeyError:
except KeyError as exc:
self.params['post'] = {}

if (b'application/x-www-form-urlencoded' in
Expand All @@ -299,7 +299,7 @@ async def form(self, limit=8 * 1048576, max_fields=100):
self._body.extend(data)

if self.body_size > limit:
break
raise ValueError('form limit reached') from exc

if 2 < self.body_size <= limit:
self.params['post'] = parse_qs(
Expand Down
4 changes: 2 additions & 2 deletions tremolo/tremolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,14 @@ def getoptions(self, func):

return options

def add_middleware(self, func, name='request', kwargs={}):
def add_middleware(self, func, name='request', kwargs=None):
if name not in self.middlewares:
raise ValueError('%s is not one of the: %s' %
(name, ', '.join(self.middlewares)))

self.middlewares[name].append((func, kwargs or self.getoptions(func)))

def add_route(self, func, path='/', kwargs={}):
def add_route(self, func, path='/', kwargs=None):
if not kwargs:
kwargs = self.getoptions(func)

Expand Down

0 comments on commit 2ad42a3

Please sign in to comment.