Using latin1 for HTTP headers, and utf8 elsewhere.
This gets all the tests passing under python 3.2 after running 2to3.
def _on_headers(self, data):
try:
- eol = data.find(b("\r\n"))
+ data = data.decode('latin1')
+ eol = data.find("\r\n")
start_line = data[:eol]
try:
method, uri, version = start_line.split(" ")
if content_type.startswith("application/x-www-form-urlencoded"):
arguments = cgi.parse_qs(self._request.body)
for name, values in arguments.iteritems():
+ name = name.decode('utf-8')
values = [v for v in values if v]
if values:
self._request.arguments.setdefault(name, []).extend(
if not name_values.get("name"):
logging.warning("multipart/form-data value missing name")
continue
- name = name_values["name"]
+ name = name_values["name"].decode("utf-8")
if name_values.get("filename"):
ctype = headers.get("Content-Type", "application/unknown")
self._request.files.setdefault(name, []).append(dict(
#!/usr/bin/env python
from __future__ import with_statement
-from tornado.escape import utf8
+from tornado.escape import utf8, _unicode
from tornado.httpclient import HTTPRequest, HTTPResponse, HTTPError, AsyncHTTPClient
from tornado.httputil import HTTPHeaders
from tornado.ioloop import IOLoop
# Timeout handle returned by IOLoop.add_timeout
self._timeout = None
with stack_context.StackContext(self.cleanup):
- parsed = urlparse.urlsplit(self.request.url)
+ parsed = urlparse.urlsplit(_unicode(self.request.url))
if ":" in parsed.netloc:
host, _, port = parsed.netloc.partition(":")
port = int(port)
error=HTTPError(599, "Connection closed")))
def _on_headers(self, data):
+ data = data.decode("latin1")
first_line, _, header_data = data.partition("\r\n")
match = re.match("HTTP/1.[01] ([0-9]+) .*", first_line)
assert match
def test_question_mark(self):
# Ensure that url-encoded question marks are handled properly
- self.assertEqual(json_decode(self.fetch('/%3F').body),
+ self.assertEqual(json_decode(self.fetch('/%3F').body.decode('utf8')),
dict(path='?', args={}))
- self.assertEqual(json_decode(self.fetch('/%3F?%3F=%3F').body),
+ self.assertEqual(json_decode(self.fetch('/%3F?%3F=%3F').body.decode('utf8')),
dict(path='?', args={'?': ['?']}))
def transform_first_chunk(self, headers, chunk, finishing):
if self._gzipping:
- ctype = headers.get("Content-Type", "").split(";")[0]
+ ctype = _unicode(headers.get("Content-Type", "")).split(";")[0]
self._gzipping = (ctype in self.CONTENT_TYPES) and \
(not finishing or len(chunk) >= self.MIN_LENGTH) and \
(finishing or "Content-Length" not in headers) and \