@@ -8,7 +8,7 @@ use iron::{
8
8
} ;
9
9
use mime_guess:: MimeGuess ;
10
10
use router:: Router ;
11
- use std:: { fs, path:: Path } ;
11
+ use std:: { ffi :: OsStr , fs, path:: Path } ;
12
12
13
13
const STYLE_CSS : & str = include_str ! ( concat!( env!( "OUT_DIR" ) , "/style.css" ) ) ;
14
14
const MENU_JS : & str = include_str ! ( concat!( env!( "OUT_DIR" ) , "/menu.js" ) ) ;
@@ -49,9 +49,22 @@ fn serve_file(req: &Request, file: &str) -> IronResult<Response> {
49
49
let contents = ctry ! ( req, fs:: read( & path) ) ;
50
50
51
51
// If we can detect the file's mime type, set it
52
- let content_type = MimeGuess :: from_path ( & path)
53
- . first ( )
54
- . map ( |mime| ContentType ( mime. as_ref ( ) . parse ( ) . unwrap ( ) ) ) ;
52
+ // MimeGuess misses a lot of the file types we need, so there's a small wrapper
53
+ // around it
54
+ let content_type = path
55
+ . extension ( )
56
+ . and_then ( OsStr :: to_str)
57
+ . and_then ( |ext| match ext {
58
+ "eot" => Some ( ContentType (
59
+ "application/vnd.ms-fontobject" . parse ( ) . unwrap ( ) ,
60
+ ) ) ,
61
+ "woff2" => Some ( ContentType ( "application/font-woff2" . parse ( ) . unwrap ( ) ) ) ,
62
+ "ttf" => Some ( ContentType ( "application/x-font-ttf" . parse ( ) . unwrap ( ) ) ) ,
63
+
64
+ _ => MimeGuess :: from_path ( & path)
65
+ . first ( )
66
+ . map ( |mime| ContentType ( mime. as_ref ( ) . parse ( ) . unwrap ( ) ) ) ,
67
+ } ) ;
55
68
56
69
serve_resource ( contents, content_type)
57
70
}
@@ -200,10 +213,40 @@ mod tests {
200
213
. send( ) ?
201
214
. status( )
202
215
. as_u16( ) ,
203
- 404
216
+ 404 ,
204
217
) ;
205
218
206
219
Ok ( ( ) )
207
220
} ) ;
208
221
}
222
+
223
+ #[ test]
224
+ fn static_mime_types ( ) {
225
+ wrapper ( |env| {
226
+ let web = env. frontend ( ) ;
227
+
228
+ let files = & [
229
+ ( "pure-min.css" , "text/css" ) ,
230
+ ( "fa-brands-400.eot" , "application/vnd.ms-fontobject" ) ,
231
+ ( "fa-brands-400.svg" , "image/svg+xml" ) ,
232
+ ( "fa-brands-400.ttf" , "application/x-font-ttf" ) ,
233
+ ( "fa-brands-400.woff" , "application/font-woff" ) ,
234
+ ( "fa-brands-400.woff2" , "application/font-woff2" ) ,
235
+ ] ;
236
+
237
+ for ( file, mime) in files {
238
+ let url = format ! ( "/-/static/{}" , file) ;
239
+ let resp = web. get ( & url) . send ( ) ?;
240
+
241
+ assert_eq ! (
242
+ resp. headers( ) . get( "Content-Type" ) ,
243
+ Some ( & mime. parse( ) . unwrap( ) ) ,
244
+ "{:?} has an incorrect content type" ,
245
+ url,
246
+ ) ;
247
+ }
248
+
249
+ Ok ( ( ) )
250
+ } ) ;
251
+ }
209
252
}
0 commit comments