~ubuntu-branches/ubuntu/vivid/curl/vivid

« back to all changes in this revision

Viewing changes to lib/url.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Schuldei
  • Date: 2009-04-02 23:35:45 UTC
  • mto: (1.2.1 upstream) (3.2.3 sid)
  • mto: This revision was merged to the branch mainline in revision 38.
  • Revision ID: james.westby@ubuntu.com-20090402233545-geixkwhe3izccjt7
Tags: upstream-7.19.4
ImportĀ upstreamĀ versionĀ 7.19.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
 
8
 * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
9
9
 *
10
10
 * This software is licensed as described in the file COPYING, which
11
11
 * you should have received as part of this distribution. The terms
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: url.c,v 1.711 2008-04-30 21:20:09 bagder Exp $
 
21
 * $Id: url.c,v 1.790 2009-03-02 23:05:31 bagder Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
/* -- WIN32 approved -- */
60
60
#ifdef HAVE_SYS_IOCTL_H
61
61
#include <sys/ioctl.h>
62
62
#endif
63
 
#ifdef HAVE_SIGNAL_H
64
 
#include <signal.h>
65
 
#endif
66
63
 
67
64
#ifdef HAVE_SYS_PARAM_H
68
65
#include <sys/param.h>
73
70
#include <inet.h>
74
71
#endif
75
72
 
76
 
#ifdef HAVE_SETJMP_H
77
 
#include <setjmp.h>
78
 
#endif
79
 
 
80
73
#ifndef HAVE_SOCKET
81
74
#error "We can't compile without socket() support!"
82
75
#endif
122
115
#include "multiif.h"
123
116
#include "easyif.h"
124
117
#include "speedcheck.h"
 
118
#include "rawstr.h"
125
119
 
126
120
/* And now for the protocols */
127
121
#include "ftp.h"
138
132
#include "http_ntlm.h"
139
133
#include "socks.h"
140
134
 
141
 
#if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
142
 
#include "inet_ntoa_r.h"
143
 
#endif
144
 
 
145
135
#define _MPRINTF_REPLACE /* use our functions only */
146
136
#include <curl/mprintf.h>
147
137
 
148
138
#include "memory.h"
149
 
 
150
139
/* The last #include file should be: */
151
140
#include "memdebug.h"
152
141
 
153
 
#ifdef __SYMBIAN32__
154
 
#undef SIGALRM
155
 
#endif
156
 
 
157
142
/* Local static prototypes */
158
143
static long ConnectionKillOne(struct SessionHandle *data);
159
 
static bool ConnectionExists(struct SessionHandle *data,
160
 
                             struct connectdata *needle,
161
 
                             struct connectdata **usethis);
162
 
static long ConnectionStore(struct SessionHandle *data,
163
 
                            struct connectdata *conn);
164
 
static bool IsPipeliningPossible(const struct SessionHandle *handle);
165
144
static void conn_free(struct connectdata *conn);
166
 
 
167
145
static void signalPipeClose(struct curl_llist *pipeline);
168
146
 
169
 
static struct SessionHandle* gethandleathead(struct curl_llist *pipeline);
170
 
static CURLcode do_init(struct connectdata *conn);
171
 
static void do_complete(struct connectdata *conn);
172
 
 
173
 
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
174
 
static void flush_cookies(struct SessionHandle *data, int cleanup);
175
 
#endif
176
 
 
177
147
#ifdef CURL_DISABLE_VERBOSE_STRINGS
178
148
#define verboseconnect(x)  do { } while (0)
179
149
#endif
180
150
 
181
 
#ifndef USE_ARES
182
 
/* not for ares builds */
183
 
 
184
 
#ifndef WIN32
185
 
/* not for WIN32 builds */
186
 
 
187
 
#ifdef HAVE_SIGSETJMP
188
 
extern sigjmp_buf curl_jmpenv;
189
 
#endif
190
 
 
191
 
#ifdef SIGALRM
192
 
static
193
 
RETSIGTYPE alarmfunc(int sig)
194
 
{
195
 
  /* this is for "-ansi -Wall -pedantic" to stop complaining!   (rabe) */
196
 
  (void)sig;
197
 
#ifdef HAVE_SIGSETJMP
198
 
  siglongjmp(curl_jmpenv, 1);
199
 
#endif
200
 
  return;
201
 
}
202
 
#endif /* SIGALRM */
203
 
#endif /* WIN32 */
204
 
#endif /* USE_ARES */
205
151
 
206
152
/*
207
153
 * Protocol table.
272
218
  ZERO_NULL,                            /* doing */
273
219
  ZERO_NULL,                            /* proto_getsock */
274
220
  ZERO_NULL,                            /* doing_getsock */
 
221
  ZERO_NULL,                            /* perform_getsock */
275
222
  ZERO_NULL,                            /* disconnect */
276
223
  0,                                    /* defport */
277
224
  0                                     /* protocol */
286
233
static void close_connections(struct SessionHandle *data)
287
234
{
288
235
  /* Loop through all open connections and kill them one by one */
289
 
  while(-1 != ConnectionKillOne(data))
290
 
    ; /* empty loop */
 
236
  long i;
 
237
  do {
 
238
    i = ConnectionKillOne(data);
 
239
  } while(i != -1L);
291
240
}
292
241
 
293
242
void Curl_freeset(struct SessionHandle * data)
320
269
  return CURLE_OK;
321
270
}
322
271
 
 
272
static CURLcode setstropt_userpwd(char *option, char **user_storage,
 
273
                                  char **pwd_storage)
 
274
{
 
275
  char* separator;
 
276
  CURLcode result = CURLE_OK;
 
277
 
 
278
  if(!option)
 
279
    return result;
 
280
 
 
281
  separator = strchr(option, ':');
 
282
  if (separator != NULL) {
 
283
 
 
284
    /* store username part of option */
 
285
    char * p;
 
286
    size_t username_len = (size_t)(separator-option);
 
287
    p = malloc(username_len+1);
 
288
    if(!p)
 
289
      result = CURLE_OUT_OF_MEMORY;
 
290
    else {
 
291
      memcpy(p, option, username_len);
 
292
      p[username_len] = '\0';
 
293
      Curl_safefree(*user_storage);
 
294
      *user_storage = p;
 
295
    }
 
296
 
 
297
    /* store password part of option */
 
298
    if (result == CURLE_OK) {
 
299
      result = setstropt(pwd_storage, separator+1);
 
300
    }
 
301
  }
 
302
  else {
 
303
    result = setstropt(user_storage, option);
 
304
  }
 
305
  return result;
 
306
}
 
307
 
323
308
CURLcode Curl_dupset(struct SessionHandle * dst, struct SessionHandle * src)
324
309
{
325
310
  CURLcode r = CURLE_OK;
346
331
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
347
332
static void flush_cookies(struct SessionHandle *data, int cleanup)
348
333
{
349
 
  Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
350
334
  if(data->set.str[STRING_COOKIEJAR]) {
351
335
    if(data->change.cookielist) {
352
336
      /* If there is a list of cookie files to read, do it first so that
353
 
         we have all the told files read before we write the new jar */
 
337
         we have all the told files read before we write the new jar.
 
338
         Curl_cookie_loadfiles() LOCKS and UNLOCKS the share itself! */
354
339
      Curl_cookie_loadfiles(data);
355
340
    }
356
341
 
 
342
    Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
 
343
 
357
344
    /* if we have a destination file for all the cookies to get dumped to */
358
345
    if(Curl_cookie_output(data->cookies, data->set.str[STRING_COOKIEJAR]))
359
346
      infof(data, "WARNING: failed to save cookies in %s\n",
364
351
      /* since nothing is written, we can just free the list of cookie file
365
352
         names */
366
353
      curl_slist_free_all(data->change.cookielist); /* clean up list */
 
354
    Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
367
355
  }
368
356
 
369
357
  if(cleanup && (!data->share || (data->cookies != data->share->cookies))) {
408
396
          if(data == (struct SessionHandle *) curr->ptr) {
409
397
            fprintf(stderr,
410
398
                    "MAJOR problem we %p are still in send pipe for %p done %d\n",
411
 
                    data, connptr, connptr->bits.done);
 
399
                    data, connptr, (int)connptr->bits.done);
412
400
          }
413
401
        }
414
402
      }
418
406
          if(data == (struct SessionHandle *) curr->ptr) {
419
407
            fprintf(stderr,
420
408
                    "MAJOR problem we %p are still in recv pipe for %p done %d\n",
421
 
                    data, connptr, connptr->bits.done);
 
409
                    data, connptr, (int)connptr->bits.done);
422
410
          }
423
411
        }
424
412
      }
428
416
          if(data == (struct SessionHandle *) curr->ptr) {
429
417
            fprintf(stderr,
430
418
                    "MAJOR problem we %p are still in pend pipe for %p done %d\n",
431
 
                    data, connptr, connptr->bits.done);
 
419
                    data, connptr, (int)connptr->bits.done);
432
420
          }
433
421
        }
434
422
      }
464
452
    return CURLE_OK;
465
453
  }
466
454
 
467
 
  if(data->dns.hostcachetype == HCACHE_PRIVATE)
 
455
  if(data->dns.hostcachetype == HCACHE_PRIVATE) {
468
456
    Curl_hash_destroy(data->dns.hostcache);
 
457
    data->dns.hostcachetype = HCACHE_NONE;
 
458
    data->dns.hostcache = NULL;
 
459
  }
469
460
 
470
461
  if(data->state.rangestringalloc)
471
462
    free(data->state.range);
478
469
  Curl_ssl_close_all(data);
479
470
  Curl_safefree(data->state.first_host);
480
471
  Curl_safefree(data->state.scratch);
 
472
  Curl_ssl_free_certinfo(data);
481
473
 
482
474
  if(data->change.referer_alloc)
483
475
    free(data->change.referer);
598
590
      data->state.lastconnect = -1;
599
591
  }
600
592
  if(newamount > 0) {
601
 
    newptr= (struct connectdata **)
602
 
      realloc(c->connects, sizeof(struct connectdata *) * newamount);
 
593
    newptr = realloc(c->connects, sizeof(struct connectdata *) * newamount);
603
594
    if(!newptr)
604
595
      /* we closed a few connections in vain, but so what? */
605
596
      return CURLE_OUT_OF_MEMORY;
631
622
  free(c);
632
623
}
633
624
 
 
625
/*
 
626
 * Initialize the UserDefined fields within a SessionHandle.
 
627
 * This may be safely called on a new or existing SessionHandle.
 
628
 */
 
629
CURLcode Curl_init_userdefined(struct UserDefined *set)
 
630
{
 
631
  CURLcode res = CURLE_OK;
 
632
 
 
633
  set->out = stdout; /* default output to stdout */
 
634
  set->in  = stdin;  /* default input from stdin */
 
635
  set->err  = stderr;  /* default stderr to stderr */
 
636
 
 
637
  /* use fwrite as default function to store output */
 
638
  set->fwrite_func = (curl_write_callback)fwrite;
 
639
 
 
640
  /* use fread as default function to read input */
 
641
  set->fread_func = (curl_read_callback)fread;
 
642
 
 
643
  set->seek_func = ZERO_NULL;
 
644
  set->seek_client = ZERO_NULL;
 
645
 
 
646
  /* conversion callbacks for non-ASCII hosts */
 
647
  set->convfromnetwork = ZERO_NULL;
 
648
  set->convtonetwork   = ZERO_NULL;
 
649
  set->convfromutf8    = ZERO_NULL;
 
650
 
 
651
  set->infilesize = -1;      /* we don't know any size */
 
652
  set->postfieldsize = -1;   /* unknown size */
 
653
  set->maxredirs = -1;       /* allow any amount by default */
 
654
 
 
655
  set->httpreq = HTTPREQ_GET; /* Default HTTP request */
 
656
  set->ftp_use_epsv = TRUE;   /* FTP defaults to EPSV operations */
 
657
  set->ftp_use_eprt = TRUE;   /* FTP defaults to EPRT operations */
 
658
  set->ftp_filemethod = FTPFILE_MULTICWD;
 
659
 
 
660
  set->dns_cache_timeout = 60; /* Timeout every 60 seconds by default */
 
661
 
 
662
  /* Set the default size of the SSL session ID cache */
 
663
  set->ssl.numsessions = 5;
 
664
 
 
665
  set->proxyport = CURL_DEFAULT_PROXY_PORT; /* from url.h */
 
666
  set->proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
 
667
  set->httpauth = CURLAUTH_BASIC;  /* defaults to basic */
 
668
  set->proxyauth = CURLAUTH_BASIC; /* defaults to basic */
 
669
 
 
670
  /* make libcurl quiet by default: */
 
671
  set->hide_progress = TRUE;  /* CURLOPT_NOPROGRESS changes these */
 
672
 
 
673
  /*
 
674
   * libcurl 7.10 introduced SSL verification *by default*! This needs to be
 
675
   * switched off unless wanted.
 
676
   */
 
677
  set->ssl.verifypeer = TRUE;
 
678
  set->ssl.verifyhost = 2;
 
679
  set->ssh_auth_types = CURLSSH_AUTH_DEFAULT; /* defaults to any auth
 
680
                                                      type */
 
681
  set->ssl.sessionid = TRUE; /* session ID caching enabled by default */
 
682
 
 
683
  set->new_file_perms = 0644;    /* Default permissions */
 
684
  set->new_directory_perms = 0755; /* Default permissions */
 
685
 
 
686
  /* for the *protocols fields we don't use the CURLPROTO_ALL convenience
 
687
     define since we internally only use the lower 16 bits for the passed
 
688
     in bitmask to not conflict with the private bits */
 
689
  set->allowed_protocols = PROT_EXTMASK;
 
690
  set->redir_protocols =
 
691
    PROT_EXTMASK & ~(CURLPROTO_FILE|CURLPROTO_SCP); /* not FILE or SCP */
 
692
 
 
693
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
 
694
  /*
 
695
   * disallow unprotected protection negotiation NEC reference implementation
 
696
   * seem not to follow rfc1961 section 4.3/4.4
 
697
   */
 
698
  set->socks5_gssapi_nec = FALSE;
 
699
  /* set default gssapi service name */
 
700
  res = setstropt(&set->str[STRING_SOCKS5_GSSAPI_SERVICE],
 
701
                  (char *) CURL_DEFAULT_SOCKS5_GSSAPI_SERVICE);
 
702
  if (res != CURLE_OK)
 
703
    return res;
 
704
#endif
 
705
 
 
706
  /* This is our preferred CA cert bundle/path since install time */
 
707
#if defined(CURL_CA_BUNDLE)
 
708
  res = setstropt(&set->str[STRING_SSL_CAFILE], (char *) CURL_CA_BUNDLE);
 
709
#elif defined(CURL_CA_PATH)
 
710
  res = setstropt(&set->str[STRING_SSL_CAPATH], (char *) CURL_CA_PATH);
 
711
#endif
 
712
 
 
713
  return res;
 
714
}
 
715
 
634
716
/**
635
717
 * Curl_open()
636
718
 *
648
730
#endif
649
731
 
650
732
  /* Very simple start-up: alloc the struct, init it with zeroes and return */
651
 
  data = (struct SessionHandle *)calloc(1, sizeof(struct SessionHandle));
 
733
  data = calloc(1, sizeof(struct SessionHandle));
652
734
  if(!data) {
653
735
    /* this is a very serious error */
654
736
    DEBUGF(fprintf(stderr, "Error: calloc of SessionHandle failed\n"));
672
754
 
673
755
  /* We do some initial setup here, all those fields that can't be just 0 */
674
756
 
675
 
  data->state.headerbuff=(char*)malloc(HEADERSIZE);
 
757
  data->state.headerbuff = malloc(HEADERSIZE);
676
758
  if(!data->state.headerbuff) {
677
759
    DEBUGF(fprintf(stderr, "Error: malloc of headerbuff failed\n"));
678
760
    res = CURLE_OUT_OF_MEMORY;
679
761
  }
680
762
  else {
 
763
    Curl_easy_initHandleData(data);
 
764
    res = Curl_init_userdefined(&data->set);
 
765
 
681
766
    data->state.headersize=HEADERSIZE;
682
767
 
683
 
    data->set.out = stdout; /* default output to stdout */
684
 
    data->set.in  = stdin;  /* default input from stdin */
685
 
    data->set.err  = stderr;  /* default stderr to stderr */
686
 
 
687
 
    /* use fwrite as default function to store output */
688
 
    data->set.fwrite_func = (curl_write_callback)fwrite;
689
 
 
690
 
    /* use fread as default function to read input */
691
 
    data->set.fread_func = (curl_read_callback)fread;
692
 
 
693
 
    /* don't use a seek function by default */
694
 
    data->set.seek_func = ZERO_NULL;
695
 
    data->set.seek_client = ZERO_NULL;
696
 
 
697
 
    /* conversion callbacks for non-ASCII hosts */
698
 
    data->set.convfromnetwork = ZERO_NULL;
699
 
    data->set.convtonetwork   = ZERO_NULL;
700
 
    data->set.convfromutf8    = ZERO_NULL;
701
 
 
702
768
#if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV)
703
769
    /* conversion descriptors for iconv calls */
704
770
    data->outbound_cd = (iconv_t)-1;
706
772
    data->utf8_cd     = (iconv_t)-1;
707
773
#endif /* CURL_DOES_CONVERSIONS && HAVE_ICONV */
708
774
 
709
 
    data->set.infilesize = -1; /* we don't know any size */
710
 
    data->set.postfieldsize = -1;
711
 
    data->set.maxredirs = -1; /* allow any amount by default */
 
775
    /* most recent connection is not yet defined */
 
776
    data->state.lastconnect = -1;
 
777
 
 
778
    data->progress.flags |= PGRS_HIDE;
712
779
    data->state.current_speed = -1; /* init to negative == impossible */
713
780
 
714
 
    data->set.httpreq = HTTPREQ_GET; /* Default HTTP request */
715
 
    data->set.ftp_use_epsv = TRUE;   /* FTP defaults to EPSV operations */
716
 
    data->set.ftp_use_eprt = TRUE;   /* FTP defaults to EPRT operations */
717
 
    data->set.ftp_filemethod = FTPFILE_MULTICWD;
718
 
    data->set.dns_cache_timeout = 60; /* Timeout every 60 seconds by default */
719
 
 
720
 
    /* make libcurl quiet by default: */
721
 
    data->set.hide_progress = TRUE;  /* CURLOPT_NOPROGRESS changes these */
722
 
    data->progress.flags |= PGRS_HIDE;
723
 
 
724
 
    /* Set the default size of the SSL session ID cache */
725
 
    data->set.ssl.numsessions = 5;
726
 
 
727
 
    data->set.proxyport = CURL_DEFAULT_PROXY_PORT; /* from url.h */
728
 
    data->set.proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
729
 
    data->set.httpauth = CURLAUTH_BASIC;  /* defaults to basic */
730
 
    data->set.proxyauth = CURLAUTH_BASIC; /* defaults to basic */
731
 
 
732
781
    /* This no longer creates a connection cache here. It is instead made on
733
782
       the first call to curl_easy_perform() or when the handle is added to a
734
783
       multi stack. */
735
 
 
736
 
    data->set.ssh_auth_types = CURLSSH_AUTH_DEFAULT; /* defaults to any auth
737
 
                                                        type */
738
 
    data->set.new_file_perms = 0644;    /* Default permissions */
739
 
    data->set.new_directory_perms = 0755; /* Default permissions */
740
 
 
741
 
    /* most recent connection is not yet defined */
742
 
    data->state.lastconnect = -1;
743
 
 
744
 
    Curl_easy_initHandleData(data);
745
 
 
746
 
    /*
747
 
     * libcurl 7.10 introduced SSL verification *by default*! This needs to be
748
 
     * switched off unless wanted.
749
 
     */
750
 
    data->set.ssl.verifypeer = TRUE;
751
 
    data->set.ssl.verifyhost = 2;
752
 
    data->set.ssl.sessionid = TRUE; /* session ID caching enabled by default */
753
 
    /* This is our preferred CA cert bundle/path since install time */
754
 
#if defined(CURL_CA_BUNDLE)
755
 
    res = setstropt(&data->set.str[STRING_SSL_CAFILE],
756
 
                         (char *) CURL_CA_BUNDLE);
757
 
#elif defined(CURL_CA_PATH)
758
 
    res = setstropt(&data->set.str[STRING_SSL_CAPATH], (char *) CURL_CA_PATH);
759
 
#endif
760
784
  }
761
785
 
762
786
  if(res) {
787
811
    data->set.dns_cache_timeout = va_arg(param, long);
788
812
    break;
789
813
  case CURLOPT_DNS_USE_GLOBAL_CACHE:
790
 
    {
791
 
      /* remember we want this enabled */
792
 
      long use_cache = va_arg(param, long);
793
 
      data->set.global_dns_cache = (bool)(0 != use_cache);
794
 
    }
795
 
    break;
 
814
  {
 
815
    /* remember we want this enabled */
 
816
    long use_cache = va_arg(param, long);
 
817
    data->set.global_dns_cache = (bool)(0 != use_cache);
 
818
  }
 
819
  break;
796
820
  case CURLOPT_SSL_CIPHER_LIST:
797
821
    /* set a list of cipher we want to use in the SSL connection */
798
822
    result = setstropt(&data->set.str[STRING_SSL_CIPHER_LIST],
799
 
                            va_arg(param, char *));
 
823
                       va_arg(param, char *));
800
824
    break;
801
825
 
802
826
  case CURLOPT_RANDOM_FILE:
805
829
     * the random SSL stuff with. The file is only used for reading.
806
830
     */
807
831
    result = setstropt(&data->set.str[STRING_SSL_RANDOM_FILE],
808
 
                            va_arg(param, char *));
 
832
                       va_arg(param, char *));
