HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulehttp-media-0.8.1.1Haskell2010

Network.HTTP.Media

A framework for parsing HTTP media type headers.

  • 6 types
  • 2 classes
  • 30 values

Media types

8 declarations
datadata MediaType
#

An HTTP media type, consisting of the type, subtype, and parameters.

Instances6Eq, Ord, Show, IsString, Accept, RenderHeader
  • Eq MediaTypeDefined in http-media-0.8.1.1 · Network.HTTP.Media.MediaType.Internal
  • Ord MediaTypeDefined in http-media-0.8.1.1 · Network.HTTP.Media.MediaType.Internal
  • Show MediaTypeDefined in http-media-0.8.1.1 · Network.HTTP.Media.MediaType.Internal
  • IsString MediaTypeDefined in http-media-0.8.1.1 · Network.HTTP.Media.MediaType.Internal
  • Accept MediaTypeDefined in http-media-0.8.1.1 · Network.HTTP.Media.MediaType.Internal
  • RenderHeader MediaTypeDefined in http-media-0.8.1.1 · Network.HTTP.Media.MediaType.Internal

Charsets

1 declaration
newtypenewtype Charset
#

Suitable for HTTP charset as defined in RFC7231.

Specifically:

charset = token / "*"
Instances6Eq, Ord, Show, IsString, Accept, RenderHeader
  • Eq CharsetDefined in http-media-0.8.1.1 · Network.HTTP.Media.Charset.Internal
  • Ord CharsetDefined in http-media-0.8.1.1 · Network.HTTP.Media.Charset.Internal
  • Show CharsetDefined in http-media-0.8.1.1 · Network.HTTP.Media.Charset.Internal
  • IsString CharsetDefined in http-media-0.8.1.1 · Network.HTTP.Media.Charset.Internal
  • Accept CharsetDefined in http-media-0.8.1.1 · Network.HTTP.Media.Charset.Internal
  • RenderHeader CharsetDefined in http-media-0.8.1.1 · Network.HTTP.Media.Charset.Internal

Encodings

1 declaration
newtypenewtype Encoding
#

Suitable for HTTP encoding as defined in RFC7231.

Specifically:

codings = content-coding / "identity" / "*"
Instances6Eq, Ord, Show, IsString, Accept, RenderHeader
  • Eq EncodingDefined in http-media-0.8.1.1 · Network.HTTP.Media.Encoding.Internal
  • Ord EncodingDefined in http-media-0.8.1.1 · Network.HTTP.Media.Encoding.Internal
  • Show EncodingDefined in http-media-0.8.1.1 · Network.HTTP.Media.Encoding.Internal
  • IsString EncodingDefined in http-media-0.8.1.1 · Network.HTTP.Media.Encoding.Internal
  • Accept EncodingDefined in http-media-0.8.1.1 · Network.HTTP.Media.Encoding.Internal
  • RenderHeader EncodingDefined in http-media-0.8.1.1 · Network.HTTP.Media.Encoding.Internal

Languages

2 declarations
newtypenewtype Language
#

Suitable for HTTP language-ranges as defined in RFC4647.

Specifically:

language-range = (1*8ALPHA *("-" 1*8alphanum)) / "*"
Instances6Eq, Ord, Show, IsString, Accept, RenderHeader
  • Eq LanguageDefined in http-media-0.8.1.1 · Network.HTTP.Media.Language.Internal
  • Ord LanguageDefined in http-media-0.8.1.1 · Network.HTTP.Media.Language.Internal
  • Show LanguageDefined in http-media-0.8.1.1 · Network.HTTP.Media.Language.Internal
  • IsString LanguageDefined in http-media-0.8.1.1 · Network.HTTP.Media.Language.Internal
  • Accept LanguageDefined in http-media-0.8.1.1 · Network.HTTP.Media.Language.Internal
  • RenderHeader LanguageDefined in http-media-0.8.1.1 · Network.HTTP.Media.Language.Internal

Accept matching

7 declarations
valuematchAccept
  1. :: Accept a
  2. => [a]

    The server-side options

  3. -> ByteString

    The client-side header value

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

valuemapAccept
  1. :: Accept a
  2. => [(a, b)]

    The map of server-side preferences to values

  3. -> ByteString

    The client-side header value

  4. -> 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)
    ]
valuemapAcceptMedia
  1. :: [(MediaType, b)]

    The map of server-side preferences to values

  2. -> ByteString

    The client-side header value

  3. -> 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)
    ]
