2016-04-07 12:10:26 +02:00
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
//! Router implementation
|
2016-04-08 15:25:20 +02:00
|
|
|
//! Processes request handling authorization and dispatching it to proper application.
|
|
|
|
|
2016-04-08 16:11:58 +02:00
|
|
|
pub mod auth;
|
2016-07-20 12:34:17 +02:00
|
|
|
mod host_validation;
|
2016-04-07 12:10:26 +02:00
|
|
|
|
2016-05-16 16:08:52 +02:00
|
|
|
use DAPPS_DOMAIN;
|
2016-04-07 18:22:53 +02:00
|
|
|
use std::sync::Arc;
|
2016-05-13 18:40:20 +02:00
|
|
|
use std::collections::HashMap;
|
2016-07-07 09:42:49 +02:00
|
|
|
use url::{Url, Host};
|
|
|
|
use hyper::{self, server, Next, Encoder, Decoder};
|
2016-04-14 20:38:48 +02:00
|
|
|
use hyper::net::HttpStream;
|
2016-05-13 18:40:20 +02:00
|
|
|
use apps;
|
2016-05-12 17:13:26 +02:00
|
|
|
use endpoint::{Endpoint, Endpoints, EndpointPath};
|
2016-07-07 09:42:49 +02:00
|
|
|
use handlers::{Redirection, extract_url};
|
2016-04-08 16:11:58 +02:00
|
|
|
use self::auth::{Authorization, Authorized};
|
2016-04-07 18:22:53 +02:00
|
|
|
|
2016-05-13 18:40:20 +02:00
|
|
|
/// Special endpoints are accessible on every domain (every dapp)
|
|
|
|
#[derive(Debug, PartialEq, Hash, Eq)]
|
|
|
|
pub enum SpecialEndpoint {
|
2016-05-12 17:13:26 +02:00
|
|
|
Rpc,
|
|
|
|
Api,
|
2016-05-13 18:40:20 +02:00
|
|
|
Utils,
|
2016-05-16 20:22:19 +02:00
|
|
|
None,
|
2016-05-12 17:13:26 +02:00
|
|
|
}
|
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
pub struct Router<A: Authorization + 'static> {
|
|
|
|
main_page: &'static str,
|
|
|
|
endpoints: Arc<Endpoints>,
|
2016-05-13 18:40:20 +02:00
|
|
|
special: Arc<HashMap<SpecialEndpoint, Box<Endpoint>>>,
|
2016-04-14 20:38:48 +02:00
|
|
|
authorization: Arc<A>,
|
2016-09-11 14:04:17 +02:00
|
|
|
allowed_hosts: Option<Vec<String>>,
|
2016-06-27 10:39:37 +02:00
|
|
|
handler: Box<server::Handler<HttpStream> + Send>,
|
2016-04-07 12:10:26 +02:00
|
|
|
}
|
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
impl<A: Authorization + 'static> server::Handler<HttpStream> for Router<A> {
|
|
|
|
|
2016-06-27 10:39:37 +02:00
|
|
|
fn on_request(&mut self, req: server::Request<HttpStream>) -> Next {
|
2016-07-20 12:34:17 +02:00
|
|
|
// Validate Host header
|
2016-09-11 14:04:17 +02:00
|
|
|
if let Some(ref hosts) = self.allowed_hosts {
|
|
|
|
if !host_validation::is_valid(&req, hosts, self.endpoints.keys().cloned().collect()) {
|
|
|
|
self.handler = host_validation::host_invalid_response();
|
|
|
|
return self.handler.on_request(req);
|
|
|
|
}
|
2016-07-20 12:34:17 +02:00
|
|
|
}
|
|
|
|
|
2016-05-12 14:45:09 +02:00
|
|
|
// Check authorization
|
2016-04-14 20:38:48 +02:00
|
|
|
let auth = self.authorization.is_authorized(&req);
|
2016-07-20 12:34:17 +02:00
|
|
|
if let Authorized::No(handler) = auth {
|
|
|
|
self.handler = handler;
|
|
|
|
return self.handler.on_request(req);
|
|
|
|
}
|
2016-05-12 14:45:09 +02:00
|
|
|
|
|
|
|
// Choose proper handler depending on path / domain
|
2016-07-20 12:34:17 +02:00
|
|
|
let url = extract_url(&req);
|
|
|
|
let endpoint = extract_endpoint(&url);
|
|
|
|
|
|
|
|
self.handler = match endpoint {
|
|
|
|
// First check special endpoints
|
|
|
|
(ref path, ref endpoint) if self.special.contains_key(endpoint) => {
|
|
|
|
self.special.get(endpoint).unwrap().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 => {
|
|
|
|
Redirection::new(self.main_page)
|
|
|
|
},
|
|
|
|
// RPC by default
|
|
|
|
_ => {
|
|
|
|
self.special.get(&SpecialEndpoint::Rpc).unwrap().to_handler(EndpointPath::default())
|
2016-04-08 15:25:20 +02:00
|
|
|
}
|
2016-04-14 20:38:48 +02:00
|
|
|
};
|
2016-05-12 14:45:09 +02:00
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
// Delegate on_request to proper handler
|
2016-05-12 14:45:09 +02:00
|
|
|
self.handler.on_request(req)
|
2016-04-14 20:38:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This event occurs each time the `Request` is ready to be read from.
|
|
|
|
fn on_request_readable(&mut self, decoder: &mut Decoder<HttpStream>) -> Next {
|
|
|
|
self.handler.on_request_readable(decoder)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This event occurs after the first time this handled signals `Next::write()`.
|
|
|
|
fn on_response(&mut self, response: &mut server::Response) -> Next {
|
|
|
|
self.handler.on_response(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This event occurs each time the `Response` is ready to be written to.
|
|
|
|
fn on_response_writable(&mut self, encoder: &mut Encoder<HttpStream>) -> Next {
|
|
|
|
self.handler.on_response_writable(encoder)
|
2016-04-07 12:10:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-08 16:11:58 +02:00
|
|
|
impl<A: Authorization> Router<A> {
|
|
|
|
pub fn new(
|
2016-04-14 20:38:48 +02:00
|
|
|
main_page: &'static str,
|
|
|
|
endpoints: Arc<Endpoints>,
|
2016-05-13 18:40:20 +02:00
|
|
|
special: Arc<HashMap<SpecialEndpoint, Box<Endpoint>>>,
|
2016-07-20 12:34:17 +02:00
|
|
|
authorization: Arc<A>,
|
2016-09-11 14:04:17 +02:00
|
|
|
allowed_hosts: Option<Vec<String>>,
|
2016-07-20 12:34:17 +02:00
|
|
|
) -> Self {
|
2016-04-14 20:38:48 +02:00
|
|
|
|
2016-05-13 18:40:20 +02:00
|
|
|
let handler = special.get(&SpecialEndpoint::Rpc).unwrap().to_handler(EndpointPath::default());
|
2016-04-07 12:10:26 +02:00
|
|
|
Router {
|
2016-04-07 15:14:39 +02:00
|
|
|
main_page: main_page,
|
2016-04-14 20:38:48 +02:00
|
|
|
endpoints: endpoints,
|
2016-05-13 18:40:20 +02:00
|
|
|
special: special,
|
2016-04-14 20:38:48 +02:00
|
|
|
authorization: authorization,
|
2016-09-11 14:04:17 +02:00
|
|
|
allowed_hosts: allowed_hosts,
|
2016-04-14 20:38:48 +02:00
|
|
|
handler: handler,
|
2016-04-07 12:10:26 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-12 15:05:59 +02:00
|
|
|
}
|
2016-04-07 12:10:26 +02:00
|
|
|
|
2016-05-12 17:13:26 +02:00
|
|
|
fn extract_endpoint(url: &Option<Url>) -> (Option<EndpointPath>, SpecialEndpoint) {
|
|
|
|
fn special_endpoint(url: &Url) -> SpecialEndpoint {
|
|
|
|
if url.path.len() <= 1 {
|
|
|
|
return SpecialEndpoint::None;
|
|
|
|
}
|
2016-05-13 18:40:20 +02:00
|
|
|
|
2016-05-12 17:13:26 +02:00
|
|
|
match url.path[0].as_ref() {
|
2016-05-13 18:40:20 +02:00
|
|
|
apps::RPC_PATH => SpecialEndpoint::Rpc,
|
|
|
|
apps::API_PATH => SpecialEndpoint::Api,
|
|
|
|
apps::UTILS_PATH => SpecialEndpoint::Utils,
|
2016-05-12 17:13:26 +02:00
|
|
|
_ => SpecialEndpoint::None,
|
|
|
|
}
|
2016-04-07 12:10:26 +02:00
|
|
|
}
|
|
|
|
|
2016-05-12 15:05:59 +02:00
|
|
|
match *url {
|
|
|
|
Some(ref url) => match url.host {
|
2016-05-16 16:08:52 +02:00
|
|
|
Host::Domain(ref domain) if domain.ends_with(DAPPS_DOMAIN) => {
|
|
|
|
let len = domain.len() - DAPPS_DOMAIN.len();
|
2016-05-12 15:05:59 +02:00
|
|
|
let id = domain[0..len].to_owned();
|
|
|
|
|
2016-05-12 17:13:26 +02:00
|
|
|
(Some(EndpointPath {
|
|
|
|
app_id: id,
|
|
|
|
host: domain.clone(),
|
|
|
|
port: url.port,
|
|
|
|
}), special_endpoint(url))
|
2016-05-12 15:05:59 +02:00
|
|
|
},
|
|
|
|
_ if url.path.len() > 1 => {
|
2016-05-12 14:45:09 +02:00
|
|
|
let id = url.path[0].clone();
|
2016-05-12 17:13:26 +02:00
|
|
|
(Some(EndpointPath {
|
|
|
|
app_id: id.clone(),
|
|
|
|
host: format!("{}", url.host),
|
|
|
|
port: url.port,
|
|
|
|
}), special_endpoint(url))
|
2016-04-07 15:14:39 +02:00
|
|
|
},
|
2016-05-12 17:13:26 +02:00
|
|
|
_ => (None, special_endpoint(url)),
|
2016-05-12 15:05:59 +02:00
|
|
|
},
|
2016-05-12 17:13:26 +02:00
|
|
|
_ => (None, SpecialEndpoint::None)
|
2016-04-07 12:10:26 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-12 14:45:09 +02:00
|
|
|
|
2016-05-12 15:05:59 +02:00
|
|
|
#[test]
|
2016-05-12 17:13:26 +02:00
|
|
|
fn should_extract_endpoint() {
|
|
|
|
assert_eq!(extract_endpoint(&None), (None, SpecialEndpoint::None));
|
2016-05-12 15:05:59 +02:00
|
|
|
|
|
|
|
// With path prefix
|
|
|
|
assert_eq!(
|
2016-05-12 17:13:26 +02:00
|
|
|
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)
|
|
|
|
);
|
2016-05-12 15:05:59 +02:00
|
|
|
|
|
|
|
// With path prefix
|
|
|
|
assert_eq!(
|
2016-05-12 17:13:26 +02:00
|
|
|
extract_endpoint(&Url::parse("http://localhost:8080/rpc/").ok()),
|
|
|
|
(Some(EndpointPath {
|
|
|
|
app_id: "rpc".to_owned(),
|
|
|
|
host: "localhost".to_owned(),
|
|
|
|
port: 8080,
|
|
|
|
}), SpecialEndpoint::Rpc)
|
|
|
|
);
|
2016-05-12 15:05:59 +02:00
|
|
|
|
2016-05-13 18:40:20 +02:00
|
|
|
assert_eq!(
|
2016-05-21 15:27:55 +02:00
|
|
|
extract_endpoint(&Url::parse("http://my.status.parity/parity-utils/inject.js").ok()),
|
2016-05-13 18:40:20 +02:00
|
|
|
(Some(EndpointPath {
|
|
|
|
app_id: "my.status".to_owned(),
|
2016-05-21 15:27:55 +02:00
|
|
|
host: "my.status.parity".to_owned(),
|
2016-05-13 18:40:20 +02:00
|
|
|
port: 80,
|
|
|
|
}), SpecialEndpoint::Utils)
|
|
|
|
);
|
|
|
|
|
2016-05-12 15:05:59 +02:00
|
|
|
// By Subdomain
|
|
|
|
assert_eq!(
|
2016-05-21 15:27:55 +02:00
|
|
|
extract_endpoint(&Url::parse("http://my.status.parity/test.html").ok()),
|
2016-05-12 17:13:26 +02:00
|
|
|
(Some(EndpointPath {
|
|
|
|
app_id: "my.status".to_owned(),
|
2016-05-21 15:27:55 +02:00
|
|
|
host: "my.status.parity".to_owned(),
|
2016-05-12 17:13:26 +02:00
|
|
|
port: 80,
|
|
|
|
}), SpecialEndpoint::None)
|
|
|
|
);
|
2016-05-12 15:05:59 +02:00
|
|
|
|
|
|
|
// RPC by subdomain
|
|
|
|
assert_eq!(
|
2016-05-21 15:27:55 +02:00
|
|
|
extract_endpoint(&Url::parse("http://my.status.parity/rpc/").ok()),
|
2016-05-12 17:13:26 +02:00
|
|
|
(Some(EndpointPath {
|
|
|
|
app_id: "my.status".to_owned(),
|
2016-05-21 15:27:55 +02:00
|
|
|
host: "my.status.parity".to_owned(),
|
2016-05-12 17:13:26 +02:00
|
|
|
port: 80,
|
|
|
|
}), SpecialEndpoint::Rpc)
|
|
|
|
);
|
|
|
|
|
|
|
|
// API by subdomain
|
|
|
|
assert_eq!(
|
2016-05-21 15:27:55 +02:00
|
|
|
extract_endpoint(&Url::parse("http://my.status.parity/api/").ok()),
|
2016-05-12 17:13:26 +02:00
|
|
|
(Some(EndpointPath {
|
|
|
|
app_id: "my.status".to_owned(),
|
2016-05-21 15:27:55 +02:00
|
|
|
host: "my.status.parity".to_owned(),
|
2016-05-12 17:13:26 +02:00
|
|
|
port: 80,
|
|
|
|
}), SpecialEndpoint::Api)
|
|
|
|
);
|
2016-05-12 14:45:09 +02:00
|
|
|
}
|