Coverage Report

Created: 2026-07-16 06:10

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
#include "curl_setup.h"
25
26
#ifndef CURL_DISABLE_HTTP
27
28
#include "urldata.h"
29
#include "curl_trc.h"
30
#include "cfilters.h"
31
#include "cf-dns.h"
32
#include "cf-setup.h"
33
#include "connect.h"
34
#include "hostip.h"
35
#include "httpsrr.h"
36
#include "multiif.h"
37
#include "cf-https-connect.h"
38
#include "http2.h"
39
#include "progress.h"
40
#include "select.h"
41
#include "vquic/vquic.h"
42
43
typedef enum {
44
  CF_HC_RESOLV,
45
  CF_HC_INIT,
46
  CF_HC_CONNECT,
47
  CF_HC_SUCCESS,
48
  CF_HC_FAILURE
49
} cf_hc_state;
50
51
struct cf_hc_baller {
52
  const char *name;
53
  struct Curl_cfilter *cf;
54
  CURLcode result;
55
  struct curltime started;
56
  int reply_ms;
57
  uint8_t transport;
58
  enum alpnid alpn_id;
59
  BIT(shutdown);
60
};
61
62
static void cf_hc_baller_discard(struct cf_hc_baller *b,
63
                                 struct Curl_easy *data)
64
0
{
65
0
  if(b->cf) {
66
0
    Curl_conn_cf_discard_chain(&b->cf, data);
67
0
    b->cf = NULL;
68
0
  }
69
0
}
70
71
static bool cf_hc_baller_is_connecting(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
  CURLcode check_h3_result;
116
  struct cf_hc_baller ballers[2];
117
  size_t baller_count;
118
  timediff_t soft_eyeballs_timeout_ms;
119
  timediff_t hard_eyeballs_timeout_ms;
120
  uint8_t def_transport;
121
  BIT(httpsrr_resolved);
122
  BIT(checked_h3);
123
  BIT(ballers_complete);
124
};
125
126
static void cf_hc_ctx_close(struct Curl_easy *data,
127
                            struct cf_hc_ctx *ctx)
128
0
{
129
0
  if(ctx) {
130
0
    size_t i;
131
0
    for(i = 0; i < ctx->baller_count; ++i)
132
0
      cf_hc_baller_discard(&ctx->ballers[i], data);
133
0
  }
134
0
}
135
136
static void cf_hc_ctx_destroy(struct Curl_easy *data,
137
                              struct cf_hc_ctx *ctx)
138
0
{
139
0
  if(ctx) {
140
0
    cf_hc_ctx_close(data, ctx);
141
0
    curlx_free(ctx);
142
0
  }
143
0
}
144
145
static void cf_hc_baller_assign(struct cf_hc_baller *b,
146
                                enum alpnid alpn_id,
147
                                uint8_t def_transport)
148
0
{
149
0
  b->alpn_id = alpn_id;
150
0
  b->transport = def_transport;
151
0
  b->cf = NULL;
152
0
  b->result = CURLE_OK;
153
0
  b->reply_ms = -1;
154
0
  b->shutdown = FALSE;
155
0
  switch(b->alpn_id) {
156
0
  case ALPN_h3:
157
0
    b->name = "h3";
158
0
    b->transport = TRNSPRT_QUIC;
159
0
    break;
160
0
  case ALPN_h2:
161
0
    b->name = "h2";
162
0
    break;
163
0
  case ALPN_h1:
164
0
    b->name = "h1";
165
0
    break;
166
0
  case ALPN_none:
167
0
    b->name = "no-alpn";
168
0
    break;
169
0
  default:
170
0
    b->result = CURLE_FAILED_INIT;
171
0
    break;
172
0
  }
173
0
}
174
175
static void cf_hc_baller_init(struct cf_hc_baller *b,
176
                              struct Curl_cfilter *cf,
177
                              struct Curl_easy *data)
