blob: f71e21cd1df7abca6913c4a39c36fb84348ba4e7 [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 team509e7fa2021-06-22 08:53:08 -070086 // OnBeginHeadersForStream(). Should return "false" to indicate that the
87 // header name or value should be rejected. This will cause the HTTP
88 // transaction to fail.
89 virtual bool OnHeaderForStream(Http2StreamId stream_id, absl::string_view key,
QUICHE teamcf9d4ed2021-03-08 15:13:32 -080090 absl::string_view value) = 0;
91
92 // Called when the connection has received the complete header block for a
93 // logical HEADERS frame on a stream (which may contain CONTINUATION frames,
94 // transparent to the user).
95 virtual void OnEndHeadersForStream(Http2StreamId stream_id) = 0;
96
97 // Called when the connection receives the beginning of a DATA frame. The data
98 // payload will be provided via subsequent calls to OnDataForStream().
99 virtual void OnBeginDataForStream(Http2StreamId stream_id,
100 size_t payload_length) = 0;
101
102 // Called when the connection receives some |data| (as part of a DATA frame
103 // payload) for a stream.
104 virtual void OnDataForStream(Http2StreamId stream_id,
105 absl::string_view data) = 0;
106
107 // Called when the peer sends the END_STREAM flag on a stream, indicating that
108 // the peer will not send additional headers or data for that stream.
109 virtual void OnEndStream(Http2StreamId stream_id) = 0;
110
111 // Called when the connection receives a RST_STREAM for a stream. This call
QUICHE teamd9050872021-04-27 09:12:49 -0700112 // will be followed by either OnCloseStream().
QUICHE teamcf9d4ed2021-03-08 15:13:32 -0800113 virtual void OnRstStream(Http2StreamId stream_id,
114 Http2ErrorCode error_code) = 0;
115
QUICHE teamd9050872021-04-27 09:12:49 -0700116 // Called when a stream is closed.
117 virtual void OnCloseStream(Http2StreamId stream_id,
QUICHE teamcf9d4ed2021-03-08 15:13:32 -0800118 Http2ErrorCode error_code) = 0;
119
120 // Called when the connection receives a PRIORITY frame.
121 virtual void OnPriorityForStream(Http2StreamId stream_id,
122 Http2StreamId parent_stream_id, int weight,
123 bool exclusive) = 0;
124
125 // Called when the connection receives a PING frame.
126 virtual void OnPing(Http2PingId ping_id, bool is_ack) = 0;
127
128 // Called when the connection receives a PUSH_PROMISE frame. The server push
129 // request headers follow in calls to OnHeaderForStream() with |stream_id|.
130 virtual void OnPushPromiseForStream(Http2StreamId stream_id,
131 Http2StreamId promised_stream_id) = 0;
132
133 // Called when the connection receives a GOAWAY frame.
134 virtual void OnGoAway(Http2StreamId last_accepted_stream_id,
135 Http2ErrorCode error_code,
136 absl::string_view opaque_data) = 0;
137
138 // Called when the connection receives a WINDOW_UPDATE frame. For
139 // connection-level window updates, the |stream_id| will be 0.
140 virtual void OnWindowUpdate(Http2StreamId stream_id,
141 int window_increment) = 0;
142
QUICHE team06606d02021-06-23 10:58:40 -0700143 // Called immediately before a frame of the given type is sent. Should return
144 // 0 on success.
145 virtual int OnBeforeFrameSent(uint8_t frame_type, Http2StreamId stream_id,
146 size_t length, uint8_t flags) = 0;
147
148 // Called immediately after a frame of the given type is sent. Should return 0
149 // on success. |error_code| is only populated for RST_STREAM and GOAWAY frame
150 // types.
151 virtual int OnFrameSent(uint8_t frame_type, Http2StreamId stream_id,
152 size_t length, uint8_t flags,
153 uint32_t error_code) = 0;
154
QUICHE teamcf9d4ed2021-03-08 15:13:32 -0800155 // Called when the connection is ready to send data for a stream. The
156 // implementation should write at most |length| bytes of the data payload to
157 // the |destination_buffer| and set |end_stream| to true IFF there will be no
158 // more data sent on this stream. Sets |written| to the number of bytes
159 // written to the |destination_buffer| or a negative value if an error occurs.
160 virtual void OnReadyToSendDataForStream(Http2StreamId stream_id,
161 char* destination_buffer,
162 size_t length,
163 ssize_t* written,
164 bool* end_stream) = 0;
165
QUICHE team70624862021-06-24 10:37:15 -0700166 // Called when the connection receives an invalid frame. |error_code| is a
167 // negative integer error code generated by the library. A return value of
168 // false will result in the connection entering an error state, with no
169 // further frame processing possible.
170 virtual bool OnInvalidFrame(Http2StreamId stream_id, int error_code) = 0;
171
QUICHE teamcf9d4ed2021-03-08 15:13:32 -0800172 // Called when the connection is ready to write metadata for |stream_id| to
173 // the wire. The implementation should write at most |length| bytes of the
174 // serialized metadata payload to the |buffer| and set |written| to the number
175 // of bytes written or a negative value if there was an error.
176 virtual void OnReadyToSendMetadataForStream(Http2StreamId stream_id,
177 char* buffer, size_t length,
178 ssize_t* written) = 0;
179
180 // Called when the connection receives the beginning of a METADATA frame
181 // (which may itself be the middle of a logical metadata block). The metadata
182 // payload will be provided via subsequent calls to OnMetadataForStream().
QUICHE teamf422b222021-06-25 08:55:17 -0700183 // TODO(birenroy): Consider removing this unnecessary method.
QUICHE teamcf9d4ed2021-03-08 15:13:32 -0800184 virtual void OnBeginMetadataForStream(Http2StreamId stream_id,
185 size_t payload_length) = 0;
186
187 // Called when the connection receives |metadata| as part of a METADATA frame
188 // payload for a stream.
189 virtual void OnMetadataForStream(Http2StreamId stream_id,
190 absl::string_view metadata) = 0;
191
192 // Called when the connection has finished receiving a logical metadata block
193 // for a stream. Note that there may be multiple metadata blocks for a stream.
QUICHE teamf422b222021-06-25 08:55:17 -0700194 // Returns false if there was an error unpacking the metadata payload.
195 virtual bool OnMetadataEndForStream(Http2StreamId stream_id) = 0;
QUICHE teamcf9d4ed2021-03-08 15:13:32 -0800196
QUICHE team0b33cbb2021-06-23 12:48:46 -0700197 // Invoked with an error message from the application.
198 virtual void OnErrorDebug(absl::string_view message) = 0;
199
QUICHE teamcf9d4ed2021-03-08 15:13:32 -0800200 protected:
201 Http2VisitorInterface() = default;
202};
203
204} // namespace adapter
205} // namespace http2
206
207#endif // QUICHE_HTTP2_ADAPTER_HTTP2_VISITOR_INTERFACE_H_