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().
This commit is contained in:
Tatsuhiro Tsujikawa 2012-08-27 23:32:27 +09:00
parent 4e5e741907
commit 6b64276c08
4 changed files with 31 additions and 1 deletions

View File

@ -546,6 +546,15 @@ Session Objects
Helper Functions 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) .. py:function:: npn_get_version(proto)
Returns SPDY version which spdylay library supports from the given Returns SPDY version which spdylay library supports from the given

View File

@ -323,5 +323,12 @@ cdef extern from 'spdylay/spdylay.h':
int32_t stream_id, int32_t stream_id,
int32_t delta_window_size) 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, uint16_t spdylay_npn_get_version(unsigned char *proto,
size_t protolen) size_t protolen)

View File

@ -982,6 +982,16 @@ cdef class Session:
cdef _strerror(int error_code): cdef _strerror(int error_code):
return cspdylay.spdylay_strerror(error_code).decode('UTF-8') 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((<char*>proto_list[i].proto)[:proto_list[i].len]\
.decode('UTF-8'))
return res
cpdef int npn_get_version(proto): cpdef int npn_get_version(proto):
cdef char *cproto cdef char *cproto
if proto == None: if proto == None:
@ -1449,7 +1459,7 @@ try:
self.ctx.options = ssl.OP_ALL | ssl.OP_NO_SSLv2 | \ self.ctx.options = ssl.OP_ALL | ssl.OP_NO_SSLv2 | \
ssl.OP_NO_COMPRESSION ssl.OP_NO_COMPRESSION
self.ctx.load_cert_chain(cert_file, key_file) 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, socketserver.TCPServer.__init__(self, server_address,
RequestHandlerCalss) RequestHandlerCalss)

View File

@ -338,5 +338,9 @@ class SpdylayTests(unittest.TestCase):
self.assertEqual(1, frame.stream_id) self.assertEqual(1, frame.stream_id)
self.assertEqual(4096, frame.delta_window_size) 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__': if __name__ == '__main__':
unittest.main() unittest.main()