Skip to content
Myr

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

1 file+27 added−10 removed

modified:requests/adapters.py+27 added-10 removed
193 unmodified lines
194 proxy = proxies.get(urlparse(url.lower()).scheme)195196 if proxy:197 except_on_missing_scheme(proxy)198 proxy_headers = self.proxy_headers(proxy)199200 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)204205 conn = self.proxy_manager[proxy].connection_from_url(url)206 else:207 conn = self.poolmanager.connection_from_url(url.lower())
31 unmodified lines
239240 return url241242 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>`.246247 This should not be called from user code, and is only exposed for use248 when subclassing the249 :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.250251 :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 pass255251 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.261254 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>`.265266 :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)271272 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)279280 return headers281282 def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None):283 """Sends PreparedRequest object. Returns Response object.284285 :param request: The :class:`PreparedRequest <PreparedRequest>` being sent.
7 unmodified lines
293 conn = self.get_connection(request.url, proxies)294295 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)298299 chunked = not (request.body is None or 'Content-Length' in request.headers)300301 try:
62 unmodified lines