X-Git-Url: http://git.cielonegro.org/gitweb.cgi?p=Lucu.git;a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FResponseWriter.hs;h=1370f05ea5581e59b7d072c3475698b2811e9f80;hp=24ee47ecc481d61b9b078d51bef8f7c4f53f9bee;hb=b495d6b8b7647b719eceef2f3e50d5bf87c430cf;hpb=bb121f1189d01b5089aa5c29f0d390fad36ade48 diff --git a/Network/HTTP/Lucu/ResponseWriter.hs b/Network/HTTP/Lucu/ResponseWriter.hs index 24ee47e..1370f05 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,46 @@ 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 + handleAsyncE ThreadKilled = return () + handleAsyncE e = dump e handleBIOS ∷ BlockedIndefinitelyOnSTM → IO () - handleBIOS = terminate + handleBIOS = dump handleOthers ∷ SomeException → IO () - handleOthers = terminate + handleOthers = dump - terminate ∷ Exception e ⇒ e → IO () - terminate e + dump ∷ Exception e ⇒ e → IO () + dump e = do hPutStrLn stderr "requestWriter caught an exception:" hPutStrLn stderr (show $ toException e) - terminate' - - terminate' ∷ IO () - terminate' = hClose h awaitSomethingToWrite ∷ HandleLike h ⇒ Context h → IO () awaitSomethingToWrite ctx@(Context {..}) @@ -85,7 +81,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 @@ -209,12 +206,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 +244,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 +256,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 ()