include/fetch/ft_h2.h

filter: Statementstmt 25.0% 1 / 4 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 25.0% 1 / 4 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/cookiejar.xs ↑ Summary include/fetch/backend_poll.c »
1 #ifndef FT_H2_H
2 #define FT_H2_H
3  
4 /* HTTP/2 client via nghttp2: one connection carries one request stream (for
5 * now - multiplexing across a pooled connection comes later). nghttp2 owns
6 * framing, HPACK and flow control; we feed it received bytes and flush its
7 * output through the same (possibly TLS) socket. The response resolves the
8 * connection's Fetch::Future with a Fetch::Response.
9 *
10 * Included from ft_http.h after ft_recv/ft_send/ft_conn_finish/ft_conn_fail.
11 * Compiles to stubs without -DFT_HAVE_NGHTTP2. */
12  
13 #ifdef FT_HAVE_NGHTTP2
14  
15 #include <nghttp2/nghttp2.h>
16  
17 /* nghttp2 wants our socket writes here; return bytes sent or a would-block. */
18 static ssize_t ft_h2_send_cb(nghttp2_session *s, const uint8_t *data,
19 size_t length, int flags, void *user) {
20 ft_conn *c = (ft_conn *)user;
21 int want = 0;
22 ssize_t n;
23 dTHX;
24 (void)s; (void)flags;
25 n = ft_send(c, data, length, &want);
26 if (n > 0) return n;
27 if (n < 0 && want) return NGHTTP2_ERR_WOULDBLOCK;
28 return NGHTTP2_ERR_CALLBACK_FAILURE;
29 }
30  
31 static int ft_h2_header_cb(nghttp2_session *s, const nghttp2_frame *frame,
32 const uint8_t *name, size_t namelen,
33 const uint8_t *value, size_t valuelen,
34 uint8_t flags, void *user) {
35 ft_conn *c = (ft_conn *)user;
36 dTHX;
37 (void)s; (void)flags;
38 if (frame->hd.type != NGHTTP2_HEADERS) return 0;
39  
40 /* TRAILERS: a second HEADERS frame, after the DATA.
41 *
42 * These used to be discarded along with everything that was not
43 * HCAT_RESPONSE, which is fine for ordinary HTTP - almost nothing uses
44 * trailers - and fatal for gRPC, where the CALL STATUS lives in them and
45 * nowhere else. A gRPC response is HTTP 200 whether it succeeded or
46 * failed; a client that cannot read trailers can only ever report
47 * success. Hence a separate list rather than merging them into the
48 * response headers: a trailer arrived after the body and a consumer that
49 * cares about the difference must be able to tell. */
50 if (frame->headers.cat == NGHTTP2_HCAT_HEADERS) {
51 if (namelen && name[0] != ':') {
52 if (!c->trailers) c->trailers = newAV();
53 av_push(c->trailers, newSVpvn((const char *)name, namelen));
54 av_push(c->trailers, newSVpvn((const char *)value, valuelen));
55 }
56 return 0;
57 }
58 if (frame->headers.cat != NGHTTP2_HCAT_RESPONSE) return 0;
59  
60 if (namelen == 7 && memcmp(name, ":status", 7) == 0) {
61 c->status = (int)strtol((const char *)value, NULL, 10);
62 } else if (namelen && name[0] != ':') { /* skip other pseudo-headers */
63 if (!c->headers) c->headers = newAV();
64 av_push(c->headers, newSVpvn((const char *)name, namelen));
65 av_push(c->headers, newSVpvn((const char *)value, valuelen));
66 }
67 return 0;
68 }
69  
70 static int ft_h2_data_cb(nghttp2_session *s, uint8_t flags, int32_t stream_id,
71 const uint8_t *data, size_t len, void *user) {
72 ft_conn *c = (ft_conn *)user;
73 dTHX;
74 (void)s; (void)flags; (void)stream_id;
75 if (c->on_body) { /* stream: emit, do not buffer */
76 ft_emit_body(aTHX_ c, (const char *)data, len);
77 c->body_recv += len;
78 return 0;
79 }
80 if (c->dblen + len > c->dbcap) {
81 c->dbcap = (c->dblen + len) * 2 + 64;
82 Renew(c->dbody, c->dbcap, char);
83 }
84 memcpy(c->dbody + c->dblen, data, len);
85 c->dblen += len;
86 return 0;
87 }
88  
89 /* Notes the END_STREAM flag on a HEADERS or DATA frame of the request's
90 * stream: the server has finished its response, whether that was a
91 * body-less reply, the last DATA frame, or a trailing HEADERS frame. */
92 static int ft_h2_on_frame(nghttp2_session *s, const nghttp2_frame *frame,
93 void *user) {
94 ft_conn *c = (ft_conn *)user;
95 (void)s;
96 if (frame->hd.stream_id > 0
97 && (frame->hd.type == NGHTTP2_DATA || frame->hd.type == NGHTTP2_HEADERS)
98 && (frame->hd.flags & NGHTTP2_FLAG_END_STREAM))
99 c->h2_ended = 1;
100 return 0;
101 }
102  
103 /* A stream closing is not a response arriving. RST_STREAM closes it too, at
104 * any point from before the headers to half way through the body, and so
105 * does a GOAWAY that refuses it. Treating every close as a response turned
106 * those into a status-0 response or into a short body under a genuine 200,
107 * which is a success report for a failure. The code is kept for
108 * ft_h2_stream_over to judge by. */
109 static int ft_h2_stream_close_cb(nghttp2_session *s, int32_t stream_id,
110 uint32_t error_code, void *user) {
111 ft_conn *c = (ft_conn *)user;
112 (void)s; (void)stream_id;
113 c->h2_done = 1;
114 c->h2_close_code = error_code;
115 return 0;
116 }
117  
118 /* The stream has closed. If END_STREAM was seen the response is complete
119 * and the close code does not matter: RFC 9113 8.1 lets a server finish its
120 * response and then RST_STREAM(NO_ERROR) to stop a request body it has no
121 * use for. Without END_STREAM the request fails and the message carries the
122 * code, because what arrived was not the response: before the headers there
123 * is no status, and after them the body stops early. */
124 static void ft_h2_stream_over(pTHX_ ft_conn *c) {
125 char msg[160];
126 if (c->h2_ended) { ft_conn_finish(aTHX_ c); return; }
127 snprintf(msg, sizeof msg, "http/2 stream reset by the server before the "
128 "response was complete (%s)",
129 c->h2_close_code ? nghttp2_http2_strerror(c->h2_close_code)
130 : "closed without END_STREAM");
131 ft_conn_fail(aTHX_ c, msg);
132 }
133  
134 /* request-body data provider for POST/PUT over h2 */
135 static ssize_t ft_h2_body_read(nghttp2_session *s, int32_t stream_id,
136 uint8_t *buf, size_t length, uint32_t *data_flags,
137 nghttp2_data_source *source, void *user) {
138 ft_conn *c = (ft_conn *)user;
139 STRLEN blen = 0;
140 const char *b;
141 size_t take;
142 dTHX;
143 (void)s; (void)stream_id; (void)source;
144 if (!c->rq_body) { *data_flags |= NGHTTP2_DATA_FLAG_EOF; return 0; }
145 b = SvPV(c->rq_body, blen);
146 take = (blen - c->rq_body_off < length) ? (blen - c->rq_body_off) : length;
147 memcpy(buf, b + c->rq_body_off, take);
148 c->rq_body_off += take;
149 if (c->rq_body_off >= blen) *data_flags |= NGHTTP2_DATA_FLAG_EOF;
150 return (ssize_t)take;
151 }
152  
153 #define FT_NV(NAME, VAL, VLEN) \
154 { (uint8_t *)(NAME), (uint8_t *)(VAL), sizeof(NAME) - 1, (VLEN), \
155 NGHTTP2_NV_FLAG_NONE }
156  
157 /* Initialise the h2 session and submit the request. Returns 0 / -1. */
158 static int ft_h2_init(pTHX_ ft_conn *c) {
159 nghttp2_session_callbacks *cbs;
160 nghttp2_session *sess;
161 nghttp2_nv *nva;
162 int nvn = 0, i, rv;
163 STRLEN ml, sl, al, pl;
164 const char *m = SvPV(c->rq_method, ml);
165 const char *sc= SvPV(c->rq_scheme, sl);
166 const char *au= SvPV(c->rq_authority, al);
167 const char *pa= SvPV(c->rq_path, pl);
168 SSize_t extra = c->rq_headers ? (av_len(c->rq_headers) + 1) / 2 : 0;
169 int32_t sid;
170 nghttp2_data_provider prd, *prdp = NULL;
171  
172 nghttp2_session_callbacks_new(&cbs);
173 nghttp2_session_callbacks_set_send_callback(cbs, ft_h2_send_cb);
174 nghttp2_session_callbacks_set_on_header_callback(cbs, ft_h2_header_cb);
175 nghttp2_session_callbacks_set_on_data_chunk_recv_callback(cbs, ft_h2_data_cb);
176 nghttp2_session_callbacks_set_on_stream_close_callback(cbs, ft_h2_stream_close_cb);
177 nghttp2_session_callbacks_set_on_frame_recv_callback(cbs, ft_h2_on_frame);
178 rv = nghttp2_session_client_new(&sess, cbs, c);
179 nghttp2_session_callbacks_del(cbs);
180 if (rv != 0) return -1;
181 c->h2 = sess;
182  
183 /* The client connection preface is the 24-byte magic (nghttp2 emits it
184 * automatically) followed by a SETTINGS frame, which we must submit
185 * ourselves - without it the peer sees magic then HEADERS and aborts
186 * with "SETTINGS expected". */
187 {
188 nghttp2_settings_entry iv[1] = {
189 { NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100 }
190 };
191 if (nghttp2_submit_settings(sess, NGHTTP2_FLAG_NONE, iv, 1) != 0)
192 return -1;
193 }
194  
195 Newx(nva, 4 + extra, nghttp2_nv);
196 { nghttp2_nv a = FT_NV(":method", m, ml); nva[nvn++] = a; }
197 { nghttp2_nv a = FT_NV(":scheme", sc, sl); nva[nvn++] = a; }
198 { nghttp2_nv a = FT_NV(":authority", au, al); nva[nvn++] = a; }
199 { nghttp2_nv a = FT_NV(":path", pa, pl); nva[nvn++] = a; }
200 if (c->rq_headers) {
201 SSize_t j;
202 for (j = 0; j + 1 <= av_len(c->rq_headers); j += 2) {
203 SV **ks = av_fetch(c->rq_headers, j, 0);
204 SV **vs = av_fetch(c->rq_headers, j + 1, 0);
205 STRLEN kl, vl;
206 char *k = ks ? SvPV(*ks, kl) : (char *)"";
207 char *v = vs ? SvPV(*vs, vl) : (char *)"";
208 if (!ks) kl = 0; if (!vs) vl = 0;
209 nva[nvn].name = (uint8_t *)k; nva[nvn].namelen = kl;
210 nva[nvn].value = (uint8_t *)v; nva[nvn].valuelen = vl;
211 nva[nvn].flags = NGHTTP2_NV_FLAG_NONE;
212 nvn++;
213 }
214 }
215 if (c->rq_body && SvOK(c->rq_body) && SvCUR(c->rq_body)) {
216 prd.source.ptr = NULL;
217 prd.read_callback = ft_h2_body_read;
218 prdp = &prd;
219 }
220 sid = nghttp2_submit_request(sess, NULL, nva, nvn, prdp, c);
221 Safefree(nva);
222 (void)i;
223 if (sid < 0) return -1;
224 return 0;
225 }
226  
227 /* Flush pending frames; arm the loop for what nghttp2 wants next. Returns
228 * 0 keep going, -1 fatal. */
229 static int ft_h2_flush(pTHX_ ft_conn *c) {
230 nghttp2_session *s = (nghttp2_session *)c->h2;
231 int rv = nghttp2_session_send(s);
232 if (rv != 0) return -1;
233 {
234 int mask = 0;
235 if (nghttp2_session_want_read(s)) mask |= HM_EV_READ;
236 if (nghttp2_session_want_write(s)) mask |= HM_EV_WRITE;
237 if (!mask) mask = HM_EV_READ; /* awaiting the response */
238 ft_arm(aTHX_ c, mask);
239 }
240 return 0;
241 }
242  
243 /* Drive one readiness pass in h2 mode. */
244 static void ft_h2_step(pTHX_ ft_conn *c) {
245 /* write out anything pending first (also flushes the request) */
246 if (ft_h2_flush(aTHX_ c) < 0) { ft_conn_fail(aTHX_ c, "http/2 send failed"); return; }
247 for (;;) {
248 int want = 0;
249 ssize_t n;
250 unsigned char buf[65536];
251 n = ft_recv(c, buf, sizeof(buf), &want);
252 if (n > 0) {
253 ssize_t rd = nghttp2_session_mem_recv((nghttp2_session *)c->h2,
254 buf, (size_t)n);
255 if (rd < 0) { ft_conn_fail(aTHX_ c, "http/2 protocol error"); return; }
256 if (ft_h2_flush(aTHX_ c) < 0) { ft_conn_fail(aTHX_ c, "http/2 send failed"); return; }
257 if (c->h2_done) { ft_h2_stream_over(aTHX_ c); return; }
258 continue;
259 }
260 if (n == 0) {
261 if (c->h2_done) ft_h2_stream_over(aTHX_ c);
262 else ft_conn_fail(aTHX_ c, "connection closed before the h2 stream completed");
263 return;
264 }
265 if (want) { ft_arm(aTHX_ c, want); return; }
266 if (errno == EINTR) continue;
267 ft_conn_fail(aTHX_ c, strerror(errno));
268 return;
269 }
270 }
271  
272 static void ft_h2_free(ft_conn *c) {
273 if (c->h2) { nghttp2_session_del((nghttp2_session *)c->h2); c->h2 = NULL; }
274 }
275  
276 #define FT_H2_AVAILABLE 1
277  
278 #else /* stubs */
279  
280 0 static int ft_h2_init(pTHX_ ft_conn *c) { (void)c; return -1; }
281 0 static void ft_h2_step(pTHX_ ft_conn *c) {
282 0 ft_conn_fail(aTHX_ c, "HTTP/2 not built (need nghttp2)"); }
283 122 static void ft_h2_free(ft_conn *c) { (void)c; }
284 #define FT_H2_AVAILABLE 0
285  
286 #endif
287  
288 #endif /* FT_H2_H */
« xs/cookiejar.xs ↑ Summary include/fetch/backend_poll.c »