valuemapAcceptCharset
  1. :: [(Charset, b)]

    The map of server-side preferences to values

  2. -> ByteString

    The client-side header value

  3. -> Maybe b
#

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

getHeader >>= maybe render406Error renderResource . mapAcceptCharset
    [ ("utf-8",    inUtf8)
    , ("us-ascii", inAscii)
    ]
valuemapAcceptEncoding
  1. :: [(Encoding, b)]

    The map of server-side preferences to values

  2. -> ByteString

    The client-side header value

  3. -> Maybe b
#

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

getHeader >>= maybe render406Error renderResource . mapAcceptEncoding
    [ ("compress", compress)
    , ("identity", id)
    ]
valuemapAcceptLanguage
  1. :: [(Language, b)]

    The map of server-side preferences to values

  2. -> ByteString

    The client-side header value

  3. -> 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
    [ ("en-gb", inBritishEnglish)
    , ("fr",    inFrench)
    ]
valuemapAcceptBytes
  1. :: [(ByteString, b)]

    The map of server-side preferences to values

  2. -> ByteString

    The client-side header value

  3. -> 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
    [ ("abc", abc)
    , ("xyz", xyz)
    ]

Content matching

6 declarations
valuematchContent
  1. :: Accept a
  2. => [a]

    The server-side response options

  3. -> ByteString

    The client's request value

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

valuemapContent
  1. :: Accept a
  2. => [(a, b)]

    The map of server-side responses

  3. -> ByteString

    The client request's header value

  4. -> 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)
    ]
valuemapContentMedia
  1. :: [(MediaType, b)]

    The map of server-side responses

  2. -> ByteString

    The client request's header value

  3. -> 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)
        ]
valuemapContentCharset
  1. :: [(Charset, b)]

    The map of server-side responses

  2. -> ByteString

    The client request's header value

  3. -> Maybe b
#

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

getContentCharset >>=
    maybe send415Error readRequestBodyWith . mapContentCharset
        [ ("utf-8",    parseUtf8)
        , ("us-ascii", parseAscii)
        ]
valuemapContentEncoding
  1. :: [(Encoding, b)]

    The map of server-side responses

  2. -> ByteString

    The client request's header value

  3. -> Maybe b
#

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

getContentEncoding >>=
    maybe send415Error readRequestBodyWith . mapContentEncoding
        [ ("compress", decompress)
        , ("identity", id)
        ]
valuemapContentLanguage
  1. :: [(Language, b)]

    The map of server-side responses

  2. -> ByteString

    The client request's header value

  3. -> Maybe b
#

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

getContentLanguage >>=
    maybe send415Error readRequestBodyWith . mapContentLanguage
        [ ("en-gb", parseBritishEnglish)
        , ("fr",    parseFrench)
        ]

Quality values

11 declarations
datadata Quality a
#

Attaches a quality value to data.

Instances5Functor, Eq, Ord, Show, RenderHeader
newtypenewtype QualityOrder
#

An opaque ordered representation of quality values without attached data.

Instances2Eq, Ord
  • Eq QualityOrderDefined in http-media-0.8.1.1 · Network.HTTP.Media.Quality
  • Ord QualityOrderDefined in http-media-0.8.1.1 · Network.HTTP.Media.Quality
valueisAcceptable :: Quality a -> Bool
#

Whether the quality value is greater than zero; otherwise the value should never be accepted, even when no other options are available.

valuematchQuality
  1. :: Accept a
  2. => [a]

    The server-side options

  3. -> [Quality a]

    The pre-parsed client-side header value

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

valuemapQuality
  1. :: Accept a
  2. => [(a, b)]

    The map of server-side preferences to values

  3. -> [Quality a]

    The client-side header value

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

1 declaration
classclass Show a => Accept a where
#

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.

Methods

  • parseAccept :: ByteString -> Maybe a

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

  • matches :: a -> a -> Bool

    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

    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

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

Instances5Accept
  • Accept ByteStringDefined in http-media-0.8.1.1 · Network.HTTP.Media.Accept
  • Accept CharsetDefined in http-media-0.8.1.1 · Network.HTTP.Media.Charset.Internal
  • Accept EncodingDefined in http-media-0.8.1.1 · Network.HTTP.Media.Encoding.Internal
  • Accept LanguageDefined in http-media-0.8.1.1 · Network.HTTP.Media.Language.Internal
  • Accept MediaTypeDefined in http-media-0.8.1.1 · Network.HTTP.Media.MediaType.Internal

Rendering

1 declaration
classclass RenderHeader h where
#

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.

Methods

Instances7RenderHeader, …