178
0
{
179
0
  struct Curl_cfilter *save = cf->next;
180
181
0
  cf->next = NULL;
182
0
  b->started = *Curl_pgrs_now(data);
183
0
  b->result = Curl_cf_setup_insert_after(cf, data, b->transport,
184
0
                                         CURL_CF_SSL_ENABLE);
185
0
  b->cf = cf->next;
186
0
  cf->next = save;
187
0
}
188
189
static CURLcode cf_hc_baller_connect(struct cf_hc_baller *b,
190
                                     struct Curl_cfilter *cf,
191
                                     struct Curl_easy *data,
192
                                     bool *done)
193
0
{
194
0
  struct Curl_cfilter *save = cf->next;
195
196
0
  cf->next = b->cf;
197
0
  b->result = Curl_conn_cf_connect(cf->next, data, done);
198
0
  b->cf = cf->next; /* it might mutate */
199
0
  cf->next = save;
200
0
  return b->result;
201
0
}
202
203
static CURLcode baller_connected(struct Curl_cfilter *cf,
204
                                 struct Curl_easy *data,
205
                                 struct cf_hc_baller *winner)
206
0
{
207
0
  struct cf_hc_ctx *ctx = cf->ctx;
208
209
  /* Make the winner's connection filter out own sub-filter, check, move,
210
   * close all remaining. */
211
0
  if(cf->next) {
212
0
    DEBUGASSERT(0);
213
0
    return CURLE_FAILED_INIT;
214
0
  }
215
0
  if(!winner->cf) {
216
0
    DEBUGASSERT(0);
217
0
    return CURLE_FAILED_INIT;
218
0
  }
219
220
0
  cf->next = winner->cf;
221
0
  winner->cf = NULL;
222
0
  ctx->state = CF_HC_SUCCESS;
223
0
  cf->connected = TRUE;
224
225
0
  cf_hc_ctx_close(data, ctx);
226
  /* ballers may have failf()'d, the winner resets it, so our
227
   * errorbuf is clean again. */
228
0
  Curl_reset_fail(data);
229
230
0
#ifdef USE_NGHTTP2
231
0
  {
232
    /* For a negotiated HTTP/2 connection insert the h2 filter. */
233
0
    const char *alpn = Curl_conn_cf_get_alpn_negotiated(cf->next, data);
234
0
    if(alpn && !strcmp("h2", alpn)) {
235
0
      CURLcode result = Curl_http2_switch_at(cf, data);
236
0
      if(result) {
237
0
        ctx->state = CF_HC_FAILURE;
238
0
        ctx->result = result;
239
0
        return result;
240
0
      }
241
0
    }
242
0
  }
243
0
#endif
244
0
  return CURLE_OK;
245
0
}
246
247
static bool time_to_start_baller2(struct Curl_cfilter *cf,
248
                                  struct Curl_easy *data)
249
0
{
250
0
  struct cf_hc_ctx *ctx = cf->ctx;
251
0
  timediff_t elapsed_ms;
252
253
0
  if(ctx->baller_count < 2)
254
0
    return FALSE;
255
0
  else if(cf_hc_baller_has_started(&ctx->ballers[1]))
256
0
    return FALSE;
257
0
  else if(ctx->ballers[0].result) {
258
0
    CURL_TRC_CF(data, cf, "%s baller failed, starting %s",
259
0
                ctx->ballers[0].name, ctx->ballers[1].name);
260
0
    return TRUE;
261
0
  }
262
263
0
  elapsed_ms = curlx_ptimediff_ms(Curl_pgrs_now(data), &ctx->started);
264
0
  if(elapsed_ms >= ctx->hard_eyeballs_timeout_ms) {
265
0
    CURL_TRC_CF(data, cf, "%s inconclusive after %" FMT_TIMEDIFF_T ", "
266
0
                "starting %s", ctx->ballers[0].name,
267
0
                ctx->hard_eyeballs_timeout_ms, ctx->ballers[1].name);
268
0
    return TRUE;
269
0
  }
270
0
  else if(elapsed_ms >= ctx->soft_eyeballs_timeout_ms) {
271
0
    if(cf_hc_baller_reply_ms(&ctx->ballers[0], data) < 0) {
272
0
      CURL_TRC_CF(data, cf, "%s has not seen any data after %"
273
0
                  FMT_TIMEDIFF_T "ms, starting %s",
274
0
                  ctx->ballers[0].name, ctx->soft_eyeballs_timeout_ms,
275
0
                  ctx->ballers[1].name);
276
0
      return TRUE;
277
0
    }
278
0
  }
279
0
  return FALSE;
280
0
}
281
282
static bool cf_hc_may_h3(struct Curl_cfilter *cf,
283
                         struct Curl_easy *data)
