Coverage Report

Created: 2026-09-04 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/http_proxy.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
#include "http_proxy.h"
27
28
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_PROXY)
29
30
#include "curl_trc.h"
31
#include "http.h"
32
#include "url.h"
33
#include "cfilters.h"
34
#include "cf-h1-proxy.h"
35
#include "cf-h2-proxy.h"
36
#include "connect.h"
37
#include "vauth/vauth.h"
38
#include "vquic/vquic.h"
39
#include "curlx/strparse.h"
40
41
static CURLcode dynhds_add_custom(struct Curl_easy *data,
42
                                  bool is_connect, int httpversion,
43
                                  bool is_udp, struct dynhds *hds)
44
1.96k
{
45
1.96k
  struct connectdata *conn = data->conn;
46
1.96k
  struct curl_slist *h[2];
47
1.96k
  struct curl_slist *headers;
48
1.96k
  int numlists = 1; /* by default */
49
1.96k
  int i;
50
51
1.96k
  enum Curl_proxy_use proxy;
52
53
1.96k
  if(is_connect && !is_udp)
54
1.96k
    proxy = HEADER_CONNECT;
55
0
  else if(is_connect && is_udp)
56
0
    proxy = HEADER_CONNECT_UDP;
57
0
  else
58
0
    proxy = conn->bits.origin_is_proxy ? HEADER_PROXY : HEADER_SERVER;
59
60
1.96k
  switch(proxy) {
61
0
  case HEADER_SERVER:
62
0
    h[0] = data->set.headers;
63
0
    break;
64
0
  case HEADER_PROXY:
65
0
    h[0] = data->set.headers;
66
0
    if(data->set.sep_headers) {
67
0
      h[1] = data->set.proxyheaders;
68
0
      numlists++;
69
0
    }
70
0
    break;
71
1.96k
  case HEADER_CONNECT:
72
1.96k
    if(data->set.sep_headers)
73
1.71k
      h[0] = data->set.proxyheaders;
74
247
    else
75
247
      h[0] = data->set.headers;
76
1.96k
    break;
77
0
  case HEADER_CONNECT_UDP:
78
0
    if(data->set.sep_headers)
79
0
      h[0] = data->set.proxyheaders;
80
0
    else
81
0
      h[0] = data->set.headers;
82
0
    break;
83
1.96k
  }
84
85
  /* loop through one or two lists */
86
3.91k
  for(i = 0; i < numlists; i++) {
87
30.6k
    for(headers = h[i]; headers; headers = headers->next) {
88
28.7k
      struct Curl_str name;
89
28.7k
      const char *value = NULL;
90
28.7k
      size_t valuelen = 0;
91
28.7k
      const char *ptr = headers->data;
92
93
      /* There are 2 quirks in place for custom headers:
94
       * 1. setting only 'name:' to suppress a header from being sent
95
       * 2. setting only 'name;' to send an empty (illegal) header
96
       */
97
28.7k
      if(!curlx_str_cspn(&ptr, &name, ";:")) {
98
27.7k
        if(!curlx_str_single(&ptr, ':')) {
99
4.79k
          curlx_str_passblanks(&ptr);
100
4.79k
          if(*ptr) {
101
4.30k
            value = ptr;
102
4.30k
            valuelen = strlen(value);
103
4.30k
          }
104
496
          else {
105
            /* quirk #1, suppress this header */
106
496
            continue;
107
496
          }
108
4.79k
        }
109
22.9k
        else if(!curlx_str_single(&ptr, ';')) {
110
22.1k
          curlx_str_passblanks(&ptr);
111
22.1k
          if(!*ptr) {
112
            /* quirk #2, send an empty header */
113
21.7k
            value = "";
114
21.7k
            valuelen = 0;
115
21.7k
          }
116
331
          else {
117
            /* this may be used for something else in the future,
118
             * ignore this for now */
119
331
            continue;
120
331
          }
121
22.1k
        }
122
806
        else
123
          /* neither : nor ; in provided header value. We ignore this
124
           * silently */
125
806
          continue;
126
27.7k
      }
127
998
      else
128
        /* no name, move on */
129
998
        continue;
130
131
26.0k
      DEBUGASSERT(curlx_strlen(&name) && value);
132
      /* trim surrounding whitespace so a padded field name (e.g.
133
         `Authorization :`) cannot slip past the Authorization/Cookie check */
134
26.0k
      curlx_str_trimblanks(&name);
135
26.0k
      if(data->state.http_host &&
136
         /* a Host: header was sent already, do not pass on any custom Host:
137
            header as that will produce *two* in the same request! */
138
0
         curlx_str_casecompare(&name, "Host"))
139
0
        ;
140
26.0k
      else if(data->state.httpreq == HTTPREQ_POST_FORM &&
141
              /* this header (extended by formdata.c) is sent later */
142
673
              curlx_str_casecompare(&name, "Content-Type"))
143
0
        ;
144
26.0k
      else if(data->state.httpreq == HTTPREQ_POST_MIME &&
145
              /* this header is sent later */
146
17.3k
              curlx_str_casecompare(&name, "Content-Type"))
147
0
        ;
148
26.0k
      else if(data->req.authneg &&
149
              /* while doing auth neg, do not allow the custom length since
150
                 we will force length zero then */
151
0
              curlx_str_casecompare(&name, "Content-Length"))
152
0
        ;
153
26.0k
      else if((httpversion >= 20) &&
154
0
              curlx_str_casecompare(&name, "Transfer-Encoding"))
155
0
        ;
156
      /* HTTP/2 and HTTP/3 do not support chunked requests */
157
26.0k
      else if((curlx_str_casecompare(&name, "Authorization") ||
158
26.0k
               curlx_str_casecompare(&name, "Cookie")) &&
159
              /* be careful of sending this potentially sensitive header to
160
                 other hosts */
161
1.22k
              !Curl_auth_allowed_to_host(data))
162
0
        ;
163
26.0k
      else {
164
26.0k
        CURLcode result =
165
26.0k
          Curl_dynhds_add(hds, curlx_str(&name), curlx_strlen(&name),
166
26.0k
                          value, valuelen);
167
26.0k
        if(result)
168
1
          return result;
169
26.0k
      }
170
26.0k
    }
171
1.96k
  }
172
173
1.95k
  return CURLE_OK;
174
1.96k
}
175
176
struct cf_proxy_ctx {
177
  struct Curl_peer *peer; /* proxy */
178
  struct Curl_peer *tunnel_peer; /* tunnel destination */
179
  uint8_t proxytype;
180
  uint8_t tunnel_transport;
181
  BIT(sub_filter_installed);
182
};
183
184
static int proxy_http_ver_major(proxy_http_ver ver)
185
1.99k
{
186
1.99k
  switch(ver) {
187
1.99k
  case PROXY_HTTP_V1:
188
1.99k
    return 11;
189
0
  case PROXY_HTTP_V2:
190
0
    return 20;
191
0
  case PROXY_HTTP_V3:
192
0
    return 30;
193
1.99k
  }
194
0
  return 0;
195
1.99k
}
196
197
static CURLcode http_proxy_create_CONNECT(struct httpreq **preq,
198
                                          struct Curl_cfilter *cf,
199
                                          struct Curl_easy *data,
200
                                          struct Curl_peer *dest,
201
                                          proxy_http_ver ver)
