From 76daf50876d3d908c2818ed3703467576ab93dde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Mon, 16 Jan 2017 10:49:27 +0100 Subject: [PATCH] Support HTML5-routed dapps (#4173) --- dapps/src/apps/mod.rs | 1 + dapps/src/router/mod.rs | 24 ++++++++++++++++++++---- dapps/src/web.rs | 15 +++++++++++---- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/dapps/src/apps/mod.rs b/dapps/src/apps/mod.rs index f32cf9042..462576a6e 100644 --- a/dapps/src/apps/mod.rs +++ b/dapps/src/apps/mod.rs @@ -38,6 +38,7 @@ pub const RPC_PATH: &'static str = "rpc"; pub const API_PATH: &'static str = "api"; pub const UTILS_PATH: &'static str = "parity-utils"; pub const WEB_PATH: &'static str = "web"; +pub const URL_REFERER: &'static str = "__referer="; pub fn utils() -> Box { Box::new(PageEndpoint::with_prefix(parity_ui::App::default(), UTILS_PATH.to_owned())) diff --git a/dapps/src/router/mod.rs b/dapps/src/router/mod.rs index 685b957d5..ac807967f 100644 --- a/dapps/src/router/mod.rs +++ b/dapps/src/router/mod.rs @@ -56,7 +56,6 @@ pub struct Router { impl server::Handler for Router { fn on_request(&mut self, req: server::Request) -> Next { - // Choose proper handler depending on path / domain let url = handlers::extract_url(&req); let endpoint = extract_endpoint(&url); @@ -92,8 +91,7 @@ impl server::Handler for Router { self.handler = match (endpoint.0, endpoint.1, referer) { // Handle invalid web requests that we can recover from (ref path, SpecialEndpoint::None, Some((ref referer, ref referer_url))) - if is_get_request - && referer.app_id == apps::WEB_PATH + if referer.app_id == apps::WEB_PATH && self.endpoints.contains_key(apps::WEB_PATH) && !is_web_endpoint(path) => @@ -225,10 +223,28 @@ fn extract_referer_endpoint(req: &server::Request) -> Option<(Endpoi let url = referer.and_then(|referer| Url::parse(&referer.0).ok()); url.and_then(|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) -> 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) -> (Option, SpecialEndpoint) { fn special_endpoint(url: &Url) -> SpecialEndpoint { if url.path.len() <= 1 { diff --git a/dapps/src/web.rs b/dapps/src/web.rs index ac10b4d7f..377097aac 100644 --- a/dapps/src/web.rs +++ b/dapps/src/web.rs @@ -68,6 +68,7 @@ impl Endpoint for Web { struct WebInstaller { embeddable_on: Embeddable, + referer: String, } impl ContentValidator for WebInstaller { @@ -84,7 +85,12 @@ impl ContentValidator for WebInstaller { self.embeddable_on.clone(), ); if is_html { - handler.set_initial_content(&format!(r#""#, apps::UTILS_PATH)); + handler.set_initial_content(&format!( + r#""#, + apps::UTILS_PATH, + apps::URL_REFERER, + &self.referer, + )); } Ok(ValidatorResponse::Streaming(handler)) } @@ -108,7 +114,7 @@ struct WebHandler { } impl WebHandler { - fn extract_target_url(&self, url: Option) -> Result> { + fn extract_target_url(&self, url: Option) -> Result<(String, String), State> { let (path, query) = match url { Some(url) => (url.path, url.query), None => { @@ -157,7 +163,7 @@ impl WebHandler { 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 server::Handler for WebHandler { let url = extract_url(&request); // 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, Err(error) => { self.state = error; @@ -180,6 +186,7 @@ impl server::Handler for WebHandler { self.control.clone(), WebInstaller { embeddable_on: self.embeddable_on.clone(), + referer: referer, }, self.embeddable_on.clone(), self.remote.clone(),