2019-09-01 19:24:04 +00:00
|
|
|
//! Navigator backend for web
|
2020-01-12 22:06:27 +00:00
|
|
|
use js_sys::{Array, ArrayBuffer, Uint8Array};
|
2020-01-18 04:11:09 +00:00
|
|
|
use ruffle_core::backend::navigator::{
|
2022-08-12 15:21:49 +00:00
|
|
|
NavigationMethod, NavigatorBackend, OwnedFuture, Request, Response,
|
2020-01-18 04:11:09 +00:00
|
|
|
};
|
2020-08-28 17:16:13 +00:00
|
|
|
use ruffle_core::indexmap::IndexMap;
|
2020-06-18 08:36:04 +00:00
|
|
|
use ruffle_core::loader::Error;
|
2020-07-23 04:00:41 +00:00
|
|
|
use std::borrow::Cow;
|
2020-12-11 11:41:44 +00:00
|
|
|
use url::Url;
|
2019-09-04 02:15:03 +00:00
|
|
|
use wasm_bindgen::JsCast;
|
2019-11-09 02:10:21 +00:00
|
|
|
use wasm_bindgen_futures::{spawn_local, JsFuture};
|
2022-04-08 12:40:11 +00:00
|
|
|
use web_sys::{
|
2022-08-12 15:21:49 +00:00
|
|
|
window, Blob, BlobPropertyBag, Request as WebRequest, RequestInit, Response as WebResponse,
|
2022-04-08 12:40:11 +00:00
|
|
|
};
|
2019-09-01 19:24:04 +00:00
|
|
|
|
2020-03-29 05:28:07 +00:00
|
|
|
pub struct WebNavigatorBackend {
|
2021-01-11 09:35:45 +00:00
|
|
|
allow_script_access: bool,
|
2020-12-11 11:41:44 +00:00
|
|
|
upgrade_to_https: bool,
|
2022-08-12 15:21:49 +00:00
|
|
|
base_url: Option<Url>,
|
2020-03-29 05:28:07 +00:00
|
|
|
}
|
2019-09-01 19:24:04 +00:00
|
|
|
|
|
|
|
impl WebNavigatorBackend {
|
2021-08-08 20:38:55 +00:00
|
|
|
pub fn new(
|
|
|
|
allow_script_access: bool,
|
|
|
|
upgrade_to_https: bool,
|
2022-08-12 15:21:49 +00:00
|
|
|
base_url: Option<String>,
|
2021-08-08 20:38:55 +00:00
|
|
|
) -> Self {
|
2020-03-29 05:28:07 +00:00
|
|
|
let window = web_sys::window().expect("window()");
|
|
|
|
|
2021-09-12 15:00:36 +00:00
|
|
|
// Upgrade to HTTPS takes effect if the current page is hosted on HTTPS.
|
2021-01-06 09:08:47 +00:00
|
|
|
let upgrade_to_https =
|
2022-08-12 15:21:49 +00:00
|
|
|
upgrade_to_https && window.location().protocol().expect("protocol()") == "https:";
|
2021-01-06 09:08:47 +00:00
|
|
|
|
2022-08-12 15:21:49 +00:00
|
|
|
// Retrieve and parse `document.baseURI`.
|
|
|
|
let document_base_uri = || {
|
|
|
|
let document = window.document().expect("document()");
|
|
|
|
if let Ok(Some(base_uri)) = document.base_uri() {
|
|
|
|
return Url::parse(&base_uri).ok();
|
2021-09-12 15:00:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 15:21:49 +00:00
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let base_url = if let Some(mut base_url) = base_url {
|
|
|
|
// Adding trailing slash so `Url::parse` will not drop the last part.
|
|
|
|
if !base_url.ends_with('/') {
|
|
|
|
base_url.push('/');
|
2021-09-07 04:44:24 +00:00
|
|
|
}
|
2022-08-12 15:21:49 +00:00
|
|
|
|
|
|
|
Url::parse(&base_url)
|
|
|
|
.ok()
|
|
|
|
.or_else(|| document_base_uri().and_then(|base_uri| base_uri.join(&base_url).ok()))
|
|
|
|
} else {
|
|
|
|
document_base_uri()
|
|
|
|
};
|
|
|
|
|
|
|
|
if base_url.is_none() {
|
|
|
|
log::error!("Could not get base URL for base directory inference.");
|
2021-09-07 04:44:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 14:09:26 +00:00
|
|
|
Self {
|
2021-01-11 09:35:45 +00:00
|
|
|
allow_script_access,
|
2020-12-11 11:41:44 +00:00
|
|
|
upgrade_to_https,
|
2021-08-08 20:38:55 +00:00
|
|
|
base_url,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-12 15:21:49 +00:00
|
|
|
fn resolve_url<'a>(&self, url: &'a str) -> Cow<'a, str> {
|
|
|
|
if let Some(base_url) = &self.base_url {
|
|
|
|
if let Ok(url) = base_url.join(url) {
|
|
|
|
return self.pre_process_url(url).to_string().into();
|
2022-04-08 13:36:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
url.into()
|
|
|
|
}
|
2019-09-01 19:24:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NavigatorBackend for WebNavigatorBackend {
|
2019-09-17 03:37:11 +00:00
|
|
|
fn navigate_to_url(
|
|
|
|
&self,
|
|
|
|
url: String,
|
2022-08-05 13:29:12 +00:00
|
|
|
target: String,
|
2020-08-28 17:16:13 +00:00
|
|
|
vars_method: Option<(NavigationMethod, IndexMap<String, String>)>,
|
2019-09-17 03:37:11 +00:00
|
|
|
) {
|
2022-08-12 15:21:49 +00:00
|
|
|
// If the URL is empty, ignore the request.
|
2021-03-21 06:02:57 +00:00
|
|
|
if url.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-07 08:24:48 +00:00
|
|
|
|
2022-08-12 15:21:49 +00:00
|
|
|
let url = self.resolve_url(&url);
|
2021-02-07 08:24:48 +00:00
|
|
|
|
2022-08-12 15:21:49 +00:00
|
|
|
// If `allowScriptAccess` is disabled, reject the `javascript:` scheme.
|
|
|
|
if let Ok(url) = Url::parse(&url) {
|
2021-03-21 06:02:57 +00:00
|
|
|
if !self.allow_script_access && url.scheme() == "javascript" {
|
|
|
|
log::warn!("SWF tried to run a script, but script access is not allowed");
|
|
|
|
return;
|
|
|
|
}
|
2022-08-12 15:21:49 +00:00
|
|
|
}
|
2021-02-07 08:24:48 +00:00
|
|
|
|
2022-08-12 15:21:49 +00:00
|
|
|
// TODO: Should we return a result for failed opens? Does Flash care?
|
|
|
|
let window = window().expect("window()");
|
|
|
|
match vars_method {
|
|
|
|
Some((navmethod, formvars)) => {
|
|
|
|
let document = window.document().expect("document()");
|
|
|
|
let body = match document.body() {
|
|
|
|
Some(body) => body,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
let form = document
|
|
|
|
.create_element("form")
|
|
|
|
.unwrap()
|
|
|
|
.dyn_into::<web_sys::HtmlFormElement>()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let _ = form.set_attribute(
|
|
|
|
"method",
|
|
|
|
match navmethod {
|
|
|
|
NavigationMethod::Get => "get",
|
|
|
|
NavigationMethod::Post => "post",
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
let _ = form.set_attribute("action", &url);
|
|
|
|
|
|
|
|
if !target.is_empty() {
|
|
|
|
let _ = form.set_attribute("target", &target);
|
|
|
|
}
|
2021-01-10 14:04:31 +00:00
|
|
|
|
2022-08-12 15:21:49 +00:00
|
|
|
for (k, v) in formvars.iter() {
|
|
|
|
let hidden = document.create_element("input").unwrap();
|
2021-01-10 14:04:31 +00:00
|
|
|
|
2022-08-12 15:21:49 +00:00
|
|
|
let _ = hidden.set_attribute("type", "hidden");
|
|
|
|
let _ = hidden.set_attribute("name", k);
|
|
|
|
let _ = hidden.set_attribute("value", v);
|
2021-02-07 08:24:48 +00:00
|
|
|
|
2022-08-12 15:21:49 +00:00
|
|
|
let _ = form.append_child(&hidden);
|
2021-02-07 08:24:48 +00:00
|
|
|
}
|
2022-08-12 15:21:49 +00:00
|
|
|
|
|
|
|
let _ = body.append_child(&form);
|
|
|
|
let _ = form.submit();
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
if target.is_empty() {
|
|
|
|
let _ = window.location().assign(&url);
|
|
|
|
} else {
|
|
|
|
let _ = window.open_with_url_and_target(&url, &target);
|
2021-02-07 08:24:48 +00:00
|
|
|
}
|
2022-08-12 15:21:49 +00:00
|
|
|
}
|
|
|
|
};
|
2021-01-10 14:04:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 08:55:17 +00:00
|
|
|
fn fetch(&self, request: Request) -> OwnedFuture<Response, Error> {
|
2022-08-12 15:21:49 +00:00
|
|
|
let url = self.resolve_url(request.url()).into_owned();
|
2020-12-11 11:41:44 +00:00
|
|
|
|
2019-11-09 02:10:21 +00:00
|
|
|
Box::pin(async move {
|
2020-01-12 22:06:27 +00:00
|
|
|
let mut init = RequestInit::new();
|
|
|
|
|
2022-06-11 08:55:17 +00:00
|
|
|
init.method(match request.method() {
|
2021-02-12 13:03:17 +00:00
|
|
|
NavigationMethod::Get => "GET",
|
|
|
|
NavigationMethod::Post => "POST",
|
2020-01-12 22:06:27 +00:00
|
|
|
});
|
|
|
|
|
2022-06-11 08:55:17 +00:00
|
|
|
if let Some((data, mime)) = request.body() {
|
2020-01-12 22:06:27 +00:00
|
|
|
let arraydata = ArrayBuffer::new(data.len() as u32);
|
|
|
|
let u8data = Uint8Array::new(&arraydata);
|
|
|
|
|
|
|
|
for (i, byte) in data.iter().enumerate() {
|
|
|
|
u8data.fill(*byte, i as u32, i as u32 + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let blobparts = Array::new();
|
|
|
|
blobparts.push(&arraydata);
|
|
|
|
|
|
|
|
let mut blobprops = BlobPropertyBag::new();
|
|
|
|
blobprops.type_(mime);
|
|
|
|
|
|
|
|
let datablob =
|
|
|
|
Blob::new_with_buffer_source_sequence_and_options(&blobparts, &blobprops)
|
|
|
|
.unwrap()
|
|
|
|
.dyn_into()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
init.body(Some(&datablob));
|
|
|
|
}
|
|
|
|
|
2022-06-11 08:55:17 +00:00
|
|
|
let request = WebRequest::new_with_str_and_init(&url, &init)
|
2022-10-26 23:46:09 +00:00
|
|
|
.map_err(|_| Error::FetchError(format!("Unable to create request for {url}")))?;
|
2020-01-12 22:06:27 +00:00
|
|
|
|
2022-08-12 15:21:49 +00:00
|
|
|
let window = web_sys::window().expect("window()");
|
2022-03-17 23:38:31 +00:00
|
|
|
let fetchval = JsFuture::from(window.fetch_with_request(&request))
|
|
|
|
.await
|
|
|
|
.map_err(|_| Error::FetchError("Got JS error".to_string()))?;
|
2021-03-27 19:43:43 +00:00
|
|
|
|
2022-04-08 12:40:11 +00:00
|
|
|
let response: WebResponse = fetchval.dyn_into().unwrap();
|
|
|
|
if !response.ok() {
|
2021-03-27 19:43:43 +00:00
|
|
|
return Err(Error::FetchError(format!(
|
|
|
|
"HTTP status is not ok, got {}",
|
2022-04-08 12:40:11 +00:00
|
|
|
response.status_text()
|
2021-03-27 19:43:43 +00:00
|
|
|
)));
|
2021-03-26 01:07:50 +00:00
|
|
|
}
|
2021-03-27 19:43:43 +00:00
|
|
|
|
2022-04-08 13:08:13 +00:00
|
|
|
let url = response.url();
|
|
|
|
|
2022-04-08 12:40:11 +00:00
|
|
|
let body: ArrayBuffer = JsFuture::from(response.array_buffer().unwrap())
|
2019-11-09 02:10:21 +00:00
|
|
|
.await
|
2021-08-25 10:32:55 +00:00
|
|
|
.map_err(|_| {
|
|
|
|
Error::FetchError("Could not allocate array buffer for response".to_string())
|
|
|
|
})?
|
2019-11-09 02:10:21 +00:00
|
|
|
.dyn_into()
|
|
|
|
.unwrap();
|
2022-04-08 12:40:11 +00:00
|
|
|
let body = Uint8Array::new(&body).to_vec();
|
2021-08-25 10:32:55 +00:00
|
|
|
|
2022-04-08 13:08:13 +00:00
|
|
|
Ok(Response { url, body })
|
2019-11-09 02:10:21 +00:00
|
|
|
})
|
2019-11-07 19:34:38 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 08:36:04 +00:00
|
|
|
fn spawn_future(&mut self, future: OwnedFuture<(), Error>) {
|
2019-11-14 19:52:13 +00:00
|
|
|
spawn_local(async move {
|
|
|
|
if let Err(e) = future.await {
|
2020-09-19 14:27:24 +00:00
|
|
|
log::error!("Asynchronous error occurred: {}", e);
|
2019-11-14 19:52:13 +00:00
|
|
|
}
|
|
|
|
})
|
2019-11-07 19:34:38 +00:00
|
|
|
}
|
2020-07-23 04:00:41 +00:00
|
|
|
|
2020-12-11 11:41:44 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
url
|
|
|
|
}
|
2019-09-17 03:37:11 +00:00
|
|
|
}
|