]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add support for username/password in url for simple_httpclient.
authorBen Darnell <ben@bendarnell.com>
Mon, 28 Mar 2011 03:30:42 +0000 (20:30 -0700)
committerBen Darnell <ben@bendarnell.com>
Mon, 28 Mar 2011 03:30:42 +0000 (20:30 -0700)
I considered making this functionality opt-in, but decided to allow it
for consistency with curl.

Closes #230.

tornado/simple_httpclient.py
tornado/test/simple_httpclient_test.py

index 38b5bc318812ee074a2dbcb6e6c500854bb9a7f4..c34f75589400cda2f36239da549115a81d7f552a 100644 (file)
@@ -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:
index 6db079c3eb79c111617ee8a72a070dfebfe15dc6..cb4c695af9d929614820156cb1ae306a42691528 100644 (file)
@@ -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)
+