3 /***************************************************************************
5 * Project ___| | | | _ \| |
7 * | (__| |_| | _ <| |___
8 * \___|\___/|_| \_\_____|
10 * Copyright (C) 1998 - 2002, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
24 ***************************************************************************/
26 /* This file is for lib internal stuff */
33 #define PORT_TELNET 23
34 #define PORT_GOPHER 70
36 #define PORT_HTTPS 443
37 #define PORT_DICT 2628
40 #define DICT_MATCH "/MATCH:"
41 #define DICT_MATCH2 "/M:"
42 #define DICT_MATCH3 "/FIND:"
43 #define DICT_DEFINE "/DEFINE:"
44 #define DICT_DEFINE2 "/D:"
45 #define DICT_DEFINE3 "/LOOKUP:"
47 #define CURL_DEFAULT_USER "anonymous"
48 #define CURL_DEFAULT_PASSWORD "curl_by_daniel@haxx.se"
54 /* SSLeay stuff usually in /usr/local/ssl/include */
56 #include "openssl/rsa.h"
57 #include "openssl/crypto.h"
58 #include "openssl/x509.h"
59 #include "openssl/pem.h"
60 #include "openssl/ssl.h"
61 #include "openssl/err.h"
62 #ifdef HAVE_OPENSSL_ENGINE_H
63 #include <openssl/engine.h>
75 #ifdef HAVE_NETINET_IN_H
76 #include <netinet/in.h>
81 #include <curl/curl.h>
83 #include "http_chunks.h" /* for the structs and enum stuff */
86 #include <zlib.h> /* for content-encoding 08/28/02 jhrg */
89 /* Download buffer size, keep it fairly big for speed reasons */
90 #define BUFSIZE CURL_MAX_WRITE_SIZE
92 /* Initial size of the buffer to store headers in, it'll be enlarged in case
94 #define HEADERSIZE 256
96 /* Just a convenience macro to get the larger value out of two given */
98 #define MAX(x,y) ((x)>(y)?(x):(y))
102 /* Types needed for krb4-ftp connections */
109 enum protection_level {
117 #ifndef HAVE_OPENSSL_ENGINE_H
120 /* struct for data related to SSL and SSL connections */
121 struct ssl_connect_data {
122 bool use; /* use ssl encrypted communications TRUE/FALSE */
124 /* these ones requires specific SSL-types */
128 #endif /* USE_SSLEAY */
131 /* information about one single SSL session */
132 struct curl_ssl_session {
133 char *name; /* host name for which this ID was used */
134 void *sessionid; /* as returned from the SSL layer */
135 long age; /* just a number, the higher the more recent */
136 unsigned short remote_port; /* remote port to connect to */
139 struct ssl_config_data {
140 long version; /* what version the client wants to use */
141 long certverifyresult; /* result from the certificate verification */
142 long verifypeer; /* set TRUE if this is desired */
143 long verifyhost; /* 0: no verify
144 1: check that CN exists
145 2: CN must match hostname */
146 char *CApath; /* DOES NOT WORK ON WINDOWS */
147 char *CAfile; /* cerficate to verify peer against */
148 char *random_file; /* path to file containing "random" data */
149 char *egdsocket; /* path to file containing the EGD daemon socket */
150 char *cipher_list; /* list of ciphers to use */
151 long numsessions; /* SSL session id cache size */
154 /****************************************************************************
156 ***************************************************************************/
158 struct FormData *sendit;
160 const char *p_pragma; /* Pragma: string */
161 const char *p_accept; /* Accept: string */
165 /* For FORM posting */
167 curl_read_callback storefread;
170 struct Curl_chunker chunk;
173 /****************************************************************************
175 ***************************************************************************/
178 char *user; /* user name string */
179 char *passwd; /* password string */
180 char *urlpath; /* the originally given path part of the URL */
181 char *dir; /* decoded directory */
182 char *file; /* decoded file */
184 char *entrypath; /* the PWD reply when we logged on */
186 char *cache; /* data cache between getresponse()-calls */
187 size_t cache_size; /* size of cache in bytes */
188 bool dont_check; /* Set to TRUE to prevent the final (post-transfer)
189 file size and 226/250 status check. It should still
190 read the line, just ignore the result. */
191 bool no_transfer; /* nothing was transfered, (possibly because a resumed
192 transfer already was complete) */
196 /****************************************************************************
198 ***************************************************************************/
200 int fd; /* open file descriptor to read from! */
204 * Boolean values that concerns this connection.
207 bool close; /* if set, we close the connection after this request */
208 bool reuse; /* if set, this is a re-used connection */
209 bool chunk; /* if set, this is a chunked transfer-encoding */
210 bool httpproxy; /* if set, this transfer is done through a http proxy */
211 bool user_passwd; /* do we use user+password for this connection? */
212 bool proxy_user_passwd; /* user+password for the proxy? */
213 bool ipv6_ip; /* we communicate with a remove site specified with pure IPv6
216 bool rangestringalloc; /* the range string is malloc()'ed */
220 * This struct is all the previously local variables from Curl_perform() moved
221 * to struct to allow the function to return and get re-invoked better without
225 struct Curl_transfer_keeper {
226 int bytecount; /* total number of bytes read */
227 int writebytecount; /* number of bytes written */
228 long contentlength; /* size of incoming data */
229 struct timeval start; /* transfer started at this time */
230 struct timeval now; /* current time */
231 bool header; /* incoming data has HTTP header */
233 HEADER_NORMAL, /* no bad header at all */
234 HEADER_PARTHEADER, /* part of the chunk is a bad header, the rest is
236 HEADER_ALLBAD /* all was believed to be header */
237 } badheader; /* the header was deemed bad and will be
239 int headerline; /* counts header lines to better track the
241 char *hbufp; /* points at *end* of header line */
243 char *str; /* within buf */
244 char *str_start; /* within buf */
245 char *end_ptr; /* within buf */
246 char *p; /* within headerbuff */
247 bool content_range; /* set TRUE if Content-Range: was found */
248 int offset; /* possible resume offset read from the
249 Content-Range: header */
250 int httpcode; /* error code from the 'HTTP/1.? XXX' line */
251 int httpversion; /* the HTTP version*10 */
252 bool write_after_100_header; /* should we enable the write after
253 we received a 100-continue/timeout
255 int content_encoding; /* What content encoding. sec 3.5, RFC2616. */
257 #define IDENTITY 0 /* No encoding */
258 #define DEFLATE 1 /* zlib delfate [RFC 1950 & 1951] */
259 #define GZIP 2 /* gzip algorithm [RFC 1952] */
260 #define COMPRESS 3 /* Not handled, added for completeness */
263 bool zlib_init; /* True if zlib already initialized;
264 undefined if Content-Encdoing header. */
265 z_stream z; /* State structure for zlib. */
268 /* for the low speed checks: */
277 /* pointers to the actual descriptors we check */
281 /* the file descriptors to play with */
292 * The connectdata struct contains all fields and variables that should be
293 * unique for an entire connection.
296 /**** Fields set when inited and not modified again */
297 struct SessionHandle *data; /* link to the root CURL struct */
298 int connectindex; /* what index in the connects index this particular
301 long protocol; /* PROT_* flags concerning the protocol set */
302 #define PROT_MISSING (1<<0)
303 #define PROT_GOPHER (1<<1)
304 #define PROT_HTTP (1<<2)
305 #define PROT_HTTPS (1<<3)
306 #define PROT_FTP (1<<4)
307 #define PROT_TELNET (1<<5)
308 #define PROT_DICT (1<<6)
309 #define PROT_LDAP (1<<7)
310 #define PROT_FILE (1<<8)
311 #define PROT_FTPS (1<<9)
312 #define PROT_SSL (1<<10) /* protocol requires SSL */
315 struct addrinfo *serv_addr; /* the particular host we use */
317 struct sockaddr_in serv_addr;
319 char protostr[64]; /* store the protocol string in this buffer */
320 char gname[513]; /* store the hostname in this buffer */
321 char *name; /* host name pointer to fool around with */
322 char *path; /* allocated buffer to store the URL's path part in */
323 char *hostname; /* hostname to connect, as parsed from url */
324 long port; /* which port to use locally */
325 unsigned short remote_port; /* what remote port to connect to,
326 not the proxy port! */
329 long headerbytecount; /* only count received headers */
331 char *range; /* range, if used. See README for detailed specification on
333 ssize_t resume_from; /* continue [ftp] transfer from here */
335 char *proxyhost; /* name of the http proxy host */
337 struct timeval now; /* "current" time */
338 struct timeval created; /* creation time */
339 int firstsocket; /* the main socket to use */
340 int secondarysocket; /* for i.e ftp transfers */
341 long maxdownload; /* in bytes, the maximum amount of data to fetch, 0
344 struct ssl_connect_data ssl; /* this is for ssl-stuff */
346 struct ConnectBits bits; /* various state-flags for this connection */
348 /* These two functions MUST be set by the curl_connect() function to be
349 be protocol dependent */
350 CURLcode (*curl_do)(struct connectdata *connect);
351 CURLcode (*curl_done)(struct connectdata *connect);
353 /* If the curl_do() function is better made in two halves, this
354 * curl_do_more() function will be called afterwards, if set. For example
355 * for doing the FTP stuff after the PASV/PORT command.
357 CURLcode (*curl_do_more)(struct connectdata *connect);
359 /* This function *MAY* be set to a protocol-dependent function that is run
360 * after the connect() and everything is done, as a step in the connection.
362 CURLcode (*curl_connect)(struct connectdata *connect);
364 /* This function *MAY* be set to a protocol-dependent function that is run
365 * by the curl_disconnect(), as a step in the disconnection.
367 CURLcode (*curl_disconnect)(struct connectdata *connect);
369 /* This function *MAY* be set to a protocol-dependent function that is run
370 * in the curl_close() function if protocol-specific cleanups are required.
372 CURLcode (*curl_close)(struct connectdata *connect);
374 /**** curl_get() phase fields */
377 int sockfd; /* socket to read from or -1 */
378 int size; /* -1 if unknown at this point */
379 bool getheader; /* TRUE if header parsing is wanted */
380 long *bytecountp; /* return number of bytes read or NULL */
383 int writesockfd; /* socket to write to, it may very well be
384 the same we read from. -1 disables */
385 long *writebytecountp; /* return number of bytes written or NULL */
387 /** Dynamicly allocated strings, may need to be freed before this **/
388 /** struct is killed. **/
389 struct dynamically_allocated_data {
390 char *proxyuserpwd; /* free later if not NULL! */
391 char *uagent; /* free later if not NULL! */
392 char *accept_encoding; /* free later if not NULL! 08/28/02 jhrg */
393 char *userpwd; /* free later if not NULL! */
394 char *rangeline; /* free later if not NULL! */
395 char *ref; /* free later if not NULL! */
396 char *cookie; /* free later if not NULL! */
397 char *host; /* free later if not NULL */
400 char *newurl; /* This can only be set if a Location: was in the
404 enum protection_level command_prot;
405 enum protection_level data_prot;
406 enum protection_level request_data_prot;
410 struct krb4buffer in_buffer, out_buffer;
414 struct Curl_sec_client_mech *mech;
415 struct sockaddr_in local_addr;
419 /*************** Request - specific items ************/
420 /* previously this was in the urldata struct */
423 struct HTTP *gopher; /* alias, just for the sake of being more readable */
424 struct HTTP *https; /* alias, just for the sake of being more readable */
427 void *telnet; /* private for telnet.c-eyes only */
428 #if 0 /* no need for special ones for these: */
435 /* This struct is inited when needed */
436 struct Curl_transfer_keeper keep;
438 /* 'upload_present' is used to keep a byte counter of how much data there is
439 still left in the buffer, aimed for upload. */
442 /* 'upload_fromhere' is used as a read-pointer when we uploaded parts of a
443 buffer, so the next read should read from where this pointer points to,
444 and the 'upload_present' contains the number of bytes available at this
446 char *upload_fromhere;
448 bool do_more; /* this is set TRUE if the ->curl_do_more() function is
449 supposed to be called, after ->curl_do() */
452 /* The end of connectdata. 08/27/02 jhrg */
455 * Struct to keep statistical and informational data.
460 long filetime; /* If requested, this is might get set. Set to -1 if
461 the time was unretrievable */
462 long header_size; /* size of read header(s) in bytes */
463 long request_size; /* the amount of bytes sent in the request(s) */
465 char *contenttype; /* the content type of the object */
470 long lastshow; /* time() of the last displayed progress meter or NULL to
471 force redraw at next call */
477 double current_speed; /* uses the currently fastest transfer */
479 bool callback; /* set when progress callback is used */
480 int width; /* screen width at download start */
481 int flags; /* see progress.h */
490 double t_pretransfer;
491 double t_starttransfer;
494 struct timeval start;
495 struct timeval t_startsingle;
496 #define CURR_TIME (5+1) /* 6 entries for 5 seconds */
498 double speeder[ CURR_TIME ];
499 struct timeval speeder_time[ CURR_TIME ];
504 HTTPREQ_NONE, /* first in list */
507 HTTPREQ_POST_FORM, /* we make a difference internally */
510 HTTPREQ_LAST /* last in list */
514 * Values that are generated, temporary or calculated internally for a
515 * "session handle" must be defined within the 'struct urlstate'. This struct
516 * will be used within the SessionHandle struct. When the 'SessionHandle'
517 * struct is cloned, this data MUST NOT be copied.
519 * Remember that any "state" information goes globally for the curl handle.
520 * Session-data MUST be put in the connectdata struct and here. */
521 #define MAX_CURL_USER_LENGTH 256
522 #define MAX_CURL_PASSWORD_LENGTH 256
531 /* buffers to store authentication data in, as parsed from input options */
532 char user[MAX_CURL_USER_LENGTH];
533 char passwd[MAX_CURL_PASSWORD_LENGTH];
534 char proxyuser[MAX_CURL_USER_LENGTH];
535 char proxypasswd[MAX_CURL_PASSWORD_LENGTH];
537 struct timeval keeps_speed; /* for the progress meter really */
539 /* 'connects' will be an allocated array with pointers. If the pointer is
540 set, it holds an allocated connection. */
541 struct connectdata **connects;
542 long numconnects; /* size of the 'connects' array */
544 char *headerbuff; /* allocated buffer to store headers in */
545 int headersize; /* size of the allocation */
547 char buffer[BUFSIZE+1]; /* download buffer */
548 char uploadbuffer[BUFSIZE+1]; /* upload buffer */
549 double current_speed; /* the ProgressShow() funcion sets this */
551 bool this_is_a_follow; /* this is a followed Location: request */
553 char *auth_host; /* if set, this should be the host name that we will
554 sent authorization to, no else. Used to make Location:
555 following not keep sending user+password... This is
559 struct curl_ssl_session *session; /* array of 'numsessions' size */
560 long sessionage; /* number of the most recent session */
562 char scratch[BUFSIZE*2]; /* huge buffer when doing upload CRLF replacing */
563 bool errorbuf; /* Set to TRUE if the error buffer is already filled in.
564 This must be set to FALSE every time _easy_perform() is
568 /* storage for the previous bag^H^H^HSIGPIPE signal handler :-) */
569 void (*prev_signal)(int sig);
571 bool allow_port; /* Is set.use_port allowed to take effect or not. This
572 is always set TRUE when curl_easy_perform() is called. */
577 * This 'DynamicStatic' struct defines dynamic states that actually change
578 * values in the 'UserDefined' area, which MUST be taken into consideration
579 * if the UserDefined struct is cloned or similar. You can probably just
580 * copy these, but each one indicate a special action on other data.
583 struct DynamicStatic {
584 char *url; /* work URL, copied from UserDefined */
585 bool url_alloc; /* URL string is malloc()'ed */
586 char *proxy; /* work proxy, copied from UserDefined */
587 bool proxy_alloc; /* http proxy string is malloc()'ed */
588 char *referer; /* referer string */
589 bool referer_alloc; /* referer sting is malloc()ed */
590 struct curl_slist *cookielist; /* list of cookie files set by
591 curl_easy_setopt(COOKIEFILE) calls */
595 * This 'UserDefined' struct must only contain data that is set once to go
596 * for many (perhaps) independent connections. Values that are generated or
597 * calculated internally for the "session handle" MUST be defined within the
598 * 'struct urlstate' instead. The only exceptions MUST note the changes in
599 * the 'DynamicStatic' struct.
603 FILE *err; /* the stderr user data goes here */
604 void *debugdata; /* the data that will be passed to fdebug */
605 char *errorbuffer; /* store failure messages in here */
606 char *proxyuserpwd; /* Proxy <user:password>, if used */
607 long proxyport; /* If non-zero, use this port number by default. If the
608 proxy string features a ":[port]" that one will override
610 void *out; /* the fetched file goes here */
611 void *in; /* the uploaded file is read from here */
612 void *writeheader; /* write the header to this is non-NULL */
613 char *set_url; /* what original URL to work on */
614 char *set_proxy; /* proxy to use */
615 long use_port; /* which port to use (when not using default) */
616 char *userpwd; /* <user:password>, if used */
617 char *set_range; /* range, if used. See README for detailed specification
619 long followlocation; /* as in HTTP Location: */
620 long maxredirs; /* maximum no. of http(s) redirects to follow */
621 char *set_referer; /* custom string */
622 bool free_referer; /* set TRUE if 'referer' points to a string we
624 char *useragent; /* User-Agent string */
625 char *encoding; /* Accept-Encoding string 08/28/02 jhrg */
626 char *postfields; /* if POST, set the fields' values here */
627 size_t postfieldsize; /* if POST, this might have a size to use instead of
628 strlen(), and then the data *may* be binary (contain
630 char *ftpport; /* port to send with the FTP PORT command */
631 char *device; /* network interface to use */
632 curl_write_callback fwrite; /* function that stores the output */
633 curl_write_callback fwrite_header; /* function that stores headers */
634 curl_read_callback fread; /* function that reads the input */
635 curl_progress_callback fprogress; /* function for progress information */
636 curl_debug_callback fdebug; /* function that write informational data */
637 void *progress_client; /* pointer to pass to the progress callback */
638 curl_passwd_callback fpasswd; /* call for password */
639 void *passwd_client; /* pass to the passwd callback */
640 long timeout; /* in seconds, 0 means no timeout */
641 long connecttimeout; /* in seconds, 0 means no timeout */
642 long infilesize; /* size of file to upload, -1 means unknown */
643 long low_speed_limit; /* bytes/second */
644 long low_speed_time; /* number of seconds */
645 int set_resume_from; /* continue [ftp] transfer from here */
646 char *cookie; /* HTTP cookie string to send */
647 struct curl_slist *headers; /* linked list of extra headers */
648 struct HttpPost *httppost; /* linked list of POST data */
649 char *cert; /* certificate */
650 char *cert_type; /* format for certificate (default: PEM) */
651 char *key; /* private key */
652 char *key_type; /* format for private key (default: PEM) */
653 char *key_passwd; /* plain text private key password */
654 char *crypto_engine; /* name of the crypto engine to use */
655 char *cookiejar; /* dump all cookies to this file */
656 bool cookiesession; /* new cookie session? */
657 bool crlf; /* convert crlf on ftp upload(?) */
658 struct curl_slist *quote; /* after connection is established */
659 struct curl_slist *postquote; /* after the transfer */
660 struct curl_slist *prequote; /* before the transfer, after type (Wesley Laxton)*/
661 struct curl_slist *telnet_options; /* linked list of telnet options */
662 curl_TimeCond timecondition; /* kind of time/date comparison */
663 time_t timevalue; /* what time to compare with */
664 curl_closepolicy closepolicy; /* connection cache close concept */
665 Curl_HttpReq httpreq; /* what kind of HTTP request (if any) is this */
666 char *customrequest; /* HTTP/FTP request to use */
667 long httpversion; /* when non-zero, a specific HTTP version requested to
668 be used in the library's request(s) */
669 char *auth_host; /* if set, this is the allocated string to the host name
670 * to which to send the authorization data to, and no other
671 * host (which location-following otherwise could lead to)
673 char *krb4_level; /* what security level */
674 struct ssl_config_data ssl; /* user defined SSL stuff */
676 curl_proxytype proxytype; /* what kind of proxy that is in use */
678 int dns_cache_timeout; /* DNS cache timeout */
679 long buffer_size; /* size of receive buffer to use */
681 /* Here follows boolean settings that define how to behave during
682 this session. They are STATIC, set by libcurl users or at least initially
683 and they don't change during operations. */
686 bool tunnel_thru_httpproxy;
692 bool http_fail_on_error;
693 bool http_follow_location;
695 #define http_include_header include_header /* former name */
697 bool http_set_referer;
698 bool http_auto_referer; /* set "correct" referer when following location: */
702 enum CURL_NETRC_OPTION
703 use_netrc; /* defined in include/curl.h */
705 bool krb4; /* kerberos4 connection requested */
706 bool reuse_forbid; /* forbidden to be reused, close after use */
707 bool reuse_fresh; /* do not re-use an existing connection */
708 bool expect100header; /* TRUE if we added Expect: 100-continue */
709 bool ftp_use_epsv; /* if EPSV is to be attempted or not */
710 bool no_signal; /* do not use any signal/alarm handler */
712 bool global_dns_cache;
716 * In August 2001, this struct was redesigned and is since stricter than
717 * before. The 'connectdata' struct MUST have all the connection oriented
718 * stuff as we may now have several simultaneous connections and connection
721 * From now on, the 'SessionHandle' must only contain data that is set once to
722 * go for many (perhaps) independent connections. Values that are generated or
723 * calculated internally for the "session handle" must be defined within the
724 * 'struct urlstate' instead. */
726 struct SessionHandle {
727 curl_hash *hostcache;
728 curl_share *share; /* Share, handles global variable mutexing */
729 struct UserDefined set; /* values set by the libcurl user */
730 struct DynamicStatic change; /* possibly modified userdefined data */
732 struct CookieInfo *cookies; /* the cookies, read from files and servers */
733 struct Progress progress; /* for all the progress meter data */
734 struct UrlState state; /* struct for fields used for state info and
735 other dynamic purposes */
736 struct PureInfo info; /* stats, reports and info data */
739 #endif /* USE_SSLEAY */
742 #define LIBCURL_NAME "libcurl"