module Network.OpenID.SSL (
SSLHandle,
sslConnect
) where
import Prelude()
import Prelude.Compat
import OpenSSL.Session as Session
import qualified Control.Exception as E
import Network.Socket
import Network.Stream
import qualified Data.ByteString as B
import Data.ByteString.Internal (w2c, c2w)
import Data.Word
data SSLHandle = SSLHandle SSLContext SSL
wrap :: IO a -> IO (Either ConnError a)
wrap m = Right `fmap` m `E.catch` handler
where
handler :: E.SomeException -> IO (Either ConnError a)
handler err = return $ Left $ ErrorMisc $ "write: " ++ show err
wrapRead :: IO String -> IO (Either ConnError String)
wrapRead m = Right `fmap` m `E.catches` handlers
where
handlers :: [E.Handler (Either ConnError String)]
handlers =
[ E.Handler ((\_ -> return $ Right "")
:: (ConnectionAbruptlyTerminated -> IO (Either ConnError String)))
, E.Handler ((\x -> return $ Left $ ErrorMisc $ "read: " ++ show x)
:: (E.SomeException -> IO (Either ConnError String)))
]
instance Stream SSLHandle where
readLine sh =
wrapRead (upd `fmap` sslReadWhile (/= c) sh)
where
c = toEnum (fromEnum '\n')
upd bs = map (toEnum . fromEnum) bs ++ "\n"
readBlock (SSLHandle _ ssl) n =
wrapRead ((map w2c . B.unpack) <$> Session.read ssl n)
writeBlock (SSLHandle _ ssl) bs
| not (null bs) = wrap $ Session.write ssl $ B.pack $ map c2w bs
| otherwise = return $ Right ()
close (SSLHandle _ ssl) = Session.shutdown ssl Bidirectional
`E.catch` ((\_ -> return ()) :: E.SomeException -> IO ())
closeOnEnd _ _ = return ()
sslConnect :: Socket -> IO (Maybe SSLHandle)
sslConnect sock = body `E.catch` handler
where
body = do
ctx <- Session.context
ssl <- Session.connection ctx sock
Session.connect ssl
return $ Just $ SSLHandle ctx ssl
handler :: E.SomeException -> IO (Maybe SSLHandle)
handler _ = return Nothing
sslReadWhile :: (Word8 -> Bool) -> SSLHandle -> IO [Word8]
sslReadWhile p (SSLHandle _ ssl) = loop
where
loop = do
txt <- Session.read ssl 1
if B.null txt
then return []
else do
let c = B.head txt
if p c
then do
cs <- loop
return (c:cs)
else
return []