Cleaned up Web.Mime, using Strings directly
This commit is contained in:
parent
fa98452452
commit
d0c9386d64
70
Web/Mime.hs
70
Web/Mime.hs
@ -3,10 +3,14 @@
|
|||||||
{-# LANGUAGE CPP #-}
|
{-# LANGUAGE CPP #-}
|
||||||
-- | Generic MIME type module. Could be spun off into its own package.
|
-- | Generic MIME type module. Could be spun off into its own package.
|
||||||
module Web.Mime
|
module Web.Mime
|
||||||
( ContentType (..)
|
( -- * Data type and conversions
|
||||||
, contentTypeFromBS
|
ContentType (..)
|
||||||
|
, contentTypeFromString
|
||||||
|
, contentTypeToString
|
||||||
|
-- * File extensions
|
||||||
, typeByExt
|
, typeByExt
|
||||||
, ext
|
, ext
|
||||||
|
-- * Utilities
|
||||||
, simpleContentType
|
, simpleContentType
|
||||||
#if TEST
|
#if TEST
|
||||||
, testSuite
|
, testSuite
|
||||||
@ -14,8 +18,6 @@ module Web.Mime
|
|||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Function (on)
|
import Data.Function (on)
|
||||||
import Data.Convertible.Text
|
|
||||||
import Data.ByteString.Char8 (pack, ByteString, unpack)
|
|
||||||
|
|
||||||
#if TEST
|
#if TEST
|
||||||
import Test.Framework (testGroup, Test)
|
import Test.Framework (testGroup, Test)
|
||||||
@ -26,6 +28,11 @@ import Test.QuickCheck
|
|||||||
import Control.Monad (when)
|
import Control.Monad (when)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
-- | Equality is determined by converting to a 'String' via
|
||||||
|
-- 'contentTypeToString'. This ensures that, for example, 'TypeJpeg' is the
|
||||||
|
-- same as 'TypeOther' \"image/jpeg\". However, note that 'TypeHtml' is *not*
|
||||||
|
-- the same as 'TypeOther' \"text/html\", since 'TypeHtml' is defined as UTF-8
|
||||||
|
-- encoded. See 'contentTypeToString'.
|
||||||
data ContentType =
|
data ContentType =
|
||||||
TypeHtml
|
TypeHtml
|
||||||
| TypePlain
|
| TypePlain
|
||||||
@ -43,33 +50,41 @@ data ContentType =
|
|||||||
| TypeOther String
|
| TypeOther String
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
||||||
instance ConvertSuccess ContentType ByteString where
|
-- | This is simply a synonym for 'TypeOther'. However, equality works as
|
||||||
convertSuccess = pack . cs
|
-- expected; see 'ContentType'.
|
||||||
|
contentTypeFromString :: String -> ContentType
|
||||||
|
contentTypeFromString = TypeOther
|
||||||
|
|
||||||
instance ConvertSuccess ContentType [Char] where
|
-- | This works as expected, with one caveat: the builtin textual content types
|
||||||
convertSuccess TypeHtml = "text/html; charset=utf-8"
|
-- ('TypeHtml', 'TypePlain', etc) all include \"; charset=utf-8\" at the end of
|
||||||
convertSuccess TypePlain = "text/plain; charset=utf-8"
|
-- their basic content-type. If another encoding is desired, please use
|
||||||
convertSuccess TypeJson = "application/json; charset=utf-8"
|
-- 'TypeOther'.
|
||||||
convertSuccess TypeXml = "text/xml"
|
contentTypeToString :: ContentType -> String
|
||||||
convertSuccess TypeAtom = "application/atom+xml"
|
contentTypeToString TypeHtml = "text/html; charset=utf-8"
|
||||||
convertSuccess TypeJpeg = "image/jpeg"
|
contentTypeToString TypePlain = "text/plain; charset=utf-8"
|
||||||
convertSuccess TypePng = "image/png"
|
contentTypeToString TypeJson = "application/json; charset=utf-8"
|
||||||
convertSuccess TypeGif = "image/gif"
|
contentTypeToString TypeXml = "text/xml"
|
||||||
convertSuccess TypeJavascript = "text/javascript; charset=utf-8"
|
contentTypeToString TypeAtom = "application/atom+xml"
|
||||||
convertSuccess TypeCss = "text/css; charset=utf-8"
|
contentTypeToString TypeJpeg = "image/jpeg"
|
||||||
convertSuccess TypeFlv = "video/x-flv"
|
contentTypeToString TypePng = "image/png"
|
||||||
convertSuccess TypeOgv = "video/ogg"
|
contentTypeToString TypeGif = "image/gif"
|
||||||
convertSuccess TypeOctet = "application/octet-stream"
|
contentTypeToString TypeJavascript = "text/javascript; charset=utf-8"
|
||||||
convertSuccess (TypeOther s) = s
|
contentTypeToString TypeCss = "text/css; charset=utf-8"
|
||||||
|
contentTypeToString TypeFlv = "video/x-flv"
|
||||||
|
contentTypeToString TypeOgv = "video/ogg"
|
||||||
|
contentTypeToString TypeOctet = "application/octet-stream"
|
||||||
|
contentTypeToString (TypeOther s) = s
|
||||||
|
|
||||||
simpleContentType :: ContentType -> String
|
-- | Removes \"extra\" information at the end of a content type string. In
|
||||||
simpleContentType = fst . span (/= ';') . cs
|
-- particular, removes everything after the semicolon, if present.
|
||||||
|
--
|
||||||
|
-- For example, \"text/html; charset=utf-8\" is commonly used to specify the
|
||||||
|
-- character encoding for HTML data. This function would return \"text/html\".
|
||||||
|
simpleContentType :: String -> String
|
||||||
|
simpleContentType = fst . span (/= ';')
|
||||||
|
|
||||||
instance Eq ContentType where
|
instance Eq ContentType where
|
||||||
(==) = (==) `on` (cs :: ContentType -> String)
|
(==) = (==) `on` contentTypeToString
|
||||||
|
|
||||||
contentTypeFromBS :: ByteString -> ContentType
|
|
||||||
contentTypeFromBS = TypeOther . unpack
|
|
||||||
|
|
||||||
-- | Determine a mime-type based on the file extension.
|
-- | Determine a mime-type based on the file extension.
|
||||||
typeByExt :: String -> ContentType
|
typeByExt :: String -> ContentType
|
||||||
@ -106,5 +121,4 @@ caseTypeByExt :: Assertion
|
|||||||
caseTypeByExt = do
|
caseTypeByExt = do
|
||||||
TypeJavascript @=? typeByExt (ext "foo.js")
|
TypeJavascript @=? typeByExt (ext "foo.js")
|
||||||
TypeHtml @=? typeByExt (ext "foo.html")
|
TypeHtml @=? typeByExt (ext "foo.html")
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -141,7 +141,7 @@ cleanupSegments :: [B.ByteString] -> [String]
|
|||||||
cleanupSegments = decodePathInfo . intercalate "/" . map B.unpack
|
cleanupSegments = decodePathInfo . intercalate "/" . map B.unpack
|
||||||
|
|
||||||
httpAccept :: W.Request -> [ContentType]
|
httpAccept :: W.Request -> [ContentType]
|
||||||
httpAccept = map contentTypeFromBS
|
httpAccept = map (contentTypeFromString . B.unpack)
|
||||||
. parseHttpAccept
|
. parseHttpAccept
|
||||||
. fromMaybe B.empty
|
. fromMaybe B.empty
|
||||||
. lookup W.Accept
|
. lookup W.Accept
|
||||||
|
|||||||
@ -133,12 +133,13 @@ instance HasReps () where
|
|||||||
|
|
||||||
instance HasReps [(ContentType, Content)] where
|
instance HasReps [(ContentType, Content)] where
|
||||||
chooseRep a cts = return $
|
chooseRep a cts = return $
|
||||||
case filter (\(ct, _) -> simpleContentType ct `elem`
|
case filter (\(ct, _) -> go ct `elem` map go cts) a of
|
||||||
map simpleContentType cts) a of
|
|
||||||
((ct, c):_) -> (ct, c)
|
((ct, c):_) -> (ct, c)
|
||||||
_ -> case a of
|
_ -> case a of
|
||||||
(x:_) -> x
|
(x:_) -> x
|
||||||
_ -> error "chooseRep [(ContentType, Content)] of empty"
|
_ -> error "chooseRep [(ContentType, Content)] of empty"
|
||||||
|
where
|
||||||
|
go = simpleContentType . contentTypeToString
|
||||||
|
|
||||||
-- | Data with a single representation.
|
-- | Data with a single representation.
|
||||||
staticRep :: ConvertSuccess x Content
|
staticRep :: ConvertSuccess x Content
|
||||||
@ -227,7 +228,7 @@ headerToPair (Header key value) =
|
|||||||
responseToWaiResponse :: Response -> IO W.Response
|
responseToWaiResponse :: Response -> IO W.Response
|
||||||
responseToWaiResponse (Response sc hs ct c) = do
|
responseToWaiResponse (Response sc hs ct c) = do
|
||||||
hs' <- mapM headerToPair hs
|
hs' <- mapM headerToPair hs
|
||||||
let hs'' = (W.ContentType, cs ct) : hs'
|
let hs'' = (W.ContentType, cs $ contentTypeToString ct) : hs'
|
||||||
return $ W.Response sc hs'' $ case c of
|
return $ W.Response sc hs'' $ case c of
|
||||||
ContentFile fp -> Left fp
|
ContentFile fp -> Left fp
|
||||||
ContentEnum e -> Right $ W.Enumerator e
|
ContentEnum e -> Right $ W.Enumerator e
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user