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