Coverage Report

Created: 2026-11-12 08:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/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(!curlx_strlen(&name))
136
0
        continue;
137
0
      if(data->state.http_host &&
138
         /* a Host: header was sent already, do not pass on any custom Host:
139
            header as that will produce *two* in the same request! */
140
0
         curlx_str_casecompare(&name, "Host"))
141
0
        ;
142
0
      else if(data->state.httpreq == HTTPREQ_POST_FORM &&
143
              /* this header (extended by formdata.c) is sent later */
144
0
              curlx_str_casecompare(&name, "Content-Type"))
145
0
        ;
146
0
      else if(data->state.httpreq == HTTPREQ_POST_MIME &&
147
              /* this header is sent later */
148
0
              curlx_str_casecompare(&name, "Content-Type"))
149
0
        ;
150
0
      else if(data->req.authneg &&
151
              /* while doing auth neg, do not allow the custom length since
152
                 we will force length zero then */
153
0
              curlx_str_casecompare(&name, "Content-Length"))
154
0
        ;
155
0
      else if((httpversion >= 20) &&
156
0
              curlx_str_casecompare(&name, "Transfer-Encoding"))
157
0
        ;
158
      /* HTTP/2 and HTTP/3 do not support chunked requests */
159
0
      else if((curlx_str_casecompare(&name, "Authorization") ||
160
0
               curlx_str_casecompare(&name, "Cookie")) &&
161
              /* be careful of sending this potentially sensitive header to
162
                 other hosts */
163
0
              !Curl_auth_allowed_to_host(data))
164
0
        ;
165
0
      else {
166
0
        CURLcode result =
167
0
          Curl_dynhds_add(hds, curlx_str(&name), curlx_strlen(&name),
168
0
                          value, valuelen);
169
0
        if(result)
170
0
          return result;
171
0
      }
172
0
    }
173
0
  }
174
175
0
  return CURLE_OK;
176
0
}
177
178
struct cf_proxy_ctx {
179
  struct Curl_peer *peer; /* proxy */
180
  struct Curl_peer *tunnel_peer; /* tunnel destination */
181
  uint8_t proxytype;
182
  uint8_t tunnel_transport;
183
  BIT(sub_filter_installed);
184
};
185
186
static int proxy_http_ver_major(proxy_http_ver ver)
187
0
{
188
0
  switch(ver) {
189
0
  case PROXY_HTTP_V1:
190
0
    return 11;
191
0
  case PROXY_HTTP_V2:
192
0
    return 20;
193
0
  case PROXY_HTTP_V3:
194
0
    return 30;
195
0
  }
196
0
  return 0;
197
0
}
198
199
static CURLcode http_proxy_create_CONNECT(struct httpreq **preq,
200
                                          struct Curl_cfilter *cf,
201
                                          struct Curl_easy *data,
202
                                          struct Curl_peer *peer,
203
                                          struct Curl_peer *dest,
204
                                          proxy_http_ver ver)
