Skip to content

Url decode fixes #2212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jun 30, 2016
43 changes: 25 additions & 18 deletions libraries/ESP8266WebServer/src/Parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
String searchStr = "";
int hasSearch = url.indexOf('?');
if (hasSearch != -1){
searchStr = url.substring(hasSearch + 1);
searchStr = urlDecode(url.substring(hasSearch + 1));
url = url.substring(0, hasSearch);
}
_currentUri = url;
Expand Down Expand Up @@ -135,6 +135,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
String headerName;
String headerValue;
bool isForm = false;
bool isEncoded = false;
uint32_t contentLength = 0;
//parse headers
while(1){
Expand All @@ -150,17 +151,20 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
headerValue.trim();
_collectHeader(headerName.c_str(),headerValue.c_str());

#ifdef DEBUG_ESP_HTTP_SERVER
DEBUG_OUTPUT.print("headerName: ");
DEBUG_OUTPUT.println(headerName);
DEBUG_OUTPUT.print("headerValue: ");
DEBUG_OUTPUT.println(headerValue);
#endif
#ifdef DEBUG_ESP_HTTP_SERVER
DEBUG_OUTPUT.print("headerName: ");
DEBUG_OUTPUT.println(headerName);
DEBUG_OUTPUT.print("headerValue: ");
DEBUG_OUTPUT.println(headerValue);
#endif

if (headerName == "Content-Type"){
if (headerValue.startsWith("text/plain")){
isForm = false;
} else if (headerValue.startsWith("multipart/form-data")){
} else if (headerValue.startsWith("application/x-www-form-urlencoded")){
isForm = false;
isEncoded = true;
} else if (headerValue.startsWith("multipart/")){
boundaryStr = headerValue.substring(headerValue.indexOf('=')+1);
isForm = true;
}
Expand All @@ -178,19 +182,22 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
free(plainBuf);
return false;
}
#ifdef DEBUG_ESP_HTTP_SERVER
DEBUG_OUTPUT.print("Plain: ");
DEBUG_OUTPUT.println(plainBuf);
#endif
if (contentLength > 0) {
if (searchStr != "") searchStr += '&';
if(plainBuf[0] == '{' || plainBuf[0] == '[' || strstr(plainBuf, "=") == NULL){
if(isEncoded){
//url encoded form
String decoded = urlDecode(plainBuf);
size_t decodedLen = decoded.length();
memcpy(plainBuf, decoded.c_str(), decodedLen);
plainBuf[decodedLen] = 0;
} else {
//plain post json or other data
searchStr += "plain=";
searchStr += plainBuf;
} else {
searchStr += plainBuf;
}
searchStr += plainBuf;
#ifdef DEBUG_ESP_HTTP_SERVER
DEBUG_OUTPUT.print("Plain: ");
DEBUG_OUTPUT.println(plainBuf);
#endif
free(plainBuf);
}
}
Expand Down Expand Up @@ -303,7 +310,7 @@ void ESP8266WebServer::_parseArguments(String data) {
}
RequestArgument& arg = _currentArgs[iarg];
arg.key = data.substring(pos, equal_sign_index);
arg.value = urlDecode(data.substring(equal_sign_index + 1, next_arg_index));
arg.value = data.substring(equal_sign_index + 1, next_arg_index);
#ifdef DEBUG_ESP_HTTP_SERVER
DEBUG_OUTPUT.print("arg ");
DEBUG_OUTPUT.print(iarg);
Expand Down