frame.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. # -*- coding: utf-8 -*-
  2. """
  3. hyperframe/frame
  4. ~~~~~~~~~~~~~~~~
  5. Defines framing logic for HTTP/2. Provides both classes to represent framed
  6. data and logic for aiding the connection when it comes to reading from the
  7. socket.
  8. """
  9. import collections
  10. import struct
  11. from .flags import Flag, Flags
  12. # The maximum initial length of a frame. Some frames have shorter maximum lengths.
  13. FRAME_MAX_LEN = (2 ** 14)
  14. # The maximum allowed length of a frame.
  15. FRAME_MAX_ALLOWED_LEN = (2 ** 24) - 1
  16. class Frame(object):
  17. """
  18. The base class for all HTTP/2 frames.
  19. """
  20. # The flags defined on this type of frame.
  21. defined_flags = []
  22. # The type of the frame.
  23. type = None
  24. # If 'has-stream', the frame's stream_id must be non-zero. If 'no-stream',
  25. # it must be zero. If 'either', it's not checked.
  26. stream_association = None
  27. def __init__(self, stream_id, flags=()):
  28. self.stream_id = stream_id
  29. self.flags = Flags(self.defined_flags)
  30. self.body_len = 0
  31. for flag in flags:
  32. self.flags.add(flag)
  33. if self.stream_association == 'has-stream' and not self.stream_id:
  34. raise ValueError('Stream ID must be non-zero')
  35. if self.stream_association == 'no-stream' and self.stream_id:
  36. raise ValueError('Stream ID must be zero')
  37. def __repr__(self):
  38. flags = ", ".join(self.flags) or "None"
  39. body = self.serialize_body()
  40. if len(body) > 100:
  41. body = str(body[:100]) + "..."
  42. return (
  43. "{type}(Stream: {stream}; Flags: {flags}): {body}"
  44. ).format(type=type(self).__name__, stream=self.stream_id, flags=flags, body=body)
  45. @staticmethod
  46. def parse_frame_header(header):
  47. """
  48. Takes a 9-byte frame header and returns a tuple of the appropriate
  49. Frame object and the length that needs to be read from the socket.
  50. """
  51. fields = struct.unpack("!HBBBL", header)
  52. # First 24 bits are frame length.
  53. length = (fields[0] << 8) + fields[1]
  54. type = fields[2]
  55. flags = fields[3]
  56. stream_id = fields[4]
  57. if type not in FRAMES:
  58. raise ValueError("Unknown frame type %d" % type)
  59. frame = FRAMES[type](stream_id)
  60. frame.parse_flags(flags)
  61. return (frame, length)
  62. def parse_flags(self, flag_byte):
  63. for flag, flag_bit in self.defined_flags:
  64. if flag_byte & flag_bit:
  65. self.flags.add(flag)
  66. return self.flags
  67. def serialize(self):
  68. body = self.serialize_body()
  69. self.body_len = len(body)
  70. # Build the common frame header.
  71. # First, get the flags.
  72. flags = 0
  73. for flag, flag_bit in self.defined_flags:
  74. if flag in self.flags:
  75. flags |= flag_bit
  76. header = struct.pack(
  77. "!HBBBL",
  78. (self.body_len & 0xFFFF00) >> 8, # Length is spread over top 24 bits
  79. self.body_len & 0x0000FF,
  80. self.type,
  81. flags,
  82. self.stream_id & 0x7FFFFFFF # Stream ID is 32 bits.
  83. )
  84. return header + body
  85. def serialize_body(self):
  86. raise NotImplementedError()
  87. def parse_body(self, data):
  88. raise NotImplementedError()
  89. class Padding(object):
  90. """
  91. Mixin for frames that contain padding.
  92. """
  93. def __init__(self, stream_id, pad_length=0, **kwargs):
  94. super(Padding, self).__init__(stream_id, **kwargs)
  95. self.pad_length = pad_length
  96. def serialize_padding_data(self):
  97. if 'PADDED' in self.flags:
  98. return struct.pack('!B', self.pad_length)
  99. return b''
  100. def parse_padding_data(self, data):
  101. if 'PADDED' in self.flags:
  102. self.pad_length = struct.unpack('!B', data[:1])[0]
  103. return 1
  104. return 0
  105. @property
  106. def total_padding(self):
  107. """Return the total length of the padding, if any."""
  108. return self.pad_length
  109. class Priority(object):
  110. """
  111. Mixin for frames that contain priority data.
  112. """
  113. def __init__(self, stream_id, depends_on=None, stream_weight=None, exclusive=None, **kwargs):
  114. super(Priority, self).__init__(stream_id, **kwargs)
  115. # The stream ID of the stream on which this stream depends.
  116. self.depends_on = depends_on
  117. # The weight of the stream. This is an integer between 0 and 256.
  118. self.stream_weight = stream_weight
  119. # Whether the exclusive bit was set.
  120. self.exclusive = exclusive
  121. def serialize_priority_data(self):
  122. return struct.pack(
  123. "!LB",
  124. self.depends_on | (int(self.exclusive) << 31),
  125. self.stream_weight
  126. )
  127. def parse_priority_data(self, data):
  128. MASK = 0x80000000
  129. self.depends_on, self.stream_weight = struct.unpack(
  130. "!LB", data[:5]
  131. )
  132. self.exclusive = bool(self.depends_on & MASK)
  133. self.depends_on &= ~MASK
  134. return 5
  135. class DataFrame(Padding, Frame):
  136. """
  137. DATA frames convey arbitrary, variable-length sequences of octets
  138. associated with a stream. One or more DATA frames are used, for instance,
  139. to carry HTTP request or response payloads.
  140. """
  141. defined_flags = [
  142. Flag('END_STREAM', 0x01),
  143. Flag('PADDED', 0x08),
  144. ]
  145. type = 0x0
  146. stream_association = 'has-stream'
  147. def __init__(self, stream_id, data=b'', **kwargs):
  148. super(DataFrame, self).__init__(stream_id, **kwargs)
  149. self.data = data
  150. def serialize_body(self):
  151. padding_data = self.serialize_padding_data()
  152. padding = b'\0' * self.total_padding
  153. return b''.join([padding_data, self.data, padding])
  154. def parse_body(self, data):
  155. padding_data_length = self.parse_padding_data(data)
  156. self.data = data[padding_data_length:len(data)-self.total_padding].tobytes()
  157. self.body_len = len(data)
  158. @property
  159. def flow_controlled_length(self):
  160. """
  161. If the frame is padded we need to include the padding length byte in
  162. the flow control used.
  163. """
  164. padding_len = self.total_padding + 1 if self.total_padding else 0
  165. return len(self.data) + padding_len
  166. class PriorityFrame(Priority, Frame):
  167. """
  168. The PRIORITY frame specifies the sender-advised priority of a stream. It
  169. can be sent at any time for an existing stream. This enables
  170. reprioritisation of existing streams.
  171. """
  172. defined_flags = []
  173. type = 0x02
  174. stream_association = 'has-stream'
  175. def serialize_body(self):
  176. return self.serialize_priority_data()
  177. def parse_body(self, data):
  178. self.parse_priority_data(data)
  179. self.body_len = len(data)
  180. class RstStreamFrame(Frame):
  181. """
  182. The RST_STREAM frame allows for abnormal termination of a stream. When sent
  183. by the initiator of a stream, it indicates that they wish to cancel the
  184. stream or that an error condition has occurred. When sent by the receiver
  185. of a stream, it indicates that either the receiver is rejecting the stream,
  186. requesting that the stream be cancelled or that an error condition has
  187. occurred.
  188. """
  189. defined_flags = []
  190. type = 0x03
  191. stream_association = 'has-stream'
  192. def __init__(self, stream_id, error_code=0, **kwargs):
  193. super(RstStreamFrame, self).__init__(stream_id, **kwargs)
  194. self.error_code = error_code
  195. def serialize_body(self):
  196. return struct.pack("!L", self.error_code)
  197. def parse_body(self, data):
  198. if len(data) != 4:
  199. raise ValueError()
  200. self.error_code = struct.unpack("!L", data)[0]
  201. self.body_len = len(data)
  202. class SettingsFrame(Frame):
  203. """
  204. The SETTINGS frame conveys configuration parameters that affect how
  205. endpoints communicate. The parameters are either constraints on peer
  206. behavior or preferences.
  207. Settings are not negotiated. Settings describe characteristics of the
  208. sending peer, which are used by the receiving peer. Different values for
  209. the same setting can be advertised by each peer. For example, a client
  210. might set a high initial flow control window, whereas a server might set a
  211. lower value to conserve resources.
  212. """
  213. defined_flags = [Flag('ACK', 0x01)]
  214. type = 0x04
  215. stream_association = 'no-stream'
  216. # We need to define the known settings, they may as well be class
  217. # attributes.
  218. HEADER_TABLE_SIZE = 0x01
  219. ENABLE_PUSH = 0x02
  220. MAX_CONCURRENT_STREAMS = 0x03
  221. INITIAL_WINDOW_SIZE = 0x04
  222. SETTINGS_MAX_FRAME_SIZE = 0x05
  223. SETTINGS_MAX_HEADER_LIST_SIZE = 0x06
  224. def __init__(self, stream_id=0, settings=None, **kwargs):
  225. super(SettingsFrame, self).__init__(stream_id, **kwargs)
  226. if settings and "ACK" in kwargs.get("flags", ()):
  227. raise ValueError("Settings must be empty if ACK flag is set.")
  228. # A dictionary of the setting type byte to the value.
  229. self.settings = settings or {}
  230. def serialize_body(self):
  231. settings = [struct.pack("!HL", setting & 0xFF, value)
  232. for setting, value in self.settings.items()]
  233. return b''.join(settings)
  234. def parse_body(self, data):
  235. for i in range(0, len(data), 6):
  236. name, value = struct.unpack("!HL", data[i:i+6])
  237. self.settings[name] = value
  238. self.body_len = len(data)
  239. class PushPromiseFrame(Padding, Frame):
  240. """
  241. The PUSH_PROMISE frame is used to notify the peer endpoint in advance of
  242. streams the sender intends to initiate.
  243. """
  244. defined_flags = [
  245. Flag('END_HEADERS', 0x04),
  246. Flag('PADDED', 0x08)
  247. ]
  248. type = 0x05
  249. stream_association = 'has-stream'
  250. def __init__(self, stream_id, promised_stream_id=0, data=b'', **kwargs):
  251. super(PushPromiseFrame, self).__init__(stream_id, **kwargs)
  252. self.promised_stream_id = promised_stream_id
  253. self.data = data
  254. def serialize_body(self):
  255. padding_data = self.serialize_padding_data()
  256. padding = b'\0' * self.total_padding
  257. data = struct.pack("!L", self.promised_stream_id)
  258. return b''.join([padding_data, data, self.data, padding])
  259. def parse_body(self, data):
  260. padding_data_length = self.parse_padding_data(data)
  261. self.promised_stream_id = struct.unpack("!L", data[padding_data_length:padding_data_length + 4])[0]
  262. self.data = data[padding_data_length + 4:].tobytes()
  263. self.body_len = len(data)
  264. class PingFrame(Frame):
  265. """
  266. The PING frame is a mechanism for measuring a minimal round-trip time from
  267. the sender, as well as determining whether an idle connection is still
  268. functional. PING frames can be sent from any endpoint.
  269. """
  270. defined_flags = [Flag('ACK', 0x01)]
  271. type = 0x06
  272. stream_association = 'no-stream'
  273. def __init__(self, stream_id=0, opaque_data=b'', **kwargs):
  274. super(PingFrame, self).__init__(stream_id, **kwargs)
  275. self.opaque_data = opaque_data
  276. def serialize_body(self):
  277. if len(self.opaque_data) > 8:
  278. raise ValueError()
  279. data = self.opaque_data
  280. data += b'\x00' * (8 - len(self.opaque_data))
  281. return data
  282. def parse_body(self, data):
  283. if len(data) > 8:
  284. raise ValueError()
  285. self.opaque_data = data.tobytes()
  286. self.body_len = len(data)
  287. class GoAwayFrame(Frame):
  288. """
  289. The GOAWAY frame informs the remote peer to stop creating streams on this
  290. connection. It can be sent from the client or the server. Once sent, the
  291. sender will ignore frames sent on new streams for the remainder of the
  292. connection.
  293. """
  294. type = 0x07
  295. stream_association = 'no-stream'
  296. def __init__(self, stream_id=0, last_stream_id=0, error_code=0, additional_data=b'', **kwargs):
  297. super(GoAwayFrame, self).__init__(stream_id, **kwargs)
  298. self.last_stream_id = last_stream_id
  299. self.error_code = error_code
  300. self.additional_data = additional_data
  301. def serialize_body(self):
  302. data = struct.pack(
  303. "!LL",
  304. self.last_stream_id & 0x7FFFFFFF,
  305. self.error_code
  306. )
  307. data += self.additional_data
  308. return data
  309. def parse_body(self, data):
  310. self.last_stream_id, self.error_code = struct.unpack("!LL", data[:8])
  311. self.body_len = len(data)
  312. if len(data) > 8:
  313. self.additional_data = data[8:].tobytes()
  314. class WindowUpdateFrame(Frame):
  315. """
  316. The WINDOW_UPDATE frame is used to implement flow control.
  317. Flow control operates at two levels: on each individual stream and on the
  318. entire connection.
  319. Both types of flow control are hop by hop; that is, only between the two
  320. endpoints. Intermediaries do not forward WINDOW_UPDATE frames between
  321. dependent connections. However, throttling of data transfer by any receiver
  322. can indirectly cause the propagation of flow control information toward the
  323. original sender.
  324. """
  325. type = 0x08
  326. stream_association = 'either'
  327. def __init__(self, stream_id, window_increment=0, **kwargs):
  328. super(WindowUpdateFrame, self).__init__(stream_id, **kwargs)
  329. self.window_increment = window_increment
  330. def serialize_body(self):
  331. return struct.pack("!L", self.window_increment & 0x7FFFFFFF)
  332. def parse_body(self, data):
  333. self.window_increment = struct.unpack("!L", data)[0]
  334. self.body_len = len(data)
  335. class HeadersFrame(Padding, Priority, Frame):
  336. """
  337. The HEADERS frame carries name-value pairs. It is used to open a stream.
  338. HEADERS frames can be sent on a stream in the "open" or "half closed
  339. (remote)" states.
  340. The HeadersFrame class is actually basically a data frame in this
  341. implementation, because of the requirement to control the sizes of frames.
  342. A header block fragment that doesn't fit in an entire HEADERS frame needs
  343. to be followed with CONTINUATION frames. From the perspective of the frame
  344. building code the header block is an opaque data segment.
  345. """
  346. type = 0x01
  347. stream_association = 'has-stream'
  348. defined_flags = [
  349. Flag('END_STREAM', 0x01),
  350. Flag('END_HEADERS', 0x04),
  351. Flag('PADDED', 0x08),
  352. Flag('PRIORITY', 0x20),
  353. ]
  354. def __init__(self, stream_id, data=b'', **kwargs):
  355. super(HeadersFrame, self).__init__(stream_id, **kwargs)
  356. self.data = data
  357. def serialize_body(self):
  358. padding_data = self.serialize_padding_data()
  359. padding = b'\0' * self.total_padding
  360. if 'PRIORITY' in self.flags:
  361. priority_data = self.serialize_priority_data()
  362. else:
  363. priority_data = b''
  364. return b''.join([padding_data, priority_data, self.data, padding])
  365. def parse_body(self, data):
  366. padding_data_length = self.parse_padding_data(data)
  367. data = data[padding_data_length:]
  368. if 'PRIORITY' in self.flags:
  369. priority_data_length = self.parse_priority_data(data)
  370. else:
  371. priority_data_length = 0
  372. self.body_len = len(data)
  373. self.data = data[priority_data_length:len(data)-self.total_padding].tobytes()
  374. class ContinuationFrame(Frame):
  375. """
  376. The CONTINUATION frame is used to continue a sequence of header block
  377. fragments. Any number of CONTINUATION frames can be sent on an existing
  378. stream, as long as the preceding frame on the same stream is one of
  379. HEADERS, PUSH_PROMISE or CONTINUATION without the END_HEADERS flag set.
  380. Much like the HEADERS frame, hyper treats this as an opaque data frame with
  381. different flags and a different type.
  382. """
  383. type = 0x09
  384. stream_association = 'has-stream'
  385. defined_flags = [Flag('END_HEADERS', 0x04),]
  386. def __init__(self, stream_id, data=b'', **kwargs):
  387. super(ContinuationFrame, self).__init__(stream_id, **kwargs)
  388. self.data = data
  389. def serialize_body(self):
  390. return self.data
  391. def parse_body(self, data):
  392. self.data = data.tobytes()
  393. self.body_len = len(data)
  394. Origin = collections.namedtuple('Origin', ['scheme', 'host', 'port'])
  395. class AltSvcFrame(Frame):
  396. """
  397. The ALTSVC frame is used to advertise alternate services that the current
  398. host, or a different one, can understand.
  399. """
  400. type = 0xA
  401. stream_association = 'no-stream'
  402. def __init__(self, stream_id=0, host=b'', port=0, protocol_id=b'', max_age=0, origin=None, **kwargs):
  403. super(AltSvcFrame, self).__init__(stream_id, **kwargs)
  404. self.host = host
  405. self.port = port
  406. self.protocol_id = protocol_id
  407. self.max_age = max_age
  408. self.origin = origin
  409. def serialize_origin(self):
  410. if self.origin is not None:
  411. if self.origin.port is None:
  412. hostport = self.origin.host
  413. else:
  414. hostport = self.origin.host + b':' + str(self.origin.port).encode('ascii')
  415. return self.origin.scheme + b'://' + hostport
  416. return b''
  417. def parse_origin(self, data):
  418. if len(data) > 0:
  419. data = data.tobytes()
  420. scheme, hostport = data.split(b'://')
  421. host, _, port = hostport.partition(b':')
  422. self.origin = Origin(scheme=scheme, host=host,
  423. port=int(port) if len(port) > 0 else None)
  424. def serialize_body(self):
  425. first = struct.pack("!LHxB", self.max_age, self.port, len(self.protocol_id))
  426. host_length = struct.pack("!B", len(self.host))
  427. return b''.join([first, self.protocol_id, host_length, self.host,
  428. self.serialize_origin()])
  429. def parse_body(self, data):
  430. self.body_len = len(data)
  431. self.max_age, self.port, protocol_id_length = struct.unpack("!LHxB", data[:8])
  432. pos = 8
  433. self.protocol_id = data[pos:pos+protocol_id_length].tobytes()
  434. pos += protocol_id_length
  435. host_length = struct.unpack("!B", data[pos:pos+1])[0]
  436. pos += 1
  437. self.host = data[pos:pos+host_length].tobytes()
  438. pos += host_length
  439. self.parse_origin(data[pos:])
  440. class BlockedFrame(Frame):
  441. """
  442. The BLOCKED frame indicates that the sender is unable to send data due to a
  443. closed flow control window.
  444. The BLOCKED frame is used to provide feedback about the performance of flow
  445. control for the purposes of performance tuning and debugging. The BLOCKED
  446. frame can be sent by a peer when flow controlled data cannot be sent due to
  447. the connection- or stream-level flow control. This frame MUST NOT be sent
  448. if there are other reasons preventing data from being sent, either a lack
  449. of available data, or the underlying transport being blocked.
  450. """
  451. type = 0x0B
  452. stream_association = 'both'
  453. defined_flags = []
  454. def serialize_body(self):
  455. return b''
  456. def parse_body(self, data):
  457. pass
  458. # A map of type byte to frame class.
  459. _FRAME_CLASSES = [
  460. DataFrame,
  461. HeadersFrame,
  462. PriorityFrame,
  463. RstStreamFrame,
  464. SettingsFrame,
  465. PushPromiseFrame,
  466. PingFrame,
  467. GoAwayFrame,
  468. WindowUpdateFrame,
  469. ContinuationFrame,
  470. AltSvcFrame,
  471. BlockedFrame
  472. ]
  473. FRAMES = {cls.type: cls for cls in _FRAME_CLASSES}