5 {-# OPTIONS_HADDOCK prune #-}
7 -- |Manipulation of MIME Types.
8 module Network.HTTP.Lucu.MIMEType
17 import Control.Applicative
18 import Data.Ascii (Ascii, AsciiBuilder, CIAscii)
19 import qualified Data.Ascii as A
20 import Data.Attoparsec.Char8 as P
21 import qualified Data.ByteString.Char8 as C8
23 import Data.Monoid.Unicode
24 import Data.Text (Text)
25 import Network.HTTP.Lucu.Parser.Http
26 import Network.HTTP.Lucu.RFC2231
27 import Prelude hiding (min)
28 import Prelude.Unicode
30 -- |@'MIMEType' \"major\" \"minor\" [(\"name\", \"value\")]@
31 -- represents \"major\/minor; name=value\".
32 data MIMEType = MIMEType {
35 , mtParams ∷ !(Map CIAscii Text)
38 -- |Convert a 'MIMEType' to 'AsciiBuilder'.
39 printMIMEType ∷ MIMEType → AsciiBuilder
40 printMIMEType (MIMEType maj min params)
41 = A.toAsciiBuilder (A.fromCIAscii maj) ⊕
42 A.toAsciiBuilder "/" ⊕
43 A.toAsciiBuilder (A.fromCIAscii min) ⊕
46 -- |Parse 'MIMEType' from an 'Ascii'. This function throws an
47 -- exception for parse error.
48 parseMIMEType ∷ Ascii → MIMEType
50 = let p = do t ← mimeTypeP
53 bs = A.toByteString str
55 case parseOnly p bs of
57 Left err → error ("unparsable MIME Type: " ⧺ C8.unpack bs ⧺ ": " ⧺ err)
59 mimeTypeP ∷ Parser MIMEType
60 mimeTypeP = do maj ← A.toCIAscii <$> token
62 min ← A.toCIAscii <$> token
64 return $ MIMEType maj min params
66 mimeTypeListP ∷ Parser [MIMEType]
67 mimeTypeListP = listOf mimeTypeP