diff --git a/dapps/src/api/api.rs b/dapps/src/api/api.rs index df3386358..064ad6d42 100644 --- a/dapps/src/api/api.rs +++ b/dapps/src/api/api.rs @@ -14,6 +14,8 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . +use std::sync::Arc; + use unicase::UniCase; use hyper::{server, net, Decoder, Encoder, Next, Control}; use hyper::header; @@ -28,16 +30,16 @@ use endpoint::{Endpoint, Endpoints, Handler, EndpointPath}; use jsonrpc_http_server::{self, AccessControlAllowOrigin}; #[derive(Clone)] -pub struct RestApi { +pub struct RestApi { // TODO [ToDr] cors_domains should be handled by the server to avoid duplicated logic. // RequestMiddleware should be able to tell that cors headers should be included. cors_domains: Option>, apps: Vec, - fetcher: F, + fetcher: Arc, } -impl RestApi { - pub fn new(cors_domains: Vec, endpoints: &Endpoints, fetcher: F) -> Box { +impl RestApi { + pub fn new(cors_domains: Vec, endpoints: &Endpoints, fetcher: Arc) -> Box { Box::new(RestApi { cors_domains: Some(cors_domains), apps: Self::list_apps(endpoints), @@ -52,22 +54,22 @@ impl RestApi { } } -impl Endpoint for RestApi { +impl Endpoint for RestApi { fn to_async_handler(&self, path: EndpointPath, control: Control) -> Box { Box::new(RestApiRouter::new((*self).clone(), path, control)) } } -struct RestApiRouter { - api: RestApi, +struct RestApiRouter { + api: RestApi, cors_header: Option, path: Option, control: Option, handler: Box, } -impl RestApiRouter { - fn new(api: RestApi, path: EndpointPath, control: Control) -> Self { +impl RestApiRouter { + fn new(api: RestApi, path: EndpointPath, control: Control) -> Self { RestApiRouter { path: Some(path), cors_header: None, @@ -82,6 +84,7 @@ impl RestApiRouter { } fn resolve_content(&self, hash: Option<&str>, path: EndpointPath, control: Control) -> Option> { + trace!(target: "dapps", "Resolving content: {:?} from path: {:?}", hash, path); match hash { Some(hash) if self.api.fetcher.contains(hash) => { Some(self.api.fetcher.to_async_handler(path, control)) @@ -114,8 +117,7 @@ impl RestApiRouter { } } -impl server::Handler for RestApiRouter { - +impl server::Handler for RestApiRouter { fn on_request(&mut self, request: server::Request) -> Next { self.cors_header = jsonrpc_http_server::cors_header(&request, &self.api.cors_domains).into(); @@ -168,5 +170,4 @@ impl server::Handler for RestApiRouter { fn on_response_writable(&mut self, encoder: &mut Encoder) -> Next { self.handler.on_response_writable(encoder) } - } diff --git a/dapps/src/apps/fetcher/mod.rs b/dapps/src/apps/fetcher/mod.rs index a824134cb..09d275014 100644 --- a/dapps/src/apps/fetcher/mod.rs +++ b/dapps/src/apps/fetcher/mod.rs @@ -47,8 +47,7 @@ pub trait Fetcher: Send + Sync + 'static { fn to_async_handler(&self, path: EndpointPath, control: hyper::Control) -> Box; } -#[derive(Clone)] -pub struct ContentFetcher { +pub struct ContentFetcher { dapps_path: PathBuf, resolver: R, cache: Arc>, @@ -58,14 +57,14 @@ pub struct ContentFetcher Drop for ContentFetcher { +impl Drop for ContentFetcher { fn drop(&mut self) { // Clear cache path let _ = fs::remove_dir_all(&self.dapps_path); } } -impl ContentFetcher { +impl ContentFetcher { pub fn new(resolver: R, sync_status: Arc, embeddable_on: Option<(String, u16)>, remote: Remote, fetch: F) -> Self { let mut dapps_path = env::temp_dir(); @@ -98,7 +97,7 @@ impl ContentFetcher { } } -impl Fetcher for ContentFetcher { +impl Fetcher for ContentFetcher { fn contains(&self, content_id: &str) -> bool { { let mut cache = self.cache.lock(); diff --git a/dapps/src/lib.rs b/dapps/src/lib.rs index 60aba30a4..5f4b83325 100644 --- a/dapps/src/lib.rs +++ b/dapps/src/lib.rs @@ -98,13 +98,13 @@ impl WebProxyTokens for F where F: Fn(String) -> bool + Send + Sync { } /// Dapps server as `jsonrpc-http-server` request middleware. -pub struct Middleware { - router: router::Router>, +pub struct Middleware { + router: router::Router, } -impl Middleware { +impl Middleware { /// Creates new Dapps server middleware. - pub fn new( + pub fn new( remote: Remote, signer_address: Option<(String, u16)>, dapps_path: PathBuf, @@ -114,13 +114,13 @@ impl Middleware { web_proxy_tokens: Arc, fetch: F, ) -> Self { - let content_fetcher = apps::fetcher::ContentFetcher::new( + let content_fetcher = Arc::new(apps::fetcher::ContentFetcher::new( hash_fetch::urlhint::URLHintContract::new(registrar), sync_status, signer_address.clone(), remote.clone(), fetch.clone(), - ); + )); let endpoints = apps::all_endpoints( dapps_path, extra_dapps, @@ -138,7 +138,11 @@ impl Middleware { special.insert(router::SpecialEndpoint::Utils, Some(apps::utils())); special.insert( router::SpecialEndpoint::Api, - Some(api::RestApi::new(cors_domains.clone(), &endpoints, content_fetcher.clone())), + Some(api::RestApi::new( + cors_domains.clone(), + &endpoints, + content_fetcher.clone() + )), ); special }; @@ -156,7 +160,7 @@ impl Middleware { } } -impl http::RequestMiddleware for Middleware { +impl http::RequestMiddleware for Middleware { fn on_request(&self, req: &hyper::server::Request, control: &hyper::Control) -> http::RequestMiddlewareAction { self.router.on_request(req, control) } diff --git a/dapps/src/router.rs b/dapps/src/router.rs index 995565f26..c7b7fb7ff 100644 --- a/dapps/src/router.rs +++ b/dapps/src/router.rs @@ -19,6 +19,7 @@ use address; use std::cmp; +use std::sync::Arc; use std::collections::HashMap; use url::{Url, Host}; @@ -40,14 +41,14 @@ pub enum SpecialEndpoint { None, } -pub struct Router { +pub struct Router { signer_address: Option<(String, u16)>, endpoints: Endpoints, - fetch: F, + fetch: Arc, special: HashMap>>, } -impl http::RequestMiddleware for Router { +impl http::RequestMiddleware for Router { fn on_request(&self, req: &server::Request, control: &Control) -> http::RequestMiddlewareAction { // Choose proper handler depending on path / domain let url = handlers::extract_url(req); @@ -146,10 +147,10 @@ impl http::RequestMiddleware for Router { } } -impl Router { +impl Router { pub fn new( signer_address: Option<(String, u16)>, - content_fetcher: F, + content_fetcher: Arc, endpoints: Endpoints, special: HashMap>>, ) -> Self { diff --git a/parity/dapps.rs b/parity/dapps.rs index e0e97c08f..1b581c0f2 100644 --- a/parity/dapps.rs +++ b/parity/dapps.rs @@ -101,12 +101,11 @@ mod server { use ethcore::transaction::{Transaction, Action}; use ethcore::client::{Client, BlockChainClient, BlockId}; use ethcore_rpc::is_major_importing; - use hash_fetch::fetch::Client as FetchClient; use hash_fetch::urlhint::ContractClient; use parity_dapps; use parity_reactor; - pub type Middleware = parity_dapps::Middleware; + pub use parity_dapps::Middleware; pub fn dapps_middleware( deps: Dependencies,