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 verif, 1: check that CN exists, 2: CN must match hostname */
140 char *CApath; /* DOES NOT WORK ON WINDOWS */
141 char *CAfile; /* cerficate to verify peer against */
142 char *random_file; /* path to file containing "random" data */
143 char *egdsocket; /* path to file containing the EGD daemon socket */
144 char *cipher_list; /* list of ciphers to use */
146 long numsessions; /* SSL session id cache size */
149 /****************************************************************************
151 ***************************************************************************/
153 struct FormData *sendit;
155 const char *p_pragma; /* Pragma: string */
156 const char *p_accept; /* Accept: string */
160 /* For FORM posting */
162 curl_read_callback storefread;
165 struct Curl_chunker chunk;
168 /****************************************************************************
170 ***************************************************************************/
173 char *user; /* user name string */
174 char *passwd; /* password string */
175 char *urlpath; /* the originally given path part of the URL */
176 char *dir; /* decoded directory */
177 char *file; /* decoded file */
179 char *entrypath; /* the PWD reply when we logged on */
181 char *cache; /* data cache between getresponse()-calls */
182 size_t cache_size; /* size of cache in bytes */
185 /****************************************************************************
187 ***************************************************************************/
189 int fd; /* open file descriptor to read from! */
193 * Boolean values that concerns this connection.
196 bool close; /* if set, we close the connection after this request */
197 bool reuse; /* if set, this is a re-used connection */
198 bool chunk; /* if set, this is a chunked transfer-encoding */
199 bool httpproxy; /* if set, this transfer is done through a http proxy */
200 bool user_passwd; /* do we use user+password for this connection? */
201 bool proxy_user_passwd; /* user+password for the proxy? */
204 bool rangestringalloc; /* the range string is malloc()'ed */
206 bool resume_done; /* nothing was transfered, resumed transfer already
211 * This struct is all the previously local variables from Curl_perform() moved
212 * to struct to allow the function to return and get re-invoked better without
216 struct Curl_transfer_keeper {
217 int bytecount; /* total number of bytes read */
218 int writebytecount; /* number of bytes written */
219 long contentlength; /* size of incoming data */
220 struct timeval start; /* transfer started at this time */
221 struct timeval now; /* current time */
222 bool header; /* incoming data has HTTP header */
223 int headerline; /* counts header lines to better track the
225 char *hbufp; /* points at *end* of header line */
227 char *str; /* within buf */
228 char *str_start; /* within buf */
229 char *end_ptr; /* within buf */
230 char *p; /* within headerbuff */
231 bool content_range; /* set TRUE if Content-Range: was found */
232 int offset; /* possible resume offset read from the
233 Content-Range: header */
234 int httpcode; /* error code from the 'HTTP/1.? XXX' line */
235 int httpversion; /* the HTTP version*10 */
236 bool write_after_100_header; /* should we enable the write after
237 we received a 100-continue/timeout
240 /* for the low speed checks: */
245 /* the highest fd we use + 1 */
246 struct SessionHandle *data;
247 struct connectdata *conn;
252 /* pointers to the actual descriptors we check */
256 /* the file descriptors to play with */
267 * The connectdata struct contains all fields and variables that should be
268 * unique for an entire connection.
271 /**** Fields set when inited and not modified again */
272 struct SessionHandle *data; /* link to the root CURL struct */
273 int connectindex; /* what index in the connects index this particular
276 long protocol; /* PROT_* flags concerning the protocol set */
277 #define PROT_MISSING (1<<0)
278 #define PROT_GOPHER (1<<1)
279 #define PROT_HTTP (1<<2)
280 #define PROT_HTTPS (1<<3)
281 #define PROT_FTP (1<<4)
282 #define PROT_TELNET (1<<5)
283 #define PROT_DICT (1<<6)
284 #define PROT_LDAP (1<<7)
285 #define PROT_FILE (1<<8)
286 #define PROT_FTPS (1<<9)
287 #define PROT_SSL (1<<10) /* protocol requires SSL */
290 struct addrinfo *serv_addr; /* the particular host we use */
292 struct sockaddr_in serv_addr;
294 char protostr[64]; /* store the protocol string in this buffer */
295 char gname[513]; /* store the hostname in this buffer */
296 char *name; /* host name pointer to fool around with */
297 char *path; /* allocated buffer to store the URL's path part in */
298 char *hostname; /* hostname to connect, as parsed from url */
299 long port; /* which port to use locally */
300 unsigned short remote_port; /* what remote port to connect to,
301 not the proxy port! */
304 long headerbytecount; /* only count received headers */
306 char *range; /* range, if used. See README for detailed specification on
308 ssize_t resume_from; /* continue [ftp] transfer from here */
310 char *proxyhost; /* name of the http proxy host */
312 struct timeval now; /* "current" time */
313 struct timeval created; /* creation time */
314 int firstsocket; /* the main socket to use */
315 int secondarysocket; /* for i.e ftp transfers */
316 long maxdownload; /* in bytes, the maximum amount of data to fetch, 0
319 struct ssl_connect_data ssl; /* this is for ssl-stuff */
321 struct ConnectBits bits; /* various state-flags for this connection */
323 /* These two functions MUST be set by the curl_connect() function to be
324 be protocol dependent */
325 CURLcode (*curl_do)(struct connectdata *connect);
326 CURLcode (*curl_done)(struct connectdata *connect);
328 /* This function *MAY* be set to a protocol-dependent function that is run
329 * after the connect() and everything is done, as a step in the connection.
331 CURLcode (*curl_connect)(struct connectdata *connect);
333 /* This function *MAY* be set to a protocol-dependent function that is run
334 * by the curl_disconnect(), as a step in the disconnection.
336 CURLcode (*curl_disconnect)(struct connectdata *connect);
338 /* This function *MAY* be set to a protocol-dependent function that is run
339 * in the curl_close() function if protocol-specific cleanups are required.
341 CURLcode (*curl_close)(struct connectdata *connect);
343 /**** curl_get() phase fields */
346 int sockfd; /* socket to read from or -1 */
347 int size; /* -1 if unknown at this point */
348 bool getheader; /* TRUE if header parsing is wanted */
349 long *bytecountp; /* return number of bytes read or NULL */
352 int writesockfd; /* socket to write to, it may very well be
353 the same we read from. -1 disables */
354 long *writebytecountp; /* return number of bytes written or NULL */
356 /** Dynamicly allocated strings, may need to be freed before this **/
357 /** struct is killed. **/
358 struct dynamically_allocated_data {
359 char *proxyuserpwd; /* free later if not NULL! */
360 char *uagent; /* free later if not NULL! */
361 char *userpwd; /* free later if not NULL! */
362 char *rangeline; /* free later if not NULL! */
363 char *ref; /* free later if not NULL! */
364 char *cookie; /* free later if not NULL! */
365 char *host; /* free later if not NULL */
368 char *newurl; /* This can only be set if a Location: was in the
372 enum protection_level command_prot;
373 enum protection_level data_prot;
374 enum protection_level request_data_prot;
378 struct krb4buffer in_buffer, out_buffer;
382 struct Curl_sec_client_mech *mech;
383 struct sockaddr_in local_addr;
387 /*************** Request - specific items ************/
388 /* previously this was in the urldata struct */
391 struct HTTP *gopher; /* alias, just for the sake of being more readable */
392 struct HTTP *https; /* alias, just for the sake of being more readable */
395 void *telnet; /* private for telnet.c-eyes only */
396 #if 0 /* no need for special ones for these: */
403 /* This struct is inited when needed */
404 struct Curl_transfer_keeper keep;
406 /* 'upload_present' is used to keep a byte counter of how much data there is
407 still left in the buffer, aimed for upload. */
410 /* 'upload_fromhere' is used as a read-pointer when we uploaded parts of a
411 buffer, so the next read should read from where this pointer points to,
412 and the 'upload_present' contains the number of bytes available at this
414 char *upload_fromhere;
418 * Struct to keep statistical and informational data.
423 long filetime; /* If requested, this is might get set. Set to -1 if
424 the time was unretrievable */
425 long header_size; /* size of read header(s) in bytes */
426 long request_size; /* the amount of bytes sent in the request(s) */
428 char *contenttype; /* the content type of the object */
433 long lastshow; /* time() of the last displayed progress meter or NULL to
434 force redraw at next call */
440 double current_speed; /* uses the currently fastest transfer */
442 bool callback; /* set when progress callback is used */
443 int width; /* screen width at download start */
444 int flags; /* see progress.h */
453 double t_pretransfer;
454 double t_starttransfer;
457 struct timeval start;
458 struct timeval t_startsingle;
459 #define CURR_TIME (5+1) /* 6 entries for 5 seconds */
461 double speeder[ CURR_TIME ];
462 struct timeval speeder_time[ CURR_TIME ];
467 HTTPREQ_NONE, /* first in list */
470 HTTPREQ_POST_FORM, /* we make a difference internally */
473 HTTPREQ_LAST /* last in list */
477 * Values that are generated, temporary or calculated internally for a
478 * "session handle" must be defined within the 'struct urlstate'. This struct
479 * will be used within the SessionHandle struct. When the 'SessionHandle'
480 * struct is cloned, this data MUST NOT be copied.
482 * Remember that any "state" information goes globally for the curl handle.
483 * Session-data MUST be put in the connectdata struct and here. */
484 #define MAX_CURL_USER_LENGTH 256
485 #define MAX_CURL_PASSWORD_LENGTH 256
488 /* buffers to store authentication data in, as parsed from input options */
489 char user[MAX_CURL_USER_LENGTH];
490 char passwd[MAX_CURL_PASSWORD_LENGTH];
491 char proxyuser[MAX_CURL_USER_LENGTH];
492 char proxypasswd[MAX_CURL_PASSWORD_LENGTH];
494 struct timeval keeps_speed; /* for the progress meter really */
496 /* 'connects' will be an allocated array with pointers. If the pointer is
497 set, it holds an allocated connection. */
498 struct connectdata **connects;
499 long numconnects; /* size of the 'connects' array */
501 char *headerbuff; /* allocated buffer to store headers in */
502 int headersize; /* size of the allocation */
504 char buffer[BUFSIZE+1]; /* download buffer */
505 char uploadbuffer[BUFSIZE+1]; /* upload buffer */
506 double current_speed; /* the ProgressShow() funcion sets this */
508 bool this_is_a_follow; /* this is a followed Location: request */
510 char *auth_host; /* if set, this should be the host name that we will
511 sent authorization to, no else. Used to make Location:
512 following not keep sending user+password... This is
516 struct curl_ssl_session *session; /* array of 'numsessions' size */
517 long sessionage; /* number of the most recent session */
519 char scratch[BUFSIZE*2]; /* huge buffer when doing upload CRLF replacing */
520 bool errorbuf; /* Set to TRUE if the error buffer is already filled in.
521 This must be set to FALSE every time _easy_perform() is
525 /* storage for the previous bag^H^H^HSIGPIPE signal handler :-) */
526 void (*prev_signal)(int sig);
528 bool allow_port; /* Is set.use_port allowed to take effect or not. This
529 is always set TRUE when curl_easy_perform() is called. */
534 * This 'DynamicStatic' struct defines dynamic states that actually change
535 * values in the 'UserDefined' area, which MUST be taken into consideration
536 * if the UserDefined struct is cloned or similar. You can probably just
537 * copy these, but each one indicate a special action on other data.
540 struct DynamicStatic {
541 char *url; /* work URL, copied from UserDefined */
542 bool url_alloc; /* URL string is malloc()'ed */
543 char *proxy; /* work proxy, copied from UserDefined */
544 bool proxy_alloc; /* http proxy string is malloc()'ed */
545 char *referer; /* referer string */
546 bool referer_alloc; /* referer sting is malloc()ed */
550 * This 'UserDefined' struct must only contain data that is set once to go
551 * for many (perhaps) independent connections. Values that are generated or
552 * calculated internally for the "session handle" MUST be defined within the
553 * 'struct urlstate' instead. The only exceptions MUST note the changes in
554 * the 'DynamicStatic' struct.
558 FILE *err; /* the stderr user data goes here */
559 void *debugdata; /* the data that will be passed to fdebug */
560 char *errorbuffer; /* store failure messages in here */
561 char *proxyuserpwd; /* Proxy <user:password>, if used */
562 long proxyport; /* If non-zero, use this port number by default. If the
563 proxy string features a ":[port]" that one will override
565 void *out; /* the fetched file goes here */
566 void *in; /* the uploaded file is read from here */
567 void *writeheader; /* write the header to this is non-NULL */
568 char *set_url; /* what original URL to work on */
569 char *set_proxy; /* proxy to use */
570 long use_port; /* which port to use (when not using default) */
571 char *userpwd; /* <user:password>, if used */
572 char *set_range; /* range, if used. See README for detailed specification
574 long followlocation; /* as in HTTP Location: */
575 long maxredirs; /* maximum no. of http(s) redirects to follow */
576 char *set_referer; /* custom string */
577 bool free_referer; /* set TRUE if 'referer' points to a string we
579 char *useragent; /* User-Agent string */
580 char *postfields; /* if POST, set the fields' values here */
581 size_t postfieldsize; /* if POST, this might have a size to use instead of
582 strlen(), and then the data *may* be binary (contain
584 char *ftpport; /* port to send with the FTP PORT command */
585 char *device; /* network interface to use */
586 curl_write_callback fwrite; /* function that stores the output */
587 curl_write_callback fwrite_header; /* function that stores headers */
588 curl_read_callback fread; /* function that reads the input */
589 curl_progress_callback fprogress; /* function for progress information */
590 curl_debug_callback fdebug; /* function that write informational data */
591 void *progress_client; /* pointer to pass to the progress callback */
592 curl_passwd_callback fpasswd; /* call for password */
593 void *passwd_client; /* pass to the passwd callback */
594 long timeout; /* in seconds, 0 means no timeout */
595 long connecttimeout; /* in seconds, 0 means no timeout */
596 long infilesize; /* size of file to upload, -1 means unknown */
597 long low_speed_limit; /* bytes/second */
598 long low_speed_time; /* number of seconds */
599 int set_resume_from; /* continue [ftp] transfer from here */
600 char *cookie; /* HTTP cookie string to send */
601 struct curl_slist *headers; /* linked list of extra headers */
602 struct HttpPost *httppost; /* linked list of POST data */
603 char *cert; /* certificate */
604 char *cert_type; /* format for certificate (default: PEM) */
605 char *key; /* private key */
606 char *key_type; /* format for private key (default: PEM) */
607 char *key_passwd; /* plain text private key password */
608 char *crypto_engine; /* name of the crypto engine to use */
609 char *cookiejar; /* dump all cookies to this file */
610 bool cookiesession; /* new cookie session? */
611 bool crlf; /* convert crlf on ftp upload(?) */
612 struct curl_slist *quote; /* after connection is established */
613 struct curl_slist *postquote; /* after the transfer */
614 struct curl_slist *prequote; /* before the transfer, after type (Wesley Laxton)*/
615 struct curl_slist *telnet_options; /* linked list of telnet options */
616 curl_TimeCond timecondition; /* kind of time/date comparison */
617 time_t timevalue; /* what time to compare with */
618 curl_closepolicy closepolicy; /* connection cache close concept */
619 Curl_HttpReq httpreq; /* what kind of HTTP request (if any) is this */
620 char *customrequest; /* HTTP/FTP request to use */
621 long httpversion; /* when non-zero, a specific HTTP version requested to
622 be used in the library's request(s) */
623 char *auth_host; /* if set, this is the allocated string to the host name
624 * to which to send the authorization data to, and no other
625 * host (which location-following otherwise could lead to)
627 char *krb4_level; /* what security level */
628 struct ssl_config_data ssl; /* user defined SSL stuff */
630 int dns_cache_timeout; /* DNS cache timeout */
632 /* Here follows boolean settings that define how to behave during
633 this session. They are STATIC, set by libcurl users or at least initially
634 and they don't change during operations. */
637 bool tunnel_thru_httpproxy;
643 bool http_fail_on_error;
644 bool http_follow_location;
646 #define http_include_header include_header /* former name */
648 bool http_set_referer;
649 bool http_auto_referer; /* set "correct" referer when following location: */
655 bool krb4; /* kerberos4 connection requested */
656 bool reuse_forbid; /* forbidden to be reused, close after use */
657 bool reuse_fresh; /* do not re-use an existing connection */
658 bool expect100header; /* TRUE if we added Expect: 100-continue */
659 bool ftp_use_epsv; /* if EPSV is to be attempted or not */
661 bool global_dns_cache;
665 * In August 2001, this struct was redesigned and is since stricter than
666 * before. The 'connectdata' struct MUST have all the connection oriented
667 * stuff as we may now have several simultaneous connections and connection
670 * From now on, the 'SessionHandle' must only contain data that is set once to
671 * go for many (perhaps) independent connections. Values that are generated or
672 * calculated internally for the "session handle" must be defined within the
673 * 'struct urlstate' instead. */
675 struct SessionHandle {
676 curl_hash *hostcache;
677 struct UserDefined set; /* values set by the libcurl user */
678 struct DynamicStatic change; /* possibly modified userdefined data */
680 struct CookieInfo *cookies; /* the cookies, read from files and servers */
681 struct Progress progress; /* for all the progress meter data */
682 struct UrlState state; /* struct for fields used for state info and
683 other dynamic purposes */
684 struct PureInfo info; /* stats, reports and info data */
687 #endif /* USE_SSLEAY */
690 #define LIBCURL_NAME "libcurl"