WebServer: rename & expose internal 'contentLength' variable for POST… (#7012)
This commit is contained in:
parent
0130856b2a
commit
0260cd66a3
3 changed files with 19 additions and 15 deletions
|
|
@ -80,7 +80,7 @@ bool WebServer::_parseRequest(WiFiClient& client) {
|
||||||
//reset header value
|
//reset header value
|
||||||
for (int i = 0; i < _headerKeysCount; ++i) {
|
for (int i = 0; i < _headerKeysCount; ++i) {
|
||||||
_currentHeaders[i].value =String();
|
_currentHeaders[i].value =String();
|
||||||
}
|
}
|
||||||
|
|
||||||
// First line of HTTP request looks like "GET /path HTTP/1.1"
|
// First line of HTTP request looks like "GET /path HTTP/1.1"
|
||||||
// Retrieve the "/path" part by finding the spaces
|
// Retrieve the "/path" part by finding the spaces
|
||||||
|
|
@ -103,6 +103,7 @@ bool WebServer::_parseRequest(WiFiClient& client) {
|
||||||
}
|
}
|
||||||
_currentUri = url;
|
_currentUri = url;
|
||||||
_chunked = false;
|
_chunked = false;
|
||||||
|
_clientContentLength = 0; // not known yet, or invalid
|
||||||
|
|
||||||
HTTPMethod method = HTTP_ANY;
|
HTTPMethod method = HTTP_ANY;
|
||||||
size_t num_methods = sizeof(_http_method_str) / sizeof(const char *);
|
size_t num_methods = sizeof(_http_method_str) / sizeof(const char *);
|
||||||
|
|
@ -136,7 +137,6 @@ bool WebServer::_parseRequest(WiFiClient& client) {
|
||||||
String headerValue;
|
String headerValue;
|
||||||
bool isForm = false;
|
bool isForm = false;
|
||||||
bool isEncoded = false;
|
bool isEncoded = false;
|
||||||
uint32_t contentLength = 0;
|
|
||||||
//parse headers
|
//parse headers
|
||||||
while(1){
|
while(1){
|
||||||
req = client.readStringUntil('\r');
|
req = client.readStringUntil('\r');
|
||||||
|
|
@ -167,7 +167,7 @@ bool WebServer::_parseRequest(WiFiClient& client) {
|
||||||
isForm = true;
|
isForm = true;
|
||||||
}
|
}
|
||||||
} else if (headerName.equalsIgnoreCase(F("Content-Length"))){
|
} else if (headerName.equalsIgnoreCase(F("Content-Length"))){
|
||||||
contentLength = headerValue.toInt();
|
_clientContentLength = headerValue.toInt();
|
||||||
} else if (headerName.equalsIgnoreCase(F("Host"))){
|
} else if (headerName.equalsIgnoreCase(F("Host"))){
|
||||||
_hostHeader = headerValue;
|
_hostHeader = headerValue;
|
||||||
}
|
}
|
||||||
|
|
@ -175,12 +175,12 @@ bool WebServer::_parseRequest(WiFiClient& client) {
|
||||||
|
|
||||||
if (!isForm){
|
if (!isForm){
|
||||||
size_t plainLength;
|
size_t plainLength;
|
||||||
char* plainBuf = readBytesWithTimeout(client, contentLength, plainLength, HTTP_MAX_POST_WAIT);
|
char* plainBuf = readBytesWithTimeout(client, _clientContentLength, plainLength, HTTP_MAX_POST_WAIT);
|
||||||
if (plainLength < contentLength) {
|
if (plainLength < _clientContentLength) {
|
||||||
free(plainBuf);
|
free(plainBuf);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (contentLength > 0) {
|
if (_clientContentLength > 0) {
|
||||||
if(isEncoded){
|
if(isEncoded){
|
||||||
//url encoded form
|
//url encoded form
|
||||||
if (searchStr != "") searchStr += '&';
|
if (searchStr != "") searchStr += '&';
|
||||||
|
|
@ -200,11 +200,10 @@ bool WebServer::_parseRequest(WiFiClient& client) {
|
||||||
// No content - but we can still have arguments in the URL.
|
// No content - but we can still have arguments in the URL.
|
||||||
_parseArguments(searchStr);
|
_parseArguments(searchStr);
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
// it IS a form
|
||||||
if (isForm){
|
|
||||||
_parseArguments(searchStr);
|
_parseArguments(searchStr);
|
||||||
if (!_parseForm(client, boundaryStr, contentLength)) {
|
if (!_parseForm(client, boundaryStr, _clientContentLength)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,7 @@ WebServer::WebServer(IPAddress addr, int port)
|
||||||
, _headerKeysCount(0)
|
, _headerKeysCount(0)
|
||||||
, _currentHeaders(nullptr)
|
, _currentHeaders(nullptr)
|
||||||
, _contentLength(0)
|
, _contentLength(0)
|
||||||
|
, _clientContentLength(0)
|
||||||
, _chunked(false)
|
, _chunked(false)
|
||||||
{
|
{
|
||||||
log_v("WebServer::Webserver(addr=%s, port=%d)", addr.toString().c_str(), port);
|
log_v("WebServer::Webserver(addr=%s, port=%d)", addr.toString().c_str(), port);
|
||||||
|
|
@ -80,6 +81,7 @@ WebServer::WebServer(int port)
|
||||||
, _headerKeysCount(0)
|
, _headerKeysCount(0)
|
||||||
, _currentHeaders(nullptr)
|
, _currentHeaders(nullptr)
|
||||||
, _contentLength(0)
|
, _contentLength(0)
|
||||||
|
, _clientContentLength(0)
|
||||||
, _chunked(false)
|
, _chunked(false)
|
||||||
{
|
{
|
||||||
log_v("WebServer::Webserver(port=%d)", port);
|
log_v("WebServer::Webserver(port=%d)", port);
|
||||||
|
|
|
||||||
|
|
@ -105,11 +105,13 @@ public:
|
||||||
int args(); // get arguments count
|
int args(); // get arguments count
|
||||||
bool hasArg(String name); // check if argument exists
|
bool hasArg(String name); // check if argument exists
|
||||||
void collectHeaders(const char* headerKeys[], const size_t headerKeysCount); // set the request headers to collect
|
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(String name); // get request header value by name
|
||||||
String header(int i); // get request header value by number
|
String header(int i); // get request header value by number
|
||||||
String headerName(int i); // get request header name by number
|
String headerName(int i); // get request header name by number
|
||||||
int headers(); // get header count
|
int headers(); // get header count
|
||||||
bool hasHeader(String name); // check if header exists
|
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
|
String hostHeader(); // get request host header if available or empty String if not
|
||||||
|
|
||||||
|
|
@ -198,6 +200,7 @@ protected:
|
||||||
int _headerKeysCount;
|
int _headerKeysCount;
|
||||||
RequestArgument* _currentHeaders;
|
RequestArgument* _currentHeaders;
|
||||||
size_t _contentLength;
|
size_t _contentLength;
|
||||||
|
int _clientContentLength; // "Content-Length" from header of incoming POST or GET request
|
||||||
String _responseHeaders;
|
String _responseHeaders;
|
||||||
|
|
||||||
String _hostHeader;
|
String _hostHeader;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue