Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix handler options #46

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='tremolo',
version='0.0.311',
version='0.0.312',
license='MIT',
author='nggit',
author_email='[email protected]',
Expand Down
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
2 changes: 1 addition & 1 deletion tests/test_tremolo_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from tremolo import Tremolo # noqa: E402
from tremolo.exceptions import BadRequest # noqa: E402
from tremolo.lib.__queue import Queue # noqa: E402
from tremolo.lib.queue import Queue # noqa: E402
from tremolo.lib.connections import KeepAliveConnections # noqa: E402
from tremolo.lib.contexts import ServerContext # noqa: E402
from tremolo.lib.pools import Pool, QueuePool # noqa: E402
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
2 changes: 1 addition & 1 deletion tremolo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.0.311'
__version__ = '0.0.312'

from .tremolo import Tremolo # noqa: E402
from . import exceptions # noqa: E402,F401
Expand Down
7 changes: 3 additions & 4 deletions tremolo/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ def connection_lost(self, exc):
super().connection_lost(exc)

async def _handle_middleware(self, func, options={}):
if not self.response.headers_sent():
self.response.set_base_header()
self.context.set('options', options)
self.response.set_base_header()
self.context.set('options', options)

data = await func(**self._server,
request=self.request,
Expand Down Expand Up @@ -284,7 +283,7 @@ async def headers_received(self):
if self.context.ON_CONNECT is not None:
await self.context.ON_CONNECT

options = self.context.options
options = {}

for middleware in self._middlewares['request']:
options = await self._handle_middleware(
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
2 changes: 1 addition & 1 deletion tremolo/lib/pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from collections import deque

from .__queue import Queue
from .queue import Queue


class Pool:
Expand Down
File renamed without changes.
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