From d005410e1ab11b4260574df04d756f416fdc0994 Mon Sep 17 00:00:00 2001 From: maciejhirsz Date: Wed, 15 Feb 2017 18:26:35 +0100 Subject: [PATCH] No .expect on mime types --- Cargo.lock | 1 + ipfs/Cargo.toml | 5 +++-- ipfs/src/lib.rs | 23 +++++++++++++++-------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1b9be74ed..9bc78e975 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1603,6 +1603,7 @@ dependencies = [ "ethcore 1.6.0", "ethcore-util 1.6.0", "hyper 0.10.0-a.0 (git+https://github.com/ethcore/hyper)", + "mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "multihash 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "rlp 0.1.0", ] diff --git a/ipfs/Cargo.toml b/ipfs/Cargo.toml index 46a1dd3aa..d1798b425 100644 --- a/ipfs/Cargo.toml +++ b/ipfs/Cargo.toml @@ -9,6 +9,7 @@ authors = ["Parity Technologies "] ethcore = { path = "../ethcore" } ethcore-util = { path = "../util" } rlp = { path = "../util/rlp" } +mime = "0.2" hyper = { default-features = false, git = "https://github.com/ethcore/hyper" } -cid = "~0.2.0" -multihash = "~0.5.0" +cid = "0.2" +multihash = "0.5" diff --git a/ipfs/src/lib.rs b/ipfs/src/lib.rs index 2a427cd14..776dfe85d 100644 --- a/ipfs/src/lib.rs +++ b/ipfs/src/lib.rs @@ -14,6 +14,8 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . +#[macro_use] +extern crate mime; extern crate hyper; extern crate multihash; extern crate cid; @@ -58,11 +60,18 @@ impl Handler for IpfsHandler { match *self.out() { OctetStream(ref bytes) => { - let headers = res.headers_mut(); + use mime::{Mime, TopLevel, SubLevel}; - headers.set(ContentLength(bytes.len() as u64)); - headers.set(ContentType("application/octet-stream".parse() - .expect("Static content type; qed"))); + // `OctetStream` is not a valid variant, so need to construct + // the type manually. + let content_type = Mime( + TopLevel::Application, + SubLevel::Ext("octet-stream".into()), + vec![] + ); + + res.headers_mut().set(ContentLength(bytes.len() as u64)); + res.headers_mut().set(ContentType(content_type)); Next::write() }, @@ -70,8 +79,7 @@ impl Handler for IpfsHandler { res.set_status(StatusCode::NotFound); res.headers_mut().set(ContentLength(reason.len() as u64)); - res.headers_mut().set(ContentType("text/plain".parse() - .expect("Static content type; qed"))); + res.headers_mut().set(ContentType(mime!(Text/Plain))); Next::write() }, @@ -79,8 +87,7 @@ impl Handler for IpfsHandler { res.set_status(StatusCode::BadRequest); res.headers_mut().set(ContentLength(reason.len() as u64)); - res.headers_mut().set(ContentType("text/plain".parse() - .expect("Static content type; qed"))); + res.headers_mut().set(ContentType(mime!(Text/Plain))); Next::write() }