Home > Back-end >  LibCurl- How to update a specific header information which is aleady set?
LibCurl- How to update a specific header information which is aleady set?

Time:10-09

I need to modify a specific information like authorization token in the header request. Is there any way to update only that specific header, keeping the rest? Currently I am doing something like below. But not sure if there is any other way to do it?

        struct curl_slist* headers = NULL;

        headers = curl_slist_append(headers, "Accept:");

        headers = curl_slist_append(headers, "Authorization: ABCDC0F725997BEF3C6B90B0E39D9C314DCB58553FA47CD3598BBB8910A8CE6E");

        headers = curl_slist_append(headers, "Host: localhost");

        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);


        //Updating the header like this
        auto *test = headers;

        while (test != NULL)
        {
            if (strstr(test->data, "Authorization:"))
            {
                strcpy_s(test->data, strlen("Authorization: A5484526D0F7A671ABCD0C091755532A63475719E460ED6A807C4AAD70BCEDA0")   1, "Authorization: A5484526D0F7A671CE400C091755532A63475719E460ED6A807C4AAD70BCEDA0");
                break;
            }
            test = test->next;
        }

CodePudding user response:

What you are doing is dangerous for these two reasons:

  1. test->data might not be long enough to hold the data copying and you'll end up writing data somewhere you shouldn't.
  2. test->data might be longer that the data you are copying, thus the header will have unwanted data at the end.

Based in curl's source code, it looks like breaking the abstraction will work ok so we can use your approach of setting test->data but with the following modification:

static const char *newheaderdata = "Authorization: A5484526D0F7A671CE400C091755532A63475719E460ED6A807C4AAD70BCEDA0";
while (test != NULL)
{
    if (strstr(test->data, "Authorization:"))
    {
        free(test->data);
        test->data = strdup(newheaderdata) 
        break;
    }
    test = test->next;
}

CodePudding user response:

Usually you can't do it like you implemented it. The allocated size of test->data is unknown. Your code will work only if the header length is fixed.

The libcurl code may be change, so free(test->data); test->data = strdup(newdata) is not a good idea.

I would do like bellow.

#include <curl/curl.h>

#if LIBCURL_VERSION_MAJOR != 7 || LIBCURL_VERSION_MINOR != 79 || LIBCURL_VERSION_PATCH != 1
# error "libcurl version is is changed. Be sure Curl_slist_duplicate is not changed in lib/slist.h"
#endif

struct curl_slist *Curl_slist_duplicate(struct curl_slist *inlist);

struct curl_slist* headers = nullptr;
struct curl_slist* commonheaders = nullptr;
commonheaders = curl_slist_append(commonheaders, "Accept:");
commonheaders = curl_slist_append(commonheaders, "Host: localhost");

// Update the headers.
curl_slist_free_all(headers);
headers = Curl_slist_duplicate(commonheaders);
headers = curl_slist_append(headers, "Authorization: ABCDC0F725997BEF3C6B90B0E39D9C314DCB58553FA47CD3598BBB8910A8CE6E");

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

Or

#include <curl/curl.h>

struct curl_slist* headers = nullptr;
struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Accept:");
headers = curl_slist_append(headers, "Host: localhost");
headers = curl_slist_append(headers, "Authorization: ABCDC0F725997BEF3C6B90B0E39D9C314DCB58553FA47CD3598BBB8910A8CE6E");

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

// Remove the last header.
curl_slist* h = headers;
for (curl_slist* next = h->next; next->next; h = next->next, next = h->next) {}
curl_slist_free_all(h->next);
h->next = nullptr;
// Append new header.
headers = curl_slist_append(headers, "Authorization: ABCDC0F725997BEF3C6B90B0E39D9C314DCB58553FA47CD3598BBB8910A8CE6E");

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  • Related