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