5
\$\begingroup\$

In a C project I want to reuse curl easy handles. The flow of program is like:

Client --> C Application --> Call URL1, do something, Call URL2, do something, Call URL3...

In short, for each client request, the same set of URLs are called.

I initially create the curl easy handle when the program starts. The main program creates a configurable number of child, so each child gets its own easy handle.

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;
}

I created a C file where the functions are created to handle GET/POST/PUT etc requests:

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;
}

When somewhere in the program I want to call the WS I call the desired function as curl_post(url).

Am I doing this correctly or are there any flaws in this implementation?

\$\endgroup\$
  • \$\begingroup\$ Should not int http_code = 0; be type long per this ref? Curl is new to me. \$\endgroup\$ – chux Feb 23 '16 at 18:54
  • \$\begingroup\$ @chux Yes, you are correct as per the ref. BTW, I am new too, this is the first curl code for me.:) \$\endgroup\$ – user3275095 Feb 24 '16 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.