summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCory Benfield <lukasaoz@gmail.com>2013-07-28 07:16:06 +0100
committerCory Benfield <lukasaoz@gmail.com>2013-07-28 07:16:06 +0100
commit840540b6b1f07ef87faab73392c03fbef0dcc9fe (patch)
treeb2c9a519436b3f210c646b36306d0a28e2984c0f
parent9473f15909fb3f2329247812e0d3c661421ceafc (diff)
downloadpython-requests-840540b6b1f07ef87faab73392c03fbef0dcc9fe.tar.gz
Proxy urls should have explicit schemes.
-rw-r--r--requests/adapters.py4
-rw-r--r--requests/utils.py26
2 files changed, 13 insertions, 17 deletions
diff --git a/requests/adapters.py b/requests/adapters.py
index 7e65c78e..e6cb50ed 100644
--- a/requests/adapters.py
+++ b/requests/adapters.py
@@ -15,7 +15,7 @@ from .packages.urllib3.poolmanager import PoolManager, ProxyManager
from .packages.urllib3.response import HTTPResponse
from .compat import urlparse, basestring, urldefrag, unquote
from .utils import (DEFAULT_CA_BUNDLE_PATH, get_encoding_from_headers,
- prepend_scheme_if_needed, get_auth_from_url)
+ except_on_missing_scheme, get_auth_from_url)
from .structures import CaseInsensitiveDict
from .packages.urllib3.exceptions import MaxRetryError
from .packages.urllib3.exceptions import TimeoutError
@@ -193,7 +193,7 @@ class HTTPAdapter(BaseAdapter):
proxy = proxies.get(urlparse(url.lower()).scheme)
if proxy:
- proxy = prepend_scheme_if_needed(proxy, urlparse(url.lower()).scheme)
+ except_on_missing_scheme(proxy)
conn = ProxyManager(self.poolmanager.connection_from_url(proxy))
else:
conn = self.poolmanager.connection_from_url(url.lower())
diff --git a/requests/utils.py b/requests/utils.py
index 37aa19e4..618a5a19 100644
--- a/requests/utils.py
+++ b/requests/utils.py
@@ -25,6 +25,7 @@ from .compat import quote, urlparse, bytes, str, OrderedDict, urlunparse
from .compat import getproxies, proxy_bypass
from .cookies import RequestsCookieJar, cookiejar_from_dict
from .structures import CaseInsensitiveDict
+from .exceptions import MissingSchema
_hush_pyflakes = (RequestsCookieJar,)
@@ -393,18 +394,18 @@ def get_environ_proxies(url):
# we're getting isn't in the no_proxy list.
no_proxy = get_proxy('no_proxy')
netloc = urlparse(url).netloc
-
+
if no_proxy:
# We need to check whether we match here. We need to see if we match
# the end of the netloc, both with and without the port.
no_proxy = no_proxy.split(',')
-
+
for host in no_proxy:
if netloc.endswith(host) or netloc.split(':')[0].endswith(host):
# The URL does match something in no_proxy, so we don't want
# to apply the proxies on this URL.
return {}
-
+
# If the system proxy settings indicate that this URL should be bypassed,
# don't proxy.
if proxy_bypass(netloc):
@@ -414,7 +415,7 @@ def get_environ_proxies(url):
# anywhere that no_proxy applies to, and the system settings don't require
# bypassing the proxy for the current URL.
return getproxies()
-
+
def default_user_agent():
"""Return a string representing the default user agent."""
@@ -524,18 +525,13 @@ def guess_json_utf(data):
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
+def except_on_missing_scheme(url):
+ """Given a URL, raise a MissingSchema exception if the scheme is missing.
+ """
+ scheme, netloc, path, params, query, fragment = urlparse(url)
- return urlunparse((scheme, netloc, path, params, query, fragment))
+ if not scheme:
+ raise MissingSchema('Proxy URLs must have explicit schemes.')
def get_auth_from_url(url):