3 /*****************************************************************************
5 * Project ___| | | | _ \| |
7 * | (__| |_| | _ <| |___
8 * \___|\___/|_| \_\_____|
10 * Copyright (C) 2000, 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 */
30 #ifndef MAXHOSTNAMELEN
31 #define MAXHOSTNAMELEN 256
35 #define PORT_TELNET 23
36 #define PORT_GOPHER 70
38 #define PORT_HTTPS 443
39 #define PORT_DICT 2628
42 #define DICT_MATCH "/MATCH:"
43 #define DICT_MATCH2 "/M:"
44 #define DICT_MATCH3 "/FIND:"
45 #define DICT_DEFINE "/DEFINE:"
46 #define DICT_DEFINE2 "/D:"
47 #define DICT_DEFINE3 "/LOOKUP:"
49 #define CURL_DEFAULT_USER "anonymous"
50 #define CURL_DEFAULT_PASSWORD "curl_by_daniel@haxx.se"
56 /* SSLeay stuff usually in /usr/local/ssl/include */
58 #include "openssl/rsa.h"
59 #include "openssl/crypto.h"
60 #include "openssl/x509.h"
61 #include "openssl/pem.h"
62 #include "openssl/ssl.h"
63 #include "openssl/err.h"
74 #ifdef HAVE_NETINET_IN_H
75 #include <netinet/in.h>
80 #include <curl/curl.h>
82 #include "http_chunks.h" /* for the structs and enum stuff */
84 /* Download buffer size, keep it fairly big for speed reasons */
85 #define BUFSIZE (1024*50)
87 /* Defaul upload buffer size, keep it smallish to get faster progress meter
88 updates. This is just default, it is dynamic and adjusts to the upload
90 #define UPLOAD_BUFSIZE (1024*2)
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 /* struct for data related to SSL and SSL connections */
118 struct ssl_connect_data {
119 bool use; /* use ssl encrypted communications TRUE/FALSE */
121 /* these ones requires specific SSL-types */
125 #endif /* USE_SSLEAY */
128 /* information about one single SSL session */
129 struct curl_ssl_session {
130 char *name; /* host name for which this ID was used */
131 void *sessionid; /* as returned from the SSL layer */
132 long age; /* just a number, the higher the more recent */
133 unsigned short remote_port; /* remote port to connect to */
136 struct ssl_config_data {
137 long version; /* what version the client wants to use */
138 long certverifyresult; /* result from the certificate verification */
139 long verifypeer; /* set TRUE if this is desired */
140 long verifyhost; /* 0: no verif, 1: check that CN exists, 2: CN must match hostname */
141 char *CApath; /* DOES NOT WORK ON WINDOWS */
142 char *CAfile; /* cerficate to verify peer against */
143 char *random_file; /* path to file containing "random" data */
144 char *egdsocket; /* path to file containing the EGD daemon socket */
146 struct curl_ssl_session *session; /* array of 'numsessions' size */
147 long numsessions; /* SSL session id cache size */
148 long sessionage; /* number of the most recent session */
151 /****************************************************************************
153 ***************************************************************************/
155 struct FormData *sendit;
157 const char *p_pragma; /* Pragma: string */
158 const char *p_accept; /* Accept: string */
162 /* For FORM posting */
164 curl_read_callback storefread;
167 struct Curl_chunker chunk;
170 /****************************************************************************
172 ***************************************************************************/
175 char *user; /* user name string */
176 char *passwd; /* password string */
177 char *urlpath; /* the originally given path part of the URL */
178 char *dir; /* decoded directory */
179 char *file; /* decoded file */
181 char *entrypath; /* the PWD reply when we logged on */
184 /****************************************************************************
186 ***************************************************************************/
188 int fd; /* open file descriptor to read from! */
192 * Boolean values that concerns this connection.
195 bool close; /* if set, we close the connection after this request */
196 bool reuse; /* if set, this is a re-used connection */
197 bool chunk; /* if set, this is a chunked transfer-encoding */
198 bool httpproxy; /* if set, this transfer is done through a http proxy */
199 bool user_passwd; /* do we use user+password for this connection? */
200 bool proxy_user_passwd; /* user+password for the proxy? */
203 bool rangestringalloc; /* the range string is malloc()'ed */
205 bool resume_done; /* nothing was transfered, resumed transfer already
210 * The connectdata struct contains all fields and variables that should be
211 * unique for an entire connection.
214 /**** Fields set when inited and not modified again */
215 struct SessionHandle *data; /* link to the root CURL struct */
216 int connectindex; /* what index in the connects index this particular
219 long protocol; /* PROT_* flags concerning the protocol set */
220 #define PROT_MISSING (1<<0)
221 #define PROT_GOPHER (1<<1)
222 #define PROT_HTTP (1<<2)
223 #define PROT_HTTPS (1<<3)
224 #define PROT_FTP (1<<4)
225 #define PROT_TELNET (1<<5)
226 #define PROT_DICT (1<<6)
227 #define PROT_LDAP (1<<7)
228 #define PROT_FILE (1<<8)
229 #define PROT_FTPS (1<<9)
232 struct addrinfo *hp; /* host info pointer list */
233 struct addrinfo *ai; /* the particular host we use */
235 char *hostent_buf; /* pointer to allocated memory for name info */
237 struct sockaddr_in serv_addr;
239 char protostr[64]; /* store the protocol string in this buffer */
240 char gname[257]; /* 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! */
250 char *range; /* range, if used. See README for detailed specification on
252 int resume_from; /* continue [ftp] transfer from here */
254 char *proxyhost; /* name of the http proxy host */
256 struct timeval now; /* "current" time */
257 struct timeval created; /* creation time */
258 int firstsocket; /* the main socket to use */
259 int secondarysocket; /* for i.e ftp transfers */
261 long upload_bufsize; /* adjust as you see fit, never bigger than BUFSIZE
262 never smaller than UPLOAD_BUFSIZE */
264 long maxdownload; /* in bytes, the maximum amount of data to fetch, 0
267 struct ssl_connect_data ssl; /* this is for ssl-stuff */
269 struct ConnectBits bits; /* various state-flags for this connection */
271 /* These two functions MUST be set by the curl_connect() function to be
272 be protocol dependent */
273 CURLcode (*curl_do)(struct connectdata *connect);
274 CURLcode (*curl_done)(struct connectdata *connect);
276 /* This function *MAY* be set to a protocol-dependent function that is run
277 * after the connect() and everything is done, as a step in the connection.
279 CURLcode (*curl_connect)(struct connectdata *connect);
281 /* This function *MAY* be set to a protocol-dependent function that is run
282 * by the curl_disconnect(), as a step in the disconnection.
284 CURLcode (*curl_disconnect)(struct connectdata *connect);
286 /* This function *MAY* be set to a protocol-dependent function that is run
287 * in the curl_close() function if protocol-specific cleanups are required.
289 CURLcode (*curl_close)(struct connectdata *connect);
291 /**** curl_get() phase fields */
294 int sockfd; /* socket to read from or -1 */
295 int size; /* -1 if unknown at this point */
296 bool getheader; /* TRUE if header parsing is wanted */
297 long *bytecountp; /* return number of bytes read or NULL */
300 int writesockfd; /* socket to write to, it may very well be
301 the same we read from. -1 disables */
302 long *writebytecountp; /* return number of bytes written or NULL */
304 /** Dynamicly allocated strings, may need to be freed before this **/
305 /** struct is killed. **/
306 struct dynamically_allocated_data {
307 char *proxyuserpwd; /* free later if not NULL! */
308 char *uagent; /* free later if not NULL! */
309 char *userpwd; /* free later if not NULL! */
310 char *rangeline; /* free later if not NULL! */
311 char *ref; /* free later if not NULL! */
312 char *cookie; /* free later if not NULL! */
313 char *host; /* free later if not NULL */
316 char *newurl; /* This can only be set if a Location: was in the
320 enum protection_level command_prot;
321 enum protection_level data_prot;
322 enum protection_level request_data_prot;
326 struct krb4buffer in_buffer, out_buffer;
330 struct Curl_sec_client_mech *mech;
331 struct sockaddr_in local_addr;
335 /*************** Request - specific items ************/
336 /* previously this was in the urldata struct */
339 struct HTTP *gopher; /* alias, just for the sake of being more readable */
340 struct HTTP *https; /* alias, just for the sake of being more readable */
343 void *telnet; /* private for telnet.c-eyes only */
344 #if 0 /* no need for special ones for these: */
354 * Struct to keep statistical and informational data.
359 time_t filetime; /* If requested, this is might get set. It may be 0 if
360 the time was unretrievable */
361 long header_size; /* size of read header(s) in bytes */
362 long request_size; /* the amount of bytes sent in the request(s) */
367 long lastshow; /* time() of the last displayed progress meter or NULL to
368 force redraw at next call */
374 double current_speed; /* uses the currently fastest transfer */
376 bool callback; /* set when progress callback is used */
377 int width; /* screen width at download start */
378 int flags; /* see progress.h */
385 double t_pretransfer;
387 struct timeval start;
388 struct timeval t_startsingle;
391 double speeder[ CURR_TIME ];
396 HTTPREQ_NONE, /* first in list */
399 HTTPREQ_POST_FORM, /* we make a difference internally */
402 HTTPREQ_LAST /* last in list */
406 * Values that are generated, temporary or calculated internally for a
407 * "session handle" must be defined within the 'struct urlstate'. This struct
408 * will be used within the SessionHandle struct. When the 'SessionHandle'
409 * struct is cloned, this data MUST NOT be copied.
411 * Remember that any "state" information goes globally for the curl handle.
412 * Session-data MUST be put in the connectdata struct and here. */
413 #define MAX_CURL_USER_LENGTH 256
414 #define MAX_CURL_PASSWORD_LENGTH 256
417 /* buffers to store authentication data in, as parsed from input options */
418 char user[MAX_CURL_USER_LENGTH];
419 char passwd[MAX_CURL_PASSWORD_LENGTH];
420 char proxyuser[MAX_CURL_USER_LENGTH];
421 char proxypasswd[MAX_CURL_PASSWORD_LENGTH];
423 struct timeval keeps_speed; /* for the progress meter really */
425 /* 'connects' will be an allocated array with pointers. If the pointer is
426 set, it holds an allocated connection. */
427 struct connectdata **connects;
428 long numconnects; /* size of the 'connects' array */
430 char *headerbuff; /* allocated buffer to store headers in */
431 int headersize; /* size of the allocation */
433 char buffer[BUFSIZE+1]; /* buffer with size BUFSIZE */
435 double current_speed; /* the ProgressShow() funcion sets this */
437 bool this_is_a_follow; /* this is a followed Location: request */
439 char *auth_host; /* if set, this should be the host name that we will
440 sent authorization to, no else. Used to make Location:
441 following not keep sending user+password... This is
448 * This 'DynamicStatic' struct defines dynamic states that actually change
449 * values in the 'UserDefined' area, which MUST be taken into consideration
450 * if the UserDefined struct is cloned or similar. You can probably just
451 * copy these, but each one indicate a special action on other data.
454 struct DynamicStatic {
455 char *url; /* work URL, copied from UserDefined */
456 bool url_alloc; /* URL string is malloc()'ed */
457 char *proxy; /* work proxy, copied from UserDefined */
458 bool proxy_alloc; /* http proxy string is malloc()'ed */
459 char *referer; /* referer string */
460 bool referer_alloc; /* referer sting is malloc()ed */
464 * This 'UserDefined' struct must only contain data that is set once to go
465 * for many (perhaps) independent connections. Values that are generated or
466 * calculated internally for the "session handle" MUST be defined within the
467 * 'struct urlstate' instead. The only exceptions MUST note the changes in
468 * the 'DynamicStatic' struct.
472 FILE *err; /* the stderr writes goes here */
473 char *errorbuffer; /* store failure messages in here */
474 char *proxyuserpwd; /* Proxy <user:password>, if used */
475 long proxyport; /* If non-zero, use this port number by default. If the
476 proxy string features a ":[port]" that one will override
478 void *out; /* the fetched file goes here */
479 void *in; /* the uploaded file is read from here */
480 void *writeheader; /* write the header to this is non-NULL */
481 char *set_url; /* what original URL to work on */
482 char *set_proxy; /* proxy to use */
483 long use_port; /* which port to use (when not using default) */
484 char *userpwd; /* <user:password>, if used */
485 char *set_range; /* range, if used. See README for detailed specification
487 long followlocation; /* as in HTTP Location: */
488 long maxredirs; /* maximum no. of http(s) redirects to follow */
489 char *set_referer; /* custom string */
490 bool free_referer; /* set TRUE if 'referer' points to a string we
492 char *useragent; /* User-Agent string */
493 char *postfields; /* if POST, set the fields' values here */
494 size_t postfieldsize; /* if POST, this might have a size to use instead of
495 strlen(), and then the data *may* be binary (contain
497 char *ftpport; /* port to send with the FTP PORT command */
498 char *device; /* network interface to use */
499 curl_write_callback fwrite; /* function that stores the output */
500 curl_write_callback fwrite_header; /* function that stores headers */
501 curl_read_callback fread; /* function that reads the input */
502 curl_progress_callback fprogress; /* function for progress information */
503 void *progress_client; /* pointer to pass to the progress callback */
504 curl_passwd_callback fpasswd; /* call for password */
505 void *passwd_client; /* pass to the passwd callback */
506 long timeout; /* in seconds, 0 means no timeout */
507 long connecttimeout; /* in seconds, 0 means no timeout */
508 long infilesize; /* size of file to upload, -1 means unknown */
509 long low_speed_limit; /* bytes/second */
510 long low_speed_time; /* number of seconds */
511 int set_resume_from; /* continue [ftp] transfer from here */
512 char *cookie; /* HTTP cookie string to send */
513 struct curl_slist *headers; /* linked list of extra headers */
514 struct HttpPost *httppost; /* linked list of POST data */
515 char *cert; /* PEM-formatted certificate */
516 char *cert_passwd; /* plain text certificate password */
517 char *cookiejar; /* dump all cookies to this file */
518 bool crlf; /* convert crlf on ftp upload(?) */
519 struct curl_slist *quote; /* before the transfer */
520 struct curl_slist *postquote; /* after the transfer */
521 struct curl_slist *telnet_options; /* linked list of telnet options */
522 TimeCond timecondition; /* kind of time/date comparison */
523 time_t timevalue; /* what time to compare with */
524 curl_closepolicy closepolicy; /* connection cache close concept */
525 Curl_HttpReq httpreq; /* what kind of HTTP request (if any) is this */
526 char *customrequest; /* HTTP/FTP request to use */
527 char *auth_host; /* if set, this is the allocated string to the host name
528 * to which to send the authorization data to, and no other
529 * host (which location-following otherwise could lead to)
531 char *krb4_level; /* what security level */
532 struct ssl_config_data ssl; /* user defined SSL stuff */
534 /* Here follows boolean settings that define how to behave during
535 this session. They are STATIC, set by libcurl users or at least initially
536 and they don't change during operations. */
539 bool tunnel_thru_httpproxy;
545 bool http_fail_on_error;
546 bool http_follow_location;
547 bool http_include_header;
548 bool http_set_referer;
549 bool http_auto_referer; /* set "correct" referer when following location: */
555 bool krb4; /* kerberos4 connection requested */
556 bool reuse_forbid; /* forbidden to be reused, close after use */
557 bool reuse_fresh; /* do not re-use an existing connection */
558 bool expect100header; /* TRUE if we added Expect: 100-continue */
562 * In August 2001, this struct was redesigned and is since stricter than
563 * before. The 'connectdata' struct MUST have all the connection oriented
564 * stuff as we may now have several simultaneous connections and connection
567 * From now on, the 'SessionHandle' must only contain data that is set once to
568 * go for many (perhaps) independent connections. Values that are generated or
569 * calculated internally for the "session handle" must be defined within the
570 * 'struct urlstate' instead. */
572 struct SessionHandle {
573 struct UserDefined set; /* values set by the libcurl user */
574 struct DynamicStatic change; /* possibly modified userdefined data */
576 struct CookieInfo *cookies; /* the cookies, read from files and servers */
577 struct Progress progress; /* for all the progress meter data */
578 struct UrlState state; /* struct for fields used for state info and
579 other dynamic purposes */
580 struct PureInfo info; /* stats, reports and info data */
583 #define LIBCURL_NAME "libcurl"
584 #define LIBCURL_ID LIBCURL_NAME " " LIBCURL_VERSION " " SSL_ID