From 32e4922c886b8e794a6d12aa09bb793297c698d0 Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 16 Mar 2026 19:44:19 +0100 Subject: [PATCH] Commit unstaged changes --- euphoxide/src/client/conn.rs | 24 +++++++++++++++++------- euphoxide/src/error.rs | 4 ++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/euphoxide/src/client/conn.rs b/euphoxide/src/client/conn.rs index 2ee0905..ddb7a20 100644 --- a/euphoxide/src/client/conn.rs +++ b/euphoxide/src/client/conn.rs @@ -29,8 +29,8 @@ enum ConnCommand { /// Configuration options for a [`ClientConn`]. #[derive(Debug, Clone)] pub struct ClientConnConfig { - /// The domain where the server is hosted. - pub domain: String, + /// The HTTP(S) url where the server is hosted. + pub url: String, /// Whether the client should present itself as a human to the server. /// /// This should only be set if the client is directly acting on behalf of a @@ -53,7 +53,7 @@ pub struct ClientConnConfig { impl Default for ClientConnConfig { fn default() -> Self { Self { - domain: "euphoria.leet.nu".to_string(), + url: "https://euphoria.leet.nu/".to_string(), human: false, channel_bufsize: 10, connect_timeout: Duration::from_secs(10), @@ -211,14 +211,24 @@ impl ClientConn { room: &str, cookies: Option, config: &ClientConnConfig, - ) -> Result<(Self, Vec)> { // Prepare URL + ) -> Result<(Self, Vec)> { let human = if config.human { "?h=1" } else { "" }; - let uri = format!("wss://{}/room/{room}/ws{human}", config.domain); - debug!("Connecting to {uri} with cookies: {cookies:?}"); + + if !(config.url.starts_with("http://") || config.url.starts_with("https://")) { + return Err(Error::InvalidUrl); + }; + let prepared_url = config + .url + .strip_prefix("http") + .ok_or(Error::InvalidUrl)? + .trim_end_matches('/'); + + let ws_url = format!("ws{prepared_url}/room/{room}/ws{human}"); + debug!("Connecting to {ws_url} with cookies: {cookies:?}"); // Prepare request - let mut request = uri.into_client_request().expect("valid request"); + let mut request = ws_url.into_client_request().expect("valid request"); if let Some(cookies) = cookies { request.headers_mut().append(header::COOKIE, cookies); } diff --git a/euphoxide/src/error.rs b/euphoxide/src/error.rs index 9e3b412..bf2397f 100644 --- a/euphoxide/src/error.rs +++ b/euphoxide/src/error.rs @@ -9,6 +9,9 @@ use crate::api::PacketType; /// Possible euphoria communication errors. #[derive(Debug)] pub enum Error { + /// The URL has an invalid format. + InvalidUrl, + /// The connection is closed. ConnectionClosed, @@ -54,6 +57,7 @@ pub enum Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { + Self::InvalidUrl => write!(f, "url has invalid format"), Self::ConnectionClosed => write!(f, "connection closed"), Self::PingTimeout => write!(f, "ping timed out"), Self::MalformedPacket(err) => write!(f, "malformed packet: {err}"),