Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #530 from alekstorm/body_args

Factored out body argument-parsing code into httputil.parse_body_arguments
  • Loading branch information...
commit 31c3f14a163fc877320830a8e3296a83aefa2ce5 2 parents ef9df4d + 4b4e890
@bdarnell bdarnell authored
Showing with 25 additions and 33 deletions.
  1. +4 −21 tornado/httpserver.py
  2. +19 −0 tornado/httputil.py
  3. +2 −12 tornado/wsgi.py
View
25 tornado/httpserver.py
@@ -31,7 +31,7 @@ class except to start a server at the beginning of the process
import socket
import time
-from tornado.escape import utf8, native_str, parse_qs_bytes
+from tornado.escape import native_str, parse_qs_bytes
from tornado import httputil
from tornado import iostream
from tornado.netutil import TCPServer
@@ -268,27 +268,10 @@ def _on_headers(self, data):
def _on_request_body(self, data):
self._request.body = data
- content_type = self._request.headers.get("Content-Type", "")
if self._request.method in ("POST", "PATCH", "PUT"):
- if content_type.startswith("application/x-www-form-urlencoded"):
- arguments = parse_qs_bytes(native_str(self._request.body))
- for name, values in arguments.iteritems():
- values = [v for v in values if v]
- if values:
- self._request.arguments.setdefault(name, []).extend(
- values)
- elif content_type.startswith("multipart/form-data"):
- fields = content_type.split(";")
- for field in fields:
- k, sep, v = field.strip().partition("=")
- if k == "boundary" and v:
- httputil.parse_multipart_form_data(
- utf8(v), data,
- self._request.arguments,
- self._request.files)
- break
- else:
- logging.warning("Invalid multipart/form-data")
+ httputil.parse_body_arguments(
+ self._request.headers.get("Content-Type", ""), data,
+ self._request.arguments, self._request.files)
self.request_callback(self._request)
View
19 tornado/httputil.py
@@ -22,6 +22,7 @@
import urllib
import re
+from tornado.escape import native_str, parse_qs_bytes, utf8
from tornado.util import b, ObjectDict
@@ -205,6 +206,24 @@ class HTTPFile(ObjectDict):
pass
+def parse_body_arguments(content_type, body, arguments, files):
+ if content_type.startswith("application/x-www-form-urlencoded"):
+ uri_arguments = parse_qs_bytes(native_str(body))
+ for name, values in uri_arguments.iteritems():
+ values = [v for v in values if v]
+ if values:
+ arguments.setdefault(name, []).extend(values)
+ elif content_type.startswith("multipart/form-data"):
+ fields = content_type.split(";")
+ for field in fields:
+ k, sep, v = field.strip().partition("=")
+ if k == "boundary" and v:
+ parse_multipart_form_data(utf8(v), body, arguments, files)
+ break
+ else:
+ logging.warning("Invalid multipart/form-data")
+
+
def parse_multipart_form_data(boundary, data, arguments, files):
"""Parses a multipart/form-data body.
View
14 tornado/wsgi.py
@@ -165,18 +165,8 @@ def __init__(self, environ):
# Parse request body
self.files = {}
- content_type = self.headers.get("Content-Type", "")
- if content_type.startswith("application/x-www-form-urlencoded"):
- for name, values in parse_qs_bytes(native_str(self.body)).iteritems():
- self.arguments.setdefault(name, []).extend(values)
- elif content_type.startswith("multipart/form-data"):
- if 'boundary=' in content_type:
- boundary = content_type.split('boundary=', 1)[1]
- if boundary:
- httputil.parse_multipart_form_data(
- utf8(boundary), self.body, self.arguments, self.files)
- else:
- logging.warning("Invalid multipart/form-data")
+ httputil.parse_body_arguments(self.headers.get("Content-Type", ""),
+ self.body, self.arguments, self.files)
self._start_time = time.time()
self._finish_time = None
Please sign in to comment.
Something went wrong with that request. Please try again.