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::{
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
async_return, create_fetch_error, create_specific_fetch_error, ErrorResponse, NavigationMethod,
|
|
|
|
NavigatorBackend, OpenURLMode, OwnedFuture, Request, SuccessResponse,
|
2020-01-18 04:11:09 +00:00
|
|
|
};
|
2023-04-12 22:58:56 +00:00
|
|
|
use ruffle_core::config::NetworkingAccessMode;
|
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;
|
2023-07-15 15:20:21 +00:00
|
|
|
use ruffle_core::socket::{SocketAction, SocketHandle};
|
2023-07-15 14:55:20 +00:00
|
|
|
use std::sync::mpsc::{Receiver, Sender};
|
2023-07-15 15:20:21 +00:00
|
|
|
use std::sync::Arc;
|
2023-03-03 03:10:12 +00:00
|
|
|
use tracing_subscriber::layer::Layered;
|
|
|
|
use tracing_subscriber::Registry;
|
|
|
|
use tracing_wasm::WASMLayer;
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
use url::{ParseError, 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::{
|
2023-04-22 19:23:24 +00:00
|
|
|
window, Blob, BlobPropertyBag, HtmlFormElement, HtmlInputElement, 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 {
|
2023-03-03 03:10:12 +00:00
|
|
|
log_subscriber: Arc<Layered<WASMLayer, Registry>>,
|
2021-01-11 09:35:45 +00:00
|
|
|
allow_script_access: bool,
|
2023-04-12 22:58:56 +00:00
|
|
|
allow_networking: NetworkingAccessMode,
|
2020-12-11 11:41:44 +00:00
|
|
|
upgrade_to_https: bool,
|
2022-08-12 15:21:49 +00:00
|
|
|
base_url: Option<Url>,
|
2023-04-12 22:58:56 +00:00
|
|
|
open_url_mode: OpenURLMode,
|
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,
|
2023-04-12 22:58:56 +00:00
|
|
|
allow_networking: NetworkingAccessMode,
|
2021-08-08 20:38:55 +00:00
|
|
|
upgrade_to_https: bool,
|
2022-08-12 15:21:49 +00:00
|
|
|
base_url: Option<String>,
|
2023-03-03 03:10:12 +00:00
|
|
|
log_subscriber: Arc<Layered<WASMLayer, Registry>>,
|
2023-04-12 22:58:56 +00:00
|
|
|
open_url_mode: OpenURLMode,
|
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() {
|
2023-01-04 11:33:10 +00:00
|
|
|
tracing::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,
|
2023-04-11 23:32:10 +00:00
|
|
|
allow_networking,
|
2020-12-11 11:41:44 +00:00
|
|
|
upgrade_to_https,
|
2021-08-08 20:38:55 +00:00
|
|
|
base_url,
|
2023-03-03 03:10:12 +00:00
|
|
|
log_subscriber,
|
2023-04-12 22:58:56 +00:00
|
|
|
open_url_mode,
|
2021-08-08 20:38:55 +00:00
|
|
|
}
|
|
|
|
}
|
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,
|
2023-04-22 14:07:37 +00:00
|
|
|
url: &str,
|
|
|
|
target: &str,
|
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
|
|
|
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
let url = match self.resolve_url(url) {
|
|
|
|
Ok(url) => {
|
|
|
|
if url.scheme() == "file" {
|
|
|
|
tracing::error!(
|
|
|
|
"Can't open the local URL {} on WASM target",
|
|
|
|
url.to_string()
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!(
|
|
|
|
"Could not parse URL because of {}, the corrupt URL was: {}",
|
|
|
|
e,
|
|
|
|
url
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2021-02-07 08:24:48 +00:00
|
|
|
|
2023-06-15 11:17:29 +00:00
|
|
|
// If `allowNetworking` is set to `internal` or `none`, block all `navigate_to_url` calls.
|
2023-04-12 22:58:56 +00:00
|
|
|
if self.allow_networking != NetworkingAccessMode::All {
|
2023-04-11 23:32:10 +00:00
|
|
|
tracing::warn!("SWF tried to open a URL, but opening URLs is not allowed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-12 15:21:49 +00:00
|
|
|
// If `allowScriptAccess` is disabled, reject the `javascript:` scheme.
|
2023-06-15 11:17:29 +00:00
|
|
|
// Also reject any attempt to open a URL when `target` is a keyword that affects the current tab.
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
if !self.allow_script_access {
|
|
|
|
if url.scheme() == "javascript" {
|
|
|
|
tracing::warn!("SWF tried to run a script, but script access is not allowed");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
match target.to_lowercase().as_str() {
|
|
|
|
"_parent" | "_self" | "_top" | "" => {
|
|
|
|
tracing::warn!("SWF tried to open a URL, but opening URLs in the current tab is prevented by script access");
|
|
|
|
return;
|
2023-06-15 11:17:29 +00:00
|
|
|
}
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
_ => (),
|
2023-06-15 11:17:29 +00:00
|
|
|
}
|
2021-03-21 06:02:57 +00:00
|
|
|
}
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
}
|
2023-04-09 16:27:20 +00:00
|
|
|
|
|
|
|
let window = window().expect("window()");
|
|
|
|
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
if url.scheme() != "javascript" {
|
2023-04-12 22:58:56 +00:00
|
|
|
if self.open_url_mode == OpenURLMode::Confirm {
|
2023-05-02 13:52:37 +00:00
|
|
|
let message = format!("The SWF file wants to open the website {}", &url);
|
2023-04-12 22:58:56 +00:00
|
|
|
// TODO: Add a checkbox with a GUI toolkit
|
2023-04-09 16:27:20 +00:00
|
|
|
let confirm = window
|
|
|
|
.confirm_with_message(&message)
|
|
|
|
.expect("confirm_with_message()");
|
|
|
|
if !confirm {
|
|
|
|
tracing::info!(
|
|
|
|
"SWF tried to open a website, but the user declined the request"
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2023-04-12 22:58:56 +00:00
|
|
|
} else if self.open_url_mode == OpenURLMode::Deny {
|
2023-04-09 16:27:20 +00:00
|
|
|
tracing::warn!("SWF tried to open a website, but opening a website is not allowed");
|
|
|
|
return;
|
|
|
|
}
|
2023-06-15 11:17:29 +00:00
|
|
|
// If the user confirmed or if in `Allow` mode, open the website.
|
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?
|
|
|
|
match vars_method {
|
|
|
|
Some((navmethod, formvars)) => {
|
|
|
|
let document = window.document().expect("document()");
|
|
|
|
let body = match document.body() {
|
|
|
|
Some(body) => body,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
|
2023-04-22 19:23:24 +00:00
|
|
|
let form: HtmlFormElement = document
|
2022-08-12 15:21:49 +00:00
|
|
|
.create_element("form")
|
2022-12-30 23:08:35 +00:00
|
|
|
.expect("create_element() must succeed")
|
2023-04-22 19:23:24 +00:00
|
|
|
.dyn_into()
|
|
|
|
.expect("create_element(\"form\") didn't give us a form");
|
2022-08-12 15:21:49 +00:00
|
|
|
|
2023-04-22 19:23:24 +00:00
|
|
|
form.set_method(&navmethod.to_string());
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
form.set_action(url.as_str());
|
2022-08-12 15:21:49 +00:00
|
|
|
|
|
|
|
if !target.is_empty() {
|
2023-04-22 19:23:24 +00:00
|
|
|
form.set_target(target);
|
2022-08-12 15:21:49 +00:00
|
|
|
}
|
2021-01-10 14:04:31 +00:00
|
|
|
|
2023-04-22 19:23:24 +00:00
|
|
|
for (key, value) in formvars {
|
|
|
|
let hidden: HtmlInputElement = document
|
2022-12-30 23:08:35 +00:00
|
|
|
.create_element("input")
|
2023-04-22 19:23:24 +00:00
|
|
|
.expect("create_element() must succeed")
|
|
|
|
.dyn_into()
|
|
|
|
.expect("create_element(\"input\") didn't give us an input");
|
2021-01-10 14:04:31 +00:00
|
|
|
|
2023-04-22 19:23:24 +00:00
|
|
|
hidden.set_type("hidden");
|
|
|
|
hidden.set_name(&key);
|
|
|
|
hidden.set_value(&value);
|
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() {
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
let _ = window.location().assign(url.as_str());
|
2022-08-12 15:21:49 +00:00
|
|
|
} else {
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
let _ = window.open_with_url_and_target(url.as_str(), 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
|
|
|
}
|
|
|
|
|
2023-05-07 19:24:36 +00:00
|
|
|
fn fetch(&self, request: Request) -> OwnedFuture<SuccessResponse, ErrorResponse> {
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
let url = match self.resolve_url(request.url()) {
|
|
|
|
Ok(url) => {
|
|
|
|
if url.scheme() == "file" {
|
|
|
|
return async_return(create_specific_fetch_error(
|
|
|
|
"WASM target can't fetch local URL",
|
|
|
|
url.as_str(),
|
|
|
|
"",
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
return async_return(create_fetch_error(request.url(), e));
|
|
|
|
}
|
|
|
|
};
|
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();
|
|
|
|
|
2023-04-22 14:08:10 +00:00
|
|
|
init.method(&request.method().to_string());
|
2020-01-12 22:06:27 +00:00
|
|
|
|
2022-06-11 08:55:17 +00:00
|
|
|
if let Some((data, mime)) = request.body() {
|
2023-04-22 14:08:35 +00:00
|
|
|
let blob = Blob::new_with_buffer_source_sequence_and_options(
|
|
|
|
&Array::from_iter([Uint8Array::from(data.as_slice()).buffer()]),
|
|
|
|
BlobPropertyBag::new().type_(mime),
|
|
|
|
)
|
2023-05-07 19:24:36 +00:00
|
|
|
.map_err(|_| ErrorResponse {
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
url: url.to_string(),
|
2023-05-07 19:24:36 +00:00
|
|
|
error: Error::FetchError("Got JS error".to_string()),
|
|
|
|
})?
|
2023-04-22 14:08:35 +00:00
|
|
|
.dyn_into()
|
2023-05-07 19:24:36 +00:00
|
|
|
.map_err(|_| ErrorResponse {
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
url: url.to_string(),
|
2023-05-07 19:24:36 +00:00
|
|
|
error: Error::FetchError("Got JS error".to_string()),
|
|
|
|
})?;
|
2023-04-22 14:08:35 +00:00
|
|
|
|
|
|
|
init.body(Some(&blob));
|
2020-01-12 22:06:27 +00:00
|
|
|
}
|
|
|
|
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
let web_request = match WebRequest::new_with_str_and_init(url.as_str(), &init) {
|
|
|
|
Ok(web_request) => web_request,
|
|
|
|
Err(_) => {
|
|
|
|
return create_specific_fetch_error(
|
|
|
|
"Unable to create request for",
|
|
|
|
url.as_str(),
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
2020-01-12 22:06:27 +00:00
|
|
|
|
2023-04-28 18:01:46 +00:00
|
|
|
let headers = web_request.headers();
|
|
|
|
|
|
|
|
for (header_name, header_val) in request.headers() {
|
|
|
|
headers
|
|
|
|
.set(header_name, header_val)
|
2023-05-07 19:24:36 +00:00
|
|
|
.map_err(|_| ErrorResponse {
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
url: url.to_string(),
|
2023-05-07 19:24:36 +00:00
|
|
|
error: Error::FetchError("Got JS error".to_string()),
|
|
|
|
})?;
|
2023-04-28 18:01:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 15:21:49 +00:00
|
|
|
let window = web_sys::window().expect("window()");
|
2023-04-28 18:01:46 +00:00
|
|
|
let fetchval = JsFuture::from(window.fetch_with_request(&web_request))
|
2022-03-17 23:38:31 +00:00
|
|
|
.await
|
2023-05-07 19:24:36 +00:00
|
|
|
.map_err(|_| ErrorResponse {
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
url: url.to_string(),
|
2023-05-07 19:24:36 +00:00
|
|
|
error: Error::FetchError("Got JS error".to_string()),
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let response: WebResponse = fetchval.dyn_into().map_err(|_| ErrorResponse {
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
url: url.to_string(),
|
2023-05-07 19:24:36 +00:00
|
|
|
error: Error::FetchError("Fetch result wasn't a WebResponse".to_string()),
|
|
|
|
})?;
|
|
|
|
let url = response.url();
|
2023-06-10 03:06:26 +00:00
|
|
|
let status = response.status();
|
|
|
|
let redirected = response.redirected();
|
2022-04-08 12:40:11 +00:00
|
|
|
if !response.ok() {
|
2023-05-07 19:24:36 +00:00
|
|
|
let error = Error::HttpNotOk(
|
2023-06-10 03:06:26 +00:00
|
|
|
format!("HTTP status is not ok, got {}", response.status_text()),
|
|
|
|
status,
|
|
|
|
redirected,
|
2023-05-07 19:24:36 +00:00
|
|
|
);
|
|
|
|
return Err(ErrorResponse { url, error });
|
2021-03-26 01:07:50 +00:00
|
|
|
}
|
2021-03-27 19:43:43 +00:00
|
|
|
|
2023-05-07 19:24:36 +00:00
|
|
|
let body: ArrayBuffer = JsFuture::from(response.array_buffer().map_err(|_| {
|
|
|
|
ErrorResponse {
|
|
|
|
url: url.clone(),
|
|
|
|
error: Error::FetchError("Got JS error".to_string()),
|
|
|
|
}
|
|
|
|
})?)
|
2022-12-30 23:08:35 +00:00
|
|
|
.await
|
2023-05-07 19:24:36 +00:00
|
|
|
.map_err(|_| ErrorResponse {
|
|
|
|
url: url.clone(),
|
|
|
|
error: Error::FetchError(
|
|
|
|
"Could not allocate array buffer for response".to_string(),
|
|
|
|
),
|
2022-12-30 23:08:35 +00:00
|
|
|
})?
|
|
|
|
.dyn_into()
|
2023-05-07 19:24:36 +00:00
|
|
|
.map_err(|_| ErrorResponse {
|
|
|
|
url: url.clone(),
|
|
|
|
error: Error::FetchError("array_buffer result wasn't an ArrayBuffer".to_string()),
|
2022-12-30 23:08:35 +00:00
|
|
|
})?;
|
2022-04-08 12:40:11 +00:00
|
|
|
let body = Uint8Array::new(&body).to_vec();
|
2021-08-25 10:32:55 +00:00
|
|
|
|
2023-05-07 19:24:36 +00:00
|
|
|
Ok(SuccessResponse {
|
2023-06-10 03:06:26 +00:00
|
|
|
url,
|
|
|
|
body,
|
|
|
|
status,
|
|
|
|
redirected,
|
|
|
|
})
|
2019-11-09 02:10:21 +00:00
|
|
|
})
|
2019-11-07 19:34:38 +00:00
|
|
|
}
|
|
|
|
|
core: Refactor & improve NavigatorBackend implementations
All NavigatorBackend implementations have been refactored, resulting in
improved code quality, less duplicated code, more consistent and easier
to understand procedures, additional error handling and better error
messages.
A resolve_url method has been added to the NavigatorBackend trait. It
takes a URL and and resolves it to the actual URL from which a file can
be fetched (including handling of relative links and pre-processing). It
has been implemented in each NavigatorBackend implementation.
Duplicated code has been put into new public functions in
core/src/backend/navigator.rs which are called by all NavigatorBackend
implementations.
ExternalNavigatorBackend:
- The navigate_to_url and fetch methods have been adapted to use
resolve_url, removing redundant code.
- Error handling has been added in case that the URL can't be converted
to a PathBuf.
- A TODO about differences between the flash player fetch and the
Ruffle fetch implementation has been added.
WebNavigatorBackend:
- The previous resolve_url method exclusively to the WebNavigatorBackend
has been replaced by the new resolve_url method. It is used by
navigate_to_url and fetch.
- resolve_url now always pre-processes the URL if it's valid (even if no
base_url exists) and explicitly returns whether the URL can be parsed.
- navigate_to_url now traces an explanatory error each if the URL can't
be parsed or is local.
- fetch now returns an explanatory error each if the URL can't be parsed
or is local (previously, a vague "Got JS error" has been returned).
TestNavigatorBackend & NullNavigatorBackend:
- fetch pre-processes the URL now (using the resolve_url implementation).
- If the URL isn't local, an explanatory error is returned (previously,
it was just an "Invalid URL" error).
- If the URL can't be parsed, an explanatory error with the reason is
returned (previously, it was just an "Invalid URL" error).
Additionally, error messages in all NavigatorBackend implementations
have been improved and made more consistent, e.g. if a local file can't
be read.
2023-05-22 15:01:50 +00:00
|
|
|
fn resolve_url(&self, url: &str) -> Result<Url, ParseError> {
|
|
|
|
if let Some(base_url) = &self.base_url {
|
|
|
|
match base_url.join(url) {
|
|
|
|
Ok(full_url) => Ok(self.pre_process_url(full_url)),
|
|
|
|
Err(error) => Err(error),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match Url::parse(url) {
|
|
|
|
Ok(parsed_url) => Ok(self.pre_process_url(parsed_url)),
|
|
|
|
Err(error) => Err(error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 08:36:04 +00:00
|
|
|
fn spawn_future(&mut self, future: OwnedFuture<(), Error>) {
|
2023-03-03 03:10:12 +00:00
|
|
|
let subscriber = self.log_subscriber.clone();
|
2019-11-14 19:52:13 +00:00
|
|
|
spawn_local(async move {
|
2023-03-03 03:10:12 +00:00
|
|
|
let _subscriber = tracing::subscriber::set_default(subscriber);
|
2019-11-14 19:52:13 +00:00
|
|
|
if let Err(e) = future.await {
|
2023-01-04 11:33:10 +00:00
|
|
|
tracing::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() {
|
2023-01-04 11:33:10 +00:00
|
|
|
tracing::error!("Url::set_scheme failed on: {}", url);
|
2020-12-11 11:41:44 +00:00
|
|
|
}
|
|
|
|
url
|
|
|
|
}
|
2023-07-14 22:15:16 +00:00
|
|
|
|
2023-07-15 14:55:20 +00:00
|
|
|
fn connect_socket(
|
|
|
|
&mut self,
|
|
|
|
_host: String,
|
|
|
|
_port: u16,
|
|
|
|
handle: SocketHandle,
|
|
|
|
_receiver: Receiver<Vec<u8>>,
|
|
|
|
sender: Sender<SocketAction>,
|
|
|
|
) {
|
2023-07-14 22:15:16 +00:00
|
|
|
// FIXME: Add way to call out to JS code.
|
2023-07-15 14:55:20 +00:00
|
|
|
sender
|
|
|
|
.send(SocketAction::Connect(handle, false))
|
|
|
|
.expect("working channel send");
|
2023-07-14 22:15:16 +00:00
|
|
|
}
|
2019-09-17 03:37:11 +00:00
|
|
|
}
|