809
833
    break;
810
834
  case CURLOPT_EGDSOCKET:
811
835
    /*
812
836
     * The Entropy Gathering Daemon socket pathname
813
837
     */
814
838
    result = setstropt(&data->set.str[STRING_SSL_EGDSOCKET],
815
 
                            va_arg(param, char *));
 
839
                       va_arg(param, char *));
816
840
    break;
817
841
  case CURLOPT_MAXCONNECTS:
818
842
    /*
888
912
    if(data->set.upload)
889
913
      /* If this is HTTP, PUT is what's needed to "upload" */
890
914
      data->set.httpreq = HTTPREQ_PUT;
 
915
    else
 
916
      /* In HTTP, the opposite of upload is either GET or a HEAD */
 
917
      data->set.httpreq = data->set.opt_no_body?HTTPREQ_HEAD:HTTPREQ_GET;
891
918
    break;
892
919
  case CURLOPT_FILETIME:
893
920
    /*
901
928
     * An FTP option that modifies an upload to create missing directories on
902
929
     * the server.
903
930
     */
904
 
    data->set.ftp_create_missing_dirs = (bool)(0 != va_arg(param, long));
 
931
    data->set.ftp_create_missing_dirs = (int)va_arg(param, long);
905
932
    break;
906
933
  case CURLOPT_FTP_RESPONSE_TIMEOUT:
907
934
    /*
910
937
     */
911
938
    data->set.ftp_response_timeout = va_arg( param , long ) * 1000;
912
939
    break;
 
940
  case CURLOPT_TFTP_BLKSIZE:
 
941
    /*
 
942
     * TFTP option that specifies the block size to use for data transmission
 
943
     */
 
944
    data->set.tftp_blksize = va_arg(param, long);
 
945
    break;
913
946
  case CURLOPT_DIRLISTONLY:
914
947
    /*
915
948
     * An option that changes the command to one that asks for a list
940
973
     * Use this file instead of the $HOME/.netrc file
941
974
     */
942
975
    result = setstropt(&data->set.str[STRING_NETRC_FILE],
943
 
                            va_arg(param, char *));
 
976
                       va_arg(param, char *));
944
977
    break;
945
978
  case CURLOPT_TRANSFERTEXT:
946
979
    /*
993
1026
     */
994
1027
    argptr = va_arg(param, char *);
995
1028
    result = setstropt(&data->set.str[STRING_ENCODING],
996
 
                            (argptr && !*argptr)?
997
 
                            (char *) ALL_CONTENT_ENCODINGS: argptr);
 
1029
                       (argptr && !*argptr)?
 
1030
                       (char *) ALL_CONTENT_ENCODINGS: argptr);
998
1031
    break;
999
1032
 
1000
1033
  case CURLOPT_FOLLOWLOCATION:
1021
1054
    data->set.maxredirs = va_arg(param, long);
1022
1055
    break;
1023
1056
 
1024
 
  case CURLOPT_POST301:
 
1057
  case CURLOPT_POSTREDIR:
 
1058
  {
1025
1059
    /*
1026
 
     * Obey RFC 2616/10.3.2 and resubmit a POST as a POST after a 301.
 
1060
     * Set the behaviour of POST when redirecting
 
1061
     * CURL_REDIR_GET_ALL - POST is changed to GET after 301 and 302
 
1062
     * CURL_REDIR_POST_301 - POST is kept as POST after 301
 
1063
     * CURL_REDIR_POST_302 - POST is kept as POST after 302
 
1064
     * CURL_REDIR_POST_ALL - POST is kept as POST after 301 and 302
 
1065
     * other - POST is kept as POST after 301 and 302
1027
1066
     */
1028
 
    data->set.post301 = (bool)(0 != va_arg(param, long));
1029
 
    break;
 
1067
    long postRedir = va_arg(param, long);
 
1068
    data->set.post301 = (bool)((postRedir & CURL_REDIR_POST_301)?TRUE:FALSE);
 
1069
    data->set.post302 = (bool)((postRedir & CURL_REDIR_POST_302)?TRUE:FALSE);
 
1070
  }
 
1071
  break;
1030
1072
 
1031
1073
  case CURLOPT_POST:
1032
1074
    /* Does this option serve a purpose anymore? Yes it does, when
1056
1098
       */
1057
1099
 
1058
1100
      if((data->set.postfieldsize < 0) ||
1059
 
          ((sizeof(curl_off_t) != sizeof(size_t)) &&
1060
 
           (data->set.postfieldsize > (curl_off_t)((size_t)-1))))
 
1101
         ((sizeof(curl_off_t) != sizeof(size_t)) &&
 
1102
          (data->set.postfieldsize > (curl_off_t)((size_t)-1))))
1061
1103
        result = CURLE_OUT_OF_MEMORY;
1062
1104
      else {
1063
1105
        char * p;
1068
1110
           later address compare to detect the COPYPOSTFIELDS mode, and
1069
1111
           to mark that postfields is used rather than read function or
1070
1112
           form data.
1071
 
         */
 
1113
        */
1072
1114
        p = malloc((size_t)(data->set.postfieldsize?data->set.postfieldsize:1));
1073
1115
 
1074
1116
        if(!p)
1104
1146
    bigsize = va_arg(param, long);
1105
1147
 
1106
1148
    if(data->set.postfieldsize < bigsize &&
1107
 
        data->set.postfields == data->set.str[STRING_COPYPOSTFIELDS]) {
 
1149
       data->set.postfields == data->set.str[STRING_COPYPOSTFIELDS]) {
1108
1150
      /* Previous CURLOPT_COPYPOSTFIELDS is no longer valid. */
1109
1151
      (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
1110
1152
      data->set.postfields = NULL;
1111
 
      }
 
1153
    }
1112
1154
 
1113
1155
    data->set.postfieldsize = bigsize;
1114
1156
    break;
1121
1163
    bigsize = va_arg(param, curl_off_t);
1122
1164
 
1123
1165
    if(data->set.postfieldsize < bigsize &&
1124
 
        data->set.postfields == data->set.str[STRING_COPYPOSTFIELDS]) {
 
1166
       data->set.postfields == data->set.str[STRING_COPYPOSTFIELDS]) {
1125
1167
      /* Previous CURLOPT_COPYPOSTFIELDS is no longer valid. */
1126
1168
      (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
1127
1169
      data->set.postfields = NULL;
1128
 
      }
 
1170
    }
1129
1171
 
1130
1172
    data->set.postfieldsize = bigsize;
1131
1173
    break;
1148
1190
      data->change.referer_alloc = FALSE;
1149
1191
    }
1150
1192
    result = setstropt(&data->set.str[STRING_SET_REFERER],
1151
 
                            va_arg(param, char *));
 
1193
                       va_arg(param, char *));
1152
1194
    data->change.referer = data->set.str[STRING_SET_REFERER];
1153
1195
    break;
1154
1196
 
1157
1199
     * String to use in the HTTP User-Agent field
1158
1200
     */
1159
1201
    result = setstropt(&data->set.str[STRING_USERAGENT],
1160
 
                            va_arg(param, char *));
 
1202
                       va_arg(param, char *));
1161
1203
    break;
1162
1204
 
1163
1205
  case CURLOPT_HTTPHEADER:
1180
1222
     * Cookie string to send to the remote server in the request.
1181
1223
     */
1182
1224
    result = setstropt(&data->set.str[STRING_COOKIE],
1183
 
                            va_arg(param, char *));
 
1225
                       va_arg(param, char *));
1184
1226
    break;
1185
1227
 
1186
1228
  case CURLOPT_COOKIEFILE:
1206
1248
     * Set cookie file name to dump all cookies to when we're done.
1207
1249
     */
1208
1250
    result = setstropt(&data->set.str[STRING_COOKIEJAR],
1209
 
                            va_arg(param, char *));
 
1251
                       va_arg(param, char *));
1210
1252
 
1211
1253
    /*
1212
1254
     * Activate the cookie parser. This may or may not already
1241
1283
    if(argptr == NULL)
1242
1284
      break;
1243
1285
 
1244
 
    if(strequal(argptr, "ALL")) {
 
1286
    if(Curl_raw_equal(argptr, "ALL")) {
1245
1287
      /* clear all cookies */
1246
1288
      Curl_cookie_clearall(data->cookies);
1247
1289
      break;
1248
1290
    }
1249
 
    else if(strequal(argptr, "SESS")) {
 
1291
    else if(Curl_raw_equal(argptr, "SESS")) {
1250
1292
      /* clear session cookies */
1251
1293
      Curl_cookie_clearsess(data->cookies);
1252
1294
      break;
1253
1295
    }
1254
 
    else if(strequal(argptr, "FLUSH")) {
 
1296
    else if(Curl_raw_equal(argptr, "FLUSH")) {
1255
1297
      /* flush cookies to file */
1256
1298
      flush_cookies(data, 0);
1257
1299
      break;
1298
1340
    data->set.httpversion = va_arg(param, long);
1299
1341
    break;
1300
1342
 
1301
 
  case CURLOPT_HTTPPROXYTUNNEL:
1302
 
    /*
1303
 
     * Tunnel operations through the proxy instead of normal proxy use
1304
 
     */
1305
 
    data->set.tunnel_thru_httpproxy = (bool)(0 != va_arg(param, long));
1306
 
    break;
1307
 
 
1308
1343
  case CURLOPT_CUSTOMREQUEST:
1309
1344
    /*
1310
1345
     * Set a custom string to use as request
1311
1346
     */
1312
1347
    result = setstropt(&data->set.str[STRING_CUSTOMREQUEST],
1313
 
                            va_arg(param, char *));
 
1348
                       va_arg(param, char *));
1314
1349
 
1315
1350
    /* we don't set
1316
1351
       data->set.httpreq = HTTPREQ_CUSTOM;
1318
1353
       and this just changes the actual request keyword */
1319
1354
    break;
1320
1355
 
 
1356
  case CURLOPT_HTTPAUTH:
 
1357
    /*
 
1358
     * Set HTTP Authentication type BITMASK.
 
1359
     */
 
1360
  {
 
1361
    long auth = va_arg(param, long);
 
1362
 
 
1363
    /* the DIGEST_IE bit is only used to set a special marker, for all the
 
1364
       rest we need to handle it as normal DIGEST */
 
1365
    data->state.authhost.iestyle = (bool)((auth & CURLAUTH_DIGEST_IE)?TRUE:FALSE);
 
1366
 
 
1367
    if(auth & CURLAUTH_DIGEST_IE) {
 
1368
      auth |= CURLAUTH_DIGEST; /* set standard digest bit */
 
1369
      auth &= ~CURLAUTH_DIGEST_IE; /* unset ie digest bit */
 
1370
    }
 
1371
 
 
1372
    /* switch off bits we can't support */
 
1373
#ifndef USE_NTLM
 
1374
    auth &= ~CURLAUTH_NTLM; /* no NTLM without SSL */
 
1375
#endif
 
1376
#ifndef HAVE_GSSAPI
 
1377
    auth &= ~CURLAUTH_GSSNEGOTIATE; /* no GSS-Negotiate without GSSAPI */
 
1378
#endif
 
1379
    if(!auth)
 
1380
      return CURLE_FAILED_INIT; /* no supported types left! */
 
1381
 
 
1382
    data->set.httpauth = auth;
 
1383
  }
 
1384
  break;
 
1385
 
 
1386
#ifndef CURL_DISABLE_PROXY
 
1387
  case CURLOPT_HTTPPROXYTUNNEL:
 
1388
    /*
 
1389
     * Tunnel operations through the proxy instead of normal proxy use
 
1390
     */
 
1391
    data->set.tunnel_thru_httpproxy = (bool)(0 != va_arg(param, long));
 
1392
    break;
 
1393
 
1321
1394
  case CURLOPT_PROXYPORT:
1322
1395
    /*
1323
1396
     * Explicitly set HTTP proxy port number.
1325
1398
    data->set.proxyport = va_arg(param, long);
1326
1399
    break;
1327
1400
 
1328
 
  case CURLOPT_HTTPAUTH:
1329
 
    /*
1330
 
     * Set HTTP Authentication type BITMASK.
1331
 
     */
1332
 
  {
1333
 
    long auth = va_arg(param, long);
1334
 
    /* switch off bits we can't support */
1335
 
#ifndef USE_NTLM
1336
 
    auth &= ~CURLAUTH_NTLM; /* no NTLM without SSL */
1337
 
#endif
1338
 
#ifndef HAVE_GSSAPI
1339
 
    auth &= ~CURLAUTH_GSSNEGOTIATE; /* no GSS-Negotiate without GSSAPI */
1340
 
#endif
1341
 
    if(!auth)
1342
 
      return CURLE_FAILED_INIT; /* no supported types left! */
1343
 
 
1344
 
    data->set.httpauth = auth;
1345
 
  }
1346
 
  break;
1347
 
 
1348
1401
  case CURLOPT_PROXYAUTH:
1349
1402
    /*
1350
1403
     * Set HTTP Authentication type BITMASK.
1351
1404
     */
1352
1405
  {
1353
1406
    long auth = va_arg(param, long);
 
1407
 
 
1408
    /* the DIGEST_IE bit is only used to set a special marker, for all the
 
1409
       rest we need to handle it as normal DIGEST */
 
1410
    data->state.authproxy.iestyle = (bool)((auth & CURLAUTH_DIGEST_IE)?TRUE:FALSE);
 
1411
 
 
1412
    if(auth & CURLAUTH_DIGEST_IE) {
 
1413
      auth |= CURLAUTH_DIGEST; /* set standard digest bit */
 
1414
      auth &= ~CURLAUTH_DIGEST_IE; /* unset ie digest bit */
 
1415
    }
1354
1416
    /* switch off bits we can't support */
1355
1417
#ifndef USE_NTLM
1356
1418
    auth &= ~CURLAUTH_NTLM; /* no NTLM without SSL */
1377
1439
     * to decide for us.
1378
1440
     */
1379
1441
    result = setstropt(&data->set.str[STRING_PROXY],
1380
 
                            va_arg(param, char *));
1381
 
    break;
 
1442
                       va_arg(param, char *));
 
1443
    break;
 
1444
 
 
1445
  case CURLOPT_PROXYTYPE:
 
1446
    /*
 
1447
     * Set proxy type. HTTP/HTTP_1_0/SOCKS4/SOCKS4a/SOCKS5/SOCKS5_HOSTNAME
 
1448
     */
 
1449
    data->set.proxytype = (curl_proxytype)va_arg(param, long);
 
1450
    break;
 
1451
 
 
1452
  case CURLOPT_PROXY_TRANSFER_MODE:
 
1453
    /*
 
1454
     * set transfer mode (;type=<a|i>) when doing FTP via an HTTP proxy
 
1455
     */
 
1456
    switch (va_arg(param, long)) {
 
1457
    case 0:
 
1458
      data->set.proxy_transfer_mode = FALSE;
 
1459
      break;
 
1460
    case 1:
 
1461
      data->set.proxy_transfer_mode = TRUE;
 
1462
      break;
 
1463
    default:
 
1464
      /* reserve other values for future use */
 
1465
      result = CURLE_FAILED_INIT;
 
1466
      break;
 
1467
    }
 
1468
    break;
 
1469
#endif
 
1470
 
 
1471
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
 
1472
  case CURLOPT_SOCKS5_GSSAPI_SERVICE:
 
1473
    /*
 
1474
     * Set gssapi service name
 
1475
     */
 
1476
    result = setstropt(&data->set.str[STRING_SOCKS5_GSSAPI_SERVICE],
 
1477
                       va_arg(param, char *));
 
1478
    break;
 
1479
 
 
1480
  case CURLOPT_SOCKS5_GSSAPI_NEC:
 
1481
    /*
 
1482
     * set flag for nec socks5 support
 
1483
     */
 
1484
    data->set.socks5_gssapi_nec = (bool)(0 != va_arg(param, long));
 
1485
    break;
 
1486
#endif
1382
1487
 
1383
1488
  case CURLOPT_WRITEHEADER:
1384
1489
    /*
1404
1509
     * Use FTP PORT, this also specifies which IP address to use
1405
1510
     */
1406
1511
    result = setstropt(&data->set.str[STRING_FTPPORT],
1407
 
                            va_arg(param, char *));
 
1512
                       va_arg(param, char *));
1408
1513
    data->set.ftp_use_port = (bool)(NULL != data->set.str[STRING_FTPPORT]);
1409
1514
    break;
1410
1515
 
1458
1563
    break;
1459
1564
  case CURLOPT_MAX_SEND_SPEED_LARGE:
1460
1565
    /*
1461
 
     * The max speed limit that sends transfer more than
1462
 
     * CURLOPT_MAX_SEND_PER_SECOND bytes per second the transfer is
1463
 
     * throttled..
 
1566
     * When transfer uploads are faster then CURLOPT_MAX_SEND_SPEED_LARGE
 
1567
     * bytes per second the transfer is throttled..
1464
1568
     */
1465
1569
    data->set.max_send_speed=va_arg(param, curl_off_t);
1466
1570
    break;
1467
1571
  case CURLOPT_MAX_RECV_SPEED_LARGE:
1468
1572
    /*
1469
 
     * The max speed limit that sends transfer more than
1470
 
     * CURLOPT_MAX_RECV_PER_SECOND bytes per second the transfer is
1471
 
     * throttled..
 
1573
     * When receiving data faster than CURLOPT_MAX_RECV_SPEED_LARGE bytes per
 
1574
     * second the transfer is throttled..
1472
1575
     */
1473
1576
    data->set.max_recv_speed=va_arg(param, curl_off_t);
1474
1577
    break;
1489
1592
      data->change.url_alloc=FALSE;
1490
1593
    }
