1 #ifndef __URLDATA_H
2 #define __URLDATA_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) 1998 - 2009, 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  * $Id: urldata.h,v 1.409 2009-03-02 23:05:31 bagder Exp $
24  ***************************************************************************/
25 
26 /* This file is for lib internal stuff */
27 
28 #include "setup.h"
29 
30 #define PORT_FTP 21
31 #define PORT_FTPS 990
32 #define PORT_TELNET 23
33 #define PORT_HTTP 80
34 #define PORT_HTTPS 443
35 #define PORT_DICT 2628
36 #define PORT_LDAP 389
37 #define PORT_LDAPS 636
38 #define PORT_TFTP 69
39 #define PORT_SSH 22
40 
41 #define DICT_MATCH "/MATCH:"
42 #define DICT_MATCH2 "/M:"
43 #define DICT_MATCH3 "/FIND:"
44 #define DICT_DEFINE "/DEFINE:"
45 #define DICT_DEFINE2 "/D:"
46 #define DICT_DEFINE3 "/LOOKUP:"
47 
48 #define CURL_DEFAULT_USER "anonymous"
49 #define CURL_DEFAULT_PASSWORD "ftp@example.com"
50 
51 /* length of longest IPv6 address string including the trailing null */
52 #define MAX_IPADR_LEN sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
53 
54 #include "cookie.h"
55 #include "formdata.h"
56 
57 #ifdef USE_SSLEAY
58 #ifdef USE_OPENSSL
59 #include "openssl/rsa.h"
60 #include "openssl/crypto.h"
61 #include "openssl/x509.h"
62 #include "openssl/pem.h"
63 #include "openssl/ssl.h"
64 #include "openssl/err.h"
65 #ifdef HAVE_OPENSSL_ENGINE_H
66 #include <openssl/engine.h>
67 #endif
68 #ifdef HAVE_OPENSSL_PKCS12_H
69 #include <openssl/pkcs12.h>
70 #endif
71 #else /* SSLeay-style includes */
72 #include "rsa.h"
73 #include "crypto.h"
74 #include "x509.h"
75 #include "pem.h"
76 #include "ssl.h"
77 #include "err.h"
78 #ifdef HAVE_OPENSSL_ENGINE_H
79 #include <engine.h>
80 #endif
81 #ifdef HAVE_OPENSSL_PKCS12_H
82 #include <pkcs12.h>
83 #endif
84 #endif /* USE_OPENSSL */
85 #ifdef USE_GNUTLS
86 #error Configuration error; cannot use GnuTLS *and* OpenSSL.
87 #endif
88 #endif /* USE_SSLEAY */
89 
90 #ifdef USE_GNUTLS
91 #include <gnutls/gnutls.h>
92 #endif
93 
94 #ifdef USE_NSS
95 #include <nspr.h>
96 #endif
97 
98 #ifdef USE_QSOSSL
99 #include <qsossl.h>
100 #endif
101 
102 #ifdef HAVE_NETINET_IN_H
103 #include <netinet/in.h>
104 #endif
105 
106 #include "timeval.h"
107 
108 #ifdef HAVE_ZLIB_H
109 #include <zlib.h>               /* for content-encoding */
110 #ifdef __SYMBIAN32__
111 /* zlib pollutes the namespace with this definition */
112 #undef WIN32
113 #endif
114 #endif
115 
116 #ifdef USE_ARES
117 #include <ares.h>
118 #endif
119 
120 #include <curl/curl.h>
121 
122 #include "http_chunks.h" /* for the structs and enum stuff */
123 #include "hostip.h"
124 #include "hash.h"
125 #include "splay.h"
126 
127 #ifdef HAVE_GSSAPI
128 # ifdef HAVE_GSSGNU
129 #  include <gss.h>
130 # elif defined HAVE_GSSMIT
131 #  include <gssapi/gssapi.h>
132 #  include <gssapi/gssapi_generic.h>
133 # else
134 #  include <gssapi.h>
135 # endif
136 #endif
137 
138 #ifdef HAVE_LIBSSH2_H
139 #include <libssh2.h>
140 #include <libssh2_sftp.h>
141 #endif /* HAVE_LIBSSH2_H */
142 
143 /* Download buffer size, keep it fairly big for speed reasons */
144 #undef BUFSIZE
145 #define BUFSIZE CURL_MAX_WRITE_SIZE
146 
147 /* Initial size of the buffer to store headers in, it'll be enlarged in case
148    of need. */
149 #define HEADERSIZE 256
150 
151 #define CURLEASY_MAGIC_NUMBER 0xc0dedbadU
152 
153 /* Some convenience macros to get the larger/smaller value out of two given.
154    We prefix with CURL to prevent name collisions. */
155 #define CURLMAX(x,y) ((x)>(y)?(x):(y))
156 #define CURLMIN(x,y) ((x)<(y)?(x):(y))
157 
158 
159 #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
160 /* Types needed for krb4/5-ftp connections */
161 struct krb4buffer {
162   void *data;
163   size_t size;
164   size_t index;
165   int eof_flag;
166 };
167 enum protection_level {
168   prot_clear,
169   prot_safe,
170   prot_confidential,
171   prot_private,
172   prot_cmd
173 };
174 #endif
175 
176 /* enum for the nonblocking SSL connection state machine */
177 typedef enum {
178   ssl_connect_1,
179   ssl_connect_2,
180   ssl_connect_2_reading,
181   ssl_connect_2_writing,
182   ssl_connect_3,
183   ssl_connect_done
184 } ssl_connect_state;
185 
186 typedef enum {
187   ssl_connection_none,
188   ssl_connection_negotiating,
189   ssl_connection_complete
190 } ssl_connection_state;
191 
192 /* struct for data related to each SSL connection */
193 struct ssl_connect_data {
194   /* Use ssl encrypted communications TRUE/FALSE, not necessarily using it atm
195      but at least asked to or meaning to use it. See 'state' for the exact
196      current state of the connection. */
197   bool use;
198   ssl_connection_state state;
199 #ifdef USE_SSLEAY
200   /* these ones requires specific SSL-types */
201   SSL_CTX* ctx;
202   SSL*     handle;
203   X509*    server_cert;
204   ssl_connect_state connecting_state;
205 #endif /* USE_SSLEAY */
206 #ifdef USE_GNUTLS
207   gnutls_session session;
208   gnutls_certificate_credentials cred;
209 #endif /* USE_GNUTLS */
210 #ifdef USE_NSS
211   PRFileDesc *handle;
212   char *client_nickname;
213 #endif /* USE_NSS */
214 #ifdef USE_QSOSSL
215   SSLHandle *handle;
216 #endif /* USE_QSOSSL */
217 };
218 
219 struct ssl_config_data {
220   long version;          /* what version the client wants to use */
221   long certverifyresult; /* result from the certificate verification */
222   long verifypeer;       /* set TRUE if this is desired */
223   long verifyhost;       /* 0: no verify
224                             1: check that CN exists
225                             2: CN must match hostname */
226   char *CApath;          /* certificate dir (doesn't work on windows) */
227   char *CAfile;          /* certificate to verify peer against */
228   const char *CRLfile;   /* CRL to check certificate revocation */
229   const char *issuercert;/* optional issuer certificate filename */
230   char *random_file;     /* path to file containing "random" data */
231   char *egdsocket;       /* path to file containing the EGD daemon socket */
232   char *cipher_list;     /* list of ciphers to use */
233   long numsessions;      /* SSL session id cache size */
234   curl_ssl_ctx_callback fsslctx; /* function to initialize ssl ctx */
235   void *fsslctxp;        /* parameter for call back */
236   bool sessionid;        /* cache session IDs or not */
237   bool certinfo;         /* gather lots of certificate info */
238 };
239 
240 /* information stored about one single SSL session */
241 struct curl_ssl_session {
242   char *name;       /* host name for which this ID was used */
243   void *sessionid;  /* as returned from the SSL layer */
244   size_t idsize;    /* if known, otherwise 0 */
245   long age;         /* just a number, the higher the more recent */
246   unsigned short remote_port; /* remote port to connect to */
247   struct ssl_config_data ssl_config; /* setup for this session */
248 };
249 
250 /* Struct used for Digest challenge-response authentication */
251 struct digestdata {
252   char *nonce;
253   char *cnonce;
254   char *realm;
255   int algo;
256   bool stale; /* set true for re-negotiation */
257   char *opaque;
258   char *qop;
259   char *algorithm;
260   int nc; /* nounce count */
261 };
262 
263 typedef enum {
264   NTLMSTATE_NONE,
265   NTLMSTATE_TYPE1,
266   NTLMSTATE_TYPE2,
267   NTLMSTATE_TYPE3,
268   NTLMSTATE_LAST
269 } curlntlm;
270 
271 #ifdef USE_WINDOWS_SSPI
272 #include "curl_sspi.h"
273 #endif
274 
275 #if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV)
276 #include <iconv.h>
277 #endif
278 
279 /* Struct used for NTLM challenge-response authentication */
280 struct ntlmdata {
281   curlntlm state;
282 #ifdef USE_WINDOWS_SSPI
283   CredHandle handle;
284   CtxtHandle c_handle;
285   SEC_WINNT_AUTH_IDENTITY identity;
286   SEC_WINNT_AUTH_IDENTITY *p_identity;
287   int has_handles;
288   void *type_2;
289   int n_type_2;
290 #else
291   unsigned int flags;
292   unsigned char nonce[8];
293 #endif
294 };
295 
296 #ifdef HAVE_GSSAPI
297 struct negotiatedata {
298   /* when doing Negotiate we first need to receive an auth token and then we
299      need to send our header */
300   enum { GSS_AUTHNONE, GSS_AUTHRECV, GSS_AUTHSENT } state;
301   bool gss; /* Whether we're processing GSS-Negotiate or Negotiate */
302   const char* protocol; /* "GSS-Negotiate" or "Negotiate" */
303   OM_uint32 status;
304   gss_ctx_id_t context;
305   gss_name_t server_name;
306   gss_buffer_desc output_token;
307 };
308 #endif
309 
310 /****************************************************************************
311  * HTTP unique setup
312  ***************************************************************************/
313 struct HTTP {
314   struct FormData *sendit;
315   curl_off_t postsize; /* off_t to handle large file sizes */
316   const char *postdata;
317 
318   const char *p_pragma;      /* Pragma: string */
319   const char *p_accept;      /* Accept: string */
320   curl_off_t readbytecount;
321   curl_off_t writebytecount;
322 
323   /* For FORM posting */
324   struct Form form;
325 
326   struct back {
327     curl_read_callback fread_func; /* backup storage for fread pointer */
328     void *fread_in;           /* backup storage for fread_in pointer */
329     const char *postdata;
330     curl_off_t postsize;
331   } backup;
332 
333   enum {
334     HTTPSEND_NADA,    /* init */
335     HTTPSEND_REQUEST, /* sending a request */
336     HTTPSEND_BODY,    /* sending body */
337     HTTPSEND_LAST     /* never use this */
338   } sending;
339 
340   void *send_buffer; /* used if the request couldn't be sent in one chunk,
341                         points to an allocated send_buffer struct */
342 };
343 
344 /****************************************************************************
345  * FTP unique setup
346  ***************************************************************************/
347 typedef enum {
348   FTP_STOP,    /* do nothing state, stops the state machine */
349   FTP_WAIT220, /* waiting for the initial 220 response immediately after
350                   a connect */
351   FTP_AUTH,
352   FTP_USER,
353   FTP_PASS,
354   FTP_ACCT,
355   FTP_PBSZ,
356   FTP_PROT,
357   FTP_CCC,
358   FTP_PWD,
359   FTP_QUOTE, /* waiting for a response to a command sent in a quote list */
360   FTP_RETR_PREQUOTE,
361   FTP_STOR_PREQUOTE,
362   FTP_POSTQUOTE,
363   FTP_CWD,  /* change dir */
364   FTP_MKD,  /* if the dir didn't exist */
365   FTP_MDTM, /* to figure out the datestamp */
366   FTP_TYPE, /* to set type when doing a head-like request */
367   FTP_LIST_TYPE, /* set type when about to do a dir list */
368   FTP_RETR_TYPE, /* set type when about to RETR a file */
369   FTP_STOR_TYPE, /* set type when about to STOR a file */
370   FTP_SIZE, /* get the remote file's size for head-like request */
371   FTP_RETR_SIZE, /* get the remote file's size for RETR */
372   FTP_STOR_SIZE, /* get the size for (resumed) STOR */
373   FTP_REST, /* when used to check if the server supports it in head-like */
374   FTP_RETR_REST, /* when asking for "resume" in for RETR */
375   FTP_PORT, /* generic state for PORT, LPRT and EPRT, check count1 */
376   FTP_PASV, /* generic state for PASV and EPSV, check count1 */
377   FTP_LIST, /* generic state for LIST, NLST or a custom list command */
378   FTP_RETR,
379   FTP_STOR, /* generic state for STOR and APPE */
380   FTP_QUIT,
381   FTP_LAST  /* never used */
382 } ftpstate;
383 
384 typedef enum {
385   FTPFILE_MULTICWD  = 1, /* as defined by RFC1738 */
386   FTPFILE_NOCWD     = 2, /* use SIZE / RETR / STOR on the full path */
387   FTPFILE_SINGLECWD = 3  /* make one CWD, then SIZE / RETR / STOR on the file */
388 } curl_ftpfile;
389 
390 typedef enum {
391   FTPTRANSFER_BODY, /* yes do transfer a body */
392   FTPTRANSFER_INFO, /* do still go through to get info/headers */
393   FTPTRANSFER_NONE, /* don't get anything and don't get info */
394   FTPTRANSFER_LAST  /* end of list marker, never used */
395 } curl_ftptransfer;
396 
397 /* This FTP struct is used in the SessionHandle. All FTP data that is
398    connection-oriented must be in FTP_conn to properly deal with the fact that
399    perhaps the SessionHandle is changed between the times the connection is
400    used. */
401 struct FTP {
402   curl_off_t *bytecountp;
403   char *user;    /* user name string */
404   char *passwd;  /* password string */
405 
406   /* transfer a file/body or not, done as a typedefed enum just to make
407      debuggers display the full symbol and not just the numerical value */
408   curl_ftptransfer transfer;
409   curl_off_t downloadsize;
410 };
411 
412 /* ftp_conn is used for struct connection-oriented data in the connectdata
413    struct */
414 struct ftp_conn {
415   char *entrypath; /* the PWD reply when we logged on */
416   char **dirs;   /* realloc()ed array for path components */
417   int dirdepth;  /* number of entries used in the 'dirs' array */
418   int diralloc;  /* number of entries allocated for the 'dirs' array */
419   char *file;    /* decoded file */
420   char *cache;       /* data cache between getresponse()-calls */
421   curl_off_t cache_size; /* size of cache in bytes */
422   bool dont_check;  /* Set to TRUE to prevent the final (post-transfer)
423                        file size and 226/250 status check. It should still
424                        read the line, just ignore the result. */
425   long response_time; /* When no timeout is given, this is the amount of
426                          seconds we await for an FTP response. Initialized
427                          in Curl_ftp_connect() */
428   bool ctl_valid;   /* Tells Curl_ftp_quit() whether or not to do anything. If
429                        the connection has timed out or been closed, this
430                        should be FALSE when it gets to Curl_ftp_quit() */
431   bool cwddone;     /* if it has been determined that the proper CWD combo
432                        already has been done */
433   bool cwdfail;     /* set TRUE if a CWD command fails, as then we must prevent
434                        caching the current directory */
435   char *prevpath;   /* conn->path from the previous transfer */
436   char transfertype; /* set by ftp_transfertype for use by Curl_client_write()a
437                         and others (A/I or zero) */
438   size_t nread_resp; /* number of bytes currently read of a server response */
439   char *linestart_resp; /* line start pointer for the FTP server response
440                            reader function */
441   bool pending_resp;  /* set TRUE when a server response is pending or in
442                          progress, and is cleared once the last response is
443                          read */
444 
445   int count1; /* general purpose counter for the state machine */
446   int count2; /* general purpose counter for the state machine */
447   int count3; /* general purpose counter for the state machine */
448   char *sendthis; /* allocated pointer to a buffer that is to be sent to the
449                      ftp server */
450   size_t sendleft; /* number of bytes left to send from the sendthis buffer */
451   size_t sendsize; /* total size of the sendthis buffer */
452   struct timeval response; /* set to Curl_tvnow() when a command has been sent
453                               off, used to time-out response reading */
454   ftpstate state; /* always use ftp.c:state() to change state! */
455 };
456 
457 /****************************************************************************
458  * SSH unique setup
459  ***************************************************************************/
460 typedef enum {
461   SSH_NO_STATE = -1,  /* Used for "nextState" so say there is none */
462   SSH_STOP = 0,       /* do nothing state, stops the state machine */
463 
464   SSH_S_STARTUP,      /* Session startup, First state in SSH-CONNECT */
465   SSH_AUTHLIST,
466   SSH_AUTH_PKEY_INIT,
467   SSH_AUTH_PKEY,
468   SSH_AUTH_PASS_INIT,
469   SSH_AUTH_PASS,
470   SSH_AUTH_HOST_INIT,
471   SSH_AUTH_HOST,
472   SSH_AUTH_KEY_INIT,
473   SSH_AUTH_KEY,
474   SSH_AUTH_DONE,
475   SSH_SFTP_INIT,
476   SSH_SFTP_REALPATH,   /* Last state in SSH-CONNECT */
477 
478   SSH_SFTP_QUOTE_INIT, /* First state in SFTP-DO */
479   SSH_SFTP_POSTQUOTE_INIT, /* (Possibly) First state in SFTP-DONE */
480   SSH_SFTP_QUOTE,
481   SSH_SFTP_NEXT_QUOTE,
482   SSH_SFTP_QUOTE_STAT,
483   SSH_SFTP_QUOTE_SETSTAT,
484   SSH_SFTP_QUOTE_SYMLINK,
485   SSH_SFTP_QUOTE_MKDIR,
486   SSH_SFTP_QUOTE_RENAME,
487   SSH_SFTP_QUOTE_RMDIR,
488   SSH_SFTP_QUOTE_UNLINK,
489   SSH_SFTP_TRANS_INIT,
490   SSH_SFTP_UPLOAD_INIT,
491   SSH_SFTP_CREATE_DIRS_INIT,
492   SSH_SFTP_CREATE_DIRS,
493   SSH_SFTP_CREATE_DIRS_MKDIR,
494   SSH_SFTP_READDIR_INIT,
495   SSH_SFTP_READDIR,
496   SSH_SFTP_READDIR_LINK,
497   SSH_SFTP_READDIR_BOTTOM,
498   SSH_SFTP_READDIR_DONE,
499   SSH_SFTP_DOWNLOAD_INIT,
500   SSH_SFTP_DOWNLOAD_STAT, /* Last state in SFTP-DO */
501   SSH_SFTP_CLOSE,    /* Last state in SFTP-DONE */
502   SSH_SFTP_SHUTDOWN, /* First state in SFTP-DISCONNECT */
503   SSH_SCP_TRANS_INIT, /* First state in SCP-DO */
504   SSH_SCP_UPLOAD_INIT,
505   SSH_SCP_DOWNLOAD_INIT,
506   SSH_SCP_DONE,
507   SSH_SCP_SEND_EOF,
508   SSH_SCP_WAIT_EOF,
509   SSH_SCP_WAIT_CLOSE,
510   SSH_SCP_CHANNEL_FREE,   /* Last state in SCP-DONE */
511   SSH_SESSION_DISCONNECT, /* First state in SCP-DISCONNECT */
512   SSH_SESSION_FREE,       /* Last state in SCP/SFTP-DISCONNECT */
513   SSH_QUIT,
514   SSH_LAST  /* never used */
515 } sshstate;
516 
517 /* this struct is used in the HandleData struct which is part of the
518    SessionHandle, which means this is used on a per-easy handle basis.
519    Everything that is strictly related to a connection is banned from this
520    struct. */
521 struct SSHPROTO {
522   char *path;                   /* the path we operate on */
523 };
524 
525 /* ssh_conn is used for struct connection-oriented data in the connectdata
526    struct */
527 struct ssh_conn {
528   const char *authlist;       /* List of auth. methods, managed by libssh2 */
529 #ifdef USE_LIBSSH2
530   const char *passphrase;     /* pass-phrase to use */
531   char *rsa_pub;              /* path name */
532   char *rsa;                  /* path name */
533   bool authed;                /* the connection has been authenticated fine */
534   sshstate state;             /* always use ssh.c:state() to change state! */
535   sshstate nextstate;         /* the state to goto after stopping */
536   CURLcode actualcode;        /* the actual error code */
537   struct curl_slist *quote_item; /* for the quote option */
538   char *quote_path1;          /* two generic pointers for the QUOTE stuff */
539   char *quote_path2;
540   LIBSSH2_SFTP_ATTRIBUTES quote_attrs; /* used by the SFTP_QUOTE state */
541   char *homedir;              /* when doing SFTP we figure out home dir in the
542                                  connect phase */
543 
544   /* Here's a set of struct members used by the SFTP_READDIR state */
545   LIBSSH2_SFTP_ATTRIBUTES readdir_attrs;
546   char *readdir_filename;
547   char *readdir_longentry;
548   int readdir_len, readdir_totalLen, readdir_currLen;
549   char *readdir_line;
550   char *readdir_linkPath;
551   /* end of READDIR stuff */
552 
553   int secondCreateDirs;         /* counter use by the code to see if the
554                                    second attempt has been made to change
555                                    to/create a directory */
556   char *slash_pos;              /* used by the SFTP_CREATE_DIRS state */
557   LIBSSH2_SESSION *ssh_session; /* Secure Shell session */
558   LIBSSH2_CHANNEL *ssh_channel; /* Secure Shell channel handle */
559   LIBSSH2_SFTP *sftp_session;   /* SFTP handle */
560   LIBSSH2_SFTP_HANDLE *sftp_handle;
561   int waitfor;                  /* current READ/WRITE bits to wait for */
562   int orig_waitfor;             /* default READ/WRITE bits wait for */
563 #endif /* USE_LIBSSH2 */
564 };
565 
566 
567 /****************************************************************************
568  * FILE unique setup
569  ***************************************************************************/
570 struct FILEPROTO {
571   char *path; /* the path we operate on */
572   char *freepath; /* pointer to the allocated block we must free, this might
573                      differ from the 'path' pointer */
574   int fd;     /* open file descriptor to read from! */
575 };
576 
577 /*
578  * Boolean values that concerns this connection.
579  */
580 struct ConnectBits {
581   bool close; /* if set, we close the connection after this request */
582   bool reuse; /* if set, this is a re-used connection */
583   bool proxy; /* if set, this transfer is done through a proxy - any type */
584   bool httpproxy;    /* if set, this transfer is done through a http proxy */
585   bool user_passwd;    /* do we use user+password for this connection? */
586   bool proxy_user_passwd; /* user+password for the proxy? */
587   bool ipv6_ip; /* we communicate with a remote site specified with pure IPv6
588                    IP address */
589   bool ipv6;    /* we communicate with a site using an IPv6 address */
590 
591   bool do_more; /* this is set TRUE if the ->curl_do_more() function is
592                    supposed to be called, after ->curl_do() */
593 
594   bool tcpconnect;    /* the TCP layer (or similar) is connected, this is set
595                          the first time on the first connect function call */
596   bool protoconnstart;/* the protocol layer has STARTED its operation after
597                          the TCP layer connect */
598 
599   bool retry;         /* this connection is about to get closed and then
600                          re-attempted at another connection. */
601   bool tunnel_proxy;  /* if CONNECT is used to "tunnel" through the proxy.
602                          This is implicit when SSL-protocols are used through
603                          proxies, but can also be enabled explicitly by
604                          apps */
605   bool tunnel_connecting; /* TRUE while we're still waiting for a proxy CONNECT
606                            */
607   bool authneg;       /* TRUE when the auth phase has started, which means
608                          that we are creating a request with an auth header,
609                          but it is not the final request in the auth
610                          negotiation. */
611   bool rewindaftersend;/* TRUE when the sending couldn't be stopped even
612                           though it will be discarded. When the whole send
613                           operation is done, we must call the data rewind
614                           callback. */
615   bool ftp_use_epsv;  /* As set with CURLOPT_FTP_USE_EPSV, but if we find out
616                          EPSV doesn't work we disable it for the forthcoming
617                          requests */
618 
619   bool ftp_use_eprt;  /* As set with CURLOPT_FTP_USE_EPRT, but if we find out
620                          EPRT doesn't work we disable it for the forthcoming
621                          requests */
622   bool netrc;         /* name+password provided by netrc */
623 
624   bool done;          /* set to FALSE when Curl_do() is called and set to TRUE
625                          when Curl_done() is called, to prevent Curl_done() to
626                          get invoked twice when the multi interface is
627                          used. */
628   bool stream_was_rewound; /* Indicates that the stream was rewound after a
629                               request read past the end of its response byte
630                               boundary */
631   bool proxy_connect_closed; /* set true if a proxy disconnected the
632                                 connection in a CONNECT request with auth, so
633                                 that libcurl should reconnect and continue. */
634   bool bound; /* set true if bind() has already been done on this socket/
635                  connection */
636 };
637 
638 struct hostname {
639   char *rawalloc; /* allocated "raw" version of the name */
640   char *encalloc; /* allocated IDN-encoded version of the name */
641   char *name;     /* name to use internally, might be encoded, might be raw */
642   const char *dispname; /* name to display, as 'name' might be encoded */
643 };
644 
645 /*
646  * Flags on the keepon member of the Curl_transfer_keeper
647  */
648 
649 #define KEEP_NONE  0
650 #define KEEP_READ  (1<<0)     /* there is or may be data to read */
651 #define KEEP_WRITE (1<<1)     /* there is or may be data to write */
652 #define KEEP_READ_HOLD (1<<2) /* when set, no reading should be done but there
653                                  might still be data to read */
654 #define KEEP_WRITE_HOLD (1<<3) /* when set, no writing should be done but there
655                                   might still be data to write */
656 #define KEEP_READ_PAUSE (1<<4) /* reading is paused */
657 #define KEEP_WRITE_PAUSE (1<<5) /* writing is paused */
658 
659 #define KEEP_READBITS (KEEP_READ | KEEP_READ_HOLD | KEEP_READ_PAUSE)
660 #define KEEP_WRITEBITS (KEEP_WRITE | KEEP_WRITE_HOLD | KEEP_WRITE_PAUSE)
661 
662 
663 #ifdef HAVE_LIBZ
664 typedef enum {
665   ZLIB_UNINIT,          /* uninitialized */
666   ZLIB_INIT,            /* initialized */
667   ZLIB_GZIP_HEADER,     /* reading gzip header */
668   ZLIB_GZIP_INFLATING,  /* inflating gzip stream */
669   ZLIB_INIT_GZIP        /* initialized in transparent gzip mode */
670 } zlibInitState;
671 #endif
672 
673 #if defined(USE_ARES) || defined(USE_THREADING_GETHOSTBYNAME) || \
674     defined(USE_THREADING_GETADDRINFO)
675 struct Curl_async {
676   char *hostname;
677   int port;
678   struct Curl_dns_entry *dns;
679   bool done;  /* set TRUE when the lookup is complete */
680   int status; /* if done is TRUE, this is the status from the callback */
681   void *os_specific;  /* 'struct thread_data' for Windows */
682 };
683 #endif
684 
685 #define FIRSTSOCKET     0
686 #define SECONDARYSOCKET 1
687 
688 /* These function pointer types are here only to allow easier typecasting
689    within the source when we need to cast between data pointers (such as NULL)
690    and function pointers. */
691 typedef CURLcode (*Curl_do_more_func)(struct connectdata *);
692 typedef CURLcode (*Curl_done_func)(struct connectdata *, CURLcode, bool);
693 
694 
695 enum expect100 {
696   EXP100_SEND_DATA,           /* enough waiting, just send the body now */
697   EXP100_AWAITING_CONTINUE,   /* waiting for the 100 Continue header */
698   EXP100_SENDING_REQUEST,     /* still sending the request but will wait for
699                                  the 100 header once done with the request */
700   EXP100_FAILED               /* used on 417 Expectation Failed */
701 };
702 
703 /*
704  * Request specific data in the easy handle (SessionHandle).  Previously,
705  * these members were on the connectdata struct but since a conn struct may
706  * now be shared between different SessionHandles, we store connection-specific
707  * data here. This struct only keeps stuff that's interesting for *this*
708  * request, as it will be cleared between multiple ones
709  */
710 struct SingleRequest {
711   curl_off_t size;        /* -1 if unknown at this point */
712   curl_off_t *bytecountp; /* return number of bytes read or NULL */
713 
714   curl_off_t maxdownload; /* in bytes, the maximum amount of data to fetch,
715                              -1 means unlimited */
716   curl_off_t *writebytecountp; /* return number of bytes written or NULL */
717 
718   curl_off_t bytecount;         /* total number of bytes read */
719   curl_off_t writebytecount;    /* number of bytes written */
720 
721   long headerbytecount;         /* only count received headers */
722   long deductheadercount; /* this amount of bytes doesn't count when we check
723                              if anything has been transfered at the end of a
724                              connection. We use this counter to make only a
725                              100 reply (without a following second response
726                              code) result in a CURLE_GOT_NOTHING error code */
727 
728   struct timeval start;         /* transfer started at this time */
729   struct timeval now;           /* current time */
730   bool header;                  /* incoming data has HTTP header */
731   enum {
732     HEADER_NORMAL,              /* no bad header at all */
733     HEADER_PARTHEADER,          /* part of the chunk is a bad header, the rest
734                                    is normal data */
735     HEADER_ALLBAD               /* all was believed to be header */
736   } badheader;                  /* the header was deemed bad and will be
737                                    written as body */
738   int headerline;               /* counts header lines to better track the
739                                    first one */
740   char *hbufp;                  /* points at *end* of header line */
741   size_t hbuflen;
742   char *str;                    /* within buf */
743   char *str_start;              /* within buf */
744   char *end_ptr;                /* within buf */
745   char *p;                      /* within headerbuff */
746   bool content_range;           /* set TRUE if Content-Range: was found */
747   curl_off_t offset;            /* possible resume offset read from the
748                                    Content-Range: header */
749   int httpcode;                 /* error code from the 'HTTP/1.? XXX' line */
750   struct timeval start100;      /* time stamp to wait for the 100 code from */
751   enum expect100 exp100;        /* expect 100 continue state */
752 
753   int content_encoding;         /* What content encoding. sec 3.5, RFC2616. */
754 
755 #define IDENTITY 0              /* No encoding */
756 #define DEFLATE 1               /* zlib deflate [RFC 1950 & 1951] */
757 #define GZIP 2                  /* gzip algorithm [RFC 1952] */
758 #define COMPRESS 3              /* Not handled, added for completeness */
759 
760 #ifdef HAVE_LIBZ
761   zlibInitState zlib_init;      /* possible zlib init state;
762                                    undefined if Content-Encoding header. */
763   z_stream z;                   /* State structure for zlib. */
764 #endif
765 
766   time_t timeofdoc;
767   long bodywrites;
768 
769   char *buf;
770   char *uploadbuf;
771   curl_socket_t maxfd;
772 
773   int keepon;
774 
775   bool upload_done; /* set to TRUE when doing chunked transfer-encoding upload
776                        and we're uploading the last chunk */
777 
778   bool ignorebody;  /* we read a response-body but we ignore it! */
779   bool ignorecl;    /* This HTTP response has no body so we ignore the Content-
780                        Length: header */
781 
782   char *location;   /* This points to an allocated version of the Location:
783                        header data */
784   char *newurl;     /* Set to the new URL to use when a redirect or a retry is
785                        wanted */
786 
787   /* 'upload_present' is used to keep a byte counter of how much data there is
788      still left in the buffer, aimed for upload. */
789   ssize_t upload_present;
790 
791    /* 'upload_fromhere' is used as a read-pointer when we uploaded parts of a
792       buffer, so the next read should read from where this pointer points to,
793       and the 'upload_present' contains the number of bytes available at this
794       position */
795   char *upload_fromhere;
796 
797   bool chunk; /* if set, this is a chunked transfer-encoding */
798   bool upload_chunky; /* set TRUE if we are doing chunked transfer-encoding
799                          on upload */
800   bool getheader;     /* TRUE if header parsing is wanted */
801 
802   bool forbidchunk;   /* used only to explicitly forbid chunk-upload for
803                          specific upload buffers. See readmoredata() in
804                          http.c for details. */
805   bool trailerhdrpresent; /* Set when Trailer: header found in HTTP response.
806                              Required to determine whether to look for trailers
807                              in case of Transfer-Encoding: chunking */
808 };
809 
810 /*
811  * Specific protocol handler.
812  */
813 
814 struct Curl_handler {
815   const char * scheme;        /* URL scheme name. */
816 
817   /* Complement to setup_connection_internals(). */
818   CURLcode (*setup_connection)(struct connectdata *);
819 
820   /* These two functions MUST be set to be protocol dependent */
821   CURLcode (*do_it)(struct connectdata *, bool *done);
822   Curl_done_func done;
823 
824   /* If the curl_do() function is better made in two halves, this
825    * curl_do_more() function will be called afterwards, if set. For example
826    * for doing the FTP stuff after the PASV/PORT command.
827    */
828   Curl_do_more_func do_more;
829 
830   /* This function *MAY* be set to a protocol-dependent function that is run
831    * after the connect() and everything is done, as a step in the connection.
832    * The 'done' pointer points to a bool that should be set to TRUE if the
833    * function completes before return. If it doesn't complete, the caller
834    * should call the curl_connecting() function until it is.
835    */
836   CURLcode (*connect_it)(struct connectdata *, bool *done);
837 
838   /* See above. Currently only used for FTP. */
839   CURLcode (*connecting)(struct connectdata *, bool *done);
840   CURLcode (*doing)(struct connectdata *, bool *done);
841 
842   /* Called from the multi interface during the PROTOCONNECT phase, and it
843      should then return a proper fd set */
844   int (*proto_getsock)(struct connectdata *conn,
845                        curl_socket_t *socks,
846                        int numsocks);
847 
848   /* Called from the multi interface during the DOING phase, and it should
849      then return a proper fd set */
850   int (*doing_getsock)(struct connectdata *conn,
851                        curl_socket_t *socks,
852                        int numsocks);
853 
854   /* Called from the multi interface during the DO_DONE, PERFORM and
855      WAITPERFORM phases, and it should then return a proper fd set. Not setting
856      this will make libcurl use the generic default one. */
857   int (*perform_getsock)(const struct connectdata *conn,
858                          curl_socket_t *socks,
859                          int numsocks);
860 
861   /* This function *MAY* be set to a protocol-dependent function that is run
862    * by the curl_disconnect(), as a step in the disconnection.
863    */
864   CURLcode (*disconnect)(struct connectdata *);
865 
866   long defport;       /* Default port. */
867   long protocol;      /* PROT_* flags concerning the protocol set */
868 };
869 
870 /*
871  * The connectdata struct contains all fields and variables that should be
872  * unique for an entire connection.
873  */
874 struct connectdata {
875   /* 'data' is the CURRENT SessionHandle using this connection -- take great
876      caution that this might very well vary between different times this
877      connection is used! */
878   struct SessionHandle *data;
879 
880   /* chunk is for HTTP chunked encoding, but is in the general connectdata
881      struct only because we can do just about any protocol through a HTTP proxy
882      and a HTTP proxy may in fact respond using chunked encoding */
883   struct Curl_chunker chunk;
884 
885   bool inuse; /* This is a marker for the connection cache logic. If this is
886                  TRUE this handle is being used by an easy handle and cannot
887                  be used by any other easy handle without careful
888                  consideration (== only for pipelining). */
889 
890   /**** Fields set when inited and not modified again */
891   long connectindex; /* what index in the connection cache connects index this
892                         particular struct has */
893   long protocol; /* PROT_* flags concerning the protocol set */
894 #define PROT_HTTP    CURLPROTO_HTTP
895 #define PROT_HTTPS   CURLPROTO_HTTPS
896 #define PROT_FTP     CURLPROTO_FTP
897 #define PROT_TELNET  CURLPROTO_TELNET
898 #define PROT_DICT    CURLPROTO_DICT
899 #define PROT_LDAP    CURLPROTO_LDAP
900 #define PROT_FILE    CURLPROTO_FILE
901 #define PROT_FTPS    CURLPROTO_FTPS
902 #define PROT_TFTP    CURLPROTO_TFTP
903 #define PROT_SCP     CURLPROTO_SCP
904 #define PROT_SFTP    CURLPROTO_SFTP
905 
906 /* CURLPROTO_TFTP (1<<11) is currently the highest used bit in the public
907    bitmask. We make sure we use "private bits" above the first 16 to make
908    things easier. */
909 
910 #define PROT_EXTMASK 0xffff
911 
912 #define PROT_SSL     (1<<22) /* protocol requires SSL */
913 #define PROT_MISSING (1<<23)
914 
915 #define PROT_CLOSEACTION PROT_FTP /* these ones need action before socket
916                                      close */
917 #define PROT_DUALCHANNEL PROT_FTP /* these protocols use two connections */
918 
919   /* 'dns_entry' is the particular host we use. This points to an entry in the
920      DNS cache and it will not get pruned while locked. It gets unlocked in
921      Curl_done(). This entry will be NULL if the connection is re-used as then
922      there is no name resolve done. */
923   struct Curl_dns_entry *dns_entry;
924 
925   /* 'ip_addr' is the particular IP we connected to. It points to a struct
926      within the DNS cache, so this pointer is only valid as long as the DNS
927      cache entry remains locked. It gets unlocked in Curl_done() */
928   Curl_addrinfo *ip_addr;
929 
930   /* 'ip_addr_str' is the ip_addr data as a human readable string.
931      It remains available as long as the connection does, which is longer than
932      the ip_addr itself. */
933   char ip_addr_str[MAX_IPADR_LEN];
934 
935   unsigned int scope;    /* address scope for IPv6 */
936 
937   char protostr[16];  /* store the protocol string in this buffer */
938   int socktype;  /* SOCK_STREAM or SOCK_DGRAM */
939 
940   struct hostname host;
941   struct hostname proxy;
942 
943   long port;       /* which port to use locally */
944   unsigned short remote_port; /* what remote port to connect to,
945                                  not the proxy port! */
946 
947   char *user;    /* user name string, allocated */
948   char *passwd;  /* password string, allocated */
949 
950   char *proxyuser;    /* proxy user name string, allocated */
951   char *proxypasswd;  /* proxy password string, allocated */
952   curl_proxytype proxytype; /* what kind of proxy that is in use */
953 
954   int httpversion;              /* the HTTP version*10 reported by the server */
955 
956   struct timeval now;     /* "current" time */
957   struct timeval created; /* creation time */
958   curl_socket_t sock[2]; /* two sockets, the second is used for the data
959                             transfer when doing FTP */
960 
961   struct ssl_connect_data ssl[2]; /* this is for ssl-stuff */
962   struct ssl_config_data ssl_config;
963 
964   struct ConnectBits bits;    /* various state-flags for this connection */
965 
966   const struct Curl_handler * handler;  /* Connection's protocol handler. */
967 
968   /**** curl_get() phase fields */
969 
970   curl_socket_t sockfd;   /* socket to read from or CURL_SOCKET_BAD */
971   curl_socket_t writesockfd; /* socket to write to, it may very
972                                 well be the same we read from.
973                                 CURL_SOCKET_BAD disables */
974 
975   /** Dynamicly allocated strings, may need to be freed before this **/
976   /** struct is killed.                                             **/
977   struct dynamically_allocated_data {
978     char *proxyuserpwd; /* free later if not NULL! */
979     char *uagent; /* free later if not NULL! */
980     char *accept_encoding; /* free later if not NULL! */
981     char *userpwd; /* free later if not NULL! */
982     char *rangeline; /* free later if not NULL! */
983     char *ref; /* free later if not NULL! */
984     char *host; /* free later if not NULL */
985     char *cookiehost; /* free later if not NULL */
986   } allocptr;
987 
988   int sec_complete; /* if kerberos is enabled for this connection */
989 #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
990   enum protection_level command_prot;
991   enum protection_level data_prot;
992   enum protection_level request_data_prot;
993   size_t buffer_size;
994   struct krb4buffer in_buffer, out_buffer;
995   void *app_data;
996   const struct Curl_sec_client_mech *mech;
997   struct sockaddr_in local_addr;
998 #endif
999 
1000   /* the two following *_inuse fields are only flags, not counters in any way.
1001      If TRUE it means the channel is in use, and if FALSE it means the channel
1002      is up for grabs by one. */
1003 
1004   bool readchannel_inuse;  /* whether the read channel is in use by an easy
1005                               handle */
1006   bool writechannel_inuse; /* whether the write channel is in use by an easy
1007                               handle */
1008   bool server_supports_pipelining; /* TRUE if server supports pipelining,
1009                                       set after first response */
1010 
1011   struct curl_llist *send_pipe; /* List of handles waiting to
1012                                    send on this pipeline */
1013   struct curl_llist *recv_pipe; /* List of handles waiting to read
1014                                    their responses on this pipeline */
1015   struct curl_llist *pend_pipe; /* List of pending handles on
1016                                    this pipeline */
1017 #define MAX_PIPELINE_LENGTH 5
1018 
1019   char* master_buffer; /* The master buffer allocated on-demand;
1020                           used for pipelining. */
1021   size_t read_pos; /* Current read position in the master buffer */
1022   size_t buf_len; /* Length of the buffer?? */
1023 
1024 
1025   curl_seek_callback seek_func; /* function that seeks the input */
1026   void *seek_client;            /* pointer to pass to the seek() above */
1027 
1028   /*************** Request - specific items ************/
1029 
1030   /* previously this was in the urldata struct */
1031   curl_read_callback fread_func; /* function that reads the input */
1032   void *fread_in;           /* pointer to pass to the fread() above */
1033 
1034   struct ntlmdata ntlm;     /* NTLM differs from other authentication schemes
1035                                because it authenticates connections, not
1036                                single requests! */
1037   struct ntlmdata proxyntlm; /* NTLM data for proxy */
1038 
1039   char syserr_buf [256]; /* buffer for Curl_strerror() */
1040 
1041 #if defined(USE_ARES) || defined(USE_THREADING_GETHOSTBYNAME) || \
1042     defined(USE_THREADING_GETADDRINFO)
1043   /* data used for the asynch name resolve callback */
1044   struct Curl_async async;
1045 #endif
1046 
1047   /* These three are used for chunked-encoding trailer support */
1048   char *trailer; /* allocated buffer to store trailer in */
1049   int trlMax;    /* allocated buffer size */
1050   int trlPos;    /* index of where to store data */
1051 
1052   union {
1053     struct ftp_conn ftpc;
1054     struct ssh_conn sshc;
1055     struct tftp_state_data *tftpc;
1056   } proto;
1057 
1058   int cselect_bits; /* bitmask of socket events */
1059 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1060   int socks5_gssapi_enctype;
1061 #endif
1062 };
1063 
1064 /* The end of connectdata. */
1065 
1066 /*
1067  * Struct to keep statistical and informational data.
1068  */
1069 struct PureInfo {
1070   int httpcode;  /* Recent HTTP or FTP response code */
1071   int httpproxycode; /* response code from proxy when received separate */
1072   int httpversion; /* the http version number X.Y = X*10+Y */
1073   long filetime; /* If requested, this is might get set. Set to -1 if the time
1074                     was unretrievable. We cannot have this of type time_t,
1075                     since time_t is unsigned on several platforms such as
1076                     OpenVMS. */
1077   bool timecond;  /* set to TRUE if the time condition didn't match, which
1078                      thus made the document NOT get fetched */
1079   long header_size;  /* size of read header(s) in bytes */
1080   long request_size; /* the amount of bytes sent in the request(s) */
1081   long proxyauthavail; /* what proxy auth types were announced */
1082   long httpauthavail;  /* what host auth types were announced */
1083   long numconnects; /* how many new connection did libcurl created */
1084   char *contenttype; /* the content type of the object */
1085   char *wouldredirect; /* URL this would've been redirected to if asked to */
1086   char ip[MAX_IPADR_LEN]; /* this buffer gets the numerical ip version stored
1087                              at the connect *attempt* so it will get the last
1088                              tried connect IP even on failures */
1089   struct curl_certinfo certs; /* info about the certs, only populated in
1090                                  OpenSSL builds. Asked for with
1091                                  CURLOPT_CERTINFO / CURLINFO_CERTINFO */
1092 };
1093 
1094 
1095 struct Progress {
1096   long lastshow; /* time() of the last displayed progress meter or NULL to
1097                     force redraw at next call */
1098   curl_off_t size_dl; /* total expected size */
1099   curl_off_t size_ul; /* total expected size */
1100   curl_off_t downloaded; /* transfered so far */
1101   curl_off_t uploaded; /* transfered so far */
1102 
1103   curl_off_t current_speed; /* uses the currently fastest transfer */
1104 
1105   bool callback;  /* set when progress callback is used */
1106   int width; /* screen width at download start */
1107   int flags; /* see progress.h */
1108 
1109   double timespent;
1110 
1111   curl_off_t dlspeed;
1112   curl_off_t ulspeed;
1113 
1114   double t_nslookup;
1115   double t_connect;
1116   double t_appconnect;
1117   double t_pretransfer;
1118   double t_starttransfer;
1119   double t_redirect;
1120 
1121   struct timeval start;
1122   struct timeval t_startsingle;
1123 #define CURR_TIME (5+1) /* 6 entries for 5 seconds */
1124 
1125   curl_off_t speeder[ CURR_TIME ];
1126   struct timeval speeder_time[ CURR_TIME ];
1127   int speeder_c;
1128 };
1129 
1130 typedef enum {
1131   HTTPREQ_NONE, /* first in list */
1132   HTTPREQ_GET,
1133   HTTPREQ_POST,
1134   HTTPREQ_POST_FORM, /* we make a difference internally */
1135   HTTPREQ_PUT,
1136   HTTPREQ_HEAD,
1137   HTTPREQ_CUSTOM,
1138   HTTPREQ_LAST /* last in list */
1139 } Curl_HttpReq;
1140 
1141 /*
1142  * Values that are generated, temporary or calculated internally for a
1143  * "session handle" must be defined within the 'struct UrlState'.  This struct
1144  * will be used within the SessionHandle struct. When the 'SessionHandle'
1145  * struct is cloned, this data MUST NOT be copied.
1146  *
1147  * Remember that any "state" information goes globally for the curl handle.
1148  * Session-data MUST be put in the connectdata struct and here.  */
1149 #define MAX_CURL_USER_LENGTH 256
1150 #define MAX_CURL_PASSWORD_LENGTH 256
1151 #define MAX_CURL_USER_LENGTH_TXT "255"
1152 #define MAX_CURL_PASSWORD_LENGTH_TXT "255"
1153 
1154 struct auth {
1155   long want;  /* Bitmask set to the authentication methods wanted by the app
1156                  (with CURLOPT_HTTPAUTH or CURLOPT_PROXYAUTH). */
1157   long picked;
1158   long avail; /* bitmask for what the server reports to support for this
1159                  resource */
1160   bool done;  /* TRUE when the auth phase is done and ready to do the *actual*
1161                  request */
1162   bool multi; /* TRUE if this is not yet authenticated but within the auth
1163                  multipass negotiation */
1164   bool iestyle; /* TRUE if digest should be done IE-style or FALSE if it should
1165                    be RFC compliant */
1166 };
1167 
1168 struct conncache {
1169   /* 'connects' will be an allocated array with pointers. If the pointer is
1170      set, it holds an allocated connection. */
1171   struct connectdata **connects;
1172   long num;           /* number of entries of the 'connects' array */
1173   enum {
1174     CONNCACHE_PRIVATE, /* used for an easy handle alone */
1175     CONNCACHE_MULTI    /* shared within a multi handle */
1176   } type;
1177 };
1178 
1179 
1180 struct UrlState {
1181   enum {
1182     Curl_if_none,
1183     Curl_if_easy,
1184     Curl_if_multi
1185   } used_interface;
1186 
1187   struct conncache *connc; /* points to the connection cache this handle
1188                               uses */
1189 
1190   /* buffers to store authentication data in, as parsed from input options */
1191   struct timeval keeps_speed; /* for the progress meter really */
1192 
1193   long lastconnect;  /* index of most recent connect or -1 if undefined */
1194 
1195   char *headerbuff; /* allocated buffer to store headers in */
1196   size_t headersize;   /* size of the allocation */
1197 
1198   char buffer[BUFSIZE+1]; /* download buffer */
1199   char uploadbuffer[BUFSIZE+1]; /* upload buffer */
1200   curl_off_t current_speed;  /* the ProgressShow() funcion sets this,
1201                                 bytes / second */
1202   bool this_is_a_follow; /* this is a followed Location: request */
1203 
1204   char *first_host; /* if set, this should be the host name that we will
1205                        sent authorization to, no else. Used to make Location:
1206                        following not keep sending user+password... This is
1207                        strdup() data.
1208                     */
1209   struct curl_ssl_session *session; /* array of 'numsessions' size */
1210   long sessionage;                  /* number of the most recent session */
1211   char *tempwrite;      /* allocated buffer to keep data in when a write
1212                            callback returns to make the connection paused */
1213   size_t tempwritesize; /* size of the 'tempwrite' allocated buffer */
1214   int tempwritetype;    /* type of the 'tempwrite' buffer as a bitmask that is
1215                            used with Curl_client_write() */
1216   char *scratch; /* huge buffer[BUFSIZE*2] when doing upload CRLF replacing */
1217   bool errorbuf; /* Set to TRUE if the error buffer is already filled in.
1218                     This must be set to FALSE every time _easy_perform() is
1219                     called. */
1220   int os_errno;  /* filled in with errno whenever an error occurs */
1221 #ifdef HAVE_SIGNAL
1222   /* storage for the previous bag^H^H^HSIGPIPE signal handler :-) */
1223   void (*prev_signal)(int sig);
1224 #endif
1225   bool allow_port; /* Is set.use_port allowed to take effect or not. This
1226                       is always set TRUE when curl_easy_perform() is called. */
1227   struct digestdata digest;      /* state data for host Digest auth */
1228   struct digestdata proxydigest; /* state data for proxy Digest auth */
1229 
1230 #ifdef HAVE_GSSAPI
1231   struct negotiatedata negotiate; /* state data for host Negotiate auth */
1232   struct negotiatedata proxyneg; /* state data for proxy Negotiate auth */
1233 #endif
1234 
1235   struct auth authhost;  /* auth details for host */
1236   struct auth authproxy; /* auth details for proxy */
1237 
1238   bool authproblem; /* TRUE if there's some problem authenticating */
1239 
1240 #ifdef USE_ARES
1241   ares_channel areschannel; /* for name resolves */
1242 #endif
1243 
1244 #if defined(USE_SSLEAY) && defined(HAVE_OPENSSL_ENGINE_H)
1245   ENGINE *engine;
1246 #endif /* USE_SSLEAY */
1247   struct timeval expiretime; /* set this with Curl_expire() only */
1248   struct Curl_tree timenode; /* for the splay stuff */
1249 
1250   /* a place to store the most recently set FTP entrypath */
1251   char *most_recent_ftp_entrypath;
1252 
1253   /* set after initial USER failure, to prevent an authentication loop */
1254   bool ftp_trying_alternative;
1255 
1256   int httpversion;       /* the lowest HTTP version*10 reported by any server
1257                             involved in this request */
1258   bool expect100header;  /* TRUE if we added Expect: 100-continue */
1259 
1260   bool pipe_broke; /* TRUE if the connection we were pipelined on broke
1261                       and we need to restart from the beginning */
1262 
1263 #if !defined(WIN32) && !defined(MSDOS) && !defined(__EMX__) && \
1264     !defined(__SYMBIAN32__)
1265 /* do FTP line-end conversions on most platforms */
1266 #define CURL_DO_LINEEND_CONV
1267   /* for FTP downloads: track CRLF sequences that span blocks */
1268   bool prev_block_had_trailing_cr;
1269   /* for FTP downloads: how many CRLFs did we converted to LFs? */
1270   curl_off_t crlf_conversions;
1271 #endif
1272   /* If set to non-NULL, there's a connection in a shared connection cache
1273      that uses this handle so we can't kill this SessionHandle just yet but
1274      must keep it around and add it to the list of handles to kill once all
1275      its connections are gone */
1276   void *shared_conn;
1277   bool closed; /* set to TRUE when curl_easy_cleanup() has been called on this
1278                   handle, but it is kept around as mentioned for
1279                   shared_conn */
1280   char *pathbuffer;/* allocated buffer to store the URL's path part in */
1281   char *path;      /* path to use, points to somewhere within the pathbuffer
1282                       area */
1283 
1284   bool use_range;
1285   bool rangestringalloc; /* the range string is malloc()'ed */
1286 
1287   char *range; /* range, if used. See README for detailed specification on
1288                   this syntax. */
1289   curl_off_t resume_from; /* continue [ftp] transfer from here */
1290 
1291   /* Protocol specific data.
1292    *
1293    *************************************************************************
1294    * Note that this data will be REMOVED after each request, so anything that
1295    * should be kept/stored on a per-connection basis and thus live for the
1296    * next request on the same connection MUST be put in the connectdata struct!
1297    *************************************************************************/
1298   union {
1299     struct HTTP *http;
1300     struct HTTP *https;  /* alias, just for the sake of being more readable */
1301     struct FTP *ftp;
1302     /* void *tftp;    not used */
1303     struct FILEPROTO *file;
1304     void *telnet;        /* private for telnet.c-eyes only */
1305     void *generic;
1306     struct SSHPROTO *ssh;
1307   } proto;
1308   /* current user of this SessionHandle instance, or NULL */
1309   struct connectdata *current_conn;
1310 };
1311 
1312 
1313 /*
1314  * This 'DynamicStatic' struct defines dynamic states that actually change
1315  * values in the 'UserDefined' area, which MUST be taken into consideration
1316  * if the UserDefined struct is cloned or similar. You can probably just
1317  * copy these, but each one indicate a special action on other data.
1318  */
1319 
1320 struct DynamicStatic {
1321   char *url;        /* work URL, copied from UserDefined */
1322   bool url_alloc;   /* URL string is malloc()'ed */
1323   char *referer;    /* referer string */
1324   bool referer_alloc; /* referer sting is malloc()ed */
1325   struct curl_slist *cookielist; /* list of cookie files set by
1326                                     curl_easy_setopt(COOKIEFILE) calls */
1327 };
1328 
1329 /*
1330  * This 'UserDefined' struct must only contain data that is set once to go
1331  * for many (perhaps) independent connections. Values that are generated or
1332  * calculated internally for the "session handle" MUST be defined within the
1333  * 'struct UrlState' instead. The only exceptions MUST note the changes in
1334  * the 'DynamicStatic' struct.
1335  * Character pointer fields point to dynamic storage, unless otherwise stated.
1336  */
1337 struct Curl_one_easy; /* declared and used only in multi.c */
1338 struct Curl_multi;    /* declared and used only in multi.c */
1339 
1340 enum dupstring {
1341   STRING_CERT,            /* client certificate file name */
1342   STRING_CERT_TYPE,       /* format for certificate (default: PEM)*/
1343   STRING_COOKIE,          /* HTTP cookie string to send */
1344   STRING_COOKIEJAR,       /* dump all cookies to this file */
1345   STRING_CUSTOMREQUEST,   /* HTTP/FTP request/method to use */
1346   STRING_DEVICE,          /* local network interface/address to use */
1347   STRING_ENCODING,        /* Accept-Encoding string */
1348   STRING_FTP_ACCOUNT,     /* ftp account data */
1349   STRING_FTP_ALTERNATIVE_TO_USER, /* command to send if USER/PASS fails */
1350   STRING_FTPPORT,         /* port to send with the FTP PORT command */
1351   STRING_KEY,             /* private key file name */
1352   STRING_KEY_PASSWD,      /* plain text private key password */
1353   STRING_KEY_TYPE,        /* format for private key (default: PEM) */
1354   STRING_KRB_LEVEL,       /* krb security level */
1355   STRING_NETRC_FILE,      /* if not NULL, use this instead of trying to find
1356                              $HOME/.netrc */
1357   STRING_COPYPOSTFIELDS,  /* if POST, set the fields' values here */
1358   STRING_PROXY,           /* proxy to use */
1359   STRING_SET_RANGE,       /* range, if used */
1360   STRING_SET_REFERER,     /* custom string for the HTTP referer field */
1361   STRING_SET_URL,         /* what original URL to work on */
1362   STRING_SSH_PRIVATE_KEY, /* path to the private key file for auth */
1363   STRING_SSH_PUBLIC_KEY,  /* path to the public key file for auth */
1364   STRING_SSL_CAPATH,      /* CA directory name (doesn't work on windows) */
1365   STRING_SSL_CAFILE,      /* certificate file to verify peer against */
1366   STRING_SSL_CIPHER_LIST, /* list of ciphers to use */
1367   STRING_SSL_EGDSOCKET,   /* path to file containing the EGD daemon socket */
1368   STRING_SSL_RANDOM_FILE, /* path to file containing "random" data */
1369   STRING_USERAGENT,       /* User-Agent string */
1370   STRING_SSH_HOST_PUBLIC_KEY_MD5, /* md5 of host public key in ascii hex */
1371   STRING_SSL_CRLFILE,     /* crl file to check certificate */
1372   STRING_SSL_ISSUERCERT,  /* issuer cert file to check certificate */
1373   STRING_USERNAME,        /* <username>, if used */
1374   STRING_PASSWORD,        /* <password>, if used */
1375   STRING_PROXYUSERNAME,   /* Proxy <username>, if used */
1376   STRING_PROXYPASSWORD,   /* Proxy <password>, if used */
1377   STRING_NOPROXY,         /* List of hosts which should not use the proxy, if
1378                              used */
1379 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1380   STRING_SOCKS5_GSSAPI_SERVICE,  /* GSSAPI service name */
1381 #endif
1382 
1383   /* -- end of strings -- */
1384   STRING_LAST /* not used, just an end-of-list marker */
1385 };
1386 
1387 struct UserDefined {
1388   FILE *err;         /* the stderr user data goes here */
1389   void *debugdata;   /* the data that will be passed to fdebug */
1390   char *errorbuffer; /* (Static) store failure messages in here */
1391   long proxyport; /* If non-zero, use this port number by default. If the
1392                      proxy string features a ":[port]" that one will override
1393                      this. */
1394   void *out;         /* the fetched file goes here */
1395   void *in;          /* the uploaded file is read from here */
1396   void *writeheader; /* write the header to this if non-NULL */
1397   long use_port;     /* which port to use (when not using default) */
1398   long httpauth;     /* what kind of HTTP authentication to use (bitmask) */
1399   long proxyauth;    /* what kind of proxy authentication to use (bitmask) */
1400   long followlocation; /* as in HTTP Location: */
1401   long maxredirs;    /* maximum no. of http(s) redirects to follow, set to -1
1402                         for infinity */
1403   bool post301;      /* Obey RFC 2616/10.3.2 and keep POSTs as POSTs after a
1404                         301 */
1405   bool post302;      /* keep POSTs as POSTs after a 302 */
1406   bool free_referer; /* set TRUE if 'referer' points to a string we
1407                         allocated */
1408   void *postfields;  /* if POST, set the fields' values here */
1409   curl_seek_callback seek_func;      /* function that seeks the input */
1410   curl_off_t postfieldsize; /* if POST, this might have a size to use instead
1411                                of strlen(), and then the data *may* be binary
1412                                (contain zero bytes) */
1413   unsigned short localport; /* local port number to bind to */
1414   int localportrange; /* number of additional port numbers to test in case the
1415                          'localport' one can't be bind()ed */
1416   curl_write_callback fwrite_func;   /* function that stores the output */
1417   curl_write_callback fwrite_header; /* function that stores headers */
1418   curl_read_callback fread_func;     /* function that reads the input */
1419   curl_progress_callback fprogress;  /* function for progress information */
1420   curl_debug_callback fdebug;      /* function that write informational data */
1421   curl_ioctl_callback ioctl_func;  /* function for I/O control */
1422   curl_sockopt_callback fsockopt;  /* function for setting socket options */
1423   void *sockopt_client; /* pointer to pass to the socket options callback */
1424   curl_opensocket_callback fopensocket; /* function for checking/translating
1425                                            the address and opening the socket */
1426   void* opensocket_client;
1427 
1428   void *seek_client;    /* pointer to pass to the seek callback */
1429   /* the 3 curl_conv_callback functions below are used on non-ASCII hosts */
1430   /* function to convert from the network encoding: */
1431   curl_conv_callback convfromnetwork;
1432   /* function to convert to the network encoding: */
1433   curl_conv_callback convtonetwork;
1434   /* function to convert from UTF-8 encoding: */
1435   curl_conv_callback convfromutf8;
1436 
1437   void *progress_client; /* pointer to pass to the progress callback */
1438   void *ioctl_client;   /* pointer to pass to the ioctl callback */
1439   long timeout;         /* in milliseconds, 0 means no timeout */
1440   long connecttimeout;  /* in milliseconds, 0 means no timeout */
1441   long ftp_response_timeout; /* in milliseconds, 0 means no timeout */
1442   long tftp_blksize ; /* in bytes, 0 means use default */
1443   curl_off_t infilesize;      /* size of file to upload, -1 means unknown */
1444   long low_speed_limit; /* bytes/second */
1445   long low_speed_time;  /* number of seconds */
1446   curl_off_t max_send_speed; /* high speed limit in bytes/second for upload */
1447   curl_off_t max_recv_speed; /* high speed limit in bytes/second for download */
1448   curl_off_t set_resume_from;  /* continue [ftp] transfer from here */
1449   struct curl_slist *headers; /* linked list of extra headers */
1450   struct curl_httppost *httppost;  /* linked list of POST data */
1451   bool cookiesession;   /* new cookie session? */
1452   bool crlf;            /* convert crlf on ftp upload(?) */
1453   struct curl_slist *quote;     /* after connection is established */
1454   struct curl_slist *postquote; /* after the transfer */
1455   struct curl_slist *prequote; /* before the transfer, after type */
1456   struct curl_slist *source_quote;  /* 3rd party quote */
1457   struct curl_slist *source_prequote;  /* in 3rd party transfer mode - before
1458                                           the transfer on source host */
1459   struct curl_slist *source_postquote; /* in 3rd party transfer mode - after
1460                                           the transfer on source host */
1461   struct curl_slist *telnet_options; /* linked list of telnet options */
1462   curl_TimeCond timecondition; /* kind of time/date comparison */
1463   time_t timevalue;       /* what time to compare with */
1464   Curl_HttpReq httpreq;   /* what kind of HTTP request (if any) is this */
1465   long httpversion; /* when non-zero, a specific HTTP version requested to
1466                        be used in the library's request(s) */
1467   struct ssl_config_data ssl;  /* user defined SSL stuff */
1468   curl_proxytype proxytype; /* what kind of proxy that is in use */
1469   long dns_cache_timeout; /* DNS cache timeout */
1470   long buffer_size;      /* size of receive buffer to use */
1471   void *private_data; /* application-private data */
1472 
1473   struct Curl_one_easy *one_easy; /* When adding an easy handle to a multi
1474                                      handle, an internal 'Curl_one_easy'
1475                                      struct is created and this is a pointer
1476                                      to the particular struct associated with
1477                                      this SessionHandle */
1478 
1479   struct curl_slist *http200aliases; /* linked list of aliases for http200 */
1480 
1481   long ip_version; /* the CURL_IPRESOLVE_* defines in the public header file
1482                       0 - whatever, 1 - v2, 2 - v6 */
1483 
1484   curl_off_t max_filesize; /* Maximum file size to download */
1485 
1486   curl_ftpfile ftp_filemethod; /* how to get to a file when FTP is used  */
1487 
1488   int ftp_create_missing_dirs; /* 1 - create directories that don't exist
1489                                   2 - the same but also allow MKD to fail once
1490                                */
1491 
1492 /* Here follows boolean settings that define how to behave during
1493    this session. They are STATIC, set by libcurl users or at least initially
1494    and they don't change during operations. */
1495 
1496   bool printhost;        /* printing host name in debug info */
1497   bool get_filetime;     /* get the time and get of the remote file */
1498   bool tunnel_thru_httpproxy; /* use CONNECT through a HTTP proxy */
1499   bool prefer_ascii;     /* ASCII rather than binary */
1500   bool ftp_append;       /* append, not overwrite, on upload */
1501   bool ftp_list_only;    /* switch FTP command for listing directories */
1502   bool ftp_use_port;     /* use the FTP PORT command */
1503   bool hide_progress;    /* don't use the progress meter */
1504   bool http_fail_on_error;  /* fail on HTTP error codes >= 300 */
1505   bool http_follow_location; /* follow HTTP redirects */
1506   bool http_disable_hostname_check_before_authentication;
1507   bool include_header;   /* include received protocol headers in data output */
1508   bool http_set_referer; /* is a custom referer used */
1509   bool http_auto_referer; /* set "correct" referer when following location: */
1510   bool opt_no_body;      /* as set with CURLOPT_NO_BODY */
1511   bool set_port;         /* custom port number used */
1512   bool upload;           /* upload request */
1513   enum CURL_NETRC_OPTION
1514        use_netrc;        /* defined in include/curl.h */
1515   bool verbose;          /* output verbosity */
1516   bool krb;              /* kerberos connection requested */
1517   bool reuse_forbid;     /* forbidden to be reused, close after use */
1518   bool reuse_fresh;      /* do not re-use an existing connection  */
1519   bool ftp_use_epsv;     /* if EPSV is to be attempted or not */
1520   bool ftp_use_eprt;     /* if EPRT is to be attempted or not */
1521 
1522   curl_usessl ftp_ssl;   /* if AUTH TLS is to be attempted etc */
1523   curl_ftpauth ftpsslauth; /* what AUTH XXX to be attempted */
1524   curl_ftpccc ftp_ccc;   /* FTP CCC options */
1525   bool no_signal;        /* do not use any signal/alarm handler */
1526   bool global_dns_cache; /* subject for future removal */
1527   bool tcp_nodelay;      /* whether to enable TCP_NODELAY or not */
1528   bool ignorecl;         /* ignore content length */
1529   bool ftp_skip_ip;      /* skip the IP address the FTP server passes on to
1530                             us */
1531   bool connect_only;     /* make connection, let application use the socket */
1532   long ssh_auth_types;   /* allowed SSH auth types */
1533   bool http_te_skip;     /* pass the raw body data to the user, even when
1534                             transfer-encoded (chunked, compressed) */
1535   bool http_ce_skip;     /* pass the raw body data to the user, even when
1536                             content-encoded (chunked, compressed) */
1537   long new_file_perms;    /* Permissions to use when creating remote files */
1538   long new_directory_perms; /* Permissions to use when creating remote dirs */
1539   bool proxy_transfer_mode; /* set transfer mode (;type=<a|i>) when doing FTP
1540                                via an HTTP proxy */
1541   char *str[STRING_LAST]; /* array of strings, pointing to allocated memory */
1542   unsigned int scope;    /* address scope for IPv6 */
1543   long allowed_protocols;
1544   long redir_protocols;
1545 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1546   long socks5_gssapi_nec; /* flag to support nec socks5 server */
1547 #endif
1548 };
1549 
1550 struct Names {
1551   struct curl_hash *hostcache;
1552   enum {
1553     HCACHE_NONE,    /* not pointing to anything */
1554     HCACHE_PRIVATE, /* points to our own */
1555     HCACHE_GLOBAL,  /* points to the (shrug) global one */
1556     HCACHE_MULTI,   /* points to a shared one in the multi handle */
1557     HCACHE_SHARED   /* points to a shared one in a shared object */
1558   } hostcachetype;
1559 };
1560 
1561 /*
1562  * The 'connectdata' struct MUST have all the connection oriented stuff as we
1563  * may have several simultaneous connections and connection structs in memory.
1564  *
1565  * The 'struct UserDefined' must only contain data that is set once to go for
1566  * many (perhaps) independent connections. Values that are generated or
1567  * calculated internally for the "session handle" must be defined within the
1568  * 'struct UrlState' instead.
1569  */
1570 
1571 struct SessionHandle {
1572   struct Names dns;
1573   struct Curl_multi *multi;    /* if non-NULL, points to the multi handle
1574                                   struct to which this "belongs" */
1575   struct Curl_one_easy *multi_pos; /* if non-NULL, points to its position
1576                                       in multi controlling structure to assist
1577                                       in removal. */
1578   struct Curl_share *share;    /* Share, handles global variable mutexing */
1579   struct SingleRequest req;    /* Request-specific data */
1580   struct UserDefined set;      /* values set by the libcurl user */
1581   struct DynamicStatic change; /* possibly modified userdefined data */
1582   struct CookieInfo *cookies;  /* the cookies, read from files and servers.
1583                                   NOTE that the 'cookie' field in the
1584                                   UserDefined struct defines if the "engine"
1585                                   is to be used or not. */
1586   struct Progress progress;    /* for all the progress meter data */
1587   struct UrlState state;       /* struct for fields used for state info and
1588                                   other dynamic purposes */
1589   struct PureInfo info;        /* stats, reports and info data */
1590 #if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV)
1591   iconv_t outbound_cd;         /* for translating to the network encoding */
1592   iconv_t inbound_cd;          /* for translating from the network encoding */
1593   iconv_t utf8_cd;             /* for translating to UTF8 */
1594 #endif /* CURL_DOES_CONVERSIONS && HAVE_ICONV */
1595   unsigned int magic;          /* set to a CURLEASY_MAGIC_NUMBER */
1596 };
1597 
1598 #define LIBCURL_NAME "libcurl"
1599 
1600 #endif
1601