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-12 22:44:53 +02:00
|
|
|
mod url;
|
2016-04-14 20:38:48 +02:00
|
|
|
mod redirect;
|
2016-04-08 16:11:58 +02:00
|
|
|
pub mod auth;
|
2016-04-07 12:10:26 +02:00
|
|
|
|
2016-04-07 18:22:53 +02:00
|
|
|
use std::sync::Arc;
|
2016-04-07 12:10:26 +02:00
|
|
|
use hyper;
|
2016-04-08 15:25:20 +02:00
|
|
|
use hyper::{server, uri, header};
|
2016-04-14 20:38:48 +02:00
|
|
|
use hyper::{Next, Encoder, Decoder};
|
|
|
|
use hyper::net::HttpStream;
|
2016-05-12 14:45:09 +02:00
|
|
|
use endpoint::{Endpoint, Endpoints, HostInfo};
|
2016-04-12 22:44:53 +02:00
|
|
|
use self::url::Url;
|
2016-04-08 16:11:58 +02:00
|
|
|
use self::auth::{Authorization, Authorized};
|
2016-04-14 20:38:48 +02:00
|
|
|
use self::redirect::Redirection;
|
2016-04-07 18:22:53 +02:00
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
pub struct Router<A: Authorization + 'static> {
|
|
|
|
main_page: &'static str,
|
|
|
|
endpoints: Arc<Endpoints>,
|
|
|
|
rpc: Arc<Box<Endpoint>>,
|
|
|
|
api: Arc<Box<Endpoint>>,
|
|
|
|
authorization: Arc<A>,
|
|
|
|
handler: Box<server::Handler<HttpStream>>,
|
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> {
|
|
|
|
|
|
|
|
fn on_request(&mut self, req: server::Request) -> Next {
|
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-05-12 14:45:09 +02:00
|
|
|
|
|
|
|
// Choose proper handler depending on path / domain
|
2016-04-14 20:38:48 +02:00
|
|
|
self.handler = match auth {
|
|
|
|
Authorized::No(handler) => handler,
|
|
|
|
Authorized::Yes => {
|
2016-05-12 14:45:09 +02:00
|
|
|
let url = self.extract_url(&req);
|
|
|
|
let app_id = self.extract_app_id(&url);
|
|
|
|
let host = url.map(|u| HostInfo {
|
|
|
|
host: u.host,
|
|
|
|
port: u.port
|
|
|
|
});
|
|
|
|
|
|
|
|
match app_id {
|
|
|
|
Some(ref app_id) if self.endpoints.contains_key(&app_id.id) => {
|
|
|
|
self.endpoints.get(&app_id.id).unwrap().to_handler(&app_id.prefix, host)
|
2016-04-14 20:38:48 +02:00
|
|
|
},
|
2016-05-12 14:45:09 +02:00
|
|
|
Some(ref app_id) if app_id.id == "api" => {
|
|
|
|
self.api.to_handler(&app_id.prefix, host)
|
2016-04-14 20:38:48 +02:00
|
|
|
},
|
|
|
|
_ if *req.method() == hyper::method::Method::Get => {
|
|
|
|
Redirection::new(self.main_page)
|
|
|
|
},
|
|
|
|
_ => {
|
2016-05-12 14:45:09 +02:00
|
|
|
self.rpc.to_handler("/", host)
|
2016-04-14 20:38:48 +02:00
|
|
|
}
|
|
|
|
}
|
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>,
|
|
|
|
rpc: Arc<Box<Endpoint>>,
|
|
|
|
api: Arc<Box<Endpoint>>,
|
|
|
|
authorization: Arc<A>) -> Self {
|
|
|
|
|
2016-05-12 14:45:09 +02:00
|
|
|
let handler = rpc.to_handler("/", None);
|
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,
|
|
|
|
rpc: rpc,
|
|
|
|
api: api,
|
|
|
|
authorization: authorization,
|
|
|
|
handler: handler,
|
2016-04-07 12:10:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-08 16:11:58 +02:00
|
|
|
fn extract_url(&self, req: &server::Request) -> Option<Url> {
|
2016-04-14 20:38:48 +02:00
|
|
|
match *req.uri() {
|
2016-04-08 15:25:20 +02:00
|
|
|
uri::RequestUri::AbsoluteUri(ref url) => {
|
2016-04-07 12:10:26 +02:00
|
|
|
match Url::from_generic_url(url.clone()) {
|
|
|
|
Ok(url) => Some(url),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
},
|
2016-04-08 15:25:20 +02:00
|
|
|
uri::RequestUri::AbsolutePath(ref path) => {
|
2016-04-07 12:10:26 +02:00
|
|
|
// Attempt to prepend the Host header (mandatory in HTTP/1.1)
|
2016-04-14 20:38:48 +02:00
|
|
|
let url_string = match req.headers().get::<header::Host>() {
|
2016-04-07 12:10:26 +02:00
|
|
|
Some(ref host) => {
|
|
|
|
format!("http://{}:{}{}", host.hostname, host.port.unwrap_or(80), path)
|
|
|
|
},
|
2016-04-08 10:13:42 +02:00
|
|
|
None => return None,
|
2016-04-07 12:10:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
match Url::parse(&url_string) {
|
|
|
|
Ok(url) => Some(url),
|
|
|
|
_ => None,
|
|
|
|
}
|
2016-04-08 10:13:42 +02:00
|
|
|
},
|
2016-04-07 12:10:26 +02:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 14:45:09 +02:00
|
|
|
fn extract_app_id(&self, url: &Option<Url>) -> Option<AppId> {
|
|
|
|
match *url {
|
2016-04-07 15:14:39 +02:00
|
|
|
Some(ref url) if url.path.len() > 1 => {
|
2016-05-12 14:45:09 +02:00
|
|
|
let id = url.path[0].clone();
|
|
|
|
Some(AppId {
|
|
|
|
id: id.clone(),
|
|
|
|
prefix: "/".to_owned() + &id
|
|
|
|
})
|
2016-04-07 15:14:39 +02:00
|
|
|
},
|
|
|
|
_ => {
|
2016-04-14 20:38:48 +02:00
|
|
|
None
|
2016-04-08 10:13:42 +02:00
|
|
|
},
|
2016-04-07 12:10:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-12 14:45:09 +02:00
|
|
|
|
|
|
|
struct AppId {
|
|
|
|
id: String,
|
|
|
|
prefix: String
|
|
|
|
}
|