Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmcurl/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 "curlx/strparse.h"
39
40
static CURLcode dynhds_add_custom(struct Curl_easy *data,
41
                                  bool is_connect, int httpversion,
42
                                  struct dynhds *hds)
43
0
{
44
0
  struct connectdata *conn = data->conn;
45
0
  struct curl_slist *h[2];
46
0
  struct curl_slist *headers;
47
0
  int numlists = 1; /* by default */
48
0
  int i;
49
50
0
  enum Curl_proxy_use proxy;
51
52
0
  if(is_connect)
53
0
    proxy = HEADER_CONNECT;
54
0
  else
55
0
    proxy = conn->bits.httpproxy && !conn->bits.tunnel_proxy ?
56
0
      HEADER_PROXY : HEADER_SERVER;
57
58
0
  switch(proxy) {
59
0
  case HEADER_SERVER:
60
0
    h[0] = data->set.headers;
61
0
    break;
62
0
  case HEADER_PROXY:
63
0
    h[0] = data->set.headers;
64
0
    if(data->set.sep_headers) {
65
0
      h[1] = data->set.proxyheaders;
66
0
      numlists++;
67
0
    }
68
0
    break;
69
0
  case HEADER_CONNECT:
70
0
    if(data->set.sep_headers)
71
0
      h[0] = data->set.proxyheaders;
72
0
    else
73
0
      h[0] = data->set.headers;
74
0
    break;
75
0
  }
76
77
  /* loop through one or two lists */
78
0
  for(i = 0; i < numlists; i++) {
79
0
    for(headers = h[i]; headers; headers = headers->next) {
80
0
      struct Curl_str name;
81
0
      const char *value = NULL;
82
0
      size_t valuelen = 0;
83
0
      const char *ptr = headers->data;
84
85
      /* There are 2 quirks in place for custom headers:
86
       * 1. setting only 'name:' to suppress a header from being sent
87
       * 2. setting only 'name;' to send an empty (illegal) header
88
       */
89
0
      if(!curlx_str_cspn(&ptr, &name, ";:")) {
90
0
        if(!curlx_str_single(&ptr, ':')) {
91
0
          curlx_str_passblanks(&ptr);
92
0
          if(*ptr) {
93
0
            value = ptr;
94
0
            valuelen = strlen(value);
95
0
          }
96
0
          else {
97
            /* quirk #1, suppress this header */
98
0
            continue;
99
0
          }
100
0
        }
101
0
        else if(!curlx_str_single(&ptr, ';')) {
102
0
          curlx_str_passblanks(&ptr);
103
0
          if(!*ptr) {
104
            /* quirk #2, send an empty header */
105
0
            value = "";
106
0
            valuelen = 0;
107
0
          }
108
0
          else {
109
            /* this may be used for something else in the future,
110
             * ignore this for now */
111
0
            continue;
112
0
          }
113
0
        }
114
0
        else
115
          /* neither : nor ; in provided header value. We ignore this
116
           * silently */
117
0
          continue;
118
0
      }
119
0
      else
120
        /* no name, move on */
121
0
        continue;
122
123
0
      DEBUGASSERT(curlx_strlen(&name) && value);
124
0
      if(data->state.aptr.host &&
125
         /* a Host: header was sent already, do not pass on any custom Host:
126
            header as that will produce *two* in the same request! */
127
0
         curlx_str_casecompare(&name, "Host"))
128
0
        ;
129
0
      else if(data->state.httpreq == HTTPREQ_POST_FORM &&
130
              /* this header (extended by formdata.c) is sent later */
131
0
              curlx_str_casecompare(&name, "Content-Type"))
132
0
        ;
133
0
      else if(data->state.httpreq == HTTPREQ_POST_MIME &&
134
              /* this header is sent later */
135
0
              curlx_str_casecompare(&name, "Content-Type"))
136
0
        ;
137
0
      else if(data->req.authneg &&
138
              /* while doing auth neg, do not allow the custom length since
139
                 we will force length zero then */
140
0
              curlx_str_casecompare(&name, "Content-Length"))
141
0
        ;
142
0
      else if((httpversion >= 20) &&
143
0
              curlx_str_casecompare(&name, "Transfer-Encoding"))
144
0
        ;
145
      /* HTTP/2 and HTTP/3 do not support chunked requests */
146
0
      else if((curlx_str_casecompare(&name, "Authorization") ||
147
0
               curlx_str_casecompare(&name, "Cookie")) &&
148
              /* be careful of sending this potentially sensitive header to
149
                 other hosts */
150
0
              !Curl_auth_allowed_to_host(data))
151
0
        ;
152
0
      else {
153
0
        CURLcode result =
154
0
          Curl_dynhds_add(hds, curlx_str(&name), curlx_strlen(&name),
155
0
                          value, valuelen);
156
0
        if(result)
157
0
          return result;
158
0
      }
159
0
    }
160
0
  }
161
162
0
  return CURLE_OK;
163
0
}
164
165
void Curl_http_proxy_get_destination(struct Curl_cfilter *cf,
166
                                     const char **phostname,
167
                                     uint16_t *pport, bool *pipv6_ip)
