Fixing HTTP file serving on ws-rs

This commit is contained in:
Tomasz Drwięga 2016-06-27 10:56:26 +02:00
parent 593d947fcd
commit 044bf5511a
3 changed files with 25 additions and 25 deletions

10
Cargo.lock generated
View File

@ -366,10 +366,10 @@ dependencies = [
"ethcore-util 1.3.0", "ethcore-util 1.3.0",
"jsonrpc-core 2.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-core 2.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-minimal-sysui 0.1.0 (git+https://github.com/ethcore/parity-dapps-minimal-sysui-rs.git)", "parity-minimal-sysui 0.2.0 (git+https://github.com/ethcore/parity-dapps-minimal-sysui-rs.git)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"ws 0.5.0 (git+https://github.com/ethcore/ws-rs.git?branch=stable)", "ws 0.5.0 (git+https://github.com/ethcore/ws-rs.git?branch=testing)",
] ]
[[package]] [[package]]
@ -934,8 +934,8 @@ dependencies = [
[[package]] [[package]]
name = "parity-minimal-sysui" name = "parity-minimal-sysui"
version = "0.1.0" version = "0.2.0"
source = "git+https://github.com/ethcore/parity-dapps-minimal-sysui-rs.git#cc5ea4bd786982f0509a8d3d5deb4217c659af85" source = "git+https://github.com/ethcore/parity-dapps-minimal-sysui-rs.git#996c9f3f0ebedc727aecb4ece191154e956ae292"
[[package]] [[package]]
name = "phf" name = "phf"
@ -1448,7 +1448,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "ws" name = "ws"
version = "0.5.0" version = "0.5.0"
source = "git+https://github.com/ethcore/ws-rs.git?branch=stable#abd219709b9dd504a7101d9ba772e0df523fe534" source = "git+https://github.com/ethcore/ws-rs.git?branch=testing#4623c51aedaea1ca0ecb34bd1a1aacb651760954"
dependencies = [ dependencies = [
"httparse 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "httparse 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -15,10 +15,10 @@ rand = "0.3.14"
jsonrpc-core = "2.0" jsonrpc-core = "2.0"
log = "0.3" log = "0.3"
env_logger = "0.3" env_logger = "0.3"
ws = { git = "https://github.com/ethcore/ws-rs.git", branch = "stable" } ws = { git = "https://github.com/ethcore/ws-rs.git", branch = "testing" }
ethcore-util = { path = "../util" } ethcore-util = { path = "../util" }
ethcore-rpc = { path = "../rpc" } ethcore-rpc = { path = "../rpc" }
parity-minimal-sysui = { git = "https://github.com/ethcore/parity-dapps-minimal-sysui-rs.git" } parity-minimal-sysui = { git = "https://github.com/ethcore/parity-dapps-minimal-sysui-rs.git", version = "0.2.0" }
clippy = { version = "0.0.77", optional = true} clippy = { version = "0.0.77", optional = true}

View File

@ -62,6 +62,19 @@ fn auth_is_valid(codes: &Path, protocols: ws::Result<Vec<&str>>) -> bool {
} }
} }
fn add_headers(mut response: ws::Response, mime: &str) -> ws::Response {
let content_len = format!("{}", response.len());
{
let mut headers = response.headers_mut();
headers.push(("X-Frame-Options".into(), b"SAMEORIGIN".to_vec()));
headers.push(("Server".into(), b"Parity/SignerUI".to_vec()));
headers.push(("Content-Length".into(), content_len.as_bytes().to_vec()));
headers.push(("Content-Type".into(), mime.as_bytes().to_vec()));
headers.push(("Connection".into(), b"close".to_vec()));
}
response
}
pub struct Session { pub struct Session {
out: ws::Sender, out: ws::Sender,
self_origin: String, self_origin: String,
@ -98,26 +111,13 @@ impl ws::Handler for Session {
} }
// Otherwise try to serve a page. // Otherwise try to serve a page.
sysui::handle(req.resource()) Ok(sysui::handle(req.resource())
.map_or_else( .map_or_else(
// return error // return 404 not found
|| Ok(ws::Response::not_found("Page not found".into())), || add_headers(ws::Response::not_found("Not found".into()), "text/plain"),
// or serve the file // or serve the file
|f| { |f| add_headers(ws::Response::ok(f.content.into()), &f.mime)
let content_len = format!("{}", f.content.as_bytes().len()); ))
let mut res = ws::Response::ok(f.content.into());
{
let mut headers = res.headers_mut();
headers.push(("Server".into(), b"Parity/SignerUI".to_vec()));
headers.push(("Connection".into(), b"Closed".to_vec()));
headers.push(("Content-Length".into(), content_len.as_bytes().to_vec()));
headers.push(("Content-Type".into(), f.mime.as_bytes().to_vec()));
if !f.safe_to_embed {
headers.push(("X-Frame-Options".into(), b"SAMEORIGIN".to_vec()));
}
}
Ok(res)
})
} }
fn on_message(&mut self, msg: ws::Message) -> ws::Result<()> { fn on_message(&mut self, msg: ws::Message) -> ws::Result<()> {