From 0a85df10e8c0413c28c61fc54610a86bdc90fa0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Thu, 12 May 2016 17:13:26 +0200 Subject: [PATCH] Proper handling of prefixed and non-prefixed apps --- webapp/src/api.rs | 4 +- webapp/src/apps.rs | 5 +- webapp/src/endpoint.rs | 9 +-- webapp/src/lib.rs | 2 +- webapp/src/page/mod.rs | 33 +++++---- webapp/src/proxypac.rs | 20 ++---- webapp/src/router/mod.rs | 145 ++++++++++++++++++++++----------------- webapp/src/rpc.rs | 6 +- 8 files changed, 120 insertions(+), 104 deletions(-) diff --git a/webapp/src/api.rs b/webapp/src/api.rs index 893e7258a..75cdb4c58 100644 --- a/webapp/src/api.rs +++ b/webapp/src/api.rs @@ -17,7 +17,7 @@ //! Simple REST API use std::sync::Arc; -use endpoint::{Endpoint, Endpoints, ContentHandler, Handler, HostInfo}; +use endpoint::{Endpoint, Endpoints, ContentHandler, Handler, EndpointPath}; pub struct RestApi { endpoints: Arc, @@ -42,7 +42,7 @@ impl RestApi { } impl Endpoint for RestApi { - fn to_handler(&self, _prefix: &str, _host: Option) -> Box { + fn to_handler(&self, _path: EndpointPath) -> Box { Box::new(ContentHandler::new(self.list_pages(), "application/json".to_owned())) } } diff --git a/webapp/src/apps.rs b/webapp/src/apps.rs index 7470786a5..c07e9919c 100644 --- a/webapp/src/apps.rs +++ b/webapp/src/apps.rs @@ -14,7 +14,6 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -use std::net::SocketAddr; use endpoint::Endpoints; use page::PageEndpoint; use proxypac::ProxyPac; @@ -28,9 +27,9 @@ pub fn main_page() -> &'static str { "/status/" } -pub fn all_endpoints(addr: &SocketAddr) -> Endpoints { +pub fn all_endpoints() -> Endpoints { let mut pages = Endpoints::new(); - pages.insert("proxy".to_owned(), ProxyPac::new(addr)); + pages.insert("proxy".to_owned(), ProxyPac::boxed()); insert::(&mut pages, "status"); insert::(&mut pages, "parity"); diff --git a/webapp/src/endpoint.rs b/webapp/src/endpoint.rs index 533570635..d367734c4 100644 --- a/webapp/src/endpoint.rs +++ b/webapp/src/endpoint.rs @@ -16,7 +16,6 @@ //! URL Endpoint traits -use url::Host; use hyper::status::StatusCode; use hyper::{header, server, Decoder, Encoder, Next}; use hyper::net::HttpStream; @@ -24,13 +23,15 @@ use hyper::net::HttpStream; use std::io::Write; use std::collections::HashMap; -pub struct HostInfo { - pub host: Host, +#[derive(Debug, PartialEq, Default, Clone)] +pub struct EndpointPath { + pub app_id: String, + pub host: String, pub port: u16, } pub trait Endpoint : Send + Sync { - fn to_handler(&self, prefix: &str, host: Option) -> Box>; + fn to_handler(&self, path: EndpointPath) -> Box>; } pub type Endpoints = HashMap>; diff --git a/webapp/src/lib.rs b/webapp/src/lib.rs index 18b088005..45157f076 100644 --- a/webapp/src/lib.rs +++ b/webapp/src/lib.rs @@ -106,7 +106,7 @@ pub struct Server { impl Server { fn start_http(addr: &SocketAddr, authorization: A, handler: Arc) -> Result { let panic_handler = Arc::new(Mutex::new(None)); - let endpoints = Arc::new(apps::all_endpoints(addr)); + let endpoints = Arc::new(apps::all_endpoints()); let authorization = Arc::new(authorization); let rpc_endpoint = Arc::new(rpc::rpc(handler, panic_handler.clone())); let api = Arc::new(api::RestApi::new(endpoints.clone())); diff --git a/webapp/src/page/mod.rs b/webapp/src/page/mod.rs index b2cf9615b..abcd1930b 100644 --- a/webapp/src/page/mod.rs +++ b/webapp/src/page/mod.rs @@ -22,7 +22,7 @@ use hyper::header; use hyper::status::StatusCode; use hyper::net::HttpStream; use hyper::{Decoder, Encoder, Next}; -use endpoint::{Endpoint, HostInfo}; +use endpoint::{Endpoint, EndpointPath}; use parity_webapp::WebApp; pub struct PageEndpoint { @@ -38,12 +38,11 @@ impl PageEndpoint { } impl Endpoint for PageEndpoint { - fn to_handler(&self, prefix: &str, _host: Option) -> Box> { + fn to_handler(&self, path: EndpointPath) -> Box> { Box::new(PageHandler { app: self.app.clone(), - prefix: prefix.to_owned(), - prefix_with_slash: prefix.to_owned() + "/", - path: None, + path: path, + file: None, write_pos: 0, }) } @@ -51,25 +50,33 @@ impl Endpoint for PageEndpoint { struct PageHandler { app: Arc, - prefix: String, - prefix_with_slash: String, - path: Option, + path: EndpointPath, + file: Option, write_pos: usize, } impl PageHandler { 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 == &self.prefix || path == &self.prefix_with_slash { + match path == "/" || path == &prefix || path == &prefix_with_slash { true => "index.html".to_owned(), - false => path[self.prefix_with_slash.len()..].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 server::Handler for PageHandler { fn on_request(&mut self, req: server::Request) -> Next { - self.path = match *req.uri() { + self.file = match *req.uri() { RequestUri::AbsolutePath(ref path) => { Some(self.extract_path(path)) }, @@ -86,7 +93,7 @@ impl server::Handler for PageHandler { } 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.headers_mut().set(header::ContentType(f.content_type.parse().unwrap())); Next::write() @@ -98,7 +105,7 @@ impl server::Handler for PageHandler { fn on_response_writable(&mut self, encoder: &mut Encoder) -> Next { 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 { None => (None, Next::end()), Some(f) if self.write_pos == f.content.len() => (None, Next::end()), diff --git a/webapp/src/proxypac.rs b/webapp/src/proxypac.rs index b39c72500..da2cab916 100644 --- a/webapp/src/proxypac.rs +++ b/webapp/src/proxypac.rs @@ -16,37 +16,31 @@ //! Serving ProxyPac file -use std::net::SocketAddr; -use endpoint::{Endpoint, Handler, ContentHandler, HostInfo}; +use endpoint::{Endpoint, Handler, ContentHandler, EndpointPath}; use DAPPS_DOMAIN; -pub struct ProxyPac { - addr: SocketAddr, -} +pub struct ProxyPac; impl ProxyPac { - pub fn new(addr: &SocketAddr) -> Box { - Box::new(ProxyPac { - addr: addr.clone(), - }) + pub fn boxed() -> Box { + Box::new(ProxyPac) } } impl Endpoint for ProxyPac { - fn to_handler(&self, _prefix: &str, host: Option) -> Box { - let host = host.map_or_else(|| format!("{}", self.addr), |h| format!("{}:{}", h.host, h.port)); + fn to_handler(&self, path: EndpointPath) -> Box { let content = format!( r#" function FindProxyForURL(url, host) {{ if (shExpMatch(host, "*{0}")) {{ - return "PROXY {1}"; + return "PROXY {1}:{2}"; }} return "DIRECT"; }} "#, - DAPPS_DOMAIN, host); + DAPPS_DOMAIN, path.host, path.port); Box::new(ContentHandler::new(content, "application/javascript".to_owned())) } } diff --git a/webapp/src/router/mod.rs b/webapp/src/router/mod.rs index dc8abdada..11205068a 100644 --- a/webapp/src/router/mod.rs +++ b/webapp/src/router/mod.rs @@ -28,11 +28,18 @@ use hyper; use hyper::{server, uri, header}; use hyper::{Next, Encoder, Decoder}; use hyper::net::HttpStream; -use endpoint::{Endpoint, Endpoints, HostInfo}; +use endpoint::{Endpoint, Endpoints, EndpointPath}; use self::url::Url; use self::auth::{Authorization, Authorized}; use self::redirect::Redirection; +#[derive(Debug, PartialEq)] +enum SpecialEndpoint { + Rpc, + Api, + None +} + pub struct Router { main_page: &'static str, endpoints: Arc, @@ -42,13 +49,6 @@ pub struct Router { handler: Box>, } -#[derive(Debug, PartialEq)] -struct AppId { - id: String, - prefix: String, - is_rpc: bool, -} - impl server::Handler for Router { fn on_request(&mut self, req: server::Request) -> Next { @@ -60,23 +60,20 @@ impl server::Handler for Router { Authorized::No(handler) => handler, Authorized::Yes => { let url = extract_url(&req); - let app_id = extract_app_id(&url); - let host = url.map(|u| HostInfo { - host: u.host, - port: u.port - }); + let endpoint = extract_endpoint(&url); - match app_id { + match endpoint { // First check RPC requests - Some(ref app_id) if app_id.is_rpc && *req.method() != hyper::method::Method::Get => { - self.rpc.to_handler("", host) + (ref path, SpecialEndpoint::Rpc) if *req.method() != hyper::method::Method::Get => { + self.rpc.to_handler(path.clone().unwrap_or_default()) + }, + // Check API requests + (ref path, SpecialEndpoint::Api) => { + self.api.to_handler(path.clone().unwrap_or_default()) }, // Then delegate to dapp - 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) - }, - Some(ref app_id) if app_id.id == "api" => { - self.api.to_handler(&app_id.prefix, host) + (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 => { @@ -84,7 +81,7 @@ impl server::Handler for Router { }, // RPC by default _ => { - self.rpc.to_handler("", host) + self.rpc.to_handler(EndpointPath::default()) } } } @@ -118,7 +115,7 @@ impl Router { api: Arc>, authorization: Arc) -> Self { - let handler = rpc.to_handler("", None); + let handler = rpc.to_handler(EndpointPath::default()); Router { main_page: main_page, endpoints: endpoints, @@ -156,9 +153,16 @@ fn extract_url(req: &server::Request) -> Option { } } -fn extract_app_id(url: &Option) -> Option { - fn is_rpc(url: &Url) -> bool { - url.path.len() > 1 && url.path[0] == "rpc" +fn extract_endpoint(url: &Option) -> (Option, 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, + } } match *url { @@ -167,64 +171,77 @@ fn extract_app_id(url: &Option) -> Option { let len = domain.len() - DAPPS_DOMAIN.len(); let id = domain[0..len].to_owned(); - Some(AppId { - id: id, - prefix: "".to_owned(), - is_rpc: is_rpc(url), - }) + (Some(EndpointPath { + app_id: id, + host: domain.clone(), + port: url.port, + }), special_endpoint(url)) }, _ if url.path.len() > 1 => { let id = url.path[0].clone(); - Some(AppId { - id: id.clone(), - prefix: "/".to_owned() + &id, - is_rpc: is_rpc(url), - }) + (Some(EndpointPath { + app_id: id.clone(), + host: format!("{}", url.host), + port: url.port, + }), special_endpoint(url)) }, - _ => None, + _ => (None, special_endpoint(url)), }, - _ => None + _ => (None, SpecialEndpoint::None) } } #[test] -fn should_extract_app_id() { - assert_eq!(extract_app_id(&None), None); +fn should_extract_endpoint() { + assert_eq!(extract_endpoint(&None), (None, SpecialEndpoint::None)); // With path prefix assert_eq!( - extract_app_id(&Url::parse("http://localhost:8080/status/index.html").ok()), - Some(AppId { - id: "status".to_owned(), - prefix: "/status".to_owned(), - is_rpc: false, - })); + 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_app_id(&Url::parse("http://localhost:8080/rpc/").ok()), - Some(AppId { - id: "rpc".to_owned(), - prefix: "/rpc".to_owned(), - is_rpc: true, - })); + 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_app_id(&Url::parse("http://my.status.dapp/test.html").ok()), - Some(AppId { - id: "my.status".to_owned(), - prefix: "".to_owned(), - is_rpc: false, - })); + 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_app_id(&Url::parse("http://my.status.dapp/rpc/").ok()), - Some(AppId { - id: "my.status".to_owned(), - prefix: "".to_owned(), - is_rpc: true, - })); + 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) + ); } diff --git a/webapp/src/rpc.rs b/webapp/src/rpc.rs index acd83aed5..7ba0b9584 100644 --- a/webapp/src/rpc.rs +++ b/webapp/src/rpc.rs @@ -15,11 +15,9 @@ // along with Parity. If not, see . use std::sync::{Arc, Mutex}; -use hyper::server; -use hyper::net::HttpStream; use jsonrpc_core::IoHandler; use jsonrpc_http_server::{ServerHandler, PanicHandler, AccessControlAllowOrigin}; -use endpoint::{Endpoint, HostInfo}; +use endpoint::{Endpoint, EndpointPath, Handler}; pub fn rpc(handler: Arc, panic_handler: Arc () + Send>>>>) -> Box { Box::new(RpcEndpoint { @@ -36,7 +34,7 @@ struct RpcEndpoint { } impl Endpoint for RpcEndpoint { - fn to_handler(&self, _prefix: &str, _host: Option) -> Box> { + fn to_handler(&self, _path: EndpointPath) -> Box { let panic_handler = PanicHandler { handler: self.panic_handler.clone() }; Box::new(ServerHandler::new(self.handler.clone(), self.cors_domain.clone(), panic_handler)) }