Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #321 from KostyaEsmukov/requests_adapter_respect_v…
Browse files Browse the repository at this point in the history
…erify

Respect `verify` option in requests adapter
  • Loading branch information
Lukasa committed May 24, 2017
2 parents 8c375ea + c4f8a89 commit acc6f20
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
22 changes: 15 additions & 7 deletions hyper/contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
HTTPAdapter = object

from hyper.common.connection import HTTPConnection
from hyper.compat import urlparse
from hyper.compat import urlparse, ssl
from hyper.tls import init_context


Expand All @@ -29,7 +29,7 @@ def __init__(self, *args, **kwargs):
#: A mapping between HTTP netlocs and ``HTTP20Connection`` objects.
self.connections = {}

def get_connection(self, host, port, scheme, cert=None):
def get_connection(self, host, port, scheme, cert=None, verify=True):
"""
Gets an appropriate HTTP/2 connection object based on
host/port/scheme/cert tuples.
Expand All @@ -40,22 +40,29 @@ def get_connection(self, host, port, scheme, cert=None):
port = 80 if not secure else 443

ssl_context = None
if cert is not None:
if not verify:
verify = False
ssl_context = init_context(cert=cert)
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
elif verify is True and cert is not None:
ssl_context = init_context(cert=cert)
elif verify is not True:
ssl_context = init_context(cert_path=verify, cert=cert)

try:
conn = self.connections[(host, port, scheme, cert)]
conn = self.connections[(host, port, scheme, cert, verify)]
except KeyError:
conn = HTTPConnection(
host,
port,
secure=secure,
ssl_context=ssl_context)
self.connections[(host, port, scheme, cert)] = conn
self.connections[(host, port, scheme, cert, verify)] = conn

return conn

def send(self, request, stream=False, cert=None, **kwargs):
def send(self, request, stream=False, cert=None, verify=True, **kwargs):
"""
Sends a HTTP message to the server.
"""
Expand All @@ -64,7 +71,8 @@ def send(self, request, stream=False, cert=None, **kwargs):
parsed.hostname,
parsed.port,
parsed.scheme,
cert=cert)
cert=cert,
verify=verify)

# Build the selector.
selector = parsed.path
Expand Down
26 changes: 25 additions & 1 deletion test/test_hyper.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)
from hyper.common.headers import HTTPHeaderMap
from hyper.common.util import to_bytestring, HTTPVersion
from hyper.compat import zlib_compressobj, is_py2
from hyper.compat import zlib_compressobj, is_py2, ssl
from hyper.contrib import HTTP20Adapter
import hyper.http20.errors as errors
import errno
Expand All @@ -31,6 +31,7 @@
TEST_DIR = os.path.abspath(os.path.dirname(__file__))
TEST_CERTS_DIR = os.path.join(TEST_DIR, 'certs')
CLIENT_PEM_FILE = os.path.join(TEST_CERTS_DIR, 'nopassword.pem')
SERVER_CERT_FILE = os.path.join(TEST_CERTS_DIR, 'server.crt')


def decode_frame(frame_data):
Expand Down Expand Up @@ -1129,6 +1130,29 @@ def test_adapter_accept_client_certificate(self):
'http',
cert=CLIENT_PEM_FILE)
assert conn1 is conn2
assert conn1._conn.ssl_context.check_hostname
assert conn1._conn.ssl_context.verify_mode == ssl.CERT_REQUIRED

def test_adapter_respects_disabled_ca_verification(self):
a = HTTP20Adapter()
conn = a.get_connection(
'http2bin.org',
80,
'http',
verify=False,
cert=CLIENT_PEM_FILE)
assert not conn._conn.ssl_context.check_hostname
assert conn._conn.ssl_context.verify_mode == ssl.CERT_NONE

def test_adapter_respects_custom_ca_verification(self):
a = HTTP20Adapter()
conn = a.get_connection(
'http2bin.org',
80,
'http',
verify=SERVER_CERT_FILE)
assert conn._conn.ssl_context.check_hostname
assert conn._conn.ssl_context.verify_mode == ssl.CERT_REQUIRED


class TestUtilities(object):
Expand Down

0 comments on commit acc6f20

Please sign in to comment.