~ubuntu-branches/ubuntu/vivid/curl/vivid

« back to all changes in this revision

Viewing changes to docs/libcurl/libcurl.html

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Schuldei
  • Date: 2009-04-02 23:35:45 UTC
  • mto: (1.2.1 upstream) (3.2.3 sid)
  • mto: This revision was merged to the branch mainline in revision 38.
  • Revision ID: james.westby@ubuntu.com-20090402233545-geixkwhe3izccjt7
Tags: upstream-7.19.4
ImportĀ upstreamĀ versionĀ 7.19.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
 
46
46
<p class="level0"><a name="NAME"></a><h2 class="nroffsh">NAME</h2>
47
47
<p class="level0">libcurl - client-side URL transfers <a name="DESCRIPTION"></a><h2 class="nroffsh">DESCRIPTION</h2>
48
 
<p class="level0">This is an short overview on how to use libcurl in your C programs. There are specific man pages for each function mentioned in here. There are also the <span Class="emphasis">libcurl-easy(3)</span> man page, the <span Class="emphasis">libcurl-multi(3)</span> man page, the <span Class="emphasis">libcurl-share(3)</span> man page and the <span Class="emphasis">libcurl-tutorial(3)</span> man page for in-depth understanding on how to program with libcurl. 
 
48
<p class="level0">This is a short overview on how to use libcurl in your C programs. There are specific man pages for each function mentioned in here. There are also the <span Class="emphasis">libcurl-easy(3)</span> man page, the <span Class="emphasis">libcurl-multi(3)</span> man page, the <span Class="emphasis">libcurl-share(3)</span> man page and the <span Class="emphasis">libcurl-tutorial(3)</span> man page for in-depth understanding on how to program with libcurl. 
49
49
<p class="level0">There are more than thirty custom bindings available that bring libcurl access to your favourite language. Look elsewhere for documentation on those. 
50
50
<p class="level0">libcurl has a global constant environment that you must set up and maintain while using libcurl.  This essentially means you call <a class="emphasis" href="./curl_global_init.html">curl_global_init(3)</a> at the start of your program and <a class="emphasis" href="./curl_global_cleanup.html">curl_global_cleanup(3)</a> at the end.  See GLOBAL CONSTANTS below for details. 
51
51
<p class="level0">To transfer files, you always set up an "easy handle" using <a class="emphasis" href="./curl_easy_init.html">curl_easy_init(3)</a>, but when you want the file(s) transferred you have the option of using the "easy" interface, or the "multi" interface. 
52
 
<p class="level0">The easy interface is a synchronous interface with which you call <a class="emphasis" href="./curl_easy_perform.html">curl_easy_perform(3)</a> and let it perform the transfer. When it is completed, the function return and you can continue. More details are found in the <span Class="emphasis">libcurl-easy(3)</span> man page. 
53
 
<p class="level0">The multi interface on the other hand is an asynchronous interface, that you call and that performs only a little piece of the transfer on each invoke. It is perfect if you want to do things while the transfer is in progress, or similar. The multi interface allows you to select() on libcurl action, and even to easily download multiple files simultaneously using a single thread. See further deails in the <span Class="emphasis">libcurl-multi(3)</span> man page. 
 
52
<p class="level0">The easy interface is a synchronous interface with which you call <a class="emphasis" href="./curl_easy_perform.html">curl_easy_perform(3)</a> and let it perform the transfer. When it is completed, the function returns and you can continue. More details are found in the <span Class="emphasis">libcurl-easy(3)</span> man page. 
 
53
<p class="level0">The multi interface on the other hand is an asynchronous interface, that you call and that performs only a little piece of the transfer on each invoke. It is perfect if you want to do things while the transfer is in progress, or similar. The multi interface allows you to select() on libcurl action, and even to easily download multiple files simultaneously using a single thread. See further details in the <span Class="emphasis">libcurl-multi(3)</span> man page. 
54
54
<p class="level0">You can have multiple easy handles share certain data, even if they are used in different threads. This magic is setup using the share interface, as described in the <span Class="emphasis">libcurl-share(3)</span> man page. 
55
55
<p class="level0">There is also a series of other helpful functions to use, including these: 
56
56
<p class="level1">
84
84
<p class="level0">Persistent connections means that libcurl can re-use the same connection for several transfers, if the conditions are right. 
85
85
<p class="level0">libcurl will <span Class="bold">always</span> attempt to use persistent connections. Whenever you use <a class="emphasis" href="./curl_easy_perform.html">curl_easy_perform(3)</a> or <a class="emphasis" href="./curl_multi_perform.html">curl_multi_perform(3)</a>, libcurl will attempt to use an existing connection to do the transfer, and if none exists it'll open a new one that will be subject for re-use on a possible following call to <a class="emphasis" href="./curl_easy_perform.html">curl_easy_perform(3)</a> or <a class="emphasis" href="./curl_multi_perform.html">curl_multi_perform(3)</a>. 
86
86
<p class="level0">To allow libcurl to take full advantage of persistent connections, you should do as many of your file transfers as possible using the same curl handle. When you call <a class="emphasis" href="./curl_easy_cleanup.html">curl_easy_cleanup(3)</a>, all the possibly open connections held by libcurl will be closed and forgotten. 
87
 
<p class="level0">Note that the options set with <a class="emphasis" href="./curl_easy_setopt.html">curl_easy_setopt(3)</a> will be used in on every repeated <a class="emphasis" href="./curl_easy_perform.html">curl_easy_perform(3)</a> call. 
 
87
<p class="level0">Note that the options set with <a class="emphasis" href="./curl_easy_setopt.html">curl_easy_setopt(3)</a> will be used on every repeated <a class="emphasis" href="./curl_easy_perform.html">curl_easy_perform(3)</a> call. 
88
88
<p class="level0"><a name="GLOBAL"></a><h2 class="nroffsh">GLOBAL CONSTANTS</h2>
89
89
<p class="level0">There are a variety of constants that libcurl uses, mainly through its internal use of other libraries, which are too complicated for the library loader to set up.  Therefore, a program must call a library function after the program is loaded and running to finish setting up the library code.  For example, when libcurl is built for SSL capability via the GNU TLS library, there is an elaborate tree inside that library that describes the SSL protocol. 
90
90
<p class="level0"><span Class="emphasis">curl_global_init()</span> is the function that you must call.  This may allocate resources (e.g. the memory for the GNU TLS tree mentioned above), so the companion function <span Class="emphasis">curl_global_cleanup()</span> releases them. 
93
93
<p class="level0">It isn't actually required that the functions be called at the beginning and end of the program -- that's just usually the easiest way to do it. It <span Class="emphasis">is</span> required that the functions be called when no other thread in the program is running. 
94
94
<p class="level0">These global constant functions are <span Class="emphasis">not thread safe</span>, so you must not call them when any other thread in the program is running.  It isn't good enough that no other thread is using libcurl at the time, because these functions internally call similar functions of other libraries, and those functions are similarly thread-unsafe.  You can't generally know what these libraries are, or whether other threads are using them. 
95
95
<p class="level0">The global constant situation merits special consideration when the code you are writing to use libcurl is not the main program, but rather a modular piece of a program, e.g. another library.  As a module, your code doesn't know about other parts of the program -- it doesn't know whether they use libcurl or not.  And its code doesn't necessarily run at the start and end of the whole program. 
96
 
<p class="level0">A module like this must have global constant functions of its own, just like <span Class="emphasis">curl_global_init()</span> and <span Class="emphasis">curl_global_cleanup()</span>. The module thus has control at the beginning and end of the program and has a place to call the libcurl functions.  Note that if multiple modules in the program use libcurl, they all will separately call the libcurl functions, and that's OK because only the first <span Class="emphasis">curl_global_init()</span> and the last <span Class="emphasis">curl_global_cleanup()</span> in a program changes anything.  (libcurl uses a reference count in static memory). 
 
96
<p class="level0">A module like this must have global constant functions of its own, just like <span Class="emphasis">curl_global_init()</span> and <span Class="emphasis">curl_global_cleanup()</span>. The module thus has control at the beginning and end of the program and has a place to call the libcurl functions.  Note that if multiple modules in the program use libcurl, they all will separately call the libcurl functions, and that's OK because only the first <span Class="emphasis">curl_global_init()</span> and the last <span Class="emphasis">curl_global_cleanup()</span> in a program change anything.  (libcurl uses a reference count in static memory). 
97
97
<p class="level0">In a C++ module, it is common to deal with the global constant situation by defining a special class that represents the global constant environment of the module.  A program always has exactly one object of the class, in static storage.  That way, the program automatically calls the constructor of the object as the program starts up and the destructor as it terminates.  As the author of this libcurl-using module, you can make the constructor call <span Class="emphasis">curl_global_init()</span> and the destructor call <span Class="emphasis">curl_global_cleanup()</span> and satisfy libcurl's requirements without your user having to think about it. 
98
98
<p class="level0"><span Class="emphasis">curl_global_init()</span> has an argument that tells what particular parts of the global constant environment to set up.  In order to successfully use any value except <span Class="emphasis">CURL_GLOBAL_ALL</span> (which says to set up the whole thing), you must have specific knowledge of internal workings of libcurl and all other parts of the program of which it is part. 
99
99
<p class="level0">A special part of the global constant environment is the identity of the memory allocator.  <span Class="emphasis">curl_global_init()</span> selects the system default memory allocator, but you can use <span Class="emphasis">curl_global_init_mem()</span> to supply one of your own.  However, there is no way to use <span Class="emphasis">curl_global_init_mem()</span> in a modular program -- all modules in the program that might use libcurl would have to agree on one allocator.