From 03ffd0a0fb9e6be91ee94ff3bb7c5341900c061c Mon Sep 17 00:00:00 2001 From: Jeremy Shaw Date: Sun, 5 Jun 2016 07:32:58 -0400 Subject: [PATCH] initial support for websockets --- src/Happstack/Server/Internal/Handler.hs | 15 +++++++++++---- src/Happstack/Server/Internal/Monads.hs | 9 ++++++++- src/Happstack/Server/Internal/TimeoutIO.hs | 1 + .../Server/Internal/TimeoutSocket.hs | 11 +++++++++++ src/Happstack/Server/Internal/Types.hs | 19 ++++++++++++++++++- src/Happstack/Server/Monads.hs | 3 +++ 6 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/Happstack/Server/Internal/Handler.hs b/src/Happstack/Server/Internal/Handler.hs index 6da2865..b1a3caa 100644 --- a/src/Happstack/Server/Internal/Handler.hs +++ b/src/Happstack/Server/Internal/Handler.hs @@ -56,7 +56,7 @@ rloop :: TimeoutIO rloop timeoutIO mlog host handler inputStr | L.null inputStr = return () | otherwise - = join $ + = (join $ do let parseRequest = do (topStr, restStr) <- required "failed to separate request" $ splitAtEmptyLine inputStr @@ -76,7 +76,7 @@ rloop timeoutIO mlog host handler inputStr case parseRequest of Left err -> error $ "failed to parse HTTP request: " ++ err Right (m, u, cookies, v, headers, body, nextRequest) - -> return $ + -> pure $ do bodyRef <- newMVar (Body body) bodyInputRef <- newEmptyMVar let req = Request (toSecure timeoutIO) m (pathEls (path u)) (path u) (query u) @@ -85,7 +85,9 @@ rloop timeoutIO mlog host handler inputStr let ioseq act = act >>= \x -> x `seq` return x (res, handlerKilled) <- ((, False) `liftM` ioseq (handler req)) - `E.catch` \(e::E.SomeException) -> return (failResponse (show e), fromException e == Just ThreadKilled) + `E.catches` [ Handler $ \(e::EscapeHTTP) -> throwIO e -- need to handle this higher up + , Handler $ \(e::E.SomeException) -> pure (failResponse (show e), fromException e == Just ThreadKilled) + ] case mlog of Nothing -> return () @@ -106,7 +108,12 @@ rloop timeoutIO mlog host handler inputStr cleanupTempFiles req -- do not continue if handler was killed when (not handlerKilled && continueHTTP req res) $ - rloop timeoutIO mlog host handler nextRequest + rloop timeoutIO mlog host handler nextRequest) `E.catch` (escapeHttpHandler timeoutIO) + +escapeHttpHandler :: TimeoutIO + -> EscapeHTTP + -> IO () +escapeHttpHandler tio (EscapeHTTP f) = f tio -- NOTE: if someone took the inputs and never put them back, then they are responsible for the cleanup cleanupTempFiles :: Request -> IO () diff --git a/src/Happstack/Server/Internal/Monads.hs b/src/Happstack/Server/Internal/Monads.hs index 6888b2f..8c89758 100644 --- a/src/Happstack/Server/Internal/Monads.hs +++ b/src/Happstack/Server/Internal/Monads.hs @@ -6,6 +6,7 @@ module Happstack.Server.Internal.Monads where import Control.Applicative (Applicative, pure, (<*>), Alternative(empty,(<|>))) import Control.Concurrent (newMVar) +import Control.Exception (throwIO) import Control.Monad ( MonadPlus(mzero, mplus), ap, liftM, msum ) @@ -50,7 +51,8 @@ import Debug.Trace (trace) import Happstack.Server.Internal.Cookie (Cookie) import Happstack.Server.Internal.RFC822Headers (parseContentType) -import Happstack.Server.Internal.Types (canHaveBody) +import Happstack.Server.Internal.Types (EscapeHTTP(..), canHaveBody) +import Happstack.Server.Internal.TimeoutIO (TimeoutIO) import Happstack.Server.Types import Prelude (Bool(..), Either(..), Eq(..), Functor(..), IO, Monad(..), Char, Maybe(..), String, Show(..), ($), (.), (>), (++), (&&), (||), (=<<), const, concatMap, flip, id, otherwise, zip) @@ -809,3 +811,8 @@ instance (FilterMonad a m) => FilterMonad a (ExceptT e m) where instance WebMonad a m => WebMonad a (ExceptT e m) where finishWith = lift . finishWith + +escapeHTTP :: (ServerMonad m, MonadIO m) => + (TimeoutIO -> IO ()) + -> m a +escapeHTTP h = liftIO (throwIO (EscapeHTTP h)) diff --git a/src/Happstack/Server/Internal/TimeoutIO.hs b/src/Happstack/Server/Internal/TimeoutIO.hs index 78a564a..3149e99 100644 --- a/src/Happstack/Server/Internal/TimeoutIO.hs +++ b/src/Happstack/Server/Internal/TimeoutIO.hs @@ -15,6 +15,7 @@ data TimeoutIO = TimeoutIO { toHandle :: Handle , toPutLazy :: L.ByteString -> IO () , toPut :: B.ByteString -> IO () + , toGet :: IO (Maybe B.ByteString) , toGetContents :: IO L.ByteString , toSendFile :: FilePath -> Offset -> ByteCount -> IO () , toShutdown :: IO () diff --git a/src/Happstack/Server/Internal/TimeoutSocket.hs b/src/Happstack/Server/Internal/TimeoutSocket.hs index ac2bd47..d304560 100644 --- a/src/Happstack/Server/Internal/TimeoutSocket.hs +++ b/src/Happstack/Server/Internal/TimeoutSocket.hs @@ -34,6 +34,16 @@ sPutTickle thandle sock cs = return () {-# INLINE sPutTickle #-} +sGet :: TM.Handle + -> Socket + -> IO (Maybe B.ByteString) +sGet handle socket = + do s <- N.recv socket 65536 + TM.tickle handle + if S.null s + then pure Nothing + else pure (Just s) + sGetContents :: TM.Handle -> Socket -- ^ Connected socket -> IO L.ByteString -- ^ Data received @@ -79,6 +89,7 @@ timeoutSocketIO handle socket = TimeoutIO { toHandle = handle , toShutdown = sClose socket , toPutLazy = sPutLazyTickle handle socket + , toGet = sGet handle socket , toPut = sPutTickle handle socket , toGetContents = sGetContents handle socket , toSendFile = sendFileTickle handle socket diff --git a/src/Happstack/Server/Internal/Types.hs b/src/Happstack/Server/Internal/Types.hs index 70d56a5..fc413d3 100644 --- a/src/Happstack/Server/Internal/Types.hs +++ b/src/Happstack/Server/Internal/Types.hs @@ -16,9 +16,11 @@ module Happstack.Server.Internal.Types HttpVersion(..), Length(..), Method(..), canHaveBody, Headers, continueHTTP, Host, ContentType(..), readDec', fromReadS, readM, FromReqURI(..), - showRsValidator + showRsValidator, EscapeHTTP(..) ) where + +import Control.Exception (Exception, SomeException) import Control.Monad.Error (Error(strMsg)) import Control.Monad.Trans (MonadIO(liftIO)) import qualified Control.Concurrent.Thread.Group as TG @@ -43,6 +45,7 @@ import Data.Char (toLower) import Happstack.Server.Internal.RFC822Headers ( ContentType(..) ) import Happstack.Server.Internal.Cookie import Happstack.Server.Internal.LogFormat (formatRequestCombined) +import Happstack.Server.Internal.TimeoutIO (TimeoutIO) import Numeric (readDec, readSigned) import System.Log.Logger (Priority(..), logM) @@ -515,3 +518,17 @@ instance FromReqURI Bool where "1" -> Just True "true" -> Just True _ -> Nothing + +------------------------------------------------------------------------------ +-- EscapeHTTP - escape hatched use by websockets +------------------------------------------------------------------------------ + +-- | Escape from the HTTP world and get direct access to the underlying 'TimeoutIO' functions +data EscapeHTTP + = EscapeHTTP (TimeoutIO -> IO ()) + deriving (Typeable) + +instance Exception EscapeHTTP + +instance Show EscapeHTTP where + show (EscapeHTTP {}) = "" diff --git a/src/Happstack/Server/Monads.hs b/src/Happstack/Server/Monads.hs index 4034d95..70bd1ca 100644 --- a/src/Happstack/Server/Monads.hs +++ b/src/Happstack/Server/Monads.hs @@ -35,6 +35,8 @@ module Happstack.Server.Monads -- * MonadPlus helpers , require , requireM + -- * escapeHTTP + , escapeHTTP ) where import Control.Applicative (Alternative, Applicative) @@ -109,3 +111,4 @@ requireM fn handle = do case mbVal of Nothing -> mzero Just a -> handle a +