mirror of
https://github.com/tbnobody/OpenDTU.git
synced 2025-12-10 16:59:52 +01:00
Use pre-defined literals in webapi
This commit is contained in:
@@ -60,7 +60,7 @@ bool WebApiClass::checkCredentials(AsyncWebServerRequest* request)
|
||||
|
||||
// WebAPI should set the X-Requested-With to prevent browser internal auth dialogs
|
||||
if (!request->hasHeader("X-Requested-With")) {
|
||||
r->addHeader("WWW-Authenticate", "Basic realm=\"Login Required\"");
|
||||
r->addHeader(asyncsrv::T_WWW_AUTH, "Basic realm=\"Login Required\"");
|
||||
}
|
||||
request->send(r);
|
||||
|
||||
@@ -79,8 +79,8 @@ bool WebApiClass::checkCredentialsReadonly(AsyncWebServerRequest* request)
|
||||
|
||||
void WebApiClass::sendTooManyRequests(AsyncWebServerRequest* request)
|
||||
{
|
||||
auto response = request->beginResponse(429, "text/plain", "Too Many Requests");
|
||||
response->addHeader("Retry-After", "60");
|
||||
auto response = request->beginResponse(429, asyncsrv::T_text_plain, "Too Many Requests");
|
||||
response->addHeader(asyncsrv::T_retry_after, "60");
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
/*
|
||||
* Copyright (C) 2022-2024 Thomas Basler and others
|
||||
* Copyright (C) 2022-2025 Thomas Basler and others
|
||||
*/
|
||||
#include "WebApi_file.h"
|
||||
#include "Configuration.h"
|
||||
@@ -186,9 +186,9 @@ void WebApiFileClass::onFileUploadFinish(AsyncWebServerRequest* request)
|
||||
// the request handler is triggered after the upload has finished...
|
||||
// create the response, add header, and send response
|
||||
|
||||
AsyncWebServerResponse* response = request->beginResponse(200, "text/plain", "OK");
|
||||
response->addHeader("Connection", "close");
|
||||
response->addHeader("Access-Control-Allow-Origin", "*");
|
||||
AsyncWebServerResponse* response = request->beginResponse(200, asyncsrv::T_text_plain, "OK");
|
||||
response->addHeader(asyncsrv::T_Connection, asyncsrv::T_close);
|
||||
response->addHeader(asyncsrv::T_CORS_ACAO, "*");
|
||||
request->send(response);
|
||||
RestartHelper.triggerRestart();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
/*
|
||||
* Copyright (C) 2022-2024 Thomas Basler and others
|
||||
* Copyright (C) 2022-2025 Thomas Basler and others
|
||||
*/
|
||||
#include "WebApi_firmware.h"
|
||||
#include "Configuration.h"
|
||||
@@ -33,9 +33,9 @@ void WebApiFirmwareClass::onFirmwareUpdateFinish(AsyncWebServerRequest* request)
|
||||
// the request handler is triggered after the upload has finished...
|
||||
// create the response, add header, and send response
|
||||
|
||||
AsyncWebServerResponse* response = request->beginResponse((Update.hasError()) ? 500 : 200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
|
||||
response->addHeader("Connection", "close");
|
||||
response->addHeader("Access-Control-Allow-Origin", "*");
|
||||
AsyncWebServerResponse* response = request->beginResponse((Update.hasError()) ? 500 : 200, asyncsrv::T_text_plain, (Update.hasError()) ? "FAIL" : "OK");
|
||||
response->addHeader(asyncsrv::T_Connection, asyncsrv::T_close);
|
||||
response->addHeader(asyncsrv::T_CORS_ACAO, "*");
|
||||
request->send(response);
|
||||
RestartHelper.triggerRestart();
|
||||
}
|
||||
@@ -49,30 +49,30 @@ void WebApiFirmwareClass::onFirmwareUpdateUpload(AsyncWebServerRequest* request,
|
||||
// Upload handler chunks in data
|
||||
if (!index) {
|
||||
if (!request->hasParam("MD5", true)) {
|
||||
return request->send(400, "text/plain", "MD5 parameter missing");
|
||||
return request->send(400, asyncsrv::T_text_plain, "MD5 parameter missing");
|
||||
}
|
||||
|
||||
if (!Update.setMD5(request->getParam("MD5", true)->value().c_str())) {
|
||||
return request->send(400, "text/plain", "MD5 parameter invalid");
|
||||
return request->send(400, asyncsrv::T_text_plain, "MD5 parameter invalid");
|
||||
}
|
||||
|
||||
if (!Update.begin(UPDATE_SIZE_UNKNOWN, U_FLASH)) { // Start with max available size
|
||||
Update.printError(Serial);
|
||||
return request->send(400, "text/plain", "OTA could not begin");
|
||||
return request->send(400, asyncsrv::T_text_plain, "OTA could not begin");
|
||||
}
|
||||
}
|
||||
|
||||
// Write chunked data to the free sketch space
|
||||
if (len) {
|
||||
if (Update.write(data, len) != len) {
|
||||
return request->send(400, "text/plain", "OTA could not begin");
|
||||
return request->send(400, asyncsrv::T_text_plain, "OTA could not begin");
|
||||
}
|
||||
}
|
||||
|
||||
if (final) { // if the final flag is set then this is the last frame of data
|
||||
if (!Update.end(true)) { // true to set the size to the current progress
|
||||
Update.printError(Serial);
|
||||
return request->send(400, "text/plain", "Could not end OTA");
|
||||
return request->send(400, asyncsrv::T_text_plain, "Could not end OTA");
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
/*
|
||||
* Copyright (C) 2024 Thomas Basler and others
|
||||
* Copyright (C) 2024-2025 Thomas Basler and others
|
||||
*/
|
||||
#include "WebApi_i18n.h"
|
||||
#include "I18n.h"
|
||||
@@ -49,8 +49,8 @@ void WebApiI18nClass::onI18nLanguage(AsyncWebServerRequest* request)
|
||||
expectedEtag += "\"";
|
||||
|
||||
bool eTagMatch = false;
|
||||
if (request->hasHeader("If-None-Match")) {
|
||||
const AsyncWebHeader* h = request->getHeader("If-None-Match");
|
||||
if (request->hasHeader(asyncsrv::T_INM)) {
|
||||
const AsyncWebHeader* h = request->getHeader(asyncsrv::T_INM);
|
||||
eTagMatch = h->value().equals(expectedEtag);
|
||||
}
|
||||
|
||||
@@ -63,8 +63,8 @@ void WebApiI18nClass::onI18nLanguage(AsyncWebServerRequest* request)
|
||||
}
|
||||
|
||||
// HTTP requires cache headers in 200 and 304 to be identical
|
||||
response->addHeader("Cache-Control", "public, must-revalidate");
|
||||
response->addHeader("ETag", expectedEtag);
|
||||
response->addHeader(asyncsrv::T_Cache_Control, "public, must-revalidate");
|
||||
response->addHeader(asyncsrv::T_ETag, expectedEtag);
|
||||
|
||||
request->send(response);
|
||||
return;
|
||||
|
||||
@@ -112,7 +112,7 @@ void WebApiPrometheusClass::onPrometheusMetricsGet(AsyncWebServerRequest* reques
|
||||
}
|
||||
}
|
||||
}
|
||||
stream->addHeader("Cache-Control", "no-cache");
|
||||
stream->addHeader(asyncsrv::T_Cache_Control, asyncsrv::T_no_cache);
|
||||
if (stream->available() > initialResponseBufferSize) {
|
||||
initialResponseBufferSize = stream->available();
|
||||
ESP_LOGI(TAG, "Increased /api/prometheus/metrics initialResponseBufferSize to %" PRIu32 " bytes", initialResponseBufferSize);
|
||||
|
||||
@@ -39,8 +39,8 @@ void WebApiWebappClass::responseBinaryDataWithETagCache(AsyncWebServerRequest* r
|
||||
expectedEtag += "\"";
|
||||
|
||||
bool eTagMatch = false;
|
||||
if (request->hasHeader("If-None-Match")) {
|
||||
const AsyncWebHeader* h = request->getHeader("If-None-Match");
|
||||
if (request->hasHeader(asyncsrv::T_INM)) {
|
||||
const AsyncWebHeader* h = request->getHeader(asyncsrv::T_INM);
|
||||
eTagMatch = h->value().equals(expectedEtag);
|
||||
}
|
||||
|
||||
@@ -51,13 +51,13 @@ void WebApiWebappClass::responseBinaryDataWithETagCache(AsyncWebServerRequest* r
|
||||
} else {
|
||||
response = request->beginResponse(200, contentType, content, len);
|
||||
if (contentEncoding.length() > 0) {
|
||||
response->addHeader("Content-Encoding", contentEncoding);
|
||||
response->addHeader(asyncsrv::T_Content_Encoding, contentEncoding);
|
||||
}
|
||||
}
|
||||
|
||||
// HTTP requires cache headers in 200 and 304 to be identical
|
||||
response->addHeader("Cache-Control", "public, must-revalidate");
|
||||
response->addHeader("ETag", expectedEtag);
|
||||
response->addHeader(asyncsrv::T_Cache_Control, "public, must-revalidate");
|
||||
response->addHeader(asyncsrv::T_ETag, expectedEtag);
|
||||
|
||||
request->send(response);
|
||||
}
|
||||
@@ -70,34 +70,34 @@ void WebApiWebappClass::init(AsyncWebServer& server, Scheduler& scheduler)
|
||||
*/
|
||||
|
||||
server.on("/", HTTP_GET, [&](AsyncWebServerRequest* request) {
|
||||
responseBinaryDataWithETagCache(request, "text/html", "gzip", file_index_html_start, file_index_html_end - file_index_html_start);
|
||||
responseBinaryDataWithETagCache(request, asyncsrv::T_text_html, asyncsrv::T_gzip, file_index_html_start, file_index_html_end - file_index_html_start);
|
||||
});
|
||||
|
||||
server.onNotFound([&](AsyncWebServerRequest* request) {
|
||||
responseBinaryDataWithETagCache(request, "text/html", "gzip", file_index_html_start, file_index_html_end - file_index_html_start);
|
||||
responseBinaryDataWithETagCache(request, asyncsrv::T_text_html, asyncsrv::T_gzip, file_index_html_start, file_index_html_end - file_index_html_start);
|
||||
});
|
||||
|
||||
server.on("/index.html", HTTP_GET, [&](AsyncWebServerRequest* request) {
|
||||
responseBinaryDataWithETagCache(request, "text/html", "gzip", file_index_html_start, file_index_html_end - file_index_html_start);
|
||||
responseBinaryDataWithETagCache(request, asyncsrv::T_text_html, asyncsrv::T_gzip, file_index_html_start, file_index_html_end - file_index_html_start);
|
||||
});
|
||||
|
||||
server.on("/favicon.ico", HTTP_GET, [&](AsyncWebServerRequest* request) {
|
||||
responseBinaryDataWithETagCache(request, "image/x-icon", "", file_favicon_ico_start, file_favicon_ico_end - file_favicon_ico_start);
|
||||
responseBinaryDataWithETagCache(request, asyncsrv::T_image_x_icon, "", file_favicon_ico_start, file_favicon_ico_end - file_favicon_ico_start);
|
||||
});
|
||||
|
||||
server.on("/favicon.png", HTTP_GET, [&](AsyncWebServerRequest* request) {
|
||||
responseBinaryDataWithETagCache(request, "image/png", "", file_favicon_png_start, file_favicon_png_end - file_favicon_png_start);
|
||||
responseBinaryDataWithETagCache(request, asyncsrv::T_image_png, "", file_favicon_png_start, file_favicon_png_end - file_favicon_png_start);
|
||||
});
|
||||
|
||||
server.on("/zones.json", HTTP_GET, [&](AsyncWebServerRequest* request) {
|
||||
responseBinaryDataWithETagCache(request, "application/json", "gzip", file_zones_json_start, file_zones_json_end - file_zones_json_start);
|
||||
responseBinaryDataWithETagCache(request, asyncsrv::T_application_json, asyncsrv::T_gzip, file_zones_json_start, file_zones_json_end - file_zones_json_start);
|
||||
});
|
||||
|
||||
server.on("/site.webmanifest", HTTP_GET, [&](AsyncWebServerRequest* request) {
|
||||
responseBinaryDataWithETagCache(request, "application/json", "", file_site_webmanifest_start, file_site_webmanifest_end - file_site_webmanifest_start);
|
||||
responseBinaryDataWithETagCache(request, asyncsrv::T_application_json, "", file_site_webmanifest_start, file_site_webmanifest_end - file_site_webmanifest_start);
|
||||
});
|
||||
|
||||
server.on("/js/app.js", HTTP_GET, [&](AsyncWebServerRequest* request) {
|
||||
responseBinaryDataWithETagCache(request, "text/javascript", "gzip", file_app_js_start, file_app_js_end - file_app_js_start);
|
||||
responseBinaryDataWithETagCache(request, asyncsrv::T_text_javascript, asyncsrv::T_gzip, file_app_js_start, file_app_js_end - file_app_js_start);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user