frame.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. """
  2. Framing logic for HTTP/2.
  3. Provides both classes to represent framed
  4. data and logic for aiding the connection when it comes to reading from the
  5. socket.
  6. """
  7. from __future__ import annotations
  8. import binascii
  9. import struct
  10. from typing import TYPE_CHECKING, Any
  11. if TYPE_CHECKING:
  12. from collections.abc import Iterable # pragma: no cover
  13. from .exceptions import InvalidDataError, InvalidFrameError, InvalidPaddingError, UnknownFrameError
  14. from .flags import Flag, Flags
  15. # The maximum initial length of a frame. Some frames have shorter maximum
  16. # lengths.
  17. FRAME_MAX_LEN = (2 ** 14)
  18. # The maximum allowed length of a frame.
  19. FRAME_MAX_ALLOWED_LEN = (2 ** 24) - 1
  20. # Stream association enumerations.
  21. _STREAM_ASSOC_HAS_STREAM = "has-stream"
  22. _STREAM_ASSOC_NO_STREAM = "no-stream"
  23. _STREAM_ASSOC_EITHER = "either"
  24. # Structs for packing and unpacking
  25. _STRUCT_HBBBL = struct.Struct(">HBBBL")
  26. _STRUCT_LL = struct.Struct(">LL")
  27. _STRUCT_HL = struct.Struct(">HL")
  28. _STRUCT_LB = struct.Struct(">LB")
  29. _STRUCT_L = struct.Struct(">L")
  30. _STRUCT_H = struct.Struct(">H")
  31. _STRUCT_B = struct.Struct(">B")
  32. class Frame:
  33. """
  34. The base class for all HTTP/2 frames.
  35. """
  36. #: The flags defined on this type of frame.
  37. defined_flags: list[Flag] = []
  38. #: The byte used to define the type of the frame.
  39. type: int | None = None
  40. # If 'has-stream', the frame's stream_id must be non-zero. If 'no-stream',
  41. # it must be zero. If 'either', it's not checked.
  42. stream_association: str | None = None
  43. def __init__(self, stream_id: int, flags: Iterable[str] = ()) -> None:
  44. #: The stream identifier for the stream this frame was received on.
  45. #: Set to 0 for frames sent on the connection (stream-id 0).
  46. self.stream_id = stream_id
  47. #: The flags set for this frame.
  48. self.flags = Flags(self.defined_flags)
  49. #: The frame length, excluding the nine-byte header.
  50. self.body_len = 0
  51. for flag in flags:
  52. self.flags.add(flag)
  53. if not self.stream_id and self.stream_association == _STREAM_ASSOC_HAS_STREAM:
  54. msg = f"Stream ID must be non-zero for {type(self).__name__}"
  55. raise InvalidDataError(msg)
  56. if self.stream_id and self.stream_association == _STREAM_ASSOC_NO_STREAM:
  57. msg = f"Stream ID must be zero for {type(self).__name__} with stream_id={self.stream_id}"
  58. raise InvalidDataError(msg)
  59. def __repr__(self) -> str:
  60. return (
  61. f"{type(self).__name__}(stream_id={self.stream_id}, flags={self.flags!r}): {self._body_repr()}"
  62. )
  63. def _body_repr(self) -> str:
  64. # More specific implementation may be provided by subclasses of Frame.
  65. # This fallback shows the serialized (and truncated) body content.
  66. return _raw_data_repr(self.serialize_body())
  67. @staticmethod
  68. def explain(data: memoryview) -> tuple[Frame, int]:
  69. """
  70. Takes a bytestring and tries to parse a single frame and print it.
  71. This function is only provided for debugging purposes.
  72. :param data: A memoryview object containing the raw data of at least
  73. one complete frame (header and body).
  74. .. versionadded:: 6.0.0
  75. """
  76. frame, length = Frame.parse_frame_header(data[:9])
  77. frame.parse_body(data[9:9 + length])
  78. print(frame) # noqa: T201
  79. return frame, length
  80. @staticmethod
  81. def parse_frame_header(header: memoryview, strict: bool = False) -> tuple[Frame, int]:
  82. """
  83. Takes a 9-byte frame header and returns a tuple of the appropriate
  84. Frame object and the length that needs to be read from the socket.
  85. This populates the flags field, and determines how long the body is.
  86. :param header: A memoryview object containing the 9-byte frame header
  87. data of a frame. Must not contain more or less.
  88. :param strict: Whether to raise an exception when encountering a frame
  89. not defined by spec and implemented by hyperframe.
  90. :raises hyperframe.exceptions.UnknownFrameError: If a frame of unknown
  91. type is received.
  92. .. versionchanged:: 5.0.0
  93. Added ``strict`` parameter to accommodate :class:`ExtensionFrame`
  94. """
  95. try:
  96. fields = _STRUCT_HBBBL.unpack(header)
  97. except struct.error as err:
  98. msg = "Invalid frame header"
  99. raise InvalidFrameError(msg) from err
  100. # First 24 bits are frame length.
  101. length = (fields[0] << 8) + fields[1]
  102. typ_e = fields[2]
  103. flags = fields[3]
  104. stream_id = fields[4] & 0x7FFFFFFF
  105. try:
  106. frame = FRAMES[typ_e](stream_id)
  107. except KeyError as err:
  108. if strict:
  109. raise UnknownFrameError(typ_e, length) from err
  110. frame = ExtensionFrame(type=typ_e, stream_id=stream_id)
  111. frame.parse_flags(flags)
  112. return (frame, length)
  113. def parse_flags(self, flag_byte: int) -> Flags:
  114. for flag, flag_bit in self.defined_flags:
  115. if flag_byte & flag_bit:
  116. self.flags.add(flag)
  117. return self.flags
  118. def serialize(self) -> bytes:
  119. """
  120. Convert a frame into a bytestring, representing the serialized form of
  121. the frame.
  122. """
  123. body = self.serialize_body()
  124. self.body_len = len(body)
  125. # Build the common frame header.
  126. # First, get the flags.
  127. flags = 0
  128. for flag, flag_bit in self.defined_flags:
  129. if flag in self.flags:
  130. flags |= flag_bit
  131. header = _STRUCT_HBBBL.pack(
  132. (self.body_len >> 8) & 0xFFFF, # Length spread over top 24 bits
  133. self.body_len & 0xFF,
  134. self.type,
  135. flags,
  136. self.stream_id & 0x7FFFFFFF, # Stream ID is 32 bits.
  137. )
  138. return header + body
  139. def serialize_body(self) -> bytes:
  140. raise NotImplementedError
  141. def parse_body(self, data: memoryview) -> None:
  142. """
  143. Given the body of a frame, parses it into frame data. This populates
  144. the non-header parts of the frame: that is, it does not populate the
  145. stream ID or flags.
  146. :param data: A memoryview object containing the body data of the frame.
  147. Must not contain *more* data than the length returned by
  148. :meth:`parse_frame_header
  149. <hyperframe.frame.Frame.parse_frame_header>`.
  150. """
  151. raise NotImplementedError
  152. class Padding:
  153. """
  154. Mixin for frames that contain padding. Defines extra fields that can be
  155. used and set by frames that can be padded.
  156. """
  157. def __init__(self, stream_id: int, pad_length: int = 0, **kwargs: Any) -> None:
  158. super().__init__(stream_id, **kwargs) # type: ignore
  159. #: The length of the padding to use.
  160. self.pad_length = pad_length
  161. def serialize_padding_data(self) -> bytes:
  162. if "PADDED" in self.flags: # type: ignore
  163. return _STRUCT_B.pack(self.pad_length)
  164. return b""
  165. def parse_padding_data(self, data: memoryview) -> int:
  166. if "PADDED" in self.flags: # type: ignore
  167. try:
  168. self.pad_length = struct.unpack("!B", data[:1])[0]
  169. except struct.error as err:
  170. msg = "Invalid Padding data"
  171. raise InvalidFrameError(msg) from err
  172. return 1
  173. return 0
  174. #: .. deprecated:: 5.2.1
  175. #: Use self.pad_length instead.
  176. @property
  177. def total_padding(self) -> int: # pragma: no cover
  178. import warnings
  179. warnings.warn(
  180. "total_padding contains the same information as pad_length.",
  181. DeprecationWarning,
  182. stacklevel=2,
  183. )
  184. return self.pad_length
  185. class Priority:
  186. """
  187. Mixin for frames that contain priority data. Defines extra fields that can
  188. be used and set by frames that contain priority data.
  189. """
  190. def __init__(self,
  191. stream_id: int,
  192. depends_on: int = 0x0,
  193. stream_weight: int = 0x0,
  194. exclusive: bool = False,
  195. **kwargs: Any) -> None:
  196. super().__init__(stream_id, **kwargs) # type: ignore
  197. #: The stream ID of the stream on which this stream depends.
  198. self.depends_on = depends_on
  199. #: The weight of the stream. This is an integer between 0 and 256.
  200. self.stream_weight = stream_weight
  201. #: Whether the exclusive bit was set.
  202. self.exclusive = exclusive
  203. def serialize_priority_data(self) -> bytes:
  204. return _STRUCT_LB.pack(
  205. self.depends_on + (0x80000000 if self.exclusive else 0),
  206. self.stream_weight,
  207. )
  208. def parse_priority_data(self, data: memoryview) -> int:
  209. try:
  210. self.depends_on, self.stream_weight = _STRUCT_LB.unpack(data[:5])
  211. except struct.error as err:
  212. msg = "Invalid Priority data"
  213. raise InvalidFrameError(msg) from err
  214. self.exclusive = bool(self.depends_on >> 31)
  215. self.depends_on &= 0x7FFFFFFF
  216. return 5
  217. class DataFrame(Padding, Frame):
  218. """
  219. DATA frames convey arbitrary, variable-length sequences of octets
  220. associated with a stream. One or more DATA frames are used, for instance,
  221. to carry HTTP request or response payloads.
  222. """
  223. #: The flags defined for DATA frames.
  224. defined_flags = [
  225. Flag("END_STREAM", 0x01),
  226. Flag("PADDED", 0x08),
  227. ]
  228. #: The type byte for data frames.
  229. type = 0x0
  230. stream_association = _STREAM_ASSOC_HAS_STREAM
  231. def __init__(self, stream_id: int, data: bytes = b"", **kwargs: Any) -> None:
  232. super().__init__(stream_id, **kwargs)
  233. #: The data contained on this frame.
  234. self.data = data
  235. def serialize_body(self) -> bytes:
  236. padding_data = self.serialize_padding_data()
  237. padding = b"\0" * self.pad_length
  238. if isinstance(self.data, memoryview):
  239. self.data = self.data.tobytes()
  240. return b"".join([padding_data, self.data, padding])
  241. def parse_body(self, data: memoryview) -> None:
  242. padding_data_length = self.parse_padding_data(data)
  243. self.data = (
  244. data[padding_data_length:len(data)-self.pad_length].tobytes()
  245. )
  246. self.body_len = len(data)
  247. if self.pad_length and self.pad_length >= self.body_len:
  248. msg = "Padding is too long."
  249. raise InvalidPaddingError(msg)
  250. @property
  251. def flow_controlled_length(self) -> int:
  252. """
  253. The length of the frame that needs to be accounted for when considering
  254. flow control.
  255. """
  256. padding_len = 0
  257. if "PADDED" in self.flags:
  258. # Account for extra 1-byte padding length field, which is still
  259. # present if possibly zero-valued.
  260. padding_len = self.pad_length + 1
  261. return len(self.data) + padding_len
  262. class PriorityFrame(Priority, Frame):
  263. """
  264. The PRIORITY frame specifies the sender-advised priority of a stream. It
  265. can be sent at any time for an existing stream. This enables
  266. reprioritisation of existing streams.
  267. """
  268. #: The flags defined for PRIORITY frames.
  269. defined_flags: list[Flag] = []
  270. #: The type byte defined for PRIORITY frames.
  271. type = 0x02
  272. stream_association = _STREAM_ASSOC_HAS_STREAM
  273. def _body_repr(self) -> str:
  274. return f"exclusive={self.exclusive}, depends_on={self.depends_on}, stream_weight={self.stream_weight}"
  275. def serialize_body(self) -> bytes:
  276. return self.serialize_priority_data()
  277. def parse_body(self, data: memoryview) -> None:
  278. if len(data) > 5:
  279. msg = f"PRIORITY must have 5 byte body: actual length {len(data)}."
  280. raise InvalidFrameError(msg)
  281. self.parse_priority_data(data)
  282. self.body_len = 5
  283. class RstStreamFrame(Frame):
  284. """
  285. The RST_STREAM frame allows for abnormal termination of a stream. When sent
  286. by the initiator of a stream, it indicates that they wish to cancel the
  287. stream or that an error condition has occurred. When sent by the receiver
  288. of a stream, it indicates that either the receiver is rejecting the stream,
  289. requesting that the stream be cancelled or that an error condition has
  290. occurred.
  291. """
  292. #: The flags defined for RST_STREAM frames.
  293. defined_flags: list[Flag] = []
  294. #: The type byte defined for RST_STREAM frames.
  295. type = 0x03
  296. stream_association = _STREAM_ASSOC_HAS_STREAM
  297. def __init__(self, stream_id: int, error_code: int = 0, **kwargs: Any) -> None:
  298. super().__init__(stream_id, **kwargs)
  299. #: The error code used when resetting the stream.
  300. self.error_code = error_code
  301. def _body_repr(self) -> str:
  302. return f"error_code={self.error_code}"
  303. def serialize_body(self) -> bytes:
  304. return _STRUCT_L.pack(self.error_code)
  305. def parse_body(self, data: memoryview) -> None:
  306. if len(data) != 4:
  307. msg = f"RST_STREAM must have 4 byte body: actual length {len(data)}."
  308. raise InvalidFrameError(msg)
  309. try:
  310. self.error_code = _STRUCT_L.unpack(data)[0]
  311. except struct.error as err: # pragma: no cover
  312. msg = "Invalid RST_STREAM body"
  313. raise InvalidFrameError(msg) from err
  314. self.body_len = 4
  315. class SettingsFrame(Frame):
  316. """
  317. The SETTINGS frame conveys configuration parameters that affect how
  318. endpoints communicate. The parameters are either constraints on peer
  319. behavior or preferences.
  320. Settings are not negotiated. Settings describe characteristics of the
  321. sending peer, which are used by the receiving peer. Different values for
  322. the same setting can be advertised by each peer. For example, a client
  323. might set a high initial flow control window, whereas a server might set a
  324. lower value to conserve resources.
  325. """
  326. #: The flags defined for SETTINGS frames.
  327. defined_flags = [Flag("ACK", 0x01)]
  328. #: The type byte defined for SETTINGS frames.
  329. type = 0x04
  330. stream_association = _STREAM_ASSOC_NO_STREAM
  331. # We need to define the known settings, they may as well be class
  332. # attributes.
  333. #: The byte that signals the SETTINGS_HEADER_TABLE_SIZE setting.
  334. HEADER_TABLE_SIZE = 0x01
  335. #: The byte that signals the SETTINGS_ENABLE_PUSH setting.
  336. ENABLE_PUSH = 0x02
  337. #: The byte that signals the SETTINGS_MAX_CONCURRENT_STREAMS setting.
  338. MAX_CONCURRENT_STREAMS = 0x03
  339. #: The byte that signals the SETTINGS_INITIAL_WINDOW_SIZE setting.
  340. INITIAL_WINDOW_SIZE = 0x04
  341. #: The byte that signals the SETTINGS_MAX_FRAME_SIZE setting.
  342. MAX_FRAME_SIZE = 0x05
  343. #: The byte that signals the SETTINGS_MAX_HEADER_LIST_SIZE setting.
  344. MAX_HEADER_LIST_SIZE = 0x06
  345. #: The byte that signals SETTINGS_ENABLE_CONNECT_PROTOCOL setting.
  346. ENABLE_CONNECT_PROTOCOL = 0x08
  347. def __init__(self, stream_id: int = 0, settings: dict[int, int] | None = None, **kwargs: Any) -> None:
  348. super().__init__(stream_id, **kwargs)
  349. if settings and "ACK" in kwargs.get("flags", ()):
  350. msg = "Settings must be empty if ACK flag is set."
  351. raise InvalidDataError(msg)
  352. #: A dictionary of the setting type byte to the value of the setting.
  353. self.settings: dict[int, int] = settings or {}
  354. def _body_repr(self) -> str:
  355. return f"settings={self.settings}"
  356. def serialize_body(self) -> bytes:
  357. return b"".join([_STRUCT_HL.pack(setting & 0xFF, value)
  358. for setting, value in self.settings.items()])
  359. def parse_body(self, data: memoryview) -> None:
  360. if "ACK" in self.flags and len(data) > 0:
  361. msg = f"SETTINGS ack frame must not have payload: got {len(data)} bytes"
  362. raise InvalidDataError(msg)
  363. body_len = 0
  364. for i in range(0, len(data), 6):
  365. try:
  366. name, value = _STRUCT_HL.unpack(data[i:i+6])
  367. except struct.error as err:
  368. msg = "Invalid SETTINGS body"
  369. raise InvalidFrameError(msg) from err
  370. self.settings[name] = value
  371. body_len += 6
  372. self.body_len = body_len
  373. class PushPromiseFrame(Padding, Frame):
  374. """
  375. The PUSH_PROMISE frame is used to notify the peer endpoint in advance of
  376. streams the sender intends to initiate.
  377. """
  378. #: The flags defined for PUSH_PROMISE frames.
  379. defined_flags = [
  380. Flag("END_HEADERS", 0x04),
  381. Flag("PADDED", 0x08),
  382. ]
  383. #: The type byte defined for PUSH_PROMISE frames.
  384. type = 0x05
  385. stream_association = _STREAM_ASSOC_HAS_STREAM
  386. def __init__(self, stream_id: int, promised_stream_id: int = 0, data: bytes = b"", **kwargs: Any) -> None:
  387. super().__init__(stream_id, **kwargs)
  388. #: The stream ID that is promised by this frame.
  389. self.promised_stream_id = promised_stream_id
  390. #: The HPACK-encoded header block for the simulated request on the new
  391. #: stream.
  392. self.data = data
  393. def _body_repr(self) -> str:
  394. return f"promised_stream_id={self.promised_stream_id}, data={_raw_data_repr(self.data)}"
  395. def serialize_body(self) -> bytes:
  396. padding_data = self.serialize_padding_data()
  397. padding = b"\0" * self.pad_length
  398. data = _STRUCT_L.pack(self.promised_stream_id)
  399. return b"".join([padding_data, data, self.data, padding])
  400. def parse_body(self, data: memoryview) -> None:
  401. padding_data_length = self.parse_padding_data(data)
  402. try:
  403. self.promised_stream_id = _STRUCT_L.unpack(
  404. data[padding_data_length:padding_data_length + 4],
  405. )[0]
  406. except struct.error as err:
  407. msg = "Invalid PUSH_PROMISE body"
  408. raise InvalidFrameError(msg) from err
  409. self.data = (
  410. data[padding_data_length + 4:len(data)-self.pad_length].tobytes()
  411. )
  412. self.body_len = len(data)
  413. if self.promised_stream_id == 0 or self.promised_stream_id % 2 != 0:
  414. msg = f"Invalid PUSH_PROMISE promised stream id: {self.promised_stream_id}"
  415. raise InvalidDataError(msg)
  416. if self.pad_length and self.pad_length >= self.body_len:
  417. msg = "Padding is too long."
  418. raise InvalidPaddingError(msg)
  419. class PingFrame(Frame):
  420. """
  421. The PING frame is a mechanism for measuring a minimal round-trip time from
  422. the sender, as well as determining whether an idle connection is still
  423. functional. PING frames can be sent from any endpoint.
  424. """
  425. #: The flags defined for PING frames.
  426. defined_flags = [Flag("ACK", 0x01)]
  427. #: The type byte defined for PING frames.
  428. type = 0x06
  429. stream_association = _STREAM_ASSOC_NO_STREAM
  430. def __init__(self, stream_id: int = 0, opaque_data: bytes = b"", **kwargs: Any) -> None:
  431. super().__init__(stream_id, **kwargs)
  432. #: The opaque data sent in this PING frame, as a bytestring.
  433. self.opaque_data = opaque_data
  434. def _body_repr(self) -> str:
  435. return f"opaque_data={self.opaque_data!r}"
  436. def serialize_body(self) -> bytes:
  437. if len(self.opaque_data) > 8:
  438. msg = f"PING frame may not have more than 8 bytes of data, got {len(self.opaque_data)}"
  439. raise InvalidFrameError(msg)
  440. data = self.opaque_data
  441. data += b"\x00" * (8 - len(self.opaque_data))
  442. return data
  443. def parse_body(self, data: memoryview) -> None:
  444. if len(data) != 8:
  445. msg = f"PING frame must have 8 byte length: got {len(data)}"
  446. raise InvalidFrameError(msg)
  447. self.opaque_data = data.tobytes()
  448. self.body_len = 8
  449. class GoAwayFrame(Frame):
  450. """
  451. The GOAWAY frame informs the remote peer to stop creating streams on this
  452. connection. It can be sent from the client or the server. Once sent, the
  453. sender will ignore frames sent on new streams for the remainder of the
  454. connection.
  455. """
  456. #: The flags defined for GOAWAY frames.
  457. defined_flags: list[Flag] = []
  458. #: The type byte defined for GOAWAY frames.
  459. type = 0x07
  460. stream_association = _STREAM_ASSOC_NO_STREAM
  461. def __init__(self,
  462. stream_id: int = 0,
  463. last_stream_id: int = 0,
  464. error_code: int = 0,
  465. additional_data: bytes = b"",
  466. **kwargs: Any) -> None:
  467. super().__init__(stream_id, **kwargs)
  468. #: The last stream ID definitely seen by the remote peer.
  469. self.last_stream_id = last_stream_id
  470. #: The error code for connection teardown.
  471. self.error_code = error_code
  472. #: Any additional data sent in the GOAWAY.
  473. self.additional_data = additional_data
  474. def _body_repr(self) -> str:
  475. return f"last_stream_id={self.last_stream_id}, error_code={self.error_code}, additional_data={self.additional_data!r}"
  476. def serialize_body(self) -> bytes:
  477. data = _STRUCT_LL.pack(
  478. self.last_stream_id & 0x7FFFFFFF,
  479. self.error_code,
  480. )
  481. data += self.additional_data
  482. return data
  483. def parse_body(self, data: memoryview) -> None:
  484. try:
  485. self.last_stream_id, self.error_code = _STRUCT_LL.unpack(
  486. data[:8],
  487. )
  488. except struct.error as err:
  489. msg = "Invalid GOAWAY body."
  490. raise InvalidFrameError(msg) from err
  491. self.body_len = len(data)
  492. if len(data) > 8:
  493. self.additional_data = data[8:].tobytes()
  494. class WindowUpdateFrame(Frame):
  495. """
  496. The WINDOW_UPDATE frame is used to implement flow control.
  497. Flow control operates at two levels: on each individual stream and on the
  498. entire connection.
  499. Both types of flow control are hop by hop; that is, only between the two
  500. endpoints. Intermediaries do not forward WINDOW_UPDATE frames between
  501. dependent connections. However, throttling of data transfer by any receiver
  502. can indirectly cause the propagation of flow control information toward the
  503. original sender.
  504. """
  505. #: The flags defined for WINDOW_UPDATE frames.
  506. defined_flags: list[Flag] = []
  507. #: The type byte defined for WINDOW_UPDATE frames.
  508. type = 0x08
  509. stream_association = _STREAM_ASSOC_EITHER
  510. def __init__(self, stream_id: int, window_increment: int = 0, **kwargs: Any) -> None:
  511. super().__init__(stream_id, **kwargs)
  512. #: The amount the flow control window is to be incremented.
  513. self.window_increment = window_increment
  514. def _body_repr(self) -> str:
  515. return f"window_increment={self.window_increment}"
  516. def serialize_body(self) -> bytes:
  517. return _STRUCT_L.pack(self.window_increment & 0x7FFFFFFF)
  518. def parse_body(self, data: memoryview) -> None:
  519. if len(data) > 4:
  520. msg = f"WINDOW_UPDATE frame must have 4 byte length: got {len(data)}"
  521. raise InvalidFrameError(msg)
  522. try:
  523. self.window_increment = _STRUCT_L.unpack(data)[0]
  524. except struct.error as err:
  525. msg = "Invalid WINDOW_UPDATE body"
  526. raise InvalidFrameError(msg) from err
  527. if not 1 <= self.window_increment <= 2**31-1:
  528. msg = "WINDOW_UPDATE increment must be between 1 to 2^31-1"
  529. raise InvalidDataError(msg)
  530. self.body_len = 4
  531. class HeadersFrame(Padding, Priority, Frame):
  532. """
  533. The HEADERS frame carries name-value pairs. It is used to open a stream.
  534. HEADERS frames can be sent on a stream in the "open" or "half closed
  535. (remote)" states.
  536. The HeadersFrame class is actually basically a data frame in this
  537. implementation, because of the requirement to control the sizes of frames.
  538. A header block fragment that doesn't fit in an entire HEADERS frame needs
  539. to be followed with CONTINUATION frames. From the perspective of the frame
  540. building code the header block is an opaque data segment.
  541. """
  542. #: The flags defined for HEADERS frames.
  543. defined_flags = [
  544. Flag("END_STREAM", 0x01),
  545. Flag("END_HEADERS", 0x04),
  546. Flag("PADDED", 0x08),
  547. Flag("PRIORITY", 0x20),
  548. ]
  549. #: The type byte defined for HEADERS frames.
  550. type = 0x01
  551. stream_association = _STREAM_ASSOC_HAS_STREAM
  552. def __init__(self, stream_id: int, data: bytes = b"", **kwargs: Any) -> None:
  553. super().__init__(stream_id, **kwargs)
  554. #: The HPACK-encoded header block.
  555. self.data = data
  556. def _body_repr(self) -> str:
  557. return f"exclusive={self.exclusive}, depends_on={self.depends_on}, stream_weight={self.stream_weight}, data={_raw_data_repr(self.data)}"
  558. def serialize_body(self) -> bytes:
  559. padding_data = self.serialize_padding_data()
  560. padding = b"\0" * self.pad_length
  561. if "PRIORITY" in self.flags:
  562. priority_data = self.serialize_priority_data()
  563. else:
  564. priority_data = b""
  565. return b"".join([padding_data, priority_data, self.data, padding])
  566. def parse_body(self, data: memoryview) -> None:
  567. padding_data_length = self.parse_padding_data(data)
  568. data = data[padding_data_length:]
  569. if "PRIORITY" in self.flags:
  570. priority_data_length = self.parse_priority_data(data)
  571. else:
  572. priority_data_length = 0
  573. self.body_len = len(data)
  574. self.data = (
  575. data[priority_data_length:len(data)-self.pad_length].tobytes()
  576. )
  577. if self.pad_length and self.pad_length >= self.body_len:
  578. msg = "Padding is too long."
  579. raise InvalidPaddingError(msg)
  580. class ContinuationFrame(Frame):
  581. """
  582. The CONTINUATION frame is used to continue a sequence of header block
  583. fragments. Any number of CONTINUATION frames can be sent on an existing
  584. stream, as long as the preceding frame on the same stream is one of
  585. HEADERS, PUSH_PROMISE or CONTINUATION without the END_HEADERS flag set.
  586. Much like the HEADERS frame, hyper treats this as an opaque data frame with
  587. different flags and a different type.
  588. """
  589. #: The flags defined for CONTINUATION frames.
  590. defined_flags = [Flag("END_HEADERS", 0x04)]
  591. #: The type byte defined for CONTINUATION frames.
  592. type = 0x09
  593. stream_association = _STREAM_ASSOC_HAS_STREAM
  594. def __init__(self, stream_id: int, data: bytes = b"", **kwargs: Any) -> None:
  595. super().__init__(stream_id, **kwargs)
  596. #: The HPACK-encoded header block.
  597. self.data = data
  598. def _body_repr(self) -> str:
  599. return f"data={_raw_data_repr(self.data)}"
  600. def serialize_body(self) -> bytes:
  601. return self.data
  602. def parse_body(self, data: memoryview) -> None:
  603. self.data = data.tobytes()
  604. self.body_len = len(data)
  605. class AltSvcFrame(Frame):
  606. """
  607. The ALTSVC frame is used to advertise alternate services that the current
  608. host, or a different one, can understand. This frame is standardised as
  609. part of RFC 7838.
  610. This frame does no work to validate that the ALTSVC field parameter is
  611. acceptable per the rules of RFC 7838.
  612. .. note:: If the ``stream_id`` of this frame is nonzero, the origin field
  613. must have zero length. Conversely, if the ``stream_id`` of this
  614. frame is zero, the origin field must have nonzero length. Put
  615. another way, a valid ALTSVC frame has ``stream_id != 0`` XOR
  616. ``len(origin) != 0``.
  617. """
  618. type = 0x0A
  619. stream_association = _STREAM_ASSOC_EITHER
  620. def __init__(self, stream_id: int, origin: bytes = b"", field: bytes = b"", **kwargs: Any) -> None:
  621. super().__init__(stream_id, **kwargs)
  622. if not isinstance(origin, bytes):
  623. msg = "AltSvc origin must be a bytestring."
  624. raise InvalidDataError(msg)
  625. if not isinstance(field, bytes):
  626. msg = "AltSvc field must be a bytestring."
  627. raise InvalidDataError(msg)
  628. self.origin = origin
  629. self.field = field
  630. def _body_repr(self) -> str:
  631. return f"origin={self.origin!r}, field={self.field!r}"
  632. def serialize_body(self) -> bytes:
  633. origin_len = _STRUCT_H.pack(len(self.origin))
  634. return b"".join([origin_len, self.origin, self.field])
  635. def parse_body(self, data: memoryview) -> None:
  636. try:
  637. origin_len = _STRUCT_H.unpack(data[0:2])[0]
  638. self.origin = data[2:2+origin_len].tobytes()
  639. if len(self.origin) != origin_len:
  640. msg = "Invalid ALTSVC frame body."
  641. raise InvalidFrameError(msg)
  642. self.field = data[2+origin_len:].tobytes()
  643. except (struct.error, ValueError) as err:
  644. msg = "Invalid ALTSVC frame body."
  645. raise InvalidFrameError(msg) from err
  646. self.body_len = len(data)
  647. class ExtensionFrame(Frame):
  648. """
  649. ExtensionFrame is used to wrap frames which are not natively interpretable
  650. by hyperframe.
  651. Although certain byte prefixes are ordained by specification to have
  652. certain contextual meanings, frames with other prefixes are not prohibited,
  653. and may be used to communicate arbitrary meaning between HTTP/2 peers.
  654. Thus, hyperframe, rather than raising an exception when such a frame is
  655. encountered, wraps it in a generic frame to be properly acted upon by
  656. upstream consumers which might have additional context on how to use it.
  657. .. versionadded:: 5.0.0
  658. """
  659. stream_association = _STREAM_ASSOC_EITHER
  660. def __init__(self, type: int, stream_id: int, flag_byte: int = 0x0, body: bytes = b"", **kwargs: Any) -> None: # noqa: A002
  661. super().__init__(stream_id, **kwargs)
  662. self.type = type
  663. self.flag_byte = flag_byte
  664. self.body = body
  665. def _body_repr(self) -> str:
  666. return f"type={self.type}, flag_byte={self.flag_byte}, body={_raw_data_repr(self.body)}"
  667. def parse_flags(self, flag_byte: int) -> None: # type: ignore
  668. """
  669. For extension frames, we parse the flags by just storing a flag byte.
  670. """
  671. self.flag_byte = flag_byte
  672. def parse_body(self, data: memoryview) -> None:
  673. self.body = data.tobytes()
  674. self.body_len = len(data)
  675. def serialize(self) -> bytes:
  676. """
  677. A broad override of the serialize method that ensures that the data
  678. comes back out exactly as it came in. This should not be used in most
  679. user code: it exists only as a helper method if frames need to be
  680. reconstituted.
  681. """
  682. # Build the frame header.
  683. # First, get the flags.
  684. flags = self.flag_byte
  685. header = _STRUCT_HBBBL.pack(
  686. (self.body_len >> 8) & 0xFFFF, # Length spread over top 24 bits
  687. self.body_len & 0xFF,
  688. self.type,
  689. flags,
  690. self.stream_id & 0x7FFFFFFF, # Stream ID is 32 bits.
  691. )
  692. return header + self.body
  693. def _raw_data_repr(data: bytes | None) -> str:
  694. if not data:
  695. return "None"
  696. r = binascii.hexlify(data).decode("ascii")
  697. if len(r) > 20:
  698. r = r[:20] + "..."
  699. return "<hex:" + r + ">"
  700. _FRAME_CLASSES: list[type[Frame]] = [
  701. DataFrame,
  702. HeadersFrame,
  703. PriorityFrame,
  704. RstStreamFrame,
  705. SettingsFrame,
  706. PushPromiseFrame,
  707. PingFrame,
  708. GoAwayFrame,
  709. WindowUpdateFrame,
  710. ContinuationFrame,
  711. AltSvcFrame,
  712. ]
  713. #: FRAMES maps the type byte for each frame to the class used to represent that
  714. #: frame.
  715. FRAMES = {cls.type: cls for cls in _FRAME_CLASSES}