Coverage Report

Created: 2026-08-31 06:47

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
0
{
45
0
  struct connectdata *conn = data->conn;
46
0
  struct curl_slist *h[2];
47
0
  struct curl_slist *headers;
48
0
  int numlists = 1; /* by default */
49
0
  int i;
50
51
0
  enum Curl_proxy_use proxy;
52
53
0
  if(is_connect && !is_udp)
54
0
    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
0
  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
0
  case HEADER_CONNECT:
72
0
    if(data->set.sep_headers)
73
0
      h[0] = data->set.proxyheaders;
74
0
    else
75
0
      h[0] = data->set.headers;
76
0
    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
0
  }
84
85
  /* loop through one or two lists */
86
0
  for(i = 0; i < numlists; i++) {
87
0
    for(headers = h[i]; headers; headers = headers->next) {
88
0
      struct Curl_str name;
89
0
      const char *value = NULL;
90
0
      size_t valuelen = 0;
91
0
      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
0
      if(!curlx_str_cspn(&ptr, &name, ";:")) {
98
0
        if(!curlx_str_single(&ptr, ':')) {
99
0
          curlx_str_passblanks(&ptr);
100
0
          if(*ptr) {
101
0
            value = ptr;
102
0
            valuelen = strlen(value);
103
0
          }
104
0
          else {
105
            /* quirk #1, suppress this header */
106
0
            continue;
107
0
          }
108
0
        }
109
0
        else if(!curlx_str_single(&ptr, ';')) {
110
0
          curlx_str_passblanks(&ptr);
111
0
          if(!*ptr) {
112
            /* quirk #2, send an empty header */
113
0
            value = "";
114
0
            valuelen = 0;
115
0
          }
116
0
          else {
117
            /* this may be used for something else in the future,
118
             * ignore this for now */
119
0
            continue;
120
0
          }
121
0
        }
122
0
        else
123
          /* neither : nor ; in provided header value. We ignore this
124
           * silently */
125
0
          continue;
126
0
      }
127
0
      else
128
        /* no name, move on */
129
0
        continue;
130
131
0
      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
0
      curlx_str_trimblanks(&name);
135
0
      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
0
      else if(data->state.httpreq == HTTPREQ_POST_FORM &&
141
              /* this header (extended by formdata.c) is sent later */
142
0
              curlx_str_casecompare(&name, "Content-Type"))
143
0
        ;
144
0
      else if(data->state.httpreq == HTTPREQ_POST_MIME &&
145
              /* this header is sent later */
146
0
              curlx_str_casecompare(&name, "Content-Type"))
147
0
        ;
148
0
      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
0
      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
0
      else if((curlx_str_casecompare(&name, "Authorization") ||
158
0
               curlx_str_casecompare(&name, "Cookie")) &&
159
              /* be careful of sending this potentially sensitive header to
160
                 other hosts */
161
0
              !Curl_auth_allowed_to_host(data))
162
0
        ;
163
0
      else {
164
0
        CURLcode result =
165
0
          Curl_dynhds_add(hds, curlx_str(&name), curlx_strlen(&name),
166
0
                          value, valuelen);
167
0
        if(result)
168
0
          return result;
169
0
      }
170
0
    }
171
0
  }
172
173
0
  return CURLE_OK;