205
0
{
206
0
  char *authority = NULL;
207
0
  const char *ua;
208
0
  int httpversion = proxy_http_ver_major(ver);
209
0
  CURLcode result;
210
0
  struct httpreq *req = NULL;
211
212
0
  (void)peer;
213
0
  authority = curl_maprintf("%s%s%s:%u",
214
0
                            dest->ipv6 ? "[" : "",
215
0
                            dest->hostname,
216
0
                            dest->ipv6 ? "]" : "",
217
0
                            dest->port);
218
0
  if(!authority) {
219
0
    result = CURLE_OUT_OF_MEMORY;
220
0
    goto out;
221
0
  }
222
223
0
  result = Curl_http_req_make(&req, STRCONST("CONNECT"),
224
0
                              NULL, 0, authority, strlen(authority),
225
0
                              NULL, 0);
226
0
  if(result)
227
0
    goto out;
228
229
  /* Setup the proxy-authorization header, if any */
230
0
  result = Curl_http_output_auth(data, cf->conn, req->method, HTTPREQ_GET,
231
0
                                 req->authority, NULL, TRUE);
232
0
  if(result)
233
0
    goto out;
234
235
  /* If user is not overriding Host: header, we add for HTTP/1.x */
236
0
  if(ver == PROXY_HTTP_V1 &&
237
0
     !Curl_checkProxyheaders(data, cf->conn, STRCONST("Host"))) {
238
0
    result = Curl_dynhds_cadd(&req->headers, "Host", authority);
239
0
    if(result)
240
0
      goto out;
241
0
  }
242
243
0
  if(data->req.hd_proxy_auth) {
244
0
    result = Curl_dynhds_h1_cadd_line(&req->headers,
245
0
                                      data->req.hd_proxy_auth);
246
0
    if(result)
247
0
      goto out;
248
0
  }
249
250
0
  ua = CURL_EASY_STR(data, STRING_USERAGENT);
251
0
  if(!Curl_checkProxyheaders(data, cf->conn, STRCONST("User-Agent")) &&
252
0
     ua && *ua) {
253
0
    result = Curl_dynhds_cadd(&req->headers, "User-Agent", ua);
254
0
    if(result)
255
0
      goto out;
256
0
  }
257
258
0
  if(ver == PROXY_HTTP_V1 &&
259
0
     !Curl_checkProxyheaders(data, cf->conn, STRCONST("Proxy-Connection"))) {
260
0
    result = Curl_dynhds_cadd(&req->headers, "Proxy-Connection", "Keep-Alive");
261
0
    if(result)
262
0
      goto out;
263
0
  }
264
265
0
  result = dynhds_add_custom(data, TRUE, httpversion,
266
0
                             FALSE, &req->headers);
267
268
0
out:
269
0
  if(result && req) {
270
0
    Curl_http_req_free(req);
271
0
    req = NULL;
272
0
  }
273
0
  curlx_safefree(data->req.hd_proxy_auth); /* not needed anymore */
274
0
  curlx_free(authority);
275
0
  *preq = req;
276
0
  return result;
277
0
}
278
279
static CURLcode http_proxy_create_CONNECTUDP(struct httpreq **preq,
280
                                             struct Curl_cfilter *cf,
281
                                             struct Curl_easy *data,
282
                                             struct Curl_peer *peer,
283
                                             struct Curl_peer *dest,
284
                                             proxy_http_ver ver)
