blob: 6618734b43bf8c97b9ec4532119df1230ff128c0 [file] [log] [blame]
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001.\" **************************************************************************
2.\" * _ _ ____ _
3.\" * Project ___| | | | _ \| |
4.\" * / __| | | | |_) | |
5.\" * | (__| |_| | _ <| |___
6.\" * \___|\___/|_| \_\_____|
7.\" *
Alex Deymo8f1a2142016-06-28 14:49:26 -07008.\" * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07009.\" *
10.\" * This software is licensed as described in the file COPYING, which
11.\" * you should have received as part of this distribution. The terms
Alex Deymo8f1a2142016-06-28 14:49:26 -070012.\" * are also available at https://curl.haxx.se/docs/copyright.html.
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070013.\" *
14.\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15.\" * copies of the Software, and permit persons to whom the Software is
16.\" * furnished to do so, under the terms of the COPYING file.
17.\" *
18.\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19.\" * KIND, either express or implied.
20.\" *
21.\" **************************************************************************
Lucas Eckels9bd90e62012-08-06 15:07:02 -070022.TH libcurl 3 "19 March 2002" "libcurl 7.9.6" "libcurl overview"
23.SH NAME
24libcurl \- client-side URL transfers
25.SH DESCRIPTION
26This is a short overview on how to use libcurl in your C programs. There are
27specific man pages for each function mentioned in here. There are also the
28\fIlibcurl-easy(3)\fP man page, the \fIlibcurl-multi(3)\fP man page, the
29\fIlibcurl-share(3)\fP man page and the \fIlibcurl-tutorial(3)\fP man page for
30in-depth understanding on how to program with libcurl.
31
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070032There are many bindings available that bring libcurl access to your favourite
33language. Look elsewhere for documentation on those.
Lucas Eckels9bd90e62012-08-06 15:07:02 -070034
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070035libcurl has a global constant environment that you must set up and maintain
36while using libcurl. This essentially means you call
Lucas Eckels9bd90e62012-08-06 15:07:02 -070037\fIcurl_global_init(3)\fP at the start of your program and
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070038\fIcurl_global_cleanup(3)\fP at the end. See \fBGLOBAL CONSTANTS\fP below for
39details.
Lucas Eckels9bd90e62012-08-06 15:07:02 -070040
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070041To transfer files, you create an "easy handle" using \fIcurl_easy_init(3)\fP
42for a single individual transfer (in either direction). You then set your
43desired set of options in that handle with \fIcurl_easy_setopt(3)\fP. Options
44you set with \fIcurl_easy_setopt(3)\fP stick. They will be used on every
45repeated use of this handle until you either change the option, or you reset
46them all with \fIcurl_easy_reset(3)\fP.
47
48To actually transfer data you have the option of using the "easy" interface,
49or the "multi" interface.
Lucas Eckels9bd90e62012-08-06 15:07:02 -070050
51The easy interface is a synchronous interface with which you call
52\fIcurl_easy_perform(3)\fP and let it perform the transfer. When it is
53completed, the function returns and you can continue. More details are found in
54the \fIlibcurl-easy(3)\fP man page.
55
56The multi interface on the other hand is an asynchronous interface, that you
57call and that performs only a little piece of the transfer on each invoke. It
58is perfect if you want to do things while the transfer is in progress, or
59similar. The multi interface allows you to select() on libcurl action, and
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070060even to easily download multiple files simultaneously using a single
61thread. See further details in the \fIlibcurl-multi(3)\fP man page.
Lucas Eckels9bd90e62012-08-06 15:07:02 -070062
63You can have multiple easy handles share certain data, even if they are used
64in different threads. This magic is setup using the share interface, as
65described in the \fIlibcurl-share(3)\fP man page.
66
67There is also a series of other helpful functions to use, including these:
68.RS
69.IP curl_version_info()
70gets detailed libcurl (and other used libraries) version info
71.IP curl_getdate()
72converts a date string to time_t
73.IP curl_easy_getinfo()
74get information about a performed transfer
75.IP curl_formadd()
76helps building an HTTP form POST
77.IP curl_formfree()
78free a list built with \fIcurl_formadd(3)\fP
79.IP curl_slist_append()
80builds a linked list
81.IP curl_slist_free_all()
82frees a whole curl_slist
83.RE
84
85.SH "LINKING WITH LIBCURL"
86On unix-like machines, there's a tool named curl-config that gets installed
87with the rest of the curl stuff when 'make install' is performed.
88
89curl-config is added to make it easier for applications to link with libcurl
90and developers to learn about libcurl and how to use it.
91
92Run 'curl-config --libs' to get the (additional) linker options you need to
93link with the particular version of libcurl you've installed. See the
94\fIcurl-config(1)\fP man page for further details.
95
96Unix-like operating system that ship libcurl as part of their distributions
97often don't provide the curl-config tool, but simply install the library and
98headers in the common path for this purpose.
99
Alex Deymo8f1a2142016-06-28 14:49:26 -0700100Many Linux and similar systems use pkg-config to provide build and link
101options about libraries and libcurl supports that as well.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700102.SH "LIBCURL SYMBOL NAMES"
103All public functions in the libcurl interface are prefixed with 'curl_' (with
104a lowercase c). You can find other functions in the library source code, but
105other prefixes indicate that the functions are private and may change without
106further notice in the next release.
107
108Only use documented functions and functionality!
109.SH "PORTABILITY"
110libcurl works
111.B exactly
112the same, on any of the platforms it compiles and builds on.
113.SH "THREADS"
Alex Deymo8f1a2142016-06-28 14:49:26 -0700114libcurl is thread safe but there are a few exceptions. Refer to
115\fIlibcurl-thread(3)\fP for more information.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700116
117.SH "PERSISTENT CONNECTIONS"
118Persistent connections means that libcurl can re-use the same connection for
119several transfers, if the conditions are right.
120
121libcurl will \fBalways\fP attempt to use persistent connections. Whenever you
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700122use \fIcurl_easy_perform(3)\fP or \fIcurl_multi_perform(3)\fP etc, libcurl
123will attempt to use an existing connection to do the transfer, and if none
124exists it'll open a new one that will be subject for re-use on a possible
125following call to \fIcurl_easy_perform(3)\fP or \fIcurl_multi_perform(3)\fP.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700126
127To allow libcurl to take full advantage of persistent connections, you should
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700128do as many of your file transfers as possible using the same handle.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700129
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700130If you use the easy interface, and you call \fIcurl_easy_cleanup(3)\fP, all
131the possibly open connections held by libcurl will be closed and forgotten.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700132
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700133When you've created a multi handle and are using the multi interface, the
134connection pool is instead kept in the multi handle so closing and creating
135new easy handles to do transfers will not affect them. Instead all added easy
136handles can take advantage of the single shared pool.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700137.SH "GLOBAL CONSTANTS"
138There are a variety of constants that libcurl uses, mainly through its
139internal use of other libraries, which are too complicated for the
140library loader to set up. Therefore, a program must call a library
141function after the program is loaded and running to finish setting up
142the library code. For example, when libcurl is built for SSL
143capability via the GNU TLS library, there is an elaborate tree inside
144that library that describes the SSL protocol.
145
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700146\fIcurl_global_init(3)\fP is the function that you must call. This may
147allocate resources (e.g. the memory for the GNU TLS tree mentioned above), so
148the companion function \fIcurl_global_cleanup(3)\fP releases them.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700149
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700150The basic rule for constructing a program that uses libcurl is this: Call
151\fIcurl_global_init(3)\fP, with a \fICURL_GLOBAL_ALL\fP argument, immediately
152after the program starts, while it is still only one thread and before it uses
153libcurl at all. Call \fIcurl_global_cleanup(3)\fP immediately before the
154program exits, when the program is again only one thread and after its last
155use of libcurl.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700156
157You can call both of these multiple times, as long as all calls meet
158these requirements and the number of calls to each is the same.
159
160It isn't actually required that the functions be called at the beginning
161and end of the program -- that's just usually the easiest way to do it.
162It \fIis\fP required that the functions be called when no other thread
163in the program is running.
164
165These global constant functions are \fInot thread safe\fP, so you must
166not call them when any other thread in the program is running. It
167isn't good enough that no other thread is using libcurl at the time,
168because these functions internally call similar functions of other
169libraries, and those functions are similarly thread-unsafe. You can't
170generally know what these libraries are, or whether other threads are
171using them.
172
173The global constant situation merits special consideration when the
174code you are writing to use libcurl is not the main program, but rather
175a modular piece of a program, e.g. another library. As a module,
176your code doesn't know about other parts of the program -- it doesn't
177know whether they use libcurl or not. And its code doesn't necessarily
178run at the start and end of the whole program.
179
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700180A module like this must have global constant functions of its own, just like
181\fIcurl_global_init(3)\fP and \fIcurl_global_cleanup(3)\fP. The module thus
182has control at the beginning and end of the program and has a place to call
183the libcurl functions. Note that if multiple modules in the program use
184libcurl, they all will separately call the libcurl functions, and that's OK
185because only the first \fIcurl_global_init(3)\fP and the last
186\fIcurl_global_cleanup(3)\fP in a program change anything. (libcurl uses a
187reference count in static memory).
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700188
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700189In a C++ module, it is common to deal with the global constant situation by
190defining a special class that represents the global constant environment of
191the module. A program always has exactly one object of the class, in static
192storage. That way, the program automatically calls the constructor of the
193object as the program starts up and the destructor as it terminates. As the
194author of this libcurl-using module, you can make the constructor call
195\fIcurl_global_init(3)\fP and the destructor call \fIcurl_global_cleanup(3)\fP
196and satisfy libcurl's requirements without your user having to think about it.
Alex Deymo8f1a2142016-06-28 14:49:26 -0700197(Caveat: If you are initializing libcurl from a Windows DLL you should not
198initialize it from DllMain or a static initializer because Windows holds the
199loader lock during that time and it could cause a deadlock.)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700200
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700201\fIcurl_global_init(3)\fP has an argument that tells what particular parts of
202the global constant environment to set up. In order to successfully use any
203value except \fICURL_GLOBAL_ALL\fP (which says to set up the whole thing), you
204must have specific knowledge of internal workings of libcurl and all other
205parts of the program of which it is part.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700206
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700207A special part of the global constant environment is the identity of the
208memory allocator. \fIcurl_global_init(3)\fP selects the system default memory
209allocator, but you can use \fIcurl_global_init_mem(3)\fP to supply one of your
210own. However, there is no way to use \fIcurl_global_init_mem(3)\fP in a
211modular program -- all modules in the program that might use libcurl would
212have to agree on one allocator.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700213
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700214There is a failsafe in libcurl that makes it usable in simple situations
215without you having to worry about the global constant environment at all:
216\fIcurl_easy_init(3)\fP sets up the environment itself if it hasn't been done
217yet. The resources it acquires to do so get released by the operating system
218automatically when the program exits.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700219
220This failsafe feature exists mainly for backward compatibility because
221there was a time when the global functions didn't exist. Because it
222is sufficient only in the simplest of programs, it is not recommended
223for any program to rely on it.