• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef QUICHE_HTTP2_ADAPTER_HTTP2_ADAPTER_H_
2 #define QUICHE_HTTP2_ADAPTER_HTTP2_ADAPTER_H_
3 
4 #include <cstdint>
5 
6 #include "absl/strings/string_view.h"
7 #include "absl/types/optional.h"
8 #include "absl/types/span.h"
9 #include "quiche/http2/adapter/data_source.h"
10 #include "quiche/http2/adapter/http2_protocol.h"
11 #include "quiche/http2/adapter/http2_session.h"
12 #include "quiche/http2/adapter/http2_visitor_interface.h"
13 #include "quiche/common/platform/api/quiche_export.h"
14 
15 namespace http2 {
16 namespace adapter {
17 
18 // Http2Adapter is an HTTP/2-processing class that exposes an interface similar
19 // to the nghttp2 library for processing the HTTP/2 wire format. As nghttp2
20 // parses HTTP/2 frames and invokes callbacks on Http2Adapter, Http2Adapter then
21 // invokes corresponding callbacks on its passed-in Http2VisitorInterface.
22 // Http2Adapter is a base class shared between client-side and server-side
23 // implementations.
24 class QUICHE_EXPORT Http2Adapter {
25  public:
26   Http2Adapter(const Http2Adapter&) = delete;
27   Http2Adapter& operator=(const Http2Adapter&) = delete;
28 
~Http2Adapter()29   virtual ~Http2Adapter() {}
30 
31   virtual bool IsServerSession() const = 0;
32 
33   virtual bool want_read() const = 0;
34   virtual bool want_write() const = 0;
35 
36   // Processes the incoming |bytes| as HTTP/2 and invokes callbacks on the
37   // |visitor_| as appropriate.
38   virtual int64_t ProcessBytes(absl::string_view bytes) = 0;
39 
40   // Submits the |settings| to be written to the peer, e.g., as part of the
41   // HTTP/2 connection preface.
42   virtual void SubmitSettings(absl::Span<const Http2Setting> settings) = 0;
43 
44   // Submits a PRIORITY frame for the given stream.
45   virtual void SubmitPriorityForStream(Http2StreamId stream_id,
46                                        Http2StreamId parent_stream_id,
47                                        int weight, bool exclusive) = 0;
48 
49   // Submits a PING on the connection.
50   virtual void SubmitPing(Http2PingId ping_id) = 0;
51 
52   // Starts a graceful shutdown. A no-op for clients.
53   virtual void SubmitShutdownNotice() = 0;
54 
55   // Submits a GOAWAY on the connection. Note that |last_accepted_stream_id|
56   // refers to stream IDs initiated by the peer. For a server sending this
57   // frame, this last stream ID must be odd (or 0).
58   virtual void SubmitGoAway(Http2StreamId last_accepted_stream_id,
59                             Http2ErrorCode error_code,
60                             absl::string_view opaque_data) = 0;
61 
62   // Submits a WINDOW_UPDATE for the given stream (a |stream_id| of 0 indicates
63   // a connection-level WINDOW_UPDATE).
64   virtual void SubmitWindowUpdate(Http2StreamId stream_id,
65                                   int window_increment) = 0;
66 
67   // Submits a RST_STREAM for the given |stream_id| and |error_code|.
68   virtual void SubmitRst(Http2StreamId stream_id,
69                          Http2ErrorCode error_code) = 0;
70 
71   // Submits a sequence of METADATA frames for the given stream. A |stream_id|
72   // of 0 indicates connection-level METADATA.
73   virtual void SubmitMetadata(Http2StreamId stream_id, size_t max_frame_size,
74                               std::unique_ptr<MetadataSource> source) = 0;
75 
76   // Invokes the visitor's OnReadyToSend() method for serialized frame data.
77   // Returns 0 on success.
78   virtual int Send() = 0;
79 
80   // Returns the connection-level flow control window advertised by the peer.
81   virtual int GetSendWindowSize() const = 0;
82 
83   // Returns the stream-level flow control window advertised by the peer.
84   virtual int GetStreamSendWindowSize(Http2StreamId stream_id) const = 0;
85 
86   // Returns the current upper bound on the flow control receive window for this
87   // stream. This value does not account for data received from the peer.
88   virtual int GetStreamReceiveWindowLimit(Http2StreamId stream_id) const = 0;
89 
90   // Returns the amount of data a peer could send on a given stream. This is
91   // the outstanding stream receive window.
92   virtual int GetStreamReceiveWindowSize(Http2StreamId stream_id) const = 0;
93 
94   // Returns the total amount of data a peer could send on the connection. This
95   // is the outstanding connection receive window.
96   virtual int GetReceiveWindowSize() const = 0;
97 
98   // Returns the size of the HPACK encoder's dynamic table, including the
99   // per-entry overhead from the specification.
100   virtual int GetHpackEncoderDynamicTableSize() const = 0;
101 
102   // Returns the size of the HPACK decoder's dynamic table, including the
103   // per-entry overhead from the specification.
104   virtual int GetHpackDecoderDynamicTableSize() const = 0;
105 
106   // Gets the highest stream ID value seen in a frame received by this endpoint.
107   // This method is only guaranteed to work for server endpoints.
108   virtual Http2StreamId GetHighestReceivedStreamId() const = 0;
109 
110   // Marks the given amount of data as consumed for the given stream, which
111   // enables the implementation layer to send WINDOW_UPDATEs as appropriate.
112   virtual void MarkDataConsumedForStream(Http2StreamId stream_id,
113                                          size_t num_bytes) = 0;
114 
115   // Returns the assigned stream ID if the operation succeeds. Otherwise,
116   // returns a negative integer indicating an error code. |data_source| may be
117   // nullptr if the request does not have a body.
118   virtual int32_t SubmitRequest(absl::Span<const Header> headers,
119                                 std::unique_ptr<DataFrameSource> data_source,
120                                 void* user_data) = 0;
121 
122   // Returns 0 on success. |data_source| may be nullptr if the response does not
123   // have a body.
124   virtual int SubmitResponse(Http2StreamId stream_id,
125                              absl::Span<const Header> headers,
126                              std::unique_ptr<DataFrameSource> data_source) = 0;
127 
128   // Queues trailers to be sent after any outstanding data on the stream with ID
129   // |stream_id|. Returns 0 on success.
130   virtual int SubmitTrailer(Http2StreamId stream_id,
131                             absl::Span<const Header> trailers) = 0;
132 
133   // Sets a user data pointer for the given stream. Can be called after
134   // SubmitRequest/SubmitResponse, or after receiving any frame for a given
135   // stream.
136   virtual void SetStreamUserData(Http2StreamId stream_id, void* user_data) = 0;
137 
138   // Returns nullptr if the stream does not exist, or if stream user data has
139   // not been set.
140   virtual void* GetStreamUserData(Http2StreamId stream_id) = 0;
141 
142   // Resumes a stream that was previously blocked (for example, due to
143   // DataFrameSource::SelectPayloadLength() returning kBlocked). Returns true if
144   // the stream was successfully resumed.
145   virtual bool ResumeStream(Http2StreamId stream_id) = 0;
146 
147  protected:
148   // Subclasses should expose a public factory method for constructing and
149   // initializing (via Initialize()) adapter instances.
Http2Adapter(Http2VisitorInterface & visitor)150   explicit Http2Adapter(Http2VisitorInterface& visitor) : visitor_(visitor) {}
151 
152   // Accessors. Do not transfer ownership.
visitor()153   Http2VisitorInterface& visitor() { return visitor_; }
154 
155  private:
156   // Http2Adapter will invoke callbacks upon the |visitor_| while processing.
157   Http2VisitorInterface& visitor_;
158 };
159 
160 }  // namespace adapter
161 }  // namespace http2
162 
163 #endif  // QUICHE_HTTP2_ADAPTER_HTTP2_ADAPTER_H_
164