4

The following is from the libcurl homepage:

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.

Just to be sure, if I create a CURL handle (curl_easy_init()) and set it's headers, make a HTTP-request, then change the headers and make another request, libcurl will still attempt to use a persistent connection even though the headers have changed?

The libcurl homepage also says

If you use the easy interface, and you call curl_easy_cleanup, all the possibly open connections held by libcurl will be closed and forgotten.

So as long as I dont call curl_easy_cleanup, CURL will attempt to re-use the same connection even though the requests are made with different headers?

Thank you.

jensa
  • 2,792
  • 2
  • 21
  • 36

1 Answers1

5

Yes, that's entirely correct. Keep re-using the same easy handle and libcurl will try to re-use connections as much as possible.

Daniel Stenberg
  • 54,736
  • 17
  • 146
  • 222
  • @daniel-stenberg I have a similar doubt. I understand that the handle will reuse the connections until closed but does that mean I never cleanup that handle? For ex: function B handle calls to the http server and function A use to call B frequently, so if I cleanup the handle in B then every call from A will create new connections to http server. Am I right in understanding? Please guide. – user3275095 Jul 21 '15 at 21:52
  • 1
    Sorry, but adding a second question in a comment to an answer seems like very bad form. Ask a new question, read the docs or ask on the libcurl mailing list. – Daniel Stenberg Jul 21 '15 at 22:04
  • 1
    @daniel-stenberg I am sorry but I thought a similar question will be marked as duplicate and closed so just asked here. No problem, I have asked it as [question](http://stackoverflow.com/questions/31554013/libcurl-http-persistent-connection). Actually I have a bigger doubt but this was small part of it. – user3275095 Jul 22 '15 at 04:46
  • @DanielStenberg if the server replies with 500 or 503 will libcurl still keep connections alive? I understand that it has no reasons to terminate connections, but just in case wanted to check – Pavel P Oct 26 '18 at 22:46
  • It will still maintain the connection. – Daniel Stenberg Oct 27 '18 at 07:40