Skip to content

Commit

Permalink
Make sure proxies have their scheme attached.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasa committed Dec 27, 2012
1 parent 594716a commit 641f461
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion requests/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from .packages.urllib3.poolmanager import PoolManager, proxy_from_url
from .hooks import dispatch_hook
from .compat import urlparse, basestring, urldefrag
from .utils import DEFAULT_CA_BUNDLE_PATH, get_encoding_from_headers
from .utils import (DEFAULT_CA_BUNDLE_PATH, get_encoding_from_headers,
prepend_scheme_if_needed)
from .structures import CaseInsensitiveDict
from .packages.urllib3.exceptions import MaxRetryError
from .packages.urllib3.exceptions import TimeoutError
Expand Down Expand Up @@ -116,6 +117,8 @@ def get_connection(self, url, proxies=None):
proxy = proxies.get(urlparse(url).scheme)

if proxy:
proxy = prepend_scheme_if_needed(proxy, urlparse(url).scheme)
print proxy
conn = proxy_from_url(proxy)
else:
conn = self.poolmanager.connection_from_url(url)
Expand Down
16 changes: 15 additions & 1 deletion requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from . import __version__
from . import certs
from .compat import parse_http_list as _parse_list_header
from .compat import quote, urlparse, bytes, str, OrderedDict
from .compat import quote, urlparse, bytes, str, OrderedDict, urlunparse
from .cookies import RequestsCookieJar, cookiejar_from_dict

_hush_pyflakes = (RequestsCookieJar,)
Expand Down Expand Up @@ -561,3 +561,17 @@ def guess_json_utf(data):
return 'utf-32-le'
# Did not detect a valid UTF-32 ascii-range character
return None


def prepend_scheme_if_needed(url, new_scheme):
'''Given a URL that may or may not have a scheme, prepend the given scheme.
Does not replace a present scheme with the one provided as an argument.'''
scheme, netloc, path, params, query, fragment = urlparse(url, new_scheme)

# urlparse is a finicky beast, and sometimes decides that there isn't a
# netloc present. Assume that it's being over-cautious, and switch netloc
# and path if urlparse decided there was no netloc.
if not netloc:
netloc, path = path, netloc

return urlunparse((scheme, netloc, path, params, query, fragment))

0 comments on commit 641f461

Please sign in to comment.