285
0
{
286
0
  const char *proxy_scheme = "http", *ua;
287
0
  int httpversion = proxy_http_ver_major(ver);
288
0
  char *authority = NULL;
289
0
  char *path = NULL;
290
0
  char *encoded_host = NULL;
291
0
  struct httpreq *req = NULL;
292
0
  CURLcode result = CURLE_OUT_OF_MEMORY;
293
294
0
  if(cf->conn->http_proxy.proxytype == CURLPROXY_HTTPS ||
295
0
     cf->conn->http_proxy.proxytype == CURLPROXY_HTTPS2 ||
296
0
     cf->conn->http_proxy.proxytype == CURLPROXY_HTTPS3)
297
0
    proxy_scheme = "https";
298
299
0
  authority = curl_maprintf("%s%s%s:%d",
300
0
                            peer->ipv6 ? "[" : "",
301
0
                            peer->hostname,
302
0
                            peer->ipv6 ? "]" : "",
303
0
                            peer->port);
304
0
  if(!authority)
305
0
    goto out;
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
0
  if(!path)
338
0
    goto out;
339
340
0
  if(ver == PROXY_HTTP_V1) {
341
0
    result = Curl_http_req_make(&req, "GET", CURL_CSTRLEN("GET"),
342
0
                                proxy_scheme, strlen(proxy_scheme),
343
0
                                authority, strlen(authority),
344
0
                                path, strlen(path));
345
0
  }
346
0
  else if(ver == PROXY_HTTP_V2 || ver == PROXY_HTTP_V3) {
347
0
    result = Curl_http_req_make(&req, STRCONST("CONNECT"),
348
0
                                proxy_scheme, strlen(proxy_scheme),
349
0
                                authority, strlen(authority),
350
0
                                path, strlen(path));
351
0
  }
352
0
  else
353
0
    result = CURLE_FAILED_INIT;
354
0
  if(result)
355
0
    goto out;
356
357
0
  if(ver > PROXY_HTTP_V1) {
358
    /* Pseudo header must go first */
359
0
    result = Curl_dynhds_cadd(&req->headers, ":Protocol", "connect-udp");
360
0
  }
361
0
  else {
362
    /* If user is not overriding Host: header, we add for HTTP/1.x */
363
0
    if(!Curl_checkProxyheaders(data, cf->conn, STRCONST("Host"))) {
364
0
      result = Curl_dynhds_cadd(&req->headers, "Host", authority);
365
0
    }
366
0
    if(!result &&
367
0
       !Curl_checkProxyheaders(data, cf->conn, STRCONST("Proxy-Connection"))) {
368
0
      result = Curl_dynhds_cadd(&req->headers, "Proxy-Connection",
369
0
                                "Keep-Alive");
370
0
    }
371
0
    if(!result)
372
0
      result = Curl_dynhds_cadd(&req->headers, "Connection", "Upgrade");
373
0
    if(!result)
374
0
      result = Curl_dynhds_cadd(&req->headers, "Upgrade", "connect-udp");
375
0
  }
376
377
0
  if(!result)
378
    /* Setup the proxy-authorization header, if any */
379
0
    result = Curl_http_output_auth(data, cf->conn, req->method, HTTPREQ_GET,
380
0
                                   req->authority, NULL, TRUE);
381
0
  if(!result && data->req.hd_proxy_auth)
382
0
    result = Curl_dynhds_h1_cadd_line(&req->headers,
383
0
                                      data->req.hd_proxy_auth);
384
0
  if(!result)
385
0
    result = Curl_dynhds_cadd(&req->headers, "Capsule-Protocol", "?1");
386
0
  if(!result) {
387
0
    ua = CURL_EASY_STR(data, STRING_USERAGENT);
388
0
    if(ver == PROXY_HTTP_V1 &&
389
0
       !Curl_checkProxyheaders(data, cf->conn, STRCONST("User-Agent")) &&
390
0
       ua && *ua) {
391
0
      result = Curl_dynhds_cadd(&req->headers, "User-Agent", ua);
392
0
      if(result)
393
0
        goto out;
394
0
    }
395
0
  }
396
0
  if(!result)
397
0
    result = dynhds_add_custom(data, TRUE, httpversion, TRUE, &req->headers);
398
399
0
out:
400
0
  if(result && req) {
401
0
    Curl_http_req_free(req);
402
0
    req = NULL;
403
0
  }
404
0
  curlx_safefree(data->req.hd_proxy_auth); /* not needed any more */
405
0
  curlx_free(authority);
406
0
  curlx_free(path);
407
0
  curlx_free(encoded_host);
408
0
  *preq = req;
409
0
  return result;
410
0
}
411
412
CURLcode Curl_http_proxy_create_tunnel_request(
413
    struct httpreq **preq, struct Curl_cfilter *cf,
414
    struct Curl_easy *data, struct Curl_peer *peer, struct Curl_peer *dest,
415
    proxy_http_ver ver, bool udp_tunnel)
416
0
{
417
0
  CURLcode result = udp_tunnel ?
418
0
    http_proxy_create_CONNECTUDP(preq, cf, data, peer, dest, ver) :
419
0
    http_proxy_create_CONNECT(preq, cf, data, peer, dest, ver);
420
0
  if(result)
421
0
    return result;
422
423
0
  infof(data, "Establishing %s proxy %stunnel to %s:%u",
424
0
        (ver == PROXY_HTTP_V2) ? "HTTP/2" :
425
0
        (ver == PROXY_HTTP_V3) ? "HTTP/3" : "HTTP",
426
0
        udp_tunnel ? "UDP " : "",
427
0
        dest->user_hostname, dest->port);
428
0
  return CURLE_OK;
429
0
}
430
431
CURLcode Curl_http_proxy_inspect_tunnel_response(
432
    struct Curl_cfilter *cf, struct Curl_easy *data,
433
    struct http_resp *resp, bool udp_tunnel,
434
    proxy_inspect_result *presult)
