Skip to content

Commit

Permalink
replace Strings with with StreamStrings to avoid String Reallocations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lan-Hekary committed Nov 17, 2023
1 parent f09b8b1 commit 9697146
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions libraries/ESP8266WebServer/src/Parsing-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(':');
Expand Down Expand Up @@ -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(':');
Expand Down Expand Up @@ -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);

Expand All @@ -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('=');
Expand All @@ -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);
Expand All @@ -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';
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 9697146

Please sign in to comment.