GCC Code Coverage Report


Directory: ./
File: PluginWebSocket.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 96 113 85.0%
Functions: 12 16 75.0%
Branches: 78 186 41.9%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2025 by the Falcon Player Developers.
4 *
5 * The Falcon Player (FPP) is free software, and is covered under
6 * multiple Open Source licenses. Please see the included 'LICENSES'
7 * file for descriptions of what files are covered by each license.
8 *
9 * This source file is covered under the LGPL v2.1 as described in the
10 * included LICENSE.LGPL file.
11 */
12
13 // The registry behind FPPPlugins::registerPluginWebSocket(). See
14 // PluginWebSocket.h for the contract; this follows the route registry in
15 // fpphttp_compat.cpp, with one addition: a WebSocket outlives the request that
16 // opened it, so a slot also tracks the connections opened through it, and
17 // disarming closes them.
18 //
19 // The endpoint is chosen by the "name" query parameter rather than by path
20 // because the drogon on FPP OS images (Debian's 1.9.0) matches WebSocket routes
21 // by exact path only.
22
23 #include <drogon/HttpAppFramework.h>
24 #include <drogon/HttpFilter.h>
25 #include <drogon/WebSocketController.h>
26 // trantor (drogon's net layer) defines LOG_* macros that collide with FPP's
27 // LogLevel enum; drop them before pulling in the FPP headers, same as httpAPI.cpp.
28 #undef LOG_WARN
29 #undef LOG_INFO
30 #undef LOG_DEBUG
31
32 #include "PluginWebSocket.h"
33 #include "fpphttp.h"
34 #include "log.h"
35
36 #include <map>
37 #include <memory>
38 #include <mutex>
39 #include <shared_mutex>
40 #include <string>
41 #include <vector>
42
43 using namespace drogon;
44
45 namespace {
46
47 struct WsSlot {
48 // Guarded by s_mutex.
49 std::shared_ptr<FPPPlugins::PluginWebSocketHandlers> handlers;
50 std::string name; // as registered, for logging
51 };
52 using WsSlotPtr = std::shared_ptr<WsSlot>;
53
54 std::mutex s_mutex;
55 // Keyed by normalizedName(). Slots are never erased, so a name that was ever
56 // registered answers 410 rather than 404 once disarmed.
57 std::map<std::string, WsSlotPtr> s_slots;
58 // Every open connection that a plugin was handed, and the slot it came through.
59 // Disarming removes a slot's connections from here before closing them, so the
60 // close and any message still in flight on them find no entry and never reach
61 // whichever plugin build arms the slot next.
62 std::map<WebSocketConnectionPtr, WsSlotPtr> s_conns;
63
64 // Same role as s_dispatchMutex in fpphttp_compat.cpp: held shared for every
65 // call into plugin code, exclusively by disarm before it destroys the handlers.
66 std::shared_mutex s_dispatchMutex;
67
68 // Matched case-insensitively and ignoring leading and trailing '/', like
69 // plugin HTTP routes.
70 5 std::string normalizedName(const std::string& name) {
71 5 size_t b = name.find_first_not_of('/');
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (b == std::string::npos) {
73 ✗ return "";
74 }
75 5 size_t e = name.find_last_not_of('/');
76
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 std::string n = name.substr(b, e - b + 1);
77
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 5 times.
62 for (auto& c : n) {
78 57 c = (char)tolower((unsigned char)c);
79 }
80 5 return n;
81 5 }
82
83 3 std::string requestName(const HttpRequestPtr& req) {
84
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
9 return normalizedName(req->getParameter("name"));
85 }
86
87 // Runs a plugin handler; a throw must not reach drogon's event loop.
88 template <typename Fn>
89 2 void callPlugin(const char* what, const std::string& name, Fn&& fn) {
90 try {
91
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 fn();
92 ✗ } catch (const std::exception& ex) {
93 ✗ LogErr(VB_PLUGIN, "Plugin WebSocket %s handler for %s threw: %s\n", what, name.c_str(), ex.what());
94 ✗ } catch (...) {
95 ✗ LogErr(VB_PLUGIN, "Plugin WebSocket %s handler for %s threw a non-standard exception\n", what, name.c_str());
96 }
97 2 }
98
99 // The slot an open connection came through, or null if it was never handed to
100 // a plugin or its slot has been disarmed since. Caller holds s_mutex.
101 2 WsSlotPtr slotFor(const WebSocketConnectionPtr& conn) {
102
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto it = s_conns.find(conn);
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 return it == s_conns.end() ? nullptr : it->second;
104 }
105
106 } // namespace
107
108 // Refuses the handshake for a name no plugin has armed, so a client can tell
109 // "no such endpoint" (404, or 410 for a plugin that has since unloaded) from a
110 // connection that failed. Arming can still change between this and
111 // handleNewConnection(); that is handled there.
112 class FPPPluginWebSocketFilter : public drogon::HttpFilter<FPPPluginWebSocketFilter> {
113 public:
114 2 void doFilter(const HttpRequestPtr& req, FilterCallback&& fcb, FilterChainCallback&& fccb) override {
115 2 int code = 404;
116 {
117
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::lock_guard<std::mutex> lock(s_mutex);
118
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 auto it = s_slots.find(requestName(req));
119
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (it != s_slots.end()) {
120
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 code = it->second->handlers ? 0 : 410;
121 }
122 2 }
123
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (code == 0) {
124 1 fccb();
125 } else {
126
5/10
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
4 fcb(makeStringResponse(code == 404 ? "Not Found" : "Plugin not loaded", code, "text/plain"));
127 }
128 2 }
129 };
130
131 // The one drogon route for every plugin WebSocket.
132 class FPPPluginWebSocket : public drogon::WebSocketController<FPPPluginWebSocket> {
133 public:
134 1 void handleNewConnection(const HttpRequestPtr& req, const WebSocketConnectionPtr& conn) override {
135 // Taken before the slot is read and released last, as in
136 // dispatchPluginApi(): a disarm must not destroy the handlers while
137 // this is about to call them.
138
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::shared_lock<std::shared_mutex> dispatchLock(s_dispatchMutex);
139 1 std::shared_ptr<FPPPlugins::PluginWebSocketHandlers> h;
140 {
141
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::lock_guard<std::mutex> lock(s_mutex);
142
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 auto it = s_slots.find(requestName(req));
143
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 if (it != s_slots.end() && it->second->handlers) {
144 1 h = it->second->handlers;
145
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 s_conns[conn] = it->second;
146 }
147 1 }
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!h) {
149 // Disarmed after the filter let the handshake through.
150 ✗ conn->shutdown(CloseCode::kEndpointGone, "Plugin not loaded");
151 ✗ return;
152 }
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (h->onConnect) {
154 ✗ callPlugin("connect", req->getParameter("name"), [&] { h->onConnect(req, conn); });
155 }
156
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 }
157
158 1 void handleNewMessage(const WebSocketConnectionPtr& conn, std::string&& message,
159 const WebSocketMessageType& type) override {
160
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::shared_lock<std::shared_mutex> dispatchLock(s_dispatchMutex);
161 1 std::shared_ptr<FPPPlugins::PluginWebSocketHandlers> h;
162 1 std::string name;
163 {
164
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::lock_guard<std::mutex> lock(s_mutex);
165
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (WsSlotPtr slot = slotFor(conn)) {
166 1 h = slot->handlers;
167
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 name = slot->name;
168 1 }
169 1 }
170
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 if (h && h->onMessage) {
171
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 callPlugin("message", name, [&] { h->onMessage(conn, std::move(message), type); });
172 }
173 1 }
174
175 1 void handleConnectionClosed(const WebSocketConnectionPtr& conn) override {
176
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::shared_lock<std::shared_mutex> dispatchLock(s_dispatchMutex);
177 1 std::shared_ptr<FPPPlugins::PluginWebSocketHandlers> h;
178 1 std::string name;
179 {
180
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::lock_guard<std::mutex> lock(s_mutex);
181
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (WsSlotPtr slot = slotFor(conn)) {
182 1 h = slot->handlers;
183
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 name = slot->name;
184 1 }
185
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 s_conns.erase(conn);
186 1 }
187
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 if (h && h->onClose) {
188 ✗ callPlugin("close", name, [&] { h->onClose(conn); });
189 }
190 1 }
191
192 // drogon creates the filter by name and nothing here constructs one, so
193 // the class template's static registrar is only instantiated -- and the
194 // filter only found -- because classTypeName() uses it. A string literal
195 // name compiles fine and leaves the route unfiltered.
196 3252 WS_PATH_LIST_BEGIN
197
5/10
✓ Branch 0 taken 3252 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3252 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3252 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3252 times.
✓ Branch 7 taken 3252 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
15894 WS_PATH_ADD("/fppdws/plugin", FPPPluginWebSocketFilter::classTypeName());
198
2/8
✓ Branch 0 taken 3252 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3252 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
6504 WS_PATH_LIST_END
199 };
200
201 namespace FPPPlugins {
202
203 1 bool registerPluginWebSocket(const std::string& name, PluginWebSocketHandlers handlers) {
204
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::string key = normalizedName(name);
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (key.empty()) {
206 ✗ LogErr(VB_PLUGIN, "Plugin WebSocket registered with an empty name\n");
207 ✗ return false;
208 }
209
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 auto h = std::make_shared<PluginWebSocketHandlers>(std::move(handlers));
210
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::lock_guard<std::mutex> lock(s_mutex);
211
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 auto& slot = s_slots[key];
212
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!slot) {
213
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 slot = std::make_shared<WsSlot>();
214 }
215 1 slot->handlers = h;
216
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 slot->name = name;
217 1 return true;
218 1 }
219
220 1 void unregisterPluginWebSocket(const std::string& name) {
221 1 std::shared_ptr<PluginWebSocketHandlers> extracted;
222 1 std::vector<WebSocketConnectionPtr> conns;
223 {
224
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::lock_guard<std::mutex> lock(s_mutex);
225
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 auto it = s_slots.find(normalizedName(name));
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (it == s_slots.end()) {
227 ✗ return;
228 }
229 // Clear and detach in one step: a connection arriving now finds the
230 // slot empty, and none of this slot's connections can reach a handler.
231 1 extracted = std::move(it->second->handlers);
232 1 it->second->handlers.reset();
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 for (auto c = s_conns.begin(); c != s_conns.end();) {
234 ✗ if (c->second == it->second) {
235 ✗ conns.push_back(c->first);
236 ✗ c = s_conns.erase(c);
237 } else {
238 ✗ ++c;
239 }
240 }
241
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 }
242 {
243
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::unique_lock<std::shared_mutex> drain(s_dispatchMutex);
244 // Nothing can be dispatching now, and this is the last reference.
245 1 extracted.reset();
246 1 }
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 for (auto& c : conns) {
248 ✗ c->shutdown(CloseCode::kEndpointGone, "Plugin unloaded");
249 }
250
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 }
251
252 } // namespace FPPPlugins
253