Show More
@@ -41,18 +41,29 b' from __future__ import absolute_import' | |||||
41 | # Many functions in this file have too many arguments. |
|
41 | # Many functions in this file have too many arguments. | |
42 | # pylint: disable=R0913 |
|
42 | # pylint: disable=R0913 | |
43 |
|
43 | |||
44 | import cStringIO |
|
|||
45 | import errno |
|
44 | import errno | |
46 | import httplib |
|
45 | import inspect | |
47 | import logging |
|
46 | import logging | |
48 | import rfc822 |
|
47 | import rfc822 | |
49 | import select |
|
48 | import select | |
50 | import socket |
|
49 | import socket | |
51 |
|
50 | |||
|
51 | try: | |||
|
52 | import cStringIO as io | |||
|
53 | io.StringIO | |||
|
54 | except ImportError: | |||
|
55 | import io | |||
|
56 | ||||
|
57 | try: | |||
|
58 | import httplib | |||
|
59 | httplib.HTTPException | |||
|
60 | except ImportError: | |||
|
61 | import http.client as httplib | |||
|
62 | ||||
52 | from . import ( |
|
63 | from . import ( | |
53 | _readers, |
|
64 | _readers, | |
54 | socketutil, |
|
65 | socketutil, | |
55 | ) |
|
66 | ) | |
56 |
|
67 | |||
57 | logger = logging.getLogger(__name__) |
|
68 | logger = logging.getLogger(__name__) | |
58 |
|
69 | |||
@@ -242,7 +253,7 b' class HTTPResponse(object):' | |||||
242 | self.status = int(self.status) |
|
253 | self.status = int(self.status) | |
243 | if self._eol != EOL: |
|
254 | if self._eol != EOL: | |
244 | hdrs = hdrs.replace(self._eol, '\r\n') |
|
255 | hdrs = hdrs.replace(self._eol, '\r\n') | |
245 |
headers = rfc822.Message( |
|
256 | headers = rfc822.Message(io.StringIO(hdrs)) | |
246 | content_len = None |
|
257 | content_len = None | |
247 | if HDR_CONTENT_LENGTH in headers: |
|
258 | if HDR_CONTENT_LENGTH in headers: | |
248 | content_len = int(headers[HDR_CONTENT_LENGTH]) |
|
259 | content_len = int(headers[HDR_CONTENT_LENGTH]) | |
@@ -296,6 +307,46 b' def _foldheaders(headers):' | |||||
296 | """ |
|
307 | """ | |
297 | return dict((k.lower(), (k, v)) for k, v in headers.iteritems()) |
|
308 | return dict((k.lower(), (k, v)) for k, v in headers.iteritems()) | |
298 |
|
309 | |||
|
310 | try: | |||
|
311 | inspect.signature | |||
|
312 | def _handlesarg(func, arg): | |||
|
313 | """ Try to determine if func accepts arg | |||
|
314 | ||||
|
315 | If it takes arg, return True | |||
|
316 | If it happens to take **args, then it could do anything: | |||
|
317 | * It could throw a different TypeError, just for fun | |||
|
318 | * It could throw an ArgumentError or anything else | |||
|
319 | * It could choose not to throw an Exception at all | |||
|
320 | ... return 'unknown' | |||
|
321 | ||||
|
322 | Otherwise, return False | |||
|
323 | """ | |||
|
324 | params = inspect.signature(func).parameters | |||
|
325 | if arg in params: | |||
|
326 | return True | |||
|
327 | for p in params: | |||
|
328 | if params[p].kind == inspect._ParameterKind.VAR_KEYWORD: | |||
|
329 | return 'unknown' | |||
|
330 | return False | |||
|
331 | except AttributeError: | |||
|
332 | def _handlesarg(func, arg): | |||
|
333 | """ Try to determine if func accepts arg | |||
|
334 | ||||
|
335 | If it takes arg, return True | |||
|
336 | If it happens to take **args, then it could do anything: | |||
|
337 | * It could throw a different TypeError, just for fun | |||
|
338 | * It could throw an ArgumentError or anything else | |||
|
339 | * It could choose not to throw an Exception at all | |||
|
340 | ... return 'unknown' | |||
|
341 | ||||
|
342 | Otherwise, return False | |||
|
343 | """ | |||
|
344 | spec = inspect.getargspec(func) | |||
|
345 | if arg in spec.args: | |||
|
346 | return True | |||
|
347 | if spec.keywords: | |||
|
348 | return 'unknown' | |||
|
349 | return False | |||
299 |
|
350 | |||
300 | class HTTPConnection(object): |
|
351 | class HTTPConnection(object): | |
301 | """Connection to a single http server. |
|
352 | """Connection to a single http server. | |
@@ -346,9 +397,31 b' class HTTPConnection(object):' | |||||
346 | if '[' in host: |
|
397 | if '[' in host: | |
347 | host = host[1:-1] |
|
398 | host = host[1:-1] | |
348 | if ssl_wrap_socket is not None: |
|
399 | if ssl_wrap_socket is not None: | |
349 |
|
|
400 | _wrap_socket = ssl_wrap_socket | |
350 | else: |
|
401 | else: | |
351 |
|
|
402 | _wrap_socket = socketutil.wrap_socket | |
|
403 | call_wrap_socket = None | |||
|
404 | handlesubar = _handlesarg(_wrap_socket, 'server_hostname') | |||
|
405 | if handlesubar is True: | |||
|
406 | # supports server_hostname | |||
|
407 | call_wrap_socket = _wrap_socket | |||
|
408 | handlesnobar = _handlesarg(_wrap_socket, 'serverhostname') | |||
|
409 | if handlesnobar is True and handlesubar is not True: | |||
|
410 | # supports serverhostname | |||
|
411 | def call_wrap_socket(sock, server_hostname=None, **ssl_opts): | |||
|
412 | return _wrap_socket(sock, serverhostname=server_hostname, | |||
|
413 | **ssl_opts) | |||
|
414 | if handlesubar is False and handlesnobar is False: | |||
|
415 | # does not support either | |||
|
416 | def call_wrap_socket(sock, server_hostname=None, **ssl_opts): | |||
|
417 | return _wrap_socket(sock, **ssl_opts) | |||
|
418 | if call_wrap_socket is None: | |||
|
419 | # we assume it takes **args | |||
|
420 | def call_wrap_socket(sock, **ssl_opts): | |||
|
421 | if 'server_hostname' in ssl_opts: | |||
|
422 | ssl_opts['serverhostname'] = ssl_opts['server_hostname'] | |||
|
423 | return _wrap_socket(sock, **ssl_opts) | |||
|
424 | self._ssl_wrap_socket = call_wrap_socket | |||
352 | if use_ssl is None and port is None: |
|
425 | if use_ssl is None and port is None: | |
353 | use_ssl = False |
|
426 | use_ssl = False | |
354 | port = 80 |
|
427 | port = 80 | |
@@ -429,7 +502,8 b' class HTTPConnection(object):' | |||||
429 | sock.setblocking(1) |
|
502 | sock.setblocking(1) | |
430 | logger.debug('wrapping socket for ssl with options %r', |
|
503 | logger.debug('wrapping socket for ssl with options %r', | |
431 | self.ssl_opts) |
|
504 | self.ssl_opts) | |
432 |
sock = self._ssl_wrap_socket(sock, |
|
505 | sock = self._ssl_wrap_socket(sock, server_hostname=self.host, | |
|
506 | **self.ssl_opts) | |||
433 | if self._ssl_validator: |
|
507 | if self._ssl_validator: | |
434 | self._ssl_validator(sock) |
|
508 | self._ssl_validator(sock) | |
435 | sock.setblocking(0) |
|
509 | sock.setblocking(0) |
@@ -33,7 +33,12 b' have any clients outside of httpplus.' | |||||
33 | """ |
|
33 | """ | |
34 | from __future__ import absolute_import |
|
34 | from __future__ import absolute_import | |
35 |
|
35 | |||
36 | import httplib |
|
36 | try: | |
|
37 | import httplib | |||
|
38 | httplib.HTTPException | |||
|
39 | except ImportError: | |||
|
40 | import http.client as httplib | |||
|
41 | ||||
37 | import logging |
|
42 | import logging | |
38 |
|
43 | |||
39 | logger = logging.getLogger(__name__) |
|
44 | logger = logging.getLogger(__name__) |
@@ -122,7 +122,8 b' else:' | |||||
122 | server_side=False, cert_reqs=CERT_NONE, |
|
122 | server_side=False, cert_reqs=CERT_NONE, | |
123 | ssl_version=_PROTOCOL_SSLv23, ca_certs=None, |
|
123 | ssl_version=_PROTOCOL_SSLv23, ca_certs=None, | |
124 | do_handshake_on_connect=True, |
|
124 | do_handshake_on_connect=True, | |
125 |
suppress_ragged_eofs=True |
|
125 | suppress_ragged_eofs=True, | |
|
126 | server_hostname=None): | |||
126 | """Backport of ssl.wrap_socket from Python 2.6.""" |
|
127 | """Backport of ssl.wrap_socket from Python 2.6.""" | |
127 | if cert_reqs != CERT_NONE and ca_certs: |
|
128 | if cert_reqs != CERT_NONE and ca_certs: | |
128 | raise CertificateValidationUnsupported( |
|
129 | raise CertificateValidationUnsupported( |
General Comments 0
You need to be logged in to leave comments.
Login now