202
1.99k
{
203
1.99k
  char *authority = NULL;
204
1.99k
  const char *ua;
205
1.99k
  int httpversion = proxy_http_ver_major(ver);
206
1.99k
  CURLcode result;
207
1.99k
  struct httpreq *req = NULL;
208
209
1.99k
  authority = curl_maprintf("%s%s%s:%u",
210
1.99k
                            dest->ipv6 ? "[" : "",
211
1.99k
                            dest->hostname,
212
1.99k
                            dest->ipv6 ? "]" : "",
213
1.99k
                            dest->port);
214
1.99k
  if(!authority) {
215
0
    result = CURLE_OUT_OF_MEMORY;
216
0
    goto out;
217
0
  }
218
219
1.99k
  result = Curl_http_req_make(&req, "CONNECT", CURL_CSTRLEN("CONNECT"),
220
1.99k
                              NULL, 0, authority, strlen(authority),
221
1.99k
                              NULL, 0);
222
1.99k
  if(result)
223
0
    goto out;
224
225
  /* Setup the proxy-authorization header, if any */
226
1.99k
  result = Curl_http_output_auth(data, cf->conn, req->method, HTTPREQ_GET,
227
1.99k
                                 req->authority, NULL, TRUE);
228
1.99k
  if(result)
229
30
    goto out;
230
231
  /* If user is not overriding Host: header, we add for HTTP/1.x */
232
1.96k
  if(ver == PROXY_HTTP_V1 &&
233
1.96k
     !Curl_checkProxyheaders(data, cf->conn, STRCONST("Host"))) {
234
1.95k
    result = Curl_dynhds_cadd(&req->headers, "Host", authority);
235
1.95k
    if(result)
236
0
      goto out;
237
1.95k
  }
238
239
1.96k
  if(data->req.hd_proxy_auth) {
240
155
    result = Curl_dynhds_h1_cadd_line(&req->headers,
241
155
                                      data->req.hd_proxy_auth);
242
155
    if(result)
243
0
      goto out;
244
155
  }
245
246
1.96k
  ua = CURL_EASY_STR(data, STRING_USERAGENT);
247
1.96k
  if(!Curl_checkProxyheaders(data, cf->conn, STRCONST("User-Agent")) &&
248
1.96k
     ua && *ua) {
249
16
    result = Curl_dynhds_cadd(&req->headers, "User-Agent", ua);
250
16
    if(result)
251
0
      goto out;
252
16
  }
253
254
1.96k
  if(ver == PROXY_HTTP_V1 &&
255
1.96k
     !Curl_checkProxyheaders(data, cf->conn, STRCONST("Proxy-Connection"))) {
256
1.96k
    result = Curl_dynhds_cadd(&req->headers, "Proxy-Connection", "Keep-Alive");
257
1.96k
    if(result)
258
0
      goto out;
259
1.96k
  }
260
261
1.96k
  result = dynhds_add_custom(data, TRUE, httpversion,
262
1.96k
                             FALSE, &req->headers);
263
264
1.99k
out:
265
1.99k
  if(result && req) {
266
31
    Curl_http_req_free(req);
267
31
    req = NULL;
268
31
  }
269
1.99k
  curlx_free(authority);
270
1.99k
  *preq = req;
271
1.99k
  return result;
272
1.96k
}
273
274
static CURLcode http_proxy_create_CONNECTUDP(struct httpreq **preq,
275
                                             struct Curl_cfilter *cf,
276
                                             struct Curl_easy *data,
277
                                             struct Curl_peer *dest,
278
                                             proxy_http_ver ver)
