code cleanup

This commit is contained in:
Mathieu Carbou
2024-10-14 09:21:59 +02:00
parent 865f1e852a
commit 5b9469073b
4 changed files with 155 additions and 139 deletions

View File

@@ -195,16 +195,16 @@ void LoggingMiddleware::run(AsyncWebServerRequest* request, ArMiddlewareNext nex
}
void CorsMiddleware::addCORSHeaders(AsyncWebServerResponse* response) {
response->addHeader(F("Access-Control-Allow-Origin"), _origin.c_str());
response->addHeader(F("Access-Control-Allow-Methods"), _methods.c_str());
response->addHeader(F("Access-Control-Allow-Headers"), _headers.c_str());
response->addHeader(F("Access-Control-Allow-Credentials"), _credentials ? F("true") : F("false"));
response->addHeader(F("Access-Control-Max-Age"), String(_maxAge).c_str());
response->addHeader(asyncsrv::T_CORS_ACAO, _origin.c_str());
response->addHeader(asyncsrv::T_CORS_ACAM, _methods.c_str());
response->addHeader(asyncsrv::T_CORS_ACAH, _headers.c_str());
response->addHeader(asyncsrv::T_CORS_ACAC, _credentials ? asyncsrv::T_TRUE : asyncsrv::T_FALSE);
response->addHeader(asyncsrv::T_CORS_ACMA, String(_maxAge).c_str());
}
void CorsMiddleware::run(AsyncWebServerRequest* request, ArMiddlewareNext next) {
// Origin header ? => CORS handling
if (request->hasHeader(F("Origin"))) {
if (request->hasHeader(asyncsrv::T_CORS_O)) {
// check if this is a preflight request => handle it and return
if (request->method() == HTTP_OPTIONS) {
AsyncWebServerResponse* response = request->beginResponse(200);
@@ -250,7 +250,7 @@ void RateLimitMiddleware::run(AsyncWebServerRequest* request, ArMiddlewareNext n
next();
} else {
AsyncWebServerResponse* response = request->beginResponse(429);
response->addHeader(F("Retry-After"), retryAfterSeconds);
response->addHeader(asyncsrv::T_retry_after, retryAfterSeconds);
request->send(response);
}
}

View File

@@ -146,7 +146,7 @@ bool AsyncStaticWebHandler::_fileExists(AsyncWebServerRequest* request, const St
bool fileFound = false;
bool gzipFound = false;
String gzip = path + F(".gz");
String gzip = path + T__gz;
if (_gzipFirst) {
if (_fs.exists(gzip)) {

View File

@@ -280,38 +280,40 @@ bool AsyncWebServerRequest::_parseReqHeader() {
}
} else if (name.equalsIgnoreCase(T_Content_Length)) {
_contentLength = atoi(value.c_str());
} else if (name.equalsIgnoreCase(T_EXPECT) && value == T_100_CONTINUE) {
} else if (name.equalsIgnoreCase(T_EXPECT) && value.equalsIgnoreCase(T_100_CONTINUE)) {
_expectingContinue = true;
} else if (name.equalsIgnoreCase(T_AUTH)) {
if (value.length() > 5 && value.substring(0, 5).equalsIgnoreCase(T_BASIC)) {
_authorization = value.substring(6);
_authMethod = AsyncAuthType::AUTH_BASIC;
} else if (value.length() > 6 && value.substring(0, 6).equalsIgnoreCase(T_DIGEST)) {
_authMethod = AsyncAuthType::AUTH_DIGEST;
_authorization = value.substring(7);
} else if (value.length() > 6 && value.substring(0, 6).equalsIgnoreCase(T_BEARER)) {
_authMethod = AsyncAuthType::AUTH_BEARER;
_authorization = value.substring(7);
} else {
int space = value.indexOf(' ');
if (space == -1) {
_authorization = value;
_authMethod = AsyncAuthType::AUTH_OTHER;
}
} else {
if (name.equalsIgnoreCase(T_UPGRADE) && value.equalsIgnoreCase(T_WS)) {
// WebSocket request can be uniquely identified by header: [Upgrade: websocket]
_reqconntype = RCT_WS;
} else if (name.equalsIgnoreCase(T_ACCEPT)) {
String lowcase(value);
lowcase.toLowerCase();
#ifndef ESP8266
const char* substr = std::strstr(lowcase.c_str(), T_text_event_stream);
#else
const char* substr = std::strstr(lowcase.c_str(), String(T_text_event_stream).c_str());
#endif
if (substr != NULL) {
// WebEvent request can be uniquely identified by header: [Accept: text/event-stream]
_reqconntype = RCT_EVENT;
} else {
String method = value.substring(0, space);
if (method.equalsIgnoreCase(T_BASIC)) {
_authMethod = AsyncAuthType::AUTH_BASIC;
} else if (method.equalsIgnoreCase(T_DIGEST)) {
_authMethod = AsyncAuthType::AUTH_DIGEST;
} else if (method.equalsIgnoreCase(T_BEARER)) {
_authMethod = AsyncAuthType::AUTH_BEARER;
} else {
_authMethod = AsyncAuthType::AUTH_OTHER;
}
_authorization = value.substring(space + 1);
}
} else if (name.equalsIgnoreCase(T_UPGRADE) && value.equalsIgnoreCase(T_WS)) {
// WebSocket request can be uniquely identified by header: [Upgrade: websocket]
_reqconntype = RCT_WS;
} else if (name.equalsIgnoreCase(T_ACCEPT)) {
String lowcase(value);
lowcase.toLowerCase();
#ifndef ESP8266
const char* substr = std::strstr(lowcase.c_str(), T_text_event_stream);
#else
const char* substr = std::strstr(lowcase.c_str(), String(T_text_event_stream).c_str());
#endif
if (substr != NULL) {
// WebEvent request can be uniquely identified by header: [Accept: text/event-stream]
_reqconntype = RCT_EVENT;
}
}
_headers.emplace_back(name, value);
@@ -831,7 +833,7 @@ void AsyncWebServerRequest::requestAuthentication(AsyncAuthType method, const ch
break;
}
case AsyncAuthType::AUTH_DIGEST: {
constexpr size_t len = strlen(T_DIGEST_) + strlen(T_realm__) + strlen(T_auth_nonce) + 32 + strlen(T__opaque) + 32 + 1;
size_t len = strlen(T_DIGEST_) + strlen(T_realm__) + strlen(T_auth_nonce) + 32 + strlen(T__opaque) + 32 + 1;
String header;
header.reserve(len + strlen(realm));
header.concat(T_DIGEST_);

View File

@@ -5,46 +5,79 @@ namespace asyncsrv {
static constexpr const char* empty = "";
#ifndef ESP8622
static constexpr const char* T__opaque = "\", opaque=\"";
static constexpr const char* T_100_CONTINUE = "100-continue";
static constexpr const char* T_ACCEPT = "Accept";
static constexpr const char* T_Accept_Ranges = "Accept-Ranges";
static constexpr const char* T_13 = "13";
static constexpr const char* T_ACCEPT = "accept";
static constexpr const char* T_Accept_Ranges = "accept-ranges";
static constexpr const char* T_app_xform_urlencoded = "application/x-www-form-urlencoded";
static constexpr const char* T_AUTH = "Authorization";
static constexpr const char* T_BASIC = "Basic";
static constexpr const char* T_BASIC_REALM = "Basic realm=\"";
static constexpr const char* T_LOGIN_REQ = "Login Required";
static constexpr const char* T_AUTH = "authorization";
static constexpr const char* T_auth_nonce = "\", qop=\"auth\", nonce=\"";
static constexpr const char* T_BASIC = "basic";
static constexpr const char* T_BASIC_REALM = "basic realm=\"";
static constexpr const char* T_BEARER = "bearer";
static constexpr const char* T_BODY = "body";
static constexpr const char* T_Cache_Control = "Cache-Control";
static constexpr const char* T_Cache_Control = "cache-control";
static constexpr const char* T_chunked = "chunked";
static constexpr const char* T_close = "close";
static constexpr const char* T_Connection = "Connection";
static constexpr const char* T_Content_Disposition = "Content-Disposition";
static constexpr const char* T_Content_Encoding = "Content-Encoding";
static constexpr const char* T_Content_Length = "Content-Length";
static constexpr const char* T_Content_Type = "Content-Type";
static constexpr const char* T_Cookie = "Cookie";
static constexpr const char* T_DIGEST = "Digest";
static constexpr const char* T_DIGEST_ = "Digest ";
static constexpr const char* T_BEARER = "Bearer";
static constexpr const char* T_ETag = "ETag";
static constexpr const char* T_EXPECT = "Expect";
static constexpr const char* T_cnonce = "cnonce";
static constexpr const char* T_Connection = "connection";
static constexpr const char* T_Content_Disposition = "content-disposition";
static constexpr const char* T_Content_Encoding = "content-encoding";
static constexpr const char* T_Content_Length = "content-length";
static constexpr const char* T_Content_Type = "content-type";
static constexpr const char* T_Cookie = "cookie";
static constexpr const char* T_CORS_ACAC = "access-control-allow-credentials";
static constexpr const char* T_CORS_ACAH = "access-control-allow-headers";
static constexpr const char* T_CORS_ACAM = "access-control-allow-methods";
static constexpr const char* T_CORS_ACAO = "access-control-allow-origin";
static constexpr const char* T_CORS_ACMA = "access-control-max-age";
static constexpr const char* T_CORS_O = "origin";
static constexpr const char* T_data_ = "data: ";
static constexpr const char* T_DIGEST = "digest";
static constexpr const char* T_DIGEST_ = "digest ";
static constexpr const char* T_ETag = "etag";
static constexpr const char* T_event_ = "event: ";
static constexpr const char* T_EXPECT = "expect";
static constexpr const char* T_FALSE = "false";
static constexpr const char* T_filename = "filename";
static constexpr const char* T_gzip = "gzip";
static constexpr const char* T_Host = "host";
static constexpr const char* T_HTTP_1_0 = "HTTP/1.0";
static constexpr const char* T_HTTP_100_CONT = "HTTP/1.1 100 Continue\r\n\r\n";
static constexpr const char* T_IMS = "If-Modified-Since";
static constexpr const char* T_INM = "If-None-Match";
static constexpr const char* T_id__ = "id: ";
static constexpr const char* T_IMS = "if-modified-since";
static constexpr const char* T_INM = "if-none-match";
static constexpr const char* T_keep_alive = "keep-alive";
static constexpr const char* T_Last_Event_ID = "Last-Event-ID";
static constexpr const char* T_Last_Modified = "Last-Modified";
static constexpr const char* T_LOCATION = "Location";
static constexpr const char* T_Last_Event_ID = "last-event-id";
static constexpr const char* T_Last_Modified = "last-modified";
static constexpr const char* T_LOCATION = "location";
static constexpr const char* T_LOGIN_REQ = "Login Required";
static constexpr const char* T_MULTIPART_ = "multipart/";
static constexpr const char* T_name = "name";
static constexpr const char* T_nc = "nc";
static constexpr const char* T_no_cache = "no-cache";
static constexpr const char* T_nonce = "nonce";
static constexpr const char* T_none = "none";
static constexpr const char* T_UPGRADE = "Upgrade";
static constexpr const char* T_opaque = "opaque";
static constexpr const char* T_qop = "qop";
static constexpr const char* T_realm = "realm";
static constexpr const char* T_realm__ = "realm=\"";
static constexpr const char* T_response = "response";
static constexpr const char* T_retry_ = "retry: ";
static constexpr const char* T_retry_after = "retry-after";
static constexpr const char* T_rn = "\r\n";
static constexpr const char* T_rnrn = "\r\n\r\n";
static constexpr const char* T_Transfer_Encoding = "transfer-encoding";
static constexpr const char* T_TRUE = "true";
static constexpr const char* T_UPGRADE = "upgrade";
static constexpr const char* T_uri = "uri";
static constexpr const char* T_username = "username";
static constexpr const char* T_WS = "websocket";
static constexpr const char* T_WWW_AUTH = "WWW-Authenticate";
static constexpr const char* T_Transfer_Encoding = "Transfer-Encoding";
static constexpr const char* T_WWW_AUTH = "www-authenticate";
// HTTP Methods
static constexpr const char* T_ANY = "ANY";
static constexpr const char* T_GET = "GET";
static constexpr const char* T_POST = "POST";
@@ -147,72 +180,78 @@ static constexpr const char* T_HTTP_CODE_504 = "Gateway Time-out";
static constexpr const char* T_HTTP_CODE_505 = "HTTP Version not supported";
static constexpr const char* T_HTTP_CODE_ANY = "Unknown code";
// other
static constexpr const char* T__opaque = "\", opaque=\"";
static constexpr const char* T_13 = "13";
static constexpr const char* T_auth_nonce = "\", qop=\"auth\", nonce=\"";
static constexpr const char* T_cnonce = "cnonce";
static constexpr const char* T_data_ = "data: ";
static constexpr const char* T_event_ = "event: ";
static constexpr const char* T_filename = "filename";
static constexpr const char* T_gzip = "gzip";
static constexpr const char* T_Host = "Host";
static constexpr const char* T_id__ = "id: ";
static constexpr const char* T_name = "name";
static constexpr const char* T_nc = "nc";
static constexpr const char* T_nonce = "nonce";
static constexpr const char* T_opaque = "opaque";
static constexpr const char* T_qop = "qop";
static constexpr const char* T_realm = "realm";
static constexpr const char* T_realm__ = "realm=\"";
static constexpr const char* T_response = "response";
static constexpr const char* T_retry_ = "retry: ";
static constexpr const char* T_rn = "\r\n";
static constexpr const char* T_rnrn = "\r\n\r\n";
static constexpr const char* T_uri = "uri";
static constexpr const char* T_username = "username";
#else // ESP8622
static const char T__opaque[] PROGMEM = "\", opaque=\"";
static const char T_100_CONTINUE[] PROGMEM = "100-continue";
static const char T_ACCEPT[] PROGMEM = "Accept";
static const char T_Accept_Ranges[] PROGMEM = "Accept-Ranges";
static const char T_13[] PROGMEM = "13";
static const char T_Accept_Ranges[] PROGMEM = "accept-ranges";
static const char T_ACCEPT[] PROGMEM = "accept";
static const char T_app_xform_urlencoded[] PROGMEM = "application/x-www-form-urlencoded";
static const char T_AUTH[] PROGMEM = "Authorization";
static const char T_BASIC[] PROGMEM = "Basic";
static const char T_BASIC_REALM[] PROGMEM = "Basic realm=\"";
static const char T_LOGIN_REQ[] PROGMEM = "Login Required";
static const char T_auth_nonce[] PROGMEM = "\", qop=\"auth\", nonce=\"";
static const char T_AUTH[] PROGMEM = "authorization";
static const char T_BASIC_REALM[] PROGMEM = "basic realm=\"";
static const char T_BASIC[] PROGMEM = "basic";
static const char T_BEARER[] PROGMEM = "bearer";
static const char T_BODY[] PROGMEM = "body";
static const char T_Cache_Control[] PROGMEM = "Cache-Control";
static const char T_Cache_Control[] PROGMEM = "cache-control";
static const char T_chunked[] PROGMEM = "chunked";
static const char T_close[] PROGMEM = "close";
static const char T_Connection[] PROGMEM = "Connection";
static const char T_Content_Disposition[] PROGMEM = "Content-Disposition";
static const char T_Content_Encoding[] PROGMEM = "Content-Encoding";
static const char T_Content_Length[] PROGMEM = "Content-Length";
static const char T_Content_Type[] PROGMEM = "Content-Type";
static const char T_Cookie[] PROGMEM = "Cookie";
static const char T_DIGEST[] PROGMEM = "Digest";
static const char T_DIGEST_[] PROGMEM = "Digest ";
static const char T_BEARER[] PROGMEM = "Bearer";
static const char T_ETag[] PROGMEM = "ETag";
static const char T_EXPECT[] PROGMEM = "Expect";
static const char T_cnonce[] PROGMEM = "cnonce";
static const char T_Connection[] PROGMEM = "connection";
static const char T_Content_Disposition[] PROGMEM = "content-disposition";
static const char T_Content_Encoding[] PROGMEM = "content-encoding";
static const char T_Content_Length[] PROGMEM = "content-length";
static const char T_Content_Type[] PROGMEM = "content-type";
static const char T_Cookie[] PROGMEM = "cookie";
static const char T_CORS_ACAC[] PROGMEM = "access-control-allow-credentials";
static const char T_CORS_ACAH[] PROGMEM = "access-control-allow-headers";
static const char T_CORS_ACAM[] PROGMEM = "access-control-allow-methods";
static const char T_CORS_ACAO[] PROGMEM = "access-control-allow-origin";
static const char T_CORS_ACMA[] PROGMEM = "access-control-max-age";
static const char T_CORS_O[] PROGMEM = "origin";
static const char T_data_[] PROGMEM = "data: ";
static const char T_DIGEST_[] PROGMEM = "digest ";
static const char T_DIGEST[] PROGMEM = "digest";
static const char T_ETag[] PROGMEM = "etag";
static const char T_event_[] PROGMEM = "event: ";
static const char T_EXPECT[] PROGMEM = "expect";
static const char T_FALSE[] PROGMEM = "false";
static const char T_filename[] PROGMEM = "filename";
static const char T_gzip[] PROGMEM = "gzip";
static const char T_Host[] PROGMEM = "host";
static const char T_HTTP_1_0[] PROGMEM = "HTTP/1.0";
static const char T_HTTP_100_CONT[] PROGMEM = "HTTP/1.1 100 Continue\r\n\r\n";
static const char T_IMS[] PROGMEM = "If-Modified-Since";
static const char T_INM[] PROGMEM = "If-None-Match";
static const char T_id__[] PROGMEM = "id: ";
static const char T_IMS[] PROGMEM = "if-modified-since";
static const char T_INM[] PROGMEM = "if-none-match";
static const char T_keep_alive[] PROGMEM = "keep-alive";
static const char T_Last_Event_ID[] PROGMEM = "Last-Event-ID";
static const char T_Last_Modified[] PROGMEM = "Last-Modified";
static const char T_LOCATION[] PROGMEM = "Location";
static const char T_Last_Event_ID[] PROGMEM = "last-event-id";
static const char T_Last_Modified[] PROGMEM = "last-modified";
static const char T_LOCATION[] PROGMEM = "location";
static const char T_LOGIN_REQ[] PROGMEM = "Login Required";
static const char T_MULTIPART_[] PROGMEM = "multipart/";
static const char T_name[] PROGMEM = "name";
static const char T_nc[] PROGMEM = "nc";
static const char T_no_cache[] PROGMEM = "no-cache";
static const char T_nonce[] PROGMEM = "nonce";
static const char T_none[] PROGMEM = "none";
static const char T_UPGRADE[] PROGMEM = "Upgrade";
static const char T_opaque[] PROGMEM = "opaque";
static const char T_qop[] PROGMEM = "qop";
static const char T_realm__[] PROGMEM = "realm=\"";
static const char T_realm[] PROGMEM = "realm";
static const char T_response[] PROGMEM = "response";
static const char T_retry_[] PROGMEM = "retry: ";
static const char T_retry_after[] PROGMEM = "retry-after";
static const char T_rn[] PROGMEM = "\r\n";
static const char T_rnrn[] PROGMEM = "\r\n\r\n";
static const char T_Transfer_Encoding[] PROGMEM = "transfer-encoding";
static const char T_TRUE[] PROGMEM = "true";
static const char T_UPGRADE[] PROGMEM = "upgrade";
static const char T_uri[] PROGMEM = "uri";
static const char T_username[] PROGMEM = "username";
static const char T_WS[] PROGMEM = "websocket";
static const char T_WWW_AUTH[] PROGMEM = "WWW-Authenticate";
static const char T_Transfer_Encoding[] PROGMEM = "Transfer-Encoding";
static const char T_WWW_AUTH[] PROGMEM = "www-authenticate";
// HTTP Methods
static const char T_ANY[] PROGMEM = "ANY";
@@ -317,31 +356,6 @@ static const char T_HTTP_CODE_504[] PROGMEM = "Gateway Time-out";
static const char T_HTTP_CODE_505[] PROGMEM = "HTTP Version not supported";
static const char T_HTTP_CODE_ANY[] PROGMEM = "Unknown code";
// other
static const char T__opaque[] PROGMEM = "\", opaque=\"";
static const char T_13[] PROGMEM = "13";
static const char T_auth_nonce[] PROGMEM = "\", qop=\"auth\", nonce=\"";
static const char T_cnonce[] PROGMEM = "cnonce";
static const char T_data_[] PROGMEM = "data: ";
static const char T_event_[] PROGMEM = "event: ";
static const char T_filename[] PROGMEM = "filename";
static const char T_gzip[] PROGMEM = "gzip";
static const char T_Host[] PROGMEM = "Host";
static const char T_id__[] PROGMEM = "id: ";
static const char T_name[] PROGMEM = "name";
static const char T_nc[] PROGMEM = "nc";
static const char T_nonce[] PROGMEM = "nonce";
static const char T_opaque[] PROGMEM = "opaque";
static const char T_qop[] PROGMEM = "qop";
static const char T_realm[] PROGMEM = "realm";
static const char T_realm__[] PROGMEM = "realm=\"";
static const char T_response[] PROGMEM = "response";
static const char T_retry_[] PROGMEM = "retry: ";
static const char T_rn[] PROGMEM = "\r\n";
static const char T_rnrn[] PROGMEM = "\r\n\r\n";
static const char T_uri[] PROGMEM = "uri";
static const char T_username[] PROGMEM = "username";
#endif // ESP8622
} // namespace asyncsrv {}