3 -- |Manipulation of MIME Types.
4 module Network.HTTP.Lucu.MIMEType
12 import qualified Data.ByteString.Lazy as B
13 import Network.HTTP.Lucu.Parser
14 import Network.HTTP.Lucu.Parser.Http
15 import Network.HTTP.Lucu.Utils
17 -- |@'MIMEType' \"major\" \"minor\" [(\"name\", \"value\")]@
18 -- represents \"major\/minor; name=value\".
19 data MIMEType = MIMEType {
22 , mtParams :: ![ (String, String) ]
26 instance Show MIMEType where
27 show (MIMEType maj min params)
28 = maj ++ "/" ++ min ++
32 "; " ++ joinWith "; " (map showPair params)
34 showPair :: (String, String) -> String
35 showPair (name, value)
36 = name ++ "=" ++ if any (not . isToken) value then
42 instance Read MIMEType where
43 readsPrec _ s = [(parseMIMEType s, "")]
45 -- |Parse 'MIMEType' from a 'Prelude.String'. This function throws an
46 -- exception for parse error.
47 parseMIMEType :: String -> MIMEType
48 parseMIMEType str = case parseStr mimeTypeP str of
49 (Success t, r) -> if B.null r then
52 error ("unparsable MIME Type: " ++ str)
53 _ -> error ("unparsable MIME Type: " ++ str)
56 mimeTypeP :: Parser MIMEType
57 mimeTypeP = allowEOF $!
62 return $ MIMEType maj min params
64 paramP :: Parser (String, String)
70 value <- token <|> quotedStr
73 mimeTypeListP :: Parser [MIMEType]
74 mimeTypeListP = allowEOF $! listOf mimeTypeP