279
0
{
280
0
  const char *proxy_scheme = "http", *ua;
281
0
  const char *proxy_host = cf->conn->http_proxy.peer->hostname;
282
0
  int httpversion = proxy_http_ver_major(ver);
283
0
  char *authority = NULL;
284
0
  char *path = NULL;
285
0
  char *encoded_host = NULL;
286
0
  struct httpreq *req = NULL;
287
0
  bool proxy_ipv6_ip;
288
0
  CURLcode result;
289
290
0
  if(cf->conn->http_proxy.proxytype == CURLPROXY_HTTPS ||
291
0
     cf->conn->http_proxy.proxytype == CURLPROXY_HTTPS2 ||
292
0
     cf->conn->http_proxy.proxytype == CURLPROXY_HTTPS3)
293
0
    proxy_scheme = "https";
294
295
0
  proxy_ipv6_ip = cf->conn->http_proxy.peer->ipv6 != 0;
296
297
0
  authority = curl_maprintf("%s%s%s:%d",
298
0
                            proxy_ipv6_ip ? "[" : "",
299
0
                            proxy_host,
300
0
                            proxy_ipv6_ip ? "]" : "",
301
0
                            cf->conn->http_proxy.peer->port);
302
0
  if(!authority) {
303
0
    result = CURLE_OUT_OF_MEMORY;
304
0
    goto out;
305
0
  }
306
307
0
  if(dest->ipv6) {
308
    /* RFC 9298: colons in IPv6 addresses MUST be percent-encoded
309
     * in the URI template (e.g. "2001:db8::1" -> "2001%3Adb8%3A%3A1") */
310
0
    const char *s = dest->hostname;
311
0
    char *d;
312
0
    size_t hlen = strlen(s);
313
0
    encoded_host = curlx_malloc(hlen * 3 + 1);
314
0
    if(!encoded_host) {
315
0
      result = CURLE_OUT_OF_MEMORY;
316
0
      goto out;
317
0
    }
318
0
    d = encoded_host;
319
0
    while(*s) {
320
0
      if(*s == ':') {
321
0
        *d++ = '%';
322
0
        *d++ = '3';
323
0
        *d++ = 'A';
324
0
      }
325
0
      else
326
0
        *d++ = *s;
327
0
      s++;
328
0
    }
329
0
    *d = '\0';
330
0
    path = curl_maprintf("/.well-known/masque/udp/%s/%u/",
331
0
                         encoded_host, (unsigned int)dest->port);
332
0
  }
333
0
  else {
334
0
    path = curl_maprintf("/.well-known/masque/udp/%s/%u/",
335
0
                         dest->hostname, (unsigned int)dest->port);
336
0
  }
337
338
0
  if(!path) {
339
0
    result = CURLE_OUT_OF_MEMORY;
340
0
    goto out;
341
0
  }
342
343
0
  if(ver == PROXY_HTTP_V1) {
344
0
    result = Curl_http_req_make(&req, "GET", CURL_CSTRLEN("GET"),
345
0
                                proxy_scheme, strlen(proxy_scheme),
346
0
                                authority, strlen(authority),
347
0
                                path, strlen(path));
348
0
    if(result)
349
0
      goto out;
350
0
  }
351
0
  else if(ver == PROXY_HTTP_V2 || ver == PROXY_HTTP_V3) {
352
0
    result = Curl_http_req_make(&req, "CONNECT", CURL_CSTRLEN("CONNECT"),
353
0
                                proxy_scheme, strlen(proxy_scheme),
354
0
                                authority, strlen(authority),
355
0
                                path, strlen(path));
356
0
    if(result)
357
0
      goto out;
358
0
  }
359
0
  else {
360
0
    result = CURLE_FAILED_INIT;
361
0
    goto out;
362
0
  }
363
364
  /* Setup the proxy-authorization header, if any */
365
0
  result = Curl_http_output_auth(data, cf->conn, req->method, HTTPREQ_GET,
366
0
                                 req->authority, NULL, TRUE);
367
0
  if(result)
368
0
    goto out;
369
370
  /* If user is not overriding Host: header, we add for HTTP/1.x */
371
0
  if(ver == PROXY_HTTP_V1 &&
372
0
     !Curl_checkProxyheaders(data, cf->conn, STRCONST("Host"))) {
373
0
    result = Curl_dynhds_cadd(&req->headers, "Host", authority);
374
0
    if(result)
375
0
      goto out;
376
0
  }
377
378
0
  if(data->req.hd_proxy_auth) {
379
0
    result = Curl_dynhds_h1_cadd_line(&req->headers,
380
0
                                      data->req.hd_proxy_auth);
381
0
    if(result)
382
0
      goto out;
383
0
  }
384
385
0
  ua = CURL_EASY_STR(data, STRING_USERAGENT);
386
0
  if(ver == PROXY_HTTP_V1 &&
387
0
     !Curl_checkProxyheaders(data, cf->conn, STRCONST("User-Agent")) &&
388
0
     ua && *ua) {
389
0
    result = Curl_dynhds_cadd(&req->headers, "User-Agent", ua);
390
0
    if(result)
391
0
      goto out;
392
0
  }
393
394
0
  if(ver == PROXY_HTTP_V1 &&
395
0
     !Curl_checkProxyheaders(data, cf->conn, STRCONST("Proxy-Connection"))) {
396
0
    result = Curl_dynhds_cadd(&req->headers, "Proxy-Connection", "Keep-Alive");
397
0
    if(result)
398
0
      goto out;
399
0
  }
400
401
0
  if(ver == PROXY_HTTP_V1) {
402
0
    result = Curl_dynhds_cadd(&req->headers, "Connection", "Upgrade");
403
0
    if(result)
404
0
      goto out;
405
406
0
    result = Curl_dynhds_cadd(&req->headers, "Upgrade", "connect-udp");
407
0
    if(result)
408
0
      goto out;
409
410
0
    result = Curl_dynhds_cadd(&req->headers, "Capsule-Protocol", "?1");
411
0
    if(result)
412
0
      goto out;
413
0
  }
414
0
  else {
415
0
    result = Curl_dynhds_cadd(&req->headers, ":Protocol", "connect-udp");
416
0
    if(result)
417
0
      goto out;
418
419
0
    if(ver >= PROXY_HTTP_V2) {
420
0
      result = Curl_dynhds_cadd(&req->headers, "Capsule-Protocol", "?1");
421
0
      if(result)
422
0
        goto out;
423
0
    }
424
0
  }
425
426
0
  result = dynhds_add_custom(data, TRUE, httpversion,
427
0
                             TRUE, &req->headers);
428
429
0
out:
430
0
  if(result && req) {
431
0
    Curl_http_req_free(req);
432
0
    req = NULL;
433
0
  }
434
0
  curlx_free(authority);
435
0
  curlx_free(path);
436
0
  curlx_free(encoded_host);
437
0
  *preq = req;
438
0
  return result;
439
0
}
440
441
CURLcode Curl_http_proxy_create_tunnel_request(
442
    struct httpreq **preq, struct Curl_cfilter *cf,
443
    struct Curl_easy *data, struct Curl_peer *dest,
444
    proxy_http_ver ver, bool udp_tunnel)
