From 6b64276c085c5f1d1eaa2e052b15dbc995374532 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 27 Aug 2012 23:32:27 +0900 Subject: [PATCH] python: add get_npn_protocols() function get_npn_protocols() is high level wrapper for spdylay_npn_get_proto_list() and the returned list can be passed to ssl.SSLContext.set_npn_protocols(). --- doc/python.rst | 9 +++++++++ python/cspdylay.pxd | 7 +++++++ python/spdylay.pyx | 12 +++++++++++- python/spdylay_tests.py | 4 ++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/doc/python.rst b/doc/python.rst index a92fc0bb..d58463f3 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 f4e57b41..f6662afc 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 423b4198..b032e616 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 53a52c87..0ae3720e 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()