174
0
}
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
0
{
186
0
  switch(ver) {
187
0
  case PROXY_HTTP_V1:
188
0
    return 11;
189
0
  case PROXY_HTTP_V2:
190
0
    return 20;
191
0
  case PROXY_HTTP_V3:
192
0
    return 30;
193
0
  }
194
0
  return 0;
195
0
}
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
0
{
203
0
  char *authority = NULL;
204
0
  const char *ua;
205
0
  int httpversion = proxy_http_ver_major(ver);
206
0
  CURLcode result;
207
0
  struct httpreq *req = NULL;
208
209
0
  authority = curl_maprintf("%s%s%s:%u",
210
0
                            dest->ipv6 ? "[" : "",
211
0
                            dest->hostname,
212
0
                            dest->ipv6 ? "]" : "",
213
0
                            dest->port);
214
0
  if(!authority) {
215
0
    result = CURLE_OUT_OF_MEMORY;
216
0
    goto out;
217
0
  }
218
219
0
  result = Curl_http_req_make(&req, "CONNECT", CURL_CSTRLEN("CONNECT"),
220
0
                              NULL, 0, authority, strlen(authority),
221
0
                              NULL, 0);
222
0
  if(result)
223
0
    goto out;
224
225
  /* Setup the proxy-authorization header, if any */
226
0
  result = Curl_http_output_auth(data, cf->conn, req->method, HTTPREQ_GET,
227
0
                                 req->authority, NULL, TRUE);
228
0
  if(result)
229
0
    goto out;
230
231
  /* If user is not overriding Host: header, we add for HTTP/1.x */
232
0
  if(ver == PROXY_HTTP_V1 &&
233
0
     !Curl_checkProxyheaders(data, cf->conn, STRCONST("Host"))) {
234
0
    result = Curl_dynhds_cadd(&req->headers, "Host", authority);
235
0
    if(result)
236
0
      goto out;
237
0
  }
238
239
0
  if(data->req.hd_proxy_auth) {
240
0
    result = Curl_dynhds_h1_cadd_line(&req->headers,
241
0
                                      data->req.hd_proxy_auth);
242
0
    if(result)
243
0
      goto out;
244
0
  }
245
246
0
  ua = CURL_EASY_STR(data, STRING_USERAGENT);
247
0
  if(!Curl_checkProxyheaders(data, cf->conn, STRCONST("User-Agent")) &&
248
0
     ua && *ua) {
249
0
    result = Curl_dynhds_cadd(&req->headers, "User-Agent", ua);
250
0
    if(result)
251
0
      goto out;
252
0
  }
253
254
0
  if(ver == PROXY_HTTP_V1 &&
255
0
     !Curl_checkProxyheaders(data, cf->conn, STRCONST("Proxy-Connection"))) {
256
0
    result = Curl_dynhds_cadd(&req->headers, "Proxy-Connection", "Keep-Alive");
257
0
    if(result)
258
0
      goto out;
259
0
  }
260
261
0
  result = dynhds_add_custom(data, TRUE, httpversion,
262
0
                             FALSE, &req->headers);
263
264
0
out:
265
0
  if(result && req) {
266
0
    Curl_http_req_free(req);
267
0
    req = NULL;
268
0
  }
269
0
  curlx_free(authority);
270
0
  *preq = req;
271
0
  return result;
272
0
}
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
0
{
446
0
  CURLcode result;
447
448
0
  if(udp_tunnel)
449
0
    result = http_proxy_create_CONNECTUDP(preq, cf, data, dest, ver);
450
0
  else
451
0
    result = http_proxy_create_CONNECT(preq, cf, data, dest, ver);
452
0
  if(result)
453
0
    return result;
454
455
0
  if(udp_tunnel)
456
0
    infof(data, "Establishing %s proxy UDP tunnel to %s:%u",
457
0
          (ver == PROXY_HTTP_V2) ? "HTTP/2" :
458
0
          (ver == PROXY_HTTP_V3) ? "HTTP/3" : "HTTP",
459
0
          dest->user_hostname, dest->port);
460
0
  else
461
0
    infof(data, "Establishing %s proxy tunnel to %s",
462
0
          (ver == PROXY_HTTP_V2) ? "HTTP/2" :
463
0
          (ver == PROXY_HTTP_V3) ? "HTTP/3" : "HTTP",
464
0
          (*preq)->authority);
465
0
  return CURLE_OK;
466
0
}
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 == 401) {
493
0
    auth_reply = Curl_dynhds_cget(&resp->headers, "WWW-Authenticate");
494
0
  }
495
0
  else if(resp->status == 407) {
496
0
    auth_reply = Curl_dynhds_cget(&resp->headers, "Proxy-Authenticate");
497
0
  }