284
0
{
285
0
  struct cf_hc_ctx *ctx = cf->ctx;
286
0
  if(!ctx->checked_h3) {
287
0
    ctx->check_h3_result =
288
0
      Curl_conn_may_http3(data, cf->conn, ctx->def_transport);
289
0
    ctx->checked_h3 = TRUE;
290
0
  }
291
0
  return !ctx->check_h3_result;
292
0
}
293
294
static enum alpnid cf_hc_get_httpsrr_alpn(struct Curl_cfilter *cf,
295
                                          struct Curl_easy *data,
296
                                          enum alpnid not_this_one)
297
0
{
298
#ifdef USE_HTTPSRR
299
  /* Is there an HTTPSRR use its ALPNs here.
300
   * We are here after having selected a connection to a host+port and
301
   * can no longer change that. Any HTTPSRR advice for other hosts and ports
302
   * we need to ignore. */
303
  const struct Curl_https_rrinfo *rr;
304
  size_t i;
305
306
  /* Do we have HTTPS-RR information? */
307
  rr = Curl_conn_dns_get_https(
308
    data, cf->sockindex, Curl_conn_get_destination(cf->conn, cf->sockindex));
309
310
  /* We do not support `rr->no_def_alpn`. */
311
  if(Curl_httpsrr_applicable(data, rr) && !rr->no_def_alpn) {
312
    for(i = 0; i < CURL_ARRAYSIZE(rr->alpns); ++i) {
313
      enum alpnid alpn_rr = (enum alpnid)rr->alpns[i];
314
      if(alpn_rr == not_this_one) /* do not want this one */
315
        continue;
316
      switch(alpn_rr) {
317
      case ALPN_h3:
318
        if((data->state.http_neg.allowed & CURL_HTTP_V3x) &&
319
           cf_hc_may_h3(cf, data)) {
320
          return alpn_rr;
321
        }
322
        break;
323
      case ALPN_h2:
324
        if(data->state.http_neg.allowed & CURL_HTTP_V2x) {
325
          return alpn_rr;
326
        }
327
        break;
328
      case ALPN_h1:
329
        if(data->state.http_neg.allowed & CURL_HTTP_V1x) {
330
          return alpn_rr;
331
        }
332
        break;
333
      default: /* ignore */
334
        break;
335
      }
336
    }
337
  }
338
#else
339
0
  (void)cf;
340
0
  (void)data;
341
0
  (void)not_this_one;
342
0
#endif
343
0
  return ALPN_none;
344
0
}
345
346
static enum alpnid cf_hc_get_pref_alpn(struct Curl_cfilter *cf,
347
                                       struct Curl_easy *data,
348
                                       enum alpnid not_this_one)
349
0
{
350
0
  if((data->state.http_neg.preferred & data->state.http_neg.allowed)) {
351
0
    switch(data->state.http_neg.preferred) {
352
0
    case CURL_HTTP_V3x:
353
0
      if(cf_hc_may_h3(cf, data) && (ALPN_h3 != not_this_one))
354
0
        return ALPN_h3;
355
0
      break;
356
0
    case CURL_HTTP_V2x:
357
0
      if(ALPN_h2 != not_this_one)
358
0
        return ALPN_h2;
359
0
      break;
360
0
    case CURL_HTTP_V1x:
361
      /* If we are trying h2 already, h1 is already used as fallback */
362
0
      if((ALPN_h1 != not_this_one) && (ALPN_h2 != not_this_one))
363
0
        return ALPN_h1;
364
0
      break;
365
0
    default:
366
0
      break;
367
0
    }
368
0
  }
369
0
  return ALPN_none;
370
0
}
371
372
static enum alpnid cf_hc_get_first_alpn(struct Curl_cfilter *cf,
373
                                        struct Curl_easy *data,
374
                                        http_majors choices,
375
                                        enum alpnid not_this_one)