435
0
{
436
0
  struct dynhds_entry *capsule_protocol = NULL;
437
0
  struct dynhds_entry *auth_reply = NULL;
438
0
  size_t i, header_count;
439
0
  CURLcode result = CURLE_OK;
440
441
0
  DEBUGASSERT(resp);
442
443
0
  header_count = Curl_dynhds_count(&resp->headers);
444
0
  infof(data, "CONNECT%s Response Status %d",
445
0
        udp_tunnel ? "-UDP" : "", resp->status);
446
0
  infof(data, "Response Headers (%zu total):", header_count);
447
0
  for(i = 0; i < header_count; i++) {
448
0
    struct dynhds_entry *entry = Curl_dynhds_getn(&resp->headers, i);
449
0
    if(entry)
450
0
      infof(data, "  %s: %s", entry->name, entry->value);
451
0
  }
452
453
0
  if(resp->status == 407) {
454
0
    auth_reply = Curl_dynhds_cget(&resp->headers, "Proxy-Authenticate");
455
0
  }
456
457
0
  if(auth_reply) {
458
0
    CURL_TRC_CF(data, cf, "[0] CONNECT%s: fwd auth header '%s'",
459
0
                udp_tunnel ? "-UDP" : "", auth_reply->value);
460
0
    result = Curl_http_input_auth(data, resp->status == 407,
461
0
                                  auth_reply->value);
462
0
    if(result)
463
0
      return result;
464
0
    if(data->req.newurl) {
465
0
      curlx_safefree(data->req.newurl);
466
0
      *presult = PROXY_INSPECT_AUTH_RETRY;
467
0
      return CURLE_OK;
468
0
    }
469
0
  }
470
471
0
  if(udp_tunnel) {
472
0
    if(resp->status / 100 == 2) {
473
0
      capsule_protocol = Curl_dynhds_cget(&resp->headers,
474
0
                                           "capsule-protocol");
475
0
      if(capsule_protocol) {
476
0
        if(!strncmp(capsule_protocol->value, "?1", 2) &&
477
0
           !capsule_protocol->value[2]) {
478
0
          infof(data, "CONNECT-UDP tunnel established, response %d",
479
0
                resp->status);
480
0
          *presult = PROXY_INSPECT_OK;
481
0
          return CURLE_OK;
482
0
        }
483
0
        failf(data, "Failed to establish CONNECT-UDP tunnel, response %d, "
484
0
              "unsupported capsule-protocol value '%s'",
485
0
              resp->status, capsule_protocol->value);
486
0
        *presult = PROXY_INSPECT_FAILED;
487
0
        return CURLE_COULDNT_CONNECT;
488
0
      }
489
0
      else {
490
        /* NOTE proxies may not set capsule protocol in the headers */
491
0
        infof(data, "CONNECT-UDP tunnel established, response %d "
492
0
                    "but no capsule-protocol header found", resp->status);
493
0
        *presult = PROXY_INSPECT_OK;
494
0
        return CURLE_OK;
495
0
      }
496
0
    }
497
0
    else {
498
0
      failf(data, "Failed to establish CONNECT-UDP tunnel, "
499
0
                  "response %d", resp->status);
500
0
      *presult = PROXY_INSPECT_FAILED;
501
0
      return CURLE_COULDNT_CONNECT;
502
0
    }
503
0
  }
504
505
0
  if(resp->status / 100 == 2) {
506
0
    infof(data, "CONNECT tunnel established, response %d", resp->status);
507
0
    *presult = PROXY_INSPECT_OK;
508
0
    return CURLE_OK;
509
0
  }
510
511
0
  *presult = PROXY_INSPECT_FAILED;
512
0
  return CURLE_COULDNT_CONNECT;
513
0
}
514
515
static CURLcode http_proxy_cf_connect(struct Curl_cfilter *cf,
516
                                      struct Curl_easy *data,
517
                                      bool *done)
