~ubuntu-branches/ubuntu/utopic/requests/utopic

« back to all changes in this revision

Viewing changes to requests/adapters.py

  • Committer: Package Import Robot
  • Author(s): Daniele Tricoli
  • Date: 2013-10-18 19:20:21 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20131018192021-hzbd8k13wgx2z8am
Tags: 2.0.0-1
* New upstream release (Closes: #725784)
* Switched to pybuild
* debian/clean
  - Switched to debian/clean for cleaning instead of using debian/rules
* debian/control
  - Bumped python(3)-urllib3 to (>=1.7.1)
* debian/copyright
  - Updated copyright year
* debian/patches/02_use-system-chardet-and-urllib3.patches
  - Refreshed
* debian/watch
  - Switched download URL to https

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
import socket
12
12
 
13
13
from .models import Response
14
 
from urllib3.poolmanager import PoolManager, ProxyManager
 
14
from urllib3.poolmanager import PoolManager, proxy_from_url
15
15
from urllib3.response import HTTPResponse
 
16
from urllib3.util import Timeout as TimeoutSauce
16
17
from .compat import urlparse, basestring, urldefrag, unquote
17
18
from .utils import (DEFAULT_CA_BUNDLE_PATH, get_encoding_from_headers,
18
 
                    prepend_scheme_if_needed, get_auth_from_url)
 
19
                    except_on_missing_scheme, get_auth_from_url)
19
20
from .structures import CaseInsensitiveDict
20
21
from urllib3.exceptions import MaxRetryError
21
22
from urllib3.exceptions import TimeoutError
71
72
                 pool_block=DEFAULT_POOLBLOCK):
72
73
        self.max_retries = max_retries
73
74
        self.config = {}
 
75
        self.proxy_manager = {}
74
76
 
75
77
        super(HTTPAdapter, self).__init__()
76
78
 
118
120
        :param verify: Whether we should actually verify the certificate.
119
121
        :param cert: The SSL certificate to verify.
120
122
        """
121
 
        if url.startswith('https') and verify:
 
123
        if url.lower().startswith('https') and verify:
122
124
 
123
125
            cert_loc = None
124
126
 
184
186
    def get_connection(self, url, proxies=None):
185
187
        """Returns a urllib3 connection for the given URL. This should not be
186
188
        called from user code, and is only exposed for use when subclassing the
187
 
        :class:`HTTPAdapter <reqeusts.adapters.HTTPAdapter>`.
 
189
        :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
188
190
 
189
191
        :param url: The URL to connect to.
190
192
        :param proxies: (optional) A Requests-style dictionary of proxies used on this request.
191
193
        """
192
194
        proxies = proxies or {}
193
 
        proxy = proxies.get(urlparse(url).scheme)
 
195
        proxy = proxies.get(urlparse(url.lower()).scheme)
194
196
 
195
197
        if proxy:
196
 
            proxy = prepend_scheme_if_needed(proxy, urlparse(url).scheme)
197
 
            conn = ProxyManager(self.poolmanager.connection_from_url(proxy))
 
198
            except_on_missing_scheme(proxy)
 
199
            proxy_headers = self.proxy_headers(proxy)
 
200
 
 
201
            if not proxy in self.proxy_manager:
 
202
                self.proxy_manager[proxy] = proxy_from_url(
 
203
                                                proxy,
 
204
                                                proxy_headers=proxy_headers)
 
205
 
 
206
            conn = self.proxy_manager[proxy].connection_from_url(url)
198
207
        else:
199
 
            conn = self.poolmanager.connection_from_url(url)
 
208
            conn = self.poolmanager.connection_from_url(url.lower())
200
209
 
201
210
        return conn
202
211
 
214
223
        If the message is being sent through a proxy, the full URL has to be
215
224
        used. Otherwise, we should only use the path portion of the URL.
216
225
 
217
 
        This shoudl not be called from user code, and is only exposed for use
 
226
        This should not be called from user code, and is only exposed for use
218
227
        when subclassing the
219
228
        :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
220
229
 
232
241
        return url
233
242
 
234
243
    def add_headers(self, request, **kwargs):
235
 
        """Add any headers needed by the connection. Currently this adds a
236
 
        Proxy-Authorization header.
 
244
        """Add any headers needed by the connection. As of v2.0 this does
 
245
        nothing by default, but is left for overriding by users that subclass
 
246
        the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
237
247
 
238
248
        This should not be called from user code, and is only exposed for use
239
249
        when subclassing the
242
252
        :param request: The :class:`PreparedRequest <PreparedRequest>` to add headers to.
243
253
        :param kwargs: The keyword arguments from the call to send().
244
254
        """
245
 
        proxies = kwargs.get('proxies', {})
246
 
 
247
 
        if proxies is None:
248
 
            proxies = {}
249
 
 
250
 
        proxy = proxies.get(urlparse(request.url).scheme)
 
255
        pass
 
256
 
 
257
    def proxy_headers(self, proxy):
 
258
        """Returns a dictionary of the headers to add to any request sent
 
259
        through a proxy. This works with urllib3 magic to ensure that they are
 
260
        correctly sent to the proxy, rather than in a tunnelled request if
 
261
        CONNECT is being used.
 
262
 
 
263
        This should not be called from user code, and is only exposed for use
 
264
        when subclassing the
 
265
        :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
 
266
 
 
267
        :param proxies: The url of the proxy being used for this request.
 
268
        :param kwargs: Optional additional keyword arguments.
 
269
        """
 
270
        headers = {}
251
271
        username, password = get_auth_from_url(proxy)
252
272
 
253
273
        if username and password:
255
275
            # to decode them.
256
276
            username = unquote(username)
257
277
            password = unquote(password)
258
 
            request.headers['Proxy-Authorization'] = _basic_auth_str(username,
259
 
                                                                     password)
 
278
            headers['Proxy-Authorization'] = _basic_auth_str(username,
 
279
                                                             password)
 
280
 
 
281
        return headers
260
282
 
261
283
    def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None):
262
284
        """Sends PreparedRequest object. Returns Response object.
273
295
 
274
296
        self.cert_verify(conn, request.url, verify, cert)
275
297
        url = self.request_url(request, proxies)
276
 
        self.add_headers(request, proxies=proxies)
 
298
        self.add_headers(request)
277
299
 
278
300
        chunked = not (request.body is None or 'Content-Length' in request.headers)
279
301
 
 
302
        if stream:
 
303
            timeout = TimeoutSauce(connect=timeout)
 
304
        else:
 
305
            timeout = TimeoutSauce(connect=timeout, read=timeout)
 
306
 
280
307
        try:
281
308
            if not chunked:
282
309
                resp = conn.urlopen(