376
0
{
377
  /* When told to not try h2, we also do not try h1 and vice versa */
378
0
  bool allow_h1_or_h2 = (not_this_one != ALPN_h1) &&
379
0
                        (not_this_one != ALPN_h2);
380
0
  if((ALPN_h3 != not_this_one) && (choices & CURL_HTTP_V3x) &&
381
0
     cf_hc_may_h3(cf, data)) {
382
0
    return ALPN_h3;
383
0
  }
384
0
  if(allow_h1_or_h2 && (choices & CURL_HTTP_V2x)) {
385
0
    return ALPN_h2;
386
0
  }
387
0
  if(allow_h1_or_h2 && (choices & CURL_HTTP_V1x)) {
388
0
    return ALPN_h1;
389
0
  }
390
0
  return ALPN_none;
391
0
}
392
393
static CURLcode cf_hc_set_baller1(struct Curl_cfilter *cf,
394
                                  struct Curl_easy *data)
395
0
{
396
0
  struct cf_hc_ctx *ctx = cf->ctx;
397
0
  enum alpnid alpn1 = ALPN_none;
398
0
  VERBOSE(const char *source = "HTTPS-RR");
399
400
0
  DEBUGASSERT(cf->conn->bits.tls_enable_alpn);
401
402
0
  alpn1 = cf_hc_get_httpsrr_alpn(cf, data, ALPN_none);
403
0
  if(alpn1 == ALPN_none) {
404
    /* preference is configured and allowed, can we use it? */
405
0
    VERBOSE(source = "preferred version");
406
0
    alpn1 = cf_hc_get_pref_alpn(cf, data, ALPN_none);
407
0
  }
408
0
  if(alpn1 == ALPN_none) {
409
0
    VERBOSE(source = "wanted versions");
410
0
    alpn1 = cf_hc_get_first_alpn(cf, data,
411
0
                                 data->state.http_neg.wanted,
412
0
                                 ALPN_none);
413
0
  }
414
0
  if(alpn1 == ALPN_none) {
415
0
    VERBOSE(source = "allowed versions");
416
0
    alpn1 = cf_hc_get_first_alpn(cf, data,
417
0
                                 data->state.http_neg.allowed,
418
0
                                 ALPN_none);
419
0
  }
420
421
0
  if(alpn1 == ALPN_none) {
422
    /* None of the wanted/allowed HTTP versions could be chosen */
423
0
    if(ctx->check_h3_result) {
424
0
      CURL_TRC_CF(data, cf, "unable to use HTTP/3");
425
0
      return ctx->check_h3_result;
426
0
    }
427
0
    CURL_TRC_CF(data, cf, "unable to select HTTP version");
428
0
    return CURLE_FAILED_INIT;
429
0
  }
430
431
0
  cf_hc_baller_assign(&ctx->ballers[0], alpn1, ctx->def_transport);
432
0
  ctx->baller_count = 1;
433
0
  CURL_TRC_CF(data, cf, "1st attempt uses %s from %s",
434
0
              ctx->ballers[0].name, source);
435
436
0
  switch(alpn1) {
437
0
  case ALPN_h1:
438
    /* We really want h1, switch off h2 to make it disappear in ALPN */
439
0
    data->state.http_neg.wanted &= (uint8_t)~CURL_HTTP_V2x;
440
0
    break;
441
0
  default:
442
0
    break;
443
0
  }
444
445
0
  return CURLE_OK;
446
0
}
447
448
static void cf_hc_set_baller2(struct Curl_cfilter *cf,
449
                              struct Curl_easy *data)
