2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-04-07 10:49:00 +02:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Ethcore Webapplications for Parity
|
|
|
|
#![warn(missing_docs)]
|
2017-02-13 16:38:47 +01:00
|
|
|
#![cfg_attr(feature="nightly", feature(plugin))]
|
2016-04-07 10:49:00 +02:00
|
|
|
#![cfg_attr(feature="nightly", plugin(clippy))]
|
|
|
|
|
2017-02-04 09:52:14 +01:00
|
|
|
extern crate base32;
|
2016-04-07 10:49:00 +02:00
|
|
|
extern crate hyper;
|
2016-11-03 12:40:53 +01:00
|
|
|
extern crate time;
|
2016-10-20 23:41:15 +02:00
|
|
|
extern crate url as url_lib;
|
2016-07-07 09:42:49 +02:00
|
|
|
extern crate unicase;
|
2016-05-17 16:55:08 +02:00
|
|
|
extern crate serde;
|
|
|
|
extern crate serde_json;
|
2016-08-18 12:19:09 +02:00
|
|
|
extern crate zip;
|
|
|
|
extern crate rand;
|
2016-04-07 10:49:00 +02:00
|
|
|
extern crate jsonrpc_core;
|
|
|
|
extern crate jsonrpc_http_server;
|
2016-08-24 16:53:07 +02:00
|
|
|
extern crate mime_guess;
|
|
|
|
extern crate rustc_serialize;
|
2016-06-01 19:37:34 +02:00
|
|
|
extern crate ethcore_rpc;
|
2016-08-18 12:19:09 +02:00
|
|
|
extern crate ethcore_util as util;
|
2016-12-11 19:14:42 +01:00
|
|
|
extern crate parity_hash_fetch as hash_fetch;
|
2016-08-30 14:04:52 +02:00
|
|
|
extern crate linked_hash_map;
|
2016-09-27 16:27:06 +02:00
|
|
|
extern crate fetch;
|
2016-10-22 15:21:41 +02:00
|
|
|
extern crate parity_dapps_glue as parity_dapps;
|
2016-12-22 18:26:39 +01:00
|
|
|
extern crate futures;
|
|
|
|
extern crate parity_reactor;
|
|
|
|
|
2016-10-20 23:41:15 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate mime;
|
2017-02-13 16:38:47 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
2016-10-20 23:41:15 +02:00
|
|
|
|
2016-09-01 10:16:04 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
extern crate ethcore_devtools as devtools;
|
2016-10-22 15:21:41 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
extern crate env_logger;
|
2016-04-07 10:49:00 +02:00
|
|
|
|
2016-10-17 11:56:42 +02:00
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
mod endpoint;
|
2016-04-07 12:10:26 +02:00
|
|
|
mod apps;
|
|
|
|
mod page;
|
|
|
|
mod router;
|
2016-07-06 11:24:29 +02:00
|
|
|
mod handlers;
|
2016-04-14 20:38:48 +02:00
|
|
|
mod rpc;
|
|
|
|
mod api;
|
2016-05-12 14:45:09 +02:00
|
|
|
mod proxypac;
|
2016-07-07 09:42:49 +02:00
|
|
|
mod url;
|
2016-12-22 18:26:39 +01:00
|
|
|
mod web;
|
2016-08-30 16:05:18 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2016-04-07 12:10:26 +02:00
|
|
|
|
2017-01-06 16:05:58 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
2016-04-23 12:29:12 +02:00
|
|
|
use std::sync::{Arc, Mutex};
|
2016-04-14 20:38:48 +02:00
|
|
|
use std::net::SocketAddr;
|
2016-05-13 18:40:20 +02:00
|
|
|
use std::collections::HashMap;
|
2016-07-13 19:59:59 +02:00
|
|
|
|
2017-02-04 22:18:19 +01:00
|
|
|
use ethcore_rpc::{Metadata};
|
2016-12-22 18:26:39 +01:00
|
|
|
use fetch::{Fetch, Client as FetchClient};
|
2017-01-11 20:02:27 +01:00
|
|
|
use hash_fetch::urlhint::ContractClient;
|
2017-02-04 22:18:19 +01:00
|
|
|
use jsonrpc_core::Middleware;
|
2017-01-11 20:02:27 +01:00
|
|
|
use jsonrpc_core::reactor::RpcHandler;
|
2016-04-08 16:11:58 +02:00
|
|
|
use router::auth::{Authorization, NoAuth, HttpBasicAuth};
|
2016-12-22 18:26:39 +01:00
|
|
|
use parity_reactor::Remote;
|
2016-04-08 16:11:58 +02:00
|
|
|
|
2016-10-22 15:21:41 +02:00
|
|
|
use self::apps::{HOME_PAGE, DAPPS_DOMAIN};
|
2016-05-16 16:08:52 +02:00
|
|
|
|
2016-09-01 11:16:19 +02:00
|
|
|
/// Indicates sync status
|
|
|
|
pub trait SyncStatus: Send + Sync {
|
|
|
|
/// Returns true if there is a major sync happening.
|
2016-10-20 23:36:18 +02:00
|
|
|
fn is_major_importing(&self) -> bool;
|
2016-09-01 11:16:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> SyncStatus for F where F: Fn() -> bool + Send + Sync {
|
2016-10-20 23:36:18 +02:00
|
|
|
fn is_major_importing(&self) -> bool { self() }
|
2016-09-01 11:16:19 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 11:15:02 +01:00
|
|
|
/// Validates Web Proxy tokens
|
|
|
|
pub trait WebProxyTokens: Send + Sync {
|
|
|
|
/// Should return true if token is a valid web proxy access token.
|
2017-02-04 09:52:14 +01:00
|
|
|
fn is_web_proxy_token_valid(&self, token: &str) -> bool;
|
2016-12-27 11:15:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> WebProxyTokens for F where F: Fn(String) -> bool + Send + Sync {
|
2017-02-04 09:52:14 +01:00
|
|
|
fn is_web_proxy_token_valid(&self, token: &str) -> bool { self(token.to_owned()) }
|
2016-12-27 11:15:02 +01:00
|
|
|
}
|
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
/// Webapps HTTP+RPC server build.
|
2016-12-27 16:38:55 +01:00
|
|
|
pub struct ServerBuilder<T: Fetch = FetchClient> {
|
2017-01-06 16:05:58 +01:00
|
|
|
dapps_path: PathBuf,
|
|
|
|
extra_dapps: Vec<PathBuf>,
|
2016-08-23 19:28:21 +02:00
|
|
|
registrar: Arc<ContractClient>,
|
2016-09-01 11:16:19 +02:00
|
|
|
sync_status: Arc<SyncStatus>,
|
2016-12-27 11:15:02 +01:00
|
|
|
web_proxy_tokens: Arc<WebProxyTokens>,
|
2016-11-09 19:41:47 +01:00
|
|
|
signer_address: Option<(String, u16)>,
|
2017-01-11 20:02:27 +01:00
|
|
|
allowed_hosts: Option<Vec<String>>,
|
2016-12-22 18:26:39 +01:00
|
|
|
remote: Remote,
|
2016-12-27 16:38:55 +01:00
|
|
|
fetch: Option<T>,
|
2016-04-07 10:49:00 +02:00
|
|
|
}
|
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
impl ServerBuilder {
|
2016-05-26 18:21:15 +02:00
|
|
|
/// Construct new dapps server
|
2017-01-06 16:05:58 +01:00
|
|
|
pub fn new<P: AsRef<Path>>(dapps_path: P, registrar: Arc<ContractClient>, remote: Remote) -> Self {
|
2016-04-14 20:38:48 +02:00
|
|
|
ServerBuilder {
|
2017-01-06 16:05:58 +01:00
|
|
|
dapps_path: dapps_path.as_ref().to_owned(),
|
|
|
|
extra_dapps: vec![],
|
2016-08-23 19:28:21 +02:00
|
|
|
registrar: registrar,
|
2016-09-01 11:16:19 +02:00
|
|
|
sync_status: Arc::new(|| false),
|
2016-12-27 11:15:02 +01:00
|
|
|
web_proxy_tokens: Arc::new(|_| false),
|
2016-11-09 19:41:47 +01:00
|
|
|
signer_address: None,
|
2017-01-11 20:02:27 +01:00
|
|
|
allowed_hosts: Some(vec![]),
|
2016-12-22 18:26:39 +01:00
|
|
|
remote: remote,
|
|
|
|
fetch: None,
|
2016-04-07 12:10:26 +02:00
|
|
|
}
|
2016-04-07 10:49:00 +02:00
|
|
|
}
|
2016-12-27 16:38:55 +01:00
|
|
|
}
|
2016-04-07 10:49:00 +02:00
|
|
|
|
2016-12-27 16:38:55 +01:00
|
|
|
impl<T: Fetch> ServerBuilder<T> {
|
2016-12-22 18:26:39 +01:00
|
|
|
/// Set a fetch client to use.
|
2016-12-27 16:38:55 +01:00
|
|
|
pub fn fetch<X: Fetch>(self, fetch: X) -> ServerBuilder<X> {
|
|
|
|
ServerBuilder {
|
|
|
|
dapps_path: self.dapps_path,
|
2017-01-06 16:05:58 +01:00
|
|
|
extra_dapps: vec![],
|
2016-12-27 16:38:55 +01:00
|
|
|
registrar: self.registrar,
|
|
|
|
sync_status: self.sync_status,
|
|
|
|
web_proxy_tokens: self.web_proxy_tokens,
|
|
|
|
signer_address: self.signer_address,
|
2017-01-11 20:02:27 +01:00
|
|
|
allowed_hosts: self.allowed_hosts,
|
2016-12-27 16:38:55 +01:00
|
|
|
remote: self.remote,
|
|
|
|
fetch: Some(fetch),
|
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
|
|
|
|
2016-09-01 11:16:19 +02:00
|
|
|
/// Change default sync status.
|
2016-12-27 16:38:55 +01:00
|
|
|
pub fn sync_status(mut self, status: Arc<SyncStatus>) -> Self {
|
2016-09-01 11:16:19 +02:00
|
|
|
self.sync_status = status;
|
2016-12-27 16:38:55 +01:00
|
|
|
self
|
2016-09-01 11:16:19 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 11:15:02 +01:00
|
|
|
/// Change default web proxy tokens validator.
|
2016-12-27 16:38:55 +01:00
|
|
|
pub fn web_proxy_tokens(mut self, tokens: Arc<WebProxyTokens>) -> Self {
|
2016-12-27 11:15:02 +01:00
|
|
|
self.web_proxy_tokens = tokens;
|
2016-12-27 16:38:55 +01:00
|
|
|
self
|
2016-12-27 11:15:02 +01:00
|
|
|
}
|
|
|
|
|
2016-10-19 11:02:21 +02:00
|
|
|
/// Change default signer port.
|
2016-12-27 16:38:55 +01:00
|
|
|
pub fn signer_address(mut self, signer_address: Option<(String, u16)>) -> Self {
|
2016-11-09 19:41:47 +01:00
|
|
|
self.signer_address = signer_address;
|
2016-12-27 16:38:55 +01:00
|
|
|
self
|
2016-10-19 11:02:21 +02:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
/// Change allowed hosts.
|
|
|
|
/// `None` - All hosts are allowed
|
|
|
|
/// `Some(whitelist)` - Allow only whitelisted hosts (+ listen address)
|
|
|
|
pub fn allowed_hosts(mut self, allowed_hosts: Option<Vec<String>>) -> Self {
|
|
|
|
self.allowed_hosts = allowed_hosts;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-01-06 16:05:58 +01:00
|
|
|
/// Change extra dapps paths (apart from `dapps_path`)
|
|
|
|
pub fn extra_dapps<P: AsRef<Path>>(mut self, extra_dapps: &[P]) -> Self {
|
|
|
|
self.extra_dapps = extra_dapps.iter().map(|p| p.as_ref().to_owned()).collect();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-04-08 16:11:58 +02:00
|
|
|
/// Asynchronously start server with no authentication,
|
2016-04-14 20:38:48 +02:00
|
|
|
/// returns result with `Server` handle on success or an error.
|
2017-02-04 22:18:19 +01:00
|
|
|
pub fn start_unsecured_http<S: Middleware<Metadata>>(self, addr: &SocketAddr, handler: RpcHandler<Metadata, S>) -> Result<Server, ServerError> {
|
2017-01-11 20:02:27 +01:00
|
|
|
let fetch = self.fetch_client()?;
|
2016-08-25 08:57:13 +02:00
|
|
|
Server::start_http(
|
|
|
|
addr,
|
2017-01-11 20:02:27 +01:00
|
|
|
self.allowed_hosts,
|
2016-08-25 08:57:13 +02:00
|
|
|
NoAuth,
|
2017-01-11 20:02:27 +01:00
|
|
|
handler,
|
|
|
|
self.dapps_path,
|
|
|
|
self.extra_dapps,
|
|
|
|
self.signer_address,
|
|
|
|
self.registrar,
|
|
|
|
self.sync_status,
|
|
|
|
self.web_proxy_tokens,
|
|
|
|
self.remote,
|
|
|
|
fetch,
|
2016-08-25 08:57:13 +02:00
|
|
|
)
|
2016-04-08 16:11:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Asynchronously start server with `HTTP Basic Authentication`,
|
2016-04-14 20:38:48 +02:00
|
|
|
/// return result with `Server` handle on success or an error.
|
2017-02-04 22:18:19 +01:00
|
|
|
pub fn start_basic_auth_http<S: Middleware<Metadata>>(self, addr: &SocketAddr, username: &str, password: &str, handler: RpcHandler<Metadata, S>) -> Result<Server, ServerError> {
|
2017-01-11 20:02:27 +01:00
|
|
|
let fetch = self.fetch_client()?;
|
2016-08-25 08:57:13 +02:00
|
|
|
Server::start_http(
|
|
|
|
addr,
|
2017-01-11 20:02:27 +01:00
|
|
|
self.allowed_hosts,
|
2016-08-25 08:57:13 +02:00
|
|
|
HttpBasicAuth::single_user(username, password),
|
2017-01-11 20:02:27 +01:00
|
|
|
handler,
|
|
|
|
self.dapps_path,
|
|
|
|
self.extra_dapps,
|
|
|
|
self.signer_address,
|
|
|
|
self.registrar,
|
|
|
|
self.sync_status,
|
|
|
|
self.web_proxy_tokens,
|
|
|
|
self.remote,
|
|
|
|
fetch,
|
2016-08-25 08:57:13 +02:00
|
|
|
)
|
2016-04-08 16:11:58 +02:00
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
|
2016-12-27 16:38:55 +01:00
|
|
|
fn fetch_client(&self) -> Result<T, ServerError> {
|
2016-12-22 18:26:39 +01:00
|
|
|
match self.fetch.clone() {
|
|
|
|
Some(fetch) => Ok(fetch),
|
2016-12-27 16:38:55 +01:00
|
|
|
None => T::new().map_err(|_| ServerError::FetchInitialization),
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-14 20:38:48 +02:00
|
|
|
}
|
2016-04-08 16:11:58 +02:00
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
/// Webapps HTTP server.
|
|
|
|
pub struct Server {
|
|
|
|
server: Option<hyper::server::Listening>,
|
2016-04-23 12:29:12 +02:00
|
|
|
panic_handler: Arc<Mutex<Option<Box<Fn() -> () + Send>>>>,
|
2016-04-07 13:09:58 +02:00
|
|
|
}
|
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
impl Server {
|
2016-08-25 08:57:13 +02:00
|
|
|
/// Returns a list of allowed hosts or `None` if all hosts are allowed.
|
|
|
|
fn allowed_hosts(hosts: Option<Vec<String>>, bind_address: String) -> Option<Vec<String>> {
|
|
|
|
let mut allowed = Vec::new();
|
|
|
|
|
|
|
|
match hosts {
|
|
|
|
Some(hosts) => allowed.extend_from_slice(&hosts),
|
|
|
|
None => return None,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add localhost domain as valid too if listening on loopback interface.
|
|
|
|
allowed.push(bind_address.replace("127.0.0.1", "localhost").into());
|
|
|
|
allowed.push(bind_address.into());
|
|
|
|
Some(allowed)
|
|
|
|
}
|
|
|
|
|
2016-10-22 15:21:41 +02:00
|
|
|
/// Returns a list of CORS domains for API endpoint.
|
2016-11-09 19:41:47 +01:00
|
|
|
fn cors_domains(signer_address: Option<(String, u16)>) -> Vec<String> {
|
|
|
|
match signer_address {
|
|
|
|
Some(signer_address) => vec![
|
2016-10-22 15:21:41 +02:00
|
|
|
format!("http://{}{}", HOME_PAGE, DAPPS_DOMAIN),
|
2017-02-08 00:11:42 +01:00
|
|
|
format!("http://{}{}:{}", HOME_PAGE, DAPPS_DOMAIN, signer_address.1),
|
|
|
|
format!("http://{}", address(&signer_address)),
|
|
|
|
format!("https://{}{}", HOME_PAGE, DAPPS_DOMAIN),
|
|
|
|
format!("https://{}{}:{}", HOME_PAGE, DAPPS_DOMAIN, signer_address.1),
|
|
|
|
format!("https://{}", address(&signer_address)),
|
|
|
|
|
2016-10-22 15:21:41 +02:00
|
|
|
],
|
|
|
|
None => vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:18:19 +01:00
|
|
|
fn start_http<A: Authorization + 'static, F: Fetch, T: Middleware<Metadata>>(
|
2016-08-23 19:28:21 +02:00
|
|
|
addr: &SocketAddr,
|
2016-08-25 08:57:13 +02:00
|
|
|
hosts: Option<Vec<String>>,
|
2016-08-23 19:28:21 +02:00
|
|
|
authorization: A,
|
2017-02-04 22:18:19 +01:00
|
|
|
handler: RpcHandler<Metadata, T>,
|
2017-01-06 16:05:58 +01:00
|
|
|
dapps_path: PathBuf,
|
|
|
|
extra_dapps: Vec<PathBuf>,
|
2016-11-09 19:41:47 +01:00
|
|
|
signer_address: Option<(String, u16)>,
|
2016-08-23 19:28:21 +02:00
|
|
|
registrar: Arc<ContractClient>,
|
2016-09-01 11:16:19 +02:00
|
|
|
sync_status: Arc<SyncStatus>,
|
2016-12-27 11:15:02 +01:00
|
|
|
web_proxy_tokens: Arc<WebProxyTokens>,
|
2016-12-22 18:26:39 +01:00
|
|
|
remote: Remote,
|
|
|
|
fetch: F,
|
2016-08-23 19:28:21 +02:00
|
|
|
) -> Result<Server, ServerError> {
|
2016-04-23 12:29:12 +02:00
|
|
|
let panic_handler = Arc::new(Mutex::new(None));
|
2016-04-14 20:38:48 +02:00
|
|
|
let authorization = Arc::new(authorization);
|
2016-12-22 18:26:39 +01:00
|
|
|
let content_fetcher = Arc::new(apps::fetcher::ContentFetcher::new(
|
|
|
|
hash_fetch::urlhint::URLHintContract::new(registrar),
|
|
|
|
sync_status,
|
|
|
|
signer_address.clone(),
|
|
|
|
remote.clone(),
|
|
|
|
fetch.clone(),
|
|
|
|
));
|
2017-01-06 16:05:58 +01:00
|
|
|
let endpoints = Arc::new(apps::all_endpoints(
|
|
|
|
dapps_path,
|
|
|
|
extra_dapps,
|
|
|
|
signer_address.clone(),
|
|
|
|
web_proxy_tokens,
|
|
|
|
remote.clone(),
|
|
|
|
fetch.clone(),
|
|
|
|
));
|
2016-11-09 19:41:47 +01:00
|
|
|
let cors_domains = Self::cors_domains(signer_address.clone());
|
2016-10-22 15:21:41 +02:00
|
|
|
|
2016-05-13 18:40:20 +02:00
|
|
|
let special = Arc::new({
|
|
|
|
let mut special = HashMap::new();
|
2017-02-14 22:45:29 +01:00
|
|
|
special.insert(router::SpecialEndpoint::Rpc, rpc::rpc(handler, cors_domains.clone(), panic_handler.clone()));
|
2016-05-13 18:40:20 +02:00
|
|
|
special.insert(router::SpecialEndpoint::Utils, apps::utils());
|
2016-09-22 18:05:36 +02:00
|
|
|
special.insert(
|
|
|
|
router::SpecialEndpoint::Api,
|
2016-10-22 15:21:41 +02:00
|
|
|
api::RestApi::new(cors_domains, endpoints.clone(), content_fetcher.clone())
|
2016-09-22 18:05:36 +02:00
|
|
|
);
|
2016-05-13 18:40:20 +02:00
|
|
|
special
|
|
|
|
});
|
2016-08-25 08:57:13 +02:00
|
|
|
let hosts = Self::allowed_hosts(hosts, format!("{}", addr));
|
2016-04-14 20:38:48 +02:00
|
|
|
|
2016-12-27 12:53:56 +01:00
|
|
|
hyper::Server::http(addr)?
|
2016-08-18 12:19:09 +02:00
|
|
|
.handle(move |ctrl| router::Router::new(
|
|
|
|
ctrl,
|
2016-11-09 19:41:47 +01:00
|
|
|
signer_address.clone(),
|
2016-09-04 23:44:46 +02:00
|
|
|
content_fetcher.clone(),
|
2016-04-14 20:38:48 +02:00
|
|
|
endpoints.clone(),
|
2016-05-13 18:40:20 +02:00
|
|
|
special.clone(),
|
2016-04-14 20:38:48 +02:00
|
|
|
authorization.clone(),
|
2016-08-25 08:57:13 +02:00
|
|
|
hosts.clone(),
|
2016-04-14 20:38:48 +02:00
|
|
|
))
|
2016-06-27 10:39:37 +02:00
|
|
|
.map(|(l, srv)| {
|
|
|
|
|
|
|
|
::std::thread::spawn(move || {
|
|
|
|
srv.run();
|
|
|
|
});
|
|
|
|
|
|
|
|
Server {
|
|
|
|
server: Some(l),
|
|
|
|
panic_handler: panic_handler,
|
|
|
|
}
|
2016-04-23 12:29:12 +02:00
|
|
|
})
|
2016-04-14 20:38:48 +02:00
|
|
|
.map_err(ServerError::from)
|
|
|
|
}
|
2016-04-23 12:29:12 +02:00
|
|
|
|
|
|
|
/// Set callback for panics.
|
|
|
|
pub fn set_panic_handler<F>(&self, handler: F) where F : Fn() -> () + Send + 'static {
|
2016-07-13 19:59:59 +02:00
|
|
|
*self.panic_handler.lock().unwrap() = Some(Box::new(handler));
|
2016-04-23 12:29:12 +02:00
|
|
|
}
|
2016-08-30 16:05:18 +02:00
|
|
|
|
2016-08-30 16:56:46 +02:00
|
|
|
#[cfg(test)]
|
2016-08-30 16:05:18 +02:00
|
|
|
/// Returns address that this server is bound to.
|
|
|
|
pub fn addr(&self) -> &SocketAddr {
|
2016-11-29 17:14:28 +01:00
|
|
|
self.server.as_ref()
|
|
|
|
.expect("server is always Some at the start; it's consumed only when object is dropped; qed")
|
|
|
|
.addrs()
|
|
|
|
.first()
|
|
|
|
.expect("You cannot start the server without binding to at least one address; qed")
|
2016-08-30 16:05:18 +02:00
|
|
|
}
|
2016-04-07 13:09:58 +02:00
|
|
|
}
|
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
impl Drop for Server {
|
2016-04-07 13:09:58 +02:00
|
|
|
fn drop(&mut self) {
|
2016-04-14 20:38:48 +02:00
|
|
|
self.server.take().unwrap().close()
|
2016-04-07 13:09:58 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-07 10:49:00 +02:00
|
|
|
|
2016-04-07 13:09:58 +02:00
|
|
|
/// Webapp Server startup error
|
|
|
|
#[derive(Debug)]
|
2016-04-14 20:38:48 +02:00
|
|
|
pub enum ServerError {
|
2016-04-07 13:15:59 +02:00
|
|
|
/// Wrapped `std::io::Error`
|
2016-04-07 13:09:58 +02:00
|
|
|
IoError(std::io::Error),
|
2016-04-07 13:15:59 +02:00
|
|
|
/// Other `hyper` error
|
2016-04-07 13:09:58 +02:00
|
|
|
Other(hyper::error::Error),
|
2016-12-22 18:26:39 +01:00
|
|
|
/// Fetch service initialization error
|
|
|
|
FetchInitialization,
|
2016-04-07 13:09:58 +02:00
|
|
|
}
|
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
impl From<hyper::error::Error> for ServerError {
|
2016-04-07 13:09:58 +02:00
|
|
|
fn from(err: hyper::error::Error) -> Self {
|
|
|
|
match err {
|
2016-04-14 20:38:48 +02:00
|
|
|
hyper::error::Error::Io(e) => ServerError::IoError(e),
|
|
|
|
e => ServerError::Other(e),
|
2016-04-07 13:09:58 +02:00
|
|
|
}
|
2016-04-07 10:49:00 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-18 12:19:09 +02:00
|
|
|
|
|
|
|
/// Random filename
|
2016-12-22 18:26:39 +01:00
|
|
|
fn random_filename() -> String {
|
2016-08-18 12:19:09 +02:00
|
|
|
use ::rand::Rng;
|
|
|
|
let mut rng = ::rand::OsRng::new().unwrap();
|
|
|
|
rng.gen_ascii_chars().take(12).collect()
|
|
|
|
}
|
|
|
|
|
2017-02-08 00:11:42 +01:00
|
|
|
fn address(address: &(String, u16)) -> String {
|
2016-11-09 19:41:47 +01:00
|
|
|
format!("{}:{}", address.0, address.1)
|
2016-10-22 15:21:41 +02:00
|
|
|
}
|
|
|
|
|
2016-08-25 08:57:13 +02:00
|
|
|
#[cfg(test)]
|
2016-08-30 16:05:18 +02:00
|
|
|
mod util_tests {
|
2016-08-25 08:57:13 +02:00
|
|
|
use super::Server;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_return_allowed_hosts() {
|
|
|
|
// given
|
|
|
|
let bind_address = "127.0.0.1".to_owned();
|
|
|
|
|
|
|
|
// when
|
|
|
|
let all = Server::allowed_hosts(None, bind_address.clone());
|
|
|
|
let address = Server::allowed_hosts(Some(Vec::new()), bind_address.clone());
|
|
|
|
let some = Server::allowed_hosts(Some(vec!["ethcore.io".into()]), bind_address.clone());
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert_eq!(all, None);
|
|
|
|
assert_eq!(address, Some(vec!["localhost".into(), "127.0.0.1".into()]));
|
|
|
|
assert_eq!(some, Some(vec!["ethcore.io".into(), "localhost".into(), "127.0.0.1".into()]));
|
|
|
|
}
|
2016-10-22 15:21:41 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_return_cors_domains() {
|
|
|
|
// given
|
|
|
|
|
|
|
|
// when
|
|
|
|
let none = Server::cors_domains(None);
|
2016-11-09 19:41:47 +01:00
|
|
|
let some = Server::cors_domains(Some(("127.0.0.1".into(), 18180)));
|
2016-10-22 15:21:41 +02:00
|
|
|
|
|
|
|
// then
|
|
|
|
assert_eq!(none, Vec::<String>::new());
|
2017-02-08 00:11:42 +01:00
|
|
|
assert_eq!(some, vec![
|
|
|
|
"http://parity.web3.site".to_owned(),
|
|
|
|
"http://parity.web3.site:18180".into(),
|
|
|
|
"http://127.0.0.1:18180".into(),
|
|
|
|
"https://parity.web3.site".into(),
|
|
|
|
"https://parity.web3.site:18180".into(),
|
|
|
|
"https://127.0.0.1:18180".into()
|
|
|
|
|
|
|
|
]);
|
2016-10-22 15:21:41 +02:00
|
|
|
}
|
2016-08-25 08:57:13 +02:00
|
|
|
}
|