@@ -357,15 +357,28 @@ fn maybe_get_compressed(compressed: &[u8], contents: &[u8]) -> Option<LitByteStr
357357 . then ( || LitByteStr :: new ( compressed, Span :: call_site ( ) ) )
358358}
359359
360- fn file_content_type ( path : & Path ) -> Result < & ' static str , error:: Error > {
360+ /// Use `mime_guess` to get the best guess of the file's MIME type
361+ /// by looking at its extension, or return an error if unable.
362+ ///
363+ /// We accept the first guess because [`mime_guess` updates the order
364+ /// according to the latest IETF RTC](https://docs.rs/mime_guess/2.0.5/mime_guess/struct.MimeGuess.html#note-ordering)
365+ fn file_content_type ( path : & Path ) -> Result < String , error:: Error > {
361366 match path. extension ( ) {
362- Some ( ext) if ext. eq_ignore_ascii_case ( "css" ) => Ok ( "text/css" ) ,
363- Some ( ext) if ext. eq_ignore_ascii_case ( "js" ) => Ok ( "text/javascript" ) ,
364- Some ( ext) if ext. eq_ignore_ascii_case ( "txt" ) => Ok ( "text/plain" ) ,
365- Some ( ext) if ext. eq_ignore_ascii_case ( "woff" ) => Ok ( "font/woff" ) ,
366- Some ( ext) if ext. eq_ignore_ascii_case ( "woff2" ) => Ok ( "font/woff2" ) ,
367- Some ( ext) if ext. eq_ignore_ascii_case ( "svg" ) => Ok ( "image/svg+xml" ) ,
368- ext => Err ( error:: Error :: UnknownFileExtension ( ext. map ( Into :: into) ) ) ,
367+ Some ( ext) => {
368+ let guesses = mime_guess:: MimeGuess :: from_ext (
369+ ext. to_str ( )
370+ . ok_or ( error:: Error :: InvalidFileExtension ( path. into ( ) ) ) ?,
371+ ) ;
372+
373+ if let Some ( guess) = guesses. first_raw ( ) {
374+ Ok ( guess. to_owned ( ) )
375+ } else {
376+ Err ( error:: Error :: UnknownFileExtension (
377+ path. extension ( ) . map ( Into :: into) ,
378+ ) )
379+ }
380+ }
381+ None => Err ( error:: Error :: UnknownFileExtension ( None ) ) ,
369382 }
370383}
371384
0 commit comments