Artifactspublic-v0

curl

master
ply4 git calls608 ms301 KiB2 cachedprotocol v2
1714 lines52 KiBRaw
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_QUICHE)
27#include <quiche.h>
28#include <openssl/err.h>
29#include <openssl/ssl.h>
31#include "bufq.h"
32#include "u32_ptrset.h"
33#include "urldata.h"
34#include "cfilters.h"
35#include "cf-socket.h"
36#include "curl_trc.h"
37#include "rand.h"
38#include "multiif.h"
39#include "connect.h"
40#include "progress.h"
41#include "select.h"
42#include "http.h"
43#include "http1.h"
44#include "sockaddr.h"
45#include "vdns/cf-dns.h"
46#include "vquic/vquic.h"
47#include "vquic/vquic_int.h"
48#include "vquic/vquic-tls.h"
49#include "vquic/cf-quiche.h"
50#include "transfer.h"
51#include "url.h"
52#include "bufref.h"
53#include "vtls/openssl.h"
54#include "vtls/vtls.h"
56/* HTTP/3 error values defined in RFC 9114, ch. 8.1 */
57#define CURL_H3_NO_ERROR 0x0100
59#define MAX_PKT_BURST 10
61#define QUIC_MAX_STREAMS 100
63#define H3_STREAM_WINDOW_SIZE (1024 * 128)
64#define H3_STREAM_CHUNK_SIZE (1024 * 16)
65/* Receive and Send max number of chunks follows from the
66 * chunk size and window size */
67#define H3_STREAM_RECV_CHUNKS \
68 (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE)
70/*
71 * Store quiche version info in this buffer.
72 */
73void Curl_quiche_ver(char *p, size_t len)
75 (void)curl_msnprintf(p, len, "quiche/%s", quiche_version());
78struct cf_quiche_ctx {
79 struct cf_quic_ctx q;
80 struct ssl_peer ssl_peer;
81 struct curl_tls_ctx tls;
82 quiche_conn *qconn;
83 quiche_config *cfg;
84 quiche_h3_conn *h3c;
85 quiche_h3_config *h3config;
86 uint8_t scid[QUICHE_MAX_CONN_ID_LEN];
87 struct curltime started_at; /* time the current attempt started */
88 struct curltime handshake_at; /* time connect handshake finished */
89 struct u32_ptrset streams; /* set of `data->mid` + `stream_ctx` */
90 struct dynbuf h1hdr; /* temp buffer for header construction */
91 struct bufq writebuf; /* temp buffer for writing bodies */
92 curl_off_t data_recvd;
93 BIT(initialized);
94 BIT(goaway); /* got GOAWAY from server */
95 BIT(x509_store_setup); /* if x509 store has been set up */
96 BIT(shutdown_started); /* queued shutdown packets */
97 BIT(stats_reported); /* connect statistics reported */
98};
100#ifdef DEBUG_QUICHE
101/* initialize debug log callback only once */
102static int debug_log_init = 0;
103static void quiche_debug_log(const char *line, void *argp)
105 (void)argp;
106 curl_mfprintf(stderr, "%s\n", line);
108#endif
110static void h3_stream_set_free(uint32_t id, void *stream);
112static CURLcode cf_quiche_ctx_init(struct cf_quiche_ctx *ctx,
113 struct Curl_peer *origin,
114 struct Curl_peer *peer,
115 struct ssl_filter_config *sslc)
117 DEBUGASSERT(!ctx->initialized);
118#ifdef DEBUG_QUICHE
119 if(!debug_log_init) {
120 quiche_enable_debug_logging(quiche_debug_log, NULL);
121 debug_log_init = 1;
122 }
123#endif
124 curlx_dyn_init(&ctx->h1hdr, CURL_MAX_HTTP_HEADER);
125 Curl_u32_ptrset_init(&ctx->streams, h3_stream_set_free);
126 Curl_bufq_init2(&ctx->writebuf, H3_STREAM_CHUNK_SIZE, H3_STREAM_RECV_CHUNKS,
127 BUFQ_OPT_SOFT_LIMIT);
128 ctx->data_recvd = 0;
129 ctx->initialized = TRUE;
130 return Curl_vquic_tls_peer_init(origin, peer, sslc, &ctx->ssl_peer);
133static void cf_quiche_ctx_free(struct cf_quiche_ctx *ctx)
135 if(ctx && ctx->initialized) {
136 /* quiche freed it */
137 ctx->tls.ossl.ssl = NULL;
138 Curl_vquic_tls_cleanup(&ctx->tls);
139 Curl_ssl_peer_cleanup(&ctx->ssl_peer);
140 Curl_vquic_ctx_free(&ctx->q);
141 Curl_u32_ptrset_clear(&ctx->streams);
142 curlx_dyn_free(&ctx->h1hdr);
143 Curl_bufq_free(&ctx->writebuf);
144 }
145 curlx_free(ctx);
148static void cf_quiche_ctx_close(struct cf_quiche_ctx *ctx)
150 if(ctx->h3c) {
151 quiche_h3_conn_free(ctx->h3c);
152 ctx->h3c = NULL;
153 }
154 if(ctx->h3config) {
155 quiche_h3_config_free(ctx->h3config);
156 ctx->h3config = NULL;
157 }
158 if(ctx->qconn) {
159 quiche_conn_free(ctx->qconn);
160 ctx->qconn = NULL;
161 }
162 if(ctx->cfg) {
163 quiche_config_free(ctx->cfg);
164 ctx->cfg = NULL;
165 }
166 Curl_ssl_peer_cleanup(&ctx->ssl_peer);
169static CURLcode cf_flush_egress(struct Curl_cfilter *cf,
170 struct Curl_easy *data);
172/**
173 * All about the H3 internals of a stream
174 */
175struct h3_stream_ctx {
176 uint64_t id; /* HTTP/3 protocol stream identifier */
177 struct h1_req_parser h1; /* h1 request parsing */
178 uint64_t error3; /* HTTP/3 stream error code */
179 int32_t status_code; /* HTTP status code */
180 CURLcode xfer_result; /* result from cf_quiche_write_(hd/body) */
181 BIT(opened); /* TRUE after stream has been opened */
182 BIT(closed); /* TRUE on stream close */
183 BIT(reset); /* TRUE on stream reset */
184 BIT(send_closed); /* stream is locally closed */
185 BIT(resp_hds_complete); /* final response has been received */
186 BIT(resp_got_header); /* TRUE when h3 stream has recvd some HEADER */
187 BIT(quic_flow_blocked); /* stream is blocked by QUIC flow control */
188};
190static void h3_stream_ctx_free(struct h3_stream_ctx *stream)
192 Curl_h1_req_parse_free(&stream->h1);
193 curlx_free(stream);
196static void h3_stream_set_free(uint32_t id, void *stream)
198 (void)id;
199 DEBUGASSERT(stream);
200 h3_stream_ctx_free((struct h3_stream_ctx *)stream);
203typedef bool cf_quiche_svisit(struct Curl_cfilter *cf,
204 struct Curl_easy *sdata,
205 struct h3_stream_ctx *stream,
206 void *user_data);
208struct cf_quiche_visit_ctx {
209 struct Curl_cfilter *cf;
210 struct Curl_multi *multi;
211 cf_quiche_svisit *cb;
212 void *user_data;
213};
215static bool cf_quiche_stream_do(uint32_t mid, void *val, void *user_data)
217 struct cf_quiche_visit_ctx *vctx = user_data;
218 struct h3_stream_ctx *stream = val;
219 struct Curl_easy *sdata = Curl_multi_get_easy(vctx->multi, mid);
220 if(sdata)
221 return vctx->cb(vctx->cf, sdata, stream, vctx->user_data);
222 return TRUE;
225static void cf_quiche_for_all_streams(struct Curl_cfilter *cf,
226 struct Curl_multi *multi,
227 cf_quiche_svisit *do_cb,
228 void *user_data)
230 struct cf_quiche_ctx *ctx = cf->ctx;
231 struct cf_quiche_visit_ctx vctx;
232 vctx.cf = cf;
233 vctx.multi = multi;
234 vctx.cb = do_cb;
235 vctx.user_data = user_data;
236 Curl_u32_ptrset_visit(&ctx->streams, cf_quiche_stream_do, &vctx);
239static bool cf_quiche_do_resume(struct Curl_cfilter *cf,
240 struct Curl_easy *sdata,
241 struct h3_stream_ctx *stream,
242 void *user_data)
244 (void)user_data;
245 if(stream->quic_flow_blocked) {
246 stream->quic_flow_blocked = FALSE;
247 Curl_multi_mark_dirty(sdata);
248 CURL_TRC_CF(sdata, cf, "[%" PRIu64 "] unblock", stream->id);
249 }
250 return TRUE;
253static bool cf_quiche_do_expire(struct Curl_cfilter *cf,
254 struct Curl_easy *sdata,
255 struct h3_stream_ctx *stream,
256 void *user_data)
258 (void)user_data;
259 CURL_TRC_CF(sdata, cf, "conn closed, mark as dirty");
260 stream->xfer_result = CURLE_SEND_ERROR;
261 Curl_multi_mark_dirty(sdata);
262 return TRUE;
265static CURLcode h3_data_setup(struct Curl_cfilter *cf,
266 struct Curl_easy *data)
268 struct cf_quiche_ctx *ctx = cf->ctx;
269 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
271 if(stream)
272 return CURLE_OK;
274 stream = curlx_calloc(1, sizeof(*stream));
275 if(!stream)
276 return CURLE_OUT_OF_MEMORY;
278 stream->id = -1;
279 Curl_h1_req_parse_init(&stream->h1, H1_PARSE_DEFAULT_MAX_LINE_LEN);
281 if(Curl_u32_ptrset_set(&ctx->streams, data->mid, stream)) {
282 h3_stream_ctx_free(stream);
283 return CURLE_OUT_OF_MEMORY;
284 }
286 return CURLE_OK;
289static void cf_quiche_stream_close(struct Curl_cfilter *cf,
290 struct Curl_easy *data,
291 struct h3_stream_ctx *stream)
293 struct cf_quiche_ctx *ctx = cf->ctx;
294 CURLcode result;
296 if(ctx->qconn && !stream->closed) {
297 quiche_conn_stream_shutdown(ctx->qconn, stream->id,
298 QUICHE_SHUTDOWN_READ, CURL_H3_NO_ERROR);
299 if(!stream->send_closed) {
300 quiche_conn_stream_shutdown(ctx->qconn, stream->id,
301 QUICHE_SHUTDOWN_WRITE, CURL_H3_NO_ERROR);
302 stream->send_closed = TRUE;
303 }
304 stream->closed = TRUE;
305 result = cf_flush_egress(cf, data);
306 if(result)
307 CURL_TRC_CF(data, cf, "[%" PRIu64 "] stream close, flush egress -> %d",
308 stream->id, (int)result);
309 }
312static void h3_data_done(struct Curl_cfilter *cf, struct Curl_easy *data)
314 struct cf_quiche_ctx *ctx = cf->ctx;
315 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
317 (void)cf;
318 if(stream) {
319 CURL_TRC_CF(data, cf, "[%" PRIu64 "] easy handle is done", stream->id);
320 cf_quiche_stream_close(cf, data, stream);
321 Curl_u32_ptrset_unset(&ctx->streams, data->mid);
322 }
325static void cf_quiche_expire_conn_closed(struct Curl_cfilter *cf,
326 struct Curl_easy *data)
328 DEBUGASSERT(data->multi);
329 CURL_TRC_CF(data, cf, "conn closed, expire all transfers");
330 cf_quiche_for_all_streams(cf, data->multi, cf_quiche_do_expire, NULL);
333static void cf_quiche_write_hd(struct Curl_cfilter *cf,
334 struct Curl_easy *data,
335 struct h3_stream_ctx *stream,
336 const char *buf, size_t blen, bool eos)
338 /* This function returns no error intentionally, but records
339 * the result at the stream, skipping further writes once the
340 * `result` of the transfer is known.
341 * The stream is subsequently cancelled "higher up" in the filter's
342 * send/recv callbacks. Closing the stream here leads to SEND/RECV
343 * errors in other places that then overwrite the transfer's result. */
344 if(!stream->xfer_result) {
345 stream->xfer_result = Curl_xfer_write_resp_hd(data, buf, blen, eos);
346 if(stream->xfer_result)
347 CURL_TRC_CF(data, cf, "[%" PRIu64 "] error %d writing %zu "
348 "bytes of headers", stream->id, (int)stream->xfer_result,
349 blen);
350 }
353struct cb_ctx {
354 struct Curl_cfilter *cf;
355 struct Curl_easy *data;
356 struct h3_stream_ctx *stream;
357};
359static bool is_valid_h3_header(const uint8_t *hdr, size_t hlen)
361 while(hlen--) {
362 switch(*hdr++) {
363 case '\n':
364 case '\r':
365 case '\0':
366 return FALSE;
367 }
368 }
369 return TRUE;
372static int cb_each_header(uint8_t *name, size_t name_len,
373 uint8_t *value, size_t value_len,
374 void *argp)
376 struct cb_ctx *x = argp;
377 struct Curl_cfilter *cf = x->cf;
378 struct Curl_easy *data = x->data;
379 struct h3_stream_ctx *stream = x->stream;
380 struct cf_quiche_ctx *ctx = cf->ctx;
381 CURLcode result = CURLE_OK;
383 if(!stream || stream->xfer_result)
384 return 1; /* abort iteration */
386 if((name_len == 7) && !strncmp(HTTP_PSEUDO_STATUS, (char *)name, 7) &&
387 is_valid_h3_header(value, value_len)) {
388 curlx_dyn_reset(&ctx->h1hdr);
389 result = Curl_http_decode_status(&stream->status_code,
390 (const char *)value, value_len);
391 if(!result)
392 result = curlx_dyn_addn(&ctx->h1hdr, STRCONST("HTTP/3 "));
393 if(!result)
394 result = curlx_dyn_addn(&ctx->h1hdr, (const char *)value, value_len);
395 if(!result)
396 result = curlx_dyn_addn(&ctx->h1hdr, STRCONST(" \r\n"));
397 if(!result)
398 cf_quiche_write_hd(cf, data, stream, curlx_dyn_ptr(&ctx->h1hdr),
399 curlx_dyn_len(&ctx->h1hdr), FALSE);
400 CURL_TRC_CF(data, cf, "[%" PRIu64 "] status: %s",
401 stream->id, curlx_dyn_ptr(&ctx->h1hdr));
402 }
403 else {
404 if(is_valid_h3_header(value, value_len) &&
405 is_valid_h3_header(name, name_len)) {
406 /* store as an HTTP1-style header */
407 CURL_TRC_CF(data, cf, "[%" PRIu64 "] header: %.*s: %.*s",
408 stream->id, (int)name_len, name,
409 (int)value_len, value);
410 curlx_dyn_reset(&ctx->h1hdr);
411 result = curlx_dyn_addn(&ctx->h1hdr, (const char *)name, name_len);
412 if(!result)
413 result = curlx_dyn_addn(&ctx->h1hdr, STRCONST(": "));
414 if(!result)
415 result = curlx_dyn_addn(&ctx->h1hdr, (const char *)value, value_len);
416 if(!result)
417 result = curlx_dyn_addn(&ctx->h1hdr, STRCONST("\r\n"));
418 if(!result)
419 cf_quiche_write_hd(cf, data, stream, curlx_dyn_ptr(&ctx->h1hdr),
420 curlx_dyn_len(&ctx->h1hdr), FALSE);
421 }
422 else
423 CURL_TRC_CF(x->data, x->cf, "[%" PRIu64 "] ignore %zu bytes bad header",
424 stream->id, value_len + name_len);
425 }
427 if(result) {
428 CURL_TRC_CF(x->data, x->cf, "[%" PRIu64 "] on header error %d",
429 stream->id, (int)result);
430 if(!stream->xfer_result)
431 stream->xfer_result = result;
432 }
433 return stream->xfer_result ? 1 : 0;
436static CURLcode stream_resp_read(void *reader_ctx,
437 unsigned char *buf, size_t len,
438 size_t *pnread)
440 struct cb_ctx *x = reader_ctx;
441 struct cf_quiche_ctx *ctx = x->cf->ctx;
442 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, x->data);
443 ssize_t nread;
445 *pnread = 0;
446 if(!stream)
447 return CURLE_RECV_ERROR;
449 nread = quiche_h3_recv_body(ctx->h3c, ctx->qconn, stream->id, buf, len);
450 if(!curlx_sztouz(nread, pnread))
451 return CURLE_AGAIN;
452 return CURLE_OK;
455static void cf_quiche_flush_body(struct Curl_cfilter *cf,
456 struct Curl_easy *data,
457 struct h3_stream_ctx *stream)
459 struct cf_quiche_ctx *ctx = cf->ctx;
460 const uint8_t *buf;
461 size_t blen;
463 while(stream && !stream->xfer_result) {
464 if(Curl_bufq_peek(&ctx->writebuf, &buf, &blen)) {
465 stream->xfer_result = Curl_xfer_write_resp(
466 data, (const char *)buf, blen, FALSE);
467 Curl_bufq_skip(&ctx->writebuf, blen);
468 if(stream->xfer_result) {
469 CURL_TRC_CF(data, cf, "[%" PRIu64 "] error %d writing %zu bytes"
470 " of data", stream->id, (int)stream->xfer_result, blen);
471 }
472 }
473 else
474 break;
475 }
476 Curl_bufq_reset(&ctx->writebuf);
479static void cf_quiche_recv_body(struct Curl_cfilter *cf,
480 struct Curl_easy *data,
481 struct h3_stream_ctx *stream)
483 struct cf_quiche_ctx *ctx = cf->ctx;
484 size_t nread;
485 struct cb_ctx cb_ctx;
486 CURLcode result = CURLE_OK;
488 if(!stream)
489 return;
491 /* Even when the transfer has already errored, we need to receive
492 * the data from quiche, as quiche otherwise gets stuck and
493 * raise events to receive over and over again. */
494 cb_ctx.cf = cf;
495 cb_ctx.data = data;
496 cb_ctx.stream = stream;
497 Curl_bufq_reset(&ctx->writebuf);
498 while(!result) {
499 result = Curl_bufq_slurp(&ctx->writebuf,
500 stream_resp_read, &cb_ctx, &nread);
501 if(!result)
502 cf_quiche_flush_body(cf, data, stream);
503 else if(result == CURLE_AGAIN)
504 break;
505 else if(result) {
506 CURL_TRC_CF(data, cf, "[%" PRIu64 "] recv_body error %d",
507 stream->id, (int)result);
508 failf(data, "[%" PRIu64 "] Error %d in HTTP/3 response body for stream",
509 stream->id, (int)result);
510 stream->closed = TRUE;
511 stream->reset = TRUE;
512 stream->send_closed = TRUE;
513 if(!stream->xfer_result)
514 stream->xfer_result = result;
515 }
516 }
517 cf_quiche_flush_body(cf, data, stream);
520static void cf_quiche_process_ev(struct Curl_cfilter *cf,
521 struct Curl_easy *data,
522 struct h3_stream_ctx *stream,
523 quiche_h3_event *ev)
525 enum quiche_h3_event_type type;
527 if(!stream)
528 return;
530 type = quiche_h3_event_type(ev);
532 switch(type) {
533 case QUICHE_H3_EVENT_HEADERS: {
534 struct cb_ctx cb_ctx;
535 stream->resp_got_header = TRUE;
536 cb_ctx.cf = cf;
537 cb_ctx.data = data;
538 cb_ctx.stream = stream;
539 quiche_h3_event_for_each_header(ev, cb_each_header, &cb_ctx);
540 CURL_TRC_CF(data, cf, "[%" PRIu64 "] <- [HEADERS]", stream->id);
541 Curl_multi_mark_dirty(data);
542 break;
543 }
544 case QUICHE_H3_EVENT_DATA:
545 if(!stream->resp_hds_complete) {
546 stream->resp_hds_complete = TRUE;
547 cf_quiche_write_hd(cf, data, stream, "\r\n", 2, FALSE);
548 }
549 cf_quiche_recv_body(cf, data, stream);
550 CURL_TRC_CF(data, cf, "[%" PRIu64 "] <- [DATA]", stream->id);
551 Curl_multi_mark_dirty(data);
552 break;
554 case QUICHE_H3_EVENT_RESET:
555 CURL_TRC_CF(data, cf, "[%" PRIu64 "] RESET", stream->id);
556 stream->closed = TRUE;
557 stream->reset = TRUE;
558 stream->send_closed = TRUE;
559 Curl_multi_mark_dirty(data);
560 break;
562 case QUICHE_H3_EVENT_FINISHED:
563 CURL_TRC_CF(data, cf, "[%" PRIu64 "] CLOSED", stream->id);
564 if(!stream->resp_hds_complete) {
565 stream->resp_hds_complete = TRUE;
566 cf_quiche_write_hd(cf, data, stream, "\r\n", 2, TRUE);
567 }
568 stream->closed = TRUE;
569 Curl_multi_mark_dirty(data);
570 break;
572 case QUICHE_H3_EVENT_GOAWAY:
573 CURL_TRC_CF(data, cf, "[%" PRIu64 "] <- [GOAWAY]", stream->id);
574 break;
576 default:
577 CURL_TRC_CF(data, cf, "[%" PRIu64 "] recv, unhandled event %d",
578 stream->id, (int)type);
579 break;
580 }
583struct cf_quiche_disp_ctx {
584 uint64_t stream_id;
585 struct Curl_cfilter *cf;
586 struct Curl_multi *multi;
587 quiche_h3_event *ev;
588};
590static bool cf_quiche_disp_event(uint32_t mid, void *val, void *user_data)
592 struct cf_quiche_disp_ctx *dctx = user_data;
593 struct h3_stream_ctx *stream = val;
595 if(stream->id == dctx->stream_id) {
596 struct Curl_easy *sdata = Curl_multi_get_easy(dctx->multi, mid);
597 if(sdata)
598 cf_quiche_process_ev(dctx->cf, sdata, stream, dctx->ev);
599 return FALSE; /* stop iterating */
600 }
601 return TRUE;
604static CURLcode cf_poll_events(struct Curl_cfilter *cf,
605 struct Curl_easy *data)
607 struct cf_quiche_ctx *ctx = cf->ctx;
608 struct h3_stream_ctx *stream = NULL;
609 quiche_h3_event *ev;
611 /* Take in the events and distribute them to the transfers. */
612 while(ctx->h3c) {
613 int64_t rv = quiche_h3_conn_poll(ctx->h3c, ctx->qconn, &ev);
614 if(rv == QUICHE_H3_ERR_DONE) {
615 break;
616 }
617 else if(rv < 0) {
618 CURL_TRC_CF(data, cf, "error poll: %" PRId64, rv);
619 return CURLE_HTTP3;
620 }
621 else {
622 stream = H3_STREAM_CTX(ctx, data);
623 if(stream && stream->id == (uint64_t)rv) {
624 /* event for calling transfer */
625 cf_quiche_process_ev(cf, data, stream, ev);
626 quiche_h3_event_free(ev);
627 if(stream->xfer_result)
628 return stream->xfer_result;
629 }
630 else {
631 /* another transfer, do not return errors, as they are not for
632 * the calling transfer */
633 struct cf_quiche_disp_ctx dctx;
634 dctx.stream_id = (uint64_t)rv;
635 dctx.cf = cf;
636 dctx.multi = data->multi;
637 dctx.ev = ev;
638 Curl_u32_ptrset_visit(&ctx->streams, cf_quiche_disp_event, &dctx);
639 quiche_h3_event_free(ev);
640 }
641 }
642 }
643 return CURLE_OK;
646struct recv_ctx {
647 struct Curl_cfilter *cf;
648 struct Curl_easy *data;
649 int pkts;
650};
652static CURLcode cf_quiche_recv_pkts(const unsigned char *buf, size_t buflen,
653 size_t gso_size,
654 struct sockaddr_storage *remote_addr,
655 socklen_t remote_addrlen, uint8_t ecn,
656 void *userp)
658 struct recv_ctx *r = userp;
659 struct cf_quiche_ctx *ctx = r->cf->ctx;
660 quiche_recv_info recv_info;
661 size_t pktlen, offset, nread;
662 ssize_t rv;
664 (void)ecn;
666 recv_info.to = (struct sockaddr *)&ctx->q.local_addr;
667 recv_info.to_len = ctx->q.local_addrlen;
668 recv_info.from = (struct sockaddr *)remote_addr;
669 recv_info.from_len = remote_addrlen;
671 for(offset = 0; offset < buflen; offset += gso_size) {
672 pktlen = ((offset + gso_size) <= buflen) ? gso_size : (buflen - offset);
673 rv = quiche_conn_recv(ctx->qconn,
674 (unsigned char *)CURL_UNCONST(buf + offset),
675 pktlen, &recv_info);
676 if(!curlx_sztouz(rv, &nread)) {
677 if(QUICHE_ERR_DONE == rv) {
678 if(quiche_conn_is_draining(ctx->qconn)) {
679 CURL_TRC_CF(r->data, r->cf, "ingress, connection is draining");
680 return CURLE_RECV_ERROR;
681 }
682 if(quiche_conn_is_closed(ctx->qconn)) {
683 CURL_TRC_CF(r->data, r->cf, "ingress, connection is closed");
684 return CURLE_RECV_ERROR;
685 }
686 CURL_TRC_CF(r->data, r->cf, "ingress, quiche is DONE");
687 return CURLE_OK;
688 }
689 else if(QUICHE_ERR_TLS_FAIL == rv) {
690 long verify_ok = SSL_get_verify_result(ctx->tls.ossl.ssl);
691 if(verify_ok != X509_V_OK) {
692 failf(r->data, "SSL certificate problem: %s",
693 X509_verify_cert_error_string(verify_ok));
694 return CURLE_PEER_FAILED_VERIFICATION;
695 }
696 failf(r->data, "ingress, quiche reports TLS fail");
697 return CURLE_RECV_ERROR;
698 }
699 else {
700 failf(r->data, "quiche reports error %zd on receive", rv);
701 return CURLE_RECV_ERROR;
702 }
703 }
704 else if(nread < pktlen) {
705 CURL_TRC_CF(r->data, r->cf, "ingress, quiche only read %zu/%zu bytes",
706 nread, pktlen);
707 }
708 ++r->pkts;
709 }
711 return CURLE_OK;
714static CURLcode cf_process_ingress(struct Curl_cfilter *cf,
715 struct Curl_easy *data)
717 struct cf_quiche_ctx *ctx = cf->ctx;
718 struct recv_ctx rctx;
719 CURLcode result;
721 DEBUGASSERT(ctx->qconn);
722 result = Curl_vquic_tls_before_recv(&ctx->tls, cf, data);
723 if(result)
724 return result;
726 rctx.cf = cf;
727 rctx.data = data;
728 rctx.pkts = 0;
730 result = Curl_vquic_recv_packets(cf, data, &ctx->q, 1000,
731 cf_quiche_recv_pkts, &rctx);
732 if(result)
733 return result;
735 if(rctx.pkts > 0) {
736 /* quiche digested ingress packets. It might have opened flow control
737 * windows again. */
738 DEBUGASSERT(data->multi);
739 cf_quiche_for_all_streams(cf, data->multi, cf_quiche_do_resume, NULL);
740 }
741 return cf_poll_events(cf, data);
744struct read_ctx {
745 struct Curl_cfilter *cf;
746 struct Curl_easy *data;
747 quiche_send_info send_info;
748};
750static CURLcode read_pkt_to_send(void *userp,
751 unsigned char *buf, size_t buflen,
752 size_t *pnread)
754 struct read_ctx *x = userp;
755 struct cf_quiche_ctx *ctx = x->cf->ctx;
756 ssize_t rv;
758 *pnread = 0;
759 rv = quiche_conn_send(ctx->qconn, buf, buflen, &x->send_info);
760 if(rv == QUICHE_ERR_DONE)
761 return CURLE_AGAIN;
763 if(!curlx_sztouz(rv, pnread)) {
764 failf(x->data, "quiche_conn_send returned %zd", rv);
765 return CURLE_SEND_ERROR;
766 }
767 return CURLE_OK;
770/*
771 * flush_egress drains the buffers and sends off data.
772 * Calls failf() on errors.
773 */
774static CURLcode cf_flush_egress(struct Curl_cfilter *cf,
775 struct Curl_easy *data)
777 struct cf_quiche_ctx *ctx = cf->ctx;
778 size_t nread;
779 CURLcode result;
780 int64_t expiry_ns;
781 int64_t timeout_ns;
782 struct read_ctx readx;
783 size_t pkt_count, gsolen;
785 expiry_ns = quiche_conn_timeout_as_nanos(ctx->qconn);
786 if(!expiry_ns) {
787 quiche_conn_on_timeout(ctx->qconn);
788 if(quiche_conn_is_closed(ctx->qconn)) {
789 if(quiche_conn_is_timed_out(ctx->qconn))
790 failf(data, "connection closed by idle timeout");
791 else
792 failf(data, "connection closed by server");
793 /* Connection timed out, expire all transfers belonging to it
794 * as it does not get any more POLL events here. */
795 cf_quiche_expire_conn_closed(cf, data);
796 return CURLE_SEND_ERROR;
797 }
798 }
800 result = Curl_vquic_flush(cf, data, &ctx->q);
801 if(result) {
802 if(result == CURLE_AGAIN) {
803 Curl_expire(data, 1, EXPIRE_QUIC);
804 return CURLE_OK;
805 }
806 return result;
807 }
809 readx.cf = cf;
810 readx.data = data;
811 memset(&readx.send_info, 0, sizeof(readx.send_info));
812 pkt_count = 0;
813 gsolen = quiche_conn_max_send_udp_payload_size(ctx->qconn);
814 for(;;) {
815 /* add the next packet to send, if any, to our buffer */
816 result = Curl_bufq_sipn(&ctx->q.sendbuf, 0,
817 read_pkt_to_send, &readx, &nread);
818 if(result) {
819 if(result != CURLE_AGAIN)
820 return result;
821 /* Nothing more to add, flush and leave */
822 result = Curl_vquic_send(cf, data, &ctx->q, gsolen);
823 if(result) {
824 if(result == CURLE_AGAIN) {
825 Curl_expire(data, 1, EXPIRE_QUIC);
826 return CURLE_OK;
827 }
828 return result;
829 }
830 goto out;
831 }
833 ++pkt_count;
834 if(nread < gsolen || pkt_count >= MAX_PKT_BURST) {
835 result = Curl_vquic_send(cf, data, &ctx->q, gsolen);
836 if(result) {
837 if(result == CURLE_AGAIN) {
838 Curl_expire(data, 1, EXPIRE_QUIC);
839 return CURLE_OK;
840 }
841 goto out;
842 }
843 pkt_count = 0;
844 }
845 }
847out:
848 timeout_ns = quiche_conn_timeout_as_nanos(ctx->qconn);
849 if(timeout_ns % 1000000)
850 timeout_ns += 1000000;
851 /* expire resolution is milliseconds */
852 Curl_expire(data, (timeout_ns / 1000000), EXPIRE_QUIC);
853 return result;
856static CURLcode recv_closed_stream(struct Curl_cfilter *cf,
857 struct Curl_easy *data,
858 size_t *pnread)
860 struct cf_quiche_ctx *ctx = cf->ctx;
861 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
862 CURLcode result = CURLE_OK;
864 DEBUGASSERT(stream);
865 *pnread = 0;
866 if(stream->reset) {
867 if(stream->error3 == CURL_H3_ERR_REQUEST_REJECTED) {
868 infof(data, "HTTP/3 stream %" PRIu64 " refused by server, try again "
869 "on a new connection", stream->id);
870 connclose(cf->conn); /* do not use this anymore */
871 data->state.refused_stream = TRUE;
872 return CURLE_RECV_ERROR; /* trigger Curl_retry_request() later */
873 }
874 else if(stream->resp_hds_complete && data->req.no_body) {
875 CURL_TRC_CF(data, cf, "[%" PRIu64 "] error after response headers, "
876 "but we did not want a body anyway, ignore error 0x%"
877 PRIx64 " %s", stream->id, stream->error3,
878 Curl_vquic_h3_err_str(stream->error3));
879 return CURLE_OK;
880 }
881 failf(data, "HTTP/3 stream %" PRIu64 " reset by server (error 0x%" PRIx64
882 " %s)", stream->id, stream->error3,
883 Curl_vquic_h3_err_str(stream->error3));
884 result = data->req.bytecount ? CURLE_PARTIAL_FILE : CURLE_HTTP3;
885 CURL_TRC_CF(data, cf, "[%" PRIu64 "] cf_recv, was reset -> %d",
886 stream->id, (int)result);
887 }
888 else if(!stream->resp_got_header) {
889 failf(data, "HTTP/3 stream %" PRIu64 " was closed cleanly, but before "
890 "getting all response header fields, treated as error",
891 stream->id);
892 result = CURLE_HTTP3;
893 }
894 return result;
897static CURLcode cf_quiche_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
898 char *buf, size_t blen, size_t *pnread)
900 struct cf_quiche_ctx *ctx = cf->ctx;
901 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
902 CURLcode result = CURLE_OK;
904 *pnread = 0;
905 (void)buf;
906 (void)blen;
907 Curl_vquic_ctx_update_time(&ctx->q, Curl_pgrs_now(data));
909 if(!stream)
910 return CURLE_RECV_ERROR;
912 result = cf_process_ingress(cf, data);
913 if(result) {
914 CURL_TRC_CF(data, cf, "cf_recv, error on ingress");
915 goto out;
916 }
918 if(stream->xfer_result) {
919 cf_quiche_stream_close(cf, data, stream);
920 result = stream->xfer_result;
921 goto out;
922 }
923 else if(stream->closed)
924 result = recv_closed_stream(cf, data, pnread);
925 else if(quiche_conn_is_draining(ctx->qconn)) {
926 failf(data, "QUIC connection is draining");
927 result = CURLE_HTTP3;
928 }
929 else
930 result = CURLE_AGAIN;
932out:
933 result = Curl_1st_fatal(result, cf_flush_egress(cf, data));
934 if(*pnread > 0)
935 ctx->data_recvd += *pnread;
936 CURL_TRC_CF(data, cf, "[%" PRIu64 "] cf_recv(len=%zu) -> %d, %zu, total=%"
937 FMT_OFF_T, stream->id, blen, (int)result, *pnread,
938 ctx->data_recvd);
939 return result;
942static CURLcode cf_quiche_send_body(struct Curl_cfilter *cf,
943 struct Curl_easy *data,
944 struct h3_stream_ctx *stream,
945 const uint8_t *buf, size_t len, bool eos,
946 size_t *pnwritten)
948 struct cf_quiche_ctx *ctx = cf->ctx;
949 ssize_t rv;
951 *pnwritten = 0;
952 rv = quiche_h3_send_body(ctx->h3c, ctx->qconn, stream->id,
953 (uint8_t *)CURL_UNCONST(buf), len, eos);
954 if(rv == QUICHE_H3_ERR_DONE || (rv == 0 && len > 0)) {
955 /* Blocked on flow control and should HOLD sending.
956 When do we open again? */
957 if(!quiche_conn_stream_writable(ctx->qconn, stream->id, len)) {
958 CURL_TRC_CF(data, cf, "[%" PRIu64 "] send_body(len=%zu) "
959 "-> window exhausted", stream->id, len);
960 stream->quic_flow_blocked = TRUE;
961 }
962 return CURLE_AGAIN;
963 }
964 else if(rv == QUICHE_H3_TRANSPORT_ERR_INVALID_STREAM_STATE) {
965 CURL_TRC_CF(data, cf, "[%" PRIu64 "] send_body(len=%zu) "
966 "-> invalid stream state", stream->id, len);
967 return CURLE_HTTP3;
968 }
969 else if(rv == QUICHE_H3_TRANSPORT_ERR_FINAL_SIZE) {
970 CURL_TRC_CF(data, cf, "[%" PRIu64 "] send_body(len=%zu) -> exceeds size",
971 stream->id, len);
972 return CURLE_SEND_ERROR;
973 }
974 else if(!curlx_sztouz(rv, pnwritten)) {
975 CURL_TRC_CF(data, cf, "[%" PRIu64 "] send_body(len=%zu) -> quiche err %zd",
976 stream->id, len, rv);
977 return CURLE_SEND_ERROR;
978 }
979 else {
980 if(eos && (len == *pnwritten))
981 stream->send_closed = TRUE;
982 CURL_TRC_CF(data, cf, "[%" PRIu64 "] send body(len=%zu, eos=%d) -> %zu",
983 stream->id, len, stream->send_closed, *pnwritten);
984 return CURLE_OK;
985 }
988static CURLcode h3_open_stream(struct Curl_cfilter *cf,
989 struct Curl_easy *data,
990 const uint8_t *buf, size_t blen, bool eos,
991 size_t *pnwritten)
993 struct cf_quiche_ctx *ctx = cf->ctx;
994 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
995 size_t nheader, i;
996 int64_t rv;
997 struct dynhds h2_headers;
998 quiche_h3_header *nva = NULL;
999 CURLcode result = CURLE_OK;
1001 *pnwritten = 0;
1002 if(!stream) {
1003 result = h3_data_setup(cf, data);
1004 if(result)
1005 return result;
1006 stream = H3_STREAM_CTX(ctx, data);
1007 DEBUGASSERT(stream);
1010 Curl_dynhds_init(&h2_headers, 0, DYN_HTTP_REQUEST);
1012 DEBUGASSERT(stream);
1014 result = Curl_h1_req_parse_read(&stream->h1, buf, blen, NULL,
1015 !data->state.http_ignorecustom ?
1016 CURL_EASY_STR(data, STRING_CUSTOMREQUEST) : NULL,
1017 0, pnwritten);
1018 if(result)
1019 goto out;
1020 if(!stream->h1.done) {
1021 /* need more data */
1022 goto out;
1024 DEBUGASSERT(stream->h1.req);
1026 result = Curl_http_req_to_h2(&h2_headers, stream->h1.req, data);
1027 if(result)
1028 goto out;
1030 /* no longer needed */
1031 Curl_h1_req_parse_free(&stream->h1);
1033 nheader = Curl_dynhds_count(&h2_headers);
1034 nva = curlx_malloc(sizeof(quiche_h3_header) * nheader);
1035 if(!nva) {
1036 result = CURLE_OUT_OF_MEMORY;
1037 goto out;
1040 for(i = 0; i < nheader; ++i) {
1041 struct dynhds_entry *e = Curl_dynhds_getn(&h2_headers, i);
1042 nva[i].name = (unsigned char *)e->name;
1043 nva[i].name_len = e->namelen;
1044 nva[i].value = (unsigned char *)e->value;
1045 nva[i].value_len = e->valuelen;
1048 buf += *pnwritten;
1049 blen -= *pnwritten;
1051 if(eos && !blen)
1052 stream->send_closed = TRUE;
1054 rv = quiche_h3_send_request(ctx->h3c, ctx->qconn, nva, nheader,
1055 stream->send_closed);
1056 CURL_TRC_CF(data, cf, "quiche_send_request() -> %" PRId64, rv);
1057 if(rv < 0) {
1058 if(QUICHE_H3_ERR_STREAM_BLOCKED == rv) {
1059 /* quiche seems to report this error if the connection window is
1060 * exhausted. Which happens frequently and intermittent. */
1061 CURL_TRC_CF(data, cf, "[%" PRIu64 "] blocked", stream->id);
1062 stream->quic_flow_blocked = TRUE;
1063 result = CURLE_AGAIN;
1064 goto out;
1066 else {
1067 CURL_TRC_CF(data, cf, "send_request(%s) -> %" PRId64,
1068 Curl_bufref_ptr(&data->state.url), rv);
1070 result = CURLE_SEND_ERROR;
1071 goto out;
1074 DEBUGASSERT(!stream->opened);
1075 stream->id = (uint64_t)rv;
1076 stream->opened = TRUE;
1077 stream->closed = FALSE;
1078 stream->reset = FALSE;
1080 if(Curl_trc_is_verbose(data)) {
1081 infof(data, "[HTTP/3] [%" PRIu64 "] OPENED stream for %s",
1082 stream->id, Curl_bufref_ptr(&data->state.url));
1083 for(i = 0; i < nheader; ++i) {
1084 infof(data, "[HTTP/3] [%" PRIu64 "] [%.*s: %.*s]", stream->id,
1085 (int)nva[i].name_len, nva[i].name,
1086 (int)nva[i].value_len, nva[i].value);
1090 if(blen) { /* after the headers, there was request BODY data */
1091 size_t nwritten;
1092 CURLcode r2 = CURLE_OK;
1094 r2 = cf_quiche_send_body(cf, data, stream, buf, blen, eos, &nwritten);
1095 if(r2 && (r2 != CURLE_AGAIN)) { /* real error, fail */
1096 result = r2;
1098 else if(nwritten > 0) {
1099 *pnwritten += nwritten;
1103out:
1104 curlx_free(nva);
1105 Curl_dynhds_free(&h2_headers);
1106 return result;
1109static CURLcode cf_quiche_send(struct Curl_cfilter *cf, struct Curl_easy *data,
1110 const uint8_t *buf, size_t len, bool eos,
1111 size_t *pnwritten)
1113 struct cf_quiche_ctx *ctx = cf->ctx;
1114 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
1115 CURLcode result;
1117 *pnwritten = 0;
1118 Curl_vquic_ctx_update_time(&ctx->q, Curl_pgrs_now(data));
1120 result = cf_process_ingress(cf, data);
1121 if(result)
1122 goto out;
1124 if(!stream || !stream->opened) {
1125 result = h3_open_stream(cf, data, buf, len, eos, pnwritten);
1126 if(result)
1127 goto out;
1128 stream = H3_STREAM_CTX(ctx, data);
1130 else if(stream->xfer_result) {
1131 cf_quiche_stream_close(cf, data, stream);
1132 result = stream->xfer_result;
1134 else if(stream->closed) {
1135 if(stream->resp_hds_complete) {
1136 /* sending request body on a stream that has been closed by the
1137 * server. If the server has send us a final response, we should
1138 * silently discard the send data.
1139 * This happens for example on redirects where the server, instead
1140 * of reading the full request body closed the stream after
1141 * sending the 30x response.
1142 * This is sort of a race: had the transfer loop called recv first,
1143 * it would see the response and stop/discard sending on its own- */
1144 CURL_TRC_CF(data, cf, "[%" PRIu64 "] discarding data "
1145 "on closed stream with response", stream->id);
1146 result = CURLE_OK;
1147 *pnwritten = len;
1148 goto out;
1150 CURL_TRC_CF(data, cf, "[%" PRIu64 "] send_body(len=%zu) "
1151 "-> stream closed", stream->id, len);
1152 result = CURLE_HTTP3;
1153 goto out;
1155 else {
1156 result = cf_quiche_send_body(cf, data, stream, buf, len, eos, pnwritten);
1159out:
1160 result = Curl_1st_fatal(result, cf_flush_egress(cf, data));
1162 CURL_TRC_CF(data, cf, "[%" PRIu64 "] cf_send(len=%zu) -> %d, %zu",
1163 stream ? stream->id : (uint64_t)~0, len,
1164 (int)result, *pnwritten);
1165 return result;
1168static bool stream_is_writable(struct Curl_cfilter *cf,
1169 struct Curl_easy *data)
1171 struct cf_quiche_ctx *ctx = cf->ctx;
1172 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
1174 return stream && (quiche_conn_stream_writable(
1175 ctx->qconn, stream->id, 1) > 0);
1178static CURLcode cf_quiche_adjust_pollset(struct Curl_cfilter *cf,
1179 struct Curl_easy *data,
1180 struct easy_pollset *ps)
1182 struct cf_quiche_ctx *ctx = cf->ctx;
1183 bool want_recv, want_send;
1184 CURLcode result = CURLE_OK;
1186 if(!ctx->qconn)
1187 return CURLE_OK;
1189 Curl_pollset_check(data, ps, ctx->q.sockfd, &want_recv, &want_send);
1190 if(want_recv || want_send) {
1191 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
1192 bool c_exhaust, s_exhaust;
1194 c_exhaust = FALSE; /* Have not found any call in quiche that tells
1195 us if the connection itself is blocked */
1196 s_exhaust = want_send && stream && stream->opened &&
1197 (stream->quic_flow_blocked || !stream_is_writable(cf, data));
1198 want_recv = (want_recv || c_exhaust || s_exhaust);
1199 want_send = (!s_exhaust && want_send) ||
1200 !Curl_bufq_is_empty(&ctx->q.sendbuf);
1202 result = Curl_pollset_set(data, ps, ctx->q.sockfd, want_recv, want_send);
1204 return result;
1207static CURLcode h3_data_pause(struct Curl_cfilter *cf,
1208 struct Curl_easy *data,
1209 bool pause)
1211 /* There seems to exist no API in quiche to shrink/enlarge the streams
1212 * windows. As we do in HTTP/2. */
1213 (void)cf;
1214 if(!pause) {
1215 Curl_multi_mark_dirty(data);
1217 return CURLE_OK;
1220static CURLcode cf_quiche_cntrl(struct Curl_cfilter *cf,
1221 struct Curl_easy *data,
1222 int event, int arg1, void *arg2)
1224 struct cf_quiche_ctx *ctx = cf->ctx;
1225 CURLcode result = CURLE_OK;
1227 (void)arg1;
1228 (void)arg2;
1229 switch(event) {
1230 case CF_CTRL_DATA_SETUP:
1231 break;
1232 case CF_CTRL_DATA_PAUSE:
1233 result = h3_data_pause(cf, data, (arg1 != 0));
1234 break;
1235 case CF_CTRL_DATA_DONE:
1236 h3_data_done(cf, data);
1237 break;
1238 case CF_CTRL_DATA_DONE_SEND: {
1239 struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
1240 if(stream && !stream->send_closed) {
1241 unsigned char body[1];
1242 size_t sent;
1244 stream->send_closed = TRUE;
1245 body[0] = 'X';
1246 result = cf_quiche_send(cf, data, body, 0, TRUE, &sent);
1247 CURL_TRC_CF(data, cf, "[%" PRIu64 "] DONE_SEND -> %d, %zu",
1248 stream->id, (int)result, sent);
1250 break;
1252 case CF_CTRL_CONN_INFO_UPDATE:
1253 if(!cf->sockindex && cf->connected) {
1254 cf->conn->httpversion_seen = 30;
1255 Curl_conn_set_multiplex(cf->conn);
1257 break;
1258 case CF_CTRL_REPORT_STATS:
1259 if(cf->connected && !ctx->stats_reported &&
1260 (ctx->handshake_at.tv_sec || ctx->handshake_at.tv_usec)) {
1261 Curl_pgrsTimeWas(data, TIMER_CONNECT, ctx->q.first_byte_at);
1262 Curl_pgrsTimeWas(data, TIMER_APPCONNECT, ctx->handshake_at);
1263 ctx->stats_reported = TRUE;
1265 break;
1266 default:
1267 break;
1269 return result;
1272static CURLcode cf_quiche_ctx_open(struct Curl_cfilter *cf,
1273 struct Curl_easy *data)
1275 struct cf_quiche_ctx *ctx = cf->ctx;
1276 int rv;
1277 CURLcode result;
1278 const struct Curl_sockaddr_ex *sockaddr;
1279 static const struct alpn_spec ALPN_SPEC_H3 = { { "h3" }, 1 };
1281 DEBUGASSERT(ctx->q.sockfd != CURL_SOCKET_BAD);
1282 DEBUGASSERT(ctx->initialized);
1284 result = Curl_vquic_ctx_init(data, &ctx->q);
1285 if(result)
1286 return result;
1288 ctx->cfg = quiche_config_new(QUICHE_PROTOCOL_VERSION);
1289 if(!ctx->cfg) {
1290 failf(data, "cannot create quiche config");
1291 return CURLE_FAILED_INIT;
1293 quiche_config_enable_pacing(ctx->cfg, FALSE);
1294 quiche_config_set_initial_max_data(ctx->cfg, (1 * 1024 * 1024)
1295 /* (QUIC_MAX_STREAMS/2) * H3_STREAM_WINDOW_SIZE */);
1296 quiche_config_set_initial_max_streams_bidi(ctx->cfg, QUIC_MAX_STREAMS);
1297 quiche_config_set_initial_max_streams_uni(ctx->cfg, QUIC_MAX_STREAMS);
1298 quiche_config_set_initial_max_stream_data_bidi_local(ctx->cfg,
1299 H3_STREAM_WINDOW_SIZE);
1300 quiche_config_set_initial_max_stream_data_bidi_remote(ctx->cfg,
1301 H3_STREAM_WINDOW_SIZE);
1302 quiche_config_set_initial_max_stream_data_uni(ctx->cfg,
1303 H3_STREAM_WINDOW_SIZE);
1304 quiche_config_set_disable_active_migration(ctx->cfg, TRUE);
1306 quiche_config_set_max_connection_window(ctx->cfg,
1307 10 * QUIC_MAX_STREAMS * H3_STREAM_WINDOW_SIZE);
1308 quiche_config_set_max_stream_window(ctx->cfg, 10 * H3_STREAM_WINDOW_SIZE);
1309 quiche_config_set_application_protos(ctx->cfg,
1310 (uint8_t *)CURL_UNCONST(QUICHE_H3_APPLICATION_PROTOCOL),
1311 CURL_CSTRLEN(QUICHE_H3_APPLICATION_PROTOCOL));
1313 result = Curl_vquic_tls_init(&ctx->tls, cf, data, &ctx->ssl_peer,
1314 &ALPN_SPEC_H3, NULL, NULL, cf, NULL);
1315 if(result)
1316 return result;
1318 result = Curl_rand(data, ctx->scid, sizeof(ctx->scid));
1319 if(result)
1320 return result;
1322 if(Curl_cf_socket_peek(cf->next, data, &ctx->q.sockfd, &sockaddr, NULL))
1323 return CURLE_QUIC_CONNECT_ERROR;
1325 ctx->q.local_addrlen = sizeof(ctx->q.local_addr);
1326 rv = getsockname(ctx->q.sockfd, (struct sockaddr *)&ctx->q.local_addr,
1327 &ctx->q.local_addrlen);
1328 if(rv == -1)
1329 return CURLE_QUIC_CONNECT_ERROR;
1331 ctx->qconn = quiche_conn_new_with_tls((const uint8_t *)ctx->scid,
1332 sizeof(ctx->scid), NULL, 0,
1333 (struct sockaddr *)&ctx->q.local_addr,
1334 ctx->q.local_addrlen,
1335 &sockaddr->curl_sa_addr,
1336 sockaddr->addrlen,
1337 ctx->cfg, ctx->tls.ossl.ssl, FALSE);
1338 if(!ctx->qconn) {
1339 failf(data, "cannot create quiche connection");
1340 return CURLE_OUT_OF_MEMORY;
1343 /* Known to not work on Windows */
1344#if !defined(_WIN32) && defined(HAVE_QUICHE_CONN_SET_QLOG_FD)
1346 int qfd;
1347 (void)Curl_qlogdir(data, ctx->scid, sizeof(ctx->scid), &qfd);
1348 if(qfd != -1)
1349 quiche_conn_set_qlog_fd(ctx->qconn, qfd, "qlog title", "curl qlog");
1351#endif
1353 result = cf_flush_egress(cf, data);
1354 if(result)
1355 return result;
1358 unsigned char alpn_protocols[] = QUICHE_H3_APPLICATION_PROTOCOL;
1359 unsigned alpn_len, offset = 0;
1361 /* Replace each ALPN length prefix by a comma. */
1362 while(offset < CURL_CSTRLEN(alpn_protocols)) {
1363 alpn_len = alpn_protocols[offset];
1364 alpn_protocols[offset] = ',';
1365 offset += 1 + alpn_len;
1368 CURL_TRC_CF(data, cf, "Sent QUIC client Initial, ALPN: %s",
1369 alpn_protocols + 1);
1372 return CURLE_OK;
1375static CURLcode cf_quiche_verify_peer(struct Curl_cfilter *cf,
1376 struct Curl_easy *data)
1378 struct cf_quiche_ctx *ctx = cf->ctx;
1379 return Curl_vquic_tls_verify_peer(&ctx->tls, cf, data, &ctx->ssl_peer);
1382static CURLcode cf_quiche_connect(struct Curl_cfilter *cf,
1383 struct Curl_easy *data,
1384 bool *done)
1386 struct cf_quiche_ctx *ctx = cf->ctx;
1387 CURLcode result = CURLE_OK;
1389 if(cf->connected) {
1390 *done = TRUE;
1391 return CURLE_OK;
1394 /* Connect the UDP filter first */
1395 if(!cf->next->connected) {
1396 result = Curl_conn_cf_connect(cf->next, data, done);
1397 if(result || !*done)
1398 return result;
1401 *done = FALSE;
1402 if(Curl_ssl_need_httpsrr(cf) &&
1403 !Curl_conn_dns_resolved_https(data, cf->sockindex, ctx->ssl_peer.peer)) {
1404 CURL_TRC_CF(data, cf, "need HTTPS-RR, delaying connect");
1405 return CURLE_OK;
1408 Curl_vquic_ctx_update_time(&ctx->q, Curl_pgrs_now(data));
1410 if(!ctx->qconn) {
1411 result = cf_quiche_ctx_open(cf, data);
1412 if(result)
1413 goto out;
1414 ctx->started_at = ctx->q.last_op;
1415 result = cf_flush_egress(cf, data);
1416 /* we do not expect to be able to recv anything yet */
1417 goto out;
1420 result = cf_process_ingress(cf, data);
1421 if(result)
1422 goto out;
1424 result = cf_flush_egress(cf, data);
1425 if(result)
1426 goto out;
1428 if(quiche_conn_is_established(ctx->qconn)) {
1429 ctx->handshake_at = ctx->q.last_op;
1430 CURL_TRC_CF(data, cf, "handshake complete after %" FMT_TIMEDIFF_T "ms",
1431 curlx_ptimediff_ms(&ctx->handshake_at, &ctx->started_at));
1432 result = cf_quiche_verify_peer(cf, data);
1433 if(!result) {
1434 CURL_TRC_CF(data, cf, "peer verified");
1435 ctx->h3config = quiche_h3_config_new();
1436 if(!ctx->h3config) {
1437 result = CURLE_OUT_OF_MEMORY;
1438 goto out;
1440 /* quiche 0.29.3+ rejects response headers larger than 32 KiB by
1441 default. Allow as much as curl itself accepts. */
1442 quiche_h3_config_set_max_field_section_size(ctx->h3config,
1443 MAX_HTTP_RESP_HEADER_SIZE);
1445 /* Create a new HTTP/3 connection on the QUIC connection. */
1446 ctx->h3c = quiche_h3_conn_new_with_transport(ctx->qconn, ctx->h3config);
1447 if(!ctx->h3c) {
1448 result = CURLE_OUT_OF_MEMORY;
1449 goto out;
1451 cf->connected = TRUE;
1452 *done = TRUE;
1455 else if(quiche_conn_is_draining(ctx->qconn)) {
1456 /* When a QUIC server instance is shutting down, it may send us a
1457 * CONNECTION_CLOSE right away. Our connection then enters the DRAINING
1458 * state. The CONNECT may work in the near future again. Indicate
1459 * that as a "weird" reply. */
1460 result = CURLE_WEIRD_SERVER_REPLY;
1463out:
1464#ifdef CURLVERBOSE
1465 if(result && result != CURLE_AGAIN) {
1466 struct ip_quadruple ip;
1468 if(!Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip))
1469 infof(data, "connect to %s port %u failed: %s",
1470 ip.remote_ip, ip.remote_port, curl_easy_strerror(result));
1471 else
1472 infof(data, "connect failed: %s", curl_easy_strerror(result));
1474#endif
1475 return result;
1478static CURLcode cf_quiche_shutdown(struct Curl_cfilter *cf,
1479 struct Curl_easy *data, bool *done)
1481 struct cf_quiche_ctx *ctx = cf->ctx;
1482 CURLcode result = CURLE_OK;
1484 if(cf->shutdown || !ctx || !ctx->qconn) {
1485 *done = TRUE;
1486 return CURLE_OK;
1489 *done = FALSE;
1490 if(!ctx->shutdown_started) {
1491 int err;
1493 ctx->shutdown_started = TRUE;
1494 Curl_vquic_ctx_update_time(&ctx->q, Curl_pgrs_now(data));
1495 err = quiche_conn_close(ctx->qconn, TRUE, 0, NULL, 0);
1496 if(err) {
1497 CURL_TRC_CF(data, cf, "error %d adding shutdown packet, "
1498 "aborting shutdown", err);
1499 result = CURLE_SEND_ERROR;
1500 goto out;
1504 if(!Curl_bufq_is_empty(&ctx->q.sendbuf)) {
1505 CURL_TRC_CF(data, cf, "shutdown, flushing sendbuf");
1506 result = cf_flush_egress(cf, data);
1507 if(result)
1508 goto out;
1511 if(Curl_bufq_is_empty(&ctx->q.sendbuf)) {
1512 /* sent everything, quiche does not seem to support a graceful shutdown
1513 * waiting for a reply, so we are done. */
1514 CURL_TRC_CF(data, cf, "shutdown completely sent off, done");
1515 *done = TRUE;
1517 else {
1518 CURL_TRC_CF(data, cf, "shutdown sending blocked");
1521out:
1522 return result;
1525static void cf_quiche_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
1527 (void)data;
1528 if(cf->ctx) {
1529 cf_quiche_ctx_close(cf->ctx);
1530 cf_quiche_ctx_free(cf->ctx);
1531 cf->ctx = NULL;
1535static CURLcode cf_quiche_query(struct Curl_cfilter *cf,
1536 struct Curl_easy *data,
1537 int query, int *pres1, void *pres2)
1539 struct cf_quiche_ctx *ctx = cf->ctx;
1541 switch(query) {
1542 case CF_QUERY_MAX_CONCURRENT: {
1543 uint64_t max_streams = cf->conn->attached_xfers;
1544 if(!ctx->goaway && ctx->qconn) {
1545 max_streams += quiche_conn_peer_streams_left_bidi(ctx->qconn);
1547 *pres1 = (max_streams > INT_MAX) ? INT_MAX : (int)max_streams;
1548 CURL_TRC_CF(data, cf, "query conn[%" FMT_OFF_T "]: "
1549 "MAX_CONCURRENT -> %d (%u in use)",
1550 cf->conn->connection_id, *pres1, cf->conn->attached_xfers);
1551 return CURLE_OK;
1553 case CF_QUERY_CONNECT_REPLY_MS:
1554 if(ctx->q.got_first_byte) {
1555 timediff_t ms = curlx_ptimediff_ms(&ctx->q.first_byte_at,
1556 &ctx->started_at);
1557 *pres1 = (ms < INT_MAX) ? (int)ms : INT_MAX;
1559 else
1560 *pres1 = -1;
1561 return CURLE_OK;
1562 case CF_QUERY_HTTP_VERSION:
1563 *pres1 = 30;
1564 return CURLE_OK;
1565 case CF_QUERY_SSL_INFO:
1566 case CF_QUERY_SSL_CTX_INFO: {
1567 struct curl_tlssessioninfo *info = pres2;
1568 if(Curl_vquic_tls_get_ssl_info(&ctx->tls,
1569 (query == CF_QUERY_SSL_CTX_INFO), info))
1570 return CURLE_OK;
1571 break;
1573 case CF_QUERY_ALPN_NEGOTIATED: {
1574 const char **palpn = pres2;
1575 DEBUGASSERT(palpn);
1576 *palpn = cf->connected ? "h3" : NULL;
1577 return CURLE_OK;
1579 default:
1580 break;
1582 return cf->next ?
1583 cf->next->cft->query(cf->next, data, query, pres1, pres2) :
1584 CURLE_UNKNOWN_OPTION;
1587static bool cf_quiche_conn_is_alive(struct Curl_cfilter *cf,
1588 struct Curl_easy *data,
1589 bool *input_pending)
1591 struct cf_quiche_ctx *ctx = cf->ctx;
1592 bool alive = TRUE;
1594 *input_pending = FALSE;
1595 if(!ctx->qconn)
1596 return FALSE;
1598 if(quiche_conn_is_closed(ctx->qconn)) {
1599 if(quiche_conn_is_timed_out(ctx->qconn))
1600 CURL_TRC_CF(data, cf, "connection was closed due to idle timeout");
1601 else
1602 CURL_TRC_CF(data, cf, "connection is closed");
1603 return FALSE;
1606 if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending))
1607 return FALSE;
1609 if(*input_pending) {
1610 /* This happens before we have sent off a request and the connection is
1611 not in use by any other transfer, there should not be any data here,
1612 only "protocol frames" */
1613 *input_pending = FALSE;
1614 if(cf_process_ingress(cf, data))
1615 alive = FALSE;
1616 else {
1617 alive = TRUE;
1621 return alive;
1624struct Curl_cftype Curl_cft_http3 = {
1625 "HTTP/3",
1626 CF_TYPE_IP_CONNECT | CF_TYPE_SSL | CF_TYPE_MULTIPLEX | CF_TYPE_HTTP,
1627 0,
1628 cf_quiche_destroy,
1629 cf_quiche_connect,
1630 cf_quiche_shutdown,
1631 cf_quiche_adjust_pollset,
1632 Curl_cf_def_data_pending,
1633 cf_quiche_send,
1634 cf_quiche_recv,
1635 cf_quiche_cntrl,
1636 cf_quiche_conn_is_alive,
1637 Curl_cf_def_conn_keep_alive,
1638 cf_quiche_query,
1641CURLcode Curl_cf_quiche_create(struct Curl_cfilter **pcf,
1642 struct Curl_easy *data,
1643 struct Curl_peer *origin,
1644 struct Curl_peer *peer,
1645 struct connectdata *conn,
1646 struct Curl_sockaddr_ex *addr)
1648 struct cf_quiche_ctx *ctx = NULL;
1649 struct Curl_cfilter *cf = NULL;
1650 CURLcode result;
1652 ctx = curlx_calloc(1, sizeof(*ctx));
1653 if(!ctx) {
1654 result = CURLE_OUT_OF_MEMORY;
1655 goto out;
1657 result = cf_quiche_ctx_init(ctx, origin, peer, &conn->ssl_config);
1658 if(!result)
1659 result = Curl_cf_create(&cf, &Curl_cft_http3, ctx);
1660 if(result)
1661 goto out;
1662 cf->conn = conn;
1664 result = Curl_cf_udp_create(&cf->next, data, origin, peer, TRNSPRT_QUIC,
1665 conn, addr, NULL, TRNSPRT_QUIC);
1666 if(result)
1667 goto out;
1668 cf->next->conn = cf->conn;
1669 cf->next->sockindex = cf->sockindex;
1671out:
1672 *pcf = (!result) ? cf : NULL;
1673 if(result) {
1674 if(cf)
1675 Curl_conn_cf_discard_chain(&cf, data);
1676 else if(ctx)
1677 cf_quiche_ctx_free(ctx);
1680 return result;
1683CURLcode Curl_cf_quiche_insert_after(struct Curl_cfilter *cf_at,
1684 struct Curl_peer *origin,
1685 struct Curl_peer *peer,
1686 struct Curl_cfilter **pcf)
1688 struct cf_quiche_ctx *ctx = NULL;
1689 struct Curl_cfilter *cf = NULL;
1690 CURLcode result;
1692 ctx = curlx_calloc(1, sizeof(*ctx));
1693 if(!ctx) {
1694 result = CURLE_OUT_OF_MEMORY;
1695 goto out;
1697 result = cf_quiche_ctx_init(ctx, origin, peer, &cf_at->conn->ssl_config);
1698 if(!result)
1699 result = Curl_cf_create(&cf, &Curl_cft_http3, ctx);
1700 if(result)
1701 goto out;
1702 Curl_conn_cf_insert_after(cf_at, cf);
1704out:
1705 if(result) {
1706 curlx_safefree(cf);
1707 if(ctx)
1708 cf_quiche_ctx_free(ctx);
1710 *pcf = cf;
1711 return result;
1714#endif
Diagnostics4 git calls · 608 ms
CallStatusSizeTime0608 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
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 · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=2
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 · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=603
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=2
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=86
Open pack map
fetch20014 KiB603 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=14523
git.response.cache=MISS
git.response.duration_ms=603
git.fetch.wants=c6456d17f885c92b5245d43d5f628527b9034dbd
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=14523
git.pkt.payload.max=14502
git.sideband.band1.bytes=14501
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
a5e37222df3b22ce
Request
9580fafa…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format