35
35
static const char Content_Type[] PROGMEM = " Content-Type" ;
36
36
static const char filename[] PROGMEM = " filename" ;
37
37
38
+ // XXX todo build a String instead of malloc/realloc and because final result is now put in a string
38
39
static char * readBytesWithTimeout (WiFiClient& client, size_t maxLength, size_t & dataLength, int timeout_ms)
39
40
{
40
41
char *buf = nullptr ;
@@ -70,6 +71,10 @@ static char* readBytesWithTimeout(WiFiClient& client, size_t maxLength, size_t&
70
71
bool ESP8266WebServer::_parseRequest (WiFiClient& client) {
71
72
// Read the first line of HTTP request
72
73
String req = client.readStringUntil (' \r ' );
74
+ #ifdef DEBUG_ESP_HTTP_SERVER
75
+ DEBUG_OUTPUT.print (" request: " );
76
+ DEBUG_OUTPUT.println (req);
77
+ #endif
73
78
client.readStringUntil (' \n ' );
74
79
// reset header value
75
80
for (int i = 0 ; i < _headerKeysCount; ++i) {
@@ -82,8 +87,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
82
87
int addr_end = req.indexOf (' ' , addr_start + 1 );
83
88
if (addr_start == -1 || addr_end == -1 ) {
84
89
#ifdef DEBUG_ESP_HTTP_SERVER
85
- DEBUG_OUTPUT.print (" Invalid request: " );
86
- DEBUG_OUTPUT.println (req);
90
+ DEBUG_OUTPUT.println (" Invalid request" );
87
91
#endif
88
92
return false ;
89
93
}
@@ -139,7 +143,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
139
143
String headerName;
140
144
String headerValue;
141
145
bool isForm = false ;
142
- // bool isEncoded = false;
146
+ bool isEncoded = false ;
143
147
uint32_t contentLength = 0 ;
144
148
// parse headers
145
149
while (1 ){
@@ -168,7 +172,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
168
172
isForm = false ;
169
173
} else if (headerValue.startsWith (F (" application/x-www-form-urlencoded" ))){
170
174
isForm = false ;
171
- // isEncoded = true;
175
+ isEncoded = true ;
172
176
} else if (headerValue.startsWith (F (" multipart/" ))){
173
177
boundaryStr = headerValue.substring (headerValue.indexOf (' =' ) + 1 );
174
178
boundaryStr.replace (" \" " ," " );
@@ -181,34 +185,40 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
181
185
}
182
186
}
183
187
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
185
208
_parseArguments (searchStr);
186
209
187
210
if (!isForm) {
188
211
if (contentLength) {
189
-
190
212
// 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
-
199
213
RequestArgument& arg = _currentArgs[_currentArgCount++];
200
214
arg.key = F (" plain" );
201
- arg.value = String (plainBuf);
202
-
203
- free (plainBuf);
204
-
215
+ arg.value = plainBuf;
205
216
}
206
217
} else { // isForm is true
207
-
218
+ // here: content is not yet read (plainBuf is still empty)
208
219
if (!_parseForm (client, boundaryStr, contentLength)) {
209
220
return false ;
210
221
}
211
-
212
222
}
213
223
} else {
214
224
String headerName;
0 commit comments