Coverage Report

Created: 2026-10-16 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pdns/pdns/dnsdistdist/dnsdist-lua-web.cc
Line
Count
Source
1
/*
2
 * This file is part of PowerDNS or dnsdist.
3
 * Copyright -- PowerDNS.COM B.V. and its contributors
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of version 2 of the GNU General Public License as
7
 * published by the Free Software Foundation.
8
 *
9
 * In addition, for the avoidance of any doubt, permission is granted to
10
 * link this program with OpenSSL and to (re)distribute the binaries
11
 * produced as the result of such linking.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
 */
22
#include <yahttp/yahttp.hpp>
23
24
#include "dnsdist.hh"
25
#include "dnsdist-lua.hh"
26
#include "dnsdist-web.hh"
27
28
namespace dnsdist::webserver
29
{
30
void registerWebHandler(const std::string& endpoint, std::function<void(const YaHTTP::Request&, YaHTTP::Response&)> handler);
31
}
32
33
void setupLuaWeb([[maybe_unused]] LuaContext& luaCtx)
34
0
{
35
0
#ifndef DISABLE_LUA_WEB_HANDLERS
36
0
  luaCtx.writeFunction("registerWebHandler", [](const std::string& path, std::function<void(const YaHTTP::Request*, YaHTTP::Response*)> handler) {
37
    /* LuaWrapper does a copy for objects passed by reference, so we pass a pointer */
38
0
    dnsdist::webserver::registerWebHandler(path, [handler](const YaHTTP::Request& req, YaHTTP::Response& resp) { handler(&req, &resp); });
39
0
  });
40
41
0
  luaCtx.registerMember<std::string(YaHTTP::Request::*)>("path", [](const YaHTTP::Request& req) -> std::string { return req.url.path; }, [](YaHTTP::Request& req, const std::string& path) { (void)req; (void) path; });
42
0
  luaCtx.registerMember<int(YaHTTP::Request::*)>("version", [](const YaHTTP::Request& req) -> int { return req.version; }, [](YaHTTP::Request& req, int version) { (void)req; (void)version; });
43
0
  luaCtx.registerMember<std::string(YaHTTP::Request::*)>("method", [](const YaHTTP::Request& req) -> std::string { return req.method; }, [](YaHTTP::Request& req, const std::string& method) { (void)req; (void) method; });
44
0
  luaCtx.registerMember<std::string(YaHTTP::Request::*)>("body", [](const YaHTTP::Request& req) -> std::string { return req.body; }, [](YaHTTP::Request& req, const std::string& body) { (void)req; (void)body; });
45
0
  luaCtx.registerMember<LuaAssociativeTable<std::string>(YaHTTP::Request::*)>("getvars", [](const YaHTTP::Request& req) {
46
0
    LuaAssociativeTable<std::string> values;
47
0
    for (const auto& entry : req.getvars) {
48
0
      values.insert({entry.first, entry.second});
49
0
    }
50
0
    return values; }, [](YaHTTP::Request& req, const LuaAssociativeTable<std::string>& values) { (void)req; (void)values; });
51
0
  luaCtx.registerMember<LuaAssociativeTable<std::string>(YaHTTP::Request::*)>("postvars", [](const YaHTTP::Request& req) {
52
0
    LuaAssociativeTable<std::string> values;
53
0
    for (const auto& entry : req.postvars) {
54
0
      values.insert({entry.first, entry.second});
55
0
    }
56
0
    return values; }, [](YaHTTP::Request& req, const LuaAssociativeTable<std::string>& values) { (void)req; (void)values; });
57
0
  luaCtx.registerMember<LuaAssociativeTable<std::string>(YaHTTP::Request::*)>("headers", [](const YaHTTP::Request& req) {
58
0
    LuaAssociativeTable<std::string> values;
59
0
    for (const auto& entry : req.headers) {
60
0
      values.insert({entry.first, entry.second});
61
0
    }
62
0
    return values; }, [](YaHTTP::Request& req, const LuaAssociativeTable<std::string>& values) { (void)req; (void)values; });
63
64
  /* Response */
65
0
  luaCtx.registerMember<std::string(YaHTTP::Response::*)>("body", [](const YaHTTP::Response& resp) -> const std::string { return resp.body; }, [](YaHTTP::Response& resp, const std::string& body) { resp.body = body; });
66
0
  luaCtx.registerMember<int(YaHTTP::Response::*)>("status", [](const YaHTTP::Response& resp) -> int { return resp.status; }, [](YaHTTP::Response& resp, int status) { resp.status = status; });
67
0
  luaCtx.registerMember<LuaAssociativeTable<std::string>(YaHTTP::Response::*)>("headers", [](const YaHTTP::Response& resp) {
68
0
    LuaAssociativeTable<std::string> values;
69
0
    for (const auto& entry : resp.headers) {
70
0
      values.insert({entry.first, entry.second});
71
0
    }
72
0
    return values; }, [](YaHTTP::Response& resp, const LuaAssociativeTable<std::string>& values) {
73
0
    resp.headers.clear();
74
0
    for (const auto& entry : values) {
75
0
      resp.headers.insert({entry.first, entry.second});
76
0
    } });
77
0
#endif /* DISABLE_LUA_WEB_HANDLERS */
78
0
}