Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more explicit url errors #281

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome!

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
Loading