Coverage Report

Created: 2026-04-29 07:01

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
                                     int *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
  if(*phostname != cf->conn->host.name)
187
0
    *pipv6_ip = (strchr(*phostname, ':') != NULL);
188
0
  else
189
0
    *pipv6_ip = (bool)cf->conn->bits.ipv6_ip;
190
0
}
191
192
struct cf_proxy_ctx {
193
  int httpversion; /* HTTP version used to CONNECT */
194
  BIT(sub_filter_installed);
195
};
196
197
CURLcode Curl_http_proxy_create_CONNECT(struct httpreq **preq,
198
                                        struct Curl_cfilter *cf,
199
                                        struct Curl_easy *data,
200
                                        int http_version_major)
201
0
{
202
0
  struct cf_proxy_ctx *ctx = cf->ctx;
203
0
  const char *hostname = NULL;
204
0
  char *authority = NULL;
205
0
  int port;
206
0
  bool ipv6_ip;
207
0
  CURLcode result;
208
0
  struct httpreq *req = NULL;
209
210
0
  Curl_http_proxy_get_destination(cf, &hostname, &port, &ipv6_ip);
211
212
0
  authority = curl_maprintf("%s%s%s:%d", ipv6_ip ? "[" : "", hostname,
213
0
                            ipv6_ip ? "]" : "", 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", sizeof("CONNECT") - 1,
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, 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(http_version_major == 1 &&
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->state.aptr.proxyuserpwd) {
240
0
    result = Curl_dynhds_h1_cadd_line(&req->headers,
241
0
                                      data->state.aptr.proxyuserpwd);
242
0
    if(result)
243
0
      goto out;
244
0
  }
245
246
0
  if(!Curl_checkProxyheaders(data, cf->conn, STRCONST("User-Agent")) &&
247
0
     data->set.str[STRING_USERAGENT] && *data->set.str[STRING_USERAGENT]) {
248
0
    result = Curl_dynhds_cadd(&req->headers, "User-Agent",
249
0
                              data->set.str[STRING_USERAGENT]);
250
0
    if(result)
251
0
      goto out;
252
0
  }
253
254
0
  if(http_version_major == 1 &&
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, ctx->httpversion, &req->headers);
262
263
0
out:
264
0
  if(result && req) {
265
0
    Curl_http_req_free(req);
266
0
    req = NULL;
267
0
  }
268
0
  curlx_free(authority);
269
0
  *preq = req;
270
0
  return result;
271
0
}
272
273
static CURLcode http_proxy_cf_connect(struct Curl_cfilter *cf,
274
                                      struct Curl_easy *data,
275
                                      bool *done)
276
0
{
277
0
  struct cf_proxy_ctx *ctx = cf->ctx;
278
0
  CURLcode result;
279
280
0
  if(cf->connected) {
281
0
    *done = TRUE;
282
0
    return CURLE_OK;
283
0
  }
284
285
0
  CURL_TRC_CF(data, cf, "connect");
286
0
connect_sub:
287
0
  result = cf->next->cft->do_connect(cf->next, data, done);
288
0
  if(result || !*done)
289
0
    return result;
290
291
0
  *done = FALSE;
292
0
  if(!ctx->sub_filter_installed) {
293
0
    int httpversion = 0;
294
0
    const char *alpn = Curl_conn_cf_get_alpn_negotiated(cf->next, data);
295
296
0
    if(alpn)
297
0
      infof(data, "CONNECT: '%s' negotiated", alpn);
298
0
    else
299
0
      infof(data, "CONNECT: no ALPN negotiated");
300
301
0
    if(alpn && !strcmp(alpn, "http/1.0")) {
302
0
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.0");
303
0
      result = Curl_cf_h1_proxy_insert_after(cf, data);
304
0
      if(result)
305
0
        goto out;
306
0
      httpversion = 10;
307
0
    }
308
0
    else if(!alpn || !strcmp(alpn, "http/1.1")) {
309
0
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.1");
310
0
      result = Curl_cf_h1_proxy_insert_after(cf, data);
311
0
      if(result)
312
0
        goto out;
313
      /* Assume that without an ALPN, we are talking to an ancient one */
314
0
      httpversion = 11;
315
0
    }
316
0
#ifdef USE_NGHTTP2
317
0
    else if(!strcmp(alpn, "h2")) {
318
0
      CURL_TRC_CF(data, cf, "installing subfilter for HTTP/2");
319
0
      result = Curl_cf_h2_proxy_insert_after(cf, data);
320
0
      if(result)
321
0
        goto out;
322
0
      httpversion = 20;
323
0
    }
324
0
#endif
325
0
    else {
326
0
      failf(data, "CONNECT: negotiated ALPN '%s' not supported", alpn);
327
0
      result = CURLE_COULDNT_CONNECT;
328
0
      goto out;
329
0
    }
330
331
0
    ctx->sub_filter_installed = TRUE;
332
0
    ctx->httpversion = httpversion;
333
    /* after we installed the filter "below" us, we call connect
334
     * on out sub-chain again.
335
     */
336
0
    goto connect_sub;
337
0
  }
338
0
  else {
339
    /* subchain connected and we had already installed the protocol filter.
340
     * This means the protocol tunnel is established, we are done.
341
     */
342
0
    DEBUGASSERT(ctx->sub_filter_installed);
343
0
    result = CURLE_OK;
344
0
  }
345
346
0
out:
347
0
  if(!result) {
348
0
    cf->connected = TRUE;
349
0
    *done = TRUE;
350
0
  }
351
0
  return result;
352
0
}
353
354
CURLcode Curl_cf_http_proxy_query(struct Curl_cfilter *cf,
355
                                  struct Curl_easy *data,
356
                                  int query, int *pres1, void *pres2)
357
0
{
358
0
  switch(query) {
359
0
  case CF_QUERY_HOST_PORT:
360
0
    *pres1 = (int)cf->conn->http_proxy.port;
361
0
    *((const char **)pres2) = cf->conn->http_proxy.host.name;
362
0
    return CURLE_OK;
363
0
  case CF_QUERY_ALPN_NEGOTIATED: {
364
0
    const char **palpn = pres2;
365
0
    DEBUGASSERT(palpn);
366
0
    *palpn = NULL;
367
0
    return CURLE_OK;
368
0
  }
369
0
  default:
370
0
    break;
371
0
  }
372
0
  return cf->next ?
373
0
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
374
0
    CURLE_UNKNOWN_OPTION;
375
0
}
376
377
static void http_proxy_cf_destroy(struct Curl_cfilter *cf,
378
                                  struct Curl_easy *data)
379
0
{
380
0
  struct cf_proxy_ctx *ctx = cf->ctx;
381
382
0
  CURL_TRC_CF(data, cf, "destroy");
383
0
  curlx_free(ctx);
384
0
}
385
386
static void http_proxy_cf_close(struct Curl_cfilter *cf,
387
                                struct Curl_easy *data)
388
0
{
389
0
  CURL_TRC_CF(data, cf, "close");
390
0
  cf->connected = FALSE;
391
0
  if(cf->next)
392
0
    cf->next->cft->do_close(cf->next, data);
393
0
}
394
395
struct Curl_cftype Curl_cft_http_proxy = {
396
  "HTTP-PROXY",
397
  CF_TYPE_IP_CONNECT | CF_TYPE_PROXY,
398
  0,
399
  http_proxy_cf_destroy,
400
  http_proxy_cf_connect,
401
  http_proxy_cf_close,
402
  Curl_cf_def_shutdown,
403
  Curl_cf_def_adjust_pollset,
404
  Curl_cf_def_data_pending,
405
  Curl_cf_def_send,
406
  Curl_cf_def_recv,
407
  Curl_cf_def_cntrl,
408
  Curl_cf_def_conn_is_alive,
409
  Curl_cf_def_conn_keep_alive,
410
  Curl_cf_http_proxy_query,
411
};
412
413
CURLcode Curl_cf_http_proxy_insert_after(struct Curl_cfilter *cf_at,
414
                                         struct Curl_easy *data)
415
0
{
416
0
  struct Curl_cfilter *cf;
417
0
  struct cf_proxy_ctx *ctx = NULL;
418
0
  CURLcode result;
419
420
0
  (void)data;
421
0
  ctx = curlx_calloc(1, sizeof(*ctx));
422
0
  if(!ctx) {
423
0
    result = CURLE_OUT_OF_MEMORY;
424
0
    goto out;
425
0
  }
426
0
  result = Curl_cf_create(&cf, &Curl_cft_http_proxy, ctx);
427
0
  if(result)
428
0
    goto out;
429
0
  ctx = NULL;
430
0
  Curl_conn_cf_insert_after(cf_at, cf);
431
432
0
out:
433
0
  curlx_free(ctx);
434
0
  return result;
435
0
}
436
437
#endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY */