def test_can_decode_a_story(self, story): d = Decoder() # We test against draft 9 of the HPACK spec. if story['draft'] != 9: skip("We test against draft 9, not draft %d" % story['draft']) for case in story['cases']: try: d.header_table_size = case['header_table_size'] except KeyError: pass decoded_headers = d.decode(unhexlify(case['wire'])) # The correct headers are a list of dicts, which is annoying. correct_headers = [ (item[0], item[1]) for header in case['headers'] for item in header.items() ] correct_headers = correct_headers assert correct_headers == decoded_headers assert all( isinstance(header, HeaderTuple) for header in decoded_headers )
def test_invalid_indexed_header(self): d = Decoder() # Refer to an indexed header that is too large. data = b'\xBE\x86\x84\x01\x0fwww.example.com' with pytest.raises(InvalidTableIndex): d.decode(data)
def __init__(self, state=None, length=0, flags=FLAG_NO_FLAGS, stream_id=0x0): valid_flags = reduce(lambda x, y: x | y, self.VALID_FLAGS, 0x0) if flags | valid_flags != valid_flags: raise ValueError('invalid flags detected.') if state is None: class State(object): pass state = State() state.http2_settings = HTTP2_DEFAULT_SETTINGS.copy() state.encoder = Encoder() state.decoder = Decoder() self.state = state self.length = length self.type = self.TYPE self.flags = flags self.stream_id = stream_id
def __init__(self, local_settings=None): super(H2Connection, self).__init__() self.encoder = Encoder() self.decoder = Decoder() self.streams = {} self.local_settings = Settings(local_settings) self.remote_settings = Settings() # 接收数据窗口 self.inbound_flow_control_window = self.local_settings.initial_window_size self.inbound_window_manager = WindowManager(self.local_settings.initial_window_size) # 发送数据窗口(根据对端接受能力,取决于对端设置) self.outbound_flow_control_window = self.remote_settings.initial_window_size self.__set_encoder() self.__set_decoder() # 接收数据缓冲区 self.inbound_buffer = FrameBuffer(self.local_settings.max_frame_size) # 待发送的数据缓冲区 self._data_to_send = b'' self.__dispatch_table = { SettingsFrame: self._receive_settings_frame, HeadersFrame: self._receive_headers_frame, DataFrame: self._receive_data_frame, WindowUpdateFrame: self.receive_window_update_frame, GoAwayFrame: self.receive_goaway_frame, RstStreamFrame: self._receive_rst_stream_frame, PingFrame: self._receive_ping_frame }
def test_ordering_applies_to_encoding(self, special_keys, boring_keys): """ When encoding a dictionary the special keys all appear first. """ def _prepend_colon(k): if isinstance(k, unicode): return u':' + k else: return b':' + k special_keys = set(map(_prepend_colon, special_keys)) input_dict = { k: b'testval' for k in itertools.chain(special_keys, boring_keys) } e = Encoder() d = Decoder() encoded = e.encode(input_dict) decoded = iter(d.decode(encoded, raw=True)) received_special = set() received_boring = set() expected_special = set(map(_to_bytes, special_keys)) expected_boring = set(map(_to_bytes, boring_keys)) for _ in special_keys: k, _ = next(decoded) received_special.add(k) for _ in boring_keys: k, _ = next(decoded) received_boring.add(k) assert expected_special == received_special assert expected_boring == received_boring
def test_invalid_indexed_literal(self): d = Decoder() # Refer to an index that is too large. data = b'\x82\x86\x84\x7f\x0a\x0fwww.example.com' with pytest.raises(InvalidTableIndex): d.decode(data)
def test_utf8_errors_raise_hpack_decoding_error(self): d = Decoder() # Invalid UTF-8 data. data = b'\x82\x86\x84\x01\x10www.\x07\xaa\xd7\x95\xd7\xa8\xd7\x94.com' with pytest.raises(HPACKDecodingError): d.decode(data)
def test_zero_length_header(self): """ If a header has a name of zero length it is invalid and the HPACK decoder raises a ZeroLengthHeaderNameError. """ d = Decoder(max_header_list_size=44) data = b"@\x80\x80" with pytest.raises(ZeroLengthHeaderNameError): d.decode(data)
def test_max_header_list_size(self): """ If the header block is larger than the max_header_list_size, the HPACK decoder throws an OversizedHeaderListError. """ d = Decoder(max_header_list_size=44) data = b'\x14\x0c/sample/path' with pytest.raises(OversizedHeaderListError): d.decode(data)
def test_table_size_middle_rejected(self): """ If a header table size change comes anywhere but first in the header block, it is forbidden. """ d = Decoder() data = b'\x82?a\x87\x84A\x8a\x08\x9d\\\x0b\x81p\xdcy\xa6\x99' with pytest.raises(HPACKDecodingError): d.decode(data)
def test_can_decode_multiple_header_table_size_changes(self): """ If multiple header table size changes are sent in at once, they are successfully decoded. """ d = Decoder() data = b'?a?\xe1\x1f\x82\x87\x84A\x8a\x08\x9d\\\x0b\x81p\xdcy\xa6\x99' expect = [(':method', 'GET'), (':scheme', 'https'), (':path', '/'), (':authority', '127.0.0.1:8443')] assert d.decode(data) == expect
def test_header_table_size_change_above_maximum(self): """ If a header table size change is received that exceeds the maximum allowed table size, it is rejected. """ d = Decoder() d.max_allowed_table_size = 127 data = b'?a\x82\x87\x84A\x8a\x08\x9d\\\x0b\x81p\xdcy\xa6\x99' with pytest.raises(InvalidTableSizeError): d.decode(data)
def test_table_size_not_adjusting(self): """ If the header table size is shrunk, and then the remote peer doesn't join in the shrinking, then an error is raised. """ d = Decoder() d.max_allowed_table_size = 128 data = b'\x82\x87\x84A\x8a\x08\x9d\\\x0b\x81p\xdcy\xa6\x99' with pytest.raises(InvalidTableSizeError): d.decode(data)
def test_literal_header_field_without_indexing(self): """ The header field representation uses an indexed name and a literal value. """ d = Decoder() header_set = [(':path', '/sample/path')] data = b'\x04\x0c/sample/path' assert d.decode(data) == header_set assert list(d.header_table.dynamic_entries) == []
def test_indexed_header_field(self): """ The header field representation uses an indexed header field, from the static table. """ d = Decoder() header_set = [(':method', 'GET')] data = b'\x82' assert d.decode(data) == header_set assert list(d.header_table.dynamic_entries) == []
def test_raw_decoding(self): """ The header field representation is decoded as a raw byte string instead of UTF-8 """ d = Decoder() header_set = [(b'\x00\x01\x99\x30\x11\x22\x55\x21\x89\x14', b'custom-header')] data = (b'\x40\x0a\x00\x01\x99\x30\x11\x22\x55\x21\x89\x14\x0d' b'custom-header') assert d.decode(data, raw=True) == header_set
def test_literal_header_field_with_indexing(self): """ The header field representation uses a literal name and a literal value. """ d = Decoder() header_set = [('custom-key', 'custom-header')] data = b'\x40\x0acustom-key\x0dcustom-header' assert d.decode(data) == header_set assert list(d.header_table) == [(n.encode('utf-8'), v.encode('utf-8')) for n, v in header_set]
def test_indexed_never_indexed_emits_neverindexedheadertuple(self): """ A header field with an indexed name that must never be indexed emits a NeverIndexedHeaderTuple. """ d = Decoder() data = b'\x14\x0c/sample/path' headers = d.decode(data) assert len(headers) == 1 header = headers[0] assert isinstance(header, NeverIndexedHeaderTuple)
def test_literal_never_indexed_emits_neverindexedheadertuple(self): """ A literal header field that must never be indexed emits a NeverIndexedHeaderTuple. """ d = Decoder() data = b'\x10\x0acustom-key\x0dcustom-header' headers = d.decode(data) assert len(headers) == 1 header = headers[0] assert isinstance(header, NeverIndexedHeaderTuple)
def test_literal_header_field_with_indexing_emits_headertuple(self): """ A header field with indexing emits a HeaderTuple. """ d = Decoder() data = b'\x00\x0acustom-key\x0dcustom-header' headers = d.decode(data) assert len(headers) == 1 header = headers[0] assert isinstance(header, HeaderTuple) assert not isinstance(header, NeverIndexedHeaderTuple)
def test_can_encode_a_story_with_huffman(self, raw_story): d = Decoder() e = Encoder() for case in raw_story['cases']: # The input headers are a list of dicts, which is annoying. input_headers = [(item[0], item[1]) for header in case['headers'] for item in header.items()] encoded = e.encode(input_headers, huffman=True) decoded_headers = d.decode(encoded) assert input_headers == decoded_headers
def __init__(self, client_side=True): self.state_machine = H2ConnectionStateMachine() self.streams = {} self.highest_inbound_stream_id = 0 self.highest_outbound_stream_id = 0 self.encoder = Encoder() self.decoder = Decoder() self.client_side = client_side # Objects that store settings, including defaults. self.local_settings = Settings(client=client_side) self.remote_settings = Settings(client=not client_side) # The curent value of the connection flow control windows on the # connection. self.outbound_flow_control_window = ( self.remote_settings.initial_window_size) self.inbound_flow_control_window = ( self.local_settings.initial_window_size) #: The maximum size of a frame that can be emitted by this peer, in #: bytes. self.max_outbound_frame_size = self.remote_settings.max_frame_size #: The maximum size of a frame that can be received by this peer, in #: bytes. self.max_inbound_frame_size = self.local_settings.max_frame_size # Buffer for incoming data. self.incoming_buffer = FrameBuffer(server=not client_side) # A private variable to store a sequence of received header frames # until completion. self._header_frames = [] # Data that needs to be sent. self._data_to_send = b'' # When in doubt use dict-dispatch. self._frame_dispatch_table = { HeadersFrame: self._receive_headers_frame, PushPromiseFrame: self._receive_push_promise_frame, SettingsFrame: self._receive_settings_frame, DataFrame: self._receive_data_frame, WindowUpdateFrame: self._receive_window_update_frame, PingFrame: self._receive_ping_frame, RstStreamFrame: self._receive_rst_stream_frame, PriorityFrame: self._receive_priority_frame, GoAwayFrame: self._receive_goaway_frame, ContinuationFrame: self._receive_naked_continuation, }
def test_truncated_header_name(self): """ If a header name is truncated an error is raised. """ d = Decoder() # This is a simple header block that has a bad ending. The interesting # part begins on the second line. This indicates a string that has # literal name and value. The name is a 5 character huffman-encoded # string that is only three bytes long. data = (b'\x82\x87\x84A\x8a\x08\x9d\\\x0b\x81p\xdcy\xa6\x99' b'\x00\x85\xf2\xb2J') with pytest.raises(HPACKDecodingError): d.decode(data)
def test_truncated_header_value(self): """ If a header value is truncated an error is raised. """ d = Decoder() # This is a simple header block that has a bad ending. The interesting # part begins on the second line. This indicates a string that has # literal name and value. The name is a 5 character huffman-encoded # string, but the entire EOS character has been written over the end. # This causes hpack to see the header value as being supposed to be # 622462 bytes long, which it clearly is not, and so this must fail. data = (b'\x82\x87\x84A\x8a\x08\x9d\\\x0b\x81p\xdcy\xa6\x99' b'\x00\x85\xf2\xb2J\x87\xff\xff\xff\xfd%B\x7f') with pytest.raises(HPACKDecodingError): d.decode(data)
def test_can_encode_a_story_no_huffman(self, raw_story): d = Decoder() e = Encoder() for case in raw_story['cases']: # The input headers are a list of dicts, which is annoying. input_headers = [ (item[0], item[1]) for header in case['headers'] for item in header.items() ] encoded = e.encode(input_headers, huffman=False) decoded_headers = d.decode(encoded) assert input_headers == decoded_headers assert all( isinstance(header, HeaderTuple) for header in decoded_headers )
def test_resizing_header_table(self): # We need to decode a substantial number of headers, to populate the # header table. This string isn't magic: it's the output from the # equivalent test for the Encoder. d = Decoder() data = ( b'\x82\x87D\x87a\x07\xa4\xacV4\xcfA\x8c\xf1\xe3\xc2\xe5\xf2:k\xa0' b'\xab\x90\xf4\xff@\x88%\xa8I\xe9[\xa9}\x7f\x89%\xa8I\xe9[\xb8\xe8' b'\xb4\xbfz\xbc\xd0\x7ff\xa2\x81\xb0\xda\xe0S\xfa\xd02\x1a\xa4\x9d' b'\x13\xfd\xa9\x92\xa4\x96\x854\x0c\x8aj\xdc\xa7\xe2\x81\x02\xef}' b'\xa9g{\x81qp\x7fjb):\x9d\x81\x00 \x00@\x150\x9a\xc2\xca\x7f,\x05' b'\xc5\xc1S\xb0I|\xa5\x89\xd3M\x1fC\xae\xba\x0cA\xa4\xc7\xa9\x8f3' b'\xa6\x9a?\xdf\x9ah\xfa\x1du\xd0b\r&=Ly\xa6\x8f\xbe\xd0\x01w\xfe' b'\xbeX\xf9\xfb\xed\x00\x17{@\x8a\xfc[=\xbdF\x81\xad\xbc\xa8O\x84y' b'\xe7\xde\x7f') d.decode(data) # Resize the header table to a size so small that nothing can be in it. d.header_table_size = 40 assert len(d.header_table.dynamic_entries) == 0
def __init__( self, tcp_handler=None, rfile=None, wfile=None, is_server=False, dump_frames=False, encoder=None, decoder=None, unhandled_frame_cb=None, ): self.tcp_handler = tcp_handler or TCPHandler(rfile, wfile) self.is_server = is_server self.dump_frames = dump_frames self.encoder = encoder or Encoder() self.decoder = decoder or Decoder() self.unhandled_frame_cb = unhandled_frame_cb self.http2_settings = self.HTTP2_DEFAULT_SETTINGS.copy() self.current_stream_id = None self.connection_preface_performed = False
def test_resizing_header_table(self): # We need to decode a substantial number of headers, to populate the # header table. This string isn't magic: it's the output from the # equivalent test for the Encoder. d = Decoder() data = ( b'\x82\x88F\x87\x087A\x07"9\xffC\x8b\xdbm\x88>h\xd1\xcb\x12%' + b'\xba\x7f\x00\x88N\xb0\x8bt\x97\x90\xfa\x7f\x89N\xb0\x8bt\x97\x9a' + b'\x17\xa8\xff|\xbe\xefo\xaa\x96\xb4\x05\x04/G\xfa\xefBT\xc8\xb6' + b'\x19\xf5t|\x19\x11_Gz\x13\xd1\xf4\xf0\xe8\xfd\xf4\x18\xa4\xaf' + b'\xab\xa1\xfc\xfd\x86\xa4\x85\xff}\x1e\xe1O&\x81\xcab\x94\xc57G' + b'\x05<qo\x98\x1a\x92\x17U\xaf\x88\xf9\xc43\x8e\x8b\xe9C\x9c\xb5' + b'%\x11SX\x1ey\xc7E\xff\xcf=\x17\xd2\x879jJ"\xa6\xb0<\xf4_W\x95' + b'\xa5%\x9d?\xd0\x7f]^V\x94\x95\xff\x00\x8a\xfd\xcb\xf2\xd7\x92 ' + b'\x89|F\x11\x84\xae\xbb+\xb3') d.decode(data) # Resize the header table to a size so small that nothing can be in it. d.header_table_size = 40 assert len(d.header_table) == 0
def test_apache_trafficserver(self): # This test reproduces the bug in #110, using exactly the same header # data. d = Decoder() data = ( b'\x10\x07:status\x03200@\x06server\tATS/6.0.0' b'@\x04date\x1dTue, 31 Mar 2015 08:09:51 GMT' b'@\x0ccontent-type\ttext/html@\x0econtent-length\x0542468' b'@\rlast-modified\x1dTue, 31 Mar 2015 01:55:51 GMT' b'@\x04vary\x0fAccept-Encoding@\x04etag\x0f"5519fea7-a5e4"' b'@\x08x-served\x05Nginx@\x14x-subdomain-tryfiles\x04True' b'@\x07x-deity\thydra-lts@\raccept-ranges\x05bytes@\x03age\x010' b'@\x19strict-transport-security\rmax-age=86400' b'@\x03via2https/1.1 ATS (ApacheTrafficServer/6.0.0 [cSsNfU])') expect = [ (':status', '200'), ('server', 'ATS/6.0.0'), ('date', 'Tue, 31 Mar 2015 08:09:51 GMT'), ('content-type', 'text/html'), ('content-length', '42468'), ('last-modified', 'Tue, 31 Mar 2015 01:55:51 GMT'), ('vary', 'Accept-Encoding'), ('etag', '"5519fea7-a5e4"'), ('x-served', 'Nginx'), ('x-subdomain-tryfiles', 'True'), ('x-deity', 'hydra-lts'), ('accept-ranges', 'bytes'), ('age', '0'), ('strict-transport-security', 'max-age=86400'), ('via', 'https/1.1 ATS (ApacheTrafficServer/6.0.0 [cSsNfU])'), ] result = d.decode(data) assert result == expect # The status header shouldn't be indexed. assert len(d.header_table.dynamic_entries) == len(expect) - 1
def test_request_examples_with_huffman(self): """ This section shows the same examples as the previous section, but using Huffman encoding for the literal values. """ d = Decoder() first_header_set = [ ( ':method', 'GET', ), ( ':scheme', 'http', ), ( ':path', '/', ), (':authority', 'www.example.com'), ] first_data = ( b'\x82\x86\x84\x01\x8c\xf1\xe3\xc2\xe5\xf2:k\xa0\xab\x90\xf4\xff') assert d.decode(first_data) == first_header_set assert list(d.header_table.dynamic_entries) == [] second_header_set = [ ( ':method', 'GET', ), ( ':scheme', 'http', ), ( ':path', '/', ), ( ':authority', 'www.example.com', ), ('cache-control', 'no-cache'), ] second_data = ( b'\x82\x86\x84\x01\x8c\xf1\xe3\xc2\xe5\xf2:k\xa0\xab\x90\xf4\xff' b'\x0f\t\x86\xa8\xeb\x10d\x9c\xbf') assert d.decode(second_data) == second_header_set assert list(d.header_table.dynamic_entries) == [] third_header_set = [ ( ':method', 'GET', ), ( ':scheme', 'https', ), ( ':path', '/index.html', ), ( ':authority', 'www.example.com', ), ('custom-key', 'custom-value'), ] third_data = ( b'\x82\x87\x85\x01\x8c\xf1\xe3\xc2\xe5\xf2:k\xa0\xab\x90\xf4\xff@' b'\x88%\xa8I\xe9[\xa9}\x7f\x89%\xa8I\xe9[\xb8\xe8\xb4\xbf') assert d.decode(third_data) == third_header_set assert len(d.header_table.dynamic_entries) == 1