feat(http): Bring esp_http_client component and example from idf

Commit ID: dd3c0329
This commit is contained in:
yuanjm
2020-08-07 11:32:37 +08:00
parent 237311e466
commit 9ea5d2abe7
17 changed files with 1020 additions and 212 deletions

View File

@ -37,7 +37,7 @@ typedef struct http_header_item {
STAILQ_HEAD(http_header, http_header_item);
http_header_handle_t http_header_init()
http_header_handle_t http_header_init(void)
{
http_header_handle_t header = calloc(1, sizeof(struct http_header));
HTTP_MEM_CHECK(TAG, header, return NULL);
@ -178,6 +178,8 @@ int http_header_generate_string(http_header_handle_t header, int index, char *bu
int idx = 0;
int ret_idx = -1;
bool is_end = false;
// iterate over the header entries to calculate buffer size and determine last item
STAILQ_FOREACH(item, header, next) {
if (item->value && idx >= index) {
siz += strlen(item->key);
@ -187,7 +189,10 @@ int http_header_generate_string(http_header_handle_t header, int index, char *bu
idx ++;
if (siz + 1 > *buffer_len - 2) {
// if this item would not fit to the buffer, return the index of the last fitting one
ret_idx = idx - 1;
ESP_LOGE(TAG, "Buffer length is small to fit all the headers");
break;
}
}
@ -195,10 +200,12 @@ int http_header_generate_string(http_header_handle_t header, int index, char *bu
return 0;
}
if (ret_idx < 0) {
// all items would fit, mark this as the end of http header string
ret_idx = idx;
is_end = true;
}
// iterate again over the header entries to write only the fitting indeces
int str_len = 0;
idx = 0;
STAILQ_FOREACH(item, header, next) {
@ -208,6 +215,7 @@ int http_header_generate_string(http_header_handle_t header, int index, char *bu
idx ++;
}
if (is_end) {
// write the http header terminator if all header entries have been written in this function call
str_len += snprintf(buffer + str_len, *buffer_len - str_len, "\r\n");
}
*buffer_len = str_len;