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

Commit acc6f20

Browse files
authored
Merge pull request #321 from KostyaEsmukov/requests_adapter_respect_verify
Respect `verify` option in requests adapter
2 parents 8c375ea + c4f8a89 commit acc6f20

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

hyper/contrib.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
HTTPAdapter = object
1616

1717
from hyper.common.connection import HTTPConnection
18-
from hyper.compat import urlparse
18+
from hyper.compat import urlparse, ssl
1919
from hyper.tls import init_context
2020

2121

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

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

4242
ssl_context = None
43-
if cert is not None:
43+
if not verify:
44+
verify = False
4445
ssl_context = init_context(cert=cert)
46+
ssl_context.check_hostname = False
47+
ssl_context.verify_mode = ssl.CERT_NONE
48+
elif verify is True and cert is not None:
49+
ssl_context = init_context(cert=cert)
50+
elif verify is not True:
51+
ssl_context = init_context(cert_path=verify, cert=cert)
4552

4653
try:
47-
conn = self.connections[(host, port, scheme, cert)]
54+
conn = self.connections[(host, port, scheme, cert, verify)]
4855
except KeyError:
4956
conn = HTTPConnection(
5057
host,
5158
port,
5259
secure=secure,
5360
ssl_context=ssl_context)
54-
self.connections[(host, port, scheme, cert)] = conn
61+
self.connections[(host, port, scheme, cert, verify)] = conn
5562

5663
return conn
5764

58-
def send(self, request, stream=False, cert=None, **kwargs):
65+
def send(self, request, stream=False, cert=None, verify=True, **kwargs):
5966
"""
6067
Sends a HTTP message to the server.
6168
"""
@@ -64,7 +71,8 @@ def send(self, request, stream=False, cert=None, **kwargs):
6471
parsed.hostname,
6572
parsed.port,
6673
parsed.scheme,
67-
cert=cert)
74+
cert=cert,
75+
verify=verify)
6876

6977
# Build the selector.
7078
selector = parsed.path

test/test_hyper.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919
from hyper.common.headers import HTTPHeaderMap
2020
from hyper.common.util import to_bytestring, HTTPVersion
21-
from hyper.compat import zlib_compressobj, is_py2
21+
from hyper.compat import zlib_compressobj, is_py2, ssl
2222
from hyper.contrib import HTTP20Adapter
2323
import hyper.http20.errors as errors
2424
import errno
@@ -31,6 +31,7 @@
3131
TEST_DIR = os.path.abspath(os.path.dirname(__file__))
3232
TEST_CERTS_DIR = os.path.join(TEST_DIR, 'certs')
3333
CLIENT_PEM_FILE = os.path.join(TEST_CERTS_DIR, 'nopassword.pem')
34+
SERVER_CERT_FILE = os.path.join(TEST_CERTS_DIR, 'server.crt')
3435

3536

3637
def decode_frame(frame_data):
@@ -1129,6 +1130,29 @@ def test_adapter_accept_client_certificate(self):
11291130
'http',
11301131
cert=CLIENT_PEM_FILE)
11311132
assert conn1 is conn2
1133+
assert conn1._conn.ssl_context.check_hostname
1134+
assert conn1._conn.ssl_context.verify_mode == ssl.CERT_REQUIRED
1135+
1136+
def test_adapter_respects_disabled_ca_verification(self):
1137+
a = HTTP20Adapter()
1138+
conn = a.get_connection(
1139+
'http2bin.org',
1140+
80,
1141+
'http',
1142+
verify=False,
1143+
cert=CLIENT_PEM_FILE)
1144+
assert not conn._conn.ssl_context.check_hostname
1145+
assert conn._conn.ssl_context.verify_mode == ssl.CERT_NONE
1146+
1147+
def test_adapter_respects_custom_ca_verification(self):
1148+
a = HTTP20Adapter()
1149+
conn = a.get_connection(
1150+
'http2bin.org',
1151+
80,
1152+
'http',
1153+
verify=SERVER_CERT_FILE)
1154+
assert conn._conn.ssl_context.check_hostname
1155+
assert conn._conn.ssl_context.verify_mode == ssl.CERT_REQUIRED
11321156

11331157

11341158
class TestUtilities(object):

0 commit comments

Comments
 (0)