498
499
0
  if(auth_reply) {
500
0
    CURL_TRC_CF(data, cf, "[0] CONNECT%s: fwd auth header '%s'",
501
0
                udp_tunnel ? "-UDP" : "", auth_reply->value);
502
0
    result = Curl_http_input_auth(data, resp->status == 407,
503
0
                                  auth_reply->value);
504
0
    if(result)
505
0
      return result;
506
0
    if(data->req.newurl) {
507
0
      curlx_safefree(data->req.newurl);
508
0
      *presult = PROXY_INSPECT_AUTH_RETRY;
509
0
      return CURLE_OK;
510
0
    }
511
0
  }
512
513
0
  if(udp_tunnel) {
514
0
    if(resp->status / 100 == 2) {
515
0
      capsule_protocol = Curl_dynhds_cget(&resp->headers,
516
0
                                           "capsule-protocol");
517
0
      if(capsule_protocol) {
518
0
        if(!strncmp(capsule_protocol->value, "?1", 2) &&
519
0
           !capsule_protocol->value[2]) {
520
0
          infof(data, "CONNECT-UDP tunnel established, response %d",
521
0
                resp->status);
522
0
          *presult = PROXY_INSPECT_OK;
523
0
          return CURLE_OK;
524
0
        }
525
0
        failf(data, "Failed to establish CONNECT-UDP tunnel, response %d, "
526
0
              "unsupported capsule-protocol value '%s'",
527
0
              resp->status, capsule_protocol->value);
528
0
        *presult = PROXY_INSPECT_FAILED;
529
0
        return CURLE_COULDNT_CONNECT;
530
0
      }
531
0
      else {
532
        /* NOTE proxies may not set capsule protocol in the headers */
533
0
        infof(data, "CONNECT-UDP tunnel established, response %d "
534
0
                    "but no capsule-protocol header found", resp->status);
535
0
        *presult = PROXY_INSPECT_OK;
536
0
        return CURLE_OK;
537
0
      }
538
0
    }
539
0
    else {
540
0
      failf(data, "Failed to establish CONNECT-UDP tunnel, "
541
0
                  "response %d", resp->status);
542
0
      *presult = PROXY_INSPECT_FAILED;
543
0
      return CURLE_COULDNT_CONNECT;
544
0
    }
545
0
  }
546
547
0
  if(resp->status / 100 == 2) {
548
0
    infof(data, "CONNECT tunnel established, response %d", resp->status);
549
0
    *presult = PROXY_INSPECT_OK;
550
0
    return CURLE_OK;
551
0
  }
552
553
0
  *presult = PROXY_INSPECT_FAILED;
554
0
  return CURLE_COULDNT_CONNECT;
555
0
}
556
557
static CURLcode http_proxy_cf_connect(struct Curl_cfilter *cf,
558
                                      struct Curl_easy *data,
559
                                      bool *done)
