3 -- |Manipulation of MIME Types.
4 module Network.HTTP.Lucu.MIMEType
12 import Network.HTTP.Lucu.Parser
13 import Network.HTTP.Lucu.Parser.Http
14 import Network.HTTP.Lucu.Utils
16 -- |@'MIMEType' \"major\" \"minor\" [(\"name\", \"value\")]@
17 -- represents \"major\/minor; name=value\".
18 data MIMEType = MIMEType {
21 , mtParams :: ![ (String, String) ]
25 instance Show MIMEType where
26 show (MIMEType maj min params)
27 = maj ++ "/" ++ min ++
31 "; " ++ joinWith "; " (map showPair params)
33 showPair :: (String, String) -> String
34 showPair (name, value)
35 = name ++ "=" ++ if any (not . isToken) value then
41 instance Read MIMEType where
42 readsPrec _ s = [(parseMIMEType s, "")]
44 -- |Parse 'MIMEType' from a 'Prelude.String'. This function throws an
45 -- exception for parse error.
46 parseMIMEType :: String -> MIMEType
47 parseMIMEType str = case parseStr mimeTypeP str of
49 _ -> error ("Unparsable MIME Type: " ++ str)
52 mimeTypeP :: Parser MIMEType
53 mimeTypeP = allowEOF $!
58 return $ MIMEType maj min params
60 paramP :: Parser (String, String)
66 value <- token <|> quotedStr
69 mimeTypeListP :: Parser [MIMEType]
70 mimeTypeListP = allowEOF $! listOf mimeTypeP