Coverage Report

Created: 2025-12-03 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/cf-https-connect.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
25
#include "curl_setup.h"
26
27
#ifndef CURL_DISABLE_HTTP
28
29
#include "urldata.h"
30
#include <curl/curl.h>
31
#include "curl_trc.h"
32
#include "cfilters.h"
33
#include "connect.h"
34
#include "hostip.h"
35
#include "multiif.h"
36
#include "cf-https-connect.h"
37
#include "http2.h"
38
#include "select.h"
39
#include "vquic/vquic.h"
40
41
typedef enum {
42
  CF_HC_INIT,
43
  CF_HC_CONNECT,
44
  CF_HC_SUCCESS,
45
  CF_HC_FAILURE
46
} cf_hc_state;
47
48
struct cf_hc_baller {
49
  const char *name;
50
  struct Curl_cfilter *cf;
51
  CURLcode result;
52
  struct curltime started;
53
  int reply_ms;
54
  uint8_t transport;
55
  enum alpnid alpn_id;
56
  BIT(shutdown);
57
};
58
59
static void cf_hc_baller_reset(struct cf_hc_baller *b,
60
                               struct Curl_easy *data)
61
0
{
62
0
  if(b->cf) {
63
0
    Curl_conn_cf_close(b->cf, data);
64
0
    Curl_conn_cf_discard_chain(&b->cf, data);
65
0
    b->cf = NULL;
66
0
  }
67
0
  b->result = CURLE_OK;
68
0
  b->reply_ms = -1;
69
0
}
70
71
static bool cf_hc_baller_is_active(struct cf_hc_baller *b)
72
0
{
73
0
  return b->cf && !b->result;
74
0
}
75
76
static bool cf_hc_baller_has_started(struct cf_hc_baller *b)
77
0
{
78
0
  return !!b->cf;
79
0
}
80
81
static int cf_hc_baller_reply_ms(struct cf_hc_baller *b,
82
                                 struct Curl_easy *data)
83
0
{
84
0
  if(b->cf && (b->reply_ms < 0))
85
0
    b->cf->cft->query(b->cf, data, CF_QUERY_CONNECT_REPLY_MS,
86
0
                      &b->reply_ms, NULL);
87
0
  return b->reply_ms;
88
0
}
89
90
static bool cf_hc_baller_data_pending(struct cf_hc_baller *b,
91
                                      const struct Curl_easy *data)
92
0
{
93
0
  return b->cf && !b->result && b->cf->cft->has_data_pending(b->cf, data);
94
0
}
95
96
static bool cf_hc_baller_needs_flush(struct cf_hc_baller *b,
97
                                     struct Curl_easy *data)
98
0
{
99
0
  return b->cf && !b->result && Curl_conn_cf_needs_flush(b->cf, data);
100
0
}
101
102
static CURLcode cf_hc_baller_cntrl(struct cf_hc_baller *b,
103
                                   struct Curl_easy *data,
104
                                   int event, int arg1, void *arg2)
105
0
{
106
0
  if(b->cf && !b->result)
107
0
    return Curl_conn_cf_cntrl(b->cf, data, FALSE, event, arg1, arg2);
108
0
  return CURLE_OK;
109
0
}
110
111
struct cf_hc_ctx {
112
  cf_hc_state state;
113
  struct curltime started;  /* when connect started */
114
  CURLcode result;          /* overall result */
115
  struct cf_hc_baller ballers[2];
116
  size_t baller_count;
117
  timediff_t soft_eyeballs_timeout_ms;
118
  timediff_t hard_eyeballs_timeout_ms;
119
};
120
121
static void cf_hc_baller_assign(struct cf_hc_baller *b,
122
                                enum alpnid alpn_id,
123
                                uint8_t def_transport)
