Skip to content

Commit 64c3b13

Browse files
committed
refactor(ap): use httpd_query_key_value for robust form parsing
Replaces manual tokenization with the built-in ESP-IDF query parser for better reliability.
1 parent d391bc9 commit 64c3b13

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

main/ap.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -414,21 +414,24 @@ static esp_err_t save_handler(httpd_req_t *req) {
414414
char ssid[100] = {0};
415415
char password[200] = {0};
416416
char image_url[400] = {0};
417+
char swap_val[2] = {0};
417418
bool swap_colors = false;
418419

419-
char *saveptr;
420-
char *token = strtok_r(buf, "&", &saveptr);
421-
while (token != NULL) {
422-
if (strncmp(token, "ssid=", 5) == 0) {
423-
strncpy(ssid, token + 5, sizeof(ssid) - 1);
424-
} else if (strncmp(token, "password=", 9) == 0) {
425-
strncpy(password, token + 9, sizeof(password) - 1);
426-
} else if (strncmp(token, "image_url=", 10) == 0) {
427-
strncpy(image_url, token + 10, sizeof(image_url) - 1);
428-
} else if (strncmp(token, "swap_colors=", 12) == 0) {
429-
swap_colors = (strncmp(token + 12, "1", 1) == 0);
430-
}
431-
token = strtok_r(NULL, "&", &saveptr);
420+
// Use httpd_query_key_value to parse form data
421+
if (httpd_query_key_value(buf, "ssid", ssid, sizeof(ssid)) != ESP_OK) {
422+
ESP_LOGD(TAG, "SSID param missing");
423+
}
424+
425+
if (httpd_query_key_value(buf, "password", password, sizeof(password)) != ESP_OK) {
426+
ESP_LOGD(TAG, "Password param missing");
427+
}
428+
429+
if (httpd_query_key_value(buf, "image_url", image_url, sizeof(image_url)) != ESP_OK) {
430+
ESP_LOGD(TAG, "Image URL param missing");
431+
}
432+
433+
if (httpd_query_key_value(buf, "swap_colors", swap_val, sizeof(swap_val)) == ESP_OK) {
434+
swap_colors = (strcmp(swap_val, "1") == 0);
432435
}
433436

434437
url_decode(ssid);

0 commit comments

Comments
 (0)