Skip to content

Commit 1fffe66

Browse files
committed
replace Strings with with StreamStrings to avoid String Reallocations.
1 parent 3cbe088 commit 1fffe66

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

libraries/ESP8266WebServer/src/Parsing-impl.h

+16-9
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ static bool readBytesWithTimeout(typename ServerType::ClientType& client, size_t
4444
template <typename ServerType>
4545
typename ESP8266WebServerTemplate<ServerType>::ClientFuture ESP8266WebServerTemplate<ServerType>::_parseRequest(ClientType& client) {
4646
// Read the first line of HTTP request
47-
String req = client.readStringUntil('\r');
47+
StreamString req;
48+
client.sendUntil(req, '\r');
4849
DBGWS("request: %s\n", req.c_str());
4950
client.readStringUntil('\n');
5051
//reset header value
@@ -122,7 +123,8 @@ typename ESP8266WebServerTemplate<ServerType>::ClientFuture ESP8266WebServerTemp
122123
uint32_t contentLength = 0;
123124
//parse headers
124125
while(1){
125-
req = client.readStringUntil('\r');
126+
req.clear();
127+
client.sendUntil(req, '\r');
126128
client.readStringUntil('\n');
127129
if (req.isEmpty()) break; //no more headers
128130
int headerDiv = req.indexOf(':');
@@ -198,7 +200,8 @@ typename ESP8266WebServerTemplate<ServerType>::ClientFuture ESP8266WebServerTemp
198200
String headerValue;
199201
//parse headers
200202
while(1){
201-
req = client.readStringUntil('\r');
203+
req.clear();
204+
client.sendUntil(req, '\r');
202205
client.readStringUntil('\n');
203206
if (req.isEmpty()) break;//no moar headers
204207
int headerDiv = req.indexOf(':');
@@ -348,10 +351,10 @@ template <typename ServerType>
348351
bool ESP8266WebServerTemplate<ServerType>::_parseForm(ClientType& client, const String& boundary, uint32_t len){
349352
(void) len;
350353
DBGWS("Parse Form: Boundary: '%s' Length: %d\n", boundary.c_str(), (int)len);
351-
String line;
354+
StreamString line;
352355
int retry = 0;
353356
do {
354-
line = client.readStringUntil('\r');
357+
client.sendUntil(line, '\r');
355358
++retry;
356359
} while (line.length() == 0 && retry < 3);
357360

@@ -368,7 +371,8 @@ bool ESP8266WebServerTemplate<ServerType>::_parseForm(ClientType& client, const
368371
String argFilename;
369372
bool argIsFile = false;
370373

371-
line = client.readStringUntil('\r');
374+
line.clear();
375+
client.sendUntil(line, '\r');
372376
client.readStringUntil('\n');
373377
if (line.length() > 19 && line.substring(0, 19).equalsIgnoreCase(F("Content-Disposition"))){
374378
int nameStart = line.indexOf('=');
@@ -389,7 +393,8 @@ bool ESP8266WebServerTemplate<ServerType>::_parseForm(ClientType& client, const
389393
DBGWS("PostArg Name: %s\n", argName.c_str());
390394
using namespace mime;
391395
argType = FPSTR(mimeTable[txt].mimeType);
392-
line = client.readStringUntil('\r');
396+
line.clear();
397+
client.sendUntil(line, '\r');
393398
client.readStringUntil('\n');
394399
if (line.length() > 12 && line.substring(0, 12).equalsIgnoreCase(FPSTR(Content_Type))){
395400
argType = line.substring(line.indexOf(':')+2);
@@ -400,7 +405,8 @@ bool ESP8266WebServerTemplate<ServerType>::_parseForm(ClientType& client, const
400405
DBGWS("PostArg Type: %s\n", argType.c_str());
401406
if (!argIsFile){
402407
while(1){
403-
line = client.readStringUntil('\r');
408+
line.clear();
409+
client.sendUntil(line, '\r');
404410
client.readStringUntil('\n');
405411
if (line.startsWith("--"+boundary)) break;
406412
if (argValue.length() > 0) argValue += '\n';
@@ -475,7 +481,8 @@ bool ESP8266WebServerTemplate<ServerType>::_parseForm(ClientType& client, const
475481
_currentUpload->type.c_str(),
476482
(int)_currentUpload->totalSize);
477483
if (!client.connected()) return _parseFormUploadAborted();
478-
line = client.readStringUntil('\r');
484+
line.clear();
485+
client.sendUntil(line, '\r');
479486
client.readStringUntil('\n');
480487
if (line == "--") { // extra two dashes mean we reached the end of all form fields
481488
DBGWS("Done Parsing POST\n");

0 commit comments

Comments
 (0)