124
0
{
125
0
  b->alpn_id = alpn_id;
126
0
  b->transport = def_transport;
127
0
  switch(b->alpn_id) {
128
0
  case ALPN_h3:
129
0
    b->name = "h3";
130
0
    b->transport = TRNSPRT_QUIC;
131
0
    break;
132
0
  case ALPN_h2:
133
0
    b->name = "h2";
134
0
    break;
135
0
  case ALPN_h1:
136
0
    b->name = "h1";
137
0
    break;
138
0
  default:
139
0
    b->result = CURLE_FAILED_INIT;
140
0
    break;
141
0
  }
142
0
}
143
144
static void cf_hc_baller_init(struct cf_hc_baller *b,
145
                              struct Curl_cfilter *cf,
146
                              struct Curl_easy *data,
147
                              uint8_t transport)
148
0
{
149
0
  struct Curl_cfilter *save = cf->next;
150
151
0
  cf->next = NULL;
152
0
  b->started = curlx_now();
153
0
  switch(b->alpn_id) {
154
0
  case ALPN_h3:
155
0
    transport = TRNSPRT_QUIC;
156
0
    break;
157
0
  default:
158
0
    break;
159
0
  }
160
161
0
  if(!b->result)
162
0
    b->result = Curl_cf_setup_insert_after(cf, data, transport,
163
0
                                           CURL_CF_SSL_ENABLE);
164
0
  b->cf = cf->next;
165
0
  cf->next = save;
166
0
}
167
168
static CURLcode cf_hc_baller_connect(struct cf_hc_baller *b,
169
                                     struct Curl_cfilter *cf,
170
                                     struct Curl_easy *data,
171
                                     bool *done)
172
0
{
173
0
  struct Curl_cfilter *save = cf->next;
174
175
0
  cf->next = b->cf;
176
0
  b->result = Curl_conn_cf_connect(cf->next, data, done);
177
0
  b->cf = cf->next; /* it might mutate */
178
0
  cf->next = save;
179
0
  return b->result;
180
0
}
181
182
static void cf_hc_reset(struct Curl_cfilter *cf, struct Curl_easy *data)
183
0
{
184
0
  struct cf_hc_ctx *ctx = cf->ctx;
185
0
  size_t i;
186
187
0
  if(ctx) {
188
0
    for(i = 0; i < ctx->baller_count; ++i)
189
0
      cf_hc_baller_reset(&ctx->ballers[i], data);
190
0
    ctx->state = CF_HC_INIT;
191
0
    ctx->result = CURLE_OK;
192
0
    ctx->hard_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout;
193
0
    ctx->soft_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout / 4;
194
0
  }
195
0
}
196
197
static CURLcode baller_connected(struct Curl_cfilter *cf,
198
                                 struct Curl_easy *data,
199
                                 struct cf_hc_baller *winner)
200
0
{
201
0
  struct cf_hc_ctx *ctx = cf->ctx;
202
0
  CURLcode result = CURLE_OK;
203
0
  int reply_ms;
204
0
  size_t i;
205
206
0
  DEBUGASSERT(winner->cf);
207
0
  for(i = 0; i < ctx->baller_count; ++i)
208
0
    if(winner != &ctx->ballers[i])
209
0
      cf_hc_baller_reset(&ctx->ballers[i], data);
210
211
0
  reply_ms = cf_hc_baller_reply_ms(winner, data);
212
0
  if(reply_ms >= 0)
213
0
    CURL_TRC_CF(data, cf, "connect+handshake %s: %dms, 1st data: %dms",
214
0
                winner->name,
215
0
                (int)curlx_timediff_ms(curlx_now(),
216
0
                                       winner->started), reply_ms);
217
0
  else
218
0
    CURL_TRC_CF(data, cf, "deferred handshake %s: %dms",
219
0
                winner->name, (int)curlx_timediff_ms(curlx_now(),
220
0
                                                     winner->started));
221
222
  /* install the winning filter below this one. */
223
0
  cf->next = winner->cf;
224
0
  winner->cf = NULL;
225
226
0
#ifdef USE_NGHTTP2
227
0
  {
228
    /* Using nghttp2, we add the filter "below" us, so when the conn
229
     * closes, we tear it down for a fresh reconnect */
230
0
    const char *alpn = Curl_conn_cf_get_alpn_negotiated(cf->next, data);
231
0
    if(alpn && !strcmp("h2", alpn)) {
232
0
      result = Curl_http2_switch_at(cf, data);
233
0
      if(result) {
234
0
        ctx->state = CF_HC_FAILURE;
235
0
        ctx->result = result;
236
0
        return result;
237
0
      }
238
0
    }
239
0
  }
240
0
#endif
241
242
0
  ctx->state = CF_HC_SUCCESS;
243
0
  cf->connected = TRUE;
244
0
  return result;
245
0
}
246
247
static bool time_to_start_next(struct Curl_cfilter *cf,
248
                               struct Curl_easy *data,
249
                               size_t idx, struct curltime now)
