1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
extern crate iron; extern crate hyper; extern crate conduit_mime_types as mime_types; #[macro_use] extern crate lazy_static; use iron::prelude::*; use iron::status; use iron::Handler; use iron::url::percent_encoding::percent_decode; use hyper::mime::Mime; use std::iter::FromIterator; use std::path::{Path, PathBuf}; use std::collections::HashMap; lazy_static! { static ref MIME_TYPES: mime_types::Types = mime_types::Types::new().unwrap(); } fn decode_percents(string: &&str) -> String { percent_decode(string.as_bytes()).decode_utf8().unwrap().into_owned() } #[derive(Clone)] pub struct EmbedStatic { map: HashMap<&'static Path, Vec<u8>>, } impl EmbedStatic { pub fn new(map: HashMap<&'static Path, Vec<u8>>) -> EmbedStatic { EmbedStatic { map: map } } } impl Handler for EmbedStatic { fn handle(&self, req: &mut Request) -> IronResult<Response> { let req_path = PathBuf::from_iter(req.url.path().iter().map(decode_percents)); match self.map.get(req_path.as_path()) { Some(b) => { let mut res = Response::with((status::Ok, b.as_slice())); let mime_str = MIME_TYPES.mime_for_path(req_path.as_path()); let _ = mime_str.parse().map(|mime: Mime| res.set_mut(mime)); Ok(res) } None => Ok(Response::with((status::NotFound))), } } }