Coverage Report

Created: 2026-10-22 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/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
#ifndef CURL_DISABLE_PROXY
27
28
#include "urldata.h"
29
#include "curl_trc.h"
30
#include "protocol.h"
31
#include "proxy.h"
32
#include "http_proxy.h"
33
#include "strcase.h"
34
#include "url.h"
35
#include "vauth/vauth.h"
36
#include "curlx/inet_pton.h"
37
#include "curlx/strparse.h"
38
39
#ifdef HAVE_NETINET_IN_H
40
#include <netinet/in.h>
41
#endif
42
43
#ifdef HAVE_ARPA_INET_H
44
#include <arpa/inet.h>
45
#endif
46
47
/*
48
 * cidr4_match() returns TRUE if the given IPv4 address is within the
49
 * specified CIDR address range.
50
 *
51
 * bits is passed as -1 if not provided.
52
 *
53
 * @unittest 1614
54
 */
55
UNITTEST bool cidr4_match(const char *ipv4,    /* 1.2.3.4 address */
56
                          const char *network, /* 1.2.3.4 address */
57
                          int bits);
58
UNITTEST bool cidr4_match(const char *ipv4,    /* 1.2.3.4 address */
59
                          const char *network, /* 1.2.3.4 address */
60
                          int bits)
61
3.45k
{
62
3.45k
  unsigned int address = 0;
63
3.45k
  unsigned int check = 0;
64
65
3.45k
  if(bits > 32)
66
    /* strange input */
67
727
    return FALSE;
68
69
2.73k
  if(curlx_inet_pton(AF_INET, ipv4, &address) != 1)
70
0
    return FALSE;
71
2.73k
  if(curlx_inet_pton(AF_INET, network, &check) != 1)
72
2.62k
    return FALSE;
73
74
108
  if((bits > 0) && (bits != 32)) {
75
10
    unsigned int mask = 0xffffffff << (32 - bits);
76
10
    unsigned int haddr = htonl(address);
77
10
    unsigned int hcheck = htonl(check);
78
#if 0
79
    curl_mfprintf(stderr, "Host %s (%x) network %s (%x) "
80
                  "bits %u mask %x => %x\n",
81
                  ipv4, haddr, network, hcheck, bits, mask,
82
                  (haddr ^ hcheck) & mask);
83
#endif
84
10
    if((haddr ^ hcheck) & mask)
85
4
      return FALSE;
86
6
    return TRUE;
87
10
  }
88
98
  else if(!bits && !check)
89
    /* 0.0.0.0/0 matches everything */
90
0
    return TRUE;
91
92
98
  return address == check;
93
108
}
94
95
/*
96
 * cidr6_match() returns TRUE if the given IPv6 address is within the
97
 * specified CIDR address range.
98
 *
99
 * bits is passed as -1 if not provided.
100
 *
101
 * @unittest 1614
102
 */
103
UNITTEST bool cidr6_match(const char *ipv6, const char *network,
104
                          int bits);
105
UNITTEST bool cidr6_match(const char *ipv6, const char *network,
106
                          int bits)
107
1.41k
{
108
1.41k
#ifdef USE_IPV6
109
1.41k
  static const char null6[16]= { 0 };
110
1.41k
  unsigned int bytes;
111
1.41k
  unsigned int rest;
112
1.41k
  unsigned char address[16];
113
1.41k
  unsigned char check[16];
114
115
1.41k
  if(bits == -1)
116
1.08k
    bits = 128;
117
118
1.41k
  bytes = bits / 8;
119
1.41k
  rest = bits & 0x07;
120
1.41k
  if((bytes > 16) || ((bytes == 16) && rest))
121
0
    return FALSE;
122
1.41k
  if(curlx_inet_pton(AF_INET6, ipv6, address) != 1)
123
0
    return FALSE;
124
1.41k
  if(curlx_inet_pton(AF_INET6, network, check) != 1)
125
1.22k
    return FALSE;
126
191
  if((bytes == 16) && !bits && !memcmp(null6, check, 16))
127
    /* ::/0 matches everything */
128
0
    return TRUE;
129
191
  if(bytes && memcmp(address, check, bytes))
130
90
    return FALSE;
131
101
  if(rest && ((address[bytes] ^ check[bytes]) & (0xff << (8 - rest))))
132
87
    return FALSE;
133
134
14
  return TRUE;
135
#else
136
  (void)ipv6;
137
  (void)network;
138
  (void)bits;
139
  return FALSE;
140
#endif
141
101
}
142
143
enum nametype {
144
  TYPE_HOST,
145
  TYPE_IPV4,
146
  TYPE_IPV6
147
};
148
149
static bool match_host(const char *token, size_t tokenlen,
150
                       const char *name, size_t namelen)