250
0
{
251
0
  struct cf_hc_ctx *ctx = cf->ctx;
252
0
  timediff_t elapsed_ms;
253
0
  size_t i;
254
255
0
  if(idx >= ctx->baller_count)
256
0
    return FALSE;
257
0
  if(cf_hc_baller_has_started(&ctx->ballers[idx]))
258
0
    return FALSE;
259
0
  for(i = 0; i < idx; i++) {
260
0
    if(!ctx->ballers[i].result)
261
0
      break;
262
0
  }
263
0
  if(i == idx) {
264
0
    CURL_TRC_CF(data, cf, "all previous attempts failed, starting %s",
265
0
                ctx->ballers[idx].name);
266
0
    return TRUE;
267
0
  }
268
0
  elapsed_ms = curlx_timediff_ms(now, ctx->started);
269
0
  if(elapsed_ms >= ctx->hard_eyeballs_timeout_ms) {
270
0
    CURL_TRC_CF(data, cf, "hard timeout of %" FMT_TIMEDIFF_T "ms reached, "
271
0
                "starting %s",
272
0
                ctx->hard_eyeballs_timeout_ms, ctx->ballers[idx].name);
273
0
    return TRUE;
274
0
  }
275
276
0
  if((idx > 0) && (elapsed_ms >= ctx->soft_eyeballs_timeout_ms)) {
277
0
    if(cf_hc_baller_reply_ms(&ctx->ballers[idx - 1], data) < 0) {
278
0
      CURL_TRC_CF(data, cf, "soft timeout of %" FMT_TIMEDIFF_T "ms reached, "
279
0
                  "%s has not seen any data, starting %s",
280
0
                  ctx->soft_eyeballs_timeout_ms,
281
0
                  ctx->ballers[idx - 1].name, ctx->ballers[idx].name);
282
0
      return TRUE;
283
0
    }
284
    /* set the effective hard timeout again */
285
0
    Curl_expire(data, ctx->hard_eyeballs_timeout_ms - elapsed_ms,
286
0
                EXPIRE_ALPN_EYEBALLS);
287
0
  }
288
0
  return FALSE;
289
0
}
290
291
static CURLcode cf_hc_connect(struct Curl_cfilter *cf,
292
                              struct Curl_easy *data,
293
                              bool *done)
294
0
{
295
0
  struct cf_hc_ctx *ctx = cf->ctx;
296
0
  struct curltime now;
297
0
  CURLcode result = CURLE_OK;
298
0
  size_t i, failed_ballers;
299
300
0
  if(cf->connected) {
301
0
    *done = TRUE;
302
0
    return CURLE_OK;
303
0
  }
304
305
0
  *done = FALSE;
306
0
  now = curlx_now();
307
0
  switch(ctx->state) {
308
0
  case CF_HC_INIT:
309
0
    DEBUGASSERT(!cf->next);
310
0
    for(i = 0; i < ctx->baller_count; i++)
311
0
      DEBUGASSERT(!ctx->ballers[i].cf);
312
0
    CURL_TRC_CF(data, cf, "connect, init");
313
0
    ctx->started = now;
314
0
    cf_hc_baller_init(&ctx->ballers[0], cf, data, ctx->ballers[0].transport);
315
0
    if(ctx->baller_count > 1) {
316
0
      Curl_expire(data, ctx->soft_eyeballs_timeout_ms, EXPIRE_ALPN_EYEBALLS);
317
0
      CURL_TRC_CF(data, cf, "set next attempt to start in %" FMT_TIMEDIFF_T
318
0
                  "ms", ctx->soft_eyeballs_timeout_ms);
319
0
    }
320
0
    ctx->state = CF_HC_CONNECT;
321
0
    FALLTHROUGH();
322
323
0
  case CF_HC_CONNECT:
324
0
    if(cf_hc_baller_is_active(&ctx->ballers[0])) {
325
0
      result = cf_hc_baller_connect(&ctx->ballers[0], cf, data, done);
326
0
      if(!result && *done) {
327
0
        result = baller_connected(cf, data, &ctx->ballers[0]);
328
0
        goto out;
329
0
      }
330
0
    }
331
332
0
    if(time_to_start_next(cf, data, 1, now)) {
333
0
      cf_hc_baller_init(&ctx->ballers[1], cf, data, ctx->ballers[1].transport);
334
0
    }
335
336
0
    if((ctx->baller_count > 1) && cf_hc_baller_is_active(&ctx->ballers[1])) {
337
0
      CURL_TRC_CF(data, cf, "connect, check %s", ctx->ballers[1].name);
338
0
      result = cf_hc_baller_connect(&ctx->ballers[1], cf, data, done);
339
0
      if(!result && *done) {
340
0
        result = baller_connected(cf, data, &ctx->ballers[1]);
341
0
        goto out;
342
0
      }
343
0
    }
344
345
0
    failed_ballers = 0;
346
0
    for(i = 0; i < ctx->baller_count; i++) {
347
0
      if(ctx->ballers[i].result)
348
0
        ++failed_ballers;
349
0
    }
350
351
0
    if(failed_ballers == ctx->baller_count) {
352
      /* all have failed. we give up */
353
0
      CURL_TRC_CF(data, cf, "connect, all attempts failed");
354
0
      for(i = 0; i < ctx->baller_count; i++) {
355
0
        if(ctx->ballers[i].result) {
356
0
          result = ctx->ballers[i].result;
357
0
          break;
358
0
        }
359
0
      }
360
0
      ctx->state = CF_HC_FAILURE;
361
0
      goto out;
362
0
    }
363
0
    result = CURLE_OK;
364
0
    *done = FALSE;
365
0
    break;
366
367
0
  case CF_HC_FAILURE:
368
0
    result = ctx->result;
369
0
    cf->connected = FALSE;
370
0
    *done = FALSE;
371
0
    break;
372
373
0
  case CF_HC_SUCCESS:
374
0
    result = CURLE_OK;
375
0
    cf->connected = TRUE;
376
0
    *done = TRUE;
377
0
    break;
378
0
  }
379
380
0
out:
381
0
  CURL_TRC_CF(data, cf, "connect -> %d, done=%d", result, *done);
382
0
  return result;
383
0
}
384
385
static CURLcode cf_hc_shutdown(struct Curl_cfilter *cf,
386
                               struct Curl_easy *data, bool *done)
