--- #hide
module Network.HTTP.Lucu.ResponseWriter
( responseWriter
)
where
import qualified Data.ByteString.Lazy.Char8 as B
-import Data.ByteString.Lazy.Char8 (ByteString)
+import Control.Concurrent
import Control.Concurrent.STM
import Control.Exception
import Control.Monad
-import Data.Maybe
import qualified Data.Sequence as S
-import Data.Sequence (Seq, ViewR(..))
+import Data.Sequence (ViewR(..))
import Network.HTTP.Lucu.Config
import Network.HTTP.Lucu.Format
+import Network.HTTP.Lucu.Headers
import Network.HTTP.Lucu.HttpVersion
import Network.HTTP.Lucu.Interaction
import Network.HTTP.Lucu.Postprocess
import Prelude hiding (catch)
import System.IO
-import Control.Concurrent
-import Debug.Trace
-import GHC.Conc (unsafeIOToSTM)
-
responseWriter :: Config -> Handle -> InteractionQueue -> ThreadId -> IO ()
responseWriter cnf h tQueue readerTID
where
awaitSomethingToWrite :: IO ()
awaitSomethingToWrite
- = do action
+ = {-# SCC "awaitSomethingToWrite" #-}
+ do action
<- atomically $!
do -- キューが空でなくなるまで待つ
queue <- readTVar tQueue
writeContinueIfNecessary :: Interaction -> STM (IO ())
writeContinueIfNecessary itr
- = itr `seq`
+ = {-# SCC "writeContinueIfNecessary" #-}
+ itr `seq`
do expectedContinue <- readItr itr itrExpectedContinue id
if expectedContinue then
do wroteContinue <- readItr itr itrWroteContinue id
-- れば、ヘッダを出力する。ヘッダ出力後であり、bodyToSend が
-- 空でなければ、それを出力する。空である時は、もし状態が
-- Done であれば後処理をする。
- = itr `seq`
+ = {-# SCC "writeHeaderOrBodyIfNecessary" #-}
+ itr `seq`
do wroteHeader <- readItr itr itrWroteHeader id
if not wroteHeader then
- return $ writeHeader itr
+ return $! writeHeader itr
else
do bodyToSend <- readItr itr itrBodyToSend id
writeContinue :: Interaction -> IO ()
writeContinue itr
- = itr `seq`
+ = {-# SCC "writeContinue" #-}
+ itr `seq`
do let cont = Response {
resVersion = HttpVersion 1 1
, resStatus = Continue
- , resHeaders = []
+ , resHeaders = emptyHeaders
}
cont' <- completeUnconditionalHeaders cnf cont
hPutResponse h cont'
writeHeader :: Interaction -> IO ()
writeHeader itr
- = itr `seq`
+ = {-# SCC "writeHeader" #-}
+ itr `seq`
do res <- atomically $! do writeItr itr itrWroteHeader True
readItr itr itrResponse id
hPutResponse h res
writeBodyChunk :: Interaction -> IO ()
writeBodyChunk itr
- = itr `seq`
+ = {-# SCC "writeBodyChunk" #-}
+ itr `seq`
do willDiscardBody <- atomically $! readItr itr itrWillDiscardBody id
willChunkBody <- atomically $! readItr itr itrWillChunkBody id
chunk <- atomically $! do chunk <- readItr itr itrBodyToSend id
finishBodyChunk :: Interaction -> IO ()
finishBodyChunk itr
- = itr `seq`
+ = {-# SCC "finishBodyChunk" #-}
+ itr `seq`
do willDiscardBody <- atomically $! readItr itr itrWillDiscardBody id
willChunkBody <- atomically $! readItr itr itrWillChunkBody id
when (not willDiscardBody && willChunkBody)
finalize :: Interaction -> IO ()
finalize itr
- = itr `seq`
+ = {-# SCC "finalize" #-}
+ itr `seq`
do finishBodyChunk itr
willClose <- atomically $!
do queue <- readTVar tQueue