blob: 275cd3fae25c39567cda6fb19e89e880f3606c07 [file] [log] [blame]
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001.\" **************************************************************************
2.\" * _ _ ____ _
3.\" * Project ___| | | | _ \| |
4.\" * / __| | | | |_) | |
5.\" * | (__| |_| | _ <| |___
6.\" * \___|\___/|_| \_\_____|
7.\" *
Elliott Hughes34dd5f42021-08-10 13:01:18 -07008.\" * Copyright (C) 1998 - 2021, 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
Elliott Hughes34dd5f42021-08-10 13:01:18 -070012.\" * are also available at https://curl.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.\" **************************************************************************
Elliott Hughes34dd5f42021-08-10 13:01:18 -070022.TH libcurl 3 "May 05, 2021" "libcurl 7.78.0" "libcurl overview"
Elliott Hughes82be86d2017-09-20 17:00:17 -070023
Lucas Eckels9bd90e62012-08-06 15:07:02 -070024.SH NAME
25libcurl \- client-side URL transfers
26.SH DESCRIPTION
27This is a short overview on how to use libcurl in your C programs. There are
Elliott Hughes34dd5f42021-08-10 13:01:18 -070028specific man pages for each function mentioned in here. See
29\fIlibcurl-easy(3)\fP, \fIlibcurl-multi(3)\fP, \fIlibcurl-share(3)\fP,
30\fIlibcurl-url(3)\fP and \fIlibcurl-tutorial(3)\fP for in-depth understanding
31on how to program with libcurl.
Lucas Eckels9bd90e62012-08-06 15:07:02 -070032
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070033There are many bindings available that bring libcurl access to your favourite
34language. Look elsewhere for documentation on those.
Lucas Eckels9bd90e62012-08-06 15:07:02 -070035
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070036libcurl has a global constant environment that you must set up and maintain
37while using libcurl. This essentially means you call
Lucas Eckels9bd90e62012-08-06 15:07:02 -070038\fIcurl_global_init(3)\fP at the start of your program and
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070039\fIcurl_global_cleanup(3)\fP at the end. See \fBGLOBAL CONSTANTS\fP below for
40details.
Lucas Eckels9bd90e62012-08-06 15:07:02 -070041
Alex Deymo486467e2017-12-19 19:04:07 +010042If libcurl was compiled with support for multiple SSL backends, the function
43\fIcurl_global_sslset(3)\fP can be called before \fIcurl_global_init(3)\fP
44to select the active SSL backend.
45
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070046To transfer files, you create an "easy handle" using \fIcurl_easy_init(3)\fP
47for a single individual transfer (in either direction). You then set your
48desired set of options in that handle with \fIcurl_easy_setopt(3)\fP. Options
49you set with \fIcurl_easy_setopt(3)\fP stick. They will be used on every
50repeated use of this handle until you either change the option, or you reset
51them all with \fIcurl_easy_reset(3)\fP.
52
53To actually transfer data you have the option of using the "easy" interface,
54or the "multi" interface.
Lucas Eckels9bd90e62012-08-06 15:07:02 -070055
56The easy interface is a synchronous interface with which you call
57\fIcurl_easy_perform(3)\fP and let it perform the transfer. When it is
58completed, the function returns and you can continue. More details are found in
59the \fIlibcurl-easy(3)\fP man page.
60
61The multi interface on the other hand is an asynchronous interface, that you
62call and that performs only a little piece of the transfer on each invoke. It
63is perfect if you want to do things while the transfer is in progress, or
64similar. The multi interface allows you to select() on libcurl action, and
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070065even to easily download multiple files simultaneously using a single
66thread. See further details in the \fIlibcurl-multi(3)\fP man page.
Lucas Eckels9bd90e62012-08-06 15:07:02 -070067
68You can have multiple easy handles share certain data, even if they are used
69in different threads. This magic is setup using the share interface, as
70described in the \fIlibcurl-share(3)\fP man page.
71
72There is also a series of other helpful functions to use, including these:
73.RS
74.IP curl_version_info()
75gets detailed libcurl (and other used libraries) version info
76.IP curl_getdate()
77converts a date string to time_t
78.IP curl_easy_getinfo()
79get information about a performed transfer
80.IP curl_formadd()
81helps building an HTTP form POST
82.IP curl_formfree()
83free a list built with \fIcurl_formadd(3)\fP
84.IP curl_slist_append()
85builds a linked list
86.IP curl_slist_free_all()
87frees a whole curl_slist
Elliott Hughes34dd5f42021-08-10 13:01:18 -070088.IP curl_url_set()
89parses a URL
Lucas Eckels9bd90e62012-08-06 15:07:02 -070090.RE
91
92.SH "LINKING WITH LIBCURL"
93On unix-like machines, there's a tool named curl-config that gets installed
94with the rest of the curl stuff when 'make install' is performed.
95
96curl-config is added to make it easier for applications to link with libcurl
97and developers to learn about libcurl and how to use it.
98
99Run 'curl-config --libs' to get the (additional) linker options you need to
100link with the particular version of libcurl you've installed. See the
101\fIcurl-config(1)\fP man page for further details.
102
103Unix-like operating system that ship libcurl as part of their distributions
104often don't provide the curl-config tool, but simply install the library and
105headers in the common path for this purpose.
106
Alex Deymo8f1a2142016-06-28 14:49:26 -0700107Many Linux and similar systems use pkg-config to provide build and link
108options about libraries and libcurl supports that as well.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700109.SH "LIBCURL SYMBOL NAMES"
110All public functions in the libcurl interface are prefixed with 'curl_' (with
111a lowercase c). You can find other functions in the library source code, but
112other prefixes indicate that the functions are private and may change without
113further notice in the next release.
114
115Only use documented functions and functionality!
116.SH "PORTABILITY"
117libcurl works
118.B exactly
119the same, on any of the platforms it compiles and builds on.
120.SH "THREADS"
Alex Deymo8f1a2142016-06-28 14:49:26 -0700121libcurl is thread safe but there are a few exceptions. Refer to
122\fIlibcurl-thread(3)\fP for more information.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700123
124.SH "PERSISTENT CONNECTIONS"
125Persistent connections means that libcurl can re-use the same connection for
126several transfers, if the conditions are right.
127
128libcurl will \fBalways\fP attempt to use persistent connections. Whenever you
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700129use \fIcurl_easy_perform(3)\fP or \fIcurl_multi_perform(3)\fP etc, libcurl
130will attempt to use an existing connection to do the transfer, and if none
131exists it'll open a new one that will be subject for re-use on a possible
132following call to \fIcurl_easy_perform(3)\fP or \fIcurl_multi_perform(3)\fP.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700133
134To allow libcurl to take full advantage of persistent connections, you should
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700135do as many of your file transfers as possible using the same handle.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700136
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700137If you use the easy interface, and you call \fIcurl_easy_cleanup(3)\fP, all
138the possibly open connections held by libcurl will be closed and forgotten.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700139
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700140When you've created a multi handle and are using the multi interface, the
141connection pool is instead kept in the multi handle so closing and creating
142new easy handles to do transfers will not affect them. Instead all added easy
143handles can take advantage of the single shared pool.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700144.SH "GLOBAL CONSTANTS"
145There are a variety of constants that libcurl uses, mainly through its
146internal use of other libraries, which are too complicated for the
147library loader to set up. Therefore, a program must call a library
148function after the program is loaded and running to finish setting up
149the library code. For example, when libcurl is built for SSL
150capability via the GNU TLS library, there is an elaborate tree inside
151that library that describes the SSL protocol.
152
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700153\fIcurl_global_init(3)\fP is the function that you must call. This may
154allocate resources (e.g. the memory for the GNU TLS tree mentioned above), so
155the companion function \fIcurl_global_cleanup(3)\fP releases them.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700156
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700157The basic rule for constructing a program that uses libcurl is this: Call
158\fIcurl_global_init(3)\fP, with a \fICURL_GLOBAL_ALL\fP argument, immediately
159after the program starts, while it is still only one thread and before it uses
160libcurl at all. Call \fIcurl_global_cleanup(3)\fP immediately before the
161program exits, when the program is again only one thread and after its last
162use of libcurl.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700163
164You can call both of these multiple times, as long as all calls meet
165these requirements and the number of calls to each is the same.
166
167It isn't actually required that the functions be called at the beginning
168and end of the program -- that's just usually the easiest way to do it.
169It \fIis\fP required that the functions be called when no other thread
170in the program is running.
171
172These global constant functions are \fInot thread safe\fP, so you must
173not call them when any other thread in the program is running. It
174isn't good enough that no other thread is using libcurl at the time,
175because these functions internally call similar functions of other
176libraries, and those functions are similarly thread-unsafe. You can't
177generally know what these libraries are, or whether other threads are
178using them.
179
180The global constant situation merits special consideration when the
181code you are writing to use libcurl is not the main program, but rather
182a modular piece of a program, e.g. another library. As a module,
183your code doesn't know about other parts of the program -- it doesn't
184know whether they use libcurl or not. And its code doesn't necessarily
185run at the start and end of the whole program.
186
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700187A module like this must have global constant functions of its own, just like
188\fIcurl_global_init(3)\fP and \fIcurl_global_cleanup(3)\fP. The module thus
189has control at the beginning and end of the program and has a place to call
190the libcurl functions. Note that if multiple modules in the program use
191libcurl, they all will separately call the libcurl functions, and that's OK
192because only the first \fIcurl_global_init(3)\fP and the last
193\fIcurl_global_cleanup(3)\fP in a program change anything. (libcurl uses a
194reference count in static memory).
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700195
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700196In a C++ module, it is common to deal with the global constant situation by
197defining a special class that represents the global constant environment of
198the module. A program always has exactly one object of the class, in static
199storage. That way, the program automatically calls the constructor of the
200object as the program starts up and the destructor as it terminates. As the
201author of this libcurl-using module, you can make the constructor call
202\fIcurl_global_init(3)\fP and the destructor call \fIcurl_global_cleanup(3)\fP
203and satisfy libcurl's requirements without your user having to think about it.
Alex Deymo8f1a2142016-06-28 14:49:26 -0700204(Caveat: If you are initializing libcurl from a Windows DLL you should not
205initialize it from DllMain or a static initializer because Windows holds the
206loader lock during that time and it could cause a deadlock.)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700207
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700208\fIcurl_global_init(3)\fP has an argument that tells what particular parts of
209the global constant environment to set up. In order to successfully use any
210value except \fICURL_GLOBAL_ALL\fP (which says to set up the whole thing), you
211must have specific knowledge of internal workings of libcurl and all other
212parts of the program of which it is part.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700213
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700214A special part of the global constant environment is the identity of the
215memory allocator. \fIcurl_global_init(3)\fP selects the system default memory
216allocator, but you can use \fIcurl_global_init_mem(3)\fP to supply one of your
217own. However, there is no way to use \fIcurl_global_init_mem(3)\fP in a
218modular program -- all modules in the program that might use libcurl would
219have to agree on one allocator.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700220
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700221There is a failsafe in libcurl that makes it usable in simple situations
222without you having to worry about the global constant environment at all:
223\fIcurl_easy_init(3)\fP sets up the environment itself if it hasn't been done
224yet. The resources it acquires to do so get released by the operating system
225automatically when the program exits.
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700226
227This failsafe feature exists mainly for backward compatibility because
228there was a time when the global functions didn't exist. Because it
229is sufficient only in the simplest of programs, it is not recommended
230for any program to rely on it.