450
0
{
451
0
  struct cf_hc_ctx *ctx = cf->ctx;
452
0
  enum alpnid alpn2 = ALPN_none, alpn1 = ctx->ballers[0].alpn_id;
453
0
  VERBOSE(const char *source = "HTTPS-RR");
454
455
0
  if(ctx->ballers_complete)
456
0
    return; /* already done */
457
0
  if(!ctx->httpsrr_resolved)
458
0
    return; /* HTTPS-RR pending */
459
460
0
  alpn2 = cf_hc_get_httpsrr_alpn(cf, data, alpn1);
461
0
  if(alpn2 == ALPN_none) {
462
    /* preference is configured and allowed, can we use it? */
463
0
    VERBOSE(source = "preferred version");
464
0
    alpn2 = cf_hc_get_pref_alpn(cf, data, alpn1);
465
0
  }
466
0
  if(alpn2 == ALPN_none) {
467
0
    VERBOSE(source = "wanted versions");
468
0
    alpn2 = cf_hc_get_first_alpn(cf, data,
469
0
                                 data->state.http_neg.wanted,
470
0
                                 alpn1);
471
0
  }
472
473
0
  if(alpn2 != ALPN_none) {
474
0
    cf_hc_baller_assign(&ctx->ballers[1], alpn2, ctx->def_transport);
475
0
    ctx->baller_count = 2;
476
0
    CURL_TRC_CF(data, cf, "2nd attempt uses %s from %s",
477
0
                ctx->ballers[1].name, source);
478
0
  }
479
0
  ctx->ballers_complete = TRUE;
480
0
}
481
482
static CURLcode cf_hc_connect(struct Curl_cfilter *cf,
483
                              struct Curl_easy *data,
484
                              bool *done)
485
0
{
486
0
  struct cf_hc_ctx *ctx = cf->ctx;
487
0
  CURLcode result = CURLE_OK;
488
489
0
  if(cf->connected) {
490
0
    *done = TRUE;
491
0
    return CURLE_OK;
492
0
  }
493
494
0
  *done = FALSE;
495
496
0
  if(!ctx->httpsrr_resolved) {
497
0
    ctx->httpsrr_resolved = Curl_conn_dns_resolved_https(
498
0
      data, cf->sockindex, Curl_conn_get_destination(cf->conn, cf->sockindex));
499
0
#ifdef DEBUGBUILD
500
0
    if(!ctx->httpsrr_resolved && getenv("CURL_DBG_AWAIT_HTTPSRR")) {
501
0
      CURL_TRC_CF(data, cf, "awaiting HTTPS-RR");
502
0
      return CURLE_OK;
503
0
    }
504
0
#endif
505
0
  }
506
507
0
  switch(ctx->state) {
508
0
  case CF_HC_RESOLV:
509
0
    ctx->state = CF_HC_INIT;
510
0
    FALLTHROUGH();
511
512
0
  case CF_HC_INIT:
513
0
    DEBUGASSERT(!cf->next);
514
0
    CURL_TRC_CF(data, cf, "connect, init");
515
0
    result = cf_hc_set_baller1(cf, data);
516
0
    if(result) {
517
0
      ctx->result = result;
518
0
      ctx->state = CF_HC_FAILURE;
519
0
      goto out;
520
0
    }
521
0
    cf_hc_set_baller2(cf, data);
522
0
    ctx->started = *Curl_pgrs_now(data);
523
0
    cf_hc_baller_init(&ctx->ballers[0], cf, data);
524
0
    if((ctx->baller_count > 1) || !ctx->ballers_complete) {
525
0
      Curl_expire(data, ctx->soft_eyeballs_timeout_ms, EXPIRE_ALPN_EYEBALLS);
526
0
    }
527
0
    ctx->state = CF_HC_CONNECT;
528
0
    FALLTHROUGH();
529
530
0
  case CF_HC_CONNECT:
531
0
    if(!ctx->ballers_complete)
532
0
      cf_hc_set_baller2(cf, data);
533
534
0
    if(cf_hc_baller_is_connecting(&ctx->ballers[0])) {
535
0
      result = cf_hc_baller_connect(&ctx->ballers[0], cf, data, done);
536
0
      if(!result && *done) {
537
0
        result = baller_connected(cf, data, &ctx->ballers[0]);
538
0
        goto out;
539
0
      }
540
0
    }
541
542
0
    if(time_to_start_baller2(cf, data)) {
543
0
      cf_hc_baller_init(&ctx->ballers[1], cf, data);
544
0
    }
545
546
0
    if(cf_hc_baller_is_connecting(&ctx->ballers[1])) {
547
0
      result = cf_hc_baller_connect(&ctx->ballers[1], cf, data, done);
548
0
      if(!result && *done) {
549
0
        result = baller_connected(cf, data, &ctx->ballers[1]);
550
0
        goto out;
551
0
      }
552
0
    }
553
554
0
    if(ctx->ballers[0].result &&
555
0
       (ctx->ballers[1].result ||
556
0
        (ctx->ballers_complete && (ctx->baller_count < 2)))) {
557
      /* all have failed. we give up */
558
0
      CURL_TRC_CF(data, cf, "connect, all attempts failed");
559
0
      ctx->result = result = ctx->ballers[0].result;
560
0
      ctx->state = CF_HC_FAILURE;
561
0
      goto out;
562
0
    }
563
0
    result = CURLE_OK;
564
0
    *done = FALSE;
565
0
    break;
566
567
0
  case CF_HC_FAILURE:
568
0
    result = ctx->result;
569
0
    cf->connected = FALSE;
570
0
    *done = FALSE;
571
0
    break;
572
573
0
  case CF_HC_SUCCESS:
574
0
    result = CURLE_OK;
575
0
    cf->connected = TRUE;
576
0
    *done = TRUE;
577
0
    break;
578
0
  }
579
580
0
out:
581
0
  CURL_TRC_CF(data, cf, "connect -> %d, done=%d", (int)result, *done);
582
0
  return result;
583
0
}
584
585
static CURLcode cf_hc_shutdown(struct Curl_cfilter *cf,
586
                               struct Curl_easy *data, bool *done)