1491
1594
    result = setstropt(&data->set.str[STRING_SET_URL],
1492
 
                            va_arg(param, char *));
 
1595
                       va_arg(param, char *));
1493
1596
    data->change.url = data->set.str[STRING_SET_URL];
1494
1597
    break;
1495
1598
  case CURLOPT_PORT:
1525
1628
    /*
1526
1629
     * user:password to use in the operation
1527
1630
     */
1528
 
    result = setstropt(&data->set.str[STRING_USERPWD],
1529
 
                            va_arg(param, char *));
 
1631
    result = setstropt_userpwd(va_arg(param, char *),
 
1632
                               &data->set.str[STRING_USERNAME],
 
1633
                               &data->set.str[STRING_PASSWORD]);
 
1634
    break;
 
1635
  case CURLOPT_USERNAME:
 
1636
    /*
 
1637
     * authentication user name to use in the operation
 
1638
     */
 
1639
    result = setstropt(&data->set.str[STRING_USERNAME],
 
1640
                       va_arg(param, char *));
 
1641
    break;
 
1642
  case CURLOPT_PASSWORD:
 
1643
    /*
 
1644
     * authentication password to use in the operation
 
1645
     */
 
1646
    result = setstropt(&data->set.str[STRING_PASSWORD],
 
1647
                       va_arg(param, char *));
1530
1648
    break;
1531
1649
  case CURLOPT_POSTQUOTE:
1532
1650
    /*
1563
1681
     */
1564
1682
    data->set.progress_client = va_arg(param, void *);
1565
1683
    break;
 
1684
 
 
1685
#ifndef CURL_DISABLE_PROXY
1566
1686
  case CURLOPT_PROXYUSERPWD:
1567
1687
    /*
1568
1688
     * user:password needed to use the proxy
1569
1689
     */
1570
 
    result = setstropt(&data->set.str[STRING_PROXYUSERPWD],
1571
 
                            va_arg(param, char *));
1572
 
    break;
 
1690
    result = setstropt_userpwd(va_arg(param, char *),
 
1691
                               &data->set.str[STRING_PROXYUSERNAME],
 
1692
                               &data->set.str[STRING_PROXYPASSWORD]);
 
1693
    break;
 
1694
  case CURLOPT_PROXYUSERNAME:
 
1695
    /*
 
1696
     * authentication user name to use in the operation
 
1697
     */
 
1698
    result = setstropt(&data->set.str[STRING_PROXYUSERNAME],
 
1699
                       va_arg(param, char *));
 
1700
    break;
 
1701
  case CURLOPT_PROXYPASSWORD:
 
1702
    /*
 
1703
     * authentication password to use in the operation
 
1704
     */
 
1705
    result = setstropt(&data->set.str[STRING_PROXYPASSWORD],
 
1706
                       va_arg(param, char *));
 
1707
    break;
 
1708
  case CURLOPT_NOPROXY:
 
1709
    /*
 
1710
     * proxy exception list
 
1711
     */
 
1712
    result = setstropt(&data->set.str[STRING_NOPROXY],
 
1713
                       va_arg(param, char *));
 
1714
    break;
 
1715
#endif
 
1716
 
1573
1717
  case CURLOPT_RANGE:
1574
1718
    /*
1575
1719
     * What range of the file you want to transfer
1576
1720
     */
1577
1721
    result = setstropt(&data->set.str[STRING_SET_RANGE],
1578
 
                            va_arg(param, char *));
 
1722
                       va_arg(param, char *));
1579
1723
    break;
1580
1724
  case CURLOPT_RESUME_FROM:
1581
1725
    /*
1685
1829
     * String that holds file name of the SSL certificate to use
1686
1830
     */
1687
1831
    result = setstropt(&data->set.str[STRING_CERT],
1688
 
                            va_arg(param, char *));
 
1832
                       va_arg(param, char *));
1689
1833
    break;
1690
1834
  case CURLOPT_SSLCERTTYPE:
1691
1835
    /*
1692
1836
     * String that holds file type of the SSL certificate to use
1693
1837
     */
1694
1838
    result = setstropt(&data->set.str[STRING_CERT_TYPE],
1695
 
                            va_arg(param, char *));
 
1839
                       va_arg(param, char *));
1696
1840
    break;
1697
1841
  case CURLOPT_SSLKEY:
1698
1842
    /*
1699
1843
     * String that holds file name of the SSL certificate to use
1700
1844
     */
1701
1845
    result = setstropt(&data->set.str[STRING_KEY],
1702
 
                            va_arg(param, char *));
 
1846
                       va_arg(param, char *));
1703
1847
    break;
1704
1848
  case CURLOPT_SSLKEYTYPE:
1705
1849
    /*
1706
1850
     * String that holds file type of the SSL certificate to use
1707
1851
     */
1708
1852
    result = setstropt(&data->set.str[STRING_KEY_TYPE],
1709
 
                            va_arg(param, char *));
 
1853
                       va_arg(param, char *));
1710
1854
    break;
1711
1855
  case CURLOPT_KEYPASSWD:
1712
1856
    /*
1713
1857
     * String that holds the SSL or SSH private key password.
1714
1858
     */
1715
1859
    result = setstropt(&data->set.str[STRING_KEY_PASSWD],
1716
 
                            va_arg(param, char *));
 
1860
                       va_arg(param, char *));
1717
1861
    break;
1718
1862
  case CURLOPT_SSLENGINE:
1719
1863
    /*
1721
1865
     */
1722
1866
    argptr = va_arg(param, char *);
1723
1867
    if(argptr && argptr[0])
1724
 
       result = Curl_ssl_set_engine(data, argptr);
 
1868
      result = Curl_ssl_set_engine(data, argptr);
1725
1869
    break;
1726
1870
 
1727
1871
  case CURLOPT_SSLENGINE_DEFAULT:
1743
1887
     * performing an operation and thus what from-IP your connection will use.
1744
1888
     */
1745
1889
    result = setstropt(&data->set.str[STRING_DEVICE],
1746
 
                            va_arg(param, char *));
 
1890
                       va_arg(param, char *));
1747
1891
    break;
1748
1892
  case CURLOPT_LOCALPORT:
1749
1893
    /*
1762
1906
     * A string that defines the kerberos security level.
1763
1907
     */
1764
1908
    result = setstropt(&data->set.str[STRING_KRB_LEVEL],
1765
 
                            va_arg(param, char *));
 
1909
                       va_arg(param, char *));
1766
1910
    data->set.krb = (bool)(NULL != data->set.str[STRING_KRB_LEVEL]);
1767
1911
    break;
1768
1912
  case CURLOPT_SSL_VERIFYPEER:
1794
1938
     */
1795
1939
    data->set.ssl.fsslctxp = va_arg(param, void *);
1796
1940
    break;
 
1941
  case CURLOPT_CERTINFO:
 
1942
    data->set.ssl.certinfo = (bool)(0 != va_arg(param, long));
 
1943
    break;
1797
1944
#endif
1798
1945
  case CURLOPT_CAINFO:
1799
1946
    /*
1800
1947
     * Set CA info for SSL connection. Specify file name of the CA certificate
1801
1948
     */
1802
1949
    result = setstropt(&data->set.str[STRING_SSL_CAFILE],
1803
 
                            va_arg(param, char *));
 
1950
                       va_arg(param, char *));
1804
1951
    break;
1805
1952
  case CURLOPT_CAPATH:
1806
1953
    /*
1809
1956
     */
1810
1957
    /* This does not work on windows. */
1811
1958
    result = setstropt(&data->set.str[STRING_SSL_CAPATH],
1812
 
                            va_arg(param, char *));
 
1959
                       va_arg(param, char *));
 
1960
    break;
 
1961
  case CURLOPT_CRLFILE:
 
1962
    /*
 
1963
     * Set CRL file info for SSL connection. Specify file name of the CRL
 
1964
     * to check certificates revocation
 
1965
     */
 
1966
    result = setstropt(&data->set.str[STRING_SSL_CRLFILE],
 
1967
                       va_arg(param, char *));
 
1968
    break;
 
1969
  case CURLOPT_ISSUERCERT:
 
1970
    /*
 
1971
     * Set Issuer certificate file
 
1972
     * to check certificates issuer
 
1973
     */
 
1974
    result = setstropt(&data->set.str[STRING_SSL_ISSUERCERT],
 
1975
                       va_arg(param, char *));
1813
1976
    break;
1814
1977
  case CURLOPT_TELNETOPTIONS:
1815
1978
    /*
1840
2003
    break;
1841
2004
 
1842
2005
  case CURLOPT_SHARE:
1843
 
    {
1844
 
      struct Curl_share *set;
1845
 
      set = va_arg(param, struct Curl_share *);
1846
 
 
1847
 
      /* disconnect from old share, if any */
1848
 
      if(data->share) {
1849
 
        Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
1850
 
 
1851
 
        if(data->dns.hostcachetype == HCACHE_SHARED) {
1852
 
          data->dns.hostcache = NULL;
1853
 
          data->dns.hostcachetype = HCACHE_NONE;
1854
 
        }
1855
 
 
1856
 
        if(data->share->cookies == data->cookies)
1857
 
          data->cookies = NULL;
1858
 
 
1859
 
        data->share->dirty--;
1860
 
 
1861
 
        Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
1862
 
        data->share = NULL;
1863
 
      }
1864
 
 
1865
 
      /* use new share if it set */
1866
 
      data->share = set;
1867
 
      if(data->share) {
1868
 
 
1869
 
        Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
1870
 
 
1871
 
        data->share->dirty++;
1872
 
 
1873
 
        if(data->share->hostcache) {
1874
 
          /* use shared host cache, first free the private one if any */
1875
 
          if(data->dns.hostcachetype == HCACHE_PRIVATE)
1876
 
            Curl_hash_destroy(data->dns.hostcache);
1877
 
 
1878
 
          data->dns.hostcache = data->share->hostcache;
1879
 
          data->dns.hostcachetype = HCACHE_SHARED;
1880
 
        }
 
2006
  {
 
2007
    struct Curl_share *set;
 
2008
    set = va_arg(param, struct Curl_share *);
 
2009
 
 
2010
    /* disconnect from old share, if any */
 
2011
    if(data->share) {
 
2012
      Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
 
2013
 
 
2014
      if(data->dns.hostcachetype == HCACHE_SHARED) {
 
2015
        data->dns.hostcache = NULL;
 
2016
        data->dns.hostcachetype = HCACHE_NONE;
 
2017
      }
 
2018
 
 
2019
      if(data->share->cookies == data->cookies)
 
2020
        data->cookies = NULL;
 
2021
 
 
2022
      data->share->dirty--;
 
2023
 
 
2024
      Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
 
2025
      data->share = NULL;
 
2026
    }
 
2027
 
 
2028
    /* use new share if it set */
 
2029
    data->share = set;
 
2030
    if(data->share) {
 
2031
 
 
2032
      Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
 
2033
 
 
2034
      data->share->dirty++;
 
2035
 
 
2036
      if(data->share->hostcache) {
 
2037
        /* use shared host cache, first free the private one if any */
 
2038
        if(data->dns.hostcachetype == HCACHE_PRIVATE)
 
2039
          Curl_hash_destroy(data->dns.hostcache);
 
2040
 
 
2041
        data->dns.hostcache = data->share->hostcache;
 
2042
        data->dns.hostcachetype = HCACHE_SHARED;
 
2043
      }
1881
2044
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
1882
 
        if(data->share->cookies) {
1883
 
          /* use shared cookie list, first free own one if any */
1884
 
          if(data->cookies)
1885
 
            Curl_cookie_cleanup(data->cookies);
1886
 
          /* enable cookies since we now use a share that uses cookies! */
1887
 
          data->cookies = data->share->cookies;
1888
 
        }
 
2045
      if(data->share->cookies) {
 
2046
        /* use shared cookie list, first free own one if any */
 
2047
        if(data->cookies)
 
2048
          Curl_cookie_cleanup(data->cookies);
 
2049
        /* enable cookies since we now use a share that uses cookies! */
 
2050
        data->cookies = data->share->cookies;
 
2051
      }
1889
2052
#endif   /* CURL_DISABLE_HTTP */
1890
 
        Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
 
2053
      Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
1891
2054
 
1892
 
      }
1893
 
      /* check for host cache not needed,
1894
 
       * it will be done by curl_easy_perform */
1895
2055
    }
1896
 
    break;
1897
 
 
1898
 
  case CURLOPT_PROXYTYPE:
1899
 
    /*
1900
 
     * Set proxy type. HTTP/SOCKS4/SOCKS4a/SOCKS5/SOCKS5_HOSTNAME
1901
 
     */
1902
 
    data->set.proxytype = (curl_proxytype)va_arg(param, long);
1903
 
    break;
 
2056
    /* check for host cache not needed,
 
2057
     * it will be done by curl_easy_perform */
 
2058
  }
 
2059
  break;
1904
2060
 
1905
2061
  case CURLOPT_PRIVATE:
1906
2062
    /*
1949
2105
    data->set.tcp_nodelay = (bool)(0 != va_arg(param, long));
1950
2106
    break;
1951
2107
 
1952
 
    /*
1953
 
      case CURLOPT_SOURCE_URL:
1954
 
      case CURLOPT_SOURCE_USERPWD:
1955
 
      case CURLOPT_SOURCE_QUOTE:
1956
 
      case CURLOPT_SOURCE_PREQUOTE:
1957
 
      case CURLOPT_SOURCE_POSTQUOTE:
1958
 
      These former 3rd party transfer options are deprecated */
1959
 
 
1960
2108
  case CURLOPT_FTP_ACCOUNT:
1961
2109
    result = setstropt(&data->set.str[STRING_FTP_ACCOUNT],
1962
 
                            va_arg(param, char *));
 
2110
                       va_arg(param, char *));
1963
2111
    break;
1964
2112
 
1965
2113
  case CURLOPT_IGNORE_CONTENT_LENGTH:
1975
2123
 
1976
2124
  case CURLOPT_FTP_ALTERNATIVE_TO_USER:
1977
2125
    result = setstropt(&data->set.str[STRING_FTP_ALTERNATIVE_TO_USER],
1978
 
                            va_arg(param, char *));
 
2126
                       va_arg(param, char *));
1979
2127
    break;
1980
2128
 
1981
2129
  case CURLOPT_SOCKOPTFUNCTION:
2020
2168
     * Use this file instead of the $HOME/.ssh/id_dsa.pub file
2021
2169
     */
2022
2170
    result = setstropt(&data->set.str[STRING_SSH_PUBLIC_KEY],
2023
 
                            va_arg(param, char *));
 
2171
                       va_arg(param, char *));
2024
2172
    break;
2025
2173
 
2026
2174
  case CURLOPT_SSH_PRIVATE_KEYFILE:
2028
2176
     * Use this file instead of the $HOME/.ssh/id_dsa file
2029
2177
     */
2030
2178
    result = setstropt(&data->set.str[STRING_SSH_PRIVATE_KEY],
2031
 
                            va_arg(param, char *));
 
2179
                       va_arg(param, char *));
2032
2180
    break;
2033
2181
  case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
2034
2182
    /*
2036
2184
     * for validation purposes.
2037
2185
     */
2038
2186
    result = setstropt(&data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5],
2039
 
                            va_arg(param, char *));
 
2187
                       va_arg(param, char *));
2040
2188
    break;
2041
2189
  case CURLOPT_HTTP_TRANSFER_DECODING:
2042
2190
    /*
2065
2213
     */
2066
2214
    data->set.new_directory_perms = va_arg(param, long);
2067
2215
    break;
2068
 
  case CURLOPT_PROXY_TRANSFER_MODE:
 
2216
 
 
2217
  case CURLOPT_ADDRESS_SCOPE:
2069
2218
    /*
2070
 
     * set transfer mode (;type=<a|i>) when doing FTP via an HTTP proxy
 
2219
     * We always get longs when passed plain numericals, but for this value we
 
2220
     * know that an unsigned int will always hold the value so we blindly
 
2221
     * typecast to this type
2071
2222
     */
2072
 
    switch (va_arg(param, long)) {
2073
 
      case 0:
2074
 
        data->set.proxy_transfer_mode = FALSE;
2075
 
        break;
2076
 
      case 1:
2077
 
        data->set.proxy_transfer_mode = TRUE;
2078
 
        break;
2079
 
      default:
2080
 
        /* reserve other values for future use */
2081
 
        result = CURLE_FAILED_INIT;
2082
 
        break;
2083
 
    }
 
2223
    data->set.scope = (unsigned int) va_arg(param, long);
 
2224
    break;
 
2225
 
 
2226
  case CURLOPT_PROTOCOLS:
 
2227
    /* set the bitmask for the protocols that are allowed to be used for the
 
2228
       transfer, which thus helps the app which takes URLs from users or other
 
2229
       external inputs and want to restrict what protocol(s) to deal
 
2230
       with. Defaults to CURLPROTO_ALL. */
 
2231
    data->set.allowed_protocols = va_arg(param, long) & PROT_EXTMASK;
 
2232
    break;
 
2233
 
 
2234
  case CURLOPT_REDIR_PROTOCOLS:
 
2235
    /* set the bitmask for the protocols that libcurl is allowed to follow to,
 
2236
       as a subset of the CURLOPT_PROTOCOLS ones. That means the protocol needs
 
2237
       to be set in both bitmasks to be allowed to get redirected to. Defaults
 
2238
       to all protocols except FILE and SCP. */
 
2239
    data->set.redir_protocols = va_arg(param, long) & PROT_EXTMASK;
2084
2240
    break;
2085
2241
 
2086
2242
  default:
2115
2271
  Curl_safefree(conn->allocptr.ref);
2116
2272
  Curl_safefree(conn->allocptr.host);
2117
2273
  Curl_safefree(conn->allocptr.cookiehost);
2118
 
  Curl_safefree(conn->ip_addr_str);
2119
2274
  Curl_safefree(conn->trailer);
2120
2275
  Curl_safefree(conn->host.rawalloc); /* host name buffer */
2121
2276
  Curl_safefree(conn->proxy.rawalloc); /* proxy name buffer */
2162
2317
  Curl_expire(data, 0); /* shut off timers */
2163
2318
  Curl_hostcache_prune(data); /* kill old DNS cache entries */
2164
2319
 