387
0
{
388
0
  struct cf_hc_ctx *ctx = cf->ctx;
389
0
  size_t i;
390
0
  CURLcode result = CURLE_OK;
391
392
0
  DEBUGASSERT(data);
393
0
  if(cf->connected) {
394
0
    *done = TRUE;
395
0
    return CURLE_OK;
396
0
  }
397
398
  /* shutdown all ballers that have not done so already. If one fails,
399
   * continue shutting down others until all are shutdown. */
400
0
  for(i = 0; i < ctx->baller_count; i++) {
401
0
    struct cf_hc_baller *b = &ctx->ballers[i];
402
0
    bool bdone = FALSE;
403
0
    if(!cf_hc_baller_is_active(b) || b->shutdown)
404
0
      continue;
405
0
    b->result = b->cf->cft->do_shutdown(b->cf, data, &bdone);
406
0
    if(b->result || bdone)
407
0
      b->shutdown = TRUE; /* treat a failed shutdown as done */
408
0
  }
409
410
0
  *done = TRUE;
411
0
  for(i = 0; i < ctx->baller_count; i++) {
412
0
    if(!ctx->ballers[i].shutdown)
413
0
      *done = FALSE;
414
0
  }
415
0
  if(*done) {
416
0
    for(i = 0; i < ctx->baller_count; i++) {
417
0
      if(ctx->ballers[i].result)
418
0
        result = ctx->ballers[i].result;
419
0
    }
420
0
  }
421
0
  CURL_TRC_CF(data, cf, "shutdown -> %d, done=%d", result, *done);
422
0
  return result;
423
0
}
424
425
static CURLcode cf_hc_adjust_pollset(struct Curl_cfilter *cf,
426
                                     struct Curl_easy *data,
427
                                     struct easy_pollset *ps)
428
0
{
429
0
  CURLcode result = CURLE_OK;
430
0
  if(!cf->connected) {
431
0
    struct cf_hc_ctx *ctx = cf->ctx;
432
0
    size_t i;
433
434
0
    for(i = 0; (i < ctx->baller_count) && !result; i++) {
435
0
      struct cf_hc_baller *b = &ctx->ballers[i];
436
0
      if(!cf_hc_baller_is_active(b))
437
0
        continue;
438
0
      result = Curl_conn_cf_adjust_pollset(b->cf, data, ps);
439
0
    }
440
0
    CURL_TRC_CF(data, cf, "adjust_pollset -> %d, %d socks", result, ps->n);
441
0
  }
442
0
  return result;
443
0
}
444
445
static bool cf_hc_data_pending(struct Curl_cfilter *cf,
446
                               const struct Curl_easy *data)
447
0
{
448
0
  struct cf_hc_ctx *ctx = cf->ctx;
449
0
  size_t i;
450
451
0
  if(cf->connected)
452
0
    return cf->next->cft->has_data_pending(cf->next, data);
453
454
0
  for(i = 0; i < ctx->baller_count; i++)
455
0
    if(cf_hc_baller_data_pending(&ctx->ballers[i], data))
456
0
      return TRUE;
457
0
  return FALSE;
458
0
}
459
460
static struct curltime cf_get_max_baller_time(struct Curl_cfilter *cf,
461
                                              struct Curl_easy *data,
462
                                              int query)
463
0
{
464
0
  struct cf_hc_ctx *ctx = cf->ctx;
465
0
  struct curltime t, tmax;
466
0
  size_t i;
467
468
0
  memset(&tmax, 0, sizeof(tmax));
469
0
  for(i = 0; i < ctx->baller_count; i++) {
470
0
    struct Curl_cfilter *cfb = ctx->ballers[i].cf;
471
0
    memset(&t, 0, sizeof(t));
472
0
    if(cfb && !cfb->cft->query(cfb, data, query, NULL, &t)) {
473
0
      if((t.tv_sec || t.tv_usec) && curlx_timediff_us(t, tmax) > 0)
474
0
        tmax = t;
475
0
    }
476
0
  }
477
0
  return tmax;
478
0
}
479
480
static CURLcode cf_hc_query(struct Curl_cfilter *cf,
481
                            struct Curl_easy *data,
482
                            int query, int *pres1, void *pres2)
