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