mirror of
https://github.com/espressif/esp-idf.git
synced 2025-11-03 00:21:44 +01:00
esp_http_client: apply generic error check macros
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user