mirror of
https://github.com/tinyhumansai/openhuman.git
synced 2026-07-27 21:08:00 +00:00
refactor(api): robust websocket URL construction (#1560)
Co-authored-by: Steven Enamakel <enamakel@tinyhumans.ai>
This commit is contained in:
+19
-8
@@ -1,16 +1,27 @@
|
||||
//! Socket.IO (Engine.IO v4) WebSocket URL for the TinyHumans backend.
|
||||
|
||||
use url::Url;
|
||||
|
||||
/// Build a Socket.IO WebSocket URL from an HTTP(S) API base (e.g. `https://api.tinyhumans.ai`).
|
||||
pub fn websocket_url(http_or_https_base: &str) -> String {
|
||||
let base = http_or_https_base.trim_end_matches('/');
|
||||
let ws_base = if base.starts_with("https://") {
|
||||
base.replacen("https://", "wss://", 1)
|
||||
} else if base.starts_with("http://") {
|
||||
base.replacen("http://", "ws://", 1)
|
||||
} else {
|
||||
base.to_string()
|
||||
let Ok(mut url) = Url::parse(http_or_https_base) else {
|
||||
return http_or_https_base.to_string();
|
||||
};
|
||||
format!("{}/socket.io/?EIO=4&transport=websocket", ws_base)
|
||||
|
||||
let scheme = match url.scheme() {
|
||||
"https" => "wss",
|
||||
"http" => "ws",
|
||||
other => other,
|
||||
}
|
||||
.to_string();
|
||||
|
||||
let _ = url.set_scheme(&scheme);
|
||||
|
||||
// Ensure path ends with /socket.io/ and includes required query params
|
||||
url.set_path(&format!("{}/socket.io/", url.path().trim_end_matches('/')));
|
||||
url.set_query(Some("EIO=4&transport=websocket"));
|
||||
|
||||
url.to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user