Skip to content

Commit

Permalink
handle variable url
Browse files Browse the repository at this point in the history
  • Loading branch information
brzep committed Nov 20, 2024
1 parent 8de27b4 commit bc5675c
Showing 1 changed file with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use super::{WhipCtx, WhipError};
use compositor_render::error::ErrorStack;
use reqwest::{
header::{HeaderMap, HeaderValue},
Client, Method, StatusCode, Url,
Client, Method, StatusCode,
};
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use tracing::{debug, error, info};
use url::{ParseError, Url};
use webrtc::{
ice_transport::{ice_candidate::RTCIceCandidate, ice_connection_state::RTCIceConnectionState},
peer_connection::{sdp::session_description::RTCSessionDescription, RTCPeerConnection},
Expand Down Expand Up @@ -103,24 +104,32 @@ pub async fn connect(
return Err(WhipError::BadStatus(status, answer.to_string()));
};

let location_url_path = response
let location_url_str = response
.headers()
.get("location")
.and_then(|url| url.to_str().ok())
.ok_or_else(|| WhipError::MissingLocationHeader)?;
error!("location header: {location_url_path}");

let scheme = endpoint_url.scheme();
let host = endpoint_url
.host_str()
.ok_or_else(|| WhipError::MissingHost)?;

let port = endpoint_url.port().ok_or_else(|| WhipError::MissingPort)?;

let formatted_url = format!("{}://{}:{}{}", scheme, host, port, location_url_path);

let location_url = Url::try_from(formatted_url.as_str())
.map_err(|e| WhipError::InvalidEndpointUrl(e, formatted_url))?;
let location_url = match Url::parse(location_url_str) {
Ok(url) => Ok(url),
Err(err) => match err {
ParseError::RelativeUrlWithoutBase => {
let scheme = endpoint_url.scheme();
let host = endpoint_url
.host_str()
.ok_or_else(|| WhipError::MissingHost)?;
let formatted_url = format!("{}://{}:{}{}", scheme, host, port, location_url_str);
let location_url = Url::try_from(formatted_url.as_str())
.map_err(|e| WhipError::InvalidEndpointUrl(e, formatted_url))?;
Ok(location_url)
}
_ => Err(WhipError::InvalidEndpointUrl(
err,
location_url_str.to_string(),
)),
},
}?;

peer_connection
.set_local_description(offer)
Expand Down

0 comments on commit bc5675c

Please sign in to comment.