UniSet @VERSION@
LauncherHttpRegistry.h
1/*
2 * Copyright (c) 2026 Pavel Vainerman.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation, version 2.1.
7 */
8// -------------------------------------------------------------------------
9#ifndef LauncherHttpRegistry_H_
10#define LauncherHttpRegistry_H_
11// -------------------------------------------------------------------------
12#ifndef DISABLE_REST_API
13// -------------------------------------------------------------------------
14#include <memory>
15#include <functional>
16#include "UHttpRequestHandler.h"
17#include "ProcessManager.h"
18// -------------------------------------------------------------------------
19namespace uniset
20{
42 public std::enable_shared_from_this<LauncherHttpRegistry>
43 {
44 public:
46 virtual ~LauncherHttpRegistry() = default;
47
48 // Configuration
49 void setReadToken(const std::string& token);
50 void setControlToken(const std::string& token);
51 void setHtmlTemplate(const std::string& path);
52
53 // IHttpRequestRegistry interface
54 Poco::JSON::Object::Ptr httpRequest(const UHttp::HttpRequestContext& ctx) override;
55 Poco::JSON::Array::Ptr httpGetObjectsList(const UHttp::HttpRequestContext& ctx) override;
56 Poco::JSON::Object::Ptr httpHelpRequest(const UHttp::HttpRequestContext& ctx) override;
57
58 // Static file serving (HTML, JS)
60 const std::string& path,
61 Poco::Net::HTTPServerRequest& req,
62 Poco::Net::HTTPServerResponse& resp) override;
63
64 private:
65 Poco::JSON::Object::Ptr handleStatus();
66 Poco::JSON::Object::Ptr handleProcesses();
67 Poco::JSON::Object::Ptr handleProcess(const std::string& name);
68 Poco::JSON::Object::Ptr handleProcessAction(
69 const std::string& name,
70 std::function<bool(const std::string&)> action,
71 const std::string& errorMessage);
72 Poco::JSON::Object::Ptr handleBulkOp(
73 BulkOperation opType,
74 std::function<void()> action,
75 const std::string& alreadyMsg,
76 const std::string& initiatedMsg);
77 Poco::JSON::Object::Ptr handleStopAll();
78 Poco::JSON::Object::Ptr handleHealth();
79 Poco::JSON::Object::Ptr handleGroups();
80 Poco::JSON::Object::Ptr handleHelp();
81
82 Poco::JSON::Object::Ptr processToJSON(const ProcessInfo& proc);
83 Poco::JSON::Object::Ptr groupToJSON(const ProcessGroup& group);
84
85 // Authorization helpers
86 bool checkReadAuth(const Poco::Net::HTTPServerRequest& req);
87 bool checkControlAuth(const Poco::Net::HTTPServerRequest& req);
88 Poco::JSON::Object::Ptr checkControlAccess(const UHttp::HttpRequestContext& ctx);
89 static bool validateBearerToken(const Poco::Net::HTTPServerRequest& req,
90 const std::string& expectedToken);
91
92 // File serving helpers
93 bool sendStaticFile(const std::string& filename,
94 const std::string& contentType,
95 bool applyVars,
96 Poco::Net::HTTPServerRequest& req,
97 Poco::Net::HTTPServerResponse& resp);
98 std::string findFile(const std::string& filename);
99 std::string applyTemplateVars(const std::string& content);
100
101 ProcessManager& pm_;
102 std::string readToken_;
103 std::string controlToken_;
104 std::string htmlTemplatePath_;
105 };
106
107} // end of namespace uniset
108// -------------------------------------------------------------------------
109#endif // DISABLE_REST_API
110// -------------------------------------------------------------------------
111#endif // LauncherHttpRegistry_H_
112// -------------------------------------------------------------------------
Definition LauncherHttpRegistry.h:43
bool httpStaticRequest(const std::string &path, Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp) override
Definition LauncherHttpRegistry.cc:589
Definition ProcessManager.h:51
Definition UHttpRequestHandler.h:146
Definition AccessConfig.h:30
BulkOperation
Definition ProcessManager.h:43
Definition ProcessInfo.h:113
Definition ProcessInfo.h:66
Definition UHttpRequestHandler.h:87