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. */
614 * @param curl is a pointer to a sessionhandle pointer that gets set by this
619 CURLcode Curl_open(struct SessionHandle **curl)
622 struct SessionHandle *data;
624 /* Very simple start-up: alloc the struct, init it with zeroes and return */
625 data = calloc(1, sizeof(struct SessionHandle));
627 /* this is a very serious error */
628 DEBUGF(fprintf(stderr, "Error: calloc of SessionHandle failed\n"));
629 return CURLE_OUT_OF_MEMORY;
632 data->magic = CURLEASY_MAGIC_NUMBER;
634 result = Curl_resolver_init(&data->state.resolver);
636 DEBUGF(fprintf(stderr, "Error: resolver_init failed\n"));
641 /* We do some initial setup here, all those fields that can't be just 0 */
643 data->state.headerbuff = malloc(HEADERSIZE);
644 if(!data->state.headerbuff) {
645 DEBUGF(fprintf(stderr, "Error: malloc of headerbuff failed\n"));
646 result = CURLE_OUT_OF_MEMORY;
649 result = Curl_init_userdefined(&data->set);
651 data->state.headersize=HEADERSIZE;
653 Curl_convert_init(data);
655 /* most recent connection is not yet defined */
656 data->state.lastconnect = NULL;
658 data->progress.flags |= PGRS_HIDE;
659 data->state.current_speed = -1; /* init to negative == impossible */
661 data->wildcard.state = CURLWC_INIT;
662 data->wildcard.filelist = NULL;
663 data->set.fnmatch = ZERO_NULL;
664 data->set.maxconnects = DEFAULT_CONNCACHE_SIZE; /* for easy handles */
668 Curl_resolver_cleanup(data->state.resolver);
669 if(data->state.headerbuff)
670 free(data->state.headerbuff);
681 CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option,
685 CURLcode result = CURLE_OK;
687 #ifndef CURL_DISABLE_HTTP
692 case CURLOPT_DNS_CACHE_TIMEOUT:
693 data->set.dns_cache_timeout = va_arg(param, long);
695 case CURLOPT_DNS_USE_GLOBAL_CACHE:
696 /* remember we want this enabled */
697 arg = va_arg(param, long);
698 data->set.global_dns_cache = (0 != arg)?TRUE:FALSE;
700 case CURLOPT_SSL_CIPHER_LIST:
701 /* set a list of cipher we want to use in the SSL connection */
702 result = setstropt(&data->set.str[STRING_SSL_CIPHER_LIST],
703 va_arg(param, char *));
706 case CURLOPT_RANDOM_FILE:
708 * This is the path name to a file that contains random data to seed
709 * the random SSL stuff with. The file is only used for reading.
711 result = setstropt(&data->set.str[STRING_SSL_RANDOM_FILE],
712 va_arg(param, char *));
714 case CURLOPT_EGDSOCKET:
716 * The Entropy Gathering Daemon socket pathname
718 result = setstropt(&data->set.str[STRING_SSL_EGDSOCKET],
719 va_arg(param, char *));
721 case CURLOPT_MAXCONNECTS:
723 * Set the absolute number of maximum simultaneous alive connection that
724 * libcurl is allowed to have.
726 data->set.maxconnects = va_arg(param, long);
728 case CURLOPT_FORBID_REUSE:
730 * When this transfer is done, it must not be left to be reused by a
731 * subsequent transfer but shall be closed immediately.
733 data->set.reuse_forbid = (0 != va_arg(param, long))?TRUE:FALSE;
735 case CURLOPT_FRESH_CONNECT:
737 * This transfer shall not use a previously cached connection but
738 * should be made with a fresh new connect!
740 data->set.reuse_fresh = (0 != va_arg(param, long))?TRUE:FALSE;
742 case CURLOPT_VERBOSE:
744 * Verbose means infof() calls that give a lot of information about
745 * the connection and transfer procedures as well as internal choices.
747 data->set.verbose = (0 != va_arg(param, long))?TRUE:FALSE;
751 * Set to include the header in the general data output stream.
753 data->set.include_header = (0 != va_arg(param, long))?TRUE:FALSE;
755 case CURLOPT_NOPROGRESS:
757 * Shut off the internal supported progress meter
759 data->set.hide_progress = (0 != va_arg(param, long))?TRUE:FALSE;
760 if(data->set.hide_progress)
761 data->progress.flags |= PGRS_HIDE;
763 data->progress.flags &= ~PGRS_HIDE;
767 * Do not include the body part in the output data stream.
769 data->set.opt_no_body = (0 != va_arg(param, long))?TRUE:FALSE;
771 case CURLOPT_FAILONERROR:
773 * Don't output the >=400 error code HTML-page, but instead only
776 data->set.http_fail_on_error = (0 != va_arg(param, long))?TRUE:FALSE;
781 * We want to sent data to the remote host. If this is HTTP, that equals
782 * using the PUT request.
784 data->set.upload = (0 != va_arg(param, long))?TRUE:FALSE;
785 if(data->set.upload) {
786 /* If this is HTTP, PUT is what's needed to "upload" */
787 data->set.httpreq = HTTPREQ_PUT;
788 data->set.opt_no_body = FALSE; /* this is implied */
791 /* In HTTP, the opposite of upload is GET (unless NOBODY is true as
792 then this can be changed to HEAD later on) */
793 data->set.httpreq = HTTPREQ_GET;
795 case CURLOPT_FILETIME:
797 * Try to get the file time of the remote document. The time will
798 * later (possibly) become available using curl_easy_getinfo().
800 data->set.get_filetime = (0 != va_arg(param, long))?TRUE:FALSE;
802 case CURLOPT_FTP_CREATE_MISSING_DIRS:
804 * An FTP option that modifies an upload to create missing directories on
807 switch(va_arg(param, long)) {
809 data->set.ftp_create_missing_dirs = 0;
812 data->set.ftp_create_missing_dirs = 1;
815 data->set.ftp_create_missing_dirs = 2;
818 /* reserve other values for future use */
819 result = CURLE_UNKNOWN_OPTION;
823 case CURLOPT_SERVER_RESPONSE_TIMEOUT:
825 * Option that specifies how quickly an server response must be obtained
826 * before it is considered failure. For pingpong protocols.
828 data->set.server_response_timeout = va_arg( param , long ) * 1000;
830 case CURLOPT_TFTP_BLKSIZE:
832 * TFTP option that specifies the block size to use for data transmission
834 data->set.tftp_blksize = va_arg(param, long);
836 case CURLOPT_DIRLISTONLY:
838 * An option that changes the command to one that asks for a list
839 * only, no file info details.
841 data->set.ftp_list_only = (0 != va_arg(param, long))?TRUE:FALSE;
845 * We want to upload and append to an existing file.
847 data->set.ftp_append = (0 != va_arg(param, long))?TRUE:FALSE;
849 case CURLOPT_FTP_FILEMETHOD:
851 * How do access files over FTP.
853 data->set.ftp_filemethod = (curl_ftpfile)va_arg(param, long);
857 * Parse the $HOME/.netrc file
859 data->set.use_netrc = (enum CURL_NETRC_OPTION)va_arg(param, long);
861 case CURLOPT_NETRC_FILE:
863 * Use this file instead of the $HOME/.netrc file
865 result = setstropt(&data->set.str[STRING_NETRC_FILE],
866 va_arg(param, char *));
868 case CURLOPT_TRANSFERTEXT:
870 * This option was previously named 'FTPASCII'. Renamed to work with
871 * more protocols than merely FTP.
873 * Transfer using ASCII (instead of BINARY).
875 data->set.prefer_ascii = (0 != va_arg(param, long))?TRUE:FALSE;
877 case CURLOPT_TIMECONDITION:
879 * Set HTTP time condition. This must be one of the defines in the
880 * curl/curl.h header file.
882 data->set.timecondition = (curl_TimeCond)va_arg(param, long);
884 case CURLOPT_TIMEVALUE:
886 * This is the value to compare with the remote document with the
887 * method set with CURLOPT_TIMECONDITION
889 data->set.timevalue = (time_t)va_arg(param, long);
891 case CURLOPT_SSLVERSION:
893 * Set explicit SSL version to try to connect with, as some SSL
894 * implementations are lame.
896 data->set.ssl.version = va_arg(param, long);
899 #ifndef CURL_DISABLE_HTTP
900 case CURLOPT_AUTOREFERER:
902 * Switch on automatic referer that gets set if curl follows locations.
904 data->set.http_auto_referer = (0 != va_arg(param, long))?TRUE:FALSE;
907 case CURLOPT_ACCEPT_ENCODING:
909 * String to use at the value of Accept-Encoding header.
911 * If the encoding is set to "" we use an Accept-Encoding header that
912 * encompasses all the encodings we support.
913 * If the encoding is set to NULL we don't send an Accept-Encoding header
914 * and ignore an received Content-Encoding header.
917 argptr = va_arg(param, char *);
918 result = setstropt(&data->set.str[STRING_ENCODING],
919 (argptr && !*argptr)?
920 (char *) ALL_CONTENT_ENCODINGS: argptr);
923 case CURLOPT_TRANSFER_ENCODING:
924 data->set.http_transfer_encoding = (0 != va_arg(param, long))?TRUE:FALSE;
927 case CURLOPT_FOLLOWLOCATION:
929 * Follow Location: header hints on a HTTP-server.
931 data->set.http_follow_location = (0 != va_arg(param, long))?TRUE:FALSE;
934 case CURLOPT_UNRESTRICTED_AUTH:
936 * Send authentication (user+password) when following locations, even when
939 data->set.http_disable_hostname_check_before_authentication =
940 (0 != va_arg(param, long))?TRUE:FALSE;
943 case CURLOPT_MAXREDIRS:
945 * The maximum amount of hops you allow curl to follow Location:
946 * headers. This should mostly be used to detect never-ending loops.
948 data->set.maxredirs = va_arg(param, long);
951 case CURLOPT_POSTREDIR:
954 * Set the behaviour of POST when redirecting
955 * CURL_REDIR_GET_ALL - POST is changed to GET after 301 and 302
956 * CURL_REDIR_POST_301 - POST is kept as POST after 301
957 * CURL_REDIR_POST_302 - POST is kept as POST after 302
958 * CURL_REDIR_POST_303 - POST is kept as POST after 303
959 * CURL_REDIR_POST_ALL - POST is kept as POST after 301, 302 and 303
960 * other - POST is kept as POST after 301 and 302
962 int postRedir = curlx_sltosi(va_arg(param, long));
963 data->set.keep_post = postRedir & CURL_REDIR_POST_ALL;
968 /* Does this option serve a purpose anymore? Yes it does, when
969 CURLOPT_POSTFIELDS isn't used and the POST data is read off the
971 if(va_arg(param, long)) {
972 data->set.httpreq = HTTPREQ_POST;
973 data->set.opt_no_body = FALSE; /* this is implied */
976 data->set.httpreq = HTTPREQ_GET;
979 case CURLOPT_COPYPOSTFIELDS:
981 * A string with POST data. Makes curl HTTP POST. Even if it is NULL.
982 * If needed, CURLOPT_POSTFIELDSIZE must have been set prior to
983 * CURLOPT_COPYPOSTFIELDS and not altered later.
985 argptr = va_arg(param, char *);
987 if(!argptr || data->set.postfieldsize == -1)
988 result = setstropt(&data->set.str[STRING_COPYPOSTFIELDS], argptr);
991 * Check that requested length does not overflow the size_t type.
994 if((data->set.postfieldsize < 0) ||
995 ((sizeof(curl_off_t) != sizeof(size_t)) &&
996 (data->set.postfieldsize > (curl_off_t)((size_t)-1))))
997 result = CURLE_OUT_OF_MEMORY;
1001 (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
1003 /* Allocate even when size == 0. This satisfies the need of possible
1004 later address compare to detect the COPYPOSTFIELDS mode, and
1005 to mark that postfields is used rather than read function or
1008 p = malloc((size_t)(data->set.postfieldsize?
1009 data->set.postfieldsize:1));
1012 result = CURLE_OUT_OF_MEMORY;
1014 if(data->set.postfieldsize)
1015 memcpy(p, argptr, (size_t)data->set.postfieldsize);
1017 data->set.str[STRING_COPYPOSTFIELDS] = p;
1022 data->set.postfields = data->set.str[STRING_COPYPOSTFIELDS];
1023 data->set.httpreq = HTTPREQ_POST;
1026 case CURLOPT_POSTFIELDS:
1028 * Like above, but use static data instead of copying it.
1030 data->set.postfields = va_arg(param, void *);
1031 /* Release old copied data. */
1032 (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
1033 data->set.httpreq = HTTPREQ_POST;
1036 case CURLOPT_POSTFIELDSIZE:
1038 * The size of the POSTFIELD data to prevent libcurl to do strlen() to
1039 * figure it out. Enables binary posts.
1041 bigsize = va_arg(param, long);
1043 if(data->set.postfieldsize < bigsize &&
1044 data->set.postfields == data->set.str[STRING_COPYPOSTFIELDS]) {
1045 /* Previous CURLOPT_COPYPOSTFIELDS is no longer valid. */
1046 (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
1047 data->set.postfields = NULL;
1050 data->set.postfieldsize = bigsize;
1053 case CURLOPT_POSTFIELDSIZE_LARGE:
1055 * The size of the POSTFIELD data to prevent libcurl to do strlen() to
1056 * figure it out. Enables binary posts.
1058 bigsize = va_arg(param, curl_off_t);
1060 if(data->set.postfieldsize < bigsize &&
1061 data->set.postfields == data->set.str[STRING_COPYPOSTFIELDS]) {
1062 /* Previous CURLOPT_COPYPOSTFIELDS is no longer valid. */
1063 (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
1064 data->set.postfields = NULL;
1067 data->set.postfieldsize = bigsize;
1070 case CURLOPT_HTTPPOST:
1072 * Set to make us do HTTP POST
1074 data->set.httppost = va_arg(param, struct curl_httppost *);
1075 data->set.httpreq = HTTPREQ_POST_FORM;
1076 data->set.opt_no_body = FALSE; /* this is implied */
1079 case CURLOPT_REFERER:
1081 * String to set in the HTTP Referer: field.
1083 if(data->change.referer_alloc) {
1084 Curl_safefree(data->change.referer);
1085 data->change.referer_alloc = FALSE;
1087 result = setstropt(&data->set.str[STRING_SET_REFERER],
1088 va_arg(param, char *));
1089 data->change.referer = data->set.str[STRING_SET_REFERER];
1092 case CURLOPT_USERAGENT:
1094 * String to use in the HTTP User-Agent field
1096 result = setstropt(&data->set.str[STRING_USERAGENT],
1097 va_arg(param, char *));
1100 case CURLOPT_HTTPHEADER:
1102 * Set a list with HTTP headers to use (or replace internals with)
1104 data->set.headers = va_arg(param, struct curl_slist *);
1107 case CURLOPT_PROXYHEADER:
1109 * Set a list with proxy headers to use (or replace internals with)
1111 * Since CURLOPT_HTTPHEADER was the only way to set HTTP headers for a
1112 * long time we remain doing it this way until CURLOPT_PROXYHEADER is
1113 * used. As soon as this option has been used, if set to anything but
1114 * NULL, custom headers for proxies are only picked from this list.
1116 * Set this option to NULL to restore the previous behavior.
1118 data->set.proxyheaders = va_arg(param, struct curl_slist *);
1121 case CURLOPT_HEADEROPT:
1123 * Set header option.
1125 arg = va_arg(param, long);
1126 data->set.sep_headers = (arg & CURLHEADER_SEPARATE)? TRUE: FALSE;
1129 case CURLOPT_HTTP200ALIASES:
1131 * Set a list of aliases for HTTP 200 in response header
1133 data->set.http200aliases = va_arg(param, struct curl_slist *);
1136 #if !defined(CURL_DISABLE_COOKIES)
1137 case CURLOPT_COOKIE:
1139 * Cookie string to send to the remote server in the request.
1141 result = setstropt(&data->set.str[STRING_COOKIE],
1142 va_arg(param, char *));
1145 case CURLOPT_COOKIEFILE:
1147 * Set cookie file to read and parse. Can be used multiple times.
1149 argptr = (char *)va_arg(param, void *);
1151 struct curl_slist *cl;
1152 /* append the cookie file name to the list of file names, and deal with
1154 cl = curl_slist_append(data->change.cookielist, argptr);
1156 curl_slist_free_all(data->change.cookielist);
1157 data->change.cookielist = NULL;
1158 return CURLE_OUT_OF_MEMORY;
1160 data->change.cookielist = cl; /* store the list for later use */
1164 case CURLOPT_COOKIEJAR:
1166 * Set cookie file name to dump all cookies to when we're done.
1169 struct CookieInfo *newcookies;
1170 result = setstropt(&data->set.str[STRING_COOKIEJAR],
1171 va_arg(param, char *));
1174 * Activate the cookie parser. This may or may not already
1177 newcookies = Curl_cookie_init(data, NULL, data->cookies,
1178 data->set.cookiesession);
1180 result = CURLE_OUT_OF_MEMORY;
1181 data->cookies = newcookies;
1185 case CURLOPT_COOKIESESSION:
1187 * Set this option to TRUE to start a new "cookie session". It will
1188 * prevent the forthcoming read-cookies-from-file actions to accept
1189 * cookies that are marked as being session cookies, as they belong to a
1192 * In the original Netscape cookie spec, "session cookies" are cookies
1193 * with no expire date set. RFC2109 describes the same action if no
1194 * 'Max-Age' is set and RFC2965 includes the RFC2109 description and adds
1195 * a 'Discard' action that can enforce the discard even for cookies that
1198 * We run mostly with the original cookie spec, as hardly anyone implements
1201 data->set.cookiesession = (0 != va_arg(param, long))?TRUE:FALSE;
1204 case CURLOPT_COOKIELIST:
1205 argptr = va_arg(param, char *);
1210 if(Curl_raw_equal(argptr, "ALL")) {
1211 /* clear all cookies */
1212 Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
1213 Curl_cookie_clearall(data->cookies);
1214 Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
1216 else if(Curl_raw_equal(argptr, "SESS")) {
1217 /* clear session cookies */
1218 Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
1219 Curl_cookie_clearsess(data->cookies);
1220 Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
1222 else if(Curl_raw_equal(argptr, "FLUSH")) {
1223 /* flush cookies to file, takes care of the locking */
1224 Curl_flush_cookies(data, 0);
1226 else if(Curl_raw_equal(argptr, "RELOAD")) {
1227 /* reload cookies from file */
1228 Curl_cookie_loadfiles(data);
1233 /* if cookie engine was not running, activate it */
1234 data->cookies = Curl_cookie_init(data, NULL, NULL, TRUE);
1236 argptr = strdup(argptr);
1237 if(!argptr || !data->cookies) {
1238 result = CURLE_OUT_OF_MEMORY;
1239 Curl_safefree(argptr);
1242 Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
1244 if(checkprefix("Set-Cookie:", argptr))
1245 /* HTTP Header format line */
1246 Curl_cookie_add(data, data->cookies, TRUE, argptr + 11, NULL, NULL);
1249 /* Netscape format line */
1250 Curl_cookie_add(data, data->cookies, FALSE, argptr, NULL, NULL);
1252 Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
1258 #endif /* CURL_DISABLE_COOKIES */
1260 case CURLOPT_HTTPGET:
1262 * Set to force us do HTTP GET
1264 if(va_arg(param, long)) {
1265 data->set.httpreq = HTTPREQ_GET;
1266 data->set.upload = FALSE; /* switch off upload */
1267 data->set.opt_no_body = FALSE; /* this is implied */
1271 case CURLOPT_HTTP_VERSION:
1273 * This sets a requested HTTP version to be used. The value is one of
1274 * the listed enums in curl/curl.h.
1276 arg = va_arg(param, long);
1278 if(arg == CURL_HTTP_VERSION_2_0)
1279 return CURLE_UNSUPPORTED_PROTOCOL;
1281 data->set.httpversion = arg;
1284 case CURLOPT_HTTPAUTH:
1286 * Set HTTP Authentication type BITMASK.
1291 unsigned long auth = va_arg(param, unsigned long);
1293 if(auth == CURLAUTH_NONE) {
1294 data->set.httpauth = auth;
1298 /* the DIGEST_IE bit is only used to set a special marker, for all the
1299 rest we need to handle it as normal DIGEST */
1300 data->state.authhost.iestyle = (auth & CURLAUTH_DIGEST_IE)?TRUE:FALSE;
1302 if(auth & CURLAUTH_DIGEST_IE) {
1303 auth |= CURLAUTH_DIGEST; /* set standard digest bit */
1304 auth &= ~CURLAUTH_DIGEST_IE; /* unset ie digest bit */
1307 /* switch off bits we can't support */
1309 auth &= ~CURLAUTH_NTLM; /* no NTLM support */
1310 auth &= ~CURLAUTH_NTLM_WB; /* no NTLM_WB support */
1311 #elif !defined(NTLM_WB_ENABLED)
1312 auth &= ~CURLAUTH_NTLM_WB; /* no NTLM_WB support */
1315 auth &= ~CURLAUTH_NEGOTIATE; /* no Negotiate (SPNEGO) auth without
1319 /* check if any auth bit lower than CURLAUTH_ONLY is still set */
1322 while(bitcheck < 31) {
1323 if(auth & (1UL << bitcheck++)) {
1329 return CURLE_NOT_BUILT_IN; /* no supported types left! */
1331 data->set.httpauth = auth;
1335 case CURLOPT_EXPECT_100_TIMEOUT_MS:
1337 * Time to wait for a response to a HTTP request containing an
1338 * Expect: 100-continue header before sending the data anyway.
1340 data->set.expect_100_timeout = va_arg(param, long);
1343 #endif /* CURL_DISABLE_HTTP */
1345 case CURLOPT_CUSTOMREQUEST:
1347 * Set a custom string to use as request
1349 result = setstropt(&data->set.str[STRING_CUSTOMREQUEST],
1350 va_arg(param, char *));
1353 data->set.httpreq = HTTPREQ_CUSTOM;
1354 here, we continue as if we were using the already set type
1355 and this just changes the actual request keyword */
1358 #ifndef CURL_DISABLE_PROXY
1359 case CURLOPT_HTTPPROXYTUNNEL:
1361 * Tunnel operations through the proxy instead of normal proxy use
1363 data->set.tunnel_thru_httpproxy = (0 != va_arg(param, long))?TRUE:FALSE;
1366 case CURLOPT_PROXYPORT:
1368 * Explicitly set HTTP proxy port number.
1370 data->set.proxyport = va_arg(param, long);
1373 case CURLOPT_PROXYAUTH:
1375 * Set HTTP Authentication type BITMASK.
1380 unsigned long auth = va_arg(param, unsigned long);
1382 if(auth == CURLAUTH_NONE) {
1383 data->set.proxyauth = auth;
1387 /* the DIGEST_IE bit is only used to set a special marker, for all the
1388 rest we need to handle it as normal DIGEST */
1389 data->state.authproxy.iestyle = (auth & CURLAUTH_DIGEST_IE)?TRUE:FALSE;
1391 if(auth & CURLAUTH_DIGEST_IE) {
1392 auth |= CURLAUTH_DIGEST; /* set standard digest bit */
1393 auth &= ~CURLAUTH_DIGEST_IE; /* unset ie digest bit */
1395 /* switch off bits we can't support */
1397 auth &= ~CURLAUTH_NTLM; /* no NTLM support */
1398 auth &= ~CURLAUTH_NTLM_WB; /* no NTLM_WB support */
1399 #elif !defined(NTLM_WB_ENABLED)
1400 auth &= ~CURLAUTH_NTLM_WB; /* no NTLM_WB support */
1403 auth &= ~CURLAUTH_NEGOTIATE; /* no Negotiate (SPNEGO) auth without
1407 /* check if any auth bit lower than CURLAUTH_ONLY is still set */
1410 while(bitcheck < 31) {
1411 if(auth & (1UL << bitcheck++)) {
1417 return CURLE_NOT_BUILT_IN; /* no supported types left! */
1419 data->set.proxyauth = auth;
1425 * Set proxy server:port to use as HTTP proxy.
1427 * If the proxy is set to "" we explicitly say that we don't want to use a
1428 * proxy (even though there might be environment variables saying so).
1430 * Setting it to NULL, means no proxy but allows the environment variables
1433 result = setstropt(&data->set.str[STRING_PROXY],
1434 va_arg(param, char *));
1437 case CURLOPT_PROXYTYPE:
1439 * Set proxy type. HTTP/HTTP_1_0/SOCKS4/SOCKS4a/SOCKS5/SOCKS5_HOSTNAME
1441 data->set.proxytype = (curl_proxytype)va_arg(param, long);
1444 case CURLOPT_PROXY_TRANSFER_MODE:
1446 * set transfer mode (;type=<a|i>) when doing FTP via an HTTP proxy
1448 switch (va_arg(param, long)) {
1450 data->set.proxy_transfer_mode = FALSE;
1453 data->set.proxy_transfer_mode = TRUE;
1456 /* reserve other values for future use */
1457 result = CURLE_UNKNOWN_OPTION;
1461 #endif /* CURL_DISABLE_PROXY */
1463 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1464 case CURLOPT_SOCKS5_GSSAPI_SERVICE:
1466 * Set GSS-API service name
1468 result = setstropt(&data->set.str[STRING_SOCKS5_GSSAPI_SERVICE],
1469 va_arg(param, char *));
1472 case CURLOPT_SOCKS5_GSSAPI_NEC:
1474 * set flag for nec socks5 support
1476 data->set.socks5_gssapi_nec = (0 != va_arg(param, long))?TRUE:FALSE;
1480 case CURLOPT_HEADERDATA:
1482 * Custom pointer to pass the header write callback function
1484 data->set.writeheader = (void *)va_arg(param, void *);
1486 case CURLOPT_ERRORBUFFER:
1488 * Error buffer provided by the caller to get the human readable
1491 data->set.errorbuffer = va_arg(param, char *);
1493 case CURLOPT_WRITEDATA:
1495 * FILE pointer to write to. Or possibly
1496 * used as argument to the write callback.
1498 data->set.out = va_arg(param, void *);
1500 case CURLOPT_FTPPORT:
1502 * Use FTP PORT, this also specifies which IP address to use
1504 result = setstropt(&data->set.str[STRING_FTPPORT],
1505 va_arg(param, char *));
1506 data->set.ftp_use_port = (NULL != data->set.str[STRING_FTPPORT]) ?
1510 case CURLOPT_FTP_USE_EPRT:
1511 data->set.ftp_use_eprt = (0 != va_arg(param, long))?TRUE:FALSE;
1514 case CURLOPT_FTP_USE_EPSV:
1515 data->set.ftp_use_epsv = (0 != va_arg(param, long))?TRUE:FALSE;
1518 case CURLOPT_FTP_USE_PRET:
1519 data->set.ftp_use_pret = (0 != va_arg(param, long))?TRUE:FALSE;
1522 case CURLOPT_FTP_SSL_CCC:
1523 data->set.ftp_ccc = (curl_ftpccc)va_arg(param, long);
1526 case CURLOPT_FTP_SKIP_PASV_IP:
1528 * Enable or disable FTP_SKIP_PASV_IP, which will disable/enable the
1529 * bypass of the IP address in PASV responses.
1531 data->set.ftp_skip_ip = (0 != va_arg(param, long))?TRUE:FALSE;
1534 case CURLOPT_READDATA:
1536 * FILE pointer to read the file to be uploaded from. Or possibly
1537 * used as argument to the read callback.
1539 data->set.in = va_arg(param, void *);
1541 case CURLOPT_INFILESIZE:
1543 * If known, this should inform curl about the file size of the
1544 * to-be-uploaded file.
1546 data->set.filesize = va_arg(param, long);
1548 case CURLOPT_INFILESIZE_LARGE:
1550 * If known, this should inform curl about the file size of the
1551 * to-be-uploaded file.
1553 data->set.filesize = va_arg(param, curl_off_t);
1555 case CURLOPT_LOW_SPEED_LIMIT:
1557 * The low speed limit that if transfers are below this for
1558 * CURLOPT_LOW_SPEED_TIME, the transfer is aborted.
1560 data->set.low_speed_limit=va_arg(param, long);
1562 case CURLOPT_MAX_SEND_SPEED_LARGE:
1564 * When transfer uploads are faster then CURLOPT_MAX_SEND_SPEED_LARGE
1565 * bytes per second the transfer is throttled..
1567 data->set.max_send_speed=va_arg(param, curl_off_t);
1569 case CURLOPT_MAX_RECV_SPEED_LARGE:
1571 * When receiving data faster than CURLOPT_MAX_RECV_SPEED_LARGE bytes per
1572 * second the transfer is throttled..
1574 data->set.max_recv_speed=va_arg(param, curl_off_t);
1576 case CURLOPT_LOW_SPEED_TIME:
1578 * The low speed time that if transfers are below the set
1579 * CURLOPT_LOW_SPEED_LIMIT during this time, the transfer is aborted.
1581 data->set.low_speed_time=va_arg(param, long);
1587 if(data->change.url_alloc) {
1588 /* the already set URL is allocated, free it first! */
1589 Curl_safefree(data->change.url);
1590 data->change.url_alloc = FALSE;
1592 result = setstropt(&data->set.str[STRING_SET_URL],
1593 va_arg(param, char *));
1594 data->change.url = data->set.str[STRING_SET_URL];
1598 * The port number to use when getting the URL
1600 data->set.use_port = va_arg(param, long);
1602 case CURLOPT_TIMEOUT:
1604 * The maximum time you allow curl to use for a single transfer
1607 data->set.timeout = va_arg(param, long) * 1000L;
1610 case CURLOPT_TIMEOUT_MS:
1611 data->set.timeout = va_arg(param, long);
1614 case CURLOPT_CONNECTTIMEOUT:
1616 * The maximum time you allow curl to use to connect.
1618 data->set.connecttimeout = va_arg(param, long) * 1000L;
1621 case CURLOPT_CONNECTTIMEOUT_MS:
1622 data->set.connecttimeout = va_arg(param, long);
1625 case CURLOPT_ACCEPTTIMEOUT_MS:
1627 * The maximum time you allow curl to wait for server connect
1629 data->set.accepttimeout = va_arg(param, long);
1632 case CURLOPT_USERPWD:
1634 * user:password to use in the operation
1636 result = setstropt_userpwd(va_arg(param, char *),
1637 &data->set.str[STRING_USERNAME],
1638 &data->set.str[STRING_PASSWORD]);
1641 case CURLOPT_USERNAME:
1643 * authentication user name to use in the operation
1645 result = setstropt(&data->set.str[STRING_USERNAME],
1646 va_arg(param, char *));
1649 case CURLOPT_PASSWORD:
1651 * authentication password to use in the operation
1653 result = setstropt(&data->set.str[STRING_PASSWORD],
1654 va_arg(param, char *));
1657 case CURLOPT_LOGIN_OPTIONS:
1659 * authentication options to use in the operation
1661 result = setstropt(&data->set.str[STRING_OPTIONS],
1662 va_arg(param, char *));
1665 case CURLOPT_XOAUTH2_BEARER:
1667 * XOAUTH2 bearer token to use in the operation
1669 result = setstropt(&data->set.str[STRING_BEARER],
1670 va_arg(param, char *));
1673 case CURLOPT_POSTQUOTE:
1675 * List of RAW FTP commands to use after a transfer
1677 data->set.postquote = va_arg(param, struct curl_slist *);
1679 case CURLOPT_PREQUOTE:
1681 * List of RAW FTP commands to use prior to RETR (Wesley Laxton)
1683 data->set.prequote = va_arg(param, struct curl_slist *);
1687 * List of RAW FTP commands to use before a transfer
1689 data->set.quote = va_arg(param, struct curl_slist *);
1691 case CURLOPT_RESOLVE:
1693 * List of NAME:[address] names to populate the DNS cache with
1694 * Prefix the NAME with dash (-) to _remove_ the name from the cache.
1696 * Names added with this API will remain in the cache until explicitly
1697 * removed or the handle is cleaned up.
1699 * This API can remove any name from the DNS cache, but only entries
1700 * that aren't actually in use right now will be pruned immediately.
1702 data->set.resolve = va_arg(param, struct curl_slist *);
1703 data->change.resolve = data->set.resolve;
1705 case CURLOPT_PROGRESSFUNCTION:
1707 * Progress callback function
1709 data->set.fprogress = va_arg(param, curl_progress_callback);
1710 if(data->set.fprogress)
1711 data->progress.callback = TRUE; /* no longer internal */
1713 data->progress.callback = FALSE; /* NULL enforces internal */
1716 case CURLOPT_XFERINFOFUNCTION:
1718 * Transfer info callback function
1720 data->set.fxferinfo = va_arg(param, curl_xferinfo_callback);
1721 if(data->set.fxferinfo)
1722 data->progress.callback = TRUE; /* no longer internal */
1724 data->progress.callback = FALSE; /* NULL enforces internal */
1728 case CURLOPT_PROGRESSDATA:
1730 * Custom client data to pass to the progress callback
1732 data->set.progress_client = va_arg(param, void *);
1735 #ifndef CURL_DISABLE_PROXY
1736 case CURLOPT_PROXYUSERPWD:
1738 * user:password needed to use the proxy
1740 result = setstropt_userpwd(va_arg(param, char *),
1741 &data->set.str[STRING_PROXYUSERNAME],
1742 &data->set.str[STRING_PROXYPASSWORD]);
1744 case CURLOPT_PROXYUSERNAME:
1746 * authentication user name to use in the operation
1748 result = setstropt(&data->set.str[STRING_PROXYUSERNAME],
1749 va_arg(param, char *));
1751 case CURLOPT_PROXYPASSWORD:
1753 * authentication password to use in the operation
1755 result = setstropt(&data->set.str[STRING_PROXYPASSWORD],
1756 va_arg(param, char *));
1758 case CURLOPT_NOPROXY:
1760 * proxy exception list
1762 result = setstropt(&data->set.str[STRING_NOPROXY],
1763 va_arg(param, char *));
1769 * What range of the file you want to transfer
1771 result = setstropt(&data->set.str[STRING_SET_RANGE],
1772 va_arg(param, char *));
1774 case CURLOPT_RESUME_FROM:
1776 * Resume transfer at the give file position
1778 data->set.set_resume_from = va_arg(param, long);
1780 case CURLOPT_RESUME_FROM_LARGE:
1782 * Resume transfer at the give file position
1784 data->set.set_resume_from = va_arg(param, curl_off_t);
1786 case CURLOPT_DEBUGFUNCTION:
1788 * stderr write callback.
1790 data->set.fdebug = va_arg(param, curl_debug_callback);
1792 * if the callback provided is NULL, it'll use the default callback
1795 case CURLOPT_DEBUGDATA:
1797 * Set to a void * that should receive all error writes. This
1798 * defaults to CURLOPT_STDERR for normal operations.
1800 data->set.debugdata = va_arg(param, void *);
1802 case CURLOPT_STDERR:
1804 * Set to a FILE * that should receive all error writes. This
1805 * defaults to stderr for normal operations.
1807 data->set.err = va_arg(param, FILE *);
1809 data->set.err = stderr;
1811 case CURLOPT_HEADERFUNCTION:
1813 * Set header write callback
1815 data->set.fwrite_header = va_arg(param, curl_write_callback);
1817 case CURLOPT_WRITEFUNCTION:
1819 * Set data write callback
1821 data->set.fwrite_func = va_arg(param, curl_write_callback);
1822 if(!data->set.fwrite_func) {
1823 data->set.is_fwrite_set = 0;
1824 /* When set to NULL, reset to our internal default function */
1825 data->set.fwrite_func = (curl_write_callback)fwrite;
1828 data->set.is_fwrite_set = 1;
1830 case CURLOPT_READFUNCTION:
1832 * Read data callback
1834 data->set.fread_func = va_arg(param, curl_read_callback);
1835 if(!data->set.fread_func) {
1836 data->set.is_fread_set = 0;
1837 /* When set to NULL, reset to our internal default function */
1838 data->set.fread_func = (curl_read_callback)fread;
1841 data->set.is_fread_set = 1;
1843 case CURLOPT_SEEKFUNCTION:
1845 * Seek callback. Might be NULL.
1847 data->set.seek_func = va_arg(param, curl_seek_callback);
1849 case CURLOPT_SEEKDATA:
1851 * Seek control callback. Might be NULL.
1853 data->set.seek_client = va_arg(param, void *);
1855 case CURLOPT_CONV_FROM_NETWORK_FUNCTION:
1857 * "Convert from network encoding" callback
1859 data->set.convfromnetwork = va_arg(param, curl_conv_callback);
1861 case CURLOPT_CONV_TO_NETWORK_FUNCTION:
1863 * "Convert to network encoding" callback
1865 data->set.convtonetwork = va_arg(param, curl_conv_callback);
1867 case CURLOPT_CONV_FROM_UTF8_FUNCTION:
1869 * "Convert from UTF-8 encoding" callback
1871 data->set.convfromutf8 = va_arg(param, curl_conv_callback);
1873 case CURLOPT_IOCTLFUNCTION:
1875 * I/O control callback. Might be NULL.
1877 data->set.ioctl_func = va_arg(param, curl_ioctl_callback);
1879 case CURLOPT_IOCTLDATA:
1881 * I/O control data pointer. Might be NULL.
1883 data->set.ioctl_client = va_arg(param, void *);
1885 case CURLOPT_SSLCERT:
1887 * String that holds file name of the SSL certificate to use
1889 result = setstropt(&data->set.str[STRING_CERT],
1890 va_arg(param, char *));
1892 case CURLOPT_SSLCERTTYPE:
1894 * String that holds file type of the SSL certificate to use
1896 result = setstropt(&data->set.str[STRING_CERT_TYPE],
1897 va_arg(param, char *));
1899 case CURLOPT_SSLKEY:
1901 * String that holds file name of the SSL key to use
1903 result = setstropt(&data->set.str[STRING_KEY],
1904 va_arg(param, char *));
1906 case CURLOPT_SSLKEYTYPE:
1908 * String that holds file type of the SSL key to use
1910 result = setstropt(&data->set.str[STRING_KEY_TYPE],
1911 va_arg(param, char *));
1913 case CURLOPT_KEYPASSWD:
1915 * String that holds the SSL or SSH private key password.
1917 result = setstropt(&data->set.str[STRING_KEY_PASSWD],
1918 va_arg(param, char *));
1920 case CURLOPT_SSLENGINE:
1922 * String that holds the SSL crypto engine.
1924 argptr = va_arg(param, char *);
1925 if(argptr && argptr[0])
1926 result = Curl_ssl_set_engine(data, argptr);
1929 case CURLOPT_SSLENGINE_DEFAULT:
1931 * flag to set engine as default.
1933 result = Curl_ssl_set_engine_default(data);
1937 * Kludgy option to enable CRLF conversions. Subject for removal.
1939 data->set.crlf = (0 != va_arg(param, long))?TRUE:FALSE;
1942 case CURLOPT_INTERFACE:
1944 * Set what interface or address/hostname to bind the socket to when
1945 * performing an operation and thus what from-IP your connection will use.
1947 result = setstropt(&data->set.str[STRING_DEVICE],
1948 va_arg(param, char *));
1950 case CURLOPT_LOCALPORT:
1952 * Set what local port to bind the socket to when performing an operation.
1954 data->set.localport = curlx_sltous(va_arg(param, long));
1956 case CURLOPT_LOCALPORTRANGE:
1958 * Set number of local ports to try, starting with CURLOPT_LOCALPORT.
1960 data->set.localportrange = curlx_sltosi(va_arg(param, long));
1962 case CURLOPT_KRBLEVEL:
1964 * A string that defines the kerberos security level.
1966 result = setstropt(&data->set.str[STRING_KRB_LEVEL],
1967 va_arg(param, char *));
1968 data->set.krb = (NULL != data->set.str[STRING_KRB_LEVEL])?TRUE:FALSE;
1970 case CURLOPT_GSSAPI_DELEGATION:
1972 * GSS-API credential delegation
1974 data->set.gssapi_delegation = va_arg(param, long);
1976 case CURLOPT_SSL_VERIFYPEER:
1978 * Enable peer SSL verifying.
1980 data->set.ssl.verifypeer = (0 != va_arg(param, long))?TRUE:FALSE;
1982 case CURLOPT_SSL_VERIFYHOST:
1984 * Enable verification of the host name in the peer certificate
1986 arg = va_arg(param, long);
1988 /* Obviously people are not reading documentation and too many thought
1989 this argument took a boolean when it wasn't and misused it. We thus ban
1990 1 as a sensible input and we warn about its use. Then we only have the
1991 2 action internally stored as TRUE. */
1994 failf(data, "CURLOPT_SSL_VERIFYHOST no longer supports 1 as value!");
1995 return CURLE_BAD_FUNCTION_ARGUMENT;
1998 data->set.ssl.verifyhost = (0 != arg)?TRUE:FALSE;
2000 case CURLOPT_SSL_CTX_FUNCTION:
2001 #ifdef have_curlssl_ssl_ctx
2003 * Set a SSL_CTX callback
2005 data->set.ssl.fsslctx = va_arg(param, curl_ssl_ctx_callback);
2007 result = CURLE_NOT_BUILT_IN;
2010 case CURLOPT_SSL_CTX_DATA:
2011 #ifdef have_curlssl_ssl_ctx
2013 * Set a SSL_CTX callback parameter pointer
2015 data->set.ssl.fsslctxp = va_arg(param, void *);
2017 result = CURLE_NOT_BUILT_IN;
2020 case CURLOPT_CERTINFO:
2021 #ifdef have_curlssl_certinfo
2022 data->set.ssl.certinfo = (0 != va_arg(param, long))?TRUE:FALSE;
2024 result = CURLE_NOT_BUILT_IN;
2027 case CURLOPT_PINNEDPUBLICKEY:
2029 * Set pinned public key for SSL connection.
2030 * Specify file name of the public key in DER format.
2032 result = setstropt(&data->set.str[STRING_SSL_PINNEDPUBLICKEY],
2033 va_arg(param, char *));
2035 case CURLOPT_CAINFO:
2037 * Set CA info for SSL connection. Specify file name of the CA certificate
2039 result = setstropt(&data->set.str[STRING_SSL_CAFILE],
2040 va_arg(param, char *));
2042 case CURLOPT_CAPATH:
2043 #ifdef have_curlssl_ca_path /* not supported by all backends */
2045 * Set CA path info for SSL connection. Specify directory name of the CA
2046 * certificates which have been prepared using openssl c_rehash utility.
2048 /* This does not work on windows. */
2049 result = setstropt(&data->set.str[STRING_SSL_CAPATH],
2050 va_arg(param, char *));
2052 result = CURLE_NOT_BUILT_IN;
2055 case CURLOPT_CRLFILE:
2057 * Set CRL file info for SSL connection. Specify file name of the CRL
2058 * to check certificates revocation
2060 result = setstropt(&data->set.str[STRING_SSL_CRLFILE],
2061 va_arg(param, char *));
2063 case CURLOPT_ISSUERCERT:
2065 * Set Issuer certificate file
2066 * to check certificates issuer
2068 result = setstropt(&data->set.str[STRING_SSL_ISSUERCERT],
2069 va_arg(param, char *));
2071 case CURLOPT_TELNETOPTIONS:
2073 * Set a linked list of telnet options
2075 data->set.telnet_options = va_arg(param, struct curl_slist *);
2078 case CURLOPT_BUFFERSIZE:
2080 * The application kindly asks for a differently sized receive buffer.
2081 * If it seems reasonable, we'll use it.
2083 data->set.buffer_size = va_arg(param, long);
2085 if((data->set.buffer_size> (BUFSIZE -1 )) ||
2086 (data->set.buffer_size < 1))
2087 data->set.buffer_size = 0; /* huge internal default */
2091 case CURLOPT_NOSIGNAL:
2093 * The application asks not to set any signal() or alarm() handlers,
2094 * even when using a timeout.
2096 data->set.no_signal = (0 != va_arg(param, long))?TRUE:FALSE;
2101 struct Curl_share *set;
2102 set = va_arg(param, struct Curl_share *);
2104 /* disconnect from old share, if any */
2106 Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
2108 if(data->dns.hostcachetype == HCACHE_SHARED) {
2109 data->dns.hostcache = NULL;
2110 data->dns.hostcachetype = HCACHE_NONE;
2113 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
2114 if(data->share->cookies == data->cookies)
2115 data->cookies = NULL;
2118 if(data->share->sslsession == data->state.session)
2119 data->state.session = NULL;
2121 data->share->dirty--;
2123 Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
2127 /* use new share if it set */
2131 Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
2133 data->share->dirty++;
2135 if(data->share->hostcache) {
2136 /* use shared host cache */
2137 data->dns.hostcache = data->share->hostcache;
2138 data->dns.hostcachetype = HCACHE_SHARED;
2140 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
2141 if(data->share->cookies) {
2142 /* use shared cookie list, first free own one if any */
2144 Curl_cookie_cleanup(data->cookies);
2145 /* enable cookies since we now use a share that uses cookies! */
2146 data->cookies = data->share->cookies;
2148 #endif /* CURL_DISABLE_HTTP */
2149 if(data->share->sslsession) {
2150 data->set.ssl.max_ssl_sessions = data->share->max_ssl_sessions;
2151 data->state.session = data->share->sslsession;
2153 Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
2156 /* check for host cache not needed,
2157 * it will be done by curl_easy_perform */
2161 case CURLOPT_PRIVATE:
2163 * Set private data pointer.
2165 data->set.private_data = va_arg(param, void *);
2168 case CURLOPT_MAXFILESIZE:
2170 * Set the maximum size of a file to download.
2172 data->set.max_filesize = va_arg(param, long);
2176 case CURLOPT_USE_SSL:
2178 * Make transfers attempt to use SSL/TLS.
2180 data->set.use_ssl = (curl_usessl)va_arg(param, long);
2183 case CURLOPT_SSL_OPTIONS:
2184 arg = va_arg(param, long);
2185 data->set.ssl_enable_beast = arg&CURLSSLOPT_ALLOW_BEAST?TRUE:FALSE;
2189 case CURLOPT_FTPSSLAUTH:
2191 * Set a specific auth for FTP-SSL transfers.
2193 data->set.ftpsslauth = (curl_ftpauth)va_arg(param, long);
2196 case CURLOPT_IPRESOLVE:
2197 data->set.ipver = va_arg(param, long);
2200 case CURLOPT_MAXFILESIZE_LARGE:
2202 * Set the maximum size of a file to download.
2204 data->set.max_filesize = va_arg(param, curl_off_t);
2207 case CURLOPT_TCP_NODELAY:
2209 * Enable or disable TCP_NODELAY, which will disable/enable the Nagle
2212 data->set.tcp_nodelay = (0 != va_arg(param, long))?TRUE:FALSE;
2215 case CURLOPT_FTP_ACCOUNT:
2216 result = setstropt(&data->set.str[STRING_FTP_ACCOUNT],
2217 va_arg(param, char *));
2220 case CURLOPT_IGNORE_CONTENT_LENGTH:
2221 data->set.ignorecl = (0 != va_arg(param, long))?TRUE:FALSE;
2224 case CURLOPT_CONNECT_ONLY:
2226 * No data transfer, set up connection and let application use the socket
2228 data->set.connect_only = (0 != va_arg(param, long))?TRUE:FALSE;
2231 case CURLOPT_FTP_ALTERNATIVE_TO_USER:
2232 result = setstropt(&data->set.str[STRING_FTP_ALTERNATIVE_TO_USER],
2233 va_arg(param, char *));
2236 case CURLOPT_SOCKOPTFUNCTION:
2238 * socket callback function: called after socket() but before connect()
2240 data->set.fsockopt = va_arg(param, curl_sockopt_callback);
2243 case CURLOPT_SOCKOPTDATA:
2245 * socket callback data pointer. Might be NULL.
2247 data->set.sockopt_client = va_arg(param, void *);
2250 case CURLOPT_OPENSOCKETFUNCTION:
2252 * open/create socket callback function: called instead of socket(),
2255 data->set.fopensocket = va_arg(param, curl_opensocket_callback);
2258 case CURLOPT_OPENSOCKETDATA:
2260 * socket callback data pointer. Might be NULL.
2262 data->set.opensocket_client = va_arg(param, void *);
2265 case CURLOPT_CLOSESOCKETFUNCTION:
2267 * close socket callback function: called instead of close()
2268 * when shutting down a connection
2270 data->set.fclosesocket = va_arg(param, curl_closesocket_callback);
2273 case CURLOPT_CLOSESOCKETDATA:
2275 * socket callback data pointer. Might be NULL.
2277 data->set.closesocket_client = va_arg(param, void *);
2280 case CURLOPT_SSL_SESSIONID_CACHE:
2281 data->set.ssl.sessionid = (0 != va_arg(param, long))?TRUE:FALSE;
2285 /* we only include SSH options if explicitly built to support SSH */
2286 case CURLOPT_SSH_AUTH_TYPES:
2287 data->set.ssh_auth_types = va_arg(param, long);
2290 case CURLOPT_SSH_PUBLIC_KEYFILE:
2292 * Use this file instead of the $HOME/.ssh/id_dsa.pub file
2294 result = setstropt(&data->set.str[STRING_SSH_PUBLIC_KEY],
2295 va_arg(param, char *));
2298 case CURLOPT_SSH_PRIVATE_KEYFILE:
2300 * Use this file instead of the $HOME/.ssh/id_dsa file
2302 result = setstropt(&data->set.str[STRING_SSH_PRIVATE_KEY],
2303 va_arg(param, char *));
2305 case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
2307 * Option to allow for the MD5 of the host public key to be checked
2308 * for validation purposes.
2310 result = setstropt(&data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5],
2311 va_arg(param, char *));
2313 #ifdef HAVE_LIBSSH2_KNOWNHOST_API
2314 case CURLOPT_SSH_KNOWNHOSTS:
2316 * Store the file name to read known hosts from.
2318 result = setstropt(&data->set.str[STRING_SSH_KNOWNHOSTS],
2319 va_arg(param, char *));
2322 case CURLOPT_SSH_KEYFUNCTION:
2323 /* setting to NULL is fine since the ssh.c functions themselves will
2324 then rever to use the internal default */
2325 data->set.ssh_keyfunc = va_arg(param, curl_sshkeycallback);
2328 case CURLOPT_SSH_KEYDATA:
2330 * Custom client data to pass to the SSH keyfunc callback
2332 data->set.ssh_keyfunc_userp = va_arg(param, void *);
2334 #endif /* HAVE_LIBSSH2_KNOWNHOST_API */
2336 #endif /* USE_LIBSSH2 */
2338 case CURLOPT_HTTP_TRANSFER_DECODING:
2340 * disable libcurl transfer encoding is used
2342 data->set.http_te_skip = (0 == va_arg(param, long))?TRUE:FALSE;
2345 case CURLOPT_HTTP_CONTENT_DECODING:
2347 * raw data passed to the application when content encoding is used
2349 data->set.http_ce_skip = (0 == va_arg(param, long))?TRUE:FALSE;
2352 case CURLOPT_NEW_FILE_PERMS:
2354 * Uses these permissions instead of 0644
2356 data->set.new_file_perms = va_arg(param, long);
2359 case CURLOPT_NEW_DIRECTORY_PERMS:
2361 * Uses these permissions instead of 0755
2363 data->set.new_directory_perms = va_arg(param, long);
2366 case CURLOPT_ADDRESS_SCOPE:
2368 * We always get longs when passed plain numericals, but for this value we
2369 * know that an unsigned int will always hold the value so we blindly
2370 * typecast to this type
2372 data->set.scope_id = curlx_sltoui(va_arg(param, long));
2375 case CURLOPT_PROTOCOLS:
2376 /* set the bitmask for the protocols that are allowed to be used for the
2377 transfer, which thus helps the app which takes URLs from users or other
2378 external inputs and want to restrict what protocol(s) to deal
2379 with. Defaults to CURLPROTO_ALL. */
2380 data->set.allowed_protocols = va_arg(param, long);
2383 case CURLOPT_REDIR_PROTOCOLS:
2384 /* set the bitmask for the protocols that libcurl is allowed to follow to,
2385 as a subset of the CURLOPT_PROTOCOLS ones. That means the protocol needs
2386 to be set in both bitmasks to be allowed to get redirected to. Defaults
2387 to all protocols except FILE and SCP. */
2388 data->set.redir_protocols = va_arg(param, long);
2391 case CURLOPT_MAIL_FROM:
2392 /* Set the SMTP mail originator */
2393 result = setstropt(&data->set.str[STRING_MAIL_FROM],
2394 va_arg(param, char *));
2397 case CURLOPT_MAIL_AUTH:
2398 /* Set the SMTP auth originator */
2399 result = setstropt(&data->set.str[STRING_MAIL_AUTH],
2400 va_arg(param, char *));
2403 case CURLOPT_MAIL_RCPT:
2404 /* Set the list of mail recipients */
2405 data->set.mail_rcpt = va_arg(param, struct curl_slist *);
2408 case CURLOPT_SASL_IR:
2409 /* Enable/disable SASL initial response */
2410 data->set.sasl_ir = (0 != va_arg(param, long)) ? TRUE : FALSE;
2413 case CURLOPT_RTSP_REQUEST:
2416 * Set the RTSP request method (OPTIONS, SETUP, PLAY, etc...)
2417 * Would this be better if the RTSPREQ_* were just moved into here?
2419 long curl_rtspreq = va_arg(param, long);
2420 Curl_RtspReq rtspreq = RTSPREQ_NONE;
2421 switch(curl_rtspreq) {
2422 case CURL_RTSPREQ_OPTIONS:
2423 rtspreq = RTSPREQ_OPTIONS;
2426 case CURL_RTSPREQ_DESCRIBE:
2427 rtspreq = RTSPREQ_DESCRIBE;
2430 case CURL_RTSPREQ_ANNOUNCE:
2431 rtspreq = RTSPREQ_ANNOUNCE;
2434 case CURL_RTSPREQ_SETUP:
2435 rtspreq = RTSPREQ_SETUP;
2438 case CURL_RTSPREQ_PLAY:
2439 rtspreq = RTSPREQ_PLAY;
2442 case CURL_RTSPREQ_PAUSE:
2443 rtspreq = RTSPREQ_PAUSE;
2446 case CURL_RTSPREQ_TEARDOWN:
2447 rtspreq = RTSPREQ_TEARDOWN;
2450 case CURL_RTSPREQ_GET_PARAMETER:
2451 rtspreq = RTSPREQ_GET_PARAMETER;
2454 case CURL_RTSPREQ_SET_PARAMETER:
2455 rtspreq = RTSPREQ_SET_PARAMETER;
2458 case CURL_RTSPREQ_RECORD:
2459 rtspreq = RTSPREQ_RECORD;
2462 case CURL_RTSPREQ_RECEIVE:
2463 rtspreq = RTSPREQ_RECEIVE;
2466 rtspreq = RTSPREQ_NONE;
2469 data->set.rtspreq = rtspreq;
2474 case CURLOPT_RTSP_SESSION_ID:
2476 * Set the RTSP Session ID manually. Useful if the application is
2477 * resuming a previously established RTSP session
2479 result = setstropt(&data->set.str[STRING_RTSP_SESSION_ID],
2480 va_arg(param, char *));
2483 case CURLOPT_RTSP_STREAM_URI:
2485 * Set the Stream URI for the RTSP request. Unless the request is
2486 * for generic server options, the application will need to set this.
2488 result = setstropt(&data->set.str[STRING_RTSP_STREAM_URI],
2489 va_arg(param, char *));
2492 case CURLOPT_RTSP_TRANSPORT:
2494 * The content of the Transport: header for the RTSP request
2496 result = setstropt(&data->set.str[STRING_RTSP_TRANSPORT],
2497 va_arg(param, char *));
2500 case CURLOPT_RTSP_CLIENT_CSEQ:
2502 * Set the CSEQ number to issue for the next RTSP request. Useful if the
2503 * application is resuming a previously broken connection. The CSEQ
2504 * will increment from this new number henceforth.
2506 data->state.rtsp_next_client_CSeq = va_arg(param, long);
2509 case CURLOPT_RTSP_SERVER_CSEQ:
2510 /* Same as the above, but for server-initiated requests */
2511 data->state.rtsp_next_client_CSeq = va_arg(param, long);
2514 case CURLOPT_INTERLEAVEDATA:
2515 data->set.rtp_out = va_arg(param, void *);
2517 case CURLOPT_INTERLEAVEFUNCTION:
2518 /* Set the user defined RTP write function */
2519 data->set.fwrite_rtp = va_arg(param, curl_write_callback);
2522 case CURLOPT_WILDCARDMATCH:
2523 data->set.wildcardmatch = (0 != va_arg(param, long))?TRUE:FALSE;
2525 case CURLOPT_CHUNK_BGN_FUNCTION:
2526 data->set.chunk_bgn = va_arg(param, curl_chunk_bgn_callback);
2528 case CURLOPT_CHUNK_END_FUNCTION:
2529 data->set.chunk_end = va_arg(param, curl_chunk_end_callback);
2531 case CURLOPT_FNMATCH_FUNCTION:
2532 data->set.fnmatch = va_arg(param, curl_fnmatch_callback);
2534 case CURLOPT_CHUNK_DATA:
2535 data->wildcard.customptr = va_arg(param, void *);
2537 case CURLOPT_FNMATCH_DATA:
2538 data->set.fnmatch_data = va_arg(param, void *);
2541 case CURLOPT_TLSAUTH_USERNAME:
2542 result = setstropt(&data->set.str[STRING_TLSAUTH_USERNAME],
2543 va_arg(param, char *));
2544 if(data->set.str[STRING_TLSAUTH_USERNAME] && !data->set.ssl.authtype)
2545 data->set.ssl.authtype = CURL_TLSAUTH_SRP; /* default to SRP */
2547 case CURLOPT_TLSAUTH_PASSWORD:
2548 result = setstropt(&data->set.str[STRING_TLSAUTH_PASSWORD],
2549 va_arg(param, char *));
2550 if(data->set.str[STRING_TLSAUTH_USERNAME] && !data->set.ssl.authtype)
2551 data->set.ssl.authtype = CURL_TLSAUTH_SRP; /* default to SRP */
2553 case CURLOPT_TLSAUTH_TYPE:
2554 if(strnequal((char *)va_arg(param, char *), "SRP", strlen("SRP")))
2555 data->set.ssl.authtype = CURL_TLSAUTH_SRP;
2557 data->set.ssl.authtype = CURL_TLSAUTH_NONE;
2560 case CURLOPT_DNS_SERVERS:
2561 result = Curl_set_dns_servers(data, va_arg(param, char *));
2563 case CURLOPT_DNS_INTERFACE:
2564 result = Curl_set_dns_interface(data, va_arg(param, char *));
2566 case CURLOPT_DNS_LOCAL_IP4:
2567 result = Curl_set_dns_local_ip4(data, va_arg(param, char *));
2569 case CURLOPT_DNS_LOCAL_IP6:
2570 result = Curl_set_dns_local_ip6(data, va_arg(param, char *));
2573 case CURLOPT_TCP_KEEPALIVE:
2574 data->set.tcp_keepalive = (0 != va_arg(param, long))?TRUE:FALSE;
2576 case CURLOPT_TCP_KEEPIDLE:
2577 data->set.tcp_keepidle = va_arg(param, long);
2579 case CURLOPT_TCP_KEEPINTVL:
2580 data->set.tcp_keepintvl = va_arg(param, long);
2582 case CURLOPT_SSL_ENABLE_NPN:
2583 data->set.ssl_enable_npn = (0 != va_arg(param, long))?TRUE:FALSE;
2585 case CURLOPT_SSL_ENABLE_ALPN:
2586 data->set.ssl_enable_alpn = (0 != va_arg(param, long))?TRUE:FALSE;
2589 #ifdef USE_UNIX_SOCKETS
2590 case CURLOPT_UNIX_SOCKET_PATH:
2591 result = setstropt(&data->set.str[STRING_UNIX_SOCKET_PATH],
2592 va_arg(param, char *));
2597 /* unknown tag and its companion, just ignore: */
2598 result = CURLE_UNKNOWN_OPTION;
2605 static void conn_free(struct connectdata *conn)
2610 /* possible left-overs from the async name resolvers */
2611 Curl_resolver_cancel(conn);
2613 /* close the SSL stuff before we close any sockets since they will/may
2614 write to the sockets */
2615 Curl_ssl_close(conn, FIRSTSOCKET);
2616 Curl_ssl_close(conn, SECONDARYSOCKET);
2618 /* close possibly still open sockets */
2619 if(CURL_SOCKET_BAD != conn->sock[SECONDARYSOCKET])
2620 Curl_closesocket(conn, conn->sock[SECONDARYSOCKET]);
2621 if(CURL_SOCKET_BAD != conn->sock[FIRSTSOCKET])
2622 Curl_closesocket(conn, conn->sock[FIRSTSOCKET]);
2623 if(CURL_SOCKET_BAD != conn->tempsock[0])
2624 Curl_closesocket(conn, conn->tempsock[0]);
2625 if(CURL_SOCKET_BAD != conn->tempsock[1])
2626 Curl_closesocket(conn, conn->tempsock[1]);
2628 #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
2629 defined(NTLM_WB_ENABLED)
2630 Curl_ntlm_wb_cleanup(conn);
2633 Curl_safefree(conn->user);
2634 Curl_safefree(conn->passwd);
2635 Curl_safefree(conn->xoauth2_bearer);
2636 Curl_safefree(conn->options);
2637 Curl_safefree(conn->proxyuser);
2638 Curl_safefree(conn->proxypasswd);
2639 Curl_safefree(conn->allocptr.proxyuserpwd);
2640 Curl_safefree(conn->allocptr.uagent);
2641 Curl_safefree(conn->allocptr.userpwd);
2642 Curl_safefree(conn->allocptr.accept_encoding);
2643 Curl_safefree(conn->allocptr.te);
2644 Curl_safefree(conn->allocptr.rangeline);
2645 Curl_safefree(conn->allocptr.ref);
2646 Curl_safefree(conn->allocptr.host);
2647 Curl_safefree(conn->allocptr.cookiehost);
2648 Curl_safefree(conn->allocptr.rtsp_transport);
2649 Curl_safefree(conn->trailer);
2650 Curl_safefree(conn->host.rawalloc); /* host name buffer */
2651 Curl_safefree(conn->proxy.rawalloc); /* proxy name buffer */
2652 Curl_safefree(conn->master_buffer);
2654 Curl_llist_destroy(conn->send_pipe, NULL);
2655 Curl_llist_destroy(conn->recv_pipe, NULL);
2657 conn->send_pipe = NULL;
2658 conn->recv_pipe = NULL;
2660 Curl_safefree(conn->localdev);
2661 Curl_free_ssl_config(&conn->ssl_config);
2663 free(conn); /* free all the connection oriented data */
2667 * Disconnects the given connection. Note the connection may not be the
2668 * primary connection, like when freeing room in the connection cache or
2669 * killing of a dead old connection.
2671 * This function MUST NOT reset state in the SessionHandle struct if that
2672 * isn't strictly bound to the life-time of *this* particular connection.
2676 CURLcode Curl_disconnect(struct connectdata *conn, bool dead_connection)
2678 struct SessionHandle *data;
2680 return CURLE_OK; /* this is closed and fine already */
2684 DEBUGF(fprintf(stderr, "DISCONNECT without easy handle, ignoring\n"));
2688 if(conn->dns_entry != NULL) {
2689 Curl_resolv_unlock(data, conn->dns_entry);
2690 conn->dns_entry = NULL;
2693 Curl_hostcache_prune(data); /* kill old DNS cache entries */
2695 #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM)
2696 /* Cleanup NTLM connection-related data */
2697 Curl_http_ntlm_cleanup(conn);
2700 if(conn->handler->disconnect)
2701 /* This is set if protocol-specific cleanups should be made */
2702 conn->handler->disconnect(conn, dead_connection);
2704 /* unlink ourselves! */
2705 infof(data, "Closing connection %ld\n", conn->connection_id);
2706 Curl_conncache_remove_conn(data->state.conn_cache, conn);
2708 #if defined(USE_LIBIDN)
2709 if(conn->host.encalloc)
2710 idn_free(conn->host.encalloc); /* encoded host name buffer, must be freed
2711 with idn_free() since this was allocated
2713 if(conn->proxy.encalloc)
2714 idn_free(conn->proxy.encalloc); /* encoded proxy name buffer, must be
2715 freed with idn_free() since this was
2716 allocated by libidn */
2717 #elif defined(USE_WIN32_IDN)
2718 free(conn->host.encalloc); /* encoded host name buffer, must be freed with
2719 idn_free() since this was allocated by
2720 curl_win32_idn_to_ascii */
2721 if(conn->proxy.encalloc)
2722 free(conn->proxy.encalloc); /* encoded proxy name buffer, must be freed
2723 with idn_free() since this was allocated by
2724 curl_win32_idn_to_ascii */
2727 Curl_ssl_close(conn, FIRSTSOCKET);
2729 /* Indicate to all handles on the pipe that we're dead */
2730 if(Curl_multi_pipeline_enabled(data->multi)) {
2731 signalPipeClose(conn->send_pipe, TRUE);
2732 signalPipeClose(conn->recv_pipe, TRUE);
2741 * This function should return TRUE if the socket is to be assumed to
2742 * be dead. Most commonly this happens when the server has closed the
2743 * connection due to inactivity.
2745 static bool SocketIsDead(curl_socket_t sock)
2748 bool ret_val = TRUE;
2750 sval = Curl_socket_ready(sock, CURL_SOCKET_BAD, 0);
2758 static bool IsPipeliningPossible(const struct SessionHandle *handle,
2759 const struct connectdata *conn)
2761 if((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
2762 Curl_multi_pipeline_enabled(handle->multi) &&
2763 (handle->set.httpreq == HTTPREQ_GET ||
2764 handle->set.httpreq == HTTPREQ_HEAD) &&
2765 handle->set.httpversion != CURL_HTTP_VERSION_1_0)
2771 bool Curl_isPipeliningEnabled(const struct SessionHandle *handle)
2773 return Curl_multi_pipeline_enabled(handle->multi);
2776 CURLcode Curl_addHandleToPipeline(struct SessionHandle *data,
2777 struct curl_llist *pipeline)
2779 if(!Curl_llist_insert_next(pipeline, pipeline->tail, data))
2780 return CURLE_OUT_OF_MEMORY;
2784 int Curl_removeHandleFromPipeline(struct SessionHandle *handle,
2785 struct curl_llist *pipeline)
2787 struct curl_llist_element *curr;
2789 curr = pipeline->head;
2791 if(curr->ptr == handle) {
2792 Curl_llist_remove(pipeline, curr, NULL);
2793 return 1; /* we removed a handle */
2801 #if 0 /* this code is saved here as it is useful for debugging purposes */
2802 static void Curl_printPipeline(struct curl_llist *pipeline)
2804 struct curl_llist_element *curr;
2806 curr = pipeline->head;
2808 struct SessionHandle *data = (struct SessionHandle *) curr->ptr;
2809 infof(data, "Handle in pipeline: %s\n", data->state.path);
2815 static struct SessionHandle* gethandleathead(struct curl_llist *pipeline)
2817 struct curl_llist_element *curr = pipeline->head;
2819 return (struct SessionHandle *) curr->ptr;
2825 /* remove the specified connection from all (possible) pipelines and related
2827 void Curl_getoff_all_pipelines(struct SessionHandle *data,
2828 struct connectdata *conn)
2830 bool recv_head = (conn->readchannel_inuse &&
2831 (gethandleathead(conn->recv_pipe) == data)) ? TRUE : FALSE;
2833 bool send_head = (conn->writechannel_inuse &&
2834 (gethandleathead(conn->send_pipe) == data)) ? TRUE : FALSE;
2836 if(Curl_removeHandleFromPipeline(data, conn->recv_pipe) && recv_head)
2837 conn->readchannel_inuse = FALSE;
2838 if(Curl_removeHandleFromPipeline(data, conn->send_pipe) && send_head)
2839 conn->writechannel_inuse = FALSE;
2842 static void signalPipeClose(struct curl_llist *pipeline, bool pipe_broke)
2844 struct curl_llist_element *curr;
2849 curr = pipeline->head;
2851 struct curl_llist_element *next = curr->next;
2852 struct SessionHandle *data = (struct SessionHandle *) curr->ptr;
2854 #ifdef DEBUGBUILD /* debug-only code */
2855 if(data->magic != CURLEASY_MAGIC_NUMBER) {
2857 infof(data, "signalPipeClose() found BAAD easy handle\n");
2862 data->state.pipe_broke = TRUE;
2863 Curl_multi_handlePipeBreak(data);
2864 Curl_llist_remove(pipeline, curr, NULL);
2870 * This function finds the connection in the connection
2871 * cache that has been unused for the longest time.
2873 * Returns the pointer to the oldest idle connection, or NULL if none was
2876 static struct connectdata *
2877 find_oldest_idle_connection(struct SessionHandle *data)
2879 struct conncache *bc = data->state.conn_cache;
2880 struct curl_hash_iterator iter;
2881 struct curl_llist_element *curr;
2882 struct curl_hash_element *he;
2886 struct connectdata *conn_candidate = NULL;
2887 struct connectbundle *bundle;
2891 Curl_hash_start_iterate(bc->hash, &iter);
2893 he = Curl_hash_next_element(&iter);
2895 struct connectdata *conn;
2899 curr = bundle->conn_list->head;
2904 /* Set higher score for the age passed since the connection was used */
2905 score = Curl_tvdiff(now, conn->now);
2907 if(score > highscore) {
2909 conn_candidate = conn;
2915 he = Curl_hash_next_element(&iter);
2918 return conn_candidate;
2922 * This function finds the connection in the connection
2923 * bundle that has been unused for the longest time.
2925 * Returns the pointer to the oldest idle connection, or NULL if none was
2928 static struct connectdata *
2929 find_oldest_idle_connection_in_bundle(struct SessionHandle *data,
2930 struct connectbundle *bundle)
2932 struct curl_llist_element *curr;
2936 struct connectdata *conn_candidate = NULL;
2937 struct connectdata *conn;
2943 curr = bundle->conn_list->head;
2948 /* Set higher score for the age passed since the connection was used */
2949 score = Curl_tvdiff(now, conn->now);
2951 if(score > highscore) {
2953 conn_candidate = conn;
2959 return conn_candidate;
2963 * This function checks if given connection is dead and disconnects if so.
2964 * (That also removes it from the connection cache.)
2966 * Returns TRUE if the connection actually was dead and disconnected.
2968 static bool disconnect_if_dead(struct connectdata *conn,
2969 struct SessionHandle *data)
2971 size_t pipeLen = conn->send_pipe->size + conn->recv_pipe->size;
2972 if(!pipeLen && !conn->inuse) {
2973 /* The check for a dead socket makes sense only if there are no
2974 handles in pipeline and the connection isn't already marked in
2977 if(conn->handler->protocol & CURLPROTO_RTSP)
2978 /* RTSP is a special case due to RTP interleaving */
2979 dead = Curl_rtsp_connisdead(conn);
2981 dead = SocketIsDead(conn->sock[FIRSTSOCKET]);
2985 infof(data, "Connection %ld seems to be dead!\n", conn->connection_id);
2987 /* disconnect resources */
2988 Curl_disconnect(conn, /* dead_connection */TRUE);
2996 * Wrapper to use disconnect_if_dead() function in Curl_conncache_foreach()
3000 static int call_disconnect_if_dead(struct connectdata *conn,
3003 struct SessionHandle* data = (struct SessionHandle*)param;
3004 disconnect_if_dead(conn, data);
3005 return 0; /* continue iteration */
3009 * This function scans the connection cache for half-open/dead connections,
3010 * closes and removes them.
3011 * The cleanup is done at most once per second.
3013 static void prune_dead_connections(struct SessionHandle *data)
3015 struct timeval now = Curl_tvnow();
3016 long elapsed = Curl_tvdiff(now, data->state.conn_cache->last_cleanup);
3018 if(elapsed >= 1000L) {
3019 Curl_conncache_foreach(data->state.conn_cache, data,
3020 call_disconnect_if_dead);
3021 data->state.conn_cache->last_cleanup = now;
3026 * Given one filled in connection struct (named needle), this function should
3027 * detect if there already is one that has all the significant details
3028 * exactly the same and thus should be used instead.
3030 * If there is a match, this function returns TRUE - and has marked the
3031 * connection as 'in-use'. It must later be called with ConnectionDone() to
3032 * return back to 'idle' (unused) state.
3034 * The force_reuse flag is set if the connection must be used, even if
3035 * the pipelining strategy wants to open a new connection instead of reusing.
3038 ConnectionExists(struct SessionHandle *data,
3039 struct connectdata *needle,
3040 struct connectdata **usethis,
3043 struct connectdata *check;
3044 struct connectdata *chosen = 0;
3045 bool canPipeline = IsPipeliningPossible(data, needle);
3046 bool wantNTLMhttp = ((data->state.authhost.want & CURLAUTH_NTLM) ||
3047 (data->state.authhost.want & CURLAUTH_NTLM_WB)) &&
3048 (needle->handler->protocol & PROTO_FAMILY_HTTP) ? TRUE : FALSE;
3049 struct connectbundle *bundle;
3051 *force_reuse = FALSE;
3053 /* We can't pipe if the site is blacklisted */
3054 if(canPipeline && Curl_pipeline_site_blacklisted(data, needle)) {
3055 canPipeline = FALSE;
3058 /* Look up the bundle with all the connections to this
3060 bundle = Curl_conncache_find_bundle(data->state.conn_cache,
3063 size_t max_pipe_len = Curl_multi_max_pipeline_length(data->multi);
3064 size_t best_pipe_len = max_pipe_len;
3065 struct curl_llist_element *curr;
3067 infof(data, "Found bundle for host %s: %p\n",
3068 needle->host.name, (void *)bundle);
3070 /* We can't pipe if we don't know anything about the server */
3071 if(canPipeline && !bundle->server_supports_pipelining) {
3072 infof(data, "Server doesn't support pipelining\n");
3073 canPipeline = FALSE;
3076 curr = bundle->conn_list->head;
3079 #if defined(USE_NTLM)
3080 bool credentialsMatch = FALSE;
3085 * Note that if we use a HTTP proxy, we check connections to that
3086 * proxy and not to the actual remote server.
3091 if(disconnect_if_dead(check, data))
3094 pipeLen = check->send_pipe->size + check->recv_pipe->size;
3097 /* Make sure the pipe has only GET requests */
3098 struct SessionHandle* sh = gethandleathead(check->send_pipe);
3099 struct SessionHandle* rh = gethandleathead(check->recv_pipe);
3101 if(!IsPipeliningPossible(sh, check))
3105 if(!IsPipeliningPossible(rh, check))
3111 /* can only happen within multi handles, and means that another easy
3112 handle is using this connection */
3116 if(Curl_resolver_asynch()) {
3117 /* ip_addr_str[0] is NUL only if the resolving of the name hasn't
3118 completed yet and until then we don't re-use this connection */
3119 if(!check->ip_addr_str[0]) {
3121 "Connection #%ld is still name resolving, can't reuse\n",
3122 check->connection_id);
3127 if((check->sock[FIRSTSOCKET] == CURL_SOCKET_BAD) ||
3128 check->bits.close) {
3129 /* Don't pick a connection that hasn't connected yet or that is going
3131 infof(data, "Connection #%ld isn't open enough, can't reuse\n",
3132 check->connection_id);
3134 if(check->recv_pipe->size > 0) {
3136 "BAD! Unconnected #%ld has a non-empty recv pipeline!\n",
3137 check->connection_id);
3144 if((needle->handler->flags&PROTOPT_SSL) !=
3145 (check->handler->flags&PROTOPT_SSL))
3146 /* don't do mixed SSL and non-SSL connections */
3147 if(!(needle->handler->protocol & check->handler->protocol))
3148 /* except protocols that have been upgraded via TLS */
3151 if(needle->handler->flags&PROTOPT_SSL) {
3152 if((data->set.ssl.verifypeer != check->verifypeer) ||
3153 (data->set.ssl.verifyhost != check->verifyhost))
3157 if(needle->bits.proxy != check->bits.proxy)
3158 /* don't do mixed proxy and non-proxy connections */
3161 if(!canPipeline && check->inuse)
3162 /* this request can't be pipelined but the checked connection is
3163 already in use so we skip it */
3166 if(needle->localdev || needle->localport) {
3167 /* If we are bound to a specific local end (IP+port), we must not
3168 re-use a random other one, although if we didn't ask for a
3169 particular one we can reuse one that was bound.
3171 This comparison is a bit rough and too strict. Since the input
3172 parameters can be specified in numerous ways and still end up the
3173 same it would take a lot of processing to make it really accurate.
3174 Instead, this matching will assume that re-uses of bound connections
3175 will most likely also re-use the exact same binding parameters and
3176 missing out a few edge cases shouldn't hurt anyone very much.
3178 if((check->localport != needle->localport) ||
3179 (check->localportrange != needle->localportrange) ||
3181 !needle->localdev ||
3182 strcmp(check->localdev, needle->localdev))
3186 if((!(needle->handler->flags & PROTOPT_CREDSPERREQUEST)) ||
3188 /* This protocol requires credentials per connection or is HTTP+NTLM,
3189 so verify that we're using the same name and password as well */
3190 if(!strequal(needle->user, check->user) ||
3191 !strequal(needle->passwd, check->passwd)) {
3192 /* one of them was different */
3195 #if defined(USE_NTLM)
3196 credentialsMatch = TRUE;
3200 if(!needle->bits.httpproxy || needle->handler->flags&PROTOPT_SSL ||
3201 (needle->bits.httpproxy && check->bits.httpproxy &&
3202 needle->bits.tunnel_proxy && check->bits.tunnel_proxy &&
3203 Curl_raw_equal(needle->proxy.name, check->proxy.name) &&
3204 (needle->port == check->port))) {
3205 /* The requested connection does not use a HTTP proxy or it uses SSL or
3206 it is a non-SSL protocol tunneled over the same http proxy name and
3207 port number or it is a non-SSL protocol which is allowed to be
3210 if((Curl_raw_equal(needle->handler->scheme, check->handler->scheme) ||
3211 needle->handler->protocol & check->handler->protocol) &&
3212 Curl_raw_equal(needle->host.name, check->host.name) &&
3213 needle->remote_port == check->remote_port) {
3214 if(needle->handler->flags & PROTOPT_SSL) {
3215 /* This is a SSL connection so verify that we're using the same
3216 SSL options as well */
3217 if(!Curl_ssl_config_matches(&needle->ssl_config,
3218 &check->ssl_config)) {
3220 "Connection #%ld has different SSL parameters, "
3222 check->connection_id));
3225 else if(check->ssl[FIRSTSOCKET].state != ssl_connection_complete) {
3227 "Connection #%ld has not started SSL connect, "
3229 check->connection_id));
3236 else { /* The requested needle connection is using a proxy,
3237 is the checked one using the same host, port and type? */
3238 if(check->bits.proxy &&
3239 (needle->proxytype == check->proxytype) &&
3240 (needle->bits.tunnel_proxy == check->bits.tunnel_proxy) &&
3241 Curl_raw_equal(needle->proxy.name, check->proxy.name) &&
3242 needle->port == check->port) {
3243 /* This is the same proxy connection, use it! */
3249 #if defined(USE_NTLM)
3250 /* If we are looking for an HTTP+NTLM connection, check if this is
3251 already authenticating with the right credentials. If not, keep
3252 looking so that we can reuse NTLM connections if
3253 possible. (Especially we must not reuse the same connection if
3254 partway through a handshake!) */
3256 if(credentialsMatch && check->ntlm.state != NTLMSTATE_NONE) {
3259 /* We must use this connection, no other */
3260 *force_reuse = TRUE;
3263 else if(credentialsMatch)
3264 /* this is a backup choice */
3271 /* We can pipeline if we want to. Let's continue looking for
3272 the optimal connection to use, i.e the shortest pipe that is not
3276 /* We have the optimal connection. Let's stop looking. */
3281 /* We can't use the connection if the pipe is full */
3282 if(pipeLen >= max_pipe_len)
3285 /* We can't use the connection if the pipe is penalized */
3286 if(Curl_pipeline_penalized(data, check))
3289 if(pipeLen < best_pipe_len) {
3290 /* This connection has a shorter pipe so far. We'll pick this
3291 and continue searching */
3293 best_pipe_len = pipeLen;
3298 /* We have found a connection. Let's stop searching. */
3308 return TRUE; /* yes, we found one to use! */
3311 return FALSE; /* no matching connecting exists */
3314 /* Mark the connection as 'idle', or close it if the cache is full.
3315 Returns TRUE if the connection is kept, or FALSE if it was closed. */
3317 ConnectionDone(struct SessionHandle *data, struct connectdata *conn)
3319 /* data->multi->maxconnects can be negative, deal with it. */
3320 size_t maxconnects =
3321 (data->multi->maxconnects < 0) ? data->multi->num_easy * 4:
3322 data->multi->maxconnects;
3323 struct connectdata *conn_candidate = NULL;
3325 /* Mark the current connection as 'unused' */
3326 conn->inuse = FALSE;
3328 if(maxconnects > 0 &&
3329 data->state.conn_cache->num_connections > maxconnects) {
3330 infof(data, "Connection cache is full, closing the oldest one.\n");
3332 conn_candidate = find_oldest_idle_connection(data);
3334 if(conn_candidate) {
3335 /* Set the connection's owner correctly */
3336 conn_candidate->data = data;
3338 /* the winner gets the honour of being disconnected */
3339 (void)Curl_disconnect(conn_candidate, /* dead_connection */ FALSE);
3343 return (conn_candidate == conn) ? FALSE : TRUE;
3347 * The given input connection struct pointer is to be stored in the connection
3348 * cache. If the cache is already full, least interesting existing connection
3349 * (if any) gets closed.
3351 * The given connection should be unique. That must've been checked prior to
3354 static CURLcode ConnectionStore(struct SessionHandle *data,
3355 struct connectdata *conn)
3357 return Curl_conncache_add_conn(data->state.conn_cache, conn);
3360 /* after a TCP connection to the proxy has been verified, this function does
3361 the next magic step.
3363 Note: this function's sub-functions call failf()
3366 CURLcode Curl_connected_proxy(struct connectdata *conn,
3369 if(!conn->bits.proxy || sockindex)
3370 /* this magic only works for the primary socket as the secondary is used
3371 for FTP only and it has FTP specific magic in ftp.c */
3374 switch(conn->proxytype) {
3375 #ifndef CURL_DISABLE_PROXY
3376 case CURLPROXY_SOCKS5:
3377 case CURLPROXY_SOCKS5_HOSTNAME:
3378 return Curl_SOCKS5(conn->proxyuser, conn->proxypasswd,
3379 conn->host.name, conn->remote_port,
3382 case CURLPROXY_SOCKS4:
3383 return Curl_SOCKS4(conn->proxyuser, conn->host.name,
3384 conn->remote_port, FIRSTSOCKET, conn, FALSE);
3386 case CURLPROXY_SOCKS4A:
3387 return Curl_SOCKS4(conn->proxyuser, conn->host.name,
3388 conn->remote_port, FIRSTSOCKET, conn, TRUE);
3390 #endif /* CURL_DISABLE_PROXY */
3391 case CURLPROXY_HTTP:
3392 case CURLPROXY_HTTP_1_0:
3393 /* do nothing here. handled later. */
3397 } /* switch proxytype */
3403 * verboseconnect() displays verbose information after a connect
3405 #ifndef CURL_DISABLE_VERBOSE_STRINGS
3406 void Curl_verboseconnect(struct connectdata *conn)
3408 if(conn->data->set.verbose)
3409 infof(conn->data, "Connected to %s (%s) port %ld (#%ld)\n",
3410 conn->bits.proxy ? conn->proxy.dispname : conn->host.dispname,
3411 conn->ip_addr_str, conn->port, conn->connection_id);
3415 int Curl_protocol_getsock(struct connectdata *conn,
3416 curl_socket_t *socks,
3419 if(conn->handler->proto_getsock)
3420 return conn->handler->proto_getsock(conn, socks, numsocks);
3421 return GETSOCK_BLANK;
3424 int Curl_doing_getsock(struct connectdata *conn,
3425 curl_socket_t *socks,
3428 if(conn && conn->handler->doing_getsock)
3429 return conn->handler->doing_getsock(conn, socks, numsocks);
3430 return GETSOCK_BLANK;
3434 * We are doing protocol-specific connecting and this is being called over and
3435 * over from the multi interface until the connection phase is done on
3439 CURLcode Curl_protocol_connecting(struct connectdata *conn,
3442 CURLcode result=CURLE_OK;
3444 if(conn && conn->handler->connecting) {
3446 result = conn->handler->connecting(conn, done);
3455 * We are DOING this is being called over and over from the multi interface
3456 * until the DOING phase is done on protocol layer.
3459 CURLcode Curl_protocol_doing(struct connectdata *conn, bool *done)
3461 CURLcode result=CURLE_OK;
3463 if(conn && conn->handler->doing) {
3465 result = conn->handler->doing(conn, done);
3474 * We have discovered that the TCP connection has been successful, we can now
3475 * proceed with some action.
3478 CURLcode Curl_protocol_connect(struct connectdata *conn,
3479 bool *protocol_done)
3481 CURLcode result=CURLE_OK;
3483 *protocol_done = FALSE;
3485 if(conn->bits.tcpconnect[FIRSTSOCKET] && conn->bits.protoconnstart) {
3486 /* We already are connected, get back. This may happen when the connect
3487 worked fine in the first call, like when we connect to a local server
3488 or proxy. Note that we don't know if the protocol is actually done.
3490 Unless this protocol doesn't have any protocol-connect callback, as
3491 then we know we're done. */
3492 if(!conn->handler->connecting)
3493 *protocol_done = TRUE;
3498 if(!conn->bits.protoconnstart) {
3500 result = Curl_proxy_connect(conn);
3504 if(conn->bits.tunnel_proxy && conn->bits.httpproxy &&
3505 (conn->tunnel_state[FIRSTSOCKET] != TUNNEL_COMPLETE))
3506 /* when using an HTTP tunnel proxy, await complete tunnel establishment
3507 before proceeding further. Return CURLE_OK so we'll be called again */
3510 if(conn->handler->connect_it) {
3511 /* is there a protocol-specific connect() procedure? */
3513 /* Call the protocol-specific connect function */
3514 result = conn->handler->connect_it(conn, protocol_done);
3517 *protocol_done = TRUE;
3519 /* it has started, possibly even completed but that knowledge isn't stored
3522 conn->bits.protoconnstart = TRUE;
3525 return result; /* pass back status */
3529 * Helpers for IDNA convertions.
3531 static bool is_ASCII_name(const char *hostname)
3533 const unsigned char *ch = (const unsigned char*)hostname;
3544 * Check if characters in hostname is allowed in Top Level Domain.
3546 static bool tld_check_name(struct SessionHandle *data,
3547 const char *ace_hostname)
3550 char *uc_name = NULL;
3552 #ifndef CURL_DISABLE_VERBOSE_STRINGS
3553 const char *tld_errmsg = "<no msg>";
3558 /* Convert (and downcase) ACE-name back into locale's character set */
3559 rc = idna_to_unicode_lzlz(ace_hostname, &uc_name, 0);
3560 if(rc != IDNA_SUCCESS)
3563 rc = tld_check_lz(uc_name, &err_pos, NULL);
3564 #ifndef CURL_DISABLE_VERBOSE_STRINGS
3565 #ifdef HAVE_TLD_STRERROR
3566 if(rc != TLD_SUCCESS)
3567 tld_errmsg = tld_strerror((Tld_rc)rc);
3569 if(rc == TLD_INVALID)
3570 infof(data, "WARNING: %s; pos %u = `%c'/0x%02X\n",
3571 tld_errmsg, err_pos, uc_name[err_pos],
3572 uc_name[err_pos] & 255);
3573 else if(rc != TLD_SUCCESS)
3574 infof(data, "WARNING: TLD check for %s failed; %s\n",
3575 uc_name, tld_errmsg);
3576 #endif /* CURL_DISABLE_VERBOSE_STRINGS */
3579 if(rc != TLD_SUCCESS)
3587 * Perform any necessary IDN conversion of hostname
3589 static void fix_hostname(struct SessionHandle *data,
3590 struct connectdata *conn, struct hostname *host)
3597 #elif defined(CURL_DISABLE_VERBOSE_STRINGS)
3601 /* set the name we use to display the host name */
3602 host->dispname = host->name;
3604 len = strlen(host->name);
3605 if(host->name[len-1] == '.')
3606 /* strip off a single trailing dot if present, primarily for SNI but
3607 there's no use for it */
3608 host->name[len-1]=0;
3610 if(!is_ASCII_name(host->name)) {
3612 /*************************************************************
3613 * Check name for non-ASCII and convert hostname to ACE form.
3614 *************************************************************/
3615 if(stringprep_check_version(LIBIDN_REQUIRED_VERSION)) {
3616 char *ace_hostname = NULL;
3617 int rc = idna_to_ascii_lz(host->name, &ace_hostname, 0);
3618 infof (data, "Input domain encoded as `%s'\n",
3619 stringprep_locale_charset ());
3620 if(rc != IDNA_SUCCESS)
3621 infof(data, "Failed to convert %s to ACE; %s\n",
3622 host->name, Curl_idn_strerror(conn,rc));
3624 /* tld_check_name() displays a warning if the host name contains
3625 "illegal" characters for this TLD */
3626 (void)tld_check_name(data, ace_hostname);
3628 host->encalloc = ace_hostname;
3629 /* change the name pointer to point to the encoded hostname */
3630 host->name = host->encalloc;
3633 #elif defined(USE_WIN32_IDN)
3634 /*************************************************************
3635 * Check name for non-ASCII and convert hostname to ACE form.
3636 *************************************************************/
3637 char *ace_hostname = NULL;
3638 int rc = curl_win32_idn_to_ascii(host->name, &ace_hostname);
3640 infof(data, "Failed to convert %s to ACE;\n",
3643 host->encalloc = ace_hostname;
3644 /* change the name pointer to point to the encoded hostname */
3645 host->name = host->encalloc;
3648 infof(data, "IDN support not present, can't parse Unicode domains\n");
3653 static void llist_dtor(void *user, void *element)
3661 * Allocate and initialize a new connectdata object.
3663 static struct connectdata *allocate_conn(struct SessionHandle *data)
3665 struct connectdata *conn = calloc(1, sizeof(struct connectdata));
3669 conn->handler = &Curl_handler_dummy; /* Be sure we have a handler defined
3670 already from start to avoid NULL
3671 situations and checks */
3673 /* and we setup a few fields in case we end up actually using this struct */
3675 conn->sock[FIRSTSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
3676 conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
3677 conn->tempsock[0] = CURL_SOCKET_BAD; /* no file descriptor */
3678 conn->tempsock[1] = CURL_SOCKET_BAD; /* no file descriptor */
3679 conn->connection_id = -1; /* no ID */
3680 conn->port = -1; /* unknown at this point */
3681 conn->remote_port = -1; /* unknown */
3683 /* Default protocol-independent behavior doesn't support persistent
3684 connections, so we set this to force-close. Protocols that support
3685 this need to set this to FALSE in their "curl_do" functions. */
3686 connclose(conn, "Default to force-close");
3688 /* Store creation time to help future close decision making */
3689 conn->created = Curl_tvnow();
3691 conn->data = data; /* Setup the association between this connection
3692 and the SessionHandle */
3694 conn->proxytype = data->set.proxytype; /* type */
3696 #ifdef CURL_DISABLE_PROXY
3698 conn->bits.proxy = FALSE;
3699 conn->bits.httpproxy = FALSE;
3700 conn->bits.proxy_user_passwd = FALSE;
3701 conn->bits.tunnel_proxy = FALSE;
3703 #else /* CURL_DISABLE_PROXY */
3705 /* note that these two proxy bits are now just on what looks to be
3706 requested, they may be altered down the road */
3707 conn->bits.proxy = (data->set.str[STRING_PROXY] &&
3708 *data->set.str[STRING_PROXY])?TRUE:FALSE;
3709 conn->bits.httpproxy = (conn->bits.proxy &&
3710 (conn->proxytype == CURLPROXY_HTTP ||
3711 conn->proxytype == CURLPROXY_HTTP_1_0))?TRUE:FALSE;
3712 conn->bits.proxy_user_passwd =
3713 (NULL != data->set.str[STRING_PROXYUSERNAME])?TRUE:FALSE;
3714 conn->bits.tunnel_proxy = data->set.tunnel_thru_httpproxy;
3716 #endif /* CURL_DISABLE_PROXY */
3718 conn->bits.user_passwd = (NULL != data->set.str[STRING_USERNAME])?TRUE:FALSE;
3719 conn->bits.ftp_use_epsv = data->set.ftp_use_epsv;
3720 conn->bits.ftp_use_eprt = data->set.ftp_use_eprt;
3722 conn->verifypeer = data->set.ssl.verifypeer;
3723 conn->verifyhost = data->set.ssl.verifyhost;
3725 conn->ip_version = data->set.ipver;
3727 #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
3728 defined(NTLM_WB_ENABLED)
3729 conn->ntlm_auth_hlpr_socket = CURL_SOCKET_BAD;
3730 conn->ntlm_auth_hlpr_pid = 0;
3731 conn->challenge_header = NULL;
3732 conn->response_header = NULL;
3735 if(Curl_multi_pipeline_enabled(data->multi) &&
3736 !conn->master_buffer) {
3737 /* Allocate master_buffer to be used for pipelining */
3738 conn->master_buffer = calloc(BUFSIZE, sizeof (char));
3739 if(!conn->master_buffer)
3743 /* Initialize the pipeline lists */
3744 conn->send_pipe = Curl_llist_alloc((curl_llist_dtor) llist_dtor);
3745 conn->recv_pipe = Curl_llist_alloc((curl_llist_dtor) llist_dtor);
3746 if(!conn->send_pipe || !conn->recv_pipe)
3750 conn->data_prot = PROT_CLEAR;
3753 /* Store the local bind parameters that will be used for this connection */
3754 if(data->set.str[STRING_DEVICE]) {
3755 conn->localdev = strdup(data->set.str[STRING_DEVICE]);
3759 conn->localportrange = data->set.localportrange;
3760 conn->localport = data->set.localport;
3762 /* the close socket stuff needs to be copied to the connection struct as
3763 it may live on without (this specific) SessionHandle */
3764 conn->fclosesocket = data->set.fclosesocket;
3765 conn->closesocket_client = data->set.closesocket_client;
3770 Curl_llist_destroy(conn->send_pipe, NULL);
3771 Curl_llist_destroy(conn->recv_pipe, NULL);
3773 conn->send_pipe = NULL;
3774 conn->recv_pipe = NULL;
3776 Curl_safefree(conn->master_buffer);
3777 Curl_safefree(conn->localdev);
3778 Curl_safefree(conn);
3782 static CURLcode findprotocol(struct SessionHandle *data,
3783 struct connectdata *conn,
3784 const char *protostr)
3786 const struct Curl_handler * const *pp;
3787 const struct Curl_handler *p;
3789 /* Scan protocol handler table and match against 'protostr' to set a few
3790 variables based on the URL. Now that the handler may be changed later
3791 when the protocol specific setup function is called. */
3792 for(pp = protocols; (p = *pp) != NULL; pp++) {
3793 if(Curl_raw_equal(p->scheme, protostr)) {
3794 /* Protocol found in table. Check if allowed */
3795 if(!(data->set.allowed_protocols & p->protocol))
3799 /* it is allowed for "normal" request, now do an extra check if this is
3800 the result of a redirect */
3801 if(data->state.this_is_a_follow &&
3802 !(data->set.redir_protocols & p->protocol))
3806 /* Perform setup complement if some. */
3807 conn->handler = conn->given = p;
3809 /* 'port' and 'remote_port' are set in setup_connection_internals() */
3815 /* The protocol was not found in the table, but we don't have to assign it
3816 to anything since it is already assigned to a dummy-struct in the
3817 create_conn() function when the connectdata struct is allocated. */
3818 failf(data, "Protocol \"%s\" not supported or disabled in " LIBCURL_NAME,
3821 return CURLE_UNSUPPORTED_PROTOCOL;
3825 * Parse URL and fill in the relevant members of the connection struct.
3827 static CURLcode parseurlandfillconn(struct SessionHandle *data,
3828 struct connectdata *conn,
3830 char **userp, char **passwdp,
3835 char *path = data->state.path;
3838 char protobuf[16] = "";
3839 const char *protop = "";
3841 bool rebuild_url = FALSE;
3843 *prot_missing = FALSE;
3845 /* We might pass the entire URL into the request so we need to make sure
3846 * there are no bad characters in there.*/
3847 if(strpbrk(data->change.url, "\r\n")) {
3848 failf(data, "Illegal characters found in URL");
3849 return CURLE_URL_MALFORMAT;
3852 /*************************************************************
3855 * We need to parse the url even when using the proxy, because we will need
3856 * the hostname and port in case we are trying to SSL connect through the
3857 * proxy -- and we don't know if we will need to use SSL until we parse the
3859 ************************************************************/
3860 if((2 == sscanf(data->change.url, "%15[^:]:%[^\n]",
3862 Curl_raw_equal(protobuf, "file")) {
3863 if(path[0] == '/' && path[1] == '/') {
3864 /* Allow omitted hostname (e.g. file:/<path>). This is not strictly
3865 * speaking a valid file: URL by RFC 1738, but treating file:/<path> as
3866 * file://localhost/<path> is similar to how other schemes treat missing
3867 * hostnames. See RFC 1808. */
3869 /* This cannot be done with strcpy() in a portable manner, since the
3870 memory areas overlap! */
3871 memmove(path, path + 2, strlen(path + 2)+1);
3874 * we deal with file://<host>/<path> differently since it supports no
3875 * hostname other than "localhost" and "127.0.0.1", which is unique among
3876 * the URL protocols specified in RFC 1738
3878 if(path[0] != '/') {
3879 /* the URL included a host name, we ignore host names in file:// URLs
3880 as the standards don't define what to do with them */
3881 char *ptr=strchr(path, '/');
3883 /* there was a slash present
3885 RFC1738 (section 3.1, page 5) says:
3887 The rest of the locator consists of data specific to the scheme,
3888 and is known as the "url-path". It supplies the details of how the
3889 specified resource can be accessed. Note that the "/" between the
3890 host (or port) and the url-path is NOT part of the url-path.
3892 As most agents use file://localhost/foo to get '/foo' although the
3893 slash preceding foo is a separator and not a slash for the path,
3894 a URL as file://localhost//foo must be valid as well, to refer to
3895 the same file with an absolute path.
3898 if(ptr[1] && ('/' == ptr[1]))
3899 /* if there was two slashes, we skip the first one as that is then
3900 used truly as a separator */
3903 /* This cannot be made with strcpy, as the memory chunks overlap! */
3904 memmove(path, ptr, strlen(ptr)+1);
3908 protop = "file"; /* protocol string */
3914 if(2 > sscanf(data->change.url,
3915 "%15[^\n:]://%[^\n/?]%[^\n]",
3917 conn->host.name, path)) {
3920 * The URL was badly formatted, let's try the browser-style _without_
3921 * protocol specified like 'http://'.
3923 rc = sscanf(data->change.url, "%[^\n/?]%[^\n]", conn->host.name, path);
3926 * We couldn't even get this format.
3927 * djgpp 2.04 has a sscanf() bug where 'conn->host.name' is
3928 * assigned, but the return value is EOF!
3930 #if defined(__DJGPP__) && (DJGPP_MINOR == 4)
3931 if(!(rc == -1 && *conn->host.name))
3934 failf(data, "<url> malformed");
3935 return CURLE_URL_MALFORMAT;
3940 * Since there was no protocol part specified, we guess what protocol it
3941 * is based on the first letters of the server name.
3944 /* Note: if you add a new protocol, please update the list in
3945 * lib/version.c too! */
3947 if(checkprefix("FTP.", conn->host.name))
3949 else if(checkprefix("DICT.", conn->host.name))
3951 else if(checkprefix("LDAP.", conn->host.name))
3953 else if(checkprefix("IMAP.", conn->host.name))
3955 else if(checkprefix("SMTP.", conn->host.name))
3957 else if(checkprefix("POP3.", conn->host.name))
3963 *prot_missing = TRUE; /* not given in URL */
3969 /* We search for '?' in the host name (but only on the right side of a
3970 * @-letter to allow ?-letters in username and password) to handle things
3971 * like http://example.com?param= (notice the missing '/').
3973 at = strchr(conn->host.name, '@');
3975 query = strchr(at+1, '?');
3977 query = strchr(conn->host.name, '?');
3980 /* We must insert a slash before the '?'-letter in the URL. If the URL had
3981 a slash after the '?', that is where the path currently begins and the
3982 '?string' is still part of the host name.
3984 We must move the trailing part from the host name and put it first in
3985 the path. And have it all prefixed with a slash.
3988 size_t hostlen = strlen(query);
3989 size_t pathlen = strlen(path);
3991 /* move the existing path plus the zero byte forward, to make room for
3992 the host-name part */
3993 memmove(path+hostlen+1, path, pathlen+1);
3995 /* now copy the trailing host part in front of the existing path */
3996 memcpy(path+1, query, hostlen);
3998 path[0]='/'; /* prepend the missing slash */
4001 *query=0; /* now cut off the hostname at the ? */
4004 /* if there's no path set, use a single slash */
4009 /* If the URL is malformatted (missing a '/' after hostname before path) we
4010 * insert a slash here. The only letter except '/' we accept to start a path
4013 if(path[0] == '?') {
4014 /* We need this function to deal with overlapping memory areas. We know
4015 that the memory area 'path' points to is 'urllen' bytes big and that
4016 is bigger than the path. Use +1 to move the zero byte too. */
4017 memmove(&path[1], path, strlen(path)+1);
4022 /* sanitise paths and remove ../ and ./ sequences according to RFC3986 */
4023 char *newp = Curl_dedotdotify(path);
4025 return CURLE_OUT_OF_MEMORY;
4027 if(strcmp(newp, path)) {
4029 free(data->state.pathbuffer);
4030 data->state.pathbuffer = newp;
4031 data->state.path = newp;
4039 * "rebuild_url" means that one or more URL components have been modified so
4040 * we need to generate an updated full version. We need the corrected URL
4041 * when communicating over HTTP proxy and we don't know at this point if
4042 * we're using a proxy or not.
4047 size_t plen = strlen(path); /* new path, should be 1 byte longer than
4049 size_t urllen = strlen(data->change.url); /* original URL length */
4051 size_t prefixlen = strlen(conn->host.name);
4054 prefixlen += strlen(protop) + strlen("://");
4056 reurl = malloc(urllen + 2); /* 2 for zerobyte + slash */
4058 return CURLE_OUT_OF_MEMORY;
4060 /* copy the prefix */
4061 memcpy(reurl, data->change.url, prefixlen);
4063 /* append the trailing piece + zerobyte */
4064 memcpy(&reurl[prefixlen], path, plen + 1);
4066 /* possible free the old one */
4067 if(data->change.url_alloc) {
4068 Curl_safefree(data->change.url);
4069 data->change.url_alloc = FALSE;
4072 infof(data, "Rebuilt URL to: %s\n", reurl);
4074 data->change.url = reurl;
4075 data->change.url_alloc = TRUE; /* free this later */
4079 * Parse the login details from the URL and strip them out of
4082 result = parse_url_login(data, conn, userp, passwdp, optionsp);
4086 if(conn->host.name[0] == '[') {
4087 /* This looks like an IPv6 address literal. See if there is an address
4088 scope if there is no location header */
4089 char *percent = strchr(conn->host.name, '%');
4091 unsigned int identifier_offset = 3;
4093 unsigned long scope;
4094 if(strncmp("%25", percent, 3) != 0) {
4096 "Please URL encode %% as %%25, see RFC 6874.\n");
4097 identifier_offset = 1;
4099 scope = strtoul(percent + identifier_offset, &endp, 10);
4101 /* The address scope was well formed. Knock it out of the
4103 memmove(percent, endp, strlen(endp)+1);
4104 conn->scope_id = (unsigned int)scope;
4107 /* Zone identifier is not numeric */
4108 #if defined(HAVE_NET_IF_H) && defined(IFNAMSIZ) && defined(HAVE_IF_NAMETOINDEX)
4109 char ifname[IFNAMSIZ + 2];
4110 char *square_bracket;
4111 unsigned int scopeidx = 0;
4112 strncpy(ifname, percent + identifier_offset, IFNAMSIZ + 2);
4113 /* Ensure nullbyte termination */
4114 ifname[IFNAMSIZ + 1] = '\0';
4115 square_bracket = strchr(ifname, ']');
4116 if(square_bracket) {
4118 *square_bracket = '\0';
4119 scopeidx = if_nametoindex(ifname);
4121 infof(data, "Invalid network interface: %s; %s\n", ifname,
4126 char *p = percent + identifier_offset + strlen(ifname);
4128 /* Remove zone identifier from hostname */
4129 memmove(percent, p, strlen(p) + 1);
4130 conn->scope_id = scopeidx;
4133 #endif /* HAVE_NET_IF_H && IFNAMSIZ */
4134 infof(data, "Invalid IPv6 address format\n");
4139 if(data->set.scope_id)
4140 /* Override any scope that was set above. */
4141 conn->scope_id = data->set.scope_id;
4143 /* Remove the fragment part of the path. Per RFC 2396, this is always the
4144 last part of the URI. We are looking for the first '#' so that we deal
4145 gracefully with non conformant URI such as http://example.com#foo#bar. */
4146 fragment = strchr(path, '#');
4150 /* we know the path part ended with a fragment, so we know the full URL
4151 string does too and we need to cut it off from there so it isn't used
4153 fragment = strchr(data->change.url, '#');
4159 * So if the URL was A://B/C#D,
4161 * conn->host.name is B
4162 * data->state.path is /C
4165 return findprotocol(data, conn, protop);
4169 * If we're doing a resumed transfer, we need to setup our stuff
4172 static CURLcode setup_range(struct SessionHandle *data)
4174 struct UrlState *s = &data->state;
4175 s->resume_from = data->set.set_resume_from;
4176 if(s->resume_from || data->set.str[STRING_SET_RANGE]) {
4177 if(s->rangestringalloc)
4181 s->range = aprintf("%" CURL_FORMAT_CURL_OFF_TU "-", s->resume_from);
4183 s->range = strdup(data->set.str[STRING_SET_RANGE]);
4185 s->rangestringalloc = (s->range)?TRUE:FALSE;
4188 return CURLE_OUT_OF_MEMORY;
4190 /* tell ourselves to fetch this range */
4191 s->use_range = TRUE; /* enable range download */
4194 s->use_range = FALSE; /* disable range download */
4201 * setup_connection_internals() -
4203 * Setup connection internals specific to the requested protocol in the
4204 * SessionHandle. This is inited and setup before the connection is made but
4205 * is about the particular protocol that is to be used.
4207 * This MUST get called after proxy magic has been figured out.
4209 static CURLcode setup_connection_internals(struct connectdata *conn)
4211 const struct Curl_handler * p;
4213 struct SessionHandle *data = conn->data;
4215 /* in some case in the multi state-machine, we go back to the CONNECT state
4216 and then a second (or third or...) call to this function will be made
4217 without doing a DISCONNECT or DONE in between (since the connection is
4218 yet in place) and therefore this function needs to first make sure
4219 there's no lingering previous data allocated. */
4220 Curl_free_request_state(data);
4222 memset(&data->req, 0, sizeof(struct SingleRequest));
4223 data->req.maxdownload = -1;
4225 conn->socktype = SOCK_STREAM; /* most of them are TCP streams */
4227 /* Perform setup complement if some. */
4230 if(p->setup_connection) {
4231 result = (*p->setup_connection)(conn);
4236 p = conn->handler; /* May have changed. */
4240 /* we check for -1 here since if proxy was detected already, this
4241 was very likely already set to the proxy port */
4242 conn->port = p->defport;
4244 /* only if remote_port was not already parsed off the URL we use the
4245 default port number */
4246 if(conn->remote_port < 0)
4247 conn->remote_port = (unsigned short)conn->given->defport;
4253 * Curl_free_request_state() should free temp data that was allocated in the
4254 * SessionHandle for this single request.
4257 void Curl_free_request_state(struct SessionHandle *data)
4259 Curl_safefree(data->req.protop);
4260 Curl_safefree(data->req.newurl);
4264 #ifndef CURL_DISABLE_PROXY
4265 /****************************************************************
4266 * Checks if the host is in the noproxy list. returns true if it matches
4267 * and therefore the proxy should NOT be used.
4268 ****************************************************************/
4269 static bool check_noproxy(const char* name, const char* no_proxy)
4271 /* no_proxy=domain1.dom,host.domain2.dom
4272 * (a comma-separated list of hosts which should
4273 * not be proxied, or an asterisk to override
4274 * all proxy variables)
4278 const char* separator = ", ";
4279 size_t no_proxy_len;
4283 if(no_proxy && no_proxy[0]) {
4284 if(Curl_raw_equal("*", no_proxy)) {
4288 /* NO_PROXY was specified and it wasn't just an asterisk */
4290 no_proxy_len = strlen(no_proxy);
4291 endptr = strchr(name, ':');
4293 namelen = endptr - name;
4295 namelen = strlen(name);
4297 for(tok_start = 0; tok_start < no_proxy_len; tok_start = tok_end + 1) {
4298 while(tok_start < no_proxy_len &&
4299 strchr(separator, no_proxy[tok_start]) != NULL) {
4300 /* Look for the beginning of the token. */
4304 if(tok_start == no_proxy_len)
4305 break; /* It was all trailing separator chars, no more tokens. */
4307 for(tok_end = tok_start; tok_end < no_proxy_len &&
4308 strchr(separator, no_proxy[tok_end]) == NULL; ++tok_end)
4309 /* Look for the end of the token. */
4312 /* To match previous behaviour, where it was necessary to specify
4313 * ".local.com" to prevent matching "notlocal.com", we will leave
4316 if(no_proxy[tok_start] == '.')
4319 if((tok_end - tok_start) <= namelen) {
4320 /* Match the last part of the name to the domain we are checking. */
4321 const char *checkn = name + namelen - (tok_end - tok_start);
4322 if(Curl_raw_nequal(no_proxy + tok_start, checkn,
4323 tok_end - tok_start)) {
4324 if((tok_end - tok_start) == namelen || *(checkn - 1) == '.') {
4325 /* We either have an exact match, or the previous character is a .
4326 * so it is within the same domain, so no proxy for this host.
4331 } /* if((tok_end - tok_start) <= namelen) */
4332 } /* for(tok_start = 0; tok_start < no_proxy_len;
4333 tok_start = tok_end + 1) */
4334 } /* NO_PROXY was specified and it wasn't just an asterisk */
4339 /****************************************************************
4340 * Detect what (if any) proxy to use. Remember that this selects a host
4341 * name and is not limited to HTTP proxies only.
4342 * The returned pointer must be freed by the caller (unless NULL)
4343 ****************************************************************/
4344 static char *detect_proxy(struct connectdata *conn)
4348 #ifndef CURL_DISABLE_HTTP
4349 /* If proxy was not specified, we check for default proxy environment
4350 * variables, to enable i.e Lynx compliance:
4352 * http_proxy=http://some.server.dom:port/
4353 * https_proxy=http://some.server.dom:port/
4354 * ftp_proxy=http://some.server.dom:port/
4355 * no_proxy=domain1.dom,host.domain2.dom
4356 * (a comma-separated list of hosts which should
4357 * not be proxied, or an asterisk to override
4358 * all proxy variables)
4359 * all_proxy=http://some.server.dom:port/
4360 * (seems to exist for the CERN www lib. Probably
4361 * the first to check for.)
4363 * For compatibility, the all-uppercase versions of these variables are
4364 * checked if the lowercase versions don't exist.
4366 char *no_proxy=NULL;
4367 char proxy_env[128];
4369 no_proxy=curl_getenv("no_proxy");
4371 no_proxy=curl_getenv("NO_PROXY");
4373 if(!check_noproxy(conn->host.name, no_proxy)) {
4374 /* It was not listed as without proxy */
4375 const char *protop = conn->handler->scheme;
4376 char *envp = proxy_env;
4379 /* Now, build <protocol>_proxy and check for such a one to use */
4381 *envp++ = (char)tolower((int)*protop++);
4384 strcpy(envp, "_proxy");
4386 /* read the protocol proxy: */
4387 prox=curl_getenv(proxy_env);
4390 * We don't try the uppercase version of HTTP_PROXY because of
4393 * When curl is used in a webserver application
4394 * environment (cgi or php), this environment variable can
4395 * be controlled by the web server user by setting the
4396 * http header 'Proxy:' to some value.
4398 * This can cause 'internal' http/ftp requests to be
4399 * arbitrarily redirected by any external attacker.
4401 if(!prox && !Curl_raw_equal("http_proxy", proxy_env)) {
4402 /* There was no lowercase variable, try the uppercase version: */
4403 Curl_strntoupper(proxy_env, proxy_env, sizeof(proxy_env));
4404 prox=curl_getenv(proxy_env);
4408 proxy = prox; /* use this */
4410 proxy = curl_getenv("all_proxy"); /* default proxy to use */
4412 proxy=curl_getenv("ALL_PROXY");
4414 } /* if(!check_noproxy(conn->host.name, no_proxy)) - it wasn't specified
4419 #else /* !CURL_DISABLE_HTTP */
4422 #endif /* CURL_DISABLE_HTTP */
4428 * If this is supposed to use a proxy, we need to figure out the proxy
4429 * host name, so that we can re-use an existing connection
4430 * that may exist registered to the same proxy host.
4431 * proxy will be freed before this function returns.
4433 static CURLcode parse_proxy(struct SessionHandle *data,
4434 struct connectdata *conn, char *proxy)
4439 /* We use 'proxyptr' to point to the proxy name from now on... */
4444 /* We do the proxy host string parsing here. We want the host name and the
4445 * port name. Accept a protocol:// prefix
4448 /* Parse the protocol part if present */
4449 endofprot = strstr(proxy, "://");
4451 proxyptr = endofprot+3;
4452 if(checkprefix("socks5h", proxy))
4453 conn->proxytype = CURLPROXY_SOCKS5_HOSTNAME;
4454 else if(checkprefix("socks5", proxy))
4455 conn->proxytype = CURLPROXY_SOCKS5;
4456 else if(checkprefix("socks4a", proxy))
4457 conn->proxytype = CURLPROXY_SOCKS4A;
4458 else if(checkprefix("socks4", proxy) || checkprefix("socks", proxy))
4459 conn->proxytype = CURLPROXY_SOCKS4;
4460 /* Any other xxx:// : change to http proxy */
4463 proxyptr = proxy; /* No xxx:// head: It's a HTTP proxy */
4465 /* Is there a username and password given in this proxy url? */
4466 atsign = strchr(proxyptr, '@');
4468 char *proxyuser = NULL;
4469 char *proxypasswd = NULL;
4471 parse_login_details(proxyptr, atsign - proxyptr,
4472 &proxyuser, &proxypasswd, NULL);
4474 /* found user and password, rip them out. note that we are
4475 unescaping them, as there is otherwise no way to have a
4476 username or password with reserved characters like ':' in
4478 Curl_safefree(conn->proxyuser);
4479 if(proxyuser && strlen(proxyuser) < MAX_CURL_USER_LENGTH)
4480 conn->proxyuser = curl_easy_unescape(data, proxyuser, 0, NULL);
4482 conn->proxyuser = strdup("");
4484 if(!conn->proxyuser)
4485 result = CURLE_OUT_OF_MEMORY;
4487 Curl_safefree(conn->proxypasswd);
4488 if(proxypasswd && strlen(proxypasswd) < MAX_CURL_PASSWORD_LENGTH)
4489 conn->proxypasswd = curl_easy_unescape(data, proxypasswd, 0, NULL);
4491 conn->proxypasswd = strdup("");
4493 if(!conn->proxypasswd)
4494 result = CURLE_OUT_OF_MEMORY;
4498 conn->bits.proxy_user_passwd = TRUE; /* enable it */
4499 atsign++; /* the right side of the @-letter */
4501 proxyptr = atsign; /* now use this instead */
4505 Curl_safefree(proxyuser);
4506 Curl_safefree(proxypasswd);
4512 /* start scanning for port number at this point */
4515 /* detect and extract RFC6874-style IPv6-addresses */
4516 if(*proxyptr == '[') {
4517 char *ptr = ++proxyptr; /* advance beyond the initial bracket */
4518 while(*ptr && (ISXDIGIT(*ptr) || (*ptr == ':') || (*ptr == '.')))
4521 /* There might be a zone identifier */
4522 if(strncmp("%25", ptr, 3))
4523 infof(data, "Please URL encode %% as %%25, see RFC 6874.\n");
4525 /* Allow unresered characters as defined in RFC 3986 */
4526 while(*ptr && (ISALPHA(*ptr) || ISXDIGIT(*ptr) || (*ptr == '-') ||
4527 (*ptr == '.') || (*ptr == '_') || (*ptr == '~')))
4531 /* yeps, it ended nicely with a bracket as well */
4534 infof(data, "Invalid IPv6 address format\n");
4536 /* Note that if this didn't end with a bracket, we still advanced the
4537 * proxyptr first, but I can't see anything wrong with that as no host
4538 * name nor a numeric can legally start with a bracket.
4542 /* Get port number off proxy.server.com:1080 */
4543 prox_portno = strchr(portptr, ':');
4545 *prox_portno = 0x0; /* cut off number from host name */
4547 /* now set the local port number */
4548 conn->port = strtol(prox_portno, NULL, 10);
4551 if(proxyptr[0]=='/')
4552 /* If the first character in the proxy string is a slash, fail
4553 immediately. The following code will otherwise clear the string which
4554 will lead to code running as if no proxy was set! */
4555 return CURLE_COULDNT_RESOLVE_PROXY;
4557 /* without a port number after the host name, some people seem to use
4558 a slash so we strip everything from the first slash */
4559 atsign = strchr(proxyptr, '/');
4561 *atsign = 0x0; /* cut off path part from host name */
4563 if(data->set.proxyport)
4564 /* None given in the proxy string, then get the default one if it is
4566 conn->port = data->set.proxyport;
4569 /* now, clone the cleaned proxy host name */
4570 conn->proxy.rawalloc = strdup(proxyptr);
4571 conn->proxy.name = conn->proxy.rawalloc;
4573 if(!conn->proxy.rawalloc)
4574 return CURLE_OUT_OF_MEMORY;
4580 * Extract the user and password from the authentication string
4582 static CURLcode parse_proxy_auth(struct SessionHandle *data,
4583 struct connectdata *conn)
4585 char proxyuser[MAX_CURL_USER_LENGTH]="";
4586 char proxypasswd[MAX_CURL_PASSWORD_LENGTH]="";
4588 if(data->set.str[STRING_PROXYUSERNAME] != NULL) {
4589 strncpy(proxyuser, data->set.str[STRING_PROXYUSERNAME],
4590 MAX_CURL_USER_LENGTH);
4591 proxyuser[MAX_CURL_USER_LENGTH-1] = '\0'; /*To be on safe side*/
4593 if(data->set.str[STRING_PROXYPASSWORD] != NULL) {
4594 strncpy(proxypasswd, data->set.str[STRING_PROXYPASSWORD],
4595 MAX_CURL_PASSWORD_LENGTH);
4596 proxypasswd[MAX_CURL_PASSWORD_LENGTH-1] = '\0'; /*To be on safe side*/
4599 conn->proxyuser = curl_easy_unescape(data, proxyuser, 0, NULL);
4600 if(!conn->proxyuser)
4601 return CURLE_OUT_OF_MEMORY;
4603 conn->proxypasswd = curl_easy_unescape(data, proxypasswd, 0, NULL);
4604 if(!conn->proxypasswd)
4605 return CURLE_OUT_OF_MEMORY;
4609 #endif /* CURL_DISABLE_PROXY */
4614 * Parse the login details (user name, password and options) from the URL and
4615 * strip them out of the host name
4617 * Inputs: data->set.use_netrc (CURLOPT_NETRC)
4620 * Outputs: (almost :- all currently undefined)
4621 * conn->bits.user_passwd - non-zero if non-default passwords exist
4622 * user - non-zero length if defined
4623 * passwd - non-zero length if defined
4624 * options - non-zero length if defined
4625 * conn->host.name - remove user name and password
4627 static CURLcode parse_url_login(struct SessionHandle *data,
4628 struct connectdata *conn,
4629 char **user, char **passwd, char **options)
4631 CURLcode result = CURLE_OK;
4633 char *passwdp = NULL;
4634 char *optionsp = NULL;
4636 /* At this point, we're hoping all the other special cases have
4637 * been taken care of, so conn->host.name is at most
4638 * [user[:password][;options]]@]hostname
4640 * We need somewhere to put the embedded details, so do that first.
4643 char *ptr = strchr(conn->host.name, '@');
4644 char *login = conn->host.name;
4646 DEBUGASSERT(!**user);
4647 DEBUGASSERT(!**passwd);
4648 DEBUGASSERT(!**options);
4653 /* We will now try to extract the
4654 * possible login information in a string like:
4655 * ftp://user:password@ftp.my.site:8021/README */
4656 conn->host.name = ++ptr;
4658 /* So the hostname is sane. Only bother interpreting the
4659 * results if we could care. It could still be wasted
4660 * work because it might be overtaken by the programmatically
4661 * set user/passwd, but doing that first adds more cases here :-(
4664 if(data->set.use_netrc == CURL_NETRC_REQUIRED)
4667 /* We could use the login information in the URL so extract it */
4668 result = parse_login_details(login, ptr - login - 1,
4669 &userp, &passwdp, &optionsp);
4676 /* We have a user in the URL */
4677 conn->bits.userpwd_in_url = TRUE;
4678 conn->bits.user_passwd = TRUE; /* enable user+password */
4680 /* Decode the user */
4681 newname = curl_easy_unescape(data, userp, 0, NULL);
4683 result = CURLE_OUT_OF_MEMORY;
4692 /* We have a password in the URL so decode it */
4693 char *newpasswd = curl_easy_unescape(data, passwdp, 0, NULL);
4695 result = CURLE_OUT_OF_MEMORY;
4700 *passwd = newpasswd;
4704 /* We have an options list in the URL so decode it */
4705 char *newoptions = curl_easy_unescape(data, optionsp, 0, NULL);
4707 result = CURLE_OUT_OF_MEMORY;
4712 *options = newoptions;
4718 Curl_safefree(userp);
4719 Curl_safefree(passwdp);
4720 Curl_safefree(optionsp);
4726 * parse_login_details()
4728 * This is used to parse a login string for user name, password and options in
4729 * the following formats:
4733 * user:password;options
4735 * user;options:password
4743 * login [in] - The login string.
4744 * len [in] - The length of the login string.
4745 * userp [in/out] - The address where a pointer to newly allocated memory
4746 * holding the user will be stored upon completion.
4747 * passdwp [in/out] - The address where a pointer to newly allocated memory
4748 * holding the password will be stored upon completion.
4749 * optionsp [in/out] - The address where a pointer to newly allocated memory
4750 * holding the options will be stored upon completion.
4752 * Returns CURLE_OK on success.
4754 static CURLcode parse_login_details(const char *login, const size_t len,
4755 char **userp, char **passwdp,
4758 CURLcode result = CURLE_OK;
4762 const char *psep = NULL;
4763 const char *osep = NULL;
4768 /* Attempt to find the password separator */
4770 psep = strchr(login, ':');
4772 /* Within the constraint of the login string */
4773 if(psep >= login + len)
4777 /* Attempt to find the options separator */
4779 osep = strchr(login, ';');
4781 /* Within the constraint of the login string */
4782 if(osep >= login + len)
4786 /* Calculate the portion lengths */
4788 (size_t)(osep && psep > osep ? osep - login : psep - login) :
4789 (osep ? (size_t)(osep - login) : len));
4791 (osep && osep > psep ? (size_t)(osep - psep) :
4792 (size_t)(login + len - psep)) - 1 : 0);
4794 (psep && psep > osep ? (size_t)(psep - osep) :
4795 (size_t)(login + len - osep)) - 1 : 0);
4797 /* Allocate the user portion buffer */
4799 ubuf = malloc(ulen + 1);
4801 result = CURLE_OUT_OF_MEMORY;
4804 /* Allocate the password portion buffer */
4805 if(!result && passwdp && plen) {
4806 pbuf = malloc(plen + 1);
4808 Curl_safefree(ubuf);
4809 result = CURLE_OUT_OF_MEMORY;
4813 /* Allocate the options portion buffer */
4814 if(!result && optionsp && olen) {
4815 obuf = malloc(olen + 1);
4817 Curl_safefree(pbuf);
4818 Curl_safefree(ubuf);
4819 result = CURLE_OUT_OF_MEMORY;
4824 /* Store the user portion if necessary */
4826 memcpy(ubuf, login, ulen);
4828 Curl_safefree(*userp);
4832 /* Store the password portion if necessary */
4834 memcpy(pbuf, psep + 1, plen);
4836 Curl_safefree(*passwdp);
4840 /* Store the options portion if necessary */
4842 memcpy(obuf, osep + 1, olen);
4844 Curl_safefree(*optionsp);
4852 /*************************************************************
4853 * Figure out the remote port number and fix it in the URL
4855 * No matter if we use a proxy or not, we have to figure out the remote
4856 * port number of various reasons.
4858 * To be able to detect port number flawlessly, we must not confuse them
4859 * IPv6-specified addresses in the [0::1] style. (RFC2732)
4861 * The conn->host.name is currently [user:passwd@]host[:port] where host
4862 * could be a hostname, IPv4 address or IPv6 address.
4864 * The port number embedded in the URL is replaced, if necessary.
4865 *************************************************************/
4866 static CURLcode parse_remote_port(struct SessionHandle *data,
4867 struct connectdata *conn)
4872 /* Note that at this point, the IPv6 address cannot contain any scope
4873 suffix as that has already been removed in the parseurlandfillconn()
4875 if((1 == sscanf(conn->host.name, "[%*45[0123456789abcdefABCDEF:.]%c",
4877 (']' == endbracket)) {
4878 /* this is a RFC2732-style specified IP-address */
4879 conn->bits.ipv6_ip = TRUE;
4881 conn->host.name++; /* skip over the starting bracket */
4882 portptr = strchr(conn->host.name, ']');
4884 *portptr++ = '\0'; /* zero terminate, killing the bracket */
4886 portptr = NULL; /* no port number available */
4891 struct in6_addr in6;
4892 if(Curl_inet_pton(AF_INET6, conn->host.name, &in6) > 0) {
4893 /* This is a numerical IPv6 address, meaning this is a wrongly formatted
4895 failf(data, "IPv6 numerical address used in URL without brackets");
4896 return CURLE_URL_MALFORMAT;
4900 portptr = strrchr(conn->host.name, ':');
4903 if(data->set.use_port && data->state.allow_port) {
4904 /* if set, we use this and ignore the port possibly given in the URL */
4905 conn->remote_port = (unsigned short)data->set.use_port;
4907 *portptr = '\0'; /* cut off the name there anyway - if there was a port
4908 number - since the port number is to be ignored! */
4909 if(conn->bits.httpproxy) {
4910 /* we need to create new URL with the new port number */
4914 if(conn->bits.type_set)
4915 snprintf(type, sizeof(type), ";type=%c",
4916 data->set.prefer_ascii?'A':
4917 (data->set.ftp_list_only?'D':'I'));
4920 * This synthesized URL isn't always right--suffixes like ;type=A are
4921 * stripped off. It would be better to work directly from the original
4922 * URL and simply replace the port part of it.
4924 url = aprintf("%s://%s%s%s:%hu%s%s%s", conn->given->scheme,
4925 conn->bits.ipv6_ip?"[":"", conn->host.name,
4926 conn->bits.ipv6_ip?"]":"", conn->remote_port,
4927 data->state.slash_removed?"/":"", data->state.path,
4930 return CURLE_OUT_OF_MEMORY;
4932 if(data->change.url_alloc) {
4933 Curl_safefree(data->change.url);
4934 data->change.url_alloc = FALSE;
4937 data->change.url = url;
4938 data->change.url_alloc = TRUE;
4942 /* no CURLOPT_PORT given, extract the one from the URL */
4947 port=strtol(portptr+1, &rest, 10); /* Port number must be decimal */
4949 if((port < 0) || (port > 0xffff)) {
4950 /* Single unix standard says port numbers are 16 bits long */
4951 failf(data, "Port number out of range");
4952 return CURLE_URL_MALFORMAT;
4955 else if(rest != &portptr[1]) {
4956 *portptr = '\0'; /* cut off the name there */
4957 conn->remote_port = curlx_ultous(port);
4960 /* Browser behavior adaptation. If there's a colon with no digits after,
4961 just cut off the name there which makes us ignore the colon and just
4962 use the default port. Firefox and Chrome both do that. */
4969 * Override the login details from the URL with that in the CURLOPT_USERPWD
4970 * option or a .netrc file, if applicable.
4972 static CURLcode override_login(struct SessionHandle *data,
4973 struct connectdata *conn,
4974 char **userp, char **passwdp, char **optionsp)
4976 if(data->set.str[STRING_USERNAME]) {
4978 *userp = strdup(data->set.str[STRING_USERNAME]);
4980 return CURLE_OUT_OF_MEMORY;
4983 if(data->set.str[STRING_PASSWORD]) {
4985 *passwdp = strdup(data->set.str[STRING_PASSWORD]);
4987 return CURLE_OUT_OF_MEMORY;
4990 if(data->set.str[STRING_OPTIONS]) {
4992 *optionsp = strdup(data->set.str[STRING_OPTIONS]);
4994 return CURLE_OUT_OF_MEMORY;
4997 conn->bits.netrc = FALSE;
4998 if(data->set.use_netrc != CURL_NETRC_IGNORED) {
4999 int ret = Curl_parsenetrc(conn->host.name,
5001 data->set.str[STRING_NETRC_FILE]);
5003 infof(data, "Couldn't find host %s in the "
5004 DOT_CHAR "netrc file; using defaults\n",
5008 return CURLE_OUT_OF_MEMORY;
5011 /* set bits.netrc TRUE to remember that we got the name from a .netrc
5012 file, so that it is safe to use even if we followed a Location: to a
5013 different host or similar. */
5014 conn->bits.netrc = TRUE;
5016 conn->bits.user_passwd = TRUE; /* enable user+password */
5024 * Set the login details so they're available in the connection
5026 static CURLcode set_login(struct connectdata *conn,
5027 const char *user, const char *passwd,
5028 const char *options)
5030 CURLcode result = CURLE_OK;
5032 /* If our protocol needs a password and we have none, use the defaults */
5033 if((conn->handler->flags & PROTOPT_NEEDSPWD) && !conn->bits.user_passwd) {
5034 /* Store the default user */
5035 conn->user = strdup(CURL_DEFAULT_USER);
5037 /* Store the default password */
5039 conn->passwd = strdup(CURL_DEFAULT_PASSWORD);
5041 conn->passwd = NULL;
5043 /* This is the default password, so DON'T set conn->bits.user_passwd */
5046 /* Store the user, zero-length if not set */
5047 conn->user = strdup(user);
5049 /* Store the password (only if user is present), zero-length if not set */
5051 conn->passwd = strdup(passwd);
5053 conn->passwd = NULL;
5056 if(!conn->user || !conn->passwd)
5057 result = CURLE_OUT_OF_MEMORY;
5059 /* Store the options, null if not set */
5060 if(!result && options[0]) {
5061 conn->options = strdup(options);
5064 result = CURLE_OUT_OF_MEMORY;
5070 /*************************************************************
5071 * Resolve the address of the server or proxy
5072 *************************************************************/
5073 static CURLcode resolve_server(struct SessionHandle *data,
5074 struct connectdata *conn,
5077 CURLcode result=CURLE_OK;
5078 long timeout_ms = Curl_timeleft(data, NULL, TRUE);
5080 /*************************************************************
5081 * Resolve the name of the server or proxy
5082 *************************************************************/
5083 if(conn->bits.reuse)
5084 /* We're reusing the connection - no need to resolve anything, and
5085 fix_hostname() was called already in create_conn() for the re-use
5090 /* this is a fresh connect */
5092 struct Curl_dns_entry *hostaddr;
5094 /* set a pointer to the hostname we display */
5095 fix_hostname(data, conn, &conn->host);
5097 #ifdef USE_UNIX_SOCKETS
5098 if(data->set.str[STRING_UNIX_SOCKET_PATH]) {
5099 /* Unix domain sockets are local. The host gets ignored, just use the
5100 * specified domain socket address. Do not cache "DNS entries". There is
5101 * no DNS involved and we already have the filesystem path available */
5102 const char *path = data->set.str[STRING_UNIX_SOCKET_PATH];
5104 hostaddr = calloc(1, sizeof(struct Curl_dns_entry));
5106 result = CURLE_OUT_OF_MEMORY;
5107 else if((hostaddr->addr = Curl_unix2addr(path)) != NULL)
5110 /* Long paths are not supported for now */
5111 if(strlen(path) >= sizeof(((struct sockaddr_un *)0)->sun_path)) {
5112 failf(data, "Unix socket path too long: '%s'", path);
5113 result = CURLE_COULDNT_RESOLVE_HOST;
5116 result = CURLE_OUT_OF_MEMORY;
5123 if(!conn->proxy.name || !*conn->proxy.name) {
5124 /* If not connecting via a proxy, extract the port from the URL, if it is
5125 * there, thus overriding any defaults that might have been set above. */
5126 conn->port = conn->remote_port; /* it is the same port */
5128 /* Resolve target host right on */
5129 rc = Curl_resolv_timeout(conn, conn->host.name, (int)conn->port,
5130 &hostaddr, timeout_ms);
5131 if(rc == CURLRESOLV_PENDING)
5134 else if(rc == CURLRESOLV_TIMEDOUT)
5135 result = CURLE_OPERATION_TIMEDOUT;
5137 else if(!hostaddr) {
5138 failf(data, "Couldn't resolve host '%s'", conn->host.dispname);
5139 result = CURLE_COULDNT_RESOLVE_HOST;
5140 /* don't return yet, we need to clean up the timeout first */
5144 /* This is a proxy that hasn't been resolved yet. */
5146 /* IDN-fix the proxy name */
5147 fix_hostname(data, conn, &conn->proxy);
5150 rc = Curl_resolv_timeout(conn, conn->proxy.name, (int)conn->port,
5151 &hostaddr, timeout_ms);
5153 if(rc == CURLRESOLV_PENDING)
5156 else if(rc == CURLRESOLV_TIMEDOUT)
5157 result = CURLE_OPERATION_TIMEDOUT;
5159 else if(!hostaddr) {
5160 failf(data, "Couldn't resolve proxy '%s'", conn->proxy.dispname);
5161 result = CURLE_COULDNT_RESOLVE_PROXY;
5162 /* don't return yet, we need to clean up the timeout first */
5165 DEBUGASSERT(conn->dns_entry == NULL);
5166 conn->dns_entry = hostaddr;
5173 * Cleanup the connection just allocated before we can move along and use the
5174 * previously existing one. All relevant data is copied over and old_conn is
5175 * ready for freeing once this function returns.
5177 static void reuse_conn(struct connectdata *old_conn,
5178 struct connectdata *conn)
5180 if(old_conn->proxy.rawalloc)
5181 free(old_conn->proxy.rawalloc);
5183 /* free the SSL config struct from this connection struct as this was
5184 allocated in vain and is targeted for destruction */
5185 Curl_free_ssl_config(&old_conn->ssl_config);
5187 conn->data = old_conn->data;
5189 /* get the user+password information from the old_conn struct since it may
5190 * be new for this request even when we re-use an existing connection */
5191 conn->bits.user_passwd = old_conn->bits.user_passwd;
5192 if(conn->bits.user_passwd) {
5193 /* use the new user name and password though */
5194 Curl_safefree(conn->user);
5195 Curl_safefree(conn->passwd);
5196 conn->user = old_conn->user;
5197 conn->passwd = old_conn->passwd;
5198 old_conn->user = NULL;
5199 old_conn->passwd = NULL;
5202 conn->bits.proxy_user_passwd = old_conn->bits.proxy_user_passwd;
5203 if(conn->bits.proxy_user_passwd) {
5204 /* use the new proxy user name and proxy password though */
5205 Curl_safefree(conn->proxyuser);
5206 Curl_safefree(conn->proxypasswd);
5207 conn->proxyuser = old_conn->proxyuser;
5208 conn->proxypasswd = old_conn->proxypasswd;
5209 old_conn->proxyuser = NULL;
5210 old_conn->proxypasswd = NULL;
5213 /* host can change, when doing keepalive with a proxy or if the case is
5214 different this time etc */
5215 Curl_safefree(conn->host.rawalloc);
5216 conn->host=old_conn->host;
5218 /* persist connection info in session handle */
5219 Curl_persistconninfo(conn);
5222 conn->bits.reuse = TRUE; /* yes, we're re-using here */
5224 Curl_safefree(old_conn->user);
5225 Curl_safefree(old_conn->passwd);
5226 Curl_safefree(old_conn->proxyuser);
5227 Curl_safefree(old_conn->proxypasswd);
5228 Curl_safefree(old_conn->localdev);
5230 Curl_llist_destroy(old_conn->send_pipe, NULL);
5231 Curl_llist_destroy(old_conn->recv_pipe, NULL);
5233 old_conn->send_pipe = NULL;
5234 old_conn->recv_pipe = NULL;
5236 Curl_safefree(old_conn->master_buffer);
5240 * create_conn() sets up a new connectdata struct, or re-uses an already
5241 * existing one, and resolves host name.
5243 * if this function returns CURLE_OK and *async is set to TRUE, the resolve
5244 * response will be coming asynchronously. If *async is FALSE, the name is
5247 * @param data The sessionhandle pointer
5248 * @param in_connect is set to the next connection data pointer
5249 * @param async is set TRUE when an async DNS resolution is pending
5250 * @see Curl_setup_conn()
5252 * *NOTE* this function assigns the conn->data pointer!
5255 static CURLcode create_conn(struct SessionHandle *data,
5256 struct connectdata **in_connect,
5259 CURLcode result = CURLE_OK;
5260 struct connectdata *conn;
5261 struct connectdata *conn_temp = NULL;
5264 char *passwd = NULL;
5265 char *options = NULL;
5268 bool prot_missing = FALSE;
5269 bool no_connections_available = FALSE;
5270 bool force_reuse = FALSE;
5271 size_t max_host_connections = Curl_multi_max_host_connections(data->multi);
5272 size_t max_total_connections = Curl_multi_max_total_connections(data->multi);
5276 /*************************************************************
5278 *************************************************************/
5280 if(!data->change.url) {
5281 result = CURLE_URL_MALFORMAT;
5285 /* First, split up the current URL in parts so that we can use the
5286 parts for checking against the already present connections. In order
5287 to not have to modify everything at once, we allocate a temporary
5288 connection data struct and fill in for comparison purposes. */
5289 conn = allocate_conn(data);
5292 result = CURLE_OUT_OF_MEMORY;
5296 /* We must set the return variable as soon as possible, so that our
5297 parent can cleanup any possible allocs we may have done before
5301 /* This initing continues below, see the comment "Continue connectdata
5302 * initialization here" */
5304 /***********************************************************
5305 * We need to allocate memory to store the path in. We get the size of the
5306 * full URL to be sure, and we need to make it at least 256 bytes since
5307 * other parts of the code will rely on this fact
5308 ***********************************************************/
5309 #define LEAST_PATH_ALLOC 256
5310 urllen=strlen(data->change.url);
5311 if(urllen < LEAST_PATH_ALLOC)
5312 urllen=LEAST_PATH_ALLOC;
5315 * We malloc() the buffers below urllen+2 to make room for 2 possibilities:
5316 * 1 - an extra terminating zero
5317 * 2 - an extra slash (in case a syntax like "www.host.com?moo" is used)
5320 Curl_safefree(data->state.pathbuffer);
5321 data->state.path = NULL;
5323 data->state.pathbuffer = malloc(urllen+2);
5324 if(NULL == data->state.pathbuffer) {
5325 result = CURLE_OUT_OF_MEMORY; /* really bad error */
5328 data->state.path = data->state.pathbuffer;
5330 conn->host.rawalloc = malloc(urllen+2);
5331 if(NULL == conn->host.rawalloc) {
5332 Curl_safefree(data->state.pathbuffer);
5333 data->state.path = NULL;
5334 result = CURLE_OUT_OF_MEMORY;
5338 conn->host.name = conn->host.rawalloc;
5339 conn->host.name[0] = 0;
5342 passwd = strdup("");
5343 options = strdup("");
5344 if(!user || !passwd || !options) {
5345 result = CURLE_OUT_OF_MEMORY;
5349 result = parseurlandfillconn(data, conn, &prot_missing, &user, &passwd,
5354 /*************************************************************
5355 * No protocol part in URL was used, add it!
5356 *************************************************************/
5358 /* We're guessing prefixes here and if we're told to use a proxy or if
5359 we're gonna follow a Location: later or... then we need the protocol
5360 part added so that we have a valid URL. */
5363 reurl = aprintf("%s://%s", conn->handler->scheme, data->change.url);
5366 result = CURLE_OUT_OF_MEMORY;
5370 if(data->change.url_alloc) {
5371 Curl_safefree(data->change.url);
5372 data->change.url_alloc = FALSE;
5375 data->change.url = reurl;
5376 data->change.url_alloc = TRUE; /* free this later */
5379 /*************************************************************
5380 * If the protocol can't handle url query strings, then cut
5381 * off the unhandable part
5382 *************************************************************/
5383 if((conn->given->flags&PROTOPT_NOURLQUERY)) {
5384 char *path_q_sep = strchr(conn->data->state.path, '?');
5386 /* according to rfc3986, allow the query (?foo=bar)
5387 also on protocols that can't handle it.
5389 cut the string-part after '?'
5392 /* terminate the string */
5397 if(data->set.str[STRING_BEARER]) {
5398 conn->xoauth2_bearer = strdup(data->set.str[STRING_BEARER]);
5399 if(!conn->xoauth2_bearer) {
5400 result = CURLE_OUT_OF_MEMORY;
5405 #ifndef CURL_DISABLE_PROXY
5406 /*************************************************************
5407 * Extract the user and password from the authentication string
5408 *************************************************************/
5409 if(conn->bits.proxy_user_passwd) {
5410 result = parse_proxy_auth(data, conn);
5415 /*************************************************************
5416 * Detect what (if any) proxy to use
5417 *************************************************************/
5418 if(data->set.str[STRING_PROXY]) {
5419 proxy = strdup(data->set.str[STRING_PROXY]);
5420 /* if global proxy is set, this is it */
5422 failf(data, "memory shortage");
5423 result = CURLE_OUT_OF_MEMORY;
5428 if(data->set.str[STRING_NOPROXY] &&
5429 check_noproxy(conn->host.name, data->set.str[STRING_NOPROXY])) {
5431 free(proxy); /* proxy is in exception list */
5436 proxy = detect_proxy(conn);
5438 #ifdef USE_UNIX_SOCKETS
5439 if(proxy && data->set.str[STRING_UNIX_SOCKET_PATH]) {
5440 free(proxy); /* Unix domain sockets cannot be proxied, so disable it */
5445 if(proxy && (!*proxy || (conn->handler->flags & PROTOPT_NONETWORK))) {
5446 free(proxy); /* Don't bother with an empty proxy string or if the
5447 protocol doesn't work with network */
5451 /***********************************************************************
5452 * If this is supposed to use a proxy, we need to figure out the proxy host
5453 * name, proxy type and port number, so that we can re-use an existing
5454 * connection that may exist registered to the same proxy host.
5455 ***********************************************************************/
5457 result = parse_proxy(data, conn, proxy);
5459 Curl_safefree(proxy); /* parse_proxy copies the proxy string */
5464 if((conn->proxytype == CURLPROXY_HTTP) ||
5465 (conn->proxytype == CURLPROXY_HTTP_1_0)) {
5466 #ifdef CURL_DISABLE_HTTP
5467 /* asking for a HTTP proxy is a bit funny when HTTP is disabled... */
5468 result = CURLE_UNSUPPORTED_PROTOCOL;
5471 /* force this connection's protocol to become HTTP if not already
5472 compatible - if it isn't tunneling through */
5473 if(!(conn->handler->protocol & PROTO_FAMILY_HTTP) &&
5474 !conn->bits.tunnel_proxy)
5475 conn->handler = &Curl_handler_http;
5477 conn->bits.httpproxy = TRUE;
5481 conn->bits.httpproxy = FALSE; /* not a HTTP proxy */
5482 conn->bits.proxy = TRUE;
5485 /* we aren't using the proxy after all... */
5486 conn->bits.proxy = FALSE;
5487 conn->bits.httpproxy = FALSE;
5488 conn->bits.proxy_user_passwd = FALSE;
5489 conn->bits.tunnel_proxy = FALSE;
5492 #endif /* CURL_DISABLE_PROXY */
5494 /*************************************************************
5495 * If the protocol is using SSL and HTTP proxy is used, we set
5496 * the tunnel_proxy bit.
5497 *************************************************************/
5498 if((conn->given->flags&PROTOPT_SSL) && conn->bits.httpproxy)
5499 conn->bits.tunnel_proxy = TRUE;
5501 /*************************************************************
5502 * Figure out the remote port number and fix it in the URL
5503 *************************************************************/
5504 result = parse_remote_port(data, conn);
5508 /* Check for overridden login details and set them accordingly so they
5509 they are known when protocol->setup_connection is called! */
5510 result = override_login(data, conn, &user, &passwd, &options);
5513 result = set_login(conn, user, passwd, options);
5517 /*************************************************************
5518 * Setup internals depending on protocol. Needs to be done after
5519 * we figured out what/if proxy to use.
5520 *************************************************************/
5521 result = setup_connection_internals(conn);
5525 conn->recv[FIRSTSOCKET] = Curl_recv_plain;
5526 conn->send[FIRSTSOCKET] = Curl_send_plain;
5527 conn->recv[SECONDARYSOCKET] = Curl_recv_plain;
5528 conn->send[SECONDARYSOCKET] = Curl_send_plain;
5530 /***********************************************************************
5531 * file: is a special case in that it doesn't need a network connection
5532 ***********************************************************************/
5533 #ifndef CURL_DISABLE_FILE
5534 if(conn->handler->flags & PROTOPT_NONETWORK) {
5536 /* this is supposed to be the connect function so we better at least check
5537 that the file is present here! */
5538 DEBUGASSERT(conn->handler->connect_it);
5539 result = conn->handler->connect_it(conn, &done);
5541 /* Setup a "faked" transfer that'll do nothing */
5544 conn->bits.tcpconnect[FIRSTSOCKET] = TRUE; /* we are "connected */
5546 ConnectionStore(data, conn);
5549 * Setup whatever necessary for a resumed transfer
5551 result = setup_range(data);
5553 DEBUGASSERT(conn->handler->done);
5554 /* we ignore the return code for the protocol-specific DONE */
5555 (void)conn->handler->done(conn, result, FALSE);
5559 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, /* no download */
5560 -1, NULL); /* no upload */
5563 /* since we skip do_init() */
5570 /* Get a cloned copy of the SSL config situation stored in the
5571 connection struct. But to get this going nicely, we must first make
5572 sure that the strings in the master copy are pointing to the correct
5573 strings in the session handle strings array!
5575 Keep in mind that the pointers in the master copy are pointing to strings
5576 that will be freed as part of the SessionHandle struct, but all cloned
5577 copies will be separately allocated.
5579 data->set.ssl.CApath = data->set.str[STRING_SSL_CAPATH];
5580 data->set.ssl.CAfile = data->set.str[STRING_SSL_CAFILE];
5581 data->set.ssl.CRLfile = data->set.str[STRING_SSL_CRLFILE];
5582 data->set.ssl.issuercert = data->set.str[STRING_SSL_ISSUERCERT];
5583 data->set.ssl.random_file = data->set.str[STRING_SSL_RANDOM_FILE];
5584 data->set.ssl.egdsocket = data->set.str[STRING_SSL_EGDSOCKET];
5585 data->set.ssl.cipher_list = data->set.str[STRING_SSL_CIPHER_LIST];
5587 data->set.ssl.username = data->set.str[STRING_TLSAUTH_USERNAME];
5588 data->set.ssl.password = data->set.str[STRING_TLSAUTH_PASSWORD];
5591 if(!Curl_clone_ssl_config(&data->set.ssl, &conn->ssl_config)) {
5592 result = CURLE_OUT_OF_MEMORY;
5596 prune_dead_connections(data);
5598 /*************************************************************
5599 * Check the current list of connections to see if we can
5600 * re-use an already existing one or if we have to create a
5602 *************************************************************/
5604 /* reuse_fresh is TRUE if we are told to use a new connection by force, but
5605 we only acknowledge this option if this is not a re-used connection
5606 already (which happens due to follow-location or during a HTTP
5607 authentication phase). */
5608 if(data->set.reuse_fresh && !data->state.this_is_a_follow)
5611 reuse = ConnectionExists(data, conn, &conn_temp, &force_reuse);
5613 /* If we found a reusable connection, we may still want to
5614 open a new connection if we are pipelining. */
5615 if(reuse && !force_reuse && IsPipeliningPossible(data, conn_temp)) {
5616 size_t pipelen = conn_temp->send_pipe->size + conn_temp->recv_pipe->size;
5618 infof(data, "Found connection %ld, with requests in the pipe (%zu)\n",
5619 conn_temp->connection_id, pipelen);
5621 if(conn_temp->bundle->num_connections < max_host_connections &&
5622 data->state.conn_cache->num_connections < max_total_connections) {
5623 /* We want a new connection anyway */
5626 infof(data, "We can reuse, but we want a new connection anyway\n");
5633 * We already have a connection for this, we got the former connection
5634 * in the conn_temp variable and thus we need to cleanup the one we
5635 * just allocated before we can move along and use the previously
5638 conn_temp->inuse = TRUE; /* mark this as being in use so that no other
5639 handle in a multi stack may nick it */
5640 reuse_conn(conn, conn_temp);
5641 free(conn); /* we don't need this anymore */
5645 /* set a pointer to the hostname we display */
5646 fix_hostname(data, conn, &conn->host);
5648 infof(data, "Re-using existing connection! (#%ld) with host %s\n",
5649 conn->connection_id,
5650 conn->proxy.name?conn->proxy.dispname:conn->host.dispname);
5653 /* We have decided that we want a new connection. However, we may not
5654 be able to do that if we have reached the limit of how many
5655 connections we are allowed to open. */
5656 struct connectbundle *bundle;
5658 bundle = Curl_conncache_find_bundle(data->state.conn_cache,
5660 if(max_host_connections > 0 && bundle &&
5661 (bundle->num_connections >= max_host_connections)) {
5662 struct connectdata *conn_candidate;
5664 /* The bundle is full. Let's see if we can kill a connection. */
5665 conn_candidate = find_oldest_idle_connection_in_bundle(data, bundle);
5667 if(conn_candidate) {
5668 /* Set the connection's owner correctly, then kill it */
5669 conn_candidate->data = data;
5670 (void)Curl_disconnect(conn_candidate, /* dead_connection */ FALSE);
5673 no_connections_available = TRUE;
5676 if(max_total_connections > 0 &&
5677 (data->state.conn_cache->num_connections >= max_total_connections)) {
5678 struct connectdata *conn_candidate;
5680 /* The cache is full. Let's see if we can kill a connection. */
5681 conn_candidate = find_oldest_idle_connection(data);
5683 if(conn_candidate) {
5684 /* Set the connection's owner correctly, then kill it */
5685 conn_candidate->data = data;
5686 (void)Curl_disconnect(conn_candidate, /* dead_connection */ FALSE);
5689 no_connections_available = TRUE;
5693 if(no_connections_available) {
5694 infof(data, "No connections available.\n");
5699 result = CURLE_NO_CONNECTION_AVAILABLE;
5704 * This is a brand new connection, so let's store it in the connection
5707 ConnectionStore(data, conn);
5710 #if defined(USE_NTLM)
5711 /* If NTLM is requested in a part of this connection, make sure we don't
5712 assume the state is fine as this is a fresh connection and NTLM is
5713 connection based. */
5714 if((data->state.authhost.picked & (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
5715 data->state.authhost.done) {
5716 infof(data, "NTLM picked AND auth done set, clear picked!\n");
5717 data->state.authhost.picked = CURLAUTH_NONE;
5720 if((data->state.authproxy.picked & (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
5721 data->state.authproxy.done) {
5722 infof(data, "NTLM-proxy picked AND auth done set, clear picked!\n");
5723 data->state.authproxy.picked = CURLAUTH_NONE;
5728 /* Mark the connection as used */
5731 /* Setup and init stuff before DO starts, in preparing for the transfer. */
5735 * Setup whatever necessary for a resumed transfer
5737 result = setup_range(data);
5741 /* Continue connectdata initialization here. */
5744 * Inherit the proper values from the urldata struct AFTER we have arranged
5745 * the persistent connection stuff
5747 conn->fread_func = data->set.fread_func;
5748 conn->fread_in = data->set.in;
5749 conn->seek_func = data->set.seek_func;
5750 conn->seek_client = data->set.seek_client;
5752 /*************************************************************
5753 * Resolve the address of the server or proxy
5754 *************************************************************/
5755 result = resolve_server(data, conn, async);
5759 Curl_safefree(options);
5760 Curl_safefree(passwd);
5761 Curl_safefree(user);
5762 Curl_safefree(proxy);
5766 /* Curl_setup_conn() is called after the name resolve initiated in
5767 * create_conn() is all done.
5769 * Curl_setup_conn() also handles reused connections
5771 * conn->data MUST already have been setup fine (in create_conn)
5774 CURLcode Curl_setup_conn(struct connectdata *conn,
5775 bool *protocol_done)
5777 CURLcode result = CURLE_OK;
5778 struct SessionHandle *data = conn->data;
5780 Curl_pgrsTime(data, TIMER_NAMELOOKUP);
5782 if(conn->handler->flags & PROTOPT_NONETWORK) {
5783 /* nothing to setup when not using a network */
5784 *protocol_done = TRUE;
5787 *protocol_done = FALSE; /* default to not done */
5789 /* set proxy_connect_closed to false unconditionally already here since it
5790 is used strictly to provide extra information to a parent function in the
5791 case of proxy CONNECT failures and we must make sure we don't have it
5792 lingering set from a previous invoke */
5793 conn->bits.proxy_connect_closed = FALSE;
5796 * Set user-agent. Used for HTTP, but since we can attempt to tunnel
5797 * basically anything through a http proxy we can't limit this based on
5800 if(data->set.str[STRING_USERAGENT]) {
5801 Curl_safefree(conn->allocptr.uagent);
5802 conn->allocptr.uagent =
5803 aprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
5804 if(!conn->allocptr.uagent)
5805 return CURLE_OUT_OF_MEMORY;
5808 data->req.headerbytecount = 0;
5810 #ifdef CURL_DO_LINEEND_CONV
5811 data->state.crlf_conversions = 0; /* reset CRLF conversion counter */
5812 #endif /* CURL_DO_LINEEND_CONV */
5814 /* set start time here for timeout purposes in the connect procedure, it
5815 is later set again for the progress meter purpose */
5816 conn->now = Curl_tvnow();
5818 if(CURL_SOCKET_BAD == conn->sock[FIRSTSOCKET]) {
5819 conn->bits.tcpconnect[FIRSTSOCKET] = FALSE;
5820 result = Curl_connecthost(conn, conn->dns_entry);
5825 Curl_pgrsTime(data, TIMER_CONNECT); /* we're connected already */
5826 Curl_pgrsTime(data, TIMER_APPCONNECT); /* we're connected already */
5827 conn->bits.tcpconnect[FIRSTSOCKET] = TRUE;
5828 *protocol_done = TRUE;
5829 Curl_updateconninfo(conn, conn->sock[FIRSTSOCKET]);
5830 Curl_verboseconnect(conn);
5833 conn->now = Curl_tvnow(); /* time this *after* the connect is done, we
5834 set this here perhaps a second time */
5838 * This check is quite a hack. We're calling _fsetmode to fix the problem
5839 * with fwrite converting newline characters (you get mangled text files,
5840 * and corrupted binary files when you download to stdout and redirect it to
5844 if((data->set.out)->_handle == NULL) {
5845 _fsetmode(stdout, "b");
5852 CURLcode Curl_connect(struct SessionHandle *data,
5853 struct connectdata **in_connect,
5855 bool *protocol_done)
5859 *asyncp = FALSE; /* assume synchronous resolves by default */
5861 /* call the stuff that needs to be called */
5862 result = create_conn(data, in_connect, asyncp);
5866 if((*in_connect)->send_pipe->size || (*in_connect)->recv_pipe->size)
5868 *protocol_done = TRUE;
5870 /* DNS resolution is done: that's either because this is a reused
5871 connection, in which case DNS was unnecessary, or because DNS
5872 really did finish already (synch resolver/fast async resolve) */
5873 result = Curl_setup_conn(*in_connect, protocol_done);
5877 if(result == CURLE_NO_CONNECTION_AVAILABLE) {
5882 if(result && *in_connect) {
5883 /* We're not allowed to return failure with memory left allocated
5884 in the connectdata struct, free those here */
5885 Curl_disconnect(*in_connect, FALSE); /* close the connection */
5886 *in_connect = NULL; /* return a NULL */
5892 CURLcode Curl_done(struct connectdata **connp,
5893 CURLcode status, /* an error if this is called after an
5894 error was detected */
5898 struct connectdata *conn;
5899 struct SessionHandle *data;
5901 DEBUGASSERT(*connp);
5907 /* Stop if Curl_done() has already been called */
5910 Curl_getoff_all_pipelines(data, conn);
5912 if((conn->send_pipe->size + conn->recv_pipe->size != 0 &&
5913 !data->set.reuse_forbid &&
5915 /* Stop if pipeline is not empty and we do not have to close
5919 conn->bits.done = TRUE; /* called just now! */
5921 /* Cleanup possible redirect junk */
5922 if(data->req.newurl) {
5923 free(data->req.newurl);
5924 data->req.newurl = NULL;
5926 if(data->req.location) {
5927 free(data->req.location);
5928 data->req.location = NULL;
5931 Curl_resolver_cancel(conn);
5933 if(conn->dns_entry) {
5934 Curl_resolv_unlock(data, conn->dns_entry); /* done with this */
5935 conn->dns_entry = NULL;
5939 case CURLE_ABORTED_BY_CALLBACK:
5940 case CURLE_READ_ERROR:
5941 case CURLE_WRITE_ERROR:
5942 /* When we're aborted due to a callback return code it basically have to
5943 be counted as premature as there is trouble ahead if we don't. We have
5944 many callbacks and protocols work differently, we could potentially do
5945 this more fine-grained in the future. */
5951 /* this calls the protocol-specific function pointer previously set */
5952 if(conn->handler->done)
5953 result = conn->handler->done(conn, status, premature);
5957 if(!result && Curl_pgrsDone(conn))
5958 result = CURLE_ABORTED_BY_CALLBACK;
5960 /* if the transfer was completed in a paused state there can be buffered
5961 data left to write and then kill */
5962 if(data->state.tempwrite) {
5963 free(data->state.tempwrite);
5964 data->state.tempwrite = NULL;
5967 /* if data->set.reuse_forbid is TRUE, it means the libcurl client has
5968 forced us to close this connection. This is ignored for requests taking
5969 place in a NTLM authentication handshake
5971 if conn->bits.close is TRUE, it means that the connection should be
5972 closed in spite of all our efforts to be nice, due to protocol
5973 restrictions in our or the server's end
5975 if premature is TRUE, it means this connection was said to be DONE before
5976 the entire request operation is complete and thus we can't know in what
5977 state it is for re-using, so we're forced to close it. In a perfect world
5978 we can add code that keep track of if we really must close it here or not,
5979 but currently we have no such detail knowledge.
5982 if((data->set.reuse_forbid
5983 #if defined(USE_NTLM)
5984 && !(conn->ntlm.state == NTLMSTATE_TYPE2 ||
5985 conn->proxyntlm.state == NTLMSTATE_TYPE2)
5987 ) || conn->bits.close || premature) {
5988 CURLcode res2 = Curl_disconnect(conn, premature); /* close connection */
5990 /* If we had an error already, make sure we return that one. But
5991 if we got a new error, return that. */
5996 /* the connection is no longer in use */
5997 if(ConnectionDone(data, conn)) {
5998 /* remember the most recently used connection */
5999 data->state.lastconnect = conn;
6001 infof(data, "Connection #%ld to host %s left intact\n",
6002 conn->connection_id,
6003 conn->bits.httpproxy?conn->proxy.dispname:conn->host.dispname);
6006 data->state.lastconnect = NULL;
6009 *connp = NULL; /* to make the caller of this function better detect that
6010 this was either closed or handed over to the connection
6011 cache here, and therefore cannot be used from this point on
6013 Curl_free_request_state(data);
6019 * do_init() inits the readwrite session. This is inited each time (in the DO
6020 * function before the protocol-specific DO functions are invoked) for a
6021 * transfer, sometimes multiple times on the same SessionHandle. Make sure
6022 * nothing in here depends on stuff that are setup dynamically for the
6026 static CURLcode do_init(struct connectdata *conn)
6028 struct SessionHandle *data = conn->data;
6029 struct SingleRequest *k = &data->req;
6031 conn->bits.done = FALSE; /* Curl_done() is not called yet */
6032 conn->bits.do_more = FALSE; /* by default there's no curl_do_more() to use */
6033 data->state.expect100header = FALSE;
6035 if(data->set.opt_no_body)
6036 /* in HTTP lingo, no body means using the HEAD request... */
6037 data->set.httpreq = HTTPREQ_HEAD;
6038 else if(HTTPREQ_HEAD == data->set.httpreq)
6039 /* ... but if unset there really is no perfect method that is the
6040 "opposite" of HEAD but in reality most people probably think GET
6041 then. The important thing is that we can't let it remain HEAD if the
6042 opt_no_body is set FALSE since then we'll behave wrong when getting
6044 data->set.httpreq = HTTPREQ_GET;
6046 k->start = Curl_tvnow(); /* start time */
6047 k->now = k->start; /* current time is now */
6048 k->header = TRUE; /* assume header */
6052 k->buf = data->state.buffer;
6053 k->uploadbuf = data->state.uploadbuffer;
6054 k->hbufp = data->state.headerbuff;
6055 k->ignorebody=FALSE;
6057 Curl_speedinit(data);
6059 Curl_pgrsSetUploadCounter(data, 0);
6060 Curl_pgrsSetDownloadCounter(data, 0);
6066 * do_complete is called when the DO actions are complete.
6068 * We init chunking and trailer bits to their default values here immediately
6069 * before receiving any header data for the current request in the pipeline.
6071 static void do_complete(struct connectdata *conn)
6073 conn->data->req.chunk=FALSE;
6074 conn->data->req.maxfd = (conn->sockfd>conn->writesockfd?
6075 conn->sockfd:conn->writesockfd)+1;
6076 Curl_pgrsTime(conn->data, TIMER_PRETRANSFER);
6079 CURLcode Curl_do(struct connectdata **connp, bool *done)
6081 CURLcode result=CURLE_OK;
6082 struct connectdata *conn = *connp;
6083 struct SessionHandle *data = conn->data;
6085 if(conn->handler->do_it) {
6086 /* generic protocol-specific function pointer set in curl_connect() */
6087 result = conn->handler->do_it(conn, done);
6089 /* This was formerly done in transfer.c, but we better do it here */
6090 if((CURLE_SEND_ERROR == result) && conn->bits.reuse) {
6092 * If the connection is using an easy handle, call reconnect
6093 * to re-establish the connection. Otherwise, let the multi logic
6094 * figure out how to re-establish the connection.
6097 result = Curl_reconnect_request(connp);
6100 /* ... finally back to actually retry the DO phase */
6101 conn = *connp; /* re-assign conn since Curl_reconnect_request
6102 creates a new connection */
6103 result = conn->handler->do_it(conn, done);
6110 if(!result && *done)
6111 /* do_complete must be called after the protocol-specific DO function */
6118 * Curl_do_more() is called during the DO_MORE multi state. It is basically a
6119 * second stage DO state which (wrongly) was introduced to support FTP's
6120 * second connection.
6122 * TODO: A future libcurl should be able to work away this state.
6124 * 'complete' can return 0 for incomplete, 1 for done and -1 for go back to
6125 * DOING state there's more work to do!
6128 CURLcode Curl_do_more(struct connectdata *conn, int *complete)
6130 CURLcode result=CURLE_OK;
6134 if(conn->handler->do_more)
6135 result = conn->handler->do_more(conn, complete);
6137 if(!result && (*complete == 1))
6138 /* do_complete must be called after the protocol-specific DO function */