Add proxy_headers functionality.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This brings us in line with urllib3's fancy new magic for sending headers to HTTP(S) proxies.
authored by Cory Benfield · committed by Cory Benfield · parent 09d97c1 · Browse files · 1 file·+27 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+27 added−10 removed
- other:+27 added−10 removed
Provenance
- Commit
2e5a674- Tree
d64ff6d- Author
Cory Benfield <lukasaoz@gmail.com>- Committer
Cory Benfield <lukasaoz@gmail.com>- Parent
09d97c1
1 file changed+27 added−10 removed
modified:requests/adapters.py+27 added-10 removed
193 unmodified lines194 proxy = proxies.get(urlparse(url.lower()).scheme)195 196 if proxy:197 except_on_missing_scheme(proxy)198+ proxy_headers = self.proxy_headers(proxy)199+200 if not proxy in self.proxy_manager:199- self.proxy_manager[proxy] = proxy_from_url(proxy)201+ self.proxy_manager[proxy] = proxy_from_url(202+ proxy,203+ proxy_headers=proxy_headers)204 205 conn = self.proxy_manager[proxy].connection_from_url(url)206 else:207 conn = self.poolmanager.connection_from_url(url.lower())31 unmodified lines239 240 return url241 242 def add_headers(self, request, **kwargs):239- """Add any headers needed by the connection. Currently this adds a240- Proxy-Authorization header.243+ """Add any headers needed by the connection. As of v2.0 this does244+ nothing by default, but is left for overriding by users that subclass245+ the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.246 247 This should not be called from user code, and is only exposed for use248 when subclassing the249 :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.250 251 :param request: The :class:`PreparedRequest <PreparedRequest>` to add headers to.252 :param kwargs: The keyword arguments from the call to send().253 """249- proxies = kwargs.get('proxies', {})254+ pass255 251- if proxies is None:252- proxies = {}256+ def proxy_headers(self, proxy):257+ """Returns a dictionary of the headers to add to any request sent258+ through a proxy. This works with urllib3 magic to ensure that they are259+ correctly sent to the proxy, rather than in a tunnelled request if260+ CONNECT is being used.261 254- proxy = proxies.get(urlparse(request.url).scheme)262+ This should not be called from user code, and is only exposed for use263+ when subclassing the264+ :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.265+266+ :param proxies: The url of the proxy being used for this request.267+ :param kwargs: Optional additional keyword arguments.268+ """269+ headers = {}270 username, password = get_auth_from_url(proxy)271 272 if username and password:273 # Proxy auth usernames and passwords will be urlencoded, we need274 # to decode them.275 username = unquote(username)276 password = unquote(password)262- request.headers['Proxy-Authorization'] = _basic_auth_str(username,263- password)277+ headers['Proxy-Authorization'] = _basic_auth_str(username,278+ password)279 280+ return headers281+282 def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None):283 """Sends PreparedRequest object. Returns Response object.284 285 :param request: The :class:`PreparedRequest <PreparedRequest>` being sent.7 unmodified lines293 conn = self.get_connection(request.url, proxies)294 295 self.cert_verify(conn, request.url, verify, cert)296 url = self.request_url(request, proxies)280- self.add_headers(request, proxies=proxies)297+ self.add_headers(request)298 299 chunked = not (request.body is None or 'Content-Length' in request.headers)300 301 try:62 unmodified lines