445
1.99k
{
446
1.99k
  CURLcode result;
447
448
1.99k
  if(udp_tunnel)
449
0
    result = http_proxy_create_CONNECTUDP(preq, cf, data, dest, ver);
450
1.99k
  else
451
1.99k
    result = http_proxy_create_CONNECT(preq, cf, data, dest, ver);
452
1.99k
  if(result)
453
31
    return result;
454
455
1.95k
  if(udp_tunnel)
456
0
    infof(data, "Establishing %s proxy UDP tunnel to %s:%u",
457
1.95k
          (ver == PROXY_HTTP_V2) ? "HTTP/2" :
458
1.95k
          (ver == PROXY_HTTP_V3) ? "HTTP/3" : "HTTP",
459
1.95k
          dest->user_hostname, dest->port);
460
1.95k
  else
461
1.95k
    infof(data, "Establishing %s proxy tunnel to %s",
462
1.95k
          (ver == PROXY_HTTP_V2) ? "HTTP/2" :
463
1.95k
          (ver == PROXY_HTTP_V3) ? "HTTP/3" : "HTTP",
464
1.95k
          (*preq)->authority);
465
1.95k
  return CURLE_OK;
466
1.99k
}
467
468
CURLcode Curl_http_proxy_inspect_tunnel_response(
469
    struct Curl_cfilter *cf, struct Curl_easy *data,
470
    struct http_resp *resp, bool udp_tunnel,
471
    proxy_inspect_result *presult)
