Esempio n. 1
0
	def _encode_header_chunk(self, headers):
		chunk = bytearray()

		#first two bytes: number of pairs
		#chunk.extend(len(headers).to_bytes(2, 'big'))
		chunk.extend(struct.pack('>H', len(headers)))

		#after that...
		for name, value in sorted(headers.items()):
			#name = bytes(name, 'UTF-8')
			name = name.encode('UTF-8')

			#value = bytes(value, 'UTF-8')
			value = value.encode('UTF-8')

			#two bytes: length of name
			#chunk.extend(len(name).to_bytes(2, 'big'))
			chunk.extend(struct.pack('>H', len(name)))

			#next name_length bytes: name
			chunk.extend(name)

			#two bytes: length of value
			#chunk.extend(len(value).to_bytes(2, 'big'))
			chunk.extend(struct.pack('>H', len(value)))

			#next value_length bytes: value
			chunk.extend(value)

		#return self.deflater.compress(bytes(chunk))
#		print 'headers', sorted(headers.items())
#		print 'decoded_headers', repr(chunk)
		compressed_headers = compress(str(chunk), level=6, dictionary=HEADER_ZLIB_DICT_2)
#		print 'compressed_headers', repr(compressed_headers)
		return compressed_headers[:-1] # Don't know why -1
Esempio n. 2
0
 def asBinary(self, compressed=True):
     hdr_tuples = []
     for name, values in self._rawHeaders.items():
         hdr_tuples.extend([(name, value,) for value in values])
     hdr_tuples.sort()
     fmt = ["!H"]
     args = [len(hdr_tuples)]
     for n, v in hdr_tuples:
         # TODO: check for overflowing n, v lengths
         fmt.append("H%dsH%ds" % (len(n), len(v)))
         args.extend([len(n), n, len(v), v])
     hdrs = struct.pack("".join(fmt), *args)
     if compressed:
         return compress(hdrs, dictionary=dictionary)
     return hdrs