Skip to content

Commit 39af37f

Browse files
committed
Add support for Mime Types
1 parent 67e13e8 commit 39af37f

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ git = "https://github.com/wycats/rust-civet.git"
2121

2222
git = "https://github.com/conduit-rust/conduit.git"
2323

24+
[dependencies.mime-types]
25+
26+
git = "https://github.com/conduit-rust/mime-types.git"
27+
2428
[dependencies.conduit-test]
2529

2630
git = "https://github.com/conduit-rust/conduit-test.git"

src/conduit-static.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
extern crate mime = "mime-types";
12
extern crate conduit;
23

34
use std::fmt::Show;
@@ -7,12 +8,17 @@ use std::io::util::NullReader;
78
use conduit::{Request, Response, Handler};
89

910
pub struct Static {
10-
path: Path
11+
path: Path,
12+
types: mime::Types
1113
}
1214

1315
impl Static {
1416
pub fn new(path: Path) -> Static {
15-
Static { path: path }
17+
Static {
18+
path: path,
19+
types: mime::Types::new()
20+
.ok().expect("Couldn't load mime-types")
21+
}
1622
}
1723
}
1824

@@ -29,11 +35,15 @@ impl Handler for Static {
2935
})
3036
}
3137

38+
let mime = self.types.mime_for_path(&path);
3239
let file = try!(File::open(&path).map_err(|e| box e as Box<Show>));
3340

41+
let mut headers = HashMap::new();
42+
headers.insert("Content-Type".to_str(), vec!(mime.to_str()));
43+
3444
Ok(Response {
3545
status: (200, "OK"),
36-
headers: HashMap::new(),
46+
headers: headers,
3747
body: box file as Box<Reader + Send>
3848
})
3949
}
@@ -54,5 +64,15 @@ mod tests {
5464
let mut res = handler.call(&mut req).ok().expect("No response");
5565
let body = res.body.read_to_str().ok().expect("No body");
5666
assert!(body.as_slice().contains("[package]"), "The Cargo.toml was provided");
67+
assert_eq!(res.headers.find_equiv(&"Content-Type").expect("No content-type"), &vec!("text/plain".to_str()));
68+
}
69+
70+
#[test]
71+
fn test_mime_types() {
72+
let root = Path::new(file!()).dir_path().dir_path();
73+
let handler = Static::new(root);
74+
let mut req = test::MockRequest::new(conduit::Get, "/src/fixture.css");
75+
let res = handler.call(&mut req).ok().expect("No response");
76+
assert_eq!(res.headers.find_equiv(&"Content-Type").expect("No content-type"), &vec!("text/css".to_str()));
5777
}
5878
}

0 commit comments

Comments
 (0)