Pass cert_reqs to context

This commit is contained in:
Jakub Roztocil 2020-05-23 20:19:16 +02:00
parent a53505f26e
commit 4c4efff56a
2 changed files with 19 additions and 6 deletions

View File

@ -14,19 +14,20 @@ from httpie import __version__
from httpie.cli.dicts import RequestHeadersDict from httpie.cli.dicts import RequestHeadersDict
from httpie.plugins import plugin_manager from httpie.plugins import plugin_manager
from httpie.sessions import get_httpie_session 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 from httpie.utils import repr_dict
try: try:
# noinspection PyPackageRequirements # noinspection PyPackageRequirements
import urllib3 import urllib3
# <https://urllib3.readthedocs.io/en/latest/security.html> # <https://urllib3.readthedocs.io/en/latest/security.html>
urllib3.disable_warnings() urllib3.disable_warnings()
except (ImportError, AttributeError): except (ImportError, AttributeError):
pass pass
FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded; charset=utf-8' FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded; charset=utf-8'
JSON_CONTENT_TYPE = 'application/json' JSON_CONTENT_TYPE = 'application/json'
JSON_ACCEPT = f'{JSON_CONTENT_TYPE}, */*;q=0.5' JSON_ACCEPT = f'{JSON_CONTENT_TYPE}, */*;q=0.5'
@ -57,6 +58,7 @@ def collect_messages(
requests_session = build_requests_session( requests_session = build_requests_session(
ssl_version=args.ssl_version, ssl_version=args.ssl_version,
ciphers=args.ciphers, ciphers=args.ciphers,
verify=bool(send_kwargs_mergeable_from_env['verify'])
) )
if httpie_session: if httpie_session:
@ -147,19 +149,22 @@ def compress_body(request: requests.PreparedRequest, always: bool):
def build_requests_session( def build_requests_session(
verify: bool,
ssl_version: str = None, ssl_version: str = None,
ciphers: str = None, ciphers: str = None,
) -> requests.Session: ) -> requests.Session:
requests_session = requests.Session() requests_session = requests.Session()
# Install our adapter. # Install our adapter.
requests_session.mount('https://', HTTPieHTTPSAdapter( https_adapter = HTTPieHTTPSAdapter(
ciphers=ciphers, ciphers=ciphers,
verify=verify,
ssl_version=( ssl_version=(
AVAILABLE_SSL_VERSION_ARG_MAPPING[ssl_version] AVAILABLE_SSL_VERSION_ARG_MAPPING[ssl_version]
if ssl_version else None if ssl_version else None
),
) )
)) requests_session.mount('https://', https_adapter)
# Install adapters from plugins. # Install adapters from plugins.
for plugin_cls in plugin_manager.get_transport_plugins(): for plugin_cls in plugin_manager.get_transport_plugins():

View File

@ -25,11 +25,19 @@ AVAILABLE_SSL_VERSION_ARG_MAPPING = {
class HTTPieHTTPSAdapter(HTTPAdapter): 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( self._ssl_context = create_urllib3_context(
ciphers=ciphers, ciphers=ciphers,
ssl_version=resolve_ssl_version(ssl_version), ssl_version=resolve_ssl_version(ssl_version),
cert_reqs=ssl.CERT_REQUIRED if verify else ssl.CERT_NONE
) )
super().__init__(**kwargs) super().__init__(**kwargs)
def init_poolmanager(self, *args, **kwargs): def init_poolmanager(self, *args, **kwargs):