Skip to content

Commit

Permalink
Fix the Host header when using basic auth credentials in the URL.
Browse files Browse the repository at this point in the history
  • Loading branch information
bdarnell committed Feb 20, 2012
1 parent 0f30e15 commit 530731c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion tornado/simple_httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ def _on_connect(self, parsed, parsed_hostname):
if "Connection" not in self.request.headers:
self.request.headers["Connection"] = "close"
if "Host" not in self.request.headers:
self.request.headers["Host"] = parsed.netloc
if '@' in parsed.netloc:
self.request.headers["Host"] = parsed.netloc.rpartition('@')[-1]
else:
self.request.headers["Host"] = parsed.netloc
username, password = None, None
if parsed.username is not None:
username, password = parsed.username, parsed.password
Expand Down
16 changes: 16 additions & 0 deletions tornado/test/simple_httpclient_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import collections
import gzip
import logging
import re
import socket

from tornado.ioloop import IOLoop
Expand Down Expand Up @@ -74,6 +75,10 @@ def get(self):
assert not self.request.body
self.write("ok")

class HostEchoHandler(RequestHandler):
def get(self):
self.write(self.request.headers["Host"])


class SimpleHTTPClientTestCase(AsyncHTTPTestCase, LogTrapTestCase):
def setUp(self):
Expand All @@ -95,6 +100,7 @@ def get_app(self):
url("/no_content", NoContentHandler),
url("/303_post", SeeOther303PostHandler),
url("/303_get", SeeOther303GetHandler),
url("/host_echo", HostEchoHandler),
], gzip=True)

def test_singleton(self):
Expand Down Expand Up @@ -239,3 +245,13 @@ def test_no_content(self):
# 204 status with non-zero content length is malformed
response = self.fetch("/no_content?error=1")
self.assertEqual(response.code, 599)

def test_host_header(self):
host_re = re.compile(b("^localhost:[0-9]+$"))
response = self.fetch("/host_echo")
self.assertTrue(host_re.match(response.body))

url = self.get_url("/host_echo").replace("http://", "http://me:secret@")
self.http_client.fetch(url, self.stop)
response = self.wait()
self.assertTrue(host_re.match(response.body), response.body)
2 changes: 2 additions & 0 deletions website/sphinx/releases/next.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ In progress
method).
* The ``Etag`` header is now returned on 304 responses to an ``If-None-Match``
request, improving compatibility with some caches.
* `tornado.simple_httpclient` no longer includes basic auth credentials
in the ``Host`` header when those credentials are extracted from the URL.

0 comments on commit 530731c

Please sign in to comment.