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

Commit 907d292

Browse files
committed
Add ssl_contex parameter to the HTTP and HTTP11 constructors
Update the abstraction and SSLContext tests to take the new ssl_context parameter into account.
1 parent 088a1e1 commit 907d292

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

hyper/common/connection.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,24 @@ class HTTPConnection(object):
3939
:param enable_push: (optional) Whether the server is allowed to push
4040
resources to the client (see
4141
:meth:`get_pushes() <hyper.HTTP20Connection.get_pushes>`).
42+
:param ssl_context: (optional) A class with custom certificate settings.
43+
If not provided then hyper's default SSLContext is used instead.
4244
"""
4345
def __init__(self,
4446
host,
4547
port=None,
4648
secure=None,
4749
window_manager=None,
4850
enable_push=False,
51+
ssl_context=None,
4952
**kwargs):
5053

5154
self._host = host
5255
self._port = port
53-
self._h1_kwargs = {'secure': secure}
56+
self._h1_kwargs = {'secure': secure, 'ssl_context': ssl_context}
5457
self._h2_kwargs = {
55-
'window_manager': window_manager, 'enable_push': enable_push
58+
'window_manager': window_manager, 'enable_push': enable_push,
59+
'ssl_context': ssl_context
5660
}
5761

5862
# Add any unexpected kwargs to both dictionaries.

hyper/http11/connection.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ class HTTP11Connection(object):
4343
:param secure: (optional) Whether the request should use TLS. Defaults to
4444
``False`` for most requests, but to ``True`` for any request issued to
4545
port 443.
46+
:param ssl_context: (optional) A class with custom certificate settings.
47+
If not provided then hyper's default SSLContext is used instead.
4648
"""
47-
def __init__(self, host, port=None, secure=None, **kwargs):
49+
def __init__(self, host, port=None, secure=None, ssl_context=None,
50+
**kwargs):
4851
if port is None:
4952
try:
5053
self.host, self.port = host.split(':')
@@ -64,6 +67,7 @@ def __init__(self, host, port=None, secure=None, **kwargs):
6467
else:
6568
self.secure = False
6669

70+
self.ssl_context = ssl_context
6771
self._sock = None
6872

6973
#: The size of the in-memory buffer used to store data from the
@@ -88,7 +92,7 @@ def connect(self):
8892
proto = None
8993

9094
if self.secure:
91-
sock, proto = wrap_socket(sock, self.host)
95+
sock, proto = wrap_socket(sock, self.host, self.ssl_context)
9296

9397
log.debug("Selected NPN protocol: %s", proto)
9498
sock = BufferedSocket(sock, self.network_buffer_size)

hyper/tls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def wrap_socket(sock, server_hostname, ssl_context=None):
3030
"""
3131
global _context
3232

33+
# create the singleton SSLContext we use
3334
if _context is None: # pragma: no cover
3435
_context = _init_context()
3536

@@ -63,7 +64,7 @@ def wrap_socket(sock, server_hostname, ssl_context=None):
6364

6465
def _init_context():
6566
"""
66-
Creates the singleton SSLContext we use.
67+
Create a pre-configured SSLContext.
6768
"""
6869
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
6970
context.set_default_verify_paths()

test/test_SSLContext.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Tests the hyper SSLContext.
44
"""
55
import hyper
6-
from hyper import HTTP20Connection
6+
from hyper.common.connection import HTTPConnection
77
from hyper.compat import ssl
88
import pytest
99

@@ -34,16 +34,16 @@ def test_custom_context(self):
3434
assert hyper.tls._context.options & ssl.OP_NO_COMPRESSION == 0
3535

3636

37-
def test_http20Connection_with_custom_context(self):
37+
def test_HTTPConnection_with_custom_context(self):
3838
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
3939
context.set_default_verify_paths()
4040
context.verify_mode = ssl.CERT_REQUIRED
4141
context.check_hostname = True
4242
context.set_npn_protocols(['h2', 'h2-15'])
4343
context.options |= ssl.OP_NO_COMPRESSION
44-
45-
conn = HTTP20Connection('http2bin.org:443', ssl_context=context)
46-
44+
45+
conn = HTTPConnection('http2bin.org', 443, ssl_context=context)
46+
4747
assert conn.ssl_context.check_hostname == True
4848
assert conn.ssl_context.verify_mode == ssl.CERT_REQUIRED
4949
assert conn.ssl_context.options & ssl.OP_NO_COMPRESSION != 0

test/test_abstraction.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,25 @@ class TestHTTPConnection(object):
88
def test_h1_kwargs(self):
99
c = HTTPConnection(
1010
'test', 443, secure=False, window_manager=True, enable_push=True,
11-
other_kwarg=True
11+
ssl_context=False, other_kwarg=True
1212
)
1313

1414
assert c._h1_kwargs == {
1515
'secure': False,
16+
'ssl_context': False,
1617
'other_kwarg': True,
1718
}
1819

1920
def test_h2_kwargs(self):
2021
c = HTTPConnection(
2122
'test', 443, secure=False, window_manager=True, enable_push=True,
22-
other_kwarg=True
23+
ssl_context=True, other_kwarg=True
2324
)
2425

2526
assert c._h2_kwargs == {
2627
'window_manager': True,
2728
'enable_push': True,
29+
'ssl_context': True,
2830
'other_kwarg': True,
2931
}
3032

0 commit comments

Comments
 (0)