3 /*****************************************************************************
5 * Project ___| | | | _ \| |
7 * | (__| |_| | _ <| |___
8 * \___|\___/|_| \_\_____|
10 * Copyright (C) 1998 - 2002, Daniel Stenberg, <daniel@haxx.se>, et al.
12 * In order to be useful for every potential user, curl and libcurl are
13 * dual-licensed under the MPL and the MIT/X-derivate licenses.
15 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
16 * copies of the Software, and permit persons to whom the Software is
17 * furnished to do so, under the terms of the MPL or the MIT/X-derivate
18 * licenses. You may pick one of these licenses.
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 */
85 /* Download buffer size, keep it fairly big for speed reasons */
86 #define BUFSIZE CURL_MAX_WRITE_SIZE
88 /* Initial size of the buffer to store headers in, it'll be enlarged in case
90 #define HEADERSIZE 256
92 /* Just a convenience macro to get the larger value out of two given */
94 #define MAX(x,y) ((x)>(y)?(x):(y))
98 /* Types needed for krb4-ftp connections */
105 enum protection_level {
113 #ifndef HAVE_OPENSSL_ENGINE_H
116 /* struct for data related to SSL and SSL connections */
117 struct ssl_connect_data {
118 bool use; /* use ssl encrypted communications TRUE/FALSE */
120 /* these ones requires specific SSL-types */
124 #endif /* USE_SSLEAY */
127 /* information about one single SSL session */
128 struct curl_ssl_session {
129 char *name; /* host name for which this ID was used */
130 void *sessionid; /* as returned from the SSL layer */
131 long age; /* just a number, the higher the more recent */
132 unsigned short remote_port; /* remote port to connect to */
135 struct ssl_config_data {
136 long version; /* what version the client wants to use */
137 long certverifyresult; /* result from the certificate verification */
138 long verifypeer; /* set TRUE if this is desired */
139 long verifyhost; /* 0: no verify
140 1: check that CN exists
141 2: CN must match hostname */
142 char *CApath; /* DOES NOT WORK ON WINDOWS */
143 char *CAfile; /* cerficate to verify peer against */
144 char *random_file; /* path to file containing "random" data */
145 char *egdsocket; /* path to file containing the EGD daemon socket */
146 char *cipher_list; /* list of ciphers to use */
147 long numsessions; /* SSL session id cache size */
150 /****************************************************************************
152 ***************************************************************************/
154 struct FormData *sendit;
156 const char *p_pragma; /* Pragma: string */
157 const char *p_accept; /* Accept: string */
161 /* For FORM posting */
163 curl_read_callback storefread;
166 struct Curl_chunker chunk;
169 /****************************************************************************
171 ***************************************************************************/
174 char *user; /* user name string */
175 char *passwd; /* password string */
176 char *urlpath; /* the originally given path part of the URL */
177 char *dir; /* decoded directory */
178 char *file; /* decoded file */
180 char *entrypath; /* the PWD reply when we logged on */
182 char *cache; /* data cache between getresponse()-calls */
183 size_t cache_size; /* size of cache in bytes */
184 bool dont_check; /* Set to TRUE to prevent the final (post-transfer)
185 file size and 226/250 status check. It should still
186 read the line, just ignore the result. */
187 bool no_transfer; /* nothing was transfered, (possibly because a resumed
188 transfer already was complete) */
192 /****************************************************************************
194 ***************************************************************************/
196 int fd; /* open file descriptor to read from! */
200 * Boolean values that concerns this connection.
203 bool close; /* if set, we close the connection after this request */
204 bool reuse; /* if set, this is a re-used connection */
205 bool chunk; /* if set, this is a chunked transfer-encoding */
206 bool httpproxy; /* if set, this transfer is done through a http proxy */
207 bool user_passwd; /* do we use user+password for this connection? */
208 bool proxy_user_passwd; /* user+password for the proxy? */
211 bool rangestringalloc; /* the range string is malloc()'ed */
215 * This struct is all the previously local variables from Curl_perform() moved
216 * to struct to allow the function to return and get re-invoked better without
220 struct Curl_transfer_keeper {
221 int bytecount; /* total number of bytes read */
222 int writebytecount; /* number of bytes written */
223 long contentlength; /* size of incoming data */
224 struct timeval start; /* transfer started at this time */
225 struct timeval now; /* current time */
226 bool header; /* incoming data has HTTP header */
227 int headerline; /* counts header lines to better track the
229 char *hbufp; /* points at *end* of header line */
231 char *str; /* within buf */
232 char *str_start; /* within buf */
233 char *end_ptr; /* within buf */
234 char *p; /* within headerbuff */
235 bool content_range; /* set TRUE if Content-Range: was found */
236 int offset; /* possible resume offset read from the
237 Content-Range: header */
238 int httpcode; /* error code from the 'HTTP/1.? XXX' line */
239 int httpversion; /* the HTTP version*10 */
240 bool write_after_100_header; /* should we enable the write after
241 we received a 100-continue/timeout
244 /* for the low speed checks: */
253 /* pointers to the actual descriptors we check */
257 /* the file descriptors to play with */
268 * The connectdata struct contains all fields and variables that should be
269 * unique for an entire connection.
272 /**** Fields set when inited and not modified again */
273 struct SessionHandle *data; /* link to the root CURL struct */
274 int connectindex; /* what index in the connects index this particular
277 long protocol; /* PROT_* flags concerning the protocol set */
278 #define PROT_MISSING (1<<0)
279 #define PROT_GOPHER (1<<1)
280 #define PROT_HTTP (1<<2)
281 #define PROT_HTTPS (1<<3)
282 #define PROT_FTP (1<<4)
283 #define PROT_TELNET (1<<5)
284 #define PROT_DICT (1<<6)
285 #define PROT_LDAP (1<<7)
286 #define PROT_FILE (1<<8)
287 #define PROT_FTPS (1<<9)
288 #define PROT_SSL (1<<10) /* protocol requires SSL */
291 struct addrinfo *serv_addr; /* the particular host we use */
293 struct sockaddr_in serv_addr;
295 char protostr[64]; /* store the protocol string in this buffer */
296 char gname[513]; /* store the hostname in this buffer */
297 char *name; /* host name pointer to fool around with */
298 char *path; /* allocated buffer to store the URL's path part in */
299 char *hostname; /* hostname to connect, as parsed from url */
300 long port; /* which port to use locally */
301 unsigned short remote_port; /* what remote port to connect to,
302 not the proxy port! */
305 long headerbytecount; /* only count received headers */
307 char *range; /* range, if used. See README for detailed specification on
309 ssize_t resume_from; /* continue [ftp] transfer from here */
311 char *proxyhost; /* name of the http proxy host */
313 struct timeval now; /* "current" time */
314 struct timeval created; /* creation time */
315 int firstsocket; /* the main socket to use */
316 int secondarysocket; /* for i.e ftp transfers */
317 long maxdownload; /* in bytes, the maximum amount of data to fetch, 0
320 struct ssl_connect_data ssl; /* this is for ssl-stuff */
322 struct ConnectBits bits; /* various state-flags for this connection */
324 /* These two functions MUST be set by the curl_connect() function to be
325 be protocol dependent */
326 CURLcode (*curl_do)(struct connectdata *connect);
327 CURLcode (*curl_done)(struct connectdata *connect);
329 /* If the curl_do() function is better made in two halves, this
330 * curl_do_more() function will be called afterwards, if set. For example
331 * for doing the FTP stuff after the PASV/PORT command.
333 CURLcode (*curl_do_more)(struct connectdata *connect);
335 /* This function *MAY* be set to a protocol-dependent function that is run
336 * after the connect() and everything is done, as a step in the connection.
338 CURLcode (*curl_connect)(struct connectdata *connect);
340 /* This function *MAY* be set to a protocol-dependent function that is run
341 * by the curl_disconnect(), as a step in the disconnection.
343 CURLcode (*curl_disconnect)(struct connectdata *connect);
345 /* This function *MAY* be set to a protocol-dependent function that is run
346 * in the curl_close() function if protocol-specific cleanups are required.
348 CURLcode (*curl_close)(struct connectdata *connect);
350 /**** curl_get() phase fields */
353 int sockfd; /* socket to read from or -1 */
354 int size; /* -1 if unknown at this point */
355 bool getheader; /* TRUE if header parsing is wanted */
356 long *bytecountp; /* return number of bytes read or NULL */
359 int writesockfd; /* socket to write to, it may very well be
360 the same we read from. -1 disables */
361 long *writebytecountp; /* return number of bytes written or NULL */
363 /** Dynamicly allocated strings, may need to be freed before this **/
364 /** struct is killed. **/
365 struct dynamically_allocated_data {
366 char *proxyuserpwd; /* free later if not NULL! */
367 char *uagent; /* free later if not NULL! */
368 char *userpwd; /* free later if not NULL! */
369 char *rangeline; /* free later if not NULL! */
370 char *ref; /* free later if not NULL! */
371 char *cookie; /* free later if not NULL! */
372 char *host; /* free later if not NULL */
375 char *newurl; /* This can only be set if a Location: was in the
379 enum protection_level command_prot;
380 enum protection_level data_prot;
381 enum protection_level request_data_prot;
385 struct krb4buffer in_buffer, out_buffer;
389 struct Curl_sec_client_mech *mech;
390 struct sockaddr_in local_addr;
394 /*************** Request - specific items ************/
395 /* previously this was in the urldata struct */
398 struct HTTP *gopher; /* alias, just for the sake of being more readable */
399 struct HTTP *https; /* alias, just for the sake of being more readable */
402 void *telnet; /* private for telnet.c-eyes only */
403 #if 0 /* no need for special ones for these: */
410 /* This struct is inited when needed */
411 struct Curl_transfer_keeper keep;
413 /* 'upload_present' is used to keep a byte counter of how much data there is
414 still left in the buffer, aimed for upload. */
417 /* 'upload_fromhere' is used as a read-pointer when we uploaded parts of a
418 buffer, so the next read should read from where this pointer points to,
419 and the 'upload_present' contains the number of bytes available at this
421 char *upload_fromhere;
423 bool do_more; /* this is set TRUE if the ->curl_do_more() function is
424 supposed to be called, after ->curl_do() */
428 * Struct to keep statistical and informational data.
433 long filetime; /* If requested, this is might get set. Set to -1 if
434 the time was unretrievable */
435 long header_size; /* size of read header(s) in bytes */
436 long request_size; /* the amount of bytes sent in the request(s) */
438 char *contenttype; /* the content type of the object */
443 long lastshow; /* time() of the last displayed progress meter or NULL to
444 force redraw at next call */
450 double current_speed; /* uses the currently fastest transfer */
452 bool callback; /* set when progress callback is used */
453 int width; /* screen width at download start */
454 int flags; /* see progress.h */
463 double t_pretransfer;
464 double t_starttransfer;
467 struct timeval start;
468 struct timeval t_startsingle;
469 #define CURR_TIME (5+1) /* 6 entries for 5 seconds */
471 double speeder[ CURR_TIME ];
472 struct timeval speeder_time[ CURR_TIME ];
477 HTTPREQ_NONE, /* first in list */
480 HTTPREQ_POST_FORM, /* we make a difference internally */
483 HTTPREQ_LAST /* last in list */
487 * Values that are generated, temporary or calculated internally for a
488 * "session handle" must be defined within the 'struct urlstate'. This struct
489 * will be used within the SessionHandle struct. When the 'SessionHandle'
490 * struct is cloned, this data MUST NOT be copied.
492 * Remember that any "state" information goes globally for the curl handle.
493 * Session-data MUST be put in the connectdata struct and here. */
494 #define MAX_CURL_USER_LENGTH 256
495 #define MAX_CURL_PASSWORD_LENGTH 256
504 /* buffers to store authentication data in, as parsed from input options */
505 char user[MAX_CURL_USER_LENGTH];
506 char passwd[MAX_CURL_PASSWORD_LENGTH];
507 char proxyuser[MAX_CURL_USER_LENGTH];
508 char proxypasswd[MAX_CURL_PASSWORD_LENGTH];
510 struct timeval keeps_speed; /* for the progress meter really */
512 /* 'connects' will be an allocated array with pointers. If the pointer is
513 set, it holds an allocated connection. */
514 struct connectdata **connects;
515 long numconnects; /* size of the 'connects' array */
517 char *headerbuff; /* allocated buffer to store headers in */
518 int headersize; /* size of the allocation */
520 char buffer[BUFSIZE+1]; /* download buffer */
521 char uploadbuffer[BUFSIZE+1]; /* upload buffer */
522 double current_speed; /* the ProgressShow() funcion sets this */
524 bool this_is_a_follow; /* this is a followed Location: request */
526 char *auth_host; /* if set, this should be the host name that we will
527 sent authorization to, no else. Used to make Location:
528 following not keep sending user+password... This is
532 struct curl_ssl_session *session; /* array of 'numsessions' size */
533 long sessionage; /* number of the most recent session */
535 char scratch[BUFSIZE*2]; /* huge buffer when doing upload CRLF replacing */
536 bool errorbuf; /* Set to TRUE if the error buffer is already filled in.
537 This must be set to FALSE every time _easy_perform() is
541 /* storage for the previous bag^H^H^HSIGPIPE signal handler :-) */
542 void (*prev_signal)(int sig);
544 bool allow_port; /* Is set.use_port allowed to take effect or not. This
545 is always set TRUE when curl_easy_perform() is called. */
550 * This 'DynamicStatic' struct defines dynamic states that actually change
551 * values in the 'UserDefined' area, which MUST be taken into consideration
552 * if the UserDefined struct is cloned or similar. You can probably just
553 * copy these, but each one indicate a special action on other data.
556 struct DynamicStatic {
557 char *url; /* work URL, copied from UserDefined */
558 bool url_alloc; /* URL string is malloc()'ed */
559 char *proxy; /* work proxy, copied from UserDefined */
560 bool proxy_alloc; /* http proxy string is malloc()'ed */
561 char *referer; /* referer string */
562 bool referer_alloc; /* referer sting is malloc()ed */
566 * This 'UserDefined' struct must only contain data that is set once to go
567 * for many (perhaps) independent connections. Values that are generated or
568 * calculated internally for the "session handle" MUST be defined within the
569 * 'struct urlstate' instead. The only exceptions MUST note the changes in
570 * the 'DynamicStatic' struct.
574 FILE *err; /* the stderr user data goes here */
575 void *debugdata; /* the data that will be passed to fdebug */
576 char *errorbuffer; /* store failure messages in here */
577 char *proxyuserpwd; /* Proxy <user:password>, if used */
578 long proxyport; /* If non-zero, use this port number by default. If the
579 proxy string features a ":[port]" that one will override
581 void *out; /* the fetched file goes here */
582 void *in; /* the uploaded file is read from here */
583 void *writeheader; /* write the header to this is non-NULL */
584 char *set_url; /* what original URL to work on */
585 char *set_proxy; /* proxy to use */
586 long use_port; /* which port to use (when not using default) */
587 char *userpwd; /* <user:password>, if used */
588 char *set_range; /* range, if used. See README for detailed specification
590 long followlocation; /* as in HTTP Location: */
591 long maxredirs; /* maximum no. of http(s) redirects to follow */
592 char *set_referer; /* custom string */
593 bool free_referer; /* set TRUE if 'referer' points to a string we
595 char *useragent; /* User-Agent string */
596 char *postfields; /* if POST, set the fields' values here */
597 size_t postfieldsize; /* if POST, this might have a size to use instead of
598 strlen(), and then the data *may* be binary (contain
600 char *ftpport; /* port to send with the FTP PORT command */
601 char *device; /* network interface to use */
602 curl_write_callback fwrite; /* function that stores the output */
603 curl_write_callback fwrite_header; /* function that stores headers */
604 curl_read_callback fread; /* function that reads the input */
605 curl_progress_callback fprogress; /* function for progress information */
606 curl_debug_callback fdebug; /* function that write informational data */
607 void *progress_client; /* pointer to pass to the progress callback */
608 curl_passwd_callback fpasswd; /* call for password */
609 void *passwd_client; /* pass to the passwd callback */
610 long timeout; /* in seconds, 0 means no timeout */
611 long connecttimeout; /* in seconds, 0 means no timeout */
612 long infilesize; /* size of file to upload, -1 means unknown */
613 long low_speed_limit; /* bytes/second */
614 long low_speed_time; /* number of seconds */
615 int set_resume_from; /* continue [ftp] transfer from here */
616 char *cookie; /* HTTP cookie string to send */
617 struct curl_slist *headers; /* linked list of extra headers */
618 struct HttpPost *httppost; /* linked list of POST data */
619 char *cert; /* certificate */
620 char *cert_type; /* format for certificate (default: PEM) */
621 char *key; /* private key */
622 char *key_type; /* format for private key (default: PEM) */
623 char *key_passwd; /* plain text private key password */
624 char *crypto_engine; /* name of the crypto engine to use */
625 char *cookiejar; /* dump all cookies to this file */
626 bool cookiesession; /* new cookie session? */
627 bool crlf; /* convert crlf on ftp upload(?) */
628 struct curl_slist *quote; /* after connection is established */
629 struct curl_slist *postquote; /* after the transfer */
630 struct curl_slist *prequote; /* before the transfer, after type (Wesley Laxton)*/
631 struct curl_slist *telnet_options; /* linked list of telnet options */
632 curl_TimeCond timecondition; /* kind of time/date comparison */
633 time_t timevalue; /* what time to compare with */
634 curl_closepolicy closepolicy; /* connection cache close concept */
635 Curl_HttpReq httpreq; /* what kind of HTTP request (if any) is this */
636 char *customrequest; /* HTTP/FTP request to use */
637 long httpversion; /* when non-zero, a specific HTTP version requested to
638 be used in the library's request(s) */
639 char *auth_host; /* if set, this is the allocated string to the host name
640 * to which to send the authorization data to, and no other
641 * host (which location-following otherwise could lead to)
643 char *krb4_level; /* what security level */
644 struct ssl_config_data ssl; /* user defined SSL stuff */
646 curl_proxytype proxytype; /* what kind of proxy that is in use */
648 int dns_cache_timeout; /* DNS cache timeout */
649 long buffer_size; /* size of receive buffer to use */
651 /* Here follows boolean settings that define how to behave during
652 this session. They are STATIC, set by libcurl users or at least initially
653 and they don't change during operations. */
656 bool tunnel_thru_httpproxy;
662 bool http_fail_on_error;
663 bool http_follow_location;
665 #define http_include_header include_header /* former name */
667 bool http_set_referer;
668 bool http_auto_referer; /* set "correct" referer when following location: */
672 enum CURL_NETRC_OPTION
673 use_netrc; /* defined in include/curl.h */
675 bool krb4; /* kerberos4 connection requested */
676 bool reuse_forbid; /* forbidden to be reused, close after use */
677 bool reuse_fresh; /* do not re-use an existing connection */
678 bool expect100header; /* TRUE if we added Expect: 100-continue */
679 bool ftp_use_epsv; /* if EPSV is to be attempted or not */
680 bool no_signal; /* do not use any signal/alarm handler */
682 bool global_dns_cache;
686 * In August 2001, this struct was redesigned and is since stricter than
687 * before. The 'connectdata' struct MUST have all the connection oriented
688 * stuff as we may now have several simultaneous connections and connection
691 * From now on, the 'SessionHandle' must only contain data that is set once to
692 * go for many (perhaps) independent connections. Values that are generated or
693 * calculated internally for the "session handle" must be defined within the
694 * 'struct urlstate' instead. */
696 struct SessionHandle {
697 curl_hash *hostcache;
698 curl_share *share; /* Share, handles global variable mutexing */
699 struct UserDefined set; /* values set by the libcurl user */
700 struct DynamicStatic change; /* possibly modified userdefined data */
702 struct CookieInfo *cookies; /* the cookies, read from files and servers */
703 struct Progress progress; /* for all the progress meter data */
704 struct UrlState state; /* struct for fields used for state info and
705 other dynamic purposes */
706 struct PureInfo info; /* stats, reports and info data */
709 #endif /* USE_SSLEAY */
712 #define LIBCURL_NAME "libcurl"