Skip to content

Commit 447e69d

Browse files
committed
try to fix http parsing # 1
1 parent 561426c commit 447e69d

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

libraries/ESP8266WebServer/src/Parsing.cpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
static const char Content_Type[] PROGMEM = "Content-Type";
3636
static const char filename[] PROGMEM = "filename";
3737

38+
// XXX todo build a String instead of malloc/realloc and because final result is now put in a string
3839
static char* readBytesWithTimeout(WiFiClient& client, size_t maxLength, size_t& dataLength, int timeout_ms)
3940
{
4041
char *buf = nullptr;
@@ -70,6 +71,10 @@ static char* readBytesWithTimeout(WiFiClient& client, size_t maxLength, size_t&
7071
bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
7172
// Read the first line of HTTP request
7273
String req = client.readStringUntil('\r');
74+
#ifdef DEBUG_ESP_HTTP_SERVER
75+
DEBUG_OUTPUT.print("request: ");
76+
DEBUG_OUTPUT.println(req);
77+
#endif
7378
client.readStringUntil('\n');
7479
//reset header value
7580
for (int i = 0; i < _headerKeysCount; ++i) {
@@ -82,8 +87,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
8287
int addr_end = req.indexOf(' ', addr_start + 1);
8388
if (addr_start == -1 || addr_end == -1) {
8489
#ifdef DEBUG_ESP_HTTP_SERVER
85-
DEBUG_OUTPUT.print("Invalid request: ");
86-
DEBUG_OUTPUT.println(req);
90+
DEBUG_OUTPUT.println("Invalid request");
8791
#endif
8892
return false;
8993
}
@@ -139,7 +143,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
139143
String headerName;
140144
String headerValue;
141145
bool isForm = false;
142-
//bool isEncoded = false;
146+
bool isEncoded = false;
143147
uint32_t contentLength = 0;
144148
//parse headers
145149
while(1){
@@ -168,7 +172,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
168172
isForm = false;
169173
} else if (headerValue.startsWith(F("application/x-www-form-urlencoded"))){
170174
isForm = false;
171-
//isEncoded = true;
175+
isEncoded = true;
172176
} else if (headerValue.startsWith(F("multipart/"))){
173177
boundaryStr = headerValue.substring(headerValue.indexOf('=') + 1);
174178
boundaryStr.replace("\"","");
@@ -181,34 +185,40 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
181185
}
182186
}
183187

184-
// always parse url for key/value pairs
188+
String plainBuf;
189+
if (!isForm) {
190+
// read content into plainBuf
191+
size_t plainLength;
192+
char* plainBufTmp = readBytesWithTimeout(client, contentLength, plainLength, HTTP_MAX_POST_WAIT);
193+
plainBuf = plainBufTmp;
194+
free(plainBufTmp);
195+
if (plainBuf.length() < contentLength)
196+
return false;
197+
}
198+
199+
if (isEncoded) {
200+
// isEncoded => !isForm => plainBuf is not empty
201+
// add plainBuf in search str
202+
if (searchStr.length())
203+
searchStr += '&';
204+
searchStr += plainBuf;
205+
}
206+
207+
// parse searchStr for key/value pairs
185208
_parseArguments(searchStr);
186209

187210
if (!isForm) {
188211
if (contentLength) {
189-
190212
// add key=value: plain={body} (post json or other data)
191-
192-
size_t plainLength;
193-
char* plainBuf = readBytesWithTimeout(client, contentLength, plainLength, HTTP_MAX_POST_WAIT);
194-
if (plainLength < contentLength) {
195-
free(plainBuf);
196-
return false;
197-
}
198-
199213
RequestArgument& arg = _currentArgs[_currentArgCount++];
200214
arg.key = F("plain");
201-
arg.value = String(plainBuf);
202-
203-
free(plainBuf);
204-
215+
arg.value = plainBuf;
205216
}
206217
} else { // isForm is true
207-
218+
// here: content is not yet read (plainBuf is still empty)
208219
if (!_parseForm(client, boundaryStr, contentLength)) {
209220
return false;
210221
}
211-
212222
}
213223
} else {
214224
String headerName;

0 commit comments

Comments
 (0)