Safe Haskell | Safe-Inferred |
---|
A framework for parsing HTTP media type headers.
- data MediaType
- (//) :: ByteString -> ByteString -> MediaType
- (/:) :: MediaType -> (ByteString, ByteString) -> MediaType
- mainType :: MediaType -> ByteString
- subType :: MediaType -> ByteString
- parameters :: MediaType -> Parameters
- (/?) :: MediaType -> ByteString -> Bool
- (/.) :: MediaType -> ByteString -> Maybe ByteString
- matchAccept :: Accept a => [a] -> ByteString -> Maybe a
- mapAccept :: Accept a => [(a, b)] -> ByteString -> Maybe b
- mapAcceptMedia :: [(MediaType, b)] -> ByteString -> Maybe b
- mapAcceptBytes :: [(ByteString, b)] -> ByteString -> Maybe b
- matchContent :: Accept a => [a] -> ByteString -> Maybe a
- mapContent :: Accept a => [(a, b)] -> ByteString -> Maybe b
- mapContentMedia :: [(MediaType, b)] -> ByteString -> Maybe b
- data Quality a
- parseQuality :: Accept a => ByteString -> Maybe [Quality a]
- matchQuality :: Accept a => [a] -> [Quality a] -> Maybe a
- mapQuality :: Accept a => [(a, b)] -> [Quality a] -> Maybe b
- class Show a => Accept a where
- parseAccept :: ByteString -> Maybe a
- showAccept :: a -> String
- matches :: a -> a -> Bool
- moreSpecificThan :: a -> a -> Bool
Media types
An HTTP media type, consisting of the type, subtype, and parameters.
(//) :: ByteString -> ByteString -> MediaTypeSource
Builds a MediaType
without parameters. Can produce an error if
either type is invalid.
(/:) :: MediaType -> (ByteString, ByteString) -> MediaTypeSource
Adds a parameter to a MediaType
. Can produce an error if either
string is invalid.
mainType :: MediaType -> ByteStringSource
Retrieves the main type of a MediaType
.
subType :: MediaType -> ByteStringSource
Retrieves the sub type of a MediaType
.
parameters :: MediaType -> ParametersSource
Retrieves the parameters of a MediaType
.
(/?) :: MediaType -> ByteString -> BoolSource
Evaluates if a MediaType
has a parameter of the given name.
(/.) :: MediaType -> ByteString -> Maybe ByteStringSource
Retrieves a parameter from a MediaType
.
Accept matching
:: Accept a | |
=> [a] | The server-side options |
-> ByteString | The client-side header value |
-> Maybe a |
Matches a list of server-side resource options against a quality-marked
list of client-side preferences. A result of Nothing
means that nothing
matched (which should indicate a 406 error). If two or more results arise
with the same quality level and specificity, then the first one in the
server list is chosen.
The use of the Accept
type class allows the application of either
MediaType
for the standard Accept header or ByteString
for any other
Accept header which can be marked with a quality value.
matchAccept ["text/html", "application/json"] <$> getHeader
For more information on the matching process see RFC 2616, section 14.1-4.
:: Accept a | |
=> [(a, b)] | The map of server-side preferences to values |
-> ByteString | The client-side header value |
-> Maybe b |
The equivalent of matchAccept
above, except the resulting choice is
mapped to another value. Convenient for specifying how to translate the
resource into each of its available formats.
getHeader >>= maybe render406Error renderResource . mapAccept [ ("text" // "html", asHtml) , ("application" // "json", asJson) ]
:: [(MediaType, b)] | The map of server-side preferences to values |
-> ByteString | The client-side header value |
-> Maybe b |
A specialisation of mapAccept
that only takes MediaType as its input,
to avoid ambiguous-type errors when using string literal overloading.
getHeader >>= maybe render406Error renderResource . mapAcceptMedia [ ("text/html", asHtml) , ("application/json", asJson) ]
:: [(ByteString, b)] | The map of server-side preferences to values |
-> ByteString | The client-side header value |
-> Maybe b |
A specialisation of mapAccept
that only takes ByteString as its input,
to avoid ambiguous-type errors when using string literal overloading.
getHeader >>= maybe render406Error encodeResourceWith . mapAcceptBytes [ ("compress", compress) , ("gzip", gzip) ]
Content matching
:: Accept a | |
=> [a] | The server-side response options |
-> ByteString | The client's request value |
-> Maybe a |
Matches a list of server-side parsing options against a the client-side
content value. A result of Nothing
means that nothing matched (which
should indicate a 415 error).
matchContent ["application/json", "text/plain"] <$> getContentType
For more information on the matching process see RFC 2616, section 14.17.
:: Accept a | |
=> [(a, b)] | The map of server-side responses |
-> ByteString | The client request's header value |
-> Maybe b |
The equivalent of matchContent
above, except the resulting choice is
mapped to another value.
getContentType >>= maybe send415Error readRequestBodyWith . mapContent [ ("application" // "json", parseJson) , ("text" // "plain", parseText) ]
:: [(MediaType, b)] | The map of server-side responses |
-> ByteString | The client request's header value |
-> Maybe b |
A specialisation of mapContent
that only takes MediaType as its input,
to avoid ambiguous-type errors when using string literal overloading.
getContentType >>= maybe send415Error readRequestBodyWith . mapContentMedia [ ("application/json", parseJson) , ("text/plain", parseText) ]
Quality values
Attaches a quality value to data.
parseQuality :: Accept a => ByteString -> Maybe [Quality a]Source
Parses a full Accept header into a list of quality-valued media types.
:: Accept a | |
=> [a] | The server-side options |
-> [Quality a] | The pre-parsed client-side header value |
-> Maybe a |
Matches a list of server-side resource options against a pre-parsed
quality-marked list of client-side preferences. A result of Nothing
means
that nothing matched (which should indicate a 406 error). If two or more
results arise with the same quality level and specificity, then the first
one in the server list is chosen.
The use of the Accept
type class allows the application of either
MediaType
for the standard Accept header or ByteString
for any other
Accept header which can be marked with a quality value.
matchQuality ["text/html", "application/json"] <$> parseQuality header
For more information on the matching process see RFC 2616, section 14.1-4.
:: Accept a | |
=> [(a, b)] | The map of server-side preferences to values |
-> [Quality a] | The client-side header value |
-> Maybe b |
The equivalent of matchQuality
above, except the resulting choice is
mapped to another value. Convenient for specifying how to translate the
resource into each of its available formats.
parseQuality header >>= maybe render406Error renderResource . mapQuality [ ("text" // "html", asHtml) , ("application" // "json", asJson) ]
Accept
class Show a => Accept a whereSource
Defines methods for a type whose values can be matched against each other in terms of an HTTP Accept-* header.
This allows functions to work on both the standard Accept header and others such as Accept-Language that still may use quality values.
parseAccept :: ByteString -> Maybe aSource
Specifies how to parse an Accept-* header after quality has been handled.
showAccept :: a -> StringSource
Specifies how to show an Accept-* header. Defaults to the standard show method.
Mostly useful just for avoiding quotes when rendering ByteString
s
with accompanying quality.
matches :: a -> a -> BoolSource
Evaluates whether either the left argument matches the right one (order may be important).
moreSpecificThan :: a -> a -> BoolSource
Evaluates whether the left argument is more specific than the right.