Skip to content
FAQ 62.4 KiB
Newer Older
  4.16 My HTTP POST or PUT requests are slow!

  libcurl makes all POST and PUT requests (except for POST requests with a
  very tiny request body) use the "Expect: 100-continue" header. This header
  allows the server to deny the operation early so that libcurl can bail out
  already before having to send any data. This is useful in authentication
  cases and others.

  However, many servers don't implement the Expect: stuff properly and if the
  server doesn't respond (positively) within 1 second libcurl will continue
  and send off the data anyway.

  You can disable libcurl's use of the Expect: header the same way you disable
  any header, using -H / CURLOPT_HTTPHEADER, or by forcing it to use HTTP 1.0.
  4.17 Non-functional connect timeouts

  In most Windows setups having a timeout longer than 21 seconds make no
  difference, as it will only send 3 TCP SYN packets and no more. The second
  packet sent three seconds after the first and the third six seconds after
  the second.  No more than three packets are sent, no matter how long the
  timeout is set.

  See option TcpMaxConnectRetransmissions on this page:
  http://support.microsoft.com/?scid=kb%3Ben-us%3B175523&x=6&y=7

  Also, even on non-Windows systems there may run a firewall or anti-virus
  software or similar that accepts the connection but does not actually do
  anything else. This will make (lib)curl to consider the connection connected
  and thus the connect timeout won't trigger.

  4.18 file:// URLs containing drive letters (Windows, NetWare)

  When using cURL to try to download a local file, one might use a URL
  in this format:

  file://D:/blah.txt

  You'll find that even if D:\blah.txt does exist, cURL returns a 'file
  not found' error.

  According to RFC 1738 (http://www.faqs.org/rfcs/rfc1738.html),
  file:// URLs must contain a host component, but it is ignored by
  most implementations. In the above example, 'D:' is treated as the
  host component, and is taken away. Thus, cURL tries to open '/blah.txt'.
  If your system is installed to drive C:, that will resolve to 'C:\blah.txt',
  and if that doesn't exist you will get the not found error.

  To fix this problem, use file:// URLs with *three* leading slashes:

  file:///D:/blah.txt

  Alternatively, if it makes more sense, specify 'localhost' as the host
  component:

  file://localhost/D:/blah.txt

  In either case, cURL should now be looking for the correct file.
  4.19 Why doesn't cURL return an error when the network cable is unplugged?

  Unplugging a cable is not an error situation. The TCP/IP protocol stack
  was designed to be fault tolerant, so even though there may be a physical
  break somewhere the connection shouldn't be affected, just possibly
  delayed.  Eventually, the physical break will be fixed or the data will be
  re-routed around the physical problem through another path.

  In such cases, the TCP/IP stack is responsible for detecting when the
  network connection is irrevocably lost. Since with some protocols it is
  perfectly legal for the client wait indefinitely for data, the stack may
  never report a problem, and even when it does, it can take up to 20 minutes
  for it to detect an issue.  The curl option --keepalive-time enables
  keep-alive support in the TCP/IP stack which makes it periodically probe the
  connection to make sure it is still available to send data. That should
  reliably detect any TCP/IP network failure.

  But even that won't detect the network going down before the TCP/IP
  connection is established (e.g. during a DNS lookup) or using protocols that
  don't use TCP.  To handle those situations, curl offers a number of timeouts
  on its own. --speed-limit/--speed-time will abort if the data transfer rate
  falls too low, and --connect-timeout and --max-time can be used to put an
  overall timeout on the connection phase or the entire transfer.

  A libcurl-using application running in a known physical environment (e.g.
  an embedded device with only a single network connection) may want to act
  immediately if its lone network connection goes down.  That can be achieved
  by having the application monitor the network connection on its own using an
  OS-specific mechanism, then signalling libcurl to abort (see also item 5.13).
  
Daniel Stenberg's avatar
Daniel Stenberg committed
5. libcurl Issues
  We have written the libcurl code specifically adjusted for multi-threaded
  programs. libcurl will use thread-safe functions instead of non-safe ones if
  your system has such.  Note that you must never share the same handle in
  multiple threads.

  If you use a OpenSSL-powered libcurl in a multi-threaded environment, you
  need to provide one or two locking functions:

    http://www.openssl.org/docs/crypto/threads.html

  If you use a GnuTLS-powered libcurl in a multi-threaded environment, you
  need to provide locking function(s) for libgcrypt (which is used by GnuTLS
  for the crypto functions).

Daniel Stenberg's avatar
Daniel Stenberg committed
    http://www.gnu.org/software/gnutls/manual/html_node/Multi_002dthreaded-applications.html
  No special locking is needed with a NSS-powered libcurl. NSS is thread-safe.

  5.2 How can I receive all data into a large memory chunk?

  [ See also the examples/getinmemory.c source ]

  You are in full control of the callback function that gets called every time
  there is data received from the remote server. You can make that callback do
  whatever you want. You do not have to write the received data to a file.

  One solution to this problem could be to have a pointer to a struct that you
  pass to the callback function. You set the pointer using the
Daniel Stenberg's avatar
Daniel Stenberg committed
  CURLOPT_WRITEDATA option. Then that pointer will be passed to the callback
  instead of a FILE * to a file:

        /* imaginary struct */
        struct MemoryStruct {
          char *memory;
          size_t size;
        };

        /* imaginary callback function */
        size_t
        WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
        {
          struct MemoryStruct *mem = (struct MemoryStruct *)data;
Daniel Stenberg's avatar
Daniel Stenberg committed

          mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
          if (mem->memory) {
            memcpy(&(mem->memory[mem->size]), ptr, realsize);
            mem->size += realsize;
            mem->memory[mem->size] = 0;
          }
          return realsize;
        }

Daniel Stenberg's avatar
Daniel Stenberg committed
  5.3 How do I fetch multiple files with libcurl?
  libcurl has excellent support for transferring multiple files. You should
  just repeatedly set new URLs with curl_easy_setopt() and then transfer it
  with curl_easy_perform(). The handle you get from curl_easy_init() is not
  only reusable, but you're even encouraged to reuse it if you can, as that
  will enable libcurl to use persistent connections.
  5.4 Does libcurl do Winsock initialization on win32 systems?
  Yes, if told to in the curl_global_init() call.
  5.5 Does CURLOPT_WRITEDATA and CURLOPT_READDATA work on win32 ?

  Yes, but you cannot open a FILE * and pass the pointer to a DLL and have
  that DLL use the FILE * (as the DLL and the client application cannot access
  each others' variable memory areas). If you set CURLOPT_WRITEDATA you must
  also use CURLOPT_WRITEFUNCTION as well to set a function that writes the
  file, even if that simply writes the data to the specified FILE *.
  Similarly, if you use CURLOPT_READDATA you must also specify
  CURLOPT_READFUNCTION.
  5.6 What about Keep-Alive or persistent connections?
  curl and libcurl have excellent support for persistent connections when
  transferring several files from the same server.  Curl will attempt to reuse
  connections for all URLs specified on the same command line/config file, and
  libcurl will reuse connections for all transfers that are made using the
  same libcurl handle.
  When you use the easy interface, the connection cache is kept within the
  easy handle. If you instead use the multi interface, the connection cache
  will be kept within the multi handle and will be shared among all the easy
  handles that are used within the same multi handle.

  5.7 Link errors when building libcurl on Windows!

  You need to make sure that your project, and all the libraries (both static
  and dynamic) that it links against, are compiled/linked against the same run
  time library.

  This is determined by the /MD, /ML, /MT (and their corresponding /M?d)
  options to the command line compiler. /MD (linking against MSVCRT dll) seems
  to be the most commonly used option.

Gisle Vanem's avatar
 
Gisle Vanem committed
  When building an application that uses the static libcurl library, you must
  add -DCURL_STATICLIB to your CFLAGS. Otherwise the linker will look for
  dynamic import symbols. If you're using Visual Studio, you need to instead
  add CURL_STATICLIB in the "Preprocessor Definitions" section.

  If you get linker error like "unknown symbol __imp__curl_easy_init ..." you
  have linked against the wrong (static) library.  If you want to use the
  libcurl.dll and import lib, you don't need any extra CFLAGS, but use one of
  the import libraries below. These are the libraries produced by the various
  lib/Makefile.* files:
Gisle Vanem's avatar
 
Gisle Vanem committed

Daniel Stenberg's avatar
Daniel Stenberg committed
       Target:          static lib.   import lib for libcurl*.dll.
       -----------------------------------------------------------
       MingW:           libcurl.a     libcurldll.a
       MSVC (release):  libcurl.lib   libcurl_imp.lib
       MSVC (debug):    libcurld.lib  libcurld_imp.lib
       Borland:         libcurl.lib   libcurl_imp.lib
Gisle Vanem's avatar
 
Gisle Vanem committed

  5.8 libcurl.so.X: open failed: No such file or directory

  This is an error message you might get when you try to run a program linked
  with a shared version of libcurl and your run-time linker (ld.so) couldn't
  find the shared library named libcurl.so.X. (Where X is the number of the
  current libcurl ABI, typically 3 or 4).
  You need to make sure that ld.so finds libcurl.so.X. You can do that
  multiple ways, and it differs somewhat between different operating systems,
  but they are usually:

  * Add an option to the linker command line that specify the hard-coded path
    the run-time linker should check for the lib (usually -R)

  * Set an environment variable (LD_LIBRARY_PATH for example) where ld.so
    should check for libs

  * Adjust the system's config to check for libs in the directory where you've
    put the dir (like Linux's /etc/ld.so.conf)

  'man ld.so' and 'man ld' will tell you more details

Daniel Stenberg's avatar
Daniel Stenberg committed
  libcurl supports a large a number of different name resolve functions. One
  of them is picked at build-time and will be used unconditionally. Thus, if
  you want to change name resolver function you must rebuild libcurl and tell
  it to use a different function.

  - The non-ipv6 resolver that can use one out of four host name resolve calls
    (depending on what your system supports):

Daniel Stenberg's avatar
Daniel Stenberg committed
      A - gethostbyname()
      B - gethostbyname_r() with 3 arguments
      C - gethostbyname_r() with 5 arguments
      D - gethostbyname_r() with 6 arguments

  - The ipv6-resolver that uses getaddrinfo()

  - The c-ares based name resolver that uses the c-ares library for resolves.
    Using this offers asynchronous name resolves.
  - The threaded resolver (default option on Windows). It uses:
Daniel Stenberg's avatar
Daniel Stenberg committed
      A - gethostbyname() on plain ipv4 hosts
      B - getaddrinfo() on ipv6-enabled hosts
Daniel Stenberg's avatar
Daniel Stenberg committed
  Also note that libcurl never resolves or reverse-lookups addresses given as
  pure numbers, such as 127.0.0.1 or ::1.

  5.10 How do I prevent libcurl from writing the response to stdout?

  libcurl provides a default built-in write function that writes received data
Daniel Stenberg's avatar
Daniel Stenberg committed
  to stdout. Set the CURLOPT_WRITEFUNCTION to receive the data, or possibly
  set CURLOPT_WRITEDATA to a different FILE * handle.
  5.11 How do I make libcurl not receive the whole HTTP response?

  You make the write callback (or progress callback) return an error and
  libcurl will then abort the transfer.

  5.12 Can I make libcurl fake or hide my real IP address?

Daniel Stenberg's avatar
Daniel Stenberg committed
  No. libcurl operates on a higher level. Besides, faking IP address would
  imply sending IP packet with a made-up source address, and then you normally
  get a problem with receiving the packet sent back as they would then not be
  routed to you!

  If you use a proxy to access remote sites, the sites will not see your local
  IP address but instead the address of the proxy.

  Also note that on many networks NATs or other IP-munging techniques are used
  that makes you see and use a different IP address locally than what the
Daniel Stenberg's avatar
Daniel Stenberg committed
  remote server will see you coming from. You may also consider using
  http://www.torproject.org .
  5.13 How do I stop an ongoing transfer?

  With the easy interface you make sure to return the correct error code from
  one of the callbacks, but none of them are instant. There is no function you
  can call from another thread or similar that will stop it immediately.
  Instead, you need to make sure that one of the callbacks you use returns an
  appropriate value that will stop the transfer.  Suitable callbacks that you
  can do this with include the progress callback, the read callback and the
  write callback.
  If you're using the multi interface, you can also stop a transfer by
  removing the particular easy handle from the multi stack at any moment you
  think the transfer is done or when you wish to abort the transfer.
  5.14 Using C++ non-static functions for callbacks?

  libcurl is a C library, it doesn't know anything about C++ member functions.

  You can overcome this "limitation" with a relative ease using a static
  member function that is passed a pointer to the class:

     // f is the pointer to your object.
Daniel Stenberg's avatar
Daniel Stenberg committed
     static YourClass::func(void *buffer, size_t sz, size_t n, void *f)
     {
       // Call non-static member function.
       static_cast<YourClass*>(f)->nonStaticFunction();
     }

     // This is how you pass pointer to the static function:
Daniel Stenberg's avatar
Daniel Stenberg committed
     curl_easy_setopt(hcurl, CURLOPT_WRITEFUNCTION, YourClass:func);
     curl_easy_setopt(hcurl, CURLOPT_WRITEDATA, this);

Yang Tse's avatar
 
Yang Tse committed
  5.15 How do I get an FTP directory listing?

  If you end the FTP URL you request with a slash, libcurl will provide you
  with a directory listing of that given directory. You can also set
  CURLOPT_CUSTOMREQUEST to alter what exact listing command libcurl would use
  to list the files.

  The follow-up question that tend to follow the previous one, is how a
  program is supposed to parse the directory listing. How does it know what's
  a file and what's a dir and what's a symlink etc. The harsh reality is that
  FTP provides no such fine and easy-to-parse output. The output format FTP
  servers respond to LIST commands are entirely at the server's own liking and
  the NLST output doesn't reveal any types and in many cases don't even
  include all the directory entries. Also, both LIST and NLST tend to hide
  unix-style hidden files (those that start with a dot) by default so you need
  to do "LIST -a" or similar to see them.

  The application thus needs to parse the LIST output. One such existing
  list parser is available at http://cr.yp.to/ftpparse.html  Versions of
  libcurl since 7.21.0 also provide the ability to specify a wildcard to
  download multiple files from one FTP directory.
  5.16 I want a different time-out!

  Time and time again users realize that CURLOPT_TIMEOUT and
  CURLOPT_CONNECTIMEOUT are not sufficiently advanced or flexible to cover all
  the various use cases and scenarios applications end up with.

  libcurl offers many more ways to time-out operations. A common alternative
  is to use the CURLOPT_LOW_SPEED_LIMIT and CURLOPT_LOW_SPEED_TIME options to
  specify the lowest possible speed to accept before to consider the transfer
  timed out.

  The most flexible way is by writing your own time-out logic and using
  CURLOPT_PROGRESSFUNCTION (perhaps in combination with other callbacks) and
  use that to figure out exactly when the right condition is met when the
  transfer should get stopped.

  5.17 Can I write a server with libcurl?

  No. libcurl offers no functions or building blocks to build any kind of
  internet protocol server. libcurl is only a client-side library. For server
  libraries, you need to continue your search elsewhere but there exist many
  good open source ones out there for most protocols you could possibly want a
  server for. And there are really good stand-alone ones that have been tested
  and proven for many years. There's no need for you to reinvent them!

  5.18 Does libcurl use threads?

  Put simply: no, libcurl will execute in the same thread you call it in. All
  callbacks will be called in the same thread as the one you call libcurl in.

  If you want to avoid your thread to be blocked by the libcurl call, you make
  sure you use the non-blocking API which will do transfers asynchronously -
  but still in the same single thread.

  libcurl will potentially internally use threads for name resolving, if it
  was built to work like that, but in those cases it'll create the child
  threads by itself and they will only be used and then killed internally by
  libcurl and never exposed to the outside.
Daniel Stenberg's avatar
Daniel Stenberg committed
6. License Issues
  Curl and libcurl are released under a MIT/X derivate license. The license is
  very liberal and should not impose a problem for your project. This section
  is just a brief summary for the cases we get the most questions. (Parts of
  this section was much enhanced by Bjorn Reese.)
Daniel Stenberg's avatar
Daniel Stenberg committed
  We are not lawyers and this is not legal advice. You should probably consult
  one if you want true and accurate legal insights without our prejudice. Note
  especially that this section concerns the libcurl license only; compiling in
  features of libcurl that depend on other libraries (e.g. OpenSSL) may affect
  the licensing obligations of your application.
Daniel Stenberg's avatar
Daniel Stenberg committed

  6.1 I have a GPL program, can I use the libcurl library?
Daniel Stenberg's avatar
Daniel Stenberg committed
  Yes!
Daniel Stenberg's avatar
Daniel Stenberg committed
  Since libcurl may be distributed under the MIT/X derivate license, it can be
  used together with GPL in any software.
  6.2 I have a closed-source program, can I use the libcurl library?
Daniel Stenberg's avatar
Daniel Stenberg committed

  libcurl does not put any restrictions on the program that uses the library.
  6.3 I have a BSD licensed program, can I use the libcurl library?
Daniel Stenberg's avatar
Daniel Stenberg committed

  libcurl does not put any restrictions on the program that uses the library.
  6.4 I have a program that uses LGPL libraries, can I use libcurl?
  The LGPL license doesn't clash with other licenses.
Daniel Stenberg's avatar
Daniel Stenberg committed

  6.5 Can I modify curl/libcurl for my program and keep the changes secret?
Daniel Stenberg's avatar
Daniel Stenberg committed

  The MIT/X derivate license practically allows you to do almost anything with
  the sources, on the condition that the copyright texts in the sources are
  left intact.
  6.6 Can you please change the curl/libcurl license to XXXX?
  We have carefully picked this license after years of development and
  discussions and a large amount of people have contributed with source code
  knowing that this is the license we use. This license puts the restrictions
  we want on curl/libcurl and it does not spread to other programs or
  libraries that use it. It should be possible for everyone to use libcurl or
  curl in their projects, no matter what license they already have in use.
Daniel Stenberg's avatar
Daniel Stenberg committed

  6.7 What are my obligations when using libcurl in my commercial apps?
Daniel Stenberg's avatar
Daniel Stenberg committed

  Next to none. All you need to adhere to is the MIT-style license (stated in
  the COPYING file) which basically says you have to include the copyright
  notice in "all copies" and that you may not use the copyright holder's name
  when promoting your software.

  You do not have to release any of your source code.

  You do not have to reveal or make public any changes to the libcurl source
  code.

  You do not have to broadcast to the world that you are using libcurl within
Daniel Stenberg's avatar
Daniel Stenberg committed
  your app.

  All we ask is that you disclose "the copyright notice and this permission
  notice" somewhere. Most probably like in the documentation or in the section
  where other third party dependencies already are mentioned and acknowledged.

  As can be seen here: http://curl.haxx.se/docs/companies.html and elsewhere,
  more and more companies are discovering the power of libcurl and take
  advantage of it even in commercial environments.
Daniel Stenberg's avatar
Daniel Stenberg committed


Daniel Stenberg's avatar
Daniel Stenberg committed
7. PHP/CURL Issues

  7.1 What is PHP/CURL?

  The module for PHP that makes it possible for PHP programs to access curl-
  functions from within PHP.

  In the cURL project we call this module PHP/CURL to differentiate it from
  curl the command line tool and libcurl the library. The PHP team however
  does not refer to it like this (for unknown reasons). They call it plain
  CURL (often using all caps) or sometimes ext/curl, but both cause much
  confusion to users which in turn gives us a higher question load.
Daniel Stenberg's avatar
Daniel Stenberg committed

Daniel Stenberg's avatar
Daniel Stenberg committed
  7.2 Who wrote PHP/CURL?
Daniel Stenberg's avatar
Daniel Stenberg committed

  PHP/CURL is a module that comes with the regular PHP package. It depends and
  uses libcurl, so you need to have libcurl installed properly first before
  PHP/CURL can be used. PHP/CURL was initially written by Sterling Hughes.
Daniel Stenberg's avatar
Daniel Stenberg committed

  7.3 Can I perform multiple requests using the same handle?

  Yes - at least in PHP version 4.3.8 and later (this has been known to not
  work in earlier versions, but the exact version when it started to work is
  unknown to me).
Daniel Stenberg's avatar
Daniel Stenberg committed

  After a transfer, you just set new options in the handle and make another
  transfer. This will make libcurl to re-use the same connection if it can.