esp_http_client: apply generic error check macros

This commit is contained in:
Harshit Malpani 2021-09-30 16:35:57 +05:30
parent 75e2705269
commit 2085e4eb92
4 changed files with 51 additions and 53 deletions

View File

@ -9,6 +9,7 @@
#include "esp_system.h"
#include "esp_log.h"
#include "esp_check.h"
#include "http_header.h"
#include "esp_transport.h"
@ -366,6 +367,7 @@ esp_err_t esp_http_client_set_authtype(esp_http_client_handle_t client, esp_http
static esp_err_t _set_config(esp_http_client_handle_t client, const esp_http_client_config_t *config)
{
esp_err_t ret = ESP_OK;
client->connection_info.method = config->method;
client->connection_info.port = config->port;
client->connection_info.auth_type = config->auth_type;
@ -401,41 +403,27 @@ static esp_err_t _set_config(esp_http_client_handle_t client, const esp_http_cli
} else {
client->connection_info.path = strdup(DEFAULT_HTTP_PATH);
}
HTTP_MEM_CHECK(TAG, client->connection_info.path, {
return ESP_ERR_NO_MEM;
});
ESP_RETURN_ON_FALSE(client->connection_info.path, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
if (config->host) {
client->connection_info.host = strdup(config->host);
HTTP_MEM_CHECK(TAG, client->connection_info.host, {
_clear_connection_info(client);
return ESP_ERR_NO_MEM;
});
ESP_GOTO_ON_FALSE(client->connection_info.host, ESP_ERR_NO_MEM, error, TAG, "Memory exhausted");
}
if (config->query) {
client->connection_info.query = strdup(config->query);
HTTP_MEM_CHECK(TAG, client->connection_info.query, {
_clear_connection_info(client);
return ESP_ERR_NO_MEM;
});
ESP_GOTO_ON_FALSE(client->connection_info.query, ESP_ERR_NO_MEM, error, TAG, "Memory exhausted");
}
if (config->username) {
client->connection_info.username = strdup(config->username);
HTTP_MEM_CHECK(TAG, client->connection_info.username, {
_clear_connection_info(client);
return ESP_ERR_NO_MEM;
});
ESP_GOTO_ON_FALSE(client->connection_info.username, ESP_ERR_NO_MEM, error, TAG, "Memory exhausted");
}
if (config->password) {
client->connection_info.password = strdup(config->password);
HTTP_MEM_CHECK(TAG, client->connection_info.password, {
_clear_connection_info(client);
return ESP_ERR_NO_MEM;
});
ESP_GOTO_ON_FALSE(client->connection_info.password, ESP_ERR_NO_MEM, error, TAG, "Memory exhausted");
}
if (config->transport_type == HTTP_TRANSPORT_OVER_SSL) {
@ -456,7 +444,11 @@ static esp_err_t _set_config(esp_http_client_handle_t client, const esp_http_cli
client->is_async = true;
}
return ESP_OK;
return ret;
error:
_clear_connection_info(client);
return ret;
}
static esp_err_t _clear_connection_info(esp_http_client_handle_t client)
@ -539,6 +531,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
{
esp_http_client_handle_t client;
esp_err_t ret = ESP_OK;
esp_transport_handle_t tcp = NULL;
char *host_name;
bool _success;
@ -582,7 +575,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
if (config->if_name) {
client->if_name = calloc(1, sizeof(struct ifreq) + 1);
HTTP_MEM_CHECK(TAG, client->if_name, goto error);
ESP_GOTO_ON_FALSE(client->if_name, ESP_FAIL, error, TAG, "Memory exhausted");
memcpy(client->if_name, config->if_name, sizeof(struct ifreq));
esp_transport_tcp_set_interface_name(tcp, client->if_name);
}
@ -712,7 +705,11 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
client->event.client = client;
client->state = HTTP_STATE_INIT;
return client;
if (ret == ESP_OK) {
return client;
}
error:
esp_http_client_cleanup(client);
return NULL;
@ -795,6 +792,7 @@ static esp_err_t esp_http_check_response(esp_http_client_handle_t client)
esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *url)
{
esp_err_t ret = ESP_OK;
char *old_host = NULL;
struct http_parser_url purl;
int old_port;
@ -819,10 +817,7 @@ esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *u
if (purl.field_data[UF_HOST].len) {
http_utils_assign_string(&client->connection_info.host, url + purl.field_data[UF_HOST].off, purl.field_data[UF_HOST].len);
HTTP_MEM_CHECK(TAG, client->connection_info.host, {
free(old_host);
return ESP_ERR_NO_MEM;
});
ESP_GOTO_ON_FALSE(client->connection_info.host, ESP_ERR_NO_MEM, error, TAG, "Memory exhausted");
}
// Close the connection if host was changed
if (old_host && client->connection_info.host
@ -842,7 +837,7 @@ esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *u
if (purl.field_data[UF_SCHEMA].len) {
http_utils_assign_string(&client->connection_info.scheme, url + purl.field_data[UF_SCHEMA].off, purl.field_data[UF_SCHEMA].len);
HTTP_MEM_CHECK(TAG, client->connection_info.scheme, return ESP_ERR_NO_MEM);
ESP_RETURN_ON_FALSE(client->connection_info.scheme, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
if (strcasecmp(client->connection_info.scheme, "http") == 0) {
client->connection_info.port = DEFAULT_HTTP_PORT;
@ -869,10 +864,10 @@ esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *u
*password = 0;
password ++;
http_utils_assign_string(&client->connection_info.password, password, -1);
HTTP_MEM_CHECK(TAG, client->connection_info.password, return ESP_ERR_NO_MEM);
ESP_RETURN_ON_FALSE(client->connection_info.password, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
}
http_utils_assign_string(&client->connection_info.username, username, -1);
HTTP_MEM_CHECK(TAG, client->connection_info.username, return ESP_ERR_NO_MEM);
ESP_RETURN_ON_FALSE(client->connection_info.username, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
free(user_info);
} else {
return ESP_ERR_NO_MEM;
@ -885,17 +880,21 @@ esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *u
} else {
http_utils_assign_string(&client->connection_info.path, "/", -1);
}
HTTP_MEM_CHECK(TAG, client->connection_info.path, return ESP_ERR_NO_MEM);
ESP_RETURN_ON_FALSE(client->connection_info.path, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
if (purl.field_data[UF_QUERY].len) {
http_utils_assign_string(&client->connection_info.query, url + purl.field_data[UF_QUERY].off, purl.field_data[UF_QUERY].len);
HTTP_MEM_CHECK(TAG, client->connection_info.query, return ESP_ERR_NO_MEM);
ESP_RETURN_ON_FALSE(client->connection_info.query, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
} else if (client->connection_info.query) {
free(client->connection_info.query);
client->connection_info.query = NULL;
}
return ESP_OK;
return ret;
error:
free(old_host);
return ret;
}
esp_err_t esp_http_client_set_method(esp_http_client_handle_t client, esp_http_client_method_t method)

View File

@ -16,6 +16,7 @@
#include "esp_system.h"
#include "esp_log.h"
#include "esp_check.h"
#include "http_utils.h"
#include "http_auth.h"
@ -66,6 +67,7 @@ char *http_auth_digest(const char *username, const char *password, esp_http_auth
char *digest = NULL;
char *auth_str = NULL;
char *temp_auth_str = NULL;
esp_err_t ret = ESP_OK;
if (username == NULL ||
password == NULL ||
@ -76,13 +78,13 @@ char *http_auth_digest(const char *username, const char *password, esp_http_auth
}
ha1 = calloc(1, MD5_MAX_LEN);
HTTP_MEM_CHECK(TAG, ha1, goto _digest_exit);
ESP_GOTO_ON_FALSE(ha1, ESP_FAIL, _digest_exit, TAG, "Memory exhausted");
ha2 = calloc(1, MD5_MAX_LEN);
HTTP_MEM_CHECK(TAG, ha2, goto _digest_exit);
ESP_GOTO_ON_FALSE(ha2, ESP_FAIL, _digest_exit, TAG, "Memory exhausted");
digest = calloc(1, MD5_MAX_LEN);
HTTP_MEM_CHECK(TAG, digest, goto _digest_exit);
ESP_GOTO_ON_FALSE(digest, ESP_FAIL, _digest_exit, TAG, "Memory exhausted");
if (md5_printf(ha1, "%s:%s:%s", username, auth_data->realm, password) <= 0) {
goto _digest_exit;
@ -128,7 +130,7 @@ _digest_exit:
free(ha1);
free(ha2);
free(digest);
return auth_str;
return (ret == ESP_OK) ? auth_str : NULL;
}
char *http_auth_basic(const char *username, const char *password)
@ -136,15 +138,16 @@ char *http_auth_basic(const char *username, const char *password)
int out;
char *user_info = NULL;
char *digest = NULL;
esp_err_t ret = ESP_OK;
size_t n = 0;
asprintf(&user_info, "%s:%s", username, password);
HTTP_MEM_CHECK(TAG, user_info, return NULL);
ESP_RETURN_ON_FALSE(user_info, NULL, TAG, "Memory exhausted");
esp_crypto_base64_encode(NULL, 0, &n, (const unsigned char *)user_info, strlen(user_info));
digest = calloc(1, 6 + n + 1);
HTTP_MEM_CHECK(TAG, digest, goto _basic_exit);
ESP_GOTO_ON_FALSE(digest, ESP_FAIL, _basic_exit, TAG, "Memory exhausted");
strcpy(digest, "Basic ");
esp_crypto_base64_encode((unsigned char *)digest + 6, n, (size_t *)&out, (const unsigned char *)user_info, strlen(user_info));
_basic_exit:
free(user_info);
return digest;
return (ret == ESP_OK) ? digest : NULL;
}

View File

@ -11,6 +11,7 @@
#include <stdio.h>
#include <stdarg.h>
#include "esp_log.h"
#include "esp_check.h"
#include "http_header.h"
#include "http_utils.h"
@ -32,7 +33,7 @@ STAILQ_HEAD(http_header, http_header_item);
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);
ESP_RETURN_ON_FALSE(header, NULL, TAG, "Memory exhausted");
STAILQ_INIT(header);
return header;
}
@ -74,23 +75,24 @@ esp_err_t http_header_get(http_header_handle_t header, const char *key, char **v
static esp_err_t http_header_new_item(http_header_handle_t header, const char *key, const char *value)
{
esp_err_t ret = ESP_OK;
http_header_item_handle_t item;
item = calloc(1, sizeof(http_header_item_t));
HTTP_MEM_CHECK(TAG, item, return ESP_ERR_NO_MEM);
ESP_RETURN_ON_FALSE(item, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
http_utils_assign_string(&item->key, key, -1);
HTTP_MEM_CHECK(TAG, item->key, goto _header_new_item_exit);
ESP_GOTO_ON_FALSE(item->key, ESP_ERR_NO_MEM, _header_new_item_exit, TAG, "Memory exhausted");
http_utils_trim_whitespace(&item->key);
http_utils_assign_string(&item->value, value, -1);
HTTP_MEM_CHECK(TAG, item->value, goto _header_new_item_exit);
ESP_GOTO_ON_FALSE(item->value, ESP_ERR_NO_MEM, _header_new_item_exit, TAG, "Memory exhausted");
http_utils_trim_whitespace(&item->value);
STAILQ_INSERT_TAIL(header, item, next);
return ESP_OK;
return ret;
_header_new_item_exit:
free(item->key);
free(item->value);
free(item);
return ESP_ERR_NO_MEM;
return ret;
}
esp_err_t http_header_set(http_header_handle_t header, const char *key, const char *value)
@ -118,7 +120,7 @@ esp_err_t http_header_set_from_string(http_header_handle_t header, const char *k
char *p_str;
p_str = strdup(key_value_data);
HTTP_MEM_CHECK(TAG, p_str, return ESP_ERR_NO_MEM);
ESP_RETURN_ON_FALSE(p_str, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
eq_ch = strchr(p_str, ':');
if (eq_ch == NULL) {
free(p_str);
@ -155,7 +157,7 @@ int http_header_set_format(http_header_handle_t header, const char *key, const c
va_start(argptr, format);
len = vasprintf(&buf, format, argptr);
va_end(argptr);
HTTP_MEM_CHECK(TAG, buf, return 0);
ESP_RETURN_ON_FALSE(buf, 0, TAG, "Memory exhausted");
if (buf == NULL) {
return 0;
}

View File

@ -83,10 +83,4 @@ char *http_utils_join_string(const char *first_str, size_t len_first, const char
*/
int http_utils_str_starts_with(const char *str, const char *start);
#define HTTP_MEM_CHECK(TAG, a, action) if (!(a)) { \
ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, "Memory exhausted"); \
action; \
}
#endif