2165
 
  if((conn->ntlm.state != NTLMSTATE_NONE) ||
2166
 
     (conn->proxyntlm.state != NTLMSTATE_NONE)) {
 
2320
  {
 
2321
    int has_host_ntlm = (conn->ntlm.state != NTLMSTATE_NONE);
 
2322
    int has_proxy_ntlm = (conn->proxyntlm.state != NTLMSTATE_NONE);
 
2323
 
2167
2324
    /* Authentication data is a mix of connection-related and sessionhandle-
2168
2325
       related stuff. NTLM is connection-related so when we close the shop
2169
2326
       we shall forget. */
2170
 
    data->state.authhost.done = FALSE;
2171
 
    data->state.authhost.picked =
2172
 
      data->state.authhost.want;
2173
 
 
2174
 
    data->state.authproxy.done = FALSE;
2175
 
    data->state.authproxy.picked =
2176
 
      data->state.authproxy.want;
2177
 
 
2178
 
    data->state.authproblem = FALSE;
2179
 
 
2180
 
    Curl_ntlm_cleanup(conn);
 
2327
 
 
2328
    if (has_host_ntlm) {
 
2329
      data->state.authhost.done = FALSE;
 
2330
      data->state.authhost.picked =
 
2331
        data->state.authhost.want;
 
2332
    }
 
2333
 
 
2334
    if (has_proxy_ntlm) {
 
2335
      data->state.authproxy.done = FALSE;
 
2336
      data->state.authproxy.picked =
 
2337
        data->state.authproxy.want;
 
2338
    }
 
2339
 
 
2340
    if (has_host_ntlm || has_proxy_ntlm) {
 
2341
      data->state.authproblem = FALSE;
 
2342
 
 
2343
      Curl_ntlm_cleanup(conn);
 
2344
    }
 
2345
  }
 
2346
 
 
2347
  /* Cleanup possible redirect junk */
 
2348
  if(data->req.newurl) {
 
2349
    free(data->req.newurl);
 
2350
    data->req.newurl = NULL;
2181
2351
  }
2182
2352
 
2183
2353
  if(conn->handler->disconnect)
2312
2482
  return NULL;
2313
2483
}
2314
2484
 
 
2485
/* remove the specified connection from all (possible) pipelines and related
 
2486
   queues */
 
2487
void Curl_getoff_all_pipelines(struct SessionHandle *data,
 
2488
                               struct connectdata *conn)
 
2489
{
 
2490
  bool recv_head = (bool)(conn->readchannel_inuse &&
 
2491
    (gethandleathead(conn->recv_pipe) == data));
 
2492
 
 
2493
  bool send_head = (bool)(conn->writechannel_inuse &&
 
2494
    (gethandleathead(conn->send_pipe) == data));
 
2495
 
 
2496
  if(Curl_removeHandleFromPipeline(data, conn->recv_pipe) && recv_head)
 
2497
    conn->readchannel_inuse = FALSE;
 
2498
  if(Curl_removeHandleFromPipeline(data, conn->send_pipe) && send_head)
 
2499
    conn->writechannel_inuse = FALSE;
 
2500
  Curl_removeHandleFromPipeline(data, conn->pend_pipe);
 
2501
}
 
2502
 
2315
2503
static void signalPipeClose(struct curl_llist *pipeline)
2316
2504
{
2317
2505
  struct curl_llist_element *curr;
2405
2593
      }
2406
2594
 
2407
2595
#ifdef CURLRES_ASYNCH
2408
 
      /* ip_addr_str is NULL only if the resolving of the name hasn't completed
2409
 
         yet and until then we don't re-use this connection */
