packages feed

simple-sendfile-0.2.34: Network/Sendfile/Fallback.hs

module Network.Sendfile.Fallback (
    sendfile
  , sendfileWithHeader
  , sendfileWith
  , sendfileWithHeaderWith
  ) where

import Control.Monad.IO.Class (MonadIO(liftIO))
import GHC.Conc (STM)
import Data.ByteString (ByteString)
import Data.Conduit
import qualified Data.Conduit.Binary as EB
import Network.Sendfile.Types
import Network.Socket
import qualified Network.Socket.ByteString as SB
import System.IO (Handle, IOMode(ReadMode), withBinaryFile)

-- |
-- Sendfile emulation using conduit.
--
--  - Used system calls: open(), stat(), read(), send() and close().

sendfile :: Socket -> FilePath -> FileRange -> IO () -> IO ()
sendfile sock path range hook = do
    checkRange range
    withBinaryFile path ReadMode $ \h -> sendHandle sock h range hook

-- |
-- Sendfile emulation using conduit.
--
--  - Used system calls: open(), stat(), read(), writev(), send() and close().
--
-- The file is opened before the header is sent, so nothing is sent
-- if the file cannot be opened.

sendfileWithHeader :: Socket -> FilePath -> FileRange -> IO () -> [ByteString] -> IO ()
sendfileWithHeader sock path range hook hdr = do
    checkRange range
    withBinaryFile path ReadMode $ \h -> do
        SB.sendMany sock hdr
        sendHandle sock h range hook

-- | 'sendfile' which gives up once the 'STM' action succeeds, while
--   waiting for the socket to become writable.  What the action gave is
--   returned as 'Left'; part of the file may have been sent by then.
sendfileWith :: Socket -> FilePath -> FileRange -> IO () -> STM a -> IO (Either a ())
sendfileWith sock path range hook giveUp = do
    checkRange range
    withBinaryFile path ReadMode $ \h -> sendHandleWith sock h range hook giveUp

-- | 'sendfileWithHeader' which gives up as 'sendfileWith' does, also
--   while sending the header.
sendfileWithHeaderWith :: Socket -> FilePath -> FileRange -> IO () -> [ByteString] -> STM a -> IO (Either a ())
sendfileWithHeaderWith sock path range hook hdr giveUp = do
    checkRange range
    withBinaryFile path ReadMode $ \h -> do
        r <- SB.sendManyWith sock hdr giveUp
        either (return . Left) (\_ -> sendHandleWith sock h range hook giveUp) r

sendHandleWith :: Socket -> Handle -> FileRange -> IO () -> STM a -> IO (Either a ())
sendHandleWith sock h range hook giveUp = runConduit $ source h range .| loop
  where
    loop = await >>= maybe (return $ Right ()) send
    send bs = do
        r <- liftIO $ SB.sendAllWith sock bs giveUp
        either (return . Left) (\_ -> liftIO hook >> loop) r

sendHandle :: Socket -> Handle -> FileRange -> IO () -> IO ()
sendHandle sock h range hook = runConduit $ source h range .| sinkSocket sock hook

source :: MonadIO m => Handle -> FileRange -> ConduitT i ByteString m ()
source h range = case range of
    EntireFile         -> EB.sourceHandle h
    PartOfFile off len -> EB.sourceHandleRange h (Just off) (Just len)

-- See sinkHandle.
sinkSocket :: MonadIO m => Socket -> IO () -> ConduitT ByteString Void m ()
sinkSocket s hook = awaitForever $ \bs -> liftIO $ SB.sendAll s bs >> hook