Coverage Report

Created: 2026-10-22 06:56

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