472
0
{
473
0
  struct dynhds_entry *capsule_protocol = NULL;
474
0
  struct dynhds_entry *auth_reply = NULL;
475
0
  size_t i, header_count;
476
0
  CURLcode result = CURLE_OK;
477
478
0
  DEBUGASSERT(resp);
479
480
0
  header_count = Curl_dynhds_count(&resp->headers);
481
0
  if(udp_tunnel)
482
0
    infof(data, "CONNECT-UDP Response Status %d", resp->status);
483
0
  else
484
0
    infof(data, "CONNECT Response Status %d", resp->status);
485
0
  infof(data, "Response Headers (%zu total):", header_count);
486
0
  for(i = 0; i < header_count; i++) {
487
0
    struct dynhds_entry *entry = Curl_dynhds_getn(&resp->headers, i);
488
0
    if(entry)
489
0
      infof(data, "  %s: %s", entry->name, entry->value);
490
0
  }
491
492
0
  if(resp->status == 407) {
493
0
    auth_reply = Curl_dynhds_cget(&resp->headers, "Proxy-Authenticate");
494
0
  }
495
496
0
  if(auth_reply) {
497
0
    CURL_TRC_CF(data, cf, "[0] CONNECT%s: fwd auth header '%s'",
498
0
                udp_tunnel ? "-UDP" : "", auth_reply->value);
499
0
    result = Curl_http_input_auth(data, resp->status == 407,
500
0
                                  auth_reply->value);
501
0
    if(result)
502
0
      return result;
503
0
    if(data->req.newurl) {
504
0
      curlx_safefree(data->req.newurl);
505
0
      *presult = PROXY_INSPECT_AUTH_RETRY;
506
0
      return CURLE_OK;
507
0
    }
508
0
  }
509
510
0
  if(udp_tunnel) {
511
0
    if(resp->status / 100 == 2) {
512
0
      capsule_protocol = Curl_dynhds_cget(&resp->headers,
513
0
                                           "capsule-protocol");
514
0
      if(capsule_protocol) {
515
0
        if(!strncmp(capsule_protocol->value, "?1", 2) &&
516
0
           !capsule_protocol->value[2]) {
517
0
          infof(data, "CONNECT-UDP tunnel established, response %d",
518
0
                resp->status);
519
0
          *presult = PROXY_INSPECT_OK;
520
0
          return CURLE_OK;
521
0
        }
522
0
        failf(data, "Failed to establish CONNECT-UDP tunnel, response %d, "
523
0
              "unsupported capsule-protocol value '%s'",
524
0
              resp->status, capsule_protocol->value);
525
0
        *presult = PROXY_INSPECT_FAILED;
526
0
        return CURLE_COULDNT_CONNECT;
527
0
      }
528
0
      else {
529
        /* NOTE proxies may not set capsule protocol in the headers */
530
0
        infof(data, "CONNECT-UDP tunnel established, response %d "
531
0
                    "but no capsule-protocol header found", resp->status);
532
0
        *presult = PROXY_INSPECT_OK;
533
0
        return CURLE_OK;
534
0
      }
535
0
    }
536
0
    else {
537
0
      failf(data, "Failed to establish CONNECT-UDP tunnel, "
538
0
                  "response %d", resp->status);
539
0
      *presult = PROXY_INSPECT_FAILED;
540
0
      return CURLE_COULDNT_CONNECT;
541
0
    }
542
0
  }
543
544
0
  if(resp->status / 100 == 2) {
545
0
    infof(data, "CONNECT tunnel established, response %d", resp->status);
546
0
    *presult = PROXY_INSPECT_OK;
547
0
    return CURLE_OK;
548
0
  }
549
550
0
  *presult = PROXY_INSPECT_FAILED;
551
0
  return CURLE_COULDNT_CONNECT;
552
0
}
553
554
static CURLcode http_proxy_cf_connect(struct Curl_cfilter *cf,
555
                                      struct Curl_easy *data,
556
                                      bool *done)
