6 module Network.HTTP.Lucu.Interaction
10 , ReceiveBodyRequest(..)
17 import Blaze.ByteString.Builder (Builder)
18 import Control.Concurrent.STM
19 import qualified Data.ByteString as Strict
20 import Data.Monoid.Unicode
21 import Data.Sequence (Seq)
22 import qualified Data.Sequence as S
24 import Network.HTTP.Lucu.Config
25 import Network.HTTP.Lucu.HttpVersion
26 import Network.HTTP.Lucu.Preprocess
27 import Network.HTTP.Lucu.Request
28 import Network.HTTP.Lucu.Response
31 data Interaction = Interaction {
33 , itrLocalPort ∷ !PortNumber
34 , itrRemoteAddr ∷ !SockAddr
35 , itrRemoteCert ∷ !(Maybe X509)
36 , itrResourcePath ∷ !(Maybe [Strict.ByteString])
37 , itrRequest ∷ !(Maybe Request)
39 , itrExpectedContinue ∷ !(Maybe Bool)
40 , itrReqBodyLength ∷ !(Maybe RequestBodyLength)
42 , itrReceiveBodyReq ∷ !(TMVar ReceiveBodyRequest)
43 , itrReceivedBody ∷ !(TMVar Strict.ByteString)
45 , itrSendContinue ∷ !(TMVar Bool)
46 , itrResponse ∷ !(TVar Response)
47 , itrWillChunkBody ∷ !(TVar Bool)
48 , itrWillDiscardBody ∷ !(TVar Bool)
49 , itrWillClose ∷ !(TVar Bool)
50 , itrResponseHasCType ∷ !(TVar Bool)
51 , itrBodyToSend ∷ !(TMVar Builder)
53 , itrState ∷ !(TVar InteractionState)
56 -- |The interaction state of Resource monad. 'ExaminingRequest' is the
64 deriving (Show, Eq, Ord, Enum)
66 type InteractionQueue = TVar (Seq Interaction)
68 data ReceiveBodyRequest
69 = ReceiveBody !Int -- ^ Maximum number of octets to receive.
73 newInteractionQueue ∷ IO InteractionQueue
74 newInteractionQueue = newTVarIO S.empty
76 newInteraction ∷ Config
80 → Either StatusCode Request
82 newInteraction conf@(Config {..}) port addr cert request
83 = do let ar = preprocess cnfServerHost port request
85 resVersion = HttpVersion 1 1
86 , resStatus = arInitialStatus ar
90 receiveBodyReq ← newEmptyTMVarIO
91 receivedBody ← newEmptyTMVarIO
93 sendContinue ← newEmptyTMVarIO
94 response ← newTVarIO res
95 willChunkBody ← newTVarIO False
96 willDiscardBody ← newTVarIO (arWillDiscardBody ar)
97 willClose ← newTVarIO (arWillClose ar)
98 bodyToSend ← newEmptyTMVarIO
99 responseHasCType ← newTVarIO False
101 state ← newTVarIO ExaminingRequest
105 , itrLocalPort = port
106 , itrRemoteAddr = addr
107 , itrRemoteCert = cert
108 , itrResourcePath = Nothing
109 , itrRequest = arRequest ar
111 , itrExpectedContinue = arExpectedContinue ar
112 , itrReqBodyLength = arReqBodyLength ar
114 , itrReceiveBodyReq = receiveBodyReq
115 , itrReceivedBody = receivedBody
117 , itrSendContinue = sendContinue
118 , itrResponse = response
119 , itrWillChunkBody = willChunkBody
120 , itrWillDiscardBody = willDiscardBody
121 , itrWillClose = willClose
122 , itrResponseHasCType = responseHasCType
123 , itrBodyToSend = bodyToSend
128 setResponseStatus ∷ Interaction → StatusCode → STM ()
129 setResponseStatus (Interaction {..}) sc
130 = do res ← readTVar itrResponse
134 writeTVar itrResponse res'