Coverage Report

Created: 2026-12-01 06:27

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