libcurl.3 11 KB

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