Debian Codegraph @

src/Network/HTTP/Media/MediaType.hs

raw
1 -- | Defines the 'MediaType' accept header with an 'Accept' instance for use
2 -- in content-type negotiation.
3 module Network.HTTP.Media.MediaType
4 ( -- * Type and creation
5 MediaType,
6 Parameters,
7 (//),
8 (/:),
9
10 -- * Querying
11 mainType,
12 subType,
13 parameters,
14 (/?),
15 (/.),
16 )
17 where
18
19 import Data.ByteString (ByteString)
20 import qualified Data.ByteString.Char8 as BS
21 import Data.CaseInsensitive (CI)
22 import qualified Data.CaseInsensitive as CI
23 import Data.Map (empty, insert)
24 import qualified Data.Map as Map
25 import Network.HTTP.Media.MediaType.Internal (MediaType (MediaType))
26 import Network.HTTP.Media.MediaType.Internal hiding (MediaType (..))
27 import qualified Network.HTTP.Media.MediaType.Internal as Internal
28 import Network.HTTP.Media.Utils
29
30 -- | Retrieves the main type of a 'MediaType'.
31 mainType :: MediaType -> CI ByteString
32 mainType = Internal.mainType
33
34 -- | Retrieves the sub type of a 'MediaType'.
35 subType :: MediaType -> CI ByteString
36 subType = Internal.subType
37
38 -- | Retrieves the parameters of a 'MediaType'.
39 parameters :: MediaType -> Parameters
40 parameters = Internal.parameters
41
42 -- | Builds a 'MediaType' without parameters. Can produce an error if
43 -- either type is invalid.
44 (//) :: ByteString -> ByteString -> MediaType
45 a // b
46 | a == "*" && b == "*" = MediaType (CI.mk a) (CI.mk b) empty
47 | b == "*" = MediaType (ensureR a) (CI.mk b) empty
48 | otherwise = MediaType (ensureR a) (ensureR b) empty
49
50 -- | Adds a parameter to a 'MediaType'. Can produce an error if either
51 -- string is invalid.
52 (/:) :: MediaType -> (ByteString, ByteString) -> MediaType
53 (MediaType a b p) /: (k, v) = MediaType a b $ insert (ensureR k) (ensureV v) p
54
55 -- | Evaluates if a 'MediaType' has a parameter of the given name.
56 (/?) :: MediaType -> ByteString -> Bool
57 (MediaType _ _ p) /? k = Map.member (CI.mk k) p
58
59 -- | Retrieves a parameter from a 'MediaType'.
60 (/.) :: MediaType -> ByteString -> Maybe (CI ByteString)
61 (MediaType _ _ p) /. k = Map.lookup (CI.mk k) p
62
63 -- | Ensures that the 'ByteString' matches the ABNF for `reg-name` in RFC
64 -- 4288.
65 ensureR :: ByteString -> CI ByteString
66 ensureR bs =
67 CI.mk $
68 if l == 0 || l > 127
69 then error $ "Invalid length for " ++ show bs
70 else ensure isMediaChar bs
71 where
72 l = BS.length bs
73
74 -- | Ensures that the 'ByteString' does not contain invalid characters for
75 -- a parameter value. RFC 4288 does not specify what characters are valid, so
76 -- here we just disallow parameter and media type breakers, ',' and ';'.
77 ensureV :: ByteString -> CI ByteString
78 ensureV = CI.mk . ensure (`notElem` [',', ';'])
79
80 -- | Ensures the predicate matches for every character in the given string.
81 ensure :: (Char -> Bool) -> ByteString -> ByteString
82 ensure f bs =
83 maybe
84 (error $ "Invalid character in " ++ show bs)
85 (const bs)
86 (BS.find f bs)