From e251fd49a11a1023276c223399dc9342ef3ed27a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Thu, 3 Nov 2016 12:22:28 +0100 Subject: [PATCH] Dapps errors embeddable on signer (#3115) --- dapps/src/apps/fetcher.rs | 18 +++++++++++------- dapps/src/handlers/content.rs | 6 +----- dapps/src/handlers/fetch.rs | 15 ++++++++++++--- dapps/src/page/handler.rs | 2 +- dapps/src/router/auth.rs | 3 ++- dapps/src/router/host_validation.rs | 3 ++- dapps/src/router/mod.rs | 2 ++ dapps/src/tests/fetch.rs | 6 +++--- dapps/src/tests/redirection.rs | 6 +++--- 9 files changed, 37 insertions(+), 24 deletions(-) diff --git a/dapps/src/apps/fetcher.rs b/dapps/src/apps/fetcher.rs index e4ee63fea..6c2e5416d 100644 --- a/dapps/src/apps/fetcher.rs +++ b/dapps/src/apps/fetcher.rs @@ -71,12 +71,13 @@ impl ContentFetcher { } } - fn still_syncing() -> Box { + fn still_syncing(port: Option) -> Box { Box::new(ContentHandler::error( StatusCode::ServiceUnavailable, "Sync In Progress", "Your node is still syncing. We cannot resolve any content before it's fully synced.", - Some("Refresh") + Some("Refresh"), + port, )) } @@ -143,7 +144,7 @@ impl ContentFetcher { match content { // Don't serve dapps if we are still syncing (but serve content) Some(URLHintResult::Dapp(_)) if self.sync.is_major_importing() => { - (None, Self::still_syncing()) + (None, Self::still_syncing(self.embeddable_at)) }, Some(URLHintResult::Dapp(dapp)) => { let (handler, fetch_control) = ContentFetcherHandler::new( @@ -155,7 +156,8 @@ impl ContentFetcher { dapps_path: self.dapps_path.clone(), on_done: Box::new(on_done), embeddable_at: self.embeddable_at, - } + }, + self.embeddable_at, ); (Some(ContentStatus::Fetching(fetch_control)), Box::new(handler) as Box) @@ -170,13 +172,14 @@ impl ContentFetcher { mime: content.mime, content_path: self.dapps_path.clone(), on_done: Box::new(on_done), - } + }, + self.embeddable_at, ); (Some(ContentStatus::Fetching(fetch_control)), Box::new(handler) as Box) }, None if self.sync.is_major_importing() => { - (None, Self::still_syncing()) + (None, Self::still_syncing(self.embeddable_at)) }, None => { // This may happen when sync status changes in between @@ -185,7 +188,8 @@ impl ContentFetcher { StatusCode::NotFound, "Resource Not Found", "Requested resource was not found.", - None + None, + self.embeddable_at, )) as Box) }, } diff --git a/dapps/src/handlers/content.rs b/dapps/src/handlers/content.rs index 6bde9cf84..a67fbcd0b 100644 --- a/dapps/src/handlers/content.rs +++ b/dapps/src/handlers/content.rs @@ -48,11 +48,7 @@ impl ContentHandler { Self::new_embeddable(code, content, mime!(Text/Html), embeddable_at) } - pub fn error(code: StatusCode, title: &str, message: &str, details: Option<&str>) -> Self { - Self::error_embeddable(code, title, message, details, None) - } - - pub fn error_embeddable(code: StatusCode, title: &str, message: &str, details: Option<&str>, embeddable_at: Option) -> Self { + pub fn error(code: StatusCode, title: &str, message: &str, details: Option<&str>, embeddable_at: Option) -> Self { Self::html(code, format!( include_str!("../error_tpl.html"), title=title, diff --git a/dapps/src/handlers/fetch.rs b/dapps/src/handlers/fetch.rs index 639fc7497..e5acfc50b 100644 --- a/dapps/src/handlers/fetch.rs +++ b/dapps/src/handlers/fetch.rs @@ -138,6 +138,7 @@ pub struct ContentFetcherHandler { client: Option, using_dapps_domains: bool, installer: H, + embeddable_at: Option, } impl Drop for ContentFetcherHandler { @@ -156,7 +157,9 @@ impl ContentFetcherHandler { url: String, control: Control, using_dapps_domains: bool, - handler: H) -> (Self, Arc) { + handler: H, + embeddable_at: Option, + ) -> (Self, Arc) { let fetch_control = Arc::new(FetchControl::default()); let client = Client::default(); @@ -167,6 +170,7 @@ impl ContentFetcherHandler { status: FetchState::NotStarted(url), using_dapps_domains: using_dapps_domains, installer: handler, + embeddable_at: embeddable_at, }; (handler, fetch_control) @@ -204,6 +208,7 @@ impl server::Handler for ContentFetcherHandler< "Unable To Start Dapp Download", "Could not initialize download of the dapp. It might be a problem with the remote server.", Some(&format!("{}", e)), + self.embeddable_at, )), } }, @@ -213,6 +218,7 @@ impl server::Handler for ContentFetcherHandler< "Method Not Allowed", "Only GET requests are allowed.", None, + self.embeddable_at, )), }) } else { None }; @@ -234,7 +240,8 @@ impl server::Handler for ContentFetcherHandler< StatusCode::GatewayTimeout, "Download Timeout", &format!("Could not fetch content within {} seconds.", FETCH_TIMEOUT), - None + None, + self.embeddable_at, ); Self::close_client(&mut self.client); (Some(FetchState::Error(timeout)), Next::write()) @@ -255,7 +262,8 @@ impl server::Handler for ContentFetcherHandler< StatusCode::BadGateway, "Invalid Dapp", "Downloaded bundle does not contain a valid content.", - Some(&format!("{:?}", e)) + Some(&format!("{:?}", e)), + self.embeddable_at, )) }, Ok((id, result)) => { @@ -274,6 +282,7 @@ impl server::Handler for ContentFetcherHandler< "Download Error", "There was an error when fetching the content.", Some(&format!("{:?}", e)), + self.embeddable_at, ); (Some(FetchState::Error(error)), Next::write()) }, diff --git a/dapps/src/page/handler.rs b/dapps/src/page/handler.rs index 0962f22c7..a2ca5e258 100644 --- a/dapps/src/page/handler.rs +++ b/dapps/src/page/handler.rs @@ -59,7 +59,7 @@ pub enum ServedFile { impl ServedFile { pub fn new(embeddable_at: Option) -> Self { - ServedFile::Error(ContentHandler::error_embeddable( + ServedFile::Error(ContentHandler::error( StatusCode::NotFound, "404 Not Found", "Requested dapp resource was not found.", diff --git a/dapps/src/router/auth.rs b/dapps/src/router/auth.rs index ff05420bc..a220e2ab0 100644 --- a/dapps/src/router/auth.rs +++ b/dapps/src/router/auth.rs @@ -59,7 +59,8 @@ impl Authorization for HttpBasicAuth { status::StatusCode::Unauthorized, "Unauthorized", "You need to provide valid credentials to access this page.", - None + None, + None, ))) }, Access::AuthRequired => { diff --git a/dapps/src/router/host_validation.rs b/dapps/src/router/host_validation.rs index 2b7e6c9e4..802466efd 100644 --- a/dapps/src/router/host_validation.rs +++ b/dapps/src/router/host_validation.rs @@ -41,6 +41,7 @@ pub fn host_invalid_response() -> Box + Send> { Box::new(ContentHandler::error(StatusCode::Forbidden, "Current Host Is Disallowed", "You are trying to access your node using incorrect address.", - Some("Use allowed URL or specify different hosts CLI options.") + Some("Use allowed URL or specify different hosts CLI options."), + None, )) } diff --git a/dapps/src/router/mod.rs b/dapps/src/router/mod.rs index 6ca453d6d..fe8061ef0 100644 --- a/dapps/src/router/mod.rs +++ b/dapps/src/router/mod.rs @@ -117,6 +117,7 @@ impl server::Handler for Router { "404 Not Found", "Requested content was not found.", None, + self.signer_port, )) }, // Redirect any other GET request to signer. @@ -131,6 +132,7 @@ impl server::Handler for Router { "404 Not Found", "Your homepage is not available when Trusted Signer is disabled.", Some("You can still access dapps by writing a correct address, though. Re-enabled Signer to get your homepage back."), + self.signer_port, )) } }, diff --git a/dapps/src/tests/fetch.rs b/dapps/src/tests/fetch.rs index 1cabca5cb..d50b2bdde 100644 --- a/dapps/src/tests/fetch.rs +++ b/dapps/src/tests/fetch.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -use tests::helpers::{serve_with_registrar, serve_with_registrar_and_sync, request, assert_security_headers}; +use tests::helpers::{serve_with_registrar, serve_with_registrar_and_sync, request, assert_security_headers_for_embed}; #[test] fn should_resolve_dapp() { @@ -34,7 +34,7 @@ fn should_resolve_dapp() { // then assert_eq!(response.status, "HTTP/1.1 404 Not Found".to_owned()); assert_eq!(registrar.calls.lock().len(), 2); - assert_security_headers(&response.headers); + assert_security_headers_for_embed(&response.headers); } #[test] @@ -63,5 +63,5 @@ fn should_return_503_when_syncing_but_should_make_the_calls() { // then assert_eq!(response.status, "HTTP/1.1 503 Service Unavailable".to_owned()); assert_eq!(registrar.calls.lock().len(), 4); - assert_security_headers(&response.headers); + assert_security_headers_for_embed(&response.headers); } diff --git a/dapps/src/tests/redirection.rs b/dapps/src/tests/redirection.rs index 398d08774..b0a5ca9a2 100644 --- a/dapps/src/tests/redirection.rs +++ b/dapps/src/tests/redirection.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -use tests::helpers::{serve, request, assert_security_headers}; +use tests::helpers::{serve, request, assert_security_headers, assert_security_headers_for_embed}; #[test] fn should_redirect_to_home() { @@ -93,7 +93,7 @@ fn should_display_404_on_invalid_dapp() { // then assert_eq!(response.status, "HTTP/1.1 404 Not Found".to_owned()); - assert_security_headers(&response.headers); + assert_security_headers_for_embed(&response.headers); } #[test] @@ -113,7 +113,7 @@ fn should_display_404_on_invalid_dapp_with_domain() { // then assert_eq!(response.status, "HTTP/1.1 404 Not Found".to_owned()); - assert_security_headers(&response.headers); + assert_security_headers_for_embed(&response.headers); } #[test]