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
- matches :: Match a => a -> a -> Bool
- data Quality a
- parseAccept :: ByteString -> Maybe [Quality MediaType]
- matchAccept :: Match a => [a] -> [Quality a] -> Maybe a
- mapAccept :: Match a => [(a, b)] -> [Quality a] -> Maybe b
- matchContent :: Match a => a -> [a] -> Maybe a
- mapContent :: Match a => a -> [(a, b)] -> Maybe b
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
.
matches :: Match a => a -> a -> BoolSource
Evaluates whether either the left argument matches the right one (order may be important).
Attaches a quality value to data.
Parsing
parseAccept :: ByteString -> Maybe [Quality MediaType]Source
Parses a full Accept header into a list of quality-valued media types.
Accept matching
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 Match
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. The standard
application of this function for MediaType
should be in conjunction with
parseAccepts
.
parseAccepts header >>= matchQuality resourceTypeOptions
For more information on the matching process see RFC 2616, section 14.
:: Match a | |
=> [(a, b)] | The map of server-side preferences to values |
-> [Quality a] | The client-side preferences |
-> 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.
maybe render406Error renderResource $ parseAccepts header >>= mapQuality [ ("text/html", asHtml) , ("application/json", asJson) ]
Content matching
:: Match a | |
=> a | The client's request value |
-> [a] | The server-side response options |
-> 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).
As with the Accept parsing, he use of the Match
type class allows the
application of either MediaType
or ByteString
.
:: Match a | |
=> a | The client request's header value |
-> [(a, b)] | The map of server-side responses |
-> Maybe b |
The equivalent of matchContent
above, except the resulting choice is
mapped to another value.