Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added spdy/3 dictionary and header length size

  • Loading branch information...
commit 207ca8032abdf82fb429e1779f1d6a247341ca18 1 parent b5734ea
Colin Marc authored

Showing 2 changed files with 108 additions and 10 deletions. Show diff stats Hide diff stats

  1. +102 6 cython/zlib_stream.pyx
  2. +6 4 python-spdy/context.py
108 cython/zlib_stream.pyx
... ... @@ -1,6 +1,6 @@
1 1 from libc.stdlib cimport malloc, free
2 2
3   -HEADER_ZLIB_DICT = \
  3 +HEADER_ZLIB_DICT_2 = \
4 4 b"optionsgetheadpostputdeletetraceacceptaccept-charsetaccept-encodingaccept-" \
5 5 b"languageauthorizationexpectfromhostif-modified-sinceif-matchif-none-matchi" \
6 6 b"f-rangeif-unmodifiedsincemax-forwardsproxy-authorizationrangerefererteuser" \
@@ -15,6 +15,97 @@ HEADER_ZLIB_DICT = \
15 15 b"ation/xhtmltext/plainpublicmax-agecharset=iso-8859-1utf-8gzipdeflateHTTP/1" \
16 16 b".1statusversionurl\x00"
17 17
  18 +HEADER_ZLIB_DICT_3 = \
  19 + b"\x00\x00\x00\x07\x6f\x70\x74\x69\x6f\x6e\x73\x00\x00\x00\x04\x68" \
  20 + b"\x65\x61\x64\x00\x00\x00\x04\x70\x6f\x73\x74\x00\x00\x00\x03\x70" \
  21 + b"\x75\x74\x00\x00\x00\x06\x64\x65\x6c\x65\x74\x65\x00\x00\x00\x05" \
  22 + b"\x74\x72\x61\x63\x65\x00\x00\x00\x06\x61\x63\x63\x65\x70\x74\x00" \
  23 + b"\x00\x00\x0e\x61\x63\x63\x65\x70\x74\x2d\x63\x68\x61\x72\x73\x65" \
  24 + b"\x74\x00\x00\x00\x0f\x61\x63\x63\x65\x70\x74\x2d\x65\x6e\x63\x6f" \
  25 + b"\x64\x69\x6e\x67\x00\x00\x00\x0f\x61\x63\x63\x65\x70\x74\x2d\x6c" \
  26 + b"\x61\x6e\x67\x75\x61\x67\x65\x00\x00\x00\x0d\x61\x63\x63\x65\x70" \
  27 + b"\x74\x2d\x72\x61\x6e\x67\x65\x73\x00\x00\x00\x03\x61\x67\x65\x00" \
  28 + b"\x00\x00\x05\x61\x6c\x6c\x6f\x77\x00\x00\x00\x0d\x61\x75\x74\x68" \
  29 + b"\x6f\x72\x69\x7a\x61\x74\x69\x6f\x6e\x00\x00\x00\x0d\x63\x61\x63" \
  30 + b"\x68\x65\x2d\x63\x6f\x6e\x74\x72\x6f\x6c\x00\x00\x00\x0a\x63\x6f" \
  31 + b"\x6e\x6e\x65\x63\x74\x69\x6f\x6e\x00\x00\x00\x0c\x63\x6f\x6e\x74" \
  32 + b"\x65\x6e\x74\x2d\x62\x61\x73\x65\x00\x00\x00\x10\x63\x6f\x6e\x74" \
  33 + b"\x65\x6e\x74\x2d\x65\x6e\x63\x6f\x64\x69\x6e\x67\x00\x00\x00\x10" \
  34 + b"\x63\x6f\x6e\x74\x65\x6e\x74\x2d\x6c\x61\x6e\x67\x75\x61\x67\x65" \
  35 + b"\x00\x00\x00\x0e\x63\x6f\x6e\x74\x65\x6e\x74\x2d\x6c\x65\x6e\x67" \
  36 + b"\x74\x68\x00\x00\x00\x10\x63\x6f\x6e\x74\x65\x6e\x74\x2d\x6c\x6f" \
  37 + b"\x63\x61\x74\x69\x6f\x6e\x00\x00\x00\x0b\x63\x6f\x6e\x74\x65\x6e" \
  38 + b"\x74\x2d\x6d\x64\x35\x00\x00\x00\x0d\x63\x6f\x6e\x74\x65\x6e\x74" \
  39 + b"\x2d\x72\x61\x6e\x67\x65\x00\x00\x00\x0c\x63\x6f\x6e\x74\x65\x6e" \
  40 + b"\x74\x2d\x74\x79\x70\x65\x00\x00\x00\x04\x64\x61\x74\x65\x00\x00" \
  41 + b"\x00\x04\x65\x74\x61\x67\x00\x00\x00\x06\x65\x78\x70\x65\x63\x74" \
  42 + b"\x00\x00\x00\x07\x65\x78\x70\x69\x72\x65\x73\x00\x00\x00\x04\x66" \
  43 + b"\x72\x6f\x6d\x00\x00\x00\x04\x68\x6f\x73\x74\x00\x00\x00\x08\x69" \
  44 + b"\x66\x2d\x6d\x61\x74\x63\x68\x00\x00\x00\x11\x69\x66\x2d\x6d\x6f" \
  45 + b"\x64\x69\x66\x69\x65\x64\x2d\x73\x69\x6e\x63\x65\x00\x00\x00\x0d" \
  46 + b"\x69\x66\x2d\x6e\x6f\x6e\x65\x2d\x6d\x61\x74\x63\x68\x00\x00\x00" \
  47 + b"\x08\x69\x66\x2d\x72\x61\x6e\x67\x65\x00\x00\x00\x13\x69\x66\x2d" \
  48 + b"\x75\x6e\x6d\x6f\x64\x69\x66\x69\x65\x64\x2d\x73\x69\x6e\x63\x65" \
  49 + b"\x00\x00\x00\x0d\x6c\x61\x73\x74\x2d\x6d\x6f\x64\x69\x66\x69\x65" \
  50 + b"\x64\x00\x00\x00\x08\x6c\x6f\x63\x61\x74\x69\x6f\x6e\x00\x00\x00" \
  51 + b"\x0c\x6d\x61\x78\x2d\x66\x6f\x72\x77\x61\x72\x64\x73\x00\x00\x00" \
  52 + b"\x06\x70\x72\x61\x67\x6d\x61\x00\x00\x00\x12\x70\x72\x6f\x78\x79" \
  53 + b"\x2d\x61\x75\x74\x68\x65\x6e\x74\x69\x63\x61\x74\x65\x00\x00\x00" \
  54 + b"\x13\x70\x72\x6f\x78\x79\x2d\x61\x75\x74\x68\x6f\x72\x69\x7a\x61" \
  55 + b"\x74\x69\x6f\x6e\x00\x00\x00\x05\x72\x61\x6e\x67\x65\x00\x00\x00" \
  56 + b"\x07\x72\x65\x66\x65\x72\x65\x72\x00\x00\x00\x0b\x72\x65\x74\x72" \
  57 + b"\x79\x2d\x61\x66\x74\x65\x72\x00\x00\x00\x06\x73\x65\x72\x76\x65" \
  58 + b"\x72\x00\x00\x00\x02\x74\x65\x00\x00\x00\x07\x74\x72\x61\x69\x6c" \
  59 + b"\x65\x72\x00\x00\x00\x11\x74\x72\x61\x6e\x73\x66\x65\x72\x2d\x65" \
  60 + b"\x6e\x63\x6f\x64\x69\x6e\x67\x00\x00\x00\x07\x75\x70\x67\x72\x61" \
  61 + b"\x64\x65\x00\x00\x00\x0a\x75\x73\x65\x72\x2d\x61\x67\x65\x6e\x74" \
  62 + b"\x00\x00\x00\x04\x76\x61\x72\x79\x00\x00\x00\x03\x76\x69\x61\x00" \
  63 + b"\x00\x00\x07\x77\x61\x72\x6e\x69\x6e\x67\x00\x00\x00\x10\x77\x77" \
  64 + b"\x77\x2d\x61\x75\x74\x68\x65\x6e\x74\x69\x63\x61\x74\x65\x00\x00" \
  65 + b"\x00\x06\x6d\x65\x74\x68\x6f\x64\x00\x00\x00\x03\x67\x65\x74\x00" \
  66 + b"\x00\x00\x06\x73\x74\x61\x74\x75\x73\x00\x00\x00\x06\x32\x30\x30" \
  67 + b"\x20\x4f\x4b\x00\x00\x00\x07\x76\x65\x72\x73\x69\x6f\x6e\x00\x00" \
  68 + b"\x00\x08\x48\x54\x54\x50\x2f\x31\x2e\x31\x00\x00\x00\x03\x75\x72" \
  69 + b"\x6c\x00\x00\x00\x06\x70\x75\x62\x6c\x69\x63\x00\x00\x00\x0a\x73" \
  70 + b"\x65\x74\x2d\x63\x6f\x6f\x6b\x69\x65\x00\x00\x00\x0a\x6b\x65\x65" \
  71 + b"\x70\x2d\x61\x6c\x69\x76\x65\x00\x00\x00\x06\x6f\x72\x69\x67\x69" \
  72 + b"\x6e\x31\x30\x30\x31\x30\x31\x32\x30\x31\x32\x30\x32\x32\x30\x35" \
  73 + b"\x32\x30\x36\x33\x30\x30\x33\x30\x32\x33\x30\x33\x33\x30\x34\x33" \
  74 + b"\x30\x35\x33\x30\x36\x33\x30\x37\x34\x30\x32\x34\x30\x35\x34\x30" \
  75 + b"\x36\x34\x30\x37\x34\x30\x38\x34\x30\x39\x34\x31\x30\x34\x31\x31" \
  76 + b"\x34\x31\x32\x34\x31\x33\x34\x31\x34\x34\x31\x35\x34\x31\x36\x34" \
  77 + b"\x31\x37\x35\x30\x32\x35\x30\x34\x35\x30\x35\x32\x30\x33\x20\x4e" \
  78 + b"\x6f\x6e\x2d\x41\x75\x74\x68\x6f\x72\x69\x74\x61\x74\x69\x76\x65" \
  79 + b"\x20\x49\x6e\x66\x6f\x72\x6d\x61\x74\x69\x6f\x6e\x32\x30\x34\x20" \
  80 + b"\x4e\x6f\x20\x43\x6f\x6e\x74\x65\x6e\x74\x33\x30\x31\x20\x4d\x6f" \
  81 + b"\x76\x65\x64\x20\x50\x65\x72\x6d\x61\x6e\x65\x6e\x74\x6c\x79\x34" \
  82 + b"\x30\x30\x20\x42\x61\x64\x20\x52\x65\x71\x75\x65\x73\x74\x34\x30" \
  83 + b"\x31\x20\x55\x6e\x61\x75\x74\x68\x6f\x72\x69\x7a\x65\x64\x34\x30" \
  84 + b"\x33\x20\x46\x6f\x72\x62\x69\x64\x64\x65\x6e\x34\x30\x34\x20\x4e" \
  85 + b"\x6f\x74\x20\x46\x6f\x75\x6e\x64\x35\x30\x30\x20\x49\x6e\x74\x65" \
  86 + b"\x72\x6e\x61\x6c\x20\x53\x65\x72\x76\x65\x72\x20\x45\x72\x72\x6f" \
  87 + b"\x72\x35\x30\x31\x20\x4e\x6f\x74\x20\x49\x6d\x70\x6c\x65\x6d\x65" \
  88 + b"\x6e\x74\x65\x64\x35\x30\x33\x20\x53\x65\x72\x76\x69\x63\x65\x20" \
  89 + b"\x55\x6e\x61\x76\x61\x69\x6c\x61\x62\x6c\x65\x4a\x61\x6e\x20\x46" \
  90 + b"\x65\x62\x20\x4d\x61\x72\x20\x41\x70\x72\x20\x4d\x61\x79\x20\x4a" \
  91 + b"\x75\x6e\x20\x4a\x75\x6c\x20\x41\x75\x67\x20\x53\x65\x70\x74\x20" \
  92 + b"\x4f\x63\x74\x20\x4e\x6f\x76\x20\x44\x65\x63\x20\x30\x30\x3a\x30" \
  93 + b"\x30\x3a\x30\x30\x20\x4d\x6f\x6e\x2c\x20\x54\x75\x65\x2c\x20\x57" \
  94 + b"\x65\x64\x2c\x20\x54\x68\x75\x2c\x20\x46\x72\x69\x2c\x20\x53\x61" \
  95 + b"\x74\x2c\x20\x53\x75\x6e\x2c\x20\x47\x4d\x54\x63\x68\x75\x6e\x6b" \
  96 + b"\x65\x64\x2c\x74\x65\x78\x74\x2f\x68\x74\x6d\x6c\x2c\x69\x6d\x61" \
  97 + b"\x67\x65\x2f\x70\x6e\x67\x2c\x69\x6d\x61\x67\x65\x2f\x6a\x70\x67" \
  98 + b"\x2c\x69\x6d\x61\x67\x65\x2f\x67\x69\x66\x2c\x61\x70\x70\x6c\x69" \
  99 + b"\x63\x61\x74\x69\x6f\x6e\x2f\x78\x6d\x6c\x2c\x61\x70\x70\x6c\x69" \
  100 + b"\x63\x61\x74\x69\x6f\x6e\x2f\x78\x68\x74\x6d\x6c\x2b\x78\x6d\x6c" \
  101 + b"\x2c\x74\x65\x78\x74\x2f\x70\x6c\x61\x69\x6e\x2c\x74\x65\x78\x74" \
  102 + b"\x2f\x6a\x61\x76\x61\x73\x63\x72\x69\x70\x74\x2c\x70\x75\x62\x6c" \
  103 + b"\x69\x63\x70\x72\x69\x76\x61\x74\x65\x6d\x61\x78\x2d\x61\x67\x65" \
  104 + b"\x3d\x67\x7a\x69\x70\x2c\x64\x65\x66\x6c\x61\x74\x65\x2c\x73\x64" \
  105 + b"\x63\x68\x63\x68\x61\x72\x73\x65\x74\x3d\x75\x74\x66\x2d\x38\x63" \
  106 + b"\x68\x61\x72\x73\x65\x74\x3d\x69\x73\x6f\x2d\x38\x38\x35\x39\x2d" \
  107 + b"\x31\x2c\x75\x74\x66\x2d\x2c\x2a\x2c\x65\x6e\x71\x3d\x30\x2e"
  108 +
