Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added spdy/3 dictionary and header length size

  • Loading branch information...
commit 207ca8032abdf82fb429e1779f1d6a247341ca18 1 parent b5734ea
@colinmarc authored
Showing with 108 additions and 10 deletions.
  1. +102 −6 cython/zlib_stream.pyx
  2. +6 −4 python-spdy/context.py
View
108 cython/zlib_stream.pyx
@@ -1,6 +1,6 @@
from libc.stdlib cimport malloc, free
-HEADER_ZLIB_DICT = \
+HEADER_ZLIB_DICT_2 = \
b"optionsgetheadpostputdeletetraceacceptaccept-charsetaccept-encodingaccept-" \
b"languageauthorizationexpectfromhostif-modified-sinceif-matchif-none-matchi" \
b"f-rangeif-unmodifiedsincemax-forwardsproxy-authorizationrangerefererteuser" \
@@ -15,6 +15,97 @@ HEADER_ZLIB_DICT = \
b"ation/xhtmltext/plainpublicmax-agecharset=iso-8859-1utf-8gzipdeflateHTTP/1" \
b".1statusversionurl\x00"
+HEADER_ZLIB_DICT_3 = \
+ b"\x00\x00\x00\x07\x6f\x70\x74\x69\x6f\x6e\x73\x00\x00\x00\x04\x68" \
+ b"\x65\x61\x64\x00\x00\x00\x04\x70\x6f\x73\x74\x00\x00\x00\x03\x70" \
+ b"\x75\x74\x00\x00\x00\x06\x64\x65\x6c\x65\x74\x65\x00\x00\x00\x05" \
+ b"\x74\x72\x61\x63\x65\x00\x00\x00\x06\x61\x63\x63\x65\x70\x74\x00" \
+ b"\x00\x00\x0e\x61\x63\x63\x65\x70\x74\x2d\x63\x68\x61\x72\x73\x65" \
+ b"\x74\x00\x00\x00\x0f\x61\x63\x63\x65\x70\x74\x2d\x65\x6e\x63\x6f" \
+ b"\x64\x69\x6e\x67\x00\x00\x00\x0f\x61\x63\x63\x65\x70\x74\x2d\x6c" \
+ b"\x61\x6e\x67\x75\x61\x67\x65\x00\x00\x00\x0d\x61\x63\x63\x65\x70" \
+ b"\x74\x2d\x72\x61\x6e\x67\x65\x73\x00\x00\x00\x03\x61\x67\x65\x00" \
+ b"\x00\x00\x05\x61\x6c\x6c\x6f\x77\x00\x00\x00\x0d\x61\x75\x74\x68" \
+ b"\x6f\x72\x69\x7a\x61\x74\x69\x6f\x6e\x00\x00\x00\x0d\x63\x61\x63" \
+ b"\x68\x65\x2d\x63\x6f\x6e\x74\x72\x6f\x6c\x00\x00\x00\x0a\x63\x6f" \
+ b"\x6e\x6e\x65\x63\x74\x69\x6f\x6e\x00\x00\x00\x0c\x63\x6f\x6e\x74" \
+ b"\x65\x6e\x74\x2d\x62\x61\x73\x65\x00\x00\x00\x10\x63\x6f\x6e\x74" \
+ b"\x65\x6e\x74\x2d\x65\x6e\x63\x6f\x64\x69\x6e\x67\x00\x00\x00\x10" \
+ b"\x63\x6f\x6e\x74\x65\x6e\x74\x2d\x6c\x61\x6e\x67\x75\x61\x67\x65" \
+ b"\x00\x00\x00\x0e\x63\x6f\x6e\x74\x65\x6e\x74\x2d\x6c\x65\x6e\x67" \
+ b"\x74\x68\x00\x00\x00\x10\x63\x6f\x6e\x74\x65\x6e\x74\x2d\x6c\x6f" \
+ b"\x63\x61\x74\x69\x6f\x6e\x00\x00\x00\x0b\x63\x6f\x6e\x74\x65\x6e" \
+ b"\x74\x2d\x6d\x64\x35\x00\x00\x00\x0d\x63\x6f\x6e\x74\x65\x6e\x74" \
+ b"\x2d\x72\x61\x6e\x67\x65\x00\x00\x00\x0c\x63\x6f\x6e\x74\x65\x6e" \
+ b"\x74\x2d\x74\x79\x70\x65\x00\x00\x00\x04\x64\x61\x74\x65\x00\x00" \
+ b"\x00\x04\x65\x74\x61\x67\x00\x00\x00\x06\x65\x78\x70\x65\x63\x74" \
+ b"\x00\x00\x00\x07\x65\x78\x70\x69\x72\x65\x73\x00\x00\x00\x04\x66" \
+ b"\x72\x6f\x6d\x00\x00\x00\x04\x68\x6f\x73\x74\x00\x00\x00\x08\x69" \
+ b"\x66\x2d\x6d\x61\x74\x63\x68\x00\x00\x00\x11\x69\x66\x2d\x6d\x6f" \
+ b"\x64\x69\x66\x69\x65\x64\x2d\x73\x69\x6e\x63\x65\x00\x00\x00\x0d" \
+ b"\x69\x66\x2d\x6e\x6f\x6e\x65\x2d\x6d\x61\x74\x63\x68\x00\x00\x00" \
+ b"\x08\x69\x66\x2d\x72\x61\x6e\x67\x65\x00\x00\x00\x13\x69\x66\x2d" \
+ b"\x75\x6e\x6d\x6f\x64\x69\x66\x69\x65\x64\x2d\x73\x69\x6e\x63\x65" \
+ b"\x00\x00\x00\x0d\x6c\x61\x73\x74\x2d\x6d\x6f\x64\x69\x66\x69\x65" \
+ b"\x64\x00\x00\x00\x08\x6c\x6f\x63\x61\x74\x69\x6f\x6e\x00\x00\x00" \
+ b"\x0c\x6d\x61\x78\x2d\x66\x6f\x72\x77\x61\x72\x64\x73\x00\x00\x00" \
+ b"\x06\x70\x72\x61\x67\x6d\x61\x00\x00\x00\x12\x70\x72\x6f\x78\x79" \
+ b"\x2d\x61\x75\x74\x68\x65\x6e\x74\x69\x63\x61\x74\x65\x00\x00\x00" \
+ b"\x13\x70\x72\x6f\x78\x79\x2d\x61\x75\x74\x68\x6f\x72\x69\x7a\x61" \
+ b"\x74\x69\x6f\x6e\x00\x00\x00\x05\x72\x61\x6e\x67\x65\x00\x00\x00" \
+ b"\x07\x72\x65\x66\x65\x72\x65\x72\x00\x00\x00\x0b\x72\x65\x74\x72" \
+ b"\x79\x2d\x61\x66\x74\x65\x72\x00\x00\x00\x06\x73\x65\x72\x76\x65" \
+ b"\x72\x00\x00\x00\x02\x74\x65\x00\x00\x00\x07\x74\x72\x61\x69\x6c" \
+ b"\x65\x72\x00\x00\x00\x11\x74\x72\x61\x6e\x73\x66\x65\x72\x2d\x65" \
+ b"\x6e\x63\x6f\x64\x69\x6e\x67\x00\x00\x00\x07\x75\x70\x67\x72\x61" \
+ b"\x64\x65\x00\x00\x00\x0a\x75\x73\x65\x72\x2d\x61\x67\x65\x6e\x74" \
+ b"\x00\x00\x00\x04\x76\x61\x72\x79\x00\x00\x00\x03\x76\x69\x61\x00" \
+ b"\x00\x00\x07\x77\x61\x72\x6e\x69\x6e\x67\x00\x00\x00\x10\x77\x77" \
+ b"\x77\x2d\x61\x75\x74\x68\x65\x6e\x74\x69\x63\x61\x74\x65\x00\x00" \
+ b"\x00\x06\x6d\x65\x74\x68\x6f\x64\x00\x00\x00\x03\x67\x65\x74\x00" \
+ b"\x00\x00\x06\x73\x74\x61\x74\x75\x73\x00\x00\x00\x06\x32\x30\x30" \
+ b"\x20\x4f\x4b\x00\x00\x00\x07\x76\x65\x72\x73\x69\x6f\x6e\x00\x00" \
+ b"\x00\x08\x48\x54\x54\x50\x2f\x31\x2e\x31\x00\x00\x00\x03\x75\x72" \
+ b"\x6c\x00\x00\x00\x06\x70\x75\x62\x6c\x69\x63\x00\x00\x00\x0a\x73" \
+ b"\x65\x74\x2d\x63\x6f\x6f\x6b\x69\x65\x00\x00\x00\x0a\x6b\x65\x65" \
+ b"\x70\x2d\x61\x6c\x69\x76\x65\x00\x00\x00\x06\x6f\x72\x69\x67\x69" \
+ b"\x6e\x31\x30\x30\x31\x30\x31\x32\x30\x31\x32\x30\x32\x32\x30\x35" \
+ b"\x32\x30\x36\x33\x30\x30\x33\x30\x32\x33\x30\x33\x33\x30\x34\x33" \
+ b"\x30\x35\x33\x30\x36\x33\x30\x37\x34\x30\x32\x34\x30\x35\x34\x30" \
+ b"\x36\x34\x30\x37\x34\x30\x38\x34\x30\x39\x34\x31\x30\x34\x31\x31" \
+ b"\x34\x31\x32\x34\x31\x33\x34\x31\x34\x34\x31\x35\x34\x31\x36\x34" \
+ b"\x31\x37\x35\x30\x32\x35\x30\x34\x35\x30\x35\x32\x30\x33\x20\x4e" \
+ b"\x6f\x6e\x2d\x41\x75\x74\x68\x6f\x72\x69\x74\x61\x74\x69\x76\x65" \
+ b"\x20\x49\x6e\x66\x6f\x72\x6d\x61\x74\x69\x6f\x6e\x32\x30\x34\x20" \
+ b"\x4e\x6f\x20\x43\x6f\x6e\x74\x65\x6e\x74\x33\x30\x31\x20\x4d\x6f" \
+ b"\x76\x65\x64\x20\x50\x65\x72\x6d\x61\x6e\x65\x6e\x74\x6c\x79\x34" \
+ b"\x30\x30\x20\x42\x61\x64\x20\x52\x65\x71\x75\x65\x73\x74\x34\x30" \
+ b"\x31\x20\x55\x6e\x61\x75\x74\x68\x6f\x72\x69\x7a\x65\x64\x34\x30" \
+ b"\x33\x20\x46\x6f\x72\x62\x69\x64\x64\x65\x6e\x34\x30\x34\x20\x4e" \
+ b"\x6f\x74\x20\x46\x6f\x75\x6e\x64\x35\x30\x30\x20\x49\x6e\x74\x65" \
+ b"\x72\x6e\x61\x6c\x20\x53\x65\x72\x76\x65\x72\x20\x45\x72\x72\x6f" \
+ b"\x72\x35\x30\x31\x20\x4e\x6f\x74\x20\x49\x6d\x70\x6c\x65\x6d\x65" \
+ b"\x6e\x74\x65\x64\x35\x30\x33\x20\x53\x65\x72\x76\x69\x63\x65\x20" \
+ b"\x55\x6e\x61\x76\x61\x69\x6c\x61\x62\x6c\x65\x4a\x61\x6e\x20\x46" \
+ b"\x65\x62\x20\x4d\x61\x72\x20\x41\x70\x72\x20\x4d\x61\x79\x20\x4a" \
+ b"\x75\x6e\x20\x4a\x75\x6c\x20\x41\x75\x67\x20\x53\x65\x70\x74\x20" \
+ b"\x4f\x63\x74\x20\x4e\x6f\x76\x20\x44\x65\x63\x20\x30\x30\x3a\x30" \
+ b"\x30\x3a\x30\x30\x20\x4d\x6f\x6e\x2c\x20\x54\x75\x65\x2c\x20\x57" \
+ b"\x65\x64\x2c\x20\x54\x68\x75\x2c\x20\x46\x72\x69\x2c\x20\x53\x61" \
+ b"\x74\x2c\x20\x53\x75\x6e\x2c\x20\x47\x4d\x54\x63\x68\x75\x6e\x6b" \
+ b"\x65\x64\x2c\x74\x65\x78\x74\x2f\x68\x74\x6d\x6c\x2c\x69\x6d\x61" \
+ b"\x67\x65\x2f\x70\x6e\x67\x2c\x69\x6d\x61\x67\x65\x2f\x6a\x70\x67" \
+ b"\x2c\x69\x6d\x61\x67\x65\x2f\x67\x69\x66\x2c\x61\x70\x70\x6c\x69" \
+ b"\x63\x61\x74\x69\x6f\x6e\x2f\x78\x6d\x6c\x2c\x61\x70\x70\x6c\x69" \
+ b"\x63\x61\x74\x69\x6f\x6e\x2f\x78\x68\x74\x6d\x6c\x2b\x78\x6d\x6c" \
+ b"\x2c\x74\x65\x78\x74\x2f\x70\x6c\x61\x69\x6e\x2c\x74\x65\x78\x74" \
+ b"\x2f\x6a\x61\x76\x61\x73\x63\x72\x69\x70\x74\x2c\x70\x75\x62\x6c" \
+ b"\x69\x63\x70\x72\x69\x76\x61\x74\x65\x6d\x61\x78\x2d\x61\x67\x65" \
+ b"\x3d\x67\x7a\x69\x70\x2c\x64\x65\x66\x6c\x61\x74\x65\x2c\x73\x64" \
+ b"\x63\x68\x63\x68\x61\x72\x73\x65\x74\x3d\x75\x74\x66\x2d\x38\x63" \
+ b"\x68\x61\x72\x73\x65\x74\x3d\x69\x73\x6f\x2d\x38\x38\x35\x39\x2d" \
+ b"\x31\x2c\x75\x74\x66\x2d\x2c\x2a\x2c\x65\x6e\x71\x3d\x30\x2e"
+
cdef extern from "zlib.h":
ctypedef void *voidp
@@ -62,10 +153,11 @@ cdef class Stream(object):
self._stream.zfree = NULL
cdef class Deflater(Stream):
- def __init__(self, level=6):
+ def __init__(self, version, level=6):
Stream.__init__(self)
deflateInit(self._stream, level)
- deflateSetDictionary(self._stream, HEADER_ZLIB_DICT, len(HEADER_ZLIB_DICT))
+ dictionary = HEADER_ZLIB_DICT_2 if version == 2 else HEADER_ZLIB_DICT_3
+ deflateSetDictionary(self._stream, dictionary, len(dictionary))
def __dealloc__(self):
deflateEnd(self._stream)
@@ -95,10 +187,12 @@ cdef class Deflater(Stream):
return bytes(buf)
cdef class Inflater(Stream):
+ cdef int version
- def __init__(self):
+ def __init__(self, version):
Stream.__init__(self)
inflateInit(self._stream)
+ self.version = version
def __dealloc__(self):
inflateEnd(self._stream)
@@ -107,6 +201,8 @@ cdef class Inflater(Stream):
def decompress(self, chunk):
self._stream.next_in = chunk
self._stream.avail_in = len(chunk)
+
+ dictionary = HEADER_ZLIB_DICT_2 if self.version == 2 else HEADER_ZLIB_DICT_3
buf = bytearray()
chunk_len = 1024 * 64
@@ -118,7 +214,7 @@ cdef class Inflater(Stream):
status = inflate(self._stream, Z_SYNC_FLUSH)
if status == Z_NEED_DICT:
- err = inflateSetDictionary(self._stream, HEADER_ZLIB_DICT, len(HEADER_ZLIB_DICT))
+ err = inflateSetDictionary(self._stream, dictionary, len(dictionary))
assert err == Z_OK
continue
@@ -128,7 +224,7 @@ cdef class Inflater(Stream):
if status == Z_STREAM_END or self._stream.avail_in == 0:
break
else:
- assert status == Z_OK
+ assert status == Z_OK, 'failed to decompress! status is ' + str(status)
return bytes(buf)
View
10 python-spdy/context.py
@@ -25,8 +25,8 @@ def __init__(self, side, version=2):
raise NotImplementedError()
self.version = version
- self.deflater = Deflater()
- self.inflater = Inflater()
+ self.deflater = Deflater(version)
+ self.inflater = Inflater(version)
self.frame_queue = []
self.input_buffer = bytearray()
@@ -71,15 +71,17 @@ def outgoing(self):
return out
def _parse_header_chunk(self, compressed_data, version):
+ print(compressed_data)
chunk = self.inflater.decompress(compressed_data)
length_size = 2 if version == 2 else 4
+ print(length_size)
headers = {}
#first two bytes: number of pairs
- num_values = int.from_bytes(chunk[0:2], 'big')
+ num_values = int.from_bytes(chunk[0:length_size], 'big')
#after that...
- cursor = 2
+ cursor = length_size
for _ in range(num_values):
#two/four bytes: length of name
name_length = int.from_bytes(chunk[cursor:cursor+length_size], 'big')
Please sign in to comment.
Something went wrong with that request. Please try again.