summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrey Petrov <andrey.petrov@shazow.net>2012-02-05 11:39:40 -0800
committerAndrey Petrov <andrey.petrov@shazow.net>2012-02-05 11:39:40 -0800
commit247b68b6610dd4e38e35ba3d5ada19a40c706d6b (patch)
treebadbaa7584aaa30ee23ffd53b6fd0a5242c101ac
parent7853d47093af2a12033d57c302179a2a165c08e7 (diff)
downloadurllib3-247b68b6610dd4e38e35ba3d5ada19a40c706d6b.tar.gz
raise LocationParseError instead of ValueError. (#39)
-rw-r--r--test/test_connectionpool.py12
-rw-r--r--urllib3/connectionpool.py15
-rw-r--r--urllib3/exceptions.py21
3 files changed, 40 insertions, 8 deletions
diff --git a/test/test_connectionpool.py b/test/test_connectionpool.py
index ff7a703e..4281d426 100644
--- a/test/test_connectionpool.py
+++ b/test/test_connectionpool.py
@@ -6,7 +6,7 @@ from urllib3.connectionpool import (
HTTPConnectionPool,
make_headers)
-from urllib3.exceptions import EmptyPoolError
+from urllib3.exceptions import EmptyPoolError, LocationParseError
class TestConnectionPool(unittest.TestCase):
@@ -54,6 +54,16 @@ class TestConnectionPool(unittest.TestCase):
c = connection_from_url(a)
self.assertFalse(c.is_same_host(b), "%s =? %s" % (a, b))
+ def test_invalid_host(self):
+ # TODO: Add more tests
+ invalid_host = [
+ 'http://google.com:foo',
+ ]
+
+ for location in invalid_host:
+ self.assertRaises(LocationParseError, get_host, location)
+
+
def test_make_headers(self):
self.assertEqual(
make_headers(accept_encoding=True),
diff --git a/urllib3/connectionpool.py b/urllib3/connectionpool.py
index 82fb944e..39e652ed 100644
--- a/urllib3/connectionpool.py
+++ b/urllib3/connectionpool.py
@@ -49,11 +49,13 @@ except ImportError:
from .packages.ssl_match_hostname import match_hostname, CertificateError
from .request import RequestMethods
from .response import HTTPResponse
-from .exceptions import (SSLError,
+from .exceptions import (
+ EmptyPoolError,
+ HostChangedError,
+ LocationParseError,
MaxRetryError,
+ SSLError,
TimeoutError,
- HostChangedError,
- EmptyPoolError,
)
from .packages.ssl_match_hostname import match_hostname, CertificateError
@@ -558,10 +560,12 @@ def get_host(url):
>>> get_host('google.com:80')
('http', 'google.com', 80)
"""
+
# This code is actually similar to urlparse.urlsplit, but much
# simplified for our needs.
port = None
scheme = 'http'
+
if '://' in url:
scheme, url = url.split('://', 1)
if '/' in url:
@@ -570,7 +574,12 @@ def get_host(url):
_auth, url = url.split('@', 1)
if ':' in url:
url, port = url.split(':', 1)
+
+ if not port.isdigit():
+ raise LocationParseError("Failed to parse: %s")
+
port = int(port)
+
return scheme, url, port
diff --git a/urllib3/exceptions.py b/urllib3/exceptions.py
index 0bffeb47..257ff83e 100644
--- a/urllib3/exceptions.py
+++ b/urllib3/exceptions.py
@@ -4,6 +4,7 @@
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
+
## Base Exceptions
class HTTPError(Exception):
@@ -27,18 +28,20 @@ class SSLError(HTTPError):
class MaxRetryError(PoolError):
"Raised when the maximum number of retries is exceeded."
+
def __init__(self, pool, url):
- PoolError.__init__(self, pool,
- "Max retries exceeded with url: %s" % url)
+ message = "Max retries exceeded with url: %s" % url
+ PoolError.__init__(self, pool, message)
self.url = url
class HostChangedError(PoolError):
"Raised when an existing pool gets a request for a foreign host."
+
def __init__(self, pool, url, retries=3):
- PoolError.__init__(self, pool,
- "Tried to open a foreign host with url: %s" % url)
+ message = "Tried to open a foreign host with url: %s" % url
+ PoolError.__init__(self, pool, message)
self.url = url
self.retries = retries
@@ -52,3 +55,13 @@ class TimeoutError(PoolError):
class EmptyPoolError(PoolError):
"Raised when a pool runs out of connections and no more are allowed."
pass
+
+
+class LocationParseError(HTTPError, ValueError):
+ "Raised when get_host or similar fails to parse the URL input."
+
+ def __init__(self, location):
+ message = "Failed to parse: %s" % location
+ super(LocationParseError, self).__init__(self, message)
+
+ self.location = location