Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http_stream: support HTTP authentication (AUD-4994) #1103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions components/audio_stream/http_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ typedef struct http_stream {
int request_range_size;
int64_t request_range_end;
bool is_last_range;
esp_http_client_auth_type_t auth_type; /* HTTP authentication type */
char *username; /* HTTP authentication username */
char *password; /* HTTP authentication password */
} http_stream_t;

static esp_err_t http_stream_auto_connect_next_track(audio_element_handle_t el);
Expand Down Expand Up @@ -586,6 +589,9 @@ static esp_err_t _http_open(audio_element_handle_t self)
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 3, 0)) && defined CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
.crt_bundle_attach = http->crt_bundle_attach,
#endif // (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 3, 0)) && defined CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
.auth_type = http->auth_type,
.username = http->username,
.password = http->password,
};
http->client = esp_http_client_init(&http_cfg);
AUDIO_MEM_CHECK(TAG, http->client, return ESP_ERR_NO_MEM);
Expand Down Expand Up @@ -995,3 +1001,32 @@ esp_err_t http_stream_set_server_cert(audio_element_handle_t el, const char *cer
http->cert_pem = cert;
return ESP_OK;
}

esp_err_t http_stream_set_auth_type(audio_element_handle_t el, esp_http_client_auth_type_t auth_type)
{
http_stream_t *http = (http_stream_t *)audio_element_getdata(el);
http->auth_type = auth_type;
return ESP_OK;
}

esp_err_t http_stream_set_username(audio_element_handle_t el, const char *username)
{
http_stream_t *http = (http_stream_t *)audio_element_getdata(el);
if (http->username != NULL) {
memset(http->username, 0, strlen(http->username));
free(http->username);
}
http->username = username ? strndup(username, strlen(username)) : NULL;
return ESP_OK;
}

esp_err_t http_stream_set_password(audio_element_handle_t el, const char *password)
{
http_stream_t *http = (http_stream_t *)audio_element_getdata(el);
if (http->password != NULL) {
memset(http->password, 0, strlen(http->password));
free(http->password);
}
http->password = password ? strndup(password, strlen(password)) : NULL;
return ESP_OK;
}
34 changes: 34 additions & 0 deletions components/audio_stream/include/http_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "audio_error.h"
#include "audio_element.h"
#include "audio_common.h"
#include "esp_http_client.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -168,6 +169,39 @@ esp_err_t http_stream_fetch_again(audio_element_handle_t el);
*/
esp_err_t http_stream_set_server_cert(audio_element_handle_t el, const char *cert);

/**
* @brief Set HTTP authentication type
*
* @param el The http_stream element handle
* @param auth_type HTTP autentication type, see `esp_http_client_auth_type_t`
*
* @return
* - ESP_OK on success
*/
esp_err_t http_stream_set_auth_type(audio_element_handle_t el, esp_http_client_auth_type_t auth_type);

/**
* @brief Set HTTP authentication username
*
* @param el The http_stream element handle
* @param username The HTTP authentication username
*
* @return
* - ESP_OK on success
*/
esp_err_t http_stream_set_username(audio_element_handle_t el, const char *username);

/**
* @brief Set HTTP authentication password
*
* @param el The http_stream element handle
* @param username The HTTP authentication password
*
* @return
* - ESP_OK on success
*/
esp_err_t http_stream_set_password(audio_element_handle_t el, const char *password);

#ifdef __cplusplus
}
#endif
Expand Down