518
0
{
519
0
  struct cf_proxy_ctx *ctx = cf->ctx;
520
0
  CURLcode result;
521
0
  bool udp_tunnel = TRNSPRT_IS_DGRAM(ctx->tunnel_transport);
522
523
0
  if(cf->connected) {
524
0
    *done = TRUE;
525
0
    return CURLE_OK;
526
0
  }
527
528
0
  CURL_TRC_CF(data, cf, "CONNECT%s", udp_tunnel ? "-UDP" : "");
529
0
connect_sub:
530
  /* in case of h3_proxy, cf->next will be NULL initially */
531
0
  if(cf->next) {
532
0
    result = cf->next->cft->do_connect(cf->next, data, done);
533
0
    if(result || !*done)
534
0
      return result;
535
0
  }
536
537
0
  *done = FALSE;
538
0
  if(!ctx->sub_filter_installed) {
539
0
    const char *alpn = NULL;
540
541
    /* in case of h3_proxy, cf->next will be NULL initially */
542
0
    if(cf->next) {
543
0
      alpn = Curl_conn_cf_get_alpn_negotiated(cf->next, data);
544
0
    }
545
546
0
    if(alpn)
547
0
      infof(data, "CONNECT%s: '%s' negotiated",
548
0
            udp_tunnel ? "-UDP" : "", alpn);
549
0
    else if(!alpn) {
550
      /* No ALPN, proxytype rules. Fake ALPN */
551
0
      infof(data, "CONNECT%s: no ALPN negotiated", udp_tunnel ? "-UDP" : "");
552
0
      switch(ctx->proxytype) {
553
0
      case CURLPROXY_HTTP_1_0:
554
0
        alpn = "http/1.0";
555
0
        break;
556
0
      case CURLPROXY_HTTPS2:
557
0
        alpn = "h2";
558
0
        break;
559
0
      case CURLPROXY_HTTPS3:
560
0
        alpn = "h3";
561
0
        break;
562
0
      default:
563
0
        alpn = "http/1.1";
564
0
        break;
565
0
      }
566
0
    }
567
568
0
    if(!strcmp(alpn, "http/1.0")) {
569
0
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.0");
570
0
      result = Curl_cf_h1_proxy_insert_after(cf, data, ctx->peer,
571
0
                                             ctx->tunnel_peer, 10, udp_tunnel);
572
0
      if(result)
573
0
        goto out;
574
0
    }
575
0
    else if(!strcmp(alpn, "http/1.1")) {
576
0
      int httpversion = (ctx->proxytype == CURLPROXY_HTTP_1_0) ? 10 : 11;
577
0
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.%d",
578
0
                  httpversion % 10);
579
0
      result = Curl_cf_h1_proxy_insert_after(cf, data, ctx->peer,
580
0
                                             ctx->tunnel_peer,
581
0
                                             httpversion, udp_tunnel);
582
0
      if(result)
583
0
        goto out;
584
0
    }
585
#ifdef USE_NGHTTP2
586
    else if(!strcmp(alpn, "h2")) {
587
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/2");
588
      result = Curl_cf_h2_proxy_insert_after(cf, data, ctx->peer,
589
                                             ctx->tunnel_peer, udp_tunnel);
590
      if(result)
591
        goto out;
592
    }
593
#endif /* USE_NGHTTP2 */
594
#if defined(USE_PROXY_HTTP3) && defined(USE_NGHTTP3) && \
595
  defined(USE_NGTCP2) && defined(USE_OPENSSL)
596
    else if(!strcmp(alpn, "h3")) {
597
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/3");
598
      result = Curl_cf_h3_proxy_insert_after(cf, data, ctx->peer, ctx->peer,
599
                                             ctx->tunnel_peer,
600
                                             ctx->tunnel_transport);
601
      if(result)
602
        goto out;
603
    }
604
#endif /* USE_PROXY_HTTP3 && USE_NGHTTP3 && USE_NGTCP2 && USE_OPENSSL */
605
0
    else {
606
0
      failf(data, "CONNECT%s: negotiated ALPN '%s' not supported",
607
0
            udp_tunnel ? "-UDP" : "", alpn);
608
0
      result = CURLE_COULDNT_CONNECT;
609
0
      goto out;
610
0
    }
611
612
0
    ctx->sub_filter_installed = TRUE;
613
    /* after we installed the filter "below" us, we call connect
614
     * on out sub-chain again.
615
     */
616
0
    goto connect_sub;
617
0
  }
618
0
  else {
619
    /* subchain connected and we had already installed the protocol filter.
620
     * This means the protocol tunnel is established, we are done. */
621
0
    DEBUGASSERT(ctx->sub_filter_installed);
622
0
    result = CURLE_OK;
623
0
  }
624
625
0
out:
626
0
  if(!result) {
627
0
    cf->connected = TRUE;
628
0
    *done = TRUE;
629
0
  }
630
0
  return result;
631
0
}
632
633
static CURLcode cf_http_proxy_query(struct Curl_cfilter *cf,
634
                                    struct Curl_easy *data,
635
                                    int query, int *pres1, void *pres2)
