Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "curl_setup.h" |
26 | | |
27 | | #ifdef USE_NGHTTP2 |
28 | | #include <stdint.h> |
29 | | #include <nghttp2/nghttp2.h> |
30 | | #include "urldata.h" |
31 | | #include "bufq.h" |
32 | | #include "http1.h" |
33 | | #include "http2.h" |
34 | | #include "http.h" |
35 | | #include "sendf.h" |
36 | | #include "select.h" |
37 | | #include "curl_base64.h" |
38 | | #include "strcase.h" |
39 | | #include "multiif.h" |
40 | | #include "url.h" |
41 | | #include "urlapi-int.h" |
42 | | #include "cfilters.h" |
43 | | #include "connect.h" |
44 | | #include "rand.h" |
45 | | #include "strtoofft.h" |
46 | | #include "strdup.h" |
47 | | #include "transfer.h" |
48 | | #include "dynbuf.h" |
49 | | #include "headers.h" |
50 | | /* The last 3 #include files should be in this order */ |
51 | | #include "curl_printf.h" |
52 | | #include "curl_memory.h" |
53 | | #include "memdebug.h" |
54 | | |
55 | | #if (NGHTTP2_VERSION_NUM < 0x010c00) |
56 | | #error too old nghttp2 version, upgrade! |
57 | | #endif |
58 | | |
59 | | #ifdef CURL_DISABLE_VERBOSE_STRINGS |
60 | | #define nghttp2_session_callbacks_set_error_callback(x,y) |
61 | | #endif |
62 | | |
63 | | #if (NGHTTP2_VERSION_NUM >= 0x010c00) |
64 | | #define NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE 1 |
65 | | #endif |
66 | | |
67 | | |
68 | | /* buffer dimensioning: |
69 | | * use 16K as chunk size, as that fits H2 DATA frames well */ |
70 | 0 | #define H2_CHUNK_SIZE (16 * 1024) |
71 | | /* this is how much we want "in flight" for a stream */ |
72 | 0 | #define H2_STREAM_WINDOW_SIZE (10 * 1024 * 1024) |
73 | | /* on receiving from TLS, we prep for holding a full stream window */ |
74 | 0 | #define H2_NW_RECV_CHUNKS (H2_STREAM_WINDOW_SIZE / H2_CHUNK_SIZE) |
75 | | /* on send into TLS, we just want to accumulate small frames */ |
76 | 0 | #define H2_NW_SEND_CHUNKS 1 |
77 | | /* stream recv/send chunks are a result of window / chunk sizes */ |
78 | 0 | #define H2_STREAM_RECV_CHUNKS (H2_STREAM_WINDOW_SIZE / H2_CHUNK_SIZE) |
79 | | /* keep smaller stream upload buffer (default h2 window size) to have |
80 | | * our progress bars and "upload done" reporting closer to reality */ |
81 | 0 | #define H2_STREAM_SEND_CHUNKS ((64 * 1024) / H2_CHUNK_SIZE) |
82 | | /* spare chunks we keep for a full window */ |
83 | 0 | #define H2_STREAM_POOL_SPARES (H2_STREAM_WINDOW_SIZE / H2_CHUNK_SIZE) |
84 | | |
85 | | /* We need to accommodate the max number of streams with their window |
86 | | * sizes on the overall connection. Streams might become PAUSED which |
87 | | * will block their received QUOTA in the connection window. And if we |
88 | | * run out of space, the server is blocked from sending us any data. |
89 | | * See #10988 for an issue with this. */ |
90 | 0 | #define HTTP2_HUGE_WINDOW_SIZE (100 * H2_STREAM_WINDOW_SIZE) |
91 | | |
92 | | #define H2_SETTINGS_IV_LEN 3 |
93 | 0 | #define H2_BINSETTINGS_LEN 80 |
94 | | |
95 | | static int populate_settings(nghttp2_settings_entry *iv, |
96 | | struct Curl_easy *data) |
97 | 0 | { |
98 | 0 | iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; |
99 | 0 | iv[0].value = Curl_multi_max_concurrent_streams(data->multi); |
100 | |
|
101 | 0 | iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE; |
102 | 0 | iv[1].value = H2_STREAM_WINDOW_SIZE; |
103 | |
|
104 | 0 | iv[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH; |
105 | 0 | iv[2].value = data->multi->push_cb != NULL; |
106 | |
|
107 | 0 | return 3; |
108 | 0 | } |
109 | | |
110 | | static ssize_t populate_binsettings(uint8_t *binsettings, |
111 | | struct Curl_easy *data) |
112 | 0 | { |
113 | 0 | nghttp2_settings_entry iv[H2_SETTINGS_IV_LEN]; |
114 | 0 | int ivlen; |
115 | |
|
116 | 0 | ivlen = populate_settings(iv, data); |
117 | | /* this returns number of bytes it wrote or a negative number on error. */ |
118 | 0 | return nghttp2_pack_settings_payload(binsettings, H2_BINSETTINGS_LEN, |
119 | 0 | iv, ivlen); |
120 | 0 | } |
121 | | |
122 | | struct cf_h2_ctx { |
123 | | nghttp2_session *h2; |
124 | | uint32_t max_concurrent_streams; |
125 | | /* The easy handle used in the current filter call, cleared at return */ |
126 | | struct cf_call_data call_data; |
127 | | |
128 | | struct bufq inbufq; /* network input */ |
129 | | struct bufq outbufq; /* network output */ |
130 | | struct bufc_pool stream_bufcp; /* spares for stream buffers */ |
131 | | |
132 | | size_t drain_total; /* sum of all stream's UrlState drain */ |
133 | | int32_t goaway_error; |
134 | | int32_t last_stream_id; |
135 | | BIT(conn_closed); |
136 | | BIT(goaway); |
137 | | BIT(enable_push); |
138 | | BIT(nw_out_blocked); |
139 | | }; |
140 | | |
141 | | /* How to access `call_data` from a cf_h2 filter */ |
142 | | #undef CF_CTX_CALL_DATA |
143 | | #define CF_CTX_CALL_DATA(cf) \ |
144 | 0 | ((struct cf_h2_ctx *)(cf)->ctx)->call_data |
145 | | |
146 | | static void cf_h2_ctx_clear(struct cf_h2_ctx *ctx) |
147 | 0 | { |
148 | 0 | struct cf_call_data save = ctx->call_data; |
149 | |
|
150 | 0 | if(ctx->h2) { |
151 | 0 | nghttp2_session_del(ctx->h2); |
152 | 0 | } |
153 | 0 | Curl_bufq_free(&ctx->inbufq); |
154 | 0 | Curl_bufq_free(&ctx->outbufq); |
155 | 0 | Curl_bufcp_free(&ctx->stream_bufcp); |
156 | 0 | memset(ctx, 0, sizeof(*ctx)); |
157 | 0 | ctx->call_data = save; |
158 | 0 | } |
159 | | |
160 | | static void cf_h2_ctx_free(struct cf_h2_ctx *ctx) |
161 | 0 | { |
162 | 0 | if(ctx) { |
163 | 0 | cf_h2_ctx_clear(ctx); |
164 | 0 | free(ctx); |
165 | 0 | } |
166 | 0 | } |
167 | | |
168 | | static CURLcode h2_progress_egress(struct Curl_cfilter *cf, |
169 | | struct Curl_easy *data); |
170 | | |
171 | | /** |
172 | | * All about the H3 internals of a stream |
173 | | */ |
174 | | struct stream_ctx { |
175 | | /*********** for HTTP/2 we store stream-local data here *************/ |
176 | | int32_t id; /* HTTP/2 protocol identifier for stream */ |
177 | | struct bufq recvbuf; /* response buffer */ |
178 | | struct bufq sendbuf; /* request buffer */ |
179 | | struct h1_req_parser h1; /* parsing the request */ |
180 | | struct dynhds resp_trailers; /* response trailer fields */ |
181 | | size_t resp_hds_len; /* amount of response header bytes in recvbuf */ |
182 | | size_t upload_blocked_len; |
183 | | curl_off_t upload_left; /* number of request bytes left to upload */ |
184 | | |
185 | | char **push_headers; /* allocated array */ |
186 | | size_t push_headers_used; /* number of entries filled in */ |
187 | | size_t push_headers_alloc; /* number of entries allocated */ |
188 | | |
189 | | int status_code; /* HTTP response status code */ |
190 | | uint32_t error; /* stream error code */ |
191 | | uint32_t local_window_size; /* the local recv window size */ |
192 | | bool resp_hds_complete; /* we have a complete, final response */ |
193 | | bool closed; /* TRUE on stream close */ |
194 | | bool reset; /* TRUE on stream reset */ |
195 | | bool close_handled; /* TRUE if stream closure is handled by libcurl */ |
196 | | bool bodystarted; |
197 | | bool send_closed; /* transfer is done sending, we might have still |
198 | | buffered data in stream->sendbuf to upload. */ |
199 | | }; |
200 | | |
201 | 0 | #define H2_STREAM_CTX(d) ((struct stream_ctx *)(((d) && (d)->req.p.http)? \ |
202 | 0 | ((struct HTTP *)(d)->req.p.http)->h2_ctx \ |
203 | 0 | : NULL)) |
204 | 0 | #define H2_STREAM_LCTX(d) ((struct HTTP *)(d)->req.p.http)->h2_ctx |
205 | | #define H2_STREAM_ID(d) (H2_STREAM_CTX(d)? \ |
206 | | H2_STREAM_CTX(d)->id : -2) |
207 | | |
208 | | /* |
209 | | * Mark this transfer to get "drained". |
210 | | */ |
211 | | static void drain_stream(struct Curl_cfilter *cf, |
212 | | struct Curl_easy *data, |
213 | | struct stream_ctx *stream) |
214 | 0 | { |
215 | 0 | unsigned char bits; |
216 | |
|
217 | 0 | (void)cf; |
218 | 0 | bits = CURL_CSELECT_IN; |
219 | 0 | if(!stream->send_closed && |
220 | 0 | (stream->upload_left || stream->upload_blocked_len)) |
221 | 0 | bits |= CURL_CSELECT_OUT; |
222 | 0 | if(data->state.dselect_bits != bits) { |
223 | 0 | CURL_TRC_CF(data, cf, "[%d] DRAIN dselect_bits=%x", |
224 | 0 | stream->id, bits); |
225 | 0 | data->state.dselect_bits = bits; |
226 | 0 | Curl_expire(data, 0, EXPIRE_RUN_NOW); |
227 | 0 | } |
228 | 0 | } |
229 | | |
230 | | static CURLcode http2_data_setup(struct Curl_cfilter *cf, |
231 | | struct Curl_easy *data, |
232 | | struct stream_ctx **pstream) |
233 | 0 | { |
234 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
235 | 0 | struct stream_ctx *stream; |
236 | |
|
237 | 0 | (void)cf; |
238 | 0 | DEBUGASSERT(data); |
239 | 0 | if(!data->req.p.http) { |
240 | 0 | failf(data, "initialization failure, transfer not http initialized"); |
241 | 0 | return CURLE_FAILED_INIT; |
242 | 0 | } |
243 | 0 | stream = H2_STREAM_CTX(data); |
244 | 0 | if(stream) { |
245 | 0 | *pstream = stream; |
246 | 0 | return CURLE_OK; |
247 | 0 | } |
248 | | |
249 | 0 | stream = calloc(1, sizeof(*stream)); |
250 | 0 | if(!stream) |
251 | 0 | return CURLE_OUT_OF_MEMORY; |
252 | | |
253 | 0 | stream->id = -1; |
254 | 0 | Curl_bufq_initp(&stream->sendbuf, &ctx->stream_bufcp, |
255 | 0 | H2_STREAM_SEND_CHUNKS, BUFQ_OPT_NONE); |
256 | 0 | Curl_bufq_initp(&stream->recvbuf, &ctx->stream_bufcp, |
257 | 0 | H2_STREAM_RECV_CHUNKS, BUFQ_OPT_SOFT_LIMIT); |
258 | 0 | Curl_h1_req_parse_init(&stream->h1, H1_PARSE_DEFAULT_MAX_LINE_LEN); |
259 | 0 | Curl_dynhds_init(&stream->resp_trailers, 0, DYN_HTTP_REQUEST); |
260 | 0 | stream->resp_hds_len = 0; |
261 | 0 | stream->bodystarted = FALSE; |
262 | 0 | stream->status_code = -1; |
263 | 0 | stream->closed = FALSE; |
264 | 0 | stream->close_handled = FALSE; |
265 | 0 | stream->error = NGHTTP2_NO_ERROR; |
266 | 0 | stream->local_window_size = H2_STREAM_WINDOW_SIZE; |
267 | 0 | stream->upload_left = 0; |
268 | |
|
269 | 0 | H2_STREAM_LCTX(data) = stream; |
270 | 0 | *pstream = stream; |
271 | 0 | return CURLE_OK; |
272 | 0 | } |
273 | | |
274 | | static void http2_data_done(struct Curl_cfilter *cf, |
275 | | struct Curl_easy *data, bool premature) |
276 | 0 | { |
277 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
278 | 0 | struct stream_ctx *stream = H2_STREAM_CTX(data); |
279 | |
|
280 | 0 | DEBUGASSERT(ctx); |
281 | 0 | (void)premature; |
282 | 0 | if(!stream) |
283 | 0 | return; |
284 | | |
285 | 0 | if(ctx->h2) { |
286 | 0 | if(!stream->closed && stream->id > 0) { |
287 | | /* RST_STREAM */ |
288 | 0 | CURL_TRC_CF(data, cf, "[%d] premature DATA_DONE, RST stream", |
289 | 0 | stream->id); |
290 | 0 | if(!nghttp2_submit_rst_stream(ctx->h2, NGHTTP2_FLAG_NONE, |
291 | 0 | stream->id, NGHTTP2_STREAM_CLOSED)) |
292 | 0 | (void)nghttp2_session_send(ctx->h2); |
293 | 0 | } |
294 | 0 | if(!Curl_bufq_is_empty(&stream->recvbuf)) { |
295 | | /* Anything in the recvbuf is still being counted |
296 | | * in stream and connection window flow control. Need |
297 | | * to free that space or the connection window might get |
298 | | * exhausted eventually. */ |
299 | 0 | nghttp2_session_consume(ctx->h2, stream->id, |
300 | 0 | Curl_bufq_len(&stream->recvbuf)); |
301 | | /* give WINDOW_UPATE a chance to be sent, but ignore any error */ |
302 | 0 | (void)h2_progress_egress(cf, data); |
303 | 0 | } |
304 | | |
305 | | /* -1 means unassigned and 0 means cleared */ |
306 | 0 | if(nghttp2_session_get_stream_user_data(ctx->h2, stream->id)) { |
307 | 0 | int rv = nghttp2_session_set_stream_user_data(ctx->h2, |
308 | 0 | stream->id, 0); |
309 | 0 | if(rv) { |
310 | 0 | infof(data, "http/2: failed to clear user_data for stream %u", |
311 | 0 | stream->id); |
312 | 0 | DEBUGASSERT(0); |
313 | 0 | } |
314 | 0 | } |
315 | 0 | } |
316 | | |
317 | 0 | Curl_bufq_free(&stream->sendbuf); |
318 | 0 | Curl_bufq_free(&stream->recvbuf); |
319 | 0 | Curl_h1_req_parse_free(&stream->h1); |
320 | 0 | Curl_dynhds_free(&stream->resp_trailers); |
321 | 0 | if(stream->push_headers) { |
322 | | /* if they weren't used and then freed before */ |
323 | 0 | for(; stream->push_headers_used > 0; --stream->push_headers_used) { |
324 | 0 | free(stream->push_headers[stream->push_headers_used - 1]); |
325 | 0 | } |
326 | 0 | free(stream->push_headers); |
327 | 0 | stream->push_headers = NULL; |
328 | 0 | } |
329 | |
|
330 | 0 | free(stream); |
331 | 0 | H2_STREAM_LCTX(data) = NULL; |
332 | 0 | } |
333 | | |
334 | | static int h2_client_new(struct Curl_cfilter *cf, |
335 | | nghttp2_session_callbacks *cbs) |
336 | 0 | { |
337 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
338 | 0 | nghttp2_option *o; |
339 | |
|
340 | 0 | int rc = nghttp2_option_new(&o); |
341 | 0 | if(rc) |
342 | 0 | return rc; |
343 | | /* We handle window updates ourself to enforce buffer limits */ |
344 | 0 | nghttp2_option_set_no_auto_window_update(o, 1); |
345 | | #if NGHTTP2_VERSION_NUM >= 0x013200 |
346 | | /* with 1.50.0 */ |
347 | | /* turn off RFC 9113 leading and trailing white spaces validation against |
348 | | HTTP field value. */ |
349 | | nghttp2_option_set_no_rfc9113_leading_and_trailing_ws_validation(o, 1); |
350 | | #endif |
351 | 0 | rc = nghttp2_session_client_new2(&ctx->h2, cbs, cf, o); |
352 | 0 | nghttp2_option_del(o); |
353 | 0 | return rc; |
354 | 0 | } |
355 | | |
356 | | static ssize_t nw_in_reader(void *reader_ctx, |
357 | | unsigned char *buf, size_t buflen, |
358 | | CURLcode *err) |
359 | 0 | { |
360 | 0 | struct Curl_cfilter *cf = reader_ctx; |
361 | 0 | struct Curl_easy *data = CF_DATA_CURRENT(cf); |
362 | |
|
363 | 0 | return Curl_conn_cf_recv(cf->next, data, (char *)buf, buflen, err); |
364 | 0 | } |
365 | | |
366 | | static ssize_t nw_out_writer(void *writer_ctx, |
367 | | const unsigned char *buf, size_t buflen, |
368 | | CURLcode *err) |
369 | 0 | { |
370 | 0 | struct Curl_cfilter *cf = writer_ctx; |
371 | 0 | struct Curl_easy *data = CF_DATA_CURRENT(cf); |
372 | |
|
373 | 0 | if(data) { |
374 | 0 | ssize_t nwritten = Curl_conn_cf_send(cf->next, data, |
375 | 0 | (const char *)buf, buflen, err); |
376 | 0 | if(nwritten > 0) |
377 | 0 | CURL_TRC_CF(data, cf, "[0] egress: wrote %zd bytes", nwritten); |
378 | 0 | return nwritten; |
379 | 0 | } |
380 | 0 | return 0; |
381 | 0 | } |
382 | | |
383 | | static ssize_t send_callback(nghttp2_session *h2, |
384 | | const uint8_t *mem, size_t length, int flags, |
385 | | void *userp); |
386 | | static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame, |
387 | | void *userp); |
388 | | #ifndef CURL_DISABLE_VERBOSE_STRINGS |
389 | | static int on_frame_send(nghttp2_session *session, const nghttp2_frame *frame, |
390 | | void *userp); |
391 | | #endif |
392 | | static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags, |
393 | | int32_t stream_id, |
394 | | const uint8_t *mem, size_t len, void *userp); |
395 | | static int on_stream_close(nghttp2_session *session, int32_t stream_id, |
396 | | uint32_t error_code, void *userp); |
397 | | static int on_begin_headers(nghttp2_session *session, |
398 | | const nghttp2_frame *frame, void *userp); |
399 | | static int on_header(nghttp2_session *session, const nghttp2_frame *frame, |
400 | | const uint8_t *name, size_t namelen, |
401 | | const uint8_t *value, size_t valuelen, |
402 | | uint8_t flags, |
403 | | void *userp); |
404 | | static int error_callback(nghttp2_session *session, const char *msg, |
405 | | size_t len, void *userp); |
406 | | |
407 | | /* |
408 | | * Initialize the cfilter context |
409 | | */ |
410 | | static CURLcode cf_h2_ctx_init(struct Curl_cfilter *cf, |
411 | | struct Curl_easy *data, |
412 | | bool via_h1_upgrade) |
413 | 0 | { |
414 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
415 | 0 | struct stream_ctx *stream; |
416 | 0 | CURLcode result = CURLE_OUT_OF_MEMORY; |
417 | 0 | int rc; |
418 | 0 | nghttp2_session_callbacks *cbs = NULL; |
419 | |
|
420 | 0 | DEBUGASSERT(!ctx->h2); |
421 | 0 | Curl_bufcp_init(&ctx->stream_bufcp, H2_CHUNK_SIZE, H2_STREAM_POOL_SPARES); |
422 | 0 | Curl_bufq_initp(&ctx->inbufq, &ctx->stream_bufcp, H2_NW_RECV_CHUNKS, 0); |
423 | 0 | Curl_bufq_initp(&ctx->outbufq, &ctx->stream_bufcp, H2_NW_SEND_CHUNKS, 0); |
424 | 0 | ctx->last_stream_id = 2147483647; |
425 | |
|
426 | 0 | rc = nghttp2_session_callbacks_new(&cbs); |
427 | 0 | if(rc) { |
428 | 0 | failf(data, "Couldn't initialize nghttp2 callbacks"); |
429 | 0 | goto out; |
430 | 0 | } |
431 | | |
432 | 0 | nghttp2_session_callbacks_set_send_callback(cbs, send_callback); |
433 | 0 | nghttp2_session_callbacks_set_on_frame_recv_callback(cbs, on_frame_recv); |
434 | 0 | #ifndef CURL_DISABLE_VERBOSE_STRINGS |
435 | 0 | nghttp2_session_callbacks_set_on_frame_send_callback(cbs, on_frame_send); |
436 | 0 | #endif |
437 | 0 | nghttp2_session_callbacks_set_on_data_chunk_recv_callback( |
438 | 0 | cbs, on_data_chunk_recv); |
439 | 0 | nghttp2_session_callbacks_set_on_stream_close_callback(cbs, on_stream_close); |
440 | 0 | nghttp2_session_callbacks_set_on_begin_headers_callback( |
441 | 0 | cbs, on_begin_headers); |
442 | 0 | nghttp2_session_callbacks_set_on_header_callback(cbs, on_header); |
443 | 0 | nghttp2_session_callbacks_set_error_callback(cbs, error_callback); |
444 | | |
445 | | /* The nghttp2 session is not yet setup, do it */ |
446 | 0 | rc = h2_client_new(cf, cbs); |
447 | 0 | if(rc) { |
448 | 0 | failf(data, "Couldn't initialize nghttp2"); |
449 | 0 | goto out; |
450 | 0 | } |
451 | 0 | ctx->max_concurrent_streams = DEFAULT_MAX_CONCURRENT_STREAMS; |
452 | |
|
453 | 0 | if(via_h1_upgrade) { |
454 | | /* HTTP/1.1 Upgrade issued. H2 Settings have already been submitted |
455 | | * in the H1 request and we upgrade from there. This stream |
456 | | * is opened implicitly as #1. */ |
457 | 0 | uint8_t binsettings[H2_BINSETTINGS_LEN]; |
458 | 0 | ssize_t binlen; /* length of the binsettings data */ |
459 | |
|
460 | 0 | binlen = populate_binsettings(binsettings, data); |
461 | 0 | if(binlen <= 0) { |
462 | 0 | failf(data, "nghttp2 unexpectedly failed on pack_settings_payload"); |
463 | 0 | result = CURLE_FAILED_INIT; |
464 | 0 | goto out; |
465 | 0 | } |
466 | | |
467 | 0 | result = http2_data_setup(cf, data, &stream); |
468 | 0 | if(result) |
469 | 0 | goto out; |
470 | 0 | DEBUGASSERT(stream); |
471 | 0 | stream->id = 1; |
472 | | /* queue SETTINGS frame (again) */ |
473 | 0 | rc = nghttp2_session_upgrade2(ctx->h2, binsettings, binlen, |
474 | 0 | data->state.httpreq == HTTPREQ_HEAD, |
475 | 0 | NULL); |
476 | 0 | if(rc) { |
477 | 0 | failf(data, "nghttp2_session_upgrade2() failed: %s(%d)", |
478 | 0 | nghttp2_strerror(rc), rc); |
479 | 0 | result = CURLE_HTTP2; |
480 | 0 | goto out; |
481 | 0 | } |
482 | | |
483 | 0 | rc = nghttp2_session_set_stream_user_data(ctx->h2, stream->id, |
484 | 0 | data); |
485 | 0 | if(rc) { |
486 | 0 | infof(data, "http/2: failed to set user_data for stream %u", |
487 | 0 | stream->id); |
488 | 0 | DEBUGASSERT(0); |
489 | 0 | } |
490 | 0 | CURL_TRC_CF(data, cf, "created session via Upgrade"); |
491 | 0 | } |
492 | 0 | else { |
493 | 0 | nghttp2_settings_entry iv[H2_SETTINGS_IV_LEN]; |
494 | 0 | int ivlen; |
495 | |
|
496 | 0 | ivlen = populate_settings(iv, data); |
497 | 0 | rc = nghttp2_submit_settings(ctx->h2, NGHTTP2_FLAG_NONE, |
498 | 0 | iv, ivlen); |
499 | 0 | if(rc) { |
500 | 0 | failf(data, "nghttp2_submit_settings() failed: %s(%d)", |
501 | 0 | nghttp2_strerror(rc), rc); |
502 | 0 | result = CURLE_HTTP2; |
503 | 0 | goto out; |
504 | 0 | } |
505 | 0 | } |
506 | | |
507 | 0 | rc = nghttp2_session_set_local_window_size(ctx->h2, NGHTTP2_FLAG_NONE, 0, |
508 | 0 | HTTP2_HUGE_WINDOW_SIZE); |
509 | 0 | if(rc) { |
510 | 0 | failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)", |
511 | 0 | nghttp2_strerror(rc), rc); |
512 | 0 | result = CURLE_HTTP2; |
513 | 0 | goto out; |
514 | 0 | } |
515 | | |
516 | | /* all set, traffic will be send on connect */ |
517 | 0 | result = CURLE_OK; |
518 | 0 | CURL_TRC_CF(data, cf, "[0] created h2 session%s", |
519 | 0 | via_h1_upgrade? " (via h1 upgrade)" : ""); |
520 | |
|
521 | 0 | out: |
522 | 0 | if(cbs) |
523 | 0 | nghttp2_session_callbacks_del(cbs); |
524 | 0 | return result; |
525 | 0 | } |
526 | | |
527 | | /* |
528 | | * Returns nonzero if current HTTP/2 session should be closed. |
529 | | */ |
530 | | static int should_close_session(struct cf_h2_ctx *ctx) |
531 | 0 | { |
532 | 0 | return ctx->drain_total == 0 && !nghttp2_session_want_read(ctx->h2) && |
533 | 0 | !nghttp2_session_want_write(ctx->h2); |
534 | 0 | } |
535 | | |
536 | | /* |
537 | | * Processes pending input left in network input buffer. |
538 | | * This function returns 0 if it succeeds, or -1 and error code will |
539 | | * be assigned to *err. |
540 | | */ |
541 | | static int h2_process_pending_input(struct Curl_cfilter *cf, |
542 | | struct Curl_easy *data, |
543 | | CURLcode *err) |
544 | 0 | { |
545 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
546 | 0 | const unsigned char *buf; |
547 | 0 | size_t blen; |
548 | 0 | ssize_t rv; |
549 | |
|
550 | 0 | while(Curl_bufq_peek(&ctx->inbufq, &buf, &blen)) { |
551 | |
|
552 | 0 | rv = nghttp2_session_mem_recv(ctx->h2, (const uint8_t *)buf, blen); |
553 | 0 | if(rv < 0) { |
554 | 0 | failf(data, |
555 | 0 | "process_pending_input: nghttp2_session_mem_recv() returned " |
556 | 0 | "%zd:%s", rv, nghttp2_strerror((int)rv)); |
557 | 0 | *err = CURLE_RECV_ERROR; |
558 | 0 | return -1; |
559 | 0 | } |
560 | 0 | Curl_bufq_skip(&ctx->inbufq, (size_t)rv); |
561 | 0 | if(Curl_bufq_is_empty(&ctx->inbufq)) { |
562 | 0 | break; |
563 | 0 | } |
564 | 0 | else { |
565 | 0 | CURL_TRC_CF(data, cf, "process_pending_input: %zu bytes left " |
566 | 0 | "in connection buffer", Curl_bufq_len(&ctx->inbufq)); |
567 | 0 | } |
568 | 0 | } |
569 | | |
570 | 0 | if(nghttp2_session_check_request_allowed(ctx->h2) == 0) { |
571 | | /* No more requests are allowed in the current session, so |
572 | | the connection may not be reused. This is set when a |
573 | | GOAWAY frame has been received or when the limit of stream |
574 | | identifiers has been reached. */ |
575 | 0 | connclose(cf->conn, "http/2: No new requests allowed"); |
576 | 0 | } |
577 | |
|
578 | 0 | return 0; |
579 | 0 | } |
580 | | |
581 | | /* |
582 | | * The server may send us data at any point (e.g. PING frames). Therefore, |
583 | | * we cannot assume that an HTTP/2 socket is dead just because it is readable. |
584 | | * |
585 | | * Check the lower filters first and, if successful, peek at the socket |
586 | | * and distinguish between closed and data. |
587 | | */ |
588 | | static bool http2_connisalive(struct Curl_cfilter *cf, struct Curl_easy *data, |
589 | | bool *input_pending) |
590 | 0 | { |
591 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
592 | 0 | bool alive = TRUE; |
593 | |
|
594 | 0 | *input_pending = FALSE; |
595 | 0 | if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending)) |
596 | 0 | return FALSE; |
597 | | |
598 | 0 | if(*input_pending) { |
599 | | /* This happens before we've sent off a request and the connection is |
600 | | not in use by any other transfer, there shouldn't be any data here, |
601 | | only "protocol frames" */ |
602 | 0 | CURLcode result; |
603 | 0 | ssize_t nread = -1; |
604 | |
|
605 | 0 | *input_pending = FALSE; |
606 | 0 | nread = Curl_bufq_slurp(&ctx->inbufq, nw_in_reader, cf, &result); |
607 | 0 | if(nread != -1) { |
608 | 0 | CURL_TRC_CF(data, cf, "%zd bytes stray data read before trying " |
609 | 0 | "h2 connection", nread); |
610 | 0 | if(h2_process_pending_input(cf, data, &result) < 0) |
611 | | /* immediate error, considered dead */ |
612 | 0 | alive = FALSE; |
613 | 0 | else { |
614 | 0 | alive = !should_close_session(ctx); |
615 | 0 | } |
616 | 0 | } |
617 | 0 | else if(result != CURLE_AGAIN) { |
618 | | /* the read failed so let's say this is dead anyway */ |
619 | 0 | alive = FALSE; |
620 | 0 | } |
621 | 0 | } |
622 | |
|
623 | 0 | return alive; |
624 | 0 | } |
625 | | |
626 | | static CURLcode http2_send_ping(struct Curl_cfilter *cf, |
627 | | struct Curl_easy *data) |
628 | 0 | { |
629 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
630 | 0 | int rc; |
631 | |
|
632 | 0 | rc = nghttp2_submit_ping(ctx->h2, 0, ZERO_NULL); |
633 | 0 | if(rc) { |
634 | 0 | failf(data, "nghttp2_submit_ping() failed: %s(%d)", |
635 | 0 | nghttp2_strerror(rc), rc); |
636 | 0 | return CURLE_HTTP2; |
637 | 0 | } |
638 | | |
639 | 0 | rc = nghttp2_session_send(ctx->h2); |
640 | 0 | if(rc) { |
641 | 0 | failf(data, "nghttp2_session_send() failed: %s(%d)", |
642 | 0 | nghttp2_strerror(rc), rc); |
643 | 0 | return CURLE_SEND_ERROR; |
644 | 0 | } |
645 | 0 | return CURLE_OK; |
646 | 0 | } |
647 | | |
648 | | /* |
649 | | * Store nghttp2 version info in this buffer. |
650 | | */ |
651 | | void Curl_http2_ver(char *p, size_t len) |
652 | 0 | { |
653 | 0 | nghttp2_info *h2 = nghttp2_version(0); |
654 | 0 | (void)msnprintf(p, len, "nghttp2/%s", h2->version_str); |
655 | 0 | } |
656 | | |
657 | | static CURLcode nw_out_flush(struct Curl_cfilter *cf, |
658 | | struct Curl_easy *data) |
659 | 0 | { |
660 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
661 | 0 | ssize_t nwritten; |
662 | 0 | CURLcode result; |
663 | |
|
664 | 0 | (void)data; |
665 | 0 | if(Curl_bufq_is_empty(&ctx->outbufq)) |
666 | 0 | return CURLE_OK; |
667 | | |
668 | 0 | nwritten = Curl_bufq_pass(&ctx->outbufq, nw_out_writer, cf, &result); |
669 | 0 | if(nwritten < 0) { |
670 | 0 | if(result == CURLE_AGAIN) { |
671 | 0 | CURL_TRC_CF(data, cf, "flush nw send buffer(%zu) -> EAGAIN", |
672 | 0 | Curl_bufq_len(&ctx->outbufq)); |
673 | 0 | ctx->nw_out_blocked = 1; |
674 | 0 | } |
675 | 0 | return result; |
676 | 0 | } |
677 | 0 | return Curl_bufq_is_empty(&ctx->outbufq)? CURLE_OK: CURLE_AGAIN; |
678 | 0 | } |
679 | | |
680 | | /* |
681 | | * The implementation of nghttp2_send_callback type. Here we write |data| with |
682 | | * size |length| to the network and return the number of bytes actually |
683 | | * written. See the documentation of nghttp2_send_callback for the details. |
684 | | */ |
685 | | static ssize_t send_callback(nghttp2_session *h2, |
686 | | const uint8_t *buf, size_t blen, int flags, |
687 | | void *userp) |
688 | 0 | { |
689 | 0 | struct Curl_cfilter *cf = userp; |
690 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
691 | 0 | struct Curl_easy *data = CF_DATA_CURRENT(cf); |
692 | 0 | ssize_t nwritten; |
693 | 0 | CURLcode result = CURLE_OK; |
694 | |
|
695 | 0 | (void)h2; |
696 | 0 | (void)flags; |
697 | 0 | DEBUGASSERT(data); |
698 | | |
699 | 0 | nwritten = Curl_bufq_write_pass(&ctx->outbufq, buf, blen, |
700 | 0 | nw_out_writer, cf, &result); |
701 | 0 | if(nwritten < 0) { |
702 | 0 | if(result == CURLE_AGAIN) { |
703 | 0 | ctx->nw_out_blocked = 1; |
704 | 0 | return NGHTTP2_ERR_WOULDBLOCK; |
705 | 0 | } |
706 | 0 | failf(data, "Failed sending HTTP2 data"); |
707 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
708 | 0 | } |
709 | | |
710 | 0 | if(!nwritten) { |
711 | 0 | ctx->nw_out_blocked = 1; |
712 | 0 | return NGHTTP2_ERR_WOULDBLOCK; |
713 | 0 | } |
714 | 0 | return nwritten; |
715 | 0 | } |
716 | | |
717 | | |
718 | | /* We pass a pointer to this struct in the push callback, but the contents of |
719 | | the struct are hidden from the user. */ |
720 | | struct curl_pushheaders { |
721 | | struct Curl_easy *data; |
722 | | const nghttp2_push_promise *frame; |
723 | | }; |
724 | | |
725 | | /* |
726 | | * push header access function. Only to be used from within the push callback |
727 | | */ |
728 | | char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num) |
729 | 0 | { |
730 | | /* Verify that we got a good easy handle in the push header struct, mostly to |
731 | | detect rubbish input fast(er). */ |
732 | 0 | if(!h || !GOOD_EASY_HANDLE(h->data)) |
733 | 0 | return NULL; |
734 | 0 | else { |
735 | 0 | struct stream_ctx *stream = H2_STREAM_CTX(h->data); |
736 | 0 | if(stream && num < stream->push_headers_used) |
737 | 0 | return stream->push_headers[num]; |
738 | 0 | } |
739 | 0 | return NULL; |
740 | 0 | } |
741 | | |
742 | | /* |
743 | | * push header access function. Only to be used from within the push callback |
744 | | */ |
745 | | char *curl_pushheader_byname(struct curl_pushheaders *h, const char *header) |
746 | 0 | { |
747 | 0 | struct stream_ctx *stream; |
748 | 0 | size_t len; |
749 | 0 | size_t i; |
750 | | /* Verify that we got a good easy handle in the push header struct, |
751 | | mostly to detect rubbish input fast(er). Also empty header name |
752 | | is just a rubbish too. We have to allow ":" at the beginning of |
753 | | the header, but header == ":" must be rejected. If we have ':' in |
754 | | the middle of header, it could be matched in middle of the value, |
755 | | this is because we do prefix match.*/ |
756 | 0 | if(!h || !GOOD_EASY_HANDLE(h->data) || !header || !header[0] || |
757 | 0 | !strcmp(header, ":") || strchr(header + 1, ':')) |
758 | 0 | return NULL; |
759 | | |
760 | 0 | stream = H2_STREAM_CTX(h->data); |
761 | 0 | if(!stream) |
762 | 0 | return NULL; |
763 | | |
764 | 0 | len = strlen(header); |
765 | 0 | for(i = 0; i<stream->push_headers_used; i++) { |
766 | 0 | if(!strncmp(header, stream->push_headers[i], len)) { |
767 | | /* sub-match, make sure that it is followed by a colon */ |
768 | 0 | if(stream->push_headers[i][len] != ':') |
769 | 0 | continue; |
770 | 0 | return &stream->push_headers[i][len + 1]; |
771 | 0 | } |
772 | 0 | } |
773 | 0 | return NULL; |
774 | 0 | } |
775 | | |
776 | | static struct Curl_easy *h2_duphandle(struct Curl_cfilter *cf, |
777 | | struct Curl_easy *data) |
778 | 0 | { |
779 | 0 | struct Curl_easy *second = curl_easy_duphandle(data); |
780 | 0 | if(second) { |
781 | | /* setup the request struct */ |
782 | 0 | struct HTTP *http = calloc(1, sizeof(struct HTTP)); |
783 | 0 | if(!http) { |
784 | 0 | (void)Curl_close(&second); |
785 | 0 | } |
786 | 0 | else { |
787 | 0 | struct stream_ctx *second_stream; |
788 | |
|
789 | 0 | second->req.p.http = http; |
790 | 0 | http2_data_setup(cf, second, &second_stream); |
791 | 0 | second->state.priority.weight = data->state.priority.weight; |
792 | 0 | } |
793 | 0 | } |
794 | 0 | return second; |
795 | 0 | } |
796 | | |
797 | | static int set_transfer_url(struct Curl_easy *data, |
798 | | struct curl_pushheaders *hp) |
799 | 0 | { |
800 | 0 | const char *v; |
801 | 0 | CURLUcode uc; |
802 | 0 | char *url = NULL; |
803 | 0 | int rc = 0; |
804 | 0 | CURLU *u = curl_url(); |
805 | |
|
806 | 0 | if(!u) |
807 | 0 | return 5; |
808 | | |
809 | 0 | v = curl_pushheader_byname(hp, HTTP_PSEUDO_SCHEME); |
810 | 0 | if(v) { |
811 | 0 | uc = curl_url_set(u, CURLUPART_SCHEME, v, 0); |
812 | 0 | if(uc) { |
813 | 0 | rc = 1; |
814 | 0 | goto fail; |
815 | 0 | } |
816 | 0 | } |
817 | | |
818 | 0 | v = curl_pushheader_byname(hp, HTTP_PSEUDO_AUTHORITY); |
819 | 0 | if(v) { |
820 | 0 | uc = Curl_url_set_authority(u, v, CURLU_DISALLOW_USER); |
821 | 0 | if(uc) { |
822 | 0 | rc = 2; |
823 | 0 | goto fail; |
824 | 0 | } |
825 | 0 | } |
826 | | |
827 | 0 | v = curl_pushheader_byname(hp, HTTP_PSEUDO_PATH); |
828 | 0 | if(v) { |
829 | 0 | uc = curl_url_set(u, CURLUPART_PATH, v, 0); |
830 | 0 | if(uc) { |
831 | 0 | rc = 3; |
832 | 0 | goto fail; |
833 | 0 | } |
834 | 0 | } |
835 | | |
836 | 0 | uc = curl_url_get(u, CURLUPART_URL, &url, 0); |
837 | 0 | if(uc) |
838 | 0 | rc = 4; |
839 | 0 | fail: |
840 | 0 | curl_url_cleanup(u); |
841 | 0 | if(rc) |
842 | 0 | return rc; |
843 | | |
844 | 0 | if(data->state.url_alloc) |
845 | 0 | free(data->state.url); |
846 | 0 | data->state.url_alloc = TRUE; |
847 | 0 | data->state.url = url; |
848 | 0 | return 0; |
849 | 0 | } |
850 | | |
851 | | static void discard_newhandle(struct Curl_cfilter *cf, |
852 | | struct Curl_easy *newhandle) |
853 | 0 | { |
854 | 0 | if(!newhandle->req.p.http) { |
855 | 0 | http2_data_done(cf, newhandle, TRUE); |
856 | 0 | newhandle->req.p.http = NULL; |
857 | 0 | } |
858 | 0 | (void)Curl_close(&newhandle); |
859 | 0 | } |
860 | | |
861 | | static int push_promise(struct Curl_cfilter *cf, |
862 | | struct Curl_easy *data, |
863 | | const nghttp2_push_promise *frame) |
864 | 0 | { |
865 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
866 | 0 | int rv; /* one of the CURL_PUSH_* defines */ |
867 | |
|
868 | 0 | CURL_TRC_CF(data, cf, "[%d] PUSH_PROMISE received", |
869 | 0 | frame->promised_stream_id); |
870 | 0 | if(data->multi->push_cb) { |
871 | 0 | struct stream_ctx *stream; |
872 | 0 | struct stream_ctx *newstream; |
873 | 0 | struct curl_pushheaders heads; |
874 | 0 | CURLMcode rc; |
875 | 0 | CURLcode result; |
876 | 0 | size_t i; |
877 | | /* clone the parent */ |
878 | 0 | struct Curl_easy *newhandle = h2_duphandle(cf, data); |
879 | 0 | if(!newhandle) { |
880 | 0 | infof(data, "failed to duplicate handle"); |
881 | 0 | rv = CURL_PUSH_DENY; /* FAIL HARD */ |
882 | 0 | goto fail; |
883 | 0 | } |
884 | | |
885 | 0 | heads.data = data; |
886 | 0 | heads.frame = frame; |
887 | | /* ask the application */ |
888 | 0 | CURL_TRC_CF(data, cf, "Got PUSH_PROMISE, ask application"); |
889 | |
|
890 | 0 | stream = H2_STREAM_CTX(data); |
891 | 0 | if(!stream) { |
892 | 0 | failf(data, "Internal NULL stream"); |
893 | 0 | discard_newhandle(cf, newhandle); |
894 | 0 | rv = CURL_PUSH_DENY; |
895 | 0 | goto fail; |
896 | 0 | } |
897 | | |
898 | 0 | rv = set_transfer_url(newhandle, &heads); |
899 | 0 | if(rv) { |
900 | 0 | discard_newhandle(cf, newhandle); |
901 | 0 | rv = CURL_PUSH_DENY; |
902 | 0 | goto fail; |
903 | 0 | } |
904 | | |
905 | 0 | result = http2_data_setup(cf, newhandle, &newstream); |
906 | 0 | if(result) { |
907 | 0 | failf(data, "error setting up stream: %d", result); |
908 | 0 | discard_newhandle(cf, newhandle); |
909 | 0 | rv = CURL_PUSH_DENY; |
910 | 0 | goto fail; |
911 | 0 | } |
912 | 0 | DEBUGASSERT(stream); |
913 | | |
914 | 0 | Curl_set_in_callback(data, true); |
915 | 0 | rv = data->multi->push_cb(data, newhandle, |
916 | 0 | stream->push_headers_used, &heads, |
917 | 0 | data->multi->push_userp); |
918 | 0 | Curl_set_in_callback(data, false); |
919 | | |
920 | | /* free the headers again */ |
921 | 0 | for(i = 0; i<stream->push_headers_used; i++) |
922 | 0 | free(stream->push_headers[i]); |
923 | 0 | free(stream->push_headers); |
924 | 0 | stream->push_headers = NULL; |
925 | 0 | stream->push_headers_used = 0; |
926 | |
|
927 | 0 | if(rv) { |
928 | 0 | DEBUGASSERT((rv > CURL_PUSH_OK) && (rv <= CURL_PUSH_ERROROUT)); |
929 | | /* denied, kill off the new handle again */ |
930 | 0 | discard_newhandle(cf, newhandle); |
931 | 0 | goto fail; |
932 | 0 | } |
933 | | |
934 | 0 | newstream->id = frame->promised_stream_id; |
935 | 0 | newhandle->req.maxdownload = -1; |
936 | 0 | newhandle->req.size = -1; |
937 | | |
938 | | /* approved, add to the multi handle and immediately switch to PERFORM |
939 | | state with the given connection !*/ |
940 | 0 | rc = Curl_multi_add_perform(data->multi, newhandle, cf->conn); |
941 | 0 | if(rc) { |
942 | 0 | infof(data, "failed to add handle to multi"); |
943 | 0 | discard_newhandle(cf, newhandle); |
944 | 0 | rv = CURL_PUSH_DENY; |
945 | 0 | goto fail; |
946 | 0 | } |
947 | | |
948 | 0 | rv = nghttp2_session_set_stream_user_data(ctx->h2, |
949 | 0 | newstream->id, |
950 | 0 | newhandle); |
951 | 0 | if(rv) { |
952 | 0 | infof(data, "failed to set user_data for stream %u", |
953 | 0 | newstream->id); |
954 | 0 | DEBUGASSERT(0); |
955 | 0 | rv = CURL_PUSH_DENY; |
956 | 0 | goto fail; |
957 | 0 | } |
958 | 0 | } |
959 | 0 | else { |
960 | 0 | CURL_TRC_CF(data, cf, "Got PUSH_PROMISE, ignore it"); |
961 | 0 | rv = CURL_PUSH_DENY; |
962 | 0 | } |
963 | 0 | fail: |
964 | 0 | return rv; |
965 | 0 | } |
966 | | |
967 | | static CURLcode recvbuf_write_hds(struct Curl_cfilter *cf, |
968 | | struct Curl_easy *data, |
969 | | const char *buf, size_t blen) |
970 | 0 | { |
971 | 0 | struct stream_ctx *stream = H2_STREAM_CTX(data); |
972 | 0 | ssize_t nwritten; |
973 | 0 | CURLcode result; |
974 | |
|
975 | 0 | (void)cf; |
976 | 0 | nwritten = Curl_bufq_write(&stream->recvbuf, |
977 | 0 | (const unsigned char *)buf, blen, &result); |
978 | 0 | if(nwritten < 0) |
979 | 0 | return result; |
980 | 0 | stream->resp_hds_len += (size_t)nwritten; |
981 | 0 | DEBUGASSERT((size_t)nwritten == blen); |
982 | 0 | return CURLE_OK; |
983 | 0 | } |
984 | | |
985 | | static CURLcode on_stream_frame(struct Curl_cfilter *cf, |
986 | | struct Curl_easy *data, |
987 | | const nghttp2_frame *frame) |
988 | 0 | { |
989 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
990 | 0 | struct stream_ctx *stream = H2_STREAM_CTX(data); |
991 | 0 | int32_t stream_id = frame->hd.stream_id; |
992 | 0 | CURLcode result; |
993 | 0 | size_t rbuflen; |
994 | 0 | int rv; |
995 | |
|
996 | 0 | if(!stream) { |
997 | 0 | CURL_TRC_CF(data, cf, "[%d] No stream_ctx set", stream_id); |
998 | 0 | return CURLE_FAILED_INIT; |
999 | 0 | } |
1000 | | |
1001 | 0 | switch(frame->hd.type) { |
1002 | 0 | case NGHTTP2_DATA: |
1003 | 0 | rbuflen = Curl_bufq_len(&stream->recvbuf); |
1004 | 0 | CURL_TRC_CF(data, cf, "[%d] DATA, buffered=%zu, window=%d/%d", |
1005 | 0 | stream_id, rbuflen, |
1006 | 0 | nghttp2_session_get_stream_effective_recv_data_length( |
1007 | 0 | ctx->h2, stream->id), |
1008 | 0 | nghttp2_session_get_stream_effective_local_window_size( |
1009 | 0 | ctx->h2, stream->id)); |
1010 | | /* If !body started on this stream, then receiving DATA is illegal. */ |
1011 | 0 | if(!stream->bodystarted) { |
1012 | 0 | rv = nghttp2_submit_rst_stream(ctx->h2, NGHTTP2_FLAG_NONE, |
1013 | 0 | stream_id, NGHTTP2_PROTOCOL_ERROR); |
1014 | |
|
1015 | 0 | if(nghttp2_is_fatal(rv)) { |
1016 | 0 | return CURLE_RECV_ERROR; |
1017 | 0 | } |
1018 | 0 | } |
1019 | 0 | if(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) { |
1020 | 0 | drain_stream(cf, data, stream); |
1021 | 0 | } |
1022 | 0 | else if(rbuflen > stream->local_window_size) { |
1023 | 0 | int32_t wsize = nghttp2_session_get_stream_local_window_size( |
1024 | 0 | ctx->h2, stream->id); |
1025 | 0 | if(wsize > 0 && (uint32_t)wsize != stream->local_window_size) { |
1026 | | /* H2 flow control is not absolute, as the server might not have the |
1027 | | * same view, yet. When we receive more than we want, we enforce |
1028 | | * the local window size again to make nghttp2 send WINDOW_UPATEs |
1029 | | * accordingly. */ |
1030 | 0 | nghttp2_session_set_local_window_size(ctx->h2, |
1031 | 0 | NGHTTP2_FLAG_NONE, |
1032 | 0 | stream->id, |
1033 | 0 | stream->local_window_size); |
1034 | 0 | } |
1035 | 0 | } |
1036 | 0 | break; |
1037 | 0 | case NGHTTP2_HEADERS: |
1038 | 0 | if(stream->bodystarted) { |
1039 | | /* Only valid HEADERS after body started is trailer HEADERS. We |
1040 | | buffer them in on_header callback. */ |
1041 | 0 | break; |
1042 | 0 | } |
1043 | | |
1044 | | /* nghttp2 guarantees that :status is received, and we store it to |
1045 | | stream->status_code. Fuzzing has proven this can still be reached |
1046 | | without status code having been set. */ |
1047 | 0 | if(stream->status_code == -1) |
1048 | 0 | return CURLE_RECV_ERROR; |
1049 | | |
1050 | | /* Only final status code signals the end of header */ |
1051 | 0 | if(stream->status_code / 100 != 1) { |
1052 | 0 | stream->bodystarted = TRUE; |
1053 | 0 | stream->status_code = -1; |
1054 | 0 | } |
1055 | |
|
1056 | 0 | result = recvbuf_write_hds(cf, data, STRCONST("\r\n")); |
1057 | 0 | if(result) |
1058 | 0 | return result; |
1059 | | |
1060 | 0 | if(stream->status_code / 100 != 1) { |
1061 | 0 | stream->resp_hds_complete = TRUE; |
1062 | 0 | } |
1063 | 0 | drain_stream(cf, data, stream); |
1064 | 0 | break; |
1065 | 0 | case NGHTTP2_PUSH_PROMISE: |
1066 | 0 | rv = push_promise(cf, data, &frame->push_promise); |
1067 | 0 | if(rv) { /* deny! */ |
1068 | 0 | DEBUGASSERT((rv > CURL_PUSH_OK) && (rv <= CURL_PUSH_ERROROUT)); |
1069 | 0 | rv = nghttp2_submit_rst_stream(ctx->h2, NGHTTP2_FLAG_NONE, |
1070 | 0 | frame->push_promise.promised_stream_id, |
1071 | 0 | NGHTTP2_CANCEL); |
1072 | 0 | if(nghttp2_is_fatal(rv)) |
1073 | 0 | return CURLE_SEND_ERROR; |
1074 | 0 | else if(rv == CURL_PUSH_ERROROUT) { |
1075 | 0 | CURL_TRC_CF(data, cf, "[%d] fail in PUSH_PROMISE received", |
1076 | 0 | stream_id); |
1077 | 0 | return CURLE_RECV_ERROR; |
1078 | 0 | } |
1079 | 0 | } |
1080 | 0 | break; |
1081 | 0 | case NGHTTP2_RST_STREAM: |
1082 | 0 | stream->closed = TRUE; |
1083 | 0 | if(frame->rst_stream.error_code) { |
1084 | 0 | stream->reset = TRUE; |
1085 | 0 | } |
1086 | 0 | stream->send_closed = TRUE; |
1087 | 0 | drain_stream(cf, data, stream); |
1088 | 0 | break; |
1089 | 0 | case NGHTTP2_WINDOW_UPDATE: |
1090 | 0 | if(CURL_WANT_SEND(data)) { |
1091 | 0 | drain_stream(cf, data, stream); |
1092 | 0 | } |
1093 | 0 | break; |
1094 | 0 | default: |
1095 | 0 | break; |
1096 | 0 | } |
1097 | 0 | return CURLE_OK; |
1098 | 0 | } |
1099 | | |
1100 | | #ifndef CURL_DISABLE_VERBOSE_STRINGS |
1101 | | static int fr_print(const nghttp2_frame *frame, char *buffer, size_t blen) |
1102 | 0 | { |
1103 | 0 | switch(frame->hd.type) { |
1104 | 0 | case NGHTTP2_DATA: { |
1105 | 0 | return msnprintf(buffer, blen, |
1106 | 0 | "FRAME[DATA, len=%d, eos=%d, padlen=%d]", |
1107 | 0 | (int)frame->hd.length, |
1108 | 0 | !!(frame->hd.flags & NGHTTP2_FLAG_END_STREAM), |
1109 | 0 | (int)frame->data.padlen); |
1110 | 0 | } |
1111 | 0 | case NGHTTP2_HEADERS: { |
1112 | 0 | return msnprintf(buffer, blen, |
1113 | 0 | "FRAME[HEADERS, len=%d, hend=%d, eos=%d]", |
1114 | 0 | (int)frame->hd.length, |
1115 | 0 | !!(frame->hd.flags & NGHTTP2_FLAG_END_HEADERS), |
1116 | 0 | !!(frame->hd.flags & NGHTTP2_FLAG_END_STREAM)); |
1117 | 0 | } |
1118 | 0 | case NGHTTP2_PRIORITY: { |
1119 | 0 | return msnprintf(buffer, blen, |
1120 | 0 | "FRAME[PRIORITY, len=%d, flags=%d]", |
1121 | 0 | (int)frame->hd.length, frame->hd.flags); |
1122 | 0 | } |
1123 | 0 | case NGHTTP2_RST_STREAM: { |
1124 | 0 | return msnprintf(buffer, blen, |
1125 | 0 | "FRAME[RST_STREAM, len=%d, flags=%d, error=%u]", |
1126 | 0 | (int)frame->hd.length, frame->hd.flags, |
1127 | 0 | frame->rst_stream.error_code); |
1128 | 0 | } |
1129 | 0 | case NGHTTP2_SETTINGS: { |
1130 | 0 | if(frame->hd.flags & NGHTTP2_FLAG_ACK) { |
1131 | 0 | return msnprintf(buffer, blen, "FRAME[SETTINGS, ack=1]"); |
1132 | 0 | } |
1133 | 0 | return msnprintf(buffer, blen, |
1134 | 0 | "FRAME[SETTINGS, len=%d]", (int)frame->hd.length); |
1135 | 0 | } |
1136 | 0 | case NGHTTP2_PUSH_PROMISE: { |
1137 | 0 | return msnprintf(buffer, blen, |
1138 | 0 | "FRAME[PUSH_PROMISE, len=%d, hend=%d]", |
1139 | 0 | (int)frame->hd.length, |
1140 | 0 | !!(frame->hd.flags & NGHTTP2_FLAG_END_HEADERS)); |
1141 | 0 | } |
1142 | 0 | case NGHTTP2_PING: { |
1143 | 0 | return msnprintf(buffer, blen, |
1144 | 0 | "FRAME[PING, len=%d, ack=%d]", |
1145 | 0 | (int)frame->hd.length, |
1146 | 0 | frame->hd.flags&NGHTTP2_FLAG_ACK); |
1147 | 0 | } |
1148 | 0 | case NGHTTP2_GOAWAY: { |
1149 | 0 | char scratch[128]; |
1150 | 0 | size_t s_len = sizeof(scratch)/sizeof(scratch[0]); |
1151 | 0 | size_t len = (frame->goaway.opaque_data_len < s_len)? |
1152 | 0 | frame->goaway.opaque_data_len : s_len-1; |
1153 | 0 | if(len) |
1154 | 0 | memcpy(scratch, frame->goaway.opaque_data, len); |
1155 | 0 | scratch[len] = '\0'; |
1156 | 0 | return msnprintf(buffer, blen, "FRAME[GOAWAY, error=%d, reason='%s', " |
1157 | 0 | "last_stream=%d]", frame->goaway.error_code, |
1158 | 0 | scratch, frame->goaway.last_stream_id); |
1159 | 0 | } |
1160 | 0 | case NGHTTP2_WINDOW_UPDATE: { |
1161 | 0 | return msnprintf(buffer, blen, |
1162 | 0 | "FRAME[WINDOW_UPDATE, incr=%d]", |
1163 | 0 | frame->window_update.window_size_increment); |
1164 | 0 | } |
1165 | 0 | default: |
1166 | 0 | return msnprintf(buffer, blen, "FRAME[%d, len=%d, flags=%d]", |
1167 | 0 | frame->hd.type, (int)frame->hd.length, |
1168 | 0 | frame->hd.flags); |
1169 | 0 | } |
1170 | 0 | } |
1171 | | |
1172 | | static int on_frame_send(nghttp2_session *session, const nghttp2_frame *frame, |
1173 | | void *userp) |
1174 | 0 | { |
1175 | 0 | struct Curl_cfilter *cf = userp; |
1176 | 0 | struct Curl_easy *data = CF_DATA_CURRENT(cf); |
1177 | |
|
1178 | 0 | (void)session; |
1179 | 0 | DEBUGASSERT(data); |
1180 | 0 | if(data && Curl_trc_cf_is_verbose(cf, data)) { |
1181 | 0 | char buffer[256]; |
1182 | 0 | int len; |
1183 | 0 | len = fr_print(frame, buffer, sizeof(buffer)-1); |
1184 | 0 | buffer[len] = 0; |
1185 | 0 | CURL_TRC_CF(data, cf, "[%d] -> %s", frame->hd.stream_id, buffer); |
1186 | 0 | } |
1187 | 0 | return 0; |
1188 | 0 | } |
1189 | | #endif /* !CURL_DISABLE_VERBOSE_STRINGS */ |
1190 | | |
1191 | | static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame, |
1192 | | void *userp) |
1193 | 0 | { |
1194 | 0 | struct Curl_cfilter *cf = userp; |
1195 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
1196 | 0 | struct Curl_easy *data = CF_DATA_CURRENT(cf), *data_s; |
1197 | 0 | int32_t stream_id = frame->hd.stream_id; |
1198 | |
|
1199 | 0 | DEBUGASSERT(data); |
1200 | 0 | #ifndef CURL_DISABLE_VERBOSE_STRINGS |
1201 | 0 | if(Curl_trc_cf_is_verbose(cf, data)) { |
1202 | 0 | char buffer[256]; |
1203 | 0 | int len; |
1204 | 0 | len = fr_print(frame, buffer, sizeof(buffer)-1); |
1205 | 0 | buffer[len] = 0; |
1206 | 0 | CURL_TRC_CF(data, cf, "[%d] <- %s",frame->hd.stream_id, buffer); |
1207 | 0 | } |
1208 | 0 | #endif /* !CURL_DISABLE_VERBOSE_STRINGS */ |
1209 | |
|
1210 | 0 | if(!stream_id) { |
1211 | | /* stream ID zero is for connection-oriented stuff */ |
1212 | 0 | DEBUGASSERT(data); |
1213 | 0 | switch(frame->hd.type) { |
1214 | 0 | case NGHTTP2_SETTINGS: { |
1215 | 0 | if(!(frame->hd.flags & NGHTTP2_FLAG_ACK)) { |
1216 | 0 | uint32_t max_conn = ctx->max_concurrent_streams; |
1217 | 0 | ctx->max_concurrent_streams = nghttp2_session_get_remote_settings( |
1218 | 0 | session, NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS); |
1219 | 0 | ctx->enable_push = nghttp2_session_get_remote_settings( |
1220 | 0 | session, NGHTTP2_SETTINGS_ENABLE_PUSH) != 0; |
1221 | 0 | CURL_TRC_CF(data, cf, "[0] MAX_CONCURRENT_STREAMS: %d", |
1222 | 0 | ctx->max_concurrent_streams); |
1223 | 0 | CURL_TRC_CF(data, cf, "[0] ENABLE_PUSH: %s", |
1224 | 0 | ctx->enable_push ? "TRUE" : "false"); |
1225 | 0 | if(data && max_conn != ctx->max_concurrent_streams) { |
1226 | | /* only signal change if the value actually changed */ |
1227 | 0 | CURL_TRC_CF(data, cf, "[0] notify MAX_CONCURRENT_STREAMS: %u", |
1228 | 0 | ctx->max_concurrent_streams); |
1229 | 0 | Curl_multi_connchanged(data->multi); |
1230 | 0 | } |
1231 | | /* Since the initial stream window is 64K, a request might be on HOLD, |
1232 | | * due to exhaustion. The (initial) SETTINGS may announce a much larger |
1233 | | * window and *assume* that we treat this like a WINDOW_UPDATE. Some |
1234 | | * servers send an explicit WINDOW_UPDATE, but not all seem to do that. |
1235 | | * To be safe, we UNHOLD a stream in order not to stall. */ |
1236 | 0 | if(CURL_WANT_SEND(data)) { |
1237 | 0 | struct stream_ctx *stream = H2_STREAM_CTX(data); |
1238 | 0 | if(stream) |
1239 | 0 | drain_stream(cf, data, stream); |
1240 | 0 | } |
1241 | 0 | } |
1242 | 0 | break; |
1243 | 0 | } |
1244 | 0 | case NGHTTP2_GOAWAY: |
1245 | 0 | ctx->goaway = TRUE; |
1246 | 0 | ctx->goaway_error = frame->goaway.error_code; |
1247 | 0 | ctx->last_stream_id = frame->goaway.last_stream_id; |
1248 | 0 | if(data) { |
1249 | 0 | infof(data, "received GOAWAY, error=%d, last_stream=%u", |
1250 | 0 | ctx->goaway_error, ctx->last_stream_id); |
1251 | 0 | Curl_multi_connchanged(data->multi); |
1252 | 0 | } |
1253 | 0 | break; |
1254 | 0 | default: |
1255 | 0 | break; |
1256 | 0 | } |
1257 | 0 | return 0; |
1258 | 0 | } |
1259 | | |
1260 | 0 | data_s = nghttp2_session_get_stream_user_data(session, stream_id); |
1261 | 0 | if(!data_s) { |
1262 | 0 | CURL_TRC_CF(data, cf, "[%d] No Curl_easy associated", stream_id); |
1263 | 0 | return 0; |
1264 | 0 | } |
1265 | | |
1266 | 0 | return on_stream_frame(cf, data_s, frame)? NGHTTP2_ERR_CALLBACK_FAILURE : 0; |
1267 | 0 | } |
1268 | | |
1269 | | static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags, |
1270 | | int32_t stream_id, |
1271 | | const uint8_t *mem, size_t len, void *userp) |
1272 | 0 | { |
1273 | 0 | struct Curl_cfilter *cf = userp; |
1274 | 0 | struct stream_ctx *stream; |
1275 | 0 | struct Curl_easy *data_s; |
1276 | 0 | ssize_t nwritten; |
1277 | 0 | CURLcode result; |
1278 | 0 | (void)flags; |
1279 | |
|
1280 | 0 | DEBUGASSERT(stream_id); /* should never be a zero stream ID here */ |
1281 | 0 | DEBUGASSERT(CF_DATA_CURRENT(cf)); |
1282 | | |
1283 | | /* get the stream from the hash based on Stream ID */ |
1284 | 0 | data_s = nghttp2_session_get_stream_user_data(session, stream_id); |
1285 | 0 | if(!data_s) { |
1286 | | /* Receiving a Stream ID not in the hash should not happen - unless |
1287 | | we have aborted a transfer artificially and there were more data |
1288 | | in the pipeline. Silently ignore. */ |
1289 | 0 | CURL_TRC_CF(CF_DATA_CURRENT(cf), cf, "[%d] Data for unknown", |
1290 | 0 | stream_id); |
1291 | | /* consumed explicitly as no one will read it */ |
1292 | 0 | nghttp2_session_consume(session, stream_id, len); |
1293 | 0 | return 0; |
1294 | 0 | } |
1295 | | |
1296 | 0 | stream = H2_STREAM_CTX(data_s); |
1297 | 0 | if(!stream) |
1298 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1299 | | |
1300 | 0 | nwritten = Curl_bufq_write(&stream->recvbuf, mem, len, &result); |
1301 | 0 | if(nwritten < 0) { |
1302 | 0 | if(result != CURLE_AGAIN) |
1303 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1304 | | |
1305 | 0 | nwritten = 0; |
1306 | 0 | } |
1307 | | |
1308 | | /* if we receive data for another handle, wake that up */ |
1309 | 0 | drain_stream(cf, data_s, stream); |
1310 | |
|
1311 | 0 | DEBUGASSERT((size_t)nwritten == len); |
1312 | 0 | return 0; |
1313 | 0 | } |
1314 | | |
1315 | | static int on_stream_close(nghttp2_session *session, int32_t stream_id, |
1316 | | uint32_t error_code, void *userp) |
1317 | 0 | { |
1318 | 0 | struct Curl_cfilter *cf = userp; |
1319 | 0 | struct Curl_easy *data_s; |
1320 | 0 | struct stream_ctx *stream; |
1321 | 0 | int rv; |
1322 | 0 | (void)session; |
1323 | | |
1324 | | /* get the stream from the hash based on Stream ID, stream ID zero is for |
1325 | | connection-oriented stuff */ |
1326 | 0 | data_s = stream_id? |
1327 | 0 | nghttp2_session_get_stream_user_data(session, stream_id) : NULL; |
1328 | 0 | if(!data_s) { |
1329 | 0 | return 0; |
1330 | 0 | } |
1331 | 0 | stream = H2_STREAM_CTX(data_s); |
1332 | 0 | if(!stream) |
1333 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1334 | | |
1335 | 0 | stream->closed = TRUE; |
1336 | 0 | stream->error = error_code; |
1337 | 0 | if(stream->error) |
1338 | 0 | stream->reset = TRUE; |
1339 | |
|
1340 | 0 | if(stream->error) |
1341 | 0 | CURL_TRC_CF(data_s, cf, "[%d] RESET: %s (err %d)", |
1342 | 0 | stream_id, nghttp2_http2_strerror(error_code), error_code); |
1343 | 0 | else |
1344 | 0 | CURL_TRC_CF(data_s, cf, "[%d] CLOSED", stream_id); |
1345 | 0 | drain_stream(cf, data_s, stream); |
1346 | | |
1347 | | /* remove `data_s` from the nghttp2 stream */ |
1348 | 0 | rv = nghttp2_session_set_stream_user_data(session, stream_id, 0); |
1349 | 0 | if(rv) { |
1350 | 0 | infof(data_s, "http/2: failed to clear user_data for stream %u", |
1351 | 0 | stream_id); |
1352 | 0 | DEBUGASSERT(0); |
1353 | 0 | } |
1354 | 0 | return 0; |
1355 | 0 | } |
1356 | | |
1357 | | static int on_begin_headers(nghttp2_session *session, |
1358 | | const nghttp2_frame *frame, void *userp) |
1359 | 0 | { |
1360 | 0 | struct Curl_cfilter *cf = userp; |
1361 | 0 | struct stream_ctx *stream; |
1362 | 0 | struct Curl_easy *data_s = NULL; |
1363 | |
|
1364 | 0 | (void)cf; |
1365 | 0 | data_s = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id); |
1366 | 0 | if(!data_s) { |
1367 | 0 | return 0; |
1368 | 0 | } |
1369 | | |
1370 | 0 | if(frame->hd.type != NGHTTP2_HEADERS) { |
1371 | 0 | return 0; |
1372 | 0 | } |
1373 | | |
1374 | 0 | stream = H2_STREAM_CTX(data_s); |
1375 | 0 | if(!stream || !stream->bodystarted) { |
1376 | 0 | return 0; |
1377 | 0 | } |
1378 | | |
1379 | 0 | return 0; |
1380 | 0 | } |
1381 | | |
1382 | | /* frame->hd.type is either NGHTTP2_HEADERS or NGHTTP2_PUSH_PROMISE */ |
1383 | | static int on_header(nghttp2_session *session, const nghttp2_frame *frame, |
1384 | | const uint8_t *name, size_t namelen, |
1385 | | const uint8_t *value, size_t valuelen, |
1386 | | uint8_t flags, |
1387 | | void *userp) |
1388 | 0 | { |
1389 | 0 | struct Curl_cfilter *cf = userp; |
1390 | 0 | struct stream_ctx *stream; |
1391 | 0 | struct Curl_easy *data_s; |
1392 | 0 | int32_t stream_id = frame->hd.stream_id; |
1393 | 0 | CURLcode result; |
1394 | 0 | (void)flags; |
1395 | |
|
1396 | 0 | DEBUGASSERT(stream_id); /* should never be a zero stream ID here */ |
1397 | | |
1398 | | /* get the stream from the hash based on Stream ID */ |
1399 | 0 | data_s = nghttp2_session_get_stream_user_data(session, stream_id); |
1400 | 0 | if(!data_s) |
1401 | | /* Receiving a Stream ID not in the hash should not happen, this is an |
1402 | | internal error more than anything else! */ |
1403 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1404 | | |
1405 | 0 | stream = H2_STREAM_CTX(data_s); |
1406 | 0 | if(!stream) { |
1407 | 0 | failf(data_s, "Internal NULL stream"); |
1408 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1409 | 0 | } |
1410 | | |
1411 | | /* Store received PUSH_PROMISE headers to be used when the subsequent |
1412 | | PUSH_PROMISE callback comes */ |
1413 | 0 | if(frame->hd.type == NGHTTP2_PUSH_PROMISE) { |
1414 | 0 | char *h; |
1415 | |
|
1416 | 0 | if(!strcmp(HTTP_PSEUDO_AUTHORITY, (const char *)name)) { |
1417 | | /* pseudo headers are lower case */ |
1418 | 0 | int rc = 0; |
1419 | 0 | char *check = aprintf("%s:%d", cf->conn->host.name, |
1420 | 0 | cf->conn->remote_port); |
1421 | 0 | if(!check) |
1422 | | /* no memory */ |
1423 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1424 | 0 | if(!strcasecompare(check, (const char *)value) && |
1425 | 0 | ((cf->conn->remote_port != cf->conn->given->defport) || |
1426 | 0 | !strcasecompare(cf->conn->host.name, (const char *)value))) { |
1427 | | /* This is push is not for the same authority that was asked for in |
1428 | | * the URL. RFC 7540 section 8.2 says: "A client MUST treat a |
1429 | | * PUSH_PROMISE for which the server is not authoritative as a stream |
1430 | | * error of type PROTOCOL_ERROR." |
1431 | | */ |
1432 | 0 | (void)nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, |
1433 | 0 | stream_id, NGHTTP2_PROTOCOL_ERROR); |
1434 | 0 | rc = NGHTTP2_ERR_CALLBACK_FAILURE; |
1435 | 0 | } |
1436 | 0 | free(check); |
1437 | 0 | if(rc) |
1438 | 0 | return rc; |
1439 | 0 | } |
1440 | | |
1441 | 0 | if(!stream->push_headers) { |
1442 | 0 | stream->push_headers_alloc = 10; |
1443 | 0 | stream->push_headers = malloc(stream->push_headers_alloc * |
1444 | 0 | sizeof(char *)); |
1445 | 0 | if(!stream->push_headers) |
1446 | 0 | return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; |
1447 | 0 | stream->push_headers_used = 0; |
1448 | 0 | } |
1449 | 0 | else if(stream->push_headers_used == |
1450 | 0 | stream->push_headers_alloc) { |
1451 | 0 | char **headp; |
1452 | 0 | if(stream->push_headers_alloc > 1000) { |
1453 | | /* this is beyond crazy many headers, bail out */ |
1454 | 0 | failf(data_s, "Too many PUSH_PROMISE headers"); |
1455 | 0 | Curl_safefree(stream->push_headers); |
1456 | 0 | return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; |
1457 | 0 | } |
1458 | 0 | stream->push_headers_alloc *= 2; |
1459 | 0 | headp = Curl_saferealloc(stream->push_headers, |
1460 | 0 | stream->push_headers_alloc * sizeof(char *)); |
1461 | 0 | if(!headp) { |
1462 | 0 | stream->push_headers = NULL; |
1463 | 0 | return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; |
1464 | 0 | } |
1465 | 0 | stream->push_headers = headp; |
1466 | 0 | } |
1467 | 0 | h = aprintf("%s:%s", name, value); |
1468 | 0 | if(h) |
1469 | 0 | stream->push_headers[stream->push_headers_used++] = h; |
1470 | 0 | return 0; |
1471 | 0 | } |
1472 | | |
1473 | 0 | if(stream->bodystarted) { |
1474 | | /* This is a trailer */ |
1475 | 0 | CURL_TRC_CF(data_s, cf, "[%d] trailer: %.*s: %.*s", |
1476 | 0 | stream->id, (int)namelen, name, (int)valuelen, value); |
1477 | 0 | result = Curl_dynhds_add(&stream->resp_trailers, |
1478 | 0 | (const char *)name, namelen, |
1479 | 0 | (const char *)value, valuelen); |
1480 | 0 | if(result) |
1481 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1482 | | |
1483 | 0 | return 0; |
1484 | 0 | } |
1485 | | |
1486 | 0 | if(namelen == sizeof(HTTP_PSEUDO_STATUS) - 1 && |
1487 | 0 | memcmp(HTTP_PSEUDO_STATUS, name, namelen) == 0) { |
1488 | | /* nghttp2 guarantees :status is received first and only once. */ |
1489 | 0 | char buffer[32]; |
1490 | 0 | result = Curl_http_decode_status(&stream->status_code, |
1491 | 0 | (const char *)value, valuelen); |
1492 | 0 | if(result) |
1493 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1494 | 0 | msnprintf(buffer, sizeof(buffer), HTTP_PSEUDO_STATUS ":%u\r", |
1495 | 0 | stream->status_code); |
1496 | 0 | result = Curl_headers_push(data_s, buffer, CURLH_PSEUDO); |
1497 | 0 | if(result) |
1498 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1499 | 0 | result = recvbuf_write_hds(cf, data_s, STRCONST("HTTP/2 ")); |
1500 | 0 | if(result) |
1501 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1502 | 0 | result = recvbuf_write_hds(cf, data_s, (const char *)value, valuelen); |
1503 | 0 | if(result) |
1504 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1505 | | /* the space character after the status code is mandatory */ |
1506 | 0 | result = recvbuf_write_hds(cf, data_s, STRCONST(" \r\n")); |
1507 | 0 | if(result) |
1508 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1509 | | /* if we receive data for another handle, wake that up */ |
1510 | 0 | if(CF_DATA_CURRENT(cf) != data_s) |
1511 | 0 | Curl_expire(data_s, 0, EXPIRE_RUN_NOW); |
1512 | |
|
1513 | 0 | CURL_TRC_CF(data_s, cf, "[%d] status: HTTP/2 %03d", |
1514 | 0 | stream->id, stream->status_code); |
1515 | 0 | return 0; |
1516 | 0 | } |
1517 | | |
1518 | | /* nghttp2 guarantees that namelen > 0, and :status was already |
1519 | | received, and this is not pseudo-header field . */ |
1520 | | /* convert to an HTTP1-style header */ |
1521 | 0 | result = recvbuf_write_hds(cf, data_s, (const char *)name, namelen); |
1522 | 0 | if(result) |
1523 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1524 | 0 | result = recvbuf_write_hds(cf, data_s, STRCONST(": ")); |
1525 | 0 | if(result) |
1526 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1527 | 0 | result = recvbuf_write_hds(cf, data_s, (const char *)value, valuelen); |
1528 | 0 | if(result) |
1529 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1530 | 0 | result = recvbuf_write_hds(cf, data_s, STRCONST("\r\n")); |
1531 | 0 | if(result) |
1532 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1533 | | /* if we receive data for another handle, wake that up */ |
1534 | 0 | if(CF_DATA_CURRENT(cf) != data_s) |
1535 | 0 | Curl_expire(data_s, 0, EXPIRE_RUN_NOW); |
1536 | |
|
1537 | 0 | CURL_TRC_CF(data_s, cf, "[%d] header: %.*s: %.*s", |
1538 | 0 | stream->id, (int)namelen, name, (int)valuelen, value); |
1539 | |
|
1540 | 0 | return 0; /* 0 is successful */ |
1541 | 0 | } |
1542 | | |
1543 | | static ssize_t req_body_read_callback(nghttp2_session *session, |
1544 | | int32_t stream_id, |
1545 | | uint8_t *buf, size_t length, |
1546 | | uint32_t *data_flags, |
1547 | | nghttp2_data_source *source, |
1548 | | void *userp) |
1549 | 0 | { |
1550 | 0 | struct Curl_cfilter *cf = userp; |
1551 | 0 | struct Curl_easy *data_s; |
1552 | 0 | struct stream_ctx *stream = NULL; |
1553 | 0 | CURLcode result; |
1554 | 0 | ssize_t nread; |
1555 | 0 | (void)source; |
1556 | |
|
1557 | 0 | (void)cf; |
1558 | 0 | if(stream_id) { |
1559 | | /* get the stream from the hash based on Stream ID, stream ID zero is for |
1560 | | connection-oriented stuff */ |
1561 | 0 | data_s = nghttp2_session_get_stream_user_data(session, stream_id); |
1562 | 0 | if(!data_s) |
1563 | | /* Receiving a Stream ID not in the hash should not happen, this is an |
1564 | | internal error more than anything else! */ |
1565 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1566 | | |
1567 | 0 | stream = H2_STREAM_CTX(data_s); |
1568 | 0 | if(!stream) |
1569 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1570 | 0 | } |
1571 | 0 | else |
1572 | 0 | return NGHTTP2_ERR_INVALID_ARGUMENT; |
1573 | | |
1574 | 0 | nread = Curl_bufq_read(&stream->sendbuf, buf, length, &result); |
1575 | 0 | if(nread < 0) { |
1576 | 0 | if(result != CURLE_AGAIN) |
1577 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
1578 | 0 | nread = 0; |
1579 | 0 | } |
1580 | | |
1581 | 0 | if(nread > 0 && stream->upload_left != -1) |
1582 | 0 | stream->upload_left -= nread; |
1583 | |
|
1584 | 0 | CURL_TRC_CF(data_s, cf, "[%d] req_body_read(len=%zu) left=%" |
1585 | 0 | CURL_FORMAT_CURL_OFF_T " -> %zd, %d", |
1586 | 0 | stream_id, length, stream->upload_left, nread, result); |
1587 | |
|
1588 | 0 | if(stream->upload_left == 0) |
1589 | 0 | *data_flags = NGHTTP2_DATA_FLAG_EOF; |
1590 | 0 | else if(nread == 0) |
1591 | 0 | return NGHTTP2_ERR_DEFERRED; |
1592 | | |
1593 | 0 | return nread; |
1594 | 0 | } |
1595 | | |
1596 | | #if !defined(CURL_DISABLE_VERBOSE_STRINGS) |
1597 | | static int error_callback(nghttp2_session *session, |
1598 | | const char *msg, |
1599 | | size_t len, |
1600 | | void *userp) |
1601 | 0 | { |
1602 | 0 | struct Curl_cfilter *cf = userp; |
1603 | 0 | struct Curl_easy *data = CF_DATA_CURRENT(cf); |
1604 | 0 | (void)session; |
1605 | 0 | failf(data, "%.*s", (int)len, msg); |
1606 | 0 | return 0; |
1607 | 0 | } |
1608 | | #endif |
1609 | | |
1610 | | /* |
1611 | | * Append headers to ask for an HTTP1.1 to HTTP2 upgrade. |
1612 | | */ |
1613 | | CURLcode Curl_http2_request_upgrade(struct dynbuf *req, |
1614 | | struct Curl_easy *data) |
1615 | 0 | { |
1616 | 0 | CURLcode result; |
1617 | 0 | char *base64; |
1618 | 0 | size_t blen; |
1619 | 0 | struct SingleRequest *k = &data->req; |
1620 | 0 | uint8_t binsettings[H2_BINSETTINGS_LEN]; |
1621 | 0 | ssize_t binlen; /* length of the binsettings data */ |
1622 | |
|
1623 | 0 | binlen = populate_binsettings(binsettings, data); |
1624 | 0 | if(binlen <= 0) { |
1625 | 0 | failf(data, "nghttp2 unexpectedly failed on pack_settings_payload"); |
1626 | 0 | Curl_dyn_free(req); |
1627 | 0 | return CURLE_FAILED_INIT; |
1628 | 0 | } |
1629 | | |
1630 | 0 | result = Curl_base64url_encode((const char *)binsettings, binlen, |
1631 | 0 | &base64, &blen); |
1632 | 0 | if(result) { |
1633 | 0 | Curl_dyn_free(req); |
1634 | 0 | return result; |
1635 | 0 | } |
1636 | | |
1637 | 0 | result = Curl_dyn_addf(req, |
1638 | 0 | "Connection: Upgrade, HTTP2-Settings\r\n" |
1639 | 0 | "Upgrade: %s\r\n" |
1640 | 0 | "HTTP2-Settings: %s\r\n", |
1641 | 0 | NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, base64); |
1642 | 0 | free(base64); |
1643 | |
|
1644 | 0 | k->upgr101 = UPGR101_H2; |
1645 | |
|
1646 | 0 | return result; |
1647 | 0 | } |
1648 | | |
1649 | | static CURLcode http2_data_done_send(struct Curl_cfilter *cf, |
1650 | | struct Curl_easy *data) |
1651 | 0 | { |
1652 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
1653 | 0 | CURLcode result = CURLE_OK; |
1654 | 0 | struct stream_ctx *stream = H2_STREAM_CTX(data); |
1655 | |
|
1656 | 0 | if(!ctx || !ctx->h2 || !stream) |
1657 | 0 | goto out; |
1658 | | |
1659 | 0 | CURL_TRC_CF(data, cf, "[%d] data done send", stream->id); |
1660 | 0 | if(!stream->send_closed) { |
1661 | 0 | stream->send_closed = TRUE; |
1662 | 0 | if(stream->upload_left) { |
1663 | | /* we now know that everything that is buffered is all there is. */ |
1664 | 0 | stream->upload_left = Curl_bufq_len(&stream->sendbuf); |
1665 | | /* resume sending here to trigger the callback to get called again so |
1666 | | that it can signal EOF to nghttp2 */ |
1667 | 0 | (void)nghttp2_session_resume_data(ctx->h2, stream->id); |
1668 | 0 | drain_stream(cf, data, stream); |
1669 | 0 | } |
1670 | 0 | } |
1671 | |
|
1672 | 0 | out: |
1673 | 0 | return result; |
1674 | 0 | } |
1675 | | |
1676 | | static ssize_t http2_handle_stream_close(struct Curl_cfilter *cf, |
1677 | | struct Curl_easy *data, |
1678 | | struct stream_ctx *stream, |
1679 | | CURLcode *err) |
1680 | 0 | { |
1681 | 0 | ssize_t rv = 0; |
1682 | |
|
1683 | 0 | if(stream->error == NGHTTP2_REFUSED_STREAM) { |
1684 | 0 | CURL_TRC_CF(data, cf, "[%d] REFUSED_STREAM, try again on a new " |
1685 | 0 | "connection", stream->id); |
1686 | 0 | connclose(cf->conn, "REFUSED_STREAM"); /* don't use this anymore */ |
1687 | 0 | data->state.refused_stream = TRUE; |
1688 | 0 | *err = CURLE_RECV_ERROR; /* trigger Curl_retry_request() later */ |
1689 | 0 | return -1; |
1690 | 0 | } |
1691 | 0 | else if(stream->error != NGHTTP2_NO_ERROR) { |
1692 | 0 | failf(data, "HTTP/2 stream %u was not closed cleanly: %s (err %u)", |
1693 | 0 | stream->id, nghttp2_http2_strerror(stream->error), |
1694 | 0 | stream->error); |
1695 | 0 | *err = CURLE_HTTP2_STREAM; |
1696 | 0 | return -1; |
1697 | 0 | } |
1698 | 0 | else if(stream->reset) { |
1699 | 0 | failf(data, "HTTP/2 stream %u was reset", stream->id); |
1700 | 0 | *err = stream->bodystarted? CURLE_PARTIAL_FILE : CURLE_RECV_ERROR; |
1701 | 0 | return -1; |
1702 | 0 | } |
1703 | | |
1704 | 0 | if(!stream->bodystarted) { |
1705 | 0 | failf(data, "HTTP/2 stream %u was closed cleanly, but before getting " |
1706 | 0 | " all response header fields, treated as error", |
1707 | 0 | stream->id); |
1708 | 0 | *err = CURLE_HTTP2_STREAM; |
1709 | 0 | return -1; |
1710 | 0 | } |
1711 | | |
1712 | 0 | if(Curl_dynhds_count(&stream->resp_trailers)) { |
1713 | 0 | struct dynhds_entry *e; |
1714 | 0 | struct dynbuf dbuf; |
1715 | 0 | size_t i; |
1716 | |
|
1717 | 0 | *err = CURLE_OK; |
1718 | 0 | Curl_dyn_init(&dbuf, DYN_TRAILERS); |
1719 | 0 | for(i = 0; i < Curl_dynhds_count(&stream->resp_trailers); ++i) { |
1720 | 0 | e = Curl_dynhds_getn(&stream->resp_trailers, i); |
1721 | 0 | if(!e) |
1722 | 0 | break; |
1723 | 0 | Curl_dyn_reset(&dbuf); |
1724 | 0 | *err = Curl_dyn_addf(&dbuf, "%.*s: %.*s\x0d\x0a", |
1725 | 0 | (int)e->namelen, e->name, |
1726 | 0 | (int)e->valuelen, e->value); |
1727 | 0 | if(*err) |
1728 | 0 | break; |
1729 | 0 | Curl_debug(data, CURLINFO_HEADER_IN, Curl_dyn_ptr(&dbuf), |
1730 | 0 | Curl_dyn_len(&dbuf)); |
1731 | 0 | *err = Curl_client_write(data, CLIENTWRITE_HEADER|CLIENTWRITE_TRAILER, |
1732 | 0 | Curl_dyn_ptr(&dbuf), Curl_dyn_len(&dbuf)); |
1733 | 0 | if(*err) |
1734 | 0 | break; |
1735 | 0 | } |
1736 | 0 | Curl_dyn_free(&dbuf); |
1737 | 0 | if(*err) |
1738 | 0 | goto out; |
1739 | 0 | } |
1740 | | |
1741 | 0 | stream->close_handled = TRUE; |
1742 | 0 | *err = CURLE_OK; |
1743 | 0 | rv = 0; |
1744 | |
|
1745 | 0 | out: |
1746 | 0 | CURL_TRC_CF(data, cf, "handle_stream_close -> %zd, %d", rv, *err); |
1747 | 0 | return rv; |
1748 | 0 | } |
1749 | | |
1750 | | static int sweight_wanted(const struct Curl_easy *data) |
1751 | 0 | { |
1752 | | /* 0 weight is not set by user and we take the nghttp2 default one */ |
1753 | 0 | return data->set.priority.weight? |
1754 | 0 | data->set.priority.weight : NGHTTP2_DEFAULT_WEIGHT; |
1755 | 0 | } |
1756 | | |
1757 | | static int sweight_in_effect(const struct Curl_easy *data) |
1758 | 0 | { |
1759 | | /* 0 weight is not set by user and we take the nghttp2 default one */ |
1760 | 0 | return data->state.priority.weight? |
1761 | 0 | data->state.priority.weight : NGHTTP2_DEFAULT_WEIGHT; |
1762 | 0 | } |
1763 | | |
1764 | | /* |
1765 | | * h2_pri_spec() fills in the pri_spec struct, used by nghttp2 to send weight |
1766 | | * and dependency to the peer. It also stores the updated values in the state |
1767 | | * struct. |
1768 | | */ |
1769 | | |
1770 | | static void h2_pri_spec(struct Curl_easy *data, |
1771 | | nghttp2_priority_spec *pri_spec) |
1772 | 0 | { |
1773 | 0 | struct Curl_data_priority *prio = &data->set.priority; |
1774 | 0 | struct stream_ctx *depstream = H2_STREAM_CTX(prio->parent); |
1775 | 0 | int32_t depstream_id = depstream? depstream->id:0; |
1776 | 0 | nghttp2_priority_spec_init(pri_spec, depstream_id, |
1777 | 0 | sweight_wanted(data), |
1778 | 0 | data->set.priority.exclusive); |
1779 | 0 | data->state.priority = *prio; |
1780 | 0 | } |
1781 | | |
1782 | | /* |
1783 | | * Check if there's been an update in the priority / |
1784 | | * dependency settings and if so it submits a PRIORITY frame with the updated |
1785 | | * info. |
1786 | | * Flush any out data pending in the network buffer. |
1787 | | */ |
1788 | | static CURLcode h2_progress_egress(struct Curl_cfilter *cf, |
1789 | | struct Curl_easy *data) |
1790 | 0 | { |
1791 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
1792 | 0 | struct stream_ctx *stream = H2_STREAM_CTX(data); |
1793 | 0 | int rv = 0; |
1794 | |
|
1795 | 0 | if(stream && stream->id > 0 && |
1796 | 0 | ((sweight_wanted(data) != sweight_in_effect(data)) || |
1797 | 0 | (data->set.priority.exclusive != data->state.priority.exclusive) || |
1798 | 0 | (data->set.priority.parent != data->state.priority.parent)) ) { |
1799 | | /* send new weight and/or dependency */ |
1800 | 0 | nghttp2_priority_spec pri_spec; |
1801 | |
|
1802 | 0 | h2_pri_spec(data, &pri_spec); |
1803 | 0 | CURL_TRC_CF(data, cf, "[%d] Queuing PRIORITY", stream->id); |
1804 | 0 | DEBUGASSERT(stream->id != -1); |
1805 | 0 | rv = nghttp2_submit_priority(ctx->h2, NGHTTP2_FLAG_NONE, |
1806 | 0 | stream->id, &pri_spec); |
1807 | 0 | if(rv) |
1808 | 0 | goto out; |
1809 | 0 | } |
1810 | | |
1811 | 0 | ctx->nw_out_blocked = 0; |
1812 | 0 | while(!rv && !ctx->nw_out_blocked && nghttp2_session_want_write(ctx->h2)) |
1813 | 0 | rv = nghttp2_session_send(ctx->h2); |
1814 | |
|
1815 | 0 | out: |
1816 | 0 | if(nghttp2_is_fatal(rv)) { |
1817 | 0 | CURL_TRC_CF(data, cf, "nghttp2_session_send error (%s)%d", |
1818 | 0 | nghttp2_strerror(rv), rv); |
1819 | 0 | return CURLE_SEND_ERROR; |
1820 | 0 | } |
1821 | 0 | return nw_out_flush(cf, data); |
1822 | 0 | } |
1823 | | |
1824 | | static ssize_t stream_recv(struct Curl_cfilter *cf, struct Curl_easy *data, |
1825 | | struct stream_ctx *stream, |
1826 | | char *buf, size_t len, CURLcode *err) |
1827 | 0 | { |
1828 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
1829 | 0 | ssize_t nread = -1; |
1830 | |
|
1831 | 0 | *err = CURLE_AGAIN; |
1832 | 0 | if(!Curl_bufq_is_empty(&stream->recvbuf)) { |
1833 | 0 | nread = Curl_bufq_read(&stream->recvbuf, |
1834 | 0 | (unsigned char *)buf, len, err); |
1835 | 0 | if(nread < 0) |
1836 | 0 | goto out; |
1837 | 0 | DEBUGASSERT(nread > 0); |
1838 | 0 | } |
1839 | | |
1840 | 0 | if(nread < 0) { |
1841 | 0 | if(stream->closed) { |
1842 | 0 | CURL_TRC_CF(data, cf, "[%d] returning CLOSE", stream->id); |
1843 | 0 | nread = http2_handle_stream_close(cf, data, stream, err); |
1844 | 0 | } |
1845 | 0 | else if(stream->reset || |
1846 | 0 | (ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) || |
1847 | 0 | (ctx->goaway && ctx->last_stream_id < stream->id)) { |
1848 | 0 | CURL_TRC_CF(data, cf, "[%d] returning ERR", stream->id); |
1849 | 0 | *err = stream->bodystarted? CURLE_PARTIAL_FILE : CURLE_RECV_ERROR; |
1850 | 0 | nread = -1; |
1851 | 0 | } |
1852 | 0 | } |
1853 | 0 | else if(nread == 0) { |
1854 | 0 | *err = CURLE_AGAIN; |
1855 | 0 | nread = -1; |
1856 | 0 | } |
1857 | |
|
1858 | 0 | out: |
1859 | 0 | if(nread < 0 && *err != CURLE_AGAIN) |
1860 | 0 | CURL_TRC_CF(data, cf, "[%d] stream_recv(len=%zu) -> %zd, %d", |
1861 | 0 | stream->id, len, nread, *err); |
1862 | 0 | return nread; |
1863 | 0 | } |
1864 | | |
1865 | | static CURLcode h2_progress_ingress(struct Curl_cfilter *cf, |
1866 | | struct Curl_easy *data) |
1867 | 0 | { |
1868 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
1869 | 0 | struct stream_ctx *stream; |
1870 | 0 | CURLcode result = CURLE_OK; |
1871 | 0 | ssize_t nread; |
1872 | | |
1873 | | /* Process network input buffer fist */ |
1874 | 0 | if(!Curl_bufq_is_empty(&ctx->inbufq)) { |
1875 | 0 | CURL_TRC_CF(data, cf, "Process %zu bytes in connection buffer", |
1876 | 0 | Curl_bufq_len(&ctx->inbufq)); |
1877 | 0 | if(h2_process_pending_input(cf, data, &result) < 0) |
1878 | 0 | return result; |
1879 | 0 | } |
1880 | | |
1881 | | /* Receive data from the "lower" filters, e.g. network until |
1882 | | * it is time to stop due to connection close or us not processing |
1883 | | * all network input */ |
1884 | 0 | while(!ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) { |
1885 | 0 | stream = H2_STREAM_CTX(data); |
1886 | 0 | if(stream && (stream->closed || Curl_bufq_is_full(&stream->recvbuf))) { |
1887 | | /* We would like to abort here and stop processing, so that |
1888 | | * the transfer loop can handle the data/close here. However, |
1889 | | * this may leave data in underlying buffers that will not |
1890 | | * be consumed. */ |
1891 | 0 | if(!cf->next || !cf->next->cft->has_data_pending(cf->next, data)) |
1892 | 0 | break; |
1893 | 0 | } |
1894 | | |
1895 | 0 | nread = Curl_bufq_slurp(&ctx->inbufq, nw_in_reader, cf, &result); |
1896 | 0 | if(nread < 0) { |
1897 | 0 | if(result != CURLE_AGAIN) { |
1898 | 0 | failf(data, "Failed receiving HTTP2 data: %d(%s)", result, |
1899 | 0 | curl_easy_strerror(result)); |
1900 | 0 | return result; |
1901 | 0 | } |
1902 | 0 | break; |
1903 | 0 | } |
1904 | 0 | else if(nread == 0) { |
1905 | 0 | CURL_TRC_CF(data, cf, "[0] ingress: connection closed"); |
1906 | 0 | ctx->conn_closed = TRUE; |
1907 | 0 | break; |
1908 | 0 | } |
1909 | 0 | else { |
1910 | 0 | CURL_TRC_CF(data, cf, "[0] ingress: read %zd bytes", |
1911 | 0 | nread); |
1912 | 0 | } |
1913 | | |
1914 | 0 | if(h2_process_pending_input(cf, data, &result)) |
1915 | 0 | return result; |
1916 | 0 | } |
1917 | | |
1918 | 0 | if(ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) { |
1919 | 0 | connclose(cf->conn, "GOAWAY received"); |
1920 | 0 | } |
1921 | |
|
1922 | 0 | return CURLE_OK; |
1923 | 0 | } |
1924 | | |
1925 | | static ssize_t cf_h2_recv(struct Curl_cfilter *cf, struct Curl_easy *data, |
1926 | | char *buf, size_t len, CURLcode *err) |
1927 | 0 | { |
1928 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
1929 | 0 | struct stream_ctx *stream = H2_STREAM_CTX(data); |
1930 | 0 | ssize_t nread = -1; |
1931 | 0 | CURLcode result; |
1932 | 0 | struct cf_call_data save; |
1933 | |
|
1934 | 0 | if(!stream) { |
1935 | | /* Abnormal call sequence: either this transfer has never opened a stream |
1936 | | * (unlikely) or the transfer has been done, cleaned up its resources, but |
1937 | | * a read() is called anyway. It is not clear what the calling sequence |
1938 | | * is for such a case. */ |
1939 | 0 | failf(data, "[%zd-%zd], http/2 recv on a transfer never opened " |
1940 | 0 | "or already cleared", (ssize_t)data->id, |
1941 | 0 | (ssize_t)cf->conn->connection_id); |
1942 | 0 | *err = CURLE_HTTP2; |
1943 | 0 | return -1; |
1944 | 0 | } |
1945 | | |
1946 | 0 | CF_DATA_SAVE(save, cf, data); |
1947 | | |
1948 | 0 | nread = stream_recv(cf, data, stream, buf, len, err); |
1949 | 0 | if(nread < 0 && *err != CURLE_AGAIN) |
1950 | 0 | goto out; |
1951 | | |
1952 | 0 | if(nread < 0) { |
1953 | 0 | *err = h2_progress_ingress(cf, data); |
1954 | 0 | if(*err) |
1955 | 0 | goto out; |
1956 | | |
1957 | 0 | nread = stream_recv(cf, data, stream, buf, len, err); |
1958 | 0 | } |
1959 | | |
1960 | 0 | if(nread > 0) { |
1961 | 0 | size_t data_consumed = (size_t)nread; |
1962 | | /* Now that we transferred this to the upper layer, we report |
1963 | | * the actual amount of DATA consumed to the H2 session, so |
1964 | | * that it adjusts stream flow control */ |
1965 | 0 | if(stream->resp_hds_len >= data_consumed) { |
1966 | 0 | stream->resp_hds_len -= data_consumed; /* no DATA */ |
1967 | 0 | } |
1968 | 0 | else { |
1969 | 0 | if(stream->resp_hds_len) { |
1970 | 0 | data_consumed -= stream->resp_hds_len; |
1971 | 0 | stream->resp_hds_len = 0; |
1972 | 0 | } |
1973 | 0 | if(data_consumed) { |
1974 | 0 | nghttp2_session_consume(ctx->h2, stream->id, data_consumed); |
1975 | 0 | } |
1976 | 0 | } |
1977 | |
|
1978 | 0 | if(stream->closed) { |
1979 | 0 | CURL_TRC_CF(data, cf, "[%d] DRAIN closed stream", stream->id); |
1980 | 0 | drain_stream(cf, data, stream); |
1981 | 0 | } |
1982 | 0 | } |
1983 | |
|
1984 | 0 | out: |
1985 | 0 | result = h2_progress_egress(cf, data); |
1986 | 0 | if(result == CURLE_AGAIN) { |
1987 | | /* pending data to send, need to be called again. Ideally, we'd |
1988 | | * monitor the socket for POLLOUT, but we might not be in SENDING |
1989 | | * transfer state any longer and are unable to make this happen. |
1990 | | */ |
1991 | 0 | drain_stream(cf, data, stream); |
1992 | 0 | } |
1993 | 0 | else if(result) { |
1994 | 0 | *err = result; |
1995 | 0 | nread = -1; |
1996 | 0 | } |
1997 | 0 | CURL_TRC_CF(data, cf, "[%d] cf_recv(len=%zu) -> %zd %d, " |
1998 | 0 | "buffered=%zu, window=%d/%d, connection %d/%d", |
1999 | 0 | stream->id, len, nread, *err, |
2000 | 0 | Curl_bufq_len(&stream->recvbuf), |
2001 | 0 | nghttp2_session_get_stream_effective_recv_data_length( |
2002 | 0 | ctx->h2, stream->id), |
2003 | 0 | nghttp2_session_get_stream_effective_local_window_size( |
2004 | 0 | ctx->h2, stream->id), |
2005 | 0 | nghttp2_session_get_local_window_size(ctx->h2), |
2006 | 0 | HTTP2_HUGE_WINDOW_SIZE); |
2007 | |
|
2008 | 0 | CF_DATA_RESTORE(cf, save); |
2009 | 0 | return nread; |
2010 | 0 | } |
2011 | | |
2012 | | static ssize_t h2_submit(struct stream_ctx **pstream, |
2013 | | struct Curl_cfilter *cf, struct Curl_easy *data, |
2014 | | const void *buf, size_t len, CURLcode *err) |
2015 | 0 | { |
2016 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
2017 | 0 | struct stream_ctx *stream = NULL; |
2018 | 0 | struct dynhds h2_headers; |
2019 | 0 | nghttp2_nv *nva = NULL; |
2020 | 0 | const void *body = NULL; |
2021 | 0 | size_t nheader, bodylen, i; |
2022 | 0 | nghttp2_data_provider data_prd; |
2023 | 0 | int32_t stream_id; |
2024 | 0 | nghttp2_priority_spec pri_spec; |
2025 | 0 | ssize_t nwritten; |
2026 | |
|
2027 | 0 | Curl_dynhds_init(&h2_headers, 0, DYN_HTTP_REQUEST); |
2028 | |
|
2029 | 0 | *err = http2_data_setup(cf, data, &stream); |
2030 | 0 | if(*err) { |
2031 | 0 | nwritten = -1; |
2032 | 0 | goto out; |
2033 | 0 | } |
2034 | | |
2035 | 0 | nwritten = Curl_h1_req_parse_read(&stream->h1, buf, len, NULL, 0, err); |
2036 | 0 | if(nwritten < 0) |
2037 | 0 | goto out; |
2038 | 0 | if(!stream->h1.done) { |
2039 | | /* need more data */ |
2040 | 0 | goto out; |
2041 | 0 | } |
2042 | 0 | DEBUGASSERT(stream->h1.req); |
2043 | | |
2044 | 0 | *err = Curl_http_req_to_h2(&h2_headers, stream->h1.req, data); |
2045 | 0 | if(*err) { |
2046 | 0 | nwritten = -1; |
2047 | 0 | goto out; |
2048 | 0 | } |
2049 | | /* no longer needed */ |
2050 | 0 | Curl_h1_req_parse_free(&stream->h1); |
2051 | |
|
2052 | 0 | nva = Curl_dynhds_to_nva(&h2_headers, &nheader); |
2053 | 0 | if(!nva) { |
2054 | 0 | *err = CURLE_OUT_OF_MEMORY; |
2055 | 0 | nwritten = -1; |
2056 | 0 | goto out; |
2057 | 0 | } |
2058 | | |
2059 | 0 | h2_pri_spec(data, &pri_spec); |
2060 | 0 | if(!nghttp2_session_check_request_allowed(ctx->h2)) |
2061 | 0 | CURL_TRC_CF(data, cf, "send request NOT allowed (via nghttp2)"); |
2062 | |
|
2063 | 0 | switch(data->state.httpreq) { |
2064 | 0 | case HTTPREQ_POST: |
2065 | 0 | case HTTPREQ_POST_FORM: |
2066 | 0 | case HTTPREQ_POST_MIME: |
2067 | 0 | case HTTPREQ_PUT: |
2068 | 0 | if(data->state.infilesize != -1) |
2069 | 0 | stream->upload_left = data->state.infilesize; |
2070 | 0 | else |
2071 | | /* data sending without specifying the data amount up front */ |
2072 | 0 | stream->upload_left = -1; /* unknown */ |
2073 | |
|
2074 | 0 | data_prd.read_callback = req_body_read_callback; |
2075 | 0 | data_prd.source.ptr = NULL; |
2076 | 0 | stream_id = nghttp2_submit_request(ctx->h2, &pri_spec, nva, nheader, |
2077 | 0 | &data_prd, data); |
2078 | 0 | break; |
2079 | 0 | default: |
2080 | 0 | stream->upload_left = 0; /* no request body */ |
2081 | 0 | stream_id = nghttp2_submit_request(ctx->h2, &pri_spec, nva, nheader, |
2082 | 0 | NULL, data); |
2083 | 0 | } |
2084 | | |
2085 | 0 | if(stream_id < 0) { |
2086 | 0 | CURL_TRC_CF(data, cf, "send: nghttp2_submit_request error (%s)%u", |
2087 | 0 | nghttp2_strerror(stream_id), stream_id); |
2088 | 0 | *err = CURLE_SEND_ERROR; |
2089 | 0 | nwritten = -1; |
2090 | 0 | goto out; |
2091 | 0 | } |
2092 | | |
2093 | 0 | #define MAX_ACC 60000 /* <64KB to account for some overhead */ |
2094 | 0 | if(Curl_trc_is_verbose(data)) { |
2095 | 0 | size_t acc = 0; |
2096 | |
|
2097 | 0 | infof(data, "[HTTP/2] [%d] OPENED stream for %s", |
2098 | 0 | stream_id, data->state.url); |
2099 | 0 | for(i = 0; i < nheader; ++i) { |
2100 | 0 | acc += nva[i].namelen + nva[i].valuelen; |
2101 | |
|
2102 | 0 | infof(data, "[HTTP/2] [%d] [%.*s: %.*s]", stream_id, |
2103 | 0 | (int)nva[i].namelen, nva[i].name, |
2104 | 0 | (int)nva[i].valuelen, nva[i].value); |
2105 | 0 | } |
2106 | |
|
2107 | 0 | if(acc > MAX_ACC) { |
2108 | 0 | infof(data, "[HTTP/2] Warning: The cumulative length of all " |
2109 | 0 | "headers exceeds %d bytes and that could cause the " |
2110 | 0 | "stream to be rejected.", MAX_ACC); |
2111 | 0 | } |
2112 | 0 | } |
2113 | |
|
2114 | 0 | stream->id = stream_id; |
2115 | 0 | stream->local_window_size = H2_STREAM_WINDOW_SIZE; |
2116 | 0 | if(data->set.max_recv_speed) { |
2117 | | /* We are asked to only receive `max_recv_speed` bytes per second. |
2118 | | * Let's limit our stream window size around that, otherwise the server |
2119 | | * will send in large bursts only. We make the window 50% larger to |
2120 | | * allow for data in flight and avoid stalling. */ |
2121 | 0 | curl_off_t n = (((data->set.max_recv_speed - 1) / H2_CHUNK_SIZE) + 1); |
2122 | 0 | n += CURLMAX((n/2), 1); |
2123 | 0 | if(n < (H2_STREAM_WINDOW_SIZE / H2_CHUNK_SIZE) && |
2124 | 0 | n < (UINT_MAX / H2_CHUNK_SIZE)) { |
2125 | 0 | stream->local_window_size = (uint32_t)n * H2_CHUNK_SIZE; |
2126 | 0 | } |
2127 | 0 | } |
2128 | |
|
2129 | 0 | body = (const char *)buf + nwritten; |
2130 | 0 | bodylen = len - nwritten; |
2131 | |
|
2132 | 0 | if(bodylen) { |
2133 | | /* We have request body to send in DATA frame */ |
2134 | 0 | ssize_t n = Curl_bufq_write(&stream->sendbuf, body, bodylen, err); |
2135 | 0 | if(n < 0) { |
2136 | 0 | *err = CURLE_SEND_ERROR; |
2137 | 0 | nwritten = -1; |
2138 | 0 | goto out; |
2139 | 0 | } |
2140 | 0 | nwritten += n; |
2141 | 0 | } |
2142 | | |
2143 | 0 | out: |
2144 | 0 | CURL_TRC_CF(data, cf, "[%d] submit -> %zd, %d", |
2145 | 0 | stream? stream->id : -1, nwritten, *err); |
2146 | 0 | Curl_safefree(nva); |
2147 | 0 | *pstream = stream; |
2148 | 0 | Curl_dynhds_free(&h2_headers); |
2149 | 0 | return nwritten; |
2150 | 0 | } |
2151 | | |
2152 | | static ssize_t cf_h2_send(struct Curl_cfilter *cf, struct Curl_easy *data, |
2153 | | const void *buf, size_t len, CURLcode *err) |
2154 | 0 | { |
2155 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
2156 | 0 | struct stream_ctx *stream = H2_STREAM_CTX(data); |
2157 | 0 | struct cf_call_data save; |
2158 | 0 | int rv; |
2159 | 0 | ssize_t nwritten; |
2160 | 0 | CURLcode result; |
2161 | 0 | int blocked = 0, was_blocked = 0; |
2162 | |
|
2163 | 0 | CF_DATA_SAVE(save, cf, data); |
2164 | | |
2165 | 0 | if(stream && stream->id != -1) { |
2166 | 0 | if(stream->upload_blocked_len) { |
2167 | | /* the data in `buf` has already been submitted or added to the |
2168 | | * buffers, but have been EAGAINed on the last invocation. */ |
2169 | | /* TODO: this assertion triggers in OSSFuzz runs and it is not |
2170 | | * clear why. Disable for now to let OSSFuzz continue its tests. */ |
2171 | 0 | DEBUGASSERT(len >= stream->upload_blocked_len); |
2172 | 0 | if(len < stream->upload_blocked_len) { |
2173 | | /* Did we get called again with a smaller `len`? This should not |
2174 | | * happen. We are not prepared to handle that. */ |
2175 | 0 | failf(data, "HTTP/2 send again with decreased length (%zd vs %zd)", |
2176 | 0 | len, stream->upload_blocked_len); |
2177 | 0 | *err = CURLE_HTTP2; |
2178 | 0 | nwritten = -1; |
2179 | 0 | goto out; |
2180 | 0 | } |
2181 | 0 | nwritten = (ssize_t)stream->upload_blocked_len; |
2182 | 0 | stream->upload_blocked_len = 0; |
2183 | 0 | was_blocked = 1; |
2184 | 0 | } |
2185 | 0 | else if(stream->closed) { |
2186 | 0 | if(stream->resp_hds_complete) { |
2187 | | /* Server decided to close the stream after having sent us a findl |
2188 | | * response. This is valid if it is not interested in the request |
2189 | | * body. This happens on 30x or 40x responses. |
2190 | | * We silently discard the data sent, since this is not a transport |
2191 | | * error situation. */ |
2192 | 0 | CURL_TRC_CF(data, cf, "[%d] discarding data" |
2193 | 0 | "on closed stream with response", stream->id); |
2194 | 0 | *err = CURLE_OK; |
2195 | 0 | nwritten = (ssize_t)len; |
2196 | 0 | goto out; |
2197 | 0 | } |
2198 | 0 | infof(data, "stream %u closed", stream->id); |
2199 | 0 | *err = CURLE_SEND_ERROR; |
2200 | 0 | nwritten = -1; |
2201 | 0 | goto out; |
2202 | 0 | } |
2203 | 0 | else { |
2204 | | /* If stream_id != -1, we have dispatched request HEADERS and |
2205 | | * optionally request body, and now are going to send or sending |
2206 | | * more request body in DATA frame */ |
2207 | 0 | nwritten = Curl_bufq_write(&stream->sendbuf, buf, len, err); |
2208 | 0 | if(nwritten < 0 && *err != CURLE_AGAIN) |
2209 | 0 | goto out; |
2210 | 0 | } |
2211 | | |
2212 | 0 | if(!Curl_bufq_is_empty(&stream->sendbuf)) { |
2213 | | /* req body data is buffered, resume the potentially suspended stream */ |
2214 | 0 | rv = nghttp2_session_resume_data(ctx->h2, stream->id); |
2215 | 0 | if(nghttp2_is_fatal(rv)) { |
2216 | 0 | *err = CURLE_SEND_ERROR; |
2217 | 0 | nwritten = -1; |
2218 | 0 | goto out; |
2219 | 0 | } |
2220 | 0 | } |
2221 | 0 | } |
2222 | 0 | else { |
2223 | 0 | nwritten = h2_submit(&stream, cf, data, buf, len, err); |
2224 | 0 | if(nwritten < 0) { |
2225 | 0 | goto out; |
2226 | 0 | } |
2227 | 0 | DEBUGASSERT(stream); |
2228 | 0 | } |
2229 | | |
2230 | | /* Call the nghttp2 send loop and flush to write ALL buffered data, |
2231 | | * headers and/or request body completely out to the network */ |
2232 | 0 | result = h2_progress_egress(cf, data); |
2233 | | /* if the stream has been closed in egress handling (nghttp2 does that |
2234 | | * when it does not like the headers, for example */ |
2235 | 0 | if(stream && stream->closed && !was_blocked) { |
2236 | 0 | infof(data, "stream %u closed", stream->id); |
2237 | 0 | *err = CURLE_SEND_ERROR; |
2238 | 0 | nwritten = -1; |
2239 | 0 | goto out; |
2240 | 0 | } |
2241 | 0 | else if(result == CURLE_AGAIN) { |
2242 | 0 | blocked = 1; |
2243 | 0 | } |
2244 | 0 | else if(result) { |
2245 | 0 | *err = result; |
2246 | 0 | nwritten = -1; |
2247 | 0 | goto out; |
2248 | 0 | } |
2249 | 0 | else if(stream && !Curl_bufq_is_empty(&stream->sendbuf)) { |
2250 | | /* although we wrote everything that nghttp2 wants to send now, |
2251 | | * there is data left in our stream send buffer unwritten. This may |
2252 | | * be due to the stream's HTTP/2 flow window being exhausted. */ |
2253 | 0 | blocked = 1; |
2254 | 0 | } |
2255 | | |
2256 | 0 | if(stream && blocked && nwritten > 0) { |
2257 | | /* Unable to send all data, due to connection blocked or H2 window |
2258 | | * exhaustion. Data is left in our stream buffer, or nghttp2's internal |
2259 | | * frame buffer or our network out buffer. */ |
2260 | 0 | size_t rwin = nghttp2_session_get_stream_remote_window_size(ctx->h2, |
2261 | 0 | stream->id); |
2262 | | /* Whatever the cause, we need to return CURL_EAGAIN for this call. |
2263 | | * We have unwritten state that needs us being invoked again and EAGAIN |
2264 | | * is the only way to ensure that. */ |
2265 | 0 | stream->upload_blocked_len = nwritten; |
2266 | 0 | CURL_TRC_CF(data, cf, "[%d] cf_send(len=%zu) BLOCK: win %u/%zu " |
2267 | 0 | "blocked_len=%zu", |
2268 | 0 | stream->id, len, |
2269 | 0 | nghttp2_session_get_remote_window_size(ctx->h2), rwin, |
2270 | 0 | nwritten); |
2271 | 0 | *err = CURLE_AGAIN; |
2272 | 0 | nwritten = -1; |
2273 | 0 | goto out; |
2274 | 0 | } |
2275 | 0 | else if(should_close_session(ctx)) { |
2276 | | /* nghttp2 thinks this session is done. If the stream has not been |
2277 | | * closed, this is an error state for out transfer */ |
2278 | 0 | if(stream->closed) { |
2279 | 0 | nwritten = http2_handle_stream_close(cf, data, stream, err); |
2280 | 0 | } |
2281 | 0 | else { |
2282 | 0 | CURL_TRC_CF(data, cf, "send: nothing to do in this session"); |
2283 | 0 | *err = CURLE_HTTP2; |
2284 | 0 | nwritten = -1; |
2285 | 0 | } |
2286 | 0 | } |
2287 | | |
2288 | 0 | out: |
2289 | 0 | if(stream) { |
2290 | 0 | CURL_TRC_CF(data, cf, "[%d] cf_send(len=%zu) -> %zd, %d, " |
2291 | 0 | "upload_left=%" CURL_FORMAT_CURL_OFF_T ", " |
2292 | 0 | "h2 windows %d-%d (stream-conn), " |
2293 | 0 | "buffers %zu-%zu (stream-conn)", |
2294 | 0 | stream->id, len, nwritten, *err, |
2295 | 0 | stream->upload_left, |
2296 | 0 | nghttp2_session_get_stream_remote_window_size( |
2297 | 0 | ctx->h2, stream->id), |
2298 | 0 | nghttp2_session_get_remote_window_size(ctx->h2), |
2299 | 0 | Curl_bufq_len(&stream->sendbuf), |
2300 | 0 | Curl_bufq_len(&ctx->outbufq)); |
2301 | 0 | } |
2302 | 0 | else { |
2303 | 0 | CURL_TRC_CF(data, cf, "cf_send(len=%zu) -> %zd, %d, " |
2304 | 0 | "connection-window=%d, nw_send_buffer(%zu)", |
2305 | 0 | len, nwritten, *err, |
2306 | 0 | nghttp2_session_get_remote_window_size(ctx->h2), |
2307 | 0 | Curl_bufq_len(&ctx->outbufq)); |
2308 | 0 | } |
2309 | 0 | CF_DATA_RESTORE(cf, save); |
2310 | 0 | return nwritten; |
2311 | 0 | } |
2312 | | |
2313 | | static void cf_h2_adjust_pollset(struct Curl_cfilter *cf, |
2314 | | struct Curl_easy *data, |
2315 | | struct easy_pollset *ps) |
2316 | 0 | { |
2317 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
2318 | 0 | bool want_recv = CURL_WANT_RECV(data); |
2319 | 0 | bool want_send = CURL_WANT_SEND(data); |
2320 | |
|
2321 | 0 | if(ctx->h2 && (want_recv || want_send)) { |
2322 | 0 | struct stream_ctx *stream = H2_STREAM_CTX(data); |
2323 | 0 | curl_socket_t sock = Curl_conn_cf_get_socket(cf, data); |
2324 | 0 | struct cf_call_data save; |
2325 | 0 | bool c_exhaust, s_exhaust; |
2326 | |
|
2327 | 0 | CF_DATA_SAVE(save, cf, data); |
2328 | 0 | c_exhaust = !nghttp2_session_get_remote_window_size(ctx->h2); |
2329 | 0 | s_exhaust = stream && stream->id >= 0 && |
2330 | 0 | !nghttp2_session_get_stream_remote_window_size(ctx->h2, |
2331 | 0 | stream->id); |
2332 | 0 | want_recv = (want_recv || c_exhaust || s_exhaust); |
2333 | 0 | want_send = (!s_exhaust && want_send) || |
2334 | 0 | (!c_exhaust && nghttp2_session_want_write(ctx->h2)); |
2335 | |
|
2336 | 0 | Curl_pollset_set(data, ps, sock, want_recv, want_send); |
2337 | 0 | CF_DATA_RESTORE(cf, save); |
2338 | 0 | } |
2339 | 0 | } |
2340 | | |
2341 | | static CURLcode cf_h2_connect(struct Curl_cfilter *cf, |
2342 | | struct Curl_easy *data, |
2343 | | bool blocking, bool *done) |
2344 | 0 | { |
2345 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
2346 | 0 | CURLcode result = CURLE_OK; |
2347 | 0 | struct cf_call_data save; |
2348 | |
|
2349 | 0 | if(cf->connected) { |
2350 | 0 | *done = TRUE; |
2351 | 0 | return CURLE_OK; |
2352 | 0 | } |
2353 | | |
2354 | | /* Connect the lower filters first */ |
2355 | 0 | if(!cf->next->connected) { |
2356 | 0 | result = Curl_conn_cf_connect(cf->next, data, blocking, done); |
2357 | 0 | if(result || !*done) |
2358 | 0 | return result; |
2359 | 0 | } |
2360 | | |
2361 | 0 | *done = FALSE; |
2362 | |
|
2363 | 0 | CF_DATA_SAVE(save, cf, data); |
2364 | 0 | if(!ctx->h2) { |
2365 | 0 | result = cf_h2_ctx_init(cf, data, FALSE); |
2366 | 0 | if(result) |
2367 | 0 | goto out; |
2368 | 0 | } |
2369 | | |
2370 | 0 | result = h2_progress_ingress(cf, data); |
2371 | 0 | if(result) |
2372 | 0 | goto out; |
2373 | | |
2374 | | /* Send out our SETTINGS and ACKs and such. If that blocks, we |
2375 | | * have it buffered and can count this filter as being connected */ |
2376 | 0 | result = h2_progress_egress(cf, data); |
2377 | 0 | if(result == CURLE_AGAIN) |
2378 | 0 | result = CURLE_OK; |
2379 | 0 | else if(result) |
2380 | 0 | goto out; |
2381 | | |
2382 | 0 | *done = TRUE; |
2383 | 0 | cf->connected = TRUE; |
2384 | 0 | result = CURLE_OK; |
2385 | |
|
2386 | 0 | out: |
2387 | 0 | CURL_TRC_CF(data, cf, "cf_connect() -> %d, %d, ", result, *done); |
2388 | 0 | CF_DATA_RESTORE(cf, save); |
2389 | 0 | return result; |
2390 | 0 | } |
2391 | | |
2392 | | static void cf_h2_close(struct Curl_cfilter *cf, struct Curl_easy *data) |
2393 | 0 | { |
2394 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
2395 | |
|
2396 | 0 | if(ctx) { |
2397 | 0 | struct cf_call_data save; |
2398 | |
|
2399 | 0 | CF_DATA_SAVE(save, cf, data); |
2400 | 0 | cf_h2_ctx_clear(ctx); |
2401 | 0 | CF_DATA_RESTORE(cf, save); |
2402 | 0 | } |
2403 | 0 | if(cf->next) |
2404 | 0 | cf->next->cft->do_close(cf->next, data); |
2405 | 0 | } |
2406 | | |
2407 | | static void cf_h2_destroy(struct Curl_cfilter *cf, struct Curl_easy *data) |
2408 | 0 | { |
2409 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
2410 | |
|
2411 | 0 | (void)data; |
2412 | 0 | if(ctx) { |
2413 | 0 | cf_h2_ctx_free(ctx); |
2414 | 0 | cf->ctx = NULL; |
2415 | 0 | } |
2416 | 0 | } |
2417 | | |
2418 | | static CURLcode http2_data_pause(struct Curl_cfilter *cf, |
2419 | | struct Curl_easy *data, |
2420 | | bool pause) |
2421 | 0 | { |
2422 | 0 | #ifdef NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE |
2423 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
2424 | 0 | struct stream_ctx *stream = H2_STREAM_CTX(data); |
2425 | |
|
2426 | 0 | DEBUGASSERT(data); |
2427 | 0 | if(ctx && ctx->h2 && stream) { |
2428 | 0 | uint32_t window = pause? 0 : stream->local_window_size; |
2429 | |
|
2430 | 0 | int rv = nghttp2_session_set_local_window_size(ctx->h2, |
2431 | 0 | NGHTTP2_FLAG_NONE, |
2432 | 0 | stream->id, |
2433 | 0 | window); |
2434 | 0 | if(rv) { |
2435 | 0 | failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)", |
2436 | 0 | nghttp2_strerror(rv), rv); |
2437 | 0 | return CURLE_HTTP2; |
2438 | 0 | } |
2439 | | |
2440 | 0 | if(!pause) |
2441 | 0 | drain_stream(cf, data, stream); |
2442 | | |
2443 | | /* attempt to send the window update */ |
2444 | 0 | (void)h2_progress_egress(cf, data); |
2445 | |
|
2446 | 0 | if(!pause) { |
2447 | | /* Unpausing a h2 transfer, requires it to be run again. The server |
2448 | | * may send new DATA on us increasing the flow window, and it may |
2449 | | * not. We may have already buffered and exhausted the new window |
2450 | | * by operating on things in flight during the handling of other |
2451 | | * transfers. */ |
2452 | 0 | drain_stream(cf, data, stream); |
2453 | 0 | Curl_expire(data, 0, EXPIRE_RUN_NOW); |
2454 | 0 | } |
2455 | 0 | DEBUGF(infof(data, "Set HTTP/2 window size to %u for stream %u", |
2456 | 0 | window, stream->id)); |
2457 | |
|
2458 | 0 | #ifdef DEBUGBUILD |
2459 | 0 | { |
2460 | | /* read out the stream local window again */ |
2461 | 0 | uint32_t window2 = |
2462 | 0 | nghttp2_session_get_stream_local_window_size(ctx->h2, |
2463 | 0 | stream->id); |
2464 | 0 | DEBUGF(infof(data, "HTTP/2 window size is now %u for stream %u", |
2465 | 0 | window2, stream->id)); |
2466 | 0 | } |
2467 | 0 | #endif |
2468 | 0 | } |
2469 | 0 | #endif |
2470 | 0 | return CURLE_OK; |
2471 | 0 | } |
2472 | | |
2473 | | static CURLcode cf_h2_cntrl(struct Curl_cfilter *cf, |
2474 | | struct Curl_easy *data, |
2475 | | int event, int arg1, void *arg2) |
2476 | 0 | { |
2477 | 0 | CURLcode result = CURLE_OK; |
2478 | 0 | struct cf_call_data save; |
2479 | |
|
2480 | 0 | (void)arg2; |
2481 | |
|
2482 | 0 | CF_DATA_SAVE(save, cf, data); |
2483 | 0 | switch(event) { |
2484 | 0 | case CF_CTRL_DATA_SETUP: |
2485 | 0 | break; |
2486 | 0 | case CF_CTRL_DATA_PAUSE: |
2487 | 0 | result = http2_data_pause(cf, data, (arg1 != 0)); |
2488 | 0 | break; |
2489 | 0 | case CF_CTRL_DATA_DONE_SEND: |
2490 | 0 | result = http2_data_done_send(cf, data); |
2491 | 0 | break; |
2492 | 0 | case CF_CTRL_DATA_DETACH: |
2493 | 0 | http2_data_done(cf, data, TRUE); |
2494 | 0 | break; |
2495 | 0 | case CF_CTRL_DATA_DONE: |
2496 | 0 | http2_data_done(cf, data, arg1 != 0); |
2497 | 0 | break; |
2498 | 0 | default: |
2499 | 0 | break; |
2500 | 0 | } |
2501 | 0 | CF_DATA_RESTORE(cf, save); |
2502 | 0 | return result; |
2503 | 0 | } |
2504 | | |
2505 | | static bool cf_h2_data_pending(struct Curl_cfilter *cf, |
2506 | | const struct Curl_easy *data) |
2507 | 0 | { |
2508 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
2509 | 0 | struct stream_ctx *stream = H2_STREAM_CTX(data); |
2510 | |
|
2511 | 0 | if(ctx && (!Curl_bufq_is_empty(&ctx->inbufq) |
2512 | 0 | || (stream && !Curl_bufq_is_empty(&stream->sendbuf)) |
2513 | 0 | || (stream && !Curl_bufq_is_empty(&stream->recvbuf)))) |
2514 | 0 | return TRUE; |
2515 | 0 | return cf->next? cf->next->cft->has_data_pending(cf->next, data) : FALSE; |
2516 | 0 | } |
2517 | | |
2518 | | static bool cf_h2_is_alive(struct Curl_cfilter *cf, |
2519 | | struct Curl_easy *data, |
2520 | | bool *input_pending) |
2521 | 0 | { |
2522 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
2523 | 0 | CURLcode result; |
2524 | 0 | struct cf_call_data save; |
2525 | |
|
2526 | 0 | CF_DATA_SAVE(save, cf, data); |
2527 | 0 | result = (ctx && ctx->h2 && http2_connisalive(cf, data, input_pending)); |
2528 | 0 | CURL_TRC_CF(data, cf, "conn alive -> %d, input_pending=%d", |
2529 | 0 | result, *input_pending); |
2530 | 0 | CF_DATA_RESTORE(cf, save); |
2531 | 0 | return result; |
2532 | 0 | } |
2533 | | |
2534 | | static CURLcode cf_h2_keep_alive(struct Curl_cfilter *cf, |
2535 | | struct Curl_easy *data) |
2536 | 0 | { |
2537 | 0 | CURLcode result; |
2538 | 0 | struct cf_call_data save; |
2539 | |
|
2540 | 0 | CF_DATA_SAVE(save, cf, data); |
2541 | 0 | result = http2_send_ping(cf, data); |
2542 | 0 | CF_DATA_RESTORE(cf, save); |
2543 | 0 | return result; |
2544 | 0 | } |
2545 | | |
2546 | | static CURLcode cf_h2_query(struct Curl_cfilter *cf, |
2547 | | struct Curl_easy *data, |
2548 | | int query, int *pres1, void *pres2) |
2549 | 0 | { |
2550 | 0 | struct cf_h2_ctx *ctx = cf->ctx; |
2551 | 0 | struct cf_call_data save; |
2552 | 0 | size_t effective_max; |
2553 | |
|
2554 | 0 | switch(query) { |
2555 | 0 | case CF_QUERY_MAX_CONCURRENT: |
2556 | 0 | DEBUGASSERT(pres1); |
2557 | | |
2558 | 0 | CF_DATA_SAVE(save, cf, data); |
2559 | 0 | if(nghttp2_session_check_request_allowed(ctx->h2) == 0) { |
2560 | | /* the limit is what we have in use right now */ |
2561 | 0 | effective_max = CONN_INUSE(cf->conn); |
2562 | 0 | } |
2563 | 0 | else { |
2564 | 0 | effective_max = ctx->max_concurrent_streams; |
2565 | 0 | } |
2566 | 0 | *pres1 = (effective_max > INT_MAX)? INT_MAX : (int)effective_max; |
2567 | 0 | CF_DATA_RESTORE(cf, save); |
2568 | 0 | return CURLE_OK; |
2569 | 0 | default: |
2570 | 0 | break; |
2571 | 0 | } |
2572 | 0 | return cf->next? |
2573 | 0 | cf->next->cft->query(cf->next, data, query, pres1, pres2) : |
2574 | 0 | CURLE_UNKNOWN_OPTION; |
2575 | 0 | } |
2576 | | |
2577 | | struct Curl_cftype Curl_cft_nghttp2 = { |
2578 | | "HTTP/2", |
2579 | | CF_TYPE_MULTIPLEX, |
2580 | | CURL_LOG_LVL_NONE, |
2581 | | cf_h2_destroy, |
2582 | | cf_h2_connect, |
2583 | | cf_h2_close, |
2584 | | Curl_cf_def_get_host, |
2585 | | cf_h2_adjust_pollset, |
2586 | | cf_h2_data_pending, |
2587 | | cf_h2_send, |
2588 | | cf_h2_recv, |
2589 | | cf_h2_cntrl, |
2590 | | cf_h2_is_alive, |
2591 | | cf_h2_keep_alive, |
2592 | | cf_h2_query, |
2593 | | }; |
2594 | | |
2595 | | static CURLcode http2_cfilter_add(struct Curl_cfilter **pcf, |
2596 | | struct Curl_easy *data, |
2597 | | struct connectdata *conn, |
2598 | | int sockindex) |
2599 | 0 | { |
2600 | 0 | struct Curl_cfilter *cf = NULL; |
2601 | 0 | struct cf_h2_ctx *ctx; |
2602 | 0 | CURLcode result = CURLE_OUT_OF_MEMORY; |
2603 | |
|
2604 | 0 | DEBUGASSERT(data->conn); |
2605 | 0 | ctx = calloc(1, sizeof(*ctx)); |
2606 | 0 | if(!ctx) |
2607 | 0 | goto out; |
2608 | | |
2609 | 0 | result = Curl_cf_create(&cf, &Curl_cft_nghttp2, ctx); |
2610 | 0 | if(result) |
2611 | 0 | goto out; |
2612 | | |
2613 | 0 | Curl_conn_cf_add(data, conn, sockindex, cf); |
2614 | 0 | result = CURLE_OK; |
2615 | |
|
2616 | 0 | out: |
2617 | 0 | if(result) |
2618 | 0 | cf_h2_ctx_free(ctx); |
2619 | 0 | *pcf = result? NULL : cf; |
2620 | 0 | return result; |
2621 | 0 | } |
2622 | | |
2623 | | static CURLcode http2_cfilter_insert_after(struct Curl_cfilter *cf, |
2624 | | struct Curl_easy *data) |
2625 | 0 | { |
2626 | 0 | struct Curl_cfilter *cf_h2 = NULL; |
2627 | 0 | struct cf_h2_ctx *ctx; |
2628 | 0 | CURLcode result = CURLE_OUT_OF_MEMORY; |
2629 | |
|
2630 | 0 | (void)data; |
2631 | 0 | ctx = calloc(1, sizeof(*ctx)); |
2632 | 0 | if(!ctx) |
2633 | 0 | goto out; |
2634 | | |
2635 | 0 | result = Curl_cf_create(&cf_h2, &Curl_cft_nghttp2, ctx); |
2636 | 0 | if(result) |
2637 | 0 | goto out; |
2638 | | |
2639 | 0 | Curl_conn_cf_insert_after(cf, cf_h2); |
2640 | 0 | result = CURLE_OK; |
2641 | |
|
2642 | 0 | out: |
2643 | 0 | if(result) |
2644 | 0 | cf_h2_ctx_free(ctx); |
2645 | 0 | return result; |
2646 | 0 | } |
2647 | | |
2648 | | static bool Curl_cf_is_http2(struct Curl_cfilter *cf, |
2649 | | const struct Curl_easy *data) |
2650 | 0 | { |
2651 | 0 | (void)data; |
2652 | 0 | for(; cf; cf = cf->next) { |
2653 | 0 | if(cf->cft == &Curl_cft_nghttp2) |
2654 | 0 | return TRUE; |
2655 | 0 | if(cf->cft->flags & CF_TYPE_IP_CONNECT) |
2656 | 0 | return FALSE; |
2657 | 0 | } |
2658 | 0 | return FALSE; |
2659 | 0 | } |
2660 | | |
2661 | | bool Curl_conn_is_http2(const struct Curl_easy *data, |
2662 | | const struct connectdata *conn, |
2663 | | int sockindex) |
2664 | 0 | { |
2665 | 0 | return conn? Curl_cf_is_http2(conn->cfilter[sockindex], data) : FALSE; |
2666 | 0 | } |
2667 | | |
2668 | | bool Curl_http2_may_switch(struct Curl_easy *data, |
2669 | | struct connectdata *conn, |
2670 | | int sockindex) |
2671 | 0 | { |
2672 | 0 | (void)sockindex; |
2673 | 0 | if(!Curl_conn_is_http2(data, conn, sockindex) && |
2674 | 0 | data->state.httpwant == CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE) { |
2675 | 0 | #ifndef CURL_DISABLE_PROXY |
2676 | 0 | if(conn->bits.httpproxy && !conn->bits.tunnel_proxy) { |
2677 | | /* We don't support HTTP/2 proxies yet. Also it's debatable |
2678 | | whether or not this setting should apply to HTTP/2 proxies. */ |
2679 | 0 | infof(data, "Ignoring HTTP/2 prior knowledge due to proxy"); |
2680 | 0 | return FALSE; |
2681 | 0 | } |
2682 | 0 | #endif |
2683 | 0 | return TRUE; |
2684 | 0 | } |
2685 | 0 | return FALSE; |
2686 | 0 | } |
2687 | | |
2688 | | CURLcode Curl_http2_switch(struct Curl_easy *data, |
2689 | | struct connectdata *conn, int sockindex) |
2690 | 0 | { |
2691 | 0 | struct Curl_cfilter *cf; |
2692 | 0 | CURLcode result; |
2693 | |
|
2694 | 0 | DEBUGASSERT(!Curl_conn_is_http2(data, conn, sockindex)); |
2695 | 0 | DEBUGF(infof(data, "switching to HTTP/2")); |
2696 | |
|
2697 | 0 | result = http2_cfilter_add(&cf, data, conn, sockindex); |
2698 | 0 | if(result) |
2699 | 0 | return result; |
2700 | | |
2701 | 0 | result = cf_h2_ctx_init(cf, data, FALSE); |
2702 | 0 | if(result) |
2703 | 0 | return result; |
2704 | | |
2705 | 0 | conn->httpversion = 20; /* we know we're on HTTP/2 now */ |
2706 | 0 | conn->bits.multiplex = TRUE; /* at least potentially multiplexed */ |
2707 | 0 | conn->bundle->multiuse = BUNDLE_MULTIPLEX; |
2708 | 0 | Curl_multi_connchanged(data->multi); |
2709 | |
|
2710 | 0 | if(cf->next) { |
2711 | 0 | bool done; |
2712 | 0 | return Curl_conn_cf_connect(cf, data, FALSE, &done); |
2713 | 0 | } |
2714 | 0 | return CURLE_OK; |
2715 | 0 | } |
2716 | | |
2717 | | CURLcode Curl_http2_switch_at(struct Curl_cfilter *cf, struct Curl_easy *data) |
2718 | 0 | { |
2719 | 0 | struct Curl_cfilter *cf_h2; |
2720 | 0 | CURLcode result; |
2721 | |
|
2722 | 0 | DEBUGASSERT(!Curl_cf_is_http2(cf, data)); |
2723 | | |
2724 | 0 | result = http2_cfilter_insert_after(cf, data); |
2725 | 0 | if(result) |
2726 | 0 | return result; |
2727 | | |
2728 | 0 | cf_h2 = cf->next; |
2729 | 0 | result = cf_h2_ctx_init(cf_h2, data, FALSE); |
2730 | 0 | if(result) |
2731 | 0 | return result; |
2732 | | |
2733 | 0 | cf->conn->httpversion = 20; /* we know we're on HTTP/2 now */ |
2734 | 0 | cf->conn->bits.multiplex = TRUE; /* at least potentially multiplexed */ |
2735 | 0 | cf->conn->bundle->multiuse = BUNDLE_MULTIPLEX; |
2736 | 0 | Curl_multi_connchanged(data->multi); |
2737 | |
|
2738 | 0 | if(cf_h2->next) { |
2739 | 0 | bool done; |
2740 | 0 | return Curl_conn_cf_connect(cf_h2, data, FALSE, &done); |
2741 | 0 | } |
2742 | 0 | return CURLE_OK; |
2743 | 0 | } |
2744 | | |
2745 | | CURLcode Curl_http2_upgrade(struct Curl_easy *data, |
2746 | | struct connectdata *conn, int sockindex, |
2747 | | const char *mem, size_t nread) |
2748 | 0 | { |
2749 | 0 | struct Curl_cfilter *cf; |
2750 | 0 | struct cf_h2_ctx *ctx; |
2751 | 0 | CURLcode result; |
2752 | |
|
2753 | 0 | DEBUGASSERT(!Curl_conn_is_http2(data, conn, sockindex)); |
2754 | 0 | DEBUGF(infof(data, "upgrading to HTTP/2")); |
2755 | 0 | DEBUGASSERT(data->req.upgr101 == UPGR101_RECEIVED); |
2756 | | |
2757 | 0 | result = http2_cfilter_add(&cf, data, conn, sockindex); |
2758 | 0 | if(result) |
2759 | 0 | return result; |
2760 | | |
2761 | 0 | DEBUGASSERT(cf->cft == &Curl_cft_nghttp2); |
2762 | 0 | ctx = cf->ctx; |
2763 | |
|
2764 | 0 | result = cf_h2_ctx_init(cf, data, TRUE); |
2765 | 0 | if(result) |
2766 | 0 | return result; |
2767 | | |
2768 | 0 | if(nread > 0) { |
2769 | | /* Remaining data from the protocol switch reply is already using |
2770 | | * the switched protocol, ie. HTTP/2. We add that to the network |
2771 | | * inbufq. */ |
2772 | 0 | ssize_t copied; |
2773 | |
|
2774 | 0 | copied = Curl_bufq_write(&ctx->inbufq, |
2775 | 0 | (const unsigned char *)mem, nread, &result); |
2776 | 0 | if(copied < 0) { |
2777 | 0 | failf(data, "error on copying HTTP Upgrade response: %d", result); |
2778 | 0 | return CURLE_RECV_ERROR; |
2779 | 0 | } |
2780 | 0 | if((size_t)copied < nread) { |
2781 | 0 | failf(data, "connection buffer size could not take all data " |
2782 | 0 | "from HTTP Upgrade response header: copied=%zd, datalen=%zu", |
2783 | 0 | copied, nread); |
2784 | 0 | return CURLE_HTTP2; |
2785 | 0 | } |
2786 | 0 | infof(data, "Copied HTTP/2 data in stream buffer to connection buffer" |
2787 | 0 | " after upgrade: len=%zu", nread); |
2788 | 0 | } |
2789 | | |
2790 | 0 | conn->httpversion = 20; /* we know we're on HTTP/2 now */ |
2791 | 0 | conn->bits.multiplex = TRUE; /* at least potentially multiplexed */ |
2792 | 0 | conn->bundle->multiuse = BUNDLE_MULTIPLEX; |
2793 | 0 | Curl_multi_connchanged(data->multi); |
2794 | |
|
2795 | 0 | if(cf->next) { |
2796 | 0 | bool done; |
2797 | 0 | return Curl_conn_cf_connect(cf, data, FALSE, &done); |
2798 | 0 | } |
2799 | 0 | return CURLE_OK; |
2800 | 0 | } |
2801 | | |
2802 | | /* Only call this function for a transfer that already got an HTTP/2 |
2803 | | CURLE_HTTP2_STREAM error! */ |
2804 | | bool Curl_h2_http_1_1_error(struct Curl_easy *data) |
2805 | 0 | { |
2806 | 0 | struct stream_ctx *stream = H2_STREAM_CTX(data); |
2807 | 0 | return (stream && stream->error == NGHTTP2_HTTP_1_1_REQUIRED); |
2808 | 0 | } |
2809 | | |
2810 | | #else /* !USE_NGHTTP2 */ |
2811 | | |
2812 | | /* Satisfy external references even if http2 is not compiled in. */ |
2813 | | #include <curl/curl.h> |
2814 | | |
2815 | | char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num) |
2816 | | { |
2817 | | (void) h; |
2818 | | (void) num; |
2819 | | return NULL; |
2820 | | } |
2821 | | |
2822 | | char *curl_pushheader_byname(struct curl_pushheaders *h, const char *header) |
2823 | | { |
2824 | | (void) h; |
2825 | | (void) header; |
2826 | | return NULL; |
2827 | | } |
2828 | | |
2829 | | #endif /* USE_NGHTTP2 */ |