168
0
{
169
0
  DEBUGASSERT(cf);
170
0
  DEBUGASSERT(cf->conn);
171
172
0
  if(cf->conn->bits.conn_to_host)
173
0
    *phostname = cf->conn->conn_to_host.name;
174
0
  else if(cf->sockindex == SECONDARYSOCKET)
175
0
    *phostname = cf->conn->secondaryhostname;
176
0
  else
177
0
    *phostname = cf->conn->host.name;
178
179
0
  if(cf->sockindex == SECONDARYSOCKET)
180
0
    *pport = cf->conn->secondary_port;
181
0
  else if(cf->conn->bits.conn_to_port)
182
0
    *pport = cf->conn->conn_to_port;
183
0
  else
184
0
    *pport = cf->conn->remote_port;
185
186
0
  *pipv6_ip = (strchr(*phostname, ':') != NULL);
187
0
}
188
189
struct cf_proxy_ctx {
190
  int httpversion; /* HTTP version used to CONNECT */
191
  BIT(sub_filter_installed);
192
};
193
194
CURLcode Curl_http_proxy_create_CONNECT(struct httpreq **preq,
195
                                        struct Curl_cfilter *cf,
196
                                        struct Curl_easy *data,
197
                                        int http_version_major)
198
0
{
199
0
  struct cf_proxy_ctx *ctx = cf->ctx;
200
0
  const char *hostname = NULL;
201
0
  char *authority = NULL;
202
0
  uint16_t port;
203
0
  bool ipv6_ip;
204
0
  CURLcode result;
205
0
  struct httpreq *req = NULL;
206
207
0
  Curl_http_proxy_get_destination(cf, &hostname, &port, &ipv6_ip);
208
209
0
  authority = curl_maprintf("%s%s%s:%u", ipv6_ip ? "[" : "", hostname,
210
0
                            ipv6_ip ? "]" : "", port);
211
0
  if(!authority) {
212
0
    result = CURLE_OUT_OF_MEMORY;
213
0
    goto out;
214
0
  }
215
216
0
  result = Curl_http_req_make(&req, "CONNECT", sizeof("CONNECT") - 1,
217
0
                              NULL, 0, authority, strlen(authority),
218
0
                              NULL, 0);
219
0
  if(result)
220
0
    goto out;
221
222
  /* Setup the proxy-authorization header, if any */
223
0
  result = Curl_http_output_auth(data, cf->conn, req->method, HTTPREQ_GET,
224
0
                                 req->authority, TRUE);
225
0
  if(result)
226
0
    goto out;
227
228
  /* If user is not overriding Host: header, we add for HTTP/1.x */
229
0
  if(http_version_major == 1 &&
230
0
     !Curl_checkProxyheaders(data, cf->conn, STRCONST("Host"))) {
231
0
    result = Curl_dynhds_cadd(&req->headers, "Host", authority);
232
0
    if(result)
233
0
      goto out;
234
0
  }
235
236
0
  if(data->req.proxyuserpwd) {
237
0
    result = Curl_dynhds_h1_cadd_line(&req->headers,
238
0
                                      data->req.proxyuserpwd);
239
0
    if(result)
240
0
      goto out;
241
0
  }
242
243
0
  if(!Curl_checkProxyheaders(data, cf->conn, STRCONST("User-Agent")) &&
244
0
     data->set.str[STRING_USERAGENT] && *data->set.str[STRING_USERAGENT]) {
245
0
    result = Curl_dynhds_cadd(&req->headers, "User-Agent",
246
0
                              data->set.str[STRING_USERAGENT]);
247
0
    if(result)
248
0
      goto out;
249
0
  }
250
251
0
  if(http_version_major == 1 &&
252
0
     !Curl_checkProxyheaders(data, cf->conn, STRCONST("Proxy-Connection"))) {
253
0
    result = Curl_dynhds_cadd(&req->headers, "Proxy-Connection", "Keep-Alive");
254
0
    if(result)
255
0
      goto out;
256
0
  }
257
258
0
  result = dynhds_add_custom(data, TRUE, ctx->httpversion, &req->headers);
259
260
0
out:
261
0
  if(result && req) {
262
0
    Curl_http_req_free(req);
263
0
    req = NULL;
264
0
  }
265
0
  curlx_free(authority);
266
0
  *preq = req;
267
0
  return result;
268
0
}
269
270
static CURLcode http_proxy_cf_connect(struct Curl_cfilter *cf,
271
                                      struct Curl_easy *data,
272
                                      bool *done)
