Skip to content

Commit

Permalink
fix issue with multiple HTTP commands per controller
Browse files Browse the repository at this point in the history
- static storage can't be used - object is created only once!

- add API ping to release, but make it authenticated
  • Loading branch information
astibal committed May 8, 2024
1 parent af3d3d8 commit b38914f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/ext/lmhpp
Submodule lmhpp updated 1 files
+9 −13 include/lmhttpd.hpp
89 changes: 45 additions & 44 deletions src/service/httpd/handlers/dispatchers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@

namespace sx::webserver::dispatchers {

void controller_add_debug_only(lmh::WebServer &server) {
#ifndef BUILD_RELEASE
static Http_Responder status_ping(
void controller_add_status(lmh::WebServer &server) {
auto* status_ping = new Http_Responder(
"POST",
"/api/status/ping",
#ifndef BUILD_RELEASE
authorized::unprotected<json>(
#else
authorized::token_protected<json>(
#endif
[]([[maybe_unused]] MHD_Connection *c, [[maybe_unused]] std::string const &meth, [[maybe_unused]] std::string const &req) -> json {
time_t uptime = time(nullptr) - SmithProxy::instance().ts_sys_started;
return {{"version", SMITH_VERSION},
Expand All @@ -29,13 +32,13 @@ namespace sx::webserver::dispatchers {
{"uptime_str", uptime_string(uptime)}};
}));

status_ping.Content_Type = "application/json";
server.addController(&status_ping);
#endif
status_ping->Content_Type = "application/json";
server.addController(std::shared_ptr<lmh::Controller>(status_ping));

}

void controller_add_commons(lmh::WebServer& server) {
static Http_Responder cacert(
auto* cacert = new Http_Responder(
"GET",
"/cacert",
authorized::unprotected<std::string>(
Expand All @@ -50,121 +53,119 @@ namespace sx::webserver::dispatchers {
return pem_ca_cert;
}));

cacert.Content_Type = "application/x-pem-file";
server.addController(&cacert);
cacert->Content_Type = "application/x-pem-file";
server.addController(std::shared_ptr<lmh::Controller>(cacert));
}


void controller_add_diag(lmh::WebServer &server) {

for(auto const& meth: { "GET", "POST"}) {
static Http_Responder handler(meth, "/api/diag/ssl/cache/stats",
auto* handler = new Http_Responder(meth, "/api/diag/ssl/cache/stats",
authorized::token_protected<json>(json_ssl_cache_stats));
handler.Content_Type = "application/json";
server.addController(&handler);
handler->Content_Type = "application/json";
server.addController(std::shared_ptr<lmh::Controller>(handler));
}

for(auto const& meth: {"GET", "POST"}) {
static Http_Responder handler(
auto* handler = new Http_Responder(
meth,
"/api/diag/ssl/cache/print",
authorized::token_protected<json>(json_ssl_cache_print)
);
handler.Content_Type = "application/json";
server.addController(&handler);
handler->Content_Type = "application/json";
server.addController(std::shared_ptr<lmh::Controller>(handler));
}


for(auto const& meth: {"GET", "POST"}) {
static Http_Responder handler(
auto* handler = new Http_Responder(
meth,
"/api/diag/proxy/session/list",
authorized::token_protected<json>(json_proxy_session_list)
);
handler.Content_Type = "application/json";
server.addController(&handler);
handler->Content_Type = "application/json";
server.addController(std::shared_ptr<lmh::Controller>(handler));
}

for(auto const& meth: {"GET", "POST"}) {
static Http_Responder handler(
auto* handler = new Http_Responder(
meth,
"/api/diag/proxy/neighbor/list",
authorized::token_protected<json>(json_proxy_neighbor_list)
);
handler.Content_Type = "application/json";
server.addController(&handler);
handler->Content_Type = "application/json";
server.addController(std::shared_ptr<lmh::Controller>(handler));
}

for(auto const& meth: {"POST"}) {
static Http_Responder handler(
auto* handler = new Http_Responder(
meth,
"/api/diag/proxy/neighbor/update",
authorized::token_protected<json>(json_proxy_neighbor_update)
);
handler.Content_Type = "application/json";
server.addController(&handler);
handler->Content_Type = "application/json";
server.addController(std::shared_ptr<lmh::Controller>(handler));
}


for(auto const& meth: {"GET", "POST"}) {
static Http_Responder handler(
auto* handler = new Http_Responder(
meth,
"/api/do/ssl/custom/reload",
authorized::token_protected<json>(json_do_ssl_custom_reload)
);
handler.Content_Type = "application/json";
server.addController(&handler);
handler->Content_Type = "application/json";
server.addController(std::shared_ptr<lmh::Controller>(handler));
}


}

void controller_add_uni(lmh::WebServer &server) {
static Http_Responder cfg_uni_add(
auto* cfg_uni_add = new Http_Responder(
"POST",
"/api/config/uni/add",
authorized::token_protected<json>(&json_add_section_entry)
);
cfg_uni_add.Content_Type = "application/json";
server.addController(&cfg_uni_add);
cfg_uni_add->Content_Type = "application/json";
server.addController(std::shared_ptr<lmh::Controller>(cfg_uni_add));

static Http_Responder cfg_uni_set(
auto* cfg_uni_set = new Http_Responder(
"POST",
"/api/config/uni/set",
authorized::token_protected<json>(&json_set_section_entry)
);
cfg_uni_set.Content_Type = "application/json";
server.addController(&cfg_uni_set);
cfg_uni_set->Content_Type = "application/json";
server.addController(std::shared_ptr<lmh::Controller>(cfg_uni_set));


static Http_Responder cfg_uni_get(
auto* cfg_uni_get = new Http_Responder(
"POST",
"/api/config/uni/get",
authorized::token_protected<json>(&json_get_section_entry)
);
cfg_uni_get.Content_Type = "application/json";
server.addController(&cfg_uni_get);
cfg_uni_get->Content_Type = "application/json";
server.addController(std::shared_ptr<lmh::Controller>(cfg_uni_get));
}

void controller_add_wh_register(lmh::WebServer& server) {
static Http_Responder webhook_register(
auto* webhook_register = new Http_Responder(
"POST",
"/api/webhook/register",
authorized::token_protected<json>(wh_register)
);
webhook_register.Content_Type = "application/json";
server.addController(&webhook_register);
webhook_register->Content_Type = "application/json";
server.addController(std::shared_ptr<lmh::Controller>(webhook_register));
}
void controller_add_wh_unregister(lmh::WebServer& server) {
static Http_Responder webhook_unregister(
auto* webhook_unregister = new Http_Responder(
"POST",
"/api/webhook/unregister",
authorized::token_protected<json>(wh_unregister)
);
webhook_unregister.Content_Type = "application/json";
server.addController(&webhook_unregister);
webhook_unregister->Content_Type = "application/json";
server.addController(std::shared_ptr<lmh::Controller>(webhook_unregister));
}


}

2 changes: 1 addition & 1 deletion src/service/httpd/handlers/dispatchers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace sx::webserver {
namespace dispatchers {
void controller_add_debug_only(lmh::WebServer &server);
void controller_add_status(lmh::WebServer &server);
void controller_add_commons(lmh::WebServer &server);
void controller_add_diag(lmh::WebServer &server);
void controller_add_uni(lmh::WebServer &server);
Expand Down
19 changes: 9 additions & 10 deletions src/service/httpd/handlers/handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace sx::webserver {
void controller_add_authorization(lmh::WebServer &server) {


static Http_Responder authorize_get(
auto* authorize_get = new Http_Responder(
"GET",
"/api/authorize",
[](MHD_Connection *conn, std::string const &meth, std::string const &req) -> Http_JsonResponseParams {
Expand All @@ -101,8 +101,10 @@ namespace sx::webserver {
ret.response_code = MHD_HTTP_FORBIDDEN;
return ret;
});
authorize_get->Content_Type = "application/json";
server.addController(std::shared_ptr<lmh::Controller>(authorize_get));

static Http_Responder authorize(
auto* authorize = new Http_Responder(
"POST",
"/api/authorize",
[](MHD_Connection *conn, std::string const &meth, std::string const &req) -> Http_JsonResponseParams {
Expand Down Expand Up @@ -141,12 +143,9 @@ namespace sx::webserver {
return ret;
}
);
authorize->Content_Type = "application/json";
server.addController(std::shared_ptr<lmh::Controller>(authorize));

authorize.Content_Type = "application/json";
authorize_get.Content_Type = "application/json";

server.addController(&authorize);
server.addController(&authorize_get);


auto split_form_data = [](std::string const& str, unsigned char sep1, unsigned char sep2) {
Expand All @@ -165,7 +164,7 @@ namespace sx::webserver {
return vals;
};

static Http_Responder login(
auto* login = new Http_Responder(
"POST",
"/api/login",
[&split_form_data](MHD_Connection *conn, std::string const &meth, std::string const &req) -> Http_JsonResponseParams {
Expand Down Expand Up @@ -226,8 +225,8 @@ namespace sx::webserver {
}
);

login.Content_Type = "application/json";
server.addController(&login);
login->Content_Type = "application/json";
server.addController(std::shared_ptr<lmh::Controller>(login));
}
}
}
2 changes: 1 addition & 1 deletion src/service/httpd/httpd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ std::thread* create_httpd_thread(unsigned short port) {

dispatchers::controller_add_authorization(server);

dispatchers::controller_add_debug_only(server);
dispatchers::controller_add_status(server);
dispatchers::controller_add_commons(server);
dispatchers::controller_add_diag(server);
dispatchers::controller_add_uni(server);
Expand Down

0 comments on commit b38914f

Please sign in to comment.