1
+ extern crate mime = "mime-types" ;
1
2
extern crate conduit;
2
3
3
4
use std:: fmt:: Show ;
@@ -7,12 +8,17 @@ use std::io::util::NullReader;
7
8
use conduit:: { Request , Response , Handler } ;
8
9
9
10
pub struct Static {
10
- path : Path
11
+ path : Path ,
12
+ types : mime:: Types
11
13
}
12
14
13
15
impl Static {
14
16
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
+ }
16
22
}
17
23
}
18
24
@@ -29,11 +35,15 @@ impl Handler for Static {
29
35
} )
30
36
}
31
37
38
+ let mime = self . types . mime_for_path ( & path) ;
32
39
let file = try!( File :: open ( & path) . map_err ( |e| box e as Box < Show > ) ) ;
33
40
41
+ let mut headers = HashMap :: new ( ) ;
42
+ headers. insert ( "Content-Type" . to_str ( ) , vec ! ( mime. to_str( ) ) ) ;
43
+
34
44
Ok ( Response {
35
45
status : ( 200 , "OK" ) ,
36
- headers : HashMap :: new ( ) ,
46
+ headers : headers ,
37
47
body : box file as Box < Reader + Send >
38
48
} )
39
49
}
@@ -54,5 +64,15 @@ mod tests {
54
64
let mut res = handler. call ( & mut req) . ok ( ) . expect ( "No response" ) ;
55
65
let body = res. body . read_to_str ( ) . ok ( ) . expect ( "No body" ) ;
56
66
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( ) ) ) ;
57
77
}
58
78
}
0 commit comments