557
2.48k
{
558
2.48k
  struct cf_proxy_ctx *ctx = cf->ctx;
559
2.48k
  CURLcode result;
560
2.48k
  bool udp_tunnel = TRNSPRT_IS_DGRAM(ctx->tunnel_transport);
561
2.48k
  const char *tunnel_type = udp_tunnel ? "CONNECT-UDP" : "CONNECT";
562
563
2.48k
  if(cf->connected) {
564
8
    *done = TRUE;
565
8
    return CURLE_OK;
566
8
  }
567
568
2.47k
  CURL_TRC_CF(data, cf, "%s", tunnel_type);
569
4.46k
connect_sub:
570
  /* in case of h3_proxy, cf->next will be NULL initially */
571
4.46k
  if(cf->next) {
572
4.46k
    result = cf->next->cft->do_connect(cf->next, data, done);
573
4.46k
    if(result || !*done)
574
2.44k
      return result;
575
4.46k
  }
576
577
2.02k
  *done = FALSE;
578
2.02k
  if(!ctx->sub_filter_installed) {
579
1.99k
    const char *alpn = NULL;
580
581
    /* in case of h3_proxy, cf->next will be NULL initially */
582
1.99k
    if(cf->next) {
583
1.99k
      alpn = Curl_conn_cf_get_alpn_negotiated(cf->next, data);
584
1.99k
    }
585
586
1.99k
    if(alpn)
587
0
      infof(data, "%s: '%s' negotiated", tunnel_type, alpn);
588
1.99k
    else if(!alpn) {
589
      /* No ALPN, proxytype rules. Fake ALPN */
590
1.99k
      infof(data, "%s: no ALPN negotiated", tunnel_type);
591
1.99k
      switch(ctx->proxytype) {
592
2
      case CURLPROXY_HTTP_1_0:
593
2
        alpn = "http/1.0";
594
2
        break;
595
0
      case CURLPROXY_HTTPS2:
596
0
        alpn = "h2";
597
0
        break;
598
0
      case CURLPROXY_HTTPS3:
599
0
        alpn = "h3";
600
0
        break;
601
1.98k
      default:
602
1.98k
        alpn = "http/1.1";
603
1.98k
        break;
604
1.99k
      }
605
1.99k
    }
606
607
1.99k
    if(!strcmp(alpn, "http/1.0")) {
608
2
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.0");
609
2
      result = Curl_cf_h1_proxy_insert_after(cf, data, ctx->tunnel_peer, 10,
610
2
                                             udp_tunnel);
611
2
      if(result)
612
0
        goto out;
613
2
    }
614
1.98k
    else if(!strcmp(alpn, "http/1.1")) {
615
1.98k
      int httpversion = (ctx->proxytype == CURLPROXY_HTTP_1_0) ? 10 : 11;
616
1.98k
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.%d",
617
1.98k
                  httpversion % 10);
618
1.98k
      result = Curl_cf_h1_proxy_insert_after(cf, data, ctx->tunnel_peer,
619
1.98k
                                             httpversion, udp_tunnel);
620
1.98k
      if(result)
621
0
        goto out;
622
1.98k
    }
623
0
#ifdef USE_NGHTTP2
624
0
    else if(!strcmp(alpn, "h2")) {
625
0
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/2");
626
0
      result = Curl_cf_h2_proxy_insert_after(cf, data, ctx->tunnel_peer,
627
0
                                             udp_tunnel);
628
0
      if(result)
629
0
        goto out;
630
0
    }
631
0
#endif /* USE_NGHTTP2 */
632
#if defined(USE_PROXY_HTTP3) && defined(USE_NGHTTP3) && \
633
  defined(USE_NGTCP2) && defined(USE_OPENSSL)
634
    else if(!strcmp(alpn, "h3")) {
635
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/3");
636
      result = Curl_cf_h3_proxy_insert_after(cf, data, ctx->peer, ctx->peer,
637
                                             ctx->tunnel_peer,
638
                                             ctx->tunnel_transport);
639
      if(result)
640
        goto out;
641
    }
642
#endif /* USE_PROXY_HTTP3 && USE_NGHTTP3 && USE_NGTCP2 && USE_OPENSSL */
643
0
    else {
644
0
      failf(data, "%s: negotiated ALPN '%s' not supported", tunnel_type, alpn);
645
0
      result = CURLE_COULDNT_CONNECT;
646
0
      goto out;
647
0
    }
648
649
1.99k
    ctx->sub_filter_installed = TRUE;
650
    /* after we installed the filter "below" us, we call connect
651
     * on out sub-chain again.
652
     */
653
1.99k
    goto connect_sub;
654
1.99k
  }
