1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
31 #ifdef HAVE_ARPA_INET_H
32 #include <arpa/inet.h>
37 #ifdef HAVE_SYS_IOCTL_H
38 #include <sys/ioctl.h>
41 #ifdef HAVE_SYS_PARAM_H
42 #include <sys/param.h>
55 #error "We can't compile without socket() support!"
65 #include <stringprep.h>
66 #ifdef HAVE_IDN_FREE_H
69 /* prototype from idn-free.h, not provided by libidn 0.4.5's make install! */
70 void idn_free (void *ptr);
73 /* if idn_free() was not found in this version of libidn use free() instead */
74 #define idn_free(x) (free)(x)
76 #elif defined(USE_WIN32_IDN)
77 /* prototype for curl_win32_idn_to_ascii() */
78 int curl_win32_idn_to_ascii(const char *in, char **out);
79 #endif /* USE_LIBIDN */
85 #include "vtls/vtls.h"
96 #include "content_encoding.h"
97 #include "http_digest.h"
98 #include "http_negotiate.h"
102 #include "speedcheck.h"
104 #include "warnless.h"
105 #include "non-ascii.h"
106 #include "inet_pton.h"
108 /* And now for the protocols */
115 #include "curl_ldap.h"
120 #include "inet_ntop.h"
121 #include "curl_ntlm.h"
122 #include "curl_ntlm_wb.h"
124 #include "curl_rtmp.h"
126 #include "http_proxy.h"
128 #include "conncache.h"
129 #include "multihandle.h"
130 #include "pipeline.h"
134 #define _MPRINTF_REPLACE /* use our functions only */
135 #include <curl/mprintf.h>
137 #include "curl_memory.h"
138 /* The last #include file should be: */
139 #include "memdebug.h"
141 /* Local static prototypes */
142 static struct connectdata *
143 find_oldest_idle_connection(struct SessionHandle *data);
144 static struct connectdata *
145 find_oldest_idle_connection_in_bundle(struct SessionHandle *data,
146 struct connectbundle *bundle);
147 static void conn_free(struct connectdata *conn);
148 static void signalPipeClose(struct curl_llist *pipeline, bool pipe_broke);
149 static CURLcode do_init(struct connectdata *conn);
150 static CURLcode parse_url_login(struct SessionHandle *data,
151 struct connectdata *conn,
152 char **userptr, char **passwdptr,
154 static CURLcode parse_login_details(const char *login, const size_t len,
155 char **userptr, char **passwdptr,
161 static const struct Curl_handler * const protocols[] = {
163 #ifndef CURL_DISABLE_HTTP
167 #if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
171 #ifndef CURL_DISABLE_FTP
175 #if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)
179 #ifndef CURL_DISABLE_TELNET
180 &Curl_handler_telnet,
183 #ifndef CURL_DISABLE_DICT
187 #ifndef CURL_DISABLE_LDAP
189 #if !defined(CURL_DISABLE_LDAPS) && \
190 ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \
191 (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))
196 #ifndef CURL_DISABLE_FILE
200 #ifndef CURL_DISABLE_TFTP
209 #ifndef CURL_DISABLE_IMAP
216 #ifndef CURL_DISABLE_POP3
223 #if !defined(CURL_DISABLE_SMB) && defined(USE_NTLM) && \
224 (CURL_SIZEOF_CURL_OFF_T > 4) && \
225 (!defined(USE_WINDOWS_SSPI) || defined(USE_WIN32_CRYPTO))
232 #ifndef CURL_DISABLE_SMTP
239 #ifndef CURL_DISABLE_RTSP
243 #ifndef CURL_DISABLE_GOPHER
244 &Curl_handler_gopher,
251 &Curl_handler_rtmpte,
253 &Curl_handler_rtmpts,
256 (struct Curl_handler *) NULL
260 * Dummy handler for undefined protocol schemes.
263 static const struct Curl_handler Curl_handler_dummy = {
264 "<no protocol>", /* scheme */
265 ZERO_NULL, /* setup_connection */
266 ZERO_NULL, /* do_it */
267 ZERO_NULL, /* done */
268 ZERO_NULL, /* do_more */
269 ZERO_NULL, /* connect_it */
270 ZERO_NULL, /* connecting */
271 ZERO_NULL, /* doing */
272 ZERO_NULL, /* proto_getsock */
273 ZERO_NULL, /* doing_getsock */
274 ZERO_NULL, /* domore_getsock */
275 ZERO_NULL, /* perform_getsock */
276 ZERO_NULL, /* disconnect */
277 ZERO_NULL, /* readwrite */
280 PROTOPT_NONE /* flags */
283 void Curl_freeset(struct SessionHandle *data)
285 /* Free all dynamic strings stored in the data->set substructure. */
287 for(i=(enum dupstring)0; i < STRING_LAST; i++) {
288 Curl_safefree(data->set.str[i]);
291 if(data->change.referer_alloc) {
292 Curl_safefree(data->change.referer);
293 data->change.referer_alloc = FALSE;
295 data->change.referer = NULL;
296 if(data->change.url_alloc) {
297 Curl_safefree(data->change.url);
298 data->change.url_alloc = FALSE;
300 data->change.url = NULL;
303 static CURLcode setstropt(char **charp, char *s)
305 /* Release the previous storage at `charp' and replace by a dynamic storage
306 copy of `s'. Return CURLE_OK or CURLE_OUT_OF_MEMORY. */
308 Curl_safefree(*charp);
314 return CURLE_OUT_OF_MEMORY;
322 static CURLcode setstropt_userpwd(char *option, char **userp, char **passwdp)
324 CURLcode result = CURLE_OK;
328 /* Parse the login details if specified. It not then we treat NULL as a hint
329 to clear the existing data */
331 result = parse_login_details(option, strlen(option),
332 (userp ? &user : NULL),
333 (passwdp ? &passwd : NULL),
338 /* Store the username part of option if required */
340 if(!user && option && option[0] == ':') {
341 /* Allocate an empty string instead of returning NULL as user name */
344 result = CURLE_OUT_OF_MEMORY;
347 Curl_safefree(*userp);
351 /* Store the password part of option if required */
353 Curl_safefree(*passwdp);
361 CURLcode Curl_dupset(struct SessionHandle *dst, struct SessionHandle *src)
363 CURLcode result = CURLE_OK;
366 /* Copy src->set into dst->set first, then deal with the strings
370 /* clear all string pointers first */
371 memset(dst->set.str, 0, STRING_LAST * sizeof(char *));
373 /* duplicate all strings */
374 for(i=(enum dupstring)0; i< STRING_LASTZEROTERMINATED; i++) {
375 result = setstropt(&dst->set.str[i], src->set.str[i]);
380 /* duplicate memory areas pointed to */
381 i = STRING_COPYPOSTFIELDS;
382 if(src->set.postfieldsize && src->set.str[i]) {
383 /* postfieldsize is curl_off_t, Curl_memdup() takes a size_t ... */
384 dst->set.str[i] = Curl_memdup(src->set.str[i],
385 curlx_sotouz(src->set.postfieldsize));
387 return CURLE_OUT_OF_MEMORY;
388 /* point to the new copy */
389 dst->set.postfields = dst->set.str[i];
396 * This is the internal function curl_easy_cleanup() calls. This should
397 * cleanup and free all resources associated with this sessionhandle.
399 * NOTE: if we ever add something that attempts to write to a socket or
400 * similar here, we must ignore SIGPIPE first. It is currently only done
401 * when curl_easy_perform() is invoked.
404 CURLcode Curl_close(struct SessionHandle *data)
406 struct Curl_multi *m;
411 Curl_expire(data, 0); /* shut off timers */
416 /* This handle is still part of a multi handle, take care of this first
417 and detach this handle from there. */
418 curl_multi_remove_handle(data->multi, data);
421 /* when curl_easy_perform() is used, it creates its own multi handle to
422 use and this is the one */
423 curl_multi_cleanup(data->multi_easy);
425 /* Destroy the timeout list that is held in the easy handle. It is
426 /normally/ done by curl_multi_remove_handle() but this is "just in
428 if(data->state.timeoutlist) {
429 Curl_llist_destroy(data->state.timeoutlist, NULL);
430 data->state.timeoutlist = NULL;
433 data->magic = 0; /* force a clear AFTER the possibly enforced removal from
434 the multi handle, since that function uses the magic
437 if(data->state.rangestringalloc)
438 free(data->state.range);
440 /* Free the pathbuffer */
441 Curl_safefree(data->state.pathbuffer);
442 data->state.path = NULL;
444 /* freed here just in case DONE wasn't called */
445 Curl_free_request_state(data);
447 /* Close down all open SSL info and sessions */
448 Curl_ssl_close_all(data);
449 Curl_safefree(data->state.first_host);
450 Curl_safefree(data->state.scratch);
451 Curl_ssl_free_certinfo(data);
453 /* Cleanup possible redirect junk */
454 if(data->req.newurl) {
455 free(data->req.newurl);
456 data->req.newurl = NULL;
459 if(data->change.referer_alloc) {
460 Curl_safefree(data->change.referer);
461 data->change.referer_alloc = FALSE;
463 data->change.referer = NULL;
465 if(data->change.url_alloc) {
466 Curl_safefree(data->change.url);
467 data->change.url_alloc = FALSE;
469 data->change.url = NULL;
471 Curl_safefree(data->state.headerbuff);
473 Curl_flush_cookies(data, 1);
475 Curl_digest_cleanup(data);
477 Curl_safefree(data->info.contenttype);
478 Curl_safefree(data->info.wouldredirect);
480 /* this destroys the channel and we cannot use it anymore after this */
481 Curl_resolver_cleanup(data->state.resolver);
483 Curl_convert_close(data);
485 /* No longer a dirty share, if it exists */
487 Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
488 data->share->dirty--;
489 Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
498 * Initialize the UserDefined fields within a SessionHandle.
499 * This may be safely called on a new or existing SessionHandle.
501 CURLcode Curl_init_userdefined(struct UserDefined *set)
503 CURLcode result = CURLE_OK;
505 set->out = stdout; /* default output to stdout */
506 set->in = stdin; /* default input from stdin */
507 set->err = stderr; /* default stderr to stderr */
509 /* use fwrite as default function to store output */
510 set->fwrite_func = (curl_write_callback)fwrite;
512 /* use fread as default function to read input */
513 set->fread_func = (curl_read_callback)fread;
514 set->is_fread_set = 0;
515 set->is_fwrite_set = 0;
517 set->seek_func = ZERO_NULL;
518 set->seek_client = ZERO_NULL;
520 /* conversion callbacks for non-ASCII hosts */
521 set->convfromnetwork = ZERO_NULL;
522 set->convtonetwork = ZERO_NULL;
523 set->convfromutf8 = ZERO_NULL;
525 set->filesize = -1; /* we don't know the size */
526 set->postfieldsize = -1; /* unknown size */
527 set->maxredirs = -1; /* allow any amount by default */
529 set->httpreq = HTTPREQ_GET; /* Default HTTP request */
530 set->rtspreq = RTSPREQ_OPTIONS; /* Default RTSP request */
531 set->ftp_use_epsv = TRUE; /* FTP defaults to EPSV operations */
532 set->ftp_use_eprt = TRUE; /* FTP defaults to EPRT operations */
533 set->ftp_use_pret = FALSE; /* mainly useful for drftpd servers */
534 set->ftp_filemethod = FTPFILE_MULTICWD;
536 set->dns_cache_timeout = 60; /* Timeout every 60 seconds by default */
538 /* Set the default size of the SSL session ID cache */
539 set->ssl.max_ssl_sessions = 5;
541 set->proxyport = CURL_DEFAULT_PROXY_PORT; /* from url.h */
542 set->proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
543 set->httpauth = CURLAUTH_BASIC; /* defaults to basic */
544 set->proxyauth = CURLAUTH_BASIC; /* defaults to basic */
546 /* make libcurl quiet by default: */
547 set->hide_progress = TRUE; /* CURLOPT_NOPROGRESS changes these */
550 * libcurl 7.10 introduced SSL verification *by default*! This needs to be
551 * switched off unless wanted.
553 set->ssl.verifypeer = TRUE;
554 set->ssl.verifyhost = TRUE;
556 set->ssl.authtype = CURL_TLSAUTH_NONE;
558 set->ssh_auth_types = CURLSSH_AUTH_DEFAULT; /* defaults to any auth
560 set->ssl.sessionid = TRUE; /* session ID caching enabled by default */
562 set->new_file_perms = 0644; /* Default permissions */
563 set->new_directory_perms = 0755; /* Default permissions */
565 /* for the *protocols fields we don't use the CURLPROTO_ALL convenience
566 define since we internally only use the lower 16 bits for the passed
567 in bitmask to not conflict with the private bits */
568 set->allowed_protocols = CURLPROTO_ALL;
569 set->redir_protocols = CURLPROTO_ALL & /* All except FILE, SCP and SMB */
570 ~(CURLPROTO_FILE | CURLPROTO_SCP | CURLPROTO_SMB |
573 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
575 * disallow unprotected protection negotiation NEC reference implementation
576 * seem not to follow rfc1961 section 4.3/4.4
578 set->socks5_gssapi_nec = FALSE;
579 /* set default GSS-API service name */
580 result = setstropt(&set->str[STRING_SOCKS5_GSSAPI_SERVICE],
581 (char *) CURL_DEFAULT_SOCKS5_GSSAPI_SERVICE);
586 /* This is our preferred CA cert bundle/path since install time */
587 #if defined(CURL_CA_BUNDLE)
588 result = setstropt(&set->str[STRING_SSL_CAFILE], (char *) CURL_CA_BUNDLE);
589 #elif defined(CURL_CA_PATH)
590 result = setstropt(&set->str[STRING_SSL_CAPATH], (char *) CURL_CA_PATH);
593 set->wildcardmatch = FALSE;
594 set->chunk_bgn = ZERO_NULL;
595 set->chunk_end = ZERO_NULL;
597 /* tcp keepalives are disabled by default, but provide reasonable values for
598 * the interval and idle times.
600 set->tcp_keepalive = FALSE;
601 set->tcp_keepintvl = 60;
602 set->tcp_keepidle = 60;
604 set->ssl_enable_npn = TRUE;
605 set->ssl_enable_alpn = TRUE;
607 set->expect_100_timeout = 1000L; /* Wait for a second by default. */
608 set->sep_headers = TRUE; /* separated header lists by default */
615 * @param curl is a pointer to a sessionhandle pointer that gets set by this
620 CURLcode Curl_open(struct SessionHandle **curl)
623 struct SessionHandle *data;
625 /* Very simple start-up: alloc the struct, init it with zeroes and return */
626 data = calloc(1, sizeof(struct SessionHandle));
628 /* this is a very serious error */
629 DEBUGF(fprintf(stderr, "Error: calloc of SessionHandle failed\n"));
630 return CURLE_OUT_OF_MEMORY;
633 data->magic = CURLEASY_MAGIC_NUMBER;
635 result = Curl_resolver_init(&data->state.resolver);
637 DEBUGF(fprintf(stderr, "Error: resolver_init failed\n"));
642 /* We do some initial setup here, all those fields that can't be just 0 */
644 data->state.headerbuff = malloc(HEADERSIZE);
645 if(!data->state.headerbuff) {
646 DEBUGF(fprintf(stderr, "Error: malloc of headerbuff failed\n"));
647 result = CURLE_OUT_OF_MEMORY;
650 result = Curl_init_userdefined(&data->set);
652 data->state.headersize=HEADERSIZE;
654 Curl_convert_init(data);
656 /* most recent connection is not yet defined */
657 data->state.lastconnect = NULL;
659 data->progress.flags |= PGRS_HIDE;
660 data->state.current_speed = -1; /* init to negative == impossible */
662 data->wildcard.state = CURLWC_INIT;
663 data->wildcard.filelist = NULL;
664 data->set.fnmatch = ZERO_NULL;
665 data->set.maxconnects = DEFAULT_CONNCACHE_SIZE; /* for easy handles */
669 Curl_resolver_cleanup(data->state.resolver);
670 if(data->state.headerbuff)
671 free(data->state.headerbuff);
682 CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option,
686 CURLcode result = CURLE_OK;
688 #ifndef CURL_DISABLE_HTTP
693 case CURLOPT_DNS_CACHE_TIMEOUT:
694 data->set.dns_cache_timeout = va_arg(param, long);
696 case CURLOPT_DNS_USE_GLOBAL_CACHE:
697 /* remember we want this enabled */
698 arg = va_arg(param, long);
699 data->set.global_dns_cache = (0 != arg)?TRUE:FALSE;
701 case CURLOPT_SSL_CIPHER_LIST:
702 /* set a list of cipher we want to use in the SSL connection */
703 result = setstropt(&data->set.str[STRING_SSL_CIPHER_LIST],
704 va_arg(param, char *));
707 case CURLOPT_RANDOM_FILE:
709 * This is the path name to a file that contains random data to seed
710 * the random SSL stuff with. The file is only used for reading.
712 result = setstropt(&data->set.str[STRING_SSL_RANDOM_FILE],
713 va_arg(param, char *));
715 case CURLOPT_EGDSOCKET:
717 * The Entropy Gathering Daemon socket pathname
719 result = setstropt(&data->set.str[STRING_SSL_EGDSOCKET],
720 va_arg(param, char *));
722 case CURLOPT_MAXCONNECTS:
724 * Set the absolute number of maximum simultaneous alive connection that
725 * libcurl is allowed to have.
727 data->set.maxconnects = va_arg(param, long);
729 case CURLOPT_FORBID_REUSE:
731 * When this transfer is done, it must not be left to be reused by a
732 * subsequent transfer but shall be closed immediately.
734 data->set.reuse_forbid = (0 != va_arg(param, long))?TRUE:FALSE;
736 case CURLOPT_FRESH_CONNECT:
738 * This transfer shall not use a previously cached connection but
739 * should be made with a fresh new connect!
741 data->set.reuse_fresh = (0 != va_arg(param, long))?TRUE:FALSE;
743 case CURLOPT_VERBOSE:
745 * Verbose means infof() calls that give a lot of information about
746 * the connection and transfer procedures as well as internal choices.
748 data->set.verbose = (0 != va_arg(param, long))?TRUE:FALSE;
752 * Set to include the header in the general data output stream.
754 data->set.include_header = (0 != va_arg(param, long))?TRUE:FALSE;
756 case CURLOPT_NOPROGRESS:
758 * Shut off the internal supported progress meter
760 data->set.hide_progress = (0 != va_arg(param, long))?TRUE:FALSE;
761 if(data->set.hide_progress)
762 data->progress.flags |= PGRS_HIDE;
764 data->progress.flags &= ~PGRS_HIDE;
768 * Do not include the body part in the output data stream.
770 data->set.opt_no_body = (0 != va_arg(param, long))?TRUE:FALSE;
772 case CURLOPT_FAILONERROR:
774 * Don't output the >=400 error code HTML-page, but instead only
777 data->set.http_fail_on_error = (0 != va_arg(param, long))?TRUE:FALSE;
782 * We want to sent data to the remote host. If this is HTTP, that equals
783 * using the PUT request.
785 data->set.upload = (0 != va_arg(param, long))?TRUE:FALSE;
786 if(data->set.upload) {
787 /* If this is HTTP, PUT is what's needed to "upload" */
788 data->set.httpreq = HTTPREQ_PUT;
789 data->set.opt_no_body = FALSE; /* this is implied */
792 /* In HTTP, the opposite of upload is GET (unless NOBODY is true as
793 then this can be changed to HEAD later on) */
794 data->set.httpreq = HTTPREQ_GET;
796 case CURLOPT_FILETIME:
798 * Try to get the file time of the remote document. The time will
799 * later (possibly) become available using curl_easy_getinfo().
801 data->set.get_filetime = (0 != va_arg(param, long))?TRUE:FALSE;
803 case CURLOPT_FTP_CREATE_MISSING_DIRS:
805 * An FTP option that modifies an upload to create missing directories on
808 switch(va_arg(param, long)) {
810 data->set.ftp_create_missing_dirs = 0;
813 data->set.ftp_create_missing_dirs = 1;
816 data->set.ftp_create_missing_dirs = 2;
819 /* reserve other values for future use */
820 result = CURLE_UNKNOWN_OPTION;
824 case CURLOPT_SERVER_RESPONSE_TIMEOUT:
826 * Option that specifies how quickly an server response must be obtained
827 * before it is considered failure. For pingpong protocols.
829 data->set.server_response_timeout = va_arg( param , long ) * 1000;
831 case CURLOPT_TFTP_BLKSIZE:
833 * TFTP option that specifies the block size to use for data transmission
835 data->set.tftp_blksize = va_arg(param, long);
837 case CURLOPT_DIRLISTONLY:
839 * An option that changes the command to one that asks for a list
840 * only, no file info details.
842 data->set.ftp_list_only = (0 != va_arg(param, long))?TRUE:FALSE;
846 * We want to upload and append to an existing file.
848 data->set.ftp_append = (0 != va_arg(param, long))?TRUE:FALSE;
850 case CURLOPT_FTP_FILEMETHOD:
852 * How do access files over FTP.
854 data->set.ftp_filemethod = (curl_ftpfile)va_arg(param, long);
858 * Parse the $HOME/.netrc file
860 data->set.use_netrc = (enum CURL_NETRC_OPTION)va_arg(param, long);
862 case CURLOPT_NETRC_FILE:
864 * Use this file instead of the $HOME/.netrc file
866 result = setstropt(&data->set.str[STRING_NETRC_FILE],
867 va_arg(param, char *));
869 case CURLOPT_TRANSFERTEXT:
871 * This option was previously named 'FTPASCII'. Renamed to work with
872 * more protocols than merely FTP.
874 * Transfer using ASCII (instead of BINARY).
876 data->set.prefer_ascii = (0 != va_arg(param, long))?TRUE:FALSE;
878 case CURLOPT_TIMECONDITION:
880 * Set HTTP time condition. This must be one of the defines in the
881 * curl/curl.h header file.
883 data->set.timecondition = (curl_TimeCond)va_arg(param, long);
885 case CURLOPT_TIMEVALUE:
887 * This is the value to compare with the remote document with the
888 * method set with CURLOPT_TIMECONDITION
890 data->set.timevalue = (time_t)va_arg(param, long);
892 case CURLOPT_SSLVERSION:
894 * Set explicit SSL version to try to connect with, as some SSL
895 * implementations are lame.
897 data->set.ssl.version = va_arg(param, long);
900 #ifndef CURL_DISABLE_HTTP
901 case CURLOPT_AUTOREFERER:
903 * Switch on automatic referer that gets set if curl follows locations.
905 data->set.http_auto_referer = (0 != va_arg(param, long))?TRUE:FALSE;
908 case CURLOPT_ACCEPT_ENCODING:
910 * String to use at the value of Accept-Encoding header.
912 * If the encoding is set to "" we use an Accept-Encoding header that
913 * encompasses all the encodings we support.
914 * If the encoding is set to NULL we don't send an Accept-Encoding header
915 * and ignore an received Content-Encoding header.
918 argptr = va_arg(param, char *);
919 result = setstropt(&data->set.str[STRING_ENCODING],
920 (argptr && !*argptr)?
921 (char *) ALL_CONTENT_ENCODINGS: argptr);
924 case CURLOPT_TRANSFER_ENCODING:
925 data->set.http_transfer_encoding = (0 != va_arg(param, long))?TRUE:FALSE;
928 case CURLOPT_FOLLOWLOCATION:
930 * Follow Location: header hints on a HTTP-server.
932 data->set.http_follow_location = (0 != va_arg(param, long))?TRUE:FALSE;
935 case CURLOPT_UNRESTRICTED_AUTH:
937 * Send authentication (user+password) when following locations, even when
940 data->set.http_disable_hostname_check_before_authentication =
941 (0 != va_arg(param, long))?TRUE:FALSE;
944 case CURLOPT_MAXREDIRS:
946 * The maximum amount of hops you allow curl to follow Location:
947 * headers. This should mostly be used to detect never-ending loops.
949 data->set.maxredirs = va_arg(param, long);
952 case CURLOPT_POSTREDIR:
955 * Set the behaviour of POST when redirecting
956 * CURL_REDIR_GET_ALL - POST is changed to GET after 301 and 302
957 * CURL_REDIR_POST_301 - POST is kept as POST after 301
958 * CURL_REDIR_POST_302 - POST is kept as POST after 302
959 * CURL_REDIR_POST_303 - POST is kept as POST after 303
960 * CURL_REDIR_POST_ALL - POST is kept as POST after 301, 302 and 303
961 * other - POST is kept as POST after 301 and 302
963 int postRedir = curlx_sltosi(va_arg(param, long));
964 data->set.keep_post = postRedir & CURL_REDIR_POST_ALL;
969 /* Does this option serve a purpose anymore? Yes it does, when
970 CURLOPT_POSTFIELDS isn't used and the POST data is read off the
972 if(va_arg(param, long)) {
973 data->set.httpreq = HTTPREQ_POST;
974 data->set.opt_no_body = FALSE; /* this is implied */
977 data->set.httpreq = HTTPREQ_GET;
980 case CURLOPT_COPYPOSTFIELDS:
982 * A string with POST data. Makes curl HTTP POST. Even if it is NULL.
983 * If needed, CURLOPT_POSTFIELDSIZE must have been set prior to
984 * CURLOPT_COPYPOSTFIELDS and not altered later.
986 argptr = va_arg(param, char *);
988 if(!argptr || data->set.postfieldsize == -1)
989 result = setstropt(&data->set.str[STRING_COPYPOSTFIELDS], argptr);
992 * Check that requested length does not overflow the size_t type.
995 if((data->set.postfieldsize < 0) ||
996 ((sizeof(curl_off_t) != sizeof(size_t)) &&
997 (data->set.postfieldsize > (curl_off_t)((size_t)-1))))
998 result = CURLE_OUT_OF_MEMORY;
1002 (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
1004 /* Allocate even when size == 0. This satisfies the need of possible
1005 later address compare to detect the COPYPOSTFIELDS mode, and
1006 to mark that postfields is used rather than read function or
1009 p = malloc((size_t)(data->set.postfieldsize?
1010 data->set.postfieldsize:1));
1013 result = CURLE_OUT_OF_MEMORY;
1015 if(data->set.postfieldsize)
1016 memcpy(p, argptr, (size_t)data->set.postfieldsize);
1018 data->set.str[STRING_COPYPOSTFIELDS] = p;
1023 data->set.postfields = data->set.str[STRING_COPYPOSTFIELDS];
1024 data->set.httpreq = HTTPREQ_POST;
1027 case CURLOPT_POSTFIELDS:
1029 * Like above, but use static data instead of copying it.
1031 data->set.postfields = va_arg(param, void *);
1032 /* Release old copied data. */
1033 (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
1034 data->set.httpreq = HTTPREQ_POST;
1037 case CURLOPT_POSTFIELDSIZE:
1039 * The size of the POSTFIELD data to prevent libcurl to do strlen() to
1040 * figure it out. Enables binary posts.
1042 bigsize = va_arg(param, long);
1044 if(data->set.postfieldsize < bigsize &&
1045 data->set.postfields == data->set.str[STRING_COPYPOSTFIELDS]) {
1046 /* Previous CURLOPT_COPYPOSTFIELDS is no longer valid. */
1047 (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
1048 data->set.postfields = NULL;
1051 data->set.postfieldsize = bigsize;
1054 case CURLOPT_POSTFIELDSIZE_LARGE:
1056 * The size of the POSTFIELD data to prevent libcurl to do strlen() to
1057 * figure it out. Enables binary posts.
1059 bigsize = va_arg(param, curl_off_t);
1061 if(data->set.postfieldsize < bigsize &&
1062 data->set.postfields == data->set.str[STRING_COPYPOSTFIELDS]) {
1063 /* Previous CURLOPT_COPYPOSTFIELDS is no longer valid. */
1064 (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
1065 data->set.postfields = NULL;
1068 data->set.postfieldsize = bigsize;
1071 case CURLOPT_HTTPPOST:
1073 * Set to make us do HTTP POST
1075 data->set.httppost = va_arg(param, struct curl_httppost *);
1076 data->set.httpreq = HTTPREQ_POST_FORM;
1077 data->set.opt_no_body = FALSE; /* this is implied */
1080 case CURLOPT_REFERER:
1082 * String to set in the HTTP Referer: field.
1084 if(data->change.referer_alloc) {
1085 Curl_safefree(data->change.referer);
1086 data->change.referer_alloc = FALSE;
1088 result = setstropt(&data->set.str[STRING_SET_REFERER],
1089 va_arg(param, char *));
1090 data->change.referer = data->set.str[STRING_SET_REFERER];
1093 case CURLOPT_USERAGENT:
1095 * String to use in the HTTP User-Agent field
1097 result = setstropt(&data->set.str[STRING_USERAGENT],
1098 va_arg(param, char *));
1101 case CURLOPT_HTTPHEADER:
1103 * Set a list with HTTP headers to use (or replace internals with)
1105 data->set.headers = va_arg(param, struct curl_slist *);
1108 case CURLOPT_PROXYHEADER:
1110 * Set a list with proxy headers to use (or replace internals with)
1112 * Since CURLOPT_HTTPHEADER was the only way to set HTTP headers for a
1113 * long time we remain doing it this way until CURLOPT_PROXYHEADER is
1114 * used. As soon as this option has been used, if set to anything but
1115 * NULL, custom headers for proxies are only picked from this list.
1117 * Set this option to NULL to restore the previous behavior.
1119 data->set.proxyheaders = va_arg(param, struct curl_slist *);
1122 case CURLOPT_HEADEROPT:
1124 * Set header option.
1126 arg = va_arg(param, long);
1127 data->set.sep_headers = (arg & CURLHEADER_SEPARATE)? TRUE: FALSE;
1130 case CURLOPT_HTTP200ALIASES:
1132 * Set a list of aliases for HTTP 200 in response header
1134 data->set.http200aliases = va_arg(param, struct curl_slist *);
1137 #if !defined(CURL_DISABLE_COOKIES)
1138 case CURLOPT_COOKIE:
1140 * Cookie string to send to the remote server in the request.
1142 result = setstropt(&data->set.str[STRING_COOKIE],
1143 va_arg(param, char *));
1146 case CURLOPT_COOKIEFILE:
1148 * Set cookie file to read and parse. Can be used multiple times.
1150 argptr = (char *)va_arg(param, void *);
1152 struct curl_slist *cl;
1153 /* append the cookie file name to the list of file names, and deal with
1155 cl = curl_slist_append(data->change.cookielist, argptr);
1157 curl_slist_free_all(data->change.cookielist);
1158 data->change.cookielist = NULL;
1159 return CURLE_OUT_OF_MEMORY;
1161 data->change.cookielist = cl; /* store the list for later use */
1165 case CURLOPT_COOKIEJAR:
1167 * Set cookie file name to dump all cookies to when we're done.
1170 struct CookieInfo *newcookies;
1171 result = setstropt(&data->set.str[STRING_COOKIEJAR],
1172 va_arg(param, char *));
1175 * Activate the cookie parser. This may or may not already
1178 newcookies = Curl_cookie_init(data, NULL, data->cookies,
1179 data->set.cookiesession);
1181 result = CURLE_OUT_OF_MEMORY;
1182 data->cookies = newcookies;
1186 case CURLOPT_COOKIESESSION:
1188 * Set this option to TRUE to start a new "cookie session". It will
1189 * prevent the forthcoming read-cookies-from-file actions to accept
1190 * cookies that are marked as being session cookies, as they belong to a
1193 * In the original Netscape cookie spec, "session cookies" are cookies
1194 * with no expire date set. RFC2109 describes the same action if no
1195 * 'Max-Age' is set and RFC2965 includes the RFC2109 description and adds
1196 * a 'Discard' action that can enforce the discard even for cookies that
1199 * We run mostly with the original cookie spec, as hardly anyone implements
1202 data->set.cookiesession = (0 != va_arg(param, long))?TRUE:FALSE;
1205 case CURLOPT_COOKIELIST:
1206 argptr = va_arg(param, char *);
1211 if(Curl_raw_equal(argptr, "ALL")) {
1212 /* clear all cookies */
1213 Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
1214 Curl_cookie_clearall(data->cookies);
1215 Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
1217 else if(Curl_raw_equal(argptr, "SESS")) {
1218 /* clear session cookies */
1219 Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
1220 Curl_cookie_clearsess(data->cookies);
1221 Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
1223 else if(Curl_raw_equal(argptr, "FLUSH")) {
1224 /* flush cookies to file, takes care of the locking */
1225 Curl_flush_cookies(data, 0);
1227 else if(Curl_raw_equal(argptr, "RELOAD")) {
1228 /* reload cookies from file */
1229 Curl_cookie_loadfiles(data);
1234 /* if cookie engine was not running, activate it */
1235 data->cookies = Curl_cookie_init(data, NULL, NULL, TRUE);
1237 argptr = strdup(argptr);
1238 if(!argptr || !data->cookies) {
1239 result = CURLE_OUT_OF_MEMORY;
1240 Curl_safefree(argptr);
1243 Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
1245 if(checkprefix("Set-Cookie:", argptr))
1246 /* HTTP Header format line */
1247 Curl_cookie_add(data, data->cookies, TRUE, argptr + 11, NULL, NULL);
1250 /* Netscape format line */
1251 Curl_cookie_add(data, data->cookies, FALSE, argptr, NULL, NULL);
1253 Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
1259 #endif /* CURL_DISABLE_COOKIES */
1261 case CURLOPT_HTTPGET:
1263 * Set to force us do HTTP GET
1265 if(va_arg(param, long)) {
1266 data->set.httpreq = HTTPREQ_GET;
1267 data->set.upload = FALSE; /* switch off upload */
1268 data->set.opt_no_body = FALSE; /* this is implied */
1272 case CURLOPT_HTTP_VERSION:
1274 * This sets a requested HTTP version to be used. The value is one of
1275 * the listed enums in curl/curl.h.
1277 arg = va_arg(param, long);
1279 if(arg == CURL_HTTP_VERSION_2_0)
1280 return CURLE_UNSUPPORTED_PROTOCOL;
1282 data->set.httpversion = arg;
1285 case CURLOPT_HTTPAUTH:
1287 * Set HTTP Authentication type BITMASK.
1292 unsigned long auth = va_arg(param, unsigned long);
1294 if(auth == CURLAUTH_NONE) {
1295 data->set.httpauth = auth;
1299 /* the DIGEST_IE bit is only used to set a special marker, for all the
1300 rest we need to handle it as normal DIGEST */
1301 data->state.authhost.iestyle = (auth & CURLAUTH_DIGEST_IE)?TRUE:FALSE;
1303 if(auth & CURLAUTH_DIGEST_IE) {
1304 auth |= CURLAUTH_DIGEST; /* set standard digest bit */
1305 auth &= ~CURLAUTH_DIGEST_IE; /* unset ie digest bit */
1308 /* switch off bits we can't support */
1310 auth &= ~CURLAUTH_NTLM; /* no NTLM support */
1311 auth &= ~CURLAUTH_NTLM_WB; /* no NTLM_WB support */
1312 #elif !defined(NTLM_WB_ENABLED)
1313 auth &= ~CURLAUTH_NTLM_WB; /* no NTLM_WB support */
1316 auth &= ~CURLAUTH_NEGOTIATE; /* no Negotiate (SPNEGO) auth without
1320 /* check if any auth bit lower than CURLAUTH_ONLY is still set */
1323 while(bitcheck < 31) {
1324 if(auth & (1UL << bitcheck++)) {
1330 return CURLE_NOT_BUILT_IN; /* no supported types left! */
1332 data->set.httpauth = auth;
1336 case CURLOPT_EXPECT_100_TIMEOUT_MS:
1338 * Time to wait for a response to a HTTP request containing an
1339 * Expect: 100-continue header before sending the data anyway.
1341 data->set.expect_100_timeout = va_arg(param, long);
1344 #endif /* CURL_DISABLE_HTTP */
1346 case CURLOPT_CUSTOMREQUEST:
1348 * Set a custom string to use as request
1350 result = setstropt(&data->set.str[STRING_CUSTOMREQUEST],
1351 va_arg(param, char *));
1354 data->set.httpreq = HTTPREQ_CUSTOM;
1355 here, we continue as if we were using the already set type
1356 and this just changes the actual request keyword */
1359 #ifndef CURL_DISABLE_PROXY
1360 case CURLOPT_HTTPPROXYTUNNEL:
1362 * Tunnel operations through the proxy instead of normal proxy use
1364 data->set.tunnel_thru_httpproxy = (0 != va_arg(param, long))?TRUE:FALSE;
1367 case CURLOPT_PROXYPORT:
1369 * Explicitly set HTTP proxy port number.
1371 data->set.proxyport = va_arg(param, long);
1374 case CURLOPT_PROXYAUTH:
1376 * Set HTTP Authentication type BITMASK.
1381 unsigned long auth = va_arg(param, unsigned long);
1383 if(auth == CURLAUTH_NONE) {
1384 data->set.proxyauth = auth;
1388 /* the DIGEST_IE bit is only used to set a special marker, for all the
1389 rest we need to handle it as normal DIGEST */
1390 data->state.authproxy.iestyle = (auth & CURLAUTH_DIGEST_IE)?TRUE:FALSE;
1392 if(auth & CURLAUTH_DIGEST_IE) {
1393 auth |= CURLAUTH_DIGEST; /* set standard digest bit */
1394 auth &= ~CURLAUTH_DIGEST_IE; /* unset ie digest bit */
1396 /* switch off bits we can't support */
1398 auth &= ~CURLAUTH_NTLM; /* no NTLM support */
1399 auth &= ~CURLAUTH_NTLM_WB; /* no NTLM_WB support */
1400 #elif !defined(NTLM_WB_ENABLED)
1401 auth &= ~CURLAUTH_NTLM_WB; /* no NTLM_WB support */
1404 auth &= ~CURLAUTH_NEGOTIATE; /* no Negotiate (SPNEGO) auth without
1408 /* check if any auth bit lower than CURLAUTH_ONLY is still set */
1411 while(bitcheck < 31) {
1412 if(auth & (1UL << bitcheck++)) {
1418 return CURLE_NOT_BUILT_IN; /* no supported types left! */
1420 data->set.proxyauth = auth;
1426 * Set proxy server:port to use as HTTP proxy.
1428 * If the proxy is set to "" we explicitly say that we don't want to use a
1429 * proxy (even though there might be environment variables saying so).
1431 * Setting it to NULL, means no proxy but allows the environment variables
1434 result = setstropt(&data->set.str[STRING_PROXY],
1435 va_arg(param, char *));
1438 case CURLOPT_PROXYTYPE:
1440 * Set proxy type. HTTP/HTTP_1_0/SOCKS4/SOCKS4a/SOCKS5/SOCKS5_HOSTNAME
1442 data->set.proxytype = (curl_proxytype)va_arg(param, long);
1445 case CURLOPT_PROXY_TRANSFER_MODE:
1447 * set transfer mode (;type=<a|i>) when doing FTP via an HTTP proxy
1449 switch (va_arg(param, long)) {
1451 data->set.proxy_transfer_mode = FALSE;
1454 data->set.proxy_transfer_mode = TRUE;
1457 /* reserve other values for future use */
1458 result = CURLE_UNKNOWN_OPTION;
1462 #endif /* CURL_DISABLE_PROXY */
1464 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1465 case CURLOPT_SOCKS5_GSSAPI_SERVICE:
1467 * Set GSS-API service name
1469 result = setstropt(&data->set.str[STRING_SOCKS5_GSSAPI_SERVICE],
1470 va_arg(param, char *));
1473 case CURLOPT_SOCKS5_GSSAPI_NEC:
1475 * set flag for nec socks5 support
1477 data->set.socks5_gssapi_nec = (0 != va_arg(param, long))?TRUE:FALSE;
1481 case CURLOPT_HEADERDATA:
1483 * Custom pointer to pass the header write callback function
1485 data->set.writeheader = (void *)va_arg(param, void *);
1487 case CURLOPT_ERRORBUFFER:
1489 * Error buffer provided by the caller to get the human readable
1492 data->set.errorbuffer = va_arg(param, char *);
1494 case CURLOPT_WRITEDATA:
1496 * FILE pointer to write to. Or possibly
1497 * used as argument to the write callback.
1499 data->set.out = va_arg(param, void *);
1501 case CURLOPT_FTPPORT:
1503 * Use FTP PORT, this also specifies which IP address to use
1505 result = setstropt(&data->set.str[STRING_FTPPORT],
1506 va_arg(param, char *));
1507 data->set.ftp_use_port = (NULL != data->set.str[STRING_FTPPORT]) ?
1511 case CURLOPT_FTP_USE_EPRT:
1512 data->set.ftp_use_eprt = (0 != va_arg(param, long))?TRUE:FALSE;
1515 case CURLOPT_FTP_USE_EPSV:
1516 data->set.ftp_use_epsv = (0 != va_arg(param, long))?TRUE:FALSE;
1519 case CURLOPT_FTP_USE_PRET:
1520 data->set.ftp_use_pret = (0 != va_arg(param, long))?TRUE:FALSE;
1523 case CURLOPT_FTP_SSL_CCC:
1524 data->set.ftp_ccc = (curl_ftpccc)va_arg(param, long);
1527 case CURLOPT_FTP_SKIP_PASV_IP:
1529 * Enable or disable FTP_SKIP_PASV_IP, which will disable/enable the
1530 * bypass of the IP address in PASV responses.
1532 data->set.ftp_skip_ip = (0 != va_arg(param, long))?TRUE:FALSE;
1535 case CURLOPT_READDATA:
1537 * FILE pointer to read the file to be uploaded from. Or possibly
1538 * used as argument to the read callback.
1540 data->set.in = va_arg(param, void *);
1542 case CURLOPT_INFILESIZE:
1544 * If known, this should inform curl about the file size of the
1545 * to-be-uploaded file.
1547 data->set.filesize = va_arg(param, long);
1549 case CURLOPT_INFILESIZE_LARGE:
1551 * If known, this should inform curl about the file size of the
1552 * to-be-uploaded file.
1554 data->set.filesize = va_arg(param, curl_off_t);
1556 case CURLOPT_LOW_SPEED_LIMIT:
1558 * The low speed limit that if transfers are below this for
1559 * CURLOPT_LOW_SPEED_TIME, the transfer is aborted.
1561 data->set.low_speed_limit=va_arg(param, long);
1563 case CURLOPT_MAX_SEND_SPEED_LARGE:
1565 * When transfer uploads are faster then CURLOPT_MAX_SEND_SPEED_LARGE
1566 * bytes per second the transfer is throttled..
1568 data->set.max_send_speed=va_arg(param, curl_off_t);
1570 case CURLOPT_MAX_RECV_SPEED_LARGE:
1572 * When receiving data faster than CURLOPT_MAX_RECV_SPEED_LARGE bytes per
1573 * second the transfer is throttled..
1575 data->set.max_recv_speed=va_arg(param, curl_off_t);
1577 case CURLOPT_LOW_SPEED_TIME:
1579 * The low speed time that if transfers are below the set
1580 * CURLOPT_LOW_SPEED_LIMIT during this time, the transfer is aborted.
1582 data->set.low_speed_time=va_arg(param, long);
1588 if(data->change.url_alloc) {
1589 /* the already set URL is allocated, free it first! */
1590 Curl_safefree(data->change.url);
1591 data->change.url_alloc = FALSE;
1593 result = setstropt(&data->set.str[STRING_SET_URL],
1594 va_arg(param, char *));
1595 data->change.url = data->set.str[STRING_SET_URL];
1599 * The port number to use when getting the URL
1601 data->set.use_port = va_arg(param, long);
1603 case CURLOPT_TIMEOUT:
1605 * The maximum time you allow curl to use for a single transfer
1608 data->set.timeout = va_arg(param, long) * 1000L;
1611 case CURLOPT_TIMEOUT_MS:
1612 data->set.timeout = va_arg(param, long);
1615 case CURLOPT_CONNECTTIMEOUT:
1617 * The maximum time you allow curl to use to connect.
1619 data->set.connecttimeout = va_arg(param, long) * 1000L;
1622 case CURLOPT_CONNECTTIMEOUT_MS:
1623 data->set.connecttimeout = va_arg(param, long);
1626 case CURLOPT_ACCEPTTIMEOUT_MS:
1628 * The maximum time you allow curl to wait for server connect
1630 data->set.accepttimeout = va_arg(param, long);
1633 case CURLOPT_USERPWD:
1635 * user:password to use in the operation
1637 result = setstropt_userpwd(va_arg(param, char *),
1638 &data->set.str[STRING_USERNAME],
1639 &data->set.str[STRING_PASSWORD]);
1642 case CURLOPT_USERNAME:
1644 * authentication user name to use in the operation
1646 result = setstropt(&data->set.str[STRING_USERNAME],
1647 va_arg(param, char *));
1650 case CURLOPT_PASSWORD:
1652 * authentication password to use in the operation
1654 result = setstropt(&data->set.str[STRING_PASSWORD],
1655 va_arg(param, char *));
1658 case CURLOPT_LOGIN_OPTIONS:
1660 * authentication options to use in the operation
1662 result = setstropt(&data->set.str[STRING_OPTIONS],
1663 va_arg(param, char *));
1666 case CURLOPT_XOAUTH2_BEARER:
1668 * XOAUTH2 bearer token to use in the operation
1670 result = setstropt(&data->set.str[STRING_BEARER],
1671 va_arg(param, char *));
1674 case CURLOPT_POSTQUOTE:
1676 * List of RAW FTP commands to use after a transfer
1678 data->set.postquote = va_arg(param, struct curl_slist *);
1680 case CURLOPT_PREQUOTE:
1682 * List of RAW FTP commands to use prior to RETR (Wesley Laxton)
1684 data->set.prequote = va_arg(param, struct curl_slist *);
1688 * List of RAW FTP commands to use before a transfer
1690 data->set.quote = va_arg(param, struct curl_slist *);
1692 case CURLOPT_RESOLVE:
1694 * List of NAME:[address] names to populate the DNS cache with
1695 * Prefix the NAME with dash (-) to _remove_ the name from the cache.
1697 * Names added with this API will remain in the cache until explicitly
1698 * removed or the handle is cleaned up.
1700 * This API can remove any name from the DNS cache, but only entries
1701 * that aren't actually in use right now will be pruned immediately.
1703 data->set.resolve = va_arg(param, struct curl_slist *);
1704 data->change.resolve = data->set.resolve;
1706 case CURLOPT_PROGRESSFUNCTION:
1708 * Progress callback function
1710 data->set.fprogress = va_arg(param, curl_progress_callback);
1711 if(data->set.fprogress)
1712 data->progress.callback = TRUE; /* no longer internal */
1714 data->progress.callback = FALSE; /* NULL enforces internal */
1717 case CURLOPT_XFERINFOFUNCTION:
1719 * Transfer info callback function
1721 data->set.fxferinfo = va_arg(param, curl_xferinfo_callback);
1722 if(data->set.fxferinfo)
1723 data->progress.callback = TRUE; /* no longer internal */
1725 data->progress.callback = FALSE; /* NULL enforces internal */
1729 case CURLOPT_PROGRESSDATA:
1731 * Custom client data to pass to the progress callback
1733 data->set.progress_client = va_arg(param, void *);
1736 #ifndef CURL_DISABLE_PROXY
1737 case CURLOPT_PROXYUSERPWD:
1739 * user:password needed to use the proxy
1741 result = setstropt_userpwd(va_arg(param, char *),
1742 &data->set.str[STRING_PROXYUSERNAME],
1743 &data->set.str[STRING_PROXYPASSWORD]);
1745 case CURLOPT_PROXYUSERNAME:
1747 * authentication user name to use in the operation
1749 result = setstropt(&data->set.str[STRING_PROXYUSERNAME],
1750 va_arg(param, char *));
1752 case CURLOPT_PROXYPASSWORD:
1754 * authentication password to use in the operation
1756 result = setstropt(&data->set.str[STRING_PROXYPASSWORD],
1757 va_arg(param, char *));
1759 case CURLOPT_NOPROXY:
1761 * proxy exception list
1763 result = setstropt(&data->set.str[STRING_NOPROXY],
1764 va_arg(param, char *));
1770 * What range of the file you want to transfer
1772 result = setstropt(&data->set.str[STRING_SET_RANGE],
1773 va_arg(param, char *));
1775 case CURLOPT_RESUME_FROM:
1777 * Resume transfer at the give file position
1779 data->set.set_resume_from = va_arg(param, long);
1781 case CURLOPT_RESUME_FROM_LARGE:
1783 * Resume transfer at the give file position
1785 data->set.set_resume_from = va_arg(param, curl_off_t);
1787 case CURLOPT_DEBUGFUNCTION:
1789 * stderr write callback.
1791 data->set.fdebug = va_arg(param, curl_debug_callback);
1793 * if the callback provided is NULL, it'll use the default callback
1796 case CURLOPT_DEBUGDATA:
1798 * Set to a void * that should receive all error writes. This
1799 * defaults to CURLOPT_STDERR for normal operations.
1801 data->set.debugdata = va_arg(param, void *);
1803 case CURLOPT_STDERR:
1805 * Set to a FILE * that should receive all error writes. This
1806 * defaults to stderr for normal operations.
1808 data->set.err = va_arg(param, FILE *);
1810 data->set.err = stderr;
1812 case CURLOPT_HEADERFUNCTION:
1814 * Set header write callback
1816 data->set.fwrite_header = va_arg(param, curl_write_callback);
1818 case CURLOPT_WRITEFUNCTION:
1820 * Set data write callback
1822 data->set.fwrite_func = va_arg(param, curl_write_callback);
1823 if(!data->set.fwrite_func) {
1824 data->set.is_fwrite_set = 0;
1825 /* When set to NULL, reset to our internal default function */
1826 data->set.fwrite_func = (curl_write_callback)fwrite;
1829 data->set.is_fwrite_set = 1;
1831 case CURLOPT_READFUNCTION:
1833 * Read data callback
1835 data->set.fread_func = va_arg(param, curl_read_callback);
1836 if(!data->set.fread_func) {
1837 data->set.is_fread_set = 0;
1838 /* When set to NULL, reset to our internal default function */
1839 data->set.fread_func = (curl_read_callback)fread;
1842 data->set.is_fread_set = 1;
1844 case CURLOPT_SEEKFUNCTION:
1846 * Seek callback. Might be NULL.
1848 data->set.seek_func = va_arg(param, curl_seek_callback);
1850 case CURLOPT_SEEKDATA:
1852 * Seek control callback. Might be NULL.
1854 data->set.seek_client = va_arg(param, void *);
1856 case CURLOPT_CONV_FROM_NETWORK_FUNCTION:
1858 * "Convert from network encoding" callback
1860 data->set.convfromnetwork = va_arg(param, curl_conv_callback);
1862 case CURLOPT_CONV_TO_NETWORK_FUNCTION:
1864 * "Convert to network encoding" callback
1866 data->set.convtonetwork = va_arg(param, curl_conv_callback);
1868 case CURLOPT_CONV_FROM_UTF8_FUNCTION:
1870 * "Convert from UTF-8 encoding" callback
1872 data->set.convfromutf8 = va_arg(param, curl_conv_callback);
1874 case CURLOPT_IOCTLFUNCTION:
1876 * I/O control callback. Might be NULL.
1878 data->set.ioctl_func = va_arg(param, curl_ioctl_callback);
1880 case CURLOPT_IOCTLDATA:
1882 * I/O control data pointer. Might be NULL.
1884 data->set.ioctl_client = va_arg(param, void *);
1886 case CURLOPT_SSLCERT:
1888 * String that holds file name of the SSL certificate to use
1890 result = setstropt(&data->set.str[STRING_CERT],
1891 va_arg(param, char *));
1893 case CURLOPT_SSLCERTTYPE:
1895 * String that holds file type of the SSL certificate to use
1897 result = setstropt(&data->set.str[STRING_CERT_TYPE],
1898 va_arg(param, char *));
1900 case CURLOPT_SSLKEY:
1902 * String that holds file name of the SSL key to use
1904 result = setstropt(&data->set.str[STRING_KEY],
1905 va_arg(param, char *));
1907 case CURLOPT_SSLKEYTYPE:
1909 * String that holds file type of the SSL key to use
1911 result = setstropt(&data->set.str[STRING_KEY_TYPE],
1912 va_arg(param, char *));
1914 case CURLOPT_KEYPASSWD:
1916 * String that holds the SSL or SSH private key password.
1918 result = setstropt(&data->set.str[STRING_KEY_PASSWD],
1919 va_arg(param, char *));
1921 case CURLOPT_SSLENGINE:
1923 * String that holds the SSL crypto engine.
1925 argptr = va_arg(param, char *);
1926 if(argptr && argptr[0])
1927 result = Curl_ssl_set_engine(data, argptr);
1930 case CURLOPT_SSLENGINE_DEFAULT:
1932 * flag to set engine as default.
1934 result = Curl_ssl_set_engine_default(data);
1938 * Kludgy option to enable CRLF conversions. Subject for removal.
1940 data->set.crlf = (0 != va_arg(param, long))?TRUE:FALSE;
1943 case CURLOPT_INTERFACE:
1945 * Set what interface or address/hostname to bind the socket to when
1946 * performing an operation and thus what from-IP your connection will use.
1948 result = setstropt(&data->set.str[STRING_DEVICE],
1949 va_arg(param, char *));
1951 case CURLOPT_LOCALPORT:
1953 * Set what local port to bind the socket to when performing an operation.
1955 data->set.localport = curlx_sltous(va_arg(param, long));
1957 case CURLOPT_LOCALPORTRANGE:
1959 * Set number of local ports to try, starting with CURLOPT_LOCALPORT.
1961 data->set.localportrange = curlx_sltosi(va_arg(param, long));
1963 case CURLOPT_KRBLEVEL:
1965 * A string that defines the kerberos security level.
1967 result = setstropt(&data->set.str[STRING_KRB_LEVEL],
1968 va_arg(param, char *));
1969 data->set.krb = (NULL != data->set.str[STRING_KRB_LEVEL])?TRUE:FALSE;
1971 case CURLOPT_GSSAPI_DELEGATION:
1973 * GSS-API credential delegation
1975 data->set.gssapi_delegation = va_arg(param, long);
1977 case CURLOPT_SSL_VERIFYPEER:
1979 * Enable peer SSL verifying.
1981 data->set.ssl.verifypeer = (0 != va_arg(param, long))?TRUE:FALSE;
1983 case CURLOPT_SSL_VERIFYHOST:
1985 * Enable verification of the host name in the peer certificate
1987 arg = va_arg(param, long);
1989 /* Obviously people are not reading documentation and too many thought
1990 this argument took a boolean when it wasn't and misused it. We thus ban
1991 1 as a sensible input and we warn about its use. Then we only have the
1992 2 action internally stored as TRUE. */
1995 failf(data, "CURLOPT_SSL_VERIFYHOST no longer supports 1 as value!");
1996 return CURLE_BAD_FUNCTION_ARGUMENT;
1999 data->set.ssl.verifyhost = (0 != arg)?TRUE:FALSE;
2001 case CURLOPT_SSL_VERIFYSTATUS:
2003 * Enable certificate status verifying.
2005 if(!Curl_ssl_cert_status_request()) {
2006 result = CURLE_NOT_BUILT_IN;
2010 data->set.ssl.verifystatus = (0 != va_arg(param, long))?TRUE:FALSE;
2012 case CURLOPT_SSL_CTX_FUNCTION:
2013 #ifdef have_curlssl_ssl_ctx
2015 * Set a SSL_CTX callback
2017 data->set.ssl.fsslctx = va_arg(param, curl_ssl_ctx_callback);
2019 result = CURLE_NOT_BUILT_IN;
2022 case CURLOPT_SSL_CTX_DATA:
2023 #ifdef have_curlssl_ssl_ctx
2025 * Set a SSL_CTX callback parameter pointer
2027 data->set.ssl.fsslctxp = va_arg(param, void *);
2029 result = CURLE_NOT_BUILT_IN;
2032 case CURLOPT_CERTINFO:
2033 #ifdef have_curlssl_certinfo
2034 data->set.ssl.certinfo = (0 != va_arg(param, long))?TRUE:FALSE;
2036 result = CURLE_NOT_BUILT_IN;
2039 case CURLOPT_PINNEDPUBLICKEY:
2041 * Set pinned public key for SSL connection.
2042 * Specify file name of the public key in DER format.
2044 result = setstropt(&data->set.str[STRING_SSL_PINNEDPUBLICKEY],
2045 va_arg(param, char *));
2047 case CURLOPT_CAINFO:
2049 * Set CA info for SSL connection. Specify file name of the CA certificate
2051 result = setstropt(&data->set.str[STRING_SSL_CAFILE],
2052 va_arg(param, char *));
2054 case CURLOPT_CAPATH:
2055 #ifdef have_curlssl_ca_path /* not supported by all backends */
2057 * Set CA path info for SSL connection. Specify directory name of the CA
2058 * certificates which have been prepared using openssl c_rehash utility.
2060 /* This does not work on windows. */
2061 result = setstropt(&data->set.str[STRING_SSL_CAPATH],
2062 va_arg(param, char *));
2064 result = CURLE_NOT_BUILT_IN;
2067 case CURLOPT_CRLFILE:
2069 * Set CRL file info for SSL connection. Specify file name of the CRL
2070 * to check certificates revocation
2072 result = setstropt(&data->set.str[STRING_SSL_CRLFILE],
2073 va_arg(param, char *));
2075 case CURLOPT_ISSUERCERT:
2077 * Set Issuer certificate file
2078 * to check certificates issuer
2080 result = setstropt(&data->set.str[STRING_SSL_ISSUERCERT],
2081 va_arg(param, char *));
2083 case CURLOPT_TELNETOPTIONS:
2085 * Set a linked list of telnet options
2087 data->set.telnet_options = va_arg(param, struct curl_slist *);
2090 case CURLOPT_BUFFERSIZE:
2092 * The application kindly asks for a differently sized receive buffer.
2093 * If it seems reasonable, we'll use it.
2095 data->set.buffer_size = va_arg(param, long);
2097 if((data->set.buffer_size> (BUFSIZE -1 )) ||
2098 (data->set.buffer_size < 1))
2099 data->set.buffer_size = 0; /* huge internal default */
2103 case CURLOPT_NOSIGNAL:
2105 * The application asks not to set any signal() or alarm() handlers,
2106 * even when using a timeout.
2108 data->set.no_signal = (0 != va_arg(param, long))?TRUE:FALSE;
2113 struct Curl_share *set;
2114 set = va_arg(param, struct Curl_share *);
2116 /* disconnect from old share, if any */
2118 Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
2120 if(data->dns.hostcachetype == HCACHE_SHARED) {
2121 data->dns.hostcache = NULL;
2122 data->dns.hostcachetype = HCACHE_NONE;
2125 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
2126 if(data->share->cookies == data->cookies)
2127 data->cookies = NULL;
2130 if(data->share->sslsession == data->state.session)
2131 data->state.session = NULL;
2133 data->share->dirty--;
2135 Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
2139 /* use new share if it set */
2143 Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
2145 data->share->dirty++;
2147 if(data->share->hostcache) {
2148 /* use shared host cache */
2149 data->dns.hostcache = data->share->hostcache;
2150 data->dns.hostcachetype = HCACHE_SHARED;
2152 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
2153 if(data->share->cookies) {
2154 /* use shared cookie list, first free own one if any */
2156 Curl_cookie_cleanup(data->cookies);
2157 /* enable cookies since we now use a share that uses cookies! */
2158 data->cookies = data->share->cookies;
2160 #endif /* CURL_DISABLE_HTTP */
2161 if(data->share->sslsession) {
2162 data->set.ssl.max_ssl_sessions = data->share->max_ssl_sessions;
2163 data->state.session = data->share->sslsession;
2165 Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
2168 /* check for host cache not needed,
2169 * it will be done by curl_easy_perform */
2173 case CURLOPT_PRIVATE:
2175 * Set private data pointer.
2177 data->set.private_data = va_arg(param, void *);
2180 case CURLOPT_MAXFILESIZE:
2182 * Set the maximum size of a file to download.
2184 data->set.max_filesize = va_arg(param, long);
2188 case CURLOPT_USE_SSL:
2190 * Make transfers attempt to use SSL/TLS.
2192 data->set.use_ssl = (curl_usessl)va_arg(param, long);
2195 case CURLOPT_SSL_OPTIONS:
2196 arg = va_arg(param, long);
2197 data->set.ssl_enable_beast = arg&CURLSSLOPT_ALLOW_BEAST?TRUE:FALSE;
2201 case CURLOPT_FTPSSLAUTH:
2203 * Set a specific auth for FTP-SSL transfers.
2205 data->set.ftpsslauth = (curl_ftpauth)va_arg(param, long);
2208 case CURLOPT_IPRESOLVE:
2209 data->set.ipver = va_arg(param, long);
2212 case CURLOPT_MAXFILESIZE_LARGE:
2214 * Set the maximum size of a file to download.
2216 data->set.max_filesize = va_arg(param, curl_off_t);
2219 case CURLOPT_TCP_NODELAY:
2221 * Enable or disable TCP_NODELAY, which will disable/enable the Nagle
2224 data->set.tcp_nodelay = (0 != va_arg(param, long))?TRUE:FALSE;
2227 case CURLOPT_FTP_ACCOUNT:
2228 result = setstropt(&data->set.str[STRING_FTP_ACCOUNT],
2229 va_arg(param, char *));
2232 case CURLOPT_IGNORE_CONTENT_LENGTH:
2233 data->set.ignorecl = (0 != va_arg(param, long))?TRUE:FALSE;
2236 case CURLOPT_CONNECT_ONLY:
2238 * No data transfer, set up connection and let application use the socket
2240 data->set.connect_only = (0 != va_arg(param, long))?TRUE:FALSE;
2243 case CURLOPT_FTP_ALTERNATIVE_TO_USER:
2244 result = setstropt(&data->set.str[STRING_FTP_ALTERNATIVE_TO_USER],
2245 va_arg(param, char *));
2248 case CURLOPT_SOCKOPTFUNCTION:
2250 * socket callback function: called after socket() but before connect()
2252 data->set.fsockopt = va_arg(param, curl_sockopt_callback);
2255 case CURLOPT_SOCKOPTDATA:
2257 * socket callback data pointer. Might be NULL.
2259 data->set.sockopt_client = va_arg(param, void *);
2262 case CURLOPT_OPENSOCKETFUNCTION:
2264 * open/create socket callback function: called instead of socket(),
2267 data->set.fopensocket = va_arg(param, curl_opensocket_callback);
2270 case CURLOPT_OPENSOCKETDATA:
2272 * socket callback data pointer. Might be NULL.
2274 data->set.opensocket_client = va_arg(param, void *);
2277 case CURLOPT_CLOSESOCKETFUNCTION:
2279 * close socket callback function: called instead of close()
2280 * when shutting down a connection
2282 data->set.fclosesocket = va_arg(param, curl_closesocket_callback);
2285 case CURLOPT_CLOSESOCKETDATA:
2287 * socket callback data pointer. Might be NULL.
2289 data->set.closesocket_client = va_arg(param, void *);
2292 case CURLOPT_SSL_SESSIONID_CACHE:
2293 data->set.ssl.sessionid = (0 != va_arg(param, long))?TRUE:FALSE;
2297 /* we only include SSH options if explicitly built to support SSH */
2298 case CURLOPT_SSH_AUTH_TYPES:
2299 data->set.ssh_auth_types = va_arg(param, long);
2302 case CURLOPT_SSH_PUBLIC_KEYFILE:
2304 * Use this file instead of the $HOME/.ssh/id_dsa.pub file
2306 result = setstropt(&data->set.str[STRING_SSH_PUBLIC_KEY],
2307 va_arg(param, char *));
2310 case CURLOPT_SSH_PRIVATE_KEYFILE:
2312 * Use this file instead of the $HOME/.ssh/id_dsa file
2314 result = setstropt(&data->set.str[STRING_SSH_PRIVATE_KEY],
2315 va_arg(param, char *));
2317 case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
2319 * Option to allow for the MD5 of the host public key to be checked
2320 * for validation purposes.
2322 result = setstropt(&data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5],
2323 va_arg(param, char *));
2325 #ifdef HAVE_LIBSSH2_KNOWNHOST_API
2326 case CURLOPT_SSH_KNOWNHOSTS:
2328 * Store the file name to read known hosts from.
2330 result = setstropt(&data->set.str[STRING_SSH_KNOWNHOSTS],
2331 va_arg(param, char *));
2334 case CURLOPT_SSH_KEYFUNCTION:
2335 /* setting to NULL is fine since the ssh.c functions themselves will
2336 then rever to use the internal default */
2337 data->set.ssh_keyfunc = va_arg(param, curl_sshkeycallback);
2340 case CURLOPT_SSH_KEYDATA:
2342 * Custom client data to pass to the SSH keyfunc callback
2344 data->set.ssh_keyfunc_userp = va_arg(param, void *);
2346 #endif /* HAVE_LIBSSH2_KNOWNHOST_API */
2348 #endif /* USE_LIBSSH2 */
2350 case CURLOPT_HTTP_TRANSFER_DECODING:
2352 * disable libcurl transfer encoding is used
2354 data->set.http_te_skip = (0 == va_arg(param, long))?TRUE:FALSE;
2357 case CURLOPT_HTTP_CONTENT_DECODING:
2359 * raw data passed to the application when content encoding is used
2361 data->set.http_ce_skip = (0 == va_arg(param, long))?TRUE:FALSE;
2364 case CURLOPT_NEW_FILE_PERMS:
2366 * Uses these permissions instead of 0644
2368 data->set.new_file_perms = va_arg(param, long);
2371 case CURLOPT_NEW_DIRECTORY_PERMS:
2373 * Uses these permissions instead of 0755
2375 data->set.new_directory_perms = va_arg(param, long);
2378 case CURLOPT_ADDRESS_SCOPE:
2380 * We always get longs when passed plain numericals, but for this value we
2381 * know that an unsigned int will always hold the value so we blindly
2382 * typecast to this type
2384 data->set.scope_id = curlx_sltoui(va_arg(param, long));
2387 case CURLOPT_PROTOCOLS:
2388 /* set the bitmask for the protocols that are allowed to be used for the
2389 transfer, which thus helps the app which takes URLs from users or other
2390 external inputs and want to restrict what protocol(s) to deal
2391 with. Defaults to CURLPROTO_ALL. */
2392 data->set.allowed_protocols = va_arg(param, long);
2395 case CURLOPT_REDIR_PROTOCOLS:
2396 /* set the bitmask for the protocols that libcurl is allowed to follow to,
2397 as a subset of the CURLOPT_PROTOCOLS ones. That means the protocol needs
2398 to be set in both bitmasks to be allowed to get redirected to. Defaults
2399 to all protocols except FILE and SCP. */
2400 data->set.redir_protocols = va_arg(param, long);
2403 case CURLOPT_MAIL_FROM:
2404 /* Set the SMTP mail originator */
2405 result = setstropt(&data->set.str[STRING_MAIL_FROM],
2406 va_arg(param, char *));
2409 case CURLOPT_MAIL_AUTH:
2410 /* Set the SMTP auth originator */
2411 result = setstropt(&data->set.str[STRING_MAIL_AUTH],
2412 va_arg(param, char *));
2415 case CURLOPT_MAIL_RCPT:
2416 /* Set the list of mail recipients */
2417 data->set.mail_rcpt = va_arg(param, struct curl_slist *);
2420 case CURLOPT_SASL_IR:
2421 /* Enable/disable SASL initial response */
2422 data->set.sasl_ir = (0 != va_arg(param, long)) ? TRUE : FALSE;
2425 case CURLOPT_RTSP_REQUEST:
2428 * Set the RTSP request method (OPTIONS, SETUP, PLAY, etc...)
2429 * Would this be better if the RTSPREQ_* were just moved into here?
2431 long curl_rtspreq = va_arg(param, long);
2432 Curl_RtspReq rtspreq = RTSPREQ_NONE;
2433 switch(curl_rtspreq) {
2434 case CURL_RTSPREQ_OPTIONS:
2435 rtspreq = RTSPREQ_OPTIONS;
2438 case CURL_RTSPREQ_DESCRIBE:
2439 rtspreq = RTSPREQ_DESCRIBE;
2442 case CURL_RTSPREQ_ANNOUNCE:
2443 rtspreq = RTSPREQ_ANNOUNCE;
2446 case CURL_RTSPREQ_SETUP:
2447 rtspreq = RTSPREQ_SETUP;
2450 case CURL_RTSPREQ_PLAY:
2451 rtspreq = RTSPREQ_PLAY;
2454 case CURL_RTSPREQ_PAUSE:
2455 rtspreq = RTSPREQ_PAUSE;
2458 case CURL_RTSPREQ_TEARDOWN:
2459 rtspreq = RTSPREQ_TEARDOWN;
2462 case CURL_RTSPREQ_GET_PARAMETER:
2463 rtspreq = RTSPREQ_GET_PARAMETER;
2466 case CURL_RTSPREQ_SET_PARAMETER:
2467 rtspreq = RTSPREQ_SET_PARAMETER;
2470 case CURL_RTSPREQ_RECORD:
2471 rtspreq = RTSPREQ_RECORD;
2474 case CURL_RTSPREQ_RECEIVE:
2475 rtspreq = RTSPREQ_RECEIVE;
2478 rtspreq = RTSPREQ_NONE;
2481 data->set.rtspreq = rtspreq;
2486 case CURLOPT_RTSP_SESSION_ID:
2488 * Set the RTSP Session ID manually. Useful if the application is
2489 * resuming a previously established RTSP session
2491 result = setstropt(&data->set.str[STRING_RTSP_SESSION_ID],
2492 va_arg(param, char *));
2495 case CURLOPT_RTSP_STREAM_URI:
2497 * Set the Stream URI for the RTSP request. Unless the request is
2498 * for generic server options, the application will need to set this.
2500 result = setstropt(&data->set.str[STRING_RTSP_STREAM_URI],
2501 va_arg(param, char *));
2504 case CURLOPT_RTSP_TRANSPORT:
2506 * The content of the Transport: header for the RTSP request
2508 result = setstropt(&data->set.str[STRING_RTSP_TRANSPORT],
2509 va_arg(param, char *));
2512 case CURLOPT_RTSP_CLIENT_CSEQ:
2514 * Set the CSEQ number to issue for the next RTSP request. Useful if the
2515 * application is resuming a previously broken connection. The CSEQ
2516 * will increment from this new number henceforth.
2518 data->state.rtsp_next_client_CSeq = va_arg(param, long);
2521 case CURLOPT_RTSP_SERVER_CSEQ:
2522 /* Same as the above, but for server-initiated requests */
2523 data->state.rtsp_next_client_CSeq = va_arg(param, long);
2526 case CURLOPT_INTERLEAVEDATA:
2527 data->set.rtp_out = va_arg(param, void *);
2529 case CURLOPT_INTERLEAVEFUNCTION:
2530 /* Set the user defined RTP write function */
2531 data->set.fwrite_rtp = va_arg(param, curl_write_callback);
2534 case CURLOPT_WILDCARDMATCH:
2535 data->set.wildcardmatch = (0 != va_arg(param, long))?TRUE:FALSE;
2537 case CURLOPT_CHUNK_BGN_FUNCTION:
2538 data->set.chunk_bgn = va_arg(param, curl_chunk_bgn_callback);
2540 case CURLOPT_CHUNK_END_FUNCTION:
2541 data->set.chunk_end = va_arg(param, curl_chunk_end_callback);
2543 case CURLOPT_FNMATCH_FUNCTION:
2544 data->set.fnmatch = va_arg(param, curl_fnmatch_callback);
2546 case CURLOPT_CHUNK_DATA:
2547 data->wildcard.customptr = va_arg(param, void *);
2549 case CURLOPT_FNMATCH_DATA:
2550 data->set.fnmatch_data = va_arg(param, void *);
2553 case CURLOPT_TLSAUTH_USERNAME:
2554 result = setstropt(&data->set.str[STRING_TLSAUTH_USERNAME],
2555 va_arg(param, char *));
2556 if(data->set.str[STRING_TLSAUTH_USERNAME] && !data->set.ssl.authtype)
2557 data->set.ssl.authtype = CURL_TLSAUTH_SRP; /* default to SRP */
2559 case CURLOPT_TLSAUTH_PASSWORD:
2560 result = setstropt(&data->set.str[STRING_TLSAUTH_PASSWORD],
2561 va_arg(param, char *));
2562 if(data->set.str[STRING_TLSAUTH_USERNAME] && !data->set.ssl.authtype)
2563 data->set.ssl.authtype = CURL_TLSAUTH_SRP; /* default to SRP */
2565 case CURLOPT_TLSAUTH_TYPE:
2566 if(strnequal((char *)va_arg(param, char *), "SRP", strlen("SRP")))
2567 data->set.ssl.authtype = CURL_TLSAUTH_SRP;
2569 data->set.ssl.authtype = CURL_TLSAUTH_NONE;
2572 case CURLOPT_DNS_SERVERS:
2573 result = Curl_set_dns_servers(data, va_arg(param, char *));
2575 case CURLOPT_DNS_INTERFACE:
2576 result = Curl_set_dns_interface(data, va_arg(param, char *));
2578 case CURLOPT_DNS_LOCAL_IP4:
2579 result = Curl_set_dns_local_ip4(data, va_arg(param, char *));
2581 case CURLOPT_DNS_LOCAL_IP6:
2582 result = Curl_set_dns_local_ip6(data, va_arg(param, char *));
2585 case CURLOPT_TCP_KEEPALIVE:
2586 data->set.tcp_keepalive = (0 != va_arg(param, long))?TRUE:FALSE;
2588 case CURLOPT_TCP_KEEPIDLE:
2589 data->set.tcp_keepidle = va_arg(param, long);
2591 case CURLOPT_TCP_KEEPINTVL:
2592 data->set.tcp_keepintvl = va_arg(param, long);
2594 case CURLOPT_SSL_ENABLE_NPN:
2595 data->set.ssl_enable_npn = (0 != va_arg(param, long))?TRUE:FALSE;
2597 case CURLOPT_SSL_ENABLE_ALPN:
2598 data->set.ssl_enable_alpn = (0 != va_arg(param, long))?TRUE:FALSE;
2601 #ifdef USE_UNIX_SOCKETS
2602 case CURLOPT_UNIX_SOCKET_PATH:
2603 result = setstropt(&data->set.str[STRING_UNIX_SOCKET_PATH],
2604 va_arg(param, char *));
2609 /* unknown tag and its companion, just ignore: */
2610 result = CURLE_UNKNOWN_OPTION;
2617 static void conn_free(struct connectdata *conn)
2622 /* possible left-overs from the async name resolvers */
2623 Curl_resolver_cancel(conn);
2625 /* close the SSL stuff before we close any sockets since they will/may
2626 write to the sockets */
2627 Curl_ssl_close(conn, FIRSTSOCKET);
2628 Curl_ssl_close(conn, SECONDARYSOCKET);
2630 /* close possibly still open sockets */
2631 if(CURL_SOCKET_BAD != conn->sock[SECONDARYSOCKET])
2632 Curl_closesocket(conn, conn->sock[SECONDARYSOCKET]);
2633 if(CURL_SOCKET_BAD != conn->sock[FIRSTSOCKET])
2634 Curl_closesocket(conn, conn->sock[FIRSTSOCKET]);
2635 if(CURL_SOCKET_BAD != conn->tempsock[0])
2636 Curl_closesocket(conn, conn->tempsock[0]);
2637 if(CURL_SOCKET_BAD != conn->tempsock[1])
2638 Curl_closesocket(conn, conn->tempsock[1]);
2640 #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
2641 defined(NTLM_WB_ENABLED)
2642 Curl_ntlm_wb_cleanup(conn);
2645 Curl_safefree(conn->user);
2646 Curl_safefree(conn->passwd);
2647 Curl_safefree(conn->xoauth2_bearer);
2648 Curl_safefree(conn->options);
2649 Curl_safefree(conn->proxyuser);
2650 Curl_safefree(conn->proxypasswd);
2651 Curl_safefree(conn->allocptr.proxyuserpwd);
2652 Curl_safefree(conn->allocptr.uagent);
2653 Curl_safefree(conn->allocptr.userpwd);
2654 Curl_safefree(conn->allocptr.accept_encoding);
2655 Curl_safefree(conn->allocptr.te);
2656 Curl_safefree(conn->allocptr.rangeline);
2657 Curl_safefree(conn->allocptr.ref);
2658 Curl_safefree(conn->allocptr.host);
2659 Curl_safefree(conn->allocptr.cookiehost);
2660 Curl_safefree(conn->allocptr.rtsp_transport);
2661 Curl_safefree(conn->trailer);
2662 Curl_safefree(conn->host.rawalloc); /* host name buffer */
2663 Curl_safefree(conn->proxy.rawalloc); /* proxy name buffer */
2664 Curl_safefree(conn->master_buffer);
2666 Curl_llist_destroy(conn->send_pipe, NULL);
2667 Curl_llist_destroy(conn->recv_pipe, NULL);
2669 conn->send_pipe = NULL;
2670 conn->recv_pipe = NULL;
2672 Curl_safefree(conn->localdev);
2673 Curl_free_ssl_config(&conn->ssl_config);
2675 free(conn); /* free all the connection oriented data */
2679 * Disconnects the given connection. Note the connection may not be the
2680 * primary connection, like when freeing room in the connection cache or
2681 * killing of a dead old connection.
2683 * This function MUST NOT reset state in the SessionHandle struct if that
2684 * isn't strictly bound to the life-time of *this* particular connection.
2688 CURLcode Curl_disconnect(struct connectdata *conn, bool dead_connection)
2690 struct SessionHandle *data;
2692 return CURLE_OK; /* this is closed and fine already */
2696 DEBUGF(fprintf(stderr, "DISCONNECT without easy handle, ignoring\n"));
2700 if(conn->dns_entry != NULL) {
2701 Curl_resolv_unlock(data, conn->dns_entry);
2702 conn->dns_entry = NULL;
2705 Curl_hostcache_prune(data); /* kill old DNS cache entries */
2707 #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM)
2708 /* Cleanup NTLM connection-related data */
2709 Curl_http_ntlm_cleanup(conn);
2712 if(conn->handler->disconnect)
2713 /* This is set if protocol-specific cleanups should be made */
2714 conn->handler->disconnect(conn, dead_connection);
2716 /* unlink ourselves! */
2717 infof(data, "Closing connection %ld\n", conn->connection_id);
2718 Curl_conncache_remove_conn(data->state.conn_cache, conn);
2720 #if defined(USE_LIBIDN)
2721 if(conn->host.encalloc)
2722 idn_free(conn->host.encalloc); /* encoded host name buffer, must be freed
2723 with idn_free() since this was allocated
2725 if(conn->proxy.encalloc)
2726 idn_free(conn->proxy.encalloc); /* encoded proxy name buffer, must be
2727 freed with idn_free() since this was
2728 allocated by libidn */
2729 #elif defined(USE_WIN32_IDN)
2730 free(conn->host.encalloc); /* encoded host name buffer, must be freed with
2731 idn_free() since this was allocated by
2732 curl_win32_idn_to_ascii */
2733 if(conn->proxy.encalloc)
2734 free(conn->proxy.encalloc); /* encoded proxy name buffer, must be freed
2735 with idn_free() since this was allocated by
2736 curl_win32_idn_to_ascii */
2739 Curl_ssl_close(conn, FIRSTSOCKET);
2741 /* Indicate to all handles on the pipe that we're dead */
2742 if(Curl_multi_pipeline_enabled(data->multi)) {
2743 signalPipeClose(conn->send_pipe, TRUE);
2744 signalPipeClose(conn->recv_pipe, TRUE);
2753 * This function should return TRUE if the socket is to be assumed to
2754 * be dead. Most commonly this happens when the server has closed the
2755 * connection due to inactivity.
2757 static bool SocketIsDead(curl_socket_t sock)
2760 bool ret_val = TRUE;
2762 sval = Curl_socket_ready(sock, CURL_SOCKET_BAD, 0);
2770 static bool IsPipeliningPossible(const struct SessionHandle *handle,
2771 const struct connectdata *conn)
2773 if((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
2774 Curl_multi_pipeline_enabled(handle->multi) &&
2775 (handle->set.httpreq == HTTPREQ_GET ||
2776 handle->set.httpreq == HTTPREQ_HEAD) &&
2777 handle->set.httpversion != CURL_HTTP_VERSION_1_0)
2783 bool Curl_isPipeliningEnabled(const struct SessionHandle *handle)
2785 return Curl_multi_pipeline_enabled(handle->multi);
2788 CURLcode Curl_addHandleToPipeline(struct SessionHandle *data,
2789 struct curl_llist *pipeline)
2791 if(!Curl_llist_insert_next(pipeline, pipeline->tail, data))
2792 return CURLE_OUT_OF_MEMORY;
2796 int Curl_removeHandleFromPipeline(struct SessionHandle *handle,
2797 struct curl_llist *pipeline)
2799 struct curl_llist_element *curr;
2801 curr = pipeline->head;
2803 if(curr->ptr == handle) {
2804 Curl_llist_remove(pipeline, curr, NULL);
2805 return 1; /* we removed a handle */
2813 #if 0 /* this code is saved here as it is useful for debugging purposes */
2814 static void Curl_printPipeline(struct curl_llist *pipeline)
2816 struct curl_llist_element *curr;
2818 curr = pipeline->head;
2820 struct SessionHandle *data = (struct SessionHandle *) curr->ptr;
2821 infof(data, "Handle in pipeline: %s\n", data->state.path);
2827 static struct SessionHandle* gethandleathead(struct curl_llist *pipeline)
2829 struct curl_llist_element *curr = pipeline->head;
2831 return (struct SessionHandle *) curr->ptr;
2837 /* remove the specified connection from all (possible) pipelines and related
2839 void Curl_getoff_all_pipelines(struct SessionHandle *data,
2840 struct connectdata *conn)
2842 bool recv_head = (conn->readchannel_inuse &&
2843 (gethandleathead(conn->recv_pipe) == data)) ? TRUE : FALSE;
2845 bool send_head = (conn->writechannel_inuse &&
2846 (gethandleathead(conn->send_pipe) == data)) ? TRUE : FALSE;
2848 if(Curl_removeHandleFromPipeline(data, conn->recv_pipe) && recv_head)
2849 conn->readchannel_inuse = FALSE;
2850 if(Curl_removeHandleFromPipeline(data, conn->send_pipe) && send_head)
2851 conn->writechannel_inuse = FALSE;
2854 static void signalPipeClose(struct curl_llist *pipeline, bool pipe_broke)
2856 struct curl_llist_element *curr;
2861 curr = pipeline->head;
2863 struct curl_llist_element *next = curr->next;
2864 struct SessionHandle *data = (struct SessionHandle *) curr->ptr;
2866 #ifdef DEBUGBUILD /* debug-only code */
2867 if(data->magic != CURLEASY_MAGIC_NUMBER) {
2869 infof(data, "signalPipeClose() found BAAD easy handle\n");
2874 data->state.pipe_broke = TRUE;
2875 Curl_multi_handlePipeBreak(data);
2876 Curl_llist_remove(pipeline, curr, NULL);
2882 * This function finds the connection in the connection
2883 * cache that has been unused for the longest time.
2885 * Returns the pointer to the oldest idle connection, or NULL if none was
2888 static struct connectdata *
2889 find_oldest_idle_connection(struct SessionHandle *data)
2891 struct conncache *bc = data->state.conn_cache;
2892 struct curl_hash_iterator iter;
2893 struct curl_llist_element *curr;
2894 struct curl_hash_element *he;
2898 struct connectdata *conn_candidate = NULL;
2899 struct connectbundle *bundle;
2903 Curl_hash_start_iterate(bc->hash, &iter);
2905 he = Curl_hash_next_element(&iter);
2907 struct connectdata *conn;
2911 curr = bundle->conn_list->head;
2916 /* Set higher score for the age passed since the connection was used */
2917 score = Curl_tvdiff(now, conn->now);
2919 if(score > highscore) {
2921 conn_candidate = conn;
2927 he = Curl_hash_next_element(&iter);
2930 return conn_candidate;
2934 * This function finds the connection in the connection
2935 * bundle that has been unused for the longest time.
2937 * Returns the pointer to the oldest idle connection, or NULL if none was
2940 static struct connectdata *
2941 find_oldest_idle_connection_in_bundle(struct SessionHandle *data,
2942 struct connectbundle *bundle)
2944 struct curl_llist_element *curr;
2948 struct connectdata *conn_candidate = NULL;
2949 struct connectdata *conn;
2955 curr = bundle->conn_list->head;
2960 /* Set higher score for the age passed since the connection was used */
2961 score = Curl_tvdiff(now, conn->now);
2963 if(score > highscore) {
2965 conn_candidate = conn;
2971 return conn_candidate;
2975 * This function checks if given connection is dead and disconnects if so.
2976 * (That also removes it from the connection cache.)
2978 * Returns TRUE if the connection actually was dead and disconnected.
2980 static bool disconnect_if_dead(struct connectdata *conn,
2981 struct SessionHandle *data)
2983 size_t pipeLen = conn->send_pipe->size + conn->recv_pipe->size;
2984 if(!pipeLen && !conn->inuse) {
2985 /* The check for a dead socket makes sense only if there are no
2986 handles in pipeline and the connection isn't already marked in
2989 if(conn->handler->protocol & CURLPROTO_RTSP)
2990 /* RTSP is a special case due to RTP interleaving */
2991 dead = Curl_rtsp_connisdead(conn);
2993 dead = SocketIsDead(conn->sock[FIRSTSOCKET]);
2997 infof(data, "Connection %ld seems to be dead!\n", conn->connection_id);
2999 /* disconnect resources */
3000 Curl_disconnect(conn, /* dead_connection */TRUE);
3008 * Wrapper to use disconnect_if_dead() function in Curl_conncache_foreach()
3012 static int call_disconnect_if_dead(struct connectdata *conn,
3015 struct SessionHandle* data = (struct SessionHandle*)param;
3016 disconnect_if_dead(conn, data);
3017 return 0; /* continue iteration */
3021 * This function scans the connection cache for half-open/dead connections,
3022 * closes and removes them.
3023 * The cleanup is done at most once per second.
3025 static void prune_dead_connections(struct SessionHandle *data)
3027 struct timeval now = Curl_tvnow();
3028 long elapsed = Curl_tvdiff(now, data->state.conn_cache->last_cleanup);
3030 if(elapsed >= 1000L) {
3031 Curl_conncache_foreach(data->state.conn_cache, data,
3032 call_disconnect_if_dead);
3033 data->state.conn_cache->last_cleanup = now;
3038 * Given one filled in connection struct (named needle), this function should
3039 * detect if there already is one that has all the significant details
3040 * exactly the same and thus should be used instead.
3042 * If there is a match, this function returns TRUE - and has marked the
3043 * connection as 'in-use'. It must later be called with ConnectionDone() to
3044 * return back to 'idle' (unused) state.
3046 * The force_reuse flag is set if the connection must be used, even if
3047 * the pipelining strategy wants to open a new connection instead of reusing.
3050 ConnectionExists(struct SessionHandle *data,
3051 struct connectdata *needle,
3052 struct connectdata **usethis,
3055 struct connectdata *check;
3056 struct connectdata *chosen = 0;
3057 bool canPipeline = IsPipeliningPossible(data, needle);
3058 bool wantNTLMhttp = ((data->state.authhost.want & CURLAUTH_NTLM) ||
3059 (data->state.authhost.want & CURLAUTH_NTLM_WB)) &&
3060 (needle->handler->protocol & PROTO_FAMILY_HTTP) ? TRUE : FALSE;
3061 struct connectbundle *bundle;
3063 *force_reuse = FALSE;
3065 /* We can't pipe if the site is blacklisted */
3066 if(canPipeline && Curl_pipeline_site_blacklisted(data, needle)) {
3067 canPipeline = FALSE;
3070 /* Look up the bundle with all the connections to this
3072 bundle = Curl_conncache_find_bundle(data->state.conn_cache,
3075 size_t max_pipe_len = Curl_multi_max_pipeline_length(data->multi);
3076 size_t best_pipe_len = max_pipe_len;
3077 struct curl_llist_element *curr;
3079 infof(data, "Found bundle for host %s: %p\n",
3080 needle->host.name, (void *)bundle);
3082 /* We can't pipe if we don't know anything about the server */
3083 if(canPipeline && !bundle->server_supports_pipelining) {
3084 infof(data, "Server doesn't support pipelining\n");
3085 canPipeline = FALSE;
3088 curr = bundle->conn_list->head;
3091 #if defined(USE_NTLM)
3092 bool credentialsMatch = FALSE;
3097 * Note that if we use a HTTP proxy, we check connections to that
3098 * proxy and not to the actual remote server.
3103 if(disconnect_if_dead(check, data))
3106 pipeLen = check->send_pipe->size + check->recv_pipe->size;
3109 /* Make sure the pipe has only GET requests */
3110 struct SessionHandle* sh = gethandleathead(check->send_pipe);
3111 struct SessionHandle* rh = gethandleathead(check->recv_pipe);
3113 if(!IsPipeliningPossible(sh, check))
3117 if(!IsPipeliningPossible(rh, check))
3123 /* can only happen within multi handles, and means that another easy
3124 handle is using this connection */
3128 if(Curl_resolver_asynch()) {
3129 /* ip_addr_str[0] is NUL only if the resolving of the name hasn't
3130 completed yet and until then we don't re-use this connection */
3131 if(!check->ip_addr_str[0]) {
3133 "Connection #%ld is still name resolving, can't reuse\n",
3134 check->connection_id);
3139 if((check->sock[FIRSTSOCKET] == CURL_SOCKET_BAD) ||
3140 check->bits.close) {
3141 /* Don't pick a connection that hasn't connected yet or that is going
3143 infof(data, "Connection #%ld isn't open enough, can't reuse\n",
3144 check->connection_id);
3146 if(check->recv_pipe->size > 0) {
3148 "BAD! Unconnected #%ld has a non-empty recv pipeline!\n",
3149 check->connection_id);
3156 if((needle->handler->flags&PROTOPT_SSL) !=
3157 (check->handler->flags&PROTOPT_SSL))
3158 /* don't do mixed SSL and non-SSL connections */
3159 if(!(needle->handler->protocol & check->handler->protocol))
3160 /* except protocols that have been upgraded via TLS */
3163 if(needle->handler->flags&PROTOPT_SSL) {
3164 if((data->set.ssl.verifypeer != check->verifypeer) ||
3165 (data->set.ssl.verifyhost != check->verifyhost))
3169 if(needle->bits.proxy != check->bits.proxy)
3170 /* don't do mixed proxy and non-proxy connections */
3173 if(!canPipeline && check->inuse)
3174 /* this request can't be pipelined but the checked connection is
3175 already in use so we skip it */
3178 if(needle->localdev || needle->localport) {
3179 /* If we are bound to a specific local end (IP+port), we must not
3180 re-use a random other one, although if we didn't ask for a
3181 particular one we can reuse one that was bound.
3183 This comparison is a bit rough and too strict. Since the input
3184 parameters can be specified in numerous ways and still end up the
3185 same it would take a lot of processing to make it really accurate.
3186 Instead, this matching will assume that re-uses of bound connections
3187 will most likely also re-use the exact same binding parameters and
3188 missing out a few edge cases shouldn't hurt anyone very much.
3190 if((check->localport != needle->localport) ||
3191 (check->localportrange != needle->localportrange) ||
3193 !needle->localdev ||
3194 strcmp(check->localdev, needle->localdev))
3198 if((!(needle->handler->flags & PROTOPT_CREDSPERREQUEST)) ||
3199 (wantNTLMhttp || check->ntlm.state != NTLMSTATE_NONE)) {
3200 /* This protocol requires credentials per connection or is HTTP+NTLM,
3201 so verify that we're using the same name and password as well */
3202 if(!strequal(needle->user, check->user) ||
3203 !strequal(needle->passwd, check->passwd)) {
3204 /* one of them was different */
3207 #if defined(USE_NTLM)
3208 credentialsMatch = TRUE;
3212 if(!needle->bits.httpproxy || needle->handler->flags&PROTOPT_SSL ||
3213 (needle->bits.httpproxy && check->bits.httpproxy &&
3214 needle->bits.tunnel_proxy && check->bits.tunnel_proxy &&
3215 Curl_raw_equal(needle->proxy.name, check->proxy.name) &&
3216 (needle->port == check->port))) {
3217 /* The requested connection does not use a HTTP proxy or it uses SSL or
3218 it is a non-SSL protocol tunneled over the same http proxy name and
3219 port number or it is a non-SSL protocol which is allowed to be
3222 if((Curl_raw_equal(needle->handler->scheme, check->handler->scheme) ||
3223 needle->handler->protocol & check->handler->protocol) &&
3224 Curl_raw_equal(needle->host.name, check->host.name) &&
3225 needle->remote_port == check->remote_port) {
3226 if(needle->handler->flags & PROTOPT_SSL) {
3227 /* This is a SSL connection so verify that we're using the same
3228 SSL options as well */
3229 if(!Curl_ssl_config_matches(&needle->ssl_config,
3230 &check->ssl_config)) {
3232 "Connection #%ld has different SSL parameters, "
3234 check->connection_id));
3237 else if(check->ssl[FIRSTSOCKET].state != ssl_connection_complete) {
3239 "Connection #%ld has not started SSL connect, "
3241 check->connection_id));
3248 else { /* The requested needle connection is using a proxy,
3249 is the checked one using the same host, port and type? */
3250 if(check->bits.proxy &&
3251 (needle->proxytype == check->proxytype) &&
3252 (needle->bits.tunnel_proxy == check->bits.tunnel_proxy) &&
3253 Curl_raw_equal(needle->proxy.name, check->proxy.name) &&
3254 needle->port == check->port) {
3255 /* This is the same proxy connection, use it! */
3261 #if defined(USE_NTLM)
3262 /* If we are looking for an HTTP+NTLM connection, check if this is
3263 already authenticating with the right credentials. If not, keep
3264 looking so that we can reuse NTLM connections if
3265 possible. (Especially we must not reuse the same connection if
3266 partway through a handshake!) */
3268 if(credentialsMatch && check->ntlm.state != NTLMSTATE_NONE) {
3271 /* We must use this connection, no other */
3272 *force_reuse = TRUE;
3275 else if(credentialsMatch)
3276 /* this is a backup choice */
3283 /* We can pipeline if we want to. Let's continue looking for
3284 the optimal connection to use, i.e the shortest pipe that is not
3288 /* We have the optimal connection. Let's stop looking. */
3293 /* We can't use the connection if the pipe is full */
3294 if(pipeLen >= max_pipe_len)
3297 /* We can't use the connection if the pipe is penalized */
3298 if(Curl_pipeline_penalized(data, check))
3301 if(pipeLen < best_pipe_len) {
3302 /* This connection has a shorter pipe so far. We'll pick this
3303 and continue searching */
3305 best_pipe_len = pipeLen;
3310 /* We have found a connection. Let's stop searching. */
3320 return TRUE; /* yes, we found one to use! */
3323 return FALSE; /* no matching connecting exists */
3326 /* Mark the connection as 'idle', or close it if the cache is full.
3327 Returns TRUE if the connection is kept, or FALSE if it was closed. */
3329 ConnectionDone(struct SessionHandle *data, struct connectdata *conn)
3331 /* data->multi->maxconnects can be negative, deal with it. */
3332 size_t maxconnects =
3333 (data->multi->maxconnects < 0) ? data->multi->num_easy * 4:
3334 data->multi->maxconnects;
3335 struct connectdata *conn_candidate = NULL;
3337 /* Mark the current connection as 'unused' */
3338 conn->inuse = FALSE;
3340 if(maxconnects > 0 &&
3341 data->state.conn_cache->num_connections > maxconnects) {
3342 infof(data, "Connection cache is full, closing the oldest one.\n");
3344 conn_candidate = find_oldest_idle_connection(data);
3346 if(conn_candidate) {
3347 /* Set the connection's owner correctly */
3348 conn_candidate->data = data;
3350 /* the winner gets the honour of being disconnected */
3351 (void)Curl_disconnect(conn_candidate, /* dead_connection */ FALSE);
3355 return (conn_candidate == conn) ? FALSE : TRUE;
3359 * The given input connection struct pointer is to be stored in the connection
3360 * cache. If the cache is already full, least interesting existing connection
3361 * (if any) gets closed.
3363 * The given connection should be unique. That must've been checked prior to
3366 static CURLcode ConnectionStore(struct SessionHandle *data,
3367 struct connectdata *conn)
3369 return Curl_conncache_add_conn(data->state.conn_cache, conn);
3372 /* after a TCP connection to the proxy has been verified, this function does
3373 the next magic step.
3375 Note: this function's sub-functions call failf()
3378 CURLcode Curl_connected_proxy(struct connectdata *conn,
3381 if(!conn->bits.proxy || sockindex)
3382 /* this magic only works for the primary socket as the secondary is used
3383 for FTP only and it has FTP specific magic in ftp.c */
3386 switch(conn->proxytype) {
3387 #ifndef CURL_DISABLE_PROXY
3388 case CURLPROXY_SOCKS5:
3389 case CURLPROXY_SOCKS5_HOSTNAME:
3390 return Curl_SOCKS5(conn->proxyuser, conn->proxypasswd,
3391 conn->host.name, conn->remote_port,
3394 case CURLPROXY_SOCKS4:
3395 return Curl_SOCKS4(conn->proxyuser, conn->host.name,
3396 conn->remote_port, FIRSTSOCKET, conn, FALSE);
3398 case CURLPROXY_SOCKS4A:
3399 return Curl_SOCKS4(conn->proxyuser, conn->host.name,
3400 conn->remote_port, FIRSTSOCKET, conn, TRUE);
3402 #endif /* CURL_DISABLE_PROXY */
3403 case CURLPROXY_HTTP:
3404 case CURLPROXY_HTTP_1_0:
3405 /* do nothing here. handled later. */
3409 } /* switch proxytype */
3415 * verboseconnect() displays verbose information after a connect
3417 #ifndef CURL_DISABLE_VERBOSE_STRINGS
3418 void Curl_verboseconnect(struct connectdata *conn)
3420 if(conn->data->set.verbose)
3421 infof(conn->data, "Connected to %s (%s) port %ld (#%ld)\n",
3422 conn->bits.proxy ? conn->proxy.dispname : conn->host.dispname,
3423 conn->ip_addr_str, conn->port, conn->connection_id);
3427 int Curl_protocol_getsock(struct connectdata *conn,
3428 curl_socket_t *socks,
3431 if(conn->handler->proto_getsock)
3432 return conn->handler->proto_getsock(conn, socks, numsocks);
3433 return GETSOCK_BLANK;
3436 int Curl_doing_getsock(struct connectdata *conn,
3437 curl_socket_t *socks,
3440 if(conn && conn->handler->doing_getsock)
3441 return conn->handler->doing_getsock(conn, socks, numsocks);
3442 return GETSOCK_BLANK;
3446 * We are doing protocol-specific connecting and this is being called over and
3447 * over from the multi interface until the connection phase is done on
3451 CURLcode Curl_protocol_connecting(struct connectdata *conn,
3454 CURLcode result=CURLE_OK;
3456 if(conn && conn->handler->connecting) {
3458 result = conn->handler->connecting(conn, done);
3467 * We are DOING this is being called over and over from the multi interface
3468 * until the DOING phase is done on protocol layer.
3471 CURLcode Curl_protocol_doing(struct connectdata *conn, bool *done)
3473 CURLcode result=CURLE_OK;
3475 if(conn && conn->handler->doing) {
3477 result = conn->handler->doing(conn, done);
3486 * We have discovered that the TCP connection has been successful, we can now
3487 * proceed with some action.
3490 CURLcode Curl_protocol_connect(struct connectdata *conn,
3491 bool *protocol_done)
3493 CURLcode result=CURLE_OK;
3495 *protocol_done = FALSE;
3497 if(conn->bits.tcpconnect[FIRSTSOCKET] && conn->bits.protoconnstart) {
3498 /* We already are connected, get back. This may happen when the connect
3499 worked fine in the first call, like when we connect to a local server
3500 or proxy. Note that we don't know if the protocol is actually done.
3502 Unless this protocol doesn't have any protocol-connect callback, as
3503 then we know we're done. */
3504 if(!conn->handler->connecting)
3505 *protocol_done = TRUE;
3510 if(!conn->bits.protoconnstart) {
3512 result = Curl_proxy_connect(conn);
3516 if(conn->bits.tunnel_proxy && conn->bits.httpproxy &&
3517 (conn->tunnel_state[FIRSTSOCKET] != TUNNEL_COMPLETE))
3518 /* when using an HTTP tunnel proxy, await complete tunnel establishment
3519 before proceeding further. Return CURLE_OK so we'll be called again */
3522 if(conn->handler->connect_it) {
3523 /* is there a protocol-specific connect() procedure? */
3525 /* Call the protocol-specific connect function */
3526 result = conn->handler->connect_it(conn, protocol_done);
3529 *protocol_done = TRUE;
3531 /* it has started, possibly even completed but that knowledge isn't stored
3534 conn->bits.protoconnstart = TRUE;
3537 return result; /* pass back status */
3541 * Helpers for IDNA convertions.
3543 static bool is_ASCII_name(const char *hostname)
3545 const unsigned char *ch = (const unsigned char*)hostname;
3556 * Check if characters in hostname is allowed in Top Level Domain.
3558 static bool tld_check_name(struct SessionHandle *data,
3559 const char *ace_hostname)
3562 char *uc_name = NULL;
3564 #ifndef CURL_DISABLE_VERBOSE_STRINGS
3565 const char *tld_errmsg = "<no msg>";
3570 /* Convert (and downcase) ACE-name back into locale's character set */
3571 rc = idna_to_unicode_lzlz(ace_hostname, &uc_name, 0);
3572 if(rc != IDNA_SUCCESS)
3575 rc = tld_check_lz(uc_name, &err_pos, NULL);
3576 #ifndef CURL_DISABLE_VERBOSE_STRINGS
3577 #ifdef HAVE_TLD_STRERROR
3578 if(rc != TLD_SUCCESS)
3579 tld_errmsg = tld_strerror((Tld_rc)rc);
3581 if(rc == TLD_INVALID)
3582 infof(data, "WARNING: %s; pos %u = `%c'/0x%02X\n",
3583 tld_errmsg, err_pos, uc_name[err_pos],
3584 uc_name[err_pos] & 255);
3585 else if(rc != TLD_SUCCESS)
3586 infof(data, "WARNING: TLD check for %s failed; %s\n",
3587 uc_name, tld_errmsg);
3588 #endif /* CURL_DISABLE_VERBOSE_STRINGS */
3591 if(rc != TLD_SUCCESS)
3599 * Perform any necessary IDN conversion of hostname
3601 static void fix_hostname(struct SessionHandle *data,
3602 struct connectdata *conn, struct hostname *host)
3609 #elif defined(CURL_DISABLE_VERBOSE_STRINGS)
3613 /* set the name we use to display the host name */
3614 host->dispname = host->name;
3616 len = strlen(host->name);
3617 if(len && (host->name[len-1] == '.'))
3618 /* strip off a single trailing dot if present, primarily for SNI but
3619 there's no use for it */
3620 host->name[len-1]=0;
3622 if(!is_ASCII_name(host->name)) {
3624 /*************************************************************
3625 * Check name for non-ASCII and convert hostname to ACE form.
3626 *************************************************************/
3627 if(stringprep_check_version(LIBIDN_REQUIRED_VERSION)) {
3628 char *ace_hostname = NULL;
3629 int rc = idna_to_ascii_lz(host->name, &ace_hostname, 0);
3630 infof (data, "Input domain encoded as `%s'\n",
3631 stringprep_locale_charset ());
3632 if(rc != IDNA_SUCCESS)
3633 infof(data, "Failed to convert %s to ACE; %s\n",
3634 host->name, Curl_idn_strerror(conn,rc));
3636 /* tld_check_name() displays a warning if the host name contains
3637 "illegal" characters for this TLD */
3638 (void)tld_check_name(data, ace_hostname);
3640 host->encalloc = ace_hostname;
3641 /* change the name pointer to point to the encoded hostname */
3642 host->name = host->encalloc;
3645 #elif defined(USE_WIN32_IDN)
3646 /*************************************************************
3647 * Check name for non-ASCII and convert hostname to ACE form.
3648 *************************************************************/
3649 char *ace_hostname = NULL;
3650 int rc = curl_win32_idn_to_ascii(host->name, &ace_hostname);
3652 infof(data, "Failed to convert %s to ACE;\n",
3655 host->encalloc = ace_hostname;
3656 /* change the name pointer to point to the encoded hostname */
3657 host->name = host->encalloc;
3660 infof(data, "IDN support not present, can't parse Unicode domains\n");
3665 static void llist_dtor(void *user, void *element)
3673 * Allocate and initialize a new connectdata object.
3675 static struct connectdata *allocate_conn(struct SessionHandle *data)
3677 struct connectdata *conn = calloc(1, sizeof(struct connectdata));
3681 conn->handler = &Curl_handler_dummy; /* Be sure we have a handler defined
3682 already from start to avoid NULL
3683 situations and checks */
3685 /* and we setup a few fields in case we end up actually using this struct */
3687 conn->sock[FIRSTSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
3688 conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
3689 conn->tempsock[0] = CURL_SOCKET_BAD; /* no file descriptor */
3690 conn->tempsock[1] = CURL_SOCKET_BAD; /* no file descriptor */
3691 conn->connection_id = -1; /* no ID */
3692 conn->port = -1; /* unknown at this point */
3693 conn->remote_port = -1; /* unknown */
3695 /* Default protocol-independent behavior doesn't support persistent
3696 connections, so we set this to force-close. Protocols that support
3697 this need to set this to FALSE in their "curl_do" functions. */
3698 connclose(conn, "Default to force-close");
3700 /* Store creation time to help future close decision making */
3701 conn->created = Curl_tvnow();
3703 conn->data = data; /* Setup the association between this connection
3704 and the SessionHandle */
3706 conn->proxytype = data->set.proxytype; /* type */
3708 #ifdef CURL_DISABLE_PROXY
3710 conn->bits.proxy = FALSE;
3711 conn->bits.httpproxy = FALSE;
3712 conn->bits.proxy_user_passwd = FALSE;
3713 conn->bits.tunnel_proxy = FALSE;
3715 #else /* CURL_DISABLE_PROXY */
3717 /* note that these two proxy bits are now just on what looks to be
3718 requested, they may be altered down the road */
3719 conn->bits.proxy = (data->set.str[STRING_PROXY] &&
3720 *data->set.str[STRING_PROXY])?TRUE:FALSE;
3721 conn->bits.httpproxy = (conn->bits.proxy &&
3722 (conn->proxytype == CURLPROXY_HTTP ||
3723 conn->proxytype == CURLPROXY_HTTP_1_0))?TRUE:FALSE;
3724 conn->bits.proxy_user_passwd =
3725 (NULL != data->set.str[STRING_PROXYUSERNAME])?TRUE:FALSE;
3726 conn->bits.tunnel_proxy = data->set.tunnel_thru_httpproxy;
3728 #endif /* CURL_DISABLE_PROXY */
3730 conn->bits.user_passwd = (NULL != data->set.str[STRING_USERNAME])?TRUE:FALSE;
3731 conn->bits.ftp_use_epsv = data->set.ftp_use_epsv;
3732 conn->bits.ftp_use_eprt = data->set.ftp_use_eprt;
3734 conn->verifypeer = data->set.ssl.verifypeer;
3735 conn->verifyhost = data->set.ssl.verifyhost;
3737 conn->ip_version = data->set.ipver;
3739 #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
3740 defined(NTLM_WB_ENABLED)
3741 conn->ntlm_auth_hlpr_socket = CURL_SOCKET_BAD;
3742 conn->ntlm_auth_hlpr_pid = 0;
3743 conn->challenge_header = NULL;
3744 conn->response_header = NULL;
3747 if(Curl_multi_pipeline_enabled(data->multi) &&
3748 !conn->master_buffer) {
3749 /* Allocate master_buffer to be used for pipelining */
3750 conn->master_buffer = calloc(BUFSIZE, sizeof (char));
3751 if(!conn->master_buffer)
3755 /* Initialize the pipeline lists */
3756 conn->send_pipe = Curl_llist_alloc((curl_llist_dtor) llist_dtor);
3757 conn->recv_pipe = Curl_llist_alloc((curl_llist_dtor) llist_dtor);
3758 if(!conn->send_pipe || !conn->recv_pipe)
3762 conn->data_prot = PROT_CLEAR;
3765 /* Store the local bind parameters that will be used for this connection */
3766 if(data->set.str[STRING_DEVICE]) {
3767 conn->localdev = strdup(data->set.str[STRING_DEVICE]);
3771 conn->localportrange = data->set.localportrange;
3772 conn->localport = data->set.localport;
3774 /* the close socket stuff needs to be copied to the connection struct as
3775 it may live on without (this specific) SessionHandle */
3776 conn->fclosesocket = data->set.fclosesocket;
3777 conn->closesocket_client = data->set.closesocket_client;
3782 Curl_llist_destroy(conn->send_pipe, NULL);
3783 Curl_llist_destroy(conn->recv_pipe, NULL);
3785 conn->send_pipe = NULL;
3786 conn->recv_pipe = NULL;
3788 Curl_safefree(conn->master_buffer);
3789 Curl_safefree(conn->localdev);
3790 Curl_safefree(conn);
3794 static CURLcode findprotocol(struct SessionHandle *data,
3795 struct connectdata *conn,
3796 const char *protostr)
3798 const struct Curl_handler * const *pp;
3799 const struct Curl_handler *p;
3801 /* Scan protocol handler table and match against 'protostr' to set a few
3802 variables based on the URL. Now that the handler may be changed later
3803 when the protocol specific setup function is called. */
3804 for(pp = protocols; (p = *pp) != NULL; pp++) {
3805 if(Curl_raw_equal(p->scheme, protostr)) {
3806 /* Protocol found in table. Check if allowed */
3807 if(!(data->set.allowed_protocols & p->protocol))
3811 /* it is allowed for "normal" request, now do an extra check if this is
3812 the result of a redirect */
3813 if(data->state.this_is_a_follow &&
3814 !(data->set.redir_protocols & p->protocol))
3818 /* Perform setup complement if some. */
3819 conn->handler = conn->given = p;
3821 /* 'port' and 'remote_port' are set in setup_connection_internals() */
3827 /* The protocol was not found in the table, but we don't have to assign it
3828 to anything since it is already assigned to a dummy-struct in the
3829 create_conn() function when the connectdata struct is allocated. */
3830 failf(data, "Protocol \"%s\" not supported or disabled in " LIBCURL_NAME,
3833 return CURLE_UNSUPPORTED_PROTOCOL;
3837 * Parse URL and fill in the relevant members of the connection struct.
3839 static CURLcode parseurlandfillconn(struct SessionHandle *data,
3840 struct connectdata *conn,
3842 char **userp, char **passwdp,
3847 char *path = data->state.path;
3850 char protobuf[16] = "";
3851 const char *protop = "";
3853 bool rebuild_url = FALSE;
3855 *prot_missing = FALSE;
3857 /* We might pass the entire URL into the request so we need to make sure
3858 * there are no bad characters in there.*/
3859 if(strpbrk(data->change.url, "\r\n")) {
3860 failf(data, "Illegal characters found in URL");
3861 return CURLE_URL_MALFORMAT;
3864 /*************************************************************
3867 * We need to parse the url even when using the proxy, because we will need
3868 * the hostname and port in case we are trying to SSL connect through the
3869 * proxy -- and we don't know if we will need to use SSL until we parse the
3871 ************************************************************/
3872 if((2 == sscanf(data->change.url, "%15[^:]:%[^\n]",
3874 Curl_raw_equal(protobuf, "file")) {
3875 if(path[0] == '/' && path[1] == '/') {
3876 /* Allow omitted hostname (e.g. file:/<path>). This is not strictly
3877 * speaking a valid file: URL by RFC 1738, but treating file:/<path> as
3878 * file://localhost/<path> is similar to how other schemes treat missing
3879 * hostnames. See RFC 1808. */
3881 /* This cannot be done with strcpy() in a portable manner, since the
3882 memory areas overlap! */
3883 memmove(path, path + 2, strlen(path + 2)+1);
3886 * we deal with file://<host>/<path> differently since it supports no
3887 * hostname other than "localhost" and "127.0.0.1", which is unique among
3888 * the URL protocols specified in RFC 1738
3890 if(path[0] != '/') {
3891 /* the URL included a host name, we ignore host names in file:// URLs
3892 as the standards don't define what to do with them */
3893 char *ptr=strchr(path, '/');
3895 /* there was a slash present
3897 RFC1738 (section 3.1, page 5) says:
3899 The rest of the locator consists of data specific to the scheme,
3900 and is known as the "url-path". It supplies the details of how the
3901 specified resource can be accessed. Note that the "/" between the
3902 host (or port) and the url-path is NOT part of the url-path.
3904 As most agents use file://localhost/foo to get '/foo' although the
3905 slash preceding foo is a separator and not a slash for the path,
3906 a URL as file://localhost//foo must be valid as well, to refer to
3907 the same file with an absolute path.
3910 if(ptr[1] && ('/' == ptr[1]))
3911 /* if there was two slashes, we skip the first one as that is then
3912 used truly as a separator */
3915 /* This cannot be made with strcpy, as the memory chunks overlap! */
3916 memmove(path, ptr, strlen(ptr)+1);
3920 protop = "file"; /* protocol string */
3926 if(2 > sscanf(data->change.url,
3927 "%15[^\n:]://%[^\n/?]%[^\n]",
3929 conn->host.name, path)) {
3932 * The URL was badly formatted, let's try the browser-style _without_
3933 * protocol specified like 'http://'.
3935 rc = sscanf(data->change.url, "%[^\n/?]%[^\n]", conn->host.name, path);
3938 * We couldn't even get this format.
3939 * djgpp 2.04 has a sscanf() bug where 'conn->host.name' is
3940 * assigned, but the return value is EOF!
3942 #if defined(__DJGPP__) && (DJGPP_MINOR == 4)
3943 if(!(rc == -1 && *conn->host.name))
3946 failf(data, "<url> malformed");
3947 return CURLE_URL_MALFORMAT;
3952 * Since there was no protocol part specified, we guess what protocol it
3953 * is based on the first letters of the server name.
3956 /* Note: if you add a new protocol, please update the list in
3957 * lib/version.c too! */
3959 if(checkprefix("FTP.", conn->host.name))
3961 else if(checkprefix("DICT.", conn->host.name))
3963 else if(checkprefix("LDAP.", conn->host.name))
3965 else if(checkprefix("IMAP.", conn->host.name))
3967 else if(checkprefix("SMTP.", conn->host.name))
3969 else if(checkprefix("POP3.", conn->host.name))
3975 *prot_missing = TRUE; /* not given in URL */
3981 /* We search for '?' in the host name (but only on the right side of a
3982 * @-letter to allow ?-letters in username and password) to handle things
3983 * like http://example.com?param= (notice the missing '/').
3985 at = strchr(conn->host.name, '@');
3987 query = strchr(at+1, '?');
3989 query = strchr(conn->host.name, '?');
3992 /* We must insert a slash before the '?'-letter in the URL. If the URL had
3993 a slash after the '?', that is where the path currently begins and the
3994 '?string' is still part of the host name.
3996 We must move the trailing part from the host name and put it first in
3997 the path. And have it all prefixed with a slash.
4000 size_t hostlen = strlen(query);
4001 size_t pathlen = strlen(path);
4003 /* move the existing path plus the zero byte forward, to make room for
4004 the host-name part */
4005 memmove(path+hostlen+1, path, pathlen+1);
4007 /* now copy the trailing host part in front of the existing path */
4008 memcpy(path+1, query, hostlen);
4010 path[0]='/'; /* prepend the missing slash */
4013 *query=0; /* now cut off the hostname at the ? */
4016 /* if there's no path set, use a single slash */
4021 /* If the URL is malformatted (missing a '/' after hostname before path) we
4022 * insert a slash here. The only letter except '/' we accept to start a path
4025 if(path[0] == '?') {
4026 /* We need this function to deal with overlapping memory areas. We know
4027 that the memory area 'path' points to is 'urllen' bytes big and that
4028 is bigger than the path. Use +1 to move the zero byte too. */
4029 memmove(&path[1], path, strlen(path)+1);
4034 /* sanitise paths and remove ../ and ./ sequences according to RFC3986 */
4035 char *newp = Curl_dedotdotify(path);
4037 return CURLE_OUT_OF_MEMORY;
4039 if(strcmp(newp, path)) {
4041 free(data->state.pathbuffer);
4042 data->state.pathbuffer = newp;
4043 data->state.path = newp;
4051 * "rebuild_url" means that one or more URL components have been modified so
4052 * we need to generate an updated full version. We need the corrected URL
4053 * when communicating over HTTP proxy and we don't know at this point if
4054 * we're using a proxy or not.
4059 size_t plen = strlen(path); /* new path, should be 1 byte longer than
4061 size_t urllen = strlen(data->change.url); /* original URL length */
4063 size_t prefixlen = strlen(conn->host.name);
4066 prefixlen += strlen(protop) + strlen("://");
4068 reurl = malloc(urllen + 2); /* 2 for zerobyte + slash */
4070 return CURLE_OUT_OF_MEMORY;
4072 /* copy the prefix */
4073 memcpy(reurl, data->change.url, prefixlen);
4075 /* append the trailing piece + zerobyte */
4076 memcpy(&reurl[prefixlen], path, plen + 1);
4078 /* possible free the old one */
4079 if(data->change.url_alloc) {
4080 Curl_safefree(data->change.url);
4081 data->change.url_alloc = FALSE;
4084 infof(data, "Rebuilt URL to: %s\n", reurl);
4086 data->change.url = reurl;
4087 data->change.url_alloc = TRUE; /* free this later */
4091 * Parse the login details from the URL and strip them out of
4094 result = parse_url_login(data, conn, userp, passwdp, optionsp);
4098 if(conn->host.name[0] == '[') {
4099 /* This looks like an IPv6 address literal. See if there is an address
4100 scope if there is no location header */
4101 char *percent = strchr(conn->host.name, '%');
4103 unsigned int identifier_offset = 3;
4105 unsigned long scope;
4106 if(strncmp("%25", percent, 3) != 0) {
4108 "Please URL encode %% as %%25, see RFC 6874.\n");
4109 identifier_offset = 1;
4111 scope = strtoul(percent + identifier_offset, &endp, 10);
4113 /* The address scope was well formed. Knock it out of the
4115 memmove(percent, endp, strlen(endp)+1);
4116 conn->scope_id = (unsigned int)scope;
4119 /* Zone identifier is not numeric */
4120 #if defined(HAVE_NET_IF_H) && defined(IFNAMSIZ) && defined(HAVE_IF_NAMETOINDEX)
4121 char ifname[IFNAMSIZ + 2];
4122 char *square_bracket;
4123 unsigned int scopeidx = 0;
4124 strncpy(ifname, percent + identifier_offset, IFNAMSIZ + 2);
4125 /* Ensure nullbyte termination */
4126 ifname[IFNAMSIZ + 1] = '\0';
4127 square_bracket = strchr(ifname, ']');
4128 if(square_bracket) {
4130 *square_bracket = '\0';
4131 scopeidx = if_nametoindex(ifname);
4133 infof(data, "Invalid network interface: %s; %s\n", ifname,
4138 char *p = percent + identifier_offset + strlen(ifname);
4140 /* Remove zone identifier from hostname */
4141 memmove(percent, p, strlen(p) + 1);
4142 conn->scope_id = scopeidx;
4145 #endif /* HAVE_NET_IF_H && IFNAMSIZ */
4146 infof(data, "Invalid IPv6 address format\n");
4151 if(data->set.scope_id)
4152 /* Override any scope that was set above. */
4153 conn->scope_id = data->set.scope_id;
4155 /* Remove the fragment part of the path. Per RFC 2396, this is always the
4156 last part of the URI. We are looking for the first '#' so that we deal
4157 gracefully with non conformant URI such as http://example.com#foo#bar. */
4158 fragment = strchr(path, '#');
4162 /* we know the path part ended with a fragment, so we know the full URL
4163 string does too and we need to cut it off from there so it isn't used
4165 fragment = strchr(data->change.url, '#');
4171 * So if the URL was A://B/C#D,
4173 * conn->host.name is B
4174 * data->state.path is /C
4177 return findprotocol(data, conn, protop);
4181 * If we're doing a resumed transfer, we need to setup our stuff
4184 static CURLcode setup_range(struct SessionHandle *data)
4186 struct UrlState *s = &data->state;
4187 s->resume_from = data->set.set_resume_from;
4188 if(s->resume_from || data->set.str[STRING_SET_RANGE]) {
4189 if(s->rangestringalloc)
4193 s->range = aprintf("%" CURL_FORMAT_CURL_OFF_TU "-", s->resume_from);
4195 s->range = strdup(data->set.str[STRING_SET_RANGE]);
4197 s->rangestringalloc = (s->range)?TRUE:FALSE;
4200 return CURLE_OUT_OF_MEMORY;
4202 /* tell ourselves to fetch this range */
4203 s->use_range = TRUE; /* enable range download */
4206 s->use_range = FALSE; /* disable range download */
4213 * setup_connection_internals() -
4215 * Setup connection internals specific to the requested protocol in the
4216 * SessionHandle. This is inited and setup before the connection is made but
4217 * is about the particular protocol that is to be used.
4219 * This MUST get called after proxy magic has been figured out.
4221 static CURLcode setup_connection_internals(struct connectdata *conn)
4223 const struct Curl_handler * p;
4225 struct SessionHandle *data = conn->data;
4227 /* in some case in the multi state-machine, we go back to the CONNECT state
4228 and then a second (or third or...) call to this function will be made
4229 without doing a DISCONNECT or DONE in between (since the connection is
4230 yet in place) and therefore this function needs to first make sure
4231 there's no lingering previous data allocated. */
4232 Curl_free_request_state(data);
4234 memset(&data->req, 0, sizeof(struct SingleRequest));
4235 data->req.maxdownload = -1;
4237 conn->socktype = SOCK_STREAM; /* most of them are TCP streams */
4239 /* Perform setup complement if some. */
4242 if(p->setup_connection) {
4243 result = (*p->setup_connection)(conn);
4248 p = conn->handler; /* May have changed. */
4252 /* we check for -1 here since if proxy was detected already, this
4253 was very likely already set to the proxy port */
4254 conn->port = p->defport;
4256 /* only if remote_port was not already parsed off the URL we use the
4257 default port number */
4258 if(conn->remote_port < 0)
4259 conn->remote_port = (unsigned short)conn->given->defport;
4265 * Curl_free_request_state() should free temp data that was allocated in the
4266 * SessionHandle for this single request.
4269 void Curl_free_request_state(struct SessionHandle *data)
4271 Curl_safefree(data->req.protop);
4272 Curl_safefree(data->req.newurl);
4276 #ifndef CURL_DISABLE_PROXY
4277 /****************************************************************
4278 * Checks if the host is in the noproxy list. returns true if it matches
4279 * and therefore the proxy should NOT be used.
4280 ****************************************************************/
4281 static bool check_noproxy(const char* name, const char* no_proxy)
4283 /* no_proxy=domain1.dom,host.domain2.dom
4284 * (a comma-separated list of hosts which should
4285 * not be proxied, or an asterisk to override
4286 * all proxy variables)
4290 const char* separator = ", ";
4291 size_t no_proxy_len;
4295 if(no_proxy && no_proxy[0]) {
4296 if(Curl_raw_equal("*", no_proxy)) {
4300 /* NO_PROXY was specified and it wasn't just an asterisk */
4302 no_proxy_len = strlen(no_proxy);
4303 endptr = strchr(name, ':');
4305 namelen = endptr - name;
4307 namelen = strlen(name);
4309 for(tok_start = 0; tok_start < no_proxy_len; tok_start = tok_end + 1) {
4310 while(tok_start < no_proxy_len &&
4311 strchr(separator, no_proxy[tok_start]) != NULL) {
4312 /* Look for the beginning of the token. */
4316 if(tok_start == no_proxy_len)
4317 break; /* It was all trailing separator chars, no more tokens. */
4319 for(tok_end = tok_start; tok_end < no_proxy_len &&
4320 strchr(separator, no_proxy[tok_end]) == NULL; ++tok_end)
4321 /* Look for the end of the token. */
4324 /* To match previous behaviour, where it was necessary to specify
4325 * ".local.com" to prevent matching "notlocal.com", we will leave
4328 if(no_proxy[tok_start] == '.')
4331 if((tok_end - tok_start) <= namelen) {
4332 /* Match the last part of the name to the domain we are checking. */
4333 const char *checkn = name + namelen - (tok_end - tok_start);
4334 if(Curl_raw_nequal(no_proxy + tok_start, checkn,
4335 tok_end - tok_start)) {
4336 if((tok_end - tok_start) == namelen || *(checkn - 1) == '.') {
4337 /* We either have an exact match, or the previous character is a .
4338 * so it is within the same domain, so no proxy for this host.
4343 } /* if((tok_end - tok_start) <= namelen) */
4344 } /* for(tok_start = 0; tok_start < no_proxy_len;
4345 tok_start = tok_end + 1) */
4346 } /* NO_PROXY was specified and it wasn't just an asterisk */
4351 /****************************************************************
4352 * Detect what (if any) proxy to use. Remember that this selects a host
4353 * name and is not limited to HTTP proxies only.
4354 * The returned pointer must be freed by the caller (unless NULL)
4355 ****************************************************************/
4356 static char *detect_proxy(struct connectdata *conn)
4360 #ifndef CURL_DISABLE_HTTP
4361 /* If proxy was not specified, we check for default proxy environment
4362 * variables, to enable i.e Lynx compliance:
4364 * http_proxy=http://some.server.dom:port/
4365 * https_proxy=http://some.server.dom:port/
4366 * ftp_proxy=http://some.server.dom:port/
4367 * no_proxy=domain1.dom,host.domain2.dom
4368 * (a comma-separated list of hosts which should
4369 * not be proxied, or an asterisk to override
4370 * all proxy variables)
4371 * all_proxy=http://some.server.dom:port/
4372 * (seems to exist for the CERN www lib. Probably
4373 * the first to check for.)
4375 * For compatibility, the all-uppercase versions of these variables are
4376 * checked if the lowercase versions don't exist.
4378 char *no_proxy=NULL;
4379 char proxy_env[128];
4381 no_proxy=curl_getenv("no_proxy");
4383 no_proxy=curl_getenv("NO_PROXY");
4385 if(!check_noproxy(conn->host.name, no_proxy)) {
4386 /* It was not listed as without proxy */
4387 const char *protop = conn->handler->scheme;
4388 char *envp = proxy_env;
4391 /* Now, build <protocol>_proxy and check for such a one to use */
4393 *envp++ = (char)tolower((int)*protop++);
4396 strcpy(envp, "_proxy");
4398 /* read the protocol proxy: */
4399 prox=curl_getenv(proxy_env);
4402 * We don't try the uppercase version of HTTP_PROXY because of
4405 * When curl is used in a webserver application
4406 * environment (cgi or php), this environment variable can
4407 * be controlled by the web server user by setting the
4408 * http header 'Proxy:' to some value.
4410 * This can cause 'internal' http/ftp requests to be
4411 * arbitrarily redirected by any external attacker.
4413 if(!prox && !Curl_raw_equal("http_proxy", proxy_env)) {
4414 /* There was no lowercase variable, try the uppercase version: */
4415 Curl_strntoupper(proxy_env, proxy_env, sizeof(proxy_env));
4416 prox=curl_getenv(proxy_env);
4420 proxy = prox; /* use this */
4422 proxy = curl_getenv("all_proxy"); /* default proxy to use */
4424 proxy=curl_getenv("ALL_PROXY");
4426 } /* if(!check_noproxy(conn->host.name, no_proxy)) - it wasn't specified
4431 #else /* !CURL_DISABLE_HTTP */
4434 #endif /* CURL_DISABLE_HTTP */
4440 * If this is supposed to use a proxy, we need to figure out the proxy
4441 * host name, so that we can re-use an existing connection
4442 * that may exist registered to the same proxy host.
4443 * proxy will be freed before this function returns.
4445 static CURLcode parse_proxy(struct SessionHandle *data,
4446 struct connectdata *conn, char *proxy)
4451 /* We use 'proxyptr' to point to the proxy name from now on... */
4456 /* We do the proxy host string parsing here. We want the host name and the
4457 * port name. Accept a protocol:// prefix
4460 /* Parse the protocol part if present */
4461 endofprot = strstr(proxy, "://");
4463 proxyptr = endofprot+3;
4464 if(checkprefix("socks5h", proxy))
4465 conn->proxytype = CURLPROXY_SOCKS5_HOSTNAME;
4466 else if(checkprefix("socks5", proxy))
4467 conn->proxytype = CURLPROXY_SOCKS5;
4468 else if(checkprefix("socks4a", proxy))
4469 conn->proxytype = CURLPROXY_SOCKS4A;
4470 else if(checkprefix("socks4", proxy) || checkprefix("socks", proxy))
4471 conn->proxytype = CURLPROXY_SOCKS4;
4472 /* Any other xxx:// : change to http proxy */
4475 proxyptr = proxy; /* No xxx:// head: It's a HTTP proxy */
4477 /* Is there a username and password given in this proxy url? */
4478 atsign = strchr(proxyptr, '@');
4480 char *proxyuser = NULL;
4481 char *proxypasswd = NULL;
4483 parse_login_details(proxyptr, atsign - proxyptr,
4484 &proxyuser, &proxypasswd, NULL);
4486 /* found user and password, rip them out. note that we are
4487 unescaping them, as there is otherwise no way to have a
4488 username or password with reserved characters like ':' in
4490 Curl_safefree(conn->proxyuser);
4491 if(proxyuser && strlen(proxyuser) < MAX_CURL_USER_LENGTH)
4492 conn->proxyuser = curl_easy_unescape(data, proxyuser, 0, NULL);
4494 conn->proxyuser = strdup("");
4496 if(!conn->proxyuser)
4497 result = CURLE_OUT_OF_MEMORY;
4499 Curl_safefree(conn->proxypasswd);
4500 if(proxypasswd && strlen(proxypasswd) < MAX_CURL_PASSWORD_LENGTH)
4501 conn->proxypasswd = curl_easy_unescape(data, proxypasswd, 0, NULL);
4503 conn->proxypasswd = strdup("");
4505 if(!conn->proxypasswd)
4506 result = CURLE_OUT_OF_MEMORY;
4510 conn->bits.proxy_user_passwd = TRUE; /* enable it */
4511 atsign++; /* the right side of the @-letter */
4513 proxyptr = atsign; /* now use this instead */
4517 Curl_safefree(proxyuser);
4518 Curl_safefree(proxypasswd);
4524 /* start scanning for port number at this point */
4527 /* detect and extract RFC6874-style IPv6-addresses */
4528 if(*proxyptr == '[') {
4529 char *ptr = ++proxyptr; /* advance beyond the initial bracket */
4530 while(*ptr && (ISXDIGIT(*ptr) || (*ptr == ':') || (*ptr == '.')))
4533 /* There might be a zone identifier */
4534 if(strncmp("%25", ptr, 3))
4535 infof(data, "Please URL encode %% as %%25, see RFC 6874.\n");
4537 /* Allow unresered characters as defined in RFC 3986 */
4538 while(*ptr && (ISALPHA(*ptr) || ISXDIGIT(*ptr) || (*ptr == '-') ||
4539 (*ptr == '.') || (*ptr == '_') || (*ptr == '~')))
4543 /* yeps, it ended nicely with a bracket as well */
4546 infof(data, "Invalid IPv6 address format\n");
4548 /* Note that if this didn't end with a bracket, we still advanced the
4549 * proxyptr first, but I can't see anything wrong with that as no host
4550 * name nor a numeric can legally start with a bracket.
4554 /* Get port number off proxy.server.com:1080 */
4555 prox_portno = strchr(portptr, ':');
4557 *prox_portno = 0x0; /* cut off number from host name */
4559 /* now set the local port number */
4560 conn->port = strtol(prox_portno, NULL, 10);
4563 if(proxyptr[0]=='/')
4564 /* If the first character in the proxy string is a slash, fail
4565 immediately. The following code will otherwise clear the string which
4566 will lead to code running as if no proxy was set! */
4567 return CURLE_COULDNT_RESOLVE_PROXY;
4569 /* without a port number after the host name, some people seem to use
4570 a slash so we strip everything from the first slash */
4571 atsign = strchr(proxyptr, '/');
4573 *atsign = 0x0; /* cut off path part from host name */
4575 if(data->set.proxyport)
4576 /* None given in the proxy string, then get the default one if it is
4578 conn->port = data->set.proxyport;
4581 /* now, clone the cleaned proxy host name */
4582 conn->proxy.rawalloc = strdup(proxyptr);
4583 conn->proxy.name = conn->proxy.rawalloc;
4585 if(!conn->proxy.rawalloc)
4586 return CURLE_OUT_OF_MEMORY;
4592 * Extract the user and password from the authentication string
4594 static CURLcode parse_proxy_auth(struct SessionHandle *data,
4595 struct connectdata *conn)
4597 char proxyuser[MAX_CURL_USER_LENGTH]="";
4598 char proxypasswd[MAX_CURL_PASSWORD_LENGTH]="";
4600 if(data->set.str[STRING_PROXYUSERNAME] != NULL) {
4601 strncpy(proxyuser, data->set.str[STRING_PROXYUSERNAME],
4602 MAX_CURL_USER_LENGTH);
4603 proxyuser[MAX_CURL_USER_LENGTH-1] = '\0'; /*To be on safe side*/
4605 if(data->set.str[STRING_PROXYPASSWORD] != NULL) {
4606 strncpy(proxypasswd, data->set.str[STRING_PROXYPASSWORD],
4607 MAX_CURL_PASSWORD_LENGTH);
4608 proxypasswd[MAX_CURL_PASSWORD_LENGTH-1] = '\0'; /*To be on safe side*/
4611 conn->proxyuser = curl_easy_unescape(data, proxyuser, 0, NULL);
4612 if(!conn->proxyuser)
4613 return CURLE_OUT_OF_MEMORY;
4615 conn->proxypasswd = curl_easy_unescape(data, proxypasswd, 0, NULL);
4616 if(!conn->proxypasswd)
4617 return CURLE_OUT_OF_MEMORY;
4621 #endif /* CURL_DISABLE_PROXY */
4626 * Parse the login details (user name, password and options) from the URL and
4627 * strip them out of the host name
4629 * Inputs: data->set.use_netrc (CURLOPT_NETRC)
4632 * Outputs: (almost :- all currently undefined)
4633 * conn->bits.user_passwd - non-zero if non-default passwords exist
4634 * user - non-zero length if defined
4635 * passwd - non-zero length if defined
4636 * options - non-zero length if defined
4637 * conn->host.name - remove user name and password
4639 static CURLcode parse_url_login(struct SessionHandle *data,
4640 struct connectdata *conn,
4641 char **user, char **passwd, char **options)
4643 CURLcode result = CURLE_OK;
4645 char *passwdp = NULL;
4646 char *optionsp = NULL;
4648 /* At this point, we're hoping all the other special cases have
4649 * been taken care of, so conn->host.name is at most
4650 * [user[:password][;options]]@]hostname
4652 * We need somewhere to put the embedded details, so do that first.
4655 char *ptr = strchr(conn->host.name, '@');
4656 char *login = conn->host.name;
4658 DEBUGASSERT(!**user);
4659 DEBUGASSERT(!**passwd);
4660 DEBUGASSERT(!**options);
4665 /* We will now try to extract the
4666 * possible login information in a string like:
4667 * ftp://user:password@ftp.my.site:8021/README */
4668 conn->host.name = ++ptr;
4670 /* So the hostname is sane. Only bother interpreting the
4671 * results if we could care. It could still be wasted
4672 * work because it might be overtaken by the programmatically
4673 * set user/passwd, but doing that first adds more cases here :-(
4676 if(data->set.use_netrc == CURL_NETRC_REQUIRED)
4679 /* We could use the login information in the URL so extract it */
4680 result = parse_login_details(login, ptr - login - 1,
4681 &userp, &passwdp, &optionsp);
4688 /* We have a user in the URL */
4689 conn->bits.userpwd_in_url = TRUE;
4690 conn->bits.user_passwd = TRUE; /* enable user+password */
4692 /* Decode the user */
4693 newname = curl_easy_unescape(data, userp, 0, NULL);
4695 result = CURLE_OUT_OF_MEMORY;
4704 /* We have a password in the URL so decode it */
4705 char *newpasswd = curl_easy_unescape(data, passwdp, 0, NULL);
4707 result = CURLE_OUT_OF_MEMORY;
4712 *passwd = newpasswd;
4716 /* We have an options list in the URL so decode it */
4717 char *newoptions = curl_easy_unescape(data, optionsp, 0, NULL);
4719 result = CURLE_OUT_OF_MEMORY;
4724 *options = newoptions;
4730 Curl_safefree(userp);
4731 Curl_safefree(passwdp);
4732 Curl_safefree(optionsp);
4738 * parse_login_details()
4740 * This is used to parse a login string for user name, password and options in
4741 * the following formats:
4745 * user:password;options
4747 * user;options:password
4755 * login [in] - The login string.
4756 * len [in] - The length of the login string.
4757 * userp [in/out] - The address where a pointer to newly allocated memory
4758 * holding the user will be stored upon completion.
4759 * passdwp [in/out] - The address where a pointer to newly allocated memory
4760 * holding the password will be stored upon completion.
4761 * optionsp [in/out] - The address where a pointer to newly allocated memory
4762 * holding the options will be stored upon completion.
4764 * Returns CURLE_OK on success.
4766 static CURLcode parse_login_details(const char *login, const size_t len,
4767 char **userp, char **passwdp,
4770 CURLcode result = CURLE_OK;
4774 const char *psep = NULL;
4775 const char *osep = NULL;
4780 /* Attempt to find the password separator */
4782 psep = strchr(login, ':');
4784 /* Within the constraint of the login string */
4785 if(psep >= login + len)
4789 /* Attempt to find the options separator */
4791 osep = strchr(login, ';');
4793 /* Within the constraint of the login string */
4794 if(osep >= login + len)
4798 /* Calculate the portion lengths */
4800 (size_t)(osep && psep > osep ? osep - login : psep - login) :
4801 (osep ? (size_t)(osep - login) : len));
4803 (osep && osep > psep ? (size_t)(osep - psep) :
4804 (size_t)(login + len - psep)) - 1 : 0);
4806 (psep && psep > osep ? (size_t)(psep - osep) :
4807 (size_t)(login + len - osep)) - 1 : 0);
4809 /* Allocate the user portion buffer */
4811 ubuf = malloc(ulen + 1);
4813 result = CURLE_OUT_OF_MEMORY;
4816 /* Allocate the password portion buffer */
4817 if(!result && passwdp && plen) {
4818 pbuf = malloc(plen + 1);
4820 Curl_safefree(ubuf);
4821 result = CURLE_OUT_OF_MEMORY;
4825 /* Allocate the options portion buffer */
4826 if(!result && optionsp && olen) {
4827 obuf = malloc(olen + 1);
4829 Curl_safefree(pbuf);
4830 Curl_safefree(ubuf);
4831 result = CURLE_OUT_OF_MEMORY;
4836 /* Store the user portion if necessary */
4838 memcpy(ubuf, login, ulen);
4840 Curl_safefree(*userp);
4844 /* Store the password portion if necessary */
4846 memcpy(pbuf, psep + 1, plen);
4848 Curl_safefree(*passwdp);
4852 /* Store the options portion if necessary */
4854 memcpy(obuf, osep + 1, olen);
4856 Curl_safefree(*optionsp);
4864 /*************************************************************
4865 * Figure out the remote port number and fix it in the URL
4867 * No matter if we use a proxy or not, we have to figure out the remote
4868 * port number of various reasons.
4870 * To be able to detect port number flawlessly, we must not confuse them
4871 * IPv6-specified addresses in the [0::1] style. (RFC2732)
4873 * The conn->host.name is currently [user:passwd@]host[:port] where host
4874 * could be a hostname, IPv4 address or IPv6 address.
4876 * The port number embedded in the URL is replaced, if necessary.
4877 *************************************************************/
4878 static CURLcode parse_remote_port(struct SessionHandle *data,
4879 struct connectdata *conn)
4884 /* Note that at this point, the IPv6 address cannot contain any scope
4885 suffix as that has already been removed in the parseurlandfillconn()
4887 if((1 == sscanf(conn->host.name, "[%*45[0123456789abcdefABCDEF:.]%c",
4889 (']' == endbracket)) {
4890 /* this is a RFC2732-style specified IP-address */
4891 conn->bits.ipv6_ip = TRUE;
4893 conn->host.name++; /* skip over the starting bracket */
4894 portptr = strchr(conn->host.name, ']');
4896 *portptr++ = '\0'; /* zero terminate, killing the bracket */
4898 portptr = NULL; /* no port number available */
4903 struct in6_addr in6;
4904 if(Curl_inet_pton(AF_INET6, conn->host.name, &in6) > 0) {
4905 /* This is a numerical IPv6 address, meaning this is a wrongly formatted
4907 failf(data, "IPv6 numerical address used in URL without brackets");
4908 return CURLE_URL_MALFORMAT;
4912 portptr = strrchr(conn->host.name, ':');
4915 if(data->set.use_port && data->state.allow_port) {
4916 /* if set, we use this and ignore the port possibly given in the URL */
4917 conn->remote_port = (unsigned short)data->set.use_port;
4919 *portptr = '\0'; /* cut off the name there anyway - if there was a port
4920 number - since the port number is to be ignored! */
4921 if(conn->bits.httpproxy) {
4922 /* we need to create new URL with the new port number */
4926 if(conn->bits.type_set)
4927 snprintf(type, sizeof(type), ";type=%c",
4928 data->set.prefer_ascii?'A':
4929 (data->set.ftp_list_only?'D':'I'));
4932 * This synthesized URL isn't always right--suffixes like ;type=A are
4933 * stripped off. It would be better to work directly from the original
4934 * URL and simply replace the port part of it.
4936 url = aprintf("%s://%s%s%s:%hu%s%s%s", conn->given->scheme,
4937 conn->bits.ipv6_ip?"[":"", conn->host.name,
4938 conn->bits.ipv6_ip?"]":"", conn->remote_port,
4939 data->state.slash_removed?"/":"", data->state.path,
4942 return CURLE_OUT_OF_MEMORY;
4944 if(data->change.url_alloc) {
4945 Curl_safefree(data->change.url);
4946 data->change.url_alloc = FALSE;
4949 data->change.url = url;
4950 data->change.url_alloc = TRUE;
4954 /* no CURLOPT_PORT given, extract the one from the URL */
4959 port=strtol(portptr+1, &rest, 10); /* Port number must be decimal */
4961 if((port < 0) || (port > 0xffff)) {
4962 /* Single unix standard says port numbers are 16 bits long */
4963 failf(data, "Port number out of range");
4964 return CURLE_URL_MALFORMAT;
4967 else if(rest != &portptr[1]) {
4968 *portptr = '\0'; /* cut off the name there */
4969 conn->remote_port = curlx_ultous(port);
4972 /* Browser behavior adaptation. If there's a colon with no digits after,
4973 just cut off the name there which makes us ignore the colon and just
4974 use the default port. Firefox and Chrome both do that. */
4981 * Override the login details from the URL with that in the CURLOPT_USERPWD
4982 * option or a .netrc file, if applicable.
4984 static CURLcode override_login(struct SessionHandle *data,
4985 struct connectdata *conn,
4986 char **userp, char **passwdp, char **optionsp)
4988 if(data->set.str[STRING_USERNAME]) {
4990 *userp = strdup(data->set.str[STRING_USERNAME]);
4992 return CURLE_OUT_OF_MEMORY;
4995 if(data->set.str[STRING_PASSWORD]) {
4997 *passwdp = strdup(data->set.str[STRING_PASSWORD]);
4999 return CURLE_OUT_OF_MEMORY;
5002 if(data->set.str[STRING_OPTIONS]) {
5004 *optionsp = strdup(data->set.str[STRING_OPTIONS]);
5006 return CURLE_OUT_OF_MEMORY;
5009 conn->bits.netrc = FALSE;
5010 if(data->set.use_netrc != CURL_NETRC_IGNORED) {
5011 int ret = Curl_parsenetrc(conn->host.name,
5013 data->set.str[STRING_NETRC_FILE]);
5015 infof(data, "Couldn't find host %s in the "
5016 DOT_CHAR "netrc file; using defaults\n",
5020 return CURLE_OUT_OF_MEMORY;
5023 /* set bits.netrc TRUE to remember that we got the name from a .netrc
5024 file, so that it is safe to use even if we followed a Location: to a
5025 different host or similar. */
5026 conn->bits.netrc = TRUE;
5028 conn->bits.user_passwd = TRUE; /* enable user+password */
5036 * Set the login details so they're available in the connection
5038 static CURLcode set_login(struct connectdata *conn,
5039 const char *user, const char *passwd,
5040 const char *options)
5042 CURLcode result = CURLE_OK;
5044 /* If our protocol needs a password and we have none, use the defaults */
5045 if((conn->handler->flags & PROTOPT_NEEDSPWD) && !conn->bits.user_passwd) {
5046 /* Store the default user */
5047 conn->user = strdup(CURL_DEFAULT_USER);
5049 /* Store the default password */
5051 conn->passwd = strdup(CURL_DEFAULT_PASSWORD);
5053 conn->passwd = NULL;
5055 /* This is the default password, so DON'T set conn->bits.user_passwd */
5058 /* Store the user, zero-length if not set */
5059 conn->user = strdup(user);
5061 /* Store the password (only if user is present), zero-length if not set */
5063 conn->passwd = strdup(passwd);
5065 conn->passwd = NULL;
5068 if(!conn->user || !conn->passwd)
5069 result = CURLE_OUT_OF_MEMORY;
5071 /* Store the options, null if not set */
5072 if(!result && options[0]) {
5073 conn->options = strdup(options);
5076 result = CURLE_OUT_OF_MEMORY;
5082 /*************************************************************
5083 * Resolve the address of the server or proxy
5084 *************************************************************/
5085 static CURLcode resolve_server(struct SessionHandle *data,
5086 struct connectdata *conn,
5089 CURLcode result=CURLE_OK;
5090 long timeout_ms = Curl_timeleft(data, NULL, TRUE);
5092 /*************************************************************
5093 * Resolve the name of the server or proxy
5094 *************************************************************/
5095 if(conn->bits.reuse)
5096 /* We're reusing the connection - no need to resolve anything, and
5097 fix_hostname() was called already in create_conn() for the re-use
5102 /* this is a fresh connect */
5104 struct Curl_dns_entry *hostaddr;
5106 /* set a pointer to the hostname we display */
5107 fix_hostname(data, conn, &conn->host);
5109 #ifdef USE_UNIX_SOCKETS
5110 if(data->set.str[STRING_UNIX_SOCKET_PATH]) {
5111 /* Unix domain sockets are local. The host gets ignored, just use the
5112 * specified domain socket address. Do not cache "DNS entries". There is
5113 * no DNS involved and we already have the filesystem path available */
5114 const char *path = data->set.str[STRING_UNIX_SOCKET_PATH];
5116 hostaddr = calloc(1, sizeof(struct Curl_dns_entry));
5118 result = CURLE_OUT_OF_MEMORY;
5119 else if((hostaddr->addr = Curl_unix2addr(path)) != NULL)
5122 /* Long paths are not supported for now */
5123 if(strlen(path) >= sizeof(((struct sockaddr_un *)0)->sun_path)) {
5124 failf(data, "Unix socket path too long: '%s'", path);
5125 result = CURLE_COULDNT_RESOLVE_HOST;
5128 result = CURLE_OUT_OF_MEMORY;
5135 if(!conn->proxy.name || !*conn->proxy.name) {
5136 /* If not connecting via a proxy, extract the port from the URL, if it is
5137 * there, thus overriding any defaults that might have been set above. */
5138 conn->port = conn->remote_port; /* it is the same port */
5140 /* Resolve target host right on */
5141 rc = Curl_resolv_timeout(conn, conn->host.name, (int)conn->port,
5142 &hostaddr, timeout_ms);
5143 if(rc == CURLRESOLV_PENDING)
5146 else if(rc == CURLRESOLV_TIMEDOUT)
5147 result = CURLE_OPERATION_TIMEDOUT;
5149 else if(!hostaddr) {
5150 failf(data, "Couldn't resolve host '%s'", conn->host.dispname);
5151 result = CURLE_COULDNT_RESOLVE_HOST;
5152 /* don't return yet, we need to clean up the timeout first */
5156 /* This is a proxy that hasn't been resolved yet. */
5158 /* IDN-fix the proxy name */
5159 fix_hostname(data, conn, &conn->proxy);
5162 rc = Curl_resolv_timeout(conn, conn->proxy.name, (int)conn->port,
5163 &hostaddr, timeout_ms);
5165 if(rc == CURLRESOLV_PENDING)
5168 else if(rc == CURLRESOLV_TIMEDOUT)
5169 result = CURLE_OPERATION_TIMEDOUT;
5171 else if(!hostaddr) {
5172 failf(data, "Couldn't resolve proxy '%s'", conn->proxy.dispname);
5173 result = CURLE_COULDNT_RESOLVE_PROXY;
5174 /* don't return yet, we need to clean up the timeout first */
5177 DEBUGASSERT(conn->dns_entry == NULL);
5178 conn->dns_entry = hostaddr;
5185 * Cleanup the connection just allocated before we can move along and use the
5186 * previously existing one. All relevant data is copied over and old_conn is
5187 * ready for freeing once this function returns.
5189 static void reuse_conn(struct connectdata *old_conn,
5190 struct connectdata *conn)
5192 if(old_conn->proxy.rawalloc)
5193 free(old_conn->proxy.rawalloc);
5195 /* free the SSL config struct from this connection struct as this was
5196 allocated in vain and is targeted for destruction */
5197 Curl_free_ssl_config(&old_conn->ssl_config);
5199 conn->data = old_conn->data;
5201 /* get the user+password information from the old_conn struct since it may
5202 * be new for this request even when we re-use an existing connection */
5203 conn->bits.user_passwd = old_conn->bits.user_passwd;
5204 if(conn->bits.user_passwd) {
5205 /* use the new user name and password though */
5206 Curl_safefree(conn->user);
5207 Curl_safefree(conn->passwd);
5208 conn->user = old_conn->user;
5209 conn->passwd = old_conn->passwd;
5210 old_conn->user = NULL;
5211 old_conn->passwd = NULL;
5214 conn->bits.proxy_user_passwd = old_conn->bits.proxy_user_passwd;
5215 if(conn->bits.proxy_user_passwd) {
5216 /* use the new proxy user name and proxy password though */
5217 Curl_safefree(conn->proxyuser);
5218 Curl_safefree(conn->proxypasswd);
5219 conn->proxyuser = old_conn->proxyuser;
5220 conn->proxypasswd = old_conn->proxypasswd;
5221 old_conn->proxyuser = NULL;
5222 old_conn->proxypasswd = NULL;
5225 /* host can change, when doing keepalive with a proxy or if the case is
5226 different this time etc */
5227 Curl_safefree(conn->host.rawalloc);
5228 conn->host=old_conn->host;
5230 /* persist connection info in session handle */
5231 Curl_persistconninfo(conn);
5234 conn->bits.reuse = TRUE; /* yes, we're re-using here */
5236 Curl_safefree(old_conn->user);
5237 Curl_safefree(old_conn->passwd);
5238 Curl_safefree(old_conn->proxyuser);
5239 Curl_safefree(old_conn->proxypasswd);
5240 Curl_safefree(old_conn->localdev);
5242 Curl_llist_destroy(old_conn->send_pipe, NULL);
5243 Curl_llist_destroy(old_conn->recv_pipe, NULL);
5245 old_conn->send_pipe = NULL;
5246 old_conn->recv_pipe = NULL;
5248 Curl_safefree(old_conn->master_buffer);
5252 * create_conn() sets up a new connectdata struct, or re-uses an already
5253 * existing one, and resolves host name.
5255 * if this function returns CURLE_OK and *async is set to TRUE, the resolve
5256 * response will be coming asynchronously. If *async is FALSE, the name is
5259 * @param data The sessionhandle pointer
5260 * @param in_connect is set to the next connection data pointer
5261 * @param async is set TRUE when an async DNS resolution is pending
5262 * @see Curl_setup_conn()
5264 * *NOTE* this function assigns the conn->data pointer!
5267 static CURLcode create_conn(struct SessionHandle *data,
5268 struct connectdata **in_connect,
5271 CURLcode result = CURLE_OK;
5272 struct connectdata *conn;
5273 struct connectdata *conn_temp = NULL;
5276 char *passwd = NULL;
5277 char *options = NULL;
5280 bool prot_missing = FALSE;
5281 bool no_connections_available = FALSE;
5282 bool force_reuse = FALSE;
5283 size_t max_host_connections = Curl_multi_max_host_connections(data->multi);
5284 size_t max_total_connections = Curl_multi_max_total_connections(data->multi);
5288 /*************************************************************
5290 *************************************************************/
5292 if(!data->change.url) {
5293 result = CURLE_URL_MALFORMAT;
5297 /* First, split up the current URL in parts so that we can use the
5298 parts for checking against the already present connections. In order
5299 to not have to modify everything at once, we allocate a temporary
5300 connection data struct and fill in for comparison purposes. */
5301 conn = allocate_conn(data);
5304 result = CURLE_OUT_OF_MEMORY;
5308 /* We must set the return variable as soon as possible, so that our
5309 parent can cleanup any possible allocs we may have done before
5313 /* This initing continues below, see the comment "Continue connectdata
5314 * initialization here" */
5316 /***********************************************************
5317 * We need to allocate memory to store the path in. We get the size of the
5318 * full URL to be sure, and we need to make it at least 256 bytes since
5319 * other parts of the code will rely on this fact
5320 ***********************************************************/
5321 #define LEAST_PATH_ALLOC 256
5322 urllen=strlen(data->change.url);
5323 if(urllen < LEAST_PATH_ALLOC)
5324 urllen=LEAST_PATH_ALLOC;
5327 * We malloc() the buffers below urllen+2 to make room for 2 possibilities:
5328 * 1 - an extra terminating zero
5329 * 2 - an extra slash (in case a syntax like "www.host.com?moo" is used)
5332 Curl_safefree(data->state.pathbuffer);
5333 data->state.path = NULL;
5335 data->state.pathbuffer = malloc(urllen+2);
5336 if(NULL == data->state.pathbuffer) {
5337 result = CURLE_OUT_OF_MEMORY; /* really bad error */
5340 data->state.path = data->state.pathbuffer;
5342 conn->host.rawalloc = malloc(urllen+2);
5343 if(NULL == conn->host.rawalloc) {
5344 Curl_safefree(data->state.pathbuffer);
5345 data->state.path = NULL;
5346 result = CURLE_OUT_OF_MEMORY;
5350 conn->host.name = conn->host.rawalloc;
5351 conn->host.name[0] = 0;
5354 passwd = strdup("");
5355 options = strdup("");
5356 if(!user || !passwd || !options) {
5357 result = CURLE_OUT_OF_MEMORY;
5361 result = parseurlandfillconn(data, conn, &prot_missing, &user, &passwd,
5366 /*************************************************************
5367 * No protocol part in URL was used, add it!
5368 *************************************************************/
5370 /* We're guessing prefixes here and if we're told to use a proxy or if
5371 we're gonna follow a Location: later or... then we need the protocol
5372 part added so that we have a valid URL. */
5375 reurl = aprintf("%s://%s", conn->handler->scheme, data->change.url);
5378 result = CURLE_OUT_OF_MEMORY;
5382 if(data->change.url_alloc) {
5383 Curl_safefree(data->change.url);
5384 data->change.url_alloc = FALSE;
5387 data->change.url = reurl;
5388 data->change.url_alloc = TRUE; /* free this later */
5391 /*************************************************************
5392 * If the protocol can't handle url query strings, then cut
5393 * off the unhandable part
5394 *************************************************************/
5395 if((conn->given->flags&PROTOPT_NOURLQUERY)) {
5396 char *path_q_sep = strchr(conn->data->state.path, '?');
5398 /* according to rfc3986, allow the query (?foo=bar)
5399 also on protocols that can't handle it.
5401 cut the string-part after '?'
5404 /* terminate the string */
5409 if(data->set.str[STRING_BEARER]) {
5410 conn->xoauth2_bearer = strdup(data->set.str[STRING_BEARER]);
5411 if(!conn->xoauth2_bearer) {
5412 result = CURLE_OUT_OF_MEMORY;
5417 #ifndef CURL_DISABLE_PROXY
5418 /*************************************************************
5419 * Extract the user and password from the authentication string
5420 *************************************************************/
5421 if(conn->bits.proxy_user_passwd) {
5422 result = parse_proxy_auth(data, conn);
5427 /*************************************************************
5428 * Detect what (if any) proxy to use
5429 *************************************************************/
5430 if(data->set.str[STRING_PROXY]) {
5431 proxy = strdup(data->set.str[STRING_PROXY]);
5432 /* if global proxy is set, this is it */
5434 failf(data, "memory shortage");
5435 result = CURLE_OUT_OF_MEMORY;
5440 if(data->set.str[STRING_NOPROXY] &&
5441 check_noproxy(conn->host.name, data->set.str[STRING_NOPROXY])) {
5443 free(proxy); /* proxy is in exception list */
5448 proxy = detect_proxy(conn);
5450 #ifdef USE_UNIX_SOCKETS
5451 if(proxy && data->set.str[STRING_UNIX_SOCKET_PATH]) {
5452 free(proxy); /* Unix domain sockets cannot be proxied, so disable it */
5457 if(proxy && (!*proxy || (conn->handler->flags & PROTOPT_NONETWORK))) {
5458 free(proxy); /* Don't bother with an empty proxy string or if the
5459 protocol doesn't work with network */
5463 /***********************************************************************
5464 * If this is supposed to use a proxy, we need to figure out the proxy host
5465 * name, proxy type and port number, so that we can re-use an existing
5466 * connection that may exist registered to the same proxy host.
5467 ***********************************************************************/
5469 result = parse_proxy(data, conn, proxy);
5471 Curl_safefree(proxy); /* parse_proxy copies the proxy string */
5476 if((conn->proxytype == CURLPROXY_HTTP) ||
5477 (conn->proxytype == CURLPROXY_HTTP_1_0)) {
5478 #ifdef CURL_DISABLE_HTTP
5479 /* asking for a HTTP proxy is a bit funny when HTTP is disabled... */
5480 result = CURLE_UNSUPPORTED_PROTOCOL;
5483 /* force this connection's protocol to become HTTP if not already
5484 compatible - if it isn't tunneling through */
5485 if(!(conn->handler->protocol & PROTO_FAMILY_HTTP) &&
5486 !conn->bits.tunnel_proxy)
5487 conn->handler = &Curl_handler_http;
5489 conn->bits.httpproxy = TRUE;
5493 conn->bits.httpproxy = FALSE; /* not a HTTP proxy */
5494 conn->bits.proxy = TRUE;
5497 /* we aren't using the proxy after all... */
5498 conn->bits.proxy = FALSE;
5499 conn->bits.httpproxy = FALSE;
5500 conn->bits.proxy_user_passwd = FALSE;
5501 conn->bits.tunnel_proxy = FALSE;
5504 #endif /* CURL_DISABLE_PROXY */
5506 /*************************************************************
5507 * If the protocol is using SSL and HTTP proxy is used, we set
5508 * the tunnel_proxy bit.
5509 *************************************************************/
5510 if((conn->given->flags&PROTOPT_SSL) && conn->bits.httpproxy)
5511 conn->bits.tunnel_proxy = TRUE;
5513 /*************************************************************
5514 * Figure out the remote port number and fix it in the URL
5515 *************************************************************/
5516 result = parse_remote_port(data, conn);
5520 /* Check for overridden login details and set them accordingly so they
5521 they are known when protocol->setup_connection is called! */
5522 result = override_login(data, conn, &user, &passwd, &options);
5525 result = set_login(conn, user, passwd, options);
5529 /*************************************************************
5530 * Setup internals depending on protocol. Needs to be done after
5531 * we figured out what/if proxy to use.
5532 *************************************************************/
5533 result = setup_connection_internals(conn);
5537 conn->recv[FIRSTSOCKET] = Curl_recv_plain;
5538 conn->send[FIRSTSOCKET] = Curl_send_plain;
5539 conn->recv[SECONDARYSOCKET] = Curl_recv_plain;
5540 conn->send[SECONDARYSOCKET] = Curl_send_plain;
5542 /***********************************************************************
5543 * file: is a special case in that it doesn't need a network connection
5544 ***********************************************************************/
5545 #ifndef CURL_DISABLE_FILE
5546 if(conn->handler->flags & PROTOPT_NONETWORK) {
5548 /* this is supposed to be the connect function so we better at least check
5549 that the file is present here! */
5550 DEBUGASSERT(conn->handler->connect_it);
5551 result = conn->handler->connect_it(conn, &done);
5553 /* Setup a "faked" transfer that'll do nothing */
5556 conn->bits.tcpconnect[FIRSTSOCKET] = TRUE; /* we are "connected */
5558 ConnectionStore(data, conn);
5561 * Setup whatever necessary for a resumed transfer
5563 result = setup_range(data);
5565 DEBUGASSERT(conn->handler->done);
5566 /* we ignore the return code for the protocol-specific DONE */
5567 (void)conn->handler->done(conn, result, FALSE);
5571 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, /* no download */
5572 -1, NULL); /* no upload */
5575 /* since we skip do_init() */
5582 /* Get a cloned copy of the SSL config situation stored in the
5583 connection struct. But to get this going nicely, we must first make
5584 sure that the strings in the master copy are pointing to the correct
5585 strings in the session handle strings array!
5587 Keep in mind that the pointers in the master copy are pointing to strings
5588 that will be freed as part of the SessionHandle struct, but all cloned
5589 copies will be separately allocated.
5591 data->set.ssl.CApath = data->set.str[STRING_SSL_CAPATH];
5592 data->set.ssl.CAfile = data->set.str[STRING_SSL_CAFILE];
5593 data->set.ssl.CRLfile = data->set.str[STRING_SSL_CRLFILE];
5594 data->set.ssl.issuercert = data->set.str[STRING_SSL_ISSUERCERT];
5595 data->set.ssl.random_file = data->set.str[STRING_SSL_RANDOM_FILE];
5596 data->set.ssl.egdsocket = data->set.str[STRING_SSL_EGDSOCKET];
5597 data->set.ssl.cipher_list = data->set.str[STRING_SSL_CIPHER_LIST];
5599 data->set.ssl.username = data->set.str[STRING_TLSAUTH_USERNAME];
5600 data->set.ssl.password = data->set.str[STRING_TLSAUTH_PASSWORD];
5603 if(!Curl_clone_ssl_config(&data->set.ssl, &conn->ssl_config)) {
5604 result = CURLE_OUT_OF_MEMORY;
5608 prune_dead_connections(data);
5610 /*************************************************************
5611 * Check the current list of connections to see if we can
5612 * re-use an already existing one or if we have to create a
5614 *************************************************************/
5616 /* reuse_fresh is TRUE if we are told to use a new connection by force, but
5617 we only acknowledge this option if this is not a re-used connection
5618 already (which happens due to follow-location or during a HTTP
5619 authentication phase). */
5620 if(data->set.reuse_fresh && !data->state.this_is_a_follow)
5623 reuse = ConnectionExists(data, conn, &conn_temp, &force_reuse);
5625 /* If we found a reusable connection, we may still want to
5626 open a new connection if we are pipelining. */
5627 if(reuse && !force_reuse && IsPipeliningPossible(data, conn_temp)) {
5628 size_t pipelen = conn_temp->send_pipe->size + conn_temp->recv_pipe->size;
5630 infof(data, "Found connection %ld, with requests in the pipe (%zu)\n",
5631 conn_temp->connection_id, pipelen);
5633 if(conn_temp->bundle->num_connections < max_host_connections &&
5634 data->state.conn_cache->num_connections < max_total_connections) {
5635 /* We want a new connection anyway */
5638 infof(data, "We can reuse, but we want a new connection anyway\n");
5645 * We already have a connection for this, we got the former connection
5646 * in the conn_temp variable and thus we need to cleanup the one we
5647 * just allocated before we can move along and use the previously
5650 conn_temp->inuse = TRUE; /* mark this as being in use so that no other
5651 handle in a multi stack may nick it */
5652 reuse_conn(conn, conn_temp);
5653 free(conn); /* we don't need this anymore */
5657 /* set a pointer to the hostname we display */
5658 fix_hostname(data, conn, &conn->host);
5660 infof(data, "Re-using existing connection! (#%ld) with host %s\n",
5661 conn->connection_id,
5662 conn->proxy.name?conn->proxy.dispname:conn->host.dispname);
5665 /* We have decided that we want a new connection. However, we may not
5666 be able to do that if we have reached the limit of how many
5667 connections we are allowed to open. */
5668 struct connectbundle *bundle;
5670 bundle = Curl_conncache_find_bundle(data->state.conn_cache,
5672 if(max_host_connections > 0 && bundle &&
5673 (bundle->num_connections >= max_host_connections)) {
5674 struct connectdata *conn_candidate;
5676 /* The bundle is full. Let's see if we can kill a connection. */
5677 conn_candidate = find_oldest_idle_connection_in_bundle(data, bundle);
5679 if(conn_candidate) {
5680 /* Set the connection's owner correctly, then kill it */
5681 conn_candidate->data = data;
5682 (void)Curl_disconnect(conn_candidate, /* dead_connection */ FALSE);
5685 no_connections_available = TRUE;
5688 if(max_total_connections > 0 &&
5689 (data->state.conn_cache->num_connections >= max_total_connections)) {
5690 struct connectdata *conn_candidate;
5692 /* The cache is full. Let's see if we can kill a connection. */
5693 conn_candidate = find_oldest_idle_connection(data);
5695 if(conn_candidate) {
5696 /* Set the connection's owner correctly, then kill it */
5697 conn_candidate->data = data;
5698 (void)Curl_disconnect(conn_candidate, /* dead_connection */ FALSE);
5701 no_connections_available = TRUE;
5705 if(no_connections_available) {
5706 infof(data, "No connections available.\n");
5711 result = CURLE_NO_CONNECTION_AVAILABLE;
5716 * This is a brand new connection, so let's store it in the connection
5719 ConnectionStore(data, conn);
5722 #if defined(USE_NTLM)
5723 /* If NTLM is requested in a part of this connection, make sure we don't
5724 assume the state is fine as this is a fresh connection and NTLM is
5725 connection based. */
5726 if((data->state.authhost.picked & (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
5727 data->state.authhost.done) {
5728 infof(data, "NTLM picked AND auth done set, clear picked!\n");
5729 data->state.authhost.picked = CURLAUTH_NONE;
5732 if((data->state.authproxy.picked & (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
5733 data->state.authproxy.done) {
5734 infof(data, "NTLM-proxy picked AND auth done set, clear picked!\n");
5735 data->state.authproxy.picked = CURLAUTH_NONE;
5740 /* Mark the connection as used */
5743 /* Setup and init stuff before DO starts, in preparing for the transfer. */
5747 * Setup whatever necessary for a resumed transfer
5749 result = setup_range(data);
5753 /* Continue connectdata initialization here. */
5756 * Inherit the proper values from the urldata struct AFTER we have arranged
5757 * the persistent connection stuff
5759 conn->fread_func = data->set.fread_func;
5760 conn->fread_in = data->set.in;
5761 conn->seek_func = data->set.seek_func;
5762 conn->seek_client = data->set.seek_client;
5764 /*************************************************************
5765 * Resolve the address of the server or proxy
5766 *************************************************************/
5767 result = resolve_server(data, conn, async);
5771 Curl_safefree(options);
5772 Curl_safefree(passwd);
5773 Curl_safefree(user);
5774 Curl_safefree(proxy);
5778 /* Curl_setup_conn() is called after the name resolve initiated in
5779 * create_conn() is all done.
5781 * Curl_setup_conn() also handles reused connections
5783 * conn->data MUST already have been setup fine (in create_conn)
5786 CURLcode Curl_setup_conn(struct connectdata *conn,
5787 bool *protocol_done)
5789 CURLcode result = CURLE_OK;
5790 struct SessionHandle *data = conn->data;
5792 Curl_pgrsTime(data, TIMER_NAMELOOKUP);
5794 if(conn->handler->flags & PROTOPT_NONETWORK) {
5795 /* nothing to setup when not using a network */
5796 *protocol_done = TRUE;
5799 *protocol_done = FALSE; /* default to not done */
5801 /* set proxy_connect_closed to false unconditionally already here since it
5802 is used strictly to provide extra information to a parent function in the
5803 case of proxy CONNECT failures and we must make sure we don't have it
5804 lingering set from a previous invoke */
5805 conn->bits.proxy_connect_closed = FALSE;
5808 * Set user-agent. Used for HTTP, but since we can attempt to tunnel
5809 * basically anything through a http proxy we can't limit this based on
5812 if(data->set.str[STRING_USERAGENT]) {
5813 Curl_safefree(conn->allocptr.uagent);
5814 conn->allocptr.uagent =
5815 aprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
5816 if(!conn->allocptr.uagent)
5817 return CURLE_OUT_OF_MEMORY;
5820 data->req.headerbytecount = 0;
5822 #ifdef CURL_DO_LINEEND_CONV
5823 data->state.crlf_conversions = 0; /* reset CRLF conversion counter */
5824 #endif /* CURL_DO_LINEEND_CONV */
5826 /* set start time here for timeout purposes in the connect procedure, it
5827 is later set again for the progress meter purpose */
5828 conn->now = Curl_tvnow();
5830 if(CURL_SOCKET_BAD == conn->sock[FIRSTSOCKET]) {
5831 conn->bits.tcpconnect[FIRSTSOCKET] = FALSE;
5832 result = Curl_connecthost(conn, conn->dns_entry);
5837 Curl_pgrsTime(data, TIMER_CONNECT); /* we're connected already */
5838 Curl_pgrsTime(data, TIMER_APPCONNECT); /* we're connected already */
5839 conn->bits.tcpconnect[FIRSTSOCKET] = TRUE;
5840 *protocol_done = TRUE;
5841 Curl_updateconninfo(conn, conn->sock[FIRSTSOCKET]);
5842 Curl_verboseconnect(conn);
5845 conn->now = Curl_tvnow(); /* time this *after* the connect is done, we
5846 set this here perhaps a second time */
5850 * This check is quite a hack. We're calling _fsetmode to fix the problem
5851 * with fwrite converting newline characters (you get mangled text files,
5852 * and corrupted binary files when you download to stdout and redirect it to
5856 if((data->set.out)->_handle == NULL) {
5857 _fsetmode(stdout, "b");
5864 CURLcode Curl_connect(struct SessionHandle *data,
5865 struct connectdata **in_connect,
5867 bool *protocol_done)
5871 *asyncp = FALSE; /* assume synchronous resolves by default */
5873 /* call the stuff that needs to be called */
5874 result = create_conn(data, in_connect, asyncp);
5878 if((*in_connect)->send_pipe->size || (*in_connect)->recv_pipe->size)
5880 *protocol_done = TRUE;
5882 /* DNS resolution is done: that's either because this is a reused
5883 connection, in which case DNS was unnecessary, or because DNS
5884 really did finish already (synch resolver/fast async resolve) */
5885 result = Curl_setup_conn(*in_connect, protocol_done);
5889 if(result == CURLE_NO_CONNECTION_AVAILABLE) {
5894 if(result && *in_connect) {
5895 /* We're not allowed to return failure with memory left allocated
5896 in the connectdata struct, free those here */
5897 Curl_disconnect(*in_connect, FALSE); /* close the connection */
5898 *in_connect = NULL; /* return a NULL */
5904 CURLcode Curl_done(struct connectdata **connp,
5905 CURLcode status, /* an error if this is called after an
5906 error was detected */
5910 struct connectdata *conn;
5911 struct SessionHandle *data;
5913 DEBUGASSERT(*connp);
5919 /* Stop if Curl_done() has already been called */
5922 Curl_getoff_all_pipelines(data, conn);
5924 if((conn->send_pipe->size + conn->recv_pipe->size != 0 &&
5925 !data->set.reuse_forbid &&
5927 /* Stop if pipeline is not empty and we do not have to close
5931 conn->bits.done = TRUE; /* called just now! */
5933 /* Cleanup possible redirect junk */
5934 if(data->req.newurl) {
5935 free(data->req.newurl);
5936 data->req.newurl = NULL;
5938 if(data->req.location) {
5939 free(data->req.location);
5940 data->req.location = NULL;
5943 Curl_resolver_cancel(conn);
5945 if(conn->dns_entry) {
5946 Curl_resolv_unlock(data, conn->dns_entry); /* done with this */
5947 conn->dns_entry = NULL;
5951 case CURLE_ABORTED_BY_CALLBACK:
5952 case CURLE_READ_ERROR:
5953 case CURLE_WRITE_ERROR:
5954 /* When we're aborted due to a callback return code it basically have to
5955 be counted as premature as there is trouble ahead if we don't. We have
5956 many callbacks and protocols work differently, we could potentially do
5957 this more fine-grained in the future. */
5963 /* this calls the protocol-specific function pointer previously set */
5964 if(conn->handler->done)
5965 result = conn->handler->done(conn, status, premature);
5969 if(!result && Curl_pgrsDone(conn))
5970 result = CURLE_ABORTED_BY_CALLBACK;
5972 /* if the transfer was completed in a paused state there can be buffered
5973 data left to write and then kill */
5974 if(data->state.tempwrite) {
5975 free(data->state.tempwrite);
5976 data->state.tempwrite = NULL;
5979 /* if data->set.reuse_forbid is TRUE, it means the libcurl client has
5980 forced us to close this connection. This is ignored for requests taking
5981 place in a NTLM authentication handshake
5983 if conn->bits.close is TRUE, it means that the connection should be
5984 closed in spite of all our efforts to be nice, due to protocol
5985 restrictions in our or the server's end
5987 if premature is TRUE, it means this connection was said to be DONE before
5988 the entire request operation is complete and thus we can't know in what
5989 state it is for re-using, so we're forced to close it. In a perfect world
5990 we can add code that keep track of if we really must close it here or not,
5991 but currently we have no such detail knowledge.
5994 if((data->set.reuse_forbid
5995 #if defined(USE_NTLM)
5996 && !(conn->ntlm.state == NTLMSTATE_TYPE2 ||
5997 conn->proxyntlm.state == NTLMSTATE_TYPE2)
5999 ) || conn->bits.close || premature) {
6000 CURLcode res2 = Curl_disconnect(conn, premature); /* close connection */
6002 /* If we had an error already, make sure we return that one. But
6003 if we got a new error, return that. */
6008 /* the connection is no longer in use */
6009 if(ConnectionDone(data, conn)) {
6010 /* remember the most recently used connection */
6011 data->state.lastconnect = conn;
6013 infof(data, "Connection #%ld to host %s left intact\n",
6014 conn->connection_id,
6015 conn->bits.httpproxy?conn->proxy.dispname:conn->host.dispname);
6018 data->state.lastconnect = NULL;
6021 *connp = NULL; /* to make the caller of this function better detect that
6022 this was either closed or handed over to the connection
6023 cache here, and therefore cannot be used from this point on
6025 Curl_free_request_state(data);
6031 * do_init() inits the readwrite session. This is inited each time (in the DO
6032 * function before the protocol-specific DO functions are invoked) for a
6033 * transfer, sometimes multiple times on the same SessionHandle. Make sure
6034 * nothing in here depends on stuff that are setup dynamically for the
6038 static CURLcode do_init(struct connectdata *conn)
6040 struct SessionHandle *data = conn->data;
6041 struct SingleRequest *k = &data->req;
6043 conn->bits.done = FALSE; /* Curl_done() is not called yet */
6044 conn->bits.do_more = FALSE; /* by default there's no curl_do_more() to use */
6045 data->state.expect100header = FALSE;
6047 if(data->set.opt_no_body)
6048 /* in HTTP lingo, no body means using the HEAD request... */
6049 data->set.httpreq = HTTPREQ_HEAD;
6050 else if(HTTPREQ_HEAD == data->set.httpreq)
6051 /* ... but if unset there really is no perfect method that is the
6052 "opposite" of HEAD but in reality most people probably think GET
6053 then. The important thing is that we can't let it remain HEAD if the
6054 opt_no_body is set FALSE since then we'll behave wrong when getting
6056 data->set.httpreq = HTTPREQ_GET;
6058 k->start = Curl_tvnow(); /* start time */
6059 k->now = k->start; /* current time is now */
6060 k->header = TRUE; /* assume header */
6064 k->buf = data->state.buffer;
6065 k->uploadbuf = data->state.uploadbuffer;
6066 k->hbufp = data->state.headerbuff;
6067 k->ignorebody=FALSE;
6069 Curl_speedinit(data);
6071 Curl_pgrsSetUploadCounter(data, 0);
6072 Curl_pgrsSetDownloadCounter(data, 0);
6078 * do_complete is called when the DO actions are complete.
6080 * We init chunking and trailer bits to their default values here immediately
6081 * before receiving any header data for the current request in the pipeline.
6083 static void do_complete(struct connectdata *conn)
6085 conn->data->req.chunk=FALSE;
6086 conn->data->req.maxfd = (conn->sockfd>conn->writesockfd?
6087 conn->sockfd:conn->writesockfd)+1;
6088 Curl_pgrsTime(conn->data, TIMER_PRETRANSFER);
6091 CURLcode Curl_do(struct connectdata **connp, bool *done)
6093 CURLcode result=CURLE_OK;
6094 struct connectdata *conn = *connp;
6095 struct SessionHandle *data = conn->data;
6097 if(conn->handler->do_it) {
6098 /* generic protocol-specific function pointer set in curl_connect() */
6099 result = conn->handler->do_it(conn, done);
6101 /* This was formerly done in transfer.c, but we better do it here */
6102 if((CURLE_SEND_ERROR == result) && conn->bits.reuse) {
6104 * If the connection is using an easy handle, call reconnect
6105 * to re-establish the connection. Otherwise, let the multi logic
6106 * figure out how to re-establish the connection.
6109 result = Curl_reconnect_request(connp);
6112 /* ... finally back to actually retry the DO phase */
6113 conn = *connp; /* re-assign conn since Curl_reconnect_request
6114 creates a new connection */
6115 result = conn->handler->do_it(conn, done);
6122 if(!result && *done)
6123 /* do_complete must be called after the protocol-specific DO function */
6130 * Curl_do_more() is called during the DO_MORE multi state. It is basically a
6131 * second stage DO state which (wrongly) was introduced to support FTP's
6132 * second connection.
6134 * TODO: A future libcurl should be able to work away this state.
6136 * 'complete' can return 0 for incomplete, 1 for done and -1 for go back to
6137 * DOING state there's more work to do!
6140 CURLcode Curl_do_more(struct connectdata *conn, int *complete)
6142 CURLcode result=CURLE_OK;
6146 if(conn->handler->do_more)
6147 result = conn->handler->do_more(conn, complete);
6149 if(!result && (*complete == 1))
6150 /* do_complete must be called after the protocol-specific DO function */