Skip to content

WebServer: rename & expose internal 'contentLength' variable for POST… #7012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions libraries/WebServer/src/Parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ bool WebServer::_parseRequest(WiFiClient& client) {
//reset header value
for (int i = 0; i < _headerKeysCount; ++i) {
_currentHeaders[i].value =String();
}
}

// First line of HTTP request looks like "GET /path HTTP/1.1"
// Retrieve the "/path" part by finding the spaces
Expand All @@ -103,6 +103,7 @@ bool WebServer::_parseRequest(WiFiClient& client) {
}
_currentUri = url;
_chunked = false;
_clientContentLength = 0; // not known yet, or invalid

HTTPMethod method = HTTP_ANY;
size_t num_methods = sizeof(_http_method_str) / sizeof(const char *);
Expand Down Expand Up @@ -136,7 +137,6 @@ bool WebServer::_parseRequest(WiFiClient& client) {
String headerValue;
bool isForm = false;
bool isEncoded = false;
uint32_t contentLength = 0;
//parse headers
while(1){
req = client.readStringUntil('\r');
Expand Down Expand Up @@ -167,20 +167,20 @@ bool WebServer::_parseRequest(WiFiClient& client) {
isForm = true;
}
} else if (headerName.equalsIgnoreCase(F("Content-Length"))){
contentLength = headerValue.toInt();
_clientContentLength = headerValue.toInt();
} else if (headerName.equalsIgnoreCase(F("Host"))){
_hostHeader = headerValue;
}
}

if (!isForm){
size_t plainLength;
char* plainBuf = readBytesWithTimeout(client, contentLength, plainLength, HTTP_MAX_POST_WAIT);
if (plainLength < contentLength) {
char* plainBuf = readBytesWithTimeout(client, _clientContentLength, plainLength, HTTP_MAX_POST_WAIT);
if (plainLength < _clientContentLength) {
free(plainBuf);
return false;
}
if (contentLength > 0) {
if (_clientContentLength > 0) {
if(isEncoded){
//url encoded form
if (searchStr != "") searchStr += '&';
Expand All @@ -200,11 +200,10 @@ bool WebServer::_parseRequest(WiFiClient& client) {
// No content - but we can still have arguments in the URL.
_parseArguments(searchStr);
}
}

if (isForm){
} else {
// it IS a form
_parseArguments(searchStr);
if (!_parseForm(client, boundaryStr, contentLength)) {
if (!_parseForm(client, boundaryStr, _clientContentLength)) {
return false;
}
}
Expand Down
2 changes: 2 additions & 0 deletions libraries/WebServer/src/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ WebServer::WebServer(IPAddress addr, int port)
, _headerKeysCount(0)
, _currentHeaders(nullptr)
, _contentLength(0)
, _clientContentLength(0)
, _chunked(false)
{
log_v("WebServer::Webserver(addr=%s, port=%d)", addr.toString().c_str(), port);
Expand All @@ -80,6 +81,7 @@ WebServer::WebServer(int port)
, _headerKeysCount(0)
, _currentHeaders(nullptr)
, _contentLength(0)
, _clientContentLength(0)
, _chunked(false)
{
log_v("WebServer::Webserver(port=%d)", port);
Expand Down
13 changes: 8 additions & 5 deletions libraries/WebServer/src/WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ class WebServer
int args(); // get arguments count
bool hasArg(String name); // check if argument exists
void collectHeaders(const char* headerKeys[], const size_t headerKeysCount); // set the request headers to collect
String header(String name); // get request header value by name
String header(int i); // get request header value by number
String headerName(int i); // get request header name by number
int headers(); // get header count
bool hasHeader(String name); // check if header exists
String header(String name); // get request header value by name
String header(int i); // get request header value by number
String headerName(int i); // get request header name by number
int headers(); // get header count
bool hasHeader(String name); // check if header exists

int clientContentLength() { return _clientContentLength; } // return "content-length" of incoming HTTP header from "_currentClient"

String hostHeader(); // get request host header if available or empty String if not

Expand Down Expand Up @@ -198,6 +200,7 @@ class WebServer
int _headerKeysCount;
RequestArgument* _currentHeaders;
size_t _contentLength;
int _clientContentLength; // "Content-Length" from header of incoming POST or GET request
String _responseHeaders;

String _hostHeader;
Expand Down