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