{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}
module Network.TLS.Backend (
HasBackend (..),
Backend (..),
RecvWith (..),
) where
import Control.Concurrent.STM (STM)
import qualified Data.ByteString as B
import qualified Network.Socket as Network
import qualified Network.Socket.ByteString as Network
import System.IO (BufferMode (..), Handle, hClose, hFlush, hSetBuffering)
import Network.TLS.Imports
data Backend = Backend
{ Backend -> IO ()
backendFlush :: IO ()
, Backend -> IO ()
backendClose :: IO ()
, Backend -> ByteString -> IO ()
backendSend :: ByteString -> IO ()
, Backend -> Int -> IO ByteString
backendRecv :: Int -> IO ByteString
}
class HasBackend a where
initializeBackend :: a -> IO ()
getBackend :: a -> Backend
getBackendRecvWith :: a -> Maybe RecvWith
getBackendRecvWith a
_ = Maybe RecvWith
forall a. Maybe a
Nothing
newtype RecvWith = RecvWith (forall g. Int -> STM g -> IO (Either g ByteString))
instance HasBackend Backend where
initializeBackend :: Backend -> IO ()
initializeBackend Backend
_ = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
getBackend :: Backend -> Backend
getBackend = Backend -> Backend
forall a. a -> a
id
safeRecv :: Network.Socket -> Int -> IO ByteString
safeRecv :: Socket -> Int -> IO ByteString
safeRecv = Socket -> Int -> IO ByteString
Network.recv
instance HasBackend Network.Socket where
initializeBackend :: Socket -> IO ()
initializeBackend Socket
_ = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
getBackend :: Socket -> Backend
getBackend Socket
sock =
Backend
{ backendFlush :: IO ()
backendFlush = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
, backendClose :: IO ()
backendClose = Socket -> IO ()
Network.close Socket
sock
, backendSend :: ByteString -> IO ()
backendSend = Socket -> ByteString -> IO ()
Network.sendAll Socket
sock
, backendRecv :: Int -> IO ByteString
backendRecv = Int -> IO ByteString
recvAll
}
where
recvAll :: Int -> IO ByteString
recvAll Int
n = [ByteString] -> ByteString
B.concat ([ByteString] -> ByteString) -> IO [ByteString] -> IO ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> IO [ByteString]
loop Int
n
where
loop :: Int -> IO [ByteString]
loop Int
0 = [ByteString] -> IO [ByteString]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return []
loop Int
left = do
r <- Socket -> Int -> IO ByteString
safeRecv Socket
sock Int
left
if B.null r
then return []
else (r :) <$> loop (left - B.length r)
#if MIN_VERSION_network(3,2,12)
getBackendRecvWith :: Socket -> Maybe RecvWith
getBackendRecvWith Socket
sock = RecvWith -> Maybe RecvWith
forall a. a -> Maybe a
Just (RecvWith -> Maybe RecvWith) -> RecvWith -> Maybe RecvWith
forall a b. (a -> b) -> a -> b
$ (forall g. Int -> STM g -> IO (Either g ByteString)) -> RecvWith
RecvWith Int -> STM g -> IO (Either g ByteString)
forall g. Int -> STM g -> IO (Either g ByteString)
recvAllWith
where
recvAllWith :: Int -> STM a -> IO (Either a ByteString)
recvAllWith Int
n STM a
giveUp = Int -> [ByteString] -> IO (Either a ByteString)
loop Int
n []
where
done :: [ByteString] -> IO (Either a ByteString)
done = Either a ByteString -> IO (Either a ByteString)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either a ByteString -> IO (Either a ByteString))
-> ([ByteString] -> Either a ByteString)
-> [ByteString]
-> IO (Either a ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either a ByteString
forall a b. b -> Either a b
Right (ByteString -> Either a ByteString)
-> ([ByteString] -> ByteString)
-> [ByteString]
-> Either a ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> ByteString
B.concat ([ByteString] -> ByteString)
-> ([ByteString] -> [ByteString]) -> [ByteString] -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> [ByteString]
forall a. [a] -> [a]
reverse
loop :: Int -> [ByteString] -> IO (Either a ByteString)
loop Int
0 [ByteString]
acc = [ByteString] -> IO (Either a ByteString)
forall {a}. [ByteString] -> IO (Either a ByteString)
done [ByteString]
acc
loop Int
left [ByteString]
acc = do
r <- Socket -> Int -> STM a -> IO (Either a ByteString)
forall a. Socket -> Int -> STM a -> IO (Either a ByteString)
Network.recvWith Socket
sock Int
left STM a
giveUp
case r of
Left a
g
| [ByteString] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ByteString]
acc -> Either a ByteString -> IO (Either a ByteString)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either a ByteString -> IO (Either a ByteString))
-> Either a ByteString -> IO (Either a ByteString)
forall a b. (a -> b) -> a -> b
$ a -> Either a ByteString
forall a b. a -> Either a b
Left a
g
| Bool
otherwise -> [ByteString] -> IO (Either a ByteString)
forall {a}. [ByteString] -> IO (Either a ByteString)
done [ByteString]
acc
Right ByteString
bs
| ByteString -> Bool
B.null ByteString
bs -> [ByteString] -> IO (Either a ByteString)
forall {a}. [ByteString] -> IO (Either a ByteString)
done [ByteString]
acc
| Bool
otherwise -> Int -> [ByteString] -> IO (Either a ByteString)
loop (Int
left Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
B.length ByteString
bs) (ByteString
bs ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
acc)
#endif
instance HasBackend Handle where
initializeBackend :: Handle -> IO ()
initializeBackend Handle
handle = Handle -> BufferMode -> IO ()
hSetBuffering Handle
handle BufferMode
NoBuffering
getBackend :: Handle -> Backend
getBackend Handle
handle =
Backend
{ backendFlush :: IO ()
backendFlush = Handle -> IO ()
hFlush Handle
handle
, backendClose :: IO ()
backendClose = Handle -> IO ()
hClose Handle
handle
, backendSend :: ByteString -> IO ()
backendSend = Handle -> ByteString -> IO ()
B.hPut Handle
handle
, backendRecv :: Int -> IO ByteString
backendRecv = Handle -> Int -> IO ByteString
B.hGet Handle
handle
}