libcurl.3 12 KB

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