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

Commit 1da4cb0

Browse files
committed
Extend SSL compat with NPN.
1 parent 6fb1799 commit 1da4cb0

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

hyper/ssl_compat.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def send(self, data, flags=None):
232232
return self._safe_ssl_call(False, self._conn.send, data, flags)
233233

234234
def selected_npn_protocol(self):
235-
raise NotImplementedError()
235+
self._conn.get_next_proto_negotiated()
236236

237237
def getpeercert(self):
238238
def resolve_alias(alias):
@@ -276,6 +276,7 @@ def __init__(self, protocol):
276276
self._ctx = ossl.Context(protocol)
277277
self.options = OP_ALL
278278
self.check_hostname = False
279+
self.npn_protos = []
279280

280281
@property
281282
def options(self):
@@ -315,8 +316,20 @@ def load_cert_chain(self, certfile, keyfile=None, password=None):
315316
self._ctx.use_privatekey_file(keyfile or certfile)
316317

317318
def set_npn_protocols(self, protocols):
318-
# TODO
319-
raise NotImplementedError()
319+
self.protocols = protocols
320+
321+
def cb(conn, protos):
322+
# Detect the overlapping set of protocols.
323+
overlap = set(protos) & set(self.protocols)
324+
325+
# Select the option that comes last in the list in the overlap.
326+
for p in self.protocols[::-1]:
327+
if p in overlap:
328+
return p
329+
else:
330+
return b''
331+
332+
self._ctx.set_npn_select_callback(cb)
320333

321334
def wrap_socket(self, sock, server_side=False, do_handshake_on_connect=True,
322335
suppress_ragged_eofs=True, server_hostname=None):

0 commit comments

Comments
 (0)