18 109 cdef extern from "zlib.h":
19 110
20 111 ctypedef void *voidp
@@ -62,10 +153,11 @@ cdef class Stream(object):
62 153 self._stream.zfree = NULL
63 154
64 155 cdef class Deflater(Stream):
65   - def __init__(self, level=6):
  156 + def __init__(self, version, level=6):
66 157 Stream.__init__(self)
67 158 deflateInit(self._stream, level)
68   - deflateSetDictionary(self._stream, HEADER_ZLIB_DICT, len(HEADER_ZLIB_DICT))
  159 + dictionary = HEADER_ZLIB_DICT_2 if version == 2 else HEADER_ZLIB_DICT_3
  160 + deflateSetDictionary(self._stream, dictionary, len(dictionary))
69 161
70 162 def __dealloc__(self):
71 163 deflateEnd(self._stream)
@@ -95,10 +187,12 @@ cdef class Deflater(Stream):
95 187 return bytes(buf)
96 188
97 189 cdef class Inflater(Stream):
  190 + cdef int version
98 191
99   - def __init__(self):
  192 + def __init__(self, version):
100 193 Stream.__init__(self)
101 194 inflateInit(self._stream)
  195 + self.version = version
102 196
103 197 def __dealloc__(self):
104 198 inflateEnd(self._stream)
@@ -107,6 +201,8 @@ cdef class Inflater(Stream):
107 201 def decompress(self, chunk):
108 202 self._stream.next_in = chunk
109 203 self._stream.avail_in = len(chunk)
  204 +
  205 + dictionary = HEADER_ZLIB_DICT_2 if self.version == 2 else HEADER_ZLIB_DICT_3
