@@ -99,6 +99,10 @@ runSession session app = ST.evalStateT (runReaderT session app) initState
99data SRequest = SRequest
99data SRequest = SRequest
100 { simpleRequest :: Request
100 { simpleRequest :: Request
101 , simpleRequestBody :: L.ByteString
101 , simpleRequestBody :: L.ByteString
102+ -- ^ Request body that will override the one set in 'simpleRequest'.
104+ -- This is usually simpler than setting the body as a stateful IO-action
105+ -- in 'simpleRequest'.
103data SResponse = SResponse
107data SResponse = SResponse
104 { simpleStatus :: H.Status
108 { simpleStatus :: H.Status
@@ -106,8 +110,16 @@ data SResponse = SResponse
106 , simpleBody :: L.ByteString
110 , simpleBody :: L.ByteString
108 deriving (Show, Eq)
112 deriving (Show, Eq)
109request :: Request -> Session SResponse
114request :: Request -> Session SResponse
110−request = srequest . flip SRequest L.empty
115+request req = do
117+ req' <- addCookiesToRequest req
118+ response <- liftIO $ do
119+ ref <- newIORef $ error "runResponse gave no result"
120+ ResponseReceived <- app req' (runResponse ref)
122+ extractSetCookieFromSResponse response
112-- | Set whole path (request path + query string).
124-- | Set whole path (request path + query string).
113setPath :: Request -> S8.ByteString -> Request
125setPath :: Request -> S8.ByteString -> Request
@@ -166,22 +178,18 @@ extractSetCookieFromSResponse response = do
166 (Map.fromList [(Cookie.setCookieName c, c) | c <- newClientCookies ]))
178 (Map.fromList [(Cookie.setCookieName c, c) | c <- newClientCookies ]))
167 return response
179 return response
181+-- | Similar to 'request', but allows setting the request body as a plain
169srequest :: SRequest -> Session SResponse
183srequest :: SRequest -> Session SResponse
170srequest (SRequest req bod) = do
184srequest (SRequest req bod) = do
172 refChunks <- liftIO $ newIORef $ L.toChunks bod
185 refChunks <- liftIO $ newIORef $ L.toChunks bod
173− let req' = req
187+ req
174− { requestBody = atomicModifyIORef refChunks $ \bss ->
188+ { requestBody = atomicModifyIORef refChunks $ \bss ->
175− case bss of
189+ case bss of
176− [] -> ([], S.empty)
190+ [] -> ([], S.empty)
177− x:y -> (y, x)
191+ x:y -> (y, x)
179− req'' <- addCookiesToRequest req'
180− response <- liftIO $ do
181− ref <- newIORef $ error "runResponse gave no result"
182− ResponseReceived <- app req'' (runResponse ref)
184− extractSetCookieFromSResponse response
186runResponse :: IORef SResponse -> Response -> IO ResponseReceived
194runResponse :: IORef SResponse -> Response -> IO ResponseReceived
187runResponse ref res = do
195runResponse ref res = do