http-media-0.6.4: Processing HTTP Content-Type and Accept headers

Safe HaskellSafe
LanguageHaskell2010

Network.HTTP.Media

Contents

Description

A framework for parsing HTTP media type headers.

Synopsis

Media types

(//) :: ByteString -> ByteString -> MediaType Source #

Builds a MediaType without parameters. Can produce an error if either type is invalid.

(/:) :: MediaType -> (ByteString, ByteString) -> MediaType Source #

Adds a parameter to a MediaType. Can produce an error if either string is invalid.

mainType :: MediaType -> CI ByteString Source #

Retrieves the main type of a MediaType.

subType :: MediaType -> CI ByteString Source #

Retrieves the sub type of a MediaType.

parameters :: MediaType -> Parameters Source #

Retrieves the parameters of a MediaType.

(/?) :: MediaType -> ByteString -> Bool Source #

Evaluates if a MediaType has a parameter of the given name.

(/.) :: MediaType -> ByteString -> Maybe (CI ByteString) Source #

Retrieves a parameter from a MediaType.

Languages

toParts :: Language -> [CI ByteString] Source #

Converts Language to a list of its language parts. The wildcard produces an empty list.

Accept matching

matchAccept Source #

Arguments

:: 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.

mapAccept Source #

Arguments

:: 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)
    ]

mapAcceptMedia Source #

Arguments

:: [(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)
    ]

mapAcceptLanguage Source #

Arguments

:: [(Language, b)]

The map of server-side preferences to values

-> ByteString

The client-side header value

-> Maybe b 

A specialisation of mapAccept that only takes Language as its input, to avoid ambiguous-type errors when using string literal overloading.

getHeader >>= maybe render406Error renderResource . mapAcceptLanguage
    [ ("text/html",        asHtml)
    , ("application/json", asJson)
    ]

mapAcceptBytes Source #

Arguments

:: [(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

matchContent Source #

Arguments

:: 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.

mapContent Source #

Arguments

:: 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)
    ]

mapContentMedia Source #

Arguments

:: [(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)
        ]

mapContentLanguage Source #

Arguments

:: [(Language, b)]

The map of server-side responses

-> ByteString

The client request's header value

-> Maybe b 

A specialisation of mapContent that only takes Language as its input, to avoid ambiguous-type errors when using string literal overloading.

getContentType >>=
    maybe send415Error readRequestBodyWith . mapContentLanguage
        [ ("application/json", parseJson)
        , ("text/plain",       parseText)
        ]

Quality values

data Quality a Source #

Attaches a quality value to data.

Instances

Eq a => Eq (Quality a) Source # 

Methods

(==) :: Quality a -> Quality a -> Bool #

(/=) :: Quality a -> Quality a -> Bool #

Ord a => Ord (Quality a) Source # 

Methods

compare :: Quality a -> Quality a -> Ordering #

(<) :: Quality a -> Quality a -> Bool #

(<=) :: Quality a -> Quality a -> Bool #

(>) :: Quality a -> Quality a -> Bool #

(>=) :: Quality a -> Quality a -> Bool #

max :: Quality a -> Quality a -> Quality a #

min :: Quality a -> Quality a -> Quality a #

RenderHeader a => Show (Quality a) Source # 

Methods

showsPrec :: Int -> Quality a -> ShowS #

show :: Quality a -> String #

showList :: [Quality a] -> ShowS #

RenderHeader h => RenderHeader (Quality h) Source # 

parseQuality :: Accept a => ByteString -> Maybe [Quality a] Source #

Parses a full Accept header into a list of quality-valued media types.

matchQuality Source #

Arguments

:: 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.

mapQuality Source #

Arguments

:: 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 where Source #

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.

Minimal complete definition

parseAccept, matches, moreSpecificThan

Methods

parseAccept :: ByteString -> Maybe a Source #

Specifies how to parse an Accept-* header after quality has been handled.

matches :: a -> a -> Bool Source #

Evaluates whether either the left argument matches the right one.

This relation must be a total order, where more specific terms on the left can produce a match, but a less specific term on the left can never produce a match. For instance, when matching against media types it is important that if the client asks for a general type then we can choose a more specific offering from the server, but if a client asks for a specific type and the server only offers a more general form, then we cannot generalise. In this case, the server types will be the left argument, and the client types the right.

For types with no concept of specificity, this operation is just equality.

moreSpecificThan :: a -> a -> Bool Source #

Evaluates whether the left argument is more specific than the right.

This relation must be irreflexive and transitive. For types with no concept of specificity, this is the empty relation (always false).

hasExtensionParameters :: Proxy a -> Bool Source #

Indicates whether extension parameters are permitted after the weight parameter when this type appears in an Accept header. Defaults to false.

Rendering

class RenderHeader h where Source #

A class for header values, so they may be rendered to their ByteString representation. Lists of header values and quality-marked header values will render appropriately.

Minimal complete definition

renderHeader

Methods

renderHeader :: h -> ByteString Source #

Render a header value to a UTF-8 ByteString.