blob: e3f7c53f9076dc0d671fe1bd29ff191d4e3179ea [file] [log] [blame]
QUICHE teamcf9d4ed2021-03-08 15:13:32 -08001#ifndef QUICHE_HTTP2_ADAPTER_HTTP2_VISITOR_INTERFACE_H_
2#define QUICHE_HTTP2_ADAPTER_HTTP2_VISITOR_INTERFACE_H_
3
4#include <vector>
5
6#include "absl/strings/string_view.h"
7#include "http2/adapter/http2_protocol.h"
8
9namespace http2 {
10namespace adapter {
11
12// Http2VisitorInterface contains callbacks for receiving HTTP/2-level events. A
13// processor like NghttpAdapter parses HTTP/2 frames and invokes the callbacks
14// on an instance of this interface. Prefer a void return type for these
15// callbacks, instead setting output parameters as needed.
16//
17// Example sequences of calls/events:
18// GET:
19// - OnBeginHeadersForStream()
20// - OnHeaderForStream()
21// - OnEndHeadersForStream()
22// - OnEndStream()
23//
24// POST:
25// - OnBeginHeadersForStream()
26// - OnHeaderForStream()
27// - OnEndHeadersForStream()
28// - OnBeginDataForStream()
29// - OnDataForStream()
30// - OnEndStream()
31//
32// Request canceled mid-stream, e.g, with error code CANCEL:
33// - OnBeginHeadersForStream()
34// - OnHeaderForStream()
35// - OnEndHeadersForStream()
36// - OnRstStream()
QUICHE teamd9050872021-04-27 09:12:49 -070037// - OnCloseStream()
QUICHE teamcf9d4ed2021-03-08 15:13:32 -080038//
39// Request closed mid-stream, e.g., with error code NO_ERROR:
40// - OnBeginHeadersForStream()
41// - OnHeaderForStream()
42// - OnEndHeadersForStream()
43// - OnRstStream()
44// - OnCloseStream()
45//
QUICHE teamd9050872021-04-27 09:12:49 -070046// More details are at RFC 7540 (go/http2spec).
QUICHE teamcf9d4ed2021-03-08 15:13:32 -080047class Http2VisitorInterface {
48 public:
49 Http2VisitorInterface(const Http2VisitorInterface&) = delete;
50 Http2VisitorInterface& operator=(const Http2VisitorInterface&) = delete;
51 virtual ~Http2VisitorInterface() = default;
52
QUICHE teamc82acf12021-05-21 17:54:00 -070053 // Called when there are serialized frames to send. Should return how many
54 // bytes were actually sent. Returning 0 indicates that sending is blocked.
55 // Returning -1 indicates an error.
56 virtual ssize_t OnReadyToSend(absl::string_view serialized) = 0;
57
QUICHE teamcf9d4ed2021-03-08 15:13:32 -080058 // Called when a connection-level processing error has been encountered.
59 virtual void OnConnectionError() = 0;
60
QUICHE team2121bae2021-04-30 13:05:38 -070061 // Called when the header for a frame is received.
62 virtual void OnFrameHeader(Http2StreamId stream_id,
63 size_t length,
64 uint8_t type,
65 uint8_t flags) {}
66
QUICHE teamcf9d4ed2021-03-08 15:13:32 -080067 // Called when a non-ack SETTINGS frame is received.
68 virtual void OnSettingsStart() = 0;
69
70 // Called for each SETTINGS id-value pair.
71 virtual void OnSetting(Http2Setting setting) = 0;
72
73 // Called at the end of a non-ack SETTINGS frame.
74 virtual void OnSettingsEnd() = 0;
75
76 // Called when a SETTINGS ack frame is received.
77 virtual void OnSettingsAck() = 0;
78
79 // Called when the connection receives the header block for a HEADERS frame on
80 // a stream but has not yet parsed individual headers.
81 virtual void OnBeginHeadersForStream(Http2StreamId stream_id) = 0;
82
83 // Called when the connection receives the header |key| and |value| for a
84 // stream. The HTTP/2 pseudo-headers defined in RFC 7540 Sections 8.1.2.3 and
85 // 8.1.2.4 are also conveyed in this callback. This method is called after
QUICHE team29e35dd2021-06-28 15:25:35 -070086 // OnBeginHeadersForStream(). May return HEADER_RST_STREAM to indicate the
87 // header block should be rejected. This will cause the library to queue a
88 // RST_STREAM frame, which will have a default error code of INTERNAL_ERROR.
89 // The visitor implementation may choose to queue a RST_STREAM with a
90 // different error code instead, which should be done before returning
91 // HEADER_RST_STREAM. Returning HEADER_CONNECTION_ERROR will lead to a
92 // non-recoverable error on the connection.
93 enum OnHeaderResult {
94 HEADER_OK,
95 HEADER_CONNECTION_ERROR,
96 HEADER_RST_STREAM,
97 };
98 virtual OnHeaderResult OnHeaderForStream(Http2StreamId stream_id,
99 absl::string_view key,
100 absl::string_view value) = 0;
QUICHE teamcf9d4ed2021-03-08 15:13:32 -0800101
102 // Called when the connection has received the complete header block for a
103 // logical HEADERS frame on a stream (which may contain CONTINUATION frames,
104 // transparent to the user).
105 virtual void OnEndHeadersForStream(Http2StreamId stream_id) = 0;
106
107 // Called when the connection receives the beginning of a DATA frame. The data
108 // payload will be provided via subsequent calls to OnDataForStream().
109 virtual void OnBeginDataForStream(Http2StreamId stream_id,
110 size_t payload_length) = 0;
111
112 // Called when the connection receives some |data| (as part of a DATA frame
113 // payload) for a stream.
114 virtual void OnDataForStream(Http2StreamId stream_id,
115 absl::string_view data) = 0;
116
117 // Called when the peer sends the END_STREAM flag on a stream, indicating that
118 // the peer will not send additional headers or data for that stream.
119 virtual void OnEndStream(Http2StreamId stream_id) = 0;
120
121 // Called when the connection receives a RST_STREAM for a stream. This call
QUICHE teamd9050872021-04-27 09:12:49 -0700122 // will be followed by either OnCloseStream().
QUICHE teamcf9d4ed2021-03-08 15:13:32 -0800123 virtual void OnRstStream(Http2StreamId stream_id,
124 Http2ErrorCode error_code) = 0;
125
QUICHE teamd9050872021-04-27 09:12:49 -0700126 // Called when a stream is closed.
127 virtual void OnCloseStream(Http2StreamId stream_id,
QUICHE teamcf9d4ed2021-03-08 15:13:32 -0800128 Http2ErrorCode error_code) = 0;
129
130 // Called when the connection receives a PRIORITY frame.
131 virtual void OnPriorityForStream(Http2StreamId stream_id,
132 Http2StreamId parent_stream_id, int weight,
133 bool exclusive) = 0;
134
135 // Called when the connection receives a PING frame.
136 virtual void OnPing(Http2PingId ping_id, bool is_ack) = 0;
137
138 // Called when the connection receives a PUSH_PROMISE frame. The server push
139 // request headers follow in calls to OnHeaderForStream() with |stream_id|.
140 virtual void OnPushPromiseForStream(Http2StreamId stream_id,
141 Http2StreamId promised_stream_id) = 0;
142
143 // Called when the connection receives a GOAWAY frame.
144 virtual void OnGoAway(Http2StreamId last_accepted_stream_id,
145 Http2ErrorCode error_code,
146 absl::string_view opaque_data) = 0;
147
148 // Called when the connection receives a WINDOW_UPDATE frame. For
149 // connection-level window updates, the |stream_id| will be 0.
150 virtual void OnWindowUpdate(Http2StreamId stream_id,
151 int window_increment) = 0;
152
QUICHE team06606d02021-06-23 10:58:40 -0700153 // Called immediately before a frame of the given type is sent. Should return
154 // 0 on success.
155 virtual int OnBeforeFrameSent(uint8_t frame_type, Http2StreamId stream_id,
156 size_t length, uint8_t flags) = 0;
157
158 // Called immediately after a frame of the given type is sent. Should return 0
159 // on success. |error_code| is only populated for RST_STREAM and GOAWAY frame
160 // types.
161 virtual int OnFrameSent(uint8_t frame_type, Http2StreamId stream_id,
162 size_t length, uint8_t flags,
163 uint32_t error_code) = 0;
164
QUICHE teamcf9d4ed2021-03-08 15:13:32 -0800165 // Called when the connection is ready to send data for a stream. The
166 // implementation should write at most |length| bytes of the data payload to
167 // the |destination_buffer| and set |end_stream| to true IFF there will be no
168 // more data sent on this stream. Sets |written| to the number of bytes
169 // written to the |destination_buffer| or a negative value if an error occurs.
170 virtual void OnReadyToSendDataForStream(Http2StreamId stream_id,
171 char* destination_buffer,
172 size_t length,
173 ssize_t* written,
174 bool* end_stream) = 0;
175
QUICHE team70624862021-06-24 10:37:15 -0700176 // Called when the connection receives an invalid frame. |error_code| is a
177 // negative integer error code generated by the library. A return value of
178 // false will result in the connection entering an error state, with no
179 // further frame processing possible.
180 virtual bool OnInvalidFrame(Http2StreamId stream_id, int error_code) = 0;
181
QUICHE teamcf9d4ed2021-03-08 15:13:32 -0800182 // Called when the connection is ready to write metadata for |stream_id| to
183 // the wire. The implementation should write at most |length| bytes of the
184 // serialized metadata payload to the |buffer| and set |written| to the number
185 // of bytes written or a negative value if there was an error.
186 virtual void OnReadyToSendMetadataForStream(Http2StreamId stream_id,
187 char* buffer, size_t length,
188 ssize_t* written) = 0;
189
190 // Called when the connection receives the beginning of a METADATA frame
191 // (which may itself be the middle of a logical metadata block). The metadata
192 // payload will be provided via subsequent calls to OnMetadataForStream().
QUICHE teamf422b222021-06-25 08:55:17 -0700193 // TODO(birenroy): Consider removing this unnecessary method.
QUICHE teamcf9d4ed2021-03-08 15:13:32 -0800194 virtual void OnBeginMetadataForStream(Http2StreamId stream_id,
195 size_t payload_length) = 0;
196
197 // Called when the connection receives |metadata| as part of a METADATA frame
198 // payload for a stream.
199 virtual void OnMetadataForStream(Http2StreamId stream_id,
200 absl::string_view metadata) = 0;
201
202 // Called when the connection has finished receiving a logical metadata block
203 // for a stream. Note that there may be multiple metadata blocks for a stream.
QUICHE teamf422b222021-06-25 08:55:17 -0700204 // Returns false if there was an error unpacking the metadata payload.
205 virtual bool OnMetadataEndForStream(Http2StreamId stream_id) = 0;
QUICHE teamcf9d4ed2021-03-08 15:13:32 -0800206
QUICHE team0b33cbb2021-06-23 12:48:46 -0700207 // Invoked with an error message from the application.
208 virtual void OnErrorDebug(absl::string_view message) = 0;
209
QUICHE teamcf9d4ed2021-03-08 15:13:32 -0800210 protected:
211 Http2VisitorInterface() = default;
212};
213
214} // namespace adapter
215} // namespace http2
216
217#endif // QUICHE_HTTP2_ADAPTER_HTTP2_VISITOR_INTERFACE_H_