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