273
0
{
274
0
  struct cf_proxy_ctx *ctx = cf->ctx;
275
0
  CURLcode result;
276
277
0
  if(cf->connected) {
278
0
    *done = TRUE;
279
0
    return CURLE_OK;
280
0
  }
281
282
0
  CURL_TRC_CF(data, cf, "connect");
283
0
connect_sub:
284
0
  result = cf->next->cft->do_connect(cf->next, data, done);
285
0
  if(result || !*done)
286
0
    return result;
287
288
0
  *done = FALSE;
289
0
  if(!ctx->sub_filter_installed) {
290
0
    int httpversion = 0;
291
0
    const char *alpn = Curl_conn_cf_get_alpn_negotiated(cf->next, data);
292
293
0
    if(alpn)
294
0
      infof(data, "CONNECT: '%s' negotiated", alpn);
295
0
    else
296
0
      infof(data, "CONNECT: no ALPN negotiated");
297
298
0
    if(alpn && !strcmp(alpn, "http/1.0")) {
299
0
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.0");
300
0
      result = Curl_cf_h1_proxy_insert_after(cf, data);
301
0
      if(result)
302
0
        goto out;
303
0
      httpversion = 10;
304
0
    }
305
0
    else if(!alpn || !strcmp(alpn, "http/1.1")) {
306
0
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.1");
307
0
      result = Curl_cf_h1_proxy_insert_after(cf, data);
308
0
      if(result)
309
0
        goto out;
310
      /* Assume that without an ALPN, we are talking to an ancient one */
311
0
      httpversion = 11;
312
0
    }
313
0
#ifdef USE_NGHTTP2
314
0
    else if(!strcmp(alpn, "h2")) {
315
0
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/2");
316
0
      result = Curl_cf_h2_proxy_insert_after(cf, data);
317
0
      if(result)
318
0
        goto out;
319
0
      httpversion = 20;
320
0
    }
321
0
#endif
322
0
    else {
323
0
      failf(data, "CONNECT: negotiated ALPN '%s' not supported", alpn);
324
0
      result = CURLE_COULDNT_CONNECT;
325
0
      goto out;
326
0
    }
327
328
0
    ctx->sub_filter_installed = TRUE;
329
0
    ctx->httpversion = httpversion;
330
    /* after we installed the filter "below" us, we call connect
331
     * on out sub-chain again.
332
     */
333
0
    goto connect_sub;
334
0
  }
335
0
  else {
336
    /* subchain connected and we had already installed the protocol filter.
337
     * This means the protocol tunnel is established, we are done.
338
     */
339
0
    DEBUGASSERT(ctx->sub_filter_installed);
340
0
    result = CURLE_OK;
341
0
  }
342
343
0
out:
344
0
  if(!result) {
345
0
    cf->connected = TRUE;
346
0
    *done = TRUE;
347
0
  }
348
0
  return result;
349
0
}
350
351
CURLcode Curl_cf_http_proxy_query(struct Curl_cfilter *cf,
352
                                  struct Curl_easy *data,
353
                                  int query, int *pres1, void *pres2)
