Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 103 lines (87 sloc) 3.921 kB
daaafff @alsonkemp Initial commit
authored
1 -- -----------------------------------------------------------------------------
2 -- Copyright 2002, Simon Marlow.
3 -- Copyright 2006, Bjorn Bringert.
4 -- All rights reserved.
5 --
6 -- Redistribution and use in source and binary forms, with or without
7 -- modification, are permitted provided that the following conditions are
8 -- met:
9 --
10 -- * Redistributions of source code must retain the above copyright notice,
11 -- this list of conditions and the following disclaimer.
12 --
13 -- * Redistributions in binary form must reproduce the above copyright
14 -- notice, this list of conditions and the following disclaimer in the
15 -- documentation and/or other materials provided with the distribution.
16 --
17 -- * Neither the name of the copyright holder(s) nor the names of
18 -- contributors may be used to endorse or promote products derived from
19 -- this software without specific prior written permission.
20 --
21 -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 -- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 -- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 -- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 -- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 -- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 -- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 -- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 -- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 -- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 -- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 -- -----------------------------------------------------------------------------
33
34 module Turbinado.Environment.MimeTypes (
35 setMimeTypes,
36 mimeTypeOf,
37 addMimeTypesToEnvironment
38 ) where
39
40 import Data.List
41 import Data.Map (Map)
42 import Data.Typeable
43 import qualified Data.Map as Map hiding (Map)
44 import Text.ParserCombinators.Parsec
c59c8d3 Checking in all of the Environment + Website changes
alson authored
45 import Control.Monad.State
46 import Control.Monad.Trans
daaafff @alsonkemp Initial commit
authored
47
c59c8d3 Checking in all of the Environment + Website changes
alson authored
48 import Turbinado.Environment.Types
daaafff @alsonkemp Initial commit
authored
49
c59c8d3 Checking in all of the Environment + Website changes
alson authored
50 setMimeTypes :: MonadState Environment (mt Environment IO) => MimeTypes -> mt Environment IO ()
51 setMimeTypes mi = do e <- get
52 put $ e {getMimeTypes = Just mi}
daaafff @alsonkemp Initial commit
authored
53
c59c8d3 Checking in all of the Environment + Website changes
alson authored
54 addMimeTypesToEnvironment :: (MonadState Environment (mt Environment IO), MonadIO (mt Environment IO)) => FilePath -> mt Environment IO ()
55 addMimeTypesToEnvironment mime_types_file =
56 do stuff <- liftIO $ readFile mime_types_file
57 setMimeTypes (MimeTypes $ Map.fromList (parseMimeTypes stuff))
daaafff @alsonkemp Initial commit
authored
58
59
60 mimeTypeOf :: MimeTypes -> FilePath -> Maybe MimeType
61 mimeTypeOf (MimeTypes mime_types) filename =
62 do let ext = extension filename
63 if null ext
64 then Nothing
65 else Map.lookup ext mime_types
66
67 extension :: String -> String
68 extension fn = go (reverse fn) ""
69 where go [] _ = ""
70 go ('.':_) ext = ext
71 go (x:s) ext = go s (x:ext)
72
73 parseMimeTypes :: String -> [(String,MimeType)]
74 parseMimeTypes file =
75 [ (ext,val)
76 | Just (val,exts) <- map (parseMimeLine . takeWhile (/= '#')) (lines file)
77 , ext <- exts
78 ]
79
80 parseMimeLine :: String -> Maybe (MimeType, [String])
81 parseMimeLine l = case parse pMimeLine "MIME line" l of
82 Left _ -> Nothing
83 Right m -> Just m
84
85 pMimeLine :: Parser (MimeType, [String])
86 pMimeLine = do t <- pMimeType
87 es <- (spaces >> sepBy pToken spaces)
88 return (t, es)
89
90 pMimeType :: Parser MimeType
91 pMimeType = do part1 <- pToken
92 char '/'
93 part2 <- pToken
94 return $ MimeType part1 part2
95
96 especials, tokenchar :: [Char]
97 especials = "()<>@,;:\\\"/[]?.="
98 tokenchar = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" \\ especials
99
100 pToken :: Parser String
101 pToken = many1 (oneOf tokenchar)
102
Something went wrong with that request. Please try again.