Limits of esp_http_client's set_header

zliudr
Posts: 357
Joined: Thu Oct 03, 2019 5:15 am

Limits of esp_http_client's set_header

Postby zliudr » Sun Mar 29, 2020 12:16 am

How many headers can I add to the esp_http_client? Is it limited only by memory, with some sort of linked list structure? The doc is very bare. No example snippets at all on headers. I figured out my answer with the source code. In case you wonder or you have comments.

Here is what I found from http_header.c so I guess it is a linked list.

Code: Select all

/**
 * dictionary item struct, with key-value pair
 */
typedef struct http_header_item {
    char *key;                          /*!< key */
    char *value;                        /*!< value */
    STAILQ_ENTRY(http_header_item) next;   /*!< Point to next entry */
} http_header_item_t;
Here you can see adding an item involves duplicating the value with strdup() so you can free the strings you use to call set_header:

Code: Select all

esp_err_t http_header_set(http_header_handle_t header, const char *key, const char *value)
{
    http_header_item_handle_t item;

    if (value == NULL) {
        return http_header_delete(header, key);
    }

    item = http_header_get_item(header, key);

    if (item) {
        free(item->value);
        item->value = strdup(value);
        http_utils_trim_whitespace(&item->value);
        return ESP_OK;
    }
    return http_header_new_item(header, key, value);
}
The esp_http_client_set_header is just a wrapper over this function:

Code: Select all

esp_err_t esp_http_client_set_header(esp_http_client_handle_t client, const char *key, const char *value)
{
    return http_header_set(client->request->headers, key, value);
}
All these http utils and header calls are convoluted with some calling realloc and others calling strdup. Maybe they were written by different teams.

zliudr
Posts: 357
Joined: Thu Oct 03, 2019 5:15 am

Re: Limits of esp_http_client's set_header

Postby zliudr » Sun Mar 29, 2020 4:10 am

What about default headers? What headers and values are included?
I tested:

Host:xxx is included by init()
Content-length:xxx is added by perform()
Content-Type: application/x-www-form-urlencoded is also added by perform() when using POST method
Connection: is not included
User-Agent: is not included
X-Amzn-Trace-Id: Root=1-5e8016c0-b21bbdbb018e6d570295aabf is sent back from httpbin.org so it's gotta be included by perform(), what for?

Who is online

Users browsing this forum: No registered users and 144 guests