Skip to content
Myr

Merge pull request #552 from slingamn/bundle_2

Before adoption: this commit came in with the history the project adopted, so no landing records it.

attempt to use the OS's certificate bundle

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 2f6b5fe, b4eb866 · Browse files · 4 files+67 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

4 files+67 added−5 removed

  • tests:+31 added−0 removed
  • other:+36 added−5 removed

4 files+67 added−5 removed

modified:requests/models.py+8 added-5 removed
25 unmodified lines
26 ConnectionError, HTTPError, RequestException, Timeout, TooManyRedirects,27 URLRequired, SSLError, MissingSchema, InvalidSchema, InvalidURL)28from .utils import (29 get_encoding_from_headers, stream_untransfer, guess_filename, requote_uri,30 dict_from_string, stream_decode_response_unicode, get_netrc_auth)30 dict_from_string, stream_decode_response_unicode, get_netrc_auth,31 DEFAULT_CA_BUNDLE_PATH)32from .compat import (33 urlparse, urlunparse, urljoin, urlsplit, urlencode, str, bytes,34 SimpleCookie, is_py2)35
488 unmodified lines
524 else:525 conn = connectionpool.connection_from_url(url)526 except LocationParseError as e:527 raise InvalidURL(e)527 528529 if url.startswith('https') and self.verify:530531 cert_loc = None532
4 unmodified lines
537 # Look for configuration.538 if not cert_loc and self.config.get('trust_env'):539 cert_loc = os.environ.get('REQUESTS_CA_BUNDLE')540540 # Curl compatiblity.541 # Curl compatibility.542 if not cert_loc and self.config.get('trust_env'):543 cert_loc = os.environ.get('CURL_CA_BUNDLE')544544 # Use the awesome certifi list.545 if not cert_loc:546 cert_loc = __import__('certifi').where()546 cert_loc = DEFAULT_CA_BUNDLE_PATH547548 if not cert_loc:549 raise Exception("Could not find a suitable SSL CA certificate bundle.")550551 conn.cert_reqs = 'CERT_REQUIRED'552 conn.ca_certs = cert_loc553 else:554 conn.cert_reqs = 'CERT_NONE'
290 unmodified lines
modified:requests/utils.py+27 added
19 unmodified lines
20from .compat import parse_http_list as _parse_list_header21from .compat import quote, cookielib, SimpleCookie, is_py2, urlparse22from .compat import basestring, bytes, str2324CERTIFI_BUNDLE_PATH = None25try:26 # see if requests's own CA certificate bundle is installed27 import certifi28 CERTIFI_BUNDLE_PATH = certifi.where()29except ImportError:30 pass3132NETRC_FILES = ('.netrc', '_netrc')3334# common paths for the OS's CA certificate bundle35POSSIBLE_CA_BUNDLE_PATHS = [36 # Red Hat, CentOS, Fedora and friends (provided by the ca-certificates package):37 '/etc/pki/tls/certs/ca-bundle.crt',38 # Ubuntu, Debian, and friends (provided by the ca-certificates package):39 '/etc/ssl/certs/ca-certificates.crt',40 # FreeBSD (provided by the ca_root_nss package):41 '/usr/local/share/certs/ca-root-nss.crt',42]4344def get_os_ca_bundle_path():45 """Try to pick an available CA certificate bundle provided by the OS."""46 for path in POSSIBLE_CA_BUNDLE_PATHS:47 if os.path.exists(path):48 return path49 return None5051# if certifi is installed, use its CA bundle;52# otherwise, try and use the OS bundle53DEFAULT_CA_BUNDLE_PATH = CERTIFI_BUNDLE_PATH or get_os_ca_bundle_path()5455def dict_to_sequence(d):56 """Returns an internal sequence dictionary update."""5758 if hasattr(d, 'items'):
443 unmodified lines
added:tests/test_requests_https.py+31 added
1#!/usr/bin/env python2# -*- coding: utf-8 -*-34import sys, os5import json6import unittest78# Path hack.9sys.path.insert(0, os.path.abspath('..'))10import requests1112class HTTPSTest(unittest.TestCase):13 """Smoke test for https functionality."""1415 smoke_url = "https://github.com"1617 def perform_smoke_test(self, verify=False):18 result = requests.get(self.smoke_url, verify=verify)19 self.assertEqual(result.status_code, 200)2021 def test_smoke(self):22 """Smoke test without verification."""23 self.perform_smoke_test(verify=False)2425 def test_smoke_verified(self):26 """Smoke test with SSL verification."""27 self.perform_smoke_test(verify=True)282930if __name__ == '__main__':31 unittest.main()
modified:AUTHORS.rst+1 added
93 unmodified lines
94- Steve Pulec95- Michael Kelly96- Michael Newman <newmaniese@gmail.com>97- Jonty Wareing <jonty@jonty.co.uk>98- Shivaram Lingamneni