Skip to content

Commit

Permalink
more explicit url errors (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
theomonnom authored Jan 11, 2024
1 parent 647eaad commit 493ea80
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 34 additions & 3 deletions livekit-api/src/signal_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ pub const PROTOCOL_VERSION: u32 = 9;
pub enum SignalError {
#[error("ws failure: {0}")]
WsError(#[from] WsError),
#[error("failed to parse the url {0}")]
UrlParse(#[from] url::ParseError),
#[error("failed to parse the url: {0}")]
UrlParse(String),
#[error("client error: {0} - {1}")]
Client(StatusCode, String),
#[error("server error: {0} - {1}")]
Expand Down Expand Up @@ -392,7 +392,20 @@ fn is_queuable(signal: &proto::signal_request::Message) -> bool {
}

fn get_livekit_url(url: &str, token: &str, options: &SignalOptions) -> SignalResult<url::Url> {
let mut lk_url = url::Url::parse(url)?;
let mut lk_url = url::Url::parse(url).map_err(|err| SignalError::UrlParse(err.to_string()))?;

if !lk_url.has_host() {
return Err(SignalError::UrlParse("missing host or scheme".into()));
}

// Automatically switch to websocket scheme when using user is providing http(s) scheme
if lk_url.scheme() == "https" {
lk_url.set_scheme("wss").unwrap();
} else if lk_url.scheme() == "http" {
lk_url.set_scheme("ws").unwrap();
} else if lk_url.scheme() != "wss" && lk_url.scheme() != "ws" {
return Err(SignalError::UrlParse(format!("unsupported scheme: {}", lk_url.scheme())));
}

if let Ok(mut segs) = lk_url.path_segments_mut() {
segs.push("rtc");
Expand Down Expand Up @@ -442,3 +455,21 @@ get_async_message!(
proto::signal_response::Message::Reconnect(msg) => msg,
proto::ReconnectResponse
);

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn livekit_url_test() {
let it = "null_token";
let io = SignalOptions::default();

assert!(get_livekit_url("localhost:7880", it, &io).is_err());
assert_eq!(get_livekit_url("https://localhost:7880", it, &io).unwrap().scheme(), "wss");
assert_eq!(get_livekit_url("http://localhost:7880", it, &io).unwrap().scheme(), "ws");
assert_eq!(get_livekit_url("wss://localhost:7880", it, &io).unwrap().scheme(), "wss");
assert_eq!(get_livekit_url("ws://localhost:7880", it, &io).unwrap().scheme(), "ws");
assert!(get_livekit_url("ftp://localhost:7880", it, &io).is_err());
}
}
9 changes: 1 addition & 8 deletions livekit-api/src/signal_client/signal_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl SignalStream {
/// SignalStream will never try to reconnect if the connection has been
/// closed.
pub async fn connect(
mut url: url::Url,
url: url::Url,
) -> SignalResult<(Self, mpsc::UnboundedReceiver<Box<proto::signal_response::Message>>)> {
{
// Don't log sensitive info
Expand All @@ -82,13 +82,6 @@ impl SignalStream {
log::info!("connecting to {}", url);
}

// Automatically switch to websocket scheme when using http
if url.scheme() == "https" {
url.set_scheme("wss").unwrap();
} else if url.scheme() == "http" {
url.set_scheme("ws").unwrap();
}

let (ws_stream, _) = connect_async(url).await?;
let (ws_writer, ws_reader) = ws_stream.split();

Expand Down

0 comments on commit 493ea80

Please sign in to comment.