Support HTML5-routed dapps (#4173)
This commit is contained in:
parent
1d618faaa6
commit
76daf50876
@ -38,6 +38,7 @@ pub const RPC_PATH: &'static str = "rpc";
|
|||||||
pub const API_PATH: &'static str = "api";
|
pub const API_PATH: &'static str = "api";
|
||||||
pub const UTILS_PATH: &'static str = "parity-utils";
|
pub const UTILS_PATH: &'static str = "parity-utils";
|
||||||
pub const WEB_PATH: &'static str = "web";
|
pub const WEB_PATH: &'static str = "web";
|
||||||
|
pub const URL_REFERER: &'static str = "__referer=";
|
||||||
|
|
||||||
pub fn utils() -> Box<Endpoint> {
|
pub fn utils() -> Box<Endpoint> {
|
||||||
Box::new(PageEndpoint::with_prefix(parity_ui::App::default(), UTILS_PATH.to_owned()))
|
Box::new(PageEndpoint::with_prefix(parity_ui::App::default(), UTILS_PATH.to_owned()))
|
||||||
|
@ -56,7 +56,6 @@ pub struct Router<A: Authorization + 'static> {
|
|||||||
impl<A: Authorization + 'static> server::Handler<HttpStream> for Router<A> {
|
impl<A: Authorization + 'static> server::Handler<HttpStream> for Router<A> {
|
||||||
|
|
||||||
fn on_request(&mut self, req: server::Request<HttpStream>) -> Next {
|
fn on_request(&mut self, req: server::Request<HttpStream>) -> Next {
|
||||||
|
|
||||||
// Choose proper handler depending on path / domain
|
// Choose proper handler depending on path / domain
|
||||||
let url = handlers::extract_url(&req);
|
let url = handlers::extract_url(&req);
|
||||||
let endpoint = extract_endpoint(&url);
|
let endpoint = extract_endpoint(&url);
|
||||||
@ -92,8 +91,7 @@ impl<A: Authorization + 'static> server::Handler<HttpStream> for Router<A> {
|
|||||||
self.handler = match (endpoint.0, endpoint.1, referer) {
|
self.handler = match (endpoint.0, endpoint.1, referer) {
|
||||||
// Handle invalid web requests that we can recover from
|
// Handle invalid web requests that we can recover from
|
||||||
(ref path, SpecialEndpoint::None, Some((ref referer, ref referer_url)))
|
(ref path, SpecialEndpoint::None, Some((ref referer, ref referer_url)))
|
||||||
if is_get_request
|
if referer.app_id == apps::WEB_PATH
|
||||||
&& referer.app_id == apps::WEB_PATH
|
|
||||||
&& self.endpoints.contains_key(apps::WEB_PATH)
|
&& self.endpoints.contains_key(apps::WEB_PATH)
|
||||||
&& !is_web_endpoint(path)
|
&& !is_web_endpoint(path)
|
||||||
=>
|
=>
|
||||||
@ -225,10 +223,28 @@ fn extract_referer_endpoint(req: &server::Request<HttpStream>) -> Option<(Endpoi
|
|||||||
let url = referer.and_then(|referer| Url::parse(&referer.0).ok());
|
let url = referer.and_then(|referer| Url::parse(&referer.0).ok());
|
||||||
url.and_then(|url| {
|
url.and_then(|url| {
|
||||||
let option = Some(url);
|
let option = Some(url);
|
||||||
extract_endpoint(&option).0.map(|endpoint| (endpoint, option.expect("Just wrapped; qed")))
|
extract_url_referer_endpoint(&option).or_else(|| {
|
||||||
|
extract_endpoint(&option).0.map(|endpoint| (endpoint, option.expect("Just wrapped; qed")))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn extract_url_referer_endpoint(url: &Option<Url>) -> Option<(EndpointPath, Url)> {
|
||||||
|
let query = url.as_ref().and_then(|url| url.query.as_ref());
|
||||||
|
match (url, query) {
|
||||||
|
(&Some(ref url), Some(ref query)) if query.starts_with(apps::URL_REFERER) => {
|
||||||
|
let referer_url = format!("http://{}:{}/{}", url.host, url.port, &query[apps::URL_REFERER.len()..]);
|
||||||
|
debug!(target: "dapps", "Recovering referer from query parameter: {}", referer_url);
|
||||||
|
|
||||||
|
let referer_url = Url::parse(&referer_url).ok();
|
||||||
|
extract_endpoint(&referer_url).0.map(|endpoint| {
|
||||||
|
(endpoint, referer_url.expect("Endpoint returned only when url `is_some`").clone())
|
||||||
|
})
|
||||||
|
},
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn extract_endpoint(url: &Option<Url>) -> (Option<EndpointPath>, SpecialEndpoint) {
|
fn extract_endpoint(url: &Option<Url>) -> (Option<EndpointPath>, SpecialEndpoint) {
|
||||||
fn special_endpoint(url: &Url) -> SpecialEndpoint {
|
fn special_endpoint(url: &Url) -> SpecialEndpoint {
|
||||||
if url.path.len() <= 1 {
|
if url.path.len() <= 1 {
|
||||||
|
@ -68,6 +68,7 @@ impl<F: Fetch> Endpoint for Web<F> {
|
|||||||
|
|
||||||
struct WebInstaller {
|
struct WebInstaller {
|
||||||
embeddable_on: Embeddable,
|
embeddable_on: Embeddable,
|
||||||
|
referer: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ContentValidator for WebInstaller {
|
impl ContentValidator for WebInstaller {
|
||||||
@ -84,7 +85,12 @@ impl ContentValidator for WebInstaller {
|
|||||||
self.embeddable_on.clone(),
|
self.embeddable_on.clone(),
|
||||||
);
|
);
|
||||||
if is_html {
|
if is_html {
|
||||||
handler.set_initial_content(&format!(r#"<script src="/{}/inject.js"></script>"#, apps::UTILS_PATH));
|
handler.set_initial_content(&format!(
|
||||||
|
r#"<script src="/{}/inject.js"></script><script>history.replaceState({{}}, "", "/?{}{}")</script>"#,
|
||||||
|
apps::UTILS_PATH,
|
||||||
|
apps::URL_REFERER,
|
||||||
|
&self.referer,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
Ok(ValidatorResponse::Streaming(handler))
|
Ok(ValidatorResponse::Streaming(handler))
|
||||||
}
|
}
|
||||||
@ -108,7 +114,7 @@ struct WebHandler<F: Fetch> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Fetch> WebHandler<F> {
|
impl<F: Fetch> WebHandler<F> {
|
||||||
fn extract_target_url(&self, url: Option<Url>) -> Result<String, State<F>> {
|
fn extract_target_url(&self, url: Option<Url>) -> Result<(String, String), State<F>> {
|
||||||
let (path, query) = match url {
|
let (path, query) = match url {
|
||||||
Some(url) => (url.path, url.query),
|
Some(url) => (url.path, url.query),
|
||||||
None => {
|
None => {
|
||||||
@ -157,7 +163,7 @@ impl<F: Fetch> WebHandler<F> {
|
|||||||
None => "".into(),
|
None => "".into(),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(format!("{}://{}{}", protocol, path[idx + 2..].join("/"), query))
|
Ok((format!("{}://{}{}", protocol, path[idx + 2..].join("/"), query), path[0..].join("/")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -166,7 +172,7 @@ impl<F: Fetch> server::Handler<net::HttpStream> for WebHandler<F> {
|
|||||||
let url = extract_url(&request);
|
let url = extract_url(&request);
|
||||||
|
|
||||||
// First extract the URL (reject invalid URLs)
|
// First extract the URL (reject invalid URLs)
|
||||||
let target_url = match self.extract_target_url(url) {
|
let (target_url, referer) = match self.extract_target_url(url) {
|
||||||
Ok(url) => url,
|
Ok(url) => url,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
self.state = error;
|
self.state = error;
|
||||||
@ -180,6 +186,7 @@ impl<F: Fetch> server::Handler<net::HttpStream> for WebHandler<F> {
|
|||||||
self.control.clone(),
|
self.control.clone(),
|
||||||
WebInstaller {
|
WebInstaller {
|
||||||
embeddable_on: self.embeddable_on.clone(),
|
embeddable_on: self.embeddable_on.clone(),
|
||||||
|
referer: referer,
|
||||||
},
|
},
|
||||||
self.embeddable_on.clone(),
|
self.embeddable_on.clone(),
|
||||||
self.remote.clone(),
|
self.remote.clone(),
|
||||||
|
Loading…
Reference in New Issue
Block a user