6 -- |Manipulation of MIME Types.
7 module Network.HTTP.Lucu.MIMEType
16 import Control.Applicative
17 import Data.Ascii (Ascii, AsciiBuilder, CIAscii)
18 import qualified Data.Ascii as A
19 import Data.Attoparsec.Char8 as P
21 import Data.Monoid.Unicode
22 import Data.Text (Text)
23 import Network.HTTP.Lucu.Parser.Http
24 import Network.HTTP.Lucu.RFC2231
25 import Prelude hiding (min)
26 import Prelude.Unicode
28 -- |@'MIMEType' \"major\" \"minor\" [(\"name\", \"value\")]@
29 -- represents \"major\/minor; name=value\".
30 data MIMEType = MIMEType {
33 , mtParams ∷ !(Map CIAscii Text)
36 -- |Convert a 'MIMEType' to 'AsciiBuilder'.
37 printMIMEType ∷ MIMEType → AsciiBuilder
38 printMIMEType (MIMEType maj min params)
39 = A.toAsciiBuilder (A.fromCIAscii maj) ⊕
40 A.toAsciiBuilder "/" ⊕
41 A.toAsciiBuilder (A.fromCIAscii min) ⊕
44 -- |Parse 'MIMEType' from an 'Ascii'. This function throws an
45 -- exception for parse error.
46 parseMIMEType ∷ Ascii → MIMEType
48 = let p = do t ← mimeTypeP
51 bs = A.toByteString str
53 case parseOnly p bs of
55 Left err → error ("unparsable MIME Type: " ⧺ A.toString str ⧺ ": " ⧺ err)
57 mimeTypeP ∷ Parser MIMEType
58 mimeTypeP = do maj ← A.toCIAscii <$> token
60 min ← A.toCIAscii <$> token
62 return $ MIMEType maj min params
64 mimeTypeListP ∷ Parser [MIMEType]
65 mimeTypeListP = listOf mimeTypeP