151
2.52k
{
152
2.52k
  bool match = FALSE;
153
154
  /* ignore trailing dots in the token to check */
155
2.52k
  if(token[tokenlen - 1] == '.')
156
481
    tokenlen--;
157
158
2.52k
  if(tokenlen && (*token == '.')) {
159
    /* ignore leading token dot as well */
160
310
    token++;
161
310
    tokenlen--;
162
310
  }
163
  /* A: example.com matches 'example.com'
164
     B: www.example.com matches 'example.com'
165
     C: nonexample.com DOES NOT match 'example.com'
166
   */
167
2.52k
  if(tokenlen == namelen)
168
    /* case A, exact match */
169
658
    match = curl_strnequal(token, name, namelen);
170
1.86k
  else if(tokenlen < namelen) {
171
    /* case B, tailmatch domain */
172
776
    match = (name[namelen - tokenlen - 1] == '.') &&
173
197
            curl_strnequal(token, name + (namelen - tokenlen), tokenlen);
174
776
  }
175
  /* case C passes through, not a match */
176
2.52k
  return match;
177
2.52k
}
178
179
static bool match_ip(int type, const char *token, size_t tokenlen,
180
                     const char *name)
181
6.10k
{
182
6.10k
  char *slash;
183
6.10k
  int bits = -1; /* negative means not provided */
184
6.10k
  char checkip[128];
185
6.10k
  if(tokenlen >= sizeof(checkip))
186
    /* this cannot match */
187
74
    return FALSE;
188
  /* copy the check name to a temp buffer */
189
6.02k
  memcpy(checkip, token, tokenlen);
190
6.02k
  checkip[tokenlen] = 0;
191
192
6.02k
  slash = strchr(checkip, '/');
193
  /* if the slash is part of this token, use it */
194
6.02k
  if(slash) {
195
2.68k
    curl_off_t value;
196
2.68k
    const char *p = &slash[1];
197
2.68k
    if(curlx_str_number(&p, &value, 128) || *p)
198
1.15k
      return FALSE;
199
    /* a too large value is rejected in the cidr function below */
200
1.53k
    bits = (int)value;
201
1.53k
    *slash = 0; /* null-terminate there */
202
1.53k
  }
203
4.87k
  if(type == TYPE_IPV6)
204
1.41k
    return cidr6_match(name, checkip, bits);
205
3.45k
  else
206
3.45k
    return cidr4_match(name, checkip, bits);
207
4.87k
}
208
209
/****************************************************************
210
 * Checks if the host is in the noproxy list. returns TRUE if it matches and
211
 * therefore the proxy should NOT be used.
212
 ****************************************************************/
213
/* @unittest 1614 */
214
UNITTEST bool proxy_check_noproxy(const char *name, const char *no_proxy);
215
UNITTEST bool proxy_check_noproxy(const char *name, const char *no_proxy)
216
23.0k
{
217
  /*
218
   * If we do not have a hostname at all, like for example with a FILE
219
   * transfer, we have nothing to interrogate the noproxy list with.
220
   */
221
23.0k
  if(!name || name[0] == '\0')
222
0
    return FALSE;
223
224
  /* no_proxy=domain1.dom,host.domain2.dom
225
   *   (a comma-separated list of hosts which should
226
   *   not be proxied, or an asterisk to override
227
   *   all proxy variables)
228
   */
229
23.0k
  if(no_proxy && no_proxy[0]) {
230
471
    const char *p = no_proxy;
231
471
    size_t namelen;
232
471
    char address[16];
233
471
    enum nametype type = TYPE_HOST;
234
471
    if(!strcmp("*", no_proxy))
235
6
      return TRUE;
236
237
    /* NO_PROXY was specified and it was not only an asterisk */
238
239
    /* Check if name is an IP address; if not, assume it being a hostname. */
240
465
    namelen = strlen(name);
241
465
    if(curlx_inet_pton(AF_INET, name, &address) == 1)
242
225
      type = TYPE_IPV4;
243
240
#ifdef USE_IPV6
244
240
    else if(curlx_inet_pton(AF_INET6, name, &address) == 1)
245
95
      type = TYPE_IPV6;
246
145
#endif
247
145
    else {
248
      /* ignore trailing dots in the hostname */
249
145
      if(name[namelen - 1] == '.')
250
7
        namelen--;
251
145
    }
252
253
8.81k
    while(*p) {
254
8.77k
      const char *token;
255
8.77k
      size_t tokenlen = 0;
256
257
      /* pass blanks */
258
8.77k
      curlx_str_passblanks(&p);
259
260
8.77k
      token = p;
261
      /* pass over the pattern */
262
146k
      while(*p && !ISBLANK(*p) && (*p != ',')) {
263
137k
        p++;
264
137k
        tokenlen++;
265
137k
      }
266
267
8.77k
      if(tokenlen) {
268
8.62k
        bool match = FALSE;
269
8.62k
        if(type == TYPE_HOST)
270
2.52k
          match = match_host(token, tokenlen, name, namelen);
271
6.10k
        else
272
6.10k
          match = match_ip(type, token, tokenlen, name);
273
274
8.62k
        if(match)
275
30
          return TRUE;
276
8.62k
      }
277
278
      /* pass blanks after pattern */
279
8.74k
      curlx_str_passblanks(&p);
280
      /* if not a comma, this ends the loop */
281
8.74k
      if(*p != ',')
282
390
        break;
283
      /* pass any number of commas */
284
18.0k
      while(*p == ',')
285
9.66k
        p++;
286
8.35k
    } /* while(*p) */
287
465
  } /* NO_PROXY was specified and it was not only an asterisk */
288
289
23.0k
  return FALSE;
290
23.0k
}
291
292
#ifndef CURL_DISABLE_HTTP
293
294
/****************************************************************
295
 * Detect what (if any) proxy to use. Remember that this selects a host
296
 * name and is not limited to HTTP proxies only.
297
 * The returned pointer must be freed by the caller.
298
 ****************************************************************/
299
static char *proxy_detect_proxy(struct Curl_easy *data,
300
                                const struct Curl_scheme *scheme)
301
21.2k
{
302
  /* If proxy was not specified, we check for default proxy environment
303
   * variables, to enable i.e Lynx compliance:
304
   *
305
   * http_proxy=http://some.server.dom:port/
306
   * https_proxy=http://some.server.dom:port/
307
   * ftp_proxy=http://some.server.dom:port/
308
   * no_proxy=domain1.dom,host.domain2.dom
309
   *   (a comma-separated list of hosts which should
310
   *   not be proxied, or an asterisk to override
311
   *   all proxy variables)
312
   * all_proxy=http://some.server.dom:port/
313
   *   (seems to exist for the CERN www lib. Probably
314
   *   the first to check for.)
315
   *
316
   * For compatibility, the all-uppercase versions of these variables are
317
   * checked if the lowercase versions do not exist.
318
   */
319
21.2k
  const char *env_name = NULL;
320
21.2k
  char *proxy = NULL;
321
21.2k
  char name_buf[20];
322
323
  /* Try scheme specific env var first, unless http(s).
324
   * lowercase first, then uppercase. */
325
21.2k
  if((scheme != &Curl_scheme_https) && (scheme != &Curl_scheme_http)) {
326
3.31k
    curl_msnprintf(name_buf, sizeof(name_buf), "%s_proxy", scheme->name);
327
3.31k
    env_name = name_buf;
328
3.31k
    proxy = curl_getenv(env_name);
329
3.31k
    if(!proxy) {
330
3.31k
      Curl_strntoupper(name_buf, name_buf, sizeof(name_buf));
331
3.31k
      proxy = curl_getenv(env_name);
332
3.31k
    }
333
3.31k
  }
334
335
21.2k
  if(!proxy &&
336
21.2k
     ((scheme == &Curl_scheme_https) || (scheme == &Curl_scheme_wss))) {
337
    /* Not found, check 'https' env vars, also for 'wss'.
338
     * Again, first lowercase then uppercase. */
339
3.20k
    env_name = "https_proxy";
340
3.20k
    proxy = curl_getenv(env_name);
341
3.20k
    if(!proxy) {
342
3.20k
      env_name = "HTTPS_PROXY";
343
3.20k
      proxy = curl_getenv(env_name);
344
3.20k
    }
345
3.20k
  }
346
18.0k
  else if(!proxy &&
347
18.0k
          ((scheme == &Curl_scheme_http) || (scheme == &Curl_scheme_ws))) {
348
    /* Not found, check 'http' env vars, also for 'ws'.
349
     * We do NOT try the uppercase version 'HTTP_PROXY' because of
350
     * security reasons:
351
     *
352
     * When curl is used in a webserver application
353
     * environment (cgi or php), this environment variable can
354
     * be controlled by the web server user by setting the
355
     * http header 'Proxy:' to some value.
356
     *
357
     * This can cause 'internal' http/ftp requests to be
358
     * arbitrarily redirected by any external attacker.
359
     */
360
18.0k
    env_name = "http_proxy";
361
18.0k
    proxy = curl_getenv(env_name);
362
18.0k
  }
363
364
21.2k
  if(!proxy) {
365
    /* still not found, last resort checks. */
366
21.2k
    env_name = "all_proxy";
367
21.2k
    proxy = curl_getenv(env_name);
368
21.2k
    if(!proxy) {
369
21.2k
      env_name = "ALL_PROXY";
370
21.2k
      proxy = curl_getenv(env_name);
371
21.2k
    }
372
21.2k
  }
373
374
21.2k
  if(proxy)
375
0
    infof(data, "Uses proxy env variable %s == '%s'", env_name, proxy);
376
377
21.2k
  return proxy;
378
21.2k
}
379
#endif /* CURL_DISABLE_HTTP */
380
381
/*
382
 * If this is supposed to use a proxy, we need to figure out the proxy
383
 * hostname, so that we can reuse an existing connection
384
 * that may exist registered to the same proxy host.
385
 */
386
static CURLcode parse_proxy(struct Curl_easy *data,
387
                            const char *proxy,
388
                            bool for_pre_proxy,
389
                            struct proxy_info *proxyinfo)
390
1.81k
{
391
1.81k
  char *proxyuser = NULL;
392
1.81k
  char *proxypasswd = NULL;
393
1.81k
  char *scheme = NULL;
394
1.81k
  CURLcode result = CURLE_OK;
395
  /* Set the start proxy type for URL scheme guessing */
396
1.81k
  uint8_t proxytype = for_pre_proxy ? CURLPROXY_SOCKS4 : data->set.proxytype;
397
1.81k
  CURLU *uhp = curl_url();
398
1.81k
  CURLUcode uc;
399
400
1.81k
  if(!uhp) {
401
0
    result = CURLE_OUT_OF_MEMORY;
402
0
    goto error;
403
0
  }
404
  /* When parsing the proxy, allowing non-supported schemes since we have
405
     these made up ones for proxies. Guess scheme for URLs without it. */
406
1.81k
  uc = curl_url_set(uhp, CURLUPART_URL, proxy,
407
1.81k
                    CURLU_NON_SUPPORT_SCHEME | CURLU_GUESS_SCHEME);
408
1.81k
  if(!uc) {
409
    /* parsed okay as a URL - only update proxytype when scheme was explicit */
410
1.81k
    uc = curl_url_get(uhp, CURLUPART_SCHEME, &scheme, CURLU_NO_GUESS_SCHEME);
411
1.81k
    if(!uc) {
412
1.81k
      result = Curl_scheme_to_proxytype(data, scheme, &proxytype, proxy);
413
1.81k
      if(result)
414
0
        goto error;
415
1.81k
    }
416
0
    else if(uc != CURLUE_NO_SCHEME) {
417
0
      result = CURLE_OUT_OF_MEMORY;
418
0
      goto error;
419
0
    }
420
    /* else: no explicit scheme, keep the configured proxytype */
421
1.81k
  }
422
0
  else {
423
0
    failf(data, "Unsupported proxy syntax in \'%s\': %s", proxy,
424
0
          curl_url_strerror(uc));
425
0
    result = CURLE_COULDNT_RESOLVE_PROXY;
426
0
    goto error;
427
0
  }
428
429
1.81k
  result = Curl_peer_from_proxy_url(uhp, data, proxy, proxytype,
430
1.81k
                                    &proxyinfo->peer, &proxytype);
431
1.81k
  if(result)
432
0
    goto error;
433
434
1.81k
  switch(proxytype) {
435
1.66k
    case CURLPROXY_HTTP:
436
1.67k
    case CURLPROXY_HTTP_1_0:
437
1.67k
    case CURLPROXY_HTTPS:
438
1.67k
    case CURLPROXY_HTTPS2:
439
1.67k
    case CURLPROXY_HTTPS3:
440
1.67k
      if(for_pre_proxy) {
441
0
        failf(data, "Unsupported pre-proxy type for \'%s\'", proxy);
442
0
        result = CURLE_COULDNT_RESOLVE_PROXY;
443
0
        goto error;
444
0
      }
445
1.67k
      break;
446
1.67k
    case CURLPROXY_SOCKS4:
447
0
    case CURLPROXY_SOCKS4A:
448
135
    case CURLPROXY_SOCKS5:
449
135
    case CURLPROXY_SOCKS5_HOSTNAME:
450
135
      break;
451
0
    default:
452
0
      failf(data, "Unsupported proxy type %u for \'%s\'", proxytype, proxy);
453
0
      result = CURLE_COULDNT_RESOLVE_PROXY;
454
0
      goto error;
455
1.81k
  }
456
457
  /* Is there a username and password given in this proxy URL? */
458
1.81k
  uc = curl_url_get(uhp, CURLUPART_USER, &proxyuser, CURLU_URLDECODE);
459
1.81k
  if(uc && (uc != CURLUE_NO_USER)) {
460
0
    result = Curl_uc_to_curlcode(uc);
461
0
    goto error;
462
0
  }
463
1.81k
  uc = curl_url_get(uhp, CURLUPART_PASSWORD, &proxypasswd, CURLU_URLDECODE);
464
1.81k
  if(uc && (uc != CURLUE_NO_PASSWORD)) {
465
0
    result = Curl_uc_to_curlcode(uc);
466
0
    goto error;
467
0
  }
468
469
1.81k
  if(proxyuser || proxypasswd) {
470
0
    result = Curl_creds_create(proxyuser, proxypasswd, NULL, NULL,
471
0
                               CURL_EASY_STR(data, STRING_PROXY_SERVICE_NAME),
472
0
                               CREDS_URL, &proxyinfo->creds);
473
0
    if(result)
474
0
      goto error;
475
0
  }
476
1.81k
  else if(!for_pre_proxy &&
477
1.67k
          (CURL_EASY_STR(data, STRING_PROXYUSERNAME) ||
478
1.39k
           CURL_EASY_STR(data, STRING_PROXYPASSWORD) ||
479
1.30k
           CURL_EASY_STR(data, STRING_PROXY_SERVICE_NAME))) {
480
    /* No user/passwd in URL, if this is not a pre-proxy, the
481
     * CURLOPT_PROXY* settings apply. */
482
398
    result = Curl_creds_create(CURL_EASY_STR(data, STRING_PROXYUSERNAME),
483
398
                               CURL_EASY_STR(data, STRING_PROXYPASSWORD),
484
398
                               NULL, NULL,
485
398
                               CURL_EASY_STR(data, STRING_PROXY_SERVICE_NAME),
486
398
                               CREDS_OPTION, &proxyinfo->creds);
487
398
  }
488
1.41k
  else
489
1.41k
    Curl_creds_unlink(&proxyinfo->creds);
490
491
1.81k
  proxyinfo->proxytype = proxytype;
492
493
1.81k
error:
494
1.81k
  curlx_free(scheme);
495
1.81k
  curlx_free(proxyuser);
496
1.81k
  curlx_free(proxypasswd);
497
1.81k
  curl_url_cleanup(uhp);
498
1.81k
#ifdef DEBUGBUILD
499
1.81k
  if(!result) {
500
1.81k
    DEBUGASSERT(proxyinfo);
501
1.81k
    DEBUGASSERT(proxyinfo->peer);
502
1.81k
  }
503
1.81k
#endif
504
1.81k
  return result;
505
1.81k
}
506
507
/* Is transfer's origin exempted from proxy use? */
508
static bool proxy_do_not_proxy(struct Curl_easy *data)
509
23.0k
{
510
23.0k
  const char *no_proxy;
511
23.0k
  char *env_no_proxy = NULL;
512
23.0k
  bool do_not_proxy;
513
514
  /* no proxying if the transfer does not use the network */
515
23.0k
  if(data->state.origin->scheme->flags & PROTOPT_NONETWORK)
516
0
    return TRUE;
517
518
23.0k
  no_proxy = CURL_EASY_STR(data, STRING_NOPROXY);
519
23.0k
  if(!no_proxy) {
520
22.5k
    const char *p = "no_proxy";
521
22.5k
    env_no_proxy = curl_getenv(p);
522
22.5k
    if(!env_no_proxy) {
523
22.5k
      p = "NO_PROXY";
524
22.5k
      env_no_proxy = curl_getenv(p);
525
22.5k
    }
526
22.5k
    if(env_no_proxy)
527
0
      infof(data, "Uses proxy env variable %s == '%s'", p, env_no_proxy);
528
22.5k
    no_proxy = env_no_proxy;
529
22.5k
  }
530
531
23.0k
  do_not_proxy = proxy_check_noproxy(data->state.origin->hostname, no_proxy);
532
23.0k
  curlx_safefree(env_no_proxy);
533
23.0k
  return do_not_proxy;
534
23.0k
}
535
536
CURLcode Curl_proxy_init_conn(struct Curl_easy *data,
537
                              struct connectdata *conn)
538
23.0k
{
539
23.0k
  char *proxy = NULL;
540
23.0k
  char *pre_proxy = NULL;
541
23.0k
  const char *str = NULL;
542
23.0k
  bool do_env_detect = TRUE;
543
23.0k
  CURLcode result = CURLE_OK;
544
545
  /* Enforce no proxy use unless we decide to use one */
546
23.0k
  conn->bits.origin_is_proxy = FALSE;
547
23.0k
  DEBUGASSERT(!conn->socks_proxy.peer);
548
23.0k
  DEBUGASSERT(!conn->http_proxy.peer);
549
550
23.0k
  if(proxy_do_not_proxy(data))
551
36
    goto out;
552
553
  /*************************************************************
554
   * Detect what (if any) proxy to use
555
   *************************************************************/
556
  /* the empty config strings disable proxy use and env detects */
557
23.0k
  str = CURL_EASY_STR(data, STRING_PROXY);
558
23.0k
  if(str) {
559
1.67k
    if(*str) {
560
1.67k
      proxy = curlx_strdup(str);
561
      /* if global proxy is set, this is it */
562
1.67k
      if(!proxy) {
563
0
        failf(data, "memory shortage");
564
0
        result = CURLE_OUT_OF_MEMORY;
565
0
        goto out;
566
0
      }
567
1.67k
    }
568
0
    else
569
0
      do_env_detect = FALSE;
570
1.67k
  }
571
572
23.0k
  str = CURL_EASY_STR(data, STRING_PRE_PROXY);
573
23.0k
  if(str) {
574
135
    if(*str) {
575
135
      pre_proxy = curlx_strdup(str);
576
      /* if global socks proxy is set, this is it */
577
135
      if(!pre_proxy) {
578
0
        failf(data, "memory shortage");
579
0
        result = CURLE_OUT_OF_MEMORY;
580
0
        goto out;
581
0
      }
582
135
    }
583
0
    else
584
0
      do_env_detect = FALSE;
585
135
  }
586
587
23.0k
#ifndef CURL_DISABLE_HTTP
588
  /* None configured, detect possible proxy from environment. */
589
23.0k
  if(!proxy && !pre_proxy && do_env_detect)
590
21.2k
    proxy = proxy_detect_proxy(data, conn->scheme);
591
#else
592
  (void)do_env_detect;
593
#endif /* CURL_DISABLE_HTTP */
594
595
23.0k
  if(!proxy && !pre_proxy)
596
21.2k
    goto out;
597
598
1.77k
  if(pre_proxy) {
599
135
    result = parse_proxy(data, pre_proxy, TRUE, &conn->socks_proxy);
600
135
    if(result)
601
0
      goto out;
602
135
  }
603
604
1.77k
  if(proxy) {
605
1.67k
    result = parse_proxy(data, proxy, FALSE, &conn->http_proxy);
606
1.67k
    if(result)
607
0
      goto out;
608
609
1.67k
    switch(conn->http_proxy.proxytype) {
610
0
    case CURLPROXY_SOCKS4:
611
0
    case CURLPROXY_SOCKS4A:
612
0
    case CURLPROXY_SOCKS5:
613
0
    case CURLPROXY_SOCKS5_HOSTNAME:
614
      /* Whoops, it is not an HTTP proxy */
615
0
      if(pre_proxy) {
616
        /* and we already have a SOCKS pre-proxy. Cannot have both */
617
0
        failf(data, "Having a SOCKS pre-proxy and proxy is not "
618
0
              "supported with \'%s\'", proxy);
619
0
        result = CURLE_COULDNT_RESOLVE_PROXY;
620
0
        goto out;
621
0
      }
622
      /* switch */
623
0
      conn->socks_proxy = conn->http_proxy;
624
0
      memset(&conn->http_proxy, 0, sizeof(conn->http_proxy));
625
0
      break;
626
1.67k
    default:
627
      /* all other types are HTTP */
628
1.67k
      break;
629
1.67k
    }
630
1.67k
  }
631
632
1.77k
  if(conn->socks_proxy.peer) {
633
135
    DEBUGASSERT(!CURL_PROXY_IS_ANY_HTTP(conn->socks_proxy.proxytype));
634
135
  }
635
636
#ifdef CURL_DISABLE_HTTP
637
  if(conn->http_proxy.peer) {
638
    /* asking for an HTTP proxy is a bit funny when HTTP is disabled... */
639
    result = CURLE_UNSUPPORTED_PROTOCOL;
640
    goto out;
641
  }
642
643
#else /* CURL_DISABLE_HTTP */
644
1.77k
  if(conn->http_proxy.peer) {
645
1.67k
    const struct Curl_scheme *scheme = data->state.origin->scheme;
646
1.67k
    bool tunnel_proxy = (bool)data->set.tunnel_thru_httpproxy;
647
1.67k
    DEBUGASSERT(CURL_PROXY_IS_ANY_HTTP(conn->http_proxy.proxytype));
648
649
1.67k
    if(!tunnel_proxy) {
650
      /* Decide if we tunnel through proxy automatically */
651
1.66k
      if(conn->via_peer) {
652
        /* With connect-to, we always tunnel */
653
1.44k
        tunnel_proxy = TRUE;
654
1.44k
      }
655
214
      else if(scheme->flags & PROTOPT_SSL) {
656
        /* If the transfer is supposed to be secure, we tunnel */
657
2
        tunnel_proxy = TRUE;
658
2
      }
659
212
      else if(scheme->flags & PROTOPT_HTTP_PROXY_TUNNEL) {
660
        /* transfer scheme required tunneling */
661
1
        tunnel_proxy = TRUE;
662
1
      }
663
211
      else if(!(scheme->protocol & PROTO_FAMILY_HTTP) &&
664
0
              !(scheme->flags & PROTOPT_PROXY_AS_HTTP)) {
665
        /* Cannot delegate transfer URL to HTTP proxy */
666
0
        tunnel_proxy = TRUE;
667
0
      }
668
1.66k
    }
669
670
1.67k
    if(!tunnel_proxy) {
671
      /* HTTP proxy used in forwarding mode. This means the connection
672
       * is really to the proxy and NOT the origin of the transfer. */
673
211
      DEBUGASSERT(!conn->via_peer);
674
211
      Curl_peer_link(&conn->origin, conn->http_proxy.peer);
675
211
      conn->scheme = conn->http_proxy.peer->scheme;
676
211
      conn->bits.origin_is_proxy = TRUE;
677
211
    }
678
679
1.67k
#ifndef CURL_DISABLE_DIGEST_AUTH
680
1.67k
    if(!Curl_safecmp(data->state.envproxy, proxy)) {
681
      /* proxy changed */
682
1.21k
      Curl_auth_digest_cleanup(&data->state.proxydigest);
683
1.21k
      curlx_free(data->state.envproxy);
684
1.21k
      data->state.envproxy = curlx_strdup(proxy);
685
1.21k
    }
686
1.67k
#endif
687
1.67k
  }
688
1.77k
#endif /* !CURL_DISABLE_HTTP */
689
690
23.0k
out:
691
23.0k
  curlx_free(pre_proxy);
692
23.0k
  curlx_free(proxy);
693
23.0k
  return result;
694
1.77k
}
695
696
#endif /* CURL_DISABLE_PROXY */