655
30
  else {
656
    /* subchain connected and we had already installed the protocol filter.
657
     * This means the protocol tunnel is established, we are done. */
658
30
    DEBUGASSERT(ctx->sub_filter_installed);
659
30
    result = CURLE_OK;
660
30
  }
661
662
30
out:
663
30
  if(!result) {
664
30
    cf->connected = TRUE;
665
30
    *done = TRUE;
666
30
  }
667
30
  return result;
668
2.02k
}
669
670
static CURLcode cf_http_proxy_query(struct Curl_cfilter *cf,
671
                                    struct Curl_easy *data,
672
                                    int query, int *pres1, void *pres2)
673
1.04k
{
674
1.04k
  struct cf_proxy_ctx *ctx = cf->ctx;
675
1.04k
  switch(query) {
676
0
  case CF_QUERY_HOST_PORT:
677
0
    *pres1 = (int)ctx->tunnel_peer->port;
678
0
    *((const char **)pres2) = ctx->tunnel_peer->hostname;
679
0
    return CURLE_OK;
680
0
  case CF_QUERY_ALPN_NEGOTIATED: {
681
0
    const char **palpn = pres2;
682
0
    DEBUGASSERT(palpn);
683
0
    *palpn = NULL;
684
0
    return CURLE_OK;
685
0
  }
686
1.04k
  default:
687
1.04k
    break;
688
1.04k
  }
689
1.04k
  return cf->next ?
690
1.04k
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
691
1.04k
    CURLE_UNKNOWN_OPTION;
692
1.04k
}
693
694
static void cf_https_proxy_ctx_free(struct cf_proxy_ctx *ctx)
695
3.98k
{
696
3.98k
  if(ctx) {
697
1.99k
    Curl_peer_unlink(&ctx->peer);
698
1.99k
    Curl_peer_unlink(&ctx->tunnel_peer);
699
1.99k
    curlx_free(ctx);
700
1.99k
  }
701
3.98k
}
702
703
static void http_proxy_cf_destroy(struct Curl_cfilter *cf,
704
                                  struct Curl_easy *data)
705
1.99k
{
706
1.99k
  struct cf_proxy_ctx *ctx = cf->ctx;
707
1.99k
  if(ctx) {
708
1.99k
    CURL_TRC_CF(data, cf, "destroy");
709
1.99k
    cf_https_proxy_ctx_free(ctx);
710
1.99k
  }
711
1.99k
}
712
713
struct Curl_cftype Curl_cft_http_proxy = {
714
  "HTTP-PROXY",
715
  CF_TYPE_IP_CONNECT | CF_TYPE_PROXY | CF_TYPE_SETUP,
716
  0,
717
  http_proxy_cf_destroy,
718
  http_proxy_cf_connect,
719
  Curl_cf_def_shutdown,
720
  Curl_cf_def_adjust_pollset,
721
  Curl_cf_def_data_pending,
722
  Curl_cf_def_send,
723
  Curl_cf_def_recv,
724
  Curl_cf_def_cntrl,
725
  Curl_cf_def_conn_is_alive,
726
  Curl_cf_def_conn_keep_alive,
727
  cf_http_proxy_query,
728
};
729
730
CURLcode Curl_cf_http_proxy_insert_after(struct Curl_cfilter *cf_at,
731
                                         struct Curl_easy *data,
732
                                         struct Curl_peer *peer,
733
                                         struct Curl_peer *tunnel_peer,
734
                                         uint8_t tunnel_transport,
735
                                         uint8_t proxytype)
736
1.99k
{
737
1.99k
  struct Curl_cfilter *cf;
738
1.99k
  struct cf_proxy_ctx *ctx = NULL;
739
1.99k
  CURLcode result;
740
741
1.99k
  (void)data;
742
1.99k
  if(!peer || !tunnel_peer)
743
0
    return CURLE_FAILED_INIT;
744
745
1.99k
  ctx = curlx_calloc(1, sizeof(*ctx));
746
1.99k
  if(!ctx) {
747
0
    result = CURLE_OUT_OF_MEMORY;
748
0
    goto out;
749
0
  }
750
1.99k
  Curl_peer_link(&ctx->peer, peer);
751
1.99k
  Curl_peer_link(&ctx->tunnel_peer, tunnel_peer);
752
1.99k
  ctx->proxytype = proxytype;
753
1.99k
  ctx->tunnel_transport = tunnel_transport;
754
755
1.99k
  result = Curl_cf_create(&cf, &Curl_cft_http_proxy, ctx);
756
1.99k
  if(result)
757
0
    goto out;
758
1.99k
  ctx = NULL;
759
1.99k
  Curl_conn_cf_insert_after(cf_at, cf);
760
761
1.99k
out:
762
1.99k
  cf_https_proxy_ctx_free(ctx);
763
1.99k
  return result;
764
1.99k
}
765
766
uint8_t Curl_http_proxy_transport(uint8_t proxytype)
767
2.04k
{
768
2.04k
  switch(proxytype) {
769
0
  case CURLPROXY_HTTPS3:
770
0
    return TRNSPRT_QUIC;
771
2.04k
  default:
772
2.04k
    return TRNSPRT_TCP;
773
2.04k
  }
774
2.04k
}
775
776
#endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY */