feat: Add support for percent-encoding in password and it's corresponding test

Add support of percent-encoded characters when passing the username and password in URI

Closes https://github.com/espressif/esp-mqtt/issues/294
This commit is contained in:
Bogdan Kolendovskyy
2026-04-23 16:05:24 +02:00
parent f192911f6b
commit 32df7e27fc
18 changed files with 427 additions and 27 deletions
+8
View File
@@ -0,0 +1,8 @@
set(srcs mqtt_utils.c)
add_library(mqtt_utils_lib ${srcs})
target_include_directories(mqtt_utils_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(mqtt_utils_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include)
idf_component_get_property(log log COMPONENT_LIB)
target_link_libraries(mqtt_utils_lib PRIVATE ${log})
add_library(idf::mqtt::utils ALIAS mqtt_utils_lib)
+17
View File
@@ -0,0 +1,17 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif // #ifdef __cplusplus
char *mqtt_create_string(const char *ptr, int len);
int esp_mqtt_decode_percent_encoded_string(char *uri);
#ifdef __cplusplus
}
#endif // #ifdef __cplusplus
+54
View File
@@ -0,0 +1,54 @@
/*
* SPDX-FileCopyrightText: 2025-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "include/mqtt_utils.h"
char *mqtt_create_string(const char *ptr, int len)
{
if (len <= 0) {
return NULL;
}
char *ret = calloc(1, len + 1);
if (ret == NULL) {
return NULL;
}
memcpy(ret, ptr, len);
return ret;
}
int esp_mqtt_decode_percent_encoded_string(char *uri)
{
if (uri == NULL) {
return -1;
}
char *write_ptr = uri;
size_t uri_len = strlen(uri);
for (intptr_t i = 0; i < uri_len; i++, write_ptr++) {
if (uri[i] == '%') {
if (!(isxdigit((unsigned char) uri[i + 1]) && isxdigit((unsigned char) uri[i + 2]))) {
// having non [0-9a-fA-F] characters after % is illegal in URI
return -1;
}
char hexvalue[3] = {0, 0, 0};
memcpy(hexvalue, uri + i + 1, 2);
*write_ptr = (char) strtol(hexvalue, NULL, 16);
i += 2;
} else {
*write_ptr = uri[i];
}
}
*write_ptr = '\0';
return (int)(write_ptr - uri);
}