483
0
{
484
0
  struct cf_hc_ctx *ctx = cf->ctx;
485
0
  size_t i;
486
487
0
  if(!cf->connected) {
488
0
    switch(query) {
489
0
    case CF_QUERY_TIMER_CONNECT: {
490
0
      struct curltime *when = pres2;
491
0
      *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_CONNECT);
492
0
      return CURLE_OK;
493
0
    }
494
0
    case CF_QUERY_TIMER_APPCONNECT: {
495
0
      struct curltime *when = pres2;
496
0
      *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_APPCONNECT);
497
0
      return CURLE_OK;
498
0
    }
499
0
    case CF_QUERY_NEED_FLUSH: {
500
0
      for(i = 0; i < ctx->baller_count; i++)
501
0
        if(cf_hc_baller_needs_flush(&ctx->ballers[i], data)) {
502
0
          *pres1 = TRUE;
503
0
          return CURLE_OK;
504
0
        }
505
0
      break;
506
0
    }
507
0
    default:
508
0
      break;
509
0
    }
510
0
  }
511
0
  return cf->next ?
512
0
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
513
0
    CURLE_UNKNOWN_OPTION;
514
0
}
515
516
static CURLcode cf_hc_cntrl(struct Curl_cfilter *cf,
517
                            struct Curl_easy *data,
518
                            int event, int arg1, void *arg2)
519
0
{
520
0
  struct cf_hc_ctx *ctx = cf->ctx;
521
0
  CURLcode result = CURLE_OK;
522
0
  size_t i;
523
524
0
  if(!cf->connected) {
525
0
    for(i = 0; i < ctx->baller_count; i++) {
526
0
      result = cf_hc_baller_cntrl(&ctx->ballers[i], data, event, arg1, arg2);
527
0
      if(result && (result != CURLE_AGAIN))
528
0
        goto out;
529
0
    }
530
0
    result = CURLE_OK;
531
0
  }
532
0
out:
533
0
  return result;
534
0
}
535
536
static void cf_hc_close(struct Curl_cfilter *cf, struct Curl_easy *data)
537
0
{
538
0
  CURL_TRC_CF(data, cf, "close");
539
0
  cf_hc_reset(cf, data);
540
0
  cf->connected = FALSE;
541
542
0
  if(cf->next) {
543
0
    cf->next->cft->do_close(cf->next, data);
544
0
    Curl_conn_cf_discard_chain(&cf->next, data);
545
0
  }
546
0
}
547
548
static void cf_hc_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
549
0
{
550
0
  struct cf_hc_ctx *ctx = cf->ctx;
551
552
0
  (void)data;
553
0
  CURL_TRC_CF(data, cf, "destroy");
554
0
  cf_hc_reset(cf, data);
555
0
  Curl_safefree(ctx);
556
0
}
557
558
struct Curl_cftype Curl_cft_http_connect = {
559
  "HTTPS-CONNECT",
560
  0,
561
  CURL_LOG_LVL_NONE,
562
  cf_hc_destroy,
563
  cf_hc_connect,
564
  cf_hc_close,
565
  cf_hc_shutdown,
566
  cf_hc_adjust_pollset,
567
  cf_hc_data_pending,
568
  Curl_cf_def_send,
569
  Curl_cf_def_recv,
570
  cf_hc_cntrl,
571
  Curl_cf_def_conn_is_alive,
572
  Curl_cf_def_conn_keep_alive,
573
  cf_hc_query,
574
};
575
576
static CURLcode cf_hc_create(struct Curl_cfilter **pcf,
577
                             struct Curl_easy *data,
578
                             enum alpnid *alpnids, size_t alpn_count,
579
                             uint8_t def_transport)
