From 4eeee1c8c1be065ec1435bc2a2544abe0313ffa8 Mon Sep 17 00:00:00 2001 From: Andreas Horn Date: Sun, 10 Mar 2024 12:26:58 +0100 Subject: [PATCH 1/2] improves readability of comments --- .../ESP8266WebServer/src/ESP8266WebServer.h | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/libraries/ESP8266WebServer/src/ESP8266WebServer.h index 397132f161..7f96449879 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -58,11 +58,11 @@ enum HTTPAuthMethod { BASIC_AUTH, DIGEST_AUTH }; #define HTTP_UPLOAD_BUFLEN 2048 #endif -#define HTTP_MAX_DATA_WAIT 5000 //ms to wait for the client to send the request -#define HTTP_MAX_DATA_AVAILABLE_WAIT 30 //ms to wait for the client to send the request when there is another client with data available -#define HTTP_MAX_POST_WAIT 5000 //ms to wait for POST data to arrive -#define HTTP_MAX_SEND_WAIT 5000 //ms to wait for data chunk to be ACKed -#define HTTP_MAX_CLOSE_WAIT 2000 //ms to wait for the client to close the connection +#define HTTP_MAX_DATA_WAIT 5000 // ms to wait for the client to send the request +#define HTTP_MAX_DATA_AVAILABLE_WAIT 30 // ms to wait for the client to send the request when there is another client with data available +#define HTTP_MAX_POST_WAIT 5000 // ms to wait for POST data to arrive +#define HTTP_MAX_SEND_WAIT 5000 // ms to wait for data chunk to be ACKed +#define HTTP_MAX_CLOSE_WAIT 2000 // ms to wait for the client to close the connection #define CONTENT_LENGTH_UNKNOWN ((size_t) -1) #define CONTENT_LENGTH_NOT_SET ((size_t) -2) @@ -72,9 +72,9 @@ typedef struct { String filename; String name; String type; - size_t totalSize; // total size of uploaded file so far - size_t currentSize; // size of data currently in buf - size_t contentLength; // size of entire post request, file size + headers and other request data. + size_t totalSize; // total size of uploaded file so far + size_t currentSize; // size of data currently in buf + size_t contentLength; // size of entire post request, file size + headers and other request data. uint8_t buf[HTTP_UPLOAD_BUFLEN]; } HTTPUpload; @@ -122,8 +122,8 @@ class ESP8266WebServerTemplate void on(const Uri &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn); void addHandler(RequestHandlerType* handler); void serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_header = NULL ); - void onNotFound(THandlerFunction fn); //called when handler is not assigned - void onFileUpload(THandlerFunction fn); //handle file uploads + void onNotFound(THandlerFunction fn); // called when handler is not assigned + void onFileUpload(THandlerFunction fn); // handle file uploads void enableCORS(bool enable); void enableETag(bool enable, ETagFunction fn = nullptr); @@ -135,21 +135,21 @@ class ESP8266WebServerTemplate // Allows setting server options (i.e. SSL keys) by the instantiator ServerType &getServer() { return _server; } - const String& pathArg(unsigned int i) const; // get request path argument by number + const String& pathArg(unsigned int i) const; // get request path argument by number const String& arg(const String& name) const; // get request argument value by name - const String& arg(int i) const; // get request argument value by number - const String& argName(int i) const; // get request argument name by number - int args() const; // get arguments count - bool hasArg(const String& name) const; // check if argument exists + const String& arg(int i) const; // get request argument value by number + const String& argName(int i) const; // get request argument name by number + int args() const; // get arguments count + bool hasArg(const String& name) const; // check if argument exists void collectHeaders(const char* headerKeys[], const size_t headerKeysCount); // set the request headers to collect template - void collectHeaders(const Args&... args); // set the request headers to collect (variadic template version) + void collectHeaders(const Args&... args); // set the request headers to collect (variadic template version) const String& header(const String& name) const; // get request header value by name - const String& header(int i) const; // get request header value by number - const String& headerName(int i) const; // get request header name by number - int headers() const; // get header count + const String& header(int i) const; // get request header value by number + const String& headerName(int i) const; // get request header name by number + int headers() const; // get header count bool hasHeader(const String& name) const; // check if header exists - const String& hostHeader() const; // get request host header if available or empty String if not + const String& hostHeader() const; // get request host header if available or empty String if not // send response to the client // code - HTTP response code, can be 200 or 404 @@ -350,4 +350,4 @@ class ESP8266WebServerTemplate using ESP8266WebServer = esp8266webserver::ESP8266WebServerTemplate; using RequestHandler = esp8266webserver::RequestHandler; -#endif //ESP8266WEBSERVER_H +#endif // ESP8266WEBSERVER_H From d9ced97c80578a09b26d80b105ce5ba28eaf4b89 Mon Sep 17 00:00:00 2001 From: Andreas Horn Date: Sun, 10 Mar 2024 12:48:11 +0100 Subject: [PATCH 2/2] implements pathArgs method in analogy to the args method This allows to use UriRegex with optional capture groups and therefore varying number of path arguments. --- .../ESP8266WebServer/src/ESP8266WebServer-impl.h | 7 +++++++ libraries/ESP8266WebServer/src/ESP8266WebServer.h | 1 + libraries/ESP8266WebServer/src/Uri.h | 2 +- .../ESP8266WebServer/src/detail/RequestHandler.h | 9 ++++++--- .../src/detail/RequestHandlersImpl.h | 2 +- libraries/ESP8266WebServer/src/uri/UriBraces.h | 12 ++++++------ libraries/ESP8266WebServer/src/uri/UriGlob.h | 2 +- libraries/ESP8266WebServer/src/uri/UriRegex.h | 8 ++++---- 8 files changed, 27 insertions(+), 16 deletions(-) diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h b/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h index f7a95da0af..4e20743902 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h @@ -588,6 +588,13 @@ const String& ESP8266WebServerTemplate::pathArg(unsigned int i) cons return emptyString; } +template +const int ESP8266WebServerTemplate::pathArgs() const { + if (_currentHandler != nullptr) + return _currentHandler->pathArgs(); + return 0; +} + template const String& ESP8266WebServerTemplate::arg(const String& name) const { for (int i = 0; i < _currentArgCount + _currentArgsHavePlain; ++i) { diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/libraries/ESP8266WebServer/src/ESP8266WebServer.h index 7f96449879..e47d890635 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -136,6 +136,7 @@ class ESP8266WebServerTemplate ServerType &getServer() { return _server; } const String& pathArg(unsigned int i) const; // get request path argument by number + const int pathArgs() const; // get path arguments count const String& arg(const String& name) const; // get request argument value by name const String& arg(int i) const; // get request argument value by number const String& argName(int i) const; // get request argument name by number diff --git a/libraries/ESP8266WebServer/src/Uri.h b/libraries/ESP8266WebServer/src/Uri.h index 95f5c89360..c146435421 100644 --- a/libraries/ESP8266WebServer/src/Uri.h +++ b/libraries/ESP8266WebServer/src/Uri.h @@ -19,7 +19,7 @@ class Uri { return new Uri(_uri); }; - virtual bool canHandle(const String &requestUri, __attribute__((unused)) std::vector &pathArgs) { + virtual bool canHandle(const String &requestUri, __attribute__((unused)) std::vector ¤tPathArgs) { return _uri == requestUri; } }; diff --git a/libraries/ESP8266WebServer/src/detail/RequestHandler.h b/libraries/ESP8266WebServer/src/detail/RequestHandler.h index 4195f0ff3f..17d528ef2b 100644 --- a/libraries/ESP8266WebServer/src/detail/RequestHandler.h +++ b/libraries/ESP8266WebServer/src/detail/RequestHandler.h @@ -24,12 +24,15 @@ class RequestHandler { RequestHandler* _next = nullptr; protected: - std::vector pathArgs; + std::vector currentPathArgs; public: const String& pathArg(unsigned int i) { - assert(i < pathArgs.size()); - return pathArgs[i]; + assert(i < currentPathArgs.size()); + return currentPathArgs[i]; + } + const int pathArgs() { + return currentPathArgs.size(); } }; diff --git a/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h b/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h index bb06033dea..57cd1e2b24 100644 --- a/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h +++ b/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h @@ -49,7 +49,7 @@ class FunctionRequestHandler : public RequestHandler { if (_method != HTTP_ANY && _method != requestMethod) return false; - return _uri->canHandle(requestUri, RequestHandler::pathArgs); + return _uri->canHandle(requestUri, RequestHandler::currentPathArgs); } bool canUpload(const String& requestUri) override { diff --git a/libraries/ESP8266WebServer/src/uri/UriBraces.h b/libraries/ESP8266WebServer/src/uri/UriBraces.h index 29652efc7f..8c241a06ce 100644 --- a/libraries/ESP8266WebServer/src/uri/UriBraces.h +++ b/libraries/ESP8266WebServer/src/uri/UriBraces.h @@ -13,11 +13,11 @@ class UriBraces : public Uri { return new UriBraces(_uri); }; - bool canHandle(const String &requestUri, std::vector &pathArgs) override final { - if (Uri::canHandle(requestUri, pathArgs)) + bool canHandle(const String &requestUri, std::vector ¤tPathArgs) override final { + if (Uri::canHandle(requestUri, currentPathArgs)) return true; - pathArgs.clear(); + currentPathArgs.clear(); size_t uriLength = _uri.length(); unsigned int requestUriIndex = 0; @@ -33,8 +33,8 @@ class UriBraces : public Uri { i += 2; // index of char after '}' if (i >= uriLength) { // there is no char after '}' - pathArgs.push_back(requestUri.substring(requestUriIndex)); - return pathArgs.back().indexOf("/") == -1; // path argument may not contain a '/' + currentPathArgs.push_back(requestUri.substring(requestUriIndex)); + return currentPathArgs.back().indexOf("/") == -1; // path argument may not contain a '/' } else { @@ -42,7 +42,7 @@ class UriBraces : public Uri { int uriIndex = requestUri.indexOf(charEnd, requestUriIndex); if (uriIndex < 0) return false; - pathArgs.push_back(requestUri.substring(requestUriIndex, uriIndex)); + currentPathArgs.push_back(requestUri.substring(requestUriIndex, uriIndex)); requestUriIndex = (unsigned int) uriIndex; } } diff --git a/libraries/ESP8266WebServer/src/uri/UriGlob.h b/libraries/ESP8266WebServer/src/uri/UriGlob.h index 1e222cbabd..4eb6197fbc 100644 --- a/libraries/ESP8266WebServer/src/uri/UriGlob.h +++ b/libraries/ESP8266WebServer/src/uri/UriGlob.h @@ -14,7 +14,7 @@ class UriGlob : public Uri { return new UriGlob(_uri); }; - bool canHandle(const String &requestUri, __attribute__((unused)) std::vector &pathArgs) override final { + bool canHandle(const String &requestUri, __attribute__((unused)) std::vector ¤tPathArgs) override final { return fnmatch(_uri.c_str(), requestUri.c_str(), 0) == 0; } }; diff --git a/libraries/ESP8266WebServer/src/uri/UriRegex.h b/libraries/ESP8266WebServer/src/uri/UriRegex.h index eef1b516d4..cfcde3c6a8 100644 --- a/libraries/ESP8266WebServer/src/uri/UriRegex.h +++ b/libraries/ESP8266WebServer/src/uri/UriRegex.h @@ -28,21 +28,21 @@ class UriRegex : public Uri { return new UriRegex(_uri); }; - bool canHandle(const String &requestUri, std::vector &pathArgs) override final { - if (Uri::canHandle(requestUri, pathArgs)) + bool canHandle(const String &requestUri, std::vector ¤tPathArgs) override final { + if (Uri::canHandle(requestUri, currentPathArgs)) return true; regmatch_t groupArray[REGEX_MAX_GROUPS]; if (regexec(&_regexCompiled, requestUri.c_str(), REGEX_MAX_GROUPS, groupArray, 0) == 0) { // matches - pathArgs.clear(); + currentPathArgs.clear(); unsigned int g = 1; for (; g < REGEX_MAX_GROUPS; g++) { if (groupArray[g].rm_so == (long int)-1) break; // No more groups - pathArgs.push_back(requestUri.substring(groupArray[g].rm_so, groupArray[g].rm_eo)); + currentPathArgs.push_back(requestUri.substring(groupArray[g].rm_so, groupArray[g].rm_eo)); } return true;