X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FResponseWriter.hs;h=b4809eaa52e4d975b4afc16b8300a396ed85fe70;hb=9ed4b7476710930bb537c71d0c2341d7ea331767;hp=d89ee9e885aa114429489cdef1fb7c59466fb65b;hpb=f402841101b4b84f263eea1a43c848f81c48ff93;p=Lucu.git diff --git a/Network/HTTP/Lucu/ResponseWriter.hs b/Network/HTTP/Lucu/ResponseWriter.hs index d89ee9e..b4809ea 100644 --- a/Network/HTTP/Lucu/ResponseWriter.hs +++ b/Network/HTTP/Lucu/ResponseWriter.hs @@ -14,6 +14,7 @@ import Control.Concurrent import Control.Concurrent.STM import Control.Exception import Control.Monad +import GHC.IO.Exception (IOException(..), IOErrorType(..)) import qualified Data.Ascii as A import Data.Monoid.Unicode import qualified Data.Sequence as S @@ -25,51 +26,42 @@ import Network.HTTP.Lucu.Interaction import Network.HTTP.Lucu.Response import Prelude.Unicode import System.IO (hPutStrLn, stderr) -import System.IO.Error data Context h = Context { cConfig ∷ !Config , cHandle ∷ !h , cQueue ∷ !InteractionQueue - , cReader ∷ !ThreadId } responseWriter ∷ HandleLike h ⇒ Config → h → InteractionQueue → ThreadId → IO () responseWriter cnf h tQueue readerTID - = awaitSomethingToWrite (Context cnf h tQueue readerTID) + = awaitSomethingToWrite (Context cnf h tQueue) `catches` [ Handler handleIOE , Handler handleAsyncE - , Handler handleBIOS , Handler handleOthers ] + `finally` + do killThread readerTID + hClose h where handleIOE ∷ IOException → IO () - handleIOE e - | isIllegalOperation e - = return () -- EPIPE: should be ignored at all. - | otherwise - = terminate e + handleIOE e@(IOError {..}) + | ioe_type ≡ ResourceVanished = return () + | otherwise = dump e handleAsyncE ∷ AsyncException → IO () - handleAsyncE ThreadKilled = terminate' - handleAsyncE e = terminate e - - handleBIOS ∷ BlockedIndefinitelyOnSTM → IO () - handleBIOS = terminate + handleAsyncE ThreadKilled = return () + handleAsyncE e = dump e handleOthers ∷ SomeException → IO () - handleOthers = terminate - - terminate ∷ Exception e ⇒ e → IO () - terminate e - = do hPutStrLn stderr "requestWriter caught an exception:" - hPutStrLn stderr (show $ toException e) - terminate' + handleOthers = dump - terminate' ∷ IO () - terminate' = hClose h + dump ∷ Exception e ⇒ e → IO () + dump e + = do hPutStrLn stderr "Lucu: responseWriter caught an exception:" + hPutStrLn stderr $ show e awaitSomethingToWrite ∷ HandleLike h ⇒ Context h → IO () awaitSomethingToWrite ctx@(Context {..}) @@ -85,7 +77,8 @@ writeSomething ∷ HandleLike h ⇒ Context h → SomeInteraction → IO () writeSomething ctx itr = let writer = writeResponseForNI ctx <$> fromInteraction itr <|> writeResponseForSEI ctx <$> fromInteraction itr <|> - writeResponseForSYI ctx <$> fromInteraction itr + writeResponseForSYI ctx <$> fromInteraction itr <|> + endOfResponses <$> fromInteraction itr in case writer of Just f → f @@ -106,7 +99,7 @@ writeContinueIfNeeded ctx@(Context {..}) ni@(NI {..}) when isNeeded $ do let cont = Response { resVersion = HttpVersion 1 1 - , resStatus = Continue + , resStatus = fromStatusCode Continue , resHeaders = (∅) } hPutBuilder cHandle $ A.toBuilder $ printResponse cont @@ -209,12 +202,7 @@ finalize ctx@(Context {..}) (NI {..}) sentContinue ← takeTMVar niSendContinue return $ if needToClose willClose sentContinue then - -- The RequestReader is probably blocking on - -- hWaitForInput so we have to kill it before closing - -- the socket. THINKME: Couldn't that somehow be - -- avoided? - do killThread cReader - hClose cHandle + return () else awaitSomethingToWrite ctx where @@ -252,8 +240,7 @@ writeResponseForSEI ctx@(Context {..}) (SEI {..}) hPutBuilder cHandle seiBodyToSend hFlush cHandle if seiWillClose ∨ seiExpectedContinue then - do killThread cReader - hClose cHandle + return () else awaitSomethingToWrite ctx @@ -265,5 +252,7 @@ writeResponseForSYI (Context {..}) (SYI {..}) = do hPutBuilder cHandle $ A.toBuilder $ printResponse syiResponse hPutBuilder cHandle syiBodyToSend hFlush cHandle - killThread cReader - hClose cHandle + return () + +endOfResponses ∷ EndOfInteraction → IO () +endOfResponses _ = return ()