5 {-# OPTIONS_HADDOCK prune #-}
7 -- |Manipulation of MIME Types.
8 module Network.HTTP.Lucu.MIMEType
16 import qualified Data.ByteString.Lazy as B
17 import Network.HTTP.Lucu.Parser
18 import Network.HTTP.Lucu.Parser.Http
19 import Network.HTTP.Lucu.Utils
20 import Prelude hiding (min)
22 -- |@'MIMEType' \"major\" \"minor\" [(\"name\", \"value\")]@
23 -- represents \"major\/minor; name=value\".
24 data MIMEType = MIMEType {
27 , mtParams :: ![ (String, String) ]
31 instance Show MIMEType where
32 show (MIMEType maj min params)
33 = maj ++ "/" ++ min ++
37 "; " ++ joinWith "; " (map showPair params)
39 showPair :: (String, String) -> String
40 showPair (name, value)
41 = name ++ "=" ++ if any (not . isToken) value then
47 instance Read MIMEType where
48 readsPrec _ s = [(parseMIMEType s, "")]
50 -- |Parse 'MIMEType' from a 'Prelude.String'. This function throws an
51 -- exception for parse error.
52 parseMIMEType :: String -> MIMEType
53 parseMIMEType str = case parseStr mimeTypeP str of
54 (# Success t, r #) -> if B.null r
56 else error ("unparsable MIME Type: " ++ str)
57 (# _ , _ #) -> error ("unparsable MIME Type: " ++ str)
60 mimeTypeP :: Parser MIMEType
61 mimeTypeP = allowEOF $!
66 return $ MIMEType maj min params
68 paramP :: Parser (String, String)
69 paramP = do _ <- many lws
74 value <- token <|> quotedStr
77 mimeTypeListP :: Parser [MIMEType]
78 mimeTypeListP = allowEOF $! listOf mimeTypeP