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

Web-Viewer: Don't auto-connect to wss://hostname when an ?url= is missing #3345

Merged
merged 6 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
124 changes: 60 additions & 64 deletions crates/re_viewer/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl WebHandle {
canvas_id,
web_options,
Box::new(move |cc| {
let app = create_app(cc, url.clone());
let app = create_app(cc, url.as_deref());
Box::new(app)
}),
)
Expand Down Expand Up @@ -77,7 +77,7 @@ impl WebHandle {
}
}

fn create_app(cc: &eframe::CreationContext<'_>, url: Option<String>) -> crate::App {
fn create_app(cc: &eframe::CreationContext<'_>, url: Option<&str>) -> crate::App {
let build_info = re_build_info::build_info!();
let app_env = crate::AppEnvironment::Web;
let startup_options = crate::StartupOptions {
Expand All @@ -91,7 +91,6 @@ fn create_app(cc: &eframe::CreationContext<'_>, url: Option<String>) -> crate::A
skip_welcome_screen: false,
};
let re_ui = crate::customize_eframe(cc);
let url = url.unwrap_or_else(|| get_url(&cc.integration_info));

let egui_ctx = cc.egui_ctx.clone();
let wake_up_ui_on_msg = Box::new(move || {
Expand All @@ -100,47 +99,60 @@ fn create_app(cc: &eframe::CreationContext<'_>, url: Option<String>) -> crate::A
egui_ctx.request_repaint_after(std::time::Duration::from_millis(10));
});

let rx = match categorize_uri(url) {
EndpointCategory::HttpRrd(url) => {
re_log_encoding::stream_rrd_from_http::stream_rrd_from_http_to_channel(
url,
Some(wake_up_ui_on_msg),
)
}
EndpointCategory::WebEventListener => {
// Process an rrd when it's posted via `window.postMessage`
let (tx, rx) = re_smart_channel::smart_channel(
re_smart_channel::SmartMessageSource::RrdWebEventCallback,
re_smart_channel::SmartChannelSource::RrdWebEventListener,
);
re_log_encoding::stream_rrd_from_http::stream_rrd_from_event_listener(Arc::new({
move |msg| {
wake_up_ui_on_msg();
use re_log_encoding::stream_rrd_from_http::HttpMessage;
match msg {
HttpMessage::LogMsg(msg) => {
tx.send(msg).warn_on_err_once("failed to send message")
}
HttpMessage::Success => {
tx.quit(None).warn_on_err_once("failed to send quit marker")
}
HttpMessage::Failure(err) => tx
.quit(Some(err))
.warn_on_err_once("failed to send quit marker"),
};
}
}));
rx
}
EndpointCategory::WebSocket(url) => {
re_data_source::connect_to_ws_url(&url, Some(wake_up_ui_on_msg)).unwrap_or_else(|err| {
panic!("Failed to connect to WebSocket server at {url}: {err}")
})
}
let mut app = crate::App::new(build_info, &app_env, startup_options, re_ui, cc.storage);

let url = match url {
Some(url) => Some(url),
None => cc
.integration_info
.web_info
.location
.query_map
.get("url")
.map(String::as_str),
};
if let Some(url) = url {
let rx = match categorize_uri(url) {
EndpointCategory::HttpRrd(url) => {
re_log_encoding::stream_rrd_from_http::stream_rrd_from_http_to_channel(
url,
Some(wake_up_ui_on_msg),
)
}
EndpointCategory::WebEventListener => {
// Process an rrd when it's posted via `window.postMessage`
let (tx, rx) = re_smart_channel::smart_channel(
re_smart_channel::SmartMessageSource::RrdWebEventCallback,
re_smart_channel::SmartChannelSource::RrdWebEventListener,
);
re_log_encoding::stream_rrd_from_http::stream_rrd_from_event_listener(Arc::new({
move |msg| {
wake_up_ui_on_msg();
use re_log_encoding::stream_rrd_from_http::HttpMessage;
match msg {
HttpMessage::LogMsg(msg) => {
tx.send(msg).warn_on_err_once("failed to send message")
}
HttpMessage::Success => {
tx.quit(None).warn_on_err_once("failed to send quit marker")
}
HttpMessage::Failure(err) => tx
.quit(Some(err))
.warn_on_err_once("failed to send quit marker"),
};
}
}));
rx
}
EndpointCategory::WebSocket(url) => {
re_data_source::connect_to_ws_url(&url, Some(wake_up_ui_on_msg)).unwrap_or_else(
|err| panic!("Failed to connect to WebSocket server at {url}: {err}"),
)
}
};
app.add_receiver(rx);
}

let mut app = crate::App::new(build_info, &app_env, startup_options, re_ui, cc.storage);
app.add_receiver(rx);
app
}

Expand Down Expand Up @@ -173,38 +185,22 @@ enum EndpointCategory {
WebEventListener,
}

fn categorize_uri(mut uri: String) -> EndpointCategory {
fn categorize_uri(uri: &str) -> EndpointCategory {
if uri.starts_with("http") || uri.ends_with(".rrd") {
EndpointCategory::HttpRrd(uri)
EndpointCategory::HttpRrd(uri.into())
} else if uri.starts_with("ws:") || uri.starts_with("wss:") {
EndpointCategory::WebSocket(uri)
EndpointCategory::WebSocket(uri.into())
} else if uri.starts_with("web_event:") {
EndpointCategory::WebEventListener
} else {
// If this is sometyhing like `foo.com` we can't know what it is until we connect to it.
// We could/should connect and see what it is, but for now we just take a wild guess instead:
re_log::info!("Assuming WebSocket endpoint");
if !uri.contains("://") {
uri = format!("{}://{uri}", re_ws_comms::PROTOCOL);
EndpointCategory::WebSocket(format!("{}://{uri}", re_ws_comms::PROTOCOL))
} else {
jprochazk marked this conversation as resolved.
Show resolved Hide resolved
EndpointCategory::WebSocket(uri.into())
}
EndpointCategory::WebSocket(uri)
}
}

fn get_url(info: &eframe::IntegrationInfo) -> String {
let mut url = String::new();
if let Some(param) = info.web_info.location.query_map.get("url") {
emilk marked this conversation as resolved.
Show resolved Hide resolved
url = param.clone();
}
if url.is_empty() {
format!(
"{}://{}:{}",
re_ws_comms::PROTOCOL,
&info.web_info.location.hostname,
re_ws_comms::DEFAULT_WS_SERVER_PORT
)
} else {
url
}
}

Expand Down
5 changes: 4 additions & 1 deletion scripts/ci/demo_assets/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ function on_wasm_loaded() {
check_for_panic();

let url = determine_url();
handle.start("the_canvas_id", url).then(on_app_started).catch(on_wasm_error);
handle
.start("the_canvas_id", url)
.then(on_app_started)
.catch(on_wasm_error);
}

function on_app_started(handle) {
Expand Down
38 changes: 5 additions & 33 deletions web_viewer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,11 @@

check_for_panic();

let url = determine_url();
handle.start("the_canvas_id", url).then(on_app_started).catch(on_wasm_error);
let url = new URLSearchParams(window.location.search).get("url");
handle
.start("the_canvas_id", url)
.then(on_app_started)
.catch(on_wasm_error);
}

function on_app_started(handle) {
Expand All @@ -345,37 +348,6 @@
}
}

function determine_url() {
// If a 'url' is provided as a url-param, use it.
// Although `web.rs` can also parse the url-param itself,
// it won't do so if we pass in a non-null url. We could
// arguably return null here instead and achieve the same
// behavior, but as long as we've queried it anyways, we
// may as well just pass it in for consistency.

const url_params = new URLSearchParams(window.location.search);

let url = url_params.get('url');

if (url) {
return url;
}

// Otherwise, look up an rrd in the data path.

// The expected data path is the current pathname relocated to inside of "/data" with the
// index.html stripped off if it's present.
// exa: 'https://app.rerun.io/version/v4.0.0/index.html' -> '/data/version/v4.0.0/'
let data_path = '/data/' + window.location.pathname.replace(/index\.html$/, '') + '/';

const rrd_file = url_params.get('file') || 'colmap_fiat.rrd';

// Normalize the extra slashes from the url
const normalized = (data_path + rrd_file).replace(/\/{2,}/g, '/');
window.location.search = `?url=${normalized}`;
return normalized;
emilk marked this conversation as resolved.
Show resolved Hide resolved
}

function on_wasm_error(error) {
console.error("Failed to start: " + error);

Expand Down
5 changes: 4 additions & 1 deletion web_viewer/index_bundled.html
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,10 @@
check_for_panic();

let url = null; // you can use this to point to an .rrd file over http, or a WebSocket Rerun server.
handle.start("the_canvas_id", url).then(on_app_started).catch(on_wasm_error);
handle
.start("the_canvas_id", url)
.then(on_app_started)
.catch(on_wasm_error);
}

function on_app_started(handle) {
Expand Down