Skip to content

Commit 14d847d

Browse files
authored
Add URL decoding
Handle SSID and passwords with special characters Fixes #8 and #11
1 parent 7ecdb75 commit 14d847d

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

WiFi_portal/WiFi_portal.ino

+34-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,38 @@ void WiFiEvent(WiFiEvent_t event)
6969
}
7070
}
7171

72+
String urlDecode(const String& text)
73+
{
74+
String decoded = "";
75+
char temp[] = "0x00";
76+
unsigned int len = text.length();
77+
unsigned int i = 0;
78+
while (i < len)
79+
{
80+
char decodedChar;
81+
char encodedChar = text.charAt(i++);
82+
if ((encodedChar == '%') && (i + 1 < len))
83+
{
84+
temp[2] = text.charAt(i++);
85+
temp[3] = text.charAt(i++);
86+
87+
decodedChar = strtol(temp, NULL, 16);
88+
}
89+
else {
90+
if (encodedChar == '+')
91+
{
92+
decodedChar = ' ';
93+
}
94+
else {
95+
decodedChar = encodedChar; // normal ascii char
96+
}
97+
}
98+
decoded += decodedChar;
99+
}
100+
return decoded;
101+
}
102+
103+
72104
void setup()
73105
{
74106
Serial.begin(115200);
@@ -177,11 +209,11 @@ void wifiDisconnectedLoop()
177209
preferences.clear();
178210

179211
String qsid;
180-
qsid = currentLine.substring(12, currentLine.indexOf('&')); //parse ssid
212+
qsid = urlDecode(currentLine.substring(12, currentLine.indexOf('&'))); //parse ssid
181213
Serial.println(qsid);
182214
Serial.println("");
183215
String qpass;
184-
qpass = currentLine.substring(currentLine.lastIndexOf('=') + 1, currentLine.lastIndexOf(' ')); //parse password
216+
qpass = urlDecode(currentLine.substring(currentLine.lastIndexOf('=') + 1, currentLine.lastIndexOf(' '))); //parse password
185217
Serial.println(qpass);
186218
Serial.println("");
187219

0 commit comments

Comments
 (0)