]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/mobile/jni/thirdparty/curl/lib/url.c
Add MuPDF native source codes
[hornmich/skoda-qr-demo.git] / QRScanner / mobile / jni / thirdparty / curl / lib / url.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
27 #endif
28 #ifdef HAVE_NETDB_H
29 #include <netdb.h>
30 #endif
31 #ifdef HAVE_ARPA_INET_H
32 #include <arpa/inet.h>
33 #endif
34 #ifdef HAVE_NET_IF_H
35 #include <net/if.h>
36 #endif
37 #ifdef HAVE_SYS_IOCTL_H
38 #include <sys/ioctl.h>
39 #endif
40
41 #ifdef HAVE_SYS_PARAM_H
42 #include <sys/param.h>
43 #endif
44
45 #ifdef __VMS
46 #include <in.h>
47 #include <inet.h>
48 #endif
49
50 #ifndef HAVE_SOCKET
51 #error "We can't compile without socket() support!"
52 #endif
53
54 #ifdef HAVE_LIMITS_H
55 #include <limits.h>
56 #endif
57
58 #ifdef USE_LIBIDN
59 #include <idna.h>
60 #include <tld.h>
61 #include <stringprep.h>
62 #ifdef HAVE_IDN_FREE_H
63 #include <idn-free.h>
64 #else
65 /* prototype from idn-free.h, not provided by libidn 0.4.5's make install! */
66 void idn_free (void *ptr);
67 #endif
68 #ifndef HAVE_IDN_FREE
69 /* if idn_free() was not found in this version of libidn use free() instead */
70 #define idn_free(x) (free)(x)
71 #endif
72 #elif defined(USE_WIN32_IDN)
73 /* prototype for curl_win32_idn_to_ascii() */
74 int curl_win32_idn_to_ascii(const char *in, char **out);
75 #endif  /* USE_LIBIDN */
76
77 #include "urldata.h"
78 #include "netrc.h"
79
80 #include "formdata.h"
81 #include "sslgen.h"
82 #include "hostip.h"
83 #include "transfer.h"
84 #include "sendf.h"
85 #include "progress.h"
86 #include "cookie.h"
87 #include "strequal.h"
88 #include "strerror.h"
89 #include "escape.h"
90 #include "strtok.h"
91 #include "share.h"
92 #include "content_encoding.h"
93 #include "http_digest.h"
94 #include "http_negotiate.h"
95 #include "select.h"
96 #include "multiif.h"
97 #include "easyif.h"
98 #include "speedcheck.h"
99 #include "rawstr.h"
100 #include "warnless.h"
101 #include "non-ascii.h"
102 #include "inet_pton.h"
103
104 /* And now for the protocols */
105 #include "ftp.h"
106 #include "dict.h"
107 #include "telnet.h"
108 #include "tftp.h"
109 #include "http.h"
110 #include "file.h"
111 #include "curl_ldap.h"
112 #include "ssh.h"
113 #include "imap.h"
114 #include "url.h"
115 #include "connect.h"
116 #include "inet_ntop.h"
117 #include "curl_ntlm.h"
118 #include "curl_ntlm_wb.h"
119 #include "socks.h"
120 #include "curl_rtmp.h"
121 #include "gopher.h"
122 #include "http_proxy.h"
123 #include "bundles.h"
124 #include "conncache.h"
125 #include "multihandle.h"
126 #include "pipeline.h"
127
128 #define _MPRINTF_REPLACE /* use our functions only */
129 #include <curl/mprintf.h>
130
131 #include "curl_memory.h"
132 /* The last #include file should be: */
133 #include "memdebug.h"
134
135 /* Local static prototypes */
136 static struct connectdata *
137 find_oldest_idle_connection(struct SessionHandle *data);
138 static struct connectdata *
139 find_oldest_idle_connection_in_bundle(struct SessionHandle *data,
140                                       struct connectbundle *bundle);
141 static void conn_free(struct connectdata *conn);
142 static void signalPipeClose(struct curl_llist *pipeline, bool pipe_broke);
143 static CURLcode do_init(struct connectdata *conn);
144 static CURLcode parse_url_login(struct SessionHandle *data,
145                                 struct connectdata *conn,
146                                 char *user, char *passwd, char *options);
147 static CURLcode parse_login_details(const char *login, const size_t len,
148                                     char **userptr, char **passwdptr,
149                                     char **optionsptr);
150 /*
151  * Protocol table.
152  */
153
154 static const struct Curl_handler * const protocols[] = {
155
156 #ifndef CURL_DISABLE_HTTP
157   &Curl_handler_http,
158 #endif
159
160 #if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
161   &Curl_handler_https,
162 #endif
163
164 #ifndef CURL_DISABLE_FTP
165   &Curl_handler_ftp,
166 #endif
167
168 #if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)
169   &Curl_handler_ftps,
170 #endif
171
172 #ifndef CURL_DISABLE_TELNET
173   &Curl_handler_telnet,
174 #endif
175
176 #ifndef CURL_DISABLE_DICT
177   &Curl_handler_dict,
178 #endif
179
180 #ifndef CURL_DISABLE_LDAP
181   &Curl_handler_ldap,
182 #if !defined(CURL_DISABLE_LDAPS) && \
183     ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \
184      (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))
185   &Curl_handler_ldaps,
186 #endif
187 #endif
188
189 #ifndef CURL_DISABLE_FILE
190   &Curl_handler_file,
191 #endif
192
193 #ifndef CURL_DISABLE_TFTP
194   &Curl_handler_tftp,
195 #endif
196
197 #ifdef USE_LIBSSH2
198   &Curl_handler_scp,
199   &Curl_handler_sftp,
200 #endif
201
202 #ifndef CURL_DISABLE_IMAP
203   &Curl_handler_imap,
204 #ifdef USE_SSL
205   &Curl_handler_imaps,
206 #endif
207 #endif
208
209 #ifndef CURL_DISABLE_POP3
210   &Curl_handler_pop3,
211 #ifdef USE_SSL
212   &Curl_handler_pop3s,
213 #endif
214 #endif
215
216 #ifndef CURL_DISABLE_SMTP
217   &Curl_handler_smtp,
218 #ifdef USE_SSL
219   &Curl_handler_smtps,
220 #endif
221 #endif
222
223 #ifndef CURL_DISABLE_RTSP
224   &Curl_handler_rtsp,
225 #endif
226
227 #ifndef CURL_DISABLE_GOPHER
228   &Curl_handler_gopher,
229 #endif
230
231 #ifdef USE_LIBRTMP
232   &Curl_handler_rtmp,
233   &Curl_handler_rtmpt,
234   &Curl_handler_rtmpe,
235   &Curl_handler_rtmpte,
236   &Curl_handler_rtmps,
237   &Curl_handler_rtmpts,
238 #endif
239
240   (struct Curl_handler *) NULL
241 };
242
243 /*
244  * Dummy handler for undefined protocol schemes.
245  */
246
247 static const struct Curl_handler Curl_handler_dummy = {
248   "<no protocol>",                      /* scheme */
249   ZERO_NULL,                            /* setup_connection */
250   ZERO_NULL,                            /* do_it */
251   ZERO_NULL,                            /* done */
252   ZERO_NULL,                            /* do_more */
253   ZERO_NULL,                            /* connect_it */
254   ZERO_NULL,                            /* connecting */
255   ZERO_NULL,                            /* doing */
256   ZERO_NULL,                            /* proto_getsock */
257   ZERO_NULL,                            /* doing_getsock */
258   ZERO_NULL,                            /* domore_getsock */
259   ZERO_NULL,                            /* perform_getsock */
260   ZERO_NULL,                            /* disconnect */
261   ZERO_NULL,                            /* readwrite */
262   0,                                    /* defport */
263   0,                                    /* protocol */
264   PROTOPT_NONE                          /* flags */
265 };
266
267 void Curl_freeset(struct SessionHandle *data)
268 {
269   /* Free all dynamic strings stored in the data->set substructure. */
270   enum dupstring i;
271   for(i=(enum dupstring)0; i < STRING_LAST; i++)
272     Curl_safefree(data->set.str[i]);
273
274   if(data->change.referer_alloc) {
275     Curl_safefree(data->change.referer);
276     data->change.referer_alloc = FALSE;
277   }
278   data->change.referer = NULL;
279 }
280
281 static CURLcode setstropt(char **charp, char *s)
282 {
283   /* Release the previous storage at `charp' and replace by a dynamic storage
284      copy of `s'. Return CURLE_OK or CURLE_OUT_OF_MEMORY. */
285
286   Curl_safefree(*charp);
287
288   if(s) {
289     s = strdup(s);
290
291     if(!s)
292       return CURLE_OUT_OF_MEMORY;
293
294     *charp = s;
295   }
296
297   return CURLE_OK;
298 }
299
300 static CURLcode setstropt_userpwd(char *option, char **userp, char **passwdp,
301                                   char **optionsp)
302 {
303   CURLcode result = CURLE_OK;
304   char *user = NULL;
305   char *passwd = NULL;
306   char *options = NULL;
307
308   /* Parse the login details if specified. It not then we treat NULL as a hint
309      to clear the existing data */
310   if(option) {
311     result = parse_login_details(option, strlen(option),
312                                  (userp ? &user : NULL),
313                                  (passwdp ? &passwd : NULL),
314                                  (optionsp ? &options : NULL));
315   }
316
317   if(!result) {
318     /* Store the username part of option if required */
319     if(userp) {
320       Curl_safefree(*userp);
321       *userp = user;
322     }
323
324     /* Store the password part of option if required */
325     if(passwdp) {
326       Curl_safefree(*passwdp);
327       *passwdp = passwd;
328     }
329
330     /* Store the options part of option if required */
331     if(optionsp) {
332       Curl_safefree(*optionsp);
333       *optionsp = options;
334     }
335   }
336
337   return result;
338 }
339
340 CURLcode Curl_dupset(struct SessionHandle *dst, struct SessionHandle *src)
341 {
342   CURLcode r = CURLE_OK;
343   enum dupstring i;
344
345   /* Copy src->set into dst->set first, then deal with the strings
346      afterwards */
347   dst->set = src->set;
348
349   /* clear all string pointers first */
350   memset(dst->set.str, 0, STRING_LAST * sizeof(char *));
351
352   /* duplicate all strings */
353   for(i=(enum dupstring)0; i< STRING_LAST; i++) {
354     r = setstropt(&dst->set.str[i], src->set.str[i]);
355     if(r != CURLE_OK)
356       break;
357   }
358
359   /* If a failure occurred, freeing has to be performed externally. */
360   return r;
361 }
362
363 /*
364  * This is the internal function curl_easy_cleanup() calls. This should
365  * cleanup and free all resources associated with this sessionhandle.
366  *
367  * NOTE: if we ever add something that attempts to write to a socket or
368  * similar here, we must ignore SIGPIPE first. It is currently only done
369  * when curl_easy_perform() is invoked.
370  */
371
372 CURLcode Curl_close(struct SessionHandle *data)
373 {
374   struct Curl_multi *m;
375
376   if(!data)
377     return CURLE_OK;
378
379   Curl_expire(data, 0); /* shut off timers */
380
381   m = data->multi;
382
383   if(m)
384     /* This handle is still part of a multi handle, take care of this first
385        and detach this handle from there. */
386     curl_multi_remove_handle(data->multi, data);
387
388   if(data->multi_easy)
389     /* when curl_easy_perform() is used, it creates its own multi handle to
390        use and this is the one */
391     curl_multi_cleanup(data->multi_easy);
392
393   /* Destroy the timeout list that is held in the easy handle. It is
394      /normally/ done by curl_multi_remove_handle() but this is "just in
395      case" */
396   if(data->state.timeoutlist) {
397     Curl_llist_destroy(data->state.timeoutlist, NULL);
398     data->state.timeoutlist = NULL;
399   }
400
401   data->magic = 0; /* force a clear AFTER the possibly enforced removal from
402                       the multi handle, since that function uses the magic
403                       field! */
404
405   if(data->state.rangestringalloc)
406     free(data->state.range);
407
408   /* Free the pathbuffer */
409   Curl_safefree(data->state.pathbuffer);
410   data->state.path = NULL;
411
412   Curl_safefree(data->state.proto.generic);
413
414   /* Close down all open SSL info and sessions */
415   Curl_ssl_close_all(data);
416   Curl_safefree(data->state.first_host);
417   Curl_safefree(data->state.scratch);
418   Curl_ssl_free_certinfo(data);
419
420   if(data->change.referer_alloc) {
421     Curl_safefree(data->change.referer);
422     data->change.referer_alloc = FALSE;
423   }
424   data->change.referer = NULL;
425
426   if(data->change.url_alloc) {
427     Curl_safefree(data->change.url);
428     data->change.url_alloc = FALSE;
429   }
430   data->change.url = NULL;
431
432   Curl_safefree(data->state.headerbuff);
433
434   Curl_flush_cookies(data, 1);
435
436   Curl_digest_cleanup(data);
437
438   Curl_safefree(data->info.contenttype);
439   Curl_safefree(data->info.wouldredirect);
440
441   /* this destroys the channel and we cannot use it anymore after this */
442   Curl_resolver_cleanup(data->state.resolver);
443
444   Curl_convert_close(data);
445
446   /* No longer a dirty share, if it exists */
447   if(data->share) {
448     Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
449     data->share->dirty--;
450     Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
451   }
452
453   Curl_freeset(data);
454   free(data);
455   return CURLE_OK;
456 }
457
458 /*
459  * Initialize the UserDefined fields within a SessionHandle.
460  * This may be safely called on a new or existing SessionHandle.
461  */
462 CURLcode Curl_init_userdefined(struct UserDefined *set)
463 {
464   CURLcode res = CURLE_OK;
465
466   set->out = stdout; /* default output to stdout */
467   set->in  = stdin;  /* default input from stdin */
468   set->err  = stderr;  /* default stderr to stderr */
469
470   /* use fwrite as default function to store output */
471   set->fwrite_func = (curl_write_callback)fwrite;
472
473   /* use fread as default function to read input */
474   set->fread_func = (curl_read_callback)fread;
475   set->is_fread_set = 0;
476   set->is_fwrite_set = 0;
477
478   set->seek_func = ZERO_NULL;
479   set->seek_client = ZERO_NULL;
480
481   /* conversion callbacks for non-ASCII hosts */
482   set->convfromnetwork = ZERO_NULL;
483   set->convtonetwork   = ZERO_NULL;
484   set->convfromutf8    = ZERO_NULL;
485
486   set->infilesize = -1;      /* we don't know any size */
487   set->postfieldsize = -1;   /* unknown size */
488   set->maxredirs = -1;       /* allow any amount by default */
489
490   set->httpreq = HTTPREQ_GET; /* Default HTTP request */
491   set->rtspreq = RTSPREQ_OPTIONS; /* Default RTSP request */
492   set->ftp_use_epsv = TRUE;   /* FTP defaults to EPSV operations */
493   set->ftp_use_eprt = TRUE;   /* FTP defaults to EPRT operations */
494   set->ftp_use_pret = FALSE;  /* mainly useful for drftpd servers */
495   set->ftp_filemethod = FTPFILE_MULTICWD;
496
497   set->dns_cache_timeout = 60; /* Timeout every 60 seconds by default */
498
499   /* Set the default size of the SSL session ID cache */
500   set->ssl.max_ssl_sessions = 5;
501
502   set->proxyport = CURL_DEFAULT_PROXY_PORT; /* from url.h */
503   set->proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
504   set->httpauth = CURLAUTH_BASIC;  /* defaults to basic */
505   set->proxyauth = CURLAUTH_BASIC; /* defaults to basic */
506
507   /* make libcurl quiet by default: */
508   set->hide_progress = TRUE;  /* CURLOPT_NOPROGRESS changes these */
509
510   /*
511    * libcurl 7.10 introduced SSL verification *by default*! This needs to be
512    * switched off unless wanted.
513    */
514   set->ssl.verifypeer = TRUE;
515   set->ssl.verifyhost = TRUE;
516 #ifdef USE_TLS_SRP
517   set->ssl.authtype = CURL_TLSAUTH_NONE;
518 #endif
519   set->ssh_auth_types = CURLSSH_AUTH_DEFAULT; /* defaults to any auth
520                                                       type */
521   set->ssl.sessionid = TRUE; /* session ID caching enabled by default */
522
523   set->new_file_perms = 0644;    /* Default permissions */
524   set->new_directory_perms = 0755; /* Default permissions */
525
526   /* for the *protocols fields we don't use the CURLPROTO_ALL convenience
527      define since we internally only use the lower 16 bits for the passed
528      in bitmask to not conflict with the private bits */
529   set->allowed_protocols = CURLPROTO_ALL;
530   set->redir_protocols =
531     CURLPROTO_ALL & ~(CURLPROTO_FILE|CURLPROTO_SCP); /* not FILE or SCP */
532
533 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
534   /*
535    * disallow unprotected protection negotiation NEC reference implementation
536    * seem not to follow rfc1961 section 4.3/4.4
537    */
538   set->socks5_gssapi_nec = FALSE;
539   /* set default gssapi service name */
540   res = setstropt(&set->str[STRING_SOCKS5_GSSAPI_SERVICE],
541                   (char *) CURL_DEFAULT_SOCKS5_GSSAPI_SERVICE);
542   if(res != CURLE_OK)
543     return res;
544 #endif
545
546   /* This is our preferred CA cert bundle/path since install time */
547 #if defined(CURL_CA_BUNDLE)
548   res = setstropt(&set->str[STRING_SSL_CAFILE], (char *) CURL_CA_BUNDLE);
549 #elif defined(CURL_CA_PATH)
550   res = setstropt(&set->str[STRING_SSL_CAPATH], (char *) CURL_CA_PATH);
551 #endif
552
553   set->wildcardmatch  = FALSE;
554   set->chunk_bgn      = ZERO_NULL;
555   set->chunk_end      = ZERO_NULL;
556
557   /* tcp keepalives are disabled by default, but provide reasonable values for
558    * the interval and idle times.
559    */
560   set->tcp_keepalive = FALSE;
561   set->tcp_keepintvl = 60;
562   set->tcp_keepidle = 60;
563
564   return res;
565 }
566
567 /**
568  * Curl_open()
569  *
570  * @param curl is a pointer to a sessionhandle pointer that gets set by this
571  * function.
572  * @return CURLcode
573  */
574
575 CURLcode Curl_open(struct SessionHandle **curl)
576 {
577   CURLcode res = CURLE_OK;
578   struct SessionHandle *data;
579   CURLcode status;
580
581   /* Very simple start-up: alloc the struct, init it with zeroes and return */
582   data = calloc(1, sizeof(struct SessionHandle));
583   if(!data) {
584     /* this is a very serious error */
585     DEBUGF(fprintf(stderr, "Error: calloc of SessionHandle failed\n"));
586     return CURLE_OUT_OF_MEMORY;
587   }
588
589   data->magic = CURLEASY_MAGIC_NUMBER;
590
591   status = Curl_resolver_init(&data->state.resolver);
592   if(status) {
593     DEBUGF(fprintf(stderr, "Error: resolver_init failed\n"));
594     free(data);
595     return status;
596   }
597
598   /* We do some initial setup here, all those fields that can't be just 0 */
599
600   data->state.headerbuff = malloc(HEADERSIZE);
601   if(!data->state.headerbuff) {
602     DEBUGF(fprintf(stderr, "Error: malloc of headerbuff failed\n"));
603     res = CURLE_OUT_OF_MEMORY;
604   }
605   else {
606     Curl_easy_initHandleData(data);
607     res = Curl_init_userdefined(&data->set);
608
609     data->state.headersize=HEADERSIZE;
610
611     Curl_convert_init(data);
612
613     /* most recent connection is not yet defined */
614     data->state.lastconnect = NULL;
615
616     data->progress.flags |= PGRS_HIDE;
617     data->state.current_speed = -1; /* init to negative == impossible */
618
619     data->wildcard.state = CURLWC_INIT;
620     data->wildcard.filelist = NULL;
621     data->set.fnmatch = ZERO_NULL;
622     data->set.maxconnects = DEFAULT_CONNCACHE_SIZE; /* for easy handles */
623   }
624
625   if(res) {
626     Curl_resolver_cleanup(data->state.resolver);
627     if(data->state.headerbuff)
628       free(data->state.headerbuff);
629     Curl_freeset(data);
630     free(data);
631     data = NULL;
632   }
633   else
634     *curl = data;
635
636   return res;
637 }
638
639 CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option,
640                      va_list param)
641 {
642   char *argptr;
643   CURLcode result = CURLE_OK;
644   long arg;
645 #ifndef CURL_DISABLE_HTTP
646   curl_off_t bigsize;
647 #endif
648
649   switch(option) {
650   case CURLOPT_DNS_CACHE_TIMEOUT:
651     data->set.dns_cache_timeout = va_arg(param, long);
652     break;
653   case CURLOPT_DNS_USE_GLOBAL_CACHE:
654     /* remember we want this enabled */
655     arg = va_arg(param, long);
656     data->set.global_dns_cache = (0 != arg)?TRUE:FALSE;
657     break;
658   case CURLOPT_SSL_CIPHER_LIST:
659     /* set a list of cipher we want to use in the SSL connection */
660     result = setstropt(&data->set.str[STRING_SSL_CIPHER_LIST],
661                        va_arg(param, char *));
662     break;
663
664   case CURLOPT_RANDOM_FILE:
665     /*
666      * This is the path name to a file that contains random data to seed
667      * the random SSL stuff with. The file is only used for reading.
668      */
669     result = setstropt(&data->set.str[STRING_SSL_RANDOM_FILE],
670                        va_arg(param, char *));
671     break;
672   case CURLOPT_EGDSOCKET:
673     /*
674      * The Entropy Gathering Daemon socket pathname
675      */
676     result = setstropt(&data->set.str[STRING_SSL_EGDSOCKET],
677                        va_arg(param, char *));
678     break;
679   case CURLOPT_MAXCONNECTS:
680     /*
681      * Set the absolute number of maximum simultaneous alive connection that
682      * libcurl is allowed to have.
683      */
684     data->set.maxconnects = va_arg(param, long);
685     break;
686   case CURLOPT_FORBID_REUSE:
687     /*
688      * When this transfer is done, it must not be left to be reused by a
689      * subsequent transfer but shall be closed immediately.
690      */
691     data->set.reuse_forbid = (0 != va_arg(param, long))?TRUE:FALSE;
692     break;
693   case CURLOPT_FRESH_CONNECT:
694     /*
695      * This transfer shall not use a previously cached connection but
696      * should be made with a fresh new connect!
697      */
698     data->set.reuse_fresh = (0 != va_arg(param, long))?TRUE:FALSE;
699     break;
700   case CURLOPT_VERBOSE:
701     /*
702      * Verbose means infof() calls that give a lot of information about
703      * the connection and transfer procedures as well as internal choices.
704      */
705     data->set.verbose = (0 != va_arg(param, long))?TRUE:FALSE;
706     break;
707   case CURLOPT_HEADER:
708     /*
709      * Set to include the header in the general data output stream.
710      */
711     data->set.include_header = (0 != va_arg(param, long))?TRUE:FALSE;
712     break;
713   case CURLOPT_NOPROGRESS:
714     /*
715      * Shut off the internal supported progress meter
716      */
717     data->set.hide_progress = (0 != va_arg(param, long))?TRUE:FALSE;
718     if(data->set.hide_progress)
719       data->progress.flags |= PGRS_HIDE;
720     else
721       data->progress.flags &= ~PGRS_HIDE;
722     break;
723   case CURLOPT_NOBODY:
724     /*
725      * Do not include the body part in the output data stream.
726      */
727     data->set.opt_no_body = (0 != va_arg(param, long))?TRUE:FALSE;
728     break;
729   case CURLOPT_FAILONERROR:
730     /*
731      * Don't output the >=300 error code HTML-page, but instead only
732      * return error.
733      */
734     data->set.http_fail_on_error = (0 != va_arg(param, long))?TRUE:FALSE;
735     break;
736   case CURLOPT_UPLOAD:
737   case CURLOPT_PUT:
738     /*
739      * We want to sent data to the remote host. If this is HTTP, that equals
740      * using the PUT request.
741      */
742     data->set.upload = (0 != va_arg(param, long))?TRUE:FALSE;
743     if(data->set.upload) {
744       /* If this is HTTP, PUT is what's needed to "upload" */
745       data->set.httpreq = HTTPREQ_PUT;
746       data->set.opt_no_body = FALSE; /* this is implied */
747     }
748     else
749       /* In HTTP, the opposite of upload is GET (unless NOBODY is true as
750          then this can be changed to HEAD later on) */
751       data->set.httpreq = HTTPREQ_GET;
752     break;
753   case CURLOPT_FILETIME:
754     /*
755      * Try to get the file time of the remote document. The time will
756      * later (possibly) become available using curl_easy_getinfo().
757      */
758     data->set.get_filetime = (0 != va_arg(param, long))?TRUE:FALSE;
759     break;
760   case CURLOPT_FTP_CREATE_MISSING_DIRS:
761     /*
762      * An FTP option that modifies an upload to create missing directories on
763      * the server.
764      */
765     switch(va_arg(param, long)) {
766     case 0:
767       data->set.ftp_create_missing_dirs = 0;
768       break;
769     case 1:
770       data->set.ftp_create_missing_dirs = 1;
771       break;
772     case 2:
773       data->set.ftp_create_missing_dirs = 2;
774       break;
775     default:
776       /* reserve other values for future use */
777       result = CURLE_UNKNOWN_OPTION;
778       break;
779     }
780     break;
781   case CURLOPT_SERVER_RESPONSE_TIMEOUT:
782     /*
783      * Option that specifies how quickly an server response must be obtained
784      * before it is considered failure. For pingpong protocols.
785      */
786     data->set.server_response_timeout = va_arg( param , long ) * 1000;
787     break;
788   case CURLOPT_TFTP_BLKSIZE:
789     /*
790      * TFTP option that specifies the block size to use for data transmission
791      */
792     data->set.tftp_blksize = va_arg(param, long);
793     break;
794   case CURLOPT_DIRLISTONLY:
795     /*
796      * An option that changes the command to one that asks for a list
797      * only, no file info details.
798      */
799     data->set.ftp_list_only = (0 != va_arg(param, long))?TRUE:FALSE;
800     break;
801   case CURLOPT_APPEND:
802     /*
803      * We want to upload and append to an existing file.
804      */
805     data->set.ftp_append = (0 != va_arg(param, long))?TRUE:FALSE;
806     break;
807   case CURLOPT_FTP_FILEMETHOD:
808     /*
809      * How do access files over FTP.
810      */
811     data->set.ftp_filemethod = (curl_ftpfile)va_arg(param, long);
812     break;
813   case CURLOPT_NETRC:
814     /*
815      * Parse the $HOME/.netrc file
816      */
817     data->set.use_netrc = (enum CURL_NETRC_OPTION)va_arg(param, long);
818     break;
819   case CURLOPT_NETRC_FILE:
820     /*
821      * Use this file instead of the $HOME/.netrc file
822      */
823     result = setstropt(&data->set.str[STRING_NETRC_FILE],
824                        va_arg(param, char *));
825     break;
826   case CURLOPT_TRANSFERTEXT:
827     /*
828      * This option was previously named 'FTPASCII'. Renamed to work with
829      * more protocols than merely FTP.
830      *
831      * Transfer using ASCII (instead of BINARY).
832      */
833     data->set.prefer_ascii = (0 != va_arg(param, long))?TRUE:FALSE;
834     break;
835   case CURLOPT_TIMECONDITION:
836     /*
837      * Set HTTP time condition. This must be one of the defines in the
838      * curl/curl.h header file.
839      */
840     data->set.timecondition = (curl_TimeCond)va_arg(param, long);
841     break;
842   case CURLOPT_TIMEVALUE:
843     /*
844      * This is the value to compare with the remote document with the
845      * method set with CURLOPT_TIMECONDITION
846      */
847     data->set.timevalue = (time_t)va_arg(param, long);
848     break;
849   case CURLOPT_SSLVERSION:
850     /*
851      * Set explicit SSL version to try to connect with, as some SSL
852      * implementations are lame.
853      */
854     data->set.ssl.version = va_arg(param, long);
855     break;
856
857 #ifndef CURL_DISABLE_HTTP
858   case CURLOPT_AUTOREFERER:
859     /*
860      * Switch on automatic referer that gets set if curl follows locations.
861      */
862     data->set.http_auto_referer = (0 != va_arg(param, long))?TRUE:FALSE;
863     break;
864
865   case CURLOPT_ACCEPT_ENCODING:
866     /*
867      * String to use at the value of Accept-Encoding header.
868      *
869      * If the encoding is set to "" we use an Accept-Encoding header that
870      * encompasses all the encodings we support.
871      * If the encoding is set to NULL we don't send an Accept-Encoding header
872      * and ignore an received Content-Encoding header.
873      *
874      */
875     argptr = va_arg(param, char *);
876     result = setstropt(&data->set.str[STRING_ENCODING],
877                        (argptr && !*argptr)?
878                        (char *) ALL_CONTENT_ENCODINGS: argptr);
879     break;
880
881   case CURLOPT_TRANSFER_ENCODING:
882     data->set.http_transfer_encoding = (0 != va_arg(param, long))?TRUE:FALSE;
883     break;
884
885   case CURLOPT_FOLLOWLOCATION:
886     /*
887      * Follow Location: header hints on a HTTP-server.
888      */
889     data->set.http_follow_location = (0 != va_arg(param, long))?TRUE:FALSE;
890     break;
891
892   case CURLOPT_UNRESTRICTED_AUTH:
893     /*
894      * Send authentication (user+password) when following locations, even when
895      * hostname changed.
896      */
897     data->set.http_disable_hostname_check_before_authentication =
898       (0 != va_arg(param, long))?TRUE:FALSE;
899     break;
900
901   case CURLOPT_MAXREDIRS:
902     /*
903      * The maximum amount of hops you allow curl to follow Location:
904      * headers. This should mostly be used to detect never-ending loops.
905      */
906     data->set.maxredirs = va_arg(param, long);
907     break;
908
909   case CURLOPT_POSTREDIR:
910   {
911     /*
912      * Set the behaviour of POST when redirecting
913      * CURL_REDIR_GET_ALL - POST is changed to GET after 301 and 302
914      * CURL_REDIR_POST_301 - POST is kept as POST after 301
915      * CURL_REDIR_POST_302 - POST is kept as POST after 302
916      * CURL_REDIR_POST_303 - POST is kept as POST after 303
917      * CURL_REDIR_POST_ALL - POST is kept as POST after 301, 302 and 303
918      * other - POST is kept as POST after 301 and 302
919      */
920     int postRedir = curlx_sltosi(va_arg(param, long));
921     data->set.keep_post = postRedir & CURL_REDIR_POST_ALL;
922   }
923   break;
924
925   case CURLOPT_POST:
926     /* Does this option serve a purpose anymore? Yes it does, when
927        CURLOPT_POSTFIELDS isn't used and the POST data is read off the
928        callback! */
929     if(va_arg(param, long)) {
930       data->set.httpreq = HTTPREQ_POST;
931       data->set.opt_no_body = FALSE; /* this is implied */
932     }
933     else
934       data->set.httpreq = HTTPREQ_GET;
935     break;
936
937   case CURLOPT_COPYPOSTFIELDS:
938     /*
939      * A string with POST data. Makes curl HTTP POST. Even if it is NULL.
940      * If needed, CURLOPT_POSTFIELDSIZE must have been set prior to
941      *  CURLOPT_COPYPOSTFIELDS and not altered later.
942      */
943     argptr = va_arg(param, char *);
944
945     if(!argptr || data->set.postfieldsize == -1)
946       result = setstropt(&data->set.str[STRING_COPYPOSTFIELDS], argptr);
947     else {
948       /*
949        *  Check that requested length does not overflow the size_t type.
950        */
951
952       if((data->set.postfieldsize < 0) ||
953          ((sizeof(curl_off_t) != sizeof(size_t)) &&
954           (data->set.postfieldsize > (curl_off_t)((size_t)-1))))
955         result = CURLE_OUT_OF_MEMORY;
956       else {
957         char * p;
958
959         (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
960
961         /* Allocate even when size == 0. This satisfies the need of possible
962            later address compare to detect the COPYPOSTFIELDS mode, and
963            to mark that postfields is used rather than read function or
964            form data.
965         */
966         p = malloc((size_t)(data->set.postfieldsize?
967                             data->set.postfieldsize:1));
968
969         if(!p)
970           result = CURLE_OUT_OF_MEMORY;
971         else {
972           if(data->set.postfieldsize)
973             memcpy(p, argptr, (size_t)data->set.postfieldsize);
974
975           data->set.str[STRING_COPYPOSTFIELDS] = p;
976         }
977       }
978     }
979
980     data->set.postfields = data->set.str[STRING_COPYPOSTFIELDS];
981     data->set.httpreq = HTTPREQ_POST;
982     break;
983
984   case CURLOPT_POSTFIELDS:
985     /*
986      * Like above, but use static data instead of copying it.
987      */
988     data->set.postfields = va_arg(param, void *);
989     /* Release old copied data. */
990     (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
991     data->set.httpreq = HTTPREQ_POST;
992     break;
993
994   case CURLOPT_POSTFIELDSIZE:
995     /*
996      * The size of the POSTFIELD data to prevent libcurl to do strlen() to
997      * figure it out. Enables binary posts.
998      */
999     bigsize = va_arg(param, long);
1000
1001     if(data->set.postfieldsize < bigsize &&
1002        data->set.postfields == data->set.str[STRING_COPYPOSTFIELDS]) {
1003       /* Previous CURLOPT_COPYPOSTFIELDS is no longer valid. */
1004       (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
1005       data->set.postfields = NULL;
1006     }
1007
1008     data->set.postfieldsize = bigsize;
1009     break;
1010
1011   case CURLOPT_POSTFIELDSIZE_LARGE:
1012     /*
1013      * The size of the POSTFIELD data to prevent libcurl to do strlen() to
1014      * figure it out. Enables binary posts.
1015      */
1016     bigsize = va_arg(param, curl_off_t);
1017
1018     if(data->set.postfieldsize < bigsize &&
1019        data->set.postfields == data->set.str[STRING_COPYPOSTFIELDS]) {
1020       /* Previous CURLOPT_COPYPOSTFIELDS is no longer valid. */
1021       (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
1022       data->set.postfields = NULL;
1023     }
1024
1025     data->set.postfieldsize = bigsize;
1026     break;
1027
1028   case CURLOPT_HTTPPOST:
1029     /*
1030      * Set to make us do HTTP POST
1031      */
1032     data->set.httppost = va_arg(param, struct curl_httppost *);
1033     data->set.httpreq = HTTPREQ_POST_FORM;
1034     data->set.opt_no_body = FALSE; /* this is implied */
1035     break;
1036
1037   case CURLOPT_REFERER:
1038     /*
1039      * String to set in the HTTP Referer: field.
1040      */
1041     if(data->change.referer_alloc) {
1042       Curl_safefree(data->change.referer);
1043       data->change.referer_alloc = FALSE;
1044     }
1045     result = setstropt(&data->set.str[STRING_SET_REFERER],
1046                        va_arg(param, char *));
1047     data->change.referer = data->set.str[STRING_SET_REFERER];
1048     break;
1049
1050   case CURLOPT_USERAGENT:
1051     /*
1052      * String to use in the HTTP User-Agent field
1053      */
1054     result = setstropt(&data->set.str[STRING_USERAGENT],
1055                        va_arg(param, char *));
1056     break;
1057
1058   case CURLOPT_HTTPHEADER:
1059     /*
1060      * Set a list with HTTP headers to use (or replace internals with)
1061      */
1062     data->set.headers = va_arg(param, struct curl_slist *);
1063     break;
1064
1065   case CURLOPT_HTTP200ALIASES:
1066     /*
1067      * Set a list of aliases for HTTP 200 in response header
1068      */
1069     data->set.http200aliases = va_arg(param, struct curl_slist *);
1070     break;
1071
1072 #if !defined(CURL_DISABLE_COOKIES)
1073   case CURLOPT_COOKIE:
1074     /*
1075      * Cookie string to send to the remote server in the request.
1076      */
1077     result = setstropt(&data->set.str[STRING_COOKIE],
1078                        va_arg(param, char *));
1079     break;
1080
1081   case CURLOPT_COOKIEFILE:
1082     /*
1083      * Set cookie file to read and parse. Can be used multiple times.
1084      */
1085     argptr = (char *)va_arg(param, void *);
1086     if(argptr) {
1087       struct curl_slist *cl;
1088       /* append the cookie file name to the list of file names, and deal with
1089          them later */
1090       cl = curl_slist_append(data->change.cookielist, argptr);
1091       if(!cl) {
1092         curl_slist_free_all(data->change.cookielist);
1093         data->change.cookielist = NULL;
1094         return CURLE_OUT_OF_MEMORY;
1095       }
1096       data->change.cookielist = cl; /* store the list for later use */
1097     }
1098     break;
1099
1100   case CURLOPT_COOKIEJAR:
1101     /*
1102      * Set cookie file name to dump all cookies to when we're done.
1103      */
1104     result = setstropt(&data->set.str[STRING_COOKIEJAR],
1105                        va_arg(param, char *));
1106
1107     /*
1108      * Activate the cookie parser. This may or may not already
1109      * have been made.
1110      */
1111     data->cookies = Curl_cookie_init(data, NULL, data->cookies,
1112                                      data->set.cookiesession);
1113     break;
1114
1115   case CURLOPT_COOKIESESSION:
1116     /*
1117      * Set this option to TRUE to start a new "cookie session". It will
1118      * prevent the forthcoming read-cookies-from-file actions to accept
1119      * cookies that are marked as being session cookies, as they belong to a
1120      * previous session.
1121      *
1122      * In the original Netscape cookie spec, "session cookies" are cookies
1123      * with no expire date set. RFC2109 describes the same action if no
1124      * 'Max-Age' is set and RFC2965 includes the RFC2109 description and adds
1125      * a 'Discard' action that can enforce the discard even for cookies that
1126      * have a Max-Age.
1127      *
1128      * We run mostly with the original cookie spec, as hardly anyone implements
1129      * anything else.
1130      */
1131     data->set.cookiesession = (0 != va_arg(param, long))?TRUE:FALSE;
1132     break;
1133
1134   case CURLOPT_COOKIELIST:
1135     argptr = va_arg(param, char *);
1136
1137     if(argptr == NULL)
1138       break;
1139
1140     Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
1141
1142     if(Curl_raw_equal(argptr, "ALL")) {
1143       /* clear all cookies */
1144       Curl_cookie_clearall(data->cookies);
1145     }
1146     else if(Curl_raw_equal(argptr, "SESS")) {
1147       /* clear session cookies */
1148       Curl_cookie_clearsess(data->cookies);
1149     }
1150     else if(Curl_raw_equal(argptr, "FLUSH")) {
1151       /* flush cookies to file */
1152       Curl_flush_cookies(data, 0);
1153     }
1154     else {
1155       if(!data->cookies)
1156         /* if cookie engine was not running, activate it */
1157         data->cookies = Curl_cookie_init(data, NULL, NULL, TRUE);
1158
1159       argptr = strdup(argptr);
1160       if(!argptr) {
1161         result = CURLE_OUT_OF_MEMORY;
1162       }
1163       else {
1164
1165         if(checkprefix("Set-Cookie:", argptr))
1166           /* HTTP Header format line */
1167           Curl_cookie_add(data, data->cookies, TRUE, argptr + 11, NULL, NULL);
1168
1169         else
1170           /* Netscape format line */
1171           Curl_cookie_add(data, data->cookies, FALSE, argptr, NULL, NULL);
1172
1173         free(argptr);
1174       }
1175     }
1176     Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
1177
1178     break;
1179 #endif /* CURL_DISABLE_COOKIES */
1180
1181   case CURLOPT_HTTPGET:
1182     /*
1183      * Set to force us do HTTP GET
1184      */
1185     if(va_arg(param, long)) {
1186       data->set.httpreq = HTTPREQ_GET;
1187       data->set.upload = FALSE; /* switch off upload */
1188       data->set.opt_no_body = FALSE; /* this is implied */
1189     }
1190     break;
1191
1192   case CURLOPT_HTTP_VERSION:
1193     /*
1194      * This sets a requested HTTP version to be used. The value is one of
1195      * the listed enums in curl/curl.h.
1196      */
1197     data->set.httpversion = va_arg(param, long);
1198     break;
1199
1200   case CURLOPT_HTTPAUTH:
1201     /*
1202      * Set HTTP Authentication type BITMASK.
1203      */
1204   {
1205     int bitcheck;
1206     bool authbits;
1207     unsigned long auth = va_arg(param, unsigned long);
1208
1209     if(auth == CURLAUTH_NONE) {
1210       data->set.httpauth = auth;
1211       break;
1212     }
1213
1214     /* the DIGEST_IE bit is only used to set a special marker, for all the
1215        rest we need to handle it as normal DIGEST */
1216     data->state.authhost.iestyle = (auth & CURLAUTH_DIGEST_IE)?TRUE:FALSE;
1217
1218     if(auth & CURLAUTH_DIGEST_IE) {
1219       auth |= CURLAUTH_DIGEST; /* set standard digest bit */
1220       auth &= ~CURLAUTH_DIGEST_IE; /* unset ie digest bit */
1221     }
1222
1223     /* switch off bits we can't support */
1224 #ifndef USE_NTLM
1225     auth &= ~CURLAUTH_NTLM;    /* no NTLM support */
1226     auth &= ~CURLAUTH_NTLM_WB; /* no NTLM_WB support */
1227 #elif !defined(NTLM_WB_ENABLED)
1228     auth &= ~CURLAUTH_NTLM_WB; /* no NTLM_WB support */
1229 #endif
1230 #ifndef USE_HTTP_NEGOTIATE
1231     auth &= ~CURLAUTH_GSSNEGOTIATE; /* no GSS-Negotiate without GSSAPI or
1232                                        WINDOWS_SSPI */
1233 #endif
1234
1235     /* check if any auth bit lower than CURLAUTH_ONLY is still set */
1236     bitcheck = 0;
1237     authbits = FALSE;
1238     while(bitcheck < 31) {
1239       if(auth & (1UL << bitcheck++)) {
1240         authbits = TRUE;
1241         break;
1242       }
1243     }
1244     if(!authbits)
1245       return CURLE_NOT_BUILT_IN; /* no supported types left! */
1246
1247     data->set.httpauth = auth;
1248   }
1249   break;
1250
1251 #endif   /* CURL_DISABLE_HTTP */
1252
1253   case CURLOPT_CUSTOMREQUEST:
1254     /*
1255      * Set a custom string to use as request
1256      */
1257     result = setstropt(&data->set.str[STRING_CUSTOMREQUEST],
1258                        va_arg(param, char *));
1259
1260     /* we don't set
1261        data->set.httpreq = HTTPREQ_CUSTOM;
1262        here, we continue as if we were using the already set type
1263        and this just changes the actual request keyword */
1264     break;
1265
1266 #ifndef CURL_DISABLE_PROXY
1267   case CURLOPT_HTTPPROXYTUNNEL:
1268     /*
1269      * Tunnel operations through the proxy instead of normal proxy use
1270      */
1271     data->set.tunnel_thru_httpproxy = (0 != va_arg(param, long))?TRUE:FALSE;
1272     break;
1273
1274   case CURLOPT_PROXYPORT:
1275     /*
1276      * Explicitly set HTTP proxy port number.
1277      */
1278     data->set.proxyport = va_arg(param, long);
1279     break;
1280
1281   case CURLOPT_PROXYAUTH:
1282     /*
1283      * Set HTTP Authentication type BITMASK.
1284      */
1285   {
1286     int bitcheck;
1287     bool authbits;
1288     unsigned long auth = va_arg(param, unsigned long);
1289
1290     if(auth == CURLAUTH_NONE) {
1291       data->set.proxyauth = auth;
1292       break;
1293     }
1294
1295     /* the DIGEST_IE bit is only used to set a special marker, for all the
1296        rest we need to handle it as normal DIGEST */
1297     data->state.authproxy.iestyle = (auth & CURLAUTH_DIGEST_IE)?TRUE:FALSE;
1298
1299     if(auth & CURLAUTH_DIGEST_IE) {
1300       auth |= CURLAUTH_DIGEST; /* set standard digest bit */
1301       auth &= ~CURLAUTH_DIGEST_IE; /* unset ie digest bit */
1302     }
1303     /* switch off bits we can't support */
1304 #ifndef USE_NTLM
1305     auth &= ~CURLAUTH_NTLM;    /* no NTLM support */
1306     auth &= ~CURLAUTH_NTLM_WB; /* no NTLM_WB support */
1307 #elif !defined(NTLM_WB_ENABLED)
1308     auth &= ~CURLAUTH_NTLM_WB; /* no NTLM_WB support */
1309 #endif
1310 #ifndef USE_HTTP_NEGOTIATE
1311     auth &= ~CURLAUTH_GSSNEGOTIATE; /* no GSS-Negotiate without GSSAPI or
1312                                        WINDOWS_SSPI */
1313 #endif
1314
1315     /* check if any auth bit lower than CURLAUTH_ONLY is still set */
1316     bitcheck = 0;
1317     authbits = FALSE;
1318     while(bitcheck < 31) {
1319       if(auth & (1UL << bitcheck++)) {
1320         authbits = TRUE;
1321         break;
1322       }
1323     }
1324     if(!authbits)
1325       return CURLE_NOT_BUILT_IN; /* no supported types left! */
1326
1327     data->set.proxyauth = auth;
1328   }
1329   break;
1330
1331   case CURLOPT_PROXY:
1332     /*
1333      * Set proxy server:port to use as HTTP proxy.
1334      *
1335      * If the proxy is set to "" we explicitly say that we don't want to use a
1336      * proxy (even though there might be environment variables saying so).
1337      *
1338      * Setting it to NULL, means no proxy but allows the environment variables
1339      * to decide for us.
1340      */
1341     result = setstropt(&data->set.str[STRING_PROXY],
1342                        va_arg(param, char *));
1343     break;
1344
1345   case CURLOPT_PROXYTYPE:
1346     /*
1347      * Set proxy type. HTTP/HTTP_1_0/SOCKS4/SOCKS4a/SOCKS5/SOCKS5_HOSTNAME
1348      */
1349     data->set.proxytype = (curl_proxytype)va_arg(param, long);
1350     break;
1351
1352   case CURLOPT_PROXY_TRANSFER_MODE:
1353     /*
1354      * set transfer mode (;type=<a|i>) when doing FTP via an HTTP proxy
1355      */
1356     switch (va_arg(param, long)) {
1357     case 0:
1358       data->set.proxy_transfer_mode = FALSE;
1359       break;
1360     case 1:
1361       data->set.proxy_transfer_mode = TRUE;
1362       break;
1363     default:
1364       /* reserve other values for future use */
1365       result = CURLE_UNKNOWN_OPTION;
1366       break;
1367     }
1368     break;
1369 #endif   /* CURL_DISABLE_PROXY */
1370
1371 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1372   case CURLOPT_SOCKS5_GSSAPI_SERVICE:
1373     /*
1374      * Set gssapi service name
1375      */
1376     result = setstropt(&data->set.str[STRING_SOCKS5_GSSAPI_SERVICE],
1377                        va_arg(param, char *));
1378     break;
1379
1380   case CURLOPT_SOCKS5_GSSAPI_NEC:
1381     /*
1382      * set flag for nec socks5 support
1383      */
1384     data->set.socks5_gssapi_nec = (0 != va_arg(param, long))?TRUE:FALSE;
1385     break;
1386 #endif
1387
1388   case CURLOPT_WRITEHEADER:
1389     /*
1390      * Custom pointer to pass the header write callback function
1391      */
1392     data->set.writeheader = (void *)va_arg(param, void *);
1393     break;
1394   case CURLOPT_ERRORBUFFER:
1395     /*
1396      * Error buffer provided by the caller to get the human readable
1397      * error string in.
1398      */
1399     data->set.errorbuffer = va_arg(param, char *);
1400     break;
1401   case CURLOPT_FILE:
1402     /*
1403      * FILE pointer to write to. Or possibly
1404      * used as argument to the write callback.
1405      */
1406     data->set.out = va_arg(param, void *);
1407     break;
1408   case CURLOPT_FTPPORT:
1409     /*
1410      * Use FTP PORT, this also specifies which IP address to use
1411      */
1412     result = setstropt(&data->set.str[STRING_FTPPORT],
1413                        va_arg(param, char *));
1414     data->set.ftp_use_port = (NULL != data->set.str[STRING_FTPPORT]) ?
1415                              TRUE:FALSE;
1416     break;
1417
1418   case CURLOPT_FTP_USE_EPRT:
1419     data->set.ftp_use_eprt = (0 != va_arg(param, long))?TRUE:FALSE;
1420     break;
1421
1422   case CURLOPT_FTP_USE_EPSV:
1423     data->set.ftp_use_epsv = (0 != va_arg(param, long))?TRUE:FALSE;
1424     break;
1425
1426   case CURLOPT_FTP_USE_PRET:
1427     data->set.ftp_use_pret = (0 != va_arg(param, long))?TRUE:FALSE;
1428     break;
1429
1430   case CURLOPT_FTP_SSL_CCC:
1431     data->set.ftp_ccc = (curl_ftpccc)va_arg(param, long);
1432     break;
1433
1434   case CURLOPT_FTP_SKIP_PASV_IP:
1435     /*
1436      * Enable or disable FTP_SKIP_PASV_IP, which will disable/enable the
1437      * bypass of the IP address in PASV responses.
1438      */
1439     data->set.ftp_skip_ip = (0 != va_arg(param, long))?TRUE:FALSE;
1440     break;
1441
1442   case CURLOPT_INFILE:
1443     /*
1444      * FILE pointer to read the file to be uploaded from. Or possibly
1445      * used as argument to the read callback.
1446      */
1447     data->set.in = va_arg(param, void *);
1448     break;
1449   case CURLOPT_INFILESIZE:
1450     /*
1451      * If known, this should inform curl about the file size of the
1452      * to-be-uploaded file.
1453      */
1454     data->set.infilesize = va_arg(param, long);
1455     break;
1456   case CURLOPT_INFILESIZE_LARGE:
1457     /*
1458      * If known, this should inform curl about the file size of the
1459      * to-be-uploaded file.
1460      */
1461     data->set.infilesize = va_arg(param, curl_off_t);
1462     break;
1463   case CURLOPT_LOW_SPEED_LIMIT:
1464     /*
1465      * The low speed limit that if transfers are below this for
1466      * CURLOPT_LOW_SPEED_TIME, the transfer is aborted.
1467      */
1468     data->set.low_speed_limit=va_arg(param, long);
1469     break;
1470   case CURLOPT_MAX_SEND_SPEED_LARGE:
1471     /*
1472      * When transfer uploads are faster then CURLOPT_MAX_SEND_SPEED_LARGE
1473      * bytes per second the transfer is throttled..
1474      */
1475     data->set.max_send_speed=va_arg(param, curl_off_t);
1476     break;
1477   case CURLOPT_MAX_RECV_SPEED_LARGE:
1478     /*
1479      * When receiving data faster than CURLOPT_MAX_RECV_SPEED_LARGE bytes per
1480      * second the transfer is throttled..
1481      */
1482     data->set.max_recv_speed=va_arg(param, curl_off_t);
1483     break;
1484   case CURLOPT_LOW_SPEED_TIME:
1485     /*
1486      * The low speed time that if transfers are below the set
1487      * CURLOPT_LOW_SPEED_LIMIT during this time, the transfer is aborted.
1488      */
1489     data->set.low_speed_time=va_arg(param, long);
1490     break;
1491   case CURLOPT_URL:
1492     /*
1493      * The URL to fetch.
1494      */
1495     if(data->change.url_alloc) {
1496       /* the already set URL is allocated, free it first! */
1497       Curl_safefree(data->change.url);
1498       data->change.url_alloc = FALSE;
1499     }
1500     result = setstropt(&data->set.str[STRING_SET_URL],
1501                        va_arg(param, char *));
1502     data->change.url = data->set.str[STRING_SET_URL];
1503     break;
1504   case CURLOPT_PORT:
1505     /*
1506      * The port number to use when getting the URL
1507      */
1508     data->set.use_port = va_arg(param, long);
1509     break;
1510   case CURLOPT_TIMEOUT:
1511     /*
1512      * The maximum time you allow curl to use for a single transfer
1513      * operation.
1514      */
1515     data->set.timeout = va_arg(param, long) * 1000L;
1516     break;
1517
1518   case CURLOPT_TIMEOUT_MS:
1519     data->set.timeout = va_arg(param, long);
1520     break;
1521
1522   case CURLOPT_CONNECTTIMEOUT:
1523     /*
1524      * The maximum time you allow curl to use to connect.
1525      */
1526     data->set.connecttimeout = va_arg(param, long) * 1000L;
1527     break;
1528
1529   case CURLOPT_CONNECTTIMEOUT_MS:
1530     data->set.connecttimeout = va_arg(param, long);
1531     break;
1532
1533   case CURLOPT_ACCEPTTIMEOUT_MS:
1534     /*
1535      * The maximum time you allow curl to wait for server connect
1536      */
1537     data->set.accepttimeout = va_arg(param, long);
1538     break;
1539
1540   case CURLOPT_USERPWD:
1541     /*
1542      * user:password;options to use in the operation
1543      */
1544     result = setstropt_userpwd(va_arg(param, char *),
1545                                &data->set.str[STRING_USERNAME],
1546                                &data->set.str[STRING_PASSWORD],
1547                                &data->set.str[STRING_OPTIONS]);
1548     break;
1549   case CURLOPT_USERNAME:
1550     /*
1551      * authentication user name to use in the operation
1552      */
1553     result = setstropt(&data->set.str[STRING_USERNAME],
1554                        va_arg(param, char *));
1555     break;
1556   case CURLOPT_PASSWORD:
1557     /*
1558      * authentication password to use in the operation
1559      */
1560     result = setstropt(&data->set.str[STRING_PASSWORD],
1561                        va_arg(param, char *));
1562     break;
1563   case CURLOPT_POSTQUOTE:
1564     /*
1565      * List of RAW FTP commands to use after a transfer
1566      */
1567     data->set.postquote = va_arg(param, struct curl_slist *);
1568     break;
1569   case CURLOPT_PREQUOTE:
1570     /*
1571      * List of RAW FTP commands to use prior to RETR (Wesley Laxton)
1572      */
1573     data->set.prequote = va_arg(param, struct curl_slist *);
1574     break;
1575   case CURLOPT_QUOTE:
1576     /*
1577      * List of RAW FTP commands to use before a transfer
1578      */
1579     data->set.quote = va_arg(param, struct curl_slist *);
1580     break;
1581   case CURLOPT_RESOLVE:
1582     /*
1583      * List of NAME:[address] names to populate the DNS cache with
1584      * Prefix the NAME with dash (-) to _remove_ the name from the cache.
1585      *
1586      * Names added with this API will remain in the cache until explicitly
1587      * removed or the handle is cleaned up.
1588      *
1589      * This API can remove any name from the DNS cache, but only entries
1590      * that aren't actually in use right now will be pruned immediately.
1591      */
1592     data->set.resolve = va_arg(param, struct curl_slist *);
1593     data->change.resolve = data->set.resolve;
1594     break;
1595   case CURLOPT_PROGRESSFUNCTION:
1596     /*
1597      * Progress callback function
1598      */
1599     data->set.fprogress = va_arg(param, curl_progress_callback);
1600     if(data->set.fprogress)
1601       data->progress.callback = TRUE; /* no longer internal */
1602     else
1603       data->progress.callback = FALSE; /* NULL enforces internal */
1604
1605     break;
1606   case CURLOPT_PROGRESSDATA:
1607     /*
1608      * Custom client data to pass to the progress callback
1609      */
1610     data->set.progress_client = va_arg(param, void *);
1611     break;
1612
1613 #ifndef CURL_DISABLE_PROXY
1614   case CURLOPT_PROXYUSERPWD:
1615     /*
1616      * user:password needed to use the proxy
1617      */
1618     result = setstropt_userpwd(va_arg(param, char *),
1619                                &data->set.str[STRING_PROXYUSERNAME],
1620                                &data->set.str[STRING_PROXYPASSWORD], NULL);
1621     break;
1622   case CURLOPT_PROXYUSERNAME:
1623     /*
1624      * authentication user name to use in the operation
1625      */
1626     result = setstropt(&data->set.str[STRING_PROXYUSERNAME],
1627                        va_arg(param, char *));
1628     break;
1629   case CURLOPT_PROXYPASSWORD:
1630     /*
1631      * authentication password to use in the operation
1632      */
1633     result = setstropt(&data->set.str[STRING_PROXYPASSWORD],
1634                        va_arg(param, char *));
1635     break;
1636   case CURLOPT_NOPROXY:
1637     /*
1638      * proxy exception list
1639      */
1640     result = setstropt(&data->set.str[STRING_NOPROXY],
1641                        va_arg(param, char *));
1642     break;
1643 #endif
1644
1645   case CURLOPT_RANGE:
1646     /*
1647      * What range of the file you want to transfer
1648      */
1649     result = setstropt(&data->set.str[STRING_SET_RANGE],
1650                        va_arg(param, char *));
1651     break;
1652   case CURLOPT_RESUME_FROM:
1653     /*
1654      * Resume transfer at the give file position
1655      */
1656     data->set.set_resume_from = va_arg(param, long);
1657     break;
1658   case CURLOPT_RESUME_FROM_LARGE:
1659     /*
1660      * Resume transfer at the give file position
1661      */
1662     data->set.set_resume_from = va_arg(param, curl_off_t);
1663     break;
1664   case CURLOPT_DEBUGFUNCTION:
1665     /*
1666      * stderr write callback.
1667      */
1668     data->set.fdebug = va_arg(param, curl_debug_callback);
1669     /*
1670      * if the callback provided is NULL, it'll use the default callback
1671      */
1672     break;
1673   case CURLOPT_DEBUGDATA:
1674     /*
1675      * Set to a void * that should receive all error writes. This
1676      * defaults to CURLOPT_STDERR for normal operations.
1677      */
1678     data->set.debugdata = va_arg(param, void *);
1679     break;
1680   case CURLOPT_STDERR:
1681     /*
1682      * Set to a FILE * that should receive all error writes. This
1683      * defaults to stderr for normal operations.
1684      */
1685     data->set.err = va_arg(param, FILE *);
1686     if(!data->set.err)
1687       data->set.err = stderr;
1688     break;
1689   case CURLOPT_HEADERFUNCTION:
1690     /*
1691      * Set header write callback
1692      */
1693     data->set.fwrite_header = va_arg(param, curl_write_callback);
1694     break;
1695   case CURLOPT_WRITEFUNCTION:
1696     /*
1697      * Set data write callback
1698      */
1699     data->set.fwrite_func = va_arg(param, curl_write_callback);
1700     if(!data->set.fwrite_func) {
1701       data->set.is_fwrite_set = 0;
1702       /* When set to NULL, reset to our internal default function */
1703       data->set.fwrite_func = (curl_write_callback)fwrite;
1704     }
1705     else
1706       data->set.is_fwrite_set = 1;
1707     break;
1708   case CURLOPT_READFUNCTION:
1709     /*
1710      * Read data callback
1711      */
1712     data->set.fread_func = va_arg(param, curl_read_callback);
1713     if(!data->set.fread_func) {
1714       data->set.is_fread_set = 0;
1715       /* When set to NULL, reset to our internal default function */
1716       data->set.fread_func = (curl_read_callback)fread;
1717     }
1718     else
1719       data->set.is_fread_set = 1;
1720     break;
1721   case CURLOPT_SEEKFUNCTION:
1722     /*
1723      * Seek callback. Might be NULL.
1724      */
1725     data->set.seek_func = va_arg(param, curl_seek_callback);
1726     break;
1727   case CURLOPT_SEEKDATA:
1728     /*
1729      * Seek control callback. Might be NULL.
1730      */
1731     data->set.seek_client = va_arg(param, void *);
1732     break;
1733   case CURLOPT_CONV_FROM_NETWORK_FUNCTION:
1734     /*
1735      * "Convert from network encoding" callback
1736      */
1737     data->set.convfromnetwork = va_arg(param, curl_conv_callback);
1738     break;
1739   case CURLOPT_CONV_TO_NETWORK_FUNCTION:
1740     /*
1741      * "Convert to network encoding" callback
1742      */
1743     data->set.convtonetwork = va_arg(param, curl_conv_callback);
1744     break;
1745   case CURLOPT_CONV_FROM_UTF8_FUNCTION:
1746     /*
1747      * "Convert from UTF-8 encoding" callback
1748      */
1749     data->set.convfromutf8 = va_arg(param, curl_conv_callback);
1750     break;
1751   case CURLOPT_IOCTLFUNCTION:
1752     /*
1753      * I/O control callback. Might be NULL.
1754      */
1755     data->set.ioctl_func = va_arg(param, curl_ioctl_callback);
1756     break;
1757   case CURLOPT_IOCTLDATA:
1758     /*
1759      * I/O control data pointer. Might be NULL.
1760      */
1761     data->set.ioctl_client = va_arg(param, void *);
1762     break;
1763   case CURLOPT_SSLCERT:
1764     /*
1765      * String that holds file name of the SSL certificate to use
1766      */
1767     result = setstropt(&data->set.str[STRING_CERT],
1768                        va_arg(param, char *));
1769     break;
1770   case CURLOPT_SSLCERTTYPE:
1771     /*
1772      * String that holds file type of the SSL certificate to use
1773      */
1774     result = setstropt(&data->set.str[STRING_CERT_TYPE],
1775                        va_arg(param, char *));
1776     break;
1777   case CURLOPT_SSLKEY:
1778     /*
1779      * String that holds file name of the SSL key to use
1780      */
1781     result = setstropt(&data->set.str[STRING_KEY],
1782                        va_arg(param, char *));
1783     break;
1784   case CURLOPT_SSLKEYTYPE:
1785     /*
1786      * String that holds file type of the SSL key to use
1787      */
1788     result = setstropt(&data->set.str[STRING_KEY_TYPE],
1789                        va_arg(param, char *));
1790     break;
1791   case CURLOPT_KEYPASSWD:
1792     /*
1793      * String that holds the SSL or SSH private key password.
1794      */
1795     result = setstropt(&data->set.str[STRING_KEY_PASSWD],
1796                        va_arg(param, char *));
1797     break;
1798   case CURLOPT_SSLENGINE:
1799     /*
1800      * String that holds the SSL crypto engine.
1801      */
1802     argptr = va_arg(param, char *);
1803     if(argptr && argptr[0])
1804       result = Curl_ssl_set_engine(data, argptr);
1805     break;
1806
1807   case CURLOPT_SSLENGINE_DEFAULT:
1808     /*
1809      * flag to set engine as default.
1810      */
1811     result = Curl_ssl_set_engine_default(data);
1812     break;
1813   case CURLOPT_CRLF:
1814     /*
1815      * Kludgy option to enable CRLF conversions. Subject for removal.
1816      */
1817     data->set.crlf = (0 != va_arg(param, long))?TRUE:FALSE;
1818     break;
1819
1820   case CURLOPT_INTERFACE:
1821     /*
1822      * Set what interface or address/hostname to bind the socket to when
1823      * performing an operation and thus what from-IP your connection will use.
1824      */
1825     result = setstropt(&data->set.str[STRING_DEVICE],
1826                        va_arg(param, char *));
1827     break;
1828   case CURLOPT_LOCALPORT:
1829     /*
1830      * Set what local port to bind the socket to when performing an operation.
1831      */
1832     data->set.localport = curlx_sltous(va_arg(param, long));
1833     break;
1834   case CURLOPT_LOCALPORTRANGE:
1835     /*
1836      * Set number of local ports to try, starting with CURLOPT_LOCALPORT.
1837      */
1838     data->set.localportrange = curlx_sltosi(va_arg(param, long));
1839     break;
1840   case CURLOPT_KRBLEVEL:
1841     /*
1842      * A string that defines the kerberos security level.
1843      */
1844     result = setstropt(&data->set.str[STRING_KRB_LEVEL],
1845                        va_arg(param, char *));
1846     data->set.krb = (NULL != data->set.str[STRING_KRB_LEVEL])?TRUE:FALSE;
1847     break;
1848   case CURLOPT_GSSAPI_DELEGATION:
1849     /*
1850      * GSSAPI credential delegation
1851      */
1852     data->set.gssapi_delegation = va_arg(param, long);
1853     break;
1854   case CURLOPT_SSL_VERIFYPEER:
1855     /*
1856      * Enable peer SSL verifying.
1857      */
1858     data->set.ssl.verifypeer = (0 != va_arg(param, long))?TRUE:FALSE;
1859     break;
1860   case CURLOPT_SSL_VERIFYHOST:
1861     /*
1862      * Enable verification of the host name in the peer certificate
1863      */
1864     arg = va_arg(param, long);
1865
1866     /* Obviously people are not reading documentation and too many thought
1867        this argument took a boolean when it wasn't and misused it. We thus ban
1868        1 as a sensible input and we warn about its use. Then we only have the
1869        2 action internally stored as TRUE. */
1870
1871     if(1 == arg) {
1872       failf(data, "CURLOPT_SSL_VERIFYHOST no longer supports 1 as value!");
1873       return CURLE_BAD_FUNCTION_ARGUMENT;
1874     }
1875
1876     data->set.ssl.verifyhost = (0 != arg)?TRUE:FALSE;
1877     break;
1878 #ifdef USE_SSLEAY
1879     /* since these two options are only possible to use on an OpenSSL-
1880        powered libcurl we #ifdef them on this condition so that libcurls
1881        built against other SSL libs will return a proper error when trying
1882        to set this option! */
1883   case CURLOPT_SSL_CTX_FUNCTION:
1884     /*
1885      * Set a SSL_CTX callback
1886      */
1887     data->set.ssl.fsslctx = va_arg(param, curl_ssl_ctx_callback);
1888     break;
1889   case CURLOPT_SSL_CTX_DATA:
1890     /*
1891      * Set a SSL_CTX callback parameter pointer
1892      */
1893     data->set.ssl.fsslctxp = va_arg(param, void *);
1894     break;
1895   case CURLOPT_CERTINFO:
1896     data->set.ssl.certinfo = (0 != va_arg(param, long))?TRUE:FALSE;
1897     break;
1898 #endif
1899   case CURLOPT_CAINFO:
1900     /*
1901      * Set CA info for SSL connection. Specify file name of the CA certificate
1902      */
1903     result = setstropt(&data->set.str[STRING_SSL_CAFILE],
1904                        va_arg(param, char *));
1905     break;
1906   case CURLOPT_CAPATH:
1907     /*
1908      * Set CA path info for SSL connection. Specify directory name of the CA
1909      * certificates which have been prepared using openssl c_rehash utility.
1910      */
1911     /* This does not work on windows. */
1912     result = setstropt(&data->set.str[STRING_SSL_CAPATH],
1913                        va_arg(param, char *));
1914     break;
1915   case CURLOPT_CRLFILE:
1916     /*
1917      * Set CRL file info for SSL connection. Specify file name of the CRL
1918      * to check certificates revocation
1919      */
1920     result = setstropt(&data->set.str[STRING_SSL_CRLFILE],
1921                        va_arg(param, char *));
1922     break;
1923   case CURLOPT_ISSUERCERT:
1924     /*
1925      * Set Issuer certificate file
1926      * to check certificates issuer
1927      */
1928     result = setstropt(&data->set.str[STRING_SSL_ISSUERCERT],
1929                        va_arg(param, char *));
1930     break;
1931   case CURLOPT_TELNETOPTIONS:
1932     /*
1933      * Set a linked list of telnet options
1934      */
1935     data->set.telnet_options = va_arg(param, struct curl_slist *);
1936     break;
1937
1938   case CURLOPT_BUFFERSIZE:
1939     /*
1940      * The application kindly asks for a differently sized receive buffer.
1941      * If it seems reasonable, we'll use it.
1942      */
1943     data->set.buffer_size = va_arg(param, long);
1944
1945     if((data->set.buffer_size> (BUFSIZE -1 )) ||
1946        (data->set.buffer_size < 1))
1947       data->set.buffer_size = 0; /* huge internal default */
1948
1949     break;
1950
1951   case CURLOPT_NOSIGNAL:
1952     /*
1953      * The application asks not to set any signal() or alarm() handlers,
1954      * even when using a timeout.
1955      */
1956     data->set.no_signal = (0 != va_arg(param, long))?TRUE:FALSE;
1957     break;
1958
1959   case CURLOPT_SHARE:
1960   {
1961     struct Curl_share *set;
1962     set = va_arg(param, struct Curl_share *);
1963
1964     /* disconnect from old share, if any */
1965     if(data->share) {
1966       Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
1967
1968       if(data->dns.hostcachetype == HCACHE_SHARED) {
1969         data->dns.hostcache = NULL;
1970         data->dns.hostcachetype = HCACHE_NONE;
1971       }
1972
1973 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
1974       if(data->share->cookies == data->cookies)
1975         data->cookies = NULL;
1976 #endif
1977
1978       if(data->share->sslsession == data->state.session)
1979         data->state.session = NULL;
1980
1981       data->share->dirty--;
1982
1983       Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
1984       data->share = NULL;
1985     }
1986
1987     /* use new share if it set */
1988     data->share = set;
1989     if(data->share) {
1990
1991       Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
1992
1993       data->share->dirty++;
1994
1995       if(data->share->hostcache) {
1996         /* use shared host cache */
1997         data->dns.hostcache = data->share->hostcache;
1998         data->dns.hostcachetype = HCACHE_SHARED;
1999       }
2000 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
2001       if(data->share->cookies) {
2002         /* use shared cookie list, first free own one if any */
2003         if(data->cookies)
2004           Curl_cookie_cleanup(data->cookies);
2005         /* enable cookies since we now use a share that uses cookies! */
2006         data->cookies = data->share->cookies;
2007       }
2008 #endif   /* CURL_DISABLE_HTTP */
2009       if(data->share->sslsession) {
2010         data->set.ssl.max_ssl_sessions = data->share->max_ssl_sessions;
2011         data->state.session = data->share->sslsession;
2012       }
2013       Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
2014
2015     }
2016     /* check for host cache not needed,
2017      * it will be done by curl_easy_perform */
2018   }
2019   break;
2020
2021   case CURLOPT_PRIVATE:
2022     /*
2023      * Set private data pointer.
2024      */
2025     data->set.private_data = va_arg(param, void *);
2026     break;
2027
2028   case CURLOPT_MAXFILESIZE:
2029     /*
2030      * Set the maximum size of a file to download.
2031      */
2032     data->set.max_filesize = va_arg(param, long);
2033     break;
2034
2035 #ifdef USE_SSL
2036   case CURLOPT_USE_SSL:
2037     /*
2038      * Make transfers attempt to use SSL/TLS.
2039      */
2040     data->set.use_ssl = (curl_usessl)va_arg(param, long);
2041     break;
2042
2043   case CURLOPT_SSL_OPTIONS:
2044     arg = va_arg(param, long);
2045     data->set.ssl_enable_beast = arg&CURLSSLOPT_ALLOW_BEAST?TRUE:FALSE;
2046     break;
2047
2048 #endif
2049   case CURLOPT_FTPSSLAUTH:
2050     /*
2051      * Set a specific auth for FTP-SSL transfers.
2052      */
2053     data->set.ftpsslauth = (curl_ftpauth)va_arg(param, long);
2054     break;
2055
2056   case CURLOPT_IPRESOLVE:
2057     data->set.ipver = va_arg(param, long);
2058     break;
2059
2060   case CURLOPT_MAXFILESIZE_LARGE:
2061     /*
2062      * Set the maximum size of a file to download.
2063      */
2064     data->set.max_filesize = va_arg(param, curl_off_t);
2065     break;
2066
2067   case CURLOPT_TCP_NODELAY:
2068     /*
2069      * Enable or disable TCP_NODELAY, which will disable/enable the Nagle
2070      * algorithm
2071      */
2072     data->set.tcp_nodelay = (0 != va_arg(param, long))?TRUE:FALSE;
2073     break;
2074
2075   case CURLOPT_FTP_ACCOUNT:
2076     result = setstropt(&data->set.str[STRING_FTP_ACCOUNT],
2077                        va_arg(param, char *));
2078     break;
2079
2080   case CURLOPT_IGNORE_CONTENT_LENGTH:
2081     data->set.ignorecl = (0 != va_arg(param, long))?TRUE:FALSE;
2082     break;
2083
2084   case CURLOPT_CONNECT_ONLY:
2085     /*
2086      * No data transfer, set up connection and let application use the socket
2087      */
2088     data->set.connect_only = (0 != va_arg(param, long))?TRUE:FALSE;
2089     break;
2090
2091   case CURLOPT_FTP_ALTERNATIVE_TO_USER:
2092     result = setstropt(&data->set.str[STRING_FTP_ALTERNATIVE_TO_USER],
2093                        va_arg(param, char *));
2094     break;
2095
2096   case CURLOPT_SOCKOPTFUNCTION:
2097     /*
2098      * socket callback function: called after socket() but before connect()
2099      */
2100     data->set.fsockopt = va_arg(param, curl_sockopt_callback);
2101     break;
2102
2103   case CURLOPT_SOCKOPTDATA:
2104     /*
2105      * socket callback data pointer. Might be NULL.
2106      */
2107     data->set.sockopt_client = va_arg(param, void *);
2108     break;
2109
2110   case CURLOPT_OPENSOCKETFUNCTION:
2111     /*
2112      * open/create socket callback function: called instead of socket(),
2113      * before connect()
2114      */
2115     data->set.fopensocket = va_arg(param, curl_opensocket_callback);
2116     break;
2117
2118   case CURLOPT_OPENSOCKETDATA:
2119     /*
2120      * socket callback data pointer. Might be NULL.
2121      */
2122     data->set.opensocket_client = va_arg(param, void *);
2123     break;
2124
2125   case CURLOPT_CLOSESOCKETFUNCTION:
2126     /*
2127      * close socket callback function: called instead of close()
2128      * when shutting down a connection
2129      */
2130     data->set.fclosesocket = va_arg(param, curl_closesocket_callback);
2131     break;
2132
2133   case CURLOPT_CLOSESOCKETDATA:
2134     /*
2135      * socket callback data pointer. Might be NULL.
2136      */
2137     data->set.closesocket_client = va_arg(param, void *);
2138     break;
2139
2140   case CURLOPT_SSL_SESSIONID_CACHE:
2141     data->set.ssl.sessionid = (0 != va_arg(param, long))?TRUE:FALSE;
2142     break;
2143
2144 #ifdef USE_LIBSSH2
2145     /* we only include SSH options if explicitly built to support SSH */
2146   case CURLOPT_SSH_AUTH_TYPES:
2147     data->set.ssh_auth_types = va_arg(param, long);
2148     break;
2149
2150   case CURLOPT_SSH_PUBLIC_KEYFILE:
2151     /*
2152      * Use this file instead of the $HOME/.ssh/id_dsa.pub file
2153      */
2154     result = setstropt(&data->set.str[STRING_SSH_PUBLIC_KEY],
2155                        va_arg(param, char *));
2156     break;
2157
2158   case CURLOPT_SSH_PRIVATE_KEYFILE:
2159     /*
2160      * Use this file instead of the $HOME/.ssh/id_dsa file
2161      */
2162     result = setstropt(&data->set.str[STRING_SSH_PRIVATE_KEY],
2163                        va_arg(param, char *));
2164     break;
2165   case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
2166     /*
2167      * Option to allow for the MD5 of the host public key to be checked
2168      * for validation purposes.
2169      */
2170     result = setstropt(&data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5],
2171                        va_arg(param, char *));
2172     break;
2173 #ifdef HAVE_LIBSSH2_KNOWNHOST_API
2174   case CURLOPT_SSH_KNOWNHOSTS:
2175     /*
2176      * Store the file name to read known hosts from.
2177      */
2178     result = setstropt(&data->set.str[STRING_SSH_KNOWNHOSTS],
2179                        va_arg(param, char *));
2180     break;
2181
2182   case CURLOPT_SSH_KEYFUNCTION:
2183     /* setting to NULL is fine since the ssh.c functions themselves will
2184        then rever to use the internal default */
2185     data->set.ssh_keyfunc = va_arg(param, curl_sshkeycallback);
2186     break;
2187
2188   case CURLOPT_SSH_KEYDATA:
2189     /*
2190      * Custom client data to pass to the SSH keyfunc callback
2191      */
2192     data->set.ssh_keyfunc_userp = va_arg(param, void *);
2193     break;
2194 #endif /* HAVE_LIBSSH2_KNOWNHOST_API */
2195
2196 #endif /* USE_LIBSSH2 */
2197
2198   case CURLOPT_HTTP_TRANSFER_DECODING:
2199     /*
2200      * disable libcurl transfer encoding is used
2201      */
2202     data->set.http_te_skip = (0 == va_arg(param, long))?TRUE:FALSE;
2203     break;
2204
2205   case CURLOPT_HTTP_CONTENT_DECODING:
2206     /*
2207      * raw data passed to the application when content encoding is used
2208      */
2209     data->set.http_ce_skip = (0 == va_arg(param, long))?TRUE:FALSE;
2210     break;
2211
2212   case CURLOPT_NEW_FILE_PERMS:
2213     /*
2214      * Uses these permissions instead of 0644
2215      */
2216     data->set.new_file_perms = va_arg(param, long);
2217     break;
2218
2219   case CURLOPT_NEW_DIRECTORY_PERMS:
2220     /*
2221      * Uses these permissions instead of 0755
2222      */
2223     data->set.new_directory_perms = va_arg(param, long);
2224     break;
2225
2226   case CURLOPT_ADDRESS_SCOPE:
2227     /*
2228      * We always get longs when passed plain numericals, but for this value we
2229      * know that an unsigned int will always hold the value so we blindly
2230      * typecast to this type
2231      */
2232     data->set.scope = curlx_sltoui(va_arg(param, long));
2233     break;
2234
2235   case CURLOPT_PROTOCOLS:
2236     /* set the bitmask for the protocols that are allowed to be used for the
2237        transfer, which thus helps the app which takes URLs from users or other
2238        external inputs and want to restrict what protocol(s) to deal
2239        with. Defaults to CURLPROTO_ALL. */
2240     data->set.allowed_protocols = va_arg(param, long);
2241     break;
2242
2243   case CURLOPT_REDIR_PROTOCOLS:
2244     /* set the bitmask for the protocols that libcurl is allowed to follow to,
2245        as a subset of the CURLOPT_PROTOCOLS ones. That means the protocol needs
2246        to be set in both bitmasks to be allowed to get redirected to. Defaults
2247        to all protocols except FILE and SCP. */
2248     data->set.redir_protocols = va_arg(param, long);
2249     break;
2250
2251   case CURLOPT_MAIL_FROM:
2252     /* Set the SMTP mail originator */
2253     result = setstropt(&data->set.str[STRING_MAIL_FROM],
2254                        va_arg(param, char *));
2255     break;
2256
2257   case CURLOPT_MAIL_AUTH:
2258     /* Set the SMTP auth originator */
2259     result = setstropt(&data->set.str[STRING_MAIL_AUTH],
2260                        va_arg(param, char *));
2261     break;
2262
2263   case CURLOPT_MAIL_RCPT:
2264     /* Set the list of mail recipients */
2265     data->set.mail_rcpt = va_arg(param, struct curl_slist *);
2266     break;
2267
2268   case CURLOPT_SASL_IR:
2269     /* Enable/disable SASL initial response */
2270     data->set.sasl_ir = (0 != va_arg(param, long)) ? TRUE : FALSE;
2271     break;
2272
2273   case CURLOPT_RTSP_REQUEST:
2274     {
2275       /*
2276        * Set the RTSP request method (OPTIONS, SETUP, PLAY, etc...)
2277        * Would this be better if the RTSPREQ_* were just moved into here?
2278        */
2279       long curl_rtspreq = va_arg(param, long);
2280       Curl_RtspReq rtspreq = RTSPREQ_NONE;
2281       switch(curl_rtspreq) {
2282         case CURL_RTSPREQ_OPTIONS:
2283           rtspreq = RTSPREQ_OPTIONS;
2284           break;
2285
2286         case CURL_RTSPREQ_DESCRIBE:
2287           rtspreq = RTSPREQ_DESCRIBE;
2288           break;
2289
2290         case CURL_RTSPREQ_ANNOUNCE:
2291           rtspreq = RTSPREQ_ANNOUNCE;
2292           break;
2293
2294         case CURL_RTSPREQ_SETUP:
2295           rtspreq = RTSPREQ_SETUP;
2296           break;
2297
2298         case CURL_RTSPREQ_PLAY:
2299           rtspreq = RTSPREQ_PLAY;
2300           break;
2301
2302         case CURL_RTSPREQ_PAUSE:
2303           rtspreq = RTSPREQ_PAUSE;
2304           break;
2305
2306         case CURL_RTSPREQ_TEARDOWN:
2307           rtspreq = RTSPREQ_TEARDOWN;
2308           break;
2309
2310         case CURL_RTSPREQ_GET_PARAMETER:
2311           rtspreq = RTSPREQ_GET_PARAMETER;
2312           break;
2313
2314         case CURL_RTSPREQ_SET_PARAMETER:
2315           rtspreq = RTSPREQ_SET_PARAMETER;
2316           break;
2317
2318         case CURL_RTSPREQ_RECORD:
2319           rtspreq = RTSPREQ_RECORD;
2320           break;
2321
2322         case CURL_RTSPREQ_RECEIVE:
2323           rtspreq = RTSPREQ_RECEIVE;
2324           break;
2325         default:
2326           rtspreq = RTSPREQ_NONE;
2327       }
2328
2329       data->set.rtspreq = rtspreq;
2330     break;
2331     }
2332
2333
2334   case CURLOPT_RTSP_SESSION_ID:
2335     /*
2336      * Set the RTSP Session ID manually. Useful if the application is
2337      * resuming a previously established RTSP session
2338      */
2339     result = setstropt(&data->set.str[STRING_RTSP_SESSION_ID],
2340                        va_arg(param, char *));
2341     break;
2342
2343   case CURLOPT_RTSP_STREAM_URI:
2344     /*
2345      * Set the Stream URI for the RTSP request. Unless the request is
2346      * for generic server options, the application will need to set this.
2347      */
2348     result = setstropt(&data->set.str[STRING_RTSP_STREAM_URI],
2349                        va_arg(param, char *));
2350     break;
2351
2352   case CURLOPT_RTSP_TRANSPORT:
2353     /*
2354      * The content of the Transport: header for the RTSP request
2355      */
2356     result = setstropt(&data->set.str[STRING_RTSP_TRANSPORT],
2357                        va_arg(param, char *));
2358     break;
2359
2360   case CURLOPT_RTSP_CLIENT_CSEQ:
2361     /*
2362      * Set the CSEQ number to issue for the next RTSP request. Useful if the
2363      * application is resuming a previously broken connection. The CSEQ
2364      * will increment from this new number henceforth.
2365      */
2366     data->state.rtsp_next_client_CSeq = va_arg(param, long);
2367     break;
2368
2369   case CURLOPT_RTSP_SERVER_CSEQ:
2370     /* Same as the above, but for server-initiated requests */
2371     data->state.rtsp_next_client_CSeq = va_arg(param, long);
2372     break;
2373
2374   case CURLOPT_INTERLEAVEDATA:
2375     data->set.rtp_out = va_arg(param, void *);
2376     break;
2377   case CURLOPT_INTERLEAVEFUNCTION:
2378     /* Set the user defined RTP write function */
2379     data->set.fwrite_rtp = va_arg(param, curl_write_callback);
2380     break;
2381
2382   case CURLOPT_WILDCARDMATCH:
2383     data->set.wildcardmatch = (0 != va_arg(param, long))?TRUE:FALSE;
2384     break;
2385   case CURLOPT_CHUNK_BGN_FUNCTION:
2386     data->set.chunk_bgn = va_arg(param, curl_chunk_bgn_callback);
2387     break;
2388   case CURLOPT_CHUNK_END_FUNCTION:
2389     data->set.chunk_end = va_arg(param, curl_chunk_end_callback);
2390     break;
2391   case CURLOPT_FNMATCH_FUNCTION:
2392     data->set.fnmatch = va_arg(param, curl_fnmatch_callback);
2393     break;
2394   case CURLOPT_CHUNK_DATA:
2395     data->wildcard.customptr = va_arg(param, void *);
2396     break;
2397   case CURLOPT_FNMATCH_DATA:
2398     data->set.fnmatch_data = va_arg(param, void *);
2399     break;
2400 #ifdef USE_TLS_SRP
2401   case CURLOPT_TLSAUTH_USERNAME:
2402     result = setstropt(&data->set.str[STRING_TLSAUTH_USERNAME],
2403                        va_arg(param, char *));
2404     if(data->set.str[STRING_TLSAUTH_USERNAME] && !data->set.ssl.authtype)
2405       data->set.ssl.authtype = CURL_TLSAUTH_SRP; /* default to SRP */
2406     break;
2407   case CURLOPT_TLSAUTH_PASSWORD:
2408     result = setstropt(&data->set.str[STRING_TLSAUTH_PASSWORD],
2409                        va_arg(param, char *));
2410     if(data->set.str[STRING_TLSAUTH_USERNAME] && !data->set.ssl.authtype)
2411       data->set.ssl.authtype = CURL_TLSAUTH_SRP; /* default to SRP */
2412     break;
2413   case CURLOPT_TLSAUTH_TYPE:
2414     if(strnequal((char *)va_arg(param, char *), "SRP", strlen("SRP")))
2415       data->set.ssl.authtype = CURL_TLSAUTH_SRP;
2416     else
2417       data->set.ssl.authtype = CURL_TLSAUTH_NONE;
2418     break;
2419 #endif
2420   case CURLOPT_DNS_SERVERS:
2421     result = Curl_set_dns_servers(data, va_arg(param, char *));
2422     break;
2423
2424   case CURLOPT_TCP_KEEPALIVE:
2425     data->set.tcp_keepalive = (0 != va_arg(param, long))?TRUE:FALSE;
2426     break;
2427   case CURLOPT_TCP_KEEPIDLE:
2428     data->set.tcp_keepidle = va_arg(param, long);
2429     break;
2430   case CURLOPT_TCP_KEEPINTVL:
2431     data->set.tcp_keepintvl = va_arg(param, long);
2432     break;
2433
2434   default:
2435     /* unknown tag and its companion, just ignore: */
2436     result = CURLE_UNKNOWN_OPTION;
2437     break;
2438   }
2439
2440   return result;
2441 }
2442
2443 static void conn_free(struct connectdata *conn)
2444 {
2445   if(!conn)
2446     return;
2447
2448   /* possible left-overs from the async name resolvers */
2449   Curl_resolver_cancel(conn);
2450
2451   /* close the SSL stuff before we close any sockets since they will/may
2452      write to the sockets */
2453   Curl_ssl_close(conn, FIRSTSOCKET);
2454   Curl_ssl_close(conn, SECONDARYSOCKET);
2455
2456   /* close possibly still open sockets */
2457   if(CURL_SOCKET_BAD != conn->sock[SECONDARYSOCKET])
2458     Curl_closesocket(conn, conn->sock[SECONDARYSOCKET]);
2459   if(CURL_SOCKET_BAD != conn->sock[FIRSTSOCKET])
2460     Curl_closesocket(conn, conn->sock[FIRSTSOCKET]);
2461
2462 #if defined(USE_NTLM) && defined(NTLM_WB_ENABLED)
2463   Curl_ntlm_wb_cleanup(conn);
2464 #endif
2465
2466   Curl_safefree(conn->user);
2467   Curl_safefree(conn->passwd);
2468   Curl_safefree(conn->options);
2469   Curl_safefree(conn->proxyuser);
2470   Curl_safefree(conn->proxypasswd);
2471   Curl_safefree(conn->allocptr.proxyuserpwd);
2472   Curl_safefree(conn->allocptr.uagent);
2473   Curl_safefree(conn->allocptr.userpwd);
2474   Curl_safefree(conn->allocptr.accept_encoding);
2475   Curl_safefree(conn->allocptr.te);
2476   Curl_safefree(conn->allocptr.rangeline);
2477   Curl_safefree(conn->allocptr.ref);
2478   Curl_safefree(conn->allocptr.host);
2479   Curl_safefree(conn->allocptr.cookiehost);
2480   Curl_safefree(conn->allocptr.rtsp_transport);
2481   Curl_safefree(conn->trailer);
2482   Curl_safefree(conn->host.rawalloc); /* host name buffer */
2483   Curl_safefree(conn->proxy.rawalloc); /* proxy name buffer */
2484   Curl_safefree(conn->master_buffer);
2485
2486   Curl_llist_destroy(conn->send_pipe, NULL);
2487   Curl_llist_destroy(conn->recv_pipe, NULL);
2488
2489   conn->send_pipe = NULL;
2490   conn->recv_pipe = NULL;
2491
2492   Curl_safefree(conn->localdev);
2493   Curl_free_ssl_config(&conn->ssl_config);
2494
2495   free(conn); /* free all the connection oriented data */
2496 }
2497
2498 CURLcode Curl_disconnect(struct connectdata *conn, bool dead_connection)
2499 {
2500   struct SessionHandle *data;
2501   if(!conn)
2502     return CURLE_OK; /* this is closed and fine already */
2503   data = conn->data;
2504
2505   if(!data) {
2506     DEBUGF(fprintf(stderr, "DISCONNECT without easy handle, ignoring\n"));
2507     return CURLE_OK;
2508   }
2509
2510   if(conn->dns_entry != NULL) {
2511     Curl_resolv_unlock(data, conn->dns_entry);
2512     conn->dns_entry = NULL;
2513   }
2514
2515   Curl_hostcache_prune(data); /* kill old DNS cache entries */
2516
2517   {
2518     int has_host_ntlm = (conn->ntlm.state != NTLMSTATE_NONE);
2519     int has_proxy_ntlm = (conn->proxyntlm.state != NTLMSTATE_NONE);
2520
2521     /* Authentication data is a mix of connection-related and sessionhandle-
2522        related stuff. NTLM is connection-related so when we close the shop
2523        we shall forget. */
2524
2525     if(has_host_ntlm) {
2526       data->state.authhost.done = FALSE;
2527       data->state.authhost.picked =
2528         data->state.authhost.want;
2529     }
2530
2531     if(has_proxy_ntlm) {
2532       data->state.authproxy.done = FALSE;
2533       data->state.authproxy.picked =
2534         data->state.authproxy.want;
2535     }
2536
2537     if(has_host_ntlm || has_proxy_ntlm)
2538       data->state.authproblem = FALSE;
2539   }
2540
2541   /* Cleanup NTLM connection-related data */
2542   Curl_http_ntlm_cleanup(conn);
2543
2544   /* Cleanup possible redirect junk */
2545   if(data->req.newurl) {
2546     free(data->req.newurl);
2547     data->req.newurl = NULL;
2548   }
2549
2550   if(conn->handler->disconnect)
2551     /* This is set if protocol-specific cleanups should be made */
2552     conn->handler->disconnect(conn, dead_connection);
2553
2554     /* unlink ourselves! */
2555   infof(data, "Closing connection %d\n", conn->connection_id);
2556   Curl_conncache_remove_conn(data->state.conn_cache, conn);
2557
2558 #if defined(USE_LIBIDN)
2559   if(conn->host.encalloc)
2560     idn_free(conn->host.encalloc); /* encoded host name buffer, must be freed
2561                                       with idn_free() since this was allocated
2562                                       by libidn */
2563   if(conn->proxy.encalloc)
2564     idn_free(conn->proxy.encalloc); /* encoded proxy name buffer, must be
2565                                        freed with idn_free() since this was
2566                                        allocated by libidn */
2567 #elif defined(USE_WIN32_IDN)
2568   free(conn->host.encalloc); /* encoded host name buffer, must be freed with
2569                                 idn_free() since this was allocated by
2570                                 curl_win32_idn_to_ascii */
2571   if(conn->proxy.encalloc)
2572     free(conn->proxy.encalloc); /* encoded proxy name buffer, must be freed
2573                                    with idn_free() since this was allocated by
2574                                    curl_win32_idn_to_ascii */
2575 #endif
2576
2577   Curl_ssl_close(conn, FIRSTSOCKET);
2578
2579   /* Indicate to all handles on the pipe that we're dead */
2580   if(Curl_multi_pipeline_enabled(data->multi)) {
2581     signalPipeClose(conn->send_pipe, TRUE);
2582     signalPipeClose(conn->recv_pipe, TRUE);
2583   }
2584
2585   conn_free(conn);
2586   data->state.current_conn = NULL;
2587   Curl_speedinit(data);
2588
2589   return CURLE_OK;
2590 }
2591
2592 /*
2593  * This function should return TRUE if the socket is to be assumed to
2594  * be dead. Most commonly this happens when the server has closed the
2595  * connection due to inactivity.
2596  */
2597 static bool SocketIsDead(curl_socket_t sock)
2598 {
2599   int sval;
2600   bool ret_val = TRUE;
2601
2602   sval = Curl_socket_ready(sock, CURL_SOCKET_BAD, 0);
2603   if(sval == 0)
2604     /* timeout */
2605     ret_val = FALSE;
2606
2607   return ret_val;
2608 }
2609
2610 static bool IsPipeliningPossible(const struct SessionHandle *handle,
2611                                  const struct connectdata *conn)
2612 {
2613   if((conn->handler->protocol & CURLPROTO_HTTP) &&
2614      Curl_multi_pipeline_enabled(handle->multi) &&
2615      (handle->set.httpreq == HTTPREQ_GET ||
2616       handle->set.httpreq == HTTPREQ_HEAD) &&
2617      handle->set.httpversion != CURL_HTTP_VERSION_1_0)
2618     return TRUE;
2619
2620   return FALSE;
2621 }
2622
2623 bool Curl_isPipeliningEnabled(const struct SessionHandle *handle)
2624 {
2625   return Curl_multi_pipeline_enabled(handle->multi);
2626 }
2627
2628 CURLcode Curl_addHandleToPipeline(struct SessionHandle *data,
2629                                   struct curl_llist *pipeline)
2630 {
2631   if(!Curl_llist_insert_next(pipeline, pipeline->tail, data))
2632     return CURLE_OUT_OF_MEMORY;
2633   infof(data, "Curl_addHandleToPipeline: length: %d\n", pipeline->size);
2634   return CURLE_OK;
2635 }
2636
2637 int Curl_removeHandleFromPipeline(struct SessionHandle *handle,
2638                                   struct curl_llist *pipeline)
2639 {
2640   struct curl_llist_element *curr;
2641
2642   curr = pipeline->head;
2643   while(curr) {
2644     if(curr->ptr == handle) {
2645       Curl_llist_remove(pipeline, curr, NULL);
2646       return 1; /* we removed a handle */
2647     }
2648     curr = curr->next;
2649   }
2650
2651   return 0;
2652 }
2653
2654 #if 0 /* this code is saved here as it is useful for debugging purposes */
2655 static void Curl_printPipeline(struct curl_llist *pipeline)
2656 {
2657   struct curl_llist_element *curr;
2658
2659   curr = pipeline->head;
2660   while(curr) {
2661     struct SessionHandle *data = (struct SessionHandle *) curr->ptr;
2662     infof(data, "Handle in pipeline: %s\n", data->state.path);
2663     curr = curr->next;
2664   }
2665 }
2666 #endif
2667
2668 static struct SessionHandle* gethandleathead(struct curl_llist *pipeline)
2669 {
2670   struct curl_llist_element *curr = pipeline->head;
2671   if(curr) {
2672     return (struct SessionHandle *) curr->ptr;
2673   }
2674
2675   return NULL;
2676 }
2677
2678 /* remove the specified connection from all (possible) pipelines and related
2679    queues */
2680 void Curl_getoff_all_pipelines(struct SessionHandle *data,
2681                                struct connectdata *conn)
2682 {
2683   bool recv_head = (conn->readchannel_inuse &&
2684     (gethandleathead(conn->recv_pipe) == data)) ? TRUE : FALSE;
2685
2686   bool send_head = (conn->writechannel_inuse &&
2687     (gethandleathead(conn->send_pipe) == data)) ? TRUE : FALSE;
2688
2689   if(Curl_removeHandleFromPipeline(data, conn->recv_pipe) && recv_head)
2690     conn->readchannel_inuse = FALSE;
2691   if(Curl_removeHandleFromPipeline(data, conn->send_pipe) && send_head)
2692     conn->writechannel_inuse = FALSE;
2693 }
2694
2695 static void signalPipeClose(struct curl_llist *pipeline, bool pipe_broke)
2696 {
2697   struct curl_llist_element *curr;
2698
2699   if(!pipeline)
2700     return;
2701
2702   curr = pipeline->head;
2703   while(curr) {
2704     struct curl_llist_element *next = curr->next;
2705     struct SessionHandle *data = (struct SessionHandle *) curr->ptr;
2706
2707 #ifdef DEBUGBUILD /* debug-only code */
2708     if(data->magic != CURLEASY_MAGIC_NUMBER) {
2709       /* MAJOR BADNESS */
2710       infof(data, "signalPipeClose() found BAAD easy handle\n");
2711     }
2712 #endif
2713
2714     if(pipe_broke)
2715       data->state.pipe_broke = TRUE;
2716     Curl_multi_handlePipeBreak(data);
2717     Curl_llist_remove(pipeline, curr, NULL);
2718     curr = next;
2719   }
2720 }
2721
2722 /*
2723  * This function finds the connection in the connection
2724  * cache that has been unused for the longest time.
2725  *
2726  * Returns the pointer to the oldest idle connection, or NULL if none was
2727  * found.
2728  */
2729 static struct connectdata *
2730 find_oldest_idle_connection(struct SessionHandle *data)
2731 {
2732   struct conncache *bc = data->state.conn_cache;
2733   struct curl_hash_iterator iter;
2734   struct curl_llist_element *curr;
2735   struct curl_hash_element *he;
2736   long highscore=-1;
2737   long score;
2738   struct timeval now;
2739   struct connectdata *conn_candidate = NULL;
2740   struct connectbundle *bundle;
2741
2742   now = Curl_tvnow();
2743
2744   Curl_hash_start_iterate(bc->hash, &iter);
2745
2746   he = Curl_hash_next_element(&iter);
2747   while(he) {
2748     struct connectdata *conn;
2749
2750     bundle = he->ptr;
2751
2752     curr = bundle->conn_list->head;
2753     while(curr) {
2754       conn = curr->ptr;
2755
2756       if(!conn->inuse) {
2757         /* Set higher score for the age passed since the connection was used */
2758         score = Curl_tvdiff(now, conn->now);
2759
2760         if(score > highscore) {
2761           highscore = score;
2762           conn_candidate = conn;
2763         }
2764       }
2765       curr = curr->next;
2766     }
2767
2768     he = Curl_hash_next_element(&iter);
2769   }
2770
2771   return conn_candidate;
2772 }
2773
2774 /*
2775  * This function finds the connection in the connection
2776  * bundle that has been unused for the longest time.
2777  *
2778  * Returns the pointer to the oldest idle connection, or NULL if none was
2779  * found.
2780  */
2781 static struct connectdata *
2782 find_oldest_idle_connection_in_bundle(struct SessionHandle *data,
2783                                       struct connectbundle *bundle)
2784 {
2785   struct curl_llist_element *curr;
2786   long highscore=-1;
2787   long score;
2788   struct timeval now;
2789   struct connectdata *conn_candidate = NULL;
2790   struct connectdata *conn;
2791
2792   (void)data;
2793
2794   now = Curl_tvnow();
2795
2796   curr = bundle->conn_list->head;
2797   while(curr) {
2798     conn = curr->ptr;
2799
2800     if(!conn->inuse) {
2801       /* Set higher score for the age passed since the connection was used */
2802       score = Curl_tvdiff(now, conn->now);
2803
2804       if(score > highscore) {
2805         highscore = score;
2806         conn_candidate = conn;
2807       }
2808     }
2809     curr = curr->next;
2810   }
2811
2812   return conn_candidate;
2813 }
2814
2815 /*
2816  * Given one filled in connection struct (named needle), this function should
2817  * detect if there already is one that has all the significant details
2818  * exactly the same and thus should be used instead.
2819  *
2820  * If there is a match, this function returns TRUE - and has marked the
2821  * connection as 'in-use'. It must later be called with ConnectionDone() to
2822  * return back to 'idle' (unused) state.
2823  *
2824  * The force_reuse flag is set if the connection must be used, even if
2825  * the pipelining strategy wants to open a new connection instead of reusing.
2826  */
2827 static bool
2828 ConnectionExists(struct SessionHandle *data,
2829                  struct connectdata *needle,
2830                  struct connectdata **usethis,
2831                  bool *force_reuse)
2832 {
2833   struct connectdata *check;
2834   struct connectdata *chosen = 0;
2835   bool canPipeline = IsPipeliningPossible(data, needle);
2836   bool wantNTLM = (data->state.authhost.want==CURLAUTH_NTLM) ||
2837                   (data->state.authhost.want==CURLAUTH_NTLM_WB) ? TRUE : FALSE;
2838   struct connectbundle *bundle;
2839
2840   *force_reuse = FALSE;
2841
2842   /* We can't pipe if the site is blacklisted */
2843   if(canPipeline && Curl_pipeline_site_blacklisted(data, needle)) {
2844     canPipeline = FALSE;
2845   }
2846
2847   /* Look up the bundle with all the connections to this
2848      particular host */
2849   bundle = Curl_conncache_find_bundle(data->state.conn_cache,
2850                                       needle->host.name);
2851   if(bundle) {
2852     size_t max_pipe_len = Curl_multi_max_pipeline_length(data->multi);
2853     size_t best_pipe_len = max_pipe_len;
2854     struct curl_llist_element *curr;
2855
2856     infof(data, "Found bundle for host %s: %p\n", needle->host.name, bundle);
2857
2858     /* We can't pipe if we don't know anything about the server */
2859     if(canPipeline && !bundle->server_supports_pipelining) {
2860       infof(data, "Server doesn't support pipelining\n");
2861       canPipeline = FALSE;
2862     }
2863
2864     curr = bundle->conn_list->head;
2865     while(curr) {
2866       bool match = FALSE;
2867       bool credentialsMatch = FALSE;
2868       size_t pipeLen;
2869
2870       /*
2871        * Note that if we use a HTTP proxy, we check connections to that
2872        * proxy and not to the actual remote server.
2873        */
2874       check = curr->ptr;
2875       curr = curr->next;
2876
2877       pipeLen = check->send_pipe->size + check->recv_pipe->size;
2878
2879       if(!pipeLen && !check->inuse) {
2880         /* The check for a dead socket makes sense only if there are no
2881            handles in pipeline and the connection isn't already marked in
2882            use */
2883         bool dead;
2884         if(check->handler->protocol & CURLPROTO_RTSP)
2885           /* RTSP is a special case due to RTP interleaving */
2886           dead = Curl_rtsp_connisdead(check);
2887         else
2888           dead = SocketIsDead(check->sock[FIRSTSOCKET]);
2889
2890         if(dead) {
2891           check->data = data;
2892           infof(data, "Connection %d seems to be dead!\n",
2893                 check->connection_id);
2894
2895           /* disconnect resources */
2896           Curl_disconnect(check, /* dead_connection */ TRUE);
2897           continue;
2898         }
2899       }
2900
2901       if(canPipeline) {
2902         /* Make sure the pipe has only GET requests */
2903         struct SessionHandle* sh = gethandleathead(check->send_pipe);
2904         struct SessionHandle* rh = gethandleathead(check->recv_pipe);
2905         if(sh) {
2906           if(!IsPipeliningPossible(sh, check))
2907             continue;
2908         }
2909         else if(rh) {
2910           if(!IsPipeliningPossible(rh, check))
2911             continue;
2912         }
2913       }
2914       else {
2915         if(pipeLen > 0) {
2916           /* can only happen within multi handles, and means that another easy
2917              handle is using this connection */
2918           continue;
2919         }
2920
2921         if(Curl_resolver_asynch()) {
2922           /* ip_addr_str[0] is NUL only if the resolving of the name hasn't
2923              completed yet and until then we don't re-use this connection */
2924           if(!check->ip_addr_str[0]) {
2925             infof(data,
2926                   "Connection #%ld is still name resolving, can't reuse\n",
2927                   check->connection_id);
2928             continue;
2929           }
2930         }
2931
2932         if((check->sock[FIRSTSOCKET] == CURL_SOCKET_BAD) ||
2933            check->bits.close) {
2934           /* Don't pick a connection that hasn't connected yet or that is going
2935              to get closed. */
2936           infof(data, "Connection #%ld isn't open enough, can't reuse\n",
2937                 check->connection_id);
2938 #ifdef DEBUGBUILD
2939           if(check->recv_pipe->size > 0) {
2940             infof(data,
2941                   "BAD! Unconnected #%ld has a non-empty recv pipeline!\n",
2942                   check->connection_id);
2943           }
2944 #endif
2945           continue;
2946         }
2947       }
2948
2949       if((needle->handler->flags&PROTOPT_SSL) !=
2950          (check->handler->flags&PROTOPT_SSL))
2951         /* don't do mixed SSL and non-SSL connections */
2952         if(!(needle->handler->protocol & check->handler->protocol))
2953           /* except protocols that have been upgraded via TLS */
2954           continue;
2955
2956       if(needle->handler->flags&PROTOPT_SSL) {
2957         if((data->set.ssl.verifypeer != check->verifypeer) ||
2958            (data->set.ssl.verifyhost != check->verifyhost))
2959           continue;
2960       }
2961
2962       if(needle->bits.proxy != check->bits.proxy)
2963         /* don't do mixed proxy and non-proxy connections */
2964         continue;
2965
2966       if(!canPipeline && check->inuse)
2967         /* this request can't be pipelined but the checked connection is
2968            already in use so we skip it */
2969         continue;
2970
2971       if(needle->localdev || needle->localport) {
2972         /* If we are bound to a specific local end (IP+port), we must not
2973            re-use a random other one, although if we didn't ask for a
2974            particular one we can reuse one that was bound.
2975
2976            This comparison is a bit rough and too strict. Since the input
2977            parameters can be specified in numerous ways and still end up the
2978            same it would take a lot of processing to make it really accurate.
2979            Instead, this matching will assume that re-uses of bound connections
2980            will most likely also re-use the exact same binding parameters and
2981            missing out a few edge cases shouldn't hurt anyone very much.
2982         */
2983         if((check->localport != needle->localport) ||
2984            (check->localportrange != needle->localportrange) ||
2985            !check->localdev ||
2986            !needle->localdev ||
2987            strcmp(check->localdev, needle->localdev))
2988           continue;
2989       }
2990
2991       if((needle->handler->protocol & CURLPROTO_FTP) ||
2992          ((needle->handler->protocol & CURLPROTO_HTTP) && wantNTLM)) {
2993          /* This is FTP or HTTP+NTLM, verify that we're using the same name
2994             and password as well */
2995          if(!strequal(needle->user, check->user) ||
2996             !strequal(needle->passwd, check->passwd)) {
2997             /* one of them was different */
2998             continue;
2999          }
3000          credentialsMatch = TRUE;
3001       }
3002
3003       if(!needle->bits.httpproxy || needle->handler->flags&PROTOPT_SSL ||
3004          (needle->bits.httpproxy && check->bits.httpproxy &&
3005           needle->bits.tunnel_proxy && check->bits.tunnel_proxy &&
3006           Curl_raw_equal(needle->proxy.name, check->proxy.name) &&
3007           (needle->port == check->port))) {
3008         /* The requested connection does not use a HTTP proxy or it uses SSL or
3009            it is a non-SSL protocol tunneled over the same http proxy name and
3010            port number or it is a non-SSL protocol which is allowed to be
3011            upgraded via TLS */
3012
3013         if((Curl_raw_equal(needle->handler->scheme, check->handler->scheme) ||
3014             needle->handler->protocol & check->handler->protocol) &&
3015            Curl_raw_equal(needle->host.name, check->host.name) &&
3016            needle->remote_port == check->remote_port) {
3017           if(needle->handler->flags & PROTOPT_SSL) {
3018             /* This is a SSL connection so verify that we're using the same
3019                SSL options as well */
3020             if(!Curl_ssl_config_matches(&needle->ssl_config,
3021                                         &check->ssl_config)) {
3022               DEBUGF(infof(data,
3023                            "Connection #%ld has different SSL parameters, "
3024                            "can't reuse\n",
3025                            check->connection_id));
3026               continue;
3027             }
3028             else if(check->ssl[FIRSTSOCKET].state != ssl_connection_complete) {
3029               DEBUGF(infof(data,
3030                            "Connection #%ld has not started SSL connect, "
3031                            "can't reuse\n",
3032                            check->connection_id));
3033               continue;
3034             }
3035           }
3036           match = TRUE;
3037         }
3038       }
3039       else { /* The requested needle connection is using a proxy,
3040                 is the checked one using the same host, port and type? */
3041         if(check->bits.proxy &&
3042            (needle->proxytype == check->proxytype) &&
3043            (needle->bits.tunnel_proxy == check->bits.tunnel_proxy) &&
3044            Curl_raw_equal(needle->proxy.name, check->proxy.name) &&
3045            needle->port == check->port) {
3046           /* This is the same proxy connection, use it! */
3047           match = TRUE;
3048         }
3049       }
3050
3051       if(match) {
3052         /* If we are looking for an NTLM connection, check if this is already
3053            authenticating with the right credentials. If not, keep looking so
3054            that we can reuse NTLM connections if possible. (Especially we
3055            must not reuse the same connection if partway through
3056            a handshake!) */
3057         if(wantNTLM) {
3058           if(credentialsMatch && check->ntlm.state != NTLMSTATE_NONE) {
3059             chosen = check;
3060
3061             /* We must use this connection, no other */
3062             *force_reuse = TRUE;
3063             break;
3064           }
3065           else
3066             continue;
3067         }
3068
3069         if(canPipeline) {
3070           /* We can pipeline if we want to. Let's continue looking for
3071              the optimal connection to use, i.e the shortest pipe that is not
3072              blacklisted. */
3073
3074           if(pipeLen == 0) {
3075             /* We have the optimal connection. Let's stop looking. */
3076             chosen = check;
3077             break;
3078           }
3079
3080           /* We can't use the connection if the pipe is full */
3081           if(pipeLen >= max_pipe_len)
3082             continue;
3083
3084           /* We can't use the connection if the pipe is penalized */
3085           if(Curl_pipeline_penalized(data, check))
3086             continue;
3087
3088           if(pipeLen < best_pipe_len) {
3089             /* This connection has a shorter pipe so far. We'll pick this
3090                and continue searching */
3091             chosen = check;
3092             best_pipe_len = pipeLen;
3093             continue;
3094           }
3095         }
3096         else {
3097           /* We have found a connection. Let's stop searching. */
3098           chosen = check;
3099           break;
3100         }
3101       }
3102     }
3103   }
3104
3105   if(chosen) {
3106     *usethis = chosen;
3107     return TRUE; /* yes, we found one to use! */
3108   }
3109
3110   return FALSE; /* no matching connecting exists */
3111 }
3112
3113 /* Mark the connection as 'idle', or close it if the cache is full.
3114    Returns TRUE if the connection is kept, or FALSE if it was closed. */
3115 static bool
3116 ConnectionDone(struct SessionHandle *data, struct connectdata *conn)
3117 {
3118   /* data->multi->maxconnects can be negative, deal with it. */
3119   size_t maxconnects =
3120     (data->multi->maxconnects < 0) ? 0 : data->multi->maxconnects;
3121   struct connectdata *conn_candidate = NULL;
3122
3123   /* Mark the current connection as 'unused' */
3124   conn->inuse = FALSE;
3125
3126   if(maxconnects > 0 &&
3127      data->state.conn_cache->num_connections > maxconnects) {
3128     infof(data, "Connection cache is full, closing the oldest one.\n");
3129
3130     conn_candidate = find_oldest_idle_connection(data);
3131
3132     if(conn_candidate) {
3133       /* Set the connection's owner correctly */
3134       conn_candidate->data = data;
3135
3136       /* the winner gets the honour of being disconnected */
3137       (void)Curl_disconnect(conn_candidate, /* dead_connection */ FALSE);
3138     }
3139   }
3140
3141   return (conn_candidate == conn) ? FALSE : TRUE;
3142 }
3143
3144 /*
3145  * The given input connection struct pointer is to be stored in the connection
3146  * cache. If the cache is already full, least interesting existing connection
3147  * (if any) gets closed.
3148  *
3149  * The given connection should be unique. That must've been checked prior to
3150  * this call.
3151  */
3152 static CURLcode ConnectionStore(struct SessionHandle *data,
3153                                 struct connectdata *conn)
3154 {
3155   static int connection_id_counter = 0;
3156
3157   CURLcode result;
3158
3159   /* Assign a number to the connection for easier tracking in the log
3160      output */
3161   conn->connection_id = connection_id_counter++;
3162
3163   result = Curl_conncache_add_conn(data->state.conn_cache, conn);
3164   if(result != CURLE_OK)
3165     conn->connection_id = -1;
3166
3167   return result;
3168 }
3169
3170 /* after a TCP connection to the proxy has been verified, this function does
3171    the next magic step.
3172
3173    Note: this function's sub-functions call failf()
3174
3175 */
3176 CURLcode Curl_connected_proxy(struct connectdata *conn)
3177 {
3178   if(!conn->bits.proxy)
3179     return CURLE_OK;
3180
3181   switch(conn->proxytype) {
3182 #ifndef CURL_DISABLE_PROXY
3183   case CURLPROXY_SOCKS5:
3184   case CURLPROXY_SOCKS5_HOSTNAME:
3185     return Curl_SOCKS5(conn->proxyuser, conn->proxypasswd,
3186                        conn->host.name, conn->remote_port,
3187                        FIRSTSOCKET, conn);
3188
3189   case CURLPROXY_SOCKS4:
3190     return Curl_SOCKS4(conn->proxyuser, conn->host.name,
3191                        conn->remote_port, FIRSTSOCKET, conn, FALSE);
3192
3193   case CURLPROXY_SOCKS4A:
3194     return Curl_SOCKS4(conn->proxyuser, conn->host.name,
3195                        conn->remote_port, FIRSTSOCKET, conn, TRUE);
3196
3197 #endif /* CURL_DISABLE_PROXY */
3198   case CURLPROXY_HTTP:
3199   case CURLPROXY_HTTP_1_0:
3200     /* do nothing here. handled later. */
3201     break;
3202   default:
3203     break;
3204   } /* switch proxytype */
3205
3206   return CURLE_OK;
3207 }
3208
3209 static CURLcode ConnectPlease(struct SessionHandle *data,
3210                               struct connectdata *conn,
3211                               bool *connected)
3212 {
3213   CURLcode result;
3214   Curl_addrinfo *addr;
3215 #ifndef CURL_DISABLE_VERBOSE_STRINGS
3216   char *hostname = conn->bits.proxy?conn->proxy.name:conn->host.name;
3217
3218   infof(data, "About to connect() to %s%s port %ld (#%ld)\n",
3219         conn->bits.proxy?"proxy ":"",
3220         hostname, conn->port, conn->connection_id);
3221 #else
3222   (void)data;
3223 #endif
3224
3225   /*************************************************************
3226    * Connect to server/proxy
3227    *************************************************************/
3228   result= Curl_connecthost(conn,
3229                            conn->dns_entry,
3230                            &conn->sock[FIRSTSOCKET],
3231                            &addr,
3232                            connected);
3233   if(CURLE_OK == result) {
3234     /* All is cool, we store the current information */
3235     conn->ip_addr = addr;
3236
3237     if(*connected) {
3238       result = Curl_connected_proxy(conn);
3239       if(!result) {
3240         conn->bits.tcpconnect[FIRSTSOCKET] = TRUE;
3241         Curl_pgrsTime(data, TIMER_CONNECT); /* connect done */
3242       }
3243     }
3244   }
3245
3246   if(result)
3247     *connected = FALSE; /* mark it as not connected */
3248
3249   return result;
3250 }
3251
3252 /*
3253  * verboseconnect() displays verbose information after a connect
3254  */
3255 #ifndef CURL_DISABLE_VERBOSE_STRINGS
3256 void Curl_verboseconnect(struct connectdata *conn)
3257 {
3258   if(conn->data->set.verbose)
3259     infof(conn->data, "Connected to %s (%s) port %ld (#%ld)\n",
3260           conn->bits.proxy ? conn->proxy.dispname : conn->host.dispname,
3261           conn->ip_addr_str, conn->port, conn->connection_id);
3262 }
3263 #endif
3264
3265 int Curl_protocol_getsock(struct connectdata *conn,
3266                           curl_socket_t *socks,
3267                           int numsocks)
3268 {
3269   if(conn->handler->proto_getsock)
3270     return conn->handler->proto_getsock(conn, socks, numsocks);
3271   return GETSOCK_BLANK;
3272 }
3273
3274 int Curl_doing_getsock(struct connectdata *conn,
3275                        curl_socket_t *socks,
3276                        int numsocks)
3277 {
3278   if(conn && conn->handler->doing_getsock)
3279     return conn->handler->doing_getsock(conn, socks, numsocks);
3280   return GETSOCK_BLANK;
3281 }
3282
3283 /*
3284  * We are doing protocol-specific connecting and this is being called over and
3285  * over from the multi interface until the connection phase is done on
3286  * protocol layer.
3287  */
3288
3289 CURLcode Curl_protocol_connecting(struct connectdata *conn,
3290                                   bool *done)
3291 {
3292   CURLcode result=CURLE_OK;
3293
3294   if(conn && conn->handler->connecting) {
3295     *done = FALSE;
3296     result = conn->handler->connecting(conn, done);
3297   }
3298   else
3299     *done = TRUE;
3300
3301   return result;
3302 }
3303
3304 /*
3305  * We are DOING this is being called over and over from the multi interface
3306  * until the DOING phase is done on protocol layer.
3307  */
3308
3309 CURLcode Curl_protocol_doing(struct connectdata *conn, bool *done)
3310 {
3311   CURLcode result=CURLE_OK;
3312
3313   if(conn && conn->handler->doing) {
3314     *done = FALSE;
3315     result = conn->handler->doing(conn, done);
3316   }
3317   else
3318     *done = TRUE;
3319
3320   return result;
3321 }
3322
3323 /*
3324  * We have discovered that the TCP connection has been successful, we can now
3325  * proceed with some action.
3326  *
3327  */
3328 CURLcode Curl_protocol_connect(struct connectdata *conn,
3329                                bool *protocol_done)
3330 {
3331   CURLcode result=CURLE_OK;
3332
3333   *protocol_done = FALSE;
3334
3335   if(conn->bits.tcpconnect[FIRSTSOCKET] && conn->bits.protoconnstart) {
3336     /* We already are connected, get back. This may happen when the connect
3337        worked fine in the first call, like when we connect to a local server
3338        or proxy. Note that we don't know if the protocol is actually done.
3339
3340        Unless this protocol doesn't have any protocol-connect callback, as
3341        then we know we're done. */
3342     if(!conn->handler->connecting)
3343       *protocol_done = TRUE;
3344
3345     return CURLE_OK;
3346   }
3347
3348   if(!conn->bits.protoconnstart) {
3349
3350     result = Curl_proxy_connect(conn);
3351     if(result)
3352       return result;
3353
3354     if(conn->bits.tunnel_proxy && conn->bits.httpproxy &&
3355        (conn->tunnel_state[FIRSTSOCKET] != TUNNEL_COMPLETE))
3356       /* when using an HTTP tunnel proxy, await complete tunnel establishment
3357          before proceeding further. Return CURLE_OK so we'll be called again */
3358       return CURLE_OK;
3359
3360     if(conn->handler->connect_it) {
3361       /* is there a protocol-specific connect() procedure? */
3362
3363       /* Call the protocol-specific connect function */
3364       result = conn->handler->connect_it(conn, protocol_done);
3365     }
3366     else
3367       *protocol_done = TRUE;
3368
3369     /* it has started, possibly even completed but that knowledge isn't stored
3370        in this bit! */
3371     if(!result)
3372       conn->bits.protoconnstart = TRUE;
3373   }
3374
3375   return result; /* pass back status */
3376 }
3377
3378 /*
3379  * Helpers for IDNA convertions.
3380  */
3381 static bool is_ASCII_name(const char *hostname)
3382 {
3383   const unsigned char *ch = (const unsigned char*)hostname;
3384
3385   while(*ch) {
3386     if(*ch++ & 0x80)
3387       return FALSE;
3388   }
3389   return TRUE;
3390 }
3391
3392 #ifdef USE_LIBIDN
3393 /*
3394  * Check if characters in hostname is allowed in Top Level Domain.
3395  */
3396 static bool tld_check_name(struct SessionHandle *data,
3397                            const char *ace_hostname)
3398 {
3399   size_t err_pos;
3400   char *uc_name = NULL;
3401   int rc;
3402 #ifndef CURL_DISABLE_VERBOSE_STRINGS
3403   const char *tld_errmsg = "<no msg>";
3404 #else
3405   (void)data;
3406 #endif
3407
3408   /* Convert (and downcase) ACE-name back into locale's character set */
3409   rc = idna_to_unicode_lzlz(ace_hostname, &uc_name, 0);
3410   if(rc != IDNA_SUCCESS)
3411     return FALSE;
3412
3413   rc = tld_check_lz(uc_name, &err_pos, NULL);
3414 #ifndef CURL_DISABLE_VERBOSE_STRINGS
3415 #ifdef HAVE_TLD_STRERROR
3416   if(rc != TLD_SUCCESS)
3417     tld_errmsg = tld_strerror((Tld_rc)rc);
3418 #endif
3419   if(rc == TLD_INVALID)
3420     infof(data, "WARNING: %s; pos %u = `%c'/0x%02X\n",
3421           tld_errmsg, err_pos, uc_name[err_pos],
3422           uc_name[err_pos] & 255);
3423   else if(rc != TLD_SUCCESS)
3424     infof(data, "WARNING: TLD check for %s failed; %s\n",
3425           uc_name, tld_errmsg);
3426 #endif /* CURL_DISABLE_VERBOSE_STRINGS */
3427   if(uc_name)
3428      idn_free(uc_name);
3429   if(rc != TLD_SUCCESS)
3430     return FALSE;
3431
3432   return TRUE;
3433 }
3434 #endif
3435
3436 /*
3437  * Perform any necessary IDN conversion of hostname
3438  */
3439 static void fix_hostname(struct SessionHandle *data,
3440                          struct connectdata *conn, struct hostname *host)
3441 {
3442 #ifndef USE_LIBIDN
3443   (void)data;
3444   (void)conn;
3445 #elif defined(CURL_DISABLE_VERBOSE_STRINGS)
3446   (void)conn;
3447 #endif
3448
3449   /* set the name we use to display the host name */
3450   host->dispname = host->name;
3451   if(!is_ASCII_name(host->name)) {
3452 #ifdef USE_LIBIDN
3453   /*************************************************************
3454    * Check name for non-ASCII and convert hostname to ACE form.
3455    *************************************************************/
3456   if(stringprep_check_version(LIBIDN_REQUIRED_VERSION)) {
3457     char *ace_hostname = NULL;
3458     int rc = idna_to_ascii_lz(host->name, &ace_hostname, 0);
3459     infof (data, "Input domain encoded as `%s'\n",
3460            stringprep_locale_charset ());
3461     if(rc != IDNA_SUCCESS)
3462       infof(data, "Failed to convert %s to ACE; %s\n",
3463             host->name, Curl_idn_strerror(conn,rc));
3464     else {
3465       /* tld_check_name() displays a warning if the host name contains
3466          "illegal" characters for this TLD */
3467       (void)tld_check_name(data, ace_hostname);
3468
3469       host->encalloc = ace_hostname;
3470       /* change the name pointer to point to the encoded hostname */
3471       host->name = host->encalloc;
3472     }
3473   }
3474 #elif defined(USE_WIN32_IDN)
3475   /*************************************************************
3476    * Check name for non-ASCII and convert hostname to ACE form.
3477    *************************************************************/
3478     char *ace_hostname = NULL;
3479     int rc = curl_win32_idn_to_ascii(host->name, &ace_hostname);
3480     if(rc == 0)
3481       infof(data, "Failed to convert %s to ACE;\n",
3482             host->name);
3483     else {
3484       host->encalloc = ace_hostname;
3485       /* change the name pointer to point to the encoded hostname */
3486       host->name = host->encalloc;
3487     }
3488 #else
3489     infof(data, "IDN support not present, can't parse Unicode domains\n");
3490 #endif
3491   }
3492 }
3493
3494 static void llist_dtor(void *user, void *element)
3495 {
3496   (void)user;
3497   (void)element;
3498   /* Do nothing */
3499 }
3500
3501 /*
3502  * Allocate and initialize a new connectdata object.
3503  */
3504 static struct connectdata *allocate_conn(struct SessionHandle *data)
3505 {
3506   struct connectdata *conn = calloc(1, sizeof(struct connectdata));
3507   if(!conn)
3508     return NULL;
3509
3510   conn->handler = &Curl_handler_dummy;  /* Be sure we have a handler defined
3511                                            already from start to avoid NULL
3512                                            situations and checks */
3513
3514   /* and we setup a few fields in case we end up actually using this struct */
3515
3516   conn->sock[FIRSTSOCKET] = CURL_SOCKET_BAD;     /* no file descriptor */
3517   conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
3518   conn->connection_id = -1;    /* no ID */
3519   conn->port = -1; /* unknown at this point */
3520
3521   /* Default protocol-independent behavior doesn't support persistent
3522      connections, so we set this to force-close. Protocols that support
3523      this need to set this to FALSE in their "curl_do" functions. */
3524   conn->bits.close = TRUE;
3525
3526   /* Store creation time to help future close decision making */
3527   conn->created = Curl_tvnow();
3528
3529   conn->data = data; /* Setup the association between this connection
3530                         and the SessionHandle */
3531
3532   conn->proxytype = data->set.proxytype; /* type */
3533
3534 #ifdef CURL_DISABLE_PROXY
3535
3536   conn->bits.proxy = FALSE;
3537   conn->bits.httpproxy = FALSE;
3538   conn->bits.proxy_user_passwd = FALSE;
3539   conn->bits.tunnel_proxy = FALSE;
3540
3541 #else /* CURL_DISABLE_PROXY */
3542
3543   /* note that these two proxy bits are now just on what looks to be
3544      requested, they may be altered down the road */
3545   conn->bits.proxy = (data->set.str[STRING_PROXY] &&
3546                       *data->set.str[STRING_PROXY])?TRUE:FALSE;
3547   conn->bits.httpproxy = (conn->bits.proxy &&
3548                           (conn->proxytype == CURLPROXY_HTTP ||
3549                            conn->proxytype == CURLPROXY_HTTP_1_0))?TRUE:FALSE;
3550   conn->bits.proxy_user_passwd =
3551     (NULL != data->set.str[STRING_PROXYUSERNAME])?TRUE:FALSE;
3552   conn->bits.tunnel_proxy = data->set.tunnel_thru_httpproxy;
3553
3554 #endif /* CURL_DISABLE_PROXY */
3555
3556   conn->bits.user_passwd = (NULL != data->set.str[STRING_USERNAME])?TRUE:FALSE;
3557   conn->bits.ftp_use_epsv = data->set.ftp_use_epsv;
3558   conn->bits.ftp_use_eprt = data->set.ftp_use_eprt;
3559
3560   conn->verifypeer = data->set.ssl.verifypeer;
3561   conn->verifyhost = data->set.ssl.verifyhost;
3562
3563   conn->ip_version = data->set.ipver;
3564
3565 #if defined(USE_NTLM) && defined(NTLM_WB_ENABLED)
3566   conn->ntlm_auth_hlpr_socket = CURL_SOCKET_BAD;
3567   conn->ntlm_auth_hlpr_pid = 0;
3568   conn->challenge_header = NULL;
3569   conn->response_header = NULL;
3570 #endif
3571
3572   if(Curl_multi_pipeline_enabled(data->multi) &&
3573       !conn->master_buffer) {
3574     /* Allocate master_buffer to be used for pipelining */
3575     conn->master_buffer = calloc(BUFSIZE, sizeof (char));
3576     if(!conn->master_buffer)
3577       goto error;
3578   }
3579
3580   /* Initialize the pipeline lists */
3581   conn->send_pipe = Curl_llist_alloc((curl_llist_dtor) llist_dtor);
3582   conn->recv_pipe = Curl_llist_alloc((curl_llist_dtor) llist_dtor);
3583   if(!conn->send_pipe || !conn->recv_pipe)
3584     goto error;
3585
3586 #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
3587   conn->data_prot = PROT_CLEAR;
3588 #endif
3589
3590   /* Store the local bind parameters that will be used for this connection */
3591   if(data->set.str[STRING_DEVICE]) {
3592     conn->localdev = strdup(data->set.str[STRING_DEVICE]);
3593     if(!conn->localdev)
3594       goto error;
3595   }
3596   conn->localportrange = data->set.localportrange;
3597   conn->localport = data->set.localport;
3598
3599   /* the close socket stuff needs to be copied to the connection struct as
3600      it may live on without (this specific) SessionHandle */
3601   conn->fclosesocket = data->set.fclosesocket;
3602   conn->closesocket_client = data->set.closesocket_client;
3603
3604   return conn;
3605   error:
3606
3607   Curl_llist_destroy(conn->send_pipe, NULL);
3608   Curl_llist_destroy(conn->recv_pipe, NULL);
3609
3610   conn->send_pipe = NULL;
3611   conn->recv_pipe = NULL;
3612
3613   Curl_safefree(conn->master_buffer);
3614   Curl_safefree(conn->localdev);
3615   Curl_safefree(conn);
3616   return NULL;
3617 }
3618
3619 static CURLcode findprotocol(struct SessionHandle *data,
3620                              struct connectdata *conn,
3621                              const char *protostr)
3622 {
3623   const struct Curl_handler * const *pp;
3624   const struct Curl_handler *p;
3625
3626   /* Scan protocol handler table and match against 'protostr' to set a few
3627      variables based on the URL. Now that the handler may be changed later
3628      when the protocol specific setup function is called. */
3629   for(pp = protocols; (p = *pp) != NULL; pp++) {
3630     if(Curl_raw_equal(p->scheme, protostr)) {
3631       /* Protocol found in table. Check if allowed */
3632       if(!(data->set.allowed_protocols & p->protocol))
3633         /* nope, get out */
3634         break;
3635
3636       /* it is allowed for "normal" request, now do an extra check if this is
3637          the result of a redirect */
3638       if(data->state.this_is_a_follow &&
3639          !(data->set.redir_protocols & p->protocol))
3640         /* nope, get out */
3641         break;
3642
3643       /* Perform setup complement if some. */
3644       conn->handler = conn->given = p;
3645
3646       /* 'port' and 'remote_port' are set in setup_connection_internals() */
3647       return CURLE_OK;
3648     }
3649   }
3650
3651
3652   /* The protocol was not found in the table, but we don't have to assign it
3653      to anything since it is already assigned to a dummy-struct in the
3654      create_conn() function when the connectdata struct is allocated. */
3655   failf(data, "Protocol %s not supported or disabled in " LIBCURL_NAME,
3656         protostr);
3657
3658   return CURLE_UNSUPPORTED_PROTOCOL;
3659 }
3660
3661 /*
3662  * Parse URL and fill in the relevant members of the connection struct.
3663  */
3664 static CURLcode parseurlandfillconn(struct SessionHandle *data,
3665                                     struct connectdata *conn,
3666                                     bool *prot_missing,
3667                                     char *user, char *passwd, char *options)
3668 {
3669   char *at;
3670   char *fragment;
3671   char *path = data->state.path;
3672   char *query;
3673   int rc;
3674   char protobuf[16];
3675   const char *protop;
3676   CURLcode result;
3677   bool fix_slash = FALSE;
3678
3679   *prot_missing = FALSE;
3680
3681   /*************************************************************
3682    * Parse the URL.
3683    *
3684    * We need to parse the url even when using the proxy, because we will need
3685    * the hostname and port in case we are trying to SSL connect through the
3686    * proxy -- and we don't know if we will need to use SSL until we parse the
3687    * url ...
3688    ************************************************************/
3689   if((2 == sscanf(data->change.url, "%15[^:]:%[^\n]",
3690                   protobuf, path)) &&
3691      Curl_raw_equal(protobuf, "file")) {
3692     if(path[0] == '/' && path[1] == '/') {
3693       /* Allow omitted hostname (e.g. file:/<path>).  This is not strictly
3694        * speaking a valid file: URL by RFC 1738, but treating file:/<path> as
3695        * file://localhost/<path> is similar to how other schemes treat missing
3696        * hostnames.  See RFC 1808. */
3697
3698       /* This cannot be done with strcpy() in a portable manner, since the
3699          memory areas overlap! */
3700       memmove(path, path + 2, strlen(path + 2)+1);
3701     }
3702     /*
3703      * we deal with file://<host>/<path> differently since it supports no
3704      * hostname other than "localhost" and "127.0.0.1", which is unique among
3705      * the URL protocols specified in RFC 1738
3706      */
3707     if(path[0] != '/') {
3708       /* the URL included a host name, we ignore host names in file:// URLs
3709          as the standards don't define what to do with them */
3710       char *ptr=strchr(path, '/');
3711       if(ptr) {
3712         /* there was a slash present
3713
3714            RFC1738 (section 3.1, page 5) says:
3715
3716            The rest of the locator consists of data specific to the scheme,
3717            and is known as the "url-path". It supplies the details of how the
3718            specified resource can be accessed. Note that the "/" between the
3719            host (or port) and the url-path is NOT part of the url-path.
3720
3721            As most agents use file://localhost/foo to get '/foo' although the
3722            slash preceding foo is a separator and not a slash for the path,
3723            a URL as file://localhost//foo must be valid as well, to refer to
3724            the same file with an absolute path.
3725         */
3726
3727         if(ptr[1] && ('/' == ptr[1]))
3728           /* if there was two slashes, we skip the first one as that is then
3729              used truly as a separator */
3730           ptr++;
3731
3732         /* This cannot be made with strcpy, as the memory chunks overlap! */
3733         memmove(path, ptr, strlen(ptr)+1);
3734       }
3735     }
3736
3737     protop = "file"; /* protocol string */
3738   }
3739   else {
3740     /* clear path */
3741     path[0]=0;
3742
3743     if(2 > sscanf(data->change.url,
3744                    "%15[^\n:]://%[^\n/?]%[^\n]",
3745                    protobuf,
3746                    conn->host.name, path)) {
3747
3748       /*
3749        * The URL was badly formatted, let's try the browser-style _without_
3750        * protocol specified like 'http://'.
3751        */
3752       rc = sscanf(data->change.url, "%[^\n/?]%[^\n]", conn->host.name, path);
3753       if(1 > rc) {
3754         /*
3755          * We couldn't even get this format.
3756          * djgpp 2.04 has a sscanf() bug where 'conn->host.name' is
3757          * assigned, but the return value is EOF!
3758          */
3759 #if defined(__DJGPP__) && (DJGPP_MINOR == 4)
3760         if(!(rc == -1 && *conn->host.name))
3761 #endif
3762         {
3763           failf(data, "<url> malformed");
3764           return CURLE_URL_MALFORMAT;
3765         }
3766       }
3767
3768       /*
3769        * Since there was no protocol part specified, we guess what protocol it
3770        * is based on the first letters of the server name.
3771        */
3772
3773       /* Note: if you add a new protocol, please update the list in
3774        * lib/version.c too! */
3775
3776       if(checkprefix("FTP.", conn->host.name))
3777         protop = "ftp";
3778       else if(checkprefix("DICT.", conn->host.name))
3779         protop = "DICT";
3780       else if(checkprefix("LDAP.", conn->host.name))
3781         protop = "LDAP";
3782       else if(checkprefix("IMAP.", conn->host.name))
3783         protop = "IMAP";
3784       else if(checkprefix("SMTP.", conn->host.name))
3785         protop = "smtp";
3786       else if(checkprefix("POP3.", conn->host.name))
3787         protop = "pop3";
3788       else {
3789         protop = "http";
3790       }
3791
3792       *prot_missing = TRUE; /* not given in URL */
3793     }
3794     else
3795       protop = protobuf;
3796   }
3797
3798   /* We search for '?' in the host name (but only on the right side of a
3799    * @-letter to allow ?-letters in username and password) to handle things
3800    * like http://example.com?param= (notice the missing '/').
3801    */
3802   at = strchr(conn->host.name, '@');
3803   if(at)
3804     query = strchr(at+1, '?');
3805   else
3806     query = strchr(conn->host.name, '?');
3807
3808   if(query) {
3809     /* We must insert a slash before the '?'-letter in the URL. If the URL had
3810        a slash after the '?', that is where the path currently begins and the
3811        '?string' is still part of the host name.
3812
3813        We must move the trailing part from the host name and put it first in
3814        the path. And have it all prefixed with a slash.
3815     */
3816
3817     size_t hostlen = strlen(query);
3818     size_t pathlen = strlen(path);
3819
3820     /* move the existing path plus the zero byte forward, to make room for
3821        the host-name part */
3822     memmove(path+hostlen+1, path, pathlen+1);
3823
3824      /* now copy the trailing host part in front of the existing path */
3825     memcpy(path+1, query, hostlen);
3826
3827     path[0]='/'; /* prepend the missing slash */
3828     fix_slash = TRUE;
3829
3830     *query=0; /* now cut off the hostname at the ? */
3831   }
3832   else if(!path[0]) {
3833     /* if there's no path set, use a single slash */
3834     strcpy(path, "/");
3835     fix_slash = TRUE;
3836   }
3837
3838   /* If the URL is malformatted (missing a '/' after hostname before path) we
3839    * insert a slash here. The only letter except '/' we accept to start a path
3840    * is '?'.
3841    */
3842   if(path[0] == '?') {
3843     /* We need this function to deal with overlapping memory areas. We know
3844        that the memory area 'path' points to is 'urllen' bytes big and that
3845        is bigger than the path. Use +1 to move the zero byte too. */
3846     memmove(&path[1], path, strlen(path)+1);
3847     path[0] = '/';
3848     fix_slash = TRUE;
3849   }
3850
3851
3852   /*
3853    * "fix_slash" means that the URL was malformatted so we need to generate an
3854    * updated version with the new slash inserted at the right place!  We need
3855    * the corrected URL when communicating over HTTP proxy and we don't know at
3856    * this point if we're using a proxy or not.
3857    */
3858   if(fix_slash) {
3859     char *reurl;
3860
3861     size_t plen = strlen(path); /* new path, should be 1 byte longer than
3862                                    the original */
3863     size_t urllen = strlen(data->change.url); /* original URL length */
3864
3865     reurl = malloc(urllen + 2); /* 2 for zerobyte + slash */
3866     if(!reurl)
3867       return CURLE_OUT_OF_MEMORY;
3868
3869     /* copy the prefix */
3870     memcpy(reurl, data->change.url, urllen - (plen-1));
3871
3872     /* append the trailing piece + zerobyte */
3873     memcpy(&reurl[urllen - (plen-1)], path, plen + 1);
3874
3875     /* possible free the old one */
3876     if(data->change.url_alloc) {
3877       Curl_safefree(data->change.url);
3878       data->change.url_alloc = FALSE;
3879     }
3880
3881     data->change.url = reurl;
3882     data->change.url_alloc = TRUE; /* free this later */
3883   }
3884
3885   /*
3886    * Parse the login details from the URL and strip them out of
3887    * the host name
3888    */
3889   result = parse_url_login(data, conn, user, passwd, options);
3890   if(result != CURLE_OK)
3891     return result;
3892
3893   if(conn->host.name[0] == '[') {
3894     /* This looks like an IPv6 address literal.  See if there is an address
3895        scope.  */
3896     char *percent = strstr (conn->host.name, "%25");
3897     if(percent) {
3898       char *endp;
3899       unsigned long scope = strtoul (percent + 3, &endp, 10);
3900       if(*endp == ']') {
3901         /* The address scope was well formed.  Knock it out of the
3902            hostname. */
3903         memmove(percent, endp, strlen(endp)+1);
3904         if(!data->state.this_is_a_follow)
3905           /* Don't honour a scope given in a Location: header */
3906           conn->scope = (unsigned int)scope;
3907       }
3908       else
3909         infof(data, "Invalid IPv6 address format\n");
3910     }
3911   }
3912
3913   if(data->set.scope)
3914     /* Override any scope that was set above.  */
3915     conn->scope = data->set.scope;
3916
3917   /* Remove the fragment part of the path. Per RFC 2396, this is always the
3918      last part of the URI. We are looking for the first '#' so that we deal
3919      gracefully with non conformant URI such as http://example.com#foo#bar. */
3920   fragment = strchr(path, '#');
3921   if(fragment) {
3922     *fragment = 0;
3923
3924     /* we know the path part ended with a fragment, so we know the full URL
3925        string does too and we need to cut it off from there so it isn't used
3926        over proxy */
3927     fragment = strchr(data->change.url, '#');
3928     if(fragment)
3929       *fragment = 0;
3930   }
3931
3932   /*
3933    * So if the URL was A://B/C#D,
3934    *   protop is A
3935    *   conn->host.name is B
3936    *   data->state.path is /C
3937    */
3938
3939   return findprotocol(data, conn, protop);
3940 }
3941
3942 /*
3943  * If we're doing a resumed transfer, we need to setup our stuff
3944  * properly.
3945  */
3946 static CURLcode setup_range(struct SessionHandle *data)
3947 {
3948   struct UrlState *s = &data->state;
3949   s->resume_from = data->set.set_resume_from;
3950   if(s->resume_from || data->set.str[STRING_SET_RANGE]) {
3951     if(s->rangestringalloc)
3952       free(s->range);
3953
3954     if(s->resume_from)
3955       s->range = aprintf("%" FORMAT_OFF_TU "-", s->resume_from);
3956     else
3957       s->range = strdup(data->set.str[STRING_SET_RANGE]);
3958
3959     s->rangestringalloc = (s->range)?TRUE:FALSE;
3960
3961     if(!s->range)
3962       return CURLE_OUT_OF_MEMORY;
3963
3964     /* tell ourselves to fetch this range */
3965     s->use_range = TRUE;        /* enable range download */
3966   }
3967   else
3968     s->use_range = FALSE; /* disable range download */
3969
3970   return CURLE_OK;
3971 }
3972
3973
3974 /***************************************************************
3975 * Setup connection internals specific to the requested protocol.
3976 * This MUST get called after proxy magic has been figured out.
3977 ***************************************************************/
3978 static CURLcode setup_connection_internals(struct connectdata *conn)
3979 {
3980   const struct Curl_handler * p;
3981   CURLcode result;
3982
3983   conn->socktype = SOCK_STREAM; /* most of them are TCP streams */
3984
3985   /* Scan protocol handler table. */
3986
3987   /* Perform setup complement if some. */
3988   p = conn->handler;
3989
3990   if(p->setup_connection) {
3991     result = (*p->setup_connection)(conn);
3992
3993     if(result != CURLE_OK)
3994       return result;
3995
3996     p = conn->handler;              /* May have changed. */
3997   }
3998
3999   if(conn->port < 0)
4000     /* we check for -1 here since if proxy was detected already, this
4001        was very likely already set to the proxy port */
4002     conn->port = p->defport;
4003   conn->remote_port = (unsigned short)conn->given->defport;
4004
4005   return CURLE_OK;
4006 }
4007
4008 #ifndef CURL_DISABLE_PROXY
4009 /****************************************************************
4010 * Checks if the host is in the noproxy list. returns true if it matches
4011 * and therefore the proxy should NOT be used.
4012 ****************************************************************/
4013 static bool check_noproxy(const char* name, const char* no_proxy)
4014 {
4015   /* no_proxy=domain1.dom,host.domain2.dom
4016    *   (a comma-separated list of hosts which should
4017    *   not be proxied, or an asterisk to override
4018    *   all proxy variables)
4019    */
4020   size_t tok_start;
4021   size_t tok_end;
4022   const char* separator = ", ";
4023   size_t no_proxy_len;
4024   size_t namelen;
4025   char *endptr;
4026
4027   if(no_proxy && no_proxy[0]) {
4028     if(Curl_raw_equal("*", no_proxy)) {
4029       return TRUE;
4030     }
4031
4032     /* NO_PROXY was specified and it wasn't just an asterisk */
4033
4034     no_proxy_len = strlen(no_proxy);
4035     endptr = strchr(name, ':');
4036     if(endptr)
4037       namelen = endptr - name;
4038     else
4039       namelen = strlen(name);
4040
4041     for(tok_start = 0; tok_start < no_proxy_len; tok_start = tok_end + 1) {
4042       while(tok_start < no_proxy_len &&
4043             strchr(separator, no_proxy[tok_start]) != NULL) {
4044         /* Look for the beginning of the token. */
4045         ++tok_start;
4046       }
4047
4048       if(tok_start == no_proxy_len)
4049         break; /* It was all trailing separator chars, no more tokens. */
4050
4051       for(tok_end = tok_start; tok_end < no_proxy_len &&
4052             strchr(separator, no_proxy[tok_end]) == NULL; ++tok_end)
4053         /* Look for the end of the token. */
4054         ;
4055
4056       /* To match previous behaviour, where it was necessary to specify
4057        * ".local.com" to prevent matching "notlocal.com", we will leave
4058        * the '.' off.
4059        */
4060       if(no_proxy[tok_start] == '.')
4061         ++tok_start;
4062
4063       if((tok_end - tok_start) <= namelen) {
4064         /* Match the last part of the name to the domain we are checking. */
4065         const char *checkn = name + namelen - (tok_end - tok_start);
4066         if(Curl_raw_nequal(no_proxy + tok_start, checkn,
4067                            tok_end - tok_start)) {
4068           if((tok_end - tok_start) == namelen || *(checkn - 1) == '.') {
4069             /* We either have an exact match, or the previous character is a .
4070              * so it is within the same domain, so no proxy for this host.
4071              */
4072             return TRUE;
4073           }
4074         }
4075       } /* if((tok_end - tok_start) <= namelen) */
4076     } /* for(tok_start = 0; tok_start < no_proxy_len;
4077          tok_start = tok_end + 1) */
4078   } /* NO_PROXY was specified and it wasn't just an asterisk */
4079
4080   return FALSE;
4081 }
4082
4083 /****************************************************************
4084 * Detect what (if any) proxy to use. Remember that this selects a host
4085 * name and is not limited to HTTP proxies only.
4086 * The returned pointer must be freed by the caller (unless NULL)
4087 ****************************************************************/
4088 static char *detect_proxy(struct connectdata *conn)
4089 {
4090   char *proxy = NULL;
4091
4092 #ifndef CURL_DISABLE_HTTP
4093   /* If proxy was not specified, we check for default proxy environment
4094    * variables, to enable i.e Lynx compliance:
4095    *
4096    * http_proxy=http://some.server.dom:port/
4097    * https_proxy=http://some.server.dom:port/
4098    * ftp_proxy=http://some.server.dom:port/
4099    * no_proxy=domain1.dom,host.domain2.dom
4100    *   (a comma-separated list of hosts which should
4101    *   not be proxied, or an asterisk to override
4102    *   all proxy variables)
4103    * all_proxy=http://some.server.dom:port/
4104    *   (seems to exist for the CERN www lib. Probably
4105    *   the first to check for.)
4106    *
4107    * For compatibility, the all-uppercase versions of these variables are
4108    * checked if the lowercase versions don't exist.
4109    */
4110   char *no_proxy=NULL;
4111   char proxy_env[128];
4112
4113   no_proxy=curl_getenv("no_proxy");
4114   if(!no_proxy)
4115     no_proxy=curl_getenv("NO_PROXY");
4116
4117   if(!check_noproxy(conn->host.name, no_proxy)) {
4118     /* It was not listed as without proxy */
4119     const char *protop = conn->handler->scheme;
4120     char *envp = proxy_env;
4121     char *prox;
4122
4123     /* Now, build <protocol>_proxy and check for such a one to use */
4124     while(*protop)
4125       *envp++ = (char)tolower((int)*protop++);
4126
4127     /* append _proxy */
4128     strcpy(envp, "_proxy");
4129
4130     /* read the protocol proxy: */
4131     prox=curl_getenv(proxy_env);
4132
4133     /*
4134      * We don't try the uppercase version of HTTP_PROXY because of
4135      * security reasons:
4136      *
4137      * When curl is used in a webserver application
4138      * environment (cgi or php), this environment variable can
4139      * be controlled by the web server user by setting the
4140      * http header 'Proxy:' to some value.
4141      *
4142      * This can cause 'internal' http/ftp requests to be
4143      * arbitrarily redirected by any external attacker.
4144      */
4145     if(!prox && !Curl_raw_equal("http_proxy", proxy_env)) {
4146       /* There was no lowercase variable, try the uppercase version: */
4147       Curl_strntoupper(proxy_env, proxy_env, sizeof(proxy_env));
4148       prox=curl_getenv(proxy_env);
4149     }
4150
4151     if(prox && *prox) { /* don't count "" strings */
4152       proxy = prox; /* use this */
4153     }
4154     else {
4155       proxy = curl_getenv("all_proxy"); /* default proxy to use */
4156       if(!proxy)
4157         proxy=curl_getenv("ALL_PROXY");
4158     }
4159   } /* if(!check_noproxy(conn->host.name, no_proxy)) - it wasn't specified
4160        non-proxy */
4161   if(no_proxy)
4162     free(no_proxy);
4163
4164 #else /* !CURL_DISABLE_HTTP */
4165
4166   (void)conn;
4167 #endif /* CURL_DISABLE_HTTP */
4168
4169   return proxy;
4170 }
4171
4172 /*
4173  * If this is supposed to use a proxy, we need to figure out the proxy
4174  * host name, so that we can re-use an existing connection
4175  * that may exist registered to the same proxy host.
4176  * proxy will be freed before this function returns.
4177  */
4178 static CURLcode parse_proxy(struct SessionHandle *data,
4179                             struct connectdata *conn, char *proxy)
4180 {
4181   char *prox_portno;
4182   char *endofprot;
4183
4184   /* We use 'proxyptr' to point to the proxy name from now on... */
4185   char *proxyptr;
4186   char *portptr;
4187   char *atsign;
4188
4189   /* We do the proxy host string parsing here. We want the host name and the
4190    * port name. Accept a protocol:// prefix
4191    */
4192
4193   /* Parse the protocol part if present */
4194   endofprot = strstr(proxy, "://");
4195   if(endofprot) {
4196     proxyptr = endofprot+3;
4197     if(checkprefix("socks5h", proxy))
4198       conn->proxytype = CURLPROXY_SOCKS5_HOSTNAME;
4199     else if(checkprefix("socks5", proxy))
4200       conn->proxytype = CURLPROXY_SOCKS5;
4201     else if(checkprefix("socks4a", proxy))
4202       conn->proxytype = CURLPROXY_SOCKS4A;
4203     else if(checkprefix("socks4", proxy) || checkprefix("socks", proxy))
4204       conn->proxytype = CURLPROXY_SOCKS4;
4205     /* Any other xxx:// : change to http proxy */
4206   }
4207   else
4208     proxyptr = proxy; /* No xxx:// head: It's a HTTP proxy */
4209
4210   /* Is there a username and password given in this proxy url? */
4211   atsign = strchr(proxyptr, '@');
4212   if(atsign) {
4213     CURLcode res = CURLE_OK;
4214     char *proxyuser = NULL;
4215     char *proxypasswd = NULL;
4216
4217     res = parse_login_details(proxyptr, atsign - proxyptr,
4218                               &proxyuser, &proxypasswd, NULL);
4219     if(!res) {
4220       /* found user and password, rip them out.  note that we are
4221          unescaping them, as there is otherwise no way to have a
4222          username or password with reserved characters like ':' in
4223          them. */
4224       Curl_safefree(conn->proxyuser);
4225       if(proxyuser && strlen(proxyuser) < MAX_CURL_USER_LENGTH)
4226         conn->proxyuser = curl_easy_unescape(data, proxyuser, 0, NULL);
4227       else
4228         conn->proxyuser = strdup("");
4229
4230       if(!conn->proxyuser)
4231         res = CURLE_OUT_OF_MEMORY;
4232       else {
4233         Curl_safefree(conn->proxypasswd);
4234         if(proxypasswd && strlen(proxypasswd) < MAX_CURL_PASSWORD_LENGTH)
4235           conn->proxypasswd = curl_easy_unescape(data, proxypasswd, 0, NULL);
4236         else
4237           conn->proxypasswd = strdup("");
4238
4239         if(!conn->proxypasswd)
4240           res = CURLE_OUT_OF_MEMORY;
4241       }
4242
4243       if(!res) {
4244         conn->bits.proxy_user_passwd = TRUE; /* enable it */
4245         atsign++; /* the right side of the @-letter */
4246
4247         if(atsign)
4248           proxyptr = atsign; /* now use this instead */
4249         else
4250           res = CURLE_OUT_OF_MEMORY;
4251       }
4252     }
4253
4254     Curl_safefree(proxyuser);
4255     Curl_safefree(proxypasswd);
4256
4257     if(res)
4258       return res;
4259   }
4260
4261   /* start scanning for port number at this point */
4262   portptr = proxyptr;
4263
4264   /* detect and extract RFC2732-style IPv6-addresses */
4265   if(*proxyptr == '[') {
4266     char *ptr = ++proxyptr; /* advance beyond the initial bracket */
4267     while(*ptr && (ISXDIGIT(*ptr) || (*ptr == ':') || (*ptr == '%') ||
4268                    (*ptr == '.')))
4269       ptr++;
4270     if(*ptr == ']')
4271       /* yeps, it ended nicely with a bracket as well */
4272       *ptr++ = 0;
4273     else
4274       infof(data, "Invalid IPv6 address format\n");
4275     portptr = ptr;
4276     /* Note that if this didn't end with a bracket, we still advanced the
4277      * proxyptr first, but I can't see anything wrong with that as no host
4278      * name nor a numeric can legally start with a bracket.
4279      */
4280   }
4281
4282   /* Get port number off proxy.server.com:1080 */
4283   prox_portno = strchr(portptr, ':');
4284   if(prox_portno) {
4285     *prox_portno = 0x0; /* cut off number from host name */
4286     prox_portno ++;
4287     /* now set the local port number */
4288     conn->port = strtol(prox_portno, NULL, 10);
4289   }
4290   else {
4291     if(proxyptr[0]=='/')
4292       /* If the first character in the proxy string is a slash, fail
4293          immediately. The following code will otherwise clear the string which
4294          will lead to code running as if no proxy was set! */
4295       return CURLE_COULDNT_RESOLVE_PROXY;
4296
4297     /* without a port number after the host name, some people seem to use
4298        a slash so we strip everything from the first slash */
4299     atsign = strchr(proxyptr, '/');
4300     if(atsign)
4301       *atsign = 0x0; /* cut off path part from host name */
4302
4303     if(data->set.proxyport)
4304       /* None given in the proxy string, then get the default one if it is
4305          given */
4306       conn->port = data->set.proxyport;
4307   }
4308
4309   /* now, clone the cleaned proxy host name */
4310   conn->proxy.rawalloc = strdup(proxyptr);
4311   conn->proxy.name = conn->proxy.rawalloc;
4312
4313   if(!conn->proxy.rawalloc)
4314     return CURLE_OUT_OF_MEMORY;
4315
4316   return CURLE_OK;
4317 }
4318
4319 /*
4320  * Extract the user and password from the authentication string
4321  */
4322 static CURLcode parse_proxy_auth(struct SessionHandle *data,
4323                                  struct connectdata *conn)
4324 {
4325   char proxyuser[MAX_CURL_USER_LENGTH]="";
4326   char proxypasswd[MAX_CURL_PASSWORD_LENGTH]="";
4327
4328   if(data->set.str[STRING_PROXYUSERNAME] != NULL) {
4329     strncpy(proxyuser, data->set.str[STRING_PROXYUSERNAME],
4330             MAX_CURL_USER_LENGTH);
4331     proxyuser[MAX_CURL_USER_LENGTH-1] = '\0';   /*To be on safe side*/
4332   }
4333   if(data->set.str[STRING_PROXYPASSWORD] != NULL) {
4334     strncpy(proxypasswd, data->set.str[STRING_PROXYPASSWORD],
4335             MAX_CURL_PASSWORD_LENGTH);
4336     proxypasswd[MAX_CURL_PASSWORD_LENGTH-1] = '\0'; /*To be on safe side*/
4337   }
4338
4339   conn->proxyuser = curl_easy_unescape(data, proxyuser, 0, NULL);
4340   if(!conn->proxyuser)
4341     return CURLE_OUT_OF_MEMORY;
4342
4343   conn->proxypasswd = curl_easy_unescape(data, proxypasswd, 0, NULL);
4344   if(!conn->proxypasswd)
4345     return CURLE_OUT_OF_MEMORY;
4346
4347   return CURLE_OK;
4348 }
4349 #endif /* CURL_DISABLE_PROXY */
4350
4351 /*
4352  * parse_url_login()
4353  *
4354  * Parse the login details (user name, password and options) from the URL and
4355  * strip them out of the host name
4356  *
4357  * Inputs: data->set.use_netrc (CURLOPT_NETRC)
4358  *         conn->host.name
4359  *
4360  * Outputs: (almost :- all currently undefined)
4361  *          conn->bits.user_passwd  - non-zero if non-default passwords exist
4362  *          user                    - non-zero length if defined
4363  *          passwd                  - non-zero length if defined
4364  *          options                 - non-zero length if defined
4365  *          conn->host.name         - remove user name and password
4366  */
4367 static CURLcode parse_url_login(struct SessionHandle *data,
4368                                 struct connectdata *conn,
4369                                 char *user, char *passwd, char *options)
4370 {
4371   CURLcode result = CURLE_OK;
4372   char *userp = NULL;
4373   char *passwdp = NULL;
4374   char *optionsp = NULL;
4375
4376   /* At this point, we're hoping all the other special cases have
4377    * been taken care of, so conn->host.name is at most
4378    *    [user[:password][;options]]@]hostname
4379    *
4380    * We need somewhere to put the embedded details, so do that first.
4381    */
4382
4383   char *ptr = strchr(conn->host.name, '@');
4384   char *login = conn->host.name;
4385
4386   user[0] = 0;   /* to make everything well-defined */
4387   passwd[0] = 0;
4388   options[0] = 0;
4389
4390   /* We will now try to extract the
4391    * possible login information in a string like:
4392    * ftp://user:password@ftp.my.site:8021/README */
4393   if(ptr) {
4394     /* There's login information to the left of the @ */
4395
4396     conn->host.name = ++ptr;
4397
4398     /* So the hostname is sane.  Only bother interpreting the
4399      * results if we could care.  It could still be wasted
4400      * work because it might be overtaken by the programmatically
4401      * set user/passwd, but doing that first adds more cases here :-(
4402      */
4403
4404     if(data->set.use_netrc != CURL_NETRC_REQUIRED) {
4405       /* We could use the login information in the URL so extract it */
4406       result = parse_login_details(login, ptr - login - 1,
4407                                    &userp, &passwdp, &optionsp);
4408       if(!result) {
4409         if(userp) {
4410           char *newname;
4411
4412           /* We have a user in the URL */
4413           conn->bits.userpwd_in_url = TRUE;
4414           conn->bits.user_passwd = TRUE; /* enable user+password */
4415
4416           /* Decode the user */
4417           newname = curl_easy_unescape(data, userp, 0, NULL);
4418           if(!newname)
4419             return CURLE_OUT_OF_MEMORY;
4420
4421           if(strlen(newname) < MAX_CURL_USER_LENGTH)
4422             strcpy(user, newname);
4423
4424           free(newname);
4425         }
4426
4427         if(passwdp) {
4428           /* We have a password in the URL so decode it */
4429           char *newpasswd = curl_easy_unescape(data, passwdp, 0, NULL);
4430           if(!newpasswd)
4431             return CURLE_OUT_OF_MEMORY;
4432
4433           if(strlen(newpasswd) < MAX_CURL_PASSWORD_LENGTH)
4434             strcpy(passwd, newpasswd);
4435
4436           free(newpasswd);
4437         }
4438
4439         if(optionsp) {
4440           /* We have an options list in the URL so decode it */
4441           char *newoptions = curl_easy_unescape(data, optionsp, 0, NULL);
4442           if(!newoptions)
4443             return CURLE_OUT_OF_MEMORY;
4444
4445           if(strlen(newoptions) < MAX_CURL_OPTIONS_LENGTH)
4446             strcpy(options, newoptions);
4447
4448           free(newoptions);
4449         }
4450       }
4451
4452       Curl_safefree(userp);
4453       Curl_safefree(passwdp);
4454       Curl_safefree(optionsp);
4455     }
4456   }
4457
4458   return result;
4459 }
4460
4461 /*
4462  * parse_login_details()
4463  *
4464  * This is used to parse a login string for user name, password and options in
4465  * the following formats:
4466  *
4467  *   user
4468  *   user:password
4469  *   user:password;options
4470  *   user;options
4471  *   user;options:password
4472  *   :password
4473  *   :password;options
4474  *   ;options
4475  *   ;options:password
4476  *
4477  * Parameters:
4478  *
4479  * login    [in]     - The login string.
4480  * len      [in]     - The length of the login string.
4481  * userp    [in/out] - The address where a pointer to newly allocated memory
4482  *                     holding the user will be stored upon completion.
4483  * passdwp  [in/out] - The address where a pointer to newly allocated memory
4484  *                     holding the password will be stored upon completion.
4485  * optionsp [in/out] - The address where a pointer to newly allocated memory
4486  *                     holding the options will be stored upon completion.
4487  *
4488  * Returns CURLE_OK on success.
4489  */
4490 static CURLcode parse_login_details(const char *login, const size_t len,
4491                                     char **userp, char **passwdp,
4492                                     char **optionsp)
4493 {
4494   CURLcode result = CURLE_OK;
4495   char *ubuf = NULL;
4496   char *pbuf = NULL;
4497   char *obuf = NULL;
4498   const char *psep = NULL;
4499   const char *osep = NULL;
4500   size_t ulen;
4501   size_t plen;
4502   size_t olen;
4503
4504   /* Attempt to find the password separator */
4505   if(passwdp) {
4506     psep = strchr(login, ':');
4507
4508     /* Within the constraint of the login string */
4509     if(psep >= login + len)
4510       psep = NULL;
4511   }
4512
4513   /* Attempt to find the options separator */
4514   if(optionsp) {
4515     osep = strchr(login, ';');
4516
4517     /* Within the constraint of the login string */
4518     if(osep >= login + len)
4519       osep = NULL;
4520   }
4521
4522   /* Calculate the portion lengths */
4523   ulen = (psep ?
4524           (size_t)(osep && psep > osep ? osep - login : psep - login) :
4525           (osep ? (size_t)(osep - login) : len));
4526   plen = (psep ?
4527           (osep && osep > psep ? (size_t)(osep - psep) :
4528                                  (size_t)(login + len - psep)) - 1 : 0);
4529   olen = (osep ?
4530           (psep && psep > osep ? (size_t)(psep - osep) :
4531                                  (size_t)(login + len - osep)) - 1 : 0);
4532
4533   /* Allocate the user portion buffer */
4534   if(userp && ulen) {
4535     ubuf = malloc(ulen + 1);
4536     if(!ubuf)
4537       result = CURLE_OUT_OF_MEMORY;
4538   }
4539
4540   /* Allocate the password portion buffer */
4541   if(!result && passwdp && plen) {
4542     pbuf = malloc(plen + 1);
4543     if(!pbuf)
4544       result = CURLE_OUT_OF_MEMORY;
4545   }
4546
4547   /* Allocate the options portion buffer */
4548   if(!result && optionsp && olen) {
4549     obuf = malloc(olen + 1);
4550     if(!obuf)
4551       result = CURLE_OUT_OF_MEMORY;
4552   }
4553
4554   if(!result) {
4555     /* Store the user portion if necessary */
4556     if(ubuf) {
4557       memcpy(ubuf, login, ulen);
4558       ubuf[ulen] = '\0';
4559       Curl_safefree(*userp);
4560       *userp = ubuf;
4561     }
4562
4563     /* Store the password portion if necessary */
4564     if(pbuf) {
4565       memcpy(pbuf, psep + 1, plen);
4566       pbuf[plen] = '\0';
4567       Curl_safefree(*passwdp);
4568       *passwdp = pbuf;
4569     }
4570
4571     /* Store the options portion if necessary */
4572     if(obuf) {
4573       memcpy(obuf, osep + 1, olen);
4574       obuf[olen] = '\0';
4575       Curl_safefree(*optionsp);
4576       *optionsp = obuf;
4577     }
4578   }
4579
4580   return result;
4581 }
4582
4583 /*************************************************************
4584  * Figure out the remote port number and fix it in the URL
4585  *
4586  * No matter if we use a proxy or not, we have to figure out the remote
4587  * port number of various reasons.
4588  *
4589  * To be able to detect port number flawlessly, we must not confuse them
4590  * IPv6-specified addresses in the [0::1] style. (RFC2732)
4591  *
4592  * The conn->host.name is currently [user:passwd@]host[:port] where host
4593  * could be a hostname, IPv4 address or IPv6 address.
4594  *
4595  * The port number embedded in the URL is replaced, if necessary.
4596  *************************************************************/
4597 static CURLcode parse_remote_port(struct SessionHandle *data,
4598                                   struct connectdata *conn)
4599 {
4600   char *portptr;
4601   char endbracket;
4602
4603   /* Note that at this point, the IPv6 address cannot contain any scope
4604      suffix as that has already been removed in the parseurlandfillconn()
4605      function */
4606   if((1 == sscanf(conn->host.name, "[%*45[0123456789abcdefABCDEF:.]%c",
4607                   &endbracket)) &&
4608      (']' == endbracket)) {
4609     /* this is a RFC2732-style specified IP-address */
4610     conn->bits.ipv6_ip = TRUE;
4611
4612     conn->host.name++; /* skip over the starting bracket */
4613     portptr = strchr(conn->host.name, ']');
4614     if(portptr) {
4615       *portptr++ = '\0'; /* zero terminate, killing the bracket */
4616       if(':' != *portptr)
4617         portptr = NULL; /* no port number available */
4618     }
4619   }
4620   else {
4621 #ifdef ENABLE_IPV6
4622     struct in6_addr in6;
4623     if(Curl_inet_pton(AF_INET6, conn->host.name, &in6) > 0) {
4624       /* This is a numerical IPv6 address, meaning this is a wrongly formatted
4625          URL */
4626       failf(data, "IPv6 numerical address used in URL without brackets");
4627       return CURLE_URL_MALFORMAT;
4628     }
4629 #endif
4630
4631     portptr = strrchr(conn->host.name, ':');
4632   }
4633
4634   if(data->set.use_port && data->state.allow_port) {
4635     /* if set, we use this and ignore the port possibly given in the URL */
4636     conn->remote_port = (unsigned short)data->set.use_port;
4637     if(portptr)
4638       *portptr = '\0'; /* cut off the name there anyway - if there was a port
4639                       number - since the port number is to be ignored! */
4640     if(conn->bits.httpproxy) {
4641       /* we need to create new URL with the new port number */
4642       char *url;
4643       char type[12]="";
4644
4645       if(conn->bits.type_set)
4646         snprintf(type, sizeof(type), ";type=%c",
4647                  data->set.prefer_ascii?'A':
4648                  (data->set.ftp_list_only?'D':'I'));
4649
4650       /*
4651        * This synthesized URL isn't always right--suffixes like ;type=A are
4652        * stripped off. It would be better to work directly from the original
4653        * URL and simply replace the port part of it.
4654        */
4655       url = aprintf("%s://%s%s%s:%hu%s%s%s", conn->given->scheme,
4656                     conn->bits.ipv6_ip?"[":"", conn->host.name,
4657                     conn->bits.ipv6_ip?"]":"", conn->remote_port,
4658                     data->state.slash_removed?"/":"", data->state.path,
4659                     type);
4660       if(!url)
4661         return CURLE_OUT_OF_MEMORY;
4662
4663       if(data->change.url_alloc) {
4664         Curl_safefree(data->change.url);
4665         data->change.url_alloc = FALSE;
4666       }
4667
4668       data->change.url = url;
4669       data->change.url_alloc = TRUE;
4670     }
4671   }
4672   else if(portptr) {
4673     /* no CURLOPT_PORT given, extract the one from the URL */
4674
4675     char *rest;
4676     unsigned long port;
4677
4678     port=strtoul(portptr+1, &rest, 10);  /* Port number must be decimal */
4679
4680     if(rest != (portptr+1) && *rest == '\0') {
4681       /* The colon really did have only digits after it,
4682        * so it is either a port number or a mistake */
4683
4684       if(port > 0xffff) {   /* Single unix standard says port numbers are
4685                               * 16 bits long */
4686         failf(data, "Port number too large: %lu", port);
4687         return CURLE_URL_MALFORMAT;
4688       }
4689
4690       *portptr = '\0'; /* cut off the name there */
4691       conn->remote_port = curlx_ultous(port);
4692     }
4693     else if(!port)
4694       /* Browser behavior adaptation. If there's a colon with no digits after,
4695          just cut off the name there which makes us ignore the colon and just
4696          use the default port. Firefox and Chrome both do that. */
4697       *portptr = '\0';
4698   }
4699   return CURLE_OK;
4700 }
4701
4702 /*
4703  * Override the login details from the URL with that in the CURLOPT_USERPWD
4704  * option or a .netrc file, if applicable.
4705  */
4706 static void override_login(struct SessionHandle *data,
4707                            struct connectdata *conn,
4708                            char *user, char *passwd, char *options)
4709 {
4710   if(data->set.str[STRING_USERNAME]) {
4711     strncpy(user, data->set.str[STRING_USERNAME], MAX_CURL_USER_LENGTH);
4712     user[MAX_CURL_USER_LENGTH - 1] = '\0';   /* To be on safe side */
4713   }
4714
4715   if(data->set.str[STRING_PASSWORD]) {
4716     strncpy(passwd, data->set.str[STRING_PASSWORD], MAX_CURL_PASSWORD_LENGTH);
4717     passwd[MAX_CURL_PASSWORD_LENGTH - 1] = '\0'; /* To be on safe side */
4718   }
4719
4720   if(data->set.str[STRING_OPTIONS]) {
4721     strncpy(options, data->set.str[STRING_OPTIONS], MAX_CURL_OPTIONS_LENGTH);
4722     options[MAX_CURL_OPTIONS_LENGTH - 1] = '\0'; /* To be on safe side */
4723   }
4724
4725   conn->bits.netrc = FALSE;
4726   if(data->set.use_netrc != CURL_NETRC_IGNORED) {
4727     if(Curl_parsenetrc(conn->host.name,
4728                        user, passwd,
4729                        data->set.str[STRING_NETRC_FILE])) {
4730       infof(data, "Couldn't find host %s in the "
4731             DOT_CHAR "netrc file; using defaults\n",
4732             conn->host.name);
4733     }
4734     else {
4735       /* set bits.netrc TRUE to remember that we got the name from a .netrc
4736          file, so that it is safe to use even if we followed a Location: to a
4737          different host or similar. */
4738       conn->bits.netrc = TRUE;
4739
4740       conn->bits.user_passwd = TRUE; /* enable user+password */
4741     }
4742   }
4743 }
4744
4745 /*
4746  * Set password so it's available in the connection.
4747  */
4748 static CURLcode set_login(struct connectdata *conn,
4749                           const char *user, const char *passwd,
4750                           const char *options)
4751 {
4752   CURLcode result = CURLE_OK;
4753
4754   /* If our protocol needs a password and we have none, use the defaults */
4755   if((conn->handler->flags & PROTOPT_NEEDSPWD) && !conn->bits.user_passwd) {
4756     /* Store the default user */
4757     conn->user = strdup(CURL_DEFAULT_USER);
4758
4759     /* Store the default password */
4760     if(conn->user)
4761       conn->passwd = strdup(CURL_DEFAULT_PASSWORD);
4762     else
4763       conn->passwd = NULL;
4764
4765     /* This is the default password, so DON'T set conn->bits.user_passwd */
4766   }
4767   else {
4768     /* Store the user, zero-length if not set */
4769     conn->user = strdup(user);
4770
4771     /* Store the password (only if user is present), zero-length if not set */
4772     if(conn->user)
4773       conn->passwd = strdup(passwd);
4774     else
4775       conn->passwd = NULL;
4776   }
4777
4778   if(!conn->user || !conn->passwd)
4779     result = CURLE_OUT_OF_MEMORY;
4780
4781   /* Store the options, null if not set */
4782   if(!result && options[0]) {
4783     conn->options = strdup(options);
4784
4785     if(!conn->options)
4786       result = CURLE_OUT_OF_MEMORY;
4787   }
4788
4789   return result;
4790 }
4791
4792 /*************************************************************
4793  * Resolve the address of the server or proxy
4794  *************************************************************/
4795 static CURLcode resolve_server(struct SessionHandle *data,
4796                                struct connectdata *conn,
4797                                bool *async)
4798 {
4799   CURLcode result=CURLE_OK;
4800   long timeout_ms = Curl_timeleft(data, NULL, TRUE);
4801
4802   /*************************************************************
4803    * Resolve the name of the server or proxy
4804    *************************************************************/
4805   if(conn->bits.reuse)
4806     /* We're reusing the connection - no need to resolve anything, and
4807        fix_hostname() was called already in create_conn() for the re-use
4808        case. */
4809     *async = FALSE;
4810
4811   else {
4812     /* this is a fresh connect */
4813     int rc;
4814     struct Curl_dns_entry *hostaddr;
4815
4816     /* set a pointer to the hostname we display */
4817     fix_hostname(data, conn, &conn->host);
4818
4819     if(!conn->proxy.name || !*conn->proxy.name) {
4820       /* If not connecting via a proxy, extract the port from the URL, if it is
4821        * there, thus overriding any defaults that might have been set above. */
4822       conn->port =  conn->remote_port; /* it is the same port */
4823
4824       /* Resolve target host right on */
4825       rc = Curl_resolv_timeout(conn, conn->host.name, (int)conn->port,
4826                                &hostaddr, timeout_ms);
4827       if(rc == CURLRESOLV_PENDING)
4828         *async = TRUE;
4829
4830       else if(rc == CURLRESOLV_TIMEDOUT)
4831         result = CURLE_OPERATION_TIMEDOUT;
4832
4833       else if(!hostaddr) {
4834         failf(data, "Couldn't resolve host '%s'", conn->host.dispname);
4835         result =  CURLE_COULDNT_RESOLVE_HOST;
4836         /* don't return yet, we need to clean up the timeout first */
4837       }
4838     }
4839     else {
4840       /* This is a proxy that hasn't been resolved yet. */
4841
4842       /* IDN-fix the proxy name */
4843       fix_hostname(data, conn, &conn->proxy);
4844
4845       /* resolve proxy */
4846       rc = Curl_resolv_timeout(conn, conn->proxy.name, (int)conn->port,
4847                                &hostaddr, timeout_ms);
4848
4849       if(rc == CURLRESOLV_PENDING)
4850         *async = TRUE;
4851
4852       else if(rc == CURLRESOLV_TIMEDOUT)
4853         result = CURLE_OPERATION_TIMEDOUT;
4854
4855       else if(!hostaddr) {
4856         failf(data, "Couldn't resolve proxy '%s'", conn->proxy.dispname);
4857         result = CURLE_COULDNT_RESOLVE_PROXY;
4858         /* don't return yet, we need to clean up the timeout first */
4859       }
4860     }
4861     DEBUGASSERT(conn->dns_entry == NULL);
4862     conn->dns_entry = hostaddr;
4863   }
4864
4865   return result;
4866 }
4867
4868 /*
4869  * Cleanup the connection just allocated before we can move along and use the
4870  * previously existing one.  All relevant data is copied over and old_conn is
4871  * ready for freeing once this function returns.
4872  */
4873 static void reuse_conn(struct connectdata *old_conn,
4874                        struct connectdata *conn)
4875 {
4876   if(old_conn->proxy.rawalloc)
4877     free(old_conn->proxy.rawalloc);
4878
4879   /* free the SSL config struct from this connection struct as this was
4880      allocated in vain and is targeted for destruction */
4881   Curl_free_ssl_config(&old_conn->ssl_config);
4882
4883   conn->data = old_conn->data;
4884
4885   /* get the user+password information from the old_conn struct since it may
4886    * be new for this request even when we re-use an existing connection */
4887   conn->bits.user_passwd = old_conn->bits.user_passwd;
4888   if(conn->bits.user_passwd) {
4889     /* use the new user name and password though */
4890     Curl_safefree(conn->user);
4891     Curl_safefree(conn->passwd);
4892     conn->user = old_conn->user;
4893     conn->passwd = old_conn->passwd;
4894     old_conn->user = NULL;
4895     old_conn->passwd = NULL;
4896   }
4897
4898   conn->bits.proxy_user_passwd = old_conn->bits.proxy_user_passwd;
4899   if(conn->bits.proxy_user_passwd) {
4900     /* use the new proxy user name and proxy password though */
4901     Curl_safefree(conn->proxyuser);
4902     Curl_safefree(conn->proxypasswd);
4903     conn->proxyuser = old_conn->proxyuser;
4904     conn->proxypasswd = old_conn->proxypasswd;
4905     old_conn->proxyuser = NULL;
4906     old_conn->proxypasswd = NULL;
4907   }
4908
4909   /* host can change, when doing keepalive with a proxy or if the case is
4910      different this time etc */
4911   Curl_safefree(conn->host.rawalloc);
4912   conn->host=old_conn->host;
4913
4914   /* persist connection info in session handle */
4915   Curl_persistconninfo(conn);
4916
4917   /* re-use init */
4918   conn->bits.reuse = TRUE; /* yes, we're re-using here */
4919
4920   Curl_safefree(old_conn->user);
4921   Curl_safefree(old_conn->passwd);
4922   Curl_safefree(old_conn->proxyuser);
4923   Curl_safefree(old_conn->proxypasswd);
4924   Curl_safefree(old_conn->localdev);
4925
4926   Curl_llist_destroy(old_conn->send_pipe, NULL);
4927   Curl_llist_destroy(old_conn->recv_pipe, NULL);
4928
4929   old_conn->send_pipe = NULL;
4930   old_conn->recv_pipe = NULL;
4931
4932   Curl_safefree(old_conn->master_buffer);
4933 }
4934
4935 /**
4936  * create_conn() sets up a new connectdata struct, or re-uses an already
4937  * existing one, and resolves host name.
4938  *
4939  * if this function returns CURLE_OK and *async is set to TRUE, the resolve
4940  * response will be coming asynchronously. If *async is FALSE, the name is
4941  * already resolved.
4942  *
4943  * @param data The sessionhandle pointer
4944  * @param in_connect is set to the next connection data pointer
4945  * @param async is set TRUE when an async DNS resolution is pending
4946  * @see Curl_setup_conn()
4947  *
4948  * *NOTE* this function assigns the conn->data pointer!
4949  */
4950
4951 static CURLcode create_conn(struct SessionHandle *data,
4952                             struct connectdata **in_connect,
4953                             bool *async)
4954 {
4955   CURLcode result = CURLE_OK;
4956   struct connectdata *conn;
4957   struct connectdata *conn_temp = NULL;
4958   size_t urllen;
4959   char user[MAX_CURL_USER_LENGTH];
4960   char passwd[MAX_CURL_PASSWORD_LENGTH];
4961   char options[MAX_CURL_OPTIONS_LENGTH];
4962   bool reuse;
4963   char *proxy = NULL;
4964   bool prot_missing = FALSE;
4965   bool no_connections_available = FALSE;
4966   bool force_reuse;
4967   size_t max_host_connections = Curl_multi_max_host_connections(data->multi);
4968   size_t max_total_connections = Curl_multi_max_total_connections(data->multi);
4969
4970   *async = FALSE;
4971
4972   /*************************************************************
4973    * Check input data
4974    *************************************************************/
4975
4976   if(!data->change.url)
4977     return CURLE_URL_MALFORMAT;
4978
4979   /* First, split up the current URL in parts so that we can use the
4980      parts for checking against the already present connections. In order
4981      to not have to modify everything at once, we allocate a temporary
4982      connection data struct and fill in for comparison purposes. */
4983   conn = allocate_conn(data);
4984
4985   if(!conn)
4986     return CURLE_OUT_OF_MEMORY;
4987
4988   /* We must set the return variable as soon as possible, so that our
4989      parent can cleanup any possible allocs we may have done before
4990      any failure */
4991   *in_connect = conn;
4992
4993   /* This initing continues below, see the comment "Continue connectdata
4994    * initialization here" */
4995
4996   /***********************************************************
4997    * We need to allocate memory to store the path in. We get the size of the
4998    * full URL to be sure, and we need to make it at least 256 bytes since
4999    * other parts of the code will rely on this fact
5000    ***********************************************************/
5001 #define LEAST_PATH_ALLOC 256
5002   urllen=strlen(data->change.url);
5003   if(urllen < LEAST_PATH_ALLOC)
5004     urllen=LEAST_PATH_ALLOC;
5005
5006   /*
5007    * We malloc() the buffers below urllen+2 to make room for 2 possibilities:
5008    * 1 - an extra terminating zero
5009    * 2 - an extra slash (in case a syntax like "www.host.com?moo" is used)
5010    */
5011
5012   Curl_safefree(data->state.pathbuffer);
5013   data->state.path = NULL;
5014
5015   data->state.pathbuffer = malloc(urllen+2);
5016   if(NULL == data->state.pathbuffer)
5017     return CURLE_OUT_OF_MEMORY; /* really bad error */
5018   data->state.path = data->state.pathbuffer;
5019
5020   conn->host.rawalloc = malloc(urllen+2);
5021   if(NULL == conn->host.rawalloc) {
5022     Curl_safefree(data->state.pathbuffer);
5023     data->state.path = NULL;
5024     return CURLE_OUT_OF_MEMORY;
5025   }
5026
5027   conn->host.name = conn->host.rawalloc;
5028   conn->host.name[0] = 0;
5029
5030   result = parseurlandfillconn(data, conn, &prot_missing, user, passwd,
5031                                options);
5032   if(result != CURLE_OK)
5033     return result;
5034
5035   /*************************************************************
5036    * No protocol part in URL was used, add it!
5037    *************************************************************/
5038   if(prot_missing) {
5039     /* We're guessing prefixes here and if we're told to use a proxy or if
5040        we're gonna follow a Location: later or... then we need the protocol
5041        part added so that we have a valid URL. */
5042     char *reurl;
5043
5044     reurl = aprintf("%s://%s", conn->handler->scheme, data->change.url);
5045
5046     if(!reurl) {
5047       Curl_safefree(proxy);
5048       return CURLE_OUT_OF_MEMORY;
5049     }
5050
5051     if(data->change.url_alloc) {
5052       Curl_safefree(data->change.url);
5053       data->change.url_alloc = FALSE;
5054     }
5055
5056     data->change.url = reurl;
5057     data->change.url_alloc = TRUE; /* free this later */
5058   }
5059
5060   /*************************************************************
5061    * If the protocol can't handle url query strings, then cut
5062    * of the unhandable part
5063    *************************************************************/
5064   if((conn->given->flags&PROTOPT_NOURLQUERY)) {
5065     char *path_q_sep = strchr(conn->data->state.path, '?');
5066     if(path_q_sep) {
5067       /* according to rfc3986, allow the query (?foo=bar)
5068          also on protocols that can't handle it.
5069
5070          cut the string-part after '?'
5071       */
5072
5073       /* terminate the string */
5074       path_q_sep[0] = 0;
5075     }
5076   }
5077
5078 #ifndef CURL_DISABLE_PROXY
5079   /*************************************************************
5080    * Extract the user and password from the authentication string
5081    *************************************************************/
5082   if(conn->bits.proxy_user_passwd) {
5083     result = parse_proxy_auth(data, conn);
5084     if(result != CURLE_OK)
5085       return result;
5086   }
5087
5088   /*************************************************************
5089    * Detect what (if any) proxy to use
5090    *************************************************************/
5091   if(data->set.str[STRING_PROXY]) {
5092     proxy = strdup(data->set.str[STRING_PROXY]);
5093     /* if global proxy is set, this is it */
5094     if(NULL == proxy) {
5095       failf(data, "memory shortage");
5096       return CURLE_OUT_OF_MEMORY;
5097     }
5098   }
5099
5100   if(data->set.str[STRING_NOPROXY] &&
5101      check_noproxy(conn->host.name, data->set.str[STRING_NOPROXY])) {
5102     if(proxy) {
5103       free(proxy);  /* proxy is in exception list */
5104       proxy = NULL;
5105     }
5106   }
5107   else if(!proxy)
5108     proxy = detect_proxy(conn);
5109
5110   if(proxy && (!*proxy || (conn->handler->flags & PROTOPT_NONETWORK))) {
5111     free(proxy);  /* Don't bother with an empty proxy string or if the
5112                      protocol doesn't work with network */
5113     proxy = NULL;
5114   }
5115
5116   /***********************************************************************
5117    * If this is supposed to use a proxy, we need to figure out the proxy host
5118    * name, proxy type and port number, so that we can re-use an existing
5119    * connection that may exist registered to the same proxy host.
5120    ***********************************************************************/
5121   if(proxy) {
5122     result = parse_proxy(data, conn, proxy);
5123
5124     free(proxy); /* parse_proxy copies the proxy string */
5125
5126     if(result)
5127       return result;
5128
5129     if((conn->proxytype == CURLPROXY_HTTP) ||
5130        (conn->proxytype == CURLPROXY_HTTP_1_0)) {
5131 #ifdef CURL_DISABLE_HTTP
5132       /* asking for a HTTP proxy is a bit funny when HTTP is disabled... */
5133       return CURLE_UNSUPPORTED_PROTOCOL;
5134 #else
5135       /* force this connection's protocol to become HTTP if not already
5136          compatible - if it isn't tunneling through */
5137       if(!(conn->handler->protocol & CURLPROTO_HTTP) &&
5138          !conn->bits.tunnel_proxy)
5139         conn->handler = &Curl_handler_http;
5140
5141       conn->bits.httpproxy = TRUE;
5142 #endif
5143     }
5144     else
5145       conn->bits.httpproxy = FALSE; /* not a HTTP proxy */
5146     conn->bits.proxy = TRUE;
5147   }
5148   else {
5149     /* we aren't using the proxy after all... */
5150     conn->bits.proxy = FALSE;
5151     conn->bits.httpproxy = FALSE;
5152     conn->bits.proxy_user_passwd = FALSE;
5153     conn->bits.tunnel_proxy = FALSE;
5154   }
5155
5156 #endif /* CURL_DISABLE_PROXY */
5157
5158   /*************************************************************
5159    * Setup internals depending on protocol. Needs to be done after
5160    * we figured out what/if proxy to use.
5161    *************************************************************/
5162   result = setup_connection_internals(conn);
5163   if(result != CURLE_OK) {
5164     Curl_safefree(proxy);
5165     return result;
5166   }
5167
5168   conn->recv[FIRSTSOCKET] = Curl_recv_plain;
5169   conn->send[FIRSTSOCKET] = Curl_send_plain;
5170   conn->recv[SECONDARYSOCKET] = Curl_recv_plain;
5171   conn->send[SECONDARYSOCKET] = Curl_send_plain;
5172
5173   /***********************************************************************
5174    * file: is a special case in that it doesn't need a network connection
5175    ***********************************************************************/
5176 #ifndef CURL_DISABLE_FILE
5177   if(conn->handler->flags & PROTOPT_NONETWORK) {
5178     bool done;
5179     /* this is supposed to be the connect function so we better at least check
5180        that the file is present here! */
5181     DEBUGASSERT(conn->handler->connect_it);
5182     result = conn->handler->connect_it(conn, &done);
5183
5184     /* Setup a "faked" transfer that'll do nothing */
5185     if(CURLE_OK == result) {
5186       conn->data = data;
5187       conn->bits.tcpconnect[FIRSTSOCKET] = TRUE; /* we are "connected */
5188
5189       ConnectionStore(data, conn);
5190
5191       /*
5192        * Setup whatever necessary for a resumed transfer
5193        */
5194       result = setup_range(data);
5195       if(result) {
5196         DEBUGASSERT(conn->handler->done);
5197         /* we ignore the return code for the protocol-specific DONE */
5198         (void)conn->handler->done(conn, result, FALSE);
5199         return result;
5200       }
5201
5202       Curl_setup_transfer(conn, -1, -1, FALSE, NULL, /* no download */
5203                           -1, NULL); /* no upload */
5204     }
5205
5206     /* since we skip do_init() */
5207     Curl_speedinit(data);
5208
5209     return result;
5210   }
5211 #endif
5212
5213   /*************************************************************
5214    * If the protocol is using SSL and HTTP proxy is used, we set
5215    * the tunnel_proxy bit.
5216    *************************************************************/
5217   if((conn->given->flags&PROTOPT_SSL) && conn->bits.httpproxy)
5218     conn->bits.tunnel_proxy = TRUE;
5219
5220   /*************************************************************
5221    * Figure out the remote port number and fix it in the URL
5222    *************************************************************/
5223   result = parse_remote_port(data, conn);
5224   if(result != CURLE_OK)
5225     return result;
5226
5227   /* Check for overridden login details and set them accordingly */
5228   override_login(data, conn, user, passwd, options);
5229   result = set_login(conn, user, passwd, options);
5230   if(result != CURLE_OK)
5231     return result;
5232
5233   /* Get a cloned copy of the SSL config situation stored in the
5234      connection struct. But to get this going nicely, we must first make
5235      sure that the strings in the master copy are pointing to the correct
5236      strings in the session handle strings array!
5237
5238      Keep in mind that the pointers in the master copy are pointing to strings
5239      that will be freed as part of the SessionHandle struct, but all cloned
5240      copies will be separately allocated.
5241   */
5242   data->set.ssl.CApath = data->set.str[STRING_SSL_CAPATH];
5243   data->set.ssl.CAfile = data->set.str[STRING_SSL_CAFILE];
5244   data->set.ssl.CRLfile = data->set.str[STRING_SSL_CRLFILE];
5245   data->set.ssl.issuercert = data->set.str[STRING_SSL_ISSUERCERT];
5246   data->set.ssl.random_file = data->set.str[STRING_SSL_RANDOM_FILE];
5247   data->set.ssl.egdsocket = data->set.str[STRING_SSL_EGDSOCKET];
5248   data->set.ssl.cipher_list = data->set.str[STRING_SSL_CIPHER_LIST];
5249 #ifdef USE_TLS_SRP
5250   data->set.ssl.username = data->set.str[STRING_TLSAUTH_USERNAME];
5251   data->set.ssl.password = data->set.str[STRING_TLSAUTH_PASSWORD];
5252 #endif
5253
5254   if(!Curl_clone_ssl_config(&data->set.ssl, &conn->ssl_config))
5255     return CURLE_OUT_OF_MEMORY;
5256
5257   /*************************************************************
5258    * Check the current list of connections to see if we can
5259    * re-use an already existing one or if we have to create a
5260    * new one.
5261    *************************************************************/
5262
5263   /* reuse_fresh is TRUE if we are told to use a new connection by force, but
5264      we only acknowledge this option if this is not a re-used connection
5265      already (which happens due to follow-location or during a HTTP
5266      authentication phase). */
5267   if(data->set.reuse_fresh && !data->state.this_is_a_follow)
5268     reuse = FALSE;
5269   else
5270     reuse = ConnectionExists(data, conn, &conn_temp, &force_reuse);
5271
5272   /* If we found a reusable connection, we may still want to
5273      open a new connection if we are pipelining. */
5274   if(reuse && !force_reuse && IsPipeliningPossible(data, conn_temp)) {
5275     size_t pipelen = conn_temp->send_pipe->size + conn_temp->recv_pipe->size;
5276     if(pipelen > 0) {
5277       infof(data, "Found connection %d, with requests in the pipe (%d)\n",
5278             conn_temp->connection_id, pipelen);
5279
5280       if(conn_temp->bundle->num_connections < max_host_connections &&
5281          data->state.conn_cache->num_connections < max_total_connections) {
5282         /* We want a new connection anyway */
5283         reuse = FALSE;
5284
5285         infof(data, "We can reuse, but we want a new connection anyway\n");
5286       }
5287     }
5288   }
5289
5290   if(reuse) {
5291     /*
5292      * We already have a connection for this, we got the former connection
5293      * in the conn_temp variable and thus we need to cleanup the one we
5294      * just allocated before we can move along and use the previously
5295      * existing one.
5296      */
5297     conn_temp->inuse = TRUE; /* mark this as being in use so that no other
5298                                 handle in a multi stack may nick it */
5299     reuse_conn(conn, conn_temp);
5300     free(conn);          /* we don't need this anymore */
5301     conn = conn_temp;
5302     *in_connect = conn;
5303
5304     /* set a pointer to the hostname we display */
5305     fix_hostname(data, conn, &conn->host);
5306
5307     infof(data, "Re-using existing connection! (#%ld) with host %s\n",
5308           conn->connection_id,
5309           conn->proxy.name?conn->proxy.dispname:conn->host.dispname);
5310   }
5311   else {
5312     /* We have decided that we want a new connection. However, we may not
5313        be able to do that if we have reached the limit of how many
5314        connections we are allowed to open. */
5315     struct connectbundle *bundle;
5316
5317     bundle = Curl_conncache_find_bundle(data->state.conn_cache,
5318                                         conn->host.name);
5319     if(max_host_connections > 0 && bundle &&
5320        (bundle->num_connections >= max_host_connections)) {
5321       struct connectdata *conn_candidate;
5322
5323       /* The bundle is full. Let's see if we can kill a connection. */
5324       conn_candidate = find_oldest_idle_connection_in_bundle(data, bundle);
5325
5326       if(conn_candidate) {
5327         /* Set the connection's owner correctly, then kill it */
5328         conn_candidate->data = data;
5329         (void)Curl_disconnect(conn_candidate, /* dead_connection */ FALSE);
5330       }
5331       else
5332         no_connections_available = TRUE;
5333     }
5334
5335     if(max_total_connections > 0 &&
5336        (data->state.conn_cache->num_connections >= max_total_connections)) {
5337       struct connectdata *conn_candidate;
5338
5339       /* The cache is full. Let's see if we can kill a connection. */
5340       conn_candidate = find_oldest_idle_connection(data);
5341
5342       if(conn_candidate) {
5343         /* Set the connection's owner correctly, then kill it */
5344         conn_candidate->data = data;
5345         (void)Curl_disconnect(conn_candidate, /* dead_connection */ FALSE);
5346       }
5347       else
5348         no_connections_available = TRUE;
5349     }
5350
5351
5352     if(no_connections_available) {
5353       infof(data, "No connections available.\n");
5354
5355       conn_free(conn);
5356       *in_connect = NULL;
5357
5358       return CURLE_NO_CONNECTION_AVAILABLE;
5359     }
5360     else {
5361       /*
5362        * This is a brand new connection, so let's store it in the connection
5363        * cache of ours!
5364        */
5365       ConnectionStore(data, conn);
5366     }
5367   }
5368
5369   /* Mark the connection as used */
5370   conn->inuse = TRUE;
5371
5372   /* Setup and init stuff before DO starts, in preparing for the transfer. */
5373   do_init(conn);
5374
5375   /*
5376    * Setup whatever necessary for a resumed transfer
5377    */
5378   result = setup_range(data);
5379   if(result)
5380     return result;
5381
5382   /* Continue connectdata initialization here. */
5383
5384   /*
5385    * Inherit the proper values from the urldata struct AFTER we have arranged
5386    * the persistent connection stuff
5387    */
5388   conn->fread_func = data->set.fread_func;
5389   conn->fread_in = data->set.in;
5390   conn->seek_func = data->set.seek_func;
5391   conn->seek_client = data->set.seek_client;
5392
5393   /*************************************************************
5394    * Resolve the address of the server or proxy
5395    *************************************************************/
5396   result = resolve_server(data, conn, async);
5397
5398   return result;
5399 }
5400
5401 /* Curl_setup_conn() is called after the name resolve initiated in
5402  * create_conn() is all done.
5403  *
5404  * Curl_setup_conn() also handles reused connections
5405  *
5406  * conn->data MUST already have been setup fine (in create_conn)
5407  */
5408
5409 CURLcode Curl_setup_conn(struct connectdata *conn,
5410                          bool *protocol_done)
5411 {
5412   CURLcode result = CURLE_OK;
5413   struct SessionHandle *data = conn->data;
5414
5415   Curl_pgrsTime(data, TIMER_NAMELOOKUP);
5416
5417   if(conn->handler->flags & PROTOPT_NONETWORK) {
5418     /* nothing to setup when not using a network */
5419     *protocol_done = TRUE;
5420     return result;
5421   }
5422   *protocol_done = FALSE; /* default to not done */
5423
5424   /* set proxy_connect_closed to false unconditionally already here since it
5425      is used strictly to provide extra information to a parent function in the
5426      case of proxy CONNECT failures and we must make sure we don't have it
5427      lingering set from a previous invoke */
5428   conn->bits.proxy_connect_closed = FALSE;
5429
5430   /*
5431    * Set user-agent. Used for HTTP, but since we can attempt to tunnel
5432    * basically anything through a http proxy we can't limit this based on
5433    * protocol.
5434    */
5435   if(data->set.str[STRING_USERAGENT]) {
5436     Curl_safefree(conn->allocptr.uagent);
5437     conn->allocptr.uagent =
5438       aprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
5439     if(!conn->allocptr.uagent)
5440       return CURLE_OUT_OF_MEMORY;
5441   }
5442
5443   data->req.headerbytecount = 0;
5444
5445 #ifdef CURL_DO_LINEEND_CONV
5446   data->state.crlf_conversions = 0; /* reset CRLF conversion counter */
5447 #endif /* CURL_DO_LINEEND_CONV */
5448
5449   /* set start time here for timeout purposes in the connect procedure, it
5450      is later set again for the progress meter purpose */
5451   conn->now = Curl_tvnow();
5452
5453   for(;;) {
5454     /* loop for CURL_SERVER_CLOSED_CONNECTION */
5455
5456     if(CURL_SOCKET_BAD == conn->sock[FIRSTSOCKET]) {
5457       /* Try to connect only if not already connected */
5458       bool connected = FALSE;
5459
5460       result = ConnectPlease(data, conn, &connected);
5461
5462       if(result && !conn->ip_addr) {
5463         /* transport connection failure not related with authentication */
5464         conn->bits.tcpconnect[FIRSTSOCKET] = FALSE;
5465         return result;
5466       }
5467
5468       if(connected) {
5469         result = Curl_protocol_connect(conn, protocol_done);
5470         if(CURLE_OK == result)
5471           conn->bits.tcpconnect[FIRSTSOCKET] = TRUE;
5472       }
5473       else
5474         conn->bits.tcpconnect[FIRSTSOCKET] = FALSE;
5475
5476       /* if the connection was closed by the server while exchanging
5477          authentication informations, retry with the new set
5478          authentication information */
5479       if(conn->bits.proxy_connect_closed) {
5480         /* reset the error buffer */
5481         if(data->set.errorbuffer)
5482           data->set.errorbuffer[0] = '\0';
5483         data->state.errorbuf = FALSE;
5484         continue;
5485       }
5486
5487       if(CURLE_OK != result)
5488         return result;
5489     }
5490     else {
5491       Curl_pgrsTime(data, TIMER_CONNECT); /* we're connected already */
5492       Curl_pgrsTime(data, TIMER_APPCONNECT); /* we're connected already */
5493       conn->bits.tcpconnect[FIRSTSOCKET] = TRUE;
5494       *protocol_done = TRUE;
5495       Curl_verboseconnect(conn);
5496       Curl_updateconninfo(conn, conn->sock[FIRSTSOCKET]);
5497     }
5498     /* Stop the loop now */
5499     break;
5500   }
5501
5502   conn->now = Curl_tvnow(); /* time this *after* the connect is done, we
5503                                set this here perhaps a second time */
5504
5505 #ifdef __EMX__
5506   /*
5507    * This check is quite a hack. We're calling _fsetmode to fix the problem
5508    * with fwrite converting newline characters (you get mangled text files,
5509    * and corrupted binary files when you download to stdout and redirect it to
5510    * a file).
5511    */
5512
5513   if((data->set.out)->_handle == NULL) {
5514     _fsetmode(stdout, "b");
5515   }
5516 #endif
5517
5518   return result;
5519 }
5520
5521 CURLcode Curl_connect(struct SessionHandle *data,
5522                       struct connectdata **in_connect,
5523                       bool *asyncp,
5524                       bool *protocol_done)
5525 {
5526   CURLcode code;
5527
5528   *asyncp = FALSE; /* assume synchronous resolves by default */
5529
5530   /* call the stuff that needs to be called */
5531   code = create_conn(data, in_connect, asyncp);
5532
5533   if(CURLE_OK == code) {
5534     /* no error */
5535     if((*in_connect)->send_pipe->size || (*in_connect)->recv_pipe->size)
5536       /* pipelining */
5537       *protocol_done = TRUE;
5538     else if(!*asyncp) {
5539       /* DNS resolution is done: that's either because this is a reused
5540          connection, in which case DNS was unnecessary, or because DNS
5541          really did finish already (synch resolver/fast async resolve) */
5542       code = Curl_setup_conn(*in_connect, protocol_done);
5543     }
5544   }
5545
5546   if(code == CURLE_NO_CONNECTION_AVAILABLE) {
5547     *in_connect = NULL;
5548     return code;
5549   }
5550
5551   if(code && *in_connect) {
5552     /* We're not allowed to return failure with memory left allocated
5553        in the connectdata struct, free those here */
5554     Curl_disconnect(*in_connect, FALSE); /* close the connection */
5555     *in_connect = NULL;           /* return a NULL */
5556   }
5557
5558   return code;
5559 }
5560
5561 CURLcode Curl_done(struct connectdata **connp,
5562                    CURLcode status,  /* an error if this is called after an
5563                                         error was detected */
5564                    bool premature)
5565 {
5566   CURLcode result;
5567   struct connectdata *conn;
5568   struct SessionHandle *data;
5569
5570   DEBUGASSERT(*connp);
5571
5572   conn = *connp;
5573   data = conn->data;
5574
5575   if(conn->bits.done)
5576     /* Stop if Curl_done() has already been called */
5577     return CURLE_OK;
5578
5579   Curl_getoff_all_pipelines(data, conn);
5580
5581   if((conn->send_pipe->size + conn->recv_pipe->size != 0 &&
5582       !data->set.reuse_forbid &&
5583       !conn->bits.close))
5584     /* Stop if pipeline is not empty and we do not have to close
5585        connection. */
5586     return CURLE_OK;
5587
5588   conn->bits.done = TRUE; /* called just now! */
5589
5590   /* Cleanup possible redirect junk */
5591   if(data->req.newurl) {
5592     free(data->req.newurl);
5593     data->req.newurl = NULL;
5594   }
5595   if(data->req.location) {
5596     free(data->req.location);
5597     data->req.location = NULL;
5598   }
5599
5600   Curl_resolver_cancel(conn);
5601
5602   if(conn->dns_entry) {
5603     Curl_resolv_unlock(data, conn->dns_entry); /* done with this */
5604     conn->dns_entry = NULL;
5605   }
5606
5607   if(status == CURLE_ABORTED_BY_CALLBACK)
5608     /* When we're aborted due to a callback return code it basically have to
5609        be counted as premature as there is trouble ahead if we don't. We have
5610        many callbacks and protocols work differently, we could potentially do
5611        this more fine-grained in the future. */
5612     premature = TRUE;
5613
5614   /* this calls the protocol-specific function pointer previously set */
5615   if(conn->handler->done)
5616     result = conn->handler->done(conn, status, premature);
5617   else
5618     result = CURLE_OK;
5619
5620   if(Curl_pgrsDone(conn) && !result)
5621     result = CURLE_ABORTED_BY_CALLBACK;
5622
5623   /* if the transfer was completed in a paused state there can be buffered
5624      data left to write and then kill */
5625   if(data->state.tempwrite) {
5626     free(data->state.tempwrite);
5627     data->state.tempwrite = NULL;
5628   }
5629
5630   /* if data->set.reuse_forbid is TRUE, it means the libcurl client has
5631      forced us to close this no matter what we think.
5632
5633      if conn->bits.close is TRUE, it means that the connection should be
5634      closed in spite of all our efforts to be nice, due to protocol
5635      restrictions in our or the server's end
5636
5637      if premature is TRUE, it means this connection was said to be DONE before
5638      the entire request operation is complete and thus we can't know in what
5639      state it is for re-using, so we're forced to close it. In a perfect world
5640      we can add code that keep track of if we really must close it here or not,
5641      but currently we have no such detail knowledge.
5642   */
5643   if(data->set.reuse_forbid || conn->bits.close || premature) {
5644     CURLcode res2 = Curl_disconnect(conn, premature); /* close connection */
5645
5646     /* If we had an error already, make sure we return that one. But
5647        if we got a new error, return that. */
5648     if(!result && res2)
5649       result = res2;
5650   }
5651   else {
5652     /* the connection is no longer in use */
5653     if(ConnectionDone(data, conn)) {
5654       /* remember the most recently used connection */
5655       data->state.lastconnect = conn;
5656
5657       infof(data, "Connection #%ld to host %s left intact\n",
5658             conn->connection_id,
5659             conn->bits.httpproxy?conn->proxy.dispname:conn->host.dispname);
5660     }
5661     else
5662       data->state.lastconnect = NULL;
5663   }
5664
5665   *connp = NULL; /* to make the caller of this function better detect that
5666                     this was either closed or handed over to the connection
5667                     cache here, and therefore cannot be used from this point on
5668                  */
5669
5670   return result;
5671 }
5672
5673 /*
5674  * do_init() inits the readwrite session. This is inited each time (in the DO
5675  * function before the protocol-specific DO functions are invoked) for a
5676  * transfer, sometimes multiple times on the same SessionHandle. Make sure
5677  * nothing in here depends on stuff that are setup dynamically for the
5678  * transfer.
5679  */
5680
5681 static CURLcode do_init(struct connectdata *conn)
5682 {
5683   struct SessionHandle *data = conn->data;
5684   struct SingleRequest *k = &data->req;
5685
5686   conn->bits.done = FALSE; /* Curl_done() is not called yet */
5687   conn->bits.do_more = FALSE; /* by default there's no curl_do_more() to use */
5688   data->state.expect100header = FALSE;
5689
5690   if(data->set.opt_no_body)
5691     /* in HTTP lingo, no body means using the HEAD request... */
5692     data->set.httpreq = HTTPREQ_HEAD;
5693   else if(HTTPREQ_HEAD == data->set.httpreq)
5694     /* ... but if unset there really is no perfect method that is the
5695        "opposite" of HEAD but in reality most people probably think GET
5696        then. The important thing is that we can't let it remain HEAD if the
5697        opt_no_body is set FALSE since then we'll behave wrong when getting
5698        HTTP. */
5699     data->set.httpreq = HTTPREQ_GET;
5700
5701   /* NB: the content encoding software depends on this initialization */
5702   Curl_easy_initHandleData(data);
5703
5704   k->start = Curl_tvnow(); /* start time */
5705   k->now = k->start;   /* current time is now */
5706   k->header = TRUE; /* assume header */
5707
5708   k->bytecount = 0;
5709
5710   k->buf = data->state.buffer;
5711   k->uploadbuf = data->state.uploadbuffer;
5712   k->hbufp = data->state.headerbuff;
5713   k->ignorebody=FALSE;
5714
5715   Curl_speedinit(data);
5716
5717   Curl_pgrsSetUploadCounter(data, 0);
5718   Curl_pgrsSetDownloadCounter(data, 0);
5719
5720   return CURLE_OK;
5721 }
5722
5723 /*
5724  * do_complete is called when the DO actions are complete.
5725  *
5726  * We init chunking and trailer bits to their default values here immediately
5727  * before receiving any header data for the current request in the pipeline.
5728  */
5729 static void do_complete(struct connectdata *conn)
5730 {
5731   conn->data->req.chunk=FALSE;
5732   conn->data->req.maxfd = (conn->sockfd>conn->writesockfd?
5733                            conn->sockfd:conn->writesockfd)+1;
5734   Curl_pgrsTime(conn->data, TIMER_PRETRANSFER);
5735 }
5736
5737 CURLcode Curl_do(struct connectdata **connp, bool *done)
5738 {
5739   CURLcode result=CURLE_OK;
5740   struct connectdata *conn = *connp;
5741   struct SessionHandle *data = conn->data;
5742
5743   if(conn->handler->do_it) {
5744     /* generic protocol-specific function pointer set in curl_connect() */
5745     result = conn->handler->do_it(conn, done);
5746
5747     /* This was formerly done in transfer.c, but we better do it here */
5748     if((CURLE_SEND_ERROR == result) && conn->bits.reuse) {
5749       /*
5750        * If the connection is using an easy handle, call reconnect
5751        * to re-establish the connection.  Otherwise, let the multi logic
5752        * figure out how to re-establish the connection.
5753        */
5754       if(!data->multi) {
5755         result = Curl_reconnect_request(connp);
5756
5757         if(result == CURLE_OK) {
5758           /* ... finally back to actually retry the DO phase */
5759           conn = *connp; /* re-assign conn since Curl_reconnect_request
5760                             creates a new connection */
5761           result = conn->handler->do_it(conn, done);
5762         }
5763       }
5764       else
5765         return result;
5766     }
5767
5768     if((result == CURLE_OK) && *done)
5769       /* do_complete must be called after the protocol-specific DO function */
5770       do_complete(conn);
5771   }
5772   return result;
5773 }
5774
5775 /*
5776  * Curl_do_more() is called during the DO_MORE multi state. It is basically a
5777  * second stage DO state which (wrongly) was introduced to support FTP's
5778  * second connection.
5779  *
5780  * TODO: A future libcurl should be able to work away this state.
5781  *
5782  */
5783
5784 CURLcode Curl_do_more(struct connectdata *conn, bool *completed)
5785 {
5786   CURLcode result=CURLE_OK;
5787
5788   *completed = FALSE;
5789
5790   if(conn->handler->do_more)
5791     result = conn->handler->do_more(conn, completed);
5792
5793   if(!result && *completed)
5794     /* do_complete must be called after the protocol-specific DO function */
5795     do_complete(conn);
5796
5797   return result;
5798 }
5799
5800 /* Called on connect, and if there's already a protocol-specific struct
5801    allocated for a different connection, this frees it that it can be setup
5802    properly later on. */
5803 void Curl_reset_reqproto(struct connectdata *conn)
5804 {
5805   struct SessionHandle *data = conn->data;
5806   if(data->state.proto.generic && data->state.current_conn != conn) {
5807     free(data->state.proto.generic);
5808     data->state.proto.generic = NULL;
5809   }
5810   data->state.current_conn = conn;
5811 }