urllib3 update
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fixes #664
authored by Kenneth Reitz · committed by Kenneth Reitz · parent 6b1e81c · Browse files · 4 files·+45 added−57 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+45 added−57 removed
- other:+45 added−57 removed
Provenance
- Commit
d3acb78- Tree
bce8e60- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
6b1e81c
4 files changed+45 added−57 removed
deleted:requests/packages/urllib3/packages/mimetools_choose_boundary/__init__.py-47 removed
1-"""The function mimetools.choose_boundary() from Python 2.7, which seems to2-have disappeared in Python 3 (although email.generator._make_boundary() might3-work as a replacement?).4-5-Tweaked to use lock from threading rather than thread.6-"""7-import os8-from threading import Lock9-_counter_lock = Lock()10-11-_counter = 012-def _get_next_counter():13- global _counter14- with _counter_lock:15- _counter += 116- return _counter17-18-_prefix = None19-20-def choose_boundary():21- """Return a string usable as a multipart boundary.22-23- The string chosen is unique within a single program run, and24- incorporates the user id (if available), process id (if available),25- and current time. So it's very unlikely the returned string appears26- in message text, but there's no guarantee.27-28- The boundary contains dots so you have to quote it in the header."""29-30- global _prefix31- import time32- if _prefix is None:33- import socket34- try:35- hostid = socket.gethostbyname(socket.gethostname())36- except socket.gaierror:37- hostid = '127.0.0.1'38- try:39- uid = repr(os.getuid())40- except AttributeError:41- uid = '1'42- try:43- pid = repr(os.getpid())44- except AttributeError:45- pid = '1'46- _prefix = hostid + '.' + uid + '.' + pid47- return "%s.%.3f.%d" % (_prefix, time.time(), _get_next_counter())modified:requests/packages/urllib3/connectionpool.py+4 added-1 removed
256 unmodified lines257 sock = getattr(conn, 'sock', False) # AppEngine doesn't have sock attr.258 if sock:259 sock.settimeout(timeout)260 261+ try: # Python 2.7+, use buffering of HTTP responses262+ httplib_response = conn.getresponse(buffering=True)263+ except TypeError: # Python 2.6 and older261- httplib_response = conn.getresponse()264+ httplib_response = conn.getresponse()265 266 # AppEngine doesn't have a version attr.267 http_version = getattr(conn, '_http_vsn_str', 'HTTP/?'),268 log.debug("\"%s %s %s\" %s %s" % (method, url, http_version,257 unmodified linesmodified:requests/packages/urllib3/filepost.py+8 added-5 removed
5 unmodified lines6 7 import codecs8 import mimetypes9 10-try:11- from mimetools import choose_boundary12-except ImportError:13- from .packages.mimetools_choose_boundary import choose_boundary14-10+from uuid import uuid411 from io import BytesIO12 13 from .packages import six14 from .packages.six import b15 16 writer = codecs.lookup('utf-8')[3]17 18 19+def choose_boundary():20+ """21+ Our embarassingly-simple replacement for mimetools.choose_boundary.22+ """23+ return uuid4().hex24+25+26 def get_content_type(filename):27 return mimetypes.guess_type(filename)[0] or 'application/octet-stream'28 29 62 unmodified linesmodified:requests/packages/urllib3/util.py+33 added-4 removed
70 unmodified lines71 72 return headers73 74 75+def split_first(s, delims):76+ """77+ Given a string and an iterable of delimiters, split on the first found78+ delimiter. Return two split parts.79+80+ If not found, then the first part is the full input string.81+82+ Scales linearly with number of delims. Not ideal for large number of delims.83+ """84+ min_idx = None85+ for d in delims:86+ idx = s.find(d)87+ if idx < 0:88+ continue89+90+ if not min_idx:91+ min_idx = idx92+ else:93+ min_idx = min(idx, min_idx)94+95+ if min_idx < 0:96+ return s, ''97+98+ return s[:min_idx], s[min_idx+1:]99+100+101 def get_host(url):102 """103 Given a url, return its scheme, host and port (None if it's not there).104 11 unmodified lines116 scheme = 'http'117 118 if '://' in url:119 scheme, url = url.split('://', 1)94- if '/' in url:95- url, _path = url.split('/', 1)120+121+ # Find the earliest Authority Terminator122+ # http://tools.ietf.org/html/rfc3986#section-3.2123+ url, _path = split_first(url, ['/', '?', '#'])124+125 if '@' in url:126 _auth, url = url.split('@', 1)127 if ':' in url:128 url, port = url.split(':', 1)17 unmodified lines146 Note: For platforms like AppEngine, this will always return ``False`` to147 let the platform handle connection recycling transparently for us.148 """149 sock = getattr(conn, 'sock', False)121- if not sock: #Platform-specific: AppEngine150+ if not sock: # Platform-specific: AppEngine151 return False152 153 if not poll: # Platform-specific125- if not select: #Platform-specific: AppEngine154+ if not select: # Platform-specific: AppEngine155 return False156 157 return select([sock], [], [], 0.0)[0]158 7 unmodified lines