580
0
{
581
0
  struct Curl_cfilter *cf = NULL;
582
0
  struct cf_hc_ctx *ctx;
583
0
  CURLcode result = CURLE_OK;
584
0
  size_t i;
585
586
0
  ctx = curlx_calloc(1, sizeof(*ctx));
587
0
  if(!ctx) {
588
0
    result = CURLE_OUT_OF_MEMORY;
589
0
    goto out;
590
0
  }
591
592
0
  DEBUGASSERT(alpnids);
593
0
  DEBUGASSERT(alpn_count);
594
0
  DEBUGASSERT(alpn_count <= CURL_ARRAYSIZE(ctx->ballers));
595
0
  if(!alpn_count || (alpn_count > CURL_ARRAYSIZE(ctx->ballers))) {
596
0
    failf(data, "https-connect filter create with unsupported %zu ALPN ids",
597
0
          alpn_count);
598
0
    result = CURLE_FAILED_INIT;
599
0
    goto out;
600
0
  }
601
602
0
  for(i = 0; i < alpn_count; ++i)
603
0
    cf_hc_baller_assign(&ctx->ballers[i], alpnids[i], def_transport);
604
0
  for(; i < CURL_ARRAYSIZE(ctx->ballers); ++i)
605
0
    ctx->ballers[i].alpn_id = ALPN_none;
606
0
  ctx->baller_count = alpn_count;
607
608
0
  result = Curl_cf_create(&cf, &Curl_cft_http_connect, ctx);
609
0
  if(result)
610
0
    goto out;
611
0
  ctx = NULL;
612
0
  cf_hc_reset(cf, data);
613
614
0
out:
615
0
  *pcf = result ? NULL : cf;
616
0
  curlx_free(ctx);
617
0
  return result;
618
0
}
619
620
static CURLcode cf_http_connect_add(struct Curl_easy *data,
621
                                    struct connectdata *conn,
622
                                    int sockindex,
623
                                    enum alpnid *alpn_ids, size_t alpn_count,
624
                                    uint8_t def_transport)
625
0
{
626
0
  struct Curl_cfilter *cf;
627
0
  CURLcode result = CURLE_OK;
628
629
0
  DEBUGASSERT(data);
630
0
  result = cf_hc_create(&cf, data, alpn_ids, alpn_count, def_transport);
631
0
  if(result)
632
0
    goto out;
633
0
  Curl_conn_cf_add(data, conn, sockindex, cf);
634
0
out:
635
0
  return result;
636
0
}
637
638
static bool cf_https_alpns_contain(enum alpnid id,
639
                                   enum alpnid *list, size_t len)
640
0
{
641
0
  size_t i;
642
0
  for(i = 0; i < len; ++i) {
643
0
    if(id == list[i])
644
0
      return TRUE;
645
0
  }
646
0
  return FALSE;
647
0
}
648
649
CURLcode Curl_cf_https_setup(struct Curl_easy *data,
650
                             struct connectdata *conn,
651
                             int sockindex)
