Coverage Report

Created: 2026-09-04 07:25

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