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

default to preserving content length/type (required virtually anytime… #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 15 additions & 9 deletions tinyweb/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,22 @@ async def read_parse_form_data(self):
# request body, at least for simple urlencoded forms - by processing
# chunks instead of accumulating payload.
gc.collect()
if b'Content-Length' not in self.headers:
return {}
# Parse payload depending on content type
if b'Content-Type' not in self.headers:
# Unknown content type, return unparsed, raw data
return {}
size = int(self.headers[b'Content-Length'])
if size > self.params['max_body_size'] or size < 0:
raise HTTPException(413)
data = await self.reader.readexactly(size)
# unknown Content-Type
raise HTTPException(400)
size = int(self.headers[b'Content-Length']) if b'Content-Length' in self.headers else None
if size is not None:
if size > self.params['max_body_size'] or size < 0:
raise HTTPException(413)
data = await self.reader.readexactly(size)
else:
max_body_size = self.params['max_body_size']
data = bytearray()
while x := await self.reader.read(128):
data.extend(x)
if len(data) > max_body_size:
raise HTTPException(413)
# Use only string before ';', e.g:
# application/x-www-form-urlencoded; charset=UTF-8
ct = self.headers[b'Content-Type'].split(b';', 1)[0]
Expand Down Expand Up @@ -519,7 +525,7 @@ def add_route(self, url, f, **kwargs):
raise ValueError('Invalid URL')
# Initial params for route
params = {'methods': ['GET'],
'save_headers': [],
'save_headers': ['Content-Length', 'Content-Type'],
'max_body_size': 1024,
'allowed_access_control_headers': '*',
'allowed_access_control_origins': '*',
Expand Down