2410
 
      if(!check->ip_addr_str) {
 
2596
      /* ip_addr_str[0] is NUL only if the resolving of the name hasn't
 
2597
         completed yet and until then we don't re-use this connection */
 
2598
      if(!check->ip_addr_str[0]) {
2411
2599
        infof(data,
2412
2600
              "Connection #%ld hasn't finished name resolve, can't reuse\n",
2413
2601
              check->connectindex);
2438
2626
      /* don't do mixed proxy and non-proxy connections */
2439
2627
      continue;
2440
2628
 
 
2629
    if(!canPipeline && check->inuse)
 
2630
      /* this request can't be pipelined but the checked connection is already
 
2631
         in use so we skip it */
 
2632
      continue;
 
2633
 
2441
2634
    if(!needle->bits.httpproxy || needle->protocol&PROT_SSL ||
2442
2635
       (needle->bits.httpproxy && check->bits.httpproxy &&
2443
2636
        needle->bits.tunnel_proxy && check->bits.tunnel_proxy &&
2444
 
        strequal(needle->proxy.name, check->proxy.name) &&
 
2637
        Curl_raw_equal(needle->proxy.name, check->proxy.name) &&
2445
2638
        (needle->port == check->port))) {
2446
2639
      /* The requested connection does not use a HTTP proxy or it uses SSL or
2447
2640
         it is a non-SSL protocol tunneled over the same http proxy name and
2448
2641
         port number */
2449
2642
 
2450
 
      if(strequal(needle->protostr, check->protostr) &&
2451
 
         strequal(needle->host.name, check->host.name) &&
 
2643
      if(Curl_raw_equal(needle->protostr, check->protostr) &&
 
2644
         Curl_raw_equal(needle->host.name, check->host.name) &&
2452
2645
         (needle->remote_port == check->remote_port) ) {
2453
2646
        if(needle->protocol & PROT_SSL) {
2454
2647
          /* This is SSL, verify that we're using the same
2487
2680
              is the checked one using the same host, port and type? */
2488
2681
      if(check->bits.proxy &&
2489
2682
         (needle->proxytype == check->proxytype) &&
2490
 
         strequal(needle->proxy.name, check->proxy.name) &&
 
2683
         Curl_raw_equal(needle->proxy.name, check->proxy.name) &&
2491
2684
         needle->port == check->port) {
2492
2685
        /* This is the same proxy connection, use it! */
2493
2686
        match = TRUE;
2495
2688
    }
2496
2689
 
2497
2690
    if(match) {
2498
 
      if(pipeLen == 0) {
2499
 
        /* The check for a dead socket makes sense only if there
2500
 
           are no handles in pipeline */
 
2691
      if(!pipeLen && !check->inuse) {
 
2692
        /* The check for a dead socket makes sense only if there are no
 
2693
           handles in pipeline and the connection isn't already marked in
 
2694
           use */
2501
2695
        bool dead = SocketIsDead(check->sock[FIRSTSOCKET]);
2502
2696
        if(dead) {
2503
2697
          check->data = data;
2652
2846
    conn->dns_entry = hostaddr;
2653
2847
    conn->ip_addr = addr;
2654
2848
 
2655
 
    result = Curl_store_ip_addr(conn);
2656
 
 
2657
 
    if(CURLE_OK == result) {
2658
 
 
2659
 
      switch(data->set.proxytype) {
2660
 
      case CURLPROXY_SOCKS5:
2661
 
      case CURLPROXY_SOCKS5_HOSTNAME:
2662
 
        result = Curl_SOCKS5(conn->proxyuser, conn->proxypasswd,
2663
 
                             conn->host.name, conn->remote_port,
2664
 
                             FIRSTSOCKET, conn);
2665
 
        break;
2666
 
      case CURLPROXY_HTTP:
2667
 
        /* do nothing here. handled later. */
2668
 
        break;
2669
 
      case CURLPROXY_SOCKS4:
2670
 
        result = Curl_SOCKS4(conn->proxyuser, conn->host.name,
2671
 
                             conn->remote_port, FIRSTSOCKET, conn, FALSE);
2672
 
        break;
2673
 
      case CURLPROXY_SOCKS4A:
2674
 
        result = Curl_SOCKS4(conn->proxyuser, conn->host.name,
2675
 
                             conn->remote_port, FIRSTSOCKET, conn, TRUE);
2676
 
        break;
2677
 
      default:
2678
 
        failf(data, "unknown proxytype option given");
2679
 
        result = CURLE_COULDNT_CONNECT;
2680
 
        break;
2681
 
      }
2682
 
    }
2683
 
  }
 
2849
    switch(data->set.proxytype) {
 
2850
#ifndef CURL_DISABLE_PROXY
 
2851
    case CURLPROXY_SOCKS5:
 
2852
    case CURLPROXY_SOCKS5_HOSTNAME:
 
2853
      result = Curl_SOCKS5(conn->proxyuser, conn->proxypasswd,
 
2854
                           conn->host.name, conn->remote_port,
 
2855
                           FIRSTSOCKET, conn);
 
2856
      break;
 
2857
    case CURLPROXY_SOCKS4:
 
2858
      result = Curl_SOCKS4(conn->proxyuser, conn->host.name,
 
2859
                           conn->remote_port, FIRSTSOCKET, conn, FALSE);
 
2860
      break;
 
2861
    case CURLPROXY_SOCKS4A:
 
2862
      result = Curl_SOCKS4(conn->proxyuser, conn->host.name,
 
2863
                           conn->remote_port, FIRSTSOCKET, conn, TRUE);
 
2864
      break;
 
2865
#endif /* CURL_DISABLE_PROXY */
 
2866
    case CURLPROXY_HTTP:
 
2867
    case CURLPROXY_HTTP_1_0:
 
2868
      /* do nothing here. handled later. */
 
2869
      break;
 
2870
    default:
 
2871
      failf(data, "unknown proxytype option given");
 
2872
      result = CURLE_COULDNT_CONNECT;
 
2873
      break;
 
2874
    } /* switch proxytype */
 
2875
  } /* if result is ok */
 
2876
 
2684
2877
  if(result)
2685
2878
    *connected = FALSE; /* mark it as not connected */
2686
2879
 
2839
3032
  char *uc_name = NULL;
2840
3033
  int rc;
2841
3034
#ifndef CURL_DISABLE_VERBOSE_STRINGS
2842
 
  char *tld_errmsg = (char *)"<no msg>";
 
3035
  const char *tld_errmsg = "<no msg>";
2843
3036
#else
2844
3037
  (void)data;
2845
3038
#endif
2853
3046
#ifndef CURL_DISABLE_VERBOSE_STRINGS
2854
3047
#ifdef HAVE_TLD_STRERROR
2855
3048
  if(rc != TLD_SUCCESS)
2856
 
    tld_errmsg = (char *)tld_strerror((Tld_rc)rc);
 
3049
    tld_errmsg = tld_strerror((Tld_rc)rc);
2857
3050
#endif
2858
3051
  if(rc == TLD_INVALID)
2859
3052
    infof(data, "WARNING: %s; pos %u = `%c'/0x%02X\n",
2869
3062
}
2870
3063
#endif
2871
3064
 
 
3065
/*
 
3066
 * Perform any necessary IDN conversion of hostname
 
3067
 */
2872
3068
static void fix_hostname(struct SessionHandle *data,
2873
3069
                         struct connectdata *conn, struct hostname *host)
2874
3070
{
2909
3105
}
2910
3106
 
2911
3107
/*
 
3108
 * Allocate and initialize a new connectdata object.
 
3109
 */
 
3110
static struct connectdata *allocate_conn(void)
 
3111
{
 
3112
  struct connectdata *conn;
 
3113
 
 
3114
  conn = calloc(1, sizeof(struct connectdata));
 
3115
  if(!conn)
 
3116
    return NULL;
 
3117
 
 
3118
  conn->handler = &Curl_handler_dummy;  /* Be sure we have a handler defined
 
3119
                                           already from start to avoid NULL
 
3120
                                           situations and checks */
 
3121
 
 
3122
  /* and we setup a few fields in case we end up actually using this struct */
 
3123
 
 
3124
  conn->sock[FIRSTSOCKET] = CURL_SOCKET_BAD;     /* no file descriptor */
 
3125
  conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
 
3126
  conn->connectindex = -1;    /* no index */
 
3127
 
 
3128
  /* Default protocol-independent behavior doesn't support persistent
 
3129
     connections, so we set this to force-close. Protocols that support
 
3130
     this need to set this to FALSE in their "curl_do" functions. */
 
3131
  conn->bits.close = TRUE;
 
3132
 
 
3133
  /* Store creation time to help future close decision making */
 
3134
  conn->created = Curl_tvnow();
 
3135
 
 
3136
  return conn;
 
3137
}
 
3138
 
 
3139
/*
2912
3140
 * Parse URL and fill in the relevant members of the connection struct.
2913
3141
 */
2914
3142
static CURLcode ParseURLAndFillConnection(struct SessionHandle *data,
2916
3144
{
2917
3145
  char *at;
2918
3146
  char *tmp;
2919
 
 
2920
3147
  char *path = data->state.path;
 
3148
  int rc;
2921
3149
 
2922
3150
  /*************************************************************
2923
3151
   * Parse the URL.
2929
3157
   ************************************************************/
2930
3158
  if((2 == sscanf(data->change.url, "%15[^:]:%[^\n]",
2931
3159
                  conn->protostr,
2932
 
                  path)) && strequal(conn->protostr, "file")) {
 
3160
                  path)) && Curl_raw_equal(conn->protostr, "file")) {
2933
3161
    if(path[0] == '/' && path[1] == '/') {
2934
3162
      /* Allow omitted hostname (e.g. file:/<path>).  This is not strictly
2935
3163
       * speaking a valid file: URL by RFC 1738, but treating file:/<path> as
2990
3218
       * The URL was badly formatted, let's try the browser-style _without_
2991
3219
       * protocol specified like 'http://'.
2992
3220
       */
2993
 
      if((1 > sscanf(data->change.url, "%[^\n/]%[^\n]",
2994
 
                     conn->host.name, path)) ) {
 
3221
      if(1 > (rc = sscanf(data->change.url, "%[^\n/]%[^\n]",
 
3222
                          conn->host.name, path)) ) {
2995
3223
        /*
2996
3224
         * We couldn't even get this format.
 
3225
         * djgpp 2.04 has a sscanf() bug where 'conn->host.name' is
 
3226
         * assigned, but the return value is EOF!
2997
3227
         */
2998
 
        failf(data, "<url> malformed");
2999
 
        return CURLE_URL_MALFORMAT;
 
3228
#if defined(__DJGPP__) && (DJGPP_MINOR == 4)
 
3229
        if (!(rc == -1 && *conn->host.name))
 
3230
#endif
 
3231
        {
 
3232
          failf(data, "<url> malformed");
 
3233
          return CURLE_URL_MALFORMAT;
 
3234
        }
3000
3235
      }
3001
3236
 
3002
3237
      /*
3071
3306
    path[0] = '/';
3072
3307
  }
3073
3308
 
 
3309
  if (conn->host.name[0] == '[') {
 
3310
    /* This looks like an IPv6 address literal.  See if there is an address
 
3311
       scope.  */
 
3312
    char *percent = strstr (conn->host.name, "%25");
 
3313
    if (percent) {
 
3314
      char *endp;
 
3315
      unsigned long scope = strtoul (percent + 3, &endp, 10);
 
3316
      if (*endp == ']') {
 
3317
        /* The address scope was well formed.  Knock it out of the hostname.  */
 
3318
        memmove(percent, endp, strlen(endp)+1);
 
3319
        if (!data->state.this_is_a_follow)
 
3320
          /* Don't honour a scope given in a Location: header */
 
3321
          conn->scope = (unsigned int)scope;
 
3322
      } else
 
3323
        infof(data, "Invalid IPv6 address format\n");
 
3324
    }
 
3325
  }
 
3326
 
 
3327
  if (data->set.scope)
 
3328
    /* Override any scope that was set above.  */
 
3329
    conn->scope = data->set.scope;
 
3330
 
3074
3331
  /*
3075
3332
   * So if the URL was A://B/C,
3076
3333
   *   conn->protostr is A
3077
3334
   *   conn->host.name is B
3078
3335
   *   data->state.path is /C
3079
3336
   */
3080
 
 
 
3337
  (void)rc;
3081
3338
  return CURLE_OK;
3082
3339
}
3083
3340
 
3088
3345
  /* Do nothing */
3089
3346
}
3090
3347
 
 
3348
/*
 
3349
 * If we're doing a resumed transfer, we need to setup our stuff
 
3350
 * properly.
 
3351
 */
3091
3352
static CURLcode setup_range(struct SessionHandle *data)
3092
3353
{
3093
 
  /*
3094
 
   * If we're doing a resumed transfer, we need to setup our stuff
3095
 
   * properly.
3096
 
   */
3097
3354
  struct UrlState *s = &data->state;
3098
3355
  s->resume_from = data->set.set_resume_from;
3099
3356
  if(s->resume_from || data->set.str[STRING_SET_RANGE]) {
3101
3358
      free(s->range);
3102
3359
 
3103
3360
    if(s->resume_from)
3104
 
      s->range = aprintf("%" FORMAT_OFF_T "-", s->resume_from);
 
3361
      s->range = aprintf("%" FORMAT_OFF_TU "-", s->resume_from);
3105
3362
    else
3106
3363
      s->range = strdup(data->set.str[STRING_SET_RANGE]);
3107
3364
 
3135
3392
  /* Scan protocol handler table. */
3136
3393
 
3137
3394
  for (pp = protocols; (p = *pp) != NULL; pp++)
3138
 
    if(strequal(p->scheme, conn->protostr)) {
3139
 
      /* Protocol found in table. Perform setup complement if some. */
 
3395
    if(Curl_raw_equal(p->scheme, conn->protostr)) {
 
3396
      /* Protocol found in table. Check if allowed */
 
3397
      if(!(data->set.allowed_protocols & p->protocol))
 
3398
        /* nope, get out */
 
3399
        break;
 
3400
 
 
3401
      /* it is allowed for "normal" request, now do an extra check if this is
 
3402
         the result of a redirect */
 
3403
      if(data->state.this_is_a_follow &&
 
3404
         !(data->set.redir_protocols & p->protocol))
 
3405
        /* nope, get out */
 
3406
        break;
 
3407
 
 
3408
      /* Perform setup complement if some. */
3140
3409
      conn->handler = p;
3141
3410
 
3142
3411
      if(p->setup_connection) {
3156
3425
 
3157
3426
  /* The protocol was not found in the table, but we don't have to assign it
3158
3427
     to anything since it is already assigned to a dummy-struct in the
3159
 
     CreateConnection() function when the connectdata struct is allocated. */
 
3428
     create_conn() function when the connectdata struct is allocated. */
3160
3429
  failf(data, "Protocol %s not supported or disabled in " LIBCURL_NAME,
3161
3430
        conn->protostr);
3162
3431
  return CURLE_UNSUPPORTED_PROTOCOL;
3163
3432
}
3164
3433
 
 
3434
#ifndef CURL_DISABLE_PROXY
 
3435
/****************************************************************
 
3436
* Checks if the host is in the noproxy list. returns true if it matches
 
3437
* and therefore the proxy should NOT be used.
 
3438
****************************************************************/
 
3439
static bool check_noproxy(const char* name, const char* no_proxy)
 
3440
{
 
3441
  /* no_proxy=domain1.dom,host.domain2.dom
 
3442
   *   (a comma-separated list of hosts which should
 
3443
   *   not be proxied, or an asterisk to override
 
3444
   *   all proxy variables)
 
3445
   */
 
3446
  size_t tok_start;
 
3447
  size_t tok_end;
 
3448
  const char* separator = ", ";
 
3449
  size_t no_proxy_len;
 
3450
  size_t namelen;
 
3451
  char *endptr;
 
3452
 
 
3453
  if(no_proxy && no_proxy[0]) {
 
3454
    if(Curl_raw_equal("*", no_proxy)) {
 
3455
      return TRUE;
 
3456
    }
 
3457
 
 
3458
    /* NO_PROXY was specified and it wasn't just an asterisk */
 
3459
 
 
3460
    no_proxy_len = strlen(no_proxy);
 
3461
    endptr = strchr(name, ':');
 
3462
    if(endptr)
 
3463
      namelen = endptr - name;
 
3464
    else
 
3465
      namelen = strlen(name);
 
3466
 
 
3467
    tok_start = 0;
 
3468
    for (tok_start = 0; tok_start < no_proxy_len; tok_start = tok_end + 1) {
 
3469
      while (tok_start < no_proxy_len &&
 
3470
             strchr(separator, no_proxy[tok_start]) != NULL) {
 
3471
        /* Look for the beginning of the token. */
 
3472
        ++tok_start;
 
3473
      }
 
3474
 
 
3475
      if(tok_start == no_proxy_len)
 
3476
        break; /* It was all trailing separator chars, no more tokens. */
 
3477
 
 
3478
      for (tok_end = tok_start; tok_end < no_proxy_len &&
 
3479
             strchr(separator, no_proxy[tok_end]) == NULL; ++tok_end) {
 
3480
        /* Look for the end of the token. */
 
3481
      }
 
3482
 
 
3483
      /* To match previous behaviour, where it was necessary to specify
 
3484
       * ".local.com" to prevent matching "notlocal.com", we will leave
 
3485
       * the '.' off.
 
3486
       */
 
3487
      if(no_proxy[tok_start] == '.')
 
3488
        ++tok_start;
 
3489
 
 
3490
      if((tok_end - tok_start) <= namelen) {
 
3491
        /* Match the last part of the name to the domain we are checking. */
 
3492
        const char *checkn = name + namelen - (tok_end - tok_start);
 
3493
        if(Curl_raw_nequal(no_proxy + tok_start, checkn, tok_end - tok_start)) {
 
3494
          if((tok_end - tok_start) == namelen || *(checkn - 1) == '.') {
 
3495
            /* We either have an exact match, or the previous character is a .
 
3496
             * so it is within the same domain, so no proxy for this host.
 
3497
             */
 
3498
            return TRUE;
 
3499
          }
 
3500
        }
 
3501
      } /* if((tok_end - tok_start) <= namelen) */
 
3502
    } /* for (tok_start = 0; tok_start < no_proxy_len;
 
3503
         tok_start = tok_end + 1) */
 
3504
  } /* NO_PROXY was specified and it wasn't just an asterisk */
 
3505
 
 
3506
  return FALSE;
 
3507
}
 
3508
 
3165
3509
/****************************************************************
3166
3510
* Detect what (if any) proxy to use. Remember that this selects a host
3167
3511
* name and is not limited to HTTP proxies only.
3190
3534
   * checked if the lowercase versions don't exist.
3191
3535
   */
3192
3536
  char *no_proxy=NULL;
3193
 
  char *no_proxy_tok_buf;
3194
3537
  char proxy_env[128];
3195
3538
 
3196
3539
  no_proxy=curl_getenv("no_proxy");
3197
3540
  if(!no_proxy)
3198
3541
    no_proxy=curl_getenv("NO_PROXY");
3199
3542
 
3200
 
  if(!no_proxy || !strequal("*", no_proxy)) {
3201
 
    /* NO_PROXY wasn't specified or it wasn't just an asterisk */
3202
 
    char *nope;
3203
 
 
3204
 
    nope=no_proxy?strtok_r(no_proxy, ", ", &no_proxy_tok_buf):NULL;
3205
 
    while(nope) {
3206
 
      size_t namelen;
3207
 
      char *endptr = strchr(conn->host.name, ':');
3208
 
      if(endptr)
3209
 
        namelen=endptr-conn->host.name;
3210
 
      else
3211
 
        namelen=strlen(conn->host.name);
3212
 
 
3213
 
      if(strlen(nope) <= namelen) {
3214
 
        char *checkn=
3215
 
          conn->host.name + namelen - strlen(nope);
3216
 
        if(checkprefix(nope, checkn)) {
3217
 
          /* no proxy for this host! */
3218
 
          break;
3219
 
        }
3220
 
      }
3221
 
      nope=strtok_r(NULL, ", ", &no_proxy_tok_buf);
3222
 
    }
3223
 
    if(!nope) {
3224
 
      /* It was not listed as without proxy */
3225
 
      char *protop = conn->protostr;
3226
 
      char *envp = proxy_env;
3227
 
      char *prox;
3228
 
 
3229
 
      /* Now, build <protocol>_proxy and check for such a one to use */
3230
 
      while(*protop)
3231
 
        *envp++ = (char)tolower((int)*protop++);
3232
 
 
3233
 
      /* append _proxy */
3234
 
      strcpy(envp, "_proxy");
3235
 
 
3236
 
      /* read the protocol proxy: */
 
3543
  if(!check_noproxy(conn->host.name, no_proxy)) {
 
3544
    /* It was not listed as without proxy */
 
3545
    char *protop = conn->protostr;
 
3546
    char *envp = proxy_env;
 
3547
    char *prox;
 
3548
 
 
3549
    /* Now, build <protocol>_proxy and check for such a one to use */
 
3550
    while(*protop)
 
3551
      *envp++ = (char)tolower((int)*protop++);
 
3552
 
 
3553
    /* append _proxy */
 
3554
    strcpy(envp, "_proxy");
 
3555
 
 
3556
    /* read the protocol proxy: */
 
3557
    prox=curl_getenv(proxy_env);
 
3558
 
 
3559
    /*
 
3560
     * We don't try the uppercase version of HTTP_PROXY because of
 
3561
     * security reasons:
 
3562
     *
 
3563
     * When curl is used in a webserver application
 
3564
     * environment (cgi or php), this environment variable can
 
3565
     * be controlled by the web server user by setting the
 
3566
     * http header 'Proxy:' to some value.
 
3567
     *
 
3568
     * This can cause 'internal' http/ftp requests to be
 
3569
     * arbitrarily redirected by any external attacker.
 
3570
     */
 
3571
    if(!prox && !Curl_raw_equal("http_proxy", proxy_env)) {
 
3572
      /* There was no lowercase variable, try the uppercase version: */
 
3573
      Curl_strntoupper(proxy_env, proxy_env, sizeof(proxy_env));
3237
3574
      prox=curl_getenv(proxy_env);
3238
 
 
3239
 
      /*
3240
 
       * We don't try the uppercase version of HTTP_PROXY because of
3241
 
       * security reasons:
3242
 
       *
3243
 
       * When curl is used in a webserver application
3244
 
       * environment (cgi or php), this environment variable can
3245
 
       * be controlled by the web server user by setting the
3246
 
       * http header 'Proxy:' to some value.
3247
 
       *
3248
 
       * This can cause 'internal' http/ftp requests to be
3249
 
       * arbitrarily redirected by any external attacker.
3250
 
       */
3251
 
      if(!prox && !strequal("http_proxy", proxy_env)) {
3252
 
        /* There was no lowercase variable, try the uppercase version: */
3253
 
        for(envp = proxy_env; *envp; envp++)
3254
 
          *envp = (char)toupper((int)*envp);
3255
 
        prox=curl_getenv(proxy_env);
3256
 
      }
3257
 
 
3258
 
      if(prox && *prox) { /* don't count "" strings */
3259
 
        proxy = prox; /* use this */
3260
 
      }
3261
 
      else {
3262
 
        proxy = curl_getenv("all_proxy"); /* default proxy to use */
3263
 
        if(!proxy)
3264
 
          proxy=curl_getenv("ALL_PROXY");
3265
 
      }
3266
 
 
3267
 
      if(proxy && *proxy) {
3268
 
        long bits = conn->protocol & (PROT_HTTPS|PROT_SSL|PROT_MISSING);
3269
 
 
3270
 
        if(conn->proxytype == CURLPROXY_HTTP) {
3271
 
          /* force this connection's protocol to become HTTP */
3272
 
          conn->protocol = PROT_HTTP | bits;
3273
 
          conn->bits.proxy = conn->bits.httpproxy = TRUE;
3274
 
        }
3275
 
      }
3276
 
    } /* if(!nope) - it wasn't specified non-proxy */
3277
 
  } /* NO_PROXY wasn't specified or '*' */
 
3575
    }
 
3576
 
 
3577
    if(prox && *prox) { /* don't count "" strings */
 
3578
      proxy = prox; /* use this */
 
3579
    }
 
3580
    else {
 
3581
      proxy = curl_getenv("all_proxy"); /* default proxy to use */
 
3582
      if(!proxy)
 
3583
        proxy=curl_getenv("ALL_PROXY");
 
3584
    }
 
3585
  } /* if(!check_noproxy(conn->host.name, no_proxy)) - it wasn't specified
 
3586
       non-proxy */
3278
3587
  if(no_proxy)
3279
3588
    free(no_proxy);
3280
3589
 
3286
3595
  return proxy;
3287
3596
}
3288
3597
 
3289
 
/* If this is supposed to use a proxy, we need to figure out the proxy
 
3598
/*
 
3599
 * If this is supposed to use a proxy, we need to figure out the proxy
3290
3600
 * host name, so that we can re-use an existing connection
3291
3601
 * that may exist registered to the same proxy host.
3292
3602
 * proxy will be freed before this function returns.
3320
3630
    proxypasswd[0] = 0;
3321
3631
 
3322
3632
    if(1 <= sscanf(proxyptr,
3323
 
                   "%" MAX_CURL_USER_LENGTH_TXT"[^:]:"
 
3633
                   "%" MAX_CURL_USER_LENGTH_TXT"[^:@]:"
3324
3634
                   "%" MAX_CURL_PASSWORD_LENGTH_TXT "[^@]",
3325
3635
                   proxyuser, proxypasswd)) {
3326
3636
      CURLcode res = CURLE_OK;
3367
3677
  /* detect and extract RFC2732-style IPv6-addresses */
3368
3678
  if(*proxyptr == '[') {
3369
3679
    char *ptr = ++proxyptr; /* advance beyond the initial bracket */
3370
 
    while(*ptr && (ISXDIGIT(*ptr) || (*ptr == ':')))
 
3680
    while(*ptr && (ISXDIGIT(*ptr) || (*ptr == ':') || (*ptr == '%') || (*ptr == '.')))
3371
3681
      ptr++;
3372
3682
    if(*ptr == ']') {
3373
3683
      /* yeps, it ended nicely with a bracket as well */
3374
 
      *ptr = 0;
3375
 
      portptr = ptr+1;
3376
 
    }
 
3684
      *ptr++ = 0;
 
3685
    } else
 
3686
      infof(data, "Invalid IPv6 address format\n");
 
3687
    portptr = ptr;
3377
3688
    /* Note that if this didn't end with a bracket, we still advanced the
3378
3689
     * proxyptr first, but I can't see anything wrong with that as no host
3379
3690
     * name nor a numeric can legally start with a bracket.
3412
3723
  return CURLE_OK;
3413
3724
}
3414
3725
 
3415
 
/* Extract the user and password from the authentication string */
 
3726
/*
 
3727
 * Extract the user and password from the authentication string
 
3728
 */
3416
3729
static CURLcode parse_proxy_auth(struct SessionHandle *data,
3417
3730
                                 struct connectdata *conn)
3418
3731
{
3419
3732
  char proxyuser[MAX_CURL_USER_LENGTH]="";
3420
3733
  char proxypasswd[MAX_CURL_PASSWORD_LENGTH]="";
3421
3734
 
3422
 
  sscanf(data->set.str[STRING_PROXYUSERPWD],
3423
 
         "%" MAX_CURL_USER_LENGTH_TXT "[^:]:"
3424
 
         "%" MAX_CURL_PASSWORD_LENGTH_TXT "[^\n]",
3425
 
         proxyuser, proxypasswd);
 
3735
  if(data->set.str[STRING_PROXYUSERNAME] != NULL) {
 
3736
    strncpy(proxyuser, data->set.str[STRING_PROXYUSERNAME],
 
3737
            MAX_CURL_USER_LENGTH);
 
3738
    proxyuser[MAX_CURL_USER_LENGTH-1] = '\0';   /*To be on safe side*/
 
3739
  }
 
3740
  if(data->set.str[STRING_PROXYPASSWORD] != NULL) {
 
3741
    strncpy(proxypasswd, data->set.str[STRING_PROXYPASSWORD],
 
3742
            MAX_CURL_PASSWORD_LENGTH);
 
3743
    proxypasswd[MAX_CURL_PASSWORD_LENGTH-1] = '\0'; /*To be on safe side*/
 
3744
  }
3426
3745
 
3427
3746
  conn->proxyuser = curl_easy_unescape(data, proxyuser, 0, NULL);
3428
3747
  if(!conn->proxyuser)
3434
3753
 
3435
3754
  return CURLE_OK;
3436
3755
}
 
3756
#endif /* CURL_DISABLE_PROXY */
 
3757
 
 
3758
/*
 
3759
 *
 
3760
 * Parse a user name and password in the URL and strip it out of the host name
 
3761
 *
 
3762
 * Inputs: data->set.use_netrc (CURLOPT_NETRC)
 
3763
 *         conn->host.name
 
3764
 *
 
3765
 * Outputs: (almost :- all currently undefined)
 
3766
 *          conn->bits.user_passwd  - non-zero if non-default passwords exist
 
3767
 *          user                    - non-zero length if defined
 
3768
 *          passwd                  -   ditto
 
3769
 *          conn->host.name         - remove user name and password
 
3770
 */
 
3771
static CURLcode parse_url_userpass(struct SessionHandle *data,
 
3772
                                   struct connectdata *conn,
 
3773
                                   char *user, char *passwd)
 
3774
{
 
3775
  /* At this point, we're hoping all the other special cases have
 
3776
   * been taken care of, so conn->host.name is at most
 
3777
   *    [user[:password]]@]hostname
 
3778
   *
 
3779
   * We need somewhere to put the embedded details, so do that first.
 
3780
   */
 
3781
 
 
3782
  user[0] =0;   /* to make everything well-defined */
 
3783
  passwd[0]=0;
 
3784
 
 
3785
  if(conn->protocol & (PROT_FTP|PROT_HTTP|PROT_SCP|PROT_SFTP)) {
 
3786
    /* This is a FTP, HTTP, SCP or SFTP URL, we will now try to extract the
 
3787
     * possible user+password pair in a string like:
 
3788
     * ftp://user:password@ftp.my.site:8021/README */
 
3789
    char *ptr=strchr(conn->host.name, '@');
 
3790
    char *userpass = conn->host.name;
 
3791
    if(ptr != NULL) {
 
3792
      /* there's a user+password given here, to the left of the @ */
 
3793
 
 
3794
      conn->host.name = ++ptr;
 
3795
 
 
3796
      /* So the hostname is sane.  Only bother interpreting the
 
3797
       * results if we could care.  It could still be wasted
 
3798
       * work because it might be overtaken by the programmatically
 
3799
       * set user/passwd, but doing that first adds more cases here :-(
 
3800
       */
 
3801
 
 
3802
      if(data->set.use_netrc != CURL_NETRC_REQUIRED) {
 
3803
        /* We could use the one in the URL */
 
3804
 
 
3805
        conn->bits.user_passwd = 1; /* enable user+password */
 
3806
 
 
3807
        if(*userpass != ':') {
 
3808
          /* the name is given, get user+password */
 
3809
          sscanf(userpass, "%" MAX_CURL_USER_LENGTH_TXT "[^:@]:"
 
3810
                 "%" MAX_CURL_PASSWORD_LENGTH_TXT "[^@]",
 
3811
                 user, passwd);
 
3812
        }
 
3813
        else
 
3814
          /* no name given, get the password only */
 
3815
          sscanf(userpass, ":%" MAX_CURL_PASSWORD_LENGTH_TXT "[^@]", passwd);
 
3816
 
 
3817
        if(user[0]) {
 
3818
          char *newname=curl_easy_unescape(data, user, 0, NULL);
 
3819
          if(!newname)
 
3820
            return CURLE_OUT_OF_MEMORY;
 
3821
          if(strlen(newname) < MAX_CURL_USER_LENGTH)
 
3822
            strcpy(user, newname);
 
3823
 
 
3824
          /* if the new name is longer than accepted, then just use
 
3825
             the unconverted name, it'll be wrong but what the heck */
 
3826
          free(newname);
 
3827
        }
 
3828
        if(passwd[0]) {
 
3829
          /* we have a password found in the URL, decode it! */
 
3830
          char *newpasswd=curl_easy_unescape(data, passwd, 0, NULL);
 
3831
          if(!newpasswd)
 
3832
            return CURLE_OUT_OF_MEMORY;
 
3833
          if(strlen(newpasswd) < MAX_CURL_PASSWORD_LENGTH)
 
3834
            strcpy(passwd, newpasswd);
 
3835
 
 
3836
          free(newpasswd);
 
3837
        }
 
3838
      }
 
3839
    }
 
3840
  }
 
3841
  return CURLE_OK;
 
3842
}
 
3843
 
 
3844
/*************************************************************
 
3845
 * Figure out the remote port number and fix it in the URL
 
3846
 *
 
3847
 * No matter if we use a proxy or not, we have to figure out the remote
 
3848
 * port number of various reasons.
 
3849
 *
 
3850
 * To be able to detect port number flawlessly, we must not confuse them
 
3851
 * IPv6-specified addresses in the [0::1] style. (RFC2732)
 
3852
 *
 
3853
 * The conn->host.name is currently [user:passwd@]host[:port] where host
 
3854
 * could be a hostname, IPv4 address or IPv6 address.
 
3855
 *
 
3856
 * The port number embedded in the URL is replaced, if necessary.
 
3857
 *************************************************************/
 
3858
static CURLcode parse_remote_port(struct SessionHandle *data,
 
3859
                                  struct connectdata *conn)
 
3860
{
 
3861
  char *portptr;
 
3862
  char endbracket;
 
3863
 
 
3864
  /* Note that at this point, the IPv6 address cannot contain any scope
 
3865
     suffix as that has already been removed in the ParseURLAndFillConnection()
 
3866
     function */
 
3867
  if((1 == sscanf(conn->host.name, "[%*45[0123456789abcdefABCDEF:.]%c",
 
3868
                  &endbracket)) &&
 
3869
     (']' == endbracket)) {
 
3870
    /* this is a RFC2732-style specified IP-address */
 
3871
    conn->bits.ipv6_ip = TRUE;
 
3872
 
 
3873
    conn->host.name++; /* skip over the starting bracket */
 
3874
    portptr = strchr(conn->host.name, ']');
 
3875
    *portptr++ = 0; /* zero terminate, killing the bracket */
 
3876
    if(':' != *portptr)
 
3877
      portptr = NULL; /* no port number available */
 
3878
  }
 
3879
  else
 
3880
    portptr = strrchr(conn->host.name, ':');
 
3881
 
 
3882
  if(data->set.use_port && data->state.allow_port) {
 
3883
    /* if set, we use this and ignore the port possibly given in the URL */
 
3884
    conn->remote_port = (unsigned short)data->set.use_port;
 
3885
    if(portptr)
 
3886
      *portptr = '\0'; /* cut off the name there anyway - if there was a port
 
3887
                      number - since the port number is to be ignored! */
 
3888
    if(conn->bits.httpproxy) {
 
3889
      /* we need to create new URL with the new port number */
 
3890
      char *url;
 
3891
      bool isftp = (bool)(Curl_raw_equal("ftp", conn->protostr) ||
 
3892
                          Curl_raw_equal("ftps", conn->protostr));
 
3893
 
 
3894
      /*
 
3895
       * This synthesized URL isn't always right--suffixes like ;type=A
 
3896
       * are stripped off. It would be better to work directly from the
 
3897
       * original URL and simply replace the port part of it.
 
3898
       */
 
3899
      url = aprintf("%s://%s%s%s:%d%s%s", conn->protostr,
 
3900
             conn->bits.ipv6_ip?"[":"", conn->host.name,
 
3901
             conn->bits.ipv6_ip?"]":"", conn->remote_port,
 
3902
             isftp?"/":"", data->state.path);
 
3903
      if(!url)
 
3904
        return CURLE_OUT_OF_MEMORY;
 
3905
 
 
3906
      if(data->change.url_alloc)
 
3907
        free(data->change.url);
 
3908
 
 
3909
      data->change.url = url;
 
3910
      data->change.url_alloc = TRUE;
 
3911
    }
 
3912
  }
 
3913
  else if(portptr) {
 
3914
    /* no CURLOPT_PORT given, extract the one from the URL */
 
3915
 
 
3916
    char *rest;
 
3917
    unsigned long port;
 
3918
 
 
3919
    port=strtoul(portptr+1, &rest, 10);  /* Port number must be decimal */
 
3920
 
 
3921
    if(rest != (portptr+1) && *rest == '\0') {
 
3922
      /* The colon really did have only digits after it,
 
3923
       * so it is either a port number or a mistake */
 
3924
 
 
3925
      if(port > 0xffff) {   /* Single unix standard says port numbers are
 
3926
                              * 16 bits long */
 
3927
        failf(data, "Port number too large: %lu", port);
 
3928
        return CURLE_URL_MALFORMAT;
 
3929
      }
 
3930
 
 
3931
      *portptr = '\0'; /* cut off the name there */
 
3932
      conn->remote_port = (unsigned short)port;
 
3933
    }
 
3934
  }
 
3935
  return CURLE_OK;
 
3936
}
 
3937
 
 
3938
/*
 
3939
 * Override a user name and password from the URL with that in the
 
3940
 * CURLOPT_USERPWD option or a .netrc file, if applicable.
 
3941
 */
 
3942
static void override_userpass(struct SessionHandle *data,
 
3943
                              struct connectdata *conn,
 
3944
                              char *user, char *passwd)
 
3945
{
 
3946
  if(data->set.str[STRING_USERNAME] != NULL) {
 
3947
    strncpy(user, data->set.str[STRING_USERNAME], MAX_CURL_USER_LENGTH);
 
3948
    user[MAX_CURL_USER_LENGTH-1] = '\0';   /*To be on safe side*/
 
3949
  }
 
3950
  if(data->set.str[STRING_PASSWORD] != NULL) {
 
3951
    strncpy(passwd, data->set.str[STRING_PASSWORD], MAX_CURL_PASSWORD_LENGTH);
 
3952
    passwd[MAX_CURL_PASSWORD_LENGTH-1] = '\0'; /*To be on safe side*/
 
3953
  }
 
3954
 
 
3955
  conn->bits.netrc = FALSE;
 
3956
  if(data->set.use_netrc != CURL_NETRC_IGNORED) {
 
3957
    if(Curl_parsenetrc(conn->host.name,
 
3958
                       user, passwd,
 
3959
                       data->set.str[STRING_NETRC_FILE])) {
 
3960
      infof(data, "Couldn't find host %s in the "
 
3961
            DOT_CHAR "netrc file; using defaults\n",
 
3962
            conn->host.name);
 
3963
    }
 
3964
    else {
 
3965
      /* set bits.netrc TRUE to remember that we got the name from a .netrc
 
3966
         file, so that it is safe to use even if we followed a Location: to a
 
3967
         different host or similar. */
 
3968
      conn->bits.netrc = TRUE;
 
3969
 
 
3970
      conn->bits.user_passwd = 1; /* enable user+password */
 
3971
    }
 
3972
  }
 
3973
}
 
3974
 
 
3975
/*
 
3976
 * Set password so it's available in the connection.
 
3977
 */
 
3978
static CURLcode set_userpass(struct connectdata *conn,
 
3979
                             const char *user, const char *passwd)
 
3980
{
 
3981
  /* If our protocol needs a password and we have none, use the defaults */
 
3982
  if( (conn->protocol & PROT_FTP) &&
 
3983
       !conn->bits.user_passwd) {
 
3984
 
 
3985
    conn->user = strdup(CURL_DEFAULT_USER);
 
3986
    conn->passwd = strdup(CURL_DEFAULT_PASSWORD);
 
3987
    /* This is the default password, so DON'T set conn->bits.user_passwd */
 
3988
  }
 
3989
  else {
 
3990
    /* store user + password, zero-length if not set */
 
3991
    conn->user = strdup(user);
 
3992
    conn->passwd = strdup(passwd);
 
3993
  }
 
3994
  if(!conn->user || !conn->passwd)
 
3995
    return CURLE_OUT_OF_MEMORY;
 
3996
 
 
3997
  return CURLE_OK;
 
3998
}
 
3999
 
 
4000
/*************************************************************
 
4001
 * Resolve the address of the server or proxy
 
4002
 *************************************************************/
 
4003
static CURLcode resolve_server(struct SessionHandle *data,
 
4004
                               struct connectdata *conn,
 
4005
                               struct Curl_dns_entry **addr,
 
4006
                               bool *async)
 
4007
{
 
4008
  CURLcode result=CURLE_OK;
 
4009
  long shortest = 0; /* default to no timeout */
 
4010
 
 
4011
  /*************************************************************
 
4012
   * Set timeout if that is being used
 
4013
   *************************************************************/
 
4014
  if(data->set.timeout || data->set.connecttimeout) {
 
4015
 
 
4016
    /* We set the timeout on the name resolving phase first, separately from
 
4017
     * the download/upload part to allow a maximum time on everything. This is
 
4018
     * a signal-based timeout, why it won't work and shouldn't be used in
 
4019
     * multi-threaded environments. */
 
4020
 
 
4021
    shortest = data->set.timeout; /* default to this timeout value */
 
4022
    if(shortest && data->set.connecttimeout &&
 
4023
       (data->set.connecttimeout < shortest))
 
4024
      /* if both are set, pick the shortest */
 
4025
      shortest = data->set.connecttimeout;
 
4026
    else if(!shortest)
 
4027
      /* if timeout is not set, use the connect timeout */
 
4028
      shortest = data->set.connecttimeout;
 
4029
  /* We can expect the conn->created time to be "now", as that was just
 
4030
     recently set in the beginning of this function and nothing slow
 
4031
     has been done since then until now. */
 
4032
  }
 
4033
 
 
4034
  /*************************************************************
 
4035
   * Resolve the name of the server or proxy
 
4036
   *************************************************************/
 
4037
  if(conn->bits.reuse) {
 
4038
    /* re-used connection, no resolving is necessary */
 
4039
    *addr = NULL;
 
4040
    /* we'll need to clear conn->dns_entry later in Curl_disconnect() */
 
4041
 
 
4042
    if(conn->bits.proxy)
 
4043
      fix_hostname(data, conn, &conn->host);
 
4044
  }
 
4045
  else {
 
4046
    /* this is a fresh connect */
 
4047
    int rc;
 
4048
    struct Curl_dns_entry *hostaddr;
 
4049
 
 
4050
    /* set a pointer to the hostname we display */
 
4051
    fix_hostname(data, conn, &conn->host);
 
4052
 
 
4053
    if(!conn->proxy.name || !*conn->proxy.name) {
 
4054
      /* If not connecting via a proxy, extract the port from the URL, if it is
 
4055
       * there, thus overriding any defaults that might have been set above. */
 
4056
      conn->port =  conn->remote_port; /* it is the same port */
 
4057
 
 
4058
      /* Resolve target host right on */
 
4059
      rc = Curl_resolv_timeout(conn, conn->host.name, (int)conn->port,
 
4060
                               &hostaddr, shortest);
 
4061
      if(rc == CURLRESOLV_PENDING)
 
4062
        *async = TRUE;
 
4063
 
 
4064
      else if (rc == CURLRESOLV_TIMEDOUT)
 
4065
        result = CURLE_OPERATION_TIMEDOUT;
 
4066
 
 
4067
      else if(!hostaddr) {
 
4068
        failf(data, "Couldn't resolve host '%s'", conn->host.dispname);
 
4069
        result =  CURLE_COULDNT_RESOLVE_HOST;
 
4070
        /* don't return yet, we need to clean up the timeout first */
 
4071
      }
 
4072
    }
 
4073
    else {
 
4074
      /* This is a proxy that hasn't been resolved yet. */
 
4075
 
 
4076
      /* IDN-fix the proxy name */
 
4077
      fix_hostname(data, conn, &conn->proxy);
 
4078
 
 
4079
      /* resolve proxy */
 
4080
      rc = Curl_resolv_timeout(conn, conn->proxy.name, (int)conn->port,
 
4081
                               &hostaddr, shortest);
 
4082
 
 
4083
      if(rc == CURLRESOLV_PENDING)
 
4084
        *async = TRUE;
 
4085
 
 
4086
      else if (rc == CURLRESOLV_TIMEDOUT)
 
4087
        result = CURLE_OPERATION_TIMEDOUT;
 
4088
 
 
4089
      else if(!hostaddr) {
 
4090
        failf(data, "Couldn't resolve proxy '%s'", conn->proxy.dispname);
 
4091
        result = CURLE_COULDNT_RESOLVE_PROXY;
 
4092
        /* don't return yet, we need to clean up the timeout first */
 
4093
      }
 
4094
    }
 
4095
    *addr = hostaddr;
 
4096
  }
 
4097
 
 
4098
  return result;
 
4099
}
 
4100
 
 
4101
/*
 
4102
 * Cleanup the connection just allocated before we can move along and use the
 
4103
 * previously existing one.  All relevant data is copied over and old_conn is
 
4104
 * ready for freeing once this function returns.
 
4105
 */
 
4106
static void reuse_conn(struct connectdata *old_conn,
 
4107
                       struct connectdata *conn)
 
4108
{
 
4109
  if(old_conn->proxy.rawalloc)
 
4110
    free(old_conn->proxy.rawalloc);
 
4111
 
 
4112
  /* free the SSL config struct from this connection struct as this was
 
4113
     allocated in vain and is targeted for destruction */
 
4114
  Curl_free_ssl_config(&old_conn->ssl_config);
 
4115
 
 
4116
  conn->data = old_conn->data;
 
4117
 
 
4118
  /* get the user+password information from the old_conn struct since it may
 
4119
   * be new for this request even when we re-use an existing connection */
 
4120
  conn->bits.user_passwd = old_conn->bits.user_passwd;
 
4121
  if(conn->bits.user_passwd) {
 
4122
    /* use the new user name and password though */
 
4123
    Curl_safefree(conn->user);
 
4124
    Curl_safefree(conn->passwd);
 
4125
    conn->user = old_conn->user;
 
4126
    conn->passwd = old_conn->passwd;
 
4127
    old_conn->user = NULL;
 
4128
    old_conn->passwd = NULL;
 
4129
  }
 
4130
 
 
4131
  conn->bits.proxy_user_passwd = old_conn->bits.proxy_user_passwd;
 
4132
  if(conn->bits.proxy_user_passwd) {
 
4133
    /* use the new proxy user name and proxy password though */
 
4134
    Curl_safefree(conn->proxyuser);
 
4135
    Curl_safefree(conn->proxypasswd);
 
4136
    conn->proxyuser = old_conn->proxyuser;
 
4137
    conn->proxypasswd = old_conn->proxypasswd;
 
4138
    old_conn->proxyuser = NULL;
 
4139
    old_conn->proxypasswd = NULL;
 
4140
  }
 
4141
 
 
4142
  /* host can change, when doing keepalive with a proxy ! */
 
4143
  if(conn->bits.proxy) {
 
4144
    free(conn->host.rawalloc);
 
4145
    conn->host=old_conn->host;
 
4146
  }
 
4147
  else
 
4148
    free(old_conn->host.rawalloc); /* free the newly allocated name buffer */
 
4149
 
 
4150
  /* re-use init */
 
4151
  conn->bits.reuse = TRUE; /* yes, we're re-using here */
 
4152
 
 
4153
  Curl_safefree(old_conn->user);
 
4154
  Curl_safefree(old_conn->passwd);
 
4155
  Curl_safefree(old_conn->proxyuser);
 
4156
  Curl_safefree(old_conn->proxypasswd);
 
4157
  Curl_llist_destroy(old_conn->send_pipe, NULL);
 
4158
  Curl_llist_destroy(old_conn->recv_pipe, NULL);
 
4159
  Curl_llist_destroy(old_conn->pend_pipe, NULL);
 
4160
  Curl_safefree(old_conn->master_buffer);
 
4161
}
3437
4162
 
3438
4163
/**
3439
 
 * CreateConnection() sets up a new connectdata struct, or re-uses an already
 
4164
 * create_conn() sets up a new connectdata struct, or re-uses an already
3440
4165
 * existing one, and resolves host name.
3441
4166
 *
3442
4167
 * if this function returns CURLE_OK and *async is set to TRUE, the resolve
3449
4174
 *        connection is re-used it will be NULL.
3450
4175
 * @param async is set TRUE/FALSE depending on the nature of this lookup
3451
4176
 * @return CURLcode
3452
 
 * @see SetupConnection()
 
4177
 * @see setup_conn()
3453
4178
 *
3454
4179
 * *NOTE* this function assigns the conn->data pointer!
3455
4180
 */
3456
4181
 
3457
 
static CURLcode CreateConnection(struct SessionHandle *data,
3458
 
                                 struct connectdata **in_connect,
3459
 
                                 struct Curl_dns_entry **addr,
3460
 
                                 bool *async)
 
4182
static CURLcode create_conn(struct SessionHandle *data,
 
4183
                            struct connectdata **in_connect,
 
4184
                            struct Curl_dns_entry **addr,
 
4185
                            bool *async)
3461
4186
{
3462
 
 
3463
 
  char *tmp;
3464
4187
  CURLcode result=CURLE_OK;
3465
4188
  struct connectdata *conn;
3466
4189
  struct connectdata *conn_temp = NULL;
3467
4190
  size_t urllen;
3468
 
  struct Curl_dns_entry *hostaddr;
3469
 
#if defined(HAVE_ALARM) && !defined(USE_ARES)
3470
 
  unsigned int prev_alarm=0;
3471
 
#endif
3472
 
  char endbracket;
3473
4191
  char user[MAX_CURL_USER_LENGTH];
3474
4192
  char passwd[MAX_CURL_PASSWORD_LENGTH];
3475
 
  int rc;
3476
4193
  bool reuse;
3477
4194
  char *proxy = NULL;
3478
4195
 
3479
 
#ifndef USE_ARES
3480
 
#ifdef SIGALRM
3481
 
#ifdef HAVE_SIGACTION
3482
 
  struct sigaction keep_sigact;   /* store the old struct here */
3483
 
  bool keep_copysig=FALSE;        /* did copy it? */
3484
 
#else
3485
 
#ifdef HAVE_SIGNAL
3486
 
  void (*keep_sigact)(int);       /* store the old handler here */
3487
 
#endif /* HAVE_SIGNAL */
3488
 
#endif /* HAVE_SIGACTION */
3489
 
#endif /* SIGALRM */
3490
 
#endif /* USE_ARES */
3491
 
 
3492
4196
  *addr = NULL; /* nothing yet */
3493
4197
  *async = FALSE;
3494
4198
 
3504
4208
     to not have to modify everything at once, we allocate a temporary
3505
4209
     connection data struct and fill in for comparison purposes. */
3506
4210
 
3507
 
  conn = (struct connectdata *)calloc(1, sizeof(struct connectdata));
3508
 
  if(!conn) {
3509
 
    *in_connect = NULL; /* clear the pointer */
3510
 
    return CURLE_OUT_OF_MEMORY;
3511
 
  }
 
4211
  conn = allocate_conn();
 
4212
 
3512
4213
  /* We must set the return variable as soon as possible, so that our
3513
4214
     parent can cleanup any possible allocs we may have done before
3514
4215
     any failure */
3515
4216
  *in_connect = conn;
3516
4217
 
3517
 
  conn->handler = &Curl_handler_dummy;  /* Be sure we have a handler defined
3518
 
                                           already from start to avoid NULL
3519
 
                                           situations and checks */
3520
 
 
3521
 
  /* and we setup a few fields in case we end up actually using this struct */
 
4218
  if(!conn)
 
4219
    return CURLE_OUT_OF_MEMORY;
3522
4220
 
3523
4221
  conn->data = data; /* Setup the association between this connection
3524
4222
                        and the SessionHandle */
3525
4223
 
3526
 
  conn->sock[FIRSTSOCKET] = CURL_SOCKET_BAD;     /* no file descriptor */
3527
 
  conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
3528
 
  conn->connectindex = -1;    /* no index */
3529
 
 
3530
4224
  conn->proxytype = data->set.proxytype; /* type */
 
4225
 
 
4226
#ifdef CURL_DISABLE_PROXY
 
4227
 
 
4228
  conn->bits.proxy = FALSE;
 
4229
  conn->bits.httpproxy = FALSE;
 
4230
  conn->bits.proxy_user_passwd = FALSE;
 
4231
  conn->bits.tunnel_proxy = FALSE;
 
4232
 
 
4233
#else /* CURL_DISABLE_PROXY */
 
4234
 
3531
4235
  conn->bits.proxy = (bool)(data->set.str[STRING_PROXY] &&
3532
4236
                            *data->set.str[STRING_PROXY]);
3533
 
  conn->bits.httpproxy = (bool)(conn->bits.proxy
3534
 
                                && (conn->proxytype == CURLPROXY_HTTP));
3535
 
 
3536
 
 
3537
 
  /* Default protocol-independent behavior doesn't support persistent
3538
 
     connections, so we set this to force-close. Protocols that support
3539
 
     this need to set this to FALSE in their "curl_do" functions. */
3540
 
  conn->bits.close = TRUE;
3541
 
 
3542
 
  conn->readchannel_inuse = FALSE;
3543
 
  conn->writechannel_inuse = FALSE;
3544
 
 
3545
 
  conn->read_pos = 0;
3546
 
  conn->buf_len = 0;
3547
 
 
3548
 
  /* Store creation time to help future close decision making */
3549
 
  conn->created = Curl_tvnow();
3550
 
 
3551
 
  conn->bits.user_passwd = (bool)(NULL != data->set.str[STRING_USERPWD]);
3552
 
  conn->bits.proxy_user_passwd = (bool)(NULL != data->set.str[STRING_PROXYUSERPWD]);
 
4237
  conn->bits.httpproxy = (bool)(conn->bits.proxy &&
 
4238
                                (conn->proxytype == CURLPROXY_HTTP ||
 
4239
                                 conn->proxytype == CURLPROXY_HTTP_1_0));
 
4240
  conn->bits.proxy_user_passwd =
 
4241
    (bool)(NULL != data->set.str[STRING_PROXYUSERNAME]);
3553
4242
  conn->bits.tunnel_proxy = data->set.tunnel_thru_httpproxy;
 
4243
 
 
4244
#endif /* CURL_DISABLE_PROXY */
 
4245
 
 
4246
  conn->bits.user_passwd = (bool)(NULL != data->set.str[STRING_USERNAME]);
3554
4247
  conn->bits.ftp_use_epsv = data->set.ftp_use_epsv;
3555
4248
  conn->bits.ftp_use_eprt = data->set.ftp_use_eprt;
3556
4249
 
3582
4275
  if(urllen < LEAST_PATH_ALLOC)
3583
4276
    urllen=LEAST_PATH_ALLOC;
3584
4277
 
3585
 
  /* Free the old buffer */
3586
 
  Curl_safefree(data->state.pathbuffer);
3587
 
 
3588
4278
  /*
3589
4279
   * We malloc() the buffers below urllen+2 to make room for to possibilities:
3590
4280
   * 1 - an extra terminating zero
3591
4281
   * 2 - an extra slash (in case a syntax like "www.host.com?moo" is used)
3592
4282
   */
3593
4283
 
3594
 
  data->state.pathbuffer=(char *)malloc(urllen+2);
 
4284
  Curl_safefree(data->state.pathbuffer);
 
4285
  data->state.pathbuffer = malloc(urllen+2);
3595
4286
  if(NULL == data->state.pathbuffer)
3596
4287
    return CURLE_OUT_OF_MEMORY; /* really bad error */
3597
4288
  data->state.path = data->state.pathbuffer;
3598
4289
 
3599
 
  conn->host.rawalloc=(char *)malloc(urllen+2);
 
4290
  conn->host.rawalloc = malloc(urllen+2);
3600
4291
  if(NULL == conn->host.rawalloc)
3601
4292
    return CURLE_OUT_OF_MEMORY;
3602
4293
 
3608
4299
      return result;
3609
4300
  }
3610
4301
 
 
4302
#ifndef CURL_DISABLE_PROXY
3611
4303
  /*************************************************************
3612
 
   * Take care of proxy authentication stuff
 
4304
   * Extract the user and password from the authentication string
3613
4305
   *************************************************************/
3614
4306
  if(conn->bits.proxy_user_passwd) {
3615
4307
    result = parse_proxy_auth(data, conn);
3631
4323
 
3632
4324
  if(!proxy)
3633
4325
    proxy = detect_proxy(conn);
 
4326
  else if(data->set.str[STRING_NOPROXY]) {
 
4327
    if(check_noproxy(conn->host.name, data->set.str[STRING_NOPROXY])) {
 
4328
      free(proxy);  /* proxy is in exception list */
 
4329
      proxy = NULL;
 
4330
    }
 
4331
  }
3634
4332
  if(proxy && !*proxy) {
3635
4333
    free(proxy);  /* Don't bother with an empty proxy string */
3636
4334
    proxy = NULL;
3637
4335
  }
3638
4336
  /* proxy must be freed later unless NULL */
 
4337
  if(proxy && *proxy) {
 
4338
    long bits = conn->protocol & (PROT_HTTPS|PROT_SSL|PROT_MISSING);
 
4339
 
 
4340
    if((conn->proxytype == CURLPROXY_HTTP) ||
 
4341
       (conn->proxytype == CURLPROXY_HTTP_1_0)) {
 
4342
      /* force this connection's protocol to become HTTP */
 
4343
      conn->protocol = PROT_HTTP | bits;
 
4344
      conn->bits.httpproxy = TRUE;
 
4345
    }
 
4346
    conn->bits.proxy = TRUE;
 
4347
  }
 
4348
  else {
 
4349
      /* we aren't using the proxy after all... */
 
4350
      conn->bits.proxy = FALSE;
 
4351
      conn->bits.httpproxy = FALSE;
 
4352
      conn->bits.proxy_user_passwd = FALSE;
 
4353
      conn->bits.tunnel_proxy = FALSE;
 
4354
  }
 
4355
#endif /* CURL_DISABLE_PROXY */
3639
4356
 
3640
4357
  /*************************************************************
3641
4358
   * No protocol part in URL was used, add it!
3668
4385
  }
3669
4386
 
3670
4387
 
 
4388
#ifndef CURL_DISABLE_PROXY
3671
4389
  /***********************************************************************
3672
4390
   * If this is supposed to use a proxy, we need to figure out the proxy
3673
4391
   * host name, so that we can re-use an existing connection
3680
4398
    if(result != CURLE_OK)
3681
4399
      return result;
3682
4400
  }
3683
 
 
 
4401
#endif /* CURL_DISABLE_PROXY */
3684
4402
 
3685
4403
  /***********************************************************************
3686
4404
   * file: is a special case in that it doesn't need a network connection
3700
4418
 
3701
4419
      ConnectionStore(data, conn);
3702
4420
 
 
4421
      /*
 
4422
       * Setup whatever necessary for a resumed transfer
 
4423
       */
3703
4424
      result = setup_range(data);
3704
4425
      if(result) {
3705
4426
        DEBUGASSERT(conn->handler->done);
3725
4446
    conn->bits.tunnel_proxy = TRUE;
3726
4447
 
3727
4448
  /*************************************************************
3728
 
   * Take care of user and password authentication stuff
3729
 
   *************************************************************/
3730
 
 
3731
 
  /*
3732
 
   * Inputs: data->set.userpwd   (CURLOPT_USERPWD)
3733
 
   *         data->set.fpasswd   (CURLOPT_PASSWDFUNCTION)
3734
 
   *         data->set.use_netrc (CURLOPT_NETRC)
3735
 
   *         conn->host.name
3736
 
   *         netrc file
3737
 
   *         hard-coded defaults
3738
 
   *
3739
 
   * Outputs: (almost :- all currently undefined)
3740
 
   *          conn->bits.user_passwd  - non-zero if non-default passwords exist
3741
 
   *          conn->user              - non-zero length if defined
3742
 
   *          conn->passwd            -   ditto
3743
 
   *          conn->host.name          - remove user name and password
3744
 
   */
3745
 
 
3746
 
  /* At this point, we're hoping all the other special cases have
3747
 
   * been taken care of, so conn->host.name is at most
3748
 
   *    [user[:password]]@]hostname
3749
 
   *
3750
 
   * We need somewhere to put the embedded details, so do that first.
3751
 
   */
3752
 
 
3753
 
  user[0] =0;   /* to make everything well-defined */
3754
 
  passwd[0]=0;
3755
 
 
3756
 
  if(conn->protocol & (PROT_FTP|PROT_HTTP|PROT_SCP|PROT_SFTP)) {
3757
 
    /* This is a FTP, HTTP, SCP or SFTP URL, we will now try to extract the
3758
 
     * possible user+password pair in a string like:
3759
 
     * ftp://user:password@ftp.my.site:8021/README */
3760
 
    char *ptr=strchr(conn->host.name, '@');
3761
 
    char *userpass = conn->host.name;
3762
 
    if(ptr != NULL) {
3763
 
      /* there's a user+password given here, to the left of the @ */
3764
 
 
3765
 
      conn->host.name = ++ptr;
3766
 
 
3767
 
      /* So the hostname is sane.  Only bother interpreting the
3768
 
       * results if we could care.  It could still be wasted
3769
 
       * work because it might be overtaken by the programmatically
3770
 
       * set user/passwd, but doing that first adds more cases here :-(
3771
 
       */
3772
 
 
3773
 
      if(data->set.use_netrc != CURL_NETRC_REQUIRED) {
3774
 
        /* We could use the one in the URL */
3775
 
 
3776
 
        conn->bits.user_passwd = 1; /* enable user+password */
3777
 
 
3778
 
        if(*userpass != ':') {
3779
 
          /* the name is given, get user+password */
3780
 
          sscanf(userpass, "%" MAX_CURL_USER_LENGTH_TXT "[^:@]:"
3781
 
                 "%" MAX_CURL_PASSWORD_LENGTH_TXT "[^@]",
3782
 
                 user, passwd);
3783
 
        }
3784
 
        else
3785
 
          /* no name given, get the password only */
3786
 
          sscanf(userpass, ":%" MAX_CURL_PASSWORD_LENGTH_TXT "[^@]", passwd);
3787
 
 
3788
 
        if(user[0]) {
3789
 
          char *newname=curl_easy_unescape(data, user, 0, NULL);
3790
 
          if(!newname)
3791
 
            return CURLE_OUT_OF_MEMORY;
3792
 
          if(strlen(newname) < sizeof(user))
3793
 
            strcpy(user, newname);
3794
 
 
3795
 
          /* if the new name is longer than accepted, then just use
3796
 
             the unconverted name, it'll be wrong but what the heck */
3797
 
          free(newname);
3798
 
        }
3799
 
        if(passwd[0]) {
3800
 
          /* we have a password found in the URL, decode it! */
3801
 
          char *newpasswd=curl_easy_unescape(data, passwd, 0, NULL);
3802
 
          if(!newpasswd)
3803
 
            return CURLE_OUT_OF_MEMORY;
3804
 
          if(strlen(newpasswd) < sizeof(passwd))
3805
 
            strcpy(passwd, newpasswd);
3806
 
 
3807
 
          free(newpasswd);
3808
 
        }
3809
 
      }
3810
 
    }
3811
 
  }
3812
 
 
3813
 
  /*************************************************************
3814
 
   * Figure out the remote port number
3815
 
   *
3816
 
   * No matter if we use a proxy or not, we have to figure out the remote
3817
 
   * port number of various reasons.
3818
 
   *
3819
 
   * To be able to detect port number flawlessly, we must not confuse them
3820
 
   * IPv6-specified addresses in the [0::1] style. (RFC2732)
3821
 
   *
3822
 
   * The conn->host.name is currently [user:passwd@]host[:port] where host
3823
 
   * could be a hostname, IPv4 address or IPv6 address.
3824
 
   *************************************************************/
3825
 
  if((1 == sscanf(conn->host.name, "[%*39[0-9a-fA-F:.]%c", &endbracket)) &&
3826
 
     (']' == endbracket)) {
3827
 
    /* this is a RFC2732-style specified IP-address */
3828
 
    conn->bits.ipv6_ip = TRUE;
3829
 
 
3830
 
    conn->host.name++; /* pass the starting bracket */
3831
 
    tmp = strchr(conn->host.name, ']');
3832
 
    *tmp = 0; /* zero terminate */
3833
 
    tmp++; /* pass the ending bracket */
3834
 
    if(':' != *tmp)
3835
 
      tmp = NULL; /* no port number available */
3836
 
  }
3837
 
  else
3838
 
    tmp = strrchr(conn->host.name, ':');
3839
 
 
3840
 
  if(data->set.use_port && data->state.allow_port) {
3841
 
    /* if set, we use this and ignore the port possibly given in the URL */
3842
 
    conn->remote_port = (unsigned short)data->set.use_port;
3843
 
    if(tmp)
3844
 
      *tmp = '\0'; /* cut off the name there anyway - if there was a port
3845
 
                      number - since the port number is to be ignored! */
3846
 
    if(conn->bits.httpproxy) {
3847
 
      /* we need to create new URL with the new port number */
3848
 
      char *url;
3849
 
 
3850
 
      url = aprintf("%s://%s:%d%s", conn->protostr, conn->host.name,
3851
 
                    conn->remote_port, data->state.path);
3852
 
      if(!url)
3853
 
        return CURLE_OUT_OF_MEMORY;
3854
 
 
3855
 
      if(data->change.url_alloc)
3856
 
        free(data->change.url);
3857
 
 
3858
 
      data->change.url = url;
3859
 
      data->change.url_alloc = TRUE;
3860
 
    }
3861
 
  }
3862
 
  else if(tmp) {
3863
 
    /* no CURLOPT_PORT given, extract the one from the URL */
3864
 
 
3865
 
    char *rest;
3866
 
    unsigned long port;
3867
 
 
3868
 
    port=strtoul(tmp+1, &rest, 10);  /* Port number must be decimal */
3869
 
 
3870
 
    if(rest != (tmp+1) && *rest == '\0') {
3871
 
      /* The colon really did have only digits after it,
3872
 
       * so it is either a port number or a mistake */
3873
 
 
3874
 
      if(port > 0xffff) {   /* Single unix standard says port numbers are
3875
 
                              * 16 bits long */
3876
 
        failf(data, "Port number too large: %lu", port);
3877
 
        return CURLE_URL_MALFORMAT;
3878
 
      }
3879
 
 
3880
 
      *tmp = '\0'; /* cut off the name there */
3881
 
      conn->remote_port = (unsigned short)port;
3882
 
    }
3883
 
  }
3884
 
 
3885
 
  /* Programmatically set password:
3886
 
   *   - always applies, if available
3887
 
   *   - takes precedence over the values we just set above
3888
 
   * so scribble it over the top.
3889
 
   * User-supplied passwords are assumed not to need unescaping.
3890
 
   *
3891
 
   * user_password is set in "inherit initial knowledge' above,
3892
 
   * so it doesn't have to be set in this block
3893
 
   */
3894
 
  if(data->set.str[STRING_USERPWD] != NULL) {
3895
 
    /* the name is given, get user+password */
3896
 
    sscanf(data->set.str[STRING_USERPWD],
3897
 
           "%" MAX_CURL_USER_LENGTH_TXT "[^:]:"
3898
 
           "%" MAX_CURL_PASSWORD_LENGTH_TXT "[^\n]",
3899
 
           user, passwd);
3900
 
  }
3901
 
 
3902
 
  conn->bits.netrc = FALSE;
3903
 
  if(data->set.use_netrc != CURL_NETRC_IGNORED) {
3904
 
    if(Curl_parsenetrc(conn->host.name,
3905
 
                       user, passwd,
3906
 
                       data->set.str[STRING_NETRC_FILE])) {
3907
 
      infof(data, "Couldn't find host %s in the " DOT_CHAR
3908
 
            "netrc file, using defaults\n",
3909
 
            conn->host.name);
3910
 
    }
3911
 
    else {
3912
 
      /* set bits.netrc TRUE to remember that we got the name from a .netrc
3913
 
         file, so that it is safe to use even if we followed a Location: to a
3914
 
         different host or similar. */
3915
 
      conn->bits.netrc = TRUE;
3916
 
 
3917
 
      conn->bits.user_passwd = 1; /* enable user+password */
3918
 
    }
3919
 
  }
3920
 
 
3921
 
  /* If our protocol needs a password and we have none, use the defaults */
3922
 
  if( (conn->protocol & PROT_FTP) &&
3923
 
       !conn->bits.user_passwd) {
3924
 
 
3925
 
    conn->user = strdup(CURL_DEFAULT_USER);
3926
 
    conn->passwd = strdup(CURL_DEFAULT_PASSWORD);
3927
 
    /* This is the default password, so DON'T set conn->bits.user_passwd */
3928
 
  }
3929
 
  else {
3930
 
    /* store user + password, zero-length if not set */
3931
 
    conn->user = strdup(user);
3932
 
    conn->passwd = strdup(passwd);
3933
 
  }
3934
 
  if(!conn->user || !conn->passwd)
3935
 
    return CURLE_OUT_OF_MEMORY;
 
4449
   * Parse a user name and password in the URL and strip it out
 
4450
   * of the host name
 
4451
   *************************************************************/
 
4452
  result = parse_url_userpass(data, conn, user, passwd);
 
4453
  if(result != CURLE_OK)
 
4454
    return result;
 
4455
 
 
4456
  /*************************************************************
 
4457
   * Figure out the remote port number and fix it in the URL
 
4458
   *************************************************************/
 
4459
  result = parse_remote_port(data, conn);
 
4460
  if(result != CURLE_OK)
 
4461
    return result;
 
4462
 
 
4463
  /*************************************************************
 
4464
   * Check for an overridden user name and password, then set it
 
4465
   * for use
 
4466
   *************************************************************/
 
4467
  override_userpass(data, conn, user, passwd);
 
4468
  result = set_userpass(conn, user, passwd);
 
4469
  if(result != CURLE_OK)
 
4470
    return result;
3936
4471
 
3937
4472
  /*************************************************************
3938
4473
   * Check the current list of connections to see if we can
3951
4486
  */
3952
4487
  data->set.ssl.CApath = data->set.str[STRING_SSL_CAPATH];
3953
4488
  data->set.ssl.CAfile = data->set.str[STRING_SSL_CAFILE];
 
4489
  data->set.ssl.CRLfile = data->set.str[STRING_SSL_CRLFILE];
 
4490
  data->set.ssl.issuercert = data->set.str[STRING_SSL_ISSUERCERT];
3954
4491
  data->set.ssl.random_file = data->set.str[STRING_SSL_RANDOM_FILE];
3955
4492
  data->set.ssl.egdsocket = data->set.str[STRING_SSL_EGDSOCKET];
3956
4493
  data->set.ssl.cipher_list = data->set.str[STRING_SSL_CIPHER_LIST];
3974
4511
     * just allocated before we can move along and use the previously
3975
4512
     * existing one.
3976
4513
     */
3977
 
    struct connectdata *old_conn = conn;
3978
 
 
3979
 
    if(old_conn->proxy.rawalloc)
3980
 
      free(old_conn->proxy.rawalloc);
3981
 
 
3982
 
    /* free the SSL config struct from this connection struct as this was
3983
 
       allocated in vain and is targeted for destruction */
3984
 
    Curl_free_ssl_config(&conn->ssl_config);
3985
 
 
3986
 
    conn = conn_temp;        /* use this connection from now on */
3987
 
 
3988
 
    conn->data = old_conn->data;
3989
 
 
3990
 
    /* get the user+password information from the old_conn struct since it may
3991
 
     * be new for this request even when we re-use an existing connection */
3992
 
    conn->bits.user_passwd = old_conn->bits.user_passwd;
3993
 
    if(conn->bits.user_passwd) {
3994
 
      /* use the new user namd and password though */
3995
 
      Curl_safefree(conn->user);
3996
 
      Curl_safefree(conn->passwd);
3997
 
      conn->user = old_conn->user;
3998
 
      conn->passwd = old_conn->passwd;
3999
 
      old_conn->user = NULL;
4000
 
      old_conn->passwd = NULL;
4001
 
    }
4002
 
 
4003
 
    conn->bits.proxy_user_passwd = old_conn->bits.proxy_user_passwd;
4004
 
    if(conn->bits.proxy_user_passwd) {
4005
 
      /* use the new proxy user name and proxy password though */
4006
 
      Curl_safefree(conn->proxyuser);
4007
 
      Curl_safefree(conn->proxypasswd);
4008
 
      conn->proxyuser = old_conn->proxyuser;
4009
 
      conn->proxypasswd = old_conn->proxypasswd;
4010
 
      old_conn->proxyuser = NULL;
4011
 
      old_conn->proxypasswd = NULL;
4012
 
    }
4013
 
 
4014
 
    /* host can change, when doing keepalive with a proxy ! */
4015
 
    if(conn->bits.proxy) {
4016
 
      free(conn->host.rawalloc);
4017
 
      conn->host=old_conn->host;
4018
 
    }
4019
 
    else
4020
 
      free(old_conn->host.rawalloc); /* free the newly allocated name buffer */
4021
 
 
4022
 
    /* re-use init */
4023
 
    conn->bits.reuse = TRUE; /* yes, we're re-using here */
4024
 
 
4025
 
    Curl_safefree(old_conn->user);
4026
 
    Curl_safefree(old_conn->passwd);
4027
 
    Curl_safefree(old_conn->proxyuser);
4028
 
    Curl_safefree(old_conn->proxypasswd);
4029
 
    Curl_llist_destroy(old_conn->send_pipe, NULL);
4030
 
    Curl_llist_destroy(old_conn->recv_pipe, NULL);
4031
 
    Curl_llist_destroy(old_conn->pend_pipe, NULL);
4032
 
    Curl_safefree(old_conn->master_buffer);
4033
 
 
4034
 
    free(old_conn);          /* we don't need this anymore */
4035
 
 
4036
 
    *in_connect = conn;      /* return this instead! */
4037
 
 
 
4514
    reuse_conn(conn, conn_temp);
 
4515
    free(conn);          /* we don't need this anymore */
 
4516
    conn = conn_temp;
 
4517
    *in_connect = conn;
4038
4518
    infof(data, "Re-using existing connection! (#%ld) with host %s\n",
4039
4519
          conn->connectindex,
4040
4520
          conn->proxy.name?conn->proxy.dispname:conn->host.dispname);
 
4521
    /* copy this IP address to the common buffer for the easy handle so that
 
4522
       the address can actually survice the removal of this connection. strcpy
 
4523
       is safe since the target buffer is big enough to hold the largest
 
4524
       possible IP address */
 
4525
    strcpy(data->info.ip, conn->ip_addr_str);
 
4526
 
4041
4527
  }
4042
4528
  else {
4043
4529
    /*
4047
4533
    ConnectionStore(data, conn);
4048
4534
  }
4049
4535
 
 
4536
  /*
 
4537
   * Setup whatever necessary for a resumed transfer
 
4538
   */
4050
4539
  result = setup_range(data);
4051
4540
  if(result)
4052
4541
    return result;
4054
4543
  /* Continue connectdata initialization here. */
4055
4544
 
4056
4545
  /*
4057
 
   *
4058
4546
   * Inherit the proper values from the urldata struct AFTER we have arranged
4059
 
   * the persistent connection stuff */
 
4547
   * the persistent connection stuff
 
4548
   */
4060
4549
  conn->fread_func = data->set.fread_func;
4061
4550
  conn->fread_in = data->set.in;
4062
4551
  conn->seek_func = data->set.seek_func;
4063
4552
  conn->seek_client = data->set.seek_client;
4064
4553
 
4065
 
#ifndef USE_ARES
4066
 
  /*************************************************************
4067
 
   * Set timeout if that is being used, and we're not using an asynchronous
4068
 
   * name resolve.
4069
 
   *************************************************************/
4070
 
  if((data->set.timeout || data->set.connecttimeout) && !data->set.no_signal) {
4071
 
    /*************************************************************
4072
 
     * Set signal handler to catch SIGALRM
4073
 
     * Store the old value to be able to set it back later!
4074
 
     *************************************************************/
4075
 
 
4076
 
#ifdef SIGALRM
4077
 
#ifdef HAVE_ALARM
4078
 
    long shortest;
4079
 
#endif
4080
 
#ifdef HAVE_SIGACTION
4081
 
    struct sigaction sigact;
4082
 
    sigaction(SIGALRM, NULL, &sigact);
4083
 
    keep_sigact = sigact;
4084
 
    keep_copysig = TRUE; /* yes, we have a copy */
4085
 
    sigact.sa_handler = alarmfunc;
4086
 
#ifdef SA_RESTART
4087
 
    /* HPUX doesn't have SA_RESTART but defaults to that behaviour! */
4088
 
    sigact.sa_flags &= ~SA_RESTART;
4089
 
#endif
4090
 
    /* now set the new struct */
4091
 
    sigaction(SIGALRM, &sigact, NULL);
4092
 
#else /* HAVE_SIGACTION */
4093
 
    /* no sigaction(), revert to the much lamer signal() */
4094
 
#ifdef HAVE_SIGNAL
4095
 
    keep_sigact = signal(SIGALRM, alarmfunc);
4096
 
#endif
4097
 
#endif /* HAVE_SIGACTION */
4098
 
 
4099
 
    /* We set the timeout on the name resolving phase first, separately from
4100
 
     * the download/upload part to allow a maximum time on everything. This is
4101
 
     * a signal-based timeout, why it won't work and shouldn't be used in
4102
 
     * multi-threaded environments. */
4103
 
 
4104
 
#ifdef HAVE_ALARM
4105
 
    shortest = data->set.timeout; /* default to this timeout value */
4106
 
    if(shortest && data->set.connecttimeout &&
4107
 
       (data->set.connecttimeout < shortest))
4108
 
      /* if both are set, pick the shortest */
4109
 
      shortest = data->set.connecttimeout;
4110
 
    else if(!shortest)
4111
 
      /* if timeout is not set, use the connect timeout */
4112
 
      shortest = data->set.connecttimeout;
4113
 
 
4114
 
    if(shortest < 1000)
4115
 
      /* the alarm() function only provide integer second resolution, so if
4116
 
         we want to wait less than one second we must bail out already now. */
4117
 
      return CURLE_OPERATION_TIMEDOUT;
4118
 
 
4119
 
    /* alarm() makes a signal get sent when the timeout fires off, and that
4120
 
       will abort system calls */
4121
 
    prev_alarm = alarm((unsigned int) (shortest ? shortest/1000L : shortest));
4122
 
    /* We can expect the conn->created time to be "now", as that was just
4123
 
       recently set in the beginning of this function and nothing slow
4124
 
       has been done since then until now. */
4125
 
#endif
4126
 
#endif /* SIGALRM */
4127
 
  }
4128
 
#endif /* USE_ARES */
4129
 
 
4130
 
  /*************************************************************
4131
 
   * Resolve the name of the server or proxy
4132
 
   *************************************************************/
4133
 
  if(conn->bits.reuse) {
4134
 
    /* re-used connection, no resolving is necessary */
4135
 
    hostaddr = NULL;
4136
 
    /* we'll need to clear conn->dns_entry later in Curl_disconnect() */
4137
 
 
4138
 
    if(conn->bits.proxy)
4139
 
      fix_hostname(data, conn, &conn->host);
4140
 
  }
4141
 
  else {
4142
 
    /* this is a fresh connect */
4143
 
 
4144
 
    /* set a pointer to the hostname we display */
4145
 
    fix_hostname(data, conn, &conn->host);
4146
 
 
4147
 
    if(!conn->proxy.name || !*conn->proxy.name) {
4148
 
      /* If not connecting via a proxy, extract the port from the URL, if it is
4149
 
       * there, thus overriding any defaults that might have been set above. */
4150
 
      conn->port =  conn->remote_port; /* it is the same port */
4151
 
 
4152
 
      /* Resolve target host right on */
4153
 
      rc = Curl_resolv(conn, conn->host.name, (int)conn->port, &hostaddr);
4154
 
      if(rc == CURLRESOLV_PENDING)
4155
 
        *async = TRUE;
4156
 
 
4157
 
      else if(!hostaddr) {
4158
 
        failf(data, "Couldn't resolve host '%s'", conn->host.dispname);
4159
 
        result =  CURLE_COULDNT_RESOLVE_HOST;
4160
 
        /* don't return yet, we need to clean up the timeout first */
4161
 
      }
4162
 
    }
4163
 
    else {
4164
 
      /* This is a proxy that hasn't been resolved yet. */
4165
 
 
4166
 
      /* IDN-fix the proxy name */
4167
 
      fix_hostname(data, conn, &conn->proxy);
4168
 
 
4169
 
      /* resolve proxy */
4170
 
      rc = Curl_resolv(conn, conn->proxy.name, (int)conn->port, &hostaddr);
4171
 
 
4172
 
      if(rc == CURLRESOLV_PENDING)
4173
 
        *async = TRUE;
4174
 
 
4175
 
      else if(!hostaddr) {
4176
 
        failf(data, "Couldn't resolve proxy '%s'", conn->proxy.dispname);
4177
 
        result = CURLE_COULDNT_RESOLVE_PROXY;
4178
 
        /* don't return yet, we need to clean up the timeout first */
4179
 
      }
4180
 
    }
4181
 
  }
4182
 
  *addr = hostaddr;
4183
 
 
4184
 
#if defined(HAVE_ALARM) && defined(SIGALRM) && !defined(USE_ARES)
4185
 
  if((data->set.timeout || data->set.connecttimeout) && !data->set.no_signal) {
4186
 
#ifdef HAVE_SIGACTION
4187
 
    if(keep_copysig) {
4188
 
      /* we got a struct as it looked before, now put that one back nice
4189
 
         and clean */
4190
 
      sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
4191
 
    }
4192
 
#else
4193
 
#ifdef HAVE_SIGNAL
4194
 
    /* restore the previous SIGALRM handler */
4195
 
    signal(SIGALRM, keep_sigact);
4196
 
#endif
4197
 
#endif /* HAVE_SIGACTION */
4198
 
 
4199
 
    /* switch back the alarm() to either zero or to what it was before minus
4200
 
       the time we spent until now! */
4201
 
    if(prev_alarm) {
4202
 
      /* there was an alarm() set before us, now put it back */
4203
 
      unsigned long elapsed_ms = Curl_tvdiff(Curl_tvnow(), conn->created);
4204
 
      unsigned long alarm_set;
4205
 
 
4206
 
      /* the alarm period is counted in even number of seconds */
4207
 
      alarm_set = prev_alarm - elapsed_ms/1000;
4208
 
 
4209
 
      if(!alarm_set ||
4210
 
         ((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) {
4211
 
        /* if the alarm time-left reached zero or turned "negative" (counted
4212
 
           with unsigned values), we should fire off a SIGALRM here, but we
4213
 
           won't, and zero would be to switch it off so we never set it to
4214
 
           less than 1! */
4215
 
        alarm(1);
4216
 
        result = CURLE_OPERATION_TIMEDOUT;
4217
 
        failf(data, "Previous alarm fired off!");
4218
 
      }
4219
 
      else
4220
 
        alarm((unsigned int)alarm_set);
4221
 
    }
4222
 
    else
4223
 
      alarm(0); /* just shut it off */
4224
 
  }
4225
 
#endif
 
4554
  /*************************************************************
 
4555
   * Resolve the address of the server or proxy
 
4556
   *************************************************************/
 
4557
  result = resolve_server(data, conn, addr, async);
4226
4558
 
4227
4559
  return result;
4228
4560
}
4229
4561
 
4230
 
/* SetupConnection() is called after the name resolve initiated in
4231
 
 * CreateConnection() is all done.
 
4562
/* setup_conn() is called after the name resolve initiated in
 
4563
 * create_conn() is all done.
4232
4564
 *
4233
4565
 * NOTE: the argument 'hostaddr' is NULL when this function is called for a
4234
4566
 * re-used connection.
4235
4567
 *
4236
 
 * conn->data MUST already have been setup fine (in CreateConnection)
 
4568
 * conn->data MUST already have been setup fine (in create_conn)
4237
4569
 */
4238
4570
 
4239
 
static CURLcode SetupConnection(struct connectdata *conn,
4240
 
                                struct Curl_dns_entry *hostaddr,
4241
 
                                bool *protocol_done)
 
4571
static CURLcode setup_conn(struct connectdata *conn,
 
4572
                           struct Curl_dns_entry *hostaddr,
 
4573
                           bool *protocol_done)
4242
4574
{
4243
4575
  CURLcode result=CURLE_OK;
4244
4576
  struct SessionHandle *data = conn->data;
4259
4591
     lingering set from a previous invoke */
4260
4592
  conn->bits.proxy_connect_closed = FALSE;
4261
4593
 
4262
 
  /*************************************************************
4263
 
   * Set user-agent for HTTP
4264
 
   *************************************************************/
4265
 
  if((conn->protocol&PROT_HTTP) && data->set.str[STRING_USERAGENT]) {
 
4594
  /*
 
4595
   * Set user-agent. Used for HTTP, but since we can attempt to tunnel
 
4596
   * basically anything through a http proxy we can't limit this based on
 
4597
   * protocol.
 
4598
   */
 
4599
  if(data->set.str[STRING_USERAGENT]) {
4266
4600
    Curl_safefree(conn->allocptr.uagent);
4267
4601
    conn->allocptr.uagent =
4268
4602
      aprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
4282
4616
    if(CURL_SOCKET_BAD == conn->sock[FIRSTSOCKET]) {
4283
4617
      bool connected = FALSE;
4284
4618
 
4285
 
      /* Connect only if not already connected! */
 
4619
      /* Connect only if not already connected!
 
4620
       *
 
4621
       * NOTE: hostaddr can be NULL when passed to this function, but that is
 
4622
       * only for the case where we re-use an existing connection and thus
 
4623
       * this code section will not be reached with hostaddr == NULL.
 
4624
       */
4286
4625
      result = ConnectPlease(data, conn, hostaddr, &connected);
4287
4626
 
4288
4627
      if(connected) {
4309
4648
    }
4310
4649
    else {
4311
4650
      Curl_pgrsTime(data, TIMER_CONNECT); /* we're connected already */
 
4651
      Curl_pgrsTime(data, TIMER_APPCONNECT); /* we're connected already */
4312
4652
      conn->bits.tcpconnect = TRUE;
4313
4653
      *protocol_done = TRUE;
4314
4654
      if(data->set.verbose)
4322
4662
                               set this here perhaps a second time */
4323
4663
 
4324
4664
#ifdef __EMX__
4325
 
  /* 20000330 mgs
4326
 
   * the check is quite a hack...
4327
 
   * we're calling _fsetmode to fix the problem with fwrite converting newline
4328
 
   * characters (you get mangled text files, and corrupted binary files when
4329
 
   * you download to stdout and redirect it to a file). */
 
4665
  /*
 
4666
   * This check is quite a hack. We're calling _fsetmode to fix the problem
 
4667
   * with fwrite converting newline characters (you get mangled text files,
 
4668
   * and corrupted binary files when you download to stdout and redirect it to
 
4669
   * a file).
 
4670
   */
4330
4671
 
4331
4672
  if((data->set.out)->_handle == NULL) {
4332
4673
    _fsetmode(stdout, "b");
4333
4674
  }
4334
4675
#endif
4335
4676
 
4336
 
  return CURLE_OK;
 
4677
  return result;
4337
4678
}
4338
4679
 
4339
4680
CURLcode Curl_connect(struct SessionHandle *data,
4347
4688
  *asyncp = FALSE; /* assume synchronous resolves by default */
4348
4689
 
4349
4690
  /* call the stuff that needs to be called */
4350
 
  code = CreateConnection(data, in_connect, &dns, asyncp);
 
4691
  code = create_conn(data, in_connect, &dns, asyncp);
4351
4692
 
4352
4693
  if(CURLE_OK == code) {
4353
4694
    /* no error */
4354
 
    if((*in_connect)->send_pipe->size +
4355
 
       (*in_connect)->recv_pipe->size != 0)
 
4695
    if((*in_connect)->send_pipe->size || (*in_connect)->recv_pipe->size)
4356
4696
      /* pipelining */
4357
4697
      *protocol_done = TRUE;
4358
4698
    else {
 
4699
 
4359
4700
      if(dns || !*asyncp)
4360
4701
        /* If an address is available it means that we already have the name
4361
4702
           resolved, OR it isn't async. if this is a re-used connection 'dns'
4362
4703
           will be NULL here. Continue connecting from here */
4363
 
        code = SetupConnection(*in_connect, dns, protocol_done);
4364
 
      /* else
4365
 
         response will be received and treated async wise */
 
4704
        code = setup_conn(*in_connect, dns, protocol_done);
 
4705
 
 
4706
      if(dns && code) {
 
4707
        /* We have the dns entry info already but failed to connect to the
 
4708
         * host and thus we must make sure to unlock the dns entry again
 
4709
         * before returning failure from here.
 
4710
         */
 
4711
        Curl_resolv_unlock(data, dns);
 
4712
      }
4366
4713
    }
4367
4714
  }
4368
4715
 
4369
 
  if(CURLE_OK != code && *in_connect) {
 
4716
  if(code && *in_connect) {
4370
4717
    /* We're not allowed to return failure with memory left allocated
4371
4718
       in the connectdata struct, free those here */
4372
4719
    Curl_disconnect(*in_connect); /* close the connection */
4386
4733
{
4387
4734
#if defined(USE_ARES) || defined(USE_THREADING_GETHOSTBYNAME) || \
4388
4735
    defined(USE_THREADING_GETADDRINFO)
4389
 
  CURLcode code = SetupConnection(conn, conn->async.dns, protocol_done);
 
4736
  CURLcode code = setup_conn(conn, conn->async.dns, protocol_done);
4390
4737
 
4391
4738
  if(code)
4392
4739
    /* We're not allowed to return failure with memory left allocated
4418
4765
 
4419
4766
  Curl_expire(data, 0); /* stop timer */
4420
4767
 
4421
 
  if(Curl_removeHandleFromPipeline(data, conn->recv_pipe) &&
4422
 
     conn->readchannel_inuse)
4423
 
    conn->readchannel_inuse = FALSE;
4424
 
  if(Curl_removeHandleFromPipeline(data, conn->send_pipe) &&
4425
 
     conn->writechannel_inuse)
4426
 
    conn->writechannel_inuse = FALSE;
4427
 
  Curl_removeHandleFromPipeline(data, conn->pend_pipe);
 
4768
  Curl_getoff_all_pipelines(data, conn);
4428
4769
 
4429
4770
  if(conn->bits.done ||
4430
4771
     (conn->send_pipe->size + conn->recv_pipe->size != 0 &&
4482
4823
     state it is for re-using, so we're forced to close it. In a perfect world
4483
4824
     we can add code that keep track of if we really must close it here or not,
4484
4825
     but currently we have no such detail knowledge.
 
4826
 
 
4827
     connectindex == -1 here means that the connection has no spot in the
 
4828
     connection cache and thus we must disconnect it here.
4485
4829
  */
4486
 
  if(data->set.reuse_forbid || conn->bits.close || premature) {
 
4830
  if(data->set.reuse_forbid || conn->bits.close || premature ||
 
4831
     (-1 == conn->connectindex)) {
4487
4832
    CURLcode res2 = Curl_disconnect(conn); /* close the connection */
4488
4833
 
4489
4834
    /* If we had an error already, make sure we return that one. But
4514
4859
 * do_init() inits the readwrite session. This is inited each time (in the DO
4515
4860
 * function before the protocol-specific DO functions are invoked) for a
4516
4861
 * transfer, sometimes multiple times on the same SessionHandle. Make sure
4517
 
 * nothing in here depends on stuff that are setup dynamicly for the transfer.
 
4862
 * nothing in here depends on stuff that are setup dynamically for the
 
4863
 * transfer.
4518
4864
 */
4519
4865
 
4520
4866
static CURLcode do_init(struct connectdata *conn)
4524
4870
 
4525
4871
  conn->bits.done = FALSE; /* Curl_done() is not called yet */
4526
4872
  conn->bits.do_more = FALSE; /* by default there's no curl_do_more() to use */
 
4873
  data->state.expect100header = FALSE;
4527
4874
 
4528
4875
  /* NB: the content encoding software depends on this initialization */
4529
4876
  Curl_easy_initHandleData(data);
4531
4878
  k->start = Curl_tvnow(); /* start time */
4532
4879
  k->now = k->start;   /* current time is now */
4533
4880
  k->header = TRUE; /* assume header */
4534
 
  k->httpversion = -1; /* unknown at this point */
4535
4881
 
4536
4882
  k->bytecount = 0;
4537
4883