mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
esp_http_client: Optimize code structure
This commit is contained in:
parent
4b07e33a12
commit
c47854f1fc
@ -195,10 +195,8 @@ static int http_on_status(http_parser *parser, const char *at, size_t length)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int http_on_header_field(http_parser *parser, const char *at, size_t length)
|
static int http_on_header_event(esp_http_client_handle_t client)
|
||||||
{
|
{
|
||||||
esp_http_client_t *client = parser->data;
|
|
||||||
|
|
||||||
if (client->current_header_key != NULL && client->current_header_value != NULL) {
|
if (client->current_header_key != NULL && client->current_header_value != NULL) {
|
||||||
ESP_LOGD(TAG, "HEADER=%s:%s", client->current_header_key, client->current_header_value);
|
ESP_LOGD(TAG, "HEADER=%s:%s", client->current_header_key, client->current_header_value);
|
||||||
client->event.header_key = client->current_header_key;
|
client->event.header_key = client->current_header_key;
|
||||||
@ -209,7 +207,13 @@ static int http_on_header_field(http_parser *parser, const char *at, size_t leng
|
|||||||
client->current_header_key = NULL;
|
client->current_header_key = NULL;
|
||||||
client->current_header_value = NULL;
|
client->current_header_value = NULL;
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int http_on_header_field(http_parser *parser, const char *at, size_t length)
|
||||||
|
{
|
||||||
|
esp_http_client_t *client = parser->data;
|
||||||
|
http_on_header_event(client);
|
||||||
http_utils_append_string(&client->current_header_key, at, length);
|
http_utils_append_string(&client->current_header_key, at, length);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@ -236,18 +240,7 @@ static int http_on_header_value(http_parser *parser, const char *at, size_t leng
|
|||||||
static int http_on_headers_complete(http_parser *parser)
|
static int http_on_headers_complete(http_parser *parser)
|
||||||
{
|
{
|
||||||
esp_http_client_handle_t client = parser->data;
|
esp_http_client_handle_t client = parser->data;
|
||||||
|
http_on_header_event(client);
|
||||||
if (client->current_header_key != NULL && client->current_header_value != NULL) {
|
|
||||||
ESP_LOGD(TAG, "HEADER=%s:%s", client->current_header_key, client->current_header_value);
|
|
||||||
client->event.header_key = client->current_header_key;
|
|
||||||
client->event.header_value = client->current_header_value;
|
|
||||||
http_dispatch_event(client, HTTP_EVENT_ON_HEADER, NULL, 0);
|
|
||||||
free(client->current_header_key);
|
|
||||||
free(client->current_header_value);
|
|
||||||
client->current_header_key = NULL;
|
|
||||||
client->current_header_value = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
client->response->status_code = parser->status_code;
|
client->response->status_code = parser->status_code;
|
||||||
client->response->data_offset = parser->nread;
|
client->response->data_offset = parser->nread;
|
||||||
client->response->content_length = parser->content_length;
|
client->response->content_length = parser->content_length;
|
||||||
|
@ -63,25 +63,25 @@ char *http_utils_assign_string(char **str, const char *new_str, int len)
|
|||||||
|
|
||||||
char *http_utils_append_string(char **str, const char *new_str, int len)
|
char *http_utils_append_string(char **str, const char *new_str, int len)
|
||||||
{
|
{
|
||||||
if (new_str == NULL) {
|
int l = len;
|
||||||
return NULL;
|
int old_len = 0;
|
||||||
}
|
|
||||||
char *old_str = *str;
|
char *old_str = *str;
|
||||||
if (len <= 0) {
|
if (new_str != NULL) {
|
||||||
len = strlen(new_str);
|
if (l < 0) {
|
||||||
|
l = strlen(new_str);
|
||||||
|
}
|
||||||
|
if (old_str) {
|
||||||
|
old_len = strlen(old_str);
|
||||||
|
old_str = realloc(old_str, old_len + l + 1);
|
||||||
|
mem_check(old_str);
|
||||||
|
old_str[old_len + l] = 0;
|
||||||
|
} else {
|
||||||
|
old_str = calloc(1, l + 1);
|
||||||
|
mem_check(old_str);
|
||||||
|
}
|
||||||
|
memcpy(old_str + old_len, new_str, l);
|
||||||
|
*str = old_str;
|
||||||
}
|
}
|
||||||
if (old_str) {
|
|
||||||
int old_len = strlen(old_str);
|
|
||||||
old_str = realloc(old_str, old_len + len + 1);
|
|
||||||
mem_check(old_str);
|
|
||||||
memcpy(old_str + old_len, new_str, len);
|
|
||||||
old_str[old_len + len] = 0;
|
|
||||||
} else {
|
|
||||||
old_str = calloc(1, len + 1);
|
|
||||||
mem_check(old_str);
|
|
||||||
memcpy(old_str, new_str, len);
|
|
||||||
}
|
|
||||||
*str = old_str;
|
|
||||||
return old_str;
|
return old_str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
*
|
*
|
||||||
* @param str pointer to string pointer
|
* @param str pointer to string pointer
|
||||||
* @param new_str assign this tring to str
|
* @param new_str assign this tring to str
|
||||||
* @param len length of string, 0 if new_str is zero terminated
|
* @param len length of string, less than 0 if new_str is zero terminated
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* - new_str pointer
|
* - new_str pointer
|
||||||
@ -31,15 +31,14 @@
|
|||||||
char *http_utils_assign_string(char **str, const char *new_str, int len);
|
char *http_utils_assign_string(char **str, const char *new_str, int len);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Realloc *str and append new_str to it, if not NULL; assign new_str to *str pointer if NULL
|
* @brief Realloc *str and append new_str to it if new_str is not NULL; return *str pointer if new_str is NULL
|
||||||
*
|
*
|
||||||
* @param str pointer to string pointer
|
* @param str pointer to string pointer
|
||||||
* @param new_str assign this string to str
|
* @param new_str append this string to str
|
||||||
* @param len length of string, 0 if new_str is zero terminated
|
* @param len length of string, less than 0 if new_str is zero terminated
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* - new_str pointer
|
* - *str pointer
|
||||||
* - NULL
|
|
||||||
*/
|
*/
|
||||||
char *http_utils_append_string(char **str, const char *new_str, int len);
|
char *http_utils_append_string(char **str, const char *new_str, int len);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user