Skip to content

Commit 9d87abc

Browse files
authored
fix(httpv2): HTTP/2 frames lose octets on rebuild (#1242-#1248, #1251) (#1341)
- Schema: bound the frame payload by its declared Length, and keep what follows as a `payload` dispatched as the next layer (#1243). Name the reserved R bit of the header, PUSH_PROMISE, GOAWAY and WINDOW_UPDATE stream fields (#1246). Keep undefined flag bits in `__flags__` (#1245). - Data: add `reserved`, `promised_reserved`, `last_reserved` and `increment_reserved`; frame types with no defined flags now carry a bare `Flags` holding the raw octet instead of `None`. - Makers: PADDED follows the flag, not a non-zero Pad Length, with a new `padded` keyword (#1244); `make` ORs its `flags` with the frame's; new `reserved` and `payload` keywords; PING opaque data must be 8 octets (#1251, HTTP/2 half). - Reader: SETTINGS keeps repeated identifiers in order in a new `Settings` multidict whose index answers the last value (#1247); `make` with raw frame octets parses them back with the frame schema (#1248). - HTTP dispatcher: record the connection preface as `preface` and rebuild it, also through an explicit `version=2` (#1242). Closes #1242 Closes #1243 Closes #1244 Closes #1245 Closes #1246 Closes #1247 Closes #1248 Refs #1251
1 parent 13aa677 commit 9d87abc

6 files changed

Lines changed: 484 additions & 83 deletions

File tree

‎pcapkit/protocols/application/http.py‎

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import struct
1919
from typing import TYPE_CHECKING, Generic
2020

21+
from pcapkit.corekit.protochain import ProtoChain
2122
from pcapkit.protocols.application.application import Application
23+
from pcapkit.protocols.misc.null import NoPayload
2224
from pcapkit.protocols.protocol import _PT, _ST
2325
from pcapkit.utilities.exceptions import ProtocolError
2426

@@ -167,22 +169,30 @@ def id(cls) -> 'tuple[Literal["HTTP"], Literal["HTTPv1"], Literal["HTTPv2"]]':
167169
return ('HTTP', 'HTTPv1', 'HTTPv2')
168170

169171
def read(self, length: 'Optional[int]' = None, *,
170-
version: 'Optional[Literal[1, 2]]' = None, **kwargs: 'Any') -> '_PT':
172+
version: 'Optional[Literal[1, 2]]' = None,
173+
preface: 'bool' = False, # pylint: disable=unused-argument
174+
**kwargs: 'Any') -> '_PT':
171175
"""Read (parse) packet data.
172176
173177
Args:
174178
length: Length of packet data.
175179
version: Version of HTTP.
180+
preface: Construction keyword of :meth:`make`; whether a preface is
181+
parsed is decided by the octets alone.
176182
**kwargs: Arbitrary keyword arguments.
177183
178184
Returns:
179-
Parsed packet data.
185+
Parsed packet data. When the payload opens with the HTTP/2
186+
connection preface, its 24 octets are recorded as ``preface``.
180187
181188
"""
182189
if length is None:
183190
length = len(self)
184191

185-
if version is None:
192+
# An explicit ``version=2`` -- what :meth:`_make_data` hands back for a
193+
# rebuild -- may still open with the connection preface, and the
194+
# identification in :meth:`_guess_version` is what skips it.
195+
if version is None or (version == 2 and self._test_preface(length)):
186196
http = self._guess_version(length, **kwargs)
187197
else:
188198
if version == 1:
@@ -207,21 +217,44 @@ def read(self, length: 'Optional[int]' = None, *,
207217
self._version = http.version
208218
self._length = http.length + self._preface_length
209219
self._http = http
210-
return http.info
220+
221+
info = http.info
222+
if self._preface_length:
223+
info.__update__({
224+
'preface': _HTTP2_PREFACE,
225+
})
226+
227+
# The versioned parser may have kept octets after its message as the
228+
# next layer, e.g. the frames after an HTTP/2 frame.
229+
next_ = getattr(http, '_next', None)
230+
if next_ is not None and not isinstance(next_, NoPayload):
231+
self._next = next_
232+
self._protos = ProtoChain(self.__class__, self.alias, basis=next_.protochain)
233+
return info
211234

212235
def make(self,
213-
version: 'Literal[1, 2]' = 1,
236+
version: 'Literal[1, 2]' = 1, *,
237+
preface: 'bool' = False,
214238
**kwargs: 'Any') -> '_ST':
215239
"""Make (construct) packet data.
216240
217241
Args:
218242
version: Version of HTTP.
243+
preface: Whether the HTTP/2 connection preface goes ahead of the
244+
frame; :meth:`pack` writes it.
219245
**kwargs: Arbitrary keyword arguments.
220246
221247
Returns:
222248
bytes: Constructed packet data.
223249
250+
Raises:
251+
ProtocolError: If ``version`` is unknown, or if ``preface`` is
252+
requested for a version other than HTTP/2.
253+
224254
"""
255+
if preface and version != 2:
256+
raise ProtocolError(f'HTTP/{version}: connection preface is HTTP/2 only')
257+
225258
if version == 1:
226259
from pcapkit.protocols.application.httpv1 import HTTP as protocol # isort: skip # pylint: disable=line-too-long,import-outside-toplevel
227260
elif version == 2:
@@ -243,6 +276,22 @@ def make(self,
243276
# ``make`` override that did would need a different dispatch here.
244277
return protocol.__new__(protocol).make(**kwargs) # type: ignore[return-value]
245278

279+
def pack(self, **kwargs: 'Any') -> 'bytes':
280+
"""Pack (construct) packet data.
281+
282+
Args:
283+
**kwargs: Arbitrary keyword arguments.
284+
285+
Returns:
286+
Constructed packet data, with the HTTP/2 connection preface ahead
287+
of the frame if ``preface`` is set.
288+
289+
"""
290+
data = super().pack(**kwargs)
291+
if kwargs.get('preface', False):
292+
return _HTTP2_PREFACE + data
293+
return data
294+
246295
##########################################################################
247296
# Utilities.
248297
##########################################################################
@@ -274,9 +323,22 @@ def _make_data(cls, data: '_PT') -> 'dict[str, Any]': # type: ignore[override]
274323
if isinstance(data, Data_HTTPv1):
275324
return {'version': 1, **HTTPv1._make_data(data)}
276325
if isinstance(data, Data_HTTPv2):
277-
return {'version': 2, **HTTPv2._make_data(data)}
326+
return {'version': 2, 'preface': bool(data.get('preface')), **HTTPv2._make_data(data)}
278327
raise ProtocolError(f"invalid HTTP data: {type(data).__name__}")
279328

329+
def _test_preface(self, length: 'int') -> 'bool':
330+
"""Test whether the first ``length`` octets open with the HTTP/2 connection preface.
331+
332+
Args:
333+
length: Length of packet data.
334+
335+
Returns:
336+
:data:`True` if they do.
337+
338+
"""
339+
preface_len = len(_HTTP2_PREFACE)
340+
return length >= preface_len and self._data[:preface_len] == _HTTP2_PREFACE
341+
280342
def _guess_version(self, length: 'int', **kwargs: 'Any') -> 'HTTP':
281343
"""Identify the HTTP version of the payload, and parse it with that version.
282344
@@ -344,7 +406,7 @@ def _guess_version(self, length: 'int', **kwargs: 'Any') -> 'HTTP':
344406
# caller may hand this method fewer octets than the buffer holds, and a
345407
# preface must not be claimed out of octets outside this payload.
346408
preface_len = len(_HTTP2_PREFACE)
347-
if length >= preface_len and self._data[:preface_len] == _HTTP2_PREFACE:
409+
if self._test_preface(length):
348410
from pcapkit.protocols.application.httpv2 import HTTP as HTTPv2 # isort: skip # pylint: disable=line-too-long,import-outside-toplevel
349411

350412
# The preface is not a frame -- the frames begin after it

0 commit comments

Comments
 (0)