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