|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011-2026 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +// WebSocket handler registry collaborator. Internal header; only reachable |
| 22 | +// when compiling libhttpserver translation units. NOT part of the installed |
| 23 | +// surface. |
| 24 | +#if !defined(HTTPSERVER_COMPILATION) |
| 25 | +#error "ws_registry.hpp is internal; only reachable when compiling libhttpserver." |
| 26 | +#endif |
| 27 | + |
| 28 | +#ifndef SRC_HTTPSERVER_DETAIL_WS_REGISTRY_HPP_ |
| 29 | +#define SRC_HTTPSERVER_DETAIL_WS_REGISTRY_HPP_ |
| 30 | + |
| 31 | +#include <map> |
| 32 | +#include <memory> |
| 33 | +#include <shared_mutex> |
| 34 | +#include <string> |
| 35 | + |
| 36 | +namespace httpserver { |
| 37 | + |
| 38 | +class websocket_handler; |
| 39 | + |
| 40 | +namespace detail { |
| 41 | + |
| 42 | +// Owns the URL -> websocket_handler map and its mutex — the whole of the |
| 43 | +// webserver's websocket-registration state, extracted from webserver_impl. |
| 44 | +// webserver::{register,unregister}_ws_resource mutate it; the dispatch-side |
| 45 | +// upgrade path resolves a handler via find(); start() consults empty() to |
| 46 | +// decide MHD_ALLOW_UPGRADE. This type never dereferences websocket_handler |
| 47 | +// (it only stores/erases/copies shared_ptrs), so it is feature-independent |
| 48 | +// and compiles even on HAVE_WEBSOCKET-off builds. |
| 49 | +class ws_registry { |
| 50 | + public: |
| 51 | + // Register @p handler at @p url_key. Returns false if a handler is |
| 52 | + // already present at that key (the caller surfaces the collision), true |
| 53 | + // on insert. Takes the write lock. |
| 54 | + bool try_register(std::string url_key, |
| 55 | + std::shared_ptr<websocket_handler> handler); |
| 56 | + |
| 57 | + // Erase any handler registered at @p url_key. Takes the write lock. |
| 58 | + void unregister(const std::string& url_key); |
| 59 | + |
| 60 | + // Return a shared_ptr copy of the handler registered at @p url, or |
| 61 | + // nullptr if none. The copy keeps the handler alive across an MHD |
| 62 | + // upgrade even if a concurrent unregister races to drop the slot. |
| 63 | + // Takes the read lock. |
| 64 | + [[nodiscard]] std::shared_ptr<websocket_handler> find( |
| 65 | + const std::string& url) const; |
| 66 | + |
| 67 | + // True iff no handler is registered. Takes the read lock. |
| 68 | + [[nodiscard]] bool empty() const; |
| 69 | + |
| 70 | + private: |
| 71 | + mutable std::shared_mutex mutex_; |
| 72 | + std::map<std::string, std::shared_ptr<websocket_handler>> handlers_; |
| 73 | +}; |
| 74 | + |
| 75 | +} // namespace detail |
| 76 | +} // namespace httpserver |
| 77 | + |
| 78 | +#endif // SRC_HTTPSERVER_DETAIL_WS_REGISTRY_HPP_ |
0 commit comments