| 1 |
-- | Defines the 'Accept' type class, designed to unify types on the matching |
| 2 |
-- functions in the Media module. |
| 3 |
module Network.HTTP.Media.Accept |
| 4 |
( Accept (..), |
| 5 |
) |
| 6 |
where |
| 7 |
|
| 8 |
import Data.ByteString (ByteString) |
| 9 |
import qualified Data.CaseInsensitive as CI |
| 10 |
import Data.Proxy (Proxy) |
| 11 |
|
| 12 |
-- | Defines methods for a type whose values can be matched against each |
| 13 |
-- other in terms of an HTTP Accept-* header. |
| 14 |
-- |
| 15 |
-- This allows functions to work on both the standard Accept header and |
| 16 |
-- others such as Accept-Language that still may use quality values. |
| 17 |
class (Show a) => Accept a where |
| 18 |
-- | Specifies how to parse an Accept-* header after quality has been |
| 19 |
-- handled. |
| 20 |
parseAccept :: ByteString -> Maybe a |
| 21 |
|
| 22 |
-- | Evaluates whether either the left argument matches the right one. |
| 23 |
-- |
| 24 |
-- This relation must be a total order, where more specific terms on the |
| 25 |
-- left can produce a match, but a less specific term on the left can |
| 26 |
-- never produce a match. For instance, when matching against media types |
| 27 |
-- it is important that if the client asks for a general type then we can |
| 28 |
-- choose a more specific offering from the server, but if a client asks |
| 29 |
-- for a specific type and the server only offers a more general form, |
| 30 |
-- then we cannot generalise. In this case, the server types will be the |
| 31 |
-- left argument, and the client types the right. |
| 32 |
-- |
| 33 |
-- For types with no concept of specificity, this operation is just |
| 34 |
-- equality. |
| 35 |
matches :: a -> a -> Bool |
| 36 |
|
| 37 |
-- | Evaluates whether the left argument is more specific than the right. |
| 38 |
-- |
| 39 |
-- This relation must be irreflexive and transitive. For types with no |
| 40 |
-- concept of specificity, this is the empty relation (always false). |
| 41 |
moreSpecificThan :: a -> a -> Bool |
| 42 |
|
| 43 |
-- | Indicates whether extension parameters are permitted after the weight |
| 44 |
-- parameter when this type appears in an Accept header. Defaults to |
| 45 |
-- false. |
| 46 |
hasExtensionParameters :: Proxy a -> Bool |
| 47 |
hasExtensionParameters _ = False |
| 48 |
|
| 49 |
instance Accept ByteString where |
| 50 |
parseAccept = Just |
| 51 |
matches a b = CI.mk a == CI.mk b |
| 52 |
moreSpecificThan _ _ = False |