Fixed an outdated mention of having keep strings around in curl_easy_setopt
[platform/upstream/curl.git] / docs / libcurl / libcurl-tutorial.3
1 .\" **************************************************************************
2 .\" *                                  _   _ ____  _
3 .\" *  Project                     ___| | | |  _ \| |
4 .\" *                             / __| | | | |_) | |
5 .\" *                            | (__| |_| |  _ <| |___
6 .\" *                             \___|\___/|_| \_\_____|
7 .\" *
8 .\" * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
9 .\" *
10 .\" * This software is licensed as described in the file COPYING, which
11 .\" * you should have received as part of this distribution. The terms
12 .\" * are also available at http://curl.haxx.se/docs/copyright.html.
13 .\" *
14 .\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 .\" * copies of the Software, and permit persons to whom the Software is
16 .\" * furnished to do so, under the terms of the COPYING file.
17 .\" *
18 .\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 .\" * KIND, either express or implied.
20 .\" *
21 .\" * $Id$
22 .\" **************************************************************************
23 .\"
24 .TH libcurl-tutorial 3 "17 Nov 2008" "libcurl" "libcurl programming"
25 .SH NAME
26 libcurl-tutorial \- libcurl programming tutorial
27 .SH "Objective"
28 This document attempts to describe the general principles and some basic
29 approaches to consider when programming with libcurl. The text will focus
30 mainly on the C interface but might apply fairly well on other interfaces as
31 well as they usually follow the C one pretty closely.
32
33 This document will refer to 'the user' as the person writing the source code
34 that uses libcurl. That would probably be you or someone in your position.
35 What will be generally referred to as 'the program' will be the collected
36 source code that you write that is using libcurl for transfers. The program
37 is outside libcurl and libcurl is outside of the program.
38
39 To get the more details on all options and functions described herein, please
40 refer to their respective man pages.
41
42 .SH "Building"
43 There are many different ways to build C programs. This chapter will assume a
44 UNIX-style build process. If you use a different build system, you can still
45 read this to get general information that may apply to your environment as
46 well.
47 .IP "Compiling the Program"
48 Your compiler needs to know where the libcurl headers are located. Therefore
49 you must set your compiler's include path to point to the directory where you
50 installed them. The 'curl-config'[3] tool can be used to get this information:
51
52 $ curl-config --cflags
53
54 .IP "Linking the Program with libcurl"
55 When having compiled the program, you need to link your object files to create
56 a single executable. For that to succeed, you need to link with libcurl and
57 possibly also with other libraries that libcurl itself depends on. Like the
58 OpenSSL libraries, but even some standard OS libraries may be needed on the
59 command line. To figure out which flags to use, once again the 'curl-config'
60 tool comes to the rescue:
61
62 $ curl-config --libs
63
64 .IP "SSL or Not"
65 libcurl can be built and customized in many ways. One of the things that
66 varies from different libraries and builds is the support for SSL-based
67 transfers, like HTTPS and FTPS. If a supported SSL library was detected
68 properly at build-time, libcurl will be built with SSL support. To figure out
69 if an installed libcurl has been built with SSL support enabled, use
70 \&'curl-config' like this:
71
72 $ curl-config --feature
73
74 And if SSL is supported, the keyword 'SSL' will be written to stdout,
75 possibly together with a few other features that can be on and off on
76 different libcurls.
77
78 See also the "Features libcurl Provides" further down.
79 .IP "autoconf macro"
80 When you write your configure script to detect libcurl and setup variables
81 accordingly, we offer a prewritten macro that probably does everything you
82 need in this area. See docs/libcurl/libcurl.m4 file - it includes docs on how
83 to use it.
84
85 .SH "Portable Code in a Portable World"
86 The people behind libcurl have put a considerable effort to make libcurl work
87 on a large amount of different operating systems and environments.
88
89 You program libcurl the same way on all platforms that libcurl runs on. There
90 are only very few minor considerations that differs. If you just make sure to
91 write your code portable enough, you may very well create yourself a very
92 portable program. libcurl shouldn't stop you from that.
93
94 .SH "Global Preparation"
95 The program must initialize some of the libcurl functionality globally. That
96 means it should be done exactly once, no matter how many times you intend to
97 use the library. Once for your program's entire life time. This is done using
98
99  curl_global_init()
100
101 and it takes one parameter which is a bit pattern that tells libcurl what to
102 initialize. Using \fICURL_GLOBAL_ALL\fP will make it initialize all known
103 internal sub modules, and might be a good default option. The current two bits
104 that are specified are:
105 .RS
106 .IP "CURL_GLOBAL_WIN32"
107 which only does anything on Windows machines. When used on
108 a Windows machine, it'll make libcurl initialize the win32 socket
109 stuff. Without having that initialized properly, your program cannot use
110 sockets properly. You should only do this once for each application, so if
111 your program already does this or of another library in use does it, you
112 should not tell libcurl to do this as well.
113 .IP CURL_GLOBAL_SSL
114 which only does anything on libcurls compiled and built SSL-enabled. On these
115 systems, this will make libcurl initialize the SSL library properly for this
116 application. This is only needed to do once for each application so if your
117 program or another library already does this, this bit should not be needed.
118 .RE
119
120 libcurl has a default protection mechanism that detects if
121 \fIcurl_global_init(3)\fP hasn't been called by the time
122 \fIcurl_easy_perform(3)\fP is called and if that is the case, libcurl runs the
123 function itself with a guessed bit pattern. Please note that depending solely
124 on this is not considered nice nor very good.
125
126 When the program no longer uses libcurl, it should call
127 \fIcurl_global_cleanup(3)\fP, which is the opposite of the init call. It will
128 then do the reversed operations to cleanup the resources the
129 \fIcurl_global_init(3)\fP call initialized.
130
131 Repeated calls to \fIcurl_global_init(3)\fP and \fIcurl_global_cleanup(3)\fP
132 should be avoided. They should only be called once each.
133
134 .SH "Features libcurl Provides"
135 It is considered best-practice to determine libcurl features at run-time
136 rather than at build-time (if possible of course). By calling
137 \fIcurl_version_info(3)\fP and checking out the details of the returned
138 struct, your program can figure out exactly what the currently running libcurl
139 supports.
140
141 .SH "Handle the Easy libcurl"
142 libcurl first introduced the so called easy interface. All operations in the
143 easy interface are prefixed with 'curl_easy'.
144
145 Recent libcurl versions also offer the multi interface. More about that
146 interface, what it is targeted for and how to use it is detailed in a separate
147 chapter further down. You still need to understand the easy interface first,
148 so please continue reading for better understanding.
149
150 To use the easy interface, you must first create yourself an easy handle. You
151 need one handle for each easy session you want to perform. Basically, you
152 should use one handle for every thread you plan to use for transferring. You
153 must never share the same handle in multiple threads.
154
155 Get an easy handle with
156
157  easyhandle = curl_easy_init();
158
159 It returns an easy handle. Using that you proceed to the next step: setting
160 up your preferred actions. A handle is just a logic entity for the upcoming
161 transfer or series of transfers.
162
163 You set properties and options for this handle using
164 \fIcurl_easy_setopt(3)\fP. They control how the subsequent transfer or
165 transfers will be made. Options remain set in the handle until set again to
166 something different. Alas, multiple requests using the same handle will use
167 the same options.
168
169 Many of the options you set in libcurl are "strings", pointers to data
170 terminated with a zero byte. When you set strings with
171 \fIcurl_easy_setopt(3)\fP, libcurl makes its own copy so that they don't
172 need to be kept around in your application after being set[4].
173
174 One of the most basic properties to set in the handle is the URL. You set
175 your preferred URL to transfer with CURLOPT_URL in a manner similar to:
176
177 .nf
178  curl_easy_setopt(handle, CURLOPT_URL, "http://domain.com/");
179 .fi
180
181 Let's assume for a while that you want to receive data as the URL identifies a
182 remote resource you want to get here. Since you write a sort of application
183 that needs this transfer, I assume that you would like to get the data passed
184 to you directly instead of simply getting it passed to stdout. So, you write
185 your own function that matches this prototype:
186
187  size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);
188
189 You tell libcurl to pass all data to this function by issuing a function
190 similar to this:
191
192  curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, write_data);
193
194 You can control what data your function get in the forth argument by setting
195 another property:
196
197  curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, &internal_struct);
198
199 Using that property, you can easily pass local data between your application
200 and the function that gets invoked by libcurl. libcurl itself won't touch the
201 data you pass with \fICURLOPT_WRITEDATA\fP.
202
203 libcurl offers its own default internal callback that'll take care of the data
204 if you don't set the callback with \fICURLOPT_WRITEFUNCTION\fP. It will then
205 simply output the received data to stdout. You can have the default callback
206 write the data to a different file handle by passing a 'FILE *' to a file
207 opened for writing with the \fICURLOPT_WRITEDATA\fP option.
208
209 Now, we need to take a step back and have a deep breath. Here's one of those
210 rare platform-dependent nitpicks. Did you spot it? On some platforms[2],
211 libcurl won't be able to operate on files opened by the program. Thus, if you
212 use the default callback and pass in an open file with
213 \fICURLOPT_WRITEDATA\fP, it will crash. You should therefore avoid this to
214 make your program run fine virtually everywhere.
215
216 (\fICURLOPT_WRITEDATA\fP was formerly known as \fICURLOPT_FILE\fP. Both names
217 still work and do the same thing).
218
219 If you're using libcurl as a win32 DLL, you MUST use the
220 \fICURLOPT_WRITEFUNCTION\fP if you set \fICURLOPT_WRITEDATA\fP - or you will
221 experience crashes.
222
223 There are of course many more options you can set, and we'll get back to a few
224 of them later. Let's instead continue to the actual transfer:
225
226  success = curl_easy_perform(easyhandle);
227
228 \fIcurl_easy_perform(3)\fP will connect to the remote site, do the necessary
229 commands and receive the transfer. Whenever it receives data, it calls the
230 callback function we previously set. The function may get one byte at a time,
231 or it may get many kilobytes at once. libcurl delivers as much as possible as
232 often as possible. Your callback function should return the number of bytes it
233 \&"took care of". If that is not the exact same amount of bytes that was
234 passed to it, libcurl will abort the operation and return with an error code.
235
236 When the transfer is complete, the function returns a return code that informs
237 you if it succeeded in its mission or not. If a return code isn't enough for
238 you, you can use the CURLOPT_ERRORBUFFER to point libcurl to a buffer of yours
239 where it'll store a human readable error message as well.
240
241 If you then want to transfer another file, the handle is ready to be used
242 again. Mind you, it is even preferred that you re-use an existing handle if
243 you intend to make another transfer. libcurl will then attempt to re-use the
244 previous connection.
245
246 For some protocols, downloading a file can involve a complicated process of
247 logging in, setting the transfer mode, changing the current directory and
248 finally transferring the file data. libcurl takes care of all that
249 complication for you. Given simply the URL to a file, libcurl will take care
250 of all the details needed to get the file moved from one machine to another.
251
252 .SH "Multi-threading Issues"
253 The first basic rule is that you must \fBnever\fP share a libcurl handle (be
254 it easy or multi or whatever) between multiple threads. Only use one handle in
255 one thread at a time.
256
257 libcurl is completely thread safe, except for two issues: signals and SSL/TLS
258 handlers. Signals are used for timing out name resolves (during DNS lookup) -
259 when built without c-ares support and not on Windows..
260
261 If you are accessing HTTPS or FTPS URLs in a multi-threaded manner, you are
262 then of course using the underlying SSL library multi-threaded and those libs
263 might have their own requirements on this issue. Basically, you need to
264 provide one or two functions to allow it to function properly. For all
265 details, see this:
266
267 OpenSSL
268
269  http://www.openssl.org/docs/crypto/threads.html#DESCRIPTION
270
271 GnuTLS
272
273  http://www.gnu.org/software/gnutls/manual/html_node/Multi_002dthreaded-applications.html
274
275 NSS
276  
277  is claimed to be thread-safe already without anything required
278
279 yassl
280
281  Required actions unknown
282
283 When using multiple threads you should set the CURLOPT_NOSIGNAL option to 1
284 for all handles. Everything will or might work fine except that timeouts are
285 not honored during the DNS lookup - which you can work around by building
286 libcurl with c-ares support. c-ares is a library that provides asynchronous
287 name resolves. Unfortunately, c-ares does not yet fully support IPv6. On some
288 platforms, libcurl simply will not function properly multi-threaded unless
289 this option is set.
290
291 Also, note that CURLOPT_DNS_USE_GLOBAL_CACHE is not thread-safe.
292
293 .SH "When It Doesn't Work"
294 There will always be times when the transfer fails for some reason. You might
295 have set the wrong libcurl option or misunderstood what the libcurl option
296 actually does, or the remote server might return non-standard replies that
297 confuse the library which then confuses your program.
298
299 There's one golden rule when these things occur: set the CURLOPT_VERBOSE
300 option to 1. It'll cause the library to spew out the entire protocol
301 details it sends, some internal info and some received protocol data as well
302 (especially when using FTP). If you're using HTTP, adding the headers in the
303 received output to study is also a clever way to get a better understanding
304 why the server behaves the way it does. Include headers in the normal body
305 output with CURLOPT_HEADER set 1.
306
307 Of course there are bugs left. We need to get to know about them to be able
308 to fix them, so we're quite dependent on your bug reports! When you do report
309 suspected bugs in libcurl, please include as much details you possibly can: a
310 protocol dump that CURLOPT_VERBOSE produces, library version, as much as
311 possible of your code that uses libcurl, operating system name and version,
312 compiler name and version etc.
313
314 If CURLOPT_VERBOSE is not enough, you increase the level of debug data your
315 application receive by using the CURLOPT_DEBUGFUNCTION.
316
317 Getting some in-depth knowledge about the protocols involved is never wrong,
318 and if you're trying to do funny things, you might very well understand
319 libcurl and how to use it better if you study the appropriate RFC documents
320 at least briefly.
321
322 .SH "Upload Data to a Remote Site"
323 libcurl tries to keep a protocol independent approach to most transfers, thus
324 uploading to a remote FTP site is very similar to uploading data to a HTTP
325 server with a PUT request.
326
327 Of course, first you either create an easy handle or you re-use one existing
328 one. Then you set the URL to operate on just like before. This is the remote
329 URL, that we now will upload.
330
331 Since we write an application, we most likely want libcurl to get the upload
332 data by asking us for it. To make it do that, we set the read callback and
333 the custom pointer libcurl will pass to our read callback. The read callback
334 should have a prototype similar to:
335
336  size_t function(char *bufptr, size_t size, size_t nitems, void *userp);
337
338 Where bufptr is the pointer to a buffer we fill in with data to upload and
339 size*nitems is the size of the buffer and therefore also the maximum amount
340 of data we can return to libcurl in this call. The 'userp' pointer is the
341 custom pointer we set to point to a struct of ours to pass private data
342 between the application and the callback.
343
344  curl_easy_setopt(easyhandle, CURLOPT_READFUNCTION, read_function);
345
346  curl_easy_setopt(easyhandle, CURLOPT_READDATA, &filedata);
347
348 Tell libcurl that we want to upload:
349
350  curl_easy_setopt(easyhandle, CURLOPT_UPLOAD, 1L);
351
352 A few protocols won't behave properly when uploads are done without any prior
353 knowledge of the expected file size. So, set the upload file size using the
354 CURLOPT_INFILESIZE_LARGE for all known file sizes like this[1]:
355
356 .nf
357  /* in this example, file_size must be an curl_off_t variable */
358  curl_easy_setopt(easyhandle, CURLOPT_INFILESIZE_LARGE, file_size);
359 .fi
360
361 When you call \fIcurl_easy_perform(3)\fP this time, it'll perform all the
362 necessary operations and when it has invoked the upload it'll call your
363 supplied callback to get the data to upload. The program should return as much
364 data as possible in every invoke, as that is likely to make the upload perform
365 as fast as possible. The callback should return the number of bytes it wrote
366 in the buffer. Returning 0 will signal the end of the upload.
367
368 .SH "Passwords"
369 Many protocols use or even require that user name and password are provided
370 to be able to download or upload the data of your choice. libcurl offers
371 several ways to specify them.
372
373 Most protocols support that you specify the name and password in the URL
374 itself. libcurl will detect this and use them accordingly. This is written
375 like this:
376
377  protocol://user:password@example.com/path/
378
379 If you need any odd letters in your user name or password, you should enter
380 them URL encoded, as %XX where XX is a two-digit hexadecimal number.
381
382 libcurl also provides options to set various passwords. The user name and
383 password as shown embedded in the URL can instead get set with the
384 CURLOPT_USERPWD option. The argument passed to libcurl should be a char * to
385 a string in the format "user:password:". In a manner like this:
386
387  curl_easy_setopt(easyhandle, CURLOPT_USERPWD, "myname:thesecret");
388
389 Another case where name and password might be needed at times, is for those
390 users who need to authenticate themselves to a proxy they use. libcurl offers
391 another option for this, the CURLOPT_PROXYUSERPWD. It is used quite similar
392 to the CURLOPT_USERPWD option like this:
393
394  curl_easy_setopt(easyhandle, CURLOPT_PROXYUSERPWD, "myname:thesecret");
395  
396 There's a long time UNIX "standard" way of storing ftp user names and
397 passwords, namely in the $HOME/.netrc file. The file should be made private
398 so that only the user may read it (see also the "Security Considerations"
399 chapter), as it might contain the password in plain text. libcurl has the
400 ability to use this file to figure out what set of user name and password to
401 use for a particular host. As an extension to the normal functionality,
402 libcurl also supports this file for non-FTP protocols such as HTTP. To make
403 curl use this file, use the CURLOPT_NETRC option:
404
405  curl_easy_setopt(easyhandle, CURLOPT_NETRC, 1L);
406
407 And a very basic example of how such a .netrc file may look like:
408
409 .nf
410  machine myhost.mydomain.com
411  login userlogin
412  password secretword
413 .fi
414
415 All these examples have been cases where the password has been optional, or
416 at least you could leave it out and have libcurl attempt to do its job
417 without it. There are times when the password isn't optional, like when
418 you're using an SSL private key for secure transfers.
419
420 To pass the known private key password to libcurl:
421
422  curl_easy_setopt(easyhandle, CURLOPT_KEYPASSWD, "keypassword");
423
424 .SH "HTTP Authentication"
425 The previous chapter showed how to set user name and password for getting
426 URLs that require authentication. When using the HTTP protocol, there are
427 many different ways a client can provide those credentials to the server and
428 you can control what way libcurl will (attempt to) use. The default HTTP
429 authentication method is called 'Basic', which is sending the name and
430 password in clear-text in the HTTP request, base64-encoded. This is insecure.
431
432 At the time of this writing libcurl can be built to use: Basic, Digest, NTLM,
433 Negotiate, GSS-Negotiate and SPNEGO. You can tell libcurl which one to use
434 with CURLOPT_HTTPAUTH as in:
435
436  curl_easy_setopt(easyhandle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
437
438 And when you send authentication to a proxy, you can also set authentication
439 type the same way but instead with CURLOPT_PROXYAUTH:
440
441  curl_easy_setopt(easyhandle, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
442
443 Both these options allow you to set multiple types (by ORing them together),
444 to make libcurl pick the most secure one out of the types the server/proxy
445 claims to support. This method does however add a round-trip since libcurl
446 must first ask the server what it supports:
447
448  curl_easy_setopt(easyhandle, CURLOPT_HTTPAUTH,
449  CURLAUTH_DIGEST|CURLAUTH_BASIC);
450
451 For convenience, you can use the 'CURLAUTH_ANY' define (instead of a list
452 with specific types) which allows libcurl to use whatever method it wants.
453
454 When asking for multiple types, libcurl will pick the available one it
455 considers "best" in its own internal order of preference.
456
457 .SH "HTTP POSTing"
458 We get many questions regarding how to issue HTTP POSTs with libcurl the
459 proper way. This chapter will thus include examples using both different
460 versions of HTTP POST that libcurl supports.
461
462 The first version is the simple POST, the most common version, that most HTML
463 pages using the <form> tag uses. We provide a pointer to the data and tell
464 libcurl to post it all to the remote site:
465
466 .nf
467     char *data="name=daniel&project=curl";
468     curl_easy_setopt(easyhandle, CURLOPT_POSTFIELDS, data);
469     curl_easy_setopt(easyhandle, CURLOPT_URL, "http://posthere.com/");
470
471     curl_easy_perform(easyhandle); /* post away! */
472 .fi
473
474 Simple enough, huh? Since you set the POST options with the
475 CURLOPT_POSTFIELDS, this automatically switches the handle to use POST in the
476 upcoming request.
477
478 Ok, so what if you want to post binary data that also requires you to set the
479 Content-Type: header of the post? Well, binary posts prevents libcurl from
480 being able to do strlen() on the data to figure out the size, so therefore we
481 must tell libcurl the size of the post data. Setting headers in libcurl
482 requests are done in a generic way, by building a list of our own headers and
483 then passing that list to libcurl.
484
485 .nf
486  struct curl_slist *headers=NULL;
487  headers = curl_slist_append(headers, "Content-Type: text/xml");
488
489  /* post binary data */
490  curl_easy_setopt(easyhandle, CURLOPT_POSTFIELDS, binaryptr);
491
492  /* set the size of the postfields data */
493  curl_easy_setopt(easyhandle, CURLOPT_POSTFIELDSIZE, 23L);
494
495  /* pass our list of custom made headers */
496  curl_easy_setopt(easyhandle, CURLOPT_HTTPHEADER, headers);
497
498  curl_easy_perform(easyhandle); /* post away! */
499
500  curl_slist_free_all(headers); /* free the header list */
501 .fi
502
503 While the simple examples above cover the majority of all cases where HTTP
504 POST operations are required, they don't do multi-part formposts. Multi-part
505 formposts were introduced as a better way to post (possibly large) binary data
506 and was first documented in the RFC1867. They're called multi-part because
507 they're built by a chain of parts, each being a single unit. Each part has its
508 own name and contents. You can in fact create and post a multi-part formpost
509 with the regular libcurl POST support described above, but that would require
510 that you build a formpost yourself and provide to libcurl. To make that
511 easier, libcurl provides \fIcurl_formadd(3)\fP. Using this function, you add
512 parts to the form. When you're done adding parts, you post the whole form.
513
514 The following example sets two simple text parts with plain textual contents,
515 and then a file with binary contents and upload the whole thing.
516
517 .nf
518  struct curl_httppost *post=NULL;
519  struct curl_httppost *last=NULL;
520  curl_formadd(&post, &last,
521               CURLFORM_COPYNAME, "name",
522               CURLFORM_COPYCONTENTS, "daniel", CURLFORM_END);
523  curl_formadd(&post, &last,
524               CURLFORM_COPYNAME, "project",
525               CURLFORM_COPYCONTENTS, "curl", CURLFORM_END);
526  curl_formadd(&post, &last,
527               CURLFORM_COPYNAME, "logotype-image",
528               CURLFORM_FILECONTENT, "curl.png", CURLFORM_END);
529
530  /* Set the form info */
531  curl_easy_setopt(easyhandle, CURLOPT_HTTPPOST, post);
532
533  curl_easy_perform(easyhandle); /* post away! */
534
535  /* free the post data again */
536  curl_formfree(post);
537 .fi
538
539 Multipart formposts are chains of parts using MIME-style separators and
540 headers. It means that each one of these separate parts get a few headers set
541 that describe the individual content-type, size etc. To enable your
542 application to handicraft this formpost even more, libcurl allows you to
543 supply your own set of custom headers to such an individual form part. You can
544 of course supply headers to as many parts you like, but this little example
545 will show how you set headers to one specific part when you add that to the
546 post handle:
547
548 .nf
549  struct curl_slist *headers=NULL;
550  headers = curl_slist_append(headers, "Content-Type: text/xml");
551
552  curl_formadd(&post, &last,
553               CURLFORM_COPYNAME, "logotype-image",
554               CURLFORM_FILECONTENT, "curl.xml",
555               CURLFORM_CONTENTHEADER, headers,
556               CURLFORM_END);
557
558  curl_easy_perform(easyhandle); /* post away! */
559
560  curl_formfree(post); /* free post */
561  curl_slist_free_all(post); /* free custom header list */
562 .fi
563
564 Since all options on an easyhandle are "sticky", they remain the same until
565 changed even if you do call \fIcurl_easy_perform(3)\fP, you may need to tell
566 curl to go back to a plain GET request if you intend to do such a one as your
567 next request. You force an easyhandle to back to GET by using the
568 CURLOPT_HTTPGET option:
569
570  curl_easy_setopt(easyhandle, CURLOPT_HTTPGET, 1L);
571
572 Just setting CURLOPT_POSTFIELDS to "" or NULL will *not* stop libcurl from
573 doing a POST. It will just make it POST without any data to send!
574
575 .SH "Showing Progress"
576
577 For historical and traditional reasons, libcurl has a built-in progress meter
578 that can be switched on and then makes it presents a progress meter in your
579 terminal.
580
581 Switch on the progress meter by, oddly enough, set CURLOPT_NOPROGRESS to
582 zero. This option is set to 1 by default.
583
584 For most applications however, the built-in progress meter is useless and
585 what instead is interesting is the ability to specify a progress
586 callback. The function pointer you pass to libcurl will then be called on
587 irregular intervals with information about the current transfer.
588
589 Set the progress callback by using CURLOPT_PROGRESSFUNCTION. And pass a
590 pointer to a function that matches this prototype:
591
592 .nf
593  int progress_callback(void *clientp,
594                        double dltotal,
595                        double dlnow,
596                        double ultotal,
597                        double ulnow);
598 .fi
599
600 If any of the input arguments is unknown, a 0 will be passed. The first
601 argument, the 'clientp' is the pointer you pass to libcurl with
602 CURLOPT_PROGRESSDATA. libcurl won't touch it.
603
604 .SH "libcurl with C++"
605
606 There's basically only one thing to keep in mind when using C++ instead of C
607 when interfacing libcurl:
608
609 The callbacks CANNOT be non-static class member functions
610
611 Example C++ code:
612
613 .nf
614 class AClass {
615     static size_t write_data(void *ptr, size_t size, size_t nmemb,
616                              void *ourpointer)
617     {
618       /* do what you want with the data */
619     }
620  }
621 .fi
622
623 .SH "Proxies"
624
625 What "proxy" means according to Merriam-Webster: "a person authorized to act
626 for another" but also "the agency, function, or office of a deputy who acts as
627 a substitute for another".
628
629 Proxies are exceedingly common these days. Companies often only offer Internet
630 access to employees through their proxies. Network clients or user-agents ask
631 the proxy for documents, the proxy does the actual request and then it returns
632 them.
633
634 libcurl supports SOCKS and HTTP proxies. When a given URL is wanted, libcurl
635 will ask the proxy for it instead of trying to connect to the actual host
636 identified in the URL.
637
638 If you're using a SOCKS proxy, you may find that libcurl doesn't quite support
639 all operations through it.
640
641 For HTTP proxies: the fact that the proxy is a HTTP proxy puts certain
642 restrictions on what can actually happen. A requested URL that might not be a
643 HTTP URL will be still be passed to the HTTP proxy to deliver back to
644 libcurl. This happens transparently, and an application may not need to
645 know. I say "may", because at times it is very important to understand that
646 all operations over a HTTP proxy is using the HTTP protocol. For example, you
647 can't invoke your own custom FTP commands or even proper FTP directory
648 listings.
649
650 .IP "Proxy Options"
651
652 To tell libcurl to use a proxy at a given port number:
653
654  curl_easy_setopt(easyhandle, CURLOPT_PROXY, "proxy-host.com:8080");
655
656 Some proxies require user authentication before allowing a request, and you
657 pass that information similar to this:
658
659  curl_easy_setopt(easyhandle, CURLOPT_PROXYUSERPWD, "user:password");
660
661 If you want to, you can specify the host name only in the CURLOPT_PROXY
662 option, and set the port number separately with CURLOPT_PROXYPORT.
663
664 Tell libcurl what kind of proxy it is with CURLOPT_PROXYTYPE (if not, it will
665 default to assume a HTTP proxy):
666
667  curl_easy_setopt(easyhandle, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
668
669 .IP "Environment Variables"
670
671 libcurl automatically checks and uses a set of environment variables to
672 know what proxies to use for certain protocols. The names of the variables
673 are following an ancient de facto standard and are built up as
674 "[protocol]_proxy" (note the lower casing). Which makes the variable
675 'http_proxy' checked for a name of a proxy to use when the input URL is
676 HTTP. Following the same rule, the variable named 'ftp_proxy' is checked
677 for FTP URLs. Again, the proxies are always HTTP proxies, the different
678 names of the variables simply allows different HTTP proxies to be used.
679
680 The proxy environment variable contents should be in the format
681 \&"[protocol://][user:password@]machine[:port]". Where the protocol:// part is
682 simply ignored if present (so http://proxy and bluerk://proxy will do the
683 same) and the optional port number specifies on which port the proxy operates
684 on the host. If not specified, the internal default port number will be used
685 and that is most likely *not* the one you would like it to be.
686
687 There are two special environment variables. 'all_proxy' is what sets proxy
688 for any URL in case the protocol specific variable wasn't set, and
689 \&'no_proxy' defines a list of hosts that should not use a proxy even though a
690 variable may say so. If 'no_proxy' is a plain asterisk ("*") it matches all
691 hosts.
692
693 To explicitly disable libcurl's checking for and using the proxy environment
694 variables, set the proxy name to "" - an empty string - with CURLOPT_PROXY.
695 .IP "SSL and Proxies"
696
697 SSL is for secure point-to-point connections. This involves strong encryption
698 and similar things, which effectively makes it impossible for a proxy to
699 operate as a "man in between" which the proxy's task is, as previously
700 discussed. Instead, the only way to have SSL work over a HTTP proxy is to ask
701 the proxy to tunnel trough everything without being able to check or fiddle
702 with the traffic.
703
704 Opening an SSL connection over a HTTP proxy is therefor a matter of asking the
705 proxy for a straight connection to the target host on a specified port. This
706 is made with the HTTP request CONNECT. ("please mr proxy, connect me to that
707 remote host").
708
709 Because of the nature of this operation, where the proxy has no idea what kind
710 of data that is passed in and out through this tunnel, this breaks some of the
711 very few advantages that come from using a proxy, such as caching.  Many
712 organizations prevent this kind of tunneling to other destination port numbers
713 than 443 (which is the default HTTPS port number).
714
715 .IP "Tunneling Through Proxy"
716 As explained above, tunneling is required for SSL to work and often even
717 restricted to the operation intended for SSL; HTTPS.
718
719 This is however not the only time proxy-tunneling might offer benefits to
720 you or your application.
721
722 As tunneling opens a direct connection from your application to the remote
723 machine, it suddenly also re-introduces the ability to do non-HTTP
724 operations over a HTTP proxy. You can in fact use things such as FTP
725 upload or FTP custom commands this way.
726
727 Again, this is often prevented by the administrators of proxies and is
728 rarely allowed.
729
730 Tell libcurl to use proxy tunneling like this:
731
732  curl_easy_setopt(easyhandle, CURLOPT_HTTPPROXYTUNNEL, 1L);
733
734 In fact, there might even be times when you want to do plain HTTP
735 operations using a tunnel like this, as it then enables you to operate on
736 the remote server instead of asking the proxy to do so. libcurl will not
737 stand in the way for such innovative actions either!
738
739 .IP "Proxy Auto-Config"
740
741 Netscape first came up with this. It is basically a web page (usually using a
742 \&.pac extension) with a javascript that when executed by the browser with the
743 requested URL as input, returns information to the browser on how to connect
744 to the URL. The returned information might be "DIRECT" (which means no proxy
745 should be used), "PROXY host:port" (to tell the browser where the proxy for
746 this particular URL is) or "SOCKS host:port" (to direct the browser to a SOCKS
747 proxy).
748
749 libcurl has no means to interpret or evaluate javascript and thus it doesn't
750 support this. If you get yourself in a position where you face this nasty
751 invention, the following advice have been mentioned and used in the past:
752
753 - Depending on the javascript complexity, write up a script that translates it
754 to another language and execute that.
755
756 - Read the javascript code and rewrite the same logic in another language.
757
758 - Implement a javascript interpreted, people have successfully used the
759 Mozilla javascript engine in the past.
760
761 - Ask your admins to stop this, for a static proxy setup or similar.
762
763 .SH "Persistence Is The Way to Happiness"
764
765 Re-cycling the same easy handle several times when doing multiple requests is
766 the way to go.
767
768 After each single \fIcurl_easy_perform(3)\fP operation, libcurl will keep the
769 connection alive and open. A subsequent request using the same easy handle to
770 the same host might just be able to use the already open connection! This
771 reduces network impact a lot.
772
773 Even if the connection is dropped, all connections involving SSL to the same
774 host again, will benefit from libcurl's session ID cache that drastically
775 reduces re-connection time.
776
777 FTP connections that are kept alive saves a lot of time, as the command-
778 response round-trips are skipped, and also you don't risk getting blocked
779 without permission to login again like on many FTP servers only allowing N
780 persons to be logged in at the same time.
781
782 libcurl caches DNS name resolving results, to make lookups of a previously
783 looked up name a lot faster.
784
785 Other interesting details that improve performance for subsequent requests
786 may also be added in the future.
787
788 Each easy handle will attempt to keep the last few connections alive for a
789 while in case they are to be used again. You can set the size of this "cache"
790 with the CURLOPT_MAXCONNECTS option. Default is 5. It is very seldom any
791 point in changing this value, and if you think of changing this it is often
792 just a matter of thinking again.
793
794 To force your upcoming request to not use an already existing connection (it
795 will even close one first if there happens to be one alive to the same host
796 you're about to operate on), you can do that by setting CURLOPT_FRESH_CONNECT
797 to 1. In a similar spirit, you can also forbid the upcoming request to be
798 "lying" around and possibly get re-used after the request by setting
799 CURLOPT_FORBID_REUSE to 1.
800
801 .SH "HTTP Headers Used by libcurl"
802 When you use libcurl to do HTTP requests, it'll pass along a series of headers
803 automatically. It might be good for you to know and understand these ones. You
804 can replace or remove them by using the CURLOPT_HTTPHEADER option.
805
806 .IP "Host"
807 This header is required by HTTP 1.1 and even many 1.0 servers and should be
808 the name of the server we want to talk to. This includes the port number if
809 anything but default.
810
811 .IP "Pragma"
812 \&"no-cache". Tells a possible proxy to not grab a copy from the cache but to
813 fetch a fresh one.
814
815 .IP "Accept"
816 \&"*/*".
817
818 .IP "Expect"
819 When doing POST requests, libcurl sets this header to \&"100-continue" to ask
820 the server for an "OK" message before it proceeds with sending the data part
821 of the post. If the POSTed data amount is deemed "small", libcurl will not use
822 this header.
823
824 .SH "Customizing Operations"
825 There is an ongoing development today where more and more protocols are built
826 upon HTTP for transport. This has obvious benefits as HTTP is a tested and
827 reliable protocol that is widely deployed and have excellent proxy-support.
828
829 When you use one of these protocols, and even when doing other kinds of
830 programming you may need to change the traditional HTTP (or FTP or...)
831 manners. You may need to change words, headers or various data.
832
833 libcurl is your friend here too.
834
835 .IP CUSTOMREQUEST
836 If just changing the actual HTTP request keyword is what you want, like when
837 GET, HEAD or POST is not good enough for you, CURLOPT_CUSTOMREQUEST is there
838 for you. It is very simple to use:
839
840  curl_easy_setopt(easyhandle, CURLOPT_CUSTOMREQUEST, "MYOWNRUQUEST");
841
842 When using the custom request, you change the request keyword of the actual
843 request you are performing. Thus, by default you make GET request but you can
844 also make a POST operation (as described before) and then replace the POST
845 keyword if you want to. You're the boss.
846
847 .IP "Modify Headers"
848 HTTP-like protocols pass a series of headers to the server when doing the
849 request, and you're free to pass any amount of extra headers that you
850 think fit. Adding headers are this easy:
851
852 .nf
853  struct curl_slist *headers=NULL; /* init to NULL is important */
854
855  headers = curl_slist_append(headers, "Hey-server-hey: how are you?");
856  headers = curl_slist_append(headers, "X-silly-content: yes");
857
858  /* pass our list of custom made headers */
859  curl_easy_setopt(easyhandle, CURLOPT_HTTPHEADER, headers);
860
861  curl_easy_perform(easyhandle); /* transfer http */
862
863  curl_slist_free_all(headers); /* free the header list */
864 .fi
865
866 \&... and if you think some of the internally generated headers, such as
867 Accept: or Host: don't contain the data you want them to contain, you can
868 replace them by simply setting them too:
869
870 .nf
871  headers = curl_slist_append(headers, "Accept: Agent-007");
872  headers = curl_slist_append(headers, "Host: munged.host.line");
873 .fi
874
875 .IP "Delete Headers"
876 If you replace an existing header with one with no contents, you will prevent
877 the header from being sent. Like if you want to completely prevent the
878 \&"Accept:" header to be sent, you can disable it with code similar to this:
879
880  headers = curl_slist_append(headers, "Accept:");
881
882 Both replacing and canceling internal headers should be done with careful
883 consideration and you should be aware that you may violate the HTTP protocol
884 when doing so.
885
886 .IP "Enforcing chunked transfer-encoding"
887
888 By making sure a request uses the custom header "Transfer-Encoding: chunked"
889 when doing a non-GET HTTP operation, libcurl will switch over to "chunked"
890 upload, even though the size of the data to upload might be known. By default,
891 libcurl usually switches over to chunked upload automatically if the upload
892 data size is unknown.
893
894 .IP "HTTP Version"
895
896 All HTTP requests includes the version number to tell the server which version
897 we support. libcurl speak HTTP 1.1 by default. Some very old servers don't
898 like getting 1.1-requests and when dealing with stubborn old things like that,
899 you can tell libcurl to use 1.0 instead by doing something like this:
900
901  curl_easy_setopt(easyhandle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
902
903 .IP "FTP Custom Commands"
904
905 Not all protocols are HTTP-like, and thus the above may not help you when
906 you want to make for example your FTP transfers to behave differently.
907
908 Sending custom commands to a FTP server means that you need to send the
909 commands exactly as the FTP server expects them (RFC959 is a good guide
910 here), and you can only use commands that work on the control-connection
911 alone. All kinds of commands that requires data interchange and thus needs
912 a data-connection must be left to libcurl's own judgment. Also be aware
913 that libcurl will do its very best to change directory to the target
914 directory before doing any transfer, so if you change directory (with CWD
915 or similar) you might confuse libcurl and then it might not attempt to
916 transfer the file in the correct remote directory.
917
918 A little example that deletes a given file before an operation:
919
920 .nf
921  headers = curl_slist_append(headers, "DELE file-to-remove");
922
923  /* pass the list of custom commands to the handle */
924  curl_easy_setopt(easyhandle, CURLOPT_QUOTE, headers);
925
926  curl_easy_perform(easyhandle); /* transfer ftp data! */
927
928  curl_slist_free_all(headers); /* free the header list */
929 .fi
930
931 If you would instead want this operation (or chain of operations) to happen
932 _after_ the data transfer took place the option to \fIcurl_easy_setopt(3)\fP
933 would instead be called CURLOPT_POSTQUOTE and used the exact same way.
934
935 The custom FTP command will be issued to the server in the same order they are
936 added to the list, and if a command gets an error code returned back from the
937 server, no more commands will be issued and libcurl will bail out with an
938 error code (CURLE_QUOTE_ERROR). Note that if you use CURLOPT_QUOTE to send
939 commands before a transfer, no transfer will actually take place when a quote
940 command has failed.
941
942 If you set the CURLOPT_HEADER to 1, you will tell libcurl to get
943 information about the target file and output "headers" about it. The headers
944 will be in "HTTP-style", looking like they do in HTTP.
945
946 The option to enable headers or to run custom FTP commands may be useful to
947 combine with CURLOPT_NOBODY. If this option is set, no actual file content
948 transfer will be performed.
949
950 .IP "FTP Custom CUSTOMREQUEST"
951 If you do what list the contents of a FTP directory using your own defined FTP
952 command, CURLOPT_CUSTOMREQUEST will do just that. "NLST" is the default one
953 for listing directories but you're free to pass in your idea of a good
954 alternative.
955
956 .SH "Cookies Without Chocolate Chips"
957 In the HTTP sense, a cookie is a name with an associated value. A server sends
958 the name and value to the client, and expects it to get sent back on every
959 subsequent request to the server that matches the particular conditions
960 set. The conditions include that the domain name and path match and that the
961 cookie hasn't become too old.
962
963 In real-world cases, servers send new cookies to replace existing one to
964 update them. Server use cookies to "track" users and to keep "sessions".
965
966 Cookies are sent from server to clients with the header Set-Cookie: and
967 they're sent from clients to servers with the Cookie: header.
968
969 To just send whatever cookie you want to a server, you can use CURLOPT_COOKIE
970 to set a cookie string like this:
971
972  curl_easy_setopt(easyhandle, CURLOPT_COOKIE, "name1=var1; name2=var2;");
973
974 In many cases, that is not enough. You might want to dynamically save
975 whatever cookies the remote server passes to you, and make sure those cookies
976 are then use accordingly on later requests.
977
978 One way to do this, is to save all headers you receive in a plain file and
979 when you make a request, you tell libcurl to read the previous headers to
980 figure out which cookies to use. Set header file to read cookies from with
981 CURLOPT_COOKIEFILE.
982
983 The CURLOPT_COOKIEFILE option also automatically enables the cookie parser in
984 libcurl. Until the cookie parser is enabled, libcurl will not parse or
985 understand incoming cookies and they will just be ignored. However, when the
986 parser is enabled the cookies will be understood and the cookies will be kept
987 in memory and used properly in subsequent requests when the same handle is
988 used. Many times this is enough, and you may not have to save the cookies to
989 disk at all. Note that the file you specify to CURLOPT_COOKIEFILE doesn't
990 have to exist to enable the parser, so a common way to just enable the parser
991 and not read able might be to use a file name you know doesn't exist.
992
993 If you rather use existing cookies that you've previously received with your
994 Netscape or Mozilla browsers, you can make libcurl use that cookie file as
995 input. The CURLOPT_COOKIEFILE is used for that too, as libcurl will
996 automatically find out what kind of file it is and act accordingly.
997
998 The perhaps most advanced cookie operation libcurl offers, is saving the
999 entire internal cookie state back into a Netscape/Mozilla formatted cookie
1000 file. We call that the cookie-jar. When you set a file name with
1001 CURLOPT_COOKIEJAR, that file name will be created and all received cookies
1002 will be stored in it when \fIcurl_easy_cleanup(3)\fP is called. This enabled
1003 cookies to get passed on properly between multiple handles without any
1004 information getting lost.
1005
1006 .SH "FTP Peculiarities We Need"
1007
1008 FTP transfers use a second TCP/IP connection for the data transfer. This is
1009 usually a fact you can forget and ignore but at times this fact will come
1010 back to haunt you. libcurl offers several different ways to custom how the
1011 second connection is being made.
1012
1013 libcurl can either connect to the server a second time or tell the server to
1014 connect back to it. The first option is the default and it is also what works
1015 best for all the people behind firewalls, NATs or IP-masquerading setups.
1016 libcurl then tells the server to open up a new port and wait for a second
1017 connection. This is by default attempted with EPSV first, and if that doesn't
1018 work it tries PASV instead. (EPSV is an extension to the original FTP spec
1019 and does not exist nor work on all FTP servers.)
1020
1021 You can prevent libcurl from first trying the EPSV command by setting
1022 CURLOPT_FTP_USE_EPSV to zero.
1023
1024 In some cases, you will prefer to have the server connect back to you for the
1025 second connection. This might be when the server is perhaps behind a firewall
1026 or something and only allows connections on a single port. libcurl then
1027 informs the remote server which IP address and port number to connect to.
1028 This is made with the CURLOPT_FTPPORT option. If you set it to "-", libcurl
1029 will use your system's "default IP address". If you want to use a particular
1030 IP, you can set the full IP address, a host name to resolve to an IP address
1031 or even a local network interface name that libcurl will get the IP address
1032 from.
1033
1034 When doing the "PORT" approach, libcurl will attempt to use the EPRT and the
1035 LPRT before trying PORT, as they work with more protocols. You can disable
1036 this behavior by setting CURLOPT_FTP_USE_EPRT to zero.
1037
1038 .SH "Headers Equal Fun"
1039
1040 Some protocols provide "headers", meta-data separated from the normal
1041 data. These headers are by default not included in the normal data stream,
1042 but you can make them appear in the data stream by setting CURLOPT_HEADER to
1043 1.
1044
1045 What might be even more useful, is libcurl's ability to separate the headers
1046 from the data and thus make the callbacks differ. You can for example set a
1047 different pointer to pass to the ordinary write callback by setting
1048 CURLOPT_WRITEHEADER.
1049
1050 Or, you can set an entirely separate function to receive the headers, by
1051 using CURLOPT_HEADERFUNCTION.
1052
1053 The headers are passed to the callback function one by one, and you can
1054 depend on that fact. It makes it easier for you to add custom header parsers
1055 etc.
1056
1057 \&"Headers" for FTP transfers equal all the FTP server responses. They aren't
1058 actually true headers, but in this case we pretend they are! ;-)
1059
1060 .SH "Post Transfer Information"
1061
1062  [ curl_easy_getinfo ]
1063
1064 .SH "Security Considerations"
1065
1066 libcurl is in itself not insecure. If used the right way, you can use libcurl
1067 to transfer data pretty safely.
1068
1069 There are of course many things to consider that may loosen up this
1070 situation:
1071
1072 .IP "Command Lines"
1073 If you use a command line tool (such as curl) that uses libcurl, and you give
1074 option to the tool on the command line those options can very likely get read
1075 by other users of your system when they use 'ps' or other tools to list
1076 currently running processes.
1077
1078 To avoid this problem, never feed sensitive things to programs using command
1079 line options.
1080
1081 .IP ".netrc"
1082 \&.netrc is a pretty handy file/feature that allows you to login quickly and
1083 automatically to frequently visited sites. The file contains passwords in
1084 clear text and is a real security risk. In some cases, your .netrc is also
1085 stored in a home directory that is NFS mounted or used on another network
1086 based file system, so the clear text password will fly through your network
1087 every time anyone reads that file!
1088
1089 To avoid this problem, don't use .netrc files and never store passwords in
1090 plain text anywhere.
1091
1092 .IP "Clear Text Passwords"
1093 Many of the protocols libcurl supports send name and password unencrypted as
1094 clear text (HTTP Basic authentication, FTP, TELNET etc). It is very easy for
1095 anyone on your network or a network nearby yours, to just fire up a network
1096 analyzer tool and eavesdrop on your passwords. Don't let the fact that HTTP
1097 uses base64 encoded passwords fool you. They may not look readable at a first
1098 glance, but they very easily "deciphered" by anyone within seconds.
1099
1100 To avoid this problem, use protocols that don't let snoopers see your
1101 password: HTTPS, FTPS and FTP-kerberos are a few examples. HTTP Digest
1102 authentication allows this too, but isn't supported by libcurl as of this
1103 writing.
1104
1105 .IP "Showing What You Do"
1106 On a related issue, be aware that even in situations like when you have
1107 problems with libcurl and ask someone for help, everything you reveal in order
1108 to get best possible help might also impose certain security related
1109 risks. Host names, user names, paths, operating system specifics etc (not to
1110 mention passwords of course) may in fact be used by intruders to gain
1111 additional information of a potential target.
1112
1113 To avoid this problem, you must of course use your common sense. Often, you
1114 can just edit out the sensitive data or just search/replace your true
1115 information with faked data.
1116
1117 .SH "Multiple Transfers Using the multi Interface"
1118
1119 The easy interface as described in detail in this document is a synchronous
1120 interface that transfers one file at a time and doesn't return until its
1121 done.
1122
1123 The multi interface on the other hand, allows your program to transfer
1124 multiple files in both directions at the same time, without forcing you
1125 to use multiple threads.  The name might make it seem that the multi
1126 interface is for multi-threaded programs, but the truth is almost the
1127 reverse.  The multi interface can allow a single-threaded application
1128 to perform the same kinds of multiple, simultaneous transfers that
1129 multi-threaded programs can perform.  It allows many of the benefits
1130 of multi-threaded transfers without the complexity of managing and
1131 synchronizing many threads.
1132
1133 To use this interface, you are better off if you first understand the basics
1134 of how to use the easy interface. The multi interface is simply a way to make
1135 multiple transfers at the same time by adding up multiple easy handles in to
1136 a "multi stack".
1137
1138 You create the easy handles you want and you set all the options just like you
1139 have been told above, and then you create a multi handle with
1140 \fIcurl_multi_init(3)\fP and add all those easy handles to that multi handle
1141 with \fIcurl_multi_add_handle(3)\fP.
1142
1143 When you've added the handles you have for the moment (you can still add new
1144 ones at any time), you start the transfers by call
1145 \fIcurl_multi_perform(3)\fP.
1146
1147 \fIcurl_multi_perform(3)\fP is asynchronous. It will only execute as little as
1148 possible and then return back control to your program. It is designed to never
1149 block. If it returns CURLM_CALL_MULTI_PERFORM you better call it again soon,
1150 as that is a signal that it still has local data to send or remote data to
1151 receive.
1152
1153 The best usage of this interface is when you do a select() on all possible
1154 file descriptors or sockets to know when to call libcurl again. This also
1155 makes it easy for you to wait and respond to actions on your own application's
1156 sockets/handles. You figure out what to select() for by using
1157 \fIcurl_multi_fdset(3)\fP, that fills in a set of fd_set variables for you
1158 with the particular file descriptors libcurl uses for the moment.
1159
1160 When you then call select(), it'll return when one of the file handles signal
1161 action and you then call \fIcurl_multi_perform(3)\fP to allow libcurl to do
1162 what it wants to do. Take note that libcurl does also feature some time-out
1163 code so we advice you to never use very long timeouts on select() before you
1164 call \fIcurl_multi_perform(3)\fP, which thus should be called unconditionally
1165 every now and then even if none of its file descriptors have signaled
1166 ready. Another precaution you should use: always call
1167 \fIcurl_multi_fdset(3)\fP immediately before the select() call since the
1168 current set of file descriptors may change when calling a curl function.
1169
1170 If you want to stop the transfer of one of the easy handles in the stack, you
1171 can use \fIcurl_multi_remove_handle(3)\fP to remove individual easy
1172 handles. Remember that easy handles should be \fIcurl_easy_cleanup(3)\fPed.
1173
1174 When a transfer within the multi stack has finished, the counter of running
1175 transfers (as filled in by \fIcurl_multi_perform(3)\fP) will decrease. When
1176 the number reaches zero, all transfers are done.
1177
1178 \fIcurl_multi_info_read(3)\fP can be used to get information about completed
1179 transfers. It then returns the CURLcode for each easy transfer, to allow you
1180 to figure out success on each individual transfer.
1181
1182 .SH "SSL, Certificates and Other Tricks"
1183
1184  [ seeding, passwords, keys, certificates, ENGINE, ca certs ]
1185
1186 .SH "Sharing Data Between Easy Handles"
1187
1188  [ fill in ]
1189
1190 .SH "Footnotes"
1191
1192 .IP "[1]"
1193 libcurl 7.10.3 and later have the ability to switch over to chunked
1194 Transfer-Encoding in cases were HTTP uploads are done with data of an unknown
1195 size.
1196 .IP "[2]"
1197 This happens on Windows machines when libcurl is built and used as a
1198 DLL. However, you can still do this on Windows if you link with a static
1199 library.
1200 .IP "[3]"
1201 The curl-config tool is generated at build-time (on UNIX-like systems) and
1202 should be installed with the 'make install' or similar instruction that
1203 installs the library, header files, man pages etc.
1204 .IP "[4]"
1205 This behavior was different in versions before 7.17.0, where strings had to
1206 remain valid past the end of the \fIcurl_easy_setopt(3)\fP call.