diff options
Diffstat (limited to 'dnsdist-web.cc')
-rw-r--r-- | dnsdist-web.cc | 38 |
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; |