1 {-# OPTIONS_HADDOCK prune #-}
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
16 import Prelude hiding (min)
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
43 instance Read MIMEType where
44 readsPrec _ s = [(parseMIMEType s, "")]
46 -- |Parse 'MIMEType' from a 'Prelude.String'. This function throws an
47 -- exception for parse error.
48 parseMIMEType :: String -> MIMEType
49 parseMIMEType str = case parseStr mimeTypeP str of
50 (# Success t, r #) -> if B.null r
52 else 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)
65 paramP = do _ <- many lws
70 value <- token <|> quotedStr
73 mimeTypeListP :: Parser [MIMEType]
74 mimeTypeListP = allowEOF $! listOf mimeTypeP