560
0
{
561
0
  struct cf_proxy_ctx *ctx = cf->ctx;
562
0
  CURLcode result;
563
0
  bool udp_tunnel = TRNSPRT_IS_DGRAM(ctx->tunnel_transport);
564
0
  const char *tunnel_type = udp_tunnel ? "CONNECT-UDP" : "CONNECT";
565
566
0
  if(cf->connected) {
567
0
    *done = TRUE;
568
0
    return CURLE_OK;
569
0
  }
570
571
0
  CURL_TRC_CF(data, cf, "%s", tunnel_type);
572
0
connect_sub:
573
  /* in case of h3_proxy, cf->next will be NULL initially */
574
0
  if(cf->next) {
575
0
    result = cf->next->cft->do_connect(cf->next, data, done);
576
0
    if(result || !*done)
577
0
      return result;
578
0
  }
579
580
0
  *done = FALSE;
581
0
  if(!ctx->sub_filter_installed) {
582
0
    const char *alpn = NULL;
583
584
    /* in case of h3_proxy, cf->next will be NULL initially */
585
0
    if(cf->next) {
586
0
      alpn = Curl_conn_cf_get_alpn_negotiated(cf->next, data);
587
0
    }
588
589
0
    if(alpn)
590
0
      infof(data, "%s: '%s' negotiated", tunnel_type, alpn);
591
0
    else if(!alpn) {
592
      /* No ALPN, proxytype rules. Fake ALPN */
593
0
      infof(data, "%s: no ALPN negotiated", tunnel_type);
594
0
      switch(ctx->proxytype) {
595
0
      case CURLPROXY_HTTP_1_0:
596
0
        alpn = "http/1.0";
597
0
        break;
598
0
      case CURLPROXY_HTTPS2:
599
0
        alpn = "h2";
600
0
        break;
601
0
      case CURLPROXY_HTTPS3:
602
0
        alpn = "h3";
603
0
        break;
604
0
      default:
605
0
        alpn = "http/1.1";
606
0
        break;
607
0
      }
608
0
    }
609
610
0
    if(!strcmp(alpn, "http/1.0")) {
611
0
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.0");
612
0
      result = Curl_cf_h1_proxy_insert_after(cf, data, ctx->tunnel_peer, 10,
613
0
                                             udp_tunnel);
614
0
      if(result)
615
0
        goto out;
616
0
    }
617
0
    else if(!strcmp(alpn, "http/1.1")) {
618
0
      int httpversion = (ctx->proxytype == CURLPROXY_HTTP_1_0) ? 10 : 11;
619
0
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.%d",
620
0
                  httpversion % 10);
621
0
      result = Curl_cf_h1_proxy_insert_after(cf, data, ctx->tunnel_peer,
622
0
                                             httpversion, udp_tunnel);
623
0
      if(result)
624
0
        goto out;
625
0
    }
626
0
#ifdef USE_NGHTTP2
627
0
    else if(!strcmp(alpn, "h2")) {
628
0
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/2");
629
0
      result = Curl_cf_h2_proxy_insert_after(cf, data, ctx->tunnel_peer,
630
0
                                             udp_tunnel);
631
0
      if(result)
632
0
        goto out;
633
0
    }
634
0
#endif /* USE_NGHTTP2 */
635
#if defined(USE_PROXY_HTTP3) && defined(USE_NGHTTP3) && \
636
  defined(USE_NGTCP2) && defined(USE_OPENSSL)
637
    else if(!strcmp(alpn, "h3")) {
638
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/3");
639
      result = Curl_cf_h3_proxy_insert_after(cf, data, ctx->peer, ctx->peer,
640
                                             ctx->tunnel_peer,
641
                                             ctx->tunnel_transport);
642
      if(result)
643
        goto out;
644
    }
645
#endif /* USE_PROXY_HTTP3 && USE_NGHTTP3 && USE_NGTCP2 && USE_OPENSSL */
646
0
    else {
647
0
      failf(data, "%s: negotiated ALPN '%s' not supported", tunnel_type, alpn);
648
0
      result = CURLE_COULDNT_CONNECT;
649
0
      goto out;
650
0
    }
651
652
0
    ctx->sub_filter_installed = TRUE;
653
    /* after we installed the filter "below" us, we call connect
654
     * on out sub-chain again.
655
     */
656
0
    goto connect_sub;
657
0
  }
658
0
  else {
659
    /* subchain connected and we had already installed the protocol filter.
660
     * This means the protocol tunnel is established, we are done. */
661
0
    DEBUGASSERT(ctx->sub_filter_installed);
662
0
    result = CURLE_OK;
663
0
  }
664
665
0
out:
666
0
  if(!result) {
667
0
    cf->connected = TRUE;
668
0
    *done = TRUE;
669
0
  }
670
0
  return result;
671
0
}
672
673
static CURLcode cf_http_proxy_query(struct Curl_cfilter *cf,
674
                                    struct Curl_easy *data,
675
                                    int query, int *pres1, void *pres2)
