esp_http_client: Update to support build for linux

This commit is contained in:
Harshit Malpani
2022-11-08 14:14:09 +05:30
parent 43e4383bb7
commit 1c77e13d35
3 changed files with 25 additions and 10 deletions

View File

@@ -9,11 +9,10 @@
#include <stdio.h>
#include <stdarg.h>
#include "lwip/sockets.h"
#include "sys/socket.h"
#include "esp_rom_md5.h"
#include "esp_tls_crypto.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_check.h"
@@ -117,11 +116,21 @@ char *http_auth_digest(const char *username, const char *password, esp_http_auth
goto _digest_exit;
}
}
asprintf(&auth_str, "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", algorithm=\"MD5\", "
int rc = asprintf(&auth_str, "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", algorithm=\"MD5\", "
"response=\"%s\", qop=%s, nc=%08x, cnonce=\"%016llx\"",
username, auth_data->realm, auth_data->nonce, auth_data->uri, digest, auth_data->qop, auth_data->nc, auth_data->cnonce);
if (rc < 0) {
ESP_LOGE(TAG, "asprintf() returned: %d", rc);
ret = ESP_FAIL;
goto _digest_exit;
}
if (auth_data->opaque) {
asprintf(&temp_auth_str, "%s, opaque=\"%s\"", auth_str, auth_data->opaque);
rc = asprintf(&temp_auth_str, "%s, opaque=\"%s\"", auth_str, auth_data->opaque);
if (rc < 0) {
ESP_LOGE(TAG, "asprintf() returned: %d", rc);
ret = ESP_FAIL;
goto _digest_exit;
}
free(auth_str);
auth_str = temp_auth_str;
}
@@ -134,18 +143,20 @@ _digest_exit:
char *http_auth_basic(const char *username, const char *password)
{
int out;
size_t 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);
if (asprintf(&user_info, "%s:%s", username, password) < 0) {
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);
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));
esp_crypto_base64_encode((unsigned char *)digest + 6, n, &out, (const unsigned char *)user_info, strlen(user_info));
_basic_exit:
free(user_info);
return (ret == ESP_OK) ? digest : NULL;