5 {-# OPTIONS_HADDOCK prune #-}
7 -- |Manipulation of MIME Types.
8 module Network.HTTP.Lucu.MIMEType
17 import Data.Ascii (Ascii, CIAscii)
18 import qualified Data.Ascii as A
19 import qualified Data.ByteString.Lazy as B
20 import Network.HTTP.Lucu.Parser.Http
21 import Network.HTTP.Lucu.Utils
22 import Prelude hiding (min)
24 -- |@'MIMEType' \"major\" \"minor\" [(\"name\", \"value\")]@
25 -- represents \"major\/minor; name=value\".
26 data MIMEType = MIMEType {
29 , mtParams :: ![ (CIAscii, Ascii) ]
32 -- |Convert a 'MIMEType' to 'Ascii'.
33 printMIMEType ∷ MIMEType → Ascii
34 printMIMEType (MIMEType maj min params)
35 = A.fromAsciiBuilder $
36 ( A.toAsciiBuilder maj ⊕
37 A.toAsciiBuilder "/" ⊕
38 A.toAsciiBuilder min ⊕
42 A.toAsciiBuilder "; " ⊕
43 joinWith "; " (map printPair params)
46 printPair ∷ (CIAscii, Ascii) → A.AsciiBuilder
47 printPair (name, value)
48 = A.toAsciiBuilder (A.fromCIAscii name) ⊕
49 A.toAsciiBuilder "=" ⊕
50 if any ((¬) ∘ isToken) value then
53 A.toAsciiBuilder value
55 -- |Parse 'MIMEType' from a 'Prelude.String'. This function throws an
56 -- exception for parse error.
57 parseMIMEType :: String -> MIMEType
58 parseMIMEType str = case parseStr mimeTypeP str of
59 (# Success t, r #) -> if B.null r
61 else error ("unparsable MIME Type: " ++ str)
62 (# _ , _ #) -> error ("unparsable MIME Type: " ++ str)
65 mimeTypeP :: Parser MIMEType
66 mimeTypeP = allowEOF $!
71 return $ MIMEType maj min params
73 paramP :: Parser (String, String)
74 paramP = do _ <- many lws
79 value <- token <|> quotedStr
82 mimeTypeListP :: Parser [MIMEType]
83 mimeTypeListP = allowEOF $! listOf mimeTypeP