diff options
author | Till Hoeppner | 2014-11-27 23:54:12 +0100 |
---|---|---|
committer | Till Hoeppner | 2014-11-27 23:54:12 +0100 |
commit | ed87bc33df3e14971e8bcb1472215d6caa9da170 (patch) | |
tree | 34f89f690399658004ba24e13dd621e2ef236e56 /src/sersve.rs | |
parent | 095ff081934bea60e2432e84f96172af1f0d10ff (diff) | |
download | sersve-ed87bc33df3e14971e8bcb1472215d6caa9da170.tar.gz sersve-ed87bc33df3e14971e8bcb1472215d6caa9da170.tar.xz sersve-ed87bc33df3e14971e8bcb1472215d6caa9da170.zip |
Add mimetype detection
Diffstat (limited to 'src/sersve.rs')
-rw-r--r-- | src/sersve.rs | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/sersve.rs b/src/sersve.rs index fb9cb5b..61c70dc 100644 --- a/src/sersve.rs +++ b/src/sersve.rs @@ -1,4 +1,4 @@ -#![feature(globs, slicing_syntax, if_let, unboxed_closures)] +#![feature(phase, globs, slicing_syntax, if_let, unboxed_closures)] extern crate getopts; extern crate serialize; @@ -6,14 +6,22 @@ extern crate iron; extern crate persistent; extern crate error; extern crate regex; +extern crate "conduit-mime-types" as conduit_mime; +extern crate mime; + +#[phase(plugin)] +extern crate lazy_static; use std::{ str, os }; +use std::str::from_str; use std::path::{ Path, GenericPath }; use std::io::{ fs, Reader }; use std::io::fs::{ File, PathExtensions }; use std::default::Default; use std::sync::Mutex; +use conduit_mime::Types; + use regex::Regex; use getopts::{ optopt, optflag, getopts, usage, OptGroup }; @@ -38,7 +46,6 @@ struct Options { host: Option<String>, port: Option<u16>, root: Option<Path>, - custom_css: Option<String>, filter: Option<String>, max_size: Option<u64>, } @@ -49,6 +56,10 @@ impl Assoc<Mutex<Options>> for OptCarrier {} static UNITS: &'static [&'static str] = &["B", "kB", "MB", "GB", "TB"]; const BRIEF: &'static str = "A minimal directory server, written in Rust with Iron."; +lazy_static! { + static ref MIME: Types = Types::new().ok().unwrap(); +} + fn size_with_unit(mut size: u64) -> String { let mut frac = 0; let mut index = 0; @@ -168,7 +179,15 @@ fn serve(req: &mut Request) -> IronResult<Response> { if filter.as_ref().map_or(false, |f| !f.is_match(path.filename_str().unwrap())) { return html("I don't think you're allowed to do this."); } - if guess_text(content[]) { plain(content[]) } else { binary(content[]) } + let mime: Option<iron::mime::Mime> = path.extension_str() + .map_or(None, |e| MIME.get_mime_type(e)) + .map_or(None, |m| from_str(m)); + if mime.as_ref().is_some() { + plain(content[]).map(|r| r.set(ContentType((*mime.as_ref().unwrap()).clone()))) + } else { + plain(content[]) + } + //if guess_text(content[]) { plain(content[]) } else { binary(content[]) } } else { let mut content = match fs::readdir(&path) { Ok(s) => s, |