652
0
{
653
0
  enum alpnid alpn_ids[2];
654
0
  size_t alpn_count = 0;
655
0
  CURLcode result = CURLE_OK;
656
0
  struct Curl_cfilter cf_fake, *cf = NULL;
657
658
0
  (void)sockindex;
659
  /* we want to log for the filter before we create it, fake it. */
660
0
  memset(&cf_fake, 0, sizeof(cf_fake));
661
0
  cf_fake.cft = &Curl_cft_http_connect;
662
0
  cf = &cf_fake;
663
664
0
  if(conn->bits.tls_enable_alpn) {
665
#ifdef USE_HTTPSRR
666
    /* Is there an HTTPSRR use its ALPNs here.
667
     * We are here after having selected a connection to a host+port and
668
     * can no longer change that. Any HTTPSRR advice for other hosts and ports
669
     * we need to ignore. */
670
    struct Curl_dns_entry *dns = data->state.dns[sockindex];
671
    struct Curl_https_rrinfo *rr = dns ? dns->hinfo : NULL;
672
    if(rr && !rr->no_def_alpn &&  /* ALPNs are defaults */
673
       (!rr->target ||      /* for same host */
674
        !rr->target[0] ||
675
        (rr->target[0] == '.' &&
676
         !rr->target[1])) &&
677
       (rr->port < 0 ||    /* for same port */
678
        rr->port == conn->remote_port)) {
679
      size_t i;
680
      for(i = 0; i < CURL_ARRAYSIZE(rr->alpns) &&
681
                 alpn_count < CURL_ARRAYSIZE(alpn_ids); ++i) {
682
        enum alpnid alpn = rr->alpns[i];
683
        if(cf_https_alpns_contain(alpn, alpn_ids, alpn_count))
684
          continue;
685
        switch(alpn) {
686
        case ALPN_h3:
687
          if(Curl_conn_may_http3(data, conn, conn->transport_wanted))
688
            break;  /* not possible */
689
          if(data->state.http_neg.allowed & CURL_HTTP_V3x) {
690
            CURL_TRC_CF(data, cf, "adding h3 via HTTPS-RR");
691
            alpn_ids[alpn_count++] = alpn;
692
          }
693
          break;
694
        case ALPN_h2:
695
          if(data->state.http_neg.allowed & CURL_HTTP_V2x) {
696
            CURL_TRC_CF(data, cf, "adding h2 via HTTPS-RR");
697
            alpn_ids[alpn_count++] = alpn;
698
          }
699
          break;
700
        case ALPN_h1:
701
          if(data->state.http_neg.allowed & CURL_HTTP_V1x) {
702
            CURL_TRC_CF(data, cf, "adding h1 via HTTPS-RR");
703
            alpn_ids[alpn_count++] = alpn;
704
          }
705
          break;
706
        default: /* ignore */
707
          break;
708
        }
709
      }
710
    }
711
#endif
712
713
0
    if((alpn_count < CURL_ARRAYSIZE(alpn_ids)) &&
714
0
       (data->state.http_neg.wanted & CURL_HTTP_V3x) &&
715
0
       !cf_https_alpns_contain(ALPN_h3, alpn_ids, alpn_count)) {
716
0
      result = Curl_conn_may_http3(data, conn, conn->transport_wanted);
717
0
      if(!result) {
718
0
        CURL_TRC_CF(data, cf, "adding wanted h3");
719
0
        alpn_ids[alpn_count++] = ALPN_h3;
720
0
      }
721
0
      else if(data->state.http_neg.wanted == CURL_HTTP_V3x)
722
0
        goto out; /* only h3 allowed, not possible, error out */
723
0
    }
724
0
    if((alpn_count < CURL_ARRAYSIZE(alpn_ids)) &&
725
0
       (data->state.http_neg.wanted & CURL_HTTP_V2x) &&
726
0
       !cf_https_alpns_contain(ALPN_h2, alpn_ids, alpn_count)) {
727
0
      CURL_TRC_CF(data, cf, "adding wanted h2");
728
0
      alpn_ids[alpn_count++] = ALPN_h2;
729
0
    }
730
0
    else if((alpn_count < CURL_ARRAYSIZE(alpn_ids)) &&
731
0
            (data->state.http_neg.wanted & CURL_HTTP_V1x) &&
732
0
            !cf_https_alpns_contain(ALPN_h1, alpn_ids, alpn_count)) {
733
0
      CURL_TRC_CF(data, cf, "adding wanted h1");
734
0
      alpn_ids[alpn_count++] = ALPN_h1;
735
0
    }
736
0
  }
737
738
  /* If we identified ALPNs to use, install our filter. Otherwise,
739
   * install nothing, so our call will use a default connect setup. */
740
0
  if(alpn_count) {
741
0
    result = cf_http_connect_add(data, conn, sockindex,
742
0
                                 alpn_ids, alpn_count,
743
0
                                 conn->transport_wanted);
744
0
  }
745
746
0
out:
747
0
  return result;
748
0
}
749
750
#endif /* !CURL_DISABLE_HTTP */