587
0
{
588
0
  struct cf_hc_ctx *ctx = cf->ctx;
589
0
  size_t i;
590
0
  CURLcode result = CURLE_OK;
591
592
0
  DEBUGASSERT(data);
593
0
  if(cf->connected) {
594
0
    *done = TRUE;
595
0
    return CURLE_OK;
596
0
  }
597
598
  /* shutdown all ballers that have not done so already. If one fails,
599
   * continue shutting down others until all are shutdown. */
600
0
  for(i = 0; i < ctx->baller_count; i++) {
601
0
    struct cf_hc_baller *b = &ctx->ballers[i];
602
0
    bool bdone = FALSE;
603
0
    if(!cf_hc_baller_is_connecting(b) || b->shutdown)
604
0
      continue;
605
0
    b->result = b->cf->cft->do_shutdown(b->cf, data, &bdone);
606
0
    if(b->result || bdone)
607
0
      b->shutdown = TRUE; /* treat a failed shutdown as done */
608
0
  }
609
610
0
  *done = TRUE;
611
0
  for(i = 0; i < ctx->baller_count; i++) {
612
0
    if(!ctx->ballers[i].shutdown)
613
0
      *done = FALSE;
614
0
  }
615
0
  if(*done) {
616
0
    for(i = 0; i < ctx->baller_count; i++) {
617
0
      if(ctx->ballers[i].result)
618
0
        result = ctx->ballers[i].result;
619
0
    }
620
0
  }
621
0
  CURL_TRC_CF(data, cf, "shutdown -> %d, done=%d", (int)result, *done);
622
0
  return result;
623
0
}
624
625
static CURLcode cf_hc_adjust_pollset(struct Curl_cfilter *cf,
626
                                     struct Curl_easy *data,
627
                                     struct easy_pollset *ps)
