Skip to content
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

Reduce copy of Strings in WebServer RequestHandler #10345

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions libraries/WebServer/examples/WebServer/WebServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,16 @@ public:
// @param requestMethod method of the http request line.
// @param requestUri request resource from the http request line.
// @return true when method can be handled.
bool canHandle(WebServer &server, HTTPMethod requestMethod, String uri) override {
bool canHandle(WebServer &server, HTTPMethod requestMethod, const String& uri) override {
return ((requestMethod == HTTP_POST) || (requestMethod == HTTP_DELETE));
} // canHandle()

bool canUpload(WebServer &server, String uri) override {
bool canUpload(WebServer &server, const String& uri) override {
// only allow upload on root fs level.
return (uri == "/");
} // canUpload()

bool handle(WebServer &server, HTTPMethod requestMethod, String requestUri) override {
bool handle(WebServer &server, HTTPMethod requestMethod, const String& requestUri) override {
// ensure that filename starts with '/'
String fName = requestUri;
if (!fName.startsWith("/")) {
Expand All @@ -177,7 +177,7 @@ public:
} // handle()

// uploading process
void upload(WebServer UNUSED &server, String requestUri, HTTPUpload &upload) override {
void upload(WebServer UNUSED &server, const String& requestUri, HTTPUpload &upload) override {
// ensure that filename starts with '/'
static size_t uploadSize;

Expand Down
18 changes: 9 additions & 9 deletions libraries/WebServer/src/detail/RequestHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ class RequestHandler {
note: old handler API for backward compatibility
*/

virtual bool canHandle(HTTPMethod method, String uri) {
virtual bool canHandle(HTTPMethod method, const String& uri) {
(void)method;
(void)uri;
return false;
}
virtual bool canUpload(String uri) {
virtual bool canUpload(const String& uri) {
(void)uri;
return false;
}
virtual bool canRaw(String uri) {
virtual bool canRaw(const String& uri) {
(void)uri;
return false;
}
Expand All @@ -30,34 +30,34 @@ class RequestHandler {
note: new handler API with support for filters etc.
*/

virtual bool canHandle(WebServer &server, HTTPMethod method, String uri) {
virtual bool canHandle(WebServer &server, HTTPMethod method, const String& uri) {
(void)server;
(void)method;
(void)uri;
return false;
}
virtual bool canUpload(WebServer &server, String uri) {
virtual bool canUpload(WebServer &server, const String& uri) {
(void)server;
(void)uri;
return false;
}
virtual bool canRaw(WebServer &server, String uri) {
virtual bool canRaw(WebServer &server, const String& uri) {
(void)server;
(void)uri;
return false;
}
virtual bool handle(WebServer &server, HTTPMethod requestMethod, String requestUri) {
virtual bool handle(WebServer &server, HTTPMethod requestMethod, const String& requestUri) {
(void)server;
(void)requestMethod;
(void)requestUri;
return false;
}
virtual void upload(WebServer &server, String requestUri, HTTPUpload &upload) {
virtual void upload(WebServer &server, const String& requestUri, HTTPUpload &upload) {
(void)server;
(void)requestUri;
(void)upload;
}
virtual void raw(WebServer &server, String requestUri, HTTPRaw &raw) {
virtual void raw(WebServer &server, const String& requestUri, HTTPRaw &raw) {
(void)server;
(void)requestUri;
(void)raw;
Expand Down
29 changes: 15 additions & 14 deletions libraries/WebServer/src/detail/RequestHandlersImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,55 @@ class FunctionRequestHandler : public RequestHandler {
delete _uri;
}

bool canHandle(HTTPMethod requestMethod, String requestUri) override {
bool canHandle(HTTPMethod requestMethod, const String& requestUri) override {
if (_method != HTTP_ANY && _method != requestMethod) {
return false;
}

return _uri->canHandle(requestUri, pathArgs);
}

bool canUpload(String requestUri) override {
bool canUpload(const String& requestUri) override {
if (!_ufn || !canHandle(HTTP_POST, requestUri)) {
return false;
}

return true;
}

bool canRaw(String requestUri) override {
bool canRaw(const String& requestUri) override {
if (!_ufn || _method == HTTP_GET) {
return false;
}

return true;
}

bool canHandle(WebServer &server, HTTPMethod requestMethod, String requestUri) override {
bool canHandle(WebServer &server, HTTPMethod requestMethod, const String& requestUri) override {
if (_method != HTTP_ANY && _method != requestMethod) {
return false;
}

return _uri->canHandle(requestUri, pathArgs) && (_filter != NULL ? _filter(server) : true);
}

bool canUpload(WebServer &server, String requestUri) override {
bool canUpload(WebServer &server, const String& requestUri) override {
if (!_ufn || !canHandle(server, HTTP_POST, requestUri)) {
return false;
}

return true;
}

bool canRaw(WebServer &server, String requestUri) override {
bool canRaw(WebServer &server, const String& requestUri) override {
if (!_ufn || _method == HTTP_GET || (_filter != NULL ? _filter(server) == false : false)) {
return false;
}

return true;
}

bool handle(WebServer &server, HTTPMethod requestMethod, String requestUri) override {
bool handle(WebServer &server, HTTPMethod requestMethod, const String& requestUri) override {
if (!canHandle(server, requestMethod, requestUri)) {
return false;
}
Expand All @@ -78,14 +78,14 @@ class FunctionRequestHandler : public RequestHandler {
return true;
}

void upload(WebServer &server, String requestUri, HTTPUpload &upload) override {
void upload(WebServer &server, const String& requestUri, HTTPUpload &upload) override {
(void)upload;
if (canUpload(server, requestUri)) {
_ufn();
}
}

void raw(WebServer &server, String requestUri, HTTPRaw &raw) override {
void raw(WebServer &server, const String& requestUri, HTTPRaw &raw) override {
(void)raw;
if (canRaw(server, requestUri)) {
_ufn();
Expand Down Expand Up @@ -118,7 +118,7 @@ class StaticRequestHandler : public RequestHandler {
_baseUriLength = _uri.length();
}

bool canHandle(HTTPMethod requestMethod, String requestUri) override {
bool canHandle(HTTPMethod requestMethod, const String& requestUri) override {
if (requestMethod != HTTP_GET) {
return false;
}
Expand All @@ -130,7 +130,7 @@ class StaticRequestHandler : public RequestHandler {
return true;
}

bool canHandle(WebServer &server, HTTPMethod requestMethod, String requestUri) override {
bool canHandle(WebServer &server, HTTPMethod requestMethod, const String& requestUri) override {
if (requestMethod != HTTP_GET) {
return false;
}
Expand All @@ -146,21 +146,20 @@ class StaticRequestHandler : public RequestHandler {
return true;
}

bool handle(WebServer &server, HTTPMethod requestMethod, String requestUri) override {
bool handle(WebServer &server, HTTPMethod requestMethod, const String& requestUri) override {
if (!canHandle(server, requestMethod, requestUri)) {
return false;
}

log_v("StaticRequestHandler::handle: request=%s _uri=%s\r\n", requestUri.c_str(), _uri.c_str());

String path(_path);
String eTagCode;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moving it changes nothing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Less allocated on the stack when it returns early (e.g. due to the added single recursive call you asked about in the previous comment.


if (!_isFile) {
// Base URI doesn't point to a file.
// If a directory is requested, look for index file.
if (requestUri.endsWith("/")) {
requestUri += "index.htm";
return handle(server, requestMethod, String(requestUri + "index.htm"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we can keep the requestUri const

}

// Append whatever follows this URI in request to get the file path.
Expand All @@ -184,6 +183,8 @@ class StaticRequestHandler : public RequestHandler {
return false;
}

String eTagCode;

if (server._eTagEnabled) {
if (server._eTagFunction) {
eTagCode = (server._eTagFunction)(_fs, path);
Expand Down
Loading