buffered_spdy_framer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_SPDY_BUFFERED_SPDY_FRAMER_H_
  5. #define NET_SPDY_BUFFERED_SPDY_FRAMER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <string>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/strings/string_piece.h"
  12. #include "base/time/time.h"
  13. #include "net/base/net_export.h"
  14. #include "net/log/net_log_source.h"
  15. #include "net/spdy/header_coalescer.h"
  16. #include "net/third_party/quiche/src/quiche/spdy/core/http2_frame_decoder_adapter.h"
  17. #include "net/third_party/quiche/src/quiche/spdy/core/http2_header_block.h"
  18. #include "net/third_party/quiche/src/quiche/spdy/core/spdy_alt_svc_wire_format.h"
  19. #include "net/third_party/quiche/src/quiche/spdy/core/spdy_framer.h"
  20. #include "net/third_party/quiche/src/quiche/spdy/core/spdy_protocol.h"
  21. namespace net {
  22. class NET_EXPORT_PRIVATE BufferedSpdyFramerVisitorInterface {
  23. public:
  24. BufferedSpdyFramerVisitorInterface() = default;
  25. BufferedSpdyFramerVisitorInterface(
  26. const BufferedSpdyFramerVisitorInterface&) = delete;
  27. BufferedSpdyFramerVisitorInterface& operator=(
  28. const BufferedSpdyFramerVisitorInterface&) = delete;
  29. // Called if an error is detected in the spdy::SpdySerializedFrame protocol.
  30. virtual void OnError(
  31. http2::Http2DecoderAdapter::SpdyFramerError spdy_framer_error) = 0;
  32. // Called if an error is detected in a HTTP2 stream.
  33. virtual void OnStreamError(spdy::SpdyStreamId stream_id,
  34. const std::string& description) = 0;
  35. // Called after all the header data for HEADERS control frame is received.
  36. virtual void OnHeaders(spdy::SpdyStreamId stream_id,
  37. bool has_priority,
  38. int weight,
  39. spdy::SpdyStreamId parent_stream_id,
  40. bool exclusive,
  41. bool fin,
  42. spdy::Http2HeaderBlock headers,
  43. base::TimeTicks recv_first_byte_time) = 0;
  44. // Called when a data frame header is received.
  45. virtual void OnDataFrameHeader(spdy::SpdyStreamId stream_id,
  46. size_t length,
  47. bool fin) = 0;
  48. // Called when data is received.
  49. // |stream_id| The stream receiving data.
  50. // |data| A buffer containing the data received.
  51. // |len| The length of the data buffer (at most 2^16 - 1 - 8).
  52. virtual void OnStreamFrameData(spdy::SpdyStreamId stream_id,
  53. const char* data,
  54. size_t len) = 0;
  55. // Called when the other side has finished sending data on this stream.
  56. // |stream_id| The stream that was receivin data.
  57. virtual void OnStreamEnd(spdy::SpdyStreamId stream_id) = 0;
  58. // Called when padding is received (padding length field or padding octets).
  59. // |stream_id| The stream receiving data.
  60. // |len| The number of padding octets.
  61. virtual void OnStreamPadding(spdy::SpdyStreamId stream_id, size_t len) = 0;
  62. // Called when a SETTINGS frame is received.
  63. virtual void OnSettings() = 0;
  64. // Called when an individual setting within a SETTINGS frame has been parsed.
  65. // Note that |id| may or may not be a SETTINGS ID defined in the HTTP/2 spec.
  66. virtual void OnSetting(spdy::SpdySettingsId id, uint32_t value) = 0;
  67. // Called when a SETTINGS frame is received with the ACK flag set.
  68. virtual void OnSettingsAck() = 0;
  69. // Called at the completion of parsing SETTINGS id and value tuples.
  70. virtual void OnSettingsEnd() = 0;
  71. // Called when a PING frame has been parsed.
  72. virtual void OnPing(spdy::SpdyPingId unique_id, bool is_ack) = 0;
  73. // Called when a RST_STREAM frame has been parsed.
  74. virtual void OnRstStream(spdy::SpdyStreamId stream_id,
  75. spdy::SpdyErrorCode error_code) = 0;
  76. // Called when a GOAWAY frame has been parsed.
  77. virtual void OnGoAway(spdy::SpdyStreamId last_accepted_stream_id,
  78. spdy::SpdyErrorCode error_code,
  79. base::StringPiece debug_data) = 0;
  80. // Called when a WINDOW_UPDATE frame has been parsed.
  81. virtual void OnWindowUpdate(spdy::SpdyStreamId stream_id,
  82. int delta_window_size) = 0;
  83. // Called when a PUSH_PROMISE frame has been parsed.
  84. virtual void OnPushPromise(spdy::SpdyStreamId stream_id,
  85. spdy::SpdyStreamId promised_stream_id,
  86. spdy::Http2HeaderBlock headers) = 0;
  87. // Called when an ALTSVC frame has been parsed.
  88. virtual void OnAltSvc(
  89. spdy::SpdyStreamId stream_id,
  90. base::StringPiece origin,
  91. const spdy::SpdyAltSvcWireFormat::AlternativeServiceVector&
  92. altsvc_vector) = 0;
  93. // Called when a frame type we don't recognize is received.
  94. // Return true if this appears to be a valid extension frame, false otherwise.
  95. // We distinguish between extension frames and nonsense by checking
  96. // whether the stream id is valid.
  97. virtual bool OnUnknownFrame(spdy::SpdyStreamId stream_id,
  98. uint8_t frame_type) = 0;
  99. protected:
  100. virtual ~BufferedSpdyFramerVisitorInterface() = default;
  101. };
  102. class NET_EXPORT_PRIVATE BufferedSpdyFramer
  103. : public spdy::SpdyFramerVisitorInterface {
  104. public:
  105. using TimeFunc = base::TimeTicks (*)();
  106. BufferedSpdyFramer(uint32_t max_header_list_size,
  107. const NetLogWithSource& net_log,
  108. TimeFunc time_func = base::TimeTicks::Now);
  109. BufferedSpdyFramer() = delete;
  110. BufferedSpdyFramer(const BufferedSpdyFramer&) = delete;
  111. BufferedSpdyFramer& operator=(const BufferedSpdyFramer&) = delete;
  112. ~BufferedSpdyFramer() override;
  113. // Sets callbacks to be called from the buffered spdy framer. A visitor must
  114. // be set, or else the framer will likely crash. It is acceptable for the
  115. // visitor to do nothing. If this is called multiple times, only the last
  116. // visitor will be used.
  117. void set_visitor(BufferedSpdyFramerVisitorInterface* visitor);
  118. // Set debug callbacks to be called from the framer. The debug visitor is
  119. // completely optional and need not be set in order for normal operation.
  120. // If this is called multiple times, only the last visitor will be used.
  121. void set_debug_visitor(spdy::SpdyFramerDebugVisitorInterface* debug_visitor);
  122. // spdy::SpdyFramerVisitorInterface
  123. void OnError(http2::Http2DecoderAdapter::SpdyFramerError spdy_framer_error,
  124. std::string detailed_error) override;
  125. void OnHeaders(spdy::SpdyStreamId stream_id,
  126. size_t payload_length,
  127. bool has_priority,
  128. int weight,
  129. spdy::SpdyStreamId parent_stream_id,
  130. bool exclusive,
  131. bool fin,
  132. bool end) override;
  133. void OnStreamFrameData(spdy::SpdyStreamId stream_id,
  134. const char* data,
  135. size_t len) override;
  136. void OnStreamEnd(spdy::SpdyStreamId stream_id) override;
  137. void OnStreamPadLength(spdy::SpdyStreamId stream_id, size_t value) override;
  138. void OnStreamPadding(spdy::SpdyStreamId stream_id, size_t len) override;
  139. spdy::SpdyHeadersHandlerInterface* OnHeaderFrameStart(
  140. spdy::SpdyStreamId stream_id) override;
  141. void OnHeaderFrameEnd(spdy::SpdyStreamId stream_id) override;
  142. void OnSettings() override;
  143. void OnSetting(spdy::SpdySettingsId id, uint32_t value) override;
  144. void OnSettingsAck() override;
  145. void OnSettingsEnd() override;
  146. void OnPing(spdy::SpdyPingId unique_id, bool is_ack) override;
  147. void OnRstStream(spdy::SpdyStreamId stream_id,
  148. spdy::SpdyErrorCode error_code) override;
  149. void OnGoAway(spdy::SpdyStreamId last_accepted_stream_id,
  150. spdy::SpdyErrorCode error_code) override;
  151. bool OnGoAwayFrameData(const char* goaway_data, size_t len) override;
  152. void OnWindowUpdate(spdy::SpdyStreamId stream_id,
  153. int delta_window_size) override;
  154. void OnPushPromise(spdy::SpdyStreamId stream_id,
  155. spdy::SpdyStreamId promised_stream_id,
  156. bool end) override;
  157. void OnAltSvc(spdy::SpdyStreamId stream_id,
  158. absl::string_view origin,
  159. const spdy::SpdyAltSvcWireFormat::AlternativeServiceVector&
  160. altsvc_vector) override;
  161. void OnDataFrameHeader(spdy::SpdyStreamId stream_id,
  162. size_t length,
  163. bool fin) override;
  164. void OnContinuation(spdy::SpdyStreamId stream_id,
  165. size_t payload_length,
  166. bool end) override;
  167. void OnPriority(spdy::SpdyStreamId stream_id,
  168. spdy::SpdyStreamId parent_stream_id,
  169. int weight,
  170. bool exclusive) override {}
  171. void OnPriorityUpdate(spdy::SpdyStreamId prioritized_stream_id,
  172. absl::string_view priority_field_value) override {}
  173. bool OnUnknownFrame(spdy::SpdyStreamId stream_id,
  174. uint8_t frame_type) override;
  175. void OnUnknownFrameStart(spdy::SpdyStreamId stream_id,
  176. size_t length,
  177. uint8_t type,
  178. uint8_t flags) override {}
  179. void OnUnknownFramePayload(spdy::SpdyStreamId stream_id,
  180. absl::string_view payload) override {}
  181. // spdy::SpdyFramer methods.
  182. size_t ProcessInput(const char* data, size_t len);
  183. void UpdateHeaderDecoderTableSize(uint32_t value);
  184. http2::Http2DecoderAdapter::SpdyFramerError spdy_framer_error() const;
  185. http2::Http2DecoderAdapter::SpdyState state() const;
  186. bool MessageFullyRead();
  187. bool HasError();
  188. std::unique_ptr<spdy::SpdySerializedFrame> CreateRstStream(
  189. spdy::SpdyStreamId stream_id,
  190. spdy::SpdyErrorCode error_code) const;
  191. std::unique_ptr<spdy::SpdySerializedFrame> CreateSettings(
  192. const spdy::SettingsMap& values) const;
  193. std::unique_ptr<spdy::SpdySerializedFrame> CreatePingFrame(
  194. spdy::SpdyPingId unique_id,
  195. bool is_ack) const;
  196. std::unique_ptr<spdy::SpdySerializedFrame> CreateWindowUpdate(
  197. spdy::SpdyStreamId stream_id,
  198. uint32_t delta_window_size) const;
  199. std::unique_ptr<spdy::SpdySerializedFrame> CreateDataFrame(
  200. spdy::SpdyStreamId stream_id,
  201. const char* data,
  202. uint32_t len,
  203. spdy::SpdyDataFlags flags);
  204. std::unique_ptr<spdy::SpdySerializedFrame> CreatePriority(
  205. spdy::SpdyStreamId stream_id,
  206. spdy::SpdyStreamId dependency_id,
  207. int weight,
  208. bool exclusive) const;
  209. // Serialize a frame of unknown type.
  210. spdy::SpdySerializedFrame SerializeFrame(const spdy::SpdyFrameIR& frame) {
  211. return spdy_framer_.SerializeFrame(frame);
  212. }
  213. int frames_received() const { return frames_received_; }
  214. // Updates the maximum size of the header encoder compression table.
  215. void UpdateHeaderEncoderTableSize(uint32_t value);
  216. // Returns the maximum size of the header encoder compression table.
  217. uint32_t header_encoder_table_size() const;
  218. private:
  219. spdy::SpdyFramer spdy_framer_;
  220. http2::Http2DecoderAdapter deframer_;
  221. raw_ptr<BufferedSpdyFramerVisitorInterface> visitor_ = nullptr;
  222. int frames_received_ = 0;
  223. // Collection of fields from control frames that we need to
  224. // buffer up from the spdy framer.
  225. struct ControlFrameFields {
  226. ControlFrameFields();
  227. spdy::SpdyFrameType type;
  228. spdy::SpdyStreamId stream_id = 0U;
  229. spdy::SpdyStreamId associated_stream_id = 0U;
  230. spdy::SpdyStreamId promised_stream_id = 0U;
  231. bool has_priority = false;
  232. spdy::SpdyPriority priority = 0U;
  233. int weight = 0;
  234. spdy::SpdyStreamId parent_stream_id = 0U;
  235. bool exclusive = false;
  236. bool fin = false;
  237. bool unidirectional = false;
  238. base::TimeTicks recv_first_byte_time;
  239. };
  240. std::unique_ptr<ControlFrameFields> control_frame_fields_;
  241. // Collection of fields of a GOAWAY frame that this class needs to buffer.
  242. struct GoAwayFields {
  243. spdy::SpdyStreamId last_accepted_stream_id;
  244. spdy::SpdyErrorCode error_code;
  245. std::string debug_data;
  246. };
  247. std::unique_ptr<GoAwayFields> goaway_fields_;
  248. std::unique_ptr<HeaderCoalescer> coalescer_;
  249. const uint32_t max_header_list_size_;
  250. NetLogWithSource net_log_;
  251. TimeFunc time_func_;
  252. };
  253. } // namespace net
  254. #endif // NET_SPDY_BUFFERED_SPDY_FRAMER_H_