354
0
{
355
0
  switch(query) {
356
0
  case CF_QUERY_HOST_PORT:
357
0
    *pres1 = (int)cf->conn->http_proxy.port;
358
0
    *((const char **)pres2) = cf->conn->http_proxy.host.name;
359
0
    return CURLE_OK;
360
0
  case CF_QUERY_ALPN_NEGOTIATED: {
361
0
    const char **palpn = pres2;
362
0
    DEBUGASSERT(palpn);
363
0
    *palpn = NULL;
364
0
    return CURLE_OK;
365
0
  }
366
0
  default:
367
0
    break;
368
0
  }
369
0
  return cf->next ?
370
0
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
371
0
    CURLE_UNKNOWN_OPTION;
372
0
}
373
374
static void http_proxy_cf_destroy(struct Curl_cfilter *cf,
375
                                  struct Curl_easy *data)
376
0
{
377
0
  struct cf_proxy_ctx *ctx = cf->ctx;
378
379
0
  CURL_TRC_CF(data, cf, "destroy");
380
0
  curlx_free(ctx);
381
0
}
382
383
static void http_proxy_cf_close(struct Curl_cfilter *cf,
384
                                struct Curl_easy *data)
385
0
{
386
0
  CURL_TRC_CF(data, cf, "close");
387
0
  cf->connected = FALSE;
388
0
  if(cf->next)
389
0
    cf->next->cft->do_close(cf->next, data);
390
0
}
391
392
struct Curl_cftype Curl_cft_http_proxy = {
393
  "HTTP-PROXY",
394
  CF_TYPE_IP_CONNECT | CF_TYPE_PROXY | CF_TYPE_SETUP,
395
  0,
396
  http_proxy_cf_destroy,
397
  http_proxy_cf_connect,
398
  http_proxy_cf_close,
399
  Curl_cf_def_shutdown,
400
  Curl_cf_def_adjust_pollset,
401
  Curl_cf_def_data_pending,
402
  Curl_cf_def_send,
403
  Curl_cf_def_recv,
404
  Curl_cf_def_cntrl,
405
  Curl_cf_def_conn_is_alive,
406
  Curl_cf_def_conn_keep_alive,
407
  Curl_cf_http_proxy_query,
408
};
409
410
CURLcode Curl_cf_http_proxy_insert_after(struct Curl_cfilter *cf_at,
411
                                         struct Curl_easy *data)
412
0
{
413
0
  struct Curl_cfilter *cf;
414
0
  struct cf_proxy_ctx *ctx = NULL;
415
0
  CURLcode result;
416
417
0
  (void)data;
418
0
  ctx = curlx_calloc(1, sizeof(*ctx));
419
0
  if(!ctx) {
420
0
    result = CURLE_OUT_OF_MEMORY;
421
0
    goto out;
422
0
  }
423
0
  result = Curl_cf_create(&cf, &Curl_cft_http_proxy, ctx);
424
0
  if(result)
425
0
    goto out;
426
0
  ctx = NULL;
427
0
  Curl_conn_cf_insert_after(cf_at, cf);
428
429
0
out:
430
0
  curlx_free(ctx);
431
0
  return result;
432
0
}
433
434
#endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY */