Skip to content

Commit

Permalink
Remove regex option and simplify hopeless logic
Browse files Browse the repository at this point in the history
I still dont know the purpose of that code because its so unreadable and undocumented wtf how can this possibly be the best library for doing http on embedded platforms, this is really fkn cursed
  • Loading branch information
hhvrc committed Jan 15, 2025
1 parent 6058e6d commit c4408f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 39 deletions.
8 changes: 1 addition & 7 deletions include/external/AsyncWebServer/ESPAsyncWebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@
#include <string>
#include <string_view>

#ifdef ASYNCWEBSERVER_REGEX
#define ASYNCWEBSERVER_REGEX_ATTRIBUTE
#else
#define ASYNCWEBSERVER_REGEX_ATTRIBUTE __attribute__((warning("ASYNCWEBSERVER_REGEX not defined")))
#endif

class AsyncWebServer;
class AsyncWebServerRequest;
class AsyncWebServerResponse;
Expand Down Expand Up @@ -277,7 +271,7 @@ class AsyncWebServerRequest {
const std::string& argName(size_t i) const; // get request argument name by number
bool hasArg(const char* name) const; // check if argument exists

const std::string& ASYNCWEBSERVER_REGEX_ATTRIBUTE pathArg(size_t i) const;
const std::string& pathArg(size_t i) const;

const std::string& header(const char* name) const; // get request header value by name
const std::string& header(size_t i) const; // get request header value by number
Expand Down
58 changes: 26 additions & 32 deletions include/external/AsyncWebServer/WebHandlerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
*/

#include <string>
#ifdef ASYNCWEBSERVER_REGEX
#include <regex>
#endif

#include "stddef.h"
#include <time.h>
Expand Down Expand Up @@ -93,35 +90,32 @@ class AsyncCallbackWebHandler : public AsyncWebHandler {

if (!(_method & request->method())) return false;

#ifdef ASYNCWEBSERVER_REGEX
if (_isRegex) {
std::regex pattern(_uri.c_str());
std::smatch matches;
std::string s(request->url().c_str());
if (std::regex_search(s, matches, pattern)) {
for (size_t i = 1; i < matches.size(); ++i) { // start from 1
request->_addPathParam(matches[i].str().c_str());
}
} else {
return false;
}
} else
#endif

using namespace std::string_view_literals;

if (OpenShock::StringStartsWith(_uri, "/*."sv)) {
std::string uriTemplate = _uri;
uriTemplate = uriTemplate.substring(uriTemplate.lastIndexOf("."));
if (!OpenShock::StringEndsWith(request->url(), uriTemplate)) return false;
} else if (OpenShock::StringEndsWith(_uri, '*')) {
std::string uriTemplate = _uri;
uriTemplate = uriTemplate.substring(0, uriTemplate.length() - 1);
if (!OpenShock::StringStartsWith(request->url(), uriTemplate)) return false;
} else if (_uri.length() && (_uri != request->url() && !OpenShock::StringStartsWith(request->url(), _uri + "/")))
return false;

return true;
using namespace std::string_view_literals;

std::string_view uri = _uri;
std::string_view requestUrl = request->url();

// Match 1 ??
if (OpenShock::StringStartsWith(uri, "/*."sv)) {
uri = uri.substr(uri.find_last_of('.'));

return OpenShock::StringEndsWith(requestUrl, uri);
}

// Match 2 ????
if (OpenShock::StringEndsWith(uri, '*')) {
uri.remove_suffix(1);

return OpenShock::StringStartsWith(requestUrl, uri);
}

// Huh ??????
if (uri.empty() || uri == requestUrl) {
return true;
}

// Way to check if requestUrl ends with uri + "/" without any allocations
return requestUrl.length() > uri.length() && requestUrl[uri.length()] == '/' && OpenShock::StringStartsWith(requestUrl, uri);
}

virtual void handleRequest(AsyncWebServerRequest* request) override final
Expand Down

0 comments on commit c4408f7

Please sign in to comment.