Skip to content

Instantly share code, notes, and snippets.

@bagder
Created June 17, 2019 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bagder/e0dd337453665dd259c008f3df14bb95 to your computer and use it in GitHub Desktop.
Save bagder/e0dd337453665dd259c008f3df14bb95 to your computer and use it in GitHub Desktop.
test CURLOPT_MAXAGE_CONN with shared connections
#include <stdio.h>
#include <unistd.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
CURLSH *shobject = curl_share_init();
curl_share_setopt(shobject, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_MAXAGE_CONN, 1L); /* one second! */
curl_easy_setopt(curl, CURLOPT_SHARE, shobject);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
sleep(2);
/* Perform the request, no re-use since the previous is now too old */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_share_cleanup(shobject);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment