diff --git a/doc/python.rst b/doc/python.rst index a92fc0b..d58463f 100644 --- a/doc/python.rst +++ b/doc/python.rst @@ -546,6 +546,15 @@ Session Objects Helper Functions ---------------- +.. py:function:: get_npn_protocols() + + Returns SPDY version strings which can be directly passed to + ``ssl.SSLContext.set_npn_protocols()``. Please note that the + returned list only includes SPDY version strings this library + supports. If the application intends to support other fallback + protocols (e.g., ``http/1.1``), the application should add them to + the returned list. + .. py:function:: npn_get_version(proto) Returns SPDY version which spdylay library supports from the given diff --git a/python/cspdylay.pxd b/python/cspdylay.pxd index f4e57b4..f6662af 100644 --- a/python/cspdylay.pxd +++ b/python/cspdylay.pxd @@ -323,5 +323,12 @@ cdef extern from 'spdylay/spdylay.h': int32_t stream_id, int32_t delta_window_size) + ctypedef struct spdylay_npn_proto: + unsigned char *proto + uint8_t len + uint16_t version + + spdylay_npn_proto* spdylay_npn_get_proto_list(size_t *len_ptr) + uint16_t spdylay_npn_get_version(unsigned char *proto, size_t protolen) diff --git a/python/spdylay.pyx b/python/spdylay.pyx index 423b419..b032e61 100644 --- a/python/spdylay.pyx +++ b/python/spdylay.pyx @@ -982,6 +982,16 @@ cdef class Session: cdef _strerror(int error_code): return cspdylay.spdylay_strerror(error_code).decode('UTF-8') +cpdef get_npn_protocols(): + cdef size_t proto_list_len + cdef cspdylay.spdylay_npn_proto *proto_list + proto_list = cspdylay.spdylay_npn_get_proto_list(&proto_list_len) + res = [] + for i in range(proto_list_len): + res.append((proto_list[i].proto)[:proto_list[i].len]\ + .decode('UTF-8')) + return res + cpdef int npn_get_version(proto): cdef char *cproto if proto == None: @@ -1449,7 +1459,7 @@ try: self.ctx.options = ssl.OP_ALL | ssl.OP_NO_SSLv2 | \ ssl.OP_NO_COMPRESSION self.ctx.load_cert_chain(cert_file, key_file) - self.ctx.set_npn_protocols(['spdy/3', 'spdy/2']) + self.ctx.set_npn_protocols(get_npn_protocols()) socketserver.TCPServer.__init__(self, server_address, RequestHandlerCalss) diff --git a/python/spdylay_tests.py b/python/spdylay_tests.py index 53a52c8..0ae3720 100644 --- a/python/spdylay_tests.py +++ b/python/spdylay_tests.py @@ -338,5 +338,9 @@ class SpdylayTests(unittest.TestCase): self.assertEqual(1, frame.stream_id) self.assertEqual(4096, frame.delta_window_size) + def test_get_npn_protocols(self): + protos = spdylay.get_npn_protocols() + self.assertEqual(['spdy/3', 'spdy/2'], protos) + if __name__ == '__main__': unittest.main()