timers: fix timer regression involving redirects / reconnects
[platform/upstream/curl.git] / lib / urldata.h
1 #ifndef HEADER_CURL_URLDATA_H
2 #define HEADER_CURL_URLDATA_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
11  *
12  * This software is licensed as described in the file COPYING, which
13  * you should have received as part of this distribution. The terms
14  * are also available at http://curl.haxx.se/docs/copyright.html.
15  *
16  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17  * copies of the Software, and permit persons to whom the Software is
18  * furnished to do so, under the terms of the COPYING file.
19  *
20  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21  * KIND, either express or implied.
22  *
23  ***************************************************************************/
24
25 /* This file is for lib internal stuff */
26
27 #include "curl_setup.h"
28
29 #define PORT_FTP 21
30 #define PORT_FTPS 990
31 #define PORT_TELNET 23
32 #define PORT_HTTP 80
33 #define PORT_HTTPS 443
34 #define PORT_DICT 2628
35 #define PORT_LDAP 389
36 #define PORT_LDAPS 636
37 #define PORT_TFTP 69
38 #define PORT_SSH 22
39 #define PORT_IMAP 143
40 #define PORT_IMAPS 993
41 #define PORT_POP3 110
42 #define PORT_POP3S 995
43 #define PORT_SMTP 25
44 #define PORT_SMTPS 465 /* sometimes called SSMTP */
45 #define PORT_RTSP 554
46 #define PORT_RTMP 1935
47 #define PORT_RTMPT PORT_HTTP
48 #define PORT_RTMPS PORT_HTTPS
49 #define PORT_GOPHER 70
50
51 #define DICT_MATCH "/MATCH:"
52 #define DICT_MATCH2 "/M:"
53 #define DICT_MATCH3 "/FIND:"
54 #define DICT_DEFINE "/DEFINE:"
55 #define DICT_DEFINE2 "/D:"
56 #define DICT_DEFINE3 "/LOOKUP:"
57
58 #define CURL_DEFAULT_USER "anonymous"
59 #define CURL_DEFAULT_PASSWORD "ftp@example.com"
60
61 /* Convenience defines for checking protocols or their SSL based version. Each
62    protocol handler should only ever have a single CURLPROTO_ in its protocol
63    field. */
64 #define PROTO_FAMILY_HTTP (CURLPROTO_HTTP|CURLPROTO_HTTPS)
65 #define PROTO_FAMILY_FTP  (CURLPROTO_FTP|CURLPROTO_FTPS)
66 #define PROTO_FAMILY_POP3 (CURLPROTO_POP3|CURLPROTO_POP3S)
67 #define PROTO_FAMILY_SMTP (CURLPROTO_SMTP|CURLPROTO_SMTPS)
68
69 #define DEFAULT_CONNCACHE_SIZE 5
70
71 /* length of longest IPv6 address string including the trailing null */
72 #define MAX_IPADR_LEN sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
73
74 /* Default FTP/IMAP etc response timeout in milliseconds.
75    Symbian OS panics when given a timeout much greater than 1/2 hour.
76 */
77 #define RESP_TIMEOUT (1800*1000)
78
79 #include "cookie.h"
80 #include "formdata.h"
81
82 #ifdef USE_SSLEAY
83 #ifdef USE_OPENSSL
84 #include <openssl/rsa.h>
85 #include <openssl/crypto.h>
86 #include <openssl/x509.h>
87 #include <openssl/pem.h>
88 #include <openssl/ssl.h>
89 #include <openssl/err.h>
90 #ifdef HAVE_OPENSSL_ENGINE_H
91 #include <openssl/engine.h>
92 #endif
93 #ifdef HAVE_OPENSSL_PKCS12_H
94 #include <openssl/pkcs12.h>
95 #endif
96 #else /* SSLeay-style includes */
97 #include <rsa.h>
98 #include <crypto.h>
99 #include <x509.h>
100 #include <pem.h>
101 #include <ssl.h>
102 #include <err.h>
103 #ifdef HAVE_OPENSSL_ENGINE_H
104 #include <engine.h>
105 #endif
106 #ifdef HAVE_OPENSSL_PKCS12_H
107 #include <pkcs12.h>
108 #endif
109 #endif /* USE_OPENSSL */
110 #ifdef USE_GNUTLS
111 #error Configuration error; cannot use GnuTLS *and* OpenSSL.
112 #endif
113 #endif /* USE_SSLEAY */
114
115 #ifdef USE_GNUTLS
116 #include <gnutls/gnutls.h>
117 #endif
118
119 #ifdef USE_POLARSSL
120 #include <polarssl/ssl.h>
121 #include <polarssl/version.h>
122 #if POLARSSL_VERSION_NUMBER<0x01010000
123 #include <polarssl/havege.h>
124 #else
125 #include <polarssl/entropy.h>
126 #include <polarssl/ctr_drbg.h>
127 #endif /* POLARSSL_VERSION_NUMBER<0x01010000 */
128 #endif /* USE_POLARSSL */
129
130 #ifdef USE_CYASSL
131 #undef OCSP_REQUEST  /* avoid cyassl/openssl/ssl.h clash with wincrypt.h */
132 #undef OCSP_RESPONSE /* avoid cyassl/openssl/ssl.h clash with wincrypt.h */
133 #include <cyassl/openssl/ssl.h>
134 #endif
135
136 #ifdef USE_NSS
137 #include <nspr.h>
138 #include <pk11pub.h>
139 #endif
140
141 #ifdef USE_QSOSSL
142 #include <qsossl.h>
143 #endif
144
145 #ifdef USE_GSKIT
146 #include <gskssl.h>
147 #endif
148
149 #ifdef USE_AXTLS
150 #include <axTLS/ssl.h>
151 #undef malloc
152 #undef calloc
153 #undef realloc
154 #endif /* USE_AXTLS */
155
156 #ifdef USE_SCHANNEL
157 #include "curl_sspi.h"
158 #include <schnlsp.h>
159 #include <schannel.h>
160 #endif
161
162 #ifdef USE_DARWINSSL
163 #include <Security/Security.h>
164 /* For some reason, when building for iOS, the omnibus header above does
165  * not include SecureTransport.h as of iOS SDK 5.1. */
166 #include <Security/SecureTransport.h>
167 #endif
168
169 #ifdef HAVE_NETINET_IN_H
170 #include <netinet/in.h>
171 #endif
172
173 #include "timeval.h"
174
175 #ifdef HAVE_ZLIB_H
176 #include <zlib.h>               /* for content-encoding */
177 #ifdef __SYMBIAN32__
178 /* zlib pollutes the namespace with this definition */
179 #undef WIN32
180 #endif
181 #endif
182
183 #include <curl/curl.h>
184
185 #include "http_chunks.h" /* for the structs and enum stuff */
186 #include "hostip.h"
187 #include "hash.h"
188 #include "splay.h"
189
190 #include "imap.h"
191 #include "pop3.h"
192 #include "smtp.h"
193 #include "ftp.h"
194 #include "file.h"
195 #include "ssh.h"
196 #include "http.h"
197 #include "rtsp.h"
198 #include "wildcard.h"
199 #include "multihandle.h"
200
201 #ifdef HAVE_GSSAPI
202 # ifdef HAVE_GSSGNU
203 #  include <gss.h>
204 # elif defined HAVE_GSSMIT
205 #  include <gssapi/gssapi.h>
206 #  include <gssapi/gssapi_generic.h>
207 # else
208 #  include <gssapi.h>
209 # endif
210 #endif
211
212 #ifdef HAVE_LIBSSH2_H
213 #include <libssh2.h>
214 #include <libssh2_sftp.h>
215 #endif /* HAVE_LIBSSH2_H */
216
217 /* Download buffer size, keep it fairly big for speed reasons */
218 #undef BUFSIZE
219 #define BUFSIZE CURL_MAX_WRITE_SIZE
220
221 /* Initial size of the buffer to store headers in, it'll be enlarged in case
222    of need. */
223 #define HEADERSIZE 256
224
225 #define CURLEASY_MAGIC_NUMBER 0xc0dedbadU
226
227 /* Some convenience macros to get the larger/smaller value out of two given.
228    We prefix with CURL to prevent name collisions. */
229 #define CURLMAX(x,y) ((x)>(y)?(x):(y))
230 #define CURLMIN(x,y) ((x)<(y)?(x):(y))
231
232
233 #ifdef HAVE_GSSAPI
234 /* Types needed for krb5-ftp connections */
235 struct krb5buffer {
236   void *data;
237   size_t size;
238   size_t index;
239   int eof_flag;
240 };
241
242 enum protection_level {
243   PROT_NONE, /* first in list */
244   PROT_CLEAR,
245   PROT_SAFE,
246   PROT_CONFIDENTIAL,
247   PROT_PRIVATE,
248   PROT_CMD,
249   PROT_LAST /* last in list */
250 };
251 #endif
252
253 #ifdef USE_SCHANNEL
254 /* Structs to store Schannel handles */
255 struct curl_schannel_cred {
256   CredHandle cred_handle;
257   TimeStamp time_stamp;
258   int refcount;
259   bool cached;
260 };
261
262 struct curl_schannel_ctxt {
263   CtxtHandle ctxt_handle;
264   TimeStamp time_stamp;
265 };
266 #endif
267
268 /* enum for the nonblocking SSL connection state machine */
269 typedef enum {
270   ssl_connect_1,
271   ssl_connect_2,
272   ssl_connect_2_reading,
273   ssl_connect_2_writing,
274   ssl_connect_3,
275   ssl_connect_done
276 } ssl_connect_state;
277
278 typedef enum {
279   ssl_connection_none,
280   ssl_connection_negotiating,
281   ssl_connection_complete
282 } ssl_connection_state;
283
284 /* struct for data related to each SSL connection */
285 struct ssl_connect_data {
286   /* Use ssl encrypted communications TRUE/FALSE, not necessarily using it atm
287      but at least asked to or meaning to use it. See 'state' for the exact
288      current state of the connection. */
289   bool use;
290   ssl_connection_state state;
291 #ifdef USE_SSLEAY
292   /* these ones requires specific SSL-types */
293   SSL_CTX* ctx;
294   SSL*     handle;
295   X509*    server_cert;
296   ssl_connect_state connecting_state;
297 #endif /* USE_SSLEAY */
298 #ifdef USE_GNUTLS
299   gnutls_session session;
300   gnutls_certificate_credentials cred;
301 #ifdef USE_TLS_SRP
302   gnutls_srp_client_credentials srp_client_cred;
303 #endif
304   ssl_connect_state connecting_state;
305 #endif /* USE_GNUTLS */
306 #ifdef USE_POLARSSL
307   ctr_drbg_context ctr_drbg;
308   entropy_context entropy;
309   ssl_context ssl;
310   ssl_session ssn;
311   int server_fd;
312   x509_crt cacert;
313   x509_crt clicert;
314   x509_crl crl;
315   rsa_context rsa;
316   ssl_connect_state connecting_state;
317 #endif /* USE_POLARSSL */
318 #ifdef USE_CYASSL
319   SSL_CTX* ctx;
320   SSL*     handle;
321   ssl_connect_state connecting_state;
322 #endif /* USE_CYASSL */
323 #ifdef USE_NSS
324   PRFileDesc *handle;
325   char *client_nickname;
326   struct SessionHandle *data;
327   struct curl_llist *obj_list;
328   PK11GenericObject *obj_clicert;
329   ssl_connect_state connecting_state;
330 #endif /* USE_NSS */
331 #ifdef USE_QSOSSL
332   SSLHandle *handle;
333 #endif /* USE_QSOSSL */
334 #ifdef USE_GSKIT
335   gsk_handle handle;
336   int iocport;
337   ssl_connect_state connecting_state;
338 #endif
339 #ifdef USE_AXTLS
340   SSL_CTX* ssl_ctx;
341   SSL*     ssl;
342   ssl_connect_state connecting_state;
343 #endif /* USE_AXTLS */
344 #ifdef USE_SCHANNEL
345   struct curl_schannel_cred *cred;
346   struct curl_schannel_ctxt *ctxt;
347   SecPkgContext_StreamSizes stream_sizes;
348   ssl_connect_state connecting_state;
349   size_t encdata_length, decdata_length;
350   size_t encdata_offset, decdata_offset;
351   unsigned char *encdata_buffer, *decdata_buffer;
352   unsigned long req_flags, ret_flags;
353 #endif /* USE_SCHANNEL */
354 #ifdef USE_DARWINSSL
355   SSLContextRef ssl_ctx;
356   curl_socket_t ssl_sockfd;
357   ssl_connect_state connecting_state;
358   bool ssl_direction; /* true if writing, false if reading */
359   size_t ssl_write_buffered_length;
360 #endif /* USE_DARWINSSL */
361 };
362
363 struct ssl_config_data {
364   long version;          /* what version the client wants to use */
365   long certverifyresult; /* result from the certificate verification */
366
367   bool verifypeer;       /* set TRUE if this is desired */
368   bool verifyhost;       /* set TRUE if CN/SAN must match hostname */
369   char *CApath;          /* certificate dir (doesn't work on windows) */
370   char *CAfile;          /* certificate to verify peer against */
371   const char *CRLfile;   /* CRL to check certificate revocation */
372   const char *issuercert;/* optional issuer certificate filename */
373   char *random_file;     /* path to file containing "random" data */
374   char *egdsocket;       /* path to file containing the EGD daemon socket */
375   char *cipher_list;     /* list of ciphers to use */
376   size_t max_ssl_sessions; /* SSL session id cache size */
377   curl_ssl_ctx_callback fsslctx; /* function to initialize ssl ctx */
378   void *fsslctxp;        /* parameter for call back */
379   bool sessionid;        /* cache session IDs or not */
380   bool certinfo;         /* gather lots of certificate info */
381
382 #ifdef USE_TLS_SRP
383   char *username; /* TLS username (for, e.g., SRP) */
384   char *password; /* TLS password (for, e.g., SRP) */
385   enum CURL_TLSAUTH authtype; /* TLS authentication type (default SRP) */
386 #endif
387 };
388
389 /* information stored about one single SSL session */
390 struct curl_ssl_session {
391   char *name;       /* host name for which this ID was used */
392   void *sessionid;  /* as returned from the SSL layer */
393   size_t idsize;    /* if known, otherwise 0 */
394   long age;         /* just a number, the higher the more recent */
395   int remote_port;  /* remote port to connect to */
396   struct ssl_config_data ssl_config; /* setup for this session */
397 };
398
399 /* Struct used for Digest challenge-response authentication */
400 struct digestdata {
401   char *nonce;
402   char *cnonce;
403   char *realm;
404   int algo;
405   bool stale; /* set true for re-negotiation */
406   char *opaque;
407   char *qop;
408   char *algorithm;
409   int nc; /* nounce count */
410 };
411
412 typedef enum {
413   NTLMSTATE_NONE,
414   NTLMSTATE_TYPE1,
415   NTLMSTATE_TYPE2,
416   NTLMSTATE_TYPE3,
417   NTLMSTATE_LAST
418 } curlntlm;
419
420 #ifdef USE_WINDOWS_SSPI
421 #include "curl_sspi.h"
422 #endif
423
424 #if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV)
425 #include <iconv.h>
426 #endif
427
428 /* Struct used for NTLM challenge-response authentication */
429 struct ntlmdata {
430   curlntlm state;
431 #ifdef USE_WINDOWS_SSPI
432   CredHandle handle;
433   CtxtHandle c_handle;
434   SEC_WINNT_AUTH_IDENTITY identity;
435   SEC_WINNT_AUTH_IDENTITY *p_identity;
436   int has_handles;
437   void *type_2;
438   unsigned long n_type_2;
439 #else
440   unsigned int flags;
441   unsigned char nonce[8];
442   void* target_info; /* TargetInfo received in the ntlm type-2 message */
443   unsigned int target_info_len;
444 #endif
445 };
446
447 #ifdef USE_HTTP_NEGOTIATE
448 struct negotiatedata {
449   /* when doing Negotiate we first need to receive an auth token and then we
450      need to send our header */
451   enum { GSS_AUTHNONE, GSS_AUTHRECV, GSS_AUTHSENT } state;
452   bool gss; /* Whether we're processing GSS-Negotiate or Negotiate */
453   const char* protocol; /* "GSS-Negotiate" or "Negotiate" */
454 #ifdef HAVE_GSSAPI
455   OM_uint32 status;
456   gss_ctx_id_t context;
457   gss_name_t server_name;
458   gss_buffer_desc output_token;
459 #else
460 #ifdef USE_WINDOWS_SSPI
461   DWORD status;
462   CtxtHandle *context;
463   CredHandle *credentials;
464   char server_name[1024];
465   size_t max_token_length;
466   BYTE *output_token;
467   size_t output_token_length;
468 #endif
469 #endif
470 };
471 #endif
472
473
474 /*
475  * Boolean values that concerns this connection.
476  */
477 struct ConnectBits {
478   bool close; /* if set, we close the connection after this request */
479   bool reuse; /* if set, this is a re-used connection */
480   bool proxy; /* if set, this transfer is done through a proxy - any type */
481   bool httpproxy;    /* if set, this transfer is done through a http proxy */
482   bool user_passwd;    /* do we use user+password for this connection? */
483   bool proxy_user_passwd; /* user+password for the proxy? */
484   bool ipv6_ip; /* we communicate with a remote site specified with pure IPv6
485                    IP address */
486   bool ipv6;    /* we communicate with a site using an IPv6 address */
487
488   bool do_more; /* this is set TRUE if the ->curl_do_more() function is
489                    supposed to be called, after ->curl_do() */
490   bool tcpconnect[2]; /* the TCP layer (or similar) is connected, this is set
491                          the first time on the first connect function call */
492   bool protoconnstart;/* the protocol layer has STARTED its operation after
493                          the TCP layer connect */
494
495   bool retry;         /* this connection is about to get closed and then
496                          re-attempted at another connection. */
497   bool tunnel_proxy;  /* if CONNECT is used to "tunnel" through the proxy.
498                          This is implicit when SSL-protocols are used through
499                          proxies, but can also be enabled explicitly by
500                          apps */
501   bool authneg;       /* TRUE when the auth phase has started, which means
502                          that we are creating a request with an auth header,
503                          but it is not the final request in the auth
504                          negotiation. */
505   bool rewindaftersend;/* TRUE when the sending couldn't be stopped even
506                           though it will be discarded. When the whole send
507                           operation is done, we must call the data rewind
508                           callback. */
509   bool ftp_use_epsv;  /* As set with CURLOPT_FTP_USE_EPSV, but if we find out
510                          EPSV doesn't work we disable it for the forthcoming
511                          requests */
512
513   bool ftp_use_eprt;  /* As set with CURLOPT_FTP_USE_EPRT, but if we find out
514                          EPRT doesn't work we disable it for the forthcoming
515                          requests */
516   bool netrc;         /* name+password provided by netrc */
517   bool userpwd_in_url; /* name+password found in url */
518
519   bool done;          /* set to FALSE when Curl_do() is called and set to TRUE
520                          when Curl_done() is called, to prevent Curl_done() to
521                          get invoked twice when the multi interface is
522                          used. */
523   bool stream_was_rewound; /* Indicates that the stream was rewound after a
524                               request read past the end of its response byte
525                               boundary */
526   bool proxy_connect_closed; /* set true if a proxy disconnected the
527                                 connection in a CONNECT request with auth, so
528                                 that libcurl should reconnect and continue. */
529   bool bound; /* set true if bind() has already been done on this socket/
530                  connection */
531   bool type_set;  /* type= was used in the URL */
532 };
533
534 struct hostname {
535   char *rawalloc; /* allocated "raw" version of the name */
536   char *encalloc; /* allocated IDN-encoded version of the name */
537   char *name;     /* name to use internally, might be encoded, might be raw */
538   const char *dispname; /* name to display, as 'name' might be encoded */
539 };
540
541 /*
542  * Flags on the keepon member of the Curl_transfer_keeper
543  */
544
545 #define KEEP_NONE  0
546 #define KEEP_RECV  (1<<0)     /* there is or may be data to read */
547 #define KEEP_SEND (1<<1)     /* there is or may be data to write */
548 #define KEEP_RECV_HOLD (1<<2) /* when set, no reading should be done but there
549                                  might still be data to read */
550 #define KEEP_SEND_HOLD (1<<3) /* when set, no writing should be done but there
551                                   might still be data to write */
552 #define KEEP_RECV_PAUSE (1<<4) /* reading is paused */
553 #define KEEP_SEND_PAUSE (1<<5) /* writing is paused */
554
555 #define KEEP_RECVBITS (KEEP_RECV | KEEP_RECV_HOLD | KEEP_RECV_PAUSE)
556 #define KEEP_SENDBITS (KEEP_SEND | KEEP_SEND_HOLD | KEEP_SEND_PAUSE)
557
558
559 #ifdef HAVE_LIBZ
560 typedef enum {
561   ZLIB_UNINIT,          /* uninitialized */
562   ZLIB_INIT,            /* initialized */
563   ZLIB_GZIP_HEADER,     /* reading gzip header */
564   ZLIB_GZIP_INFLATING,  /* inflating gzip stream */
565   ZLIB_INIT_GZIP        /* initialized in transparent gzip mode */
566 } zlibInitState;
567 #endif
568
569 #ifdef CURLRES_ASYNCH
570 struct Curl_async {
571   char *hostname;
572   int port;
573   struct Curl_dns_entry *dns;
574   bool done;  /* set TRUE when the lookup is complete */
575   int status; /* if done is TRUE, this is the status from the callback */
576   void *os_specific;  /* 'struct thread_data' for Windows */
577 };
578 #endif
579
580 #define FIRSTSOCKET     0
581 #define SECONDARYSOCKET 1
582
583 /* These function pointer types are here only to allow easier typecasting
584    within the source when we need to cast between data pointers (such as NULL)
585    and function pointers. */
586 typedef CURLcode (*Curl_do_more_func)(struct connectdata *, int *);
587 typedef CURLcode (*Curl_done_func)(struct connectdata *, CURLcode, bool);
588
589 enum expect100 {
590   EXP100_SEND_DATA,           /* enough waiting, just send the body now */
591   EXP100_AWAITING_CONTINUE,   /* waiting for the 100 Continue header */
592   EXP100_SENDING_REQUEST,     /* still sending the request but will wait for
593                                  the 100 header once done with the request */
594   EXP100_FAILED               /* used on 417 Expectation Failed */
595 };
596
597 enum upgrade101 {
598   UPGR101_INIT,               /* default state */
599   UPGR101_REQUESTED,          /* upgrade requested */
600   UPGR101_RECEIVED,           /* response received */
601   UPGR101_WORKING             /* talking upgraded protocol */
602 };
603
604 enum negotiatenpn {
605   NPN_INIT,                   /* default state */
606   NPN_HTTP1_1,                /* HTTP/1.1 negotiated */
607   NPN_HTTP2                   /* HTTP2 (draft-xx) negotiated */
608 };
609
610 /*
611  * Request specific data in the easy handle (SessionHandle).  Previously,
612  * these members were on the connectdata struct but since a conn struct may
613  * now be shared between different SessionHandles, we store connection-specific
614  * data here. This struct only keeps stuff that's interesting for *this*
615  * request, as it will be cleared between multiple ones
616  */
617 struct SingleRequest {
618   curl_off_t size;        /* -1 if unknown at this point */
619   curl_off_t *bytecountp; /* return number of bytes read or NULL */
620
621   curl_off_t maxdownload; /* in bytes, the maximum amount of data to fetch,
622                              -1 means unlimited */
623   curl_off_t *writebytecountp; /* return number of bytes written or NULL */
624
625   curl_off_t bytecount;         /* total number of bytes read */
626   curl_off_t writebytecount;    /* number of bytes written */
627
628   long headerbytecount;         /* only count received headers */
629   long deductheadercount; /* this amount of bytes doesn't count when we check
630                              if anything has been transferred at the end of a
631                              connection. We use this counter to make only a
632                              100 reply (without a following second response
633                              code) result in a CURLE_GOT_NOTHING error code */
634
635   struct timeval start;         /* transfer started at this time */
636   struct timeval now;           /* current time */
637   bool header;                  /* incoming data has HTTP header */
638   enum {
639     HEADER_NORMAL,              /* no bad header at all */
640     HEADER_PARTHEADER,          /* part of the chunk is a bad header, the rest
641                                    is normal data */
642     HEADER_ALLBAD               /* all was believed to be header */
643   } badheader;                  /* the header was deemed bad and will be
644                                    written as body */
645   int headerline;               /* counts header lines to better track the
646                                    first one */
647   char *hbufp;                  /* points at *end* of header line */
648   size_t hbuflen;
649   char *str;                    /* within buf */
650   char *str_start;              /* within buf */
651   char *end_ptr;                /* within buf */
652   char *p;                      /* within headerbuff */
653   bool content_range;           /* set TRUE if Content-Range: was found */
654   curl_off_t offset;            /* possible resume offset read from the
655                                    Content-Range: header */
656   int httpcode;                 /* error code from the 'HTTP/1.? XXX' or
657                                    'RTSP/1.? XXX' line */
658   struct timeval start100;      /* time stamp to wait for the 100 code from */
659   enum expect100 exp100;        /* expect 100 continue state */
660   enum upgrade101 upgr101;      /* 101 upgrade state */
661
662   int auto_decoding;            /* What content encoding. sec 3.5, RFC2616. */
663
664 #define IDENTITY 0              /* No encoding */
665 #define DEFLATE 1               /* zlib deflate [RFC 1950 & 1951] */
666 #define GZIP 2                  /* gzip algorithm [RFC 1952] */
667 #define COMPRESS 3              /* Not handled, added for completeness */
668
669 #ifdef HAVE_LIBZ
670   zlibInitState zlib_init;      /* possible zlib init state;
671                                    undefined if Content-Encoding header. */
672   z_stream z;                   /* State structure for zlib. */
673 #endif
674
675   time_t timeofdoc;
676   long bodywrites;
677
678   char *buf;
679   char *uploadbuf;
680   curl_socket_t maxfd;
681
682   int keepon;
683
684   bool upload_done; /* set to TRUE when doing chunked transfer-encoding upload
685                        and we're uploading the last chunk */
686
687   bool ignorebody;  /* we read a response-body but we ignore it! */
688   bool ignorecl;    /* This HTTP response has no body so we ignore the Content-
689                        Length: header */
690
691   char *location;   /* This points to an allocated version of the Location:
692                        header data */
693   char *newurl;     /* Set to the new URL to use when a redirect or a retry is
694                        wanted */
695
696   /* 'upload_present' is used to keep a byte counter of how much data there is
697      still left in the buffer, aimed for upload. */
698   ssize_t upload_present;
699
700    /* 'upload_fromhere' is used as a read-pointer when we uploaded parts of a
701       buffer, so the next read should read from where this pointer points to,
702       and the 'upload_present' contains the number of bytes available at this
703       position */
704   char *upload_fromhere;
705
706   bool chunk; /* if set, this is a chunked transfer-encoding */
707   bool upload_chunky; /* set TRUE if we are doing chunked transfer-encoding
708                          on upload */
709   bool getheader;     /* TRUE if header parsing is wanted */
710
711   bool forbidchunk;   /* used only to explicitly forbid chunk-upload for
712                          specific upload buffers. See readmoredata() in
713                          http.c for details. */
714
715   void *protop;       /* Allocated protocol-specific data. Each protocol
716                          handler makes sure this points to data it needs. */
717 };
718
719 /*
720  * Specific protocol handler.
721  */
722
723 struct Curl_handler {
724   const char * scheme;        /* URL scheme name. */
725
726   /* Complement to setup_connection_internals(). */
727   CURLcode (*setup_connection)(struct connectdata *);
728
729   /* These two functions MUST be set to be protocol dependent */
730   CURLcode (*do_it)(struct connectdata *, bool *done);
731   Curl_done_func done;
732
733   /* If the curl_do() function is better made in two halves, this
734    * curl_do_more() function will be called afterwards, if set. For example
735    * for doing the FTP stuff after the PASV/PORT command.
736    */
737   Curl_do_more_func do_more;
738
739   /* This function *MAY* be set to a protocol-dependent function that is run
740    * after the connect() and everything is done, as a step in the connection.
741    * The 'done' pointer points to a bool that should be set to TRUE if the
742    * function completes before return. If it doesn't complete, the caller
743    * should call the curl_connecting() function until it is.
744    */
745   CURLcode (*connect_it)(struct connectdata *, bool *done);
746
747   /* See above. Currently only used for FTP. */
748   CURLcode (*connecting)(struct connectdata *, bool *done);
749   CURLcode (*doing)(struct connectdata *, bool *done);
750
751   /* Called from the multi interface during the PROTOCONNECT phase, and it
752      should then return a proper fd set */
753   int (*proto_getsock)(struct connectdata *conn,
754                        curl_socket_t *socks,
755                        int numsocks);
756
757   /* Called from the multi interface during the DOING phase, and it should
758      then return a proper fd set */
759   int (*doing_getsock)(struct connectdata *conn,
760                        curl_socket_t *socks,
761                        int numsocks);
762
763   /* Called from the multi interface during the DO_MORE phase, and it should
764      then return a proper fd set */
765   int (*domore_getsock)(struct connectdata *conn,
766                         curl_socket_t *socks,
767                         int numsocks);
768
769   /* Called from the multi interface during the DO_DONE, PERFORM and
770      WAITPERFORM phases, and it should then return a proper fd set. Not setting
771      this will make libcurl use the generic default one. */
772   int (*perform_getsock)(const struct connectdata *conn,
773                          curl_socket_t *socks,
774                          int numsocks);
775
776   /* This function *MAY* be set to a protocol-dependent function that is run
777    * by the curl_disconnect(), as a step in the disconnection.  If the handler
778    * is called because the connection has been considered dead, dead_connection
779    * is set to TRUE.
780    */
781   CURLcode (*disconnect)(struct connectdata *, bool dead_connection);
782
783   /* If used, this function gets called from transfer.c:readwrite_data() to
784      allow the protocol to do extra reads/writes */
785   CURLcode (*readwrite)(struct SessionHandle *data, struct connectdata *conn,
786                         ssize_t *nread, bool *readmore);
787
788   long defport;           /* Default port. */
789   unsigned int protocol;  /* See CURLPROTO_* - this needs to be the single
790                              specific protocol bit */
791   unsigned int flags;     /* Extra particular characteristics, see PROTOPT_* */
792 };
793
794 #define PROTOPT_NONE 0             /* nothing extra */
795 #define PROTOPT_SSL (1<<0)         /* uses SSL */
796 #define PROTOPT_DUAL (1<<1)        /* this protocol uses two connections */
797 #define PROTOPT_CLOSEACTION (1<<2) /* need action before socket close */
798 /* some protocols will have to call the underlying functions without regard to
799    what exact state the socket signals. IE even if the socket says "readable",
800    the send function might need to be called while uploading, or vice versa.
801 */
802 #define PROTOPT_DIRLOCK (1<<3)
803 #define PROTOPT_NONETWORK (1<<4)   /* protocol doesn't use the network! */
804 #define PROTOPT_NEEDSPWD (1<<5)    /* needs a password, and if none is set it
805                                       gets a default */
806 #define PROTOPT_NOURLQUERY (1<<6)   /* protocol can't handle
807                                         url query strings (?foo=bar) ! */
808 #define PROTOPT_CREDSPERREQUEST (1<<7) /* requires login credentials per
809                                           request instead of per connection */
810
811
812 /* return the count of bytes sent, or -1 on error */
813 typedef ssize_t (Curl_send)(struct connectdata *conn, /* connection data */
814                             int sockindex,            /* socketindex */
815                             const void *buf,          /* data to write */
816                             size_t len,               /* max amount to write */
817                             CURLcode *err);           /* error to return */
818
819 /* return the count of bytes read, or -1 on error */
820 typedef ssize_t (Curl_recv)(struct connectdata *conn, /* connection data */
821                             int sockindex,            /* socketindex */
822                             char *buf,                /* store data here */
823                             size_t len,               /* max amount to read */
824                             CURLcode *err);           /* error to return */
825
826 /*
827  * The connectdata struct contains all fields and variables that should be
828  * unique for an entire connection.
829  */
830 struct connectdata {
831   /* 'data' is the CURRENT SessionHandle using this connection -- take great
832      caution that this might very well vary between different times this
833      connection is used! */
834   struct SessionHandle *data;
835
836   /* chunk is for HTTP chunked encoding, but is in the general connectdata
837      struct only because we can do just about any protocol through a HTTP proxy
838      and a HTTP proxy may in fact respond using chunked encoding */
839   struct Curl_chunker chunk;
840
841   curl_closesocket_callback fclosesocket; /* function closing the socket(s) */
842   void *closesocket_client;
843
844   bool inuse; /* This is a marker for the connection cache logic. If this is
845                  TRUE this handle is being used by an easy handle and cannot
846                  be used by any other easy handle without careful
847                  consideration (== only for pipelining). */
848
849   /**** Fields set when inited and not modified again */
850   long connection_id; /* Contains a unique number to make it easier to
851                          track the connections in the log output */
852
853   /* 'dns_entry' is the particular host we use. This points to an entry in the
854      DNS cache and it will not get pruned while locked. It gets unlocked in
855      Curl_done(). This entry will be NULL if the connection is re-used as then
856      there is no name resolve done. */
857   struct Curl_dns_entry *dns_entry;
858
859   /* 'ip_addr' is the particular IP we connected to. It points to a struct
860      within the DNS cache, so this pointer is only valid as long as the DNS
861      cache entry remains locked. It gets unlocked in Curl_done() */
862   Curl_addrinfo *ip_addr;
863   Curl_addrinfo *tempaddr[2]; /* for happy eyeballs */
864
865   /* 'ip_addr_str' is the ip_addr data as a human readable string.
866      It remains available as long as the connection does, which is longer than
867      the ip_addr itself. */
868   char ip_addr_str[MAX_IPADR_LEN];
869
870   unsigned int scope;    /* address scope for IPv6 */
871
872   int socktype;  /* SOCK_STREAM or SOCK_DGRAM */
873
874   struct hostname host;
875   struct hostname proxy;
876
877   long port;       /* which port to use locally */
878   int remote_port; /* what remote port to connect to, not the proxy port! */
879
880   /* 'primary_ip' and 'primary_port' get filled with peer's numerical
881      ip address and port number whenever an outgoing connection is
882      *attempted* from the primary socket to a remote address. When more
883      than one address is tried for a connection these will hold data
884      for the last attempt. When the connection is actually established
885      these are updated with data which comes directly from the socket. */
886
887   char primary_ip[MAX_IPADR_LEN];
888   long primary_port;
889
890   /* 'local_ip' and 'local_port' get filled with local's numerical
891      ip address and port number whenever an outgoing connection is
892      **established** from the primary socket to a remote address. */
893
894   char local_ip[MAX_IPADR_LEN];
895   long local_port;
896
897   char *user;    /* user name string, allocated */
898   char *passwd;  /* password string, allocated */
899   char *options; /* options string, allocated */
900
901   char *xoauth2_bearer; /* bearer token for xoauth2, allocated */
902
903   char *proxyuser;    /* proxy user name string, allocated */
904   char *proxypasswd;  /* proxy password string, allocated */
905   curl_proxytype proxytype; /* what kind of proxy that is in use */
906
907   int httpversion;        /* the HTTP version*10 reported by the server */
908   int rtspversion;        /* the RTSP version*10 reported by the server */
909
910   struct timeval now;     /* "current" time */
911   struct timeval created; /* creation time */
912   curl_socket_t sock[2]; /* two sockets, the second is used for the data
913                             transfer when doing FTP */
914   curl_socket_t tempsock[2]; /* temporary sockets for happy eyeballs */
915   bool sock_accepted[2]; /* TRUE if the socket on this index was created with
916                             accept() */
917   Curl_recv *recv[2];
918   Curl_send *send[2];
919
920   struct ssl_connect_data ssl[2]; /* this is for ssl-stuff */
921   struct ssl_config_data ssl_config;
922
923   struct ConnectBits bits;    /* various state-flags for this connection */
924
925  /* connecttime: when connect() is called on the current IP address. Used to
926     be able to track when to move on to try next IP - but only when the multi
927     interface is used. */
928   struct timeval connecttime;
929   /* The two fields below get set in Curl_connecthost */
930   int num_addr; /* number of addresses to try to connect to */
931   long timeoutms_per_addr; /* how long time in milliseconds to spend on
932                               trying to connect to each IP address */
933
934   const struct Curl_handler *handler; /* Connection's protocol handler */
935   const struct Curl_handler *given;   /* The protocol first given */
936
937   long ip_version; /* copied from the SessionHandle at creation time */
938
939   /**** curl_get() phase fields */
940
941   curl_socket_t sockfd;   /* socket to read from or CURL_SOCKET_BAD */
942   curl_socket_t writesockfd; /* socket to write to, it may very
943                                 well be the same we read from.
944                                 CURL_SOCKET_BAD disables */
945
946   /** Dynamicly allocated strings, MUST be freed before this **/
947   /** struct is killed.                                      **/
948   struct dynamically_allocated_data {
949     char *proxyuserpwd;
950     char *uagent;
951     char *accept_encoding;
952     char *userpwd;
953     char *rangeline;
954     char *ref;
955     char *host;
956     char *cookiehost;
957     char *rtsp_transport;
958     char *te; /* TE: request header */
959   } allocptr;
960
961   int sec_complete; /* if kerberos is enabled for this connection */
962 #ifdef HAVE_GSSAPI
963   enum protection_level command_prot;
964   enum protection_level data_prot;
965   enum protection_level request_data_prot;
966   size_t buffer_size;
967   struct krb5buffer in_buffer;
968   void *app_data;
969   const struct Curl_sec_client_mech *mech;
970   struct sockaddr_in local_addr;
971 #endif
972
973   /* the two following *_inuse fields are only flags, not counters in any way.
974      If TRUE it means the channel is in use, and if FALSE it means the channel
975      is up for grabs by one. */
976
977   bool readchannel_inuse;  /* whether the read channel is in use by an easy
978                               handle */
979   bool writechannel_inuse; /* whether the write channel is in use by an easy
980                               handle */
981   struct curl_llist *send_pipe; /* List of handles waiting to
982                                    send on this pipeline */
983   struct curl_llist *recv_pipe; /* List of handles waiting to read
984                                    their responses on this pipeline */
985   char* master_buffer; /* The master buffer allocated on-demand;
986                           used for pipelining. */
987   size_t read_pos; /* Current read position in the master buffer */
988   size_t buf_len; /* Length of the buffer?? */
989
990
991   curl_seek_callback seek_func; /* function that seeks the input */
992   void *seek_client;            /* pointer to pass to the seek() above */
993
994   /*************** Request - specific items ************/
995
996   /* previously this was in the urldata struct */
997   curl_read_callback fread_func; /* function that reads the input */
998   void *fread_in;           /* pointer to pass to the fread() above */
999
1000   struct ntlmdata ntlm;     /* NTLM differs from other authentication schemes
1001                                because it authenticates connections, not
1002                                single requests! */
1003   struct ntlmdata proxyntlm; /* NTLM data for proxy */
1004
1005 #if defined(USE_NTLM) && defined(NTLM_WB_ENABLED)
1006   /* used for communication with Samba's winbind daemon helper ntlm_auth */
1007   curl_socket_t ntlm_auth_hlpr_socket;
1008   pid_t ntlm_auth_hlpr_pid;
1009   char* challenge_header;
1010   char* response_header;
1011 #endif
1012
1013   char syserr_buf [256]; /* buffer for Curl_strerror() */
1014
1015 #ifdef CURLRES_ASYNCH
1016   /* data used for the asynch name resolve callback */
1017   struct Curl_async async;
1018 #endif
1019
1020   /* These three are used for chunked-encoding trailer support */
1021   char *trailer; /* allocated buffer to store trailer in */
1022   int trlMax;    /* allocated buffer size */
1023   int trlPos;    /* index of where to store data */
1024
1025   union {
1026     struct ftp_conn ftpc;
1027     struct http_conn httpc;
1028     struct ssh_conn sshc;
1029     struct tftp_state_data *tftpc;
1030     struct imap_conn imapc;
1031     struct pop3_conn pop3c;
1032     struct smtp_conn smtpc;
1033     struct rtsp_conn rtspc;
1034     void *generic; /* RTMP and LDAP use this */
1035   } proto;
1036
1037   int cselect_bits; /* bitmask of socket events */
1038   int waitfor;      /* current READ/WRITE bits to wait for */
1039
1040 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1041   int socks5_gssapi_enctype;
1042 #endif
1043
1044   bool verifypeer;
1045   bool verifyhost;
1046
1047   /* When this connection is created, store the conditions for the local end
1048      bind. This is stored before the actual bind and before any connection is
1049      made and will serve the purpose of being used for comparison reasons so
1050      that subsequent bound-requested connections aren't accidentally re-using
1051      wrong connections. */
1052   char *localdev;
1053   unsigned short localport;
1054   int localportrange;
1055
1056   /* tunnel as in tunnel through a HTTP proxy with CONNECT */
1057   enum {
1058     TUNNEL_INIT,    /* init/default/no tunnel state */
1059     TUNNEL_CONNECT, /* CONNECT has been sent off */
1060     TUNNEL_COMPLETE /* CONNECT response received completely */
1061   } tunnel_state[2]; /* two separate ones to allow FTP */
1062   struct connectbundle *bundle; /* The bundle we are member of */
1063
1064   enum negotiatenpn negnpn;
1065 };
1066
1067 /* The end of connectdata. */
1068
1069 /*
1070  * Struct to keep statistical and informational data.
1071  */
1072 struct PureInfo {
1073   int httpcode;  /* Recent HTTP, FTP, or RTSP response code */
1074   int httpproxycode; /* response code from proxy when received separate */
1075   int httpversion; /* the http version number X.Y = X*10+Y */
1076   long filetime; /* If requested, this is might get set. Set to -1 if the time
1077                     was unretrievable. We cannot have this of type time_t,
1078                     since time_t is unsigned on several platforms such as
1079                     OpenVMS. */
1080   bool timecond;  /* set to TRUE if the time condition didn't match, which
1081                      thus made the document NOT get fetched */
1082   long header_size;  /* size of read header(s) in bytes */
1083   long request_size; /* the amount of bytes sent in the request(s) */
1084   unsigned long proxyauthavail; /* what proxy auth types were announced */
1085   unsigned long httpauthavail;  /* what host auth types were announced */
1086   long numconnects; /* how many new connection did libcurl created */
1087   char *contenttype; /* the content type of the object */
1088   char *wouldredirect; /* URL this would've been redirected to if asked to */
1089
1090   /* PureInfo members 'conn_primary_ip', 'conn_primary_port', 'conn_local_ip'
1091      and, 'conn_local_port' are copied over from the connectdata struct in
1092      order to allow curl_easy_getinfo() to return this information even when
1093      the session handle is no longer associated with a connection, and also
1094      allow curl_easy_reset() to clear this information from the session handle
1095      without disturbing information which is still alive, and that might be
1096      reused, in the connection cache. */
1097
1098   char conn_primary_ip[MAX_IPADR_LEN];
1099   long conn_primary_port;
1100
1101   char conn_local_ip[MAX_IPADR_LEN];
1102   long conn_local_port;
1103
1104   struct curl_certinfo certs; /* info about the certs, only populated in
1105                                  OpenSSL builds. Asked for with
1106                                  CURLOPT_CERTINFO / CURLINFO_CERTINFO */
1107 };
1108
1109
1110 struct Progress {
1111   long lastshow; /* time() of the last displayed progress meter or NULL to
1112                     force redraw at next call */
1113   curl_off_t size_dl; /* total expected size */
1114   curl_off_t size_ul; /* total expected size */
1115   curl_off_t downloaded; /* transferred so far */
1116   curl_off_t uploaded; /* transferred so far */
1117
1118   curl_off_t current_speed; /* uses the currently fastest transfer */
1119
1120   bool callback;  /* set when progress callback is used */
1121   int width; /* screen width at download start */
1122   int flags; /* see progress.h */
1123
1124   double timespent;
1125
1126   curl_off_t dlspeed;
1127   curl_off_t ulspeed;
1128
1129   double t_nslookup;
1130   double t_connect;
1131   double t_appconnect;
1132   double t_pretransfer;
1133   double t_starttransfer;
1134   double t_redirect;
1135
1136   struct timeval start;
1137   struct timeval t_startsingle;
1138   struct timeval t_startop;
1139   struct timeval t_acceptdata;
1140 #define CURR_TIME (5+1) /* 6 entries for 5 seconds */
1141
1142   curl_off_t speeder[ CURR_TIME ];
1143   struct timeval speeder_time[ CURR_TIME ];
1144   int speeder_c;
1145 };
1146
1147 typedef enum {
1148   HTTPREQ_NONE, /* first in list */
1149   HTTPREQ_GET,
1150   HTTPREQ_POST,
1151   HTTPREQ_POST_FORM, /* we make a difference internally */
1152   HTTPREQ_PUT,
1153   HTTPREQ_HEAD,
1154   HTTPREQ_CUSTOM,
1155   HTTPREQ_LAST /* last in list */
1156 } Curl_HttpReq;
1157
1158 typedef enum {
1159     RTSPREQ_NONE, /* first in list */
1160     RTSPREQ_OPTIONS,
1161     RTSPREQ_DESCRIBE,
1162     RTSPREQ_ANNOUNCE,
1163     RTSPREQ_SETUP,
1164     RTSPREQ_PLAY,
1165     RTSPREQ_PAUSE,
1166     RTSPREQ_TEARDOWN,
1167     RTSPREQ_GET_PARAMETER,
1168     RTSPREQ_SET_PARAMETER,
1169     RTSPREQ_RECORD,
1170     RTSPREQ_RECEIVE,
1171     RTSPREQ_LAST /* last in list */
1172 } Curl_RtspReq;
1173
1174 /*
1175  * Values that are generated, temporary or calculated internally for a
1176  * "session handle" must be defined within the 'struct UrlState'.  This struct
1177  * will be used within the SessionHandle struct. When the 'SessionHandle'
1178  * struct is cloned, this data MUST NOT be copied.
1179  *
1180  * Remember that any "state" information goes globally for the curl handle.
1181  * Session-data MUST be put in the connectdata struct and here.  */
1182 #define MAX_CURL_USER_LENGTH 256
1183 #define MAX_CURL_PASSWORD_LENGTH 256
1184
1185 struct auth {
1186   unsigned long want;  /* Bitmask set to the authentication methods wanted by
1187                           app (with CURLOPT_HTTPAUTH or CURLOPT_PROXYAUTH). */
1188   unsigned long picked;
1189   unsigned long avail; /* Bitmask for what the server reports to support for
1190                           this resource */
1191   bool done;  /* TRUE when the auth phase is done and ready to do the *actual*
1192                  request */
1193   bool multi; /* TRUE if this is not yet authenticated but within the auth
1194                  multipass negotiation */
1195   bool iestyle; /* TRUE if digest should be done IE-style or FALSE if it should
1196                    be RFC compliant */
1197 };
1198
1199 struct UrlState {
1200
1201   /* Points to the connection cache */
1202   struct conncache *conn_cache;
1203
1204   /* when curl_easy_perform() is called, the multi handle is "owned" by
1205      the easy handle so curl_easy_cleanup() on such an easy handle will
1206      also close the multi handle! */
1207   bool multi_owned_by_easy;
1208
1209   /* buffers to store authentication data in, as parsed from input options */
1210   struct timeval keeps_speed; /* for the progress meter really */
1211
1212   struct connectdata *lastconnect; /* The last connection, NULL if undefined */
1213
1214   char *headerbuff; /* allocated buffer to store headers in */
1215   size_t headersize;   /* size of the allocation */
1216
1217   char buffer[BUFSIZE+1]; /* download buffer */
1218   char uploadbuffer[BUFSIZE+1]; /* upload buffer */
1219   curl_off_t current_speed;  /* the ProgressShow() funcion sets this,
1220                                 bytes / second */
1221   bool this_is_a_follow; /* this is a followed Location: request */
1222
1223   char *first_host; /* if set, this should be the host name that we will
1224                        sent authorization to, no else. Used to make Location:
1225                        following not keep sending user+password... This is
1226                        strdup() data.
1227                     */
1228   struct curl_ssl_session *session; /* array of 'max_ssl_sessions' size */
1229   long sessionage;                  /* number of the most recent session */
1230   char *tempwrite;      /* allocated buffer to keep data in when a write
1231                            callback returns to make the connection paused */
1232   size_t tempwritesize; /* size of the 'tempwrite' allocated buffer */
1233   int tempwritetype;    /* type of the 'tempwrite' buffer as a bitmask that is
1234                            used with Curl_client_write() */
1235   char *scratch; /* huge buffer[BUFSIZE*2] when doing upload CRLF replacing */
1236   bool errorbuf; /* Set to TRUE if the error buffer is already filled in.
1237                     This must be set to FALSE every time _easy_perform() is
1238                     called. */
1239   int os_errno;  /* filled in with errno whenever an error occurs */
1240 #ifdef HAVE_SIGNAL
1241   /* storage for the previous bag^H^H^HSIGPIPE signal handler :-) */
1242   void (*prev_signal)(int sig);
1243 #endif
1244   bool allow_port; /* Is set.use_port allowed to take effect or not. This
1245                       is always set TRUE when curl_easy_perform() is called. */
1246   struct digestdata digest;      /* state data for host Digest auth */
1247   struct digestdata proxydigest; /* state data for proxy Digest auth */
1248
1249 #ifdef USE_HTTP_NEGOTIATE
1250   struct negotiatedata negotiate; /* state data for host Negotiate auth */
1251   struct negotiatedata proxyneg; /* state data for proxy Negotiate auth */
1252 #endif
1253
1254   struct auth authhost;  /* auth details for host */
1255   struct auth authproxy; /* auth details for proxy */
1256
1257   bool authproblem; /* TRUE if there's some problem authenticating */
1258
1259   void *resolver; /* resolver state, if it is used in the URL state -
1260                      ares_channel f.e. */
1261
1262 #if defined(USE_SSLEAY) && defined(HAVE_OPENSSL_ENGINE_H)
1263   ENGINE *engine;
1264 #endif /* USE_SSLEAY */
1265   struct timeval expiretime; /* set this with Curl_expire() only */
1266   struct Curl_tree timenode; /* for the splay stuff */
1267   struct curl_llist *timeoutlist; /* list of pending timeouts */
1268
1269   /* a place to store the most recently set FTP entrypath */
1270   char *most_recent_ftp_entrypath;
1271
1272   /* set after initial USER failure, to prevent an authentication loop */
1273   bool ftp_trying_alternative;
1274
1275   int httpversion;       /* the lowest HTTP version*10 reported by any server
1276                             involved in this request */
1277   bool expect100header;  /* TRUE if we added Expect: 100-continue */
1278
1279   bool pipe_broke; /* TRUE if the connection we were pipelined on broke
1280                       and we need to restart from the beginning */
1281
1282 #if !defined(WIN32) && !defined(MSDOS) && !defined(__EMX__) && \
1283     !defined(__SYMBIAN32__)
1284 /* do FTP line-end conversions on most platforms */
1285 #define CURL_DO_LINEEND_CONV
1286   /* for FTP downloads: track CRLF sequences that span blocks */
1287   bool prev_block_had_trailing_cr;
1288   /* for FTP downloads: how many CRLFs did we converted to LFs? */
1289   curl_off_t crlf_conversions;
1290 #endif
1291   char *pathbuffer;/* allocated buffer to store the URL's path part in */
1292   char *path;      /* path to use, points to somewhere within the pathbuffer
1293                       area */
1294   bool slash_removed; /* set TRUE if the 'path' points to a path where the
1295                          initial URL slash separator has been taken off */
1296   bool use_range;
1297   bool rangestringalloc; /* the range string is malloc()'ed */
1298
1299   char *range; /* range, if used. See README for detailed specification on
1300                   this syntax. */
1301   curl_off_t resume_from; /* continue [ftp] transfer from here */
1302
1303   /* This RTSP state information survives requests and connections */
1304   long rtsp_next_client_CSeq; /* the session's next client CSeq */
1305   long rtsp_next_server_CSeq; /* the session's next server CSeq */
1306   long rtsp_CSeq_recv; /* most recent CSeq received */
1307
1308   /* if true, force SSL connection retry (workaround for certain servers) */
1309   bool ssl_connect_retry;
1310   curl_off_t infilesize; /* size of file to upload, -1 means unknown.
1311                             Copied from set.filesize at start of operation */
1312 };
1313
1314
1315 /*
1316  * This 'DynamicStatic' struct defines dynamic states that actually change
1317  * values in the 'UserDefined' area, which MUST be taken into consideration
1318  * if the UserDefined struct is cloned or similar. You can probably just
1319  * copy these, but each one indicate a special action on other data.
1320  */
1321
1322 struct DynamicStatic {
1323   char *url;        /* work URL, copied from UserDefined */
1324   bool url_alloc;   /* URL string is malloc()'ed */
1325   char *referer;    /* referer string */
1326   bool referer_alloc; /* referer sting is malloc()ed */
1327   struct curl_slist *cookielist; /* list of cookie files set by
1328                                     curl_easy_setopt(COOKIEFILE) calls */
1329   struct curl_slist *resolve; /* set to point to the set.resolve list when
1330                                  this should be dealt with in pretransfer */
1331 };
1332
1333 /*
1334  * This 'UserDefined' struct must only contain data that is set once to go
1335  * for many (perhaps) independent connections. Values that are generated or
1336  * calculated internally for the "session handle" MUST be defined within the
1337  * 'struct UrlState' instead. The only exceptions MUST note the changes in
1338  * the 'DynamicStatic' struct.
1339  * Character pointer fields point to dynamic storage, unless otherwise stated.
1340  */
1341
1342 struct Curl_multi;    /* declared and used only in multi.c */
1343
1344 enum dupstring {
1345   STRING_CERT,            /* client certificate file name */
1346   STRING_CERT_TYPE,       /* format for certificate (default: PEM)*/
1347   STRING_COOKIE,          /* HTTP cookie string to send */
1348   STRING_COOKIEJAR,       /* dump all cookies to this file */
1349   STRING_CUSTOMREQUEST,   /* HTTP/FTP/RTSP request/method to use */
1350   STRING_DEVICE,          /* local network interface/address to use */
1351   STRING_ENCODING,        /* Accept-Encoding string */
1352   STRING_FTP_ACCOUNT,     /* ftp account data */
1353   STRING_FTP_ALTERNATIVE_TO_USER, /* command to send if USER/PASS fails */
1354   STRING_FTPPORT,         /* port to send with the FTP PORT command */
1355   STRING_KEY,             /* private key file name */
1356   STRING_KEY_PASSWD,      /* plain text private key password */
1357   STRING_KEY_TYPE,        /* format for private key (default: PEM) */
1358   STRING_KRB_LEVEL,       /* krb security level */
1359   STRING_NETRC_FILE,      /* if not NULL, use this instead of trying to find
1360                              $HOME/.netrc */
1361   STRING_COPYPOSTFIELDS,  /* if POST, set the fields' values here */
1362   STRING_PROXY,           /* proxy to use */
1363   STRING_SET_RANGE,       /* range, if used */
1364   STRING_SET_REFERER,     /* custom string for the HTTP referer field */
1365   STRING_SET_URL,         /* what original URL to work on */
1366   STRING_SSL_CAPATH,      /* CA directory name (doesn't work on windows) */
1367   STRING_SSL_CAFILE,      /* certificate file to verify peer against */
1368   STRING_SSL_CIPHER_LIST, /* list of ciphers to use */
1369   STRING_SSL_EGDSOCKET,   /* path to file containing the EGD daemon socket */
1370   STRING_SSL_RANDOM_FILE, /* path to file containing "random" data */
1371   STRING_USERAGENT,       /* User-Agent string */
1372   STRING_SSL_CRLFILE,     /* crl file to check certificate */
1373   STRING_SSL_ISSUERCERT,  /* issuer cert file to check certificate */
1374   STRING_USERNAME,        /* <username>, if used */
1375   STRING_PASSWORD,        /* <password>, if used */
1376   STRING_OPTIONS,         /* <options>, if used */
1377   STRING_PROXYUSERNAME,   /* Proxy <username>, if used */
1378   STRING_PROXYPASSWORD,   /* Proxy <password>, if used */
1379   STRING_NOPROXY,         /* List of hosts which should not use the proxy, if
1380                              used */
1381   STRING_RTSP_SESSION_ID, /* Session ID to use */
1382   STRING_RTSP_STREAM_URI, /* Stream URI for this request */
1383   STRING_RTSP_TRANSPORT,  /* Transport for this session */
1384 #ifdef USE_LIBSSH2
1385   STRING_SSH_PRIVATE_KEY, /* path to the private key file for auth */
1386   STRING_SSH_PUBLIC_KEY,  /* path to the public key file for auth */
1387   STRING_SSH_HOST_PUBLIC_KEY_MD5, /* md5 of host public key in ascii hex */
1388   STRING_SSH_KNOWNHOSTS,  /* file name of knownhosts file */
1389 #endif
1390 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1391   STRING_SOCKS5_GSSAPI_SERVICE,  /* GSSAPI service name */
1392 #endif
1393   STRING_MAIL_FROM,
1394   STRING_MAIL_AUTH,
1395
1396 #ifdef USE_TLS_SRP
1397   STRING_TLSAUTH_USERNAME,     /* TLS auth <username> */
1398   STRING_TLSAUTH_PASSWORD,     /* TLS auth <password> */
1399 #endif
1400
1401   STRING_BEARER,          /* <bearer>, if used */
1402
1403   /* -- end of strings -- */
1404   STRING_LAST /* not used, just an end-of-list marker */
1405 };
1406
1407 struct UserDefined {
1408   FILE *err;         /* the stderr user data goes here */
1409   void *debugdata;   /* the data that will be passed to fdebug */
1410   char *errorbuffer; /* (Static) store failure messages in here */
1411   long proxyport; /* If non-zero, use this port number by default. If the
1412                      proxy string features a ":[port]" that one will override
1413                      this. */
1414   void *out;         /* the fetched file goes here */
1415   void *in;          /* the uploaded file is read from here */
1416   void *writeheader; /* write the header to this if non-NULL */
1417   void *rtp_out;     /* write RTP to this if non-NULL */
1418   long use_port;     /* which port to use (when not using default) */
1419   unsigned long httpauth;  /* kind of HTTP authentication to use (bitmask) */
1420   unsigned long proxyauth; /* kind of proxy authentication to use (bitmask) */
1421   long followlocation; /* as in HTTP Location: */
1422   long maxredirs;    /* maximum no. of http(s) redirects to follow, set to -1
1423                         for infinity */
1424
1425   int keep_post;     /* keep POSTs as POSTs after a 30x request; each
1426                         bit represents a request, from 301 to 303 */
1427   bool free_referer; /* set TRUE if 'referer' points to a string we
1428                         allocated */
1429   void *postfields;  /* if POST, set the fields' values here */
1430   curl_seek_callback seek_func;      /* function that seeks the input */
1431   curl_off_t postfieldsize; /* if POST, this might have a size to use instead
1432                                of strlen(), and then the data *may* be binary
1433                                (contain zero bytes) */
1434   unsigned short localport; /* local port number to bind to */
1435   int localportrange; /* number of additional port numbers to test in case the
1436                          'localport' one can't be bind()ed */
1437   curl_write_callback fwrite_func;   /* function that stores the output */
1438   curl_write_callback fwrite_header; /* function that stores headers */
1439   curl_write_callback fwrite_rtp;    /* function that stores interleaved RTP */
1440   curl_read_callback fread_func;     /* function that reads the input */
1441   int is_fread_set; /* boolean, has read callback been set to non-NULL? */
1442   int is_fwrite_set; /* boolean, has write callback been set to non-NULL? */
1443   curl_progress_callback fprogress; /* OLD and deprecated progress callback  */
1444   curl_xferinfo_callback fxferinfo; /* progress callback */
1445   curl_debug_callback fdebug;      /* function that write informational data */
1446   curl_ioctl_callback ioctl_func;  /* function for I/O control */
1447   curl_sockopt_callback fsockopt;  /* function for setting socket options */
1448   void *sockopt_client; /* pointer to pass to the socket options callback */
1449   curl_opensocket_callback fopensocket; /* function for checking/translating
1450                                            the address and opening the
1451                                            socket */
1452   void* opensocket_client;
1453   curl_closesocket_callback fclosesocket; /* function for closing the
1454                                              socket */
1455   void* closesocket_client;
1456
1457   void *seek_client;    /* pointer to pass to the seek callback */
1458   /* the 3 curl_conv_callback functions below are used on non-ASCII hosts */
1459   /* function to convert from the network encoding: */
1460   curl_conv_callback convfromnetwork;
1461   /* function to convert to the network encoding: */
1462   curl_conv_callback convtonetwork;
1463   /* function to convert from UTF-8 encoding: */
1464   curl_conv_callback convfromutf8;
1465
1466   void *progress_client; /* pointer to pass to the progress callback */
1467   void *ioctl_client;   /* pointer to pass to the ioctl callback */
1468   long timeout;         /* in milliseconds, 0 means no timeout */
1469   long connecttimeout;  /* in milliseconds, 0 means no timeout */
1470   long accepttimeout;   /* in milliseconds, 0 means no timeout */
1471   long server_response_timeout; /* in milliseconds, 0 means no timeout */
1472   long tftp_blksize ; /* in bytes, 0 means use default */
1473   curl_off_t filesize;  /* size of file to upload, -1 means unknown */
1474   long low_speed_limit; /* bytes/second */
1475   long low_speed_time;  /* number of seconds */
1476   curl_off_t max_send_speed; /* high speed limit in bytes/second for upload */
1477   curl_off_t max_recv_speed; /* high speed limit in bytes/second for
1478                                 download */
1479   curl_off_t set_resume_from;  /* continue [ftp] transfer from here */
1480   struct curl_slist *headers; /* linked list of extra headers */
1481   struct curl_slist *proxyheaders; /* linked list of extra CONNECT headers */
1482   struct curl_httppost *httppost;  /* linked list of POST data */
1483   bool sep_headers;     /* handle host and proxy headers separately */
1484   bool cookiesession;   /* new cookie session? */
1485   bool crlf;            /* convert crlf on ftp upload(?) */
1486   struct curl_slist *quote;     /* after connection is established */
1487   struct curl_slist *postquote; /* after the transfer */
1488   struct curl_slist *prequote; /* before the transfer, after type */
1489   struct curl_slist *source_quote;  /* 3rd party quote */
1490   struct curl_slist *source_prequote;  /* in 3rd party transfer mode - before
1491                                           the transfer on source host */
1492   struct curl_slist *source_postquote; /* in 3rd party transfer mode - after
1493                                           the transfer on source host */
1494   struct curl_slist *telnet_options; /* linked list of telnet options */
1495   struct curl_slist *resolve;     /* list of names to add/remove from
1496                                      DNS cache */
1497   curl_TimeCond timecondition; /* kind of time/date comparison */
1498   time_t timevalue;       /* what time to compare with */
1499   Curl_HttpReq httpreq;   /* what kind of HTTP request (if any) is this */
1500   long httpversion; /* when non-zero, a specific HTTP version requested to
1501                        be used in the library's request(s) */
1502   struct ssl_config_data ssl;  /* user defined SSL stuff */
1503   curl_proxytype proxytype; /* what kind of proxy that is in use */
1504   long dns_cache_timeout; /* DNS cache timeout */
1505   long buffer_size;      /* size of receive buffer to use */
1506   void *private_data; /* application-private data */
1507
1508   struct curl_slist *http200aliases; /* linked list of aliases for http200 */
1509
1510   long ipver; /* the CURL_IPRESOLVE_* defines in the public header file
1511                  0 - whatever, 1 - v2, 2 - v6 */
1512
1513   curl_off_t max_filesize; /* Maximum file size to download */
1514
1515   curl_ftpfile ftp_filemethod; /* how to get to a file when FTP is used  */
1516
1517   int ftp_create_missing_dirs; /* 1 - create directories that don't exist
1518                                   2 - the same but also allow MKD to fail once
1519                                */
1520
1521   curl_sshkeycallback ssh_keyfunc; /* key matching callback */
1522   void *ssh_keyfunc_userp;         /* custom pointer to callback */
1523
1524 /* Here follows boolean settings that define how to behave during
1525    this session. They are STATIC, set by libcurl users or at least initially
1526    and they don't change during operations. */
1527
1528   bool printhost;        /* printing host name in debug info */
1529   bool get_filetime;     /* get the time and get of the remote file */
1530   bool tunnel_thru_httpproxy; /* use CONNECT through a HTTP proxy */
1531   bool prefer_ascii;     /* ASCII rather than binary */
1532   bool ftp_append;       /* append, not overwrite, on upload */
1533   bool ftp_list_only;    /* switch FTP command for listing directories */
1534   bool ftp_use_port;     /* use the FTP PORT command */
1535   bool hide_progress;    /* don't use the progress meter */
1536   bool http_fail_on_error;  /* fail on HTTP error codes >= 300 */
1537   bool http_follow_location; /* follow HTTP redirects */
1538   bool http_transfer_encoding; /* request compressed HTTP transfer-encoding */
1539   bool http_disable_hostname_check_before_authentication;
1540   bool include_header;   /* include received protocol headers in data output */
1541   bool http_set_referer; /* is a custom referer used */
1542   bool http_auto_referer; /* set "correct" referer when following location: */
1543   bool opt_no_body;      /* as set with CURLOPT_NOBODY */
1544   bool set_port;         /* custom port number used */
1545   bool upload;           /* upload request */
1546   enum CURL_NETRC_OPTION
1547        use_netrc;        /* defined in include/curl.h */
1548   bool verbose;          /* output verbosity */
1549   bool krb;              /* kerberos connection requested */
1550   bool reuse_forbid;     /* forbidden to be reused, close after use */
1551   bool reuse_fresh;      /* do not re-use an existing connection  */
1552   bool ftp_use_epsv;     /* if EPSV is to be attempted or not */
1553   bool ftp_use_eprt;     /* if EPRT is to be attempted or not */
1554   bool ftp_use_pret;     /* if PRET is to be used before PASV or not */
1555
1556   curl_usessl use_ssl;   /* if AUTH TLS is to be attempted etc, for FTP or
1557                             IMAP or POP3 or others! */
1558   curl_ftpauth ftpsslauth; /* what AUTH XXX to be attempted */
1559   curl_ftpccc ftp_ccc;   /* FTP CCC options */
1560   bool no_signal;        /* do not use any signal/alarm handler */
1561   bool global_dns_cache; /* subject for future removal */
1562   bool tcp_nodelay;      /* whether to enable TCP_NODELAY or not */
1563   bool ignorecl;         /* ignore content length */
1564   bool ftp_skip_ip;      /* skip the IP address the FTP server passes on to
1565                             us */
1566   bool connect_only;     /* make connection, let application use the socket */
1567   bool ssl_enable_beast; /* especially allow this flaw for interoperability's
1568                             sake*/
1569   long ssh_auth_types;   /* allowed SSH auth types */
1570   bool http_te_skip;     /* pass the raw body data to the user, even when
1571                             transfer-encoded (chunked, compressed) */
1572   bool http_ce_skip;     /* pass the raw body data to the user, even when
1573                             content-encoded (chunked, compressed) */
1574   long new_file_perms;    /* Permissions to use when creating remote files */
1575   long new_directory_perms; /* Permissions to use when creating remote dirs */
1576   bool proxy_transfer_mode; /* set transfer mode (;type=<a|i>) when doing FTP
1577                                via an HTTP proxy */
1578   char *str[STRING_LAST]; /* array of strings, pointing to allocated memory */
1579   unsigned int scope;    /* address scope for IPv6 */
1580   long allowed_protocols;
1581   long redir_protocols;
1582 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1583   long socks5_gssapi_nec; /* flag to support nec socks5 server */
1584 #endif
1585   struct curl_slist *mail_rcpt; /* linked list of mail recipients */
1586   bool sasl_ir;         /* Enable/disable SASL initial response */
1587   /* Common RTSP header options */
1588   Curl_RtspReq rtspreq; /* RTSP request type */
1589   long rtspversion; /* like httpversion, for RTSP */
1590   bool wildcardmatch; /* enable wildcard matching */
1591   curl_chunk_bgn_callback chunk_bgn; /* called before part of transfer
1592                                         starts */
1593   curl_chunk_end_callback chunk_end; /* called after part transferring
1594                                         stopped */
1595   curl_fnmatch_callback fnmatch; /* callback to decide which file corresponds
1596                                     to pattern (e.g. if WILDCARDMATCH is on) */
1597   void *fnmatch_data;
1598
1599   long gssapi_delegation; /* GSSAPI credential delegation, see the
1600                              documentation of CURLOPT_GSSAPI_DELEGATION */
1601
1602   bool tcp_keepalive;    /* use TCP keepalives */
1603   long tcp_keepidle;     /* seconds in idle before sending keepalive probe */
1604   long tcp_keepintvl;    /* seconds between TCP keepalive probes */
1605
1606   size_t maxconnects;  /* Max idle connections in the connection cache */
1607
1608   bool ssl_enable_npn;  /* TLS NPN extension? */
1609   bool ssl_enable_alpn; /* TLS ALPN extension? */
1610
1611   long expect_100_timeout; /* in milliseconds */
1612 };
1613
1614 struct Names {
1615   struct curl_hash *hostcache;
1616   enum {
1617     HCACHE_NONE,    /* not pointing to anything */
1618     HCACHE_GLOBAL,  /* points to the (shrug) global one */
1619     HCACHE_MULTI,   /* points to a shared one in the multi handle */
1620     HCACHE_SHARED   /* points to a shared one in a shared object */
1621   } hostcachetype;
1622 };
1623
1624 /*
1625  * The 'connectdata' struct MUST have all the connection oriented stuff as we
1626  * may have several simultaneous connections and connection structs in memory.
1627  *
1628  * The 'struct UserDefined' must only contain data that is set once to go for
1629  * many (perhaps) independent connections. Values that are generated or
1630  * calculated internally for the "session handle" must be defined within the
1631  * 'struct UrlState' instead.
1632  */
1633
1634 struct SessionHandle {
1635   /* first, two fields for the linked list of these */
1636   struct SessionHandle *next;
1637   struct SessionHandle *prev;
1638
1639   struct connectdata *easy_conn;     /* the "unit's" connection */
1640
1641   CURLMstate mstate;  /* the handle's state */
1642   CURLcode result;   /* previous result */
1643
1644   struct Curl_message msg; /* A single posted message. */
1645
1646   /* Array with the plain socket numbers this handle takes care of, in no
1647      particular order. Note that all sockets are added to the sockhash, where
1648      the state etc are also kept. This array is mostly used to detect when a
1649      socket is to be removed from the hash. See singlesocket(). */
1650   curl_socket_t sockets[MAX_SOCKSPEREASYHANDLE];
1651   int numsocks;
1652
1653   struct Names dns;
1654   struct Curl_multi *multi;    /* if non-NULL, points to the multi handle
1655                                   struct to which this "belongs" when used by
1656                                   the multi interface */
1657   struct Curl_multi *multi_easy; /* if non-NULL, points to the multi handle
1658                                     struct to which this "belongs" when used
1659                                     by the easy interface */
1660   struct Curl_share *share;    /* Share, handles global variable mutexing */
1661   struct SingleRequest req;    /* Request-specific data */
1662   struct UserDefined set;      /* values set by the libcurl user */
1663   struct DynamicStatic change; /* possibly modified userdefined data */
1664   struct CookieInfo *cookies;  /* the cookies, read from files and servers.
1665                                   NOTE that the 'cookie' field in the
1666                                   UserDefined struct defines if the "engine"
1667                                   is to be used or not. */
1668   struct Progress progress;    /* for all the progress meter data */
1669   struct UrlState state;       /* struct for fields used for state info and
1670                                   other dynamic purposes */
1671   struct WildcardData wildcard; /* wildcard download state info */
1672   struct PureInfo info;        /* stats, reports and info data */
1673   struct curl_tlssessioninfo tsi; /* Information about the TLS session, only
1674                                      valid after a client has asked for it */
1675 #if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV)
1676   iconv_t outbound_cd;         /* for translating to the network encoding */
1677   iconv_t inbound_cd;          /* for translating from the network encoding */
1678   iconv_t utf8_cd;             /* for translating to UTF8 */
1679 #endif /* CURL_DOES_CONVERSIONS && HAVE_ICONV */
1680   unsigned int magic;          /* set to a CURLEASY_MAGIC_NUMBER */
1681 };
1682
1683 #define LIBCURL_NAME "libcurl"
1684
1685 #endif /* HEADER_CURL_URLDATA_H */