Imported Upstream version 7.62.0
[platform/upstream/curl.git] / lib / url.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
27 #endif
28 #ifdef HAVE_NETDB_H
29 #include <netdb.h>
30 #endif
31 #ifdef HAVE_ARPA_INET_H
32 #include <arpa/inet.h>
33 #endif
34 #ifdef HAVE_NET_IF_H
35 #include <net/if.h>
36 #endif
37 #ifdef HAVE_SYS_IOCTL_H
38 #include <sys/ioctl.h>
39 #endif
40
41 #ifdef HAVE_SYS_PARAM_H
42 #include <sys/param.h>
43 #endif
44
45 #ifdef __VMS
46 #include <in.h>
47 #include <inet.h>
48 #endif
49
50 #ifdef HAVE_SYS_UN_H
51 #include <sys/un.h>
52 #endif
53
54 #ifndef HAVE_SOCKET
55 #error "We can't compile without socket() support!"
56 #endif
57
58 #include <limits.h>
59
60 #ifdef USE_LIBIDN2
61 #include <idn2.h>
62
63 #elif defined(USE_WIN32_IDN)
64 /* prototype for curl_win32_idn_to_ascii() */
65 bool curl_win32_idn_to_ascii(const char *in, char **out);
66 #endif  /* USE_LIBIDN2 */
67
68 #include "urldata.h"
69 #include "netrc.h"
70
71 #include "formdata.h"
72 #include "mime.h"
73 #include "vtls/vtls.h"
74 #include "hostip.h"
75 #include "transfer.h"
76 #include "sendf.h"
77 #include "progress.h"
78 #include "cookie.h"
79 #include "strcase.h"
80 #include "strerror.h"
81 #include "escape.h"
82 #include "strtok.h"
83 #include "share.h"
84 #include "content_encoding.h"
85 #include "http_digest.h"
86 #include "http_negotiate.h"
87 #include "select.h"
88 #include "multiif.h"
89 #include "easyif.h"
90 #include "speedcheck.h"
91 #include "warnless.h"
92 #include "non-ascii.h"
93 #include "inet_pton.h"
94 #include "getinfo.h"
95 #include "urlapi-int.h"
96
97 /* And now for the protocols */
98 #include "ftp.h"
99 #include "dict.h"
100 #include "telnet.h"
101 #include "tftp.h"
102 #include "http.h"
103 #include "http2.h"
104 #include "file.h"
105 #include "curl_ldap.h"
106 #include "ssh.h"
107 #include "imap.h"
108 #include "url.h"
109 #include "connect.h"
110 #include "inet_ntop.h"
111 #include "http_ntlm.h"
112 #include "curl_ntlm_wb.h"
113 #include "socks.h"
114 #include "curl_rtmp.h"
115 #include "gopher.h"
116 #include "http_proxy.h"
117 #include "conncache.h"
118 #include "multihandle.h"
119 #include "pipeline.h"
120 #include "dotdot.h"
121 #include "strdup.h"
122 #include "setopt.h"
123
124 /* The last 3 #include files should be in this order */
125 #include "curl_printf.h"
126 #include "curl_memory.h"
127 #include "memdebug.h"
128
129 static void conn_free(struct connectdata *conn);
130 static void free_fixed_hostname(struct hostname *host);
131 static unsigned int get_protocol_family(unsigned int protocol);
132
133 /* Some parts of the code (e.g. chunked encoding) assume this buffer has at
134  * more than just a few bytes to play with. Don't let it become too small or
135  * bad things will happen.
136  */
137 #if READBUFFER_SIZE < READBUFFER_MIN
138 # error READBUFFER_SIZE is too small
139 #endif
140
141
142 /*
143  * Protocol table.
144  */
145
146 static const struct Curl_handler * const protocols[] = {
147
148 #ifndef CURL_DISABLE_HTTP
149   &Curl_handler_http,
150 #endif
151
152 #if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
153   &Curl_handler_https,
154 #endif
155
156 #ifndef CURL_DISABLE_FTP
157   &Curl_handler_ftp,
158 #endif
159
160 #if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)
161   &Curl_handler_ftps,
162 #endif
163
164 #ifndef CURL_DISABLE_TELNET
165   &Curl_handler_telnet,
166 #endif
167
168 #ifndef CURL_DISABLE_DICT
169   &Curl_handler_dict,
170 #endif
171
172 #ifndef CURL_DISABLE_LDAP
173   &Curl_handler_ldap,
174 #if !defined(CURL_DISABLE_LDAPS) && \
175     ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \
176      (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))
177   &Curl_handler_ldaps,
178 #endif
179 #endif
180
181 #ifndef CURL_DISABLE_FILE
182   &Curl_handler_file,
183 #endif
184
185 #ifndef CURL_DISABLE_TFTP
186   &Curl_handler_tftp,
187 #endif
188
189 #if defined(USE_LIBSSH2) || defined(USE_LIBSSH)
190   &Curl_handler_scp,
191 #endif
192
193 #if defined(USE_LIBSSH2) || defined(USE_LIBSSH)
194   &Curl_handler_sftp,
195 #endif
196
197 #ifndef CURL_DISABLE_IMAP
198   &Curl_handler_imap,
199 #ifdef USE_SSL
200   &Curl_handler_imaps,
201 #endif
202 #endif
203
204 #ifndef CURL_DISABLE_POP3
205   &Curl_handler_pop3,
206 #ifdef USE_SSL
207   &Curl_handler_pop3s,
208 #endif
209 #endif
210
211 #if !defined(CURL_DISABLE_SMB) && defined(USE_NTLM) && \
212    (CURL_SIZEOF_CURL_OFF_T > 4) && \
213    (!defined(USE_WINDOWS_SSPI) || defined(USE_WIN32_CRYPTO))
214   &Curl_handler_smb,
215 #ifdef USE_SSL
216   &Curl_handler_smbs,
217 #endif
218 #endif
219
220 #ifndef CURL_DISABLE_SMTP
221   &Curl_handler_smtp,
222 #ifdef USE_SSL
223   &Curl_handler_smtps,
224 #endif
225 #endif
226
227 #ifndef CURL_DISABLE_RTSP
228   &Curl_handler_rtsp,
229 #endif
230
231 #ifndef CURL_DISABLE_GOPHER
232   &Curl_handler_gopher,
233 #endif
234
235 #ifdef USE_LIBRTMP
236   &Curl_handler_rtmp,
237   &Curl_handler_rtmpt,
238   &Curl_handler_rtmpe,
239   &Curl_handler_rtmpte,
240   &Curl_handler_rtmps,
241   &Curl_handler_rtmpts,
242 #endif
243
244   (struct Curl_handler *) NULL
245 };
246
247 /*
248  * Dummy handler for undefined protocol schemes.
249  */
250
251 static const struct Curl_handler Curl_handler_dummy = {
252   "<no protocol>",                      /* scheme */
253   ZERO_NULL,                            /* setup_connection */
254   ZERO_NULL,                            /* do_it */
255   ZERO_NULL,                            /* done */
256   ZERO_NULL,                            /* do_more */
257   ZERO_NULL,                            /* connect_it */
258   ZERO_NULL,                            /* connecting */
259   ZERO_NULL,                            /* doing */
260   ZERO_NULL,                            /* proto_getsock */
261   ZERO_NULL,                            /* doing_getsock */
262   ZERO_NULL,                            /* domore_getsock */
263   ZERO_NULL,                            /* perform_getsock */
264   ZERO_NULL,                            /* disconnect */
265   ZERO_NULL,                            /* readwrite */
266   ZERO_NULL,                            /* connection_check */
267   0,                                    /* defport */
268   0,                                    /* protocol */
269   PROTOPT_NONE                          /* flags */
270 };
271
272 void Curl_freeset(struct Curl_easy *data)
273 {
274   /* Free all dynamic strings stored in the data->set substructure. */
275   enum dupstring i;
276   for(i = (enum dupstring)0; i < STRING_LAST; i++) {
277     Curl_safefree(data->set.str[i]);
278   }
279
280   if(data->change.referer_alloc) {
281     Curl_safefree(data->change.referer);
282     data->change.referer_alloc = FALSE;
283   }
284   data->change.referer = NULL;
285   if(data->change.url_alloc) {
286     Curl_safefree(data->change.url);
287     data->change.url_alloc = FALSE;
288   }
289   data->change.url = NULL;
290
291   Curl_mime_cleanpart(&data->set.mimepost);
292 }
293
294 /* free the URL pieces */
295 void Curl_up_free(struct Curl_easy *data)
296 {
297   struct urlpieces *up = &data->state.up;
298   Curl_safefree(up->scheme);
299   Curl_safefree(up->hostname);
300   Curl_safefree(up->port);
301   Curl_safefree(up->user);
302   Curl_safefree(up->password);
303   Curl_safefree(up->options);
304   Curl_safefree(up->path);
305   Curl_safefree(up->query);
306   curl_url_cleanup(data->state.uh);
307   data->state.uh = NULL;
308 }
309
310 /*
311  * This is the internal function curl_easy_cleanup() calls. This should
312  * cleanup and free all resources associated with this sessionhandle.
313  *
314  * NOTE: if we ever add something that attempts to write to a socket or
315  * similar here, we must ignore SIGPIPE first. It is currently only done
316  * when curl_easy_perform() is invoked.
317  */
318
319 CURLcode Curl_close(struct Curl_easy *data)
320 {
321   struct Curl_multi *m;
322
323   if(!data)
324     return CURLE_OK;
325
326   Curl_expire_clear(data); /* shut off timers */
327
328   m = data->multi;
329   if(m)
330     /* This handle is still part of a multi handle, take care of this first
331        and detach this handle from there. */
332     curl_multi_remove_handle(data->multi, data);
333
334   if(data->multi_easy) {
335     /* when curl_easy_perform() is used, it creates its own multi handle to
336        use and this is the one */
337     curl_multi_cleanup(data->multi_easy);
338     data->multi_easy = NULL;
339   }
340
341   /* Destroy the timeout list that is held in the easy handle. It is
342      /normally/ done by curl_multi_remove_handle() but this is "just in
343      case" */
344   Curl_llist_destroy(&data->state.timeoutlist, NULL);
345
346   data->magic = 0; /* force a clear AFTER the possibly enforced removal from
347                       the multi handle, since that function uses the magic
348                       field! */
349
350   if(data->state.rangestringalloc)
351     free(data->state.range);
352
353   /* freed here just in case DONE wasn't called */
354   Curl_free_request_state(data);
355
356   /* Close down all open SSL info and sessions */
357   Curl_ssl_close_all(data);
358   Curl_safefree(data->state.first_host);
359   Curl_safefree(data->state.scratch);
360   Curl_ssl_free_certinfo(data);
361
362   /* Cleanup possible redirect junk */
363   free(data->req.newurl);
364   data->req.newurl = NULL;
365
366   if(data->change.referer_alloc) {
367     Curl_safefree(data->change.referer);
368     data->change.referer_alloc = FALSE;
369   }
370   data->change.referer = NULL;
371
372   Curl_up_free(data);
373   Curl_safefree(data->state.buffer);
374   Curl_safefree(data->state.headerbuff);
375   Curl_safefree(data->state.ulbuf);
376   Curl_flush_cookies(data, 1);
377   Curl_digest_cleanup(data);
378   Curl_safefree(data->info.contenttype);
379   Curl_safefree(data->info.wouldredirect);
380
381   /* this destroys the channel and we cannot use it anymore after this */
382   Curl_resolver_cleanup(data->state.resolver);
383
384   Curl_http2_cleanup_dependencies(data);
385   Curl_convert_close(data);
386
387   /* No longer a dirty share, if it exists */
388   if(data->share) {
389     Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
390     data->share->dirty--;
391     Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
392   }
393
394   /* destruct wildcard structures if it is needed */
395   Curl_wildcard_dtor(&data->wildcard);
396   Curl_freeset(data);
397   free(data);
398   return CURLE_OK;
399 }
400
401 /*
402  * Initialize the UserDefined fields within a Curl_easy.
403  * This may be safely called on a new or existing Curl_easy.
404  */
405 CURLcode Curl_init_userdefined(struct Curl_easy *data)
406 {
407   struct UserDefined *set = &data->set;
408   CURLcode result = CURLE_OK;
409
410   set->out = stdout; /* default output to stdout */
411   set->in_set = stdin;  /* default input from stdin */
412   set->err  = stderr;  /* default stderr to stderr */
413
414   /* use fwrite as default function to store output */
415   set->fwrite_func = (curl_write_callback)fwrite;
416
417   /* use fread as default function to read input */
418   set->fread_func_set = (curl_read_callback)fread;
419   set->is_fread_set = 0;
420   set->is_fwrite_set = 0;
421
422   set->seek_func = ZERO_NULL;
423   set->seek_client = ZERO_NULL;
424
425   /* conversion callbacks for non-ASCII hosts */
426   set->convfromnetwork = ZERO_NULL;
427   set->convtonetwork   = ZERO_NULL;
428   set->convfromutf8    = ZERO_NULL;
429
430   set->filesize = -1;        /* we don't know the size */
431   set->postfieldsize = -1;   /* unknown size */
432   set->maxredirs = -1;       /* allow any amount by default */
433
434   set->httpreq = HTTPREQ_GET; /* Default HTTP request */
435   set->rtspreq = RTSPREQ_OPTIONS; /* Default RTSP request */
436   set->ftp_use_epsv = TRUE;   /* FTP defaults to EPSV operations */
437   set->ftp_use_eprt = TRUE;   /* FTP defaults to EPRT operations */
438   set->ftp_use_pret = FALSE;  /* mainly useful for drftpd servers */
439   set->ftp_filemethod = FTPFILE_MULTICWD;
440
441   set->dns_cache_timeout = 60; /* Timeout every 60 seconds by default */
442
443   /* Set the default size of the SSL session ID cache */
444   set->general_ssl.max_ssl_sessions = 5;
445
446   set->proxyport = 0;
447   set->proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
448   set->httpauth = CURLAUTH_BASIC;  /* defaults to basic */
449   set->proxyauth = CURLAUTH_BASIC; /* defaults to basic */
450
451   /* SOCKS5 proxy auth defaults to username/password + GSS-API */
452   set->socks5auth = CURLAUTH_BASIC | CURLAUTH_GSSAPI;
453
454   /* make libcurl quiet by default: */
455   set->hide_progress = TRUE;  /* CURLOPT_NOPROGRESS changes these */
456
457   Curl_mime_initpart(&set->mimepost, data);
458
459   /*
460    * libcurl 7.10 introduced SSL verification *by default*! This needs to be
461    * switched off unless wanted.
462    */
463   set->ssl.primary.verifypeer = TRUE;
464   set->ssl.primary.verifyhost = TRUE;
465 #ifdef USE_TLS_SRP
466   set->ssl.authtype = CURL_TLSAUTH_NONE;
467 #endif
468   set->ssh_auth_types = CURLSSH_AUTH_DEFAULT; /* defaults to any auth
469                                                       type */
470   set->ssl.primary.sessionid = TRUE; /* session ID caching enabled by
471                                         default */
472   set->proxy_ssl = set->ssl;
473
474   set->new_file_perms = 0644;    /* Default permissions */
475   set->new_directory_perms = 0755; /* Default permissions */
476
477   /* for the *protocols fields we don't use the CURLPROTO_ALL convenience
478      define since we internally only use the lower 16 bits for the passed
479      in bitmask to not conflict with the private bits */
480   set->allowed_protocols = CURLPROTO_ALL;
481   set->redir_protocols = CURLPROTO_ALL &  /* All except FILE, SCP and SMB */
482                           ~(CURLPROTO_FILE | CURLPROTO_SCP | CURLPROTO_SMB |
483                             CURLPROTO_SMBS);
484
485 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
486   /*
487    * disallow unprotected protection negotiation NEC reference implementation
488    * seem not to follow rfc1961 section 4.3/4.4
489    */
490   set->socks5_gssapi_nec = FALSE;
491 #endif
492
493   /* Set the default CA cert bundle/path detected/specified at build time.
494    *
495    * If Schannel (WinSSL) is the selected SSL backend then these locations
496    * are ignored. We allow setting CA location for schannel only when
497    * explicitly specified by the user via CURLOPT_CAINFO / --cacert.
498    */
499   if(Curl_ssl_backend() != CURLSSLBACKEND_SCHANNEL) {
500 #if defined(CURL_CA_BUNDLE)
501     result = Curl_setstropt(&set->str[STRING_SSL_CAFILE_ORIG], CURL_CA_BUNDLE);
502     if(result)
503       return result;
504
505     result = Curl_setstropt(&set->str[STRING_SSL_CAFILE_PROXY],
506                             CURL_CA_BUNDLE);
507     if(result)
508       return result;
509 #endif
510 #if defined(CURL_CA_PATH)
511     result = Curl_setstropt(&set->str[STRING_SSL_CAPATH_ORIG], CURL_CA_PATH);
512     if(result)
513       return result;
514
515     result = Curl_setstropt(&set->str[STRING_SSL_CAPATH_PROXY], CURL_CA_PATH);
516     if(result)
517       return result;
518 #endif
519   }
520
521   set->wildcard_enabled = FALSE;
522   set->chunk_bgn      = ZERO_NULL;
523   set->chunk_end      = ZERO_NULL;
524   set->tcp_keepalive = FALSE;
525   set->tcp_keepintvl = 60;
526   set->tcp_keepidle = 60;
527   set->tcp_fastopen = FALSE;
528   set->tcp_nodelay = TRUE;
529   set->ssl_enable_npn = TRUE;
530   set->ssl_enable_alpn = TRUE;
531   set->expect_100_timeout = 1000L; /* Wait for a second by default. */
532   set->sep_headers = TRUE; /* separated header lists by default */
533   set->buffer_size = READBUFFER_SIZE;
534   set->upload_buffer_size = UPLOADBUFFER_DEFAULT;
535   set->happy_eyeballs_timeout = CURL_HET_DEFAULT;
536   set->fnmatch = ZERO_NULL;
537   set->upkeep_interval_ms = CURL_UPKEEP_INTERVAL_DEFAULT;
538   set->maxconnects = DEFAULT_CONNCACHE_SIZE; /* for easy handles */
539   set->httpversion =
540 #ifdef USE_NGHTTP2
541     CURL_HTTP_VERSION_2TLS
542 #else
543     CURL_HTTP_VERSION_1_1
544 #endif
545     ;
546   Curl_http2_init_userset(set);
547   return result;
548 }
549
550 /**
551  * Curl_open()
552  *
553  * @param curl is a pointer to a sessionhandle pointer that gets set by this
554  * function.
555  * @return CURLcode
556  */
557
558 CURLcode Curl_open(struct Curl_easy **curl)
559 {
560   CURLcode result;
561   struct Curl_easy *data;
562
563   /* Very simple start-up: alloc the struct, init it with zeroes and return */
564   data = calloc(1, sizeof(struct Curl_easy));
565   if(!data) {
566     /* this is a very serious error */
567     DEBUGF(fprintf(stderr, "Error: calloc of Curl_easy failed\n"));
568     return CURLE_OUT_OF_MEMORY;
569   }
570
571   data->magic = CURLEASY_MAGIC_NUMBER;
572
573   result = Curl_resolver_init(&data->state.resolver);
574   if(result) {
575     DEBUGF(fprintf(stderr, "Error: resolver_init failed\n"));
576     free(data);
577     return result;
578   }
579
580   /* We do some initial setup here, all those fields that can't be just 0 */
581
582   data->state.buffer = malloc(READBUFFER_SIZE + 1);
583   if(!data->state.buffer) {
584     DEBUGF(fprintf(stderr, "Error: malloc of buffer failed\n"));
585     result = CURLE_OUT_OF_MEMORY;
586   }
587   else {
588     data->state.headerbuff = malloc(HEADERSIZE);
589     if(!data->state.headerbuff) {
590       DEBUGF(fprintf(stderr, "Error: malloc of headerbuff failed\n"));
591       result = CURLE_OUT_OF_MEMORY;
592     }
593     else {
594       result = Curl_init_userdefined(data);
595
596       data->state.headersize = HEADERSIZE;
597       Curl_convert_init(data);
598       Curl_initinfo(data);
599
600       /* most recent connection is not yet defined */
601       data->state.lastconnect = NULL;
602
603       data->progress.flags |= PGRS_HIDE;
604       data->state.current_speed = -1; /* init to negative == impossible */
605
606       Curl_http2_init_state(&data->state);
607     }
608   }
609
610   if(result) {
611     Curl_resolver_cleanup(data->state.resolver);
612     free(data->state.buffer);
613     free(data->state.headerbuff);
614     Curl_freeset(data);
615     free(data);
616     data = NULL;
617   }
618   else
619     *curl = data;
620
621   return result;
622 }
623
624 #ifdef USE_RECV_BEFORE_SEND_WORKAROUND
625 static void conn_reset_postponed_data(struct connectdata *conn, int num)
626 {
627   struct postponed_data * const psnd = &(conn->postponed[num]);
628   if(psnd->buffer) {
629     DEBUGASSERT(psnd->allocated_size > 0);
630     DEBUGASSERT(psnd->recv_size <= psnd->allocated_size);
631     DEBUGASSERT(psnd->recv_size ?
632                 (psnd->recv_processed < psnd->recv_size) :
633                 (psnd->recv_processed == 0));
634     DEBUGASSERT(psnd->bindsock != CURL_SOCKET_BAD);
635     free(psnd->buffer);
636     psnd->buffer = NULL;
637     psnd->allocated_size = 0;
638     psnd->recv_size = 0;
639     psnd->recv_processed = 0;
640 #ifdef DEBUGBUILD
641     psnd->bindsock = CURL_SOCKET_BAD; /* used only for DEBUGASSERT */
642 #endif /* DEBUGBUILD */
643   }
644   else {
645     DEBUGASSERT(psnd->allocated_size == 0);
646     DEBUGASSERT(psnd->recv_size == 0);
647     DEBUGASSERT(psnd->recv_processed == 0);
648     DEBUGASSERT(psnd->bindsock == CURL_SOCKET_BAD);
649   }
650 }
651
652 static void conn_reset_all_postponed_data(struct connectdata *conn)
653 {
654   conn_reset_postponed_data(conn, 0);
655   conn_reset_postponed_data(conn, 1);
656 }
657 #else  /* ! USE_RECV_BEFORE_SEND_WORKAROUND */
658 /* Use "do-nothing" macro instead of function when workaround not used */
659 #define conn_reset_all_postponed_data(c) do {} WHILE_FALSE
660 #endif /* ! USE_RECV_BEFORE_SEND_WORKAROUND */
661
662 static void conn_free(struct connectdata *conn)
663 {
664   if(!conn)
665     return;
666
667   /* possible left-overs from the async name resolvers */
668   Curl_resolver_cancel(conn);
669
670   /* close the SSL stuff before we close any sockets since they will/may
671      write to the sockets */
672   Curl_ssl_close(conn, FIRSTSOCKET);
673   Curl_ssl_close(conn, SECONDARYSOCKET);
674
675   /* close possibly still open sockets */
676   if(CURL_SOCKET_BAD != conn->sock[SECONDARYSOCKET])
677     Curl_closesocket(conn, conn->sock[SECONDARYSOCKET]);
678   if(CURL_SOCKET_BAD != conn->sock[FIRSTSOCKET])
679     Curl_closesocket(conn, conn->sock[FIRSTSOCKET]);
680   if(CURL_SOCKET_BAD != conn->tempsock[0])
681     Curl_closesocket(conn, conn->tempsock[0]);
682   if(CURL_SOCKET_BAD != conn->tempsock[1])
683     Curl_closesocket(conn, conn->tempsock[1]);
684
685 #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
686     defined(NTLM_WB_ENABLED)
687   Curl_ntlm_wb_cleanup(conn);
688 #endif
689
690   Curl_safefree(conn->user);
691   Curl_safefree(conn->passwd);
692   Curl_safefree(conn->oauth_bearer);
693   Curl_safefree(conn->options);
694   Curl_safefree(conn->http_proxy.user);
695   Curl_safefree(conn->socks_proxy.user);
696   Curl_safefree(conn->http_proxy.passwd);
697   Curl_safefree(conn->socks_proxy.passwd);
698   Curl_safefree(conn->allocptr.proxyuserpwd);
699   Curl_safefree(conn->allocptr.uagent);
700   Curl_safefree(conn->allocptr.userpwd);
701   Curl_safefree(conn->allocptr.accept_encoding);
702   Curl_safefree(conn->allocptr.te);
703   Curl_safefree(conn->allocptr.rangeline);
704   Curl_safefree(conn->allocptr.ref);
705   Curl_safefree(conn->allocptr.host);
706   Curl_safefree(conn->allocptr.cookiehost);
707   Curl_safefree(conn->allocptr.rtsp_transport);
708   Curl_safefree(conn->trailer);
709   Curl_safefree(conn->host.rawalloc); /* host name buffer */
710   Curl_safefree(conn->conn_to_host.rawalloc); /* host name buffer */
711   Curl_safefree(conn->secondaryhostname);
712   Curl_safefree(conn->http_proxy.host.rawalloc); /* http proxy name buffer */
713   Curl_safefree(conn->socks_proxy.host.rawalloc); /* socks proxy name buffer */
714   Curl_safefree(conn->master_buffer);
715   Curl_safefree(conn->connect_state);
716
717   conn_reset_all_postponed_data(conn);
718
719   Curl_llist_destroy(&conn->send_pipe, NULL);
720   Curl_llist_destroy(&conn->recv_pipe, NULL);
721
722   Curl_safefree(conn->localdev);
723   Curl_free_primary_ssl_config(&conn->ssl_config);
724   Curl_free_primary_ssl_config(&conn->proxy_ssl_config);
725
726 #ifdef USE_UNIX_SOCKETS
727   Curl_safefree(conn->unix_domain_socket);
728 #endif
729
730 #ifdef USE_SSL
731   Curl_safefree(conn->ssl_extra);
732 #endif
733   free(conn); /* free all the connection oriented data */
734 }
735
736 /*
737  * Disconnects the given connection. Note the connection may not be the
738  * primary connection, like when freeing room in the connection cache or
739  * killing of a dead old connection.
740  *
741  * A connection needs an easy handle when closing down. We support this passed
742  * in separately since the connection to get closed here is often already
743  * disassociated from an easy handle.
744  *
745  * This function MUST NOT reset state in the Curl_easy struct if that
746  * isn't strictly bound to the life-time of *this* particular connection.
747  *
748  */
749
750 CURLcode Curl_disconnect(struct Curl_easy *data,
751                          struct connectdata *conn, bool dead_connection)
752 {
753   if(!conn)
754     return CURLE_OK; /* this is closed and fine already */
755
756   if(!data) {
757     DEBUGF(infof(data, "DISCONNECT without easy handle, ignoring\n"));
758     return CURLE_OK;
759   }
760
761   /*
762    * If this connection isn't marked to force-close, leave it open if there
763    * are other users of it
764    */
765   if(CONN_INUSE(conn) && !dead_connection) {
766     DEBUGF(infof(data, "Curl_disconnect when inuse: %zu\n", CONN_INUSE(conn)));
767     return CURLE_OK;
768   }
769
770   conn->data = data;
771   if(conn->dns_entry != NULL) {
772     Curl_resolv_unlock(data, conn->dns_entry);
773     conn->dns_entry = NULL;
774   }
775
776   Curl_hostcache_prune(data); /* kill old DNS cache entries */
777
778 #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM)
779   /* Cleanup NTLM connection-related data */
780   Curl_http_ntlm_cleanup(conn);
781 #endif
782
783   if(conn->handler->disconnect)
784     /* This is set if protocol-specific cleanups should be made */
785     conn->handler->disconnect(conn, dead_connection);
786
787     /* unlink ourselves! */
788   infof(data, "Closing connection %ld\n", conn->connection_id);
789   Curl_conncache_remove_conn(conn, TRUE);
790
791   free_fixed_hostname(&conn->host);
792   free_fixed_hostname(&conn->conn_to_host);
793   free_fixed_hostname(&conn->http_proxy.host);
794   free_fixed_hostname(&conn->socks_proxy.host);
795
796   DEBUGASSERT(conn->data == data);
797   /* this assumes that the pointer is still there after the connection was
798      detected from the cache */
799   Curl_ssl_close(conn, FIRSTSOCKET);
800
801   conn_free(conn);
802   return CURLE_OK;
803 }
804
805 /*
806  * This function should return TRUE if the socket is to be assumed to
807  * be dead. Most commonly this happens when the server has closed the
808  * connection due to inactivity.
809  */
810 static bool SocketIsDead(curl_socket_t sock)
811 {
812   int sval;
813   bool ret_val = TRUE;
814
815   sval = SOCKET_READABLE(sock, 0);
816   if(sval == 0)
817     /* timeout */
818     ret_val = FALSE;
819
820   return ret_val;
821 }
822
823 /*
824  * IsPipeliningPossible()
825  *
826  * Return a bitmask with the available pipelining and multiplexing options for
827  * the given requested connection.
828  */
829 static int IsPipeliningPossible(const struct Curl_easy *handle,
830                                 const struct connectdata *conn)
831 {
832   int avail = 0;
833
834   /* If a HTTP protocol and pipelining is enabled */
835   if((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
836      (!conn->bits.protoconnstart || !conn->bits.close)) {
837
838     if(Curl_pipeline_wanted(handle->multi, CURLPIPE_HTTP1) &&
839        (handle->set.httpversion != CURL_HTTP_VERSION_1_0) &&
840        (handle->set.httpreq == HTTPREQ_GET ||
841         handle->set.httpreq == HTTPREQ_HEAD))
842       /* didn't ask for HTTP/1.0 and a GET or HEAD */
843       avail |= CURLPIPE_HTTP1;
844
845     if(Curl_pipeline_wanted(handle->multi, CURLPIPE_MULTIPLEX) &&
846        (handle->set.httpversion >= CURL_HTTP_VERSION_2))
847       /* allows HTTP/2 */
848       avail |= CURLPIPE_MULTIPLEX;
849   }
850   return avail;
851 }
852
853 /* Returns non-zero if a handle was removed */
854 int Curl_removeHandleFromPipeline(struct Curl_easy *handle,
855                                   struct curl_llist *pipeline)
856 {
857   if(pipeline) {
858     struct curl_llist_element *curr;
859
860     curr = pipeline->head;
861     while(curr) {
862       if(curr->ptr == handle) {
863         Curl_llist_remove(pipeline, curr, NULL);
864         return 1; /* we removed a handle */
865       }
866       curr = curr->next;
867     }
868   }
869
870   return 0;
871 }
872
873 #if 0 /* this code is saved here as it is useful for debugging purposes */
874 static void Curl_printPipeline(struct curl_llist *pipeline)
875 {
876   struct curl_llist_element *curr;
877
878   curr = pipeline->head;
879   while(curr) {
880     struct Curl_easy *data = (struct Curl_easy *) curr->ptr;
881     infof(data, "Handle in pipeline: %s\n", data->state.path);
882     curr = curr->next;
883   }
884 }
885 #endif
886
887 static struct Curl_easy* gethandleathead(struct curl_llist *pipeline)
888 {
889   struct curl_llist_element *curr = pipeline->head;
890 #ifdef DEBUGBUILD
891   {
892     struct curl_llist_element *p = pipeline->head;
893     while(p) {
894       struct Curl_easy *e = p->ptr;
895       DEBUGASSERT(GOOD_EASY_HANDLE(e));
896       p = p->next;
897     }
898   }
899 #endif
900   if(curr) {
901     return (struct Curl_easy *) curr->ptr;
902   }
903
904   return NULL;
905 }
906
907 /* remove the specified connection from all (possible) pipelines and related
908    queues */
909 void Curl_getoff_all_pipelines(struct Curl_easy *data,
910                                struct connectdata *conn)
911 {
912   if(!conn->bundle)
913     return;
914   if(conn->bundle->multiuse == BUNDLE_PIPELINING) {
915     bool recv_head = (conn->readchannel_inuse &&
916                       Curl_recvpipe_head(data, conn));
917     bool send_head = (conn->writechannel_inuse &&
918                       Curl_sendpipe_head(data, conn));
919
920     if(Curl_removeHandleFromPipeline(data, &conn->recv_pipe) && recv_head)
921       Curl_pipeline_leave_read(conn);
922     if(Curl_removeHandleFromPipeline(data, &conn->send_pipe) && send_head)
923       Curl_pipeline_leave_write(conn);
924   }
925   else {
926     (void)Curl_removeHandleFromPipeline(data, &conn->recv_pipe);
927     (void)Curl_removeHandleFromPipeline(data, &conn->send_pipe);
928   }
929 }
930
931 static bool
932 proxy_info_matches(const struct proxy_info* data,
933                    const struct proxy_info* needle)
934 {
935   if((data->proxytype == needle->proxytype) &&
936      (data->port == needle->port) &&
937      Curl_safe_strcasecompare(data->host.name, needle->host.name))
938     return TRUE;
939
940   return FALSE;
941 }
942
943 /*
944  * This function checks if the given connection is dead and extracts it from
945  * the connection cache if so.
946  *
947  * When this is called as a Curl_conncache_foreach() callback, the connection
948  * cache lock is held!
949  *
950  * Returns TRUE if the connection was dead and extracted.
951  */
952 static bool extract_if_dead(struct connectdata *conn,
953                             struct Curl_easy *data)
954 {
955   size_t pipeLen = conn->send_pipe.size + conn->recv_pipe.size;
956   if(!pipeLen && !CONN_INUSE(conn)) {
957     /* The check for a dead socket makes sense only if there are no
958        handles in pipeline and the connection isn't already marked in
959        use */
960     bool dead;
961
962     conn->data = data;
963     if(conn->handler->connection_check) {
964       /* The protocol has a special method for checking the state of the
965          connection. Use it to check if the connection is dead. */
966       unsigned int state;
967
968       state = conn->handler->connection_check(conn, CONNCHECK_ISDEAD);
969       dead = (state & CONNRESULT_DEAD);
970     }
971     else {
972       /* Use the general method for determining the death of a connection */
973       dead = SocketIsDead(conn->sock[FIRSTSOCKET]);
974     }
975
976     if(dead) {
977       infof(data, "Connection %ld seems to be dead!\n", conn->connection_id);
978       Curl_conncache_remove_conn(conn, FALSE);
979       conn->data = NULL; /* detach */
980       return TRUE;
981     }
982   }
983   return FALSE;
984 }
985
986 struct prunedead {
987   struct Curl_easy *data;
988   struct connectdata *extracted;
989 };
990
991 /*
992  * Wrapper to use extract_if_dead() function in Curl_conncache_foreach()
993  *
994  */
995 static int call_extract_if_dead(struct connectdata *conn, void *param)
996 {
997   struct prunedead *p = (struct prunedead *)param;
998   if(extract_if_dead(conn, p->data)) {
999     /* stop the iteration here, pass back the connection that was extracted */
1000     p->extracted = conn;
1001     return 1;
1002   }
1003   return 0; /* continue iteration */
1004 }
1005
1006 /*
1007  * This function scans the connection cache for half-open/dead connections,
1008  * closes and removes them.
1009  * The cleanup is done at most once per second.
1010  */
1011 static void prune_dead_connections(struct Curl_easy *data)
1012 {
1013   struct curltime now = Curl_now();
1014   time_t elapsed = Curl_timediff(now, data->state.conn_cache->last_cleanup);
1015
1016   if(elapsed >= 1000L) {
1017     struct prunedead prune;
1018     prune.data = data;
1019     prune.extracted = NULL;
1020     while(Curl_conncache_foreach(data, data->state.conn_cache, &prune,
1021                                  call_extract_if_dead)) {
1022       /* disconnect it */
1023       (void)Curl_disconnect(data, prune.extracted, /* dead_connection */TRUE);
1024     }
1025     data->state.conn_cache->last_cleanup = now;
1026   }
1027 }
1028
1029
1030 static size_t max_pipeline_length(struct Curl_multi *multi)
1031 {
1032   return multi ? multi->max_pipeline_length : 0;
1033 }
1034
1035
1036 /*
1037  * Given one filled in connection struct (named needle), this function should
1038  * detect if there already is one that has all the significant details
1039  * exactly the same and thus should be used instead.
1040  *
1041  * If there is a match, this function returns TRUE - and has marked the
1042  * connection as 'in-use'. It must later be called with ConnectionDone() to
1043  * return back to 'idle' (unused) state.
1044  *
1045  * The force_reuse flag is set if the connection must be used, even if
1046  * the pipelining strategy wants to open a new connection instead of reusing.
1047  */
1048 static bool
1049 ConnectionExists(struct Curl_easy *data,
1050                  struct connectdata *needle,
1051                  struct connectdata **usethis,
1052                  bool *force_reuse,
1053                  bool *waitpipe)
1054 {
1055   struct connectdata *check;
1056   struct connectdata *chosen = 0;
1057   bool foundPendingCandidate = FALSE;
1058   int canpipe = IsPipeliningPossible(data, needle);
1059   struct connectbundle *bundle;
1060
1061 #ifdef USE_NTLM
1062   bool wantNTLMhttp = ((data->state.authhost.want &
1063                       (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
1064                       (needle->handler->protocol & PROTO_FAMILY_HTTP));
1065   bool wantProxyNTLMhttp = (needle->bits.proxy_user_passwd &&
1066                            ((data->state.authproxy.want &
1067                            (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
1068                            (needle->handler->protocol & PROTO_FAMILY_HTTP)));
1069 #endif
1070
1071   *force_reuse = FALSE;
1072   *waitpipe = FALSE;
1073
1074   /* We can't pipeline if the site is blacklisted */
1075   if((canpipe & CURLPIPE_HTTP1) &&
1076      Curl_pipeline_site_blacklisted(data, needle))
1077     canpipe &= ~ CURLPIPE_HTTP1;
1078
1079   /* Look up the bundle with all the connections to this particular host.
1080      Locks the connection cache, beware of early returns! */
1081   bundle = Curl_conncache_find_bundle(needle, data->state.conn_cache);
1082   if(bundle) {
1083     /* Max pipe length is zero (unlimited) for multiplexed connections */
1084     size_t max_pipe_len = (bundle->multiuse != BUNDLE_MULTIPLEX)?
1085       max_pipeline_length(data->multi):0;
1086     size_t best_pipe_len = max_pipe_len;
1087     struct curl_llist_element *curr;
1088
1089     infof(data, "Found bundle for host %s: %p [%s]\n",
1090           (needle->bits.conn_to_host ? needle->conn_to_host.name :
1091            needle->host.name), (void *)bundle,
1092           (bundle->multiuse == BUNDLE_PIPELINING ?
1093            "can pipeline" :
1094            (bundle->multiuse == BUNDLE_MULTIPLEX ?
1095             "can multiplex" : "serially")));
1096
1097     /* We can't pipeline if we don't know anything about the server */
1098     if(canpipe) {
1099       if(bundle->multiuse <= BUNDLE_UNKNOWN) {
1100         if((bundle->multiuse == BUNDLE_UNKNOWN) && data->set.pipewait) {
1101           infof(data, "Server doesn't support multi-use yet, wait\n");
1102           *waitpipe = TRUE;
1103           Curl_conncache_unlock(needle);
1104           return FALSE; /* no re-use */
1105         }
1106
1107         infof(data, "Server doesn't support multi-use (yet)\n");
1108         canpipe = 0;
1109       }
1110       if((bundle->multiuse == BUNDLE_PIPELINING) &&
1111          !Curl_pipeline_wanted(data->multi, CURLPIPE_HTTP1)) {
1112         /* not asked for, switch off */
1113         infof(data, "Could pipeline, but not asked to!\n");
1114         canpipe = 0;
1115       }
1116       else if((bundle->multiuse == BUNDLE_MULTIPLEX) &&
1117               !Curl_pipeline_wanted(data->multi, CURLPIPE_MULTIPLEX)) {
1118         infof(data, "Could multiplex, but not asked to!\n");
1119         canpipe = 0;
1120       }
1121     }
1122
1123     curr = bundle->conn_list.head;
1124     while(curr) {
1125       bool match = FALSE;
1126       size_t pipeLen;
1127
1128       /*
1129        * Note that if we use a HTTP proxy in normal mode (no tunneling), we
1130        * check connections to that proxy and not to the actual remote server.
1131        */
1132       check = curr->ptr;
1133       curr = curr->next;
1134
1135       if(extract_if_dead(check, data)) {
1136         /* disconnect it */
1137         (void)Curl_disconnect(data, check, /* dead_connection */TRUE);
1138         continue;
1139       }
1140
1141       pipeLen = check->send_pipe.size + check->recv_pipe.size;
1142
1143       if(canpipe) {
1144         if(check->bits.protoconnstart && check->bits.close)
1145           continue;
1146
1147         if(!check->bits.multiplex) {
1148           /* If not multiplexing, make sure the connection is fine for HTTP/1
1149              pipelining */
1150           struct Curl_easy* sh = gethandleathead(&check->send_pipe);
1151           struct Curl_easy* rh = gethandleathead(&check->recv_pipe);
1152           if(sh) {
1153             if(!(IsPipeliningPossible(sh, check) & CURLPIPE_HTTP1))
1154               continue;
1155           }
1156           else if(rh) {
1157             if(!(IsPipeliningPossible(rh, check) & CURLPIPE_HTTP1))
1158               continue;
1159           }
1160         }
1161       }
1162       else {
1163         if(pipeLen > 0) {
1164           /* can only happen within multi handles, and means that another easy
1165              handle is using this connection */
1166           continue;
1167         }
1168
1169         if(Curl_resolver_asynch()) {
1170           /* ip_addr_str[0] is NUL only if the resolving of the name hasn't
1171              completed yet and until then we don't re-use this connection */
1172           if(!check->ip_addr_str[0]) {
1173             infof(data,
1174                   "Connection #%ld is still name resolving, can't reuse\n",
1175                   check->connection_id);
1176             continue;
1177           }
1178         }
1179
1180         if((check->sock[FIRSTSOCKET] == CURL_SOCKET_BAD) ||
1181            check->bits.close) {
1182           if(!check->bits.close)
1183             foundPendingCandidate = TRUE;
1184           /* Don't pick a connection that hasn't connected yet or that is going
1185              to get closed. */
1186           infof(data, "Connection #%ld isn't open enough, can't reuse\n",
1187                 check->connection_id);
1188 #ifdef DEBUGBUILD
1189           if(check->recv_pipe.size > 0) {
1190             infof(data,
1191                   "BAD! Unconnected #%ld has a non-empty recv pipeline!\n",
1192                   check->connection_id);
1193           }
1194 #endif
1195           continue;
1196         }
1197       }
1198
1199 #ifdef USE_UNIX_SOCKETS
1200       if(needle->unix_domain_socket) {
1201         if(!check->unix_domain_socket)
1202           continue;
1203         if(strcmp(needle->unix_domain_socket, check->unix_domain_socket))
1204           continue;
1205         if(needle->abstract_unix_socket != check->abstract_unix_socket)
1206           continue;
1207       }
1208       else if(check->unix_domain_socket)
1209         continue;
1210 #endif
1211
1212       if((needle->handler->flags&PROTOPT_SSL) !=
1213          (check->handler->flags&PROTOPT_SSL))
1214         /* don't do mixed SSL and non-SSL connections */
1215         if(get_protocol_family(check->handler->protocol) !=
1216            needle->handler->protocol || !check->tls_upgraded)
1217           /* except protocols that have been upgraded via TLS */
1218           continue;
1219
1220       if(needle->bits.httpproxy != check->bits.httpproxy ||
1221          needle->bits.socksproxy != check->bits.socksproxy)
1222         continue;
1223
1224       if(needle->bits.socksproxy && !proxy_info_matches(&needle->socks_proxy,
1225                                                         &check->socks_proxy))
1226         continue;
1227
1228       if(needle->bits.conn_to_host != check->bits.conn_to_host)
1229         /* don't mix connections that use the "connect to host" feature and
1230          * connections that don't use this feature */
1231         continue;
1232
1233       if(needle->bits.conn_to_port != check->bits.conn_to_port)
1234         /* don't mix connections that use the "connect to port" feature and
1235          * connections that don't use this feature */
1236         continue;
1237
1238       if(needle->bits.httpproxy) {
1239         if(!proxy_info_matches(&needle->http_proxy, &check->http_proxy))
1240           continue;
1241
1242         if(needle->bits.tunnel_proxy != check->bits.tunnel_proxy)
1243           continue;
1244
1245         if(needle->http_proxy.proxytype == CURLPROXY_HTTPS) {
1246           /* use https proxy */
1247           if(needle->handler->flags&PROTOPT_SSL) {
1248             /* use double layer ssl */
1249             if(!Curl_ssl_config_matches(&needle->proxy_ssl_config,
1250                                         &check->proxy_ssl_config))
1251               continue;
1252             if(check->proxy_ssl[FIRSTSOCKET].state != ssl_connection_complete)
1253               continue;
1254           }
1255           else {
1256             if(!Curl_ssl_config_matches(&needle->ssl_config,
1257                                         &check->ssl_config))
1258               continue;
1259             if(check->ssl[FIRSTSOCKET].state != ssl_connection_complete)
1260               continue;
1261           }
1262         }
1263       }
1264
1265       if(!canpipe && CONN_INUSE(check))
1266         /* this request can't be pipelined but the checked connection is
1267            already in use so we skip it */
1268         continue;
1269
1270       if(CONN_INUSE(check) && (check->data->multi != needle->data->multi))
1271         /* this could be subject for pipeline/multiplex use, but only
1272            if they belong to the same multi handle */
1273         continue;
1274
1275       if(needle->localdev || needle->localport) {
1276         /* If we are bound to a specific local end (IP+port), we must not
1277            re-use a random other one, although if we didn't ask for a
1278            particular one we can reuse one that was bound.
1279
1280            This comparison is a bit rough and too strict. Since the input
1281            parameters can be specified in numerous ways and still end up the
1282            same it would take a lot of processing to make it really accurate.
1283            Instead, this matching will assume that re-uses of bound connections
1284            will most likely also re-use the exact same binding parameters and
1285            missing out a few edge cases shouldn't hurt anyone very much.
1286         */
1287         if((check->localport != needle->localport) ||
1288            (check->localportrange != needle->localportrange) ||
1289            (needle->localdev &&
1290             (!check->localdev || strcmp(check->localdev, needle->localdev))))
1291           continue;
1292       }
1293
1294       if(!(needle->handler->flags & PROTOPT_CREDSPERREQUEST)) {
1295         /* This protocol requires credentials per connection,
1296            so verify that we're using the same name and password as well */
1297         if(strcmp(needle->user, check->user) ||
1298            strcmp(needle->passwd, check->passwd)) {
1299           /* one of them was different */
1300           continue;
1301         }
1302       }
1303
1304       if(!needle->bits.httpproxy || (needle->handler->flags&PROTOPT_SSL) ||
1305          needle->bits.tunnel_proxy) {
1306         /* The requested connection does not use a HTTP proxy or it uses SSL or
1307            it is a non-SSL protocol tunneled or it is a non-SSL protocol which
1308            is allowed to be upgraded via TLS */
1309
1310         if((strcasecompare(needle->handler->scheme, check->handler->scheme) ||
1311             (get_protocol_family(check->handler->protocol) ==
1312              needle->handler->protocol && check->tls_upgraded)) &&
1313            (!needle->bits.conn_to_host || strcasecompare(
1314             needle->conn_to_host.name, check->conn_to_host.name)) &&
1315            (!needle->bits.conn_to_port ||
1316              needle->conn_to_port == check->conn_to_port) &&
1317            strcasecompare(needle->host.name, check->host.name) &&
1318            needle->remote_port == check->remote_port) {
1319           /* The schemes match or the the protocol family is the same and the
1320              previous connection was TLS upgraded, and the hostname and host
1321              port match */
1322           if(needle->handler->flags & PROTOPT_SSL) {
1323             /* This is a SSL connection so verify that we're using the same
1324                SSL options as well */
1325             if(!Curl_ssl_config_matches(&needle->ssl_config,
1326                                         &check->ssl_config)) {
1327               DEBUGF(infof(data,
1328                            "Connection #%ld has different SSL parameters, "
1329                            "can't reuse\n",
1330                            check->connection_id));
1331               continue;
1332             }
1333             if(check->ssl[FIRSTSOCKET].state != ssl_connection_complete) {
1334               foundPendingCandidate = TRUE;
1335               DEBUGF(infof(data,
1336                            "Connection #%ld has not started SSL connect, "
1337                            "can't reuse\n",
1338                            check->connection_id));
1339               continue;
1340             }
1341           }
1342           match = TRUE;
1343         }
1344       }
1345       else {
1346         /* The requested connection is using the same HTTP proxy in normal
1347            mode (no tunneling) */
1348         match = TRUE;
1349       }
1350
1351       if(match) {
1352 #if defined(USE_NTLM)
1353         /* If we are looking for an HTTP+NTLM connection, check if this is
1354            already authenticating with the right credentials. If not, keep
1355            looking so that we can reuse NTLM connections if
1356            possible. (Especially we must not reuse the same connection if
1357            partway through a handshake!) */
1358         if(wantNTLMhttp) {
1359           if(strcmp(needle->user, check->user) ||
1360              strcmp(needle->passwd, check->passwd))
1361             continue;
1362         }
1363         else if(check->ntlm.state != NTLMSTATE_NONE) {
1364           /* Connection is using NTLM auth but we don't want NTLM */
1365           continue;
1366         }
1367
1368         /* Same for Proxy NTLM authentication */
1369         if(wantProxyNTLMhttp) {
1370           /* Both check->http_proxy.user and check->http_proxy.passwd can be
1371            * NULL */
1372           if(!check->http_proxy.user || !check->http_proxy.passwd)
1373             continue;
1374
1375           if(strcmp(needle->http_proxy.user, check->http_proxy.user) ||
1376              strcmp(needle->http_proxy.passwd, check->http_proxy.passwd))
1377             continue;
1378         }
1379         else if(check->proxyntlm.state != NTLMSTATE_NONE) {
1380           /* Proxy connection is using NTLM auth but we don't want NTLM */
1381           continue;
1382         }
1383
1384         if(wantNTLMhttp || wantProxyNTLMhttp) {
1385           /* Credentials are already checked, we can use this connection */
1386           chosen = check;
1387
1388           if((wantNTLMhttp &&
1389              (check->ntlm.state != NTLMSTATE_NONE)) ||
1390               (wantProxyNTLMhttp &&
1391                (check->proxyntlm.state != NTLMSTATE_NONE))) {
1392             /* We must use this connection, no other */
1393             *force_reuse = TRUE;
1394             break;
1395           }
1396
1397           /* Continue look up for a better connection */
1398           continue;
1399         }
1400 #endif
1401         if(canpipe) {
1402           /* We can pipeline if we want to. Let's continue looking for
1403              the optimal connection to use, i.e the shortest pipe that is not
1404              blacklisted. */
1405
1406           if(pipeLen == 0) {
1407             /* We have the optimal connection. Let's stop looking. */
1408             chosen = check;
1409             break;
1410           }
1411
1412           /* We can't use the connection if the pipe is full */
1413           if(max_pipe_len && (pipeLen >= max_pipe_len)) {
1414             infof(data, "Pipe is full, skip (%zu)\n", pipeLen);
1415             continue;
1416           }
1417 #ifdef USE_NGHTTP2
1418           /* If multiplexed, make sure we don't go over concurrency limit */
1419           if(check->bits.multiplex) {
1420             /* Multiplexed connections can only be HTTP/2 for now */
1421             struct http_conn *httpc = &check->proto.httpc;
1422             if(pipeLen >= httpc->settings.max_concurrent_streams) {
1423               infof(data, "MAX_CONCURRENT_STREAMS reached, skip (%zu)\n",
1424                     pipeLen);
1425               continue;
1426             }
1427           }
1428 #endif
1429           /* We can't use the connection if the pipe is penalized */
1430           if(Curl_pipeline_penalized(data, check)) {
1431             infof(data, "Penalized, skip\n");
1432             continue;
1433           }
1434
1435           if(max_pipe_len) {
1436             if(pipeLen < best_pipe_len) {
1437               /* This connection has a shorter pipe so far. We'll pick this
1438                  and continue searching */
1439               chosen = check;
1440               best_pipe_len = pipeLen;
1441               continue;
1442             }
1443           }
1444           else {
1445             /* When not pipelining (== multiplexed), we have a match here! */
1446             chosen = check;
1447             infof(data, "Multiplexed connection found!\n");
1448             break;
1449           }
1450         }
1451         else {
1452           /* We have found a connection. Let's stop searching. */
1453           chosen = check;
1454           break;
1455         }
1456       }
1457     }
1458   }
1459
1460   if(chosen) {
1461     /* mark it as used before releasing the lock */
1462     chosen->data = data; /* own it! */
1463     Curl_conncache_unlock(needle);
1464     *usethis = chosen;
1465     return TRUE; /* yes, we found one to use! */
1466   }
1467   Curl_conncache_unlock(needle);
1468
1469   if(foundPendingCandidate && data->set.pipewait) {
1470     infof(data,
1471           "Found pending candidate for reuse and CURLOPT_PIPEWAIT is set\n");
1472     *waitpipe = TRUE;
1473   }
1474
1475   return FALSE; /* no matching connecting exists */
1476 }
1477
1478 /* after a TCP connection to the proxy has been verified, this function does
1479    the next magic step.
1480
1481    Note: this function's sub-functions call failf()
1482
1483 */
1484 CURLcode Curl_connected_proxy(struct connectdata *conn, int sockindex)
1485 {
1486   CURLcode result = CURLE_OK;
1487
1488   if(conn->bits.socksproxy) {
1489 #ifndef CURL_DISABLE_PROXY
1490     /* for the secondary socket (FTP), use the "connect to host"
1491      * but ignore the "connect to port" (use the secondary port)
1492      */
1493     const char * const host = conn->bits.httpproxy ?
1494                               conn->http_proxy.host.name :
1495                               conn->bits.conn_to_host ?
1496                               conn->conn_to_host.name :
1497                               sockindex == SECONDARYSOCKET ?
1498                               conn->secondaryhostname : conn->host.name;
1499     const int port = conn->bits.httpproxy ? (int)conn->http_proxy.port :
1500                      sockindex == SECONDARYSOCKET ? conn->secondary_port :
1501                      conn->bits.conn_to_port ? conn->conn_to_port :
1502                      conn->remote_port;
1503     conn->bits.socksproxy_connecting = TRUE;
1504     switch(conn->socks_proxy.proxytype) {
1505     case CURLPROXY_SOCKS5:
1506     case CURLPROXY_SOCKS5_HOSTNAME:
1507       result = Curl_SOCKS5(conn->socks_proxy.user, conn->socks_proxy.passwd,
1508                          host, port, sockindex, conn);
1509       break;
1510
1511     case CURLPROXY_SOCKS4:
1512     case CURLPROXY_SOCKS4A:
1513       result = Curl_SOCKS4(conn->socks_proxy.user, host, port, sockindex,
1514                            conn);
1515       break;
1516
1517     default:
1518       failf(conn->data, "unknown proxytype option given");
1519       result = CURLE_COULDNT_CONNECT;
1520     } /* switch proxytype */
1521     conn->bits.socksproxy_connecting = FALSE;
1522 #else
1523   (void)sockindex;
1524 #endif /* CURL_DISABLE_PROXY */
1525   }
1526
1527   return result;
1528 }
1529
1530 /*
1531  * verboseconnect() displays verbose information after a connect
1532  */
1533 #ifndef CURL_DISABLE_VERBOSE_STRINGS
1534 void Curl_verboseconnect(struct connectdata *conn)
1535 {
1536   if(conn->data->set.verbose)
1537     infof(conn->data, "Connected to %s (%s) port %ld (#%ld)\n",
1538           conn->bits.socksproxy ? conn->socks_proxy.host.dispname :
1539           conn->bits.httpproxy ? conn->http_proxy.host.dispname :
1540           conn->bits.conn_to_host ? conn->conn_to_host.dispname :
1541           conn->host.dispname,
1542           conn->ip_addr_str, conn->port, conn->connection_id);
1543 }
1544 #endif
1545
1546 int Curl_protocol_getsock(struct connectdata *conn,
1547                           curl_socket_t *socks,
1548                           int numsocks)
1549 {
1550   if(conn->handler->proto_getsock)
1551     return conn->handler->proto_getsock(conn, socks, numsocks);
1552   /* Backup getsock logic. Since there is a live socket in use, we must wait
1553      for it or it will be removed from watching when the multi_socket API is
1554      used. */
1555   socks[0] = conn->sock[FIRSTSOCKET];
1556   return GETSOCK_READSOCK(0) | GETSOCK_WRITESOCK(0);
1557 }
1558
1559 int Curl_doing_getsock(struct connectdata *conn,
1560                        curl_socket_t *socks,
1561                        int numsocks)
1562 {
1563   if(conn && conn->handler->doing_getsock)
1564     return conn->handler->doing_getsock(conn, socks, numsocks);
1565   return GETSOCK_BLANK;
1566 }
1567
1568 /*
1569  * We are doing protocol-specific connecting and this is being called over and
1570  * over from the multi interface until the connection phase is done on
1571  * protocol layer.
1572  */
1573
1574 CURLcode Curl_protocol_connecting(struct connectdata *conn,
1575                                   bool *done)
1576 {
1577   CURLcode result = CURLE_OK;
1578
1579   if(conn && conn->handler->connecting) {
1580     *done = FALSE;
1581     result = conn->handler->connecting(conn, done);
1582   }
1583   else
1584     *done = TRUE;
1585
1586   return result;
1587 }
1588
1589 /*
1590  * We are DOING this is being called over and over from the multi interface
1591  * until the DOING phase is done on protocol layer.
1592  */
1593
1594 CURLcode Curl_protocol_doing(struct connectdata *conn, bool *done)
1595 {
1596   CURLcode result = CURLE_OK;
1597
1598   if(conn && conn->handler->doing) {
1599     *done = FALSE;
1600     result = conn->handler->doing(conn, done);
1601   }
1602   else
1603     *done = TRUE;
1604
1605   return result;
1606 }
1607
1608 /*
1609  * We have discovered that the TCP connection has been successful, we can now
1610  * proceed with some action.
1611  *
1612  */
1613 CURLcode Curl_protocol_connect(struct connectdata *conn,
1614                                bool *protocol_done)
1615 {
1616   CURLcode result = CURLE_OK;
1617
1618   *protocol_done = FALSE;
1619
1620   if(conn->bits.tcpconnect[FIRSTSOCKET] && conn->bits.protoconnstart) {
1621     /* We already are connected, get back. This may happen when the connect
1622        worked fine in the first call, like when we connect to a local server
1623        or proxy. Note that we don't know if the protocol is actually done.
1624
1625        Unless this protocol doesn't have any protocol-connect callback, as
1626        then we know we're done. */
1627     if(!conn->handler->connecting)
1628       *protocol_done = TRUE;
1629
1630     return CURLE_OK;
1631   }
1632
1633   if(!conn->bits.protoconnstart) {
1634
1635     result = Curl_proxy_connect(conn, FIRSTSOCKET);
1636     if(result)
1637       return result;
1638
1639     if(CONNECT_FIRSTSOCKET_PROXY_SSL())
1640       /* wait for HTTPS proxy SSL initialization to complete */
1641       return CURLE_OK;
1642
1643     if(conn->bits.tunnel_proxy && conn->bits.httpproxy &&
1644        Curl_connect_ongoing(conn))
1645       /* when using an HTTP tunnel proxy, await complete tunnel establishment
1646          before proceeding further. Return CURLE_OK so we'll be called again */
1647       return CURLE_OK;
1648
1649     if(conn->handler->connect_it) {
1650       /* is there a protocol-specific connect() procedure? */
1651
1652       /* Call the protocol-specific connect function */
1653       result = conn->handler->connect_it(conn, protocol_done);
1654     }
1655     else
1656       *protocol_done = TRUE;
1657
1658     /* it has started, possibly even completed but that knowledge isn't stored
1659        in this bit! */
1660     if(!result)
1661       conn->bits.protoconnstart = TRUE;
1662   }
1663
1664   return result; /* pass back status */
1665 }
1666
1667 /*
1668  * Helpers for IDNA conversions.
1669  */
1670 static bool is_ASCII_name(const char *hostname)
1671 {
1672   const unsigned char *ch = (const unsigned char *)hostname;
1673
1674   while(*ch) {
1675     if(*ch++ & 0x80)
1676       return FALSE;
1677   }
1678   return TRUE;
1679 }
1680
1681 /*
1682  * Perform any necessary IDN conversion of hostname
1683  */
1684 static CURLcode fix_hostname(struct connectdata *conn, struct hostname *host)
1685 {
1686   size_t len;
1687   struct Curl_easy *data = conn->data;
1688
1689 #ifndef USE_LIBIDN2
1690   (void)data;
1691   (void)conn;
1692 #elif defined(CURL_DISABLE_VERBOSE_STRINGS)
1693   (void)conn;
1694 #endif
1695
1696   /* set the name we use to display the host name */
1697   host->dispname = host->name;
1698
1699   len = strlen(host->name);
1700   if(len && (host->name[len-1] == '.'))
1701     /* strip off a single trailing dot if present, primarily for SNI but
1702        there's no use for it */
1703     host->name[len-1] = 0;
1704
1705   /* Check name for non-ASCII and convert hostname to ACE form if we can */
1706   if(!is_ASCII_name(host->name)) {
1707 #ifdef USE_LIBIDN2
1708     if(idn2_check_version(IDN2_VERSION)) {
1709       char *ace_hostname = NULL;
1710 #if IDN2_VERSION_NUMBER >= 0x00140000
1711       /* IDN2_NFC_INPUT: Normalize input string using normalization form C.
1712          IDN2_NONTRANSITIONAL: Perform Unicode TR46 non-transitional
1713          processing. */
1714       int flags = IDN2_NFC_INPUT | IDN2_NONTRANSITIONAL;
1715 #else
1716       int flags = IDN2_NFC_INPUT;
1717 #endif
1718       int rc = idn2_lookup_ul((const char *)host->name, &ace_hostname, flags);
1719       if(rc == IDN2_OK) {
1720         host->encalloc = (char *)ace_hostname;
1721         /* change the name pointer to point to the encoded hostname */
1722         host->name = host->encalloc;
1723       }
1724       else {
1725         failf(data, "Failed to convert %s to ACE; %s\n", host->name,
1726               idn2_strerror(rc));
1727         return CURLE_URL_MALFORMAT;
1728       }
1729     }
1730 #elif defined(USE_WIN32_IDN)
1731     char *ace_hostname = NULL;
1732
1733     if(curl_win32_idn_to_ascii(host->name, &ace_hostname)) {
1734       host->encalloc = ace_hostname;
1735       /* change the name pointer to point to the encoded hostname */
1736       host->name = host->encalloc;
1737     }
1738     else {
1739       failf(data, "Failed to convert %s to ACE;\n", host->name);
1740       return CURLE_URL_MALFORMAT;
1741     }
1742 #else
1743     infof(data, "IDN support not present, can't parse Unicode domains\n");
1744 #endif
1745   }
1746   {
1747     char *hostp;
1748     for(hostp = host->name; *hostp; hostp++) {
1749       if(*hostp <= 32) {
1750         failf(data, "Host name '%s' contains bad letter", host->name);
1751         return CURLE_URL_MALFORMAT;
1752       }
1753     }
1754   }
1755   return CURLE_OK;
1756 }
1757
1758 /*
1759  * Frees data allocated by fix_hostname()
1760  */
1761 static void free_fixed_hostname(struct hostname *host)
1762 {
1763 #if defined(USE_LIBIDN2)
1764   if(host->encalloc) {
1765     idn2_free(host->encalloc); /* must be freed with idn2_free() since this was
1766                                  allocated by libidn */
1767     host->encalloc = NULL;
1768   }
1769 #elif defined(USE_WIN32_IDN)
1770   free(host->encalloc); /* must be freed with free() since this was
1771                            allocated by curl_win32_idn_to_ascii */
1772   host->encalloc = NULL;
1773 #else
1774   (void)host;
1775 #endif
1776 }
1777
1778 static void llist_dtor(void *user, void *element)
1779 {
1780   (void)user;
1781   (void)element;
1782   /* Do nothing */
1783 }
1784
1785 /*
1786  * Allocate and initialize a new connectdata object.
1787  */
1788 static struct connectdata *allocate_conn(struct Curl_easy *data)
1789 {
1790   struct connectdata *conn = calloc(1, sizeof(struct connectdata));
1791   if(!conn)
1792     return NULL;
1793
1794 #ifdef USE_SSL
1795   /* The SSL backend-specific data (ssl_backend_data) objects are allocated as
1796      a separate array to ensure suitable alignment.
1797      Note that these backend pointers can be swapped by vtls (eg ssl backend
1798      data becomes proxy backend data). */
1799   {
1800     size_t sslsize = Curl_ssl->sizeof_ssl_backend_data;
1801     char *ssl = calloc(4, sslsize);
1802     if(!ssl) {
1803       free(conn);
1804       return NULL;
1805     }
1806     conn->ssl_extra = ssl;
1807     conn->ssl[0].backend = (void *)ssl;
1808     conn->ssl[1].backend = (void *)(ssl + sslsize);
1809     conn->proxy_ssl[0].backend = (void *)(ssl + 2 * sslsize);
1810     conn->proxy_ssl[1].backend = (void *)(ssl + 3 * sslsize);
1811   }
1812 #endif
1813
1814   conn->handler = &Curl_handler_dummy;  /* Be sure we have a handler defined
1815                                            already from start to avoid NULL
1816                                            situations and checks */
1817
1818   /* and we setup a few fields in case we end up actually using this struct */
1819
1820   conn->sock[FIRSTSOCKET] = CURL_SOCKET_BAD;     /* no file descriptor */
1821   conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
1822   conn->tempsock[0] = CURL_SOCKET_BAD; /* no file descriptor */
1823   conn->tempsock[1] = CURL_SOCKET_BAD; /* no file descriptor */
1824   conn->connection_id = -1;    /* no ID */
1825   conn->port = -1; /* unknown at this point */
1826   conn->remote_port = -1; /* unknown at this point */
1827 #if defined(USE_RECV_BEFORE_SEND_WORKAROUND) && defined(DEBUGBUILD)
1828   conn->postponed[0].bindsock = CURL_SOCKET_BAD; /* no file descriptor */
1829   conn->postponed[1].bindsock = CURL_SOCKET_BAD; /* no file descriptor */
1830 #endif /* USE_RECV_BEFORE_SEND_WORKAROUND && DEBUGBUILD */
1831
1832   /* Default protocol-independent behavior doesn't support persistent
1833      connections, so we set this to force-close. Protocols that support
1834      this need to set this to FALSE in their "curl_do" functions. */
1835   connclose(conn, "Default to force-close");
1836
1837   /* Store creation time to help future close decision making */
1838   conn->created = Curl_now();
1839
1840   /* Store current time to give a baseline to keepalive connection times. */
1841   conn->keepalive = Curl_now();
1842
1843   /* Store off the configured connection upkeep time. */
1844   conn->upkeep_interval_ms = data->set.upkeep_interval_ms;
1845
1846   conn->data = data; /* Setup the association between this connection
1847                         and the Curl_easy */
1848
1849   conn->http_proxy.proxytype = data->set.proxytype;
1850   conn->socks_proxy.proxytype = CURLPROXY_SOCKS4;
1851
1852 #ifdef CURL_DISABLE_PROXY
1853
1854   conn->bits.proxy = FALSE;
1855   conn->bits.httpproxy = FALSE;
1856   conn->bits.socksproxy = FALSE;
1857   conn->bits.proxy_user_passwd = FALSE;
1858   conn->bits.tunnel_proxy = FALSE;
1859
1860 #else /* CURL_DISABLE_PROXY */
1861
1862   /* note that these two proxy bits are now just on what looks to be
1863      requested, they may be altered down the road */
1864   conn->bits.proxy = (data->set.str[STRING_PROXY] &&
1865                       *data->set.str[STRING_PROXY]) ? TRUE : FALSE;
1866   conn->bits.httpproxy = (conn->bits.proxy &&
1867                           (conn->http_proxy.proxytype == CURLPROXY_HTTP ||
1868                            conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0 ||
1869                            conn->http_proxy.proxytype == CURLPROXY_HTTPS)) ?
1870                            TRUE : FALSE;
1871   conn->bits.socksproxy = (conn->bits.proxy &&
1872                            !conn->bits.httpproxy) ? TRUE : FALSE;
1873
1874   if(data->set.str[STRING_PRE_PROXY] && *data->set.str[STRING_PRE_PROXY]) {
1875     conn->bits.proxy = TRUE;
1876     conn->bits.socksproxy = TRUE;
1877   }
1878
1879   conn->bits.proxy_user_passwd =
1880     (data->set.str[STRING_PROXYUSERNAME]) ? TRUE : FALSE;
1881   conn->bits.tunnel_proxy = data->set.tunnel_thru_httpproxy;
1882
1883 #endif /* CURL_DISABLE_PROXY */
1884
1885   conn->bits.user_passwd = (data->set.str[STRING_USERNAME]) ? TRUE : FALSE;
1886   conn->bits.ftp_use_epsv = data->set.ftp_use_epsv;
1887   conn->bits.ftp_use_eprt = data->set.ftp_use_eprt;
1888
1889   conn->ssl_config.verifystatus = data->set.ssl.primary.verifystatus;
1890   conn->ssl_config.verifypeer = data->set.ssl.primary.verifypeer;
1891   conn->ssl_config.verifyhost = data->set.ssl.primary.verifyhost;
1892   conn->proxy_ssl_config.verifystatus =
1893     data->set.proxy_ssl.primary.verifystatus;
1894   conn->proxy_ssl_config.verifypeer = data->set.proxy_ssl.primary.verifypeer;
1895   conn->proxy_ssl_config.verifyhost = data->set.proxy_ssl.primary.verifyhost;
1896
1897   conn->ip_version = data->set.ipver;
1898
1899 #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
1900     defined(NTLM_WB_ENABLED)
1901   conn->ntlm_auth_hlpr_socket = CURL_SOCKET_BAD;
1902   conn->ntlm_auth_hlpr_pid = 0;
1903   conn->challenge_header = NULL;
1904   conn->response_header = NULL;
1905 #endif
1906
1907   if(Curl_pipeline_wanted(data->multi, CURLPIPE_HTTP1) &&
1908      !conn->master_buffer) {
1909     /* Allocate master_buffer to be used for HTTP/1 pipelining */
1910     conn->master_buffer = calloc(MASTERBUF_SIZE, sizeof(char));
1911     if(!conn->master_buffer)
1912       goto error;
1913   }
1914
1915   /* Initialize the pipeline lists */
1916   Curl_llist_init(&conn->send_pipe, (curl_llist_dtor) llist_dtor);
1917   Curl_llist_init(&conn->recv_pipe, (curl_llist_dtor) llist_dtor);
1918
1919 #ifdef HAVE_GSSAPI
1920   conn->data_prot = PROT_CLEAR;
1921 #endif
1922
1923   /* Store the local bind parameters that will be used for this connection */
1924   if(data->set.str[STRING_DEVICE]) {
1925     conn->localdev = strdup(data->set.str[STRING_DEVICE]);
1926     if(!conn->localdev)
1927       goto error;
1928   }
1929   conn->localportrange = data->set.localportrange;
1930   conn->localport = data->set.localport;
1931
1932   /* the close socket stuff needs to be copied to the connection struct as
1933      it may live on without (this specific) Curl_easy */
1934   conn->fclosesocket = data->set.fclosesocket;
1935   conn->closesocket_client = data->set.closesocket_client;
1936
1937   return conn;
1938   error:
1939
1940   Curl_llist_destroy(&conn->send_pipe, NULL);
1941   Curl_llist_destroy(&conn->recv_pipe, NULL);
1942
1943   free(conn->master_buffer);
1944   free(conn->localdev);
1945 #ifdef USE_SSL
1946   free(conn->ssl_extra);
1947 #endif
1948   free(conn);
1949   return NULL;
1950 }
1951
1952 /* returns the handler if the given scheme is built-in */
1953 const struct Curl_handler *Curl_builtin_scheme(const char *scheme)
1954 {
1955   const struct Curl_handler * const *pp;
1956   const struct Curl_handler *p;
1957   /* Scan protocol handler table and match against 'scheme'. The handler may
1958      be changed later when the protocol specific setup function is called. */
1959   for(pp = protocols; (p = *pp) != NULL; pp++)
1960     if(strcasecompare(p->scheme, scheme))
1961       /* Protocol found in table. Check if allowed */
1962       return p;
1963   return NULL; /* not found */
1964 }
1965
1966
1967 static CURLcode findprotocol(struct Curl_easy *data,
1968                              struct connectdata *conn,
1969                              const char *protostr)
1970 {
1971   const struct Curl_handler *p = Curl_builtin_scheme(protostr);
1972
1973   if(p && /* Protocol found in table. Check if allowed */
1974      (data->set.allowed_protocols & p->protocol)) {
1975
1976     /* it is allowed for "normal" request, now do an extra check if this is
1977        the result of a redirect */
1978     if(data->state.this_is_a_follow &&
1979        !(data->set.redir_protocols & p->protocol))
1980       /* nope, get out */
1981       ;
1982     else {
1983       /* Perform setup complement if some. */
1984       conn->handler = conn->given = p;
1985
1986       /* 'port' and 'remote_port' are set in setup_connection_internals() */
1987       return CURLE_OK;
1988     }
1989   }
1990
1991   /* The protocol was not found in the table, but we don't have to assign it
1992      to anything since it is already assigned to a dummy-struct in the
1993      create_conn() function when the connectdata struct is allocated. */
1994   failf(data, "Protocol \"%s\" not supported or disabled in " LIBCURL_NAME,
1995         protostr);
1996
1997   return CURLE_UNSUPPORTED_PROTOCOL;
1998 }
1999
2000
2001 CURLcode Curl_uc_to_curlcode(CURLUcode uc)
2002 {
2003   switch(uc) {
2004   default:
2005     return CURLE_URL_MALFORMAT;
2006   case CURLUE_UNSUPPORTED_SCHEME:
2007     return CURLE_UNSUPPORTED_PROTOCOL;
2008   case CURLUE_OUT_OF_MEMORY:
2009     return CURLE_OUT_OF_MEMORY;
2010   case CURLUE_USER_NOT_ALLOWED:
2011     return CURLE_LOGIN_DENIED;
2012   }
2013 }
2014
2015 /*
2016  * Parse URL and fill in the relevant members of the connection struct.
2017  */
2018 static CURLcode parseurlandfillconn(struct Curl_easy *data,
2019                                     struct connectdata *conn)
2020 {
2021   CURLcode result;
2022   CURLU *uh;
2023   CURLUcode uc;
2024   char *hostname;
2025
2026   Curl_up_free(data); /* cleanup previous leftovers first */
2027
2028   /* parse the URL */
2029   uh = data->state.uh = curl_url();
2030   if(!uh)
2031     return CURLE_OUT_OF_MEMORY;
2032
2033   if(data->set.str[STRING_DEFAULT_PROTOCOL] &&
2034      !Curl_is_absolute_url(data->change.url, NULL, MAX_SCHEME_LEN)) {
2035     char *url;
2036     if(data->change.url_alloc)
2037       free(data->change.url);
2038     url = aprintf("%s://%s", data->set.str[STRING_DEFAULT_PROTOCOL],
2039                   data->change.url);
2040     if(!url)
2041       return CURLE_OUT_OF_MEMORY;
2042     data->change.url = url;
2043     data->change.url_alloc = TRUE;
2044   }
2045
2046   uc = curl_url_set(uh, CURLUPART_URL, data->change.url,
2047                     CURLU_GUESS_SCHEME |
2048                     CURLU_NON_SUPPORT_SCHEME |
2049                     (data->set.disallow_username_in_url ?
2050                      CURLU_DISALLOW_USER : 0) |
2051                     (data->set.path_as_is ? CURLU_PATH_AS_IS : 0));
2052   if(uc)
2053     return Curl_uc_to_curlcode(uc);
2054
2055   uc = curl_url_get(uh, CURLUPART_SCHEME, &data->state.up.scheme, 0);
2056   if(uc)
2057     return Curl_uc_to_curlcode(uc);
2058
2059   result = findprotocol(data, conn, data->state.up.scheme);
2060   if(result)
2061     return result;
2062
2063   uc = curl_url_get(uh, CURLUPART_USER, &data->state.up.user,
2064                     CURLU_URLDECODE);
2065   if(!uc) {
2066     conn->user = strdup(data->state.up.user);
2067     if(!conn->user)
2068       return CURLE_OUT_OF_MEMORY;
2069     conn->bits.user_passwd = TRUE;
2070   }
2071   else if(uc != CURLUE_NO_USER)
2072     return Curl_uc_to_curlcode(uc);
2073
2074   uc = curl_url_get(uh, CURLUPART_PASSWORD, &data->state.up.password,
2075                     CURLU_URLDECODE);
2076   if(!uc) {
2077     conn->passwd = strdup(data->state.up.password);
2078     if(!conn->passwd)
2079       return CURLE_OUT_OF_MEMORY;
2080     conn->bits.user_passwd = TRUE;
2081   }
2082   else if(uc != CURLUE_NO_PASSWORD)
2083     return Curl_uc_to_curlcode(uc);
2084
2085   uc = curl_url_get(uh, CURLUPART_OPTIONS, &data->state.up.options,
2086                     CURLU_URLDECODE);
2087   if(!uc) {
2088     conn->options = strdup(data->state.up.options);
2089     if(!conn->options)
2090       return CURLE_OUT_OF_MEMORY;
2091   }
2092   else if(uc != CURLUE_NO_OPTIONS)
2093     return Curl_uc_to_curlcode(uc);
2094
2095   uc = curl_url_get(uh, CURLUPART_HOST, &data->state.up.hostname, 0);
2096   if(uc) {
2097     if(!strcasecompare("file", data->state.up.scheme))
2098       return CURLE_OUT_OF_MEMORY;
2099   }
2100
2101   uc = curl_url_get(uh, CURLUPART_PATH, &data->state.up.path, 0);
2102   if(uc)
2103     return Curl_uc_to_curlcode(uc);
2104
2105   uc = curl_url_get(uh, CURLUPART_PORT, &data->state.up.port,
2106                     CURLU_DEFAULT_PORT);
2107   if(uc) {
2108     if(!strcasecompare("file", data->state.up.scheme))
2109       return CURLE_OUT_OF_MEMORY;
2110   }
2111   else {
2112     unsigned long port = strtoul(data->state.up.port, NULL, 10);
2113     conn->remote_port = curlx_ultous(port);
2114   }
2115
2116   (void)curl_url_get(uh, CURLUPART_QUERY, &data->state.up.query, 0);
2117
2118   hostname = data->state.up.hostname;
2119   if(!hostname)
2120     /* this is for file:// transfers, get a dummy made */
2121     hostname = (char *)"";
2122
2123   if(hostname[0] == '[') {
2124     /* This looks like an IPv6 address literal.  See if there is an address
2125        scope. */
2126     char *percent = strchr(++hostname, '%');
2127     conn->bits.ipv6_ip = TRUE;
2128     if(percent) {
2129       unsigned int identifier_offset = 3;
2130       char *endp;
2131       unsigned long scope;
2132       if(strncmp("%25", percent, 3) != 0) {
2133         infof(data,
2134               "Please URL encode %% as %%25, see RFC 6874.\n");
2135         identifier_offset = 1;
2136       }
2137       scope = strtoul(percent + identifier_offset, &endp, 10);
2138       if(*endp == ']') {
2139         /* The address scope was well formed.  Knock it out of the
2140            hostname. */
2141         memmove(percent, endp, strlen(endp) + 1);
2142         conn->scope_id = (unsigned int)scope;
2143       }
2144       else {
2145         /* Zone identifier is not numeric */
2146 #if defined(HAVE_NET_IF_H) && defined(IFNAMSIZ) && defined(HAVE_IF_NAMETOINDEX)
2147         char ifname[IFNAMSIZ + 2];
2148         char *square_bracket;
2149         unsigned int scopeidx = 0;
2150         strncpy(ifname, percent + identifier_offset, IFNAMSIZ + 2);
2151         /* Ensure nullbyte termination */
2152         ifname[IFNAMSIZ + 1] = '\0';
2153         square_bracket = strchr(ifname, ']');
2154         if(square_bracket) {
2155           /* Remove ']' */
2156           *square_bracket = '\0';
2157           scopeidx = if_nametoindex(ifname);
2158           if(scopeidx == 0) {
2159             infof(data, "Invalid network interface: %s; %s\n", ifname,
2160                   strerror(errno));
2161           }
2162         }
2163         if(scopeidx > 0) {
2164           char *p = percent + identifier_offset + strlen(ifname);
2165
2166           /* Remove zone identifier from hostname */
2167           memmove(percent, p, strlen(p) + 1);
2168           conn->scope_id = scopeidx;
2169         }
2170         else
2171 #endif /* HAVE_NET_IF_H && IFNAMSIZ */
2172           infof(data, "Invalid IPv6 address format\n");
2173       }
2174     }
2175     percent = strchr(hostname, ']');
2176     if(percent)
2177       /* terminate IPv6 numerical at end bracket */
2178       *percent = 0;
2179   }
2180
2181   /* make sure the connect struct gets its own copy of the host name */
2182   conn->host.rawalloc = strdup(hostname);
2183   if(!conn->host.rawalloc)
2184     return CURLE_OUT_OF_MEMORY;
2185   conn->host.name = conn->host.rawalloc;
2186
2187   if(data->set.scope_id)
2188     /* Override any scope that was set above.  */
2189     conn->scope_id = data->set.scope_id;
2190
2191   return CURLE_OK;
2192 }
2193
2194 /*
2195  * If we're doing a resumed transfer, we need to setup our stuff
2196  * properly.
2197  */
2198 static CURLcode setup_range(struct Curl_easy *data)
2199 {
2200   struct UrlState *s = &data->state;
2201   s->resume_from = data->set.set_resume_from;
2202   if(s->resume_from || data->set.str[STRING_SET_RANGE]) {
2203     if(s->rangestringalloc)
2204       free(s->range);
2205
2206     if(s->resume_from)
2207       s->range = aprintf("%" CURL_FORMAT_CURL_OFF_T "-", s->resume_from);
2208     else
2209       s->range = strdup(data->set.str[STRING_SET_RANGE]);
2210
2211     s->rangestringalloc = (s->range) ? TRUE : FALSE;
2212
2213     if(!s->range)
2214       return CURLE_OUT_OF_MEMORY;
2215
2216     /* tell ourselves to fetch this range */
2217     s->use_range = TRUE;        /* enable range download */
2218   }
2219   else
2220     s->use_range = FALSE; /* disable range download */
2221
2222   return CURLE_OK;
2223 }
2224
2225
2226 /*
2227  * setup_connection_internals() -
2228  *
2229  * Setup connection internals specific to the requested protocol in the
2230  * Curl_easy. This is inited and setup before the connection is made but
2231  * is about the particular protocol that is to be used.
2232  *
2233  * This MUST get called after proxy magic has been figured out.
2234  */
2235 static CURLcode setup_connection_internals(struct connectdata *conn)
2236 {
2237   const struct Curl_handler * p;
2238   CURLcode result;
2239   conn->socktype = SOCK_STREAM; /* most of them are TCP streams */
2240
2241   /* Perform setup complement if some. */
2242   p = conn->handler;
2243
2244   if(p->setup_connection) {
2245     result = (*p->setup_connection)(conn);
2246
2247     if(result)
2248       return result;
2249
2250     p = conn->handler;              /* May have changed. */
2251   }
2252
2253   if(conn->port < 0)
2254     /* we check for -1 here since if proxy was detected already, this
2255        was very likely already set to the proxy port */
2256     conn->port = p->defport;
2257
2258   return CURLE_OK;
2259 }
2260
2261 /*
2262  * Curl_free_request_state() should free temp data that was allocated in the
2263  * Curl_easy for this single request.
2264  */
2265
2266 void Curl_free_request_state(struct Curl_easy *data)
2267 {
2268   Curl_safefree(data->req.protop);
2269   Curl_safefree(data->req.newurl);
2270 }
2271
2272
2273 #ifndef CURL_DISABLE_PROXY
2274 /****************************************************************
2275 * Checks if the host is in the noproxy list. returns true if it matches
2276 * and therefore the proxy should NOT be used.
2277 ****************************************************************/
2278 static bool check_noproxy(const char *name, const char *no_proxy)
2279 {
2280   /* no_proxy=domain1.dom,host.domain2.dom
2281    *   (a comma-separated list of hosts which should
2282    *   not be proxied, or an asterisk to override
2283    *   all proxy variables)
2284    */
2285   if(no_proxy && no_proxy[0]) {
2286     size_t tok_start;
2287     size_t tok_end;
2288     const char *separator = ", ";
2289     size_t no_proxy_len;
2290     size_t namelen;
2291     char *endptr;
2292     if(strcasecompare("*", no_proxy)) {
2293       return TRUE;
2294     }
2295
2296     /* NO_PROXY was specified and it wasn't just an asterisk */
2297
2298     no_proxy_len = strlen(no_proxy);
2299     if(name[0] == '[') {
2300       /* IPv6 numerical address */
2301       endptr = strchr(name, ']');
2302       if(!endptr)
2303         return FALSE;
2304       name++;
2305       namelen = endptr - name;
2306     }
2307     else
2308       namelen = strlen(name);
2309
2310     for(tok_start = 0; tok_start < no_proxy_len; tok_start = tok_end + 1) {
2311       while(tok_start < no_proxy_len &&
2312             strchr(separator, no_proxy[tok_start]) != NULL) {
2313         /* Look for the beginning of the token. */
2314         ++tok_start;
2315       }
2316
2317       if(tok_start == no_proxy_len)
2318         break; /* It was all trailing separator chars, no more tokens. */
2319
2320       for(tok_end = tok_start; tok_end < no_proxy_len &&
2321             strchr(separator, no_proxy[tok_end]) == NULL; ++tok_end)
2322         /* Look for the end of the token. */
2323         ;
2324
2325       /* To match previous behaviour, where it was necessary to specify
2326        * ".local.com" to prevent matching "notlocal.com", we will leave
2327        * the '.' off.
2328        */
2329       if(no_proxy[tok_start] == '.')
2330         ++tok_start;
2331
2332       if((tok_end - tok_start) <= namelen) {
2333         /* Match the last part of the name to the domain we are checking. */
2334         const char *checkn = name + namelen - (tok_end - tok_start);
2335         if(strncasecompare(no_proxy + tok_start, checkn,
2336                            tok_end - tok_start)) {
2337           if((tok_end - tok_start) == namelen || *(checkn - 1) == '.') {
2338             /* We either have an exact match, or the previous character is a .
2339              * so it is within the same domain, so no proxy for this host.
2340              */
2341             return TRUE;
2342           }
2343         }
2344       } /* if((tok_end - tok_start) <= namelen) */
2345     } /* for(tok_start = 0; tok_start < no_proxy_len;
2346          tok_start = tok_end + 1) */
2347   } /* NO_PROXY was specified and it wasn't just an asterisk */
2348
2349   return FALSE;
2350 }
2351
2352 #ifndef CURL_DISABLE_HTTP
2353 /****************************************************************
2354 * Detect what (if any) proxy to use. Remember that this selects a host
2355 * name and is not limited to HTTP proxies only.
2356 * The returned pointer must be freed by the caller (unless NULL)
2357 ****************************************************************/
2358 static char *detect_proxy(struct connectdata *conn)
2359 {
2360   char *proxy = NULL;
2361
2362   /* If proxy was not specified, we check for default proxy environment
2363    * variables, to enable i.e Lynx compliance:
2364    *
2365    * http_proxy=http://some.server.dom:port/
2366    * https_proxy=http://some.server.dom:port/
2367    * ftp_proxy=http://some.server.dom:port/
2368    * no_proxy=domain1.dom,host.domain2.dom
2369    *   (a comma-separated list of hosts which should
2370    *   not be proxied, or an asterisk to override
2371    *   all proxy variables)
2372    * all_proxy=http://some.server.dom:port/
2373    *   (seems to exist for the CERN www lib. Probably
2374    *   the first to check for.)
2375    *
2376    * For compatibility, the all-uppercase versions of these variables are
2377    * checked if the lowercase versions don't exist.
2378    */
2379   char proxy_env[128];
2380   const char *protop = conn->handler->scheme;
2381   char *envp = proxy_env;
2382   char *prox;
2383
2384   /* Now, build <protocol>_proxy and check for such a one to use */
2385   while(*protop)
2386     *envp++ = (char)tolower((int)*protop++);
2387
2388   /* append _proxy */
2389   strcpy(envp, "_proxy");
2390
2391   /* read the protocol proxy: */
2392   prox = curl_getenv(proxy_env);
2393
2394   /*
2395    * We don't try the uppercase version of HTTP_PROXY because of
2396    * security reasons:
2397    *
2398    * When curl is used in a webserver application
2399    * environment (cgi or php), this environment variable can
2400    * be controlled by the web server user by setting the
2401    * http header 'Proxy:' to some value.
2402    *
2403    * This can cause 'internal' http/ftp requests to be
2404    * arbitrarily redirected by any external attacker.
2405    */
2406   if(!prox && !strcasecompare("http_proxy", proxy_env)) {
2407     /* There was no lowercase variable, try the uppercase version: */
2408     Curl_strntoupper(proxy_env, proxy_env, sizeof(proxy_env));
2409     prox = curl_getenv(proxy_env);
2410   }
2411
2412   envp = proxy_env;
2413   if(prox) {
2414     proxy = prox; /* use this */
2415   }
2416   else {
2417     envp = (char *)"all_proxy";
2418     proxy = curl_getenv(envp); /* default proxy to use */
2419     if(!proxy) {
2420       envp = (char *)"ALL_PROXY";
2421       proxy = curl_getenv(envp);
2422     }
2423   }
2424   if(proxy)
2425     infof(conn->data, "Uses proxy env variable %s == '%s'\n", envp, proxy);
2426
2427   return proxy;
2428 }
2429 #endif /* CURL_DISABLE_HTTP */
2430
2431 /*
2432  * If this is supposed to use a proxy, we need to figure out the proxy
2433  * host name, so that we can re-use an existing connection
2434  * that may exist registered to the same proxy host.
2435  */
2436 static CURLcode parse_proxy(struct Curl_easy *data,
2437                             struct connectdata *conn, char *proxy,
2438                             curl_proxytype proxytype)
2439 {
2440   char *prox_portno;
2441   char *endofprot;
2442
2443   /* We use 'proxyptr' to point to the proxy name from now on... */
2444   char *proxyptr;
2445   char *portptr;
2446   char *atsign;
2447   long port = -1;
2448   char *proxyuser = NULL;
2449   char *proxypasswd = NULL;
2450   bool sockstype;
2451
2452   /* We do the proxy host string parsing here. We want the host name and the
2453    * port name. Accept a protocol:// prefix
2454    */
2455
2456   /* Parse the protocol part if present */
2457   endofprot = strstr(proxy, "://");
2458   if(endofprot) {
2459     proxyptr = endofprot + 3;
2460     if(checkprefix("https", proxy))
2461       proxytype = CURLPROXY_HTTPS;
2462     else if(checkprefix("socks5h", proxy))
2463       proxytype = CURLPROXY_SOCKS5_HOSTNAME;
2464     else if(checkprefix("socks5", proxy))
2465       proxytype = CURLPROXY_SOCKS5;
2466     else if(checkprefix("socks4a", proxy))
2467       proxytype = CURLPROXY_SOCKS4A;
2468     else if(checkprefix("socks4", proxy) || checkprefix("socks", proxy))
2469       proxytype = CURLPROXY_SOCKS4;
2470     else if(checkprefix("http:", proxy))
2471       ; /* leave it as HTTP or HTTP/1.0 */
2472     else {
2473       /* Any other xxx:// reject! */
2474       failf(data, "Unsupported proxy scheme for \'%s\'", proxy);
2475       return CURLE_COULDNT_CONNECT;
2476     }
2477   }
2478   else
2479     proxyptr = proxy; /* No xxx:// head: It's a HTTP proxy */
2480
2481 #ifdef USE_SSL
2482   if(!(Curl_ssl->supports & SSLSUPP_HTTPS_PROXY))
2483 #endif
2484     if(proxytype == CURLPROXY_HTTPS) {
2485       failf(data, "Unsupported proxy \'%s\', libcurl is built without the "
2486                   "HTTPS-proxy support.", proxy);
2487       return CURLE_NOT_BUILT_IN;
2488     }
2489
2490   sockstype = proxytype == CURLPROXY_SOCKS5_HOSTNAME ||
2491               proxytype == CURLPROXY_SOCKS5 ||
2492               proxytype == CURLPROXY_SOCKS4A ||
2493               proxytype == CURLPROXY_SOCKS4;
2494
2495   /* Is there a username and password given in this proxy url? */
2496   atsign = strchr(proxyptr, '@');
2497   if(atsign) {
2498     CURLcode result =
2499       Curl_parse_login_details(proxyptr, atsign - proxyptr,
2500                                &proxyuser, &proxypasswd, NULL);
2501     if(result)
2502       return result;
2503     proxyptr = atsign + 1;
2504   }
2505
2506   /* start scanning for port number at this point */
2507   portptr = proxyptr;
2508
2509   /* detect and extract RFC6874-style IPv6-addresses */
2510   if(*proxyptr == '[') {
2511     char *ptr = ++proxyptr; /* advance beyond the initial bracket */
2512     while(*ptr && (ISXDIGIT(*ptr) || (*ptr == ':') || (*ptr == '.')))
2513       ptr++;
2514     if(*ptr == '%') {
2515       /* There might be a zone identifier */
2516       if(strncmp("%25", ptr, 3))
2517         infof(data, "Please URL encode %% as %%25, see RFC 6874.\n");
2518       ptr++;
2519       /* Allow unreserved characters as defined in RFC 3986 */
2520       while(*ptr && (ISALPHA(*ptr) || ISXDIGIT(*ptr) || (*ptr == '-') ||
2521                      (*ptr == '.') || (*ptr == '_') || (*ptr == '~')))
2522         ptr++;
2523     }
2524     if(*ptr == ']')
2525       /* yeps, it ended nicely with a bracket as well */
2526       *ptr++ = 0;
2527     else
2528       infof(data, "Invalid IPv6 address format\n");
2529     portptr = ptr;
2530     /* Note that if this didn't end with a bracket, we still advanced the
2531      * proxyptr first, but I can't see anything wrong with that as no host
2532      * name nor a numeric can legally start with a bracket.
2533      */
2534   }
2535
2536   /* Get port number off proxy.server.com:1080 */
2537   prox_portno = strchr(portptr, ':');
2538   if(prox_portno) {
2539     char *endp = NULL;
2540
2541     *prox_portno = 0x0; /* cut off number from host name */
2542     prox_portno ++;
2543     /* now set the local port number */
2544     port = strtol(prox_portno, &endp, 10);
2545     if((endp && *endp && (*endp != '/') && (*endp != ' ')) ||
2546        (port < 0) || (port > 65535)) {
2547       /* meant to detect for example invalid IPv6 numerical addresses without
2548          brackets: "2a00:fac0:a000::7:13". Accept a trailing slash only
2549          because we then allow "URL style" with the number followed by a
2550          slash, used in curl test cases already. Space is also an acceptable
2551          terminating symbol. */
2552       infof(data, "No valid port number in proxy string (%s)\n",
2553             prox_portno);
2554     }
2555     else
2556       conn->port = port;
2557   }
2558   else {
2559     if(proxyptr[0]=='/') {
2560       /* If the first character in the proxy string is a slash, fail
2561          immediately. The following code will otherwise clear the string which
2562          will lead to code running as if no proxy was set! */
2563       Curl_safefree(proxyuser);
2564       Curl_safefree(proxypasswd);
2565       return CURLE_COULDNT_RESOLVE_PROXY;
2566     }
2567
2568     /* without a port number after the host name, some people seem to use
2569        a slash so we strip everything from the first slash */
2570     atsign = strchr(proxyptr, '/');
2571     if(atsign)
2572       *atsign = '\0'; /* cut off path part from host name */
2573
2574     if(data->set.proxyport)
2575       /* None given in the proxy string, then get the default one if it is
2576          given */
2577       port = data->set.proxyport;
2578     else {
2579       if(proxytype == CURLPROXY_HTTPS)
2580         port = CURL_DEFAULT_HTTPS_PROXY_PORT;
2581       else
2582         port = CURL_DEFAULT_PROXY_PORT;
2583     }
2584   }
2585
2586   if(*proxyptr) {
2587     struct proxy_info *proxyinfo =
2588       sockstype ? &conn->socks_proxy : &conn->http_proxy;
2589     proxyinfo->proxytype = proxytype;
2590
2591     if(proxyuser) {
2592       /* found user and password, rip them out.  note that we are unescaping
2593          them, as there is otherwise no way to have a username or password
2594          with reserved characters like ':' in them. */
2595       Curl_safefree(proxyinfo->user);
2596       proxyinfo->user = curl_easy_unescape(data, proxyuser, 0, NULL);
2597       Curl_safefree(proxyuser);
2598
2599       if(!proxyinfo->user) {
2600         Curl_safefree(proxypasswd);
2601         return CURLE_OUT_OF_MEMORY;
2602       }
2603
2604       Curl_safefree(proxyinfo->passwd);
2605       if(proxypasswd && strlen(proxypasswd) < MAX_CURL_PASSWORD_LENGTH)
2606         proxyinfo->passwd = curl_easy_unescape(data, proxypasswd, 0, NULL);
2607       else
2608         proxyinfo->passwd = strdup("");
2609       Curl_safefree(proxypasswd);
2610
2611       if(!proxyinfo->passwd)
2612         return CURLE_OUT_OF_MEMORY;
2613
2614       conn->bits.proxy_user_passwd = TRUE; /* enable it */
2615     }
2616
2617     if(port >= 0) {
2618       proxyinfo->port = port;
2619       if(conn->port < 0 || sockstype || !conn->socks_proxy.host.rawalloc)
2620         conn->port = port;
2621     }
2622
2623     /* now, clone the cleaned proxy host name */
2624     Curl_safefree(proxyinfo->host.rawalloc);
2625     proxyinfo->host.rawalloc = strdup(proxyptr);
2626     proxyinfo->host.name = proxyinfo->host.rawalloc;
2627
2628     if(!proxyinfo->host.rawalloc)
2629       return CURLE_OUT_OF_MEMORY;
2630   }
2631
2632   Curl_safefree(proxyuser);
2633   Curl_safefree(proxypasswd);
2634
2635   return CURLE_OK;
2636 }
2637
2638 /*
2639  * Extract the user and password from the authentication string
2640  */
2641 static CURLcode parse_proxy_auth(struct Curl_easy *data,
2642                                  struct connectdata *conn)
2643 {
2644   char proxyuser[MAX_CURL_USER_LENGTH]="";
2645   char proxypasswd[MAX_CURL_PASSWORD_LENGTH]="";
2646   CURLcode result;
2647
2648   if(data->set.str[STRING_PROXYUSERNAME] != NULL) {
2649     strncpy(proxyuser, data->set.str[STRING_PROXYUSERNAME],
2650             MAX_CURL_USER_LENGTH);
2651     proxyuser[MAX_CURL_USER_LENGTH-1] = '\0';   /*To be on safe side*/
2652   }
2653   if(data->set.str[STRING_PROXYPASSWORD] != NULL) {
2654     strncpy(proxypasswd, data->set.str[STRING_PROXYPASSWORD],
2655             MAX_CURL_PASSWORD_LENGTH);
2656     proxypasswd[MAX_CURL_PASSWORD_LENGTH-1] = '\0'; /*To be on safe side*/
2657   }
2658
2659   result = Curl_urldecode(data, proxyuser, 0, &conn->http_proxy.user, NULL,
2660                           FALSE);
2661   if(!result)
2662     result = Curl_urldecode(data, proxypasswd, 0, &conn->http_proxy.passwd,
2663                             NULL, FALSE);
2664   return result;
2665 }
2666
2667 /* create_conn helper to parse and init proxy values. to be called after unix
2668    socket init but before any proxy vars are evaluated. */
2669 static CURLcode create_conn_helper_init_proxy(struct connectdata *conn)
2670 {
2671   char *proxy = NULL;
2672   char *socksproxy = NULL;
2673   char *no_proxy = NULL;
2674   CURLcode result = CURLE_OK;
2675   struct Curl_easy *data = conn->data;
2676
2677   /*************************************************************
2678    * Extract the user and password from the authentication string
2679    *************************************************************/
2680   if(conn->bits.proxy_user_passwd) {
2681     result = parse_proxy_auth(data, conn);
2682     if(result)
2683       goto out;
2684   }
2685
2686   /*************************************************************
2687    * Detect what (if any) proxy to use
2688    *************************************************************/
2689   if(data->set.str[STRING_PROXY]) {
2690     proxy = strdup(data->set.str[STRING_PROXY]);
2691     /* if global proxy is set, this is it */
2692     if(NULL == proxy) {
2693       failf(data, "memory shortage");
2694       result = CURLE_OUT_OF_MEMORY;
2695       goto out;
2696     }
2697   }
2698
2699   if(data->set.str[STRING_PRE_PROXY]) {
2700     socksproxy = strdup(data->set.str[STRING_PRE_PROXY]);
2701     /* if global socks proxy is set, this is it */
2702     if(NULL == socksproxy) {
2703       failf(data, "memory shortage");
2704       result = CURLE_OUT_OF_MEMORY;
2705       goto out;
2706     }
2707   }
2708
2709   if(!data->set.str[STRING_NOPROXY]) {
2710     const char *p = "no_proxy";
2711     no_proxy = curl_getenv(p);
2712     if(!no_proxy) {
2713       p = "NO_PROXY";
2714       no_proxy = curl_getenv(p);
2715     }
2716     if(no_proxy) {
2717       infof(conn->data, "Uses proxy env variable %s == '%s'\n", p, no_proxy);
2718     }
2719   }
2720
2721   if(check_noproxy(conn->host.name, data->set.str[STRING_NOPROXY] ?
2722       data->set.str[STRING_NOPROXY] : no_proxy)) {
2723     Curl_safefree(proxy);
2724     Curl_safefree(socksproxy);
2725   }
2726 #ifndef CURL_DISABLE_HTTP
2727   else if(!proxy && !socksproxy)
2728     /* if the host is not in the noproxy list, detect proxy. */
2729     proxy = detect_proxy(conn);
2730 #endif /* CURL_DISABLE_HTTP */
2731
2732   Curl_safefree(no_proxy);
2733
2734 #ifdef USE_UNIX_SOCKETS
2735   /* For the time being do not mix proxy and unix domain sockets. See #1274 */
2736   if(proxy && conn->unix_domain_socket) {
2737     free(proxy);
2738     proxy = NULL;
2739   }
2740 #endif
2741
2742   if(proxy && (!*proxy || (conn->handler->flags & PROTOPT_NONETWORK))) {
2743     free(proxy);  /* Don't bother with an empty proxy string or if the
2744                      protocol doesn't work with network */
2745     proxy = NULL;
2746   }
2747   if(socksproxy && (!*socksproxy ||
2748                     (conn->handler->flags & PROTOPT_NONETWORK))) {
2749     free(socksproxy);  /* Don't bother with an empty socks proxy string or if
2750                           the protocol doesn't work with network */
2751     socksproxy = NULL;
2752   }
2753
2754   /***********************************************************************
2755    * If this is supposed to use a proxy, we need to figure out the proxy host
2756    * name, proxy type and port number, so that we can re-use an existing
2757    * connection that may exist registered to the same proxy host.
2758    ***********************************************************************/
2759   if(proxy || socksproxy) {
2760     if(proxy) {
2761       result = parse_proxy(data, conn, proxy, conn->http_proxy.proxytype);
2762       Curl_safefree(proxy); /* parse_proxy copies the proxy string */
2763       if(result)
2764         goto out;
2765     }
2766
2767     if(socksproxy) {
2768       result = parse_proxy(data, conn, socksproxy,
2769                            conn->socks_proxy.proxytype);
2770       /* parse_proxy copies the socks proxy string */
2771       Curl_safefree(socksproxy);
2772       if(result)
2773         goto out;
2774     }
2775
2776     if(conn->http_proxy.host.rawalloc) {
2777 #ifdef CURL_DISABLE_HTTP
2778       /* asking for a HTTP proxy is a bit funny when HTTP is disabled... */
2779       result = CURLE_UNSUPPORTED_PROTOCOL;
2780       goto out;
2781 #else
2782       /* force this connection's protocol to become HTTP if compatible */
2783       if(!(conn->handler->protocol & PROTO_FAMILY_HTTP)) {
2784         if((conn->handler->flags & PROTOPT_PROXY_AS_HTTP) &&
2785            !conn->bits.tunnel_proxy)
2786           conn->handler = &Curl_handler_http;
2787         else
2788           /* if not converting to HTTP over the proxy, enforce tunneling */
2789           conn->bits.tunnel_proxy = TRUE;
2790       }
2791       conn->bits.httpproxy = TRUE;
2792 #endif
2793     }
2794     else {
2795       conn->bits.httpproxy = FALSE; /* not a HTTP proxy */
2796       conn->bits.tunnel_proxy = FALSE; /* no tunneling if not HTTP */
2797     }
2798
2799     if(conn->socks_proxy.host.rawalloc) {
2800       if(!conn->http_proxy.host.rawalloc) {
2801         /* once a socks proxy */
2802         if(!conn->socks_proxy.user) {
2803           conn->socks_proxy.user = conn->http_proxy.user;
2804           conn->http_proxy.user = NULL;
2805           Curl_safefree(conn->socks_proxy.passwd);
2806           conn->socks_proxy.passwd = conn->http_proxy.passwd;
2807           conn->http_proxy.passwd = NULL;
2808         }
2809       }
2810       conn->bits.socksproxy = TRUE;
2811     }
2812     else
2813       conn->bits.socksproxy = FALSE; /* not a socks proxy */
2814   }
2815   else {
2816     conn->bits.socksproxy = FALSE;
2817     conn->bits.httpproxy = FALSE;
2818   }
2819   conn->bits.proxy = conn->bits.httpproxy || conn->bits.socksproxy;
2820
2821   if(!conn->bits.proxy) {
2822     /* we aren't using the proxy after all... */
2823     conn->bits.proxy = FALSE;
2824     conn->bits.httpproxy = FALSE;
2825     conn->bits.socksproxy = FALSE;
2826     conn->bits.proxy_user_passwd = FALSE;
2827     conn->bits.tunnel_proxy = FALSE;
2828   }
2829
2830 out:
2831
2832   free(socksproxy);
2833   free(proxy);
2834   return result;
2835 }
2836 #endif /* CURL_DISABLE_PROXY */
2837
2838 /*
2839  * Curl_parse_login_details()
2840  *
2841  * This is used to parse a login string for user name, password and options in
2842  * the following formats:
2843  *
2844  *   user
2845  *   user:password
2846  *   user:password;options
2847  *   user;options
2848  *   user;options:password
2849  *   :password
2850  *   :password;options
2851  *   ;options
2852  *   ;options:password
2853  *
2854  * Parameters:
2855  *
2856  * login    [in]     - The login string.
2857  * len      [in]     - The length of the login string.
2858  * userp    [in/out] - The address where a pointer to newly allocated memory
2859  *                     holding the user will be stored upon completion.
2860  * passwdp  [in/out] - The address where a pointer to newly allocated memory
2861  *                     holding the password will be stored upon completion.
2862  * optionsp [in/out] - The address where a pointer to newly allocated memory
2863  *                     holding the options will be stored upon completion.
2864  *
2865  * Returns CURLE_OK on success.
2866  */
2867 CURLcode Curl_parse_login_details(const char *login, const size_t len,
2868                                   char **userp, char **passwdp,
2869                                   char **optionsp)
2870 {
2871   CURLcode result = CURLE_OK;
2872   char *ubuf = NULL;
2873   char *pbuf = NULL;
2874   char *obuf = NULL;
2875   const char *psep = NULL;
2876   const char *osep = NULL;
2877   size_t ulen;
2878   size_t plen;
2879   size_t olen;
2880
2881   /* Attempt to find the password separator */
2882   if(passwdp) {
2883     psep = strchr(login, ':');
2884
2885     /* Within the constraint of the login string */
2886     if(psep >= login + len)
2887       psep = NULL;
2888   }
2889
2890   /* Attempt to find the options separator */
2891   if(optionsp) {
2892     osep = strchr(login, ';');
2893
2894     /* Within the constraint of the login string */
2895     if(osep >= login + len)
2896       osep = NULL;
2897   }
2898
2899   /* Calculate the portion lengths */
2900   ulen = (psep ?
2901           (size_t)(osep && psep > osep ? osep - login : psep - login) :
2902           (osep ? (size_t)(osep - login) : len));
2903   plen = (psep ?
2904           (osep && osep > psep ? (size_t)(osep - psep) :
2905                                  (size_t)(login + len - psep)) - 1 : 0);
2906   olen = (osep ?
2907           (psep && psep > osep ? (size_t)(psep - osep) :
2908                                  (size_t)(login + len - osep)) - 1 : 0);
2909
2910   /* Allocate the user portion buffer */
2911   if(userp && ulen) {
2912     ubuf = malloc(ulen + 1);
2913     if(!ubuf)
2914       result = CURLE_OUT_OF_MEMORY;
2915   }
2916
2917   /* Allocate the password portion buffer */
2918   if(!result && passwdp && plen) {
2919     pbuf = malloc(plen + 1);
2920     if(!pbuf) {
2921       free(ubuf);
2922       result = CURLE_OUT_OF_MEMORY;
2923     }
2924   }
2925
2926   /* Allocate the options portion buffer */
2927   if(!result && optionsp && olen) {
2928     obuf = malloc(olen + 1);
2929     if(!obuf) {
2930       free(pbuf);
2931       free(ubuf);
2932       result = CURLE_OUT_OF_MEMORY;
2933     }
2934   }
2935
2936   if(!result) {
2937     /* Store the user portion if necessary */
2938     if(ubuf) {
2939       memcpy(ubuf, login, ulen);
2940       ubuf[ulen] = '\0';
2941       Curl_safefree(*userp);
2942       *userp = ubuf;
2943     }
2944
2945     /* Store the password portion if necessary */
2946     if(pbuf) {
2947       memcpy(pbuf, psep + 1, plen);
2948       pbuf[plen] = '\0';
2949       Curl_safefree(*passwdp);
2950       *passwdp = pbuf;
2951     }
2952
2953     /* Store the options portion if necessary */
2954     if(obuf) {
2955       memcpy(obuf, osep + 1, olen);
2956       obuf[olen] = '\0';
2957       Curl_safefree(*optionsp);
2958       *optionsp = obuf;
2959     }
2960   }
2961
2962   return result;
2963 }
2964
2965 /*************************************************************
2966  * Figure out the remote port number and fix it in the URL
2967  *
2968  * No matter if we use a proxy or not, we have to figure out the remote
2969  * port number of various reasons.
2970  *
2971  * The port number embedded in the URL is replaced, if necessary.
2972  *************************************************************/
2973 static CURLcode parse_remote_port(struct Curl_easy *data,
2974                                   struct connectdata *conn)
2975 {
2976
2977   if(data->set.use_port && data->state.allow_port) {
2978     /* if set, we use this instead of the port possibly given in the URL */
2979     char portbuf[16];
2980     CURLUcode uc;
2981     conn->remote_port = (unsigned short)data->set.use_port;
2982     snprintf(portbuf, sizeof(portbuf), "%u", conn->remote_port);
2983     uc = curl_url_set(data->state.uh, CURLUPART_PORT, portbuf, 0);
2984     if(uc)
2985       return CURLE_OUT_OF_MEMORY;
2986   }
2987
2988   return CURLE_OK;
2989 }
2990
2991 /*
2992  * Override the login details from the URL with that in the CURLOPT_USERPWD
2993  * option or a .netrc file, if applicable.
2994  */
2995 static CURLcode override_login(struct Curl_easy *data,
2996                                struct connectdata *conn,
2997                                char **userp, char **passwdp, char **optionsp)
2998 {
2999   bool user_changed = FALSE;
3000   bool passwd_changed = FALSE;
3001   CURLUcode uc;
3002   if(data->set.str[STRING_USERNAME]) {
3003     free(*userp);
3004     *userp = strdup(data->set.str[STRING_USERNAME]);
3005     if(!*userp)
3006       return CURLE_OUT_OF_MEMORY;
3007     conn->bits.user_passwd = TRUE; /* enable user+password */
3008     user_changed = TRUE;
3009   }
3010
3011   if(data->set.str[STRING_PASSWORD]) {
3012     free(*passwdp);
3013     *passwdp = strdup(data->set.str[STRING_PASSWORD]);
3014     if(!*passwdp)
3015       return CURLE_OUT_OF_MEMORY;
3016     conn->bits.user_passwd = TRUE; /* enable user+password */
3017     passwd_changed = TRUE;
3018   }
3019
3020   if(data->set.str[STRING_OPTIONS]) {
3021     free(*optionsp);
3022     *optionsp = strdup(data->set.str[STRING_OPTIONS]);
3023     if(!*optionsp)
3024       return CURLE_OUT_OF_MEMORY;
3025   }
3026
3027   conn->bits.netrc = FALSE;
3028   if(data->set.use_netrc != CURL_NETRC_IGNORED) {
3029     char *nuser = NULL;
3030     char *npasswd = NULL;
3031     int ret;
3032
3033     if(data->set.use_netrc == CURL_NETRC_OPTIONAL)
3034       nuser = *userp; /* to separate otherwise identical machines */
3035
3036     ret = Curl_parsenetrc(conn->host.name,
3037                           &nuser, &npasswd,
3038                           data->set.str[STRING_NETRC_FILE]);
3039     if(ret > 0) {
3040       infof(data, "Couldn't find host %s in the "
3041             DOT_CHAR "netrc file; using defaults\n",
3042             conn->host.name);
3043     }
3044     else if(ret < 0) {
3045       return CURLE_OUT_OF_MEMORY;
3046     }
3047     else {
3048       /* set bits.netrc TRUE to remember that we got the name from a .netrc
3049          file, so that it is safe to use even if we followed a Location: to a
3050          different host or similar. */
3051       conn->bits.netrc = TRUE;
3052       conn->bits.user_passwd = TRUE; /* enable user+password */
3053
3054       if(data->set.use_netrc == CURL_NETRC_OPTIONAL) {
3055         /* prefer credentials outside netrc */
3056         if(nuser && !*userp) {
3057           free(*userp);
3058           *userp = nuser;
3059           user_changed = TRUE;
3060         }
3061         if(npasswd && !*passwdp) {
3062           free(*passwdp);
3063           *passwdp = npasswd;
3064           passwd_changed = TRUE;
3065         }
3066       }
3067       else {
3068         /* prefer netrc credentials */
3069         if(nuser) {
3070           free(*userp);
3071           *userp = nuser;
3072           user_changed = TRUE;
3073         }
3074         if(npasswd) {
3075           free(*passwdp);
3076           *passwdp = npasswd;
3077           passwd_changed = TRUE;
3078         }
3079       }
3080     }
3081   }
3082
3083   /* for updated strings, we update them in the URL */
3084   if(user_changed) {
3085     uc = curl_url_set(data->state.uh, CURLUPART_USER, *userp, 0);
3086     if(uc)
3087       return Curl_uc_to_curlcode(uc);
3088   }
3089   if(passwd_changed) {
3090     uc = curl_url_set(data->state.uh, CURLUPART_PASSWORD, *passwdp, 0);
3091     if(uc)
3092       return Curl_uc_to_curlcode(uc);
3093   }
3094   return CURLE_OK;
3095 }
3096
3097 /*
3098  * Set the login details so they're available in the connection
3099  */
3100 static CURLcode set_login(struct connectdata *conn)
3101 {
3102   CURLcode result = CURLE_OK;
3103   const char *setuser = CURL_DEFAULT_USER;
3104   const char *setpasswd = CURL_DEFAULT_PASSWORD;
3105
3106   /* If our protocol needs a password and we have none, use the defaults */
3107   if((conn->handler->flags & PROTOPT_NEEDSPWD) && !conn->bits.user_passwd)
3108     ;
3109   else {
3110     setuser = "";
3111     setpasswd = "";
3112   }
3113   /* Store the default user */
3114   if(!conn->user) {
3115     conn->user = strdup(setuser);
3116     if(!conn->user)
3117       return CURLE_OUT_OF_MEMORY;
3118   }
3119
3120   /* Store the default password */
3121   if(!conn->passwd) {
3122     conn->passwd = strdup(setpasswd);
3123     if(!conn->passwd)
3124       result = CURLE_OUT_OF_MEMORY;
3125   }
3126
3127   /* if there's a user without password, consider password blank */
3128   if(conn->user && !conn->passwd) {
3129     conn->passwd = strdup("");
3130     if(!conn->passwd)
3131       result = CURLE_OUT_OF_MEMORY;
3132   }
3133
3134   return result;
3135 }
3136
3137 /*
3138  * Parses a "host:port" string to connect to.
3139  * The hostname and the port may be empty; in this case, NULL is returned for
3140  * the hostname and -1 for the port.
3141  */
3142 static CURLcode parse_connect_to_host_port(struct Curl_easy *data,
3143                                            const char *host,
3144                                            char **hostname_result,
3145                                            int *port_result)
3146 {
3147   char *host_dup;
3148   char *hostptr;
3149   char *host_portno;
3150   char *portptr;
3151   int port = -1;
3152
3153 #if defined(CURL_DISABLE_VERBOSE_STRINGS)
3154   (void) data;
3155 #endif
3156
3157   *hostname_result = NULL;
3158   *port_result = -1;
3159
3160   if(!host || !*host)
3161     return CURLE_OK;
3162
3163   host_dup = strdup(host);
3164   if(!host_dup)
3165     return CURLE_OUT_OF_MEMORY;
3166
3167   hostptr = host_dup;
3168
3169   /* start scanning for port number at this point */
3170   portptr = hostptr;
3171
3172   /* detect and extract RFC6874-style IPv6-addresses */
3173   if(*hostptr == '[') {
3174 #ifdef ENABLE_IPV6
3175     char *ptr = ++hostptr; /* advance beyond the initial bracket */
3176     while(*ptr && (ISXDIGIT(*ptr) || (*ptr == ':') || (*ptr == '.')))
3177       ptr++;
3178     if(*ptr == '%') {
3179       /* There might be a zone identifier */
3180       if(strncmp("%25", ptr, 3))
3181         infof(data, "Please URL encode %% as %%25, see RFC 6874.\n");
3182       ptr++;
3183       /* Allow unreserved characters as defined in RFC 3986 */
3184       while(*ptr && (ISALPHA(*ptr) || ISXDIGIT(*ptr) || (*ptr == '-') ||
3185                      (*ptr == '.') || (*ptr == '_') || (*ptr == '~')))
3186         ptr++;
3187     }
3188     if(*ptr == ']')
3189       /* yeps, it ended nicely with a bracket as well */
3190       *ptr++ = '\0';
3191     else
3192       infof(data, "Invalid IPv6 address format\n");
3193     portptr = ptr;
3194     /* Note that if this didn't end with a bracket, we still advanced the
3195      * hostptr first, but I can't see anything wrong with that as no host
3196      * name nor a numeric can legally start with a bracket.
3197      */
3198 #else
3199     failf(data, "Use of IPv6 in *_CONNECT_TO without IPv6 support built-in!");
3200     free(host_dup);
3201     return CURLE_NOT_BUILT_IN;
3202 #endif
3203   }
3204
3205   /* Get port number off server.com:1080 */
3206   host_portno = strchr(portptr, ':');
3207   if(host_portno) {
3208     char *endp = NULL;
3209     *host_portno = '\0'; /* cut off number from host name */
3210     host_portno++;
3211     if(*host_portno) {
3212       long portparse = strtol(host_portno, &endp, 10);
3213       if((endp && *endp) || (portparse < 0) || (portparse > 65535)) {
3214         infof(data, "No valid port number in connect to host string (%s)\n",
3215               host_portno);
3216         hostptr = NULL;
3217         port = -1;
3218       }
3219       else
3220         port = (int)portparse; /* we know it will fit */
3221     }
3222   }
3223
3224   /* now, clone the cleaned host name */
3225   if(hostptr) {
3226     *hostname_result = strdup(hostptr);
3227     if(!*hostname_result) {
3228       free(host_dup);
3229       return CURLE_OUT_OF_MEMORY;
3230     }
3231   }
3232
3233   *port_result = port;
3234
3235   free(host_dup);
3236   return CURLE_OK;
3237 }
3238
3239 /*
3240  * Parses one "connect to" string in the form:
3241  * "HOST:PORT:CONNECT-TO-HOST:CONNECT-TO-PORT".
3242  */
3243 static CURLcode parse_connect_to_string(struct Curl_easy *data,
3244                                         struct connectdata *conn,
3245                                         const char *conn_to_host,
3246                                         char **host_result,
3247                                         int *port_result)
3248 {
3249   CURLcode result = CURLE_OK;
3250   const char *ptr = conn_to_host;
3251   int host_match = FALSE;
3252   int port_match = FALSE;
3253
3254   *host_result = NULL;
3255   *port_result = -1;
3256
3257   if(*ptr == ':') {
3258     /* an empty hostname always matches */
3259     host_match = TRUE;
3260     ptr++;
3261   }
3262   else {
3263     /* check whether the URL's hostname matches */
3264     size_t hostname_to_match_len;
3265     char *hostname_to_match = aprintf("%s%s%s",
3266                                       conn->bits.ipv6_ip ? "[" : "",
3267                                       conn->host.name,
3268                                       conn->bits.ipv6_ip ? "]" : "");
3269     if(!hostname_to_match)
3270       return CURLE_OUT_OF_MEMORY;
3271     hostname_to_match_len = strlen(hostname_to_match);
3272     host_match = strncasecompare(ptr, hostname_to_match,
3273                                  hostname_to_match_len);
3274     free(hostname_to_match);
3275     ptr += hostname_to_match_len;
3276
3277     host_match = host_match && *ptr == ':';
3278     ptr++;
3279   }
3280
3281   if(host_match) {
3282     if(*ptr == ':') {
3283       /* an empty port always matches */
3284       port_match = TRUE;
3285       ptr++;
3286     }
3287     else {
3288       /* check whether the URL's port matches */
3289       char *ptr_next = strchr(ptr, ':');
3290       if(ptr_next) {
3291         char *endp = NULL;
3292         long port_to_match = strtol(ptr, &endp, 10);
3293         if((endp == ptr_next) && (port_to_match == conn->remote_port)) {
3294           port_match = TRUE;
3295           ptr = ptr_next + 1;
3296         }
3297       }
3298     }
3299   }
3300
3301   if(host_match && port_match) {
3302     /* parse the hostname and port to connect to */
3303     result = parse_connect_to_host_port(data, ptr, host_result, port_result);
3304   }
3305
3306   return result;
3307 }
3308
3309 /*
3310  * Processes all strings in the "connect to" slist, and uses the "connect
3311  * to host" and "connect to port" of the first string that matches.
3312  */
3313 static CURLcode parse_connect_to_slist(struct Curl_easy *data,
3314                                        struct connectdata *conn,
3315                                        struct curl_slist *conn_to_host)
3316 {
3317   CURLcode result = CURLE_OK;
3318   char *host = NULL;
3319   int port = -1;
3320
3321   while(conn_to_host && !host && port == -1) {
3322     result = parse_connect_to_string(data, conn, conn_to_host->data,
3323                                      &host, &port);
3324     if(result)
3325       return result;
3326
3327     if(host && *host) {
3328       conn->conn_to_host.rawalloc = host;
3329       conn->conn_to_host.name = host;
3330       conn->bits.conn_to_host = TRUE;
3331
3332       infof(data, "Connecting to hostname: %s\n", host);
3333     }
3334     else {
3335       /* no "connect to host" */
3336       conn->bits.conn_to_host = FALSE;
3337       Curl_safefree(host);
3338     }
3339
3340     if(port >= 0) {
3341       conn->conn_to_port = port;
3342       conn->bits.conn_to_port = TRUE;
3343       infof(data, "Connecting to port: %d\n", port);
3344     }
3345     else {
3346       /* no "connect to port" */
3347       conn->bits.conn_to_port = FALSE;
3348       port = -1;
3349     }
3350
3351     conn_to_host = conn_to_host->next;
3352   }
3353
3354   return result;
3355 }
3356
3357 /*************************************************************
3358  * Resolve the address of the server or proxy
3359  *************************************************************/
3360 static CURLcode resolve_server(struct Curl_easy *data,
3361                                struct connectdata *conn,
3362                                bool *async)
3363 {
3364   CURLcode result = CURLE_OK;
3365   timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
3366
3367   /*************************************************************
3368    * Resolve the name of the server or proxy
3369    *************************************************************/
3370   if(conn->bits.reuse)
3371     /* We're reusing the connection - no need to resolve anything, and
3372        fix_hostname() was called already in create_conn() for the re-use
3373        case. */
3374     *async = FALSE;
3375
3376   else {
3377     /* this is a fresh connect */
3378     int rc;
3379     struct Curl_dns_entry *hostaddr;
3380
3381 #ifdef USE_UNIX_SOCKETS
3382     if(conn->unix_domain_socket) {
3383       /* Unix domain sockets are local. The host gets ignored, just use the
3384        * specified domain socket address. Do not cache "DNS entries". There is
3385        * no DNS involved and we already have the filesystem path available */
3386       const char *path = conn->unix_domain_socket;
3387
3388       hostaddr = calloc(1, sizeof(struct Curl_dns_entry));
3389       if(!hostaddr)
3390         result = CURLE_OUT_OF_MEMORY;
3391       else {
3392         bool longpath = FALSE;
3393         hostaddr->addr = Curl_unix2addr(path, &longpath,
3394                                         conn->abstract_unix_socket);
3395         if(hostaddr->addr)
3396           hostaddr->inuse++;
3397         else {
3398           /* Long paths are not supported for now */
3399           if(longpath) {
3400             failf(data, "Unix socket path too long: '%s'", path);
3401             result = CURLE_COULDNT_RESOLVE_HOST;
3402           }
3403           else
3404             result = CURLE_OUT_OF_MEMORY;
3405           free(hostaddr);
3406           hostaddr = NULL;
3407         }
3408       }
3409     }
3410     else
3411 #endif
3412     if(!conn->bits.proxy) {
3413       struct hostname *connhost;
3414       if(conn->bits.conn_to_host)
3415         connhost = &conn->conn_to_host;
3416       else
3417         connhost = &conn->host;
3418
3419       /* If not connecting via a proxy, extract the port from the URL, if it is
3420        * there, thus overriding any defaults that might have been set above. */
3421       if(conn->bits.conn_to_port)
3422         conn->port = conn->conn_to_port;
3423       else
3424         conn->port = conn->remote_port;
3425
3426       /* Resolve target host right on */
3427       rc = Curl_resolv_timeout(conn, connhost->name, (int)conn->port,
3428                                &hostaddr, timeout_ms);
3429       if(rc == CURLRESOLV_PENDING)
3430         *async = TRUE;
3431
3432       else if(rc == CURLRESOLV_TIMEDOUT)
3433         result = CURLE_OPERATION_TIMEDOUT;
3434
3435       else if(!hostaddr) {
3436         failf(data, "Couldn't resolve host '%s'", connhost->dispname);
3437         result =  CURLE_COULDNT_RESOLVE_HOST;
3438         /* don't return yet, we need to clean up the timeout first */
3439       }
3440     }
3441     else {
3442       /* This is a proxy that hasn't been resolved yet. */
3443
3444       struct hostname * const host = conn->bits.socksproxy ?
3445         &conn->socks_proxy.host : &conn->http_proxy.host;
3446
3447       /* resolve proxy */
3448       rc = Curl_resolv_timeout(conn, host->name, (int)conn->port,
3449                                &hostaddr, timeout_ms);
3450
3451       if(rc == CURLRESOLV_PENDING)
3452         *async = TRUE;
3453
3454       else if(rc == CURLRESOLV_TIMEDOUT)
3455         result = CURLE_OPERATION_TIMEDOUT;
3456
3457       else if(!hostaddr) {
3458         failf(data, "Couldn't resolve proxy '%s'", host->dispname);
3459         result = CURLE_COULDNT_RESOLVE_PROXY;
3460         /* don't return yet, we need to clean up the timeout first */
3461       }
3462     }
3463     DEBUGASSERT(conn->dns_entry == NULL);
3464     conn->dns_entry = hostaddr;
3465   }
3466
3467   return result;
3468 }
3469
3470 /*
3471  * Cleanup the connection just allocated before we can move along and use the
3472  * previously existing one.  All relevant data is copied over and old_conn is
3473  * ready for freeing once this function returns.
3474  */
3475 static void reuse_conn(struct connectdata *old_conn,
3476                        struct connectdata *conn)
3477 {
3478   free_fixed_hostname(&old_conn->http_proxy.host);
3479   free_fixed_hostname(&old_conn->socks_proxy.host);
3480
3481   free(old_conn->http_proxy.host.rawalloc);
3482   free(old_conn->socks_proxy.host.rawalloc);
3483
3484   /* free the SSL config struct from this connection struct as this was
3485      allocated in vain and is targeted for destruction */
3486   Curl_free_primary_ssl_config(&old_conn->ssl_config);
3487   Curl_free_primary_ssl_config(&old_conn->proxy_ssl_config);
3488
3489   conn->data = old_conn->data;
3490
3491   /* get the user+password information from the old_conn struct since it may
3492    * be new for this request even when we re-use an existing connection */
3493   conn->bits.user_passwd = old_conn->bits.user_passwd;
3494   if(conn->bits.user_passwd) {
3495     /* use the new user name and password though */
3496     Curl_safefree(conn->user);
3497     Curl_safefree(conn->passwd);
3498     conn->user = old_conn->user;
3499     conn->passwd = old_conn->passwd;
3500     old_conn->user = NULL;
3501     old_conn->passwd = NULL;
3502   }
3503
3504   conn->bits.proxy_user_passwd = old_conn->bits.proxy_user_passwd;
3505   if(conn->bits.proxy_user_passwd) {
3506     /* use the new proxy user name and proxy password though */
3507     Curl_safefree(conn->http_proxy.user);
3508     Curl_safefree(conn->socks_proxy.user);
3509     Curl_safefree(conn->http_proxy.passwd);
3510     Curl_safefree(conn->socks_proxy.passwd);
3511     conn->http_proxy.user = old_conn->http_proxy.user;
3512     conn->socks_proxy.user = old_conn->socks_proxy.user;
3513     conn->http_proxy.passwd = old_conn->http_proxy.passwd;
3514     conn->socks_proxy.passwd = old_conn->socks_proxy.passwd;
3515     old_conn->http_proxy.user = NULL;
3516     old_conn->socks_proxy.user = NULL;
3517     old_conn->http_proxy.passwd = NULL;
3518     old_conn->socks_proxy.passwd = NULL;
3519   }
3520
3521   /* host can change, when doing keepalive with a proxy or if the case is
3522      different this time etc */
3523   free_fixed_hostname(&conn->host);
3524   free_fixed_hostname(&conn->conn_to_host);
3525   Curl_safefree(conn->host.rawalloc);
3526   Curl_safefree(conn->conn_to_host.rawalloc);
3527   conn->host = old_conn->host;
3528   conn->conn_to_host = old_conn->conn_to_host;
3529   conn->conn_to_port = old_conn->conn_to_port;
3530   conn->remote_port = old_conn->remote_port;
3531
3532   /* persist connection info in session handle */
3533   Curl_persistconninfo(conn);
3534
3535   conn_reset_all_postponed_data(old_conn); /* free buffers */
3536
3537   /* re-use init */
3538   conn->bits.reuse = TRUE; /* yes, we're re-using here */
3539
3540   Curl_safefree(old_conn->user);
3541   Curl_safefree(old_conn->passwd);
3542   Curl_safefree(old_conn->options);
3543   Curl_safefree(old_conn->http_proxy.user);
3544   Curl_safefree(old_conn->socks_proxy.user);
3545   Curl_safefree(old_conn->http_proxy.passwd);
3546   Curl_safefree(old_conn->socks_proxy.passwd);
3547   Curl_safefree(old_conn->localdev);
3548
3549   Curl_llist_destroy(&old_conn->send_pipe, NULL);
3550   Curl_llist_destroy(&old_conn->recv_pipe, NULL);
3551
3552   Curl_safefree(old_conn->master_buffer);
3553
3554 #ifdef USE_UNIX_SOCKETS
3555   Curl_safefree(old_conn->unix_domain_socket);
3556 #endif
3557 }
3558
3559 /**
3560  * create_conn() sets up a new connectdata struct, or re-uses an already
3561  * existing one, and resolves host name.
3562  *
3563  * if this function returns CURLE_OK and *async is set to TRUE, the resolve
3564  * response will be coming asynchronously. If *async is FALSE, the name is
3565  * already resolved.
3566  *
3567  * @param data The sessionhandle pointer
3568  * @param in_connect is set to the next connection data pointer
3569  * @param async is set TRUE when an async DNS resolution is pending
3570  * @see Curl_setup_conn()
3571  *
3572  * *NOTE* this function assigns the conn->data pointer!
3573  */
3574
3575 static CURLcode create_conn(struct Curl_easy *data,
3576                             struct connectdata **in_connect,
3577                             bool *async)
3578 {
3579   CURLcode result = CURLE_OK;
3580   struct connectdata *conn;
3581   struct connectdata *conn_temp = NULL;
3582   bool reuse;
3583   bool connections_available = TRUE;
3584   bool force_reuse = FALSE;
3585   bool waitpipe = FALSE;
3586   size_t max_host_connections = Curl_multi_max_host_connections(data->multi);
3587   size_t max_total_connections = Curl_multi_max_total_connections(data->multi);
3588
3589   *async = FALSE;
3590
3591   /*************************************************************
3592    * Check input data
3593    *************************************************************/
3594   if(!data->change.url) {
3595     result = CURLE_URL_MALFORMAT;
3596     goto out;
3597   }
3598
3599   /* First, split up the current URL in parts so that we can use the
3600      parts for checking against the already present connections. In order
3601      to not have to modify everything at once, we allocate a temporary
3602      connection data struct and fill in for comparison purposes. */
3603   conn = allocate_conn(data);
3604
3605   if(!conn) {
3606     result = CURLE_OUT_OF_MEMORY;
3607     goto out;
3608   }
3609
3610   /* We must set the return variable as soon as possible, so that our
3611      parent can cleanup any possible allocs we may have done before
3612      any failure */
3613   *in_connect = conn;
3614
3615   result = parseurlandfillconn(data, conn);
3616   if(result)
3617     goto out;
3618
3619   if(data->set.str[STRING_BEARER]) {
3620     conn->oauth_bearer = strdup(data->set.str[STRING_BEARER]);
3621     if(!conn->oauth_bearer) {
3622       result = CURLE_OUT_OF_MEMORY;
3623       goto out;
3624     }
3625   }
3626
3627 #ifdef USE_UNIX_SOCKETS
3628   if(data->set.str[STRING_UNIX_SOCKET_PATH]) {
3629     conn->unix_domain_socket = strdup(data->set.str[STRING_UNIX_SOCKET_PATH]);
3630     if(conn->unix_domain_socket == NULL) {
3631       result = CURLE_OUT_OF_MEMORY;
3632       goto out;
3633     }
3634     conn->abstract_unix_socket = data->set.abstract_unix_socket;
3635   }
3636 #endif
3637
3638   /* After the unix socket init but before the proxy vars are used, parse and
3639      initialize the proxy vars */
3640 #ifndef CURL_DISABLE_PROXY
3641   result = create_conn_helper_init_proxy(conn);
3642   if(result)
3643     goto out;
3644 #endif
3645
3646   /*************************************************************
3647    * If the protocol is using SSL and HTTP proxy is used, we set
3648    * the tunnel_proxy bit.
3649    *************************************************************/
3650   if((conn->given->flags&PROTOPT_SSL) && conn->bits.httpproxy)
3651     conn->bits.tunnel_proxy = TRUE;
3652
3653   /*************************************************************
3654    * Figure out the remote port number and fix it in the URL
3655    *************************************************************/
3656   result = parse_remote_port(data, conn);
3657   if(result)
3658     goto out;
3659
3660   /* Check for overridden login details and set them accordingly so they
3661      they are known when protocol->setup_connection is called! */
3662   result = override_login(data, conn, &conn->user, &conn->passwd,
3663                           &conn->options);
3664   if(result)
3665     goto out;
3666
3667   result = set_login(conn); /* default credentials */
3668   if(result)
3669     goto out;
3670
3671   /*************************************************************
3672    * Process the "connect to" linked list of hostname/port mappings.
3673    * Do this after the remote port number has been fixed in the URL.
3674    *************************************************************/
3675   result = parse_connect_to_slist(data, conn, data->set.connect_to);
3676   if(result)
3677     goto out;
3678
3679   /*************************************************************
3680    * IDN-fix the hostnames
3681    *************************************************************/
3682   result = fix_hostname(conn, &conn->host);
3683   if(result)
3684     goto out;
3685   if(conn->bits.conn_to_host) {
3686     result = fix_hostname(conn, &conn->conn_to_host);
3687     if(result)
3688       goto out;
3689   }
3690   if(conn->bits.httpproxy) {
3691     result = fix_hostname(conn, &conn->http_proxy.host);
3692     if(result)
3693       goto out;
3694   }
3695   if(conn->bits.socksproxy) {
3696     result = fix_hostname(conn, &conn->socks_proxy.host);
3697     if(result)
3698       goto out;
3699   }
3700
3701   /*************************************************************
3702    * Check whether the host and the "connect to host" are equal.
3703    * Do this after the hostnames have been IDN-fixed.
3704    *************************************************************/
3705   if(conn->bits.conn_to_host &&
3706      strcasecompare(conn->conn_to_host.name, conn->host.name)) {
3707     conn->bits.conn_to_host = FALSE;
3708   }
3709
3710   /*************************************************************
3711    * Check whether the port and the "connect to port" are equal.
3712    * Do this after the remote port number has been fixed in the URL.
3713    *************************************************************/
3714   if(conn->bits.conn_to_port && conn->conn_to_port == conn->remote_port) {
3715     conn->bits.conn_to_port = FALSE;
3716   }
3717
3718   /*************************************************************
3719    * If the "connect to" feature is used with an HTTP proxy,
3720    * we set the tunnel_proxy bit.
3721    *************************************************************/
3722   if((conn->bits.conn_to_host || conn->bits.conn_to_port) &&
3723       conn->bits.httpproxy)
3724     conn->bits.tunnel_proxy = TRUE;
3725
3726   /*************************************************************
3727    * Setup internals depending on protocol. Needs to be done after
3728    * we figured out what/if proxy to use.
3729    *************************************************************/
3730   result = setup_connection_internals(conn);
3731   if(result)
3732     goto out;
3733
3734   conn->recv[FIRSTSOCKET] = Curl_recv_plain;
3735   conn->send[FIRSTSOCKET] = Curl_send_plain;
3736   conn->recv[SECONDARYSOCKET] = Curl_recv_plain;
3737   conn->send[SECONDARYSOCKET] = Curl_send_plain;
3738
3739   conn->bits.tcp_fastopen = data->set.tcp_fastopen;
3740
3741   /***********************************************************************
3742    * file: is a special case in that it doesn't need a network connection
3743    ***********************************************************************/
3744 #ifndef CURL_DISABLE_FILE
3745   if(conn->handler->flags & PROTOPT_NONETWORK) {
3746     bool done;
3747     /* this is supposed to be the connect function so we better at least check
3748        that the file is present here! */
3749     DEBUGASSERT(conn->handler->connect_it);
3750     Curl_persistconninfo(conn);
3751     result = conn->handler->connect_it(conn, &done);
3752
3753     /* Setup a "faked" transfer that'll do nothing */
3754     if(!result) {
3755       conn->data = data;
3756       conn->bits.tcpconnect[FIRSTSOCKET] = TRUE; /* we are "connected */
3757
3758       result = Curl_conncache_add_conn(data->state.conn_cache, conn);
3759       if(result)
3760         goto out;
3761
3762       /*
3763        * Setup whatever necessary for a resumed transfer
3764        */
3765       result = setup_range(data);
3766       if(result) {
3767         DEBUGASSERT(conn->handler->done);
3768         /* we ignore the return code for the protocol-specific DONE */
3769         (void)conn->handler->done(conn, result, FALSE);
3770         goto out;
3771       }
3772
3773       Curl_setup_transfer(conn, -1, -1, FALSE, NULL, /* no download */
3774                           -1, NULL); /* no upload */
3775     }
3776
3777     /* since we skip do_init() */
3778     Curl_init_do(data, conn);
3779
3780     goto out;
3781   }
3782 #endif
3783
3784   /* Get a cloned copy of the SSL config situation stored in the
3785      connection struct. But to get this going nicely, we must first make
3786      sure that the strings in the master copy are pointing to the correct
3787      strings in the session handle strings array!
3788
3789      Keep in mind that the pointers in the master copy are pointing to strings
3790      that will be freed as part of the Curl_easy struct, but all cloned
3791      copies will be separately allocated.
3792   */
3793   data->set.ssl.primary.CApath = data->set.str[STRING_SSL_CAPATH_ORIG];
3794   data->set.proxy_ssl.primary.CApath = data->set.str[STRING_SSL_CAPATH_PROXY];
3795   data->set.ssl.primary.CAfile = data->set.str[STRING_SSL_CAFILE_ORIG];
3796   data->set.proxy_ssl.primary.CAfile = data->set.str[STRING_SSL_CAFILE_PROXY];
3797   data->set.ssl.primary.random_file = data->set.str[STRING_SSL_RANDOM_FILE];
3798   data->set.proxy_ssl.primary.random_file =
3799     data->set.str[STRING_SSL_RANDOM_FILE];
3800   data->set.ssl.primary.egdsocket = data->set.str[STRING_SSL_EGDSOCKET];
3801   data->set.proxy_ssl.primary.egdsocket = data->set.str[STRING_SSL_EGDSOCKET];
3802   data->set.ssl.primary.cipher_list =
3803     data->set.str[STRING_SSL_CIPHER_LIST_ORIG];
3804   data->set.proxy_ssl.primary.cipher_list =
3805     data->set.str[STRING_SSL_CIPHER_LIST_PROXY];
3806   data->set.ssl.primary.cipher_list13 =
3807     data->set.str[STRING_SSL_CIPHER13_LIST_ORIG];
3808   data->set.proxy_ssl.primary.cipher_list13 =
3809     data->set.str[STRING_SSL_CIPHER13_LIST_PROXY];
3810
3811   data->set.ssl.CRLfile = data->set.str[STRING_SSL_CRLFILE_ORIG];
3812   data->set.proxy_ssl.CRLfile = data->set.str[STRING_SSL_CRLFILE_PROXY];
3813   data->set.ssl.issuercert = data->set.str[STRING_SSL_ISSUERCERT_ORIG];
3814   data->set.proxy_ssl.issuercert = data->set.str[STRING_SSL_ISSUERCERT_PROXY];
3815   data->set.ssl.cert = data->set.str[STRING_CERT_ORIG];
3816   data->set.proxy_ssl.cert = data->set.str[STRING_CERT_PROXY];
3817   data->set.ssl.cert_type = data->set.str[STRING_CERT_TYPE_ORIG];
3818   data->set.proxy_ssl.cert_type = data->set.str[STRING_CERT_TYPE_PROXY];
3819   data->set.ssl.key = data->set.str[STRING_KEY_ORIG];
3820   data->set.proxy_ssl.key = data->set.str[STRING_KEY_PROXY];
3821   data->set.ssl.key_type = data->set.str[STRING_KEY_TYPE_ORIG];
3822   data->set.proxy_ssl.key_type = data->set.str[STRING_KEY_TYPE_PROXY];
3823   data->set.ssl.key_passwd = data->set.str[STRING_KEY_PASSWD_ORIG];
3824   data->set.proxy_ssl.key_passwd = data->set.str[STRING_KEY_PASSWD_PROXY];
3825   data->set.ssl.primary.clientcert = data->set.str[STRING_CERT_ORIG];
3826   data->set.proxy_ssl.primary.clientcert = data->set.str[STRING_CERT_PROXY];
3827 #ifdef USE_TLS_SRP
3828   data->set.ssl.username = data->set.str[STRING_TLSAUTH_USERNAME_ORIG];
3829   data->set.proxy_ssl.username = data->set.str[STRING_TLSAUTH_USERNAME_PROXY];
3830   data->set.ssl.password = data->set.str[STRING_TLSAUTH_PASSWORD_ORIG];
3831   data->set.proxy_ssl.password = data->set.str[STRING_TLSAUTH_PASSWORD_PROXY];
3832 #endif
3833
3834   if(!Curl_clone_primary_ssl_config(&data->set.ssl.primary,
3835      &conn->ssl_config)) {
3836     result = CURLE_OUT_OF_MEMORY;
3837     goto out;
3838   }
3839
3840   if(!Curl_clone_primary_ssl_config(&data->set.proxy_ssl.primary,
3841                                     &conn->proxy_ssl_config)) {
3842     result = CURLE_OUT_OF_MEMORY;
3843     goto out;
3844   }
3845
3846   prune_dead_connections(data);
3847
3848   /*************************************************************
3849    * Check the current list of connections to see if we can
3850    * re-use an already existing one or if we have to create a
3851    * new one.
3852    *************************************************************/
3853
3854   DEBUGASSERT(conn->user);
3855   DEBUGASSERT(conn->passwd);
3856
3857   /* reuse_fresh is TRUE if we are told to use a new connection by force, but
3858      we only acknowledge this option if this is not a re-used connection
3859      already (which happens due to follow-location or during a HTTP
3860      authentication phase). */
3861   if(data->set.reuse_fresh && !data->state.this_is_a_follow)
3862     reuse = FALSE;
3863   else
3864     reuse = ConnectionExists(data, conn, &conn_temp, &force_reuse, &waitpipe);
3865
3866   /* If we found a reusable connection that is now marked as in use, we may
3867      still want to open a new connection if we are pipelining. */
3868   if(reuse && !force_reuse && IsPipeliningPossible(data, conn_temp)) {
3869     size_t pipelen = conn_temp->send_pipe.size + conn_temp->recv_pipe.size;
3870     if(pipelen > 0) {
3871       infof(data, "Found connection %ld, with requests in the pipe (%zu)\n",
3872             conn_temp->connection_id, pipelen);
3873
3874       if(Curl_conncache_bundle_size(conn_temp) < max_host_connections &&
3875          Curl_conncache_size(data) < max_total_connections) {
3876         /* We want a new connection anyway */
3877         reuse = FALSE;
3878
3879         infof(data, "We can reuse, but we want a new connection anyway\n");
3880         Curl_conncache_return_conn(conn_temp);
3881       }
3882     }
3883   }
3884
3885   if(reuse) {
3886     /*
3887      * We already have a connection for this, we got the former connection
3888      * in the conn_temp variable and thus we need to cleanup the one we
3889      * just allocated before we can move along and use the previously
3890      * existing one.
3891      */
3892     reuse_conn(conn, conn_temp);
3893 #ifdef USE_SSL
3894     free(conn->ssl_extra);
3895 #endif
3896     free(conn);          /* we don't need this anymore */
3897     conn = conn_temp;
3898     *in_connect = conn;
3899
3900     infof(data, "Re-using existing connection! (#%ld) with %s %s\n",
3901           conn->connection_id,
3902           conn->bits.proxy?"proxy":"host",
3903           conn->socks_proxy.host.name ? conn->socks_proxy.host.dispname :
3904           conn->http_proxy.host.name ? conn->http_proxy.host.dispname :
3905                                        conn->host.dispname);
3906   }
3907   else {
3908     /* We have decided that we want a new connection. However, we may not
3909        be able to do that if we have reached the limit of how many
3910        connections we are allowed to open. */
3911
3912     if(conn->handler->flags & PROTOPT_ALPN_NPN) {
3913       /* The protocol wants it, so set the bits if enabled in the easy handle
3914          (default) */
3915       if(data->set.ssl_enable_alpn)
3916         conn->bits.tls_enable_alpn = TRUE;
3917       if(data->set.ssl_enable_npn)
3918         conn->bits.tls_enable_npn = TRUE;
3919     }
3920
3921     if(waitpipe)
3922       /* There is a connection that *might* become usable for pipelining
3923          "soon", and we wait for that */
3924       connections_available = FALSE;
3925     else {
3926       /* this gets a lock on the conncache */
3927       struct connectbundle *bundle =
3928         Curl_conncache_find_bundle(conn, data->state.conn_cache);
3929
3930       if(max_host_connections > 0 && bundle &&
3931          (bundle->num_connections >= max_host_connections)) {
3932         struct connectdata *conn_candidate;
3933
3934         /* The bundle is full. Extract the oldest connection. */
3935         conn_candidate = Curl_conncache_extract_bundle(data, bundle);
3936         Curl_conncache_unlock(conn);
3937
3938         if(conn_candidate)
3939           (void)Curl_disconnect(data, conn_candidate,
3940                                 /* dead_connection */ FALSE);
3941         else {
3942           infof(data, "No more connections allowed to host: %zu\n",
3943                 max_host_connections);
3944           connections_available = FALSE;
3945         }
3946       }
3947       else
3948         Curl_conncache_unlock(conn);
3949
3950     }
3951
3952     if(connections_available &&
3953        (max_total_connections > 0) &&
3954        (Curl_conncache_size(data) >= max_total_connections)) {
3955       struct connectdata *conn_candidate;
3956
3957       /* The cache is full. Let's see if we can kill a connection. */
3958       conn_candidate = Curl_conncache_extract_oldest(data);
3959       if(conn_candidate)
3960         (void)Curl_disconnect(data, conn_candidate,
3961                               /* dead_connection */ FALSE);
3962       else {
3963         infof(data, "No connections available in cache\n");
3964         connections_available = FALSE;
3965       }
3966     }
3967
3968     if(!connections_available) {
3969       infof(data, "No connections available.\n");
3970
3971       conn_free(conn);
3972       *in_connect = NULL;
3973
3974       result = CURLE_NO_CONNECTION_AVAILABLE;
3975       goto out;
3976     }
3977     else {
3978       /*
3979        * This is a brand new connection, so let's store it in the connection
3980        * cache of ours!
3981        */
3982       result = Curl_conncache_add_conn(data->state.conn_cache, conn);
3983       if(result)
3984         goto out;
3985     }
3986
3987 #if defined(USE_NTLM)
3988     /* If NTLM is requested in a part of this connection, make sure we don't
3989        assume the state is fine as this is a fresh connection and NTLM is
3990        connection based. */
3991     if((data->state.authhost.picked & (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
3992        data->state.authhost.done) {
3993       infof(data, "NTLM picked AND auth done set, clear picked!\n");
3994       data->state.authhost.picked = CURLAUTH_NONE;
3995       data->state.authhost.done = FALSE;
3996     }
3997
3998     if((data->state.authproxy.picked & (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
3999        data->state.authproxy.done) {
4000       infof(data, "NTLM-proxy picked AND auth done set, clear picked!\n");
4001       data->state.authproxy.picked = CURLAUTH_NONE;
4002       data->state.authproxy.done = FALSE;
4003     }
4004 #endif
4005   }
4006
4007   /* Setup and init stuff before DO starts, in preparing for the transfer. */
4008   Curl_init_do(data, conn);
4009
4010   /*
4011    * Setup whatever necessary for a resumed transfer
4012    */
4013   result = setup_range(data);
4014   if(result)
4015     goto out;
4016
4017   /* Continue connectdata initialization here. */
4018
4019   /*
4020    * Inherit the proper values from the urldata struct AFTER we have arranged
4021    * the persistent connection stuff
4022    */
4023   conn->seek_func = data->set.seek_func;
4024   conn->seek_client = data->set.seek_client;
4025
4026   /*************************************************************
4027    * Resolve the address of the server or proxy
4028    *************************************************************/
4029   result = resolve_server(data, conn, async);
4030
4031 out:
4032   return result;
4033 }
4034
4035 /* Curl_setup_conn() is called after the name resolve initiated in
4036  * create_conn() is all done.
4037  *
4038  * Curl_setup_conn() also handles reused connections
4039  *
4040  * conn->data MUST already have been setup fine (in create_conn)
4041  */
4042
4043 CURLcode Curl_setup_conn(struct connectdata *conn,
4044                          bool *protocol_done)
4045 {
4046   CURLcode result = CURLE_OK;
4047   struct Curl_easy *data = conn->data;
4048
4049   Curl_pgrsTime(data, TIMER_NAMELOOKUP);
4050
4051   if(conn->handler->flags & PROTOPT_NONETWORK) {
4052     /* nothing to setup when not using a network */
4053     *protocol_done = TRUE;
4054     return result;
4055   }
4056   *protocol_done = FALSE; /* default to not done */
4057
4058   /* set proxy_connect_closed to false unconditionally already here since it
4059      is used strictly to provide extra information to a parent function in the
4060      case of proxy CONNECT failures and we must make sure we don't have it
4061      lingering set from a previous invoke */
4062   conn->bits.proxy_connect_closed = FALSE;
4063
4064   /*
4065    * Set user-agent. Used for HTTP, but since we can attempt to tunnel
4066    * basically anything through a http proxy we can't limit this based on
4067    * protocol.
4068    */
4069   if(data->set.str[STRING_USERAGENT]) {
4070     Curl_safefree(conn->allocptr.uagent);
4071     conn->allocptr.uagent =
4072       aprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
4073     if(!conn->allocptr.uagent)
4074       return CURLE_OUT_OF_MEMORY;
4075   }
4076
4077   data->req.headerbytecount = 0;
4078
4079 #ifdef CURL_DO_LINEEND_CONV
4080   data->state.crlf_conversions = 0; /* reset CRLF conversion counter */
4081 #endif /* CURL_DO_LINEEND_CONV */
4082
4083   /* set start time here for timeout purposes in the connect procedure, it
4084      is later set again for the progress meter purpose */
4085   conn->now = Curl_now();
4086
4087   if(CURL_SOCKET_BAD == conn->sock[FIRSTSOCKET]) {
4088     conn->bits.tcpconnect[FIRSTSOCKET] = FALSE;
4089     result = Curl_connecthost(conn, conn->dns_entry);
4090     if(result)
4091       return result;
4092   }
4093   else {
4094     Curl_pgrsTime(data, TIMER_CONNECT);    /* we're connected already */
4095     Curl_pgrsTime(data, TIMER_APPCONNECT); /* we're connected already */
4096     conn->bits.tcpconnect[FIRSTSOCKET] = TRUE;
4097     *protocol_done = TRUE;
4098     Curl_updateconninfo(conn, conn->sock[FIRSTSOCKET]);
4099     Curl_verboseconnect(conn);
4100   }
4101
4102   conn->now = Curl_now(); /* time this *after* the connect is done, we set
4103                              this here perhaps a second time */
4104   return result;
4105 }
4106
4107 CURLcode Curl_connect(struct Curl_easy *data,
4108                       struct connectdata **in_connect,
4109                       bool *asyncp,
4110                       bool *protocol_done)
4111 {
4112   CURLcode result;
4113
4114   *asyncp = FALSE; /* assume synchronous resolves by default */
4115
4116   /* init the single-transfer specific data */
4117   Curl_free_request_state(data);
4118   memset(&data->req, 0, sizeof(struct SingleRequest));
4119   data->req.maxdownload = -1;
4120
4121   /* call the stuff that needs to be called */
4122   result = create_conn(data, in_connect, asyncp);
4123
4124   if(!result) {
4125     if(CONN_INUSE(*in_connect))
4126       /* pipelining */
4127       *protocol_done = TRUE;
4128     else if(!*asyncp) {
4129       /* DNS resolution is done: that's either because this is a reused
4130          connection, in which case DNS was unnecessary, or because DNS
4131          really did finish already (synch resolver/fast async resolve) */
4132       result = Curl_setup_conn(*in_connect, protocol_done);
4133     }
4134   }
4135
4136   if(result == CURLE_NO_CONNECTION_AVAILABLE) {
4137     *in_connect = NULL;
4138     return result;
4139   }
4140   else if(result && *in_connect) {
4141     /* We're not allowed to return failure with memory left allocated in the
4142        connectdata struct, free those here */
4143     Curl_disconnect(data, *in_connect, TRUE);
4144     *in_connect = NULL; /* return a NULL */
4145   }
4146
4147   return result;
4148 }
4149
4150 /*
4151  * Curl_init_do() inits the readwrite session. This is inited each time (in
4152  * the DO function before the protocol-specific DO functions are invoked) for
4153  * a transfer, sometimes multiple times on the same Curl_easy. Make sure
4154  * nothing in here depends on stuff that are setup dynamically for the
4155  * transfer.
4156  *
4157  * Allow this function to get called with 'conn' set to NULL.
4158  */
4159
4160 CURLcode Curl_init_do(struct Curl_easy *data, struct connectdata *conn)
4161 {
4162   struct SingleRequest *k = &data->req;
4163
4164   if(conn) {
4165     conn->bits.do_more = FALSE; /* by default there's no curl_do_more() to
4166                                    use */
4167     /* if the protocol used doesn't support wildcards, switch it off */
4168     if(data->state.wildcardmatch &&
4169        !(conn->handler->flags & PROTOPT_WILDCARD))
4170       data->state.wildcardmatch = FALSE;
4171   }
4172
4173   data->state.done = FALSE; /* *_done() is not called yet */
4174   data->state.expect100header = FALSE;
4175
4176
4177   if(data->set.opt_no_body)
4178     /* in HTTP lingo, no body means using the HEAD request... */
4179     data->set.httpreq = HTTPREQ_HEAD;
4180   else if(HTTPREQ_HEAD == data->set.httpreq)
4181     /* ... but if unset there really is no perfect method that is the
4182        "opposite" of HEAD but in reality most people probably think GET
4183        then. The important thing is that we can't let it remain HEAD if the
4184        opt_no_body is set FALSE since then we'll behave wrong when getting
4185        HTTP. */
4186     data->set.httpreq = HTTPREQ_GET;
4187
4188   k->start = Curl_now(); /* start time */
4189   k->now = k->start;   /* current time is now */
4190   k->header = TRUE; /* assume header */
4191
4192   k->bytecount = 0;
4193
4194   k->buf = data->state.buffer;
4195   k->hbufp = data->state.headerbuff;
4196   k->ignorebody = FALSE;
4197
4198   Curl_speedinit(data);
4199
4200   Curl_pgrsSetUploadCounter(data, 0);
4201   Curl_pgrsSetDownloadCounter(data, 0);
4202
4203   return CURLE_OK;
4204 }
4205
4206 /*
4207 * get_protocol_family()
4208 *
4209 * This is used to return the protocol family for a given protocol.
4210 *
4211 * Parameters:
4212 *
4213 * protocol  [in]  - A single bit protocol identifier such as HTTP or HTTPS.
4214 *
4215 * Returns the family as a single bit protocol identifier.
4216 */
4217
4218 static unsigned int get_protocol_family(unsigned int protocol)
4219 {
4220   unsigned int family;
4221
4222   switch(protocol) {
4223   case CURLPROTO_HTTP:
4224   case CURLPROTO_HTTPS:
4225     family = CURLPROTO_HTTP;
4226     break;
4227
4228   case CURLPROTO_FTP:
4229   case CURLPROTO_FTPS:
4230     family = CURLPROTO_FTP;
4231     break;
4232
4233   case CURLPROTO_SCP:
4234     family = CURLPROTO_SCP;
4235     break;
4236
4237   case CURLPROTO_SFTP:
4238     family = CURLPROTO_SFTP;
4239     break;
4240
4241   case CURLPROTO_TELNET:
4242     family = CURLPROTO_TELNET;
4243     break;
4244
4245   case CURLPROTO_LDAP:
4246   case CURLPROTO_LDAPS:
4247     family = CURLPROTO_LDAP;
4248     break;
4249
4250   case CURLPROTO_DICT:
4251     family = CURLPROTO_DICT;
4252     break;
4253
4254   case CURLPROTO_FILE:
4255     family = CURLPROTO_FILE;
4256     break;
4257
4258   case CURLPROTO_TFTP:
4259     family = CURLPROTO_TFTP;
4260     break;
4261
4262   case CURLPROTO_IMAP:
4263   case CURLPROTO_IMAPS:
4264     family = CURLPROTO_IMAP;
4265     break;
4266
4267   case CURLPROTO_POP3:
4268   case CURLPROTO_POP3S:
4269     family = CURLPROTO_POP3;
4270     break;
4271
4272   case CURLPROTO_SMTP:
4273   case CURLPROTO_SMTPS:
4274       family = CURLPROTO_SMTP;
4275       break;
4276
4277   case CURLPROTO_RTSP:
4278     family = CURLPROTO_RTSP;
4279     break;
4280
4281   case CURLPROTO_RTMP:
4282   case CURLPROTO_RTMPS:
4283     family = CURLPROTO_RTMP;
4284     break;
4285
4286   case CURLPROTO_RTMPT:
4287   case CURLPROTO_RTMPTS:
4288     family = CURLPROTO_RTMPT;
4289     break;
4290
4291   case CURLPROTO_RTMPE:
4292     family = CURLPROTO_RTMPE;
4293     break;
4294
4295   case CURLPROTO_RTMPTE:
4296     family = CURLPROTO_RTMPTE;
4297     break;
4298
4299   case CURLPROTO_GOPHER:
4300     family = CURLPROTO_GOPHER;
4301     break;
4302
4303   case CURLPROTO_SMB:
4304   case CURLPROTO_SMBS:
4305     family = CURLPROTO_SMB;
4306     break;
4307
4308   default:
4309       family = 0;
4310       break;
4311   }
4312
4313   return family;
4314 }
4315
4316
4317 /*
4318  * Wrapper to call functions in Curl_conncache_foreach()
4319  *
4320  * Returns always 0.
4321  */
4322 static int conn_upkeep(struct connectdata *conn,
4323                        void *param)
4324 {
4325   /* Param is unused. */
4326   (void)param;
4327
4328   if(conn->handler->connection_check) {
4329     /* Do a protocol-specific keepalive check on the connection. */
4330     conn->handler->connection_check(conn, CONNCHECK_KEEPALIVE);
4331   }
4332
4333   return 0; /* continue iteration */
4334 }
4335
4336 CURLcode Curl_upkeep(struct conncache *conn_cache,
4337                           void *data)
4338 {
4339   /* Loop over every connection and make connection alive. */
4340   Curl_conncache_foreach(data,
4341                          conn_cache,
4342                          data,
4343                          conn_upkeep);
4344   return CURLE_OK;
4345 }