Coverage Report

Created: 2026-09-14 07:06

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