• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1---
2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al.
3SPDX-License-Identifier: curl
4Title: libcurl-tutorial
5Section: 3
6Source: libcurl
7See-also:
8  - libcurl-easy (3)
9  - libcurl-errors (3)
10  - libcurl-multi (3)
11  - libcurl-url (3)
12---
13
14# NAME
15
16libcurl-tutorial - libcurl programming tutorial
17
18# Objective
19
20This document attempts to describe the general principles and some basic
21approaches to consider when programming with libcurl. The text focuses on the
22C interface but should apply fairly well on other language bindings as well as
23they usually follow the C API pretty closely.
24
25This document refers to 'the user' as the person writing the source code that
26uses libcurl. That would probably be you or someone in your position. What is
27generally referred to as 'the program' is the collected source code that you
28write that is using libcurl for transfers. The program is outside libcurl and
29libcurl is outside of the program.
30
31To get more details on all options and functions described herein, please
32refer to their respective man pages.
33
34# Building
35
36There are many different ways to build C programs. This chapter assumes a Unix
37style build process. If you use a different build system, you can still read
38this to get general information that may apply to your environment as well.
39
40## Compiling the Program
41
42Your compiler needs to know where the libcurl headers are located. Therefore
43you must set your compiler's include path to point to the directory where you
44installed them. The 'curl-config'[3] tool can be used to get this information:
45~~~c
46  $ curl-config --cflags
47~~~
48
49## Linking the Program with libcurl
50
51When having compiled the program, you need to link your object files to create
52a single executable. For that to succeed, you need to link with libcurl and
53possibly also with other libraries that libcurl itself depends on. Like the
54OpenSSL libraries, but even some standard OS libraries may be needed on the
55command line. To figure out which flags to use, once again the 'curl-config'
56tool comes to the rescue:
57~~~c
58  $ curl-config --libs
59~~~
60
61## SSL or Not
62
63libcurl can be built and customized in many ways. One of the things that
64varies from different libraries and builds is the support for SSL-based
65transfers, like HTTPS and FTPS. If a supported SSL library was detected
66properly at build-time, libcurl is built with SSL support. To figure out if an
67installed libcurl has been built with SSL support enabled, use *curl-config*
68like this:
69
70~~~c
71  $ curl-config --feature
72~~~
73
74If SSL is supported, the keyword *SSL* is written to stdout, possibly together
75with a other features that could be either on or off on for different
76libcurls.
77
78See also the "Features libcurl Provides" further down.
79
80## autoconf macro
81
82When you write your configure script to detect libcurl and setup variables
83accordingly, we offer a macro that probably does everything you need in this
84area. See docs/libcurl/libcurl.m4 file - it includes docs on how to use it.
85
86# Portable Code in a Portable World
87
88The people behind libcurl have put a considerable effort to make libcurl work
89on a large amount of different operating systems and environments.
90
91You program libcurl the same way on all platforms that libcurl runs on. There
92are only a few minor details that differ. If you just make sure to write your
93code portable enough, you can create a portable program. libcurl should not
94stop you from that.
95
96# Global Preparation
97
98The program must initialize some of the libcurl functionality globally. That
99means it should be done exactly once, no matter how many times you intend to
100use the library. Once for your program's entire life time. This is done using
101~~~c
102 curl_global_init()
103~~~
104and it takes one parameter which is a bit pattern that tells libcurl what to
105initialize. Using *CURL_GLOBAL_ALL* makes it initialize all known internal
106sub modules, and might be a good default option. The current two bits that are
107specified are:
108
109## CURL_GLOBAL_WIN32
110
111which only does anything on Windows machines. When used on a Windows machine,
112it makes libcurl initialize the win32 socket stuff. Without having that
113initialized properly, your program cannot use sockets properly. You should
114only do this once for each application, so if your program already does this
115or of another library in use does it, you should not tell libcurl to do this
116as well.
117
118## CURL_GLOBAL_SSL
119
120which only does anything on libcurls compiled and built SSL-enabled. On these
121systems, this makes libcurl initialize the SSL library properly for this
122application. This only needs to be done once for each application so if your
123program or another library already does this, this bit should not be needed.
124
125libcurl has a default protection mechanism that detects if
126curl_global_init(3) has not been called by the time
127curl_easy_perform(3) is called and if that is the case, libcurl runs the
128function itself with a guessed bit pattern. Please note that depending solely
129on this is not considered nice nor good.
130
131When the program no longer uses libcurl, it should call
132curl_global_cleanup(3), which is the opposite of the init call. It
133performs the reversed operations to cleanup the resources the
134curl_global_init(3) call initialized.
135
136Repeated calls to curl_global_init(3) and curl_global_cleanup(3)
137should be avoided. They should only be called once each.
138
139# Features libcurl Provides
140
141It is considered best-practice to determine libcurl features at runtime rather
142than at build-time (if possible of course). By calling
143curl_version_info(3) and checking out the details of the returned
144struct, your program can figure out exactly what the currently running libcurl
145supports.
146
147# Two Interfaces
148
149libcurl first introduced the so called easy interface. All operations in the
150easy interface are prefixed with 'curl_easy'. The easy interface lets you do
151single transfers with a synchronous and blocking function call.
152
153libcurl also offers another interface that allows multiple simultaneous
154transfers in a single thread, the so called multi interface. More about that
155interface is detailed in a separate chapter further down. You still need to
156understand the easy interface first, so please continue reading for better
157understanding.
158
159# Handle the Easy libcurl
160
161To use the easy interface, you must first create yourself an easy handle. You
162need one handle for each easy session you want to perform. Basically, you
163should use one handle for every thread you plan to use for transferring. You
164must never share the same handle in multiple threads.
165
166Get an easy handle with
167~~~c
168 handle = curl_easy_init();
169~~~
170It returns an easy handle. Using that you proceed to the next step: setting
171up your preferred actions. A handle is just a logic entity for the upcoming
172transfer or series of transfers.
173
174You set properties and options for this handle using
175curl_easy_setopt(3). They control how the subsequent transfer or
176transfers using this handle are made. Options remain set in the handle until
177set again to something different. They are sticky. Multiple requests using the
178same handle use the same options.
179
180If you at any point would like to blank all previously set options for a
181single easy handle, you can call curl_easy_reset(3) and you can also
182make a clone of an easy handle (with all its set options) using
183curl_easy_duphandle(3).
184
185Many of the options you set in libcurl are "strings", pointers to data
186terminated with a zero byte. When you set strings with
187curl_easy_setopt(3), libcurl makes its own copy so that they do not need
188to be kept around in your application after being set[4].
189
190One of the most basic properties to set in the handle is the URL. You set your
191preferred URL to transfer with CURLOPT_URL(3) in a manner similar to:
192
193~~~c
194 curl_easy_setopt(handle, CURLOPT_URL, "http://domain.com/");
195~~~
196
197Let's assume for a while that you want to receive data as the URL identifies a
198remote resource you want to get here. Since you write a sort of application
199that needs this transfer, I assume that you would like to get the data passed
200to you directly instead of simply getting it passed to stdout. So, you write
201your own function that matches this prototype:
202~~~c
203 size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);
204~~~
205You tell libcurl to pass all data to this function by issuing a function
206similar to this:
207~~~c
208 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data);
209~~~
210You can control what data your callback function gets in the fourth argument
211by setting another property:
212~~~c
213 curl_easy_setopt(handle, CURLOPT_WRITEDATA, &internal_struct);
214~~~
215Using that property, you can easily pass local data between your application
216and the function that gets invoked by libcurl. libcurl itself does not touch
217the data you pass with CURLOPT_WRITEDATA(3).
218
219libcurl offers its own default internal callback that takes care of the data
220if you do not set the callback with CURLOPT_WRITEFUNCTION(3). It simply
221outputs the received data to stdout. You can have the default callback write
222the data to a different file handle by passing a 'FILE *' to a file opened for
223writing with the CURLOPT_WRITEDATA(3) option.
224
225Now, we need to take a step back and take a deep breath. Here is one of those
226rare platform-dependent nitpicks. Did you spot it? On some platforms[2],
227libcurl is not able to operate on file handles opened by the
228program. Therefore, if you use the default callback and pass in an open file
229handle with CURLOPT_WRITEDATA(3), libcurl crashes. You should avoid this
230to make your program run fine virtually everywhere.
231
232(CURLOPT_WRITEDATA(3) was formerly known as *CURLOPT_FILE*. Both names still
233work and do the same thing).
234
235If you are using libcurl as a win32 DLL, you MUST use the
236CURLOPT_WRITEFUNCTION(3) if you set CURLOPT_WRITEDATA(3) - or experience
237crashes.
238
239There are of course many more options you can set, and we get back to a few of
240them later. Let's instead continue to the actual transfer:
241
242~~~c
243 success = curl_easy_perform(handle);
244~~~
245
246curl_easy_perform(3) connects to the remote site, does the necessary commands
247and performs the transfer. Whenever it receives data, it calls the callback
248function we previously set. The function may get one byte at a time, or it may
249get many kilobytes at once. libcurl delivers as much as possible as often as
250possible. Your callback function should return the number of bytes it "took
251care of". If that is not the same amount of bytes that was passed to it,
252libcurl aborts the operation and returns with an error code.
253
254When the transfer is complete, the function returns a return code that informs
255you if it succeeded in its mission or not. If a return code is not enough for
256you, you can use the CURLOPT_ERRORBUFFER(3) to point libcurl to a buffer of
257yours where it stores a human readable error message as well.
258
259If you then want to transfer another file, the handle is ready to be used
260again. It is even preferred and encouraged that you reuse an existing handle
261if you intend to make another transfer. libcurl then attempts to reuse a
262previous connection.
263
264For some protocols, downloading a file can involve a complicated process of
265logging in, setting the transfer mode, changing the current directory and
266finally transferring the file data. libcurl takes care of all that
267complication for you. Given simply the URL to a file, libcurl takes care of
268all the details needed to get the file moved from one machine to another.
269
270# Multi-threading Issues
271
272libcurl is thread safe but there are a few exceptions. Refer to
273libcurl-thread(3) for more information.
274
275# When It does not Work
276
277There are times when the transfer fails for some reason. You might have set
278the wrong libcurl option or misunderstood what the libcurl option actually
279does, or the remote server might return non-standard replies that confuse the
280library which then confuses your program.
281
282There is one golden rule when these things occur: set the
283CURLOPT_VERBOSE(3) option to 1. it causes the library to spew out the
284entire protocol details it sends, some internal info and some received
285protocol data as well (especially when using FTP). If you are using HTTP,
286adding the headers in the received output to study is also a clever way to get
287a better understanding why the server behaves the way it does. Include headers
288in the normal body output with CURLOPT_HEADER(3) set 1.
289
290Of course, there are bugs left. We need to know about them to be able to fix
291them, so we are quite dependent on your bug reports. When you do report
292suspected bugs in libcurl, please include as many details as you possibly can:
293a protocol dump that CURLOPT_VERBOSE(3) produces, library version, as
294much as possible of your code that uses libcurl, operating system name and
295version, compiler name and version etc.
296
297If CURLOPT_VERBOSE(3) is not enough, you increase the level of debug
298data your application receive by using the CURLOPT_DEBUGFUNCTION(3).
299
300Getting some in-depth knowledge about the protocols involved is never wrong,
301and if you are trying to do funny things, you might understand libcurl and how
302to use it better if you study the appropriate RFC documents at least briefly.
303
304# Upload Data to a Remote Site
305
306libcurl tries to keep a protocol independent approach to most transfers, thus
307uploading to a remote FTP site is similar to uploading data to an HTTP server
308with a PUT request.
309
310Of course, first you either create an easy handle or you reuse one existing
311one. Then you set the URL to operate on just like before. This is the remote
312URL, that we now upload.
313
314Since we write an application, we most likely want libcurl to get the upload
315data by asking us for it. To make it do that, we set the read callback and the
316custom pointer libcurl passes to our read callback. The read callback should
317have a prototype similar to:
318~~~c
319 size_t function(char *bufptr, size_t size, size_t nitems, void *userp);
320~~~
321Where *bufptr* is the pointer to a buffer we fill in with data to upload
322and *size*nitems* is the size of the buffer and therefore also the maximum
323amount of data we can return to libcurl in this call. The *userp* pointer
324is the custom pointer we set to point to a struct of ours to pass private data
325between the application and the callback.
326~~~c
327 curl_easy_setopt(handle, CURLOPT_READFUNCTION, read_function);
328
329 curl_easy_setopt(handle, CURLOPT_READDATA, &filedata);
330~~~
331Tell libcurl that we want to upload:
332~~~c
333 curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L);
334~~~
335A few protocols do not behave properly when uploads are done without any prior
336knowledge of the expected file size. So, set the upload file size using the
337CURLOPT_INFILESIZE_LARGE(3) for all known file sizes like this[1]:
338
339~~~c
340 /* in this example, file_size must be an curl_off_t variable */
341 curl_easy_setopt(handle, CURLOPT_INFILESIZE_LARGE, file_size);
342~~~
343
344When you call curl_easy_perform(3) this time, it performs all the
345necessary operations and when it has invoked the upload it calls your supplied
346callback to get the data to upload. The program should return as much data as
347possible in every invoke, as that is likely to make the upload perform as fast
348as possible. The callback should return the number of bytes it wrote in the
349buffer. Returning 0 signals the end of the upload.
350
351# Passwords
352
353Many protocols use or even require that user name and password are provided
354to be able to download or upload the data of your choice. libcurl offers
355several ways to specify them.
356
357Most protocols support that you specify the name and password in the URL
358itself. libcurl detects this and use them accordingly. This is written like
359this:
360~~~c
361 protocol://user:password@example.com/path/
362~~~
363If you need any odd letters in your user name or password, you should enter
364them URL encoded, as %XX where XX is a two-digit hexadecimal number.
365
366libcurl also provides options to set various passwords. The user name and
367password as shown embedded in the URL can instead get set with the
368CURLOPT_USERPWD(3) option. The argument passed to libcurl should be a
369char * to a string in the format "user:password". In a manner like this:
370
371~~~c
372 curl_easy_setopt(handle, CURLOPT_USERPWD, "myname:thesecret");
373~~~
374
375Another case where name and password might be needed at times, is for those
376users who need to authenticate themselves to a proxy they use. libcurl offers
377another option for this, the CURLOPT_PROXYUSERPWD(3). It is used quite similar
378to the CURLOPT_USERPWD(3) option like this:
379
380~~~c
381 curl_easy_setopt(handle, CURLOPT_PROXYUSERPWD, "myname:thesecret");
382~~~
383
384There is a long time Unix "standard" way of storing FTP user names and
385passwords, namely in the $HOME/.netrc file (on Windows, libcurl also checks
386the *%USERPROFILE% environment* variable if *%HOME%* is unset, and tries
387"_netrc" as name). The file should be made private so that only the user may
388read it (see also the "Security Considerations" chapter), as it might contain
389the password in plain text. libcurl has the ability to use this file to figure
390out what set of user name and password to use for a particular host. As an
391extension to the normal functionality, libcurl also supports this file for
392non-FTP protocols such as HTTP. To make curl use this file, use the
393CURLOPT_NETRC(3) option:
394
395~~~c
396 curl_easy_setopt(handle, CURLOPT_NETRC, 1L);
397~~~
398
399A basic example of how such a .netrc file may look like:
400
401~~~c
402 machine myhost.mydomain.com
403 login userlogin
404 password secretword
405~~~
406
407All these examples have been cases where the password has been optional, or
408at least you could leave it out and have libcurl attempt to do its job
409without it. There are times when the password is not optional, like when
410you are using an SSL private key for secure transfers.
411
412To pass the known private key password to libcurl:
413~~~c
414 curl_easy_setopt(handle, CURLOPT_KEYPASSWD, "keypassword");
415~~~
416
417# HTTP Authentication
418
419The previous chapter showed how to set user name and password for getting URLs
420that require authentication. When using the HTTP protocol, there are many
421different ways a client can provide those credentials to the server and you
422can control which way libcurl uses them. The default HTTP authentication
423method is called 'Basic', which is sending the name and password in clear-text
424in the HTTP request, base64-encoded. This is insecure.
425
426At the time of this writing, libcurl can be built to use: Basic, Digest, NTLM,
427Negotiate (SPNEGO). You can tell libcurl which one to use with
428CURLOPT_HTTPAUTH(3) as in:
429
430~~~c
431 curl_easy_setopt(handle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
432
433~~~
434
435When you send authentication to a proxy, you can also set authentication type
436the same way but instead with CURLOPT_PROXYAUTH(3):
437
438~~~c
439 curl_easy_setopt(handle, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
440~~~
441
442Both these options allow you to set multiple types (by ORing them together),
443to make libcurl pick the most secure one out of the types the server/proxy
444claims to support. This method does however add a round-trip since libcurl
445must first ask the server what it supports:
446
447~~~c
448 curl_easy_setopt(handle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST|CURLAUTH_BASIC);
449~~~
450
451For convenience, you can use the *CURLAUTH_ANY* define (instead of a list with
452specific types) which allows libcurl to use whatever method it wants.
453
454When asking for multiple types, libcurl picks the available one it considers
455"best" in its own internal order of preference.
456
457# HTTP POSTing
458
459We get many questions regarding how to issue HTTP POSTs with libcurl the
460proper way. This chapter thus includes examples using both different versions
461of HTTP POST that libcurl supports.
462
463The first version is the simple POST, the most common version, that most HTML
464pages using the <form> tag uses. We provide a pointer to the data and tell
465libcurl to post it all to the remote site:
466
467~~~c
468    char *data="name=daniel&project=curl";
469    curl_easy_setopt(handle, CURLOPT_POSTFIELDS, data);
470    curl_easy_setopt(handle, CURLOPT_URL, "http://posthere.com/");
471
472    curl_easy_perform(handle); /* post away! */
473~~~
474
475Simple enough, huh? Since you set the POST options with the
476CURLOPT_POSTFIELDS(3), this automatically switches the handle to use
477POST in the upcoming request.
478
479What if you want to post binary data that also requires you to set the
480Content-Type: header of the post? Well, binary posts prevent libcurl from being
481able to do strlen() on the data to figure out the size, so therefore we must
482tell libcurl the size of the post data. Setting headers in libcurl requests are
483done in a generic way, by building a list of our own headers and then passing
484that list to libcurl.
485
486~~~c
487 struct curl_slist *headers=NULL;
488 headers = curl_slist_append(headers, "Content-Type: text/xml");
489
490 /* post binary data */
491 curl_easy_setopt(handle, CURLOPT_POSTFIELDS, binaryptr);
492
493 /* set the size of the postfields data */
494 curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, 23L);
495
496 /* pass our list of custom made headers */
497 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
498
499 curl_easy_perform(handle); /* post away! */
500
501 curl_slist_free_all(headers); /* free the header list */
502~~~
503
504While the simple examples above cover the majority of all cases where HTTP
505POST operations are required, they do not do multi-part formposts. Multi-part
506formposts were introduced as a better way to post (possibly large) binary data
507and were first documented in the RFC 1867 (updated in RFC 2388). They are
508called multi-part because they are built by a chain of parts, each part being
509a single unit of data. Each part has its own name and contents. You can in
510fact create and post a multi-part formpost with the regular libcurl POST
511support described above, but that would require that you build a formpost
512yourself and provide to libcurl.
513
514To make that easier, libcurl provides a MIME API consisting in several
515functions: using those, you can create and fill a multi-part form. Function
516curl_mime_init(3) creates a multi-part body; you can then append new parts
517to a multi-part body using curl_mime_addpart(3).
518
519There are three possible data sources for a part: memory using
520curl_mime_data(3), file using curl_mime_filedata(3) and user-defined data
521read callback using curl_mime_data_cb(3). curl_mime_name(3) sets a part's
522(i.e.: form field) name, while curl_mime_filename(3) fills in the remote
523filename. With curl_mime_type(3), you can tell the MIME type of a part,
524curl_mime_headers(3) allows defining the part's headers. When a multi-part
525body is no longer needed, you can destroy it using curl_mime_free(3).
526
527The following example sets two simple text parts with plain textual contents,
528and then a file with binary contents and uploads the whole thing.
529
530~~~c
531 curl_mime *multipart = curl_mime_init(handle);
532 curl_mimepart *part = curl_mime_addpart(multipart);
533 curl_mime_name(part, "name");
534 curl_mime_data(part, "daniel", CURL_ZERO_TERMINATED);
535 part = curl_mime_addpart(multipart);
536 curl_mime_name(part, "project");
537 curl_mime_data(part, "curl", CURL_ZERO_TERMINATED);
538 part = curl_mime_addpart(multipart);
539 curl_mime_name(part, "logotype-image");
540 curl_mime_filedata(part, "curl.png");
541
542 /* Set the form info */
543 curl_easy_setopt(handle, CURLOPT_MIMEPOST, multipart);
544
545 curl_easy_perform(handle); /* post away! */
546
547 /* free the post data again */
548 curl_mime_free(multipart);
549~~~
550
551To post multiple files for a single form field, you must supply each file in
552a separate part, all with the same field name. Although function
553curl_mime_subparts(3) implements nested multi-parts, this way of
554multiple files posting is deprecated by RFC 7578, chapter 4.3.
555
556To set the data source from an already opened FILE pointer, use:
557
558~~~c
559 curl_mime_data_cb(part, filesize, (curl_read_callback) fread,
560                   (curl_seek_callback) fseek, NULL, filepointer);
561~~~
562
563A deprecated curl_formadd(3) function is still supported in libcurl.
564It should however not be used anymore for new designs and programs using it
565ought to be converted to the MIME API. It is however described here as an
566aid to conversion.
567
568Using *curl_formadd*, you add parts to the form. When you are done adding
569parts, you post the whole form.
570
571The MIME API example above is expressed as follows using this function:
572
573~~~c
574 struct curl_httppost *post=NULL;
575 struct curl_httppost *last=NULL;
576 curl_formadd(&post, &last,
577              CURLFORM_COPYNAME, "name",
578              CURLFORM_COPYCONTENTS, "daniel", CURLFORM_END);
579 curl_formadd(&post, &last,
580              CURLFORM_COPYNAME, "project",
581              CURLFORM_COPYCONTENTS, "curl", CURLFORM_END);
582 curl_formadd(&post, &last,
583              CURLFORM_COPYNAME, "logotype-image",
584              CURLFORM_FILECONTENT, "curl.png", CURLFORM_END);
585
586 /* Set the form info */
587 curl_easy_setopt(handle, CURLOPT_HTTPPOST, post);
588
589 curl_easy_perform(handle); /* post away! */
590
591 /* free the post data again */
592 curl_formfree(post);
593~~~
594
595Multipart formposts are chains of parts using MIME-style separators and
596headers. It means that each one of these separate parts get a few headers set
597that describe the individual content-type, size etc. To enable your
598application to handicraft this formpost even more, libcurl allows you to
599supply your own set of custom headers to such an individual form part. You can
600of course supply headers to as many parts as you like, but this little example
601shows how you set headers to one specific part when you add that to the post
602handle:
603
604~~~c
605 struct curl_slist *headers=NULL;
606 headers = curl_slist_append(headers, "Content-Type: text/xml");
607
608 curl_formadd(&post, &last,
609              CURLFORM_COPYNAME, "logotype-image",
610              CURLFORM_FILECONTENT, "curl.xml",
611              CURLFORM_CONTENTHEADER, headers,
612              CURLFORM_END);
613
614 curl_easy_perform(handle); /* post away! */
615
616 curl_formfree(post); /* free post */
617 curl_slist_free_all(headers); /* free custom header list */
618~~~
619
620Since all options on an easy handle are "sticky", they remain the same until
621changed even if you do call curl_easy_perform(3), you may need to tell
622curl to go back to a plain GET request if you intend to do one as your next
623request. You force an easy handle to go back to GET by using the
624CURLOPT_HTTPGET(3) option:
625~~~c
626 curl_easy_setopt(handle, CURLOPT_HTTPGET, 1L);
627~~~
628Just setting CURLOPT_POSTFIELDS(3) to "" or NULL does *not* stop libcurl
629from doing a POST. It just makes it POST without any data to send!
630
631# Converting from deprecated form API to MIME API
632
633Four rules have to be respected in building the multi-part:
634
635- The easy handle must be created before building the multi-part.
636
637- The multi-part is always created by a call to curl_mime_init(handle).
638
639- Each part is created by a call to curl_mime_addpart(multipart).
640
641- When complete, the multi-part must be bound to the easy handle using
642CURLOPT_MIMEPOST(3) instead of CURLOPT_HTTPPOST(3).
643
644Here are some example of *curl_formadd* calls to MIME API sequences:
645
646~~~c
647 curl_formadd(&post, &last,
648              CURLFORM_COPYNAME, "id",
649              CURLFORM_COPYCONTENTS, "daniel", CURLFORM_END);
650              CURLFORM_CONTENTHEADER, headers,
651              CURLFORM_END);
652~~~
653becomes:
654~~~c
655 part = curl_mime_addpart(multipart);
656 curl_mime_name(part, "id");
657 curl_mime_data(part, "daniel", CURL_ZERO_TERMINATED);
658 curl_mime_headers(part, headers, FALSE);
659~~~
660
661Setting the last curl_mime_headers(3) argument to TRUE would have caused
662the headers to be automatically released upon destroyed the multi-part, thus
663saving a clean-up call to curl_slist_free_all(3).
664
665~~~c
666 curl_formadd(&post, &last,
667              CURLFORM_PTRNAME, "logotype-image",
668              CURLFORM_FILECONTENT, "-",
669              CURLFORM_END);
670~~~
671becomes:
672~~~c
673 part = curl_mime_addpart(multipart);
674 curl_mime_name(part, "logotype-image");
675 curl_mime_data_cb(part, (curl_off_t) -1, fread, fseek, NULL, stdin);
676~~~
677
678curl_mime_name(3) always copies the field name. The special file name
679"-" is not supported by curl_mime_filename(3): to read an open file, use
680a callback source using fread(). The transfer is be chunk-encoded since the
681data size is unknown.
682
683~~~c
684 curl_formadd(&post, &last,
685              CURLFORM_COPYNAME, "datafile[]",
686              CURLFORM_FILE, "file1",
687              CURLFORM_FILE, "file2",
688              CURLFORM_END);
689~~~
690becomes:
691~~~c
692 part = curl_mime_addpart(multipart);
693 curl_mime_name(part, "datafile[]");
694 curl_mime_filedata(part, "file1");
695 part = curl_mime_addpart(multipart);
696 curl_mime_name(part, "datafile[]");
697 curl_mime_filedata(part, "file2");
698~~~
699
700The deprecated multipart/mixed implementation of multiple files field is
701translated to two distinct parts with the same name.
702
703~~~c
704 curl_easy_setopt(handle, CURLOPT_READFUNCTION, myreadfunc);
705 curl_formadd(&post, &last,
706              CURLFORM_COPYNAME, "stream",
707              CURLFORM_STREAM, arg,
708              CURLFORM_CONTENTLEN, (curl_off_t) datasize,
709              CURLFORM_FILENAME, "archive.zip",
710              CURLFORM_CONTENTTYPE, "application/zip",
711              CURLFORM_END);
712~~~
713becomes:
714~~~c
715 part = curl_mime_addpart(multipart);
716 curl_mime_name(part, "stream");
717 curl_mime_data_cb(part, (curl_off_t) datasize,
718                   myreadfunc, NULL, NULL, arg);
719 curl_mime_filename(part, "archive.zip");
720 curl_mime_type(part, "application/zip");
721~~~
722
723CURLOPT_READFUNCTION(3) callback is not used: it is replace by directly
724setting the part source data from the callback read function.
725
726~~~c
727 curl_formadd(&post, &last,
728              CURLFORM_COPYNAME, "memfile",
729              CURLFORM_BUFFER, "memfile.bin",
730              CURLFORM_BUFFERPTR, databuffer,
731              CURLFORM_BUFFERLENGTH, (long) sizeof databuffer,
732              CURLFORM_END);
733~~~
734becomes:
735~~~c
736 part = curl_mime_addpart(multipart);
737 curl_mime_name(part, "memfile");
738 curl_mime_data(part, databuffer, (curl_off_t) sizeof databuffer);
739 curl_mime_filename(part, "memfile.bin");
740~~~
741
742curl_mime_data(3) always copies the initial data: data buffer is thus
743free for immediate reuse.
744
745~~~c
746 curl_formadd(&post, &last,
747              CURLFORM_COPYNAME, "message",
748              CURLFORM_FILECONTENT, "msg.txt",
749              CURLFORM_END);
750~~~
751becomes:
752~~~c
753 part = curl_mime_addpart(multipart);
754 curl_mime_name(part, "message");
755 curl_mime_filedata(part, "msg.txt");
756 curl_mime_filename(part, NULL);
757~~~
758
759Use of curl_mime_filedata(3) sets the remote filename as a side effect: it is
760therefore necessary to clear it for *CURLFORM_FILECONTENT* emulation.
761
762# Showing Progress
763
764For historical and traditional reasons, libcurl has a built-in progress meter
765that can be switched on and then makes it present a progress meter in your
766terminal.
767
768Switch on the progress meter by, oddly enough, setting
769CURLOPT_NOPROGRESS(3) to zero. This option is set to 1 by default.
770
771For most applications however, the built-in progress meter is useless and what
772instead is interesting is the ability to specify a progress callback. The
773function pointer you pass to libcurl is then called on irregular intervals
774with information about the current transfer.
775
776Set the progress callback by using CURLOPT_PROGRESSFUNCTION(3). Pass a pointer
777to a function that matches this prototype:
778
779~~~c
780 int progress_callback(void *clientp,
781                       double dltotal,
782                       double dlnow,
783                       double ultotal,
784                       double ulnow);
785~~~
786
787If any of the input arguments is unknown, a 0 is provided. The first argument,
788the 'clientp' is the pointer you pass to libcurl with
789CURLOPT_PROGRESSDATA(3). libcurl does not touch it.
790
791# libcurl with C++
792
793There is basically only one thing to keep in mind when using C++ instead of C
794when interfacing libcurl:
795
796The callbacks CANNOT be non-static class member functions
797
798Example C++ code:
799
800~~~c
801class AClass {
802    static size_t write_data(void *ptr, size_t size, size_t nmemb,
803                             void *ourpointer)
804    {
805      /* do what you want with the data */
806    }
807 }
808~~~
809
810# Proxies
811
812What "proxy" means according to Merriam-Webster: "a person authorized to act
813for another" but also "the agency, function, or office of a deputy who acts as
814a substitute for another".
815
816Proxies are exceedingly common these days. Companies often only offer Internet
817access to employees through their proxies. Network clients or user-agents ask
818the proxy for documents, the proxy does the actual request and then it returns
819them.
820
821libcurl supports SOCKS and HTTP proxies. When a given URL is wanted, libcurl
822asks the proxy for it instead of trying to connect to the actual remote host
823identified in the URL.
824
825If you are using a SOCKS proxy, you may find that libcurl does not quite support
826all operations through it.
827
828For HTTP proxies: the fact that the proxy is an HTTP proxy puts certain
829restrictions on what can actually happen. A requested URL that might not be a
830HTTP URL is passed to the HTTP proxy to deliver back to libcurl. This happens
831transparently, and an application may not need to know. I say "may", because
832at times it is important to understand that all operations over an HTTP proxy
833use the HTTP protocol. For example, you cannot invoke your own custom FTP
834commands or even proper FTP directory listings.
835
836## Proxy Options
837
838To tell libcurl to use a proxy at a given port number:
839~~~c
840 curl_easy_setopt(handle, CURLOPT_PROXY, "proxy-host.com:8080");
841~~~
842Some proxies require user authentication before allowing a request, and you
843pass that information similar to this:
844~~~c
845 curl_easy_setopt(handle, CURLOPT_PROXYUSERPWD, "user:password");
846~~~
847If you want to, you can specify the hostname only in the
848CURLOPT_PROXY(3) option, and set the port number separately with
849CURLOPT_PROXYPORT(3).
850
851Tell libcurl what kind of proxy it is with CURLOPT_PROXYTYPE(3) (if not,
852it defaults to assuming an HTTP proxy):
853~~~c
854 curl_easy_setopt(handle, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
855~~~
856
857## Environment Variables
858
859libcurl automatically checks and uses a set of environment variables to know
860what proxies to use for certain protocols. The names of the variables are
861following an old tradition and are built up as "[protocol]_proxy" (note the
862lower casing). Which makes the variable 'http_proxy' checked for a name of a
863proxy to use when the input URL is HTTP. Following the same rule, the variable
864named 'ftp_proxy' is checked for FTP URLs. Again, the proxies are always HTTP
865proxies, the different names of the variables simply allows different HTTP
866proxies to be used.
867
868The proxy environment variable contents should be in the format
869"[protocol://][user:password@]machine[:port]". Where the protocol:// part
870specifies which type of proxy it is, and the optional port number specifies on
871which port the proxy operates. If not specified, the internal default port
872number is used and that is most likely not the one you would like it to be.
873
874There are two special environment variables. 'all_proxy' is what sets proxy
875for any URL in case the protocol specific variable was not set, and 'no_proxy'
876defines a list of hosts that should not use a proxy even though a variable may
877say so. If 'no_proxy' is a plain asterisk ("*") it matches all hosts.
878
879To explicitly disable libcurl's checking for and using the proxy environment
880variables, set the proxy name to "" - an empty string - with
881CURLOPT_PROXY(3).
882
883## SSL and Proxies
884
885SSL is for secure point-to-point connections. This involves strong encryption
886and similar things, which effectively makes it impossible for a proxy to
887operate as a "man in between" which the proxy's task is, as previously
888discussed. Instead, the only way to have SSL work over an HTTP proxy is to ask
889the proxy to tunnel everything through without being able to check or fiddle
890with the traffic.
891
892Opening an SSL connection over an HTTP proxy is therefore a matter of asking the
893proxy for a straight connection to the target host on a specified port. This
894is made with the HTTP request CONNECT. ("please dear proxy, connect me to that
895remote host").
896
897Because of the nature of this operation, where the proxy has no idea what kind
898of data that is passed in and out through this tunnel, this breaks some of the
899few advantages that come from using a proxy, such as caching. Many
900organizations prevent this kind of tunneling to other destination port numbers
901than 443 (which is the default HTTPS port number).
902
903## Tunneling Through Proxy
904
905As explained above, tunneling is required for SSL to work and often even
906restricted to the operation intended for SSL; HTTPS.
907
908This is however not the only time proxy-tunneling might offer benefits to
909you or your application.
910
911As tunneling opens a direct connection from your application to the remote
912machine, it suddenly also re-introduces the ability to do non-HTTP
913operations over an HTTP proxy. You can in fact use things such as FTP
914upload or FTP custom commands this way.
915
916Again, this is often prevented by the administrators of proxies and is
917rarely allowed.
918
919Tell libcurl to use proxy tunneling like this:
920~~~c
921 curl_easy_setopt(handle, CURLOPT_HTTPPROXYTUNNEL, 1L);
922~~~
923In fact, there might even be times when you want to do plain HTTP operations
924using a tunnel like this, as it then enables you to operate on the remote
925server instead of asking the proxy to do so. libcurl does not stand in the way
926for such innovative actions either!
927
928## Proxy Auto-Config
929
930Netscape first came up with this. It is basically a webpage (usually using a
931.pac extension) with a JavaScript that when executed by the browser with the
932requested URL as input, returns information to the browser on how to connect
933to the URL. The returned information might be "DIRECT" (which means no proxy
934should be used), "PROXY host:port" (to tell the browser where the proxy for
935this particular URL is) or "SOCKS host:port" (to direct the browser to a SOCKS
936proxy).
937
938libcurl has no means to interpret or evaluate JavaScript and thus it does not
939support this. If you get yourself in a position where you face this nasty
940invention, the following advice have been mentioned and used in the past:
941
942- Depending on the JavaScript complexity, write up a script that translates it
943to another language and execute that.
944
945- Read the JavaScript code and rewrite the same logic in another language.
946
947- Implement a JavaScript interpreter; people have successfully used the
948Mozilla JavaScript engine in the past.
949
950- Ask your admins to stop this, for a static proxy setup or similar.
951
952# Persistence Is The Way to Happiness
953
954Re-cycling the same easy handle several times when doing multiple requests is
955the way to go.
956
957After each single curl_easy_perform(3) operation, libcurl keeps the
958connection alive and open. A subsequent request using the same easy handle to
959the same host might just be able to use the already open connection! This
960reduces network impact a lot.
961
962Even if the connection is dropped, all connections involving SSL to the same
963host again, benefit from libcurl's session ID cache that drastically reduces
964re-connection time.
965
966FTP connections that are kept alive save a lot of time, as the command-
967response round-trips are skipped, and also you do not risk getting blocked
968without permission to login again like on many FTP servers only allowing N
969persons to be logged in at the same time.
970
971libcurl caches DNS name resolving results, to make lookups of a previously
972looked up name a lot faster.
973
974Other interesting details that improve performance for subsequent requests
975may also be added in the future.
976
977Each easy handle attempts to keep the last few connections alive for a while
978in case they are to be used again. You can set the size of this "cache" with
979the CURLOPT_MAXCONNECTS(3) option. Default is 5. There is rarely any
980point in changing this value, and if you think of changing this it is often
981just a matter of thinking again.
982
983To force your upcoming request to not use an already existing connection, you
984can do that by setting CURLOPT_FRESH_CONNECT(3) to 1. In a similar
985spirit, you can also forbid the upcoming request to be "lying" around and
986possibly get reused after the request by setting
987CURLOPT_FORBID_REUSE(3) to 1.
988
989# HTTP Headers Used by libcurl
990
991When you use libcurl to do HTTP requests, it passes along a series of headers
992automatically. It might be good for you to know and understand these. You can
993replace or remove them by using the CURLOPT_HTTPHEADER(3) option.
994
995## Host
996
997This header is required by HTTP 1.1 and even many 1.0 servers and should be
998the name of the server we want to talk to. This includes the port number if
999anything but default.
1000
1001## Accept
1002
1003"*/*"
1004
1005## Expect
1006
1007When doing POST requests, libcurl sets this header to "100-continue" to ask
1008the server for an "OK" message before it proceeds with sending the data part
1009of the post. If the posted data amount is deemed "small", libcurl does not use
1010this header.
1011
1012# Customizing Operations
1013
1014There is an ongoing development today where more and more protocols are built
1015upon HTTP for transport. This has obvious benefits as HTTP is a tested and
1016reliable protocol that is widely deployed and has excellent proxy-support.
1017
1018When you use one of these protocols, and even when doing other kinds of
1019programming you may need to change the traditional HTTP (or FTP or...)
1020manners. You may need to change words, headers or various data.
1021
1022libcurl is your friend here too.
1023
1024## CUSTOMREQUEST
1025
1026If just changing the actual HTTP request keyword is what you want, like when
1027GET, HEAD or POST is not good enough for you, CURLOPT_CUSTOMREQUEST(3)
1028is there for you. It is simple to use:
1029~~~c
1030curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "MYOWNREQUEST");
1031~~~
1032When using the custom request, you change the request keyword of the actual
1033request you are performing. Thus, by default you make a GET request but you
1034can also make a POST operation (as described before) and then replace the POST
1035keyword if you want to. You are the boss.
1036
1037## Modify Headers
1038
1039HTTP-like protocols pass a series of headers to the server when doing the
1040request, and you are free to pass any amount of extra headers that you
1041think fit. Adding headers is this easy:
1042
1043~~~c
1044struct curl_slist *headers=NULL; /* init to NULL is important */
1045
1046headers = curl_slist_append(headers, "Hey-server-hey: how are you?");
1047headers = curl_slist_append(headers, "X-silly-content: yes");
1048
1049/* pass our list of custom made headers */
1050curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
1051
1052curl_easy_perform(handle); /* transfer http */
1053
1054curl_slist_free_all(headers); /* free the header list */
1055~~~
1056
1057... and if you think some of the internally generated headers, such as Accept:
1058or Host: do not contain the data you want them to contain, you can replace
1059them by simply setting them too:
1060
1061~~~c
1062headers = curl_slist_append(headers, "Accept: Agent-007");
1063headers = curl_slist_append(headers, "Host: munged.host.line");
1064~~~
1065
1066## Delete Headers
1067
1068If you replace an existing header with one with no contents, you prevent the
1069header from being sent. For instance, if you want to completely prevent the
1070"Accept:" header from being sent, you can disable it with code similar to
1071this:
1072
1073 headers = curl_slist_append(headers, "Accept:");
1074
1075Both replacing and canceling internal headers should be done with careful
1076consideration and you should be aware that you may violate the HTTP protocol
1077when doing so.
1078
1079## Enforcing chunked transfer-encoding
1080
1081By making sure a request uses the custom header "Transfer-Encoding: chunked"
1082when doing a non-GET HTTP operation, libcurl switches over to "chunked"
1083upload, even though the size of the data to upload might be known. By default,
1084libcurl usually switches over to chunked upload automatically if the upload
1085data size is unknown.
1086
1087## HTTP Version
1088
1089All HTTP requests includes the version number to tell the server which version
1090we support. libcurl speaks HTTP 1.1 by default. Some old servers do not like
1091getting 1.1-requests and when dealing with stubborn old things like that, you
1092can tell libcurl to use 1.0 instead by doing something like this:
1093
1094 curl_easy_setopt(handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
1095
1096## FTP Custom Commands
1097
1098Not all protocols are HTTP-like, and thus the above may not help you when
1099you want to make, for example, your FTP transfers to behave differently.
1100
1101Sending custom commands to an FTP server means that you need to send the
1102commands exactly as the FTP server expects them (RFC 959 is a good guide
1103here), and you can only use commands that work on the control-connection
1104alone. All kinds of commands that require data interchange and thus need a
1105data-connection must be left to libcurl's own judgment. Also be aware that
1106libcurl does its best to change directory to the target directory before doing
1107any transfer, so if you change directory (with CWD or similar) you might
1108confuse libcurl and then it might not attempt to transfer the file in the
1109correct remote directory.
1110
1111A little example that deletes a given file before an operation:
1112
1113~~~c
1114 headers = curl_slist_append(headers, "DELE file-to-remove");
1115
1116 /* pass the list of custom commands to the handle */
1117 curl_easy_setopt(handle, CURLOPT_QUOTE, headers);
1118
1119 curl_easy_perform(handle); /* transfer ftp data! */
1120
1121 curl_slist_free_all(headers); /* free the header list */
1122~~~
1123
1124If you would instead want this operation (or chain of operations) to happen
1125_after_ the data transfer took place the option to curl_easy_setopt(3)
1126would instead be called CURLOPT_POSTQUOTE(3) and used the exact same
1127way.
1128
1129The custom FTP commands are issued to the server in the same order they are
1130added to the list, and if a command gets an error code returned back from the
1131server, no more commands are issued and libcurl bails out with an error code
1132(CURLE_QUOTE_ERROR). Note that if you use CURLOPT_QUOTE(3) to send
1133commands before a transfer, no transfer actually takes place when a quote
1134command has failed.
1135
1136If you set the CURLOPT_HEADER(3) to 1, you tell libcurl to get
1137information about the target file and output "headers" about it. The headers
1138are in "HTTP-style", looking like they do in HTTP.
1139
1140The option to enable headers or to run custom FTP commands may be useful to
1141combine with CURLOPT_NOBODY(3). If this option is set, no actual file
1142content transfer is performed.
1143
1144## FTP Custom CUSTOMREQUEST
1145
1146If you do want to list the contents of an FTP directory using your own defined
1147FTP command, CURLOPT_CUSTOMREQUEST(3) does just that. "NLST" is the
1148default one for listing directories but you are free to pass in your idea of a
1149good alternative.
1150
1151# Cookies Without Chocolate Chips
1152
1153In the HTTP sense, a cookie is a name with an associated value. A server sends
1154the name and value to the client, and expects it to get sent back on every
1155subsequent request to the server that matches the particular conditions
1156set. The conditions include that the domain name and path match and that the
1157cookie has not become too old.
1158
1159In real-world cases, servers send new cookies to replace existing ones to
1160update them. Server use cookies to "track" users and to keep "sessions".
1161
1162Cookies are sent from server to clients with the header Set-Cookie: and
1163they are sent from clients to servers with the Cookie: header.
1164
1165To just send whatever cookie you want to a server, you can use
1166CURLOPT_COOKIE(3) to set a cookie string like this:
1167~~~c
1168 curl_easy_setopt(handle, CURLOPT_COOKIE, "name1=var1; name2=var2;");
1169~~~
1170In many cases, that is not enough. You might want to dynamically save
1171whatever cookies the remote server passes to you, and make sure those cookies
1172are then used accordingly on later requests.
1173
1174One way to do this, is to save all headers you receive in a plain file and
1175when you make a request, you tell libcurl to read the previous headers to
1176figure out which cookies to use. Set the header file to read cookies from with
1177CURLOPT_COOKIEFILE(3).
1178
1179The CURLOPT_COOKIEFILE(3) option also automatically enables the cookie
1180parser in libcurl. Until the cookie parser is enabled, libcurl does not parse
1181or understand incoming cookies and they are just be ignored. However, when the
1182parser is enabled the cookies are understood and the cookies are kept in
1183memory and used properly in subsequent requests when the same handle is
1184used. Many times this is enough, and you may not have to save the cookies to
1185disk at all. Note that the file you specify to CURLOPT_COOKIEFILE(3)
1186does not have to exist to enable the parser, so a common way to just enable
1187the parser and not read any cookies is to use the name of a file you know does
1188not exist.
1189
1190If you would rather use existing cookies that you have previously received
1191with your Netscape or Mozilla browsers, you can make libcurl use that cookie
1192file as input. The CURLOPT_COOKIEFILE(3) is used for that too, as
1193libcurl automatically finds out what kind of file it is and acts accordingly.
1194
1195Perhaps the most advanced cookie operation libcurl offers, is saving the
1196entire internal cookie state back into a Netscape/Mozilla formatted cookie
1197file. We call that the cookie-jar. When you set a filename with
1198CURLOPT_COOKIEJAR(3), that filename is created and all received cookies get
1199stored in it when curl_easy_cleanup(3) is called. This enables cookies to get
1200passed on properly between multiple handles without any information getting
1201lost.
1202
1203# FTP Peculiarities We Need
1204
1205FTP transfers use a second TCP/IP connection for the data transfer. This is
1206usually a fact you can forget and ignore but at times this detail comes back
1207to haunt you. libcurl offers several different ways to customize how the
1208second connection is being made.
1209
1210libcurl can either connect to the server a second time or tell the server to
1211connect back to it. The first option is the default and it is also what works
1212best for all the people behind firewalls, NATs or IP-masquerading setups.
1213libcurl then tells the server to open up a new port and wait for a second
1214connection. This is by default attempted with EPSV first, and if that does not
1215work it tries PASV instead. (EPSV is an extension to the original FTP spec
1216and does not exist nor work on all FTP servers.)
1217
1218You can prevent libcurl from first trying the EPSV command by setting
1219CURLOPT_FTP_USE_EPSV(3) to zero.
1220
1221In some cases, you want to have the server connect back to you for the second
1222connection. This might be when the server is perhaps behind a firewall or
1223something and only allows connections on a single port. libcurl then informs
1224the remote server which IP address and port number to connect to. This is made
1225with the CURLOPT_FTPPORT(3) option. If you set it to "-", libcurl uses your
1226system's "default IP address". If you want to use a particular IP, you can set
1227the full IP address, a hostname to resolve to an IP address or even a local
1228network interface name that libcurl gets the IP address from.
1229
1230When doing the "PORT" approach, libcurl attempts to use the EPRT and the LPRT
1231before trying PORT, as they work with more protocols. You can disable this
1232behavior by setting CURLOPT_FTP_USE_EPRT(3) to zero.
1233
1234# MIME API revisited for SMTP and IMAP
1235
1236In addition to support HTTP multi-part form fields, the MIME API can be used
1237to build structured email messages and send them via SMTP or append such
1238messages to IMAP directories.
1239
1240A structured email message may contain several parts: some are displayed
1241inline by the MUA, some are attachments. Parts can also be structured as
1242multi-part, for example to include another email message or to offer several
1243text formats alternatives. This can be nested to any level.
1244
1245To build such a message, you prepare the nth-level multi-part and then include
1246it as a source to the parent multi-part using function
1247curl_mime_subparts(3). Once it has been
1248bound to its parent multi-part, a nth-level multi-part belongs to it and
1249should not be freed explicitly.
1250
1251Email messages data is not supposed to be non-ascii and line length is
1252limited: fortunately, some transfer encodings are defined by the standards to
1253support the transmission of such incompatible data. Function
1254curl_mime_encoder(3) tells a part that its source data must be encoded
1255before being sent. It also generates the corresponding header for that part.
1256If the part data you want to send is already encoded in such a scheme, do not
1257use this function (this would over-encode it), but explicitly set the
1258corresponding part header.
1259
1260Upon sending such a message, libcurl prepends it with the header list
1261set with CURLOPT_HTTPHEADER(3), as zero level mime part headers.
1262
1263Here is an example building an email message with an inline plain/html text
1264alternative and a file attachment encoded in base64:
1265
1266~~~c
1267 curl_mime *message = curl_mime_init(handle);
1268
1269 /* The inline part is an alternative proposing the html and the text
1270    versions of the email. */
1271 curl_mime *alt = curl_mime_init(handle);
1272
1273 /* HTML message. */
1274 curl_mimepart *part = curl_mime_addpart(alt);
1275 curl_mime_data(part, "<html><body><p>This is HTML</p></body></html>",
1276                      CURL_ZERO_TERMINATED);
1277 curl_mime_type(part, "text/html");
1278
1279 /* Text message. */
1280 part = curl_mime_addpart(alt);
1281 curl_mime_data(part, "This is plain text message",
1282                      CURL_ZERO_TERMINATED);
1283
1284 /* Create the inline part. */
1285 part = curl_mime_addpart(message);
1286 curl_mime_subparts(part, alt);
1287 curl_mime_type(part, "multipart/alternative");
1288 struct curl_slist *headers = curl_slist_append(NULL,
1289                   "Content-Disposition: inline");
1290 curl_mime_headers(part, headers, TRUE);
1291
1292 /* Add the attachment. */
1293 part = curl_mime_addpart(message);
1294 curl_mime_filedata(part, "manual.pdf");
1295 curl_mime_encoder(part, "base64");
1296
1297 /* Build the mail headers. */
1298 headers = curl_slist_append(NULL, "From: me@example.com");
1299 headers = curl_slist_append(headers, "To: you@example.com");
1300
1301 /* Set these into the easy handle. */
1302 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
1303 curl_easy_setopt(handle, CURLOPT_MIMEPOST, mime);
1304~~~
1305
1306It should be noted that appending a message to an IMAP directory requires
1307the message size to be known prior upload. It is therefore not possible to
1308include parts with unknown data size in this context.
1309
1310# Headers Equal Fun
1311
1312Some protocols provide "headers", meta-data separated from the normal
1313data. These headers are by default not included in the normal data stream, but
1314you can make them appear in the data stream by setting CURLOPT_HEADER(3)
1315to 1.
1316
1317What might be even more useful, is libcurl's ability to separate the headers
1318from the data and thus make the callbacks differ. You can for example set a
1319different pointer to pass to the ordinary write callback by setting
1320CURLOPT_HEADERDATA(3).
1321
1322Or, you can set an entirely separate function to receive the headers, by using
1323CURLOPT_HEADERFUNCTION(3).
1324
1325The headers are passed to the callback function one by one, and you can
1326depend on that fact. It makes it easier for you to add custom header parsers
1327etc.
1328
1329"Headers" for FTP transfers equal all the FTP server responses. They are not
1330actually true headers, but in this case we pretend they are! ;-)
1331
1332# Post Transfer Information
1333
1334See curl_easy_getinfo(3).
1335
1336# The multi Interface
1337
1338The easy interface as described in detail in this document is a synchronous
1339interface that transfers one file at a time and does not return until it is
1340done.
1341
1342The multi interface, on the other hand, allows your program to transfer
1343multiple files in both directions at the same time, without forcing you to use
1344multiple threads. The name might make it seem that the multi interface is for
1345multi-threaded programs, but the truth is almost the reverse. The multi
1346interface allows a single-threaded application to perform the same kinds of
1347multiple, simultaneous transfers that multi-threaded programs can perform. It
1348allows many of the benefits of multi-threaded transfers without the complexity
1349of managing and synchronizing many threads.
1350
1351To complicate matters somewhat more, there are even two versions of the multi
1352interface. The event based one, also called multi_socket and the "normal one"
1353designed for using with select(). See the libcurl-multi.3 man page for details
1354on the multi_socket event based API, this description here is for the select()
1355oriented one.
1356
1357To use this interface, you are better off if you first understand the basics
1358of how to use the easy interface. The multi interface is simply a way to make
1359multiple transfers at the same time by adding up multiple easy handles into
1360a "multi stack".
1361
1362You create the easy handles you want, one for each concurrent transfer, and
1363you set all the options just like you learned above, and then you create a
1364multi handle with curl_multi_init(3) and add all those easy handles to
1365that multi handle with curl_multi_add_handle(3).
1366
1367When you have added the handles you have for the moment (you can still add new
1368ones at any time), you start the transfers by calling
1369curl_multi_perform(3).
1370
1371curl_multi_perform(3) is asynchronous. It only performs what can be done
1372now and then return control to your program. It is designed to never
1373block. You need to keep calling the function until all transfers are
1374completed.
1375
1376The best usage of this interface is when you do a select() on all possible
1377file descriptors or sockets to know when to call libcurl again. This also
1378makes it easy for you to wait and respond to actions on your own application's
1379sockets/handles. You figure out what to select() for by using
1380curl_multi_fdset(3), that fills in a set of *fd_set* variables for
1381you with the particular file descriptors libcurl uses for the moment.
1382
1383When you then call select(), it returns when one of the file handles signal
1384action and you then call curl_multi_perform(3) to allow libcurl to do
1385what it wants to do. Take note that libcurl does also feature some time-out
1386code so we advise you to never use long timeouts on select() before you call
1387curl_multi_perform(3) again. curl_multi_timeout(3) is provided to
1388help you get a suitable timeout period.
1389
1390Another precaution you should use: always call curl_multi_fdset(3)
1391immediately before the select() call since the current set of file descriptors
1392may change in any curl function invoke.
1393
1394If you want to stop the transfer of one of the easy handles in the stack, you
1395can use curl_multi_remove_handle(3) to remove individual easy
1396handles. Remember that easy handles should be curl_easy_cleanup(3)ed.
1397
1398When a transfer within the multi stack has finished, the counter of running
1399transfers (as filled in by curl_multi_perform(3)) decreases. When the
1400number reaches zero, all transfers are done.
1401
1402curl_multi_info_read(3) can be used to get information about completed
1403transfers. It then returns the CURLcode for each easy transfer, to allow you
1404to figure out success on each individual transfer.
1405
1406# SSL, Certificates and Other Tricks
1407
1408 [ seeding, passwords, keys, certificates, ENGINE, ca certs ]
1409
1410# Sharing Data Between Easy Handles
1411
1412You can share some data between easy handles when the easy interface is used,
1413and some data is share automatically when you use the multi interface.
1414
1415When you add easy handles to a multi handle, these easy handles automatically
1416share a lot of the data that otherwise would be kept on a per-easy handle
1417basis when the easy interface is used.
1418
1419The DNS cache is shared between handles within a multi handle, making
1420subsequent name resolving faster, and the connection pool that is kept to
1421better allow persistent connections and connection reuse is also shared. If
1422you are using the easy interface, you can still share these between specific
1423easy handles by using the share interface, see libcurl-share(3).
1424
1425Some things are never shared automatically, not within multi handles, like for
1426example cookies so the only way to share that is with the share interface.
1427
1428# Footnotes
1429
1430## [1]
1431
1432libcurl 7.10.3 and later have the ability to switch over to chunked
1433Transfer-Encoding in cases where HTTP uploads are done with data of an unknown
1434size.
1435
1436## [2]
1437
1438This happens on Windows machines when libcurl is built and used as a
1439DLL. However, you can still do this on Windows if you link with a static
1440library.
1441
1442## [3]
1443
1444The curl-config tool is generated at build-time (on Unix-like systems) and
1445should be installed with the 'make install' or similar instruction that
1446installs the library, header files, man pages etc.
1447
1448## [4]
1449
1450This behavior was different in versions before 7.17.0, where strings had to
1451remain valid past the end of the curl_easy_setopt(3) call.
1452