From 1fffe66888ab20cba4208043960ca5d6cea0588d Mon Sep 17 00:00:00 2001
From: Ahmed El-Sharnoby <lan.hekary@gmail.com>
Date: Thu, 19 Oct 2023 19:35:00 +0000
Subject: [PATCH] replace Strings with with StreamStrings to avoid String
 Reallocations.

---
 libraries/ESP8266WebServer/src/Parsing-impl.h | 25 ++++++++++++-------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/libraries/ESP8266WebServer/src/Parsing-impl.h b/libraries/ESP8266WebServer/src/Parsing-impl.h
index 59b02c3182..27915d4077 100644
--- a/libraries/ESP8266WebServer/src/Parsing-impl.h
+++ b/libraries/ESP8266WebServer/src/Parsing-impl.h
@@ -44,7 +44,8 @@ static bool readBytesWithTimeout(typename ServerType::ClientType& client, size_t
 template <typename ServerType>
 typename ESP8266WebServerTemplate<ServerType>::ClientFuture ESP8266WebServerTemplate<ServerType>::_parseRequest(ClientType& client) {
   // Read the first line of HTTP request
-  String req = client.readStringUntil('\r');
+  StreamString req;
+  client.sendUntil(req, '\r');
   DBGWS("request: %s\n", req.c_str());
   client.readStringUntil('\n');
   //reset header value
@@ -122,7 +123,8 @@ typename ESP8266WebServerTemplate<ServerType>::ClientFuture ESP8266WebServerTemp
     uint32_t contentLength = 0;
     //parse headers
     while(1){
-      req = client.readStringUntil('\r');
+      req.clear();
+      client.sendUntil(req, '\r');
       client.readStringUntil('\n');
       if (req.isEmpty()) break; //no more headers
       int headerDiv = req.indexOf(':');
@@ -198,7 +200,8 @@ typename ESP8266WebServerTemplate<ServerType>::ClientFuture ESP8266WebServerTemp
     String headerValue;
     //parse headers
     while(1){
-      req = client.readStringUntil('\r');
+      req.clear();
+      client.sendUntil(req, '\r');
       client.readStringUntil('\n');
       if (req.isEmpty()) break;//no moar headers
       int headerDiv = req.indexOf(':');
@@ -348,10 +351,10 @@ template <typename ServerType>
 bool ESP8266WebServerTemplate<ServerType>::_parseForm(ClientType& client, const String& boundary, uint32_t len){
   (void) len;
   DBGWS("Parse Form: Boundary: '%s' Length: %d\n", boundary.c_str(), (int)len);
-  String line;
+  StreamString line;
   int retry = 0;
   do {
-    line = client.readStringUntil('\r');
+    client.sendUntil(line, '\r');
     ++retry;
   } while (line.length() == 0 && retry < 3);
 
@@ -368,7 +371,8 @@ bool ESP8266WebServerTemplate<ServerType>::_parseForm(ClientType& client, const
       String argFilename;
       bool argIsFile = false;
 
-      line = client.readStringUntil('\r');
+      line.clear();
+      client.sendUntil(line, '\r');
       client.readStringUntil('\n');
       if (line.length() > 19 && line.substring(0, 19).equalsIgnoreCase(F("Content-Disposition"))){
         int nameStart = line.indexOf('=');
@@ -389,7 +393,8 @@ bool ESP8266WebServerTemplate<ServerType>::_parseForm(ClientType& client, const
           DBGWS("PostArg Name: %s\n", argName.c_str());
           using namespace mime;
           argType = FPSTR(mimeTable[txt].mimeType);
-          line = client.readStringUntil('\r');
+          line.clear();
+          client.sendUntil(line, '\r');
           client.readStringUntil('\n');
           if (line.length() > 12 && line.substring(0, 12).equalsIgnoreCase(FPSTR(Content_Type))){
             argType = line.substring(line.indexOf(':')+2);
@@ -400,7 +405,8 @@ bool ESP8266WebServerTemplate<ServerType>::_parseForm(ClientType& client, const
           DBGWS("PostArg Type: %s\n", argType.c_str());
           if (!argIsFile){
             while(1){
-              line = client.readStringUntil('\r');
+              line.clear();
+              client.sendUntil(line, '\r');
               client.readStringUntil('\n');
               if (line.startsWith("--"+boundary)) break;
               if (argValue.length() > 0) argValue += '\n';
@@ -475,7 +481,8 @@ bool ESP8266WebServerTemplate<ServerType>::_parseForm(ClientType& client, const
                 _currentUpload->type.c_str(),
                 (int)_currentUpload->totalSize);
             if (!client.connected()) return _parseFormUploadAborted();
-            line = client.readStringUntil('\r');
+            line.clear();
+            client.sendUntil(line, '\r');
             client.readStringUntil('\n');
             if (line == "--") {     // extra two dashes mean we reached the end of all form fields
                 DBGWS("Done Parsing POST\n");