mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
fixes : set_url discards username and password
This commit is contained in:
parent
57335cfc26
commit
7566bfb548
@ -294,6 +294,21 @@ esp_err_t esp_http_client_get_username(esp_http_client_handle_t client, char **v
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_http_client_set_username(esp_http_client_handle_t client, const char *username)
|
||||
{
|
||||
if (client == NULL) {
|
||||
ESP_LOGE(TAG, "client must not be NULL");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (username == NULL && client->connection_info.username != NULL) {
|
||||
free(client->connection_info.username);
|
||||
client->connection_info.username = NULL;
|
||||
} else if (username != NULL) {
|
||||
client->connection_info.username = strdup(username);
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_http_client_get_password(esp_http_client_handle_t client, char **value)
|
||||
{
|
||||
if (client == NULL || value == NULL) {
|
||||
@ -304,6 +319,22 @@ esp_err_t esp_http_client_get_password(esp_http_client_handle_t client, char **v
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_http_client_set_password(esp_http_client_handle_t client, char *password)
|
||||
{
|
||||
if (client == NULL) {
|
||||
ESP_LOGE(TAG, "client must not be NULL");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (password == NULL && client->connection_info.password != NULL) {
|
||||
memset(client->connection_info.password, 0, strlen(client->connection_info.password));
|
||||
free(client->connection_info.password);
|
||||
client->connection_info.password = NULL;
|
||||
} else if (password != NULL) {
|
||||
client->connection_info.password = strdup(password);
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t _set_config(esp_http_client_handle_t client, const esp_http_client_config_t *config)
|
||||
{
|
||||
client->connection_info.method = config->method;
|
||||
@ -660,10 +691,7 @@ esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *u
|
||||
}
|
||||
old_port = client->connection_info.port;
|
||||
|
||||
// Whether the passed url is absolute or is just a path
|
||||
bool is_absolute_url = (bool) purl.field_data[UF_HOST].len;
|
||||
|
||||
if (is_absolute_url) {
|
||||
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, return ESP_ERR_NO_MEM);
|
||||
}
|
||||
@ -720,15 +748,8 @@ esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *u
|
||||
} else {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
} else if (is_absolute_url) {
|
||||
// Only reset authentication info if the passed URL is full
|
||||
free(client->connection_info.username);
|
||||
free(client->connection_info.password);
|
||||
client->connection_info.username = NULL;
|
||||
client->connection_info.password = NULL;
|
||||
}
|
||||
|
||||
|
||||
//Reset path and query if there are no information
|
||||
if (purl.field_data[UF_PATH].len) {
|
||||
http_utils_assign_string(&client->connection_info.path, url + purl.field_data[UF_PATH].off, purl.field_data[UF_PATH].len);
|
||||
|
@ -264,6 +264,20 @@ esp_err_t esp_http_client_get_header(esp_http_client_handle_t client, const char
|
||||
*/
|
||||
esp_err_t esp_http_client_get_username(esp_http_client_handle_t client, char **value);
|
||||
|
||||
/**
|
||||
* @brief Set http request username.
|
||||
* The value of username parameter will be assigned to username buffer.
|
||||
* If the username parameter is NULL then username buffer will be freed.
|
||||
*
|
||||
* @param[in] client The esp_http_client handle
|
||||
* @param[in] username The username value
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK
|
||||
* - ESP_ERR_INVALID_ARG
|
||||
*/
|
||||
esp_err_t esp_http_client_set_username(esp_http_client_handle_t client, const char *username);
|
||||
|
||||
/**
|
||||
* @brief Get http request password.
|
||||
* The address of password buffer will be assigned to value parameter.
|
||||
@ -278,6 +292,20 @@ esp_err_t esp_http_client_get_username(esp_http_client_handle_t client, char **v
|
||||
*/
|
||||
esp_err_t esp_http_client_get_password(esp_http_client_handle_t client, char **value);
|
||||
|
||||
/**
|
||||
* @brief Set http request password.
|
||||
* The value of password parameter will be assigned to password buffer.
|
||||
* If the password parameter is NULL then password buffer will be freed.
|
||||
*
|
||||
* @param[in] client The esp_http_client handle
|
||||
* @param[in] password The password value
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK
|
||||
* - ESP_ERR_INVALID_ARG
|
||||
*/
|
||||
esp_err_t esp_http_client_set_password(esp_http_client_handle_t client, char *password);
|
||||
|
||||
/**
|
||||
* @brief Set http request method
|
||||
*
|
||||
|
@ -103,10 +103,11 @@ TEST_CASE("Username is unmodified when we change to new path", "[ESP HTTP CLIENT
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case to test that, the esp_http_client_set_url will reset username and password
|
||||
* when passing a full URL with username & password missing.
|
||||
* Test case to test that, the esp_http_client_set_url do not reset the auth credentials
|
||||
* Explicit APIs esp_http_client_set_username and esp_http_client_set_password are used to change
|
||||
* the auth credentials
|
||||
**/
|
||||
TEST_CASE("Username is reset if new absolute URL doesnot specify username.", "[ESP HTTP CLIENT]")
|
||||
TEST_CASE("Username and password will not reset if new absolute URL doesnot specify auth credentials.", "[ESP HTTP CLIENT]")
|
||||
{
|
||||
esp_http_client_config_t config_with_auth = {
|
||||
.host = HOST,
|
||||
@ -122,8 +123,17 @@ TEST_CASE("Username is reset if new absolute URL doesnot specify username.", "[E
|
||||
TEST_ASSERT_NOT_NULL(value);
|
||||
TEST_ASSERT_EQUAL_STRING(USERNAME, value);
|
||||
esp_http_client_set_url(client, "http://" HOST "/get");
|
||||
esp_http_client_set_username(client, value);
|
||||
esp_http_client_set_password(client, value);
|
||||
//checks if username is set or not
|
||||
r = esp_http_client_get_username(client, &value);
|
||||
TEST_ASSERT_EQUAL(ESP_OK, r);
|
||||
TEST_ASSERT_NULL(value);
|
||||
//If username is set then value should not be NULL
|
||||
TEST_ASSERT_NOT_NULL(value);
|
||||
//checks if password is set or not
|
||||
r = esp_http_client_get_password(client, &value);
|
||||
TEST_ASSERT_EQUAL(ESP_OK, r);
|
||||
//If password is set then value should not be NULL
|
||||
TEST_ASSERT_NOT_NULL(value);
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user