3 /*****************************************************************************
5 * Project ___| | | | _ \| |
7 * | (__| |_| | _ <| |___
8 * \___|\___/|_| \_\_____|
10 * Copyright (C) 2001, 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 */
32 #define PORT_TELNET 23
33 #define PORT_GOPHER 70
35 #define PORT_HTTPS 443
36 #define PORT_DICT 2628
39 #define DICT_MATCH "/MATCH:"
40 #define DICT_MATCH2 "/M:"
41 #define DICT_MATCH3 "/FIND:"
42 #define DICT_DEFINE "/DEFINE:"
43 #define DICT_DEFINE2 "/D:"
44 #define DICT_DEFINE3 "/LOOKUP:"
46 #define CURL_DEFAULT_USER "anonymous"
47 #define CURL_DEFAULT_PASSWORD "curl_by_daniel@haxx.se"
53 /* SSLeay stuff usually in /usr/local/ssl/include */
55 #include "openssl/rsa.h"
56 #include "openssl/crypto.h"
57 #include "openssl/x509.h"
58 #include "openssl/pem.h"
59 #include "openssl/ssl.h"
60 #include "openssl/err.h"
71 #ifdef HAVE_NETINET_IN_H
72 #include <netinet/in.h>
77 #include <curl/curl.h>
79 #include "http_chunks.h" /* for the structs and enum stuff */
81 /* Download buffer size, keep it fairly big for speed reasons */
82 #define BUFSIZE (1024*50)
84 /* Defaul upload buffer size, keep it smallish to get faster progress meter
85 updates. This is just default, it is dynamic and adjusts to the upload
87 #define UPLOAD_BUFSIZE (1024*2)
89 /* Initial size of the buffer to store headers in, it'll be enlarged in case
91 #define HEADERSIZE 256
93 /* Just a convenience macro to get the larger value out of two given */
95 #define MAX(x,y) ((x)>(y)?(x):(y))
99 /* Types needed for krb4-ftp connections */
106 enum protection_level {
114 /* struct for data related to SSL and SSL connections */
115 struct ssl_connect_data {
116 bool use; /* use ssl encrypted communications TRUE/FALSE */
118 /* these ones requires specific SSL-types */
122 #endif /* USE_SSLEAY */
125 /* information about one single SSL session */
126 struct curl_ssl_session {
127 char *name; /* host name for which this ID was used */
128 void *sessionid; /* as returned from the SSL layer */
129 long age; /* just a number, the higher the more recent */
130 unsigned short remote_port; /* remote port to connect to */
133 struct ssl_config_data {
134 long version; /* what version the client wants to use */
135 long certverifyresult; /* result from the certificate verification */
136 long verifypeer; /* set TRUE if this is desired */
137 long verifyhost; /* 0: no verif, 1: check that CN exists, 2: CN must match hostname */
138 char *CApath; /* DOES NOT WORK ON WINDOWS */
139 char *CAfile; /* cerficate to verify peer against */
140 char *random_file; /* path to file containing "random" data */
141 char *egdsocket; /* path to file containing the EGD daemon socket */
142 char *cipher_list; /* list of ciphers to use */
144 long numsessions; /* SSL session id cache size */
147 /****************************************************************************
149 ***************************************************************************/
151 struct FormData *sendit;
153 const char *p_pragma; /* Pragma: string */
154 const char *p_accept; /* Accept: string */
158 /* For FORM posting */
160 curl_read_callback storefread;
163 struct Curl_chunker chunk;
166 /****************************************************************************
168 ***************************************************************************/
171 char *user; /* user name string */
172 char *passwd; /* password string */
173 char *urlpath; /* the originally given path part of the URL */
174 char *dir; /* decoded directory */
175 char *file; /* decoded file */
177 char *entrypath; /* the PWD reply when we logged on */
179 char *cache; /* data cache between getresponse()-calls */
180 size_t cache_size; /* size of cache in bytes */
183 /****************************************************************************
185 ***************************************************************************/
187 int fd; /* open file descriptor to read from! */
191 * Boolean values that concerns this connection.
194 bool close; /* if set, we close the connection after this request */
195 bool reuse; /* if set, this is a re-used connection */
196 bool chunk; /* if set, this is a chunked transfer-encoding */
197 bool httpproxy; /* if set, this transfer is done through a http proxy */
198 bool user_passwd; /* do we use user+password for this connection? */
199 bool proxy_user_passwd; /* user+password for the proxy? */
202 bool rangestringalloc; /* the range string is malloc()'ed */
204 bool resume_done; /* nothing was transfered, resumed transfer already
209 * The connectdata struct contains all fields and variables that should be
210 * unique for an entire connection.
213 /**** Fields set when inited and not modified again */
214 struct SessionHandle *data; /* link to the root CURL struct */
215 int connectindex; /* what index in the connects index this particular
218 long protocol; /* PROT_* flags concerning the protocol set */
219 #define PROT_MISSING (1<<0)
220 #define PROT_GOPHER (1<<1)
221 #define PROT_HTTP (1<<2)
222 #define PROT_HTTPS (1<<3)
223 #define PROT_FTP (1<<4)
224 #define PROT_TELNET (1<<5)
225 #define PROT_DICT (1<<6)
226 #define PROT_LDAP (1<<7)
227 #define PROT_FILE (1<<8)
228 #define PROT_FTPS (1<<9)
229 #define PROT_SSL (1<<10) /* protocol requires SSL */
231 Curl_addrinfo *hostaddr; /* IP-protocol independent host info pointer list */
232 char *hostent_buf; /* pointer to allocated memory for name info */
235 struct addrinfo *serv_addr; /* the particular host we use */
237 struct sockaddr_in serv_addr;
239 char protostr[64]; /* store the protocol string in this buffer */
240 char gname[513]; /* store the hostname in this buffer */
241 char *name; /* host name pointer to fool around with */
242 char *path; /* allocated buffer to store the URL's path part in */
243 char *hostname; /* hostname to connect, as parsed from url */
244 long port; /* which port to use locally */
245 unsigned short remote_port; /* what remote port to connect to,
246 not the proxy port! */
249 long headerbytecount; /* only count received headers */
251 char *range; /* range, if used. See README for detailed specification on
253 ssize_t resume_from; /* continue [ftp] transfer from here */
255 char *proxyhost; /* name of the http proxy host */
257 struct timeval now; /* "current" time */
258 struct timeval created; /* creation time */
259 int firstsocket; /* the main socket to use */
260 int secondarysocket; /* for i.e ftp transfers */
262 long upload_bufsize; /* adjust as you see fit, never bigger than BUFSIZE
263 never smaller than UPLOAD_BUFSIZE */
265 long maxdownload; /* in bytes, the maximum amount of data to fetch, 0
268 struct ssl_connect_data ssl; /* this is for ssl-stuff */
270 struct ConnectBits bits; /* various state-flags for this connection */
272 /* These two functions MUST be set by the curl_connect() function to be
273 be protocol dependent */
274 CURLcode (*curl_do)(struct connectdata *connect);
275 CURLcode (*curl_done)(struct connectdata *connect);
277 /* This function *MAY* be set to a protocol-dependent function that is run
278 * after the connect() and everything is done, as a step in the connection.
280 CURLcode (*curl_connect)(struct connectdata *connect);
282 /* This function *MAY* be set to a protocol-dependent function that is run
283 * by the curl_disconnect(), as a step in the disconnection.
285 CURLcode (*curl_disconnect)(struct connectdata *connect);
287 /* This function *MAY* be set to a protocol-dependent function that is run
288 * in the curl_close() function if protocol-specific cleanups are required.
290 CURLcode (*curl_close)(struct connectdata *connect);
292 /**** curl_get() phase fields */
295 int sockfd; /* socket to read from or -1 */
296 int size; /* -1 if unknown at this point */
297 bool getheader; /* TRUE if header parsing is wanted */
298 long *bytecountp; /* return number of bytes read or NULL */
301 int writesockfd; /* socket to write to, it may very well be
302 the same we read from. -1 disables */
303 long *writebytecountp; /* return number of bytes written or NULL */
305 /** Dynamicly allocated strings, may need to be freed before this **/
306 /** struct is killed. **/
307 struct dynamically_allocated_data {
308 char *proxyuserpwd; /* free later if not NULL! */
309 char *uagent; /* free later if not NULL! */
310 char *userpwd; /* free later if not NULL! */
311 char *rangeline; /* free later if not NULL! */
312 char *ref; /* free later if not NULL! */
313 char *cookie; /* free later if not NULL! */
314 char *host; /* free later if not NULL */
317 char *newurl; /* This can only be set if a Location: was in the
321 enum protection_level command_prot;
322 enum protection_level data_prot;
323 enum protection_level request_data_prot;
327 struct krb4buffer in_buffer, out_buffer;
331 struct Curl_sec_client_mech *mech;
332 struct sockaddr_in local_addr;
336 /*************** Request - specific items ************/
337 /* previously this was in the urldata struct */
340 struct HTTP *gopher; /* alias, just for the sake of being more readable */
341 struct HTTP *https; /* alias, just for the sake of being more readable */
344 void *telnet; /* private for telnet.c-eyes only */
345 #if 0 /* no need for special ones for these: */
355 * Struct to keep statistical and informational data.
360 long filetime; /* If requested, this is might get set. Set to -1 if
361 the time was unretrievable */
362 long header_size; /* size of read header(s) in bytes */
363 long request_size; /* the amount of bytes sent in the request(s) */
368 long lastshow; /* time() of the last displayed progress meter or NULL to
369 force redraw at next call */
375 double current_speed; /* uses the currently fastest transfer */
377 bool callback; /* set when progress callback is used */
378 int width; /* screen width at download start */
379 int flags; /* see progress.h */
388 double t_pretransfer;
389 double t_starttransfer;
391 struct timeval start;
392 struct timeval t_startsingle;
393 #define CURR_TIME (5+1) /* 6 entries for 5 seconds */
395 double speeder[ CURR_TIME ];
396 struct timeval speeder_time[ CURR_TIME ];
401 HTTPREQ_NONE, /* first in list */
404 HTTPREQ_POST_FORM, /* we make a difference internally */
407 HTTPREQ_LAST /* last in list */
411 * Values that are generated, temporary or calculated internally for a
412 * "session handle" must be defined within the 'struct urlstate'. This struct
413 * will be used within the SessionHandle struct. When the 'SessionHandle'
414 * struct is cloned, this data MUST NOT be copied.
416 * Remember that any "state" information goes globally for the curl handle.
417 * Session-data MUST be put in the connectdata struct and here. */
418 #define MAX_CURL_USER_LENGTH 256
419 #define MAX_CURL_PASSWORD_LENGTH 256
422 /* buffers to store authentication data in, as parsed from input options */
423 char user[MAX_CURL_USER_LENGTH];
424 char passwd[MAX_CURL_PASSWORD_LENGTH];
425 char proxyuser[MAX_CURL_USER_LENGTH];
426 char proxypasswd[MAX_CURL_PASSWORD_LENGTH];
428 struct timeval keeps_speed; /* for the progress meter really */
430 /* 'connects' will be an allocated array with pointers. If the pointer is
431 set, it holds an allocated connection. */
432 struct connectdata **connects;
433 long numconnects; /* size of the 'connects' array */
435 char *headerbuff; /* allocated buffer to store headers in */
436 int headersize; /* size of the allocation */
438 char buffer[BUFSIZE+1]; /* buffer with size BUFSIZE */
440 double current_speed; /* the ProgressShow() funcion sets this */
442 bool this_is_a_follow; /* this is a followed Location: request */
444 char *auth_host; /* if set, this should be the host name that we will
445 sent authorization to, no else. Used to make Location:
446 following not keep sending user+password... This is
450 struct curl_ssl_session *session; /* array of 'numsessions' size */
451 long sessionage; /* number of the most recent session */
453 char scratch[BUFSIZE*2]; /* huge buffer when doing upload CRLF replacing */
454 bool errorbuf; /* Set to TRUE if the error buffer is already filled in.
455 This must be set to FALSE every time _easy_perform() is
461 * This 'DynamicStatic' struct defines dynamic states that actually change
462 * values in the 'UserDefined' area, which MUST be taken into consideration
463 * if the UserDefined struct is cloned or similar. You can probably just
464 * copy these, but each one indicate a special action on other data.
467 struct DynamicStatic {
468 char *url; /* work URL, copied from UserDefined */
469 bool url_alloc; /* URL string is malloc()'ed */
470 char *proxy; /* work proxy, copied from UserDefined */
471 bool proxy_alloc; /* http proxy string is malloc()'ed */
472 char *referer; /* referer string */
473 bool referer_alloc; /* referer sting is malloc()ed */
477 * This 'UserDefined' struct must only contain data that is set once to go
478 * for many (perhaps) independent connections. Values that are generated or
479 * calculated internally for the "session handle" MUST be defined within the
480 * 'struct urlstate' instead. The only exceptions MUST note the changes in
481 * the 'DynamicStatic' struct.
485 FILE *err; /* the stderr writes goes here */
486 char *errorbuffer; /* store failure messages in here */
487 char *proxyuserpwd; /* Proxy <user:password>, if used */
488 long proxyport; /* If non-zero, use this port number by default. If the
489 proxy string features a ":[port]" that one will override
491 void *out; /* the fetched file goes here */
492 void *in; /* the uploaded file is read from here */
493 void *writeheader; /* write the header to this is non-NULL */
494 char *set_url; /* what original URL to work on */
495 char *set_proxy; /* proxy to use */
496 long use_port; /* which port to use (when not using default) */
497 char *userpwd; /* <user:password>, if used */
498 char *set_range; /* range, if used. See README for detailed specification
500 long followlocation; /* as in HTTP Location: */
501 long maxredirs; /* maximum no. of http(s) redirects to follow */
502 char *set_referer; /* custom string */
503 bool free_referer; /* set TRUE if 'referer' points to a string we
505 char *useragent; /* User-Agent string */
506 char *postfields; /* if POST, set the fields' values here */
507 size_t postfieldsize; /* if POST, this might have a size to use instead of
508 strlen(), and then the data *may* be binary (contain
510 char *ftpport; /* port to send with the FTP PORT command */
511 char *device; /* network interface to use */
512 curl_write_callback fwrite; /* function that stores the output */
513 curl_write_callback fwrite_header; /* function that stores headers */
514 curl_read_callback fread; /* function that reads the input */
515 curl_progress_callback fprogress; /* function for progress information */
516 void *progress_client; /* pointer to pass to the progress callback */
517 curl_passwd_callback fpasswd; /* call for password */
518 void *passwd_client; /* pass to the passwd callback */
519 long timeout; /* in seconds, 0 means no timeout */
520 long connecttimeout; /* in seconds, 0 means no timeout */
521 long infilesize; /* size of file to upload, -1 means unknown */
522 long low_speed_limit; /* bytes/second */
523 long low_speed_time; /* number of seconds */
524 int set_resume_from; /* continue [ftp] transfer from here */
525 char *cookie; /* HTTP cookie string to send */
526 struct curl_slist *headers; /* linked list of extra headers */
527 struct HttpPost *httppost; /* linked list of POST data */
528 char *cert; /* PEM-formatted certificate */
529 char *cert_passwd; /* plain text certificate password */
530 char *cookiejar; /* dump all cookies to this file */
531 bool crlf; /* convert crlf on ftp upload(?) */
532 struct curl_slist *quote; /* before the transfer */
533 struct curl_slist *postquote; /* after the transfer */
534 struct curl_slist *telnet_options; /* linked list of telnet options */
535 curl_TimeCond timecondition; /* kind of time/date comparison */
536 time_t timevalue; /* what time to compare with */
537 curl_closepolicy closepolicy; /* connection cache close concept */
538 Curl_HttpReq httpreq; /* what kind of HTTP request (if any) is this */
539 char *customrequest; /* HTTP/FTP request to use */
540 long httpversion; /* when non-zero, a specific HTTP version requested to
541 be used in the library's request(s) */
542 char *auth_host; /* if set, this is the allocated string to the host name
543 * to which to send the authorization data to, and no other
544 * host (which location-following otherwise could lead to)
546 char *krb4_level; /* what security level */
547 struct ssl_config_data ssl; /* user defined SSL stuff */
549 /* Here follows boolean settings that define how to behave during
550 this session. They are STATIC, set by libcurl users or at least initially
551 and they don't change during operations. */
554 bool tunnel_thru_httpproxy;
560 bool http_fail_on_error;
561 bool http_follow_location;
562 bool http_include_header;
563 bool http_set_referer;
564 bool http_auto_referer; /* set "correct" referer when following location: */
570 bool krb4; /* kerberos4 connection requested */
571 bool reuse_forbid; /* forbidden to be reused, close after use */
572 bool reuse_fresh; /* do not re-use an existing connection */
573 bool expect100header; /* TRUE if we added Expect: 100-continue */
574 bool ftp_use_epsv; /* if EPSV is to be attempted or not */
578 * In August 2001, this struct was redesigned and is since stricter than
579 * before. The 'connectdata' struct MUST have all the connection oriented
580 * stuff as we may now have several simultaneous connections and connection
583 * From now on, the 'SessionHandle' must only contain data that is set once to
584 * go for many (perhaps) independent connections. Values that are generated or
585 * calculated internally for the "session handle" must be defined within the
586 * 'struct urlstate' instead. */
588 struct SessionHandle {
589 struct UserDefined set; /* values set by the libcurl user */
590 struct DynamicStatic change; /* possibly modified userdefined data */
592 struct CookieInfo *cookies; /* the cookies, read from files and servers */
593 struct Progress progress; /* for all the progress meter data */
594 struct UrlState state; /* struct for fields used for state info and
595 other dynamic purposes */
596 struct PureInfo info; /* stats, reports and info data */
599 #define LIBCURL_NAME "libcurl"