From 6112726b6d47c067e8e13da27187252342adff41 Mon Sep 17 00:00:00 2001 From: Terrence Date: Tue, 10 Sep 2024 00:46:58 +0800 Subject: [PATCH] fix wifi name with space and Chinese --- main/WifiConfigurationAp.cc | 27 ++++++++++++++++++++++++++- main/WifiConfigurationAp.h | 1 + 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/main/WifiConfigurationAp.cc b/main/WifiConfigurationAp.cc index 456acdd9..88f08955 100644 --- a/main/WifiConfigurationAp.cc +++ b/main/WifiConfigurationAp.cc @@ -159,9 +159,12 @@ void WifiConfigurationAp::StartWebServer() buf[ret] = '\0'; ESP_LOGI(TAG, "Received form data: %s", buf); + std::string decoded = UrlDecode(buf); + ESP_LOGI(TAG, "Decoded form data: %s", decoded.c_str()); + // Parse the form data char ssid[32], password[64]; - if (sscanf(buf, "ssid=%32[^&]&password=%64s", ssid, password) != 2) { + if (sscanf(decoded.c_str(), "ssid=%32[^&]&password=%64s", ssid, password) != 2) { httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid form data"); return ESP_FAIL; } @@ -194,6 +197,28 @@ void WifiConfigurationAp::StartWebServer() ESP_LOGI(TAG, "Web server started"); } +std::string WifiConfigurationAp::UrlDecode(const std::string &url) +{ + std::string decoded; + for (size_t i = 0; i < url.length(); ++i) { + if (url[i] == '%') { + char hex[3]; + hex[0] = url[i + 1]; + hex[1] = url[i + 2]; + hex[2] = '\0'; + char ch = static_cast(std::stoi(hex, nullptr, 16)); + decoded += ch; + i += 2; + } else if (url[i] == '+') { + decoded += ' '; + } else { + decoded += url[i]; + } + } + return decoded; +} + + void WifiConfigurationAp::Start() { builtin_led_.SetBlue(); diff --git a/main/WifiConfigurationAp.h b/main/WifiConfigurationAp.h index 8c14df70..f7d46ef5 100644 --- a/main/WifiConfigurationAp.h +++ b/main/WifiConfigurationAp.h @@ -20,6 +20,7 @@ private: void StartWebServer(); bool ConnectToWifi(const std::string &ssid, const std::string &password); void Save(const std::string &ssid, const std::string &password); + static std::string UrlDecode(const std::string &url); }; #endif // _WIFI_CONFIGURATION_AP_H_