676
0
{
677
0
  struct cf_proxy_ctx *ctx = cf->ctx;
678
0
  switch(query) {
679
0
  case CF_QUERY_HOST_PORT:
680
0
    *pres1 = (int)ctx->tunnel_peer->port;
681
0
    *((const char **)pres2) = ctx->tunnel_peer->hostname;
682
0
    return CURLE_OK;
683
0
  case CF_QUERY_ALPN_NEGOTIATED: {
684
0
    const char **palpn = pres2;
685
0
    DEBUGASSERT(palpn);
686
0
    *palpn = NULL;
687
0
    return CURLE_OK;
688
0
  }
689
0
  default:
690
0
    break;
691
0
  }
692
0
  return cf->next ?
693
0
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
694
0
    CURLE_UNKNOWN_OPTION;
695
0
}
696
697
static void cf_https_proxy_ctx_free(struct cf_proxy_ctx *ctx)
698
0
{
699
0
  if(ctx) {
700
0
    Curl_peer_unlink(&ctx->peer);
701
0
    Curl_peer_unlink(&ctx->tunnel_peer);
702
0
    curlx_free(ctx);
703
0
  }
704
0
}
705
706
static void http_proxy_cf_destroy(struct Curl_cfilter *cf,
707
                                  struct Curl_easy *data)
708
0
{
709
0
  struct cf_proxy_ctx *ctx = cf->ctx;
710
0
  if(ctx) {
711
0
    CURL_TRC_CF(data, cf, "destroy");
712
0
    cf_https_proxy_ctx_free(ctx);
713
0
  }
714
0
}
715
716
struct Curl_cftype Curl_cft_http_proxy = {
717
  "HTTP-PROXY",
718
  CF_TYPE_IP_CONNECT | CF_TYPE_PROXY | CF_TYPE_SETUP,
719
  0,
720
  http_proxy_cf_destroy,
721
  http_proxy_cf_connect,
722
  Curl_cf_def_shutdown,
723
  Curl_cf_def_adjust_pollset,
724
  Curl_cf_def_data_pending,
725
  Curl_cf_def_send,
726
  Curl_cf_def_recv,
727
  Curl_cf_def_cntrl,
728
  Curl_cf_def_conn_is_alive,
729
  Curl_cf_def_conn_keep_alive,
730
  cf_http_proxy_query,
731
};
732
733
CURLcode Curl_cf_http_proxy_insert_after(struct Curl_cfilter *cf_at,
734
                                         struct Curl_easy *data,
735
                                         struct Curl_peer *peer,
736
                                         struct Curl_peer *tunnel_peer,
737
                                         uint8_t tunnel_transport,
738
                                         uint8_t proxytype)
739
0
{
740
0
  struct Curl_cfilter *cf;
741
0
  struct cf_proxy_ctx *ctx = NULL;
742
0
  CURLcode result;
743
744
0
  (void)data;
745
0
  if(!peer || !tunnel_peer)
746
0
    return CURLE_FAILED_INIT;
747
748
0
  ctx = curlx_calloc(1, sizeof(*ctx));
749
0
  if(!ctx) {
750
0
    result = CURLE_OUT_OF_MEMORY;
751
0
    goto out;
752
0
  }
753
0
  Curl_peer_link(&ctx->peer, peer);
754
0
  Curl_peer_link(&ctx->tunnel_peer, tunnel_peer);
755
0
  ctx->proxytype = proxytype;
756
0
  ctx->tunnel_transport = tunnel_transport;
757
758
0
  result = Curl_cf_create(&cf, &Curl_cft_http_proxy, ctx);
759
0
  if(result)
760
0
    goto out;
761
0
  ctx = NULL;
762
0
  Curl_conn_cf_insert_after(cf_at, cf);
763
764
0
out:
765
0
  cf_https_proxy_ctx_free(ctx);
766
0
  return result;
767
0
}
768
769
uint8_t Curl_http_proxy_transport(uint8_t proxytype)
770
0
{
771
0
  switch(proxytype) {
772
0
  case CURLPROXY_HTTPS3:
773
0
    return TRNSPRT_QUIC;
774
0
  default:
775
0
    return TRNSPRT_TCP;
776
0
  }
777
0
}
778
779
#endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY */