Merge branch 'master' into types-binary

This commit is contained in:
Nikolay Volf 2016-05-16 17:48:18 +03:00
commit 71278def5e
10 changed files with 320 additions and 126 deletions

View File

@ -56,6 +56,7 @@ pub struct TraceId {
} }
/// Uniquely identifies Uncle. /// Uniquely identifies Uncle.
#[derive(Debug)]
pub struct UncleId ( pub struct UncleId (
/// Block id. /// Block id.
pub BlockId, pub BlockId,

View File

@ -123,8 +123,8 @@ impl<C, S, A, M, EM> EthClient<C, S, A, M, EM>
fn uncle(&self, id: UncleId) -> Result<Value, Error> { fn uncle(&self, id: UncleId) -> Result<Value, Error> {
let client = take_weak!(self.client); let client = take_weak!(self.client);
match client.uncle(id).and_then(|u| client.block_total_difficulty(BlockId::Hash(u.hash())).map(|diff| (diff, u))) { match client.uncle(id).and_then(|u| client.block_total_difficulty(BlockId::Hash(u.parent_hash().clone())).map(|diff| (diff, u))) {
Some((difficulty, uncle)) => { Some((parent_difficulty, uncle)) => {
let block = Block { let block = Block {
hash: OptionalValue::Value(uncle.hash()), hash: OptionalValue::Value(uncle.hash()),
parent_hash: uncle.parent_hash, parent_hash: uncle.parent_hash,
@ -139,7 +139,7 @@ impl<C, S, A, M, EM> EthClient<C, S, A, M, EM>
logs_bloom: uncle.log_bloom, logs_bloom: uncle.log_bloom,
timestamp: U256::from(uncle.timestamp), timestamp: U256::from(uncle.timestamp),
difficulty: uncle.difficulty, difficulty: uncle.difficulty,
total_difficulty: difficulty, total_difficulty: uncle.difficulty + parent_difficulty,
receipts_root: uncle.receipts_root, receipts_root: uncle.receipts_root,
extra_data: Bytes::new(uncle.extra_data), extra_data: Bytes::new(uncle.extra_data),
seal_fields: uncle.seal.into_iter().map(Bytes::new).collect(), seal_fields: uncle.seal.into_iter().map(Bytes::new).collect(),

View File

@ -16,12 +16,8 @@
//! Simple REST API //! Simple REST API
use std::io::Write;
use std::sync::Arc; use std::sync::Arc;
use hyper::status::StatusCode; use endpoint::{Endpoint, Endpoints, ContentHandler, Handler, EndpointPath};
use hyper::{header, server, Decoder, Encoder, Next};
use hyper::net::HttpStream;
use endpoint::{Endpoint, Endpoints};
pub struct RestApi { pub struct RestApi {
endpoints: Arc<Endpoints>, endpoints: Arc<Endpoints>,
@ -46,49 +42,8 @@ impl RestApi {
} }
impl Endpoint for RestApi { impl Endpoint for RestApi {
fn to_handler(&self, _prefix: &str) -> Box<server::Handler<HttpStream>> { fn to_handler(&self, _path: EndpointPath) -> Box<Handler> {
Box::new(RestApiHandler { Box::new(ContentHandler::new(self.list_pages(), "application/json".to_owned()))
pages: self.list_pages(),
write_pos: 0,
})
} }
} }
struct RestApiHandler {
pages: String,
write_pos: usize,
}
impl server::Handler<HttpStream> for RestApiHandler {
fn on_request(&mut self, _request: server::Request) -> Next {
Next::write()
}
fn on_request_readable(&mut self, _decoder: &mut Decoder<HttpStream>) -> Next {
Next::write()
}
fn on_response(&mut self, res: &mut server::Response) -> Next {
res.set_status(StatusCode::Ok);
res.headers_mut().set(header::ContentType("application/json".parse().unwrap()));
Next::write()
}
fn on_response_writable(&mut self, encoder: &mut Encoder<HttpStream>) -> Next {
let bytes = self.pages.as_bytes();
if self.write_pos == bytes.len() {
return Next::end();
}
match encoder.write(&bytes[self.write_pos..]) {
Ok(bytes) => {
self.write_pos += bytes;
Next::write()
},
Err(e) => match e.kind() {
::std::io::ErrorKind::WouldBlock => Next::write(),
_ => Next::end()
},
}
}
}

View File

@ -16,27 +16,36 @@
use endpoint::Endpoints; use endpoint::Endpoints;
use page::PageEndpoint; use page::PageEndpoint;
use proxypac::ProxyPac;
use parity_webapp::WebApp;
extern crate parity_status; extern crate parity_status;
#[cfg(feature = "parity-wallet")] #[cfg(feature = "parity-wallet")]
extern crate parity_wallet; extern crate parity_wallet;
pub fn main_page() -> &'static str { pub fn main_page() -> &'static str {
"/status/" "/status/"
} }
pub fn all_endpoints() -> Endpoints { pub fn all_endpoints() -> Endpoints {
let mut pages = Endpoints::new(); let mut pages = Endpoints::new();
pages.insert("status".to_owned(), Box::new(PageEndpoint::new(parity_status::App::default()))); pages.insert("proxy".to_owned(), ProxyPac::boxed());
insert::<parity_status::App>(&mut pages, "status");
insert::<parity_status::App>(&mut pages, "parity");
wallet_page(&mut pages); wallet_page(&mut pages);
pages pages
} }
#[cfg(feature = "parity-wallet")] #[cfg(feature = "parity-wallet")]
fn wallet_page(pages: &mut Endpoints) { fn wallet_page(pages: &mut Endpoints) {
pages.insert("wallet".to_owned(), Box::new(PageEndpoint::new(parity_wallet::App::default()))); insert::<parity_wallet::App>(pages, "wallet");
} }
#[cfg(not(feature = "parity-wallet"))] #[cfg(not(feature = "parity-wallet"))]
fn wallet_page(_pages: &mut Endpoints) {} fn wallet_page(_pages: &mut Endpoints) {}
fn insert<T : WebApp + Default + 'static>(pages: &mut Endpoints, id: &str) {
pages.insert(id.to_owned(), Box::new(PageEndpoint::new(T::default())));
}

View File

@ -16,12 +16,73 @@
//! URL Endpoint traits //! URL Endpoint traits
use hyper::server; use hyper::status::StatusCode;
use hyper::{header, server, Decoder, Encoder, Next};
use hyper::net::HttpStream; use hyper::net::HttpStream;
use std::io::Write;
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Debug, PartialEq, Default, Clone)]
pub struct EndpointPath {
pub app_id: String,
pub host: String,
pub port: u16,
}
pub trait Endpoint : Send + Sync { pub trait Endpoint : Send + Sync {
fn to_handler(&self, prefix: &str) -> Box<server::Handler<HttpStream>>; fn to_handler(&self, path: EndpointPath) -> Box<server::Handler<HttpStream>>;
} }
pub type Endpoints = HashMap<String, Box<Endpoint>>; pub type Endpoints = HashMap<String, Box<Endpoint>>;
pub type Handler = server::Handler<HttpStream>;
pub struct ContentHandler {
content: String,
mimetype: String,
write_pos: usize,
}
impl ContentHandler {
pub fn new(content: String, mimetype: String) -> Self {
ContentHandler {
content: content,
mimetype: mimetype,
write_pos: 0
}
}
}
impl server::Handler<HttpStream> for ContentHandler {
fn on_request(&mut self, _request: server::Request) -> Next {
Next::write()
}
fn on_request_readable(&mut self, _decoder: &mut Decoder<HttpStream>) -> Next {
Next::write()
}
fn on_response(&mut self, res: &mut server::Response) -> Next {
res.set_status(StatusCode::Ok);
res.headers_mut().set(header::ContentType(self.mimetype.parse().unwrap()));
Next::write()
}
fn on_response_writable(&mut self, encoder: &mut Encoder<HttpStream>) -> Next {
let bytes = self.content.as_bytes();
if self.write_pos == bytes.len() {
return Next::end();
}
match encoder.write(&bytes[self.write_pos..]) {
Ok(bytes) => {
self.write_pos += bytes;
Next::write()
},
Err(e) => match e.kind() {
::std::io::ErrorKind::WouldBlock => Next::write(),
_ => Next::end()
},
}
}
}

View File

@ -57,12 +57,15 @@ mod page;
mod router; mod router;
mod rpc; mod rpc;
mod api; mod api;
mod proxypac;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::net::SocketAddr; use std::net::SocketAddr;
use jsonrpc_core::{IoHandler, IoDelegate}; use jsonrpc_core::{IoHandler, IoDelegate};
use router::auth::{Authorization, NoAuth, HttpBasicAuth}; use router::auth::{Authorization, NoAuth, HttpBasicAuth};
static DAPPS_DOMAIN : &'static str = ".parity";
/// Webapps HTTP+RPC server build. /// Webapps HTTP+RPC server build.
pub struct ServerBuilder { pub struct ServerBuilder {
handler: Arc<IoHandler>, handler: Arc<IoHandler>,

View File

@ -22,7 +22,7 @@ use hyper::header;
use hyper::status::StatusCode; use hyper::status::StatusCode;
use hyper::net::HttpStream; use hyper::net::HttpStream;
use hyper::{Decoder, Encoder, Next}; use hyper::{Decoder, Encoder, Next};
use endpoint::Endpoint; use endpoint::{Endpoint, EndpointPath};
use parity_webapp::WebApp; use parity_webapp::WebApp;
pub struct PageEndpoint<T : WebApp + 'static> { pub struct PageEndpoint<T : WebApp + 'static> {
@ -38,12 +38,11 @@ impl<T: WebApp + 'static> PageEndpoint<T> {
} }
impl<T: WebApp> Endpoint for PageEndpoint<T> { impl<T: WebApp> Endpoint for PageEndpoint<T> {
fn to_handler(&self, prefix: &str) -> Box<server::Handler<HttpStream>> { fn to_handler(&self, path: EndpointPath) -> Box<server::Handler<HttpStream>> {
Box::new(PageHandler { Box::new(PageHandler {
app: self.app.clone(), app: self.app.clone(),
prefix: prefix.to_owned(), path: path,
prefix_with_slash: prefix.to_owned() + "/", file: None,
path: None,
write_pos: 0, write_pos: 0,
}) })
} }
@ -51,21 +50,41 @@ impl<T: WebApp> Endpoint for PageEndpoint<T> {
struct PageHandler<T: WebApp + 'static> { struct PageHandler<T: WebApp + 'static> {
app: Arc<T>, app: Arc<T>,
prefix: String, path: EndpointPath,
prefix_with_slash: String, file: Option<String>,
path: Option<String>,
write_pos: usize, write_pos: usize,
} }
impl<T: WebApp + 'static> PageHandler<T> {
fn extract_path(&self, path: &str) -> String {
let prefix = "/".to_owned() + &self.path.app_id;
let prefix_with_slash = prefix.clone() + "/";
// Index file support
match path == "/" || path == &prefix || path == &prefix_with_slash {
true => "index.html".to_owned(),
false => if path.starts_with(&prefix_with_slash) {
path[prefix_with_slash.len()..].to_owned()
} else if path.starts_with("/") {
path[1..].to_owned()
} else {
path.to_owned()
}
}
}
}
impl<T: WebApp + 'static> server::Handler<HttpStream> for PageHandler<T> { impl<T: WebApp + 'static> server::Handler<HttpStream> for PageHandler<T> {
fn on_request(&mut self, req: server::Request) -> Next { fn on_request(&mut self, req: server::Request) -> Next {
if let RequestUri::AbsolutePath(ref path) = *req.uri() { self.file = match *req.uri() {
// Index file support RequestUri::AbsolutePath(ref path) => {
self.path = match path == &self.prefix || path == &self.prefix_with_slash { Some(self.extract_path(path))
true => Some("index.html".to_owned()), },
false => Some(path[self.prefix_with_slash.len()..].to_owned()), RequestUri::AbsoluteUri(ref url) => {
Some(self.extract_path(url.path()))
},
_ => None,
}; };
}
Next::write() Next::write()
} }
@ -74,7 +93,7 @@ impl<T: WebApp + 'static> server::Handler<HttpStream> for PageHandler<T> {
} }
fn on_response(&mut self, res: &mut server::Response) -> Next { fn on_response(&mut self, res: &mut server::Response) -> Next {
if let Some(f) = self.path.as_ref().and_then(|f| self.app.file(f)) { if let Some(f) = self.file.as_ref().and_then(|f| self.app.file(f)) {
res.set_status(StatusCode::Ok); res.set_status(StatusCode::Ok);
res.headers_mut().set(header::ContentType(f.content_type.parse().unwrap())); res.headers_mut().set(header::ContentType(f.content_type.parse().unwrap()));
Next::write() Next::write()
@ -86,7 +105,7 @@ impl<T: WebApp + 'static> server::Handler<HttpStream> for PageHandler<T> {
fn on_response_writable(&mut self, encoder: &mut Encoder<HttpStream>) -> Next { fn on_response_writable(&mut self, encoder: &mut Encoder<HttpStream>) -> Next {
let (wrote, res) = { let (wrote, res) = {
let file = self.path.as_ref().and_then(|f| self.app.file(f)); let file = self.file.as_ref().and_then(|f| self.app.file(f));
match file { match file {
None => (None, Next::end()), None => (None, Next::end()),
Some(f) if self.write_pos == f.content.len() => (None, Next::end()), Some(f) if self.write_pos == f.content.len() => (None, Next::end()),

48
webapp/src/proxypac.rs Normal file
View File

@ -0,0 +1,48 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// 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/>.
//! Serving ProxyPac file
use endpoint::{Endpoint, Handler, ContentHandler, EndpointPath};
use DAPPS_DOMAIN;
pub struct ProxyPac;
impl ProxyPac {
pub fn boxed() -> Box<Endpoint> {
Box::new(ProxyPac)
}
}
impl Endpoint for ProxyPac {
fn to_handler(&self, path: EndpointPath) -> Box<Handler> {
let content = format!(
r#"
function FindProxyForURL(url, host) {{
if (shExpMatch(host, "*{0}"))
{{
return "PROXY {1}:{2}";
}}
return "DIRECT";
}}
"#,
DAPPS_DOMAIN, path.host, path.port);
Box::new(ContentHandler::new(content, "application/javascript".to_owned()))
}
}

View File

@ -21,16 +21,25 @@ mod url;
mod redirect; mod redirect;
pub mod auth; pub mod auth;
use DAPPS_DOMAIN;
use std::sync::Arc; use std::sync::Arc;
use url::Host;
use hyper; use hyper;
use hyper::{server, uri, header}; use hyper::{server, uri, header};
use hyper::{Next, Encoder, Decoder}; use hyper::{Next, Encoder, Decoder};
use hyper::net::HttpStream; use hyper::net::HttpStream;
use endpoint::{Endpoint, Endpoints}; use endpoint::{Endpoint, Endpoints, EndpointPath};
use self::url::Url; use self::url::Url;
use self::auth::{Authorization, Authorized}; use self::auth::{Authorization, Authorized};
use self::redirect::Redirection; use self::redirect::Redirection;
#[derive(Debug, PartialEq)]
enum SpecialEndpoint {
Rpc,
Api,
None
}
pub struct Router<A: Authorization + 'static> { pub struct Router<A: Authorization + 'static> {
main_page: &'static str, main_page: &'static str,
endpoints: Arc<Endpoints>, endpoints: Arc<Endpoints>,
@ -43,32 +52,43 @@ pub struct Router<A: Authorization + 'static> {
impl<A: Authorization + 'static> server::Handler<HttpStream> for Router<A> { impl<A: Authorization + 'static> server::Handler<HttpStream> for Router<A> {
fn on_request(&mut self, req: server::Request) -> Next { fn on_request(&mut self, req: server::Request) -> Next {
// Check authorization
let auth = self.authorization.is_authorized(&req); let auth = self.authorization.is_authorized(&req);
// Choose proper handler depending on path / domain
self.handler = match auth { self.handler = match auth {
Authorized::No(handler) => handler, Authorized::No(handler) => handler,
Authorized::Yes => { Authorized::Yes => {
let path = self.extract_request_path(&req); let url = extract_url(&req);
match path { let endpoint = extract_endpoint(&url);
Some(ref url) if self.endpoints.contains_key(url) => {
let prefix = "/".to_owned() + url; match endpoint {
self.endpoints.get(url).unwrap().to_handler(&prefix) // First check RPC requests
(ref path, SpecialEndpoint::Rpc) if *req.method() != hyper::method::Method::Get => {
self.rpc.to_handler(path.clone().unwrap_or_default())
}, },
Some(ref url) if url == "api" => { // Check API requests
self.api.to_handler("/api") (ref path, SpecialEndpoint::Api) => {
self.api.to_handler(path.clone().unwrap_or_default())
}, },
// Then delegate to dapp
(Some(ref path), _) if self.endpoints.contains_key(&path.app_id) => {
self.endpoints.get(&path.app_id).unwrap().to_handler(path.clone())
},
// Redirection to main page
_ if *req.method() == hyper::method::Method::Get => { _ if *req.method() == hyper::method::Method::Get => {
Redirection::new(self.main_page) Redirection::new(self.main_page)
}, },
// RPC by default
_ => { _ => {
self.rpc.to_handler(&"/") self.rpc.to_handler(EndpointPath::default())
} }
} }
} }
}; };
self.handler.on_request(req)
// Check authorization
// Choose proper handler depending on path
// Delegate on_request to proper handler // Delegate on_request to proper handler
self.handler.on_request(req)
} }
/// This event occurs each time the `Request` is ready to be read from. /// This event occurs each time the `Request` is ready to be read from.
@ -95,7 +115,7 @@ impl<A: Authorization> Router<A> {
api: Arc<Box<Endpoint>>, api: Arc<Box<Endpoint>>,
authorization: Arc<A>) -> Self { authorization: Arc<A>) -> Self {
let handler = rpc.to_handler(&"/"); let handler = rpc.to_handler(EndpointPath::default());
Router { Router {
main_page: main_page, main_page: main_page,
endpoints: endpoints, endpoints: endpoints,
@ -105,8 +125,9 @@ impl<A: Authorization> Router<A> {
handler: handler, handler: handler,
} }
} }
}
fn extract_url(&self, req: &server::Request) -> Option<Url> { fn extract_url(req: &server::Request) -> Option<Url> {
match *req.uri() { match *req.uri() {
uri::RequestUri::AbsoluteUri(ref url) => { uri::RequestUri::AbsoluteUri(ref url) => {
match Url::from_generic_url(url.clone()) { match Url::from_generic_url(url.clone()) {
@ -130,18 +151,97 @@ impl<A: Authorization> Router<A> {
}, },
_ => None, _ => None,
} }
}
fn extract_endpoint(url: &Option<Url>) -> (Option<EndpointPath>, SpecialEndpoint) {
fn special_endpoint(url: &Url) -> SpecialEndpoint {
if url.path.len() <= 1 {
return SpecialEndpoint::None;
}
match url.path[0].as_ref() {
"rpc" => SpecialEndpoint::Rpc,
"api" => SpecialEndpoint::Api,
_ => SpecialEndpoint::None,
}
} }
fn extract_request_path(&self, req: &server::Request) -> Option<String> { match *url {
let url = self.extract_url(&req); Some(ref url) => match url.host {
match url { Host::Domain(ref domain) if domain.ends_with(DAPPS_DOMAIN) => {
Some(ref url) if url.path.len() > 1 => { let len = domain.len() - DAPPS_DOMAIN.len();
let part = url.path[0].clone(); let id = domain[0..len].to_owned();
Some(part)
(Some(EndpointPath {
app_id: id,
host: domain.clone(),
port: url.port,
}), special_endpoint(url))
}, },
_ => { _ if url.path.len() > 1 => {
None let id = url.path[0].clone();
(Some(EndpointPath {
app_id: id.clone(),
host: format!("{}", url.host),
port: url.port,
}), special_endpoint(url))
}, },
} _ => (None, special_endpoint(url)),
},
_ => (None, SpecialEndpoint::None)
} }
} }
#[test]
fn should_extract_endpoint() {
assert_eq!(extract_endpoint(&None), (None, SpecialEndpoint::None));
// With path prefix
assert_eq!(
extract_endpoint(&Url::parse("http://localhost:8080/status/index.html").ok()),
(Some(EndpointPath {
app_id: "status".to_owned(),
host: "localhost".to_owned(),
port: 8080,
}), SpecialEndpoint::None)
);
// With path prefix
assert_eq!(
extract_endpoint(&Url::parse("http://localhost:8080/rpc/").ok()),
(Some(EndpointPath {
app_id: "rpc".to_owned(),
host: "localhost".to_owned(),
port: 8080,
}), SpecialEndpoint::Rpc)
);
// By Subdomain
assert_eq!(
extract_endpoint(&Url::parse("http://my.status.dapp/test.html").ok()),
(Some(EndpointPath {
app_id: "my.status".to_owned(),
host: "my.status.dapp".to_owned(),
port: 80,
}), SpecialEndpoint::None)
);
// RPC by subdomain
assert_eq!(
extract_endpoint(&Url::parse("http://my.status.dapp/rpc/").ok()),
(Some(EndpointPath {
app_id: "my.status".to_owned(),
host: "my.status.dapp".to_owned(),
port: 80,
}), SpecialEndpoint::Rpc)
);
// API by subdomain
assert_eq!(
extract_endpoint(&Url::parse("http://my.status.dapp/rpc/").ok()),
(Some(EndpointPath {
app_id: "my.status".to_owned(),
host: "my.status.dapp".to_owned(),
port: 80,
}), SpecialEndpoint::Api)
);
}

View File

@ -15,11 +15,9 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use hyper::server;
use hyper::net::HttpStream;
use jsonrpc_core::IoHandler; use jsonrpc_core::IoHandler;
use jsonrpc_http_server::{ServerHandler, PanicHandler, AccessControlAllowOrigin}; use jsonrpc_http_server::{ServerHandler, PanicHandler, AccessControlAllowOrigin};
use endpoint::Endpoint; use endpoint::{Endpoint, EndpointPath, Handler};
pub fn rpc(handler: Arc<IoHandler>, panic_handler: Arc<Mutex<Option<Box<Fn() -> () + Send>>>>) -> Box<Endpoint> { pub fn rpc(handler: Arc<IoHandler>, panic_handler: Arc<Mutex<Option<Box<Fn() -> () + Send>>>>) -> Box<Endpoint> {
Box::new(RpcEndpoint { Box::new(RpcEndpoint {
@ -36,7 +34,7 @@ struct RpcEndpoint {
} }
impl Endpoint for RpcEndpoint { impl Endpoint for RpcEndpoint {
fn to_handler(&self, _prefix: &str) -> Box<server::Handler<HttpStream>> { fn to_handler(&self, _path: EndpointPath) -> Box<Handler> {
let panic_handler = PanicHandler { handler: self.panic_handler.clone() }; let panic_handler = PanicHandler { handler: self.panic_handler.clone() };
Box::new(ServerHandler::new(self.handler.clone(), self.cors_domain.clone(), panic_handler)) Box::new(ServerHandler::new(self.handler.clone(), self.cors_domain.clone(), panic_handler))
} }