636
0
{
637
0
  struct cf_proxy_ctx *ctx = cf->ctx;
638
0
  switch(query) {
639
0
  case CF_QUERY_HOST_PORT:
640
0
    *pres1 = (int)ctx->tunnel_peer->port;
641
0
    *((const char **)pres2) = ctx->tunnel_peer->hostname;
642
0
    return CURLE_OK;
643
0
  case CF_QUERY_ALPN_NEGOTIATED: {
644
0
    const char **palpn = pres2;
645
0
    DEBUGASSERT(palpn);
646
0
    *palpn = NULL;
647
0
    return CURLE_OK;
648
0
  }
649
0
  default:
650
0
    break;
651
0
  }
652
0
  return cf->next ?
653
0
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
654
0
    CURLE_UNKNOWN_OPTION;
655
0
}
656
657
static void cf_https_proxy_ctx_free(struct cf_proxy_ctx *ctx)
658
0
{
659
0
  if(ctx) {
660
0
    Curl_peer_unlink(&ctx->peer);
661
0
    Curl_peer_unlink(&ctx->tunnel_peer);
662
0
    curlx_free(ctx);
663
0
  }
664
0
}
665
666
static void http_proxy_cf_destroy(struct Curl_cfilter *cf,
667
                                  struct Curl_easy *data)
668
0
{
669
0
  struct cf_proxy_ctx *ctx = cf->ctx;
670
0
  if(ctx) {
671
0
    CURL_TRC_CF(data, cf, "destroy");
672
0
    cf_https_proxy_ctx_free(ctx);
673
0
  }
674
0
}
675
676
struct Curl_cftype Curl_cft_http_proxy = {
677
  "HTTP-PROXY",
678
  CF_TYPE_IP_CONNECT | CF_TYPE_PROXY | CF_TYPE_SETUP,
679
  0,
680
  http_proxy_cf_destroy,
681
  http_proxy_cf_connect,
682
  Curl_cf_def_shutdown,
683
  Curl_cf_def_adjust_pollset,
684
  Curl_cf_def_data_pending,
685
  Curl_cf_def_send,
686
  Curl_cf_def_recv,
687
  Curl_cf_def_cntrl,
688
  Curl_cf_def_conn_is_alive,
689
  Curl_cf_def_conn_keep_alive,
690
  cf_http_proxy_query,
691
};
692
693
CURLcode Curl_cf_http_proxy_insert_after(struct Curl_cfilter *cf_at,
694
                                         struct Curl_easy *data,
695
                                         struct Curl_peer *peer,
696
                                         struct Curl_peer *tunnel_peer,
697
                                         uint8_t tunnel_transport,
698
                                         uint8_t proxytype)
699
0
{
700
0
  struct Curl_cfilter *cf;
701
0
  struct cf_proxy_ctx *ctx = NULL;
702
0
  CURLcode result;
703
704
0
  (void)data;
705
0
  if(!peer || !tunnel_peer)
706
0
    return CURLE_FAILED_INIT;
707
708
0
  ctx = curlx_calloc(1, sizeof(*ctx));
709
0
  if(!ctx) {
710
0
    result = CURLE_OUT_OF_MEMORY;
711
0
    goto out;
712
0
  }
713
0
  Curl_peer_link(&ctx->peer, peer);
714
0
  Curl_peer_link(&ctx->tunnel_peer, tunnel_peer);
715
0
  ctx->proxytype = proxytype;
716
0
  ctx->tunnel_transport = tunnel_transport;
717
718
0
  result = Curl_cf_create(&cf, &Curl_cft_http_proxy, ctx);
719
0
  if(result)
720
0
    goto out;
721
0
  ctx = NULL;
722
0
  Curl_conn_cf_insert_after(cf_at, cf);
723
724
0
out:
725
0
  cf_https_proxy_ctx_free(ctx);
726
0
  return result;
727
0
}
728
729
uint8_t Curl_http_proxy_transport(uint8_t proxytype)
730
114
{
731
114
  switch(proxytype) {
732
0
  case CURLPROXY_HTTPS3:
733
0
    return TRNSPRT_QUIC;
734
114
  default:
735
114
    return TRNSPRT_TCP;
736
114
  }
737
114
}
738
739
#endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY */