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
0
{
62
0
  unsigned int address = 0;
63
0
  unsigned int check = 0;
64
65
0
  if(bits > 32)
66
    /* strange input */
67
0
    return FALSE;
68
69
0
  if(curlx_inet_pton(AF_INET, ipv4, &address) != 1)
70
0
    return FALSE;
71
0
  if(curlx_inet_pton(AF_INET, network, &check) != 1)
72
0
    return FALSE;
73
74
0
  if((bits > 0) && (bits != 32)) {
75
0
    unsigned int mask = 0xffffffff << (32 - bits);
76
0
    unsigned int haddr = htonl(address);
77
0
    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
0
    if((haddr ^ hcheck) & mask)
85
0
      return FALSE;
86
0
    return TRUE;
87
0
  }
88
0
  else if(!bits && !check)
89
    /* 0.0.0.0/0 matches everything */
90
0
    return TRUE;
91
92
0
  return address == check;
93
0
}
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
0
{
108
0
#ifdef USE_IPV6
109
0
  static const char null6[16]= { 0 };
110
0
  unsigned int bytes;
111
0
  unsigned int rest;
112
0
  unsigned char address[16];
113
0
  unsigned char check[16];
114
115
0
  if(bits == -1)
116
0
    bits = 128;
117
118
0
  bytes = bits / 8;
119
0
  rest = bits & 0x07;
120
0
  if((bytes > 16) || ((bytes == 16) && rest))
121
0
    return FALSE;
122
0
  if(curlx_inet_pton(AF_INET6, ipv6, address) != 1)
123
0
    return FALSE;
124
0
  if(curlx_inet_pton(AF_INET6, network, check) != 1)
125
0
    return FALSE;
126
0
  if((bytes == 16) && !bits && !memcmp(null6, check, 16))
127
    /* ::/0 matches everything */
128
0
    return TRUE;
129
0
  if(bytes && memcmp(address, check, bytes))
130
0
    return FALSE;
131
0
  if(rest && ((address[bytes] ^ check[bytes]) & (0xff << (8 - rest))))
132
0
    return FALSE;
133
134
0
  return TRUE;
135
#else
136
  (void)ipv6;
137
  (void)network;
138
  (void)bits;
139
  return FALSE;
140
#endif
141
0
}
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
0
{
152
0
  bool match = FALSE;
153
154
  /* ignore trailing dots in the token to check */
155
0
  if(token[tokenlen - 1] == '.')
156
0
    tokenlen--;
157
158
0
  if(tokenlen && (*token == '.')) {
159
    /* ignore leading token dot as well */
160
0
    token++;
161
0
    tokenlen--;
162
0
  }
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
0
  if(tokenlen == namelen)
168
    /* case A, exact match */
169
0
    match = curl_strnequal(token, name, namelen);
170
0
  else if(tokenlen < namelen) {
171
    /* case B, tailmatch domain */
172
0
    match = (name[namelen - tokenlen - 1] == '.') &&
173
0
            curl_strnequal(token, name + (namelen - tokenlen), tokenlen);
174
0
  }
175
  /* case C passes through, not a match */
176
0
  return match;
177
0
}
178
179
static bool match_ip(int type, const char *token, size_t tokenlen,
180
                     const char *name)
