Skip to content

Commit

Permalink
log when multipart/form-data boundary is invalid, and unify sync hand…
Browse files Browse the repository at this point in the history
…ling between wsgi.py and httpserver.py
  • Loading branch information
jehiah committed May 17, 2010
1 parent 18cb45c commit 9d67963
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
9 changes: 7 additions & 2 deletions tornado/httpserver.py
Expand Up @@ -305,11 +305,16 @@ def _on_request_body(self, data):
self._request.arguments.setdefault(name, []).extend(
values)
elif content_type.startswith("multipart/form-data"):
boundary = content_type[30:]
if boundary: self._parse_mime_body(boundary, data)
if 'boundary=' in content_type:
boundary = content_type.split('boundary=',1)[1]
if boundary: self._parse_mime_body(boundary, data)
else:
logging.warning("Invalid multipart/form-data")
self.request_callback(self._request)

def _parse_mime_body(self, boundary, data):
if boundary.startswith('"') and boundary.endswith('"'):
boundary = boundary[1:-1]
if data.endswith("\r\n"):
footer_length = len(boundary) + 6
else:
Expand Down
13 changes: 8 additions & 5 deletions tornado/wsgi.py
Expand Up @@ -125,9 +125,12 @@ def __init__(self, environ):
if content_type.startswith("application/x-www-form-urlencoded"):
for name, values in cgi.parse_qs(self.body).iteritems():
self.arguments.setdefault(name, []).extend(values)
elif content_type.startswith("multipart/form-data") and '=' in content_type:
boundary = content_type.split('=',1)[1]
if boundary: self._parse_mime_body(boundary)
elif content_type.startswith("multipart/form-data"):
if 'boundary=' in content_type:
boundary = content_type.split('boundary=',1)[1]
if boundary: self._parse_mime_body(boundary)
else:
logging.warning("Invalid multipart/form-data")

self._start_time = time.time()
self._finish_time = None
Expand All @@ -148,8 +151,8 @@ def request_time(self):
return self._finish_time - self._start_time

def _parse_mime_body(self, boundary):
if boundary.startswith('"'):
boundary = boundary.strip('"')
if boundary.startswith('"') and boundary.endswith('"'):
boundary = boundary[1:-1]
if self.body.endswith("\r\n"):
footer_length = len(boundary) + 6
else:
Expand Down

0 comments on commit 9d67963

Please sign in to comment.