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 */
31 #define PORT_TELNET 23
32 #define PORT_GOPHER 70
34 #define PORT_HTTPS 443
35 #define PORT_DICT 2628
38 #define DICT_MATCH "/MATCH:"
39 #define DICT_MATCH2 "/M:"
40 #define DICT_MATCH3 "/FIND:"
41 #define DICT_DEFINE "/DEFINE:"
42 #define DICT_DEFINE2 "/D:"
43 #define DICT_DEFINE3 "/LOOKUP:"
45 #define CURL_DEFAULT_USER "anonymous"
46 #define CURL_DEFAULT_PASSWORD "curl_by_daniel@haxx.se"
52 /* SSLeay stuff usually in /usr/local/ssl/include */
54 #include "openssl/rsa.h"
55 #include "openssl/crypto.h"
56 #include "openssl/x509.h"
57 #include "openssl/pem.h"
58 #include "openssl/ssl.h"
59 #include "openssl/err.h"
70 #ifdef HAVE_NETINET_IN_H
71 #include <netinet/in.h>
76 #include <curl/curl.h>
78 #include "http_chunks.h" /* for the structs and enum stuff */
80 /* Download buffer size, keep it fairly big for speed reasons */
81 #define BUFSIZE (1024*50)
83 /* Defaul upload buffer size, keep it smallish to get faster progress meter
84 updates. This is just default, it is dynamic and adjusts to the upload
86 #define UPLOAD_BUFSIZE (1024*2)
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 /* struct for data related to SSL and SSL connections */
114 struct ssl_connect_data {
115 bool use; /* use ssl encrypted communications TRUE/FALSE */
117 /* these ones requires specific SSL-types */
121 #endif /* USE_SSLEAY */
124 /* information about one single SSL session */
125 struct curl_ssl_session {
126 char *name; /* host name for which this ID was used */
127 void *sessionid; /* as returned from the SSL layer */
128 long age; /* just a number, the higher the more recent */
129 unsigned short remote_port; /* remote port to connect to */
132 struct ssl_config_data {
133 long version; /* what version the client wants to use */
134 long certverifyresult; /* result from the certificate verification */
135 long verifypeer; /* set TRUE if this is desired */
136 long verifyhost; /* 0: no verif, 1: check that CN exists, 2: CN must match hostname */
137 char *CApath; /* DOES NOT WORK ON WINDOWS */
138 char *CAfile; /* cerficate to verify peer against */
139 char *random_file; /* path to file containing "random" data */
140 char *egdsocket; /* path to file containing the EGD daemon socket */
142 long numsessions; /* SSL session id cache size */
145 /****************************************************************************
147 ***************************************************************************/
149 struct FormData *sendit;
151 const char *p_pragma; /* Pragma: string */
152 const char *p_accept; /* Accept: string */
156 /* For FORM posting */
158 curl_read_callback storefread;
161 struct Curl_chunker chunk;
164 /****************************************************************************
166 ***************************************************************************/
169 char *user; /* user name string */
170 char *passwd; /* password string */
171 char *urlpath; /* the originally given path part of the URL */
172 char *dir; /* decoded directory */
173 char *file; /* decoded file */
175 char *entrypath; /* the PWD reply when we logged on */
178 /****************************************************************************
180 ***************************************************************************/
182 int fd; /* open file descriptor to read from! */
186 * Boolean values that concerns this connection.
189 bool close; /* if set, we close the connection after this request */
190 bool reuse; /* if set, this is a re-used connection */
191 bool chunk; /* if set, this is a chunked transfer-encoding */
192 bool httpproxy; /* if set, this transfer is done through a http proxy */
193 bool user_passwd; /* do we use user+password for this connection? */
194 bool proxy_user_passwd; /* user+password for the proxy? */
197 bool rangestringalloc; /* the range string is malloc()'ed */
199 bool resume_done; /* nothing was transfered, resumed transfer already
204 * The connectdata struct contains all fields and variables that should be
205 * unique for an entire connection.
208 /**** Fields set when inited and not modified again */
209 struct SessionHandle *data; /* link to the root CURL struct */
210 int connectindex; /* what index in the connects index this particular
213 long protocol; /* PROT_* flags concerning the protocol set */
214 #define PROT_MISSING (1<<0)
215 #define PROT_GOPHER (1<<1)
216 #define PROT_HTTP (1<<2)
217 #define PROT_HTTPS (1<<3)
218 #define PROT_FTP (1<<4)
219 #define PROT_TELNET (1<<5)
220 #define PROT_DICT (1<<6)
221 #define PROT_LDAP (1<<7)
222 #define PROT_FILE (1<<8)
223 #define PROT_FTPS (1<<9)
226 struct addrinfo *hp; /* host info pointer list */
227 struct addrinfo *ai; /* the particular host we use */
229 char *hostent_buf; /* pointer to allocated memory for name info */
231 struct sockaddr_in serv_addr;
233 char protostr[64]; /* store the protocol string in this buffer */
234 char gname[257]; /* store the hostname in this buffer */
235 char *name; /* host name pointer to fool around with */
236 char *path; /* allocated buffer to store the URL's path part in */
237 char *hostname; /* hostname to connect, as parsed from url */
238 long port; /* which port to use locally */
239 unsigned short remote_port; /* what remote port to connect to,
240 not the proxy port! */
244 char *range; /* range, if used. See README for detailed specification on
246 int resume_from; /* continue [ftp] transfer from here */
248 char *proxyhost; /* name of the http proxy host */
250 struct timeval now; /* "current" time */
251 struct timeval created; /* creation time */
252 int firstsocket; /* the main socket to use */
253 int secondarysocket; /* for i.e ftp transfers */
255 long upload_bufsize; /* adjust as you see fit, never bigger than BUFSIZE
256 never smaller than UPLOAD_BUFSIZE */
258 long maxdownload; /* in bytes, the maximum amount of data to fetch, 0
261 struct ssl_connect_data ssl; /* this is for ssl-stuff */
263 struct ConnectBits bits; /* various state-flags for this connection */
265 /* These two functions MUST be set by the curl_connect() function to be
266 be protocol dependent */
267 CURLcode (*curl_do)(struct connectdata *connect);
268 CURLcode (*curl_done)(struct connectdata *connect);
270 /* This function *MAY* be set to a protocol-dependent function that is run
271 * after the connect() and everything is done, as a step in the connection.
273 CURLcode (*curl_connect)(struct connectdata *connect);
275 /* This function *MAY* be set to a protocol-dependent function that is run
276 * by the curl_disconnect(), as a step in the disconnection.
278 CURLcode (*curl_disconnect)(struct connectdata *connect);
280 /* This function *MAY* be set to a protocol-dependent function that is run
281 * in the curl_close() function if protocol-specific cleanups are required.
283 CURLcode (*curl_close)(struct connectdata *connect);
285 /**** curl_get() phase fields */
288 int sockfd; /* socket to read from or -1 */
289 int size; /* -1 if unknown at this point */
290 bool getheader; /* TRUE if header parsing is wanted */
291 long *bytecountp; /* return number of bytes read or NULL */
294 int writesockfd; /* socket to write to, it may very well be
295 the same we read from. -1 disables */
296 long *writebytecountp; /* return number of bytes written or NULL */
298 /** Dynamicly allocated strings, may need to be freed before this **/
299 /** struct is killed. **/
300 struct dynamically_allocated_data {
301 char *proxyuserpwd; /* free later if not NULL! */
302 char *uagent; /* free later if not NULL! */
303 char *userpwd; /* free later if not NULL! */
304 char *rangeline; /* free later if not NULL! */
305 char *ref; /* free later if not NULL! */
306 char *cookie; /* free later if not NULL! */
307 char *host; /* free later if not NULL */
310 char *newurl; /* This can only be set if a Location: was in the
314 enum protection_level command_prot;
315 enum protection_level data_prot;
316 enum protection_level request_data_prot;
320 struct krb4buffer in_buffer, out_buffer;
324 struct Curl_sec_client_mech *mech;
325 struct sockaddr_in local_addr;
329 /*************** Request - specific items ************/
330 /* previously this was in the urldata struct */
333 struct HTTP *gopher; /* alias, just for the sake of being more readable */
334 struct HTTP *https; /* alias, just for the sake of being more readable */
337 void *telnet; /* private for telnet.c-eyes only */
338 #if 0 /* no need for special ones for these: */
348 * Struct to keep statistical and informational data.
353 time_t filetime; /* If requested, this is might get set. It may be 0 if
354 the time was unretrievable */
355 long header_size; /* size of read header(s) in bytes */
356 long request_size; /* the amount of bytes sent in the request(s) */
361 long lastshow; /* time() of the last displayed progress meter or NULL to
362 force redraw at next call */
368 double current_speed; /* uses the currently fastest transfer */
370 bool callback; /* set when progress callback is used */
371 int width; /* screen width at download start */
372 int flags; /* see progress.h */
379 double t_pretransfer;
381 struct timeval start;
382 struct timeval t_startsingle;
385 double speeder[ CURR_TIME ];
390 HTTPREQ_NONE, /* first in list */
393 HTTPREQ_POST_FORM, /* we make a difference internally */
396 HTTPREQ_LAST /* last in list */
400 * Values that are generated, temporary or calculated internally for a
401 * "session handle" must be defined within the 'struct urlstate'. This struct
402 * will be used within the SessionHandle struct. When the 'SessionHandle'
403 * struct is cloned, this data MUST NOT be copied.
405 * Remember that any "state" information goes globally for the curl handle.
406 * Session-data MUST be put in the connectdata struct and here. */
407 #define MAX_CURL_USER_LENGTH 256
408 #define MAX_CURL_PASSWORD_LENGTH 256
411 /* buffers to store authentication data in, as parsed from input options */
412 char user[MAX_CURL_USER_LENGTH];
413 char passwd[MAX_CURL_PASSWORD_LENGTH];
414 char proxyuser[MAX_CURL_USER_LENGTH];
415 char proxypasswd[MAX_CURL_PASSWORD_LENGTH];
417 struct timeval keeps_speed; /* for the progress meter really */
419 /* 'connects' will be an allocated array with pointers. If the pointer is
420 set, it holds an allocated connection. */
421 struct connectdata **connects;
422 long numconnects; /* size of the 'connects' array */
424 char *headerbuff; /* allocated buffer to store headers in */
425 int headersize; /* size of the allocation */
427 char buffer[BUFSIZE+1]; /* buffer with size BUFSIZE */
429 double current_speed; /* the ProgressShow() funcion sets this */
431 bool this_is_a_follow; /* this is a followed Location: request */
433 char *auth_host; /* if set, this should be the host name that we will
434 sent authorization to, no else. Used to make Location:
435 following not keep sending user+password... This is
439 struct curl_ssl_session *session; /* array of 'numsessions' size */
440 long sessionage; /* number of the most recent session */
445 * This 'DynamicStatic' struct defines dynamic states that actually change
446 * values in the 'UserDefined' area, which MUST be taken into consideration
447 * if the UserDefined struct is cloned or similar. You can probably just
448 * copy these, but each one indicate a special action on other data.
451 struct DynamicStatic {
452 char *url; /* work URL, copied from UserDefined */
453 bool url_alloc; /* URL string is malloc()'ed */
454 char *proxy; /* work proxy, copied from UserDefined */
455 bool proxy_alloc; /* http proxy string is malloc()'ed */
456 char *referer; /* referer string */
457 bool referer_alloc; /* referer sting is malloc()ed */
461 * This 'UserDefined' struct must only contain data that is set once to go
462 * for many (perhaps) independent connections. Values that are generated or
463 * calculated internally for the "session handle" MUST be defined within the
464 * 'struct urlstate' instead. The only exceptions MUST note the changes in
465 * the 'DynamicStatic' struct.
469 FILE *err; /* the stderr writes goes here */
470 char *errorbuffer; /* store failure messages in here */
471 char *proxyuserpwd; /* Proxy <user:password>, if used */
472 long proxyport; /* If non-zero, use this port number by default. If the
473 proxy string features a ":[port]" that one will override
475 void *out; /* the fetched file goes here */
476 void *in; /* the uploaded file is read from here */
477 void *writeheader; /* write the header to this is non-NULL */
478 char *set_url; /* what original URL to work on */
479 char *set_proxy; /* proxy to use */
480 long use_port; /* which port to use (when not using default) */
481 char *userpwd; /* <user:password>, if used */
482 char *set_range; /* range, if used. See README for detailed specification
484 long followlocation; /* as in HTTP Location: */
485 long maxredirs; /* maximum no. of http(s) redirects to follow */
486 char *set_referer; /* custom string */
487 bool free_referer; /* set TRUE if 'referer' points to a string we
489 char *useragent; /* User-Agent string */
490 char *postfields; /* if POST, set the fields' values here */
491 size_t postfieldsize; /* if POST, this might have a size to use instead of
492 strlen(), and then the data *may* be binary (contain
494 char *ftpport; /* port to send with the FTP PORT command */
495 char *device; /* network interface to use */
496 curl_write_callback fwrite; /* function that stores the output */
497 curl_write_callback fwrite_header; /* function that stores headers */
498 curl_read_callback fread; /* function that reads the input */
499 curl_progress_callback fprogress; /* function for progress information */
500 void *progress_client; /* pointer to pass to the progress callback */
501 curl_passwd_callback fpasswd; /* call for password */
502 void *passwd_client; /* pass to the passwd callback */
503 long timeout; /* in seconds, 0 means no timeout */
504 long connecttimeout; /* in seconds, 0 means no timeout */
505 long infilesize; /* size of file to upload, -1 means unknown */
506 long low_speed_limit; /* bytes/second */
507 long low_speed_time; /* number of seconds */
508 int set_resume_from; /* continue [ftp] transfer from here */
509 char *cookie; /* HTTP cookie string to send */
510 struct curl_slist *headers; /* linked list of extra headers */
511 struct HttpPost *httppost; /* linked list of POST data */
512 char *cert; /* PEM-formatted certificate */
513 char *cert_passwd; /* plain text certificate password */
514 char *cookiejar; /* dump all cookies to this file */
515 bool crlf; /* convert crlf on ftp upload(?) */
516 struct curl_slist *quote; /* before the transfer */
517 struct curl_slist *postquote; /* after the transfer */
518 struct curl_slist *telnet_options; /* linked list of telnet options */
519 TimeCond timecondition; /* kind of time/date comparison */
520 time_t timevalue; /* what time to compare with */
521 curl_closepolicy closepolicy; /* connection cache close concept */
522 Curl_HttpReq httpreq; /* what kind of HTTP request (if any) is this */
523 char *customrequest; /* HTTP/FTP request to use */
524 char *auth_host; /* if set, this is the allocated string to the host name
525 * to which to send the authorization data to, and no other
526 * host (which location-following otherwise could lead to)
528 char *krb4_level; /* what security level */
529 struct ssl_config_data ssl; /* user defined SSL stuff */
531 /* Here follows boolean settings that define how to behave during
532 this session. They are STATIC, set by libcurl users or at least initially
533 and they don't change during operations. */
536 bool tunnel_thru_httpproxy;
542 bool http_fail_on_error;
543 bool http_follow_location;
544 bool http_include_header;
545 bool http_set_referer;
546 bool http_auto_referer; /* set "correct" referer when following location: */
552 bool krb4; /* kerberos4 connection requested */
553 bool reuse_forbid; /* forbidden to be reused, close after use */
554 bool reuse_fresh; /* do not re-use an existing connection */
555 bool expect100header; /* TRUE if we added Expect: 100-continue */
559 * In August 2001, this struct was redesigned and is since stricter than
560 * before. The 'connectdata' struct MUST have all the connection oriented
561 * stuff as we may now have several simultaneous connections and connection
564 * From now on, the 'SessionHandle' must only contain data that is set once to
565 * go for many (perhaps) independent connections. Values that are generated or
566 * calculated internally for the "session handle" must be defined within the
567 * 'struct urlstate' instead. */
569 struct SessionHandle {
570 struct UserDefined set; /* values set by the libcurl user */
571 struct DynamicStatic change; /* possibly modified userdefined data */
573 struct CookieInfo *cookies; /* the cookies, read from files and servers */
574 struct Progress progress; /* for all the progress meter data */
575 struct UrlState state; /* struct for fields used for state info and
576 other dynamic purposes */
577 struct PureInfo info; /* stats, reports and info data */
580 #define LIBCURL_NAME "libcurl"