2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-06-03 11:51:11 +02:00
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
use mime_guess;
|
2017-10-05 12:35:01 +02:00
|
|
|
use std::{fs, fmt};
|
2016-09-04 23:44:46 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2017-10-05 12:35:01 +02:00
|
|
|
use futures::{future};
|
|
|
|
use futures_cpupool::CpuPool;
|
|
|
|
use page::handler::{self, PageCache};
|
|
|
|
use endpoint::{Endpoint, EndpointInfo, EndpointPath, Request, Response};
|
|
|
|
use hyper::mime::Mime;
|
2017-07-04 16:04:09 +02:00
|
|
|
use Embeddable;
|
2016-06-03 11:51:11 +02:00
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Dapp {
|
|
|
|
pool: CpuPool,
|
2016-06-03 11:51:11 +02:00
|
|
|
path: PathBuf,
|
2016-12-22 18:26:39 +01:00
|
|
|
mime: Option<Mime>,
|
2016-09-04 23:44:46 +02:00
|
|
|
info: Option<EndpointInfo>,
|
2016-11-03 12:40:53 +01:00
|
|
|
cache: PageCache,
|
2017-07-04 16:04:09 +02:00
|
|
|
embeddable_on: Embeddable,
|
2016-06-03 11:51:11 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
impl fmt::Debug for Dapp {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt.debug_struct("Dapp")
|
|
|
|
.field("path", &self.path)
|
|
|
|
.field("mime", &self.mime)
|
|
|
|
.field("info", &self.info)
|
|
|
|
.field("cache", &self.cache)
|
|
|
|
.field("embeddable_on", &self.embeddable_on)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Dapp {
|
|
|
|
pub fn new(pool: CpuPool, path: PathBuf, info: EndpointInfo, cache: PageCache, embeddable_on: Embeddable) -> Self {
|
|
|
|
Dapp {
|
|
|
|
pool,
|
|
|
|
path,
|
2016-09-04 23:44:46 +02:00
|
|
|
mime: None,
|
|
|
|
info: Some(info),
|
2017-10-05 12:35:01 +02:00
|
|
|
cache,
|
|
|
|
embeddable_on,
|
2016-09-04 23:44:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
pub fn single_file(pool: CpuPool, path: PathBuf, mime: Mime, cache: PageCache) -> Self {
|
|
|
|
Dapp {
|
|
|
|
pool,
|
|
|
|
path,
|
2016-09-04 23:44:46 +02:00
|
|
|
mime: Some(mime),
|
|
|
|
info: None,
|
2017-10-05 12:35:01 +02:00
|
|
|
cache,
|
2016-11-09 19:41:47 +01:00
|
|
|
embeddable_on: None,
|
2016-06-03 11:51:11 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 14:04:52 +02:00
|
|
|
|
|
|
|
pub fn path(&self) -> PathBuf {
|
|
|
|
self.path.clone()
|
|
|
|
}
|
2016-12-15 14:46:10 +01:00
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
fn get_file(&self, path: &EndpointPath) -> Option<LocalFile> {
|
|
|
|
if let Some(ref mime) = self.mime {
|
|
|
|
return LocalFile::from_path(&self.path, mime.to_owned());
|
2016-12-15 14:46:10 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
let mut file_path = self.path.to_owned();
|
2016-12-15 14:46:10 +01:00
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
if path.has_no_params() {
|
|
|
|
file_path.push("index.html");
|
2016-12-15 14:46:10 +01:00
|
|
|
} else {
|
2017-10-05 12:35:01 +02:00
|
|
|
for part in &path.app_params {
|
|
|
|
file_path.push(part);
|
|
|
|
}
|
2016-12-15 14:46:10 +01:00
|
|
|
}
|
2016-06-03 11:51:11 +02:00
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
let mime = mime_guess::guess_mime_type(&file_path);
|
|
|
|
LocalFile::from_path(&file_path, mime)
|
2016-06-03 11:51:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
pub fn to_response(&self, path: &EndpointPath) -> Response {
|
|
|
|
let (reader, response) = handler::PageHandler {
|
|
|
|
file: self.get_file(path),
|
|
|
|
cache: self.cache,
|
|
|
|
safe_to_embed_on: self.embeddable_on.clone(),
|
2018-02-15 11:05:20 +01:00
|
|
|
allow_js_eval: self.info.as_ref().and_then(|x| x.allow_js_eval).unwrap_or(false),
|
2017-10-05 12:35:01 +02:00
|
|
|
}.into_response();
|
2016-06-03 11:51:11 +02:00
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
self.pool.spawn(reader).forget();
|
2016-09-04 23:44:46 +02:00
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
Box::new(future::ok(response))
|
2016-06-03 11:51:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
impl Endpoint for Dapp {
|
|
|
|
fn info(&self) -> Option<&EndpointInfo> {
|
|
|
|
self.info.as_ref()
|
|
|
|
}
|
2016-06-03 11:51:11 +02:00
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
fn respond(&self, path: EndpointPath, _req: Request) -> Response {
|
|
|
|
self.to_response(&path)
|
2016-09-04 23:44:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct LocalFile {
|
2017-10-05 12:35:01 +02:00
|
|
|
content_type: Mime,
|
2016-09-04 23:44:46 +02:00
|
|
|
file: fs::File,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalFile {
|
2017-10-05 12:35:01 +02:00
|
|
|
fn from_path<P: AsRef<Path>>(path: P, content_type: Mime) -> Option<Self> {
|
|
|
|
trace!(target: "dapps", "Local file: {:?}", path.as_ref());
|
2016-06-03 11:51:11 +02:00
|
|
|
// Check if file exists
|
2016-09-04 23:44:46 +02:00
|
|
|
fs::File::open(&path).ok().map(|file| {
|
2016-06-03 11:51:11 +02:00
|
|
|
LocalFile {
|
2017-10-05 12:35:01 +02:00
|
|
|
content_type,
|
|
|
|
file,
|
2016-06-03 11:51:11 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl handler::DappFile for LocalFile {
|
2017-10-05 12:35:01 +02:00
|
|
|
type Reader = fs::File;
|
2016-06-03 11:51:11 +02:00
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
fn content_type(&self) -> &Mime {
|
|
|
|
&self.content_type
|
2016-06-03 11:51:11 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
fn into_reader(self) -> Self::Reader {
|
|
|
|
self.file
|
2016-06-03 11:51:11 +02:00
|
|
|
}
|
|
|
|
}
|