628
0
{
629
0
  CURLcode result = CURLE_OK;
630
0
  if(!cf->connected) {
631
0
    struct cf_hc_ctx *ctx = cf->ctx;
632
0
    size_t i;
633
634
0
    for(i = 0; (i < ctx->baller_count) && !result; i++) {
635
0
      struct cf_hc_baller *b = &ctx->ballers[i];
636
0
      if(!cf_hc_baller_is_connecting(b))
637
0
        continue;
638
0
      result = Curl_conn_cf_adjust_pollset(b->cf, data, ps);
639
0
    }
640
0
    CURL_TRC_CF(data, cf, "adjust_pollset -> %d, %u socks", (int)result,
641
0
                ps->n);
642
0
  }
643
0
  return result;
644
0
}
645
646
static bool cf_hc_data_pending(struct Curl_cfilter *cf,
647
                               const struct Curl_easy *data)
648
0
{
649
0
  struct cf_hc_ctx *ctx = cf->ctx;
650
0
  size_t i;
651
652
0
  if(cf->connected)
653
0
    return cf->next->cft->has_data_pending(cf->next, data);
654
655
0
  for(i = 0; i < ctx->baller_count; i++)
656
0
    if(cf_hc_baller_data_pending(&ctx->ballers[i], data))
657
0
      return TRUE;
658
0
  return FALSE;
659
0
}
660
661
static struct curltime cf_get_max_baller_time(struct Curl_cfilter *cf,
662
                                              struct Curl_easy *data,
663
                                              int query)
664
0
{
665
0
  struct cf_hc_ctx *ctx = cf->ctx;
666
0
  struct curltime t, tmax;
667
0
  size_t i;
668
669
0
  memset(&tmax, 0, sizeof(tmax));
670
0
  for(i = 0; i < ctx->baller_count; i++) {
671
0
    struct Curl_cfilter *cfb = ctx->ballers[i].cf;
672
0
    memset(&t, 0, sizeof(t));
673
0
    if(cfb && !cfb->cft->query(cfb, data, query, NULL, &t)) {
674
0
      if((t.tv_sec || t.tv_usec) && curlx_ptimediff_us(&t, &tmax) > 0)
675
0
        tmax = t;
676
0
    }
677
0
  }
678
0
  return tmax;
679
0
}
680
681
static CURLcode cf_hc_query(struct Curl_cfilter *cf,
682
                            struct Curl_easy *data,
683
                            int query, int *pres1, void *pres2)
684
0
{
685
0
  struct cf_hc_ctx *ctx = cf->ctx;
686
0
  size_t i;
687
688
0
  if(!cf->connected) {
689
0
    switch(query) {
690
0
    case CF_QUERY_TIMER_CONNECT: {
691
0
      struct curltime *when = pres2;
692
0
      *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_CONNECT);
693
0
      return CURLE_OK;
694
0
    }
695
0
    case CF_QUERY_TIMER_APPCONNECT: {
696
0
      struct curltime *when = pres2;
697
0
      *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_APPCONNECT);
698
0
      return CURLE_OK;
699
0
    }
700
0
    case CF_QUERY_NEED_FLUSH: {
701
0
      for(i = 0; i < ctx->baller_count; i++)
702
0
        if(cf_hc_baller_needs_flush(&ctx->ballers[i], data)) {
703
0
          *pres1 = TRUE;
704
0
          return CURLE_OK;
705
0
        }
706
0
      break;
707
0
    }
708
0
    default:
709
0
      break;
710
0
    }
711
0
  }
712
0
  return cf->next ?
713
0
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
714
0
    CURLE_UNKNOWN_OPTION;
715
0
}
716
717
static CURLcode cf_hc_cntrl(struct Curl_cfilter *cf,
718
                            struct Curl_easy *data,
719
                            int event, int arg1, void *arg2)
