{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}

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

import qualified UnliftIO
import qualified Network.HTTP.Types as H
import qualified Network.HTTP2.Server as H2

import Network.Wai
import Network.Wai.Handler.Warp.FileInfoCache
import Network.Wai.Handler.Warp.HTTP2.Request (getHTTP2Data)
import Network.Wai.Handler.Warp.HTTP2.Types
import Network.Wai.Handler.Warp.Imports
import Network.Wai.Handler.Warp.Types

fromPushPromises :: InternalInfo -> Request -> IO [H2.PushPromise]
fromPushPromises :: InternalInfo -> Request -> IO [PushPromise]
fromPushPromises InternalInfo
ii Request
req = do
    Maybe HTTP2Data
mh2data <- Request -> IO (Maybe HTTP2Data)
getHTTP2Data Request
req
    let pp :: [PushPromise]
pp = case Maybe HTTP2Data
mh2data of
          Maybe HTTP2Data
Nothing     -> []
          Just HTTP2Data
h2data -> HTTP2Data -> [PushPromise]
http2dataPushPromise HTTP2Data
h2data
    forall a. [Maybe a] -> [a]
catMaybes forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (InternalInfo -> PushPromise -> IO (Maybe PushPromise)
fromPushPromise InternalInfo
ii) [PushPromise]
pp

fromPushPromise :: InternalInfo -> PushPromise -> IO (Maybe H2.PushPromise)
fromPushPromise :: InternalInfo -> PushPromise -> IO (Maybe PushPromise)
fromPushPromise InternalInfo
ii (PushPromise ByteString
path FilePath
file ResponseHeaders
rsphdr Weight
w) = do
    Either IOException FileInfo
efinfo <- forall (m :: * -> *) a.
MonadUnliftIO m =>
m a -> m (Either IOException a)
UnliftIO.tryIO forall a b. (a -> b) -> a -> b
$ InternalInfo -> FilePath -> IO FileInfo
getFileInfo InternalInfo
ii FilePath
file
    case Either IOException FileInfo
efinfo of
      Left (IOException
_ex :: UnliftIO.IOException) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
      Right FileInfo
finfo -> do
          let !siz :: ByteCount
siz = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ FileInfo -> Integer
fileInfoSize FileInfo
finfo
              !fileSpec :: FileSpec
fileSpec = FilePath -> ByteCount -> ByteCount -> FileSpec
H2.FileSpec FilePath
file ByteCount
0 ByteCount
siz
              !rsp :: Response
rsp = Status -> ResponseHeaders -> FileSpec -> Response
H2.responseFile Status
H.ok200 ResponseHeaders
rsphdr FileSpec
fileSpec
              !pp :: PushPromise
pp = ByteString -> Response -> Weight -> PushPromise
H2.pushPromise ByteString
path Response
rsp Weight
w
          forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just PushPromise
pp