Skip to content

Commit

Permalink
update urllib3
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Reitz committed Jun 25, 2012
1 parent 64da351 commit 8f0ac66
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
20 changes: 18 additions & 2 deletions requests/packages/urllib3/__init__.py
Expand Up @@ -28,7 +28,7 @@

# Set default logging handler to avoid "No handler found" warnings.
import logging
try:
try: # Python 2.7+
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
Expand All @@ -37,6 +37,22 @@ def emit(self, record):

logging.getLogger(__name__).addHandler(NullHandler())

def add_stderr_logger(level=logging.DEBUG):
"""
Helper for quickly adding a StreamHandler to the logger. Useful for
debugging.
Returns the handler after adding it.
"""
# This method needs to be in this __init__.py to get the __name__ correct
# even if urllib3 is vendored within another package.
logger = logging.getLogger(__name__)
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
logger.addHandler(handler)
logger.setLevel(level)
logger.debug('Added an stderr logging handler to logger: %s' % __name__)
return handler

# ... Clean up.
del logging
del NullHandler
6 changes: 3 additions & 3 deletions requests/packages/urllib3/connectionpool.py
Expand Up @@ -264,7 +264,7 @@ def _make_request(self, conn, method, url, timeout=_Default,
httplib_response = conn.getresponse()

# AppEngine doesn't have a version attr.
http_version = getattr(conn, '_http_vsn_str', 'HTTP/?'),
http_version = getattr(conn, '_http_vsn_str', 'HTTP/?')
log.debug("\"%s %s %s\" %s %s" % (method, url, http_version,
httplib_response.status,
httplib_response.length))
Expand Down Expand Up @@ -324,8 +324,8 @@ def urlopen(self, method, url, body=None, headers=None, retries=3,
Number of retries to allow before raising a MaxRetryError exception.
:param redirect:
Automatically handle redirects (status codes 301, 302, 303, 307),
each redirect counts as a retry.
If True, automatically handle redirects (status codes 301, 302,
303, 307). Each redirect counts as a retry.
:param assert_same_host:
If ``True``, will make sure that the host of the pool requests is
Expand Down
29 changes: 23 additions & 6 deletions requests/packages/urllib3/util.py
Expand Up @@ -110,30 +110,47 @@ def get_host(url):
('http', 'google.com', 80)
"""

# This code is actually similar to urlparse.urlsplit, but much
# simplified for our needs.
port = None
# While this code has overlap with stdlib's urlparse, it is much
# simplified for our needs and less annoying.
# Additionally, this imeplementations does silly things to be optimal
# on CPython.

scheme = 'http'
host = None
port = None

# Scheme
if '://' in url:
scheme, url = url.split('://', 1)

# Find the earliest Authority Terminator
# http://tools.ietf.org/html/rfc3986#section-3.2
# (http://tools.ietf.org/html/rfc3986#section-3.2)
url, _path = split_first(url, ['/', '?', '#'])

# Auth
if '@' in url:
_auth, url = url.split('@', 1)

# IPv6
if url and url[0] == '[':
host, url = url[1:].split(']', 1)

# Port
if ':' in url:
url, port = url.split(':', 1)
_host, port = url.split(':', 1)

if not host:
host = _host

if not port.isdigit():
raise LocationParseError("Failed to parse: %s" % url)

port = int(port)

return scheme, url, port
elif not host:
host = url

return scheme, host, port


def is_connection_dropped(conn):
Expand Down

0 comments on commit 8f0ac66

Please sign in to comment.