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