Merge pull request #4371 from Twistbioscience/zipfile_compat
Before adoption: this commit came in with the history the project adopted, so no landing records it.
support extraction of certificate bundle from a zip archive
authored by Cory Benfield · committed by GitHub · parents 81b6341, c86b09b · Browse files · 5 files·+71 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+71 added−5 removed
- tests:+29 added−1 removed
- other:+42 added−4 removed
Provenance
- Commit
234f80a- Tree
dd1b859- Author
Cory Benfield <lukasaoz@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
81b6341- Parent 2
c86b09b
5 files changed+71 added−5 removed
modified:requests/adapters.py+4 added-4 removed
26 unmodified lines27 from urllib3.exceptions import ResponseError28 29 from .models import Response30 from .compat import urlparse, basestring31-from .utils import (DEFAULT_CA_BUNDLE_PATH, get_encoding_from_headers,32- prepend_scheme_if_needed, get_auth_from_url, urldefragauth,33- select_proxy)31+from .utils import (DEFAULT_CA_BUNDLE_PATH, extract_zipped_paths,32+ get_encoding_from_headers, prepend_scheme_if_needed,33+ get_auth_from_url, urldefragauth, select_proxy)34 from .structures import CaseInsensitiveDict35 from .cookies import extract_cookies_to_jar36 from .exceptions import (ConnectionError, ConnectTimeout, ReadTimeout, SSLError,37 ProxyError, RetryError, InvalidSchema)180 unmodified lines218 if verify is not True:219 cert_loc = verify220 221 if not cert_loc:222- cert_loc = DEFAULT_CA_BUNDLE_PATH222+ cert_loc = extract_zipped_paths(DEFAULT_CA_BUNDLE_PATH)223 224 if not cert_loc or not os.path.exists(cert_loc):225 raise IOError("Could not find a suitable TLS CA certificate bundle, "226 "invalid path: {0}".format(cert_loc))299 unmodified linesmodified:requests/utils.py+34 added
16 unmodified lines17 import re18 import socket19 import struct20 import sys21+import tempfile22 import warnings23+import zipfile24 25 from .__version__ import __version__26 from . import certs27 # to_native_string is unused here, but imported here for backwards compatibility189 unmodified lines217 name[-1] != '>'):218 return os.path.basename(name)219 220 221+def extract_zipped_paths(path):222+ """Replace nonexistant paths that look like they refer to a member of a zip223+ archive with the location of an extracted copy of the target, or else224+ just return the provided path unchanged.225+ """226+ if os.path.exists(path):227+ # this is already a valid path, no need to do anything further228+ return path229+230+ # find the first valid part of the provided path and treat that as a zip archive231+ # assume the rest of the path is the name of a member in the archive232+ archive, member = os.path.split(path)233+ while archive and not os.path.exists(archive):234+ archive, prefix = os.path.split(archive)235+ member = '/'.join([prefix, member])236+237+ if not zipfile.is_zipfile(archive):238+ return path239+240+ zip_file = zipfile.ZipFile(archive)241+ if member not in zip_file.namelist():242+ return path243+244+ # we have a valid zip archive and a valid member of that archive245+ tmp = tempfile.gettempdir()246+ extracted_path = os.path.join(tmp, *member.split('/'))247+ if not os.path.exists(extracted_path):248+ extracted_path = zip_file.extract(member, path=tmp)249+250+ return extracted_path251+252+253 def from_key_val_list(value):254 """Take an object and test to see if it can be represented as a255 dictionary. Unless it can not be represented as such, return an256 OrderedDict, e.g.,686 unmodified linesmodified:tests/test_utils.py+29 added-1 removed
1 # -*- coding: utf-8 -*-2 3 import os4 import copy5+import filecmp6 from io import BytesIO7+import zipfile8 9 import pytest10 from requests import compat11 from requests.cookies import RequestsCookieJar12 from requests.structures import CaseInsensitiveDict13 from requests.utils import (12- address_in_network, dotted_netmask,14+ address_in_network, dotted_netmask, extract_zipped_paths,15 get_auth_from_url, get_encoding_from_headers,16 get_encodings_from_content, get_environ_proxies,17 guess_filename, guess_json_utf, is_ipv4_address,18 is_valid_cidr, iter_slices, parse_dict_header,238 unmodified lines257 assert result == value258 assert isinstance(result, expected_type)259 260 261+class TestExtractZippedPaths:262+263+ @pytest.mark.parametrize(264+ 'path', (265+ '/',266+ __file__,267+ pytest.__file__,268+ '/etc/invalid/location',269+ ))270+ def test_unzipped_paths_unchanged(self, path):271+ assert path == extract_zipped_paths(path)272+273+ def test_zipped_paths_extracted(self, tmpdir):274+ zipped_py = tmpdir.join('test.zip')275+ with zipfile.ZipFile(zipped_py.strpath, 'w') as f:276+ f.write(__file__)277+278+ _, name = os.path.splitdrive(__file__)279+ zipped_path = os.path.join(zipped_py.strpath, name.lstrip(r'\/'))280+ extracted_path = extract_zipped_paths(zipped_path)281+282+ assert extracted_path != zipped_path283+ assert os.path.exists(extracted_path)284+ assert filecmp.cmp(extracted_path, __file__)285+286+287 class TestContentEncodingDetection:288 289 def test_none(self):290 encodings = get_encodings_from_content('')422 unmodified linesmodified:AUTHORS.rst+1 added
177 unmodified lines178 - Ryan Pineo (`@ryanpineo <https://github.com/ryanpineo>`_)179 - Ed Morley (`@edmorley <https://github.com/edmorley>`_)180 - Matt Liu <liumatt@gmail.com> (`@mlcrazy <https://github.com/mlcrazy>`_)181 - Taylor Hoff <primdevs@protonmail.com> (`@PrimordialHelios <https://github.com/PrimordialHelios>`_)182+- Arthur Vigil (`@ahvigil <https://github.com/ahvigil>`_)modified:HISTORY.rst+3 added
11 unmodified lines12 13 **Bugfixes**14 15 - Parsing empty ``Link`` headers with ``parse_header_links()`` no longer return one bogus entry16+- Fixed issue where loading the default certificate bundle from a zip archive17+ would raise an ``IOError``18 19+20 2.18.4 (2017-08-15)21 +++++++++++++++++++22 23 **Improvements**1518 unmodified lines