Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Dapps errors embeddable on signer (#3115)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomusdrw authored and gavofyork committed Nov 3, 2016
1 parent d99f1b5 commit e251fd4
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 24 deletions.
18 changes: 11 additions & 7 deletions dapps/src/apps/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ impl<R: URLHint> ContentFetcher<R> {
}
}

fn still_syncing() -> Box<Handler> {
fn still_syncing(port: Option<u16>) -> Box<Handler> {
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("<a href=\"javascript:window.location.reload()\">Refresh</a>")
Some("<a href=\"javascript:window.location.reload()\">Refresh</a>"),
port,
))
}

Expand Down Expand Up @@ -143,7 +144,7 @@ impl<R: URLHint> ContentFetcher<R> {
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(
Expand All @@ -155,7 +156,8 @@ impl<R: URLHint> ContentFetcher<R> {
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<Handler>)
Expand All @@ -170,13 +172,14 @@ impl<R: URLHint> ContentFetcher<R> {
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<Handler>)
},
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
Expand All @@ -185,7 +188,8 @@ impl<R: URLHint> ContentFetcher<R> {
StatusCode::NotFound,
"Resource Not Found",
"Requested resource was not found.",
None
None,
self.embeddable_at,
)) as Box<Handler>)
},
}
Expand Down
6 changes: 1 addition & 5 deletions dapps/src/handlers/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16>) -> Self {
pub fn error(code: StatusCode, title: &str, message: &str, details: Option<&str>, embeddable_at: Option<u16>) -> Self {
Self::html(code, format!(
include_str!("../error_tpl.html"),
title=title,
Expand Down
15 changes: 12 additions & 3 deletions dapps/src/handlers/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ pub struct ContentFetcherHandler<H: ContentValidator> {
client: Option<Client>,
using_dapps_domains: bool,
installer: H,
embeddable_at: Option<u16>,
}

impl<H: ContentValidator> Drop for ContentFetcherHandler<H> {
Expand All @@ -156,7 +157,9 @@ impl<H: ContentValidator> ContentFetcherHandler<H> {
url: String,
control: Control,
using_dapps_domains: bool,
handler: H) -> (Self, Arc<FetchControl>) {
handler: H,
embeddable_at: Option<u16>,
) -> (Self, Arc<FetchControl>) {

let fetch_control = Arc::new(FetchControl::default());
let client = Client::default();
Expand All @@ -167,6 +170,7 @@ impl<H: ContentValidator> ContentFetcherHandler<H> {
status: FetchState::NotStarted(url),
using_dapps_domains: using_dapps_domains,
installer: handler,
embeddable_at: embeddable_at,
};

(handler, fetch_control)
Expand Down Expand Up @@ -204,6 +208,7 @@ impl<H: ContentValidator> server::Handler<HttpStream> 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,
)),
}
},
Expand All @@ -213,6 +218,7 @@ impl<H: ContentValidator> server::Handler<HttpStream> for ContentFetcherHandler<
"Method Not Allowed",
"Only <code>GET</code> requests are allowed.",
None,
self.embeddable_at,
)),
})
} else { None };
Expand All @@ -234,7 +240,8 @@ impl<H: ContentValidator> server::Handler<HttpStream> 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())
Expand All @@ -255,7 +262,8 @@ impl<H: ContentValidator> server::Handler<HttpStream> 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)) => {
Expand All @@ -274,6 +282,7 @@ impl<H: ContentValidator> server::Handler<HttpStream> for ContentFetcherHandler<
"Download Error",
"There was an error when fetching the content.",
Some(&format!("{:?}", e)),
self.embeddable_at,
);
(Some(FetchState::Error(error)), Next::write())
},
Expand Down
2 changes: 1 addition & 1 deletion dapps/src/page/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub enum ServedFile<T: Dapp> {

impl<T: Dapp> ServedFile<T> {
pub fn new(embeddable_at: Option<u16>) -> Self {
ServedFile::Error(ContentHandler::error_embeddable(
ServedFile::Error(ContentHandler::error(
StatusCode::NotFound,
"404 Not Found",
"Requested dapp resource was not found.",
Expand Down
3 changes: 2 additions & 1 deletion dapps/src/router/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
3 changes: 2 additions & 1 deletion dapps/src/router/host_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub fn host_invalid_response() -> Box<server::Handler<HttpStream> + 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 <code>hosts</code> CLI options.")
Some("Use allowed URL or specify different <code>hosts</code> CLI options."),
None,
))
}
2 changes: 2 additions & 0 deletions dapps/src/router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ impl<A: Authorization + 'static> server::Handler<HttpStream> for Router<A> {
"404 Not Found",
"Requested content was not found.",
None,
self.signer_port,
))
},
// Redirect any other GET request to signer.
Expand All @@ -131,6 +132,7 @@ impl<A: Authorization + 'static> server::Handler<HttpStream> for Router<A> {
"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,
))
}
},
Expand Down
6 changes: 3 additions & 3 deletions dapps/src/tests/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

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() {
Expand All @@ -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]
Expand Down Expand Up @@ -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);
}
6 changes: 3 additions & 3 deletions dapps/src/tests/redirection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

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() {
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down

0 comments on commit e251fd4

Please sign in to comment.