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 struct ssl_config_data {
129 long version; /* what version the client wants to use */
130 long certverifyresult; /* result from the certificate verification */
131 long verifypeer; /* set TRUE if this is desired */
132 long verifyhost; /* 0: no verif, 1: check that CN exists, 2: CN must match hostname */
133 char *CApath; /* DOES NOT WORK ON WINDOWS */
134 char *CAfile; /* cerficate to verify peer against */
135 char *random_file; /* path to file containing "random" data */
136 char *egdsocket; /* path to file containing the EGD daemon socket */
139 /****************************************************************************
141 ***************************************************************************/
143 struct FormData *sendit;
145 const char *p_pragma; /* Pragma: string */
146 const char *p_accept; /* Accept: string */
150 /* For FORM posting */
152 curl_read_callback storefread;
155 struct Curl_chunker chunk;
158 /****************************************************************************
160 ***************************************************************************/
163 char *user; /* user name string */
164 char *passwd; /* password string */
165 char *urlpath; /* the originally given path part of the URL */
166 char *dir; /* decoded directory */
167 char *file; /* decoded file */
169 char *entrypath; /* the PWD reply when we logged on */
172 /****************************************************************************
174 ***************************************************************************/
176 int fd; /* open file descriptor to read from! */
180 * Boolean values that concerns this connection.
183 bool close; /* if set, we close the connection after this request */
184 bool reuse; /* if set, this is a re-used connection */
185 bool chunk; /* if set, this is a chunked transfer-encoding */
186 bool httpproxy; /* if set, this transfer is done through a http proxy */
187 bool user_passwd; /* do we use user+password for this connection? */
188 bool proxy_user_passwd; /* user+password for the proxy? */
191 bool rangestringalloc; /* the range string is malloc()'ed */
193 bool resume_done; /* nothing was transfered, resumed transfer already
198 * The connectdata struct contains all fields and variables that should be
199 * unique for an entire connection.
202 /**** Fields set when inited and not modified again */
203 struct UrlData *data; /* link to the root CURL struct */
204 int connectindex; /* what index in the connects index this particular
207 long protocol; /* PROT_* flags concerning the protocol set */
208 #define PROT_MISSING (1<<0)
209 #define PROT_GOPHER (1<<1)
210 #define PROT_HTTP (1<<2)
211 #define PROT_HTTPS (1<<3)
212 #define PROT_FTP (1<<4)
213 #define PROT_TELNET (1<<5)
214 #define PROT_DICT (1<<6)
215 #define PROT_LDAP (1<<7)
216 #define PROT_FILE (1<<8)
217 #define PROT_FTPS (1<<9)
220 struct addrinfo *hp; /* host info pointer list */
221 struct addrinfo *ai; /* the particular host we use */
223 char *hostent_buf; /* pointer to allocated memory for name info */
225 struct sockaddr_in serv_addr;
227 char protostr[64]; /* store the protocol string in this buffer */
228 char gname[257]; /* store the hostname in this buffer */
229 char *name; /* host name pointer to fool around with */
230 char *path; /* allocated buffer to store the URL's path part in */
231 char *hostname; /* hostname to connect, as parsed from url */
232 long port; /* which port to use locally */
233 unsigned short remote_port; /* what remote port to connect to,
234 not the proxy port! */
238 char *range; /* range, if used. See README for detailed specification on
240 int resume_from; /* continue [ftp] transfer from here */
242 char *proxyhost; /* name of the http proxy host */
244 struct timeval now; /* "current" time */
245 struct timeval created; /* creation time */
246 int firstsocket; /* the main socket to use */
247 int secondarysocket; /* for i.e ftp transfers */
249 long upload_bufsize; /* adjust as you see fit, never bigger than BUFSIZE
250 never smaller than UPLOAD_BUFSIZE */
252 long maxdownload; /* in bytes, the maximum amount of data to fetch, 0
255 struct ssl_connect_data ssl; /* this is for ssl-stuff */
257 struct ConnectBits bits; /* various state-flags for this connection */
259 /* These two functions MUST be set by the curl_connect() function to be
260 be protocol dependent */
261 CURLcode (*curl_do)(struct connectdata *connect);
262 CURLcode (*curl_done)(struct connectdata *connect);
264 /* This function *MAY* be set to a protocol-dependent function that is run
265 * after the connect() and everything is done, as a step in the connection.
267 CURLcode (*curl_connect)(struct connectdata *connect);
269 /* This function *MAY* be set to a protocol-dependent function that is run
270 * by the curl_disconnect(), as a step in the disconnection.
272 CURLcode (*curl_disconnect)(struct connectdata *connect);
274 /* This function *MAY* be set to a protocol-dependent function that is run
275 * in the curl_close() function if protocol-specific cleanups are required.
277 CURLcode (*curl_close)(struct connectdata *connect);
279 /**** curl_get() phase fields */
282 int sockfd; /* socket to read from or -1 */
283 int size; /* -1 if unknown at this point */
284 bool getheader; /* TRUE if header parsing is wanted */
285 long *bytecountp; /* return number of bytes read or NULL */
288 int writesockfd; /* socket to write to, it may very well be
289 the same we read from. -1 disables */
290 long *writebytecountp; /* return number of bytes written or NULL */
292 /** Dynamicly allocated strings, may need to be freed before this **/
293 /** struct is killed. **/
294 struct dynamically_allocated_data {
295 char *proxyuserpwd; /* free later if not NULL! */
296 char *uagent; /* free later if not NULL! */
297 char *userpwd; /* free later if not NULL! */
298 char *rangeline; /* free later if not NULL! */
299 char *ref; /* free later if not NULL! */
300 char *cookie; /* free later if not NULL! */
301 char *host; /* free later if not NULL */
304 char *newurl; /* This can only be set if a Location: was in the
308 enum protection_level command_prot;
309 enum protection_level data_prot;
310 enum protection_level request_data_prot;
314 struct krb4buffer in_buffer, out_buffer;
320 /*************** Request - specific items ************/
321 /* previously this was in the urldata struct */
324 struct HTTP *gopher; /* alias, just for the sake of being more readable */
325 struct HTTP *https; /* alias, just for the sake of being more readable */
328 void *telnet; /* private for telnet.c-eyes only */
329 #if 0 /* no need for special ones for these: */
340 long lastshow; /* time() of the last displayed progress meter or NULL to
341 force redraw at next call */
347 double current_speed; /* uses the currently fastest transfer */
349 bool callback; /* set when progress callback is used */
350 int width; /* screen width at download start */
351 int flags; /* see progress.h */
356 struct timeval start;
357 struct timeval t_startsingle;
358 /* various data stored for possible later report */
361 double t_pretransfer;
364 time_t filetime; /* If requested, this is might get set. It may be 0 if
365 the time was unretrievable */
369 double speeder[ CURR_TIME ];
374 HTTPREQ_NONE, /* first in list */
377 HTTPREQ_POST_FORM, /* we make a difference internally */
380 HTTPREQ_LAST /* last in list */
383 /* This struct is for boolean settings that define how to behave during
387 bool tunnel_thru_httpproxy;
393 bool http_fail_on_error;
394 bool http_follow_location;
395 bool http_include_header;
396 bool http_set_referer;
397 bool http_auto_referer; /* set "correct" referer when following location: */
405 bool this_is_a_follow; /* this is a followed Location: request */
406 bool krb4; /* kerberos4 connection requested */
407 bool proxystringalloc; /* the http proxy string is malloc()'ed */
408 bool urlstringalloc; /* the URL string is malloc()'ed */
409 bool reuse_forbid; /* if this is forbidden to be reused, close
411 bool reuse_fresh; /* do not re-use an existing connection for this
416 * As of April 11, 2000 we're now trying to split up the urldata struct in
417 * three different parts:
420 * 1 - No matter how many hosts and requests that are being performed, this
421 * goes for all of them.
424 * 2 - Host and protocol-specific. No matter if we do several transfers to and
425 * from this host, these variables stay the same.
428 * 3 - Request-specific. Variables that are of interest for this particular
429 * transfer being made right now. THIS IS WRONG STRUCT FOR THOSE.
431 * In Febrary 2001, this is being done stricter. The 'connectdata' struct
432 * MUST have all the connection oriented stuff as we may now have several
433 * simultaneous connections and connection structs in memory.
435 * From now on, the 'UrlData' must only contain data that is set once to go
436 * for many (perhaps) independent connections. Values that are generated or
437 * calculated internally MUST NOT be a part of this struct.
441 /*************** Global - specific items ************/
442 FILE *err; /* the stderr writes goes here */
443 char *errorbuffer; /* store failure messages in here */
445 /*************** Session - specific items ************/
446 char *proxy; /* if proxy, set it here */
447 char *proxyuserpwd; /* Proxy <user:password>, if used */
448 long proxyport; /* If non-zero, use this port number by default. If the
449 proxy string features a ":[port]" that one will override
453 long header_size; /* size of read header(s) in bytes */
454 long request_size; /* the amount of bytes sent in the request(s) */
456 void *out; /* the fetched file goes here */
457 void *in; /* the uploaded file is read from here */
458 void *writeheader; /* write the header to this is non-NULL */
460 char *url; /* what to get */
461 char *freethis; /* if non-NULL, an allocated string for the URL */
462 long use_port; /* which port to use (when not using default) */
463 struct Configbits bits; /* new-style (v7) flag data */
464 struct ssl_config_data ssl; /* this is for ssl-stuff */
466 char *userpwd; /* <user:password>, if used */
467 char *set_range; /* range, if used. See README for detailed specification on
470 /* stuff related to HTTP */
473 long maxredirs; /* maximum no. of http(s) redirects to follow */
475 bool free_referer; /* set TRUE if 'referer' points to a string we
477 char *useragent; /* User-Agent string */
478 char *postfields; /* if POST, set the fields' values here */
479 size_t postfieldsize; /* if POST, this might have a size to use instead of
480 strlen(), and then the data *may* be binary (contain
483 /* stuff related to FTP */
484 char *ftpport; /* port to send with the PORT command */
487 char *device; /* Interface to use */
489 /* function that stores the output:*/
490 curl_write_callback fwrite;
492 /* optional function that stores the header output:*/
493 curl_write_callback fwrite_header;
495 /* function that reads the input:*/
496 curl_read_callback fread;
498 /* function that wants progress information */
499 curl_progress_callback fprogress;
500 void *progress_client; /* pointer to pass to the progress callback */
502 /* function to call instead of the internal for password */
503 curl_passwd_callback fpasswd;
504 void *passwd_client; /* pointer to 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 */
510 char buffer[BUFSIZE+1]; /* buffer with size BUFSIZE */
512 double current_speed; /* the ProgressShow() funcion sets this */
514 long low_speed_limit; /* bytes/second */
515 long low_speed_time; /* number of seconds */
517 int set_resume_from; /* continue [ftp] transfer from here */
519 char *cookie; /* HTTP cookie string to send */
521 struct curl_slist *headers; /* linked list of extra headers */
522 struct HttpPost *httppost; /* linked list of POST data */
524 char *cert; /* PEM-formatted certificate */
525 char *cert_passwd; /* plain text certificate password */
527 struct CookieInfo *cookies;
530 struct curl_slist *quote; /* before the transfer */
531 struct curl_slist *postquote; /* after the transfer */
533 /* Telnet negotiation options */
534 struct curl_slist *telnet_options; /* linked list of telnet options */
536 TimeCond timecondition; /* kind of comparison */
537 time_t timevalue; /* what time to compare with */
539 Curl_HttpReq httpreq; /* what kind of HTTP request (if any) is this */
541 char *customrequest; /* http/ftp request to use */
543 char *headerbuff; /* allocated buffer to store headers in */
544 int headersize; /* size of the allocation */
546 struct Progress progress; /* for all the progress meter data */
548 #define MAX_CURL_USER_LENGTH 128
549 #define MAX_CURL_PASSWORD_LENGTH 128
551 char *auth_host; /* if set, this is the allocated string to the host name
552 * to which to send the authorization data to, and no other
553 * host (which location-following otherwise could lead to)
556 /* buffers to store authentication data in */
557 char user[MAX_CURL_USER_LENGTH];
558 char passwd[MAX_CURL_PASSWORD_LENGTH];
559 char proxyuser[MAX_CURL_USER_LENGTH];
560 char proxypasswd[MAX_CURL_PASSWORD_LENGTH];
562 char *krb4_level; /* what security level */
563 struct timeval keeps_speed; /* this should be request-specific */
565 /* 'connects' will be an allocated array with pointers. If the pointer is
566 set, it holds an allocated connection. */
567 struct connectdata **connects;
568 long numconnects; /* size of the 'connects' array */
569 curl_closepolicy closepolicy;
573 #define LIBCURL_NAME "libcurl"
574 #define LIBCURL_ID LIBCURL_NAME " " LIBCURL_VERSION " " SSL_ID
576 CURLcode Curl_getinfo(CURL *curl, CURLINFO info, ...);
579 * Here follows function prototypes from what we used to plan to call
580 * the "low level" interface. It is no longer prioritized and it is not likely
581 * to ever be supported to external users.
583 * I removed all the comments to them as well, as they were no longer accurate
584 * and they're not meant for "public use" anymore.
587 CURLcode Curl_open(CURL **curl);
588 CURLcode Curl_setopt(CURL *handle, CURLoption option, ...);
589 CURLcode Curl_close(CURL *curl); /* the opposite of curl_open() */
590 CURLcode Curl_connect(struct UrlData *,
591 struct connectdata **,
593 CURLcode Curl_do(struct connectdata *);
594 CURLcode Curl_done(struct connectdata *);
595 CURLcode Curl_disconnect(struct connectdata *);