cURL / Mailing Lists / curl-users / Single Mail

curl-users

Is this the correct way of reusing the curl_easy handles?

From: Suryaveer Chauhan <chauhan.suryaveer_at_gmail.com>
Date: Sun, 21 Feb 2016 02:29:29 -0500

Hi All,

I couldn't find a proper way to pool the curl_handles and in some comments
I read that it's better to keep open the single handle. So, I want to
confirm if this is the correct way or does it have any side effects or bad
performance.

This method creates curl_easy handles as the program starts:

*##main.c*

*static int child_init(int rank) {*
* LM_NOTICE("init_child [%d] pid [%d]\n", rank, getpid());*

* pid = my_pid();*

* curl_global_init(CURL_GLOBAL_ALL);*
* // initialize curl handle*
* curl = curl_easy_init();*
* if (!curl) {*
* LM_ERR("Child %d: Curl initialization failed.\n", rank);*
* return -1;*
* }*
* //create some connections before actual requests come.*
* curl_head(URL);*
* return 0;*
*}*

*The .C where all the methods *
*#RepositoryAccessClient.c*

*int curl_head(const char* url) {*
* if (!url) {*
* LM_ERR("URL not provided. Returning with error.\n");*
* return -1;*
* }*
* CURLcode res;*
* int http_code = 0;*
* struct curl_slist *headers = NULL;*
* headers = curl_slist_append(headers, "charsets: utf-8");*

* /* set URL */*
* curl_easy_setopt(curl, CURLOPT_URL, url);*
* curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);*
* curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "HEAD");*
* curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);*

* res = curl_easy_perform(curl);*
* if (res != CURLE_OK) {*
* LM_ERR("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));*
* }*
* curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);*
* LM_DBG("HTTP return CODE %d\n", http_code);*
* curl_slist_free_all(headers);*
* curl_easy_reset(curl);*
* return http_code;*
*}*

*int curl_post(const char* url, char *postdata) {*
* if (!url) {*
* LM_ERR("URL not provided. Returning with error.\n");*
* return -1;*
* }*

* CURLcode res;*

* int http_code = 0;*
* struct curl_slist *headers = NULL;*
* headers = curl_slist_append(headers, "Accept: application/json");*
* headers = curl_slist_append(headers, "Content-Type: application/json");*
* headers = curl_slist_append(headers, "charsets: utf-8");*

* /* set URL */*
* curl_easy_setopt(curl, CURLOPT_URL, url);*
* curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);*
* curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata);*
* curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcrp/0.1");*

* res = curl_easy_perform(curl);*
* if (res != CURLE_OK) {*
* LM_ERR("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));*
* }*

* curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);*
* curl_slist_free_all(headers);*
* curl_easy_reset(curl);*
* return http_code;*
*}*

Other methods such as for GET/PUT/DELETE are similar, the only difference
is the value of *CURLOPT_CUSTOMREQUEST *which is set as per the HTTP verb
used.

I close the connection when the program terminates.

Please suggest if there's a better way to do the same and what
complications may arise in this.

Thanks & Regards,
Suryaveer Singh Chauhan

-------------------------------------------------------------------
List admin: https://cool.haxx.se/list/listinfo/curl-users
FAQ: https://curl.haxx.se/docs/faq.html
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2016-02-21