diff options
Diffstat (limited to 'src/zenhttp')
| -rw-r--r-- | src/zenhttp/clients/httpwsclient.cpp | 30 | ||||
| -rw-r--r-- | src/zenhttp/include/zenhttp/httpwsclient.h | 10 | ||||
| -rw-r--r-- | src/zenhttp/include/zenhttp/websocket.h | 2 |
3 files changed, 42 insertions, 0 deletions
diff --git a/src/zenhttp/clients/httpwsclient.cpp b/src/zenhttp/clients/httpwsclient.cpp index fbae9f5fe..770213738 100644 --- a/src/zenhttp/clients/httpwsclient.cpp +++ b/src/zenhttp/clients/httpwsclient.cpp @@ -638,4 +638,34 @@ HttpWsClient::IsOpen() const return m_Impl->m_IsOpen.load(std::memory_order_relaxed); } +std::string +HttpToWsUrl(std::string_view Endpoint, std::string_view Path) +{ + std::string_view Scheme = "ws://"; + std::string_view Host = Endpoint; + + if (Endpoint.starts_with("http://")) + { + Host = Endpoint.substr(7); + } + else if (Endpoint.starts_with("https://")) + { + Scheme = "wss://"; + Host = Endpoint.substr(8); + } + + // Strip trailing slash from host to avoid double-slash when Path starts with '/' + if (!Host.empty() && Host.back() == '/') + { + Host = Host.substr(0, Host.size() - 1); + } + + std::string Url; + Url.reserve(Scheme.size() + Host.size() + Path.size()); + Url.append(Scheme); + Url.append(Host); + Url.append(Path); + return Url; +} + } // namespace zen diff --git a/src/zenhttp/include/zenhttp/httpwsclient.h b/src/zenhttp/include/zenhttp/httpwsclient.h index 2ca9b7ab1..9c3b909a2 100644 --- a/src/zenhttp/include/zenhttp/httpwsclient.h +++ b/src/zenhttp/include/zenhttp/httpwsclient.h @@ -80,4 +80,14 @@ private: std::unique_ptr<Impl> m_Impl; }; +/// Convert an HTTP(S) endpoint to a WebSocket URL by replacing the scheme +/// and appending the given path. If the endpoint has no recognised scheme, +/// it is treated as a plain host:port and gets the ws:// prefix. +/// +/// Examples: +/// HttpToWsUrl("http://host:8080", "/orch/ws") → "ws://host:8080/orch/ws" +/// HttpToWsUrl("https://host", "/foo") → "wss://host/foo" +/// HttpToWsUrl("host:8080", "/bar") → "ws://host:8080/bar" +std::string HttpToWsUrl(std::string_view Endpoint, std::string_view Path); + } // namespace zen diff --git a/src/zenhttp/include/zenhttp/websocket.h b/src/zenhttp/include/zenhttp/websocket.h index bc3293282..710579faa 100644 --- a/src/zenhttp/include/zenhttp/websocket.h +++ b/src/zenhttp/include/zenhttp/websocket.h @@ -43,6 +43,8 @@ public: virtual void SendBinary(std::span<const uint8_t> Data) = 0; virtual void Close(uint16_t Code = 1000, std::string_view Reason = {}) = 0; virtual bool IsOpen() const = 0; + + void SendBinary(MemoryView Data) { SendBinary(std::span<const uint8_t>(static_cast<const uint8_t*>(Data.GetData()), Data.GetSize())); } }; /** |