From 9301963d984b80f88c908a6f5331509148346cd5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= <tomusdrw@users.noreply.github.com>
Date: Mon, 16 May 2016 16:08:52 +0200
Subject: [PATCH] Subdomains support in content server (webapps server).
 (#1082)

* Proxy.pac serving

* Subdomains router for dapps

* Proper handling of prefixed and non-prefixed apps

* Changing dapps domain to parity
---
 webapp/src/api.rs        |  51 +---------
 webapp/src/apps.rs       |  15 ++-
 webapp/src/endpoint.rs   |  65 ++++++++++++-
 webapp/src/lib.rs        |   3 +
 webapp/src/page/mod.rs   |  53 +++++++----
 webapp/src/proxypac.rs   |  48 ++++++++++
 webapp/src/router/mod.rs | 198 +++++++++++++++++++++++++++++----------
 webapp/src/rpc.rs        |   6 +-
 8 files changed, 316 insertions(+), 123 deletions(-)
 create mode 100644 webapp/src/proxypac.rs

diff --git a/webapp/src/api.rs b/webapp/src/api.rs
index bad49ec61..75cdb4c58 100644
--- a/webapp/src/api.rs
+++ b/webapp/src/api.rs
@@ -16,12 +16,8 @@
 
 //! Simple REST API
 
-use std::io::Write;
 use std::sync::Arc;
-use hyper::status::StatusCode;
-use hyper::{header, server, Decoder, Encoder, Next};
-use hyper::net::HttpStream;
-use endpoint::{Endpoint, Endpoints};
+use endpoint::{Endpoint, Endpoints, ContentHandler, Handler, EndpointPath};
 
 pub struct RestApi {
 	endpoints: Arc<Endpoints>,
@@ -46,49 +42,8 @@ impl RestApi {
 }
 
 impl Endpoint for RestApi {
-	fn to_handler(&self, _prefix: &str) -> Box<server::Handler<HttpStream>> {
-		Box::new(RestApiHandler {
-			pages: self.list_pages(),
-			write_pos: 0,
-		})
+	fn to_handler(&self, _path: EndpointPath) -> Box<Handler> {
+		Box::new(ContentHandler::new(self.list_pages(), "application/json".to_owned()))
 	}
 }
 
-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()
-			},
-		}
-	}
-}
diff --git a/webapp/src/apps.rs b/webapp/src/apps.rs
index b0a4472f7..c07e9919c 100644
--- a/webapp/src/apps.rs
+++ b/webapp/src/apps.rs
@@ -16,27 +16,36 @@
 
 use endpoint::Endpoints;
 use page::PageEndpoint;
+use proxypac::ProxyPac;
+use parity_webapp::WebApp;
 
 extern crate parity_status;
 #[cfg(feature = "parity-wallet")]
 extern crate parity_wallet;
 
-
 pub fn main_page() -> &'static str {
 	"/status/"
 }
 
 pub fn all_endpoints() -> Endpoints {
 	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);
 	pages
 }
 
 #[cfg(feature = "parity-wallet")]
 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"))]
 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())));
+}
diff --git a/webapp/src/endpoint.rs b/webapp/src/endpoint.rs
index dee2cadd8..d367734c4 100644
--- a/webapp/src/endpoint.rs
+++ b/webapp/src/endpoint.rs
@@ -16,12 +16,73 @@
 
 //! URL Endpoint traits
 
-use hyper::server;
+use hyper::status::StatusCode;
+use hyper::{header, server, Decoder, Encoder, Next};
 use hyper::net::HttpStream;
+
+use std::io::Write;
 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 {
-	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 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()
+			},
+		}
+	}
+}
diff --git a/webapp/src/lib.rs b/webapp/src/lib.rs
index 30431578f..2c74dbdc3 100644
--- a/webapp/src/lib.rs
+++ b/webapp/src/lib.rs
@@ -57,12 +57,15 @@ mod page;
 mod router;
 mod rpc;
 mod api;
+mod proxypac;
 
 use std::sync::{Arc, Mutex};
 use std::net::SocketAddr;
 use jsonrpc_core::{IoHandler, IoDelegate};
 use router::auth::{Authorization, NoAuth, HttpBasicAuth};
 
+static DAPPS_DOMAIN : &'static str = ".parity";
+
 /// Webapps HTTP+RPC server build.
 pub struct ServerBuilder {
 	handler: Arc<IoHandler>,
diff --git a/webapp/src/page/mod.rs b/webapp/src/page/mod.rs
index cd527578b..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;
+use endpoint::{Endpoint, EndpointPath};
 use parity_webapp::WebApp;
 
 pub struct PageEndpoint<T : WebApp + 'static> {
@@ -38,12 +38,11 @@ impl<T: WebApp + 'static> 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 {
 			app: self.app.clone(),
-			prefix: prefix.to_owned(),
-			prefix_with_slash: prefix.to_owned() + "/",
-			path: None,
+			path: path,
+			file: None,
 			write_pos: 0,
 		})
 	}
