From 4c4efff56ad9b0df356fafa3267c2f39135f17c8 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Sat, 23 May 2020 20:19:16 +0200 Subject: [PATCH] Pass cert_reqs to context --- httpie/client.py | 15 ++++++++++----- httpie/ssl.py | 10 +++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/httpie/client.py b/httpie/client.py index 991d5755..cd1ec417 100644 --- a/httpie/client.py +++ b/httpie/client.py @@ -14,19 +14,20 @@ from httpie import __version__ from httpie.cli.dicts import RequestHeadersDict from httpie.plugins import plugin_manager from httpie.sessions import get_httpie_session -from httpie.ssl import HTTPieHTTPSAdapter, AVAILABLE_SSL_VERSION_ARG_MAPPING +from httpie.ssl import AVAILABLE_SSL_VERSION_ARG_MAPPING, HTTPieHTTPSAdapter from httpie.utils import repr_dict try: # noinspection PyPackageRequirements import urllib3 + + # urllib3.disable_warnings() except (ImportError, AttributeError): pass - FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded; charset=utf-8' JSON_CONTENT_TYPE = 'application/json' JSON_ACCEPT = f'{JSON_CONTENT_TYPE}, */*;q=0.5' @@ -57,6 +58,7 @@ def collect_messages( requests_session = build_requests_session( ssl_version=args.ssl_version, ciphers=args.ciphers, + verify=bool(send_kwargs_mergeable_from_env['verify']) ) if httpie_session: @@ -147,19 +149,22 @@ def compress_body(request: requests.PreparedRequest, always: bool): def build_requests_session( + verify: bool, ssl_version: str = None, ciphers: str = None, ) -> requests.Session: requests_session = requests.Session() # Install our adapter. - requests_session.mount('https://', HTTPieHTTPSAdapter( + https_adapter = HTTPieHTTPSAdapter( ciphers=ciphers, + verify=verify, ssl_version=( AVAILABLE_SSL_VERSION_ARG_MAPPING[ssl_version] if ssl_version else None - ) - )) + ), + ) + requests_session.mount('https://', https_adapter) # Install adapters from plugins. for plugin_cls in plugin_manager.get_transport_plugins(): diff --git a/httpie/ssl.py b/httpie/ssl.py index 53e38782..3390bb60 100644 --- a/httpie/ssl.py +++ b/httpie/ssl.py @@ -25,11 +25,19 @@ AVAILABLE_SSL_VERSION_ARG_MAPPING = { class HTTPieHTTPSAdapter(HTTPAdapter): - def __init__(self, ssl_version: str = None, ciphers: str = None, **kwargs): + def __init__( + self, + verify: bool, + ssl_version: str = None, + ciphers: str = None, + **kwargs + ): self._ssl_context = create_urllib3_context( ciphers=ciphers, ssl_version=resolve_ssl_version(ssl_version), + cert_reqs=ssl.CERT_REQUIRED if verify else ssl.CERT_NONE ) + super().__init__(**kwargs) def init_poolmanager(self, *args, **kwargs):