110 206
111 207 buf = bytearray()
112 208 chunk_len = 1024 * 64
@@ -118,7 +214,7 @@ cdef class Inflater(Stream):
118 214
119 215 status = inflate(self._stream, Z_SYNC_FLUSH)
120 216 if status == Z_NEED_DICT:
121   - err = inflateSetDictionary(self._stream, HEADER_ZLIB_DICT, len(HEADER_ZLIB_DICT))
  217 + err = inflateSetDictionary(self._stream, dictionary, len(dictionary))
122 218 assert err == Z_OK
123 219 continue
124 220
@@ -128,7 +224,7 @@ cdef class Inflater(Stream):
128 224 if status == Z_STREAM_END or self._stream.avail_in == 0:
129 225 break
130 226 else:
131   - assert status == Z_OK
  227 + assert status == Z_OK, 'failed to decompress! status is ' + str(status)
132 228
133 229 return bytes(buf)
134 230
10 python-spdy/context.py
@@ -25,8 +25,8 @@ def __init__(self, side, version=2):
25 25 raise NotImplementedError()
26 26 self.version = version
27 27
28   - self.deflater = Deflater()
29   - self.inflater = Inflater()
  28 + self.deflater = Deflater(version)
  29 + self.inflater = Inflater(version)
30 30 self.frame_queue = []
31 31 self.input_buffer = bytearray()
32 32
@@ -71,15 +71,17 @@ def outgoing(self):
71 71 return out
72 72
73 73 def _parse_header_chunk(self, compressed_data, version):
  74 + print(compressed_data)
74 75 chunk = self.inflater.decompress(compressed_data)
75 76 length_size = 2 if version == 2 else 4
  77 + print(length_size)
76 78 headers = {}
77 79
78 80 #first two bytes: number of pairs
79   - num_values = int.from_bytes(chunk[0:2], 'big')
  81 + num_values = int.from_bytes(chunk[0:length_size], 'big')
80 82
81 83 #after that...
82   - cursor = 2
  84 + cursor = length_size
83 85 for _ in range(num_values):
84 86 #two/four bytes: length of name
85 87 name_length = int.from_bytes(chunk[cursor:cursor+length_size], 'big')

0 comments on commit 207ca80

Please sign in to comment.
Something went wrong with that request. Please try again.