Copyright | (c) Raghu Kaippully 2020 |
---|---|
License | MPL-2.0 |
Maintainer | rkaippully@gmail.com |
Safe Haskell | None |
Language | Haskell2010 |
Middlewares related to HTTP headers.
Synopsis
- type Header (name :: Symbol) (val :: Type) = Header' Required Strict name val
- data Header' (e :: Existence) (p :: ParseStyle) (name :: Symbol) (val :: Type)
- data HeaderNotFound = HeaderNotFound
- newtype HeaderParseError = HeaderParseError Text
- type HeaderMatch (name :: Symbol) (val :: Symbol) = HeaderMatch' Required name val
- data HeaderMatch' (e :: Existence) (name :: Symbol) (val :: Symbol)
- data HeaderMismatch = HeaderMismatch {}
- header :: forall name val m req a. (KnownSymbol name, FromHttpApiData val, MonadRouter m) => RequestMiddleware' m req (Header name val ': req) a
- optionalHeader :: forall name val m req a. (KnownSymbol name, FromHttpApiData val, MonadRouter m) => RequestMiddleware' m req (Header' Optional Strict name val ': req) a
- lenientHeader :: forall name val m req a. (KnownSymbol name, FromHttpApiData val, MonadRouter m) => RequestMiddleware' m req (Header' Required Lenient name val ': req) a
- optionalLenientHeader :: forall name val m req a. (KnownSymbol name, FromHttpApiData val, MonadRouter m) => RequestMiddleware' m req (Header' Optional Lenient name val ': req) a
- headerMatch :: forall name val m req a. (KnownSymbol name, KnownSymbol val, MonadRouter m) => RequestMiddleware' m req (HeaderMatch name val ': req) a
- optionalHeaderMatch :: forall name val m req a. (KnownSymbol name, KnownSymbol val, MonadRouter m) => RequestMiddleware' m req (HeaderMatch' Optional name val ': req) a
- requestContentTypeHeader :: forall val m req a. (KnownSymbol val, MonadRouter m) => RequestMiddleware' m req (HeaderMatch "Content-Type" val ': req) a
- addResponseHeader :: forall t m req a. (ToHttpApiData t, Monad m) => HeaderName -> t -> ResponseMiddleware' m req a a
Traits
type Header (name :: Symbol) (val :: Type) = Header' Required Strict name val Source #
A Trait
for capturing a header with name name
in a request or
response and convert it to some type val
via FromHttpApiData
.
data Header' (e :: Existence) (p :: ParseStyle) (name :: Symbol) (val :: Type) Source #
A Trait
for capturing an HTTP header of specified name
and
converting it to some type val
via FromHttpApiData
. The
modifiers e
and p
determine how missing headers and parsing
errors are handled. The header name is compared case-insensitively.
Instances
data HeaderNotFound Source #
Indicates a missing header
Instances
Eq HeaderNotFound Source # | |
Defined in WebGear.Middlewares.Header (==) :: HeaderNotFound -> HeaderNotFound -> Bool # (/=) :: HeaderNotFound -> HeaderNotFound -> Bool # | |
Read HeaderNotFound Source # | |
Defined in WebGear.Middlewares.Header readsPrec :: Int -> ReadS HeaderNotFound # readList :: ReadS [HeaderNotFound] # | |
Show HeaderNotFound Source # | |
Defined in WebGear.Middlewares.Header showsPrec :: Int -> HeaderNotFound -> ShowS # show :: HeaderNotFound -> String # showList :: [HeaderNotFound] -> ShowS # |
newtype HeaderParseError Source #
Error in converting a header
Instances
Eq HeaderParseError Source # | |
Defined in WebGear.Middlewares.Header (==) :: HeaderParseError -> HeaderParseError -> Bool # (/=) :: HeaderParseError -> HeaderParseError -> Bool # | |
Read HeaderParseError Source # | |
Defined in WebGear.Middlewares.Header | |
Show HeaderParseError Source # | |
Defined in WebGear.Middlewares.Header showsPrec :: Int -> HeaderParseError -> ShowS # show :: HeaderParseError -> String # showList :: [HeaderParseError] -> ShowS # |
type HeaderMatch (name :: Symbol) (val :: Symbol) = HeaderMatch' Required name val Source #
A Trait
for ensuring that a header with a specified name
has
value val
.
data HeaderMatch' (e :: Existence) (name :: Symbol) (val :: Symbol) Source #
A Trait
for ensuring that an HTTP header with specified name
has value val
. The modifier e
determines how missing headers
are handled. The header name is compared case-insensitively.
Instances
data HeaderMismatch Source #
Failure in extracting a header value
Instances
Eq HeaderMismatch Source # | |
Defined in WebGear.Middlewares.Header (==) :: HeaderMismatch -> HeaderMismatch -> Bool # (/=) :: HeaderMismatch -> HeaderMismatch -> Bool # | |
Read HeaderMismatch Source # | |
Defined in WebGear.Middlewares.Header readsPrec :: Int -> ReadS HeaderMismatch # readList :: ReadS [HeaderMismatch] # | |
Show HeaderMismatch Source # | |
Defined in WebGear.Middlewares.Header showsPrec :: Int -> HeaderMismatch -> ShowS # show :: HeaderMismatch -> String # showList :: [HeaderMismatch] -> ShowS # |
Middlewares
header :: forall name val m req a. (KnownSymbol name, FromHttpApiData val, MonadRouter m) => RequestMiddleware' m req (Header name val ': req) a Source #
A middleware to extract a header value and convert it to a value
of type val
using FromHttpApiData
.
Example usage:
header @"Content-Length" @Integer handler
The associated trait attribute has type val
. A 400 Bad Request
response is returned if the header is not found or could not be
parsed.
optionalHeader :: forall name val m req a. (KnownSymbol name, FromHttpApiData val, MonadRouter m) => RequestMiddleware' m req (Header' Optional Strict name val ': req) a Source #
A middleware to extract a header value and convert it to a value
of type val
using FromHttpApiData
.
Example usage:
optionalHeader @"Content-Length" @Integer handler
The associated trait attribute has type Maybe val
; a Nothing
value indicates that the header is missing from the request. A 400
Bad Request response is returned if the header could not be parsed.
lenientHeader :: forall name val m req a. (KnownSymbol name, FromHttpApiData val, MonadRouter m) => RequestMiddleware' m req (Header' Required Lenient name val ': req) a Source #
A middleware to extract a header value and convert it to a value
of type val
using FromHttpApiData
.
Example usage:
lenientHeader @"Content-Length" @Integer handler
The associated trait attribute has type Either Text val
. A 400
Bad Request reponse is returned if the header is missing. The
parsing is done leniently; the trait attribute is set to Left
Text
in case of parse errors or Right val
on success.
optionalLenientHeader :: forall name val m req a. (KnownSymbol name, FromHttpApiData val, MonadRouter m) => RequestMiddleware' m req (Header' Optional Lenient name val ': req) a Source #
A middleware to extract an optional header value and convert it
to a value of type val
using FromHttpApiData
.
Example usage:
optionalLenientHeader @"Content-Length" @Integer handler
The associated trait attribute has type Maybe (Either Text
val)
. This middleware never fails.
headerMatch :: forall name val m req a. (KnownSymbol name, KnownSymbol val, MonadRouter m) => RequestMiddleware' m req (HeaderMatch name val ': req) a Source #
A middleware to ensure that a header in the request has a specific value. Fails the handler with a 400 Bad Request response if the header does not exist or does not match.
optionalHeaderMatch :: forall name val m req a. (KnownSymbol name, KnownSymbol val, MonadRouter m) => RequestMiddleware' m req (HeaderMatch' Optional name val ': req) a Source #
A middleware to ensure that an optional header in the request has a specific value. Fails the handler with a 400 Bad Request response if the header has a different value.
requestContentTypeHeader :: forall val m req a. (KnownSymbol val, MonadRouter m) => RequestMiddleware' m req (HeaderMatch "Content-Type" val ': req) a Source #
A middleware to check that the Content-Type header in the request has a specific value. It will fail the handler if the header did not match.
Example usage:
requestContentTypeHeader @"application/json" handler
addResponseHeader :: forall t m req a. (ToHttpApiData t, Monad m) => HeaderName -> t -> ResponseMiddleware' m req a a Source #
A middleware to create or update a response header.
Example usage:
addResponseHeader "Content-type" "application/json" handler