{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}

module Network.Wai.Handler.Warp.HTTP2.Types where

import qualified Data.ByteString as BS
import qualified Network.HTTP.Types as H
import Network.HTTP2.Frame
import qualified Network.HTTP2.Server as H2

import Network.Wai.Handler.Warp.Imports
import Network.Wai.Handler.Warp.Types

----------------------------------------------------------------

isHTTP2 :: Transport -> Bool
isHTTP2 :: Transport -> Bool
isHTTP2 Transport
TCP = Bool
False
isHTTP2 Transport
tls = Bool
useHTTP2
  where
    useHTTP2 :: Bool
useHTTP2 = case Transport -> Maybe ByteString
tlsNegotiatedProtocol Transport
tls of
        Maybe ByteString
Nothing -> Bool
False
        Just ByteString
proto -> ByteString
"h2" ByteString -> ByteString -> Bool
`BS.isPrefixOf` ByteString
proto

----------------------------------------------------------------

-- | HTTP/2 specific data.
--
--   Since: 3.2.7
data HTTP2Data = HTTP2Data
    { HTTP2Data -> [PushPromise]
http2dataPushPromise :: [PushPromise]
    -- ^ Accessor for 'PushPromise' in 'HTTP2Data'.
    --
    --   Since: 3.2.7
    , HTTP2Data -> TrailersMaker
http2dataTrailers :: H2.TrailersMaker
    -- ^ Accessor for 'H2.TrailersMaker' in 'HTTP2Data'.
    --
    --   Since: 3.2.8 but the type changed in 3.3.0
    }

-- | Default HTTP/2 specific data.
--
--   Since: 3.2.7
defaultHTTP2Data :: HTTP2Data
defaultHTTP2Data :: HTTP2Data
defaultHTTP2Data = [PushPromise] -> TrailersMaker -> HTTP2Data
HTTP2Data [] TrailersMaker
H2.defaultTrailersMaker

-- | HTTP/2 push promise or sever push.
--   This allows files only for backward-compatibility
--   while the HTTP/2 library supports other types.
--
--   Since: 3.2.7
data PushPromise = PushPromise
    { PushPromise -> ByteString
promisedPath :: ByteString
    -- ^ Accessor for a URL path in 'PushPromise'.
    --   E.g. \"\/style\/default.css\".
    --
    --   Since: 3.2.7
    , PushPromise -> FilePath
promisedFile :: FilePath
    -- ^ Accessor for 'FilePath' in 'PushPromise'.
    --   E.g. \"FILE_PATH/default.css\".
    --
    --   Since: 3.2.7
    , PushPromise -> ResponseHeaders
promisedResponseHeaders :: H.ResponseHeaders
    -- ^ Accessor for 'H.ResponseHeaders' in 'PushPromise'
    --   \"content-type\" must be specified.
    --   Default value: [].
    --
    --
    --   Since: 3.2.7
    , PushPromise -> Weight
promisedWeight :: Weight
    -- ^ Accessor for 'Weight' in 'PushPromise'.
    --    Default value: 16.
    --
    --   Since: 3.2.7
    }
    deriving (PushPromise -> PushPromise -> Bool
(PushPromise -> PushPromise -> Bool)
-> (PushPromise -> PushPromise -> Bool) -> Eq PushPromise
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PushPromise -> PushPromise -> Bool
== :: PushPromise -> PushPromise -> Bool
$c/= :: PushPromise -> PushPromise -> Bool
/= :: PushPromise -> PushPromise -> Bool
Eq, Eq PushPromise
Eq PushPromise =>
(PushPromise -> PushPromise -> Ordering)
-> (PushPromise -> PushPromise -> Bool)
-> (PushPromise -> PushPromise -> Bool)
-> (PushPromise -> PushPromise -> Bool)
-> (PushPromise -> PushPromise -> Bool)
-> (PushPromise -> PushPromise -> PushPromise)
-> (PushPromise -> PushPromise -> PushPromise)
-> Ord PushPromise
PushPromise -> PushPromise -> Bool
PushPromise -> PushPromise -> Ordering
PushPromise -> PushPromise -> PushPromise
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: PushPromise -> PushPromise -> Ordering
compare :: PushPromise -> PushPromise -> Ordering
$c< :: PushPromise -> PushPromise -> Bool
< :: PushPromise -> PushPromise -> Bool
$c<= :: PushPromise -> PushPromise -> Bool
<= :: PushPromise -> PushPromise -> Bool
$c> :: PushPromise -> PushPromise -> Bool
> :: PushPromise -> PushPromise -> Bool
$c>= :: PushPromise -> PushPromise -> Bool
>= :: PushPromise -> PushPromise -> Bool
$cmax :: PushPromise -> PushPromise -> PushPromise
max :: PushPromise -> PushPromise -> PushPromise
$cmin :: PushPromise -> PushPromise -> PushPromise
min :: PushPromise -> PushPromise -> PushPromise
Ord, Weight -> PushPromise -> ShowS
[PushPromise] -> ShowS
PushPromise -> FilePath
(Weight -> PushPromise -> ShowS)
-> (PushPromise -> FilePath)
-> ([PushPromise] -> ShowS)
-> Show PushPromise
forall a.
(Weight -> a -> ShowS)
-> (a -> FilePath) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Weight -> PushPromise -> ShowS
showsPrec :: Weight -> PushPromise -> ShowS
$cshow :: PushPromise -> FilePath
show :: PushPromise -> FilePath
$cshowList :: [PushPromise] -> ShowS
showList :: [PushPromise] -> ShowS
Show)

-- | Default push promise.
--
--   Since: 3.2.7
defaultPushPromise :: PushPromise
defaultPushPromise :: PushPromise
defaultPushPromise = ByteString -> FilePath -> ResponseHeaders -> Weight -> PushPromise
PushPromise ByteString
"" FilePath
"" [] Weight
16