{-# LANGUAGE CPP #-}
module Network.Wai.Handler.Warp.HTTP2.File where
import Network.HTTP2.Server
import Network.Wai.Handler.Warp.Types
#ifdef WINDOWS
pReadMaker :: InternalInfo -> PositionReadMaker
pReadMaker _ = defaultPositionReadMaker
#else
import Network.Wai.Handler.Warp.FdCache
import Network.Wai.Handler.Warp.SendFile (positionRead)
pReadMaker :: InternalInfo -> PositionReadMaker
pReadMaker :: InternalInfo -> PositionReadMaker
pReadMaker InternalInfo
ii FilePath
path = do
(mfd, refresh) <- InternalInfo -> FilePath -> IO (Maybe Fd, Refresh)
getFd InternalInfo
ii FilePath
path
case mfd of
Just Fd
fd -> (PositionRead, Sentinel) -> IO (PositionRead, Sentinel)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Fd -> PositionRead
pread Fd
fd, Refresh -> Sentinel
Refresher Refresh
refresh)
Maybe Fd
Nothing -> do
fd <- FilePath -> IO Fd
openFile FilePath
path
return (pread fd, Closer $ closeFile fd)
where
pread :: Fd -> PositionRead
pread :: Fd -> PositionRead
pread Fd
fd ByteCount
off ByteCount
bytes Buffer
buf = Int -> ByteCount
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> ByteCount) -> IO Int -> IO ByteCount
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Fd -> Buffer -> Int -> Integer -> IO Int
positionRead Fd
fd Buffer
buf Int
bytes' Integer
off'
where
bytes' :: Int
bytes' = ByteCount -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
bytes
off' :: Integer
off' = ByteCount -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteCount
off
#endif