{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_HADDOCK not-home #-}
module Servant.API.ResponseHeaders
( Headers(..)
, ResponseHeader (..)
, AddHeader
, addHeader
, noHeader
, HasResponseHeader
, lookupResponseHeader
, BuildHeadersTo(buildHeadersTo)
, GetHeaders(getHeaders)
, GetHeaders'
, HeaderValMap
, HList(..)
) where
import Control.DeepSeq
(NFData (..))
import Data.ByteString.Char8 as BS
(ByteString, init, pack, unlines)
import qualified Data.CaseInsensitive as CI
import qualified Data.List as L
import Data.Proxy
import Data.Typeable
(Typeable)
import GHC.TypeLits
(KnownSymbol, Symbol, symbolVal)
import qualified Network.HTTP.Types.Header as HTTP
import Web.HttpApiData
(FromHttpApiData, ToHttpApiData, parseHeader, toHeader)
import Prelude ()
import Prelude.Compat
import Servant.API.Header
(Header)
import Servant.API.UVerb.Union
import qualified Data.SOP.BasicFunctors as SOP
import qualified Data.SOP.NS as SOP
data ls a = { forall (ls :: [*]) a. Headers ls a -> a
getResponse :: a
, :: HList ls
} deriving (forall (ls :: [*]) a b. a -> Headers ls b -> Headers ls a
forall (ls :: [*]) a b. (a -> b) -> Headers ls a -> Headers ls b
forall a b. a -> Headers ls b -> Headers ls a
forall a b. (a -> b) -> Headers ls a -> Headers ls b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Headers ls b -> Headers ls a
$c<$ :: forall (ls :: [*]) a b. a -> Headers ls b -> Headers ls a
fmap :: forall a b. (a -> b) -> Headers ls a -> Headers ls b
$cfmap :: forall (ls :: [*]) a b. (a -> b) -> Headers ls a -> Headers ls b
Functor)
instance (NFDataHList ls, NFData a) => NFData (Headers ls a) where
rnf :: Headers ls a -> ()
rnf (Headers a
x HList ls
hdrs) = forall a. NFData a => a -> ()
rnf a
x seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf HList ls
hdrs
data (sym :: Symbol) a
= a
|
| ByteString
deriving (Typeable, ResponseHeader sym a -> ResponseHeader sym a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (sym :: Symbol) a.
Eq a =>
ResponseHeader sym a -> ResponseHeader sym a -> Bool
/= :: ResponseHeader sym a -> ResponseHeader sym a -> Bool
$c/= :: forall (sym :: Symbol) a.
Eq a =>
ResponseHeader sym a -> ResponseHeader sym a -> Bool
== :: ResponseHeader sym a -> ResponseHeader sym a -> Bool
$c== :: forall (sym :: Symbol) a.
Eq a =>
ResponseHeader sym a -> ResponseHeader sym a -> Bool
Eq, Int -> ResponseHeader sym a -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (sym :: Symbol) a.
Show a =>
Int -> ResponseHeader sym a -> ShowS
forall (sym :: Symbol) a. Show a => [ResponseHeader sym a] -> ShowS
forall (sym :: Symbol) a. Show a => ResponseHeader sym a -> String
showList :: [ResponseHeader sym a] -> ShowS
$cshowList :: forall (sym :: Symbol) a. Show a => [ResponseHeader sym a] -> ShowS
show :: ResponseHeader sym a -> String
$cshow :: forall (sym :: Symbol) a. Show a => ResponseHeader sym a -> String
showsPrec :: Int -> ResponseHeader sym a -> ShowS
$cshowsPrec :: forall (sym :: Symbol) a.
Show a =>
Int -> ResponseHeader sym a -> ShowS
Show, forall a b. a -> ResponseHeader sym b -> ResponseHeader sym a
forall a b.
(a -> b) -> ResponseHeader sym a -> ResponseHeader sym b
forall (sym :: Symbol) a b.
a -> ResponseHeader sym b -> ResponseHeader sym a
forall (sym :: Symbol) a b.
(a -> b) -> ResponseHeader sym a -> ResponseHeader sym b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> ResponseHeader sym b -> ResponseHeader sym a
$c<$ :: forall (sym :: Symbol) a b.
a -> ResponseHeader sym b -> ResponseHeader sym a
fmap :: forall a b.
(a -> b) -> ResponseHeader sym a -> ResponseHeader sym b
$cfmap :: forall (sym :: Symbol) a b.
(a -> b) -> ResponseHeader sym a -> ResponseHeader sym b
Functor)
instance NFData a => NFData (ResponseHeader sym a) where
rnf :: ResponseHeader sym a -> ()
rnf ResponseHeader sym a
MissingHeader = ()
rnf (UndecodableHeader ByteString
bs) = forall a. NFData a => a -> ()
rnf ByteString
bs
rnf (Header a
x) = forall a. NFData a => a -> ()
rnf a
x
data HList a where
HNil :: HList '[]
HCons :: ResponseHeader h x -> HList xs -> HList (Header h x ': xs)
class NFDataHList xs where rnfHList :: HList xs -> ()
instance NFDataHList '[] where rnfHList :: HList '[] -> ()
rnfHList HList '[]
HNil = ()
instance (y ~ Header h x, NFData x, NFDataHList xs) => NFDataHList (y ': xs) where
rnfHList :: HList (y : xs) -> ()
rnfHList (HCons ResponseHeader h x
h HList xs
xs) = forall a. NFData a => a -> ()
rnf ResponseHeader h x
h seq :: forall a b. a -> b -> b
`seq` forall (xs :: [*]). NFDataHList xs => HList xs -> ()
rnfHList HList xs
xs
instance NFDataHList xs => NFData (HList xs) where
rnf :: HList xs -> ()
rnf = forall (xs :: [*]). NFDataHList xs => HList xs -> ()
rnfHList
type family (f :: * -> *) (xs :: [*]) where
f '[] = '[]
f (Header h x ': xs) = Header h (f x) ': HeaderValMap f xs
class hs where
:: [HTTP.Header] -> HList hs
instance {-# OVERLAPPING #-} BuildHeadersTo '[] where
buildHeadersTo :: [Header] -> HList '[]
buildHeadersTo [Header]
_ = HList '[]
HNil
instance {-# OVERLAPPABLE #-} ( FromHttpApiData v, BuildHeadersTo xs, KnownSymbol h )
=> BuildHeadersTo (Header h v ': xs) where
buildHeadersTo :: [Header] -> HList (Header h v : xs)
buildHeadersTo [Header]
headers = case forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
L.find Header -> Bool
wantedHeader [Header]
headers of
Maybe Header
Nothing -> forall (sym :: Symbol) a. ResponseHeader sym a
MissingHeader forall (h :: Symbol) x (xs :: [*]).
ResponseHeader h x -> HList xs -> HList (Header h x : xs)
`HCons` forall (hs :: [*]). BuildHeadersTo hs => [Header] -> HList hs
buildHeadersTo [Header]
headers
Just header :: Header
header@(CI ByteString
_, ByteString
val) -> case forall a. FromHttpApiData a => ByteString -> Either Text a
parseHeader ByteString
val of
Left Text
_err -> forall (sym :: Symbol) a. ByteString -> ResponseHeader sym a
UndecodableHeader ByteString
val forall (h :: Symbol) x (xs :: [*]).
ResponseHeader h x -> HList xs -> HList (Header h x : xs)
`HCons` forall (hs :: [*]). BuildHeadersTo hs => [Header] -> HList hs
buildHeadersTo (forall a. Eq a => a -> [a] -> [a]
L.delete Header
header [Header]
headers)
Right v
h -> forall (sym :: Symbol) a. a -> ResponseHeader sym a
Header v
h forall (h :: Symbol) x (xs :: [*]).
ResponseHeader h x -> HList xs -> HList (Header h x : xs)
`HCons` forall (hs :: [*]). BuildHeadersTo hs => [Header] -> HList hs
buildHeadersTo (forall a. Eq a => a -> [a] -> [a]
L.delete Header
header [Header]
headers)
where wantedHeader :: Header -> Bool
wantedHeader (CI ByteString
h, ByteString
_) = CI ByteString
h forall a. Eq a => a -> a -> Bool
== CI ByteString
wantedHeaderName
wantedHeaderName :: CI ByteString
wantedHeaderName = forall s. FoldCase s => s -> CI s
CI.mk forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
pack forall a b. (a -> b) -> a -> b
$ forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (forall {k} (t :: k). Proxy t
Proxy :: Proxy h)
class ls where
:: ls -> [HTTP.Header]
class hs where
:: HList hs -> [HTTP.Header]
instance GetHeadersFromHList hs => GetHeaders (HList hs) where
getHeaders :: HList hs -> [Header]
getHeaders = forall (hs :: [*]). GetHeadersFromHList hs => HList hs -> [Header]
getHeadersFromHList
instance GetHeadersFromHList '[] where
getHeadersFromHList :: HList '[] -> [Header]
getHeadersFromHList HList '[]
_ = []
instance (KnownSymbol h, ToHttpApiData x, GetHeadersFromHList xs)
=> GetHeadersFromHList (Header h x ': xs)
where
getHeadersFromHList :: HList (Header h x : xs) -> [Header]
getHeadersFromHList HList (Header h x : xs)
hdrs = case HList (Header h x : xs)
hdrs of
Header x
val `HCons` HList xs
rest -> (CI ByteString
headerName , forall a. ToHttpApiData a => a -> ByteString
toHeader x
val) forall a. a -> [a] -> [a]
: forall (hs :: [*]). GetHeadersFromHList hs => HList hs -> [Header]
getHeadersFromHList HList xs
rest
UndecodableHeader ByteString
h `HCons` HList xs
rest -> (CI ByteString
headerName, ByteString
h) forall a. a -> [a] -> [a]
: forall (hs :: [*]). GetHeadersFromHList hs => HList hs -> [Header]
getHeadersFromHList HList xs
rest
ResponseHeader h x
MissingHeader `HCons` HList xs
rest -> forall (hs :: [*]). GetHeadersFromHList hs => HList hs -> [Header]
getHeadersFromHList HList xs
rest
where
headerName :: CI ByteString
headerName = forall s. FoldCase s => s -> CI s
CI.mk forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
pack forall a b. (a -> b) -> a -> b
$ forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (forall {k} (t :: k). Proxy t
Proxy :: Proxy h)
class hs where
:: Headers hs a -> [HTTP.Header]
instance GetHeaders' hs => GetHeaders (Headers hs a) where
getHeaders :: Headers hs a -> [Header]
getHeaders = forall (hs :: [*]) a. GetHeaders' hs => Headers hs a -> [Header]
getHeaders'
instance GetHeaders' '[] where
getHeaders' :: forall a. Headers '[] a -> [Header]
getHeaders' Headers '[] a
_ = []
instance (KnownSymbol h, GetHeadersFromHList rest, ToHttpApiData v)
=> GetHeaders' (Header h v ': rest)
where
getHeaders' :: forall a. Headers (Header h v : rest) a -> [Header]
getHeaders' Headers (Header h v : rest) a
hs = forall (hs :: [*]). GetHeadersFromHList hs => HList hs -> [Header]
getHeadersFromHList forall a b. (a -> b) -> a -> b
$ forall (ls :: [*]) a. Headers ls a -> HList ls
getHeadersHList Headers (Header h v : rest) a
hs
class h v orig new
| h v orig -> new, new -> h, new -> v, new -> orig where
:: ResponseHeader h v -> orig -> new
instance {-# OVERLAPPING #-} ( KnownSymbol h, ToHttpApiData v )
=> AddHeader h v (Headers (fst ': rest) a) (Headers (Header h v ': fst ': rest) a) where
addOptionalHeader :: ResponseHeader h v
-> Headers (fst : rest) a -> Headers (Header h v : fst : rest) a
addOptionalHeader ResponseHeader h v
hdr (Headers a
resp HList (fst : rest)
heads) = forall (ls :: [*]) a. a -> HList ls -> Headers ls a
Headers a
resp (forall (h :: Symbol) x (xs :: [*]).
ResponseHeader h x -> HList xs -> HList (Header h x : xs)
HCons ResponseHeader h v
hdr HList (fst : rest)
heads)
instance {-# OVERLAPPABLE #-} ( KnownSymbol h, ToHttpApiData v , new ~ Headers '[Header h v] a)
=> AddHeader h v a new where
addOptionalHeader :: ResponseHeader h v -> a -> new
addOptionalHeader ResponseHeader h v
hdr a
resp = forall (ls :: [*]) a. a -> HList ls -> Headers ls a
Headers a
resp (forall (h :: Symbol) x (xs :: [*]).
ResponseHeader h x -> HList xs -> HList (Header h x : xs)
HCons ResponseHeader h v
hdr HList '[]
HNil)
instance (AddHeader h v old new) => AddHeader h v (Union '[old]) (Union '[new]) where
addOptionalHeader :: ResponseHeader h v -> Union '[old] -> Union '[new]
addOptionalHeader ResponseHeader h v
hdr Union '[old]
resp =
forall {k} (a :: k -> *) (x :: k) (xs :: [k]). a x -> NS a (x : xs)
SOP.Z forall a b. (a -> b) -> a -> b
$ forall a. a -> I a
SOP.I forall a b. (a -> b) -> a -> b
$ forall (h :: Symbol) v orig new.
AddHeader h v orig new =>
ResponseHeader h v -> orig -> new
addOptionalHeader ResponseHeader h v
hdr forall a b. (a -> b) -> a -> b
$ forall a. I a -> a
SOP.unI forall a b. (a -> b) -> a -> b
$ forall {k} (f :: k -> *) (x :: k). NS f '[x] -> f x
SOP.unZ forall a b. (a -> b) -> a -> b
$ Union '[old]
resp
instance
( AddHeader h v old new, AddHeader h v (Union oldrest) (Union newrest)
, oldrest ~ (a ': as), newrest ~ (b ': bs))
=> AddHeader h v (Union (old ': (a ': as))) (Union (new ': (b ': bs))) where
addOptionalHeader :: ResponseHeader h v -> Union (old : a : as) -> Union (new : b : bs)
addOptionalHeader ResponseHeader h v
hdr Union (old : a : as)
resp = case Union (old : a : as)
resp of
SOP.Z (SOP.I x
rHead) -> forall {k} (a :: k -> *) (x :: k) (xs :: [k]). a x -> NS a (x : xs)
SOP.Z forall a b. (a -> b) -> a -> b
$ forall a. a -> I a
SOP.I forall a b. (a -> b) -> a -> b
$ forall (h :: Symbol) v orig new.
AddHeader h v orig new =>
ResponseHeader h v -> orig -> new
addOptionalHeader ResponseHeader h v
hdr x
rHead
SOP.S NS I xs
rOthers -> forall {k} (a :: k -> *) (xs :: [k]) (x :: k).
NS a xs -> NS a (x : xs)
SOP.S forall a b. (a -> b) -> a -> b
$ forall (h :: Symbol) v orig new.
AddHeader h v orig new =>
ResponseHeader h v -> orig -> new
addOptionalHeader ResponseHeader h v
hdr NS I xs
rOthers
addHeader :: AddHeader h v orig new => v -> orig -> new
= forall (h :: Symbol) v orig new.
AddHeader h v orig new =>
ResponseHeader h v -> orig -> new
addOptionalHeader forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (sym :: Symbol) a. a -> ResponseHeader sym a
Header
noHeader :: AddHeader h v orig new => orig -> new
= forall (h :: Symbol) v orig new.
AddHeader h v orig new =>
ResponseHeader h v -> orig -> new
addOptionalHeader forall (sym :: Symbol) a. ResponseHeader sym a
MissingHeader
class h a headers where
:: HList headers -> ResponseHeader h a
instance {-# OVERLAPPING #-} HasResponseHeader h a (Header h a ': rest) where
hlistLookupHeader :: HList (Header h a : rest) -> ResponseHeader h a
hlistLookupHeader (HCons ResponseHeader h x
ha HList xs
_) = ResponseHeader h x
ha
instance {-# OVERLAPPABLE #-} (HasResponseHeader h a rest) => HasResponseHeader h a (first ': rest) where
hlistLookupHeader :: HList (first : rest) -> ResponseHeader h a
hlistLookupHeader (HCons ResponseHeader h x
_ HList xs
hs) = forall (h :: Symbol) a (headers :: [*]).
HasResponseHeader h a headers =>
HList headers -> ResponseHeader h a
hlistLookupHeader HList xs
hs
lookupResponseHeader :: (HasResponseHeader h a headers)
=> Headers headers r -> ResponseHeader h a
= forall (h :: Symbol) a (headers :: [*]).
HasResponseHeader h a headers =>
HList headers -> ResponseHeader h a
hlistLookupHeader forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (ls :: [*]) a. Headers ls a -> HList ls
getHeadersHList