181
0
{
182
0
  char *slash;
183
0
  int bits = -1; /* negative means not provided */
184
0
  char checkip[128];
185
0
  if(tokenlen >= sizeof(checkip))
186
    /* this cannot match */
187
0
    return FALSE;
188
  /* copy the check name to a temp buffer */
189
0
  memcpy(checkip, token, tokenlen);
190
0
  checkip[tokenlen] = 0;
191
192
0
  slash = strchr(checkip, '/');
193
  /* if the slash is part of this token, use it */
194
0
  if(slash) {
195
0
    curl_off_t value;
196
0
    const char *p = &slash[1];
197
0
    if(curlx_str_number(&p, &value, 128) || *p)
198
0
      return FALSE;
199
    /* a too large value is rejected in the cidr function below */
200
0
    bits = (int)value;
201
0
    *slash = 0; /* null-terminate there */
202
0
  }
203
0
  if(type == TYPE_IPV6)
204
0
    return cidr6_match(name, checkip, bits);
205
0
  else
206
0
    return cidr4_match(name, checkip, bits);
207
0
}
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
11.6k
{
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
11.6k
  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
11.6k
  if(no_proxy && no_proxy[0]) {
230
0
    const char *p = no_proxy;
231
0
    size_t namelen;
232
0
    char address[16];
233
0
    enum nametype type = TYPE_HOST;
234
0
    if(!strcmp("*", no_proxy))
235
0
      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
0
    namelen = strlen(name);
241
0
    if(curlx_inet_pton(AF_INET, name, &address) == 1)
242
0
      type = TYPE_IPV4;
243
0
#ifdef USE_IPV6
244
0
    else if(curlx_inet_pton(AF_INET6, name, &address) == 1)
245
0
      type = TYPE_IPV6;
246
0
#endif
247
0
    else {
248
      /* ignore trailing dots in the hostname */
249
0
      if(name[namelen - 1] == '.')
250
0
        namelen--;
251
0
    }
252
253
0
    while(*p) {
254
0
      const char *token;
255
0
      size_t tokenlen = 0;
256
257
      /* pass blanks */
258
0
      curlx_str_passblanks(&p);
259
260
0
      token = p;
261
      /* pass over the pattern */
262
0
      while(*p && !ISBLANK(*p) && (*p != ',')) {
263
0
        p++;
264
0
        tokenlen++;
265
0
      }
266
267
0
      if(tokenlen) {
268
0
        bool match = FALSE;
269
0
        if(type == TYPE_HOST)
270
0
          match = match_host(token, tokenlen, name, namelen);
271
0
        else
272
0
          match = match_ip(type, token, tokenlen, name);
273
274
0
        if(match)
275
0
          return TRUE;
276
0
      }
277
278
      /* pass blanks after pattern */
279
0
      curlx_str_passblanks(&p);
280
      /* if not a comma, this ends the loop */
281
0
      if(*p != ',')
282
0
        break;
283
      /* pass any number of commas */
284
0
      while(*p == ',')
285
0
        p++;
286
0
    } /* while(*p) */
287
0
  } /* NO_PROXY was specified and it was not only an asterisk */
288
289
11.6k
  return FALSE;
290
11.6k
}
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
19
{
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
19
  const char *env_name = NULL;
320
19
  char *proxy = NULL;
321
19
  char name_buf[20];
322
323
  /* Try scheme specific env var first, unless http(s).
324
   * lowercase first, then uppercase. */
325
19
  if((scheme != &Curl_scheme_https) && (scheme != &Curl_scheme_http)) {
326
19
    curl_msnprintf(name_buf, sizeof(name_buf), "%s_proxy", scheme->name);
327
19
    env_name = name_buf;
328
19
    proxy = curl_getenv(env_name);
329
19
    if(!proxy) {
330
19
      Curl_strntoupper(name_buf, name_buf, sizeof(name_buf));
331
19
      proxy = curl_getenv(env_name);
332
19
    }
333
19
  }
334
335
19
  if(!proxy &&
336
19
     ((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
19
    env_name = "https_proxy";
340
19
    proxy = curl_getenv(env_name);
341
19
    if(!proxy) {
342
19
      env_name = "HTTPS_PROXY";
343
19
      proxy = curl_getenv(env_name);
344
19
    }
345
19
  }
346
0
  else if(!proxy &&
347
0
          ((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
0
    env_name = "http_proxy";
361
0
    proxy = curl_getenv(env_name);
362
0
  }
363
364
19
  if(!proxy) {
365
    /* still not found, last resort checks. */
366
19
    env_name = "all_proxy";
367
19
    proxy = curl_getenv(env_name);
368
19
    if(!proxy) {
369
19
      env_name = "ALL_PROXY";
370
19
      proxy = curl_getenv(env_name);
371
19
    }
372
19
  }
373
374
19
  if(proxy)
375
0
    infof(data, "Uses proxy env variable %s == '%s'", env_name, proxy);
376
377
19
  return proxy;
378
19
}
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
0
{
391
0
  char *proxyuser = NULL;
392
0
  char *proxypasswd = NULL;
393
0
  char *scheme = NULL;
394
0
  CURLcode result = CURLE_OK;
395
  /* Set the start proxy type for URL scheme guessing */
396
0
  uint8_t proxytype = for_pre_proxy ? CURLPROXY_SOCKS4 : data->set.proxytype;
397
0
  CURLU *uhp = curl_url();
398
0
  CURLUcode uc;
399
400
0
  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
0
  uc = curl_url_set(uhp, CURLUPART_URL, proxy,
407
0
                    CURLU_NON_SUPPORT_SCHEME | CURLU_GUESS_SCHEME);
408
0
  if(!uc) {
409
    /* parsed okay as a URL - only update proxytype when scheme was explicit */
410
0
    uc = curl_url_get(uhp, CURLUPART_SCHEME, &scheme, CURLU_NO_GUESS_SCHEME);
411
0
    if(!uc) {
412
0
      result = Curl_scheme_to_proxytype(data, scheme, &proxytype, proxy);
413
0
      if(result)
414
0
        goto error;
415
0
    }
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
0
  }
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
0
  result = Curl_peer_from_proxy_url(uhp, data, proxy, proxytype,
430
0
                                    &proxyinfo->peer, &proxytype);
431
0
  if(result)
432
0
    goto error;
433
434
0
  switch(proxytype) {
435
0
    case CURLPROXY_HTTP:
436
0
    case CURLPROXY_HTTP_1_0:
437
0
    case CURLPROXY_HTTPS:
438
0
    case CURLPROXY_HTTPS2:
439
0
    case CURLPROXY_HTTPS3:
440
0
      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
0
      break;
446
0
    case CURLPROXY_SOCKS4:
447
0
    case CURLPROXY_SOCKS4A:
448
0
    case CURLPROXY_SOCKS5:
449
0
    case CURLPROXY_SOCKS5_HOSTNAME:
450
0
      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
0
  }
456
457
  /* Is there a username and password given in this proxy URL? */
458
0
  uc = curl_url_get(uhp, CURLUPART_USER, &proxyuser, CURLU_URLDECODE);
459
0
  if(uc && (uc != CURLUE_NO_USER)) {
460
0
    result = Curl_uc_to_curlcode(uc);
461
0
    goto error;
462
0
  }
463
0
  uc = curl_url_get(uhp, CURLUPART_PASSWORD, &proxypasswd, CURLU_URLDECODE);
464
0
  if(uc && (uc != CURLUE_NO_PASSWORD)) {
465
0
    result = Curl_uc_to_curlcode(uc);
466
0
    goto error;
467
0
  }
468
469
0
  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
0
  else if(!for_pre_proxy &&
477
0
          (CURL_EASY_STR(data, STRING_PROXYUSERNAME) ||
478
0
           CURL_EASY_STR(data, STRING_PROXYPASSWORD) ||
479
0
           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
0
    result = Curl_creds_create(CURL_EASY_STR(data, STRING_PROXYUSERNAME),
483
0
                               CURL_EASY_STR(data, STRING_PROXYPASSWORD),
484
0
                               NULL, NULL,
485
0
                               CURL_EASY_STR(data, STRING_PROXY_SERVICE_NAME),
486
0
                               CREDS_OPTION, &proxyinfo->creds);
487
0
  }
488
0
  else
489
0
    Curl_creds_unlink(&proxyinfo->creds);
490
491
0
  proxyinfo->proxytype = proxytype;
492
493
0
error:
494
0
  curlx_free(scheme);
495
0
  curlx_free(proxyuser);
496
0
  curlx_free(proxypasswd);
497
0
  curl_url_cleanup(uhp);
498
0
#ifdef DEBUGBUILD
499
0
  if(!result) {
500
0
    DEBUGASSERT(proxyinfo);
501
0
    DEBUGASSERT(proxyinfo->peer);
502
0
  }
503
0
#endif
504
0
  return result;
505
0
}
506
507
/* Is transfer's origin exempted from proxy use? */
508
static bool proxy_do_not_proxy(struct Curl_easy *data)
509
11.6k
{
510
11.6k
  const char *no_proxy;
511
11.6k
  char *env_no_proxy = NULL;
512
11.6k
  bool do_not_proxy;
513
514
  /* no proxying if the transfer does not use the network */
515
11.6k
  if(data->state.origin->scheme->flags & PROTOPT_NONETWORK)
516
0
    return TRUE;
517
518
11.6k
  no_proxy = CURL_EASY_STR(data, STRING_NOPROXY);
519
11.6k
  if(!no_proxy) {
520
11.6k
    const char *p = "no_proxy";
521
11.6k
    env_no_proxy = curl_getenv(p);
522
11.6k
    if(!env_no_proxy) {
523
11.6k
      p = "NO_PROXY";
524
11.6k
      env_no_proxy = curl_getenv(p);
525
11.6k
    }
526
11.6k
    if(env_no_proxy)
527
0
      infof(data, "Uses proxy env variable %s == '%s'", p, env_no_proxy);
528
11.6k
    no_proxy = env_no_proxy;
529
11.6k
  }
530
531
11.6k
  do_not_proxy = proxy_check_noproxy(data->state.origin->hostname, no_proxy);
532
11.6k
  curlx_safefree(env_no_proxy);
533
11.6k
  return do_not_proxy;
534
11.6k
}
535
536
CURLcode Curl_proxy_init_conn(struct Curl_easy *data,
537
                              struct connectdata *conn)
538
11.6k
{
539
11.6k
  char *proxy = NULL;
540
11.6k
  char *pre_proxy = NULL;
541
11.6k
  const char *str = NULL;
542
11.6k
  bool do_env_detect = TRUE;
543
11.6k
  CURLcode result = CURLE_OK;
544
545
  /* Enforce no proxy use unless we decide to use one */
546
11.6k
  conn->bits.origin_is_proxy = FALSE;
547
11.6k
  DEBUGASSERT(!conn->socks_proxy.peer);
548
11.6k
  DEBUGASSERT(!conn->http_proxy.peer);
549
550
11.6k
  if(proxy_do_not_proxy(data))
551
0
    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
11.6k
  str = CURL_EASY_STR(data, STRING_PROXY);
558
11.6k
  if(str) {
559
11.6k
    if(*str) {
560
0
      proxy = curlx_strdup(str);
561
      /* if global proxy is set, this is it */
562
0
      if(!proxy) {
563
0
        failf(data, "memory shortage");
564
0
        result = CURLE_OUT_OF_MEMORY;
565
0
        goto out;
566
0
      }
567
0
    }
568
11.6k
    else
569
11.6k
      do_env_detect = FALSE;
570
11.6k
  }
571
572
11.6k
  str = CURL_EASY_STR(data, STRING_PRE_PROXY);
573
11.6k
  if(str) {
574
0
    if(*str) {
575
0
      pre_proxy = curlx_strdup(str);
576
      /* if global socks proxy is set, this is it */
577
0
      if(!pre_proxy) {
578
0
        failf(data, "memory shortage");
579
0
        result = CURLE_OUT_OF_MEMORY;
580
0
        goto out;
581
0
      }
582
0
    }
583
0
    else
584
0
      do_env_detect = FALSE;
585
0
  }
586
587
11.6k
#ifndef CURL_DISABLE_HTTP
588
  /* None configured, detect possible proxy from environment. */
589
11.6k
  if(!proxy && !pre_proxy && do_env_detect)
590
19
    proxy = proxy_detect_proxy(data, conn->scheme);
591
#else
592
  (void)do_env_detect;
593
#endif /* CURL_DISABLE_HTTP */
594
595
11.6k
  if(!proxy && !pre_proxy)
596
11.6k
    goto out;
597
598
0
  if(pre_proxy) {
599
0
    result = parse_proxy(data, pre_proxy, TRUE, &conn->socks_proxy);
600
0
    if(result)
601
0
      goto out;
602
0
  }
603
604
0
  if(proxy) {
605
0
    result = parse_proxy(data, proxy, FALSE, &conn->http_proxy);
606
0
    if(result)
607
0
      goto out;
608
609
0
    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
0
    default:
627
      /* all other types are HTTP */
628
0
      break;
629
0
    }
630
0
  }
631
632
0
  if(conn->socks_proxy.peer) {
633
0
    DEBUGASSERT(!CURL_PROXY_IS_ANY_HTTP(conn->socks_proxy.proxytype));
634
0
  }
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
0
  if(conn->http_proxy.peer) {
645
0
    const struct Curl_scheme *scheme = data->state.origin->scheme;
646
0
    bool tunnel_proxy = (bool)data->set.tunnel_thru_httpproxy;
647
0
    DEBUGASSERT(CURL_PROXY_IS_ANY_HTTP(conn->http_proxy.proxytype));
648
649
0
    if(!tunnel_proxy) {
650
      /* Decide if we tunnel through proxy automatically */
651
0
      if(conn->via_peer) {
652
        /* With connect-to, we always tunnel */
653
0
        tunnel_proxy = TRUE;
654
0
      }
655
0
      else if(scheme->flags & PROTOPT_SSL) {
656
        /* If the transfer is supposed to be secure, we tunnel */
657
0
        tunnel_proxy = TRUE;
658
0
      }
659
0
      else if(scheme->flags & PROTOPT_HTTP_PROXY_TUNNEL) {
660
        /* transfer scheme required tunneling */
661
0
        tunnel_proxy = TRUE;
662
0
      }
663
0
      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
0
    }
669
670
0
    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
0
      DEBUGASSERT(!conn->via_peer);
674
0
      Curl_peer_link(&conn->origin, conn->http_proxy.peer);
675
0
      conn->scheme = conn->http_proxy.peer->scheme;
676
0
      conn->bits.origin_is_proxy = TRUE;
677
0
    }
678
679
0
#ifndef CURL_DISABLE_DIGEST_AUTH
680
0
    if(!Curl_safecmp(data->state.envproxy, proxy)) {
681
      /* proxy changed */
682
0
      Curl_auth_digest_cleanup(&data->state.proxydigest);
683
0
      curlx_free(data->state.envproxy);
684
0
      data->state.envproxy = curlx_strdup(proxy);
685
0
    }
686
0
#endif
687
0
  }
688
0
#endif /* !CURL_DISABLE_HTTP */
689
690
11.6k
out:
691
11.6k
  curlx_free(pre_proxy);
692
11.6k
  curlx_free(proxy);
693
11.6k
  return result;
694
0
}
695
696
#endif /* CURL_DISABLE_PROXY */