From cb7afe6ba4289b22cb4fd80e75091a7dffc4e74d Mon Sep 17 00:00:00 2001 From: Jarry Shaw Date: Tue, 6 Oct 2026 18:56:05 -0400 Subject: [PATCH] fix(http): HTTP.from_data raises 'invalid HTTP version: 0' - http: _make_data read a `version` key that neither HTTP/1.* nor HTTP/2 data carries; dispatch on the data class instead, and return the `version` that make() dispatches on. - httpv2: _make_data returned the frame's `length`, which ProtocolBase.__init__ took as the parse length (payload only, nine octets short), so every HTTP/2 from_data raised; make() computes the Length itself, so the key is dropped. - tests: new test_http_from_data_1154_unit; two test_http_unit cases that drove _make_data with a fake `version` key now use parsed data. Closes #1154 --- pcapkit/protocols/application/http.py | 28 +++-- pcapkit/protocols/application/httpv2.py | 5 +- .../test_http_from_data_1154_unit.py | 113 ++++++++++++++++++ tests/protocols/application/test_http_unit.py | 34 +++--- 4 files changed, 153 insertions(+), 27 deletions(-) create mode 100644 tests/protocols/application/test_http_from_data_1154_unit.py diff --git a/pcapkit/protocols/application/http.py b/pcapkit/protocols/application/http.py index de83872ac6..afff174eb1 100644 --- a/pcapkit/protocols/application/http.py +++ b/pcapkit/protocols/application/http.py @@ -255,17 +255,27 @@ def _make_data(cls, data: '_PT') -> 'dict[str, Any]': # type: ignore[override] data: protocol data Returns: - Key-value pairs for protocol construction. + Key-value pairs for protocol construction, including the ``version`` + that :meth:`make` dispatches on. + + Raises: + ProtocolError: If ``data`` is neither HTTP/1.* nor HTTP/2 data. """ - version = data.get('version', 0) - if version == 1: - from pcapkit.protocols.application.httpv1 import HTTP as protocol - elif version == 2: - from pcapkit.protocols.application.httpv2 import HTTP as protocol # type: ignore[assignment] # isort: skip - else: - raise ProtocolError(f"invalid HTTP version: {version}") - return protocol._make_data(data) # type: ignore[arg-type] + # NOTE: Neither versioned data class carries a ``version`` field -- + # HTTP/1.*'s lives in ``receipt`` as the start line's string, and HTTP/2 + # has none -- so the version is the class of ``data`` that :meth:`read` + # returned from the versioned parser. + from pcapkit.protocols.application.httpv1 import HTTP as HTTPv1 # isort: skip # pylint: disable=line-too-long,import-outside-toplevel + from pcapkit.protocols.application.httpv2 import HTTP as HTTPv2 # isort: skip # pylint: disable=line-too-long,import-outside-toplevel + from pcapkit.protocols.data.application.httpv1 import HTTP as Data_HTTPv1 # isort: skip # pylint: disable=line-too-long,import-outside-toplevel + from pcapkit.protocols.data.application.httpv2 import HTTP as Data_HTTPv2 # isort: skip # pylint: disable=line-too-long,import-outside-toplevel + + if isinstance(data, Data_HTTPv1): + return {'version': 1, **HTTPv1._make_data(data)} + if isinstance(data, Data_HTTPv2): + return {'version': 2, **HTTPv2._make_data(data)} + raise ProtocolError(f"invalid HTTP data: {type(data).__name__}") def _guess_version(self, length: 'int', **kwargs: 'Any') -> 'HTTP': """Identify the HTTP version of the payload, and parse it with that version. diff --git a/pcapkit/protocols/application/httpv2.py b/pcapkit/protocols/application/httpv2.py index 92bc6e0678..172d133ad8 100644 --- a/pcapkit/protocols/application/httpv2.py +++ b/pcapkit/protocols/application/httpv2.py @@ -464,8 +464,11 @@ def _make_data(cls, data: 'Data_HTTP') -> 'dict[str, Any]': # type: ignore[over Key-value pairs for protocol construction. """ + # NOTE: No ``length``: :meth:`make` computes the frame's Length from the + # frame it builds, and a ``length`` keyword would instead be taken by + # ``ProtocolBase.__init__`` as the number of octets to parse back -- + # the payload length alone, nine octets short of the frame. return { - 'length': data.length, 'type': data.type, 'flags': data.flags.__value__ if data.flags is not None else 0, 'sid': data.sid, diff --git a/tests/protocols/application/test_http_from_data_1154_unit.py b/tests/protocols/application/test_http_from_data_1154_unit.py new file mode 100644 index 0000000000..c82883ecda --- /dev/null +++ b/tests/protocols/application/test_http_from_data_1154_unit.py @@ -0,0 +1,113 @@ +# -*- coding: utf-8 -*- +"""``HTTP.from_data`` rebuilds the message it was parsed from. C.f. #1154. + +:meth:`HTTP._make_data ` +chose the versioned class from a ``version`` key that neither HTTP/1.* nor +HTTP/2 data carries, so every ``HTTP.from_data(http.info)`` raised +``ProtocolError: invalid HTTP version: 0``. It now dispatches on the class of +the data, and passes the ``version`` that :meth:`HTTP.make +` dispatches on. + +Behind that, :meth:`httpv2.HTTP._make_data +` returned the frame's +``length``, which ``ProtocolBase.__init__`` takes as the number of octets to +parse back -- the payload alone, nine short of the frame -- so every HTTP/2 +``from_data`` raised as well. ``make`` computes the Length itself, so the key +is no longer returned. + +The frames are the Scapy-built ones of +``test_http_guess_http2_1141_unit.LEGAL_FRAMES``, embedded as bytes. + +""" + +from __future__ import annotations + +import importlib.util +import unittest +import warnings + +from tests._support import reimport_once_per_class, time_limit + +RUNTIME_DEPS = ('tbtrim', 'aenum', 'chardet', 'dictdumper') +HAS_RUNTIME = all(importlib.util.find_spec(name) is not None for name in RUNTIME_DEPS) + +#: HTTP/1.* messages: name, octets. +HTTP1_MESSAGES = ( + ('request', b'GET /index.html HTTP/1.1\r\nHost: example.test\r\n\r\n'), + ('request with body', b'POST /form HTTP/1.0\r\nContent-Length: 4\r\n\r\nbody'), + ('response', b'HTTP/1.1 200 OK\r\nServer: test\r\n\r\nhello'), +) + +#: One HTTP/2 frame of each type :rfc:`9113` defines, with flag and stream +#: variants: name, octets. +HTTP2_FRAMES = ( + ('DATA', '00000500000000000168656c6c6f'), + ('DATA, empty, END_STREAM', '000000000100000001'), + ('DATA, PADDED', '000006000900000003047800000000'), + ('HEADERS', '00000101050000000182'), + ('HEADERS, PADDED, PRIORITY', '000009012c0000000102000000000f820000'), + ('PRIORITY', '0000050200000000050000000110'), + ('RST_STREAM', '00000403000000000100000008'), + ('SETTINGS', '000006040000000000000300000064'), + ('SETTINGS, ACK', '000000040100000000'), + ('PUSH_PROMISE', '0000050504000000010000000282'), + ('PING', '0000080600000000000000000000000001'), + ('GOAWAY, debug data', '00000b0700000000000000000100000002627965'), + ('WINDOW_UPDATE, stream 0', '000004080000000000000003e8'), + ('CONTINUATION', '00000109040000000182'), +) + + +@unittest.skipUnless(HAS_RUNTIME, 'runtime dependencies not installed') +class HTTPFromDataTests(unittest.TestCase): + """``from_data`` on the dispatcher and on ``httpv2.HTTP`` round-trips.""" + + def setUp(self) -> None: + reimport_once_per_class(self) + + def _round_trip(self, cls: 'type', wire: bytes) -> None: + with warnings.catch_warnings(): + warnings.simplefilter('ignore') + with time_limit(): + parsed = cls(wire, len(wire)) + rebuilt = cls.from_data(parsed.info) + self.assertEqual(bytes(rebuilt), wire) + self.assertEqual(rebuilt.info, parsed.info) + if hasattr(parsed, 'version'): + self.assertEqual(rebuilt.version, parsed.version) + + def test_dispatcher_rebuilds_http1_messages(self) -> None: + """The #1154 reproduction: ``invalid HTTP version: 0`` on HTTP/1.*.""" + from pcapkit.protocols.application.http import HTTP + + for name, wire in HTTP1_MESSAGES: + with self.subTest(message=name): + self._round_trip(HTTP, wire) + + def test_dispatcher_rebuilds_http2_frames(self) -> None: + """The same, for every HTTP/2 frame type.""" + from pcapkit.protocols.application.http import HTTP + + for name, octets in HTTP2_FRAMES: + with self.subTest(frame=name): + self._round_trip(HTTP, bytes.fromhex(octets)) + + def test_httpv2_rebuilds_its_own_frames(self) -> None: + """``httpv2.HTTP.from_data`` no longer parses back only Length octets.""" + from pcapkit.protocols.application.httpv2 import HTTP as HTTPv2 + + for name, octets in HTTP2_FRAMES: + with self.subTest(frame=name): + self._round_trip(HTTPv2, bytes.fromhex(octets)) + + def test_dispatcher_rejects_data_of_no_http_version(self) -> None: + """Data that is neither HTTP/1.* nor HTTP/2 is a protocol error.""" + from pcapkit.protocols.application.http import HTTP + from pcapkit.utilities.exceptions import ProtocolError + + with self.assertRaisesRegex(ProtocolError, 'invalid HTTP data'): + HTTP._make_data({'version': 1}) # type: ignore[arg-type] + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/protocols/application/test_http_unit.py b/tests/protocols/application/test_http_unit.py index 8ff00bc586..a1a6f45d0e 100644 --- a/tests/protocols/application/test_http_unit.py +++ b/tests/protocols/application/test_http_unit.py @@ -363,18 +363,22 @@ def test_http_read_explicit_version_still_wraps_a_bare_value_error(self) -> None self.assertIsNone(ctx.exception.__cause__) def test_http_make_data_delegates_to_httpv1(self) -> None: + """The version comes from the class of the parsed data (#1154). + + This used to drive ``_make_data`` with a stand-in carrying a ``version`` + key, which no parsed HTTP/1.* data has, so it pinned the lookup that made + every real ``from_data`` raise ``invalid HTTP version: 0``. + + """ from pcapkit.const.http.method import Method - from pcapkit.corekit.multidict import OrderedMultiDict from pcapkit.protocols.application.http import HTTP + from pcapkit.protocols.application.httpv1 import HTTP as HTTPv1 - data = DummyData( - version=1, - receipt=SimpleNamespace(version='1.1', method=Method.GET, uri='/index.html'), - header=OrderedMultiDict([('Host', 'example.test')]), - body=b'body', - ) + wire = b'GET /index.html HTTP/1.1\r\nHost: example.test\r\n\r\nbody' + data = HTTPv1(wire, len(wire)).info values = HTTP._make_data(data) + self.assertEqual(values['version'], 1) self.assertEqual(values['http_version'], '1.1') self.assertEqual(values['method'], Method.GET) self.assertEqual(values['uri'], '/index.html') @@ -382,21 +386,17 @@ def test_http_make_data_delegates_to_httpv1(self) -> None: self.assertEqual(values['body'], b'body') def test_http_make_data_delegates_to_httpv2(self) -> None: + """As for HTTP/1.*, from parsed data, and with no ``length`` (#1154).""" from pcapkit.const.http.frame import Frame from pcapkit.protocols.application.http import HTTP + from pcapkit.protocols.application.httpv2 import HTTP as HTTPv2 - flags = SimpleNamespace(__value__=0x01) - data = DummyData( - version=2, - length=13, - type=Frame.DATA, - flags=flags, - sid=1, - __next_type__=None, - ) + wire = bytes.fromhex('00000400010000000162796520') + data = HTTPv2(wire, len(wire)).info values = HTTP._make_data(data) - self.assertEqual(values['length'], 13) + self.assertEqual(values['version'], 2) + self.assertNotIn('length', values) self.assertEqual(values['type'], Frame.DATA) self.assertEqual(values['flags'], 0x01) self.assertEqual(values['sid'], 1)