@@ -51,21 +50,41 @@ impl<T: WebApp> Endpoint for PageEndpoint<T> {
 
 struct PageHandler<T: WebApp + 'static> {
 	app: Arc<T>,
-	prefix: String,
-	prefix_with_slash: String,
-	path: Option<String>,
+	path: EndpointPath,
+	file: Option<String>,
 	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> {
 	fn on_request(&mut self, req: server::Request) -> Next {
-		if let RequestUri::AbsolutePath(ref path) = *req.uri() {
-			// Index file support
-			self.path = match path == &self.prefix || path == &self.prefix_with_slash {
-				true => Some("index.html".to_owned()),
-				false => Some(path[self.prefix_with_slash.len()..].to_owned()),
-			};
-		}
+		self.file = match *req.uri() {
+			RequestUri::AbsolutePath(ref path) => {
+				Some(self.extract_path(path))
+			},
+			RequestUri::AbsoluteUri(ref url) => {
+				Some(self.extract_path(url.path()))
+			},
+			_ => None,
+		};
 		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 {
-		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()
@@ -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 {
 		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
new file mode 100644
index 000000000..da2cab916
--- /dev/null
+++ b/webapp/src/proxypac.rs
@@ -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()))
+	}
+}
+
+
diff --git a/webapp/src/router/mod.rs b/webapp/src/router/mod.rs
index 15fc496f9..11205068a 100644
--- a/webapp/src/router/mod.rs
+++ b/webapp/src/router/mod.rs
@@ -21,16 +21,25 @@ mod url;
 mod redirect;
 pub mod auth;
 
+use DAPPS_DOMAIN;
 use std::sync::Arc;
+use url::Host;
 use hyper;
 use hyper::{server, uri, header};
 use hyper::{Next, Encoder, Decoder};
 use hyper::net::HttpStream;
-use endpoint::{Endpoint, Endpoints};
+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<A: Authorization + 'static> {
 	main_page: &'static str,
 	endpoints: Arc<Endpoints>,
@@ -43,32 +52,43 @@ pub struct Router<A: Authorization + 'static> {
 impl<A: Authorization + 'static> server::Handler<HttpStream> for Router<A> {
 
 	fn on_request(&mut self, req: server::Request) -> Next {
+		// Check authorization
 		let auth = self.authorization.is_authorized(&req);
+
+		// Choose proper handler depending on path / domain
 		self.handler = match auth {
 			Authorized::No(handler) => handler,
 			Authorized::Yes => {
-				let path = self.extract_request_path(&req);
-				match path {
-					Some(ref url) if self.endpoints.contains_key(url) => {
-						let prefix = "/".to_owned() + url;
-						self.endpoints.get(url).unwrap().to_handler(&prefix)
+				let url = extract_url(&req);
+				let endpoint = extract_endpoint(&url);
+
+				match endpoint {
+					// 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" => {
-						self.api.to_handler("/api")
+					// Check API requests
+					(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 => {
 						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
+		self.handler.on_request(req)
 	}
 
 	/// 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>>,
 		authorization: Arc<A>) -> Self {
 
-		let handler = rpc.to_handler(&"/");
+		let handler = rpc.to_handler(EndpointPath::default());
 		Router {
 			main_page: main_page,
 			endpoints: endpoints,
@@ -105,43 +125,123 @@ impl<A: Authorization> Router<A> {
 			handler: handler,
 		}
 	}
+}
 
-	fn extract_url(&self, req: &server::Request) -> Option<Url> {
-		match *req.uri() {
-			uri::RequestUri::AbsoluteUri(ref url) => {
-				match Url::from_generic_url(url.clone()) {
-					Ok(url) => Some(url),
-					_ => None,
-				}
-			},
-			uri::RequestUri::AbsolutePath(ref path) => {
-				// Attempt to prepend the Host header (mandatory in HTTP/1.1)
-				let url_string = match req.headers().get::<header::Host>() {
-					Some(ref host) => {
-						format!("http://{}:{}{}", host.hostname, host.port.unwrap_or(80), path)
-					},
-					None => return None,
-				};
+fn extract_url(req: &server::Request) -> Option<Url> {
+	match *req.uri() {
+		uri::RequestUri::AbsoluteUri(ref url) => {
+			match Url::from_generic_url(url.clone()) {
+				Ok(url) => Some(url),
+				_ => None,
+			}
+		},
+		uri::RequestUri::AbsolutePath(ref path) => {
+			// Attempt to prepend the Host header (mandatory in HTTP/1.1)
+			let url_string = match req.headers().get::<header::Host>() {
+				Some(ref host) => {
+					format!("http://{}:{}{}", host.hostname, host.port.unwrap_or(80), path)
+				},
+				None => return None,
+			};
 
-				match Url::parse(&url_string) {
-					Ok(url) => Some(url),
-					_ => None,
-				}
-			},
-			_ => None,
-		}
-	}
-
-	fn extract_request_path(&self, req: &server::Request) -> Option<String> {
-		let url = self.extract_url(&req);
-		match url {
-			Some(ref url) if url.path.len() > 1 => {
-				let part = url.path[0].clone();
-				Some(part)
-			},
-			_ => {
-				None
-			},
-		}
+			match Url::parse(&url_string) {
+				Ok(url) => Some(url),
+				_ => 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,
+		}
+	}
+
+	match *url {
+		Some(ref url) => match url.host {
+			Host::Domain(ref domain) if domain.ends_with(DAPPS_DOMAIN) => {
+				let len = domain.len() - DAPPS_DOMAIN.len();
+				let id = domain[0..len].to_owned();
+
+				(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(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)
+	);
+}
diff --git a/webapp/src/rpc.rs b/webapp/src/rpc.rs
index 51e171d8d..7ba0b9584 100644
--- a/webapp/src/rpc.rs
+++ b/webapp/src/rpc.rs
@@ -15,11 +15,9 @@
 // along with Parity.  If not, see <http://www.gnu.org/licenses/>.
 
 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;
+use endpoint::{Endpoint, EndpointPath, Handler};
 
 pub fn rpc(handler: Arc<IoHandler>, panic_handler: Arc<Mutex<Option<Box<Fn() -> () + Send>>>>) -> Box<Endpoint> {
 	Box::new(RpcEndpoint {
@@ -36,7 +34,7 @@ struct 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() };
 		Box::new(ServerHandler::new(self.handler.clone(), self.cors_domain.clone(), panic_handler))
 	}