From: Ben Darnell Date: Mon, 28 Mar 2011 03:30:42 +0000 (-0700) Subject: Add support for username/password in url for simple_httpclient. X-Git-Tag: v2.0.0~110 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8eb6dd249e263f9a93218ec0d8608f96d88b1cef;p=thirdparty%2Ftornado.git Add support for username/password in url for simple_httpclient. I considered making this functionality opt-in, but decided to allow it for consistency with curl. Closes #230. --- diff --git a/tornado/simple_httpclient.py b/tornado/simple_httpclient.py index 38b5bc318..c34f75589 100644 --- a/tornado/simple_httpclient.py +++ b/tornado/simple_httpclient.py @@ -127,12 +127,11 @@ class _HTTPConnection(object): self._timeout = None with stack_context.StackContext(self.cleanup): parsed = urlparse.urlsplit(self.request.url) - if ":" in parsed.netloc: - host, _, port = parsed.netloc.partition(":") - port = int(port) - else: - host = parsed.netloc + host = parsed.hostname + if parsed.port is None: port = 443 if parsed.scheme == "https" else 80 + else: + port = parsed.port if self.client.hostname_mapping is not None: host = self.client.hostname_mapping.get(host, host) @@ -178,7 +177,7 @@ class _HTTPConnection(object): if (self.request.validate_cert and isinstance(self.stream, SSLIOStream)): match_hostname(self.stream.socket.getpeercert(), - parsed.netloc.partition(":")[0]) + parsed.hostname) if (self.request.method not in self._SUPPORTED_METHODS and not self.request.allow_nonstandard_methods): raise KeyError("unknown method %s" % self.request.method) @@ -189,9 +188,14 @@ class _HTTPConnection(object): raise NotImplementedError('%s not supported' % key) if "Host" not in self.request.headers: self.request.headers["Host"] = parsed.netloc - if self.request.auth_username: - auth = "%s:%s" % (self.request.auth_username, - self.request.auth_password) + username, password = None, None + if parsed.username is not None: + username, password = parsed.username, parsed.password + elif self.request.auth_username is not None: + username = self.request.auth_username + password = self.request.auth_password + if username is not None: + auth = "%s:%s" % (username, password) self.request.headers["Authorization"] = ("Basic %s" % auth.encode("base64")) if self.request.user_agent: diff --git a/tornado/test/simple_httpclient_test.py b/tornado/test/simple_httpclient_test.py index 6db079c3e..cb4c695af 100644 --- a/tornado/test/simple_httpclient_test.py +++ b/tornado/test/simple_httpclient_test.py @@ -2,6 +2,7 @@ from __future__ import with_statement +import base64 import collections import gzip import logging @@ -217,3 +218,10 @@ class SimpleHTTPClientTestCase(AsyncHTTPTestCase, LogTrapTestCase): def test_default_certificates_exist(self): open(_DEFAULT_CA_CERTS) + def test_credentials_in_url(self): + url = self.get_url("/auth").replace("http://", "http://me:secret@") + self.http_client.fetch(url, self.stop) + response = self.wait() + self.assertEqual("Basic " + base64.b64encode("me:secret"), + response.body) +