UniSet 2.45.1
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 <mutex>
17#include <thread>
18#include "UHttpRequestHandler.h"
19#include "ProcessManager.h"
20// -------------------------------------------------------------------------
21namespace uniset
22{
42 class LauncherHttpRegistry :
44 public std::enable_shared_from_this<LauncherHttpRegistry>
45 {
46 public:
47 explicit LauncherHttpRegistry(ProcessManager& pm);
48 virtual ~LauncherHttpRegistry();
49
50 // Configuration
51 void setReadToken(const std::string& token);
52 void setControlToken(const std::string& token);
53 void setHtmlTemplate(const std::string& path);
54
55 // IHttpRequestRegistry interface
56 Poco::JSON::Object::Ptr httpRequest(const UHttp::HttpRequestContext& ctx) override;
57 Poco::JSON::Array::Ptr httpGetObjectsList(const UHttp::HttpRequestContext& ctx) override;
58 Poco::JSON::Object::Ptr httpHelpRequest(const UHttp::HttpRequestContext& ctx) override;
59
60 // Static file serving (HTML, JS)
62 const std::string& path,
63 Poco::Net::HTTPServerRequest& req,
64 Poco::Net::HTTPServerResponse& resp) override;
65
66 private:
67 Poco::JSON::Object::Ptr handleStatus();
68 Poco::JSON::Object::Ptr handleProcesses();
69 Poco::JSON::Object::Ptr handleProcess(const std::string& name);
70 Poco::JSON::Object::Ptr handleProcessAction(
71 const std::string& name,
72 std::function<bool(const std::string&)> action,
73 const std::string& errorMessage);
74 Poco::JSON::Object::Ptr handleBulkOp(
75 BulkOperation opType,
76 std::function<void()> action,
77 const std::string& alreadyMsg,
78 const std::string& initiatedMsg);
79 Poco::JSON::Object::Ptr handleStopAll();
80 Poco::JSON::Object::Ptr handleHealth();
81 Poco::JSON::Object::Ptr handleGroups();
82 Poco::JSON::Object::Ptr handleHelp();
83
84 Poco::JSON::Object::Ptr processToJSON(const ProcessInfo& proc);
85 Poco::JSON::Object::Ptr groupToJSON(const ProcessGroup& group);
86
87 // Authorization helpers
88 bool checkReadAuth(const Poco::Net::HTTPServerRequest& req);
89 bool checkControlAuth(const Poco::Net::HTTPServerRequest& req);
90 Poco::JSON::Object::Ptr checkControlAccess(const UHttp::HttpRequestContext& ctx);
91 static bool validateBearerToken(const Poco::Net::HTTPServerRequest& req,
92 const std::string& expectedToken);
93
94 // File serving helpers
95 bool sendStaticFile(const std::string& filename,
96 const std::string& contentType,
97 bool applyVars,
98 Poco::Net::HTTPServerRequest& req,
99 Poco::Net::HTTPServerResponse& resp);
100 std::string findFile(const std::string& filename);
101 std::string applyTemplateVars(const std::string& content);
102
103 ProcessManager& pm_;
104 std::string readToken_;
105 std::string controlToken_;
106 std::string htmlTemplatePath_;
107
108 std::mutex bulkThreadMutex_;
109 std::thread bulkOpThread_;
110
111 void joinBulkOp_();
112 };
113
114} // end of namespace uniset
115// -------------------------------------------------------------------------
116#endif // DISABLE_REST_API
117// -------------------------------------------------------------------------
118#endif // LauncherHttpRegistry_H_
119// -------------------------------------------------------------------------
bool httpStaticRequest(const std::string &path, Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp) override
Определения LauncherHttpRegistry.cc:624
Определения ProcessManager.h:51
Определения UHttpRequestHandler.h:146
Определения Calibration.h:27
BulkOperation
Определения ProcessManager.h:43
Определения ProcessInfo.h:113
Определения ProcessInfo.h:66
Определения UHttpRequestHandler.h:87