31
31
#define DEBUG_OUTPUT Serial
32
32
#endif
33
33
34
+ static char * readBytesWithTimeout (WiFiClient& client, size_t maxLength, size_t & dataLength, int timeout_ms)
35
+ {
36
+ char *buf = nullptr ;
37
+ dataLength = 0 ;
38
+ while (dataLength < maxLength) {
39
+ int tries = timeout_ms;
40
+ size_t newLength;
41
+ while (!(newLength = client.available ()) && tries--) delay (1 );
42
+ if (!newLength) {
43
+ break ;
44
+ }
45
+ if (!buf) {
46
+ buf = (char *) malloc (newLength + 1 );
47
+ if (!buf) {
48
+ return nullptr ;
49
+ }
50
+ }
51
+ else {
52
+ char * newBuf = (char *) realloc (buf, dataLength + newLength + 1 );
53
+ if (!newBuf) {
54
+ free (buf);
55
+ return nullptr ;
56
+ }
57
+ buf = newBuf;
58
+ }
59
+ client.readBytes (buf + dataLength, newLength);
60
+ dataLength += newLength;
61
+ buf[dataLength] = ' \0 ' ;
62
+ }
63
+ return buf;
64
+ }
65
+
34
66
bool ESP8266WebServer::_parseRequest (WiFiClient& client) {
35
67
// Read the first line of HTTP request
36
68
String req = client.readStringUntil (' \r ' );
@@ -114,14 +146,14 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
114
146
headerValue = req.substring (headerDiv + 1 );
115
147
headerValue.trim ();
116
148
_collectHeader (headerName.c_str (),headerValue.c_str ());
117
-
149
+
118
150
#ifdef DEBUG_ESP_HTTP_SERVER
119
151
DEBUG_OUTPUT.print (" headerName: " );
120
152
DEBUG_OUTPUT.println (headerName);
121
153
DEBUG_OUTPUT.print (" headerValue: " );
122
154
DEBUG_OUTPUT.println (headerValue);
123
155
#endif
124
-
156
+
125
157
if (headerName == " Content-Type" ){
126
158
if (headerValue.startsWith (" text/plain" )){
127
159
isForm = false ;
@@ -137,41 +169,27 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
137
169
}
138
170
139
171
if (!isForm){
140
- if (searchStr != " " ) searchStr += ' &' ;
141
- char *plainBuf = nullptr ;
142
- size_t plainLen = 0 ;
143
- do
144
- {
145
- // some clients send headers first and data after (like we do)
146
- // give them a chance
147
- int tries = 1000 ;// 1000ms max wait
148
- size_t newLen;
149
- while ( !(newLen = client.available ()) && tries--) delay (1 );
150
- if (!newLen) break ;
151
- plainBuf = (plainBuf == nullptr ) ? (char *) malloc (newLen + 1 ) : (char *) realloc (plainBuf, plainLen + newLen + 1 );
152
- client.readBytes (&plainBuf[plainLen], newLen);
153
- plainLen += newLen;
154
- plainBuf[plainLen] = ' \0 ' ;
155
- } while (plainLen < contentLength);
156
- /* if data loss, exit */
157
- if (plainBuf == nullptr ) return false ;
158
- if (plainLen < contentLength)
159
- {
172
+ size_t plainLength;
173
+ char * plainBuf = readBytesWithTimeout (client, contentLength, plainLength, HTTP_MAX_POST_WAIT);
174
+ if (plainLength < contentLength) {
160
175
free (plainBuf);
161
176
return false ;
162
177
}
163
178
#ifdef DEBUG_ESP_HTTP_SERVER
164
179
DEBUG_OUTPUT.print (" Plain: " );
165
180
DEBUG_OUTPUT.println (plainBuf);
166
181
#endif
167
- if (plainBuf[0 ] == ' {' || plainBuf[0 ] == ' [' || strstr (plainBuf, " =" ) == NULL ){
168
- // plain post json or other data
169
- searchStr += " plain=" ;
170
- searchStr += plainBuf;
171
- } else {
172
- searchStr += plainBuf;
182
+ if (contentLength > 0 ) {
183
+ if (searchStr != " " ) searchStr += ' &' ;
184
+ if (plainBuf[0 ] == ' {' || plainBuf[0 ] == ' [' || strstr (plainBuf, " =" ) == NULL ){
185
+ // plain post json or other data
186
+ searchStr += " plain=" ;
187
+ searchStr += plainBuf;
188
+ } else {
189
+ searchStr += plainBuf;
190
+ }
191
+ free (plainBuf);
173
192
}
174
- free (plainBuf);
175
193
}
176
194
_parseArguments (searchStr);
177
195
if (isForm){
@@ -194,14 +212,14 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
194
212
headerName = req.substring (0 , headerDiv);
195
213
headerValue = req.substring (headerDiv + 2 );
196
214
_collectHeader (headerName.c_str (),headerValue.c_str ());
197
-
215
+
198
216
#ifdef DEBUG_ESP_HTTP_SERVER
199
217
DEBUG_OUTPUT.print (" headerName: " );
200
218
DEBUG_OUTPUT.println (headerName);
201
219
DEBUG_OUTPUT.print (" headerValue: " );
202
220
DEBUG_OUTPUT.println (headerValue);
203
221
#endif
204
-
222
+
205
223
if (headerName == " Host" ){
206
224
_hostHeader = headerValue;
207
225
}
0 commit comments