720
0
{
721
0
  struct cf_hc_ctx *ctx = cf->ctx;
722
0
  CURLcode result = CURLE_OK;
723
0
  size_t i;
724
725
0
  if(!cf->connected) {
726
0
    for(i = 0; i < ctx->baller_count; i++) {
727
0
      result = cf_hc_baller_cntrl(&ctx->ballers[i], data, event, arg1, arg2);
728
0
      if(result && (result != CURLE_AGAIN))
729
0
        goto out;
730
0
    }
731
0
    result = CURLE_OK;
732
0
  }
733
0
out:
734
0
  return result;
735
0
}
736
737
static void cf_hc_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
738
0
{
739
0
  struct cf_hc_ctx *ctx = cf->ctx;
740
741
0
  CURL_TRC_CF(data, cf, "destroy");
742
0
  cf_hc_ctx_destroy(data, ctx);
743
0
}
744
745
struct Curl_cftype Curl_cft_http_connect = {
746
  "HTTPS-CONNECT",
747
  CF_TYPE_SETUP | CF_TYPE_HTTPSRR,
748
  CURL_LOG_LVL_NONE,
749
  cf_hc_destroy,
750
  cf_hc_connect,
751
  cf_hc_shutdown,
752
  cf_hc_adjust_pollset,
753
  cf_hc_data_pending,
754
  Curl_cf_def_send,
755
  Curl_cf_def_recv,
756
  cf_hc_cntrl,
757
  Curl_cf_def_conn_is_alive,
758
  Curl_cf_def_conn_keep_alive,
759
  cf_hc_query,
760
};
761
762
static CURLcode cf_hc_create(struct Curl_cfilter **pcf,
763
                             struct Curl_easy *data,
764
                             uint8_t def_transport)
765
0
{
766
0
  struct Curl_cfilter *cf = NULL;
767
0
  struct cf_hc_ctx *ctx;
768
0
  CURLcode result = CURLE_OK;
769
770
0
  ctx = curlx_calloc(1, sizeof(*ctx));
771
0
  if(!ctx) {
772
0
    result = CURLE_OUT_OF_MEMORY;
773
0
    goto out;
774
0
  }
775
0
  ctx->def_transport = def_transport;
776
0
  ctx->hard_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout;
777
0
  ctx->soft_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout / 2;
778
779
0
  result = Curl_cf_create(&cf, &Curl_cft_http_connect, ctx);
780
0
  if(result)
781
0
    goto out;
782
0
  ctx = NULL;
783
784
0
out:
785
0
  *pcf = result ? NULL : cf;
786
0
  cf_hc_ctx_destroy(data, ctx);
787
0
  return result;
788
0
}
789
790
static CURLcode cf_hc_add(struct Curl_easy *data,
791
                          struct connectdata *conn,
792
                          int sockindex,
793
                          uint8_t def_transport)
794
0
{
795
0
  struct Curl_cfilter *cf;
796
0
  CURLcode result = CURLE_OK;
797
798
0
  DEBUGASSERT(data);
799
0
  result = cf_hc_create(&cf, data, def_transport);
800
0
  if(result)
801
0
    goto out;
802
0
  Curl_conn_cf_add(data, conn, sockindex, cf);
803
0
out:
804
0
  return result;
805
0
}
806
807
CURLcode Curl_cf_https_setup(struct Curl_easy *data,
808
                             struct connectdata *conn,
809
                             int sockindex)
810
0
{
811
0
  CURLcode result = CURLE_OK;
812
813
0
  DEBUGASSERT(conn->scheme->protocol == CURLPROTO_HTTPS);
814
815
  /* This filter is intended for HTTPS using ALPN and does
816
   * not support HTTPS Eyeballing to a proxy. */
817
0
  if((conn->scheme->protocol != CURLPROTO_HTTPS) ||
818
0
#ifndef CURL_DISABLE_PROXY
819
0
     conn->bits.origin_is_proxy ||
820
0
#endif
821
0
     !conn->bits.tls_enable_alpn)
822
0
    goto out;
823
824
0
  result = cf_hc_add(data, conn, sockindex, conn->transport_wanted);
825
826
0
out:
827
0
  return result;
828
0
}
829
830
#endif /* !CURL_DISABLE_HTTP */