Skip to content

Commit

Permalink
Handle non-ascii headers correctly in curl_httpclient.
Browse files Browse the repository at this point in the history
Fixes #1608
  • Loading branch information
bdarnell committed Feb 14, 2016
1 parent 7b61d9e commit d7d9c46
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tornado/curl_httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def ioctl(cmd):
request.prepare_curl_callback(curl)

def _curl_header_callback(self, headers, header_callback, header_line):
header_line = native_str(header_line)
header_line = native_str(header_line.decode('latin1'))
if header_callback is not None:
self.io_loop.add_callback(header_callback, header_line)
# header_line as returned by curl includes the end-of-line characters.
Expand Down
18 changes: 17 additions & 1 deletion tornado/test/httpclient_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import datetime
from io import BytesIO

from tornado.escape import utf8
from tornado.escape import utf8, native_str
from tornado import gen
from tornado.httpclient import HTTPRequest, HTTPResponse, _RequestProxy, HTTPError, HTTPClient
from tornado.httpserver import HTTPServer
Expand Down Expand Up @@ -113,6 +113,15 @@ def method(self):

get = post = put = delete = options = patch = other = method


class SetHeaderHandler(RequestHandler):
def get(self):
# Use get_arguments for keys to get strings, but
# request.arguments for values to get bytes.
for k, v in zip(self.get_arguments('k'),
self.request.arguments['v']):
self.set_header(k, v)

# These tests end up getting run redundantly: once here with the default
# HTTPClient implementation, and then again in each implementation's own
# test suite.
Expand All @@ -133,6 +142,7 @@ def get_app(self):
url("/304_with_content_length", ContentLength304Handler),
url("/all_methods", AllMethodsHandler),
url('/patch', PatchHandler),
url('/set_header', SetHeaderHandler),
], gzip=True)

def test_patch_receives_payload(self):
Expand Down Expand Up @@ -520,6 +530,12 @@ def test_put_307(self):
response.rethrow()
self.assertEqual(response.body, b"Put body: hello")

def test_non_ascii_header(self):
# Non-ascii headers are sent as latin1.
response = self.fetch("/set_header?k=foo&v=%E9")
response.rethrow()
self.assertEqual(response.headers["Foo"], native_str(u"\u00e9"))


class RequestProxyTest(unittest.TestCase):
def test_request_set(self):
Expand Down

0 comments on commit d7d9c46

Please sign in to comment.