From ed87bc33df3e14971e8bcb1472215d6caa9da170 Mon Sep 17 00:00:00 2001 From: Till Hoeppner Date: Thu, 27 Nov 2014 23:54:12 +0100 Subject: Add mimetype detection --- src/sersve.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'src') 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, port: Option, root: Option, - custom_css: Option, filter: Option, max_size: Option, } @@ -49,6 +56,10 @@ impl Assoc> 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 { 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 = 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, -- cgit v1.2.3