summaryrefslogtreecommitdiffstats
path: root/dnsdist-web.cc
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 06:28:35 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 06:28:35 +0000
commit037d21f508ef664d9592182d7b9b8d6989c28098 (patch)
treed6e5a84872adb93665f8a7e8831b70981c1c2351 /dnsdist-web.cc
parentAdding upstream version 1.9.4. (diff)
downloaddnsdist-037d21f508ef664d9592182d7b9b8d6989c28098.tar.xz
dnsdist-037d21f508ef664d9592182d7b9b8d6989c28098.zip
Adding upstream version 1.9.5.upstream/1.9.5
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dnsdist-web.cc')
-rw-r--r--dnsdist-web.cc38
1 files changed, 30 insertions, 8 deletions
diff --git a/dnsdist-web.cc b/dnsdist-web.cc
index 066b5c1..84ea079 100644
--- a/dnsdist-web.cc
+++ b/dnsdist-web.cc
@@ -1720,18 +1720,26 @@ static void handleRings(const YaHTTP::Request& req, YaHTTP::Response& resp)
resp.headers["Content-Type"] = "application/json";
}
-static std::unordered_map<std::string, std::function<void(const YaHTTP::Request&, YaHTTP::Response&)>> s_webHandlers;
+using WebHandler = std::function<void(const YaHTTP::Request&, YaHTTP::Response&)>;
+struct WebHandlerContext
+{
+ WebHandler d_handler;
+ bool d_isLua{false};
+};
-void registerWebHandler(const std::string& endpoint, std::function<void(const YaHTTP::Request&, YaHTTP::Response&)> handler);
+static SharedLockGuarded<std::unordered_map<std::string, WebHandlerContext>> s_webHandlers;
-void registerWebHandler(const std::string& endpoint, std::function<void(const YaHTTP::Request&, YaHTTP::Response&)> handler)
+void registerWebHandler(const std::string& endpoint, WebHandler handler, bool isLua = false);
+
+void registerWebHandler(const std::string& endpoint, WebHandler handler, bool isLua)
{
- s_webHandlers[endpoint] = std::move(handler);
+ auto handlers = s_webHandlers.write_lock();
+ (*handlers)[endpoint] = WebHandlerContext{std::move(handler), isLua};
}
void clearWebHandlers()
{
- s_webHandlers.clear();
+ s_webHandlers.write_lock()->clear();
}
#ifndef DISABLE_BUILTIN_HTML
@@ -1862,9 +1870,23 @@ static void connectionThread(WebClientConnection&& conn)
resp.status = 405;
}
else {
- const auto it = s_webHandlers.find(req.url.path);
- if (it != s_webHandlers.end()) {
- it->second(req, resp);
+ std::optional<WebHandlerContext> handlerCtx{std::nullopt};
+ {
+ auto handlers = s_webHandlers.read_lock();
+ const auto webHandlersIt = handlers->find(req.url.path);
+ if (webHandlersIt != handlers->end()) {
+ handlerCtx = webHandlersIt->second;
+ }
+ }
+
+ if (handlerCtx) {
+ if (handlerCtx->d_isLua) {
+ auto lua = g_lua.lock();
+ handlerCtx->d_handler(req, resp);
+ }
+ else {
+ handlerCtx->d_handler(req, resp);
+ }
}
else {
resp.status = 404;