libcurl.3 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. .\" $Id$
  2. .\"
  3. .TH libcurl 3 "19 March 2002" "libcurl 7.9.6" "libcurl overview"
  4. .SH NAME
  5. libcurl \- client-side URL transfers
  6. .SH DESCRIPTION
  7. This is a short overview on how to use libcurl in your C programs. There are
  8. specific man pages for each function mentioned in here. There are also the
  9. \fIlibcurl-easy(3)\fP man page, the \fIlibcurl-multi(3)\fP man page, the
  10. \fIlibcurl-share(3)\fP man page and the \fIlibcurl-tutorial(3)\fP man page for
  11. in-depth understanding on how to program with libcurl.
  12. There are more than thirty custom bindings available that bring libcurl access
  13. to your favourite language. Look elsewhere for documentation on those.
  14. libcurl has a global constant environment that you must set up and
  15. maintain while using libcurl. This essentially means you call
  16. \fIcurl_global_init(3)\fP at the start of your program and
  17. \fIcurl_global_cleanup(3)\fP at the end. See GLOBAL CONSTANTS below
  18. for details.
  19. To transfer files, you always set up an "easy handle" using
  20. \fIcurl_easy_init(3)\fP, but when you want the file(s) transferred you have
  21. the option of using the "easy" interface, 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 thread. See further details in the \fIlibcurl-multi(3)\fP man page.
  31. You can have multiple easy handles share certain data, even if they are used
  32. in different threads. This magic is setup using the share interface, as
  33. described in the \fIlibcurl-share(3)\fP man page.
  34. There is also a series of other helpful functions to use, including these:
  35. .RS
  36. .IP curl_version_info()
  37. gets detailed libcurl (and other used libraries) version info
  38. .IP curl_getdate()
  39. converts a date string to time_t
  40. .IP curl_easy_getinfo()
  41. get information about a performed transfer
  42. .IP curl_formadd()
  43. helps building an HTTP form POST
  44. .IP curl_formfree()
  45. free a list built with \fIcurl_formadd(3)\fP
  46. .IP curl_slist_append()
  47. builds a linked list
  48. .IP curl_slist_free_all()
  49. frees a whole curl_slist
  50. .RE
  51. .SH "LINKING WITH LIBCURL"
  52. On unix-like machines, there's a tool named curl-config that gets installed
  53. with the rest of the curl stuff when 'make install' is performed.
  54. curl-config is added to make it easier for applications to link with libcurl
  55. and developers to learn about libcurl and how to use it.
  56. Run 'curl-config --libs' to get the (additional) linker options you need to
  57. link with the particular version of libcurl you've installed. See the
  58. \fIcurl-config(1)\fP man page for further details.
  59. Unix-like operating system that ship libcurl as part of their distributions
  60. often don't provide the curl-config tool, but simply install the library and
  61. headers in the common path for this purpose.
  62. .SH "LIBCURL SYMBOL NAMES"
  63. All public functions in the libcurl interface are prefixed with 'curl_' (with
  64. a lowercase c). You can find other functions in the library source code, but
  65. other prefixes indicate that the functions are private and may change without
  66. further notice in the next release.
  67. Only use documented functions and functionality!
  68. .SH "PORTABILITY"
  69. libcurl works
  70. .B exactly
  71. the same, on any of the platforms it compiles and builds on.
  72. .SH "THREADS"
  73. Never ever call curl-functions simultaneously using the same handle from
  74. several threads. libcurl is thread-safe and can be used in any number of
  75. threads, but you must use separate curl handles if you want to use libcurl in
  76. more than one thread simultaneously.
  77. The global environment functions are not thread-safe. See GLOBAL CONSTANTS
  78. below for details.
  79. .SH "PERSISTENT CONNECTIONS"
  80. Persistent connections means that libcurl can re-use the same connection for
  81. several transfers, if the conditions are right.
  82. libcurl will \fBalways\fP attempt to use persistent connections. Whenever you
  83. use \fIcurl_easy_perform(3)\fP or \fIcurl_multi_perform(3)\fP, libcurl will
  84. attempt to use an existing connection to do the transfer, and if none exists
  85. it'll open a new one that will be subject for re-use on a possible following
  86. call to \fIcurl_easy_perform(3)\fP or \fIcurl_multi_perform(3)\fP.
  87. To allow libcurl to take full advantage of persistent connections, you should
  88. do as many of your file transfers as possible using the same curl handle. When
  89. you call \fIcurl_easy_cleanup(3)\fP, all the possibly open connections held by
  90. libcurl will be closed and forgotten.
  91. Note that the options set with \fIcurl_easy_setopt(3)\fP will be used on
  92. every repeated \fIcurl_easy_perform(3)\fP call.
  93. .SH "GLOBAL CONSTANTS"
  94. There are a variety of constants that libcurl uses, mainly through its
  95. internal use of other libraries, which are too complicated for the
  96. library loader to set up. Therefore, a program must call a library
  97. function after the program is loaded and running to finish setting up
  98. the library code. For example, when libcurl is built for SSL
  99. capability via the GNU TLS library, there is an elaborate tree inside
  100. that library that describes the SSL protocol.
  101. \fIcurl_global_init()\fP is the function that you must call. This may
  102. allocate resources (e.g. the memory for the GNU TLS tree mentioned
  103. above), so the companion function \fIcurl_global_cleanup()\fP releases
  104. them.
  105. The basic rule for constructing a program that uses libcurl is this:
  106. Call \fIcurl_global_init()\fP, with a \fICURL_GLOBAL_ALL\fP argument,
  107. immediately after the program starts, while it is still only one
  108. thread and before it uses libcurl at all. Call
  109. \fIcurl_global_cleanup()\fP immediately before the program exits, when
  110. the program is again only one thread and after its last use of
  111. libcurl.
  112. You can call both of these multiple times, as long as all calls meet
  113. these requirements and the number of calls to each is the same.
  114. It isn't actually required that the functions be called at the beginning
  115. and end of the program -- that's just usually the easiest way to do it.
  116. It \fIis\fP required that the functions be called when no other thread
  117. in the program is running.
  118. These global constant functions are \fInot thread safe\fP, so you must
  119. not call them when any other thread in the program is running. It
  120. isn't good enough that no other thread is using libcurl at the time,
  121. because these functions internally call similar functions of other
  122. libraries, and those functions are similarly thread-unsafe. You can't
  123. generally know what these libraries are, or whether other threads are
  124. using them.
  125. The global constant situation merits special consideration when the
  126. code you are writing to use libcurl is not the main program, but rather
  127. a modular piece of a program, e.g. another library. As a module,
  128. your code doesn't know about other parts of the program -- it doesn't
  129. know whether they use libcurl or not. And its code doesn't necessarily
  130. run at the start and end of the whole program.
  131. A module like this must have global constant functions of its own,
  132. just like \fIcurl_global_init()\fP and \fIcurl_global_cleanup()\fP.
  133. The module thus has control at the beginning and end of the program
  134. and has a place to call the libcurl functions. Note that if multiple
  135. modules in the program use libcurl, they all will separately call the
  136. libcurl functions, and that's OK because only the first
  137. \fIcurl_global_init()\fP and the last \fIcurl_global_cleanup()\fP in a
  138. program change anything. (libcurl uses a reference count in static
  139. memory).
  140. In a C++ module, it is common to deal with the global constant
  141. situation by defining a special class that represents the global
  142. constant environment of the module. A program always has exactly one
  143. object of the class, in static storage. That way, the program
  144. automatically calls the constructor of the object as the program
  145. starts up and the destructor as it terminates. As the author of this
  146. libcurl-using module, you can make the constructor call
  147. \fIcurl_global_init()\fP and the destructor call
  148. \fIcurl_global_cleanup()\fP and satisfy libcurl's requirements without
  149. your user having to think about it.
  150. \fIcurl_global_init()\fP has an argument that tells what particular
  151. parts of the global constant environment to set up. In order to
  152. successfully use any value except \fICURL_GLOBAL_ALL\fP (which says to
  153. set up the whole thing), you must have specific knowledge of internal
  154. workings of libcurl and all other parts of the program of which it is
  155. part.
  156. A special part of the global constant environment is the identity of
  157. the memory allocator. \fIcurl_global_init()\fP selects the system
  158. default memory allocator, but you can use \fIcurl_global_init_mem()\fP
  159. to supply one of your own. However, there is no way to use
  160. \fIcurl_global_init_mem()\fP in a modular program -- all modules in
  161. the program that might use libcurl would have to agree on one
  162. allocator.
  163. There is a failsafe in libcurl that makes it usable in simple
  164. situations without you having to worry about the global constant
  165. environment at all: \fIcurl_easy_init()\fP sets up the environment
  166. itself if it hasn't been done yet. The resources it acquires to do so
  167. get released by the operating system automatically when the program
  168. exits.
  169. This failsafe feature exists mainly for backward compatibility because
  170. there was a time when the global functions didn't exist. Because it
  171. is sufficient only in the simplest of programs, it is not recommended
  172. for any program to rely on it.