Skip to content

Commit

Permalink
core: Remove NavigatorBackend::resolve_relative_url
Browse files Browse the repository at this point in the history
The resolved URL only used by `NavigatorBackend::fetch`. So simply
inline `NavigatorBackend::resolve_relative_url` into `NavigatorBackend::fetch`,
per implementation.
  • Loading branch information
relrelb authored and Herschel committed May 7, 2022
1 parent ea665d9 commit 87ce0f5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 76 deletions.
20 changes: 0 additions & 20 deletions core/src/backend/navigator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::loader::Error;
use crate::string::WStr;
use indexmap::IndexMap;
use std::borrow::Cow;
use std::future::Future;
use std::path::{Path, PathBuf};
use std::pin::Pin;
Expand Down Expand Up @@ -191,16 +190,6 @@ pub trait NavigatorBackend {
/// This seems highly limiting.
fn spawn_future(&mut self, future: OwnedFuture<(), Error>);

/// Resolve a relative URL.
///
/// This function must not change URLs which are already protocol, domain,
/// and path absolute. For URLs that are relative, the implementer of
/// this function may opt to convert them to absolute using an implementor
/// defined base. For a web browser, the most obvious base would be the
/// current document's base URL, while the most obvious base for a desktop
/// client would be the file-URL form of the current path.
fn resolve_relative_url<'a>(&self, url: &'a str) -> Cow<'a, str>;

/// Handle any context specific pre-processing
///
/// Changing http -> https for example. This function may alter any part of the
Expand Down Expand Up @@ -349,15 +338,6 @@ impl NavigatorBackend for NullNavigatorBackend {
self.spawner.spawn_local(future);
}

fn resolve_relative_url<'a>(&self, url: &'a str) -> Cow<'a, str> {
let relative = url_from_relative_path(&self.relative_base_path, url);
if let Ok(relative) = relative {
String::from(relative).into()
} else {
url.into()
}
}

fn pre_process_url(&self, url: Url) -> Url {
url
}
Expand Down
36 changes: 4 additions & 32 deletions core/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,7 @@ impl<'gc> Loader<'gc> {
.expect("Could not upgrade weak reference to player");

Box::pin(async move {
// clippy reports a false positive for explicitly dropped guards:
// https://github.com/rust-lang/rust-clippy/issues/6446
// A workaround for this is to wrap the `.lock()` call in a block instead of explicitly dropping the guard.
let fetch = {
let player_lock = player.lock().unwrap();
let url = player_lock.navigator().resolve_relative_url(&url);
player_lock.navigator().fetch(&url, options)
};
let fetch = player.lock().unwrap().navigator().fetch(&url, options);

let response = fetch.await.map_err(|error| {
player
Expand Down Expand Up @@ -402,14 +395,7 @@ impl<'gc> Loader<'gc> {
.expect("Could not upgrade weak reference to player");

Box::pin(async move {
// clippy reports a false positive for explicitly dropped guards:
// https://github.com/rust-lang/rust-clippy/issues/6446
// A workaround for this is to wrap the `.lock()` call in a block instead of explicitly dropping the guard.
let fetch = {
let player_lock = player.lock().unwrap();
let url = player_lock.navigator().resolve_relative_url(&url);
player_lock.navigator().fetch(&url, options)
};
let fetch = player.lock().unwrap().navigator().fetch(&url, options);

let mut replacing_root_movie = false;
player.lock().unwrap().update(|uc| -> Result<(), Error> {
Expand Down Expand Up @@ -517,14 +503,7 @@ impl<'gc> Loader<'gc> {
.expect("Could not upgrade weak reference to player");

Box::pin(async move {
// clippy reports a false positive for explicitly dropped guards:
// https://github.com/rust-lang/rust-clippy/issues/6446
// A workaround for this is to wrap the `.lock()` call in a block instead of explicitly dropping the guard.
let fetch = {
let player_lock = player.lock().unwrap();
let url = player_lock.navigator().resolve_relative_url(&url);
player_lock.navigator().fetch(&url, options)
};
let fetch = player.lock().unwrap().navigator().fetch(&url, options);

let response = fetch.await?;

Expand Down Expand Up @@ -572,14 +551,7 @@ impl<'gc> Loader<'gc> {
.expect("Could not upgrade weak reference to player");

Box::pin(async move {
// clippy reports a false positive for explicitly dropped guards:
// https://github.com/rust-lang/rust-clippy/issues/6446
// A workaround for this is to wrap the `.lock()` call in a block instead of explicitly dropping the guard.
let fetch = {
let player_lock = player.lock().unwrap();
let url = player_lock.navigator().resolve_relative_url(&url);
player_lock.navigator().fetch(&url, options)
};
let fetch = player.lock().unwrap().navigator().fetch(&url, options);

let data = fetch.await;

Expand Down
12 changes: 1 addition & 11 deletions desktop/src/navigator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use ruffle_core::backend::navigator::{
};
use ruffle_core::indexmap::IndexMap;
use ruffle_core::loader::Error;
use std::borrow::Cow;
use std::rc::Rc;
use std::sync::mpsc::Sender;
use url::Url;
Expand Down Expand Up @@ -106,7 +105,7 @@ impl NavigatorBackend for ExternalNavigatorBackend {

fn fetch(&self, url: &str, options: RequestOptions) -> OwnedFuture<Response, Error> {
// TODO: honor sandbox type (local-with-filesystem, local-with-network, remote, ...)
let full_url = match self.movie_url.clone().join(url) {
let full_url = match self.movie_url.join(url) {
Ok(url) => url,
Err(e) => {
let msg = format!("Invalid URL {}: {}", url, e);
Expand Down Expand Up @@ -202,15 +201,6 @@ impl NavigatorBackend for ExternalNavigatorBackend {
}
}

fn resolve_relative_url<'a>(&self, url: &'a str) -> Cow<'a, str> {
let relative = self.movie_url.join(url);
if let Ok(relative) = relative {
String::from(relative).into()
} else {
url.into()
}
}

fn pre_process_url(&self, mut url: Url) -> Url {
if self.upgrade_to_https && url.scheme() == "http" && url.set_scheme("https").is_err() {
log::error!("Url::set_scheme failed on: {}", url);
Expand Down
26 changes: 13 additions & 13 deletions web/src/navigator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ impl WebNavigatorBackend {
None
}
}

fn resolve_relative_url<'a>(&self, url: &'a str) -> Cow<'a, str> {
let window = web_sys::window().expect("window()");
let document = window.document().expect("document()");

if let Some(base_uri) = self.base_uri(&document) {
if let Ok(new_url) = url_from_relative_url(&base_uri, url) {
return String::from(new_url).into();
}
}

url.into()
}
}

impl NavigatorBackend for WebNavigatorBackend {
Expand Down Expand Up @@ -237,19 +250,6 @@ impl NavigatorBackend for WebNavigatorBackend {
})
}

fn resolve_relative_url<'a>(&self, url: &'a str) -> Cow<'a, str> {
let window = web_sys::window().expect("window()");
let document = window.document().expect("document()");

if let Some(base_uri) = self.base_uri(&document) {
if let Ok(new_url) = url_from_relative_url(&base_uri, url) {
return String::from(new_url).into();
}
}

url.into()
}

fn pre_process_url(&self, mut url: Url) -> Url {
if self.upgrade_to_https && url.scheme() == "http" && url.set_scheme("https").is_err() {
log::error!("Url::set_scheme failed on: {}", url);
Expand Down

0 comments on commit 87ce0f5

Please sign in to comment.