3 -- |Manipulation of MIME Types.
4 module Network.HTTP.Lucu.MIMEType
14 import Network.HTTP.Lucu.Parser
15 import Network.HTTP.Lucu.Parser.Http
16 import Network.HTTP.Lucu.Utils
18 -- |@'MIMEType' \"major\" \"minor\" [(\"name\", \"value\")]@
19 -- represents \"major\/minor; name=value\".
20 data MIMEType = MIMEType {
23 , mtParams :: [ (String, String) ]
27 instance Show MIMEType where
28 show (MIMEType maj min params)
29 = maj ++ "/" ++ min ++
33 "; " ++ joinWith "; " (map showPair params)
35 showPair :: (String, String) -> String
36 showPair (name, value)
37 = name ++ "=" ++ if any (not . isToken) value then
46 -- |@\"major\" \<\/\> \"minor\"@ constructs a MIME Type
48 (</>) :: String -> String -> MIMEType
52 -- |This operator appends a @(name, value)@ pair to a MIME Type.
53 (<:>) :: MIMEType -> (String, String) -> MIMEType
54 mt@(MIMEType _ _ params) <:> pair
56 mtParams = mtParams mt ++ [pair]
59 -- |This operator takes two strings and makes a tuple of them. So you
62 -- > "text" </> "xml" <:> "charset" <=> "UTF-8" <:> "q" <=> "0.9"
64 -- to represent \"text\/xml; charset=UTF-8; q=0.9\".
65 (<=>) :: String -> String -> (String, String)
66 name <=> value = (name, value)
69 mimeTypeP :: Parser MIMEType
70 mimeTypeP = allowEOF $
75 return $ MIMEType maj min params
77 paramP :: Parser (String, String)
83 value <- token <|> quotedStr
86 mimeTypeListP :: Parser [MIMEType]
87 mimeTypeListP = allowEOF $ listOf mimeTypeP