on master help sign in
Allow for bracketing with ResponseRaw
commit1ba8711a7a0f471446efa3234af2bb3747bcf258 authorMichael Snoyman <michael@snoyman.com> dateThu 6 Mar 2014, 15:57 (13 years ago) parentdc2fd07
3 files changed+11−5
wai/Network/Wai.hs+4−1 wai/Network/Wai/Internal.hs+4−1 warp/Network/Wai/Handler/Warp/Response.hs+3−3
wai/Network/Wai.hs+4−1
@@ -161,11 +161,14 @@ responseSourceBracket setup teardown action =
161161-- This function requires a backup response to be provided, for the case where
162162-- the handler in question does not support such upgrading (e.g., CGI apps).
163163--
164+-- In the event that you read from the request body before returning a
165+-- @responseRaw@, behavior is undefined.
166+--
164167-- Since 2.1.0
165168responseRaw :: (C.Source IO B.ByteString -> C.Sink B.ByteString IO () -> IO ())
166169 -> Response
167170 -> Response
168−responseRaw = ResponseRaw
171+responseRaw rawApp fallback = ResponseRaw ($ rawApp) fallback
169172
170173----------------------------------------------------------------
171174
wai/Network/Wai/Internal.hs+4−1
@@ -87,9 +87,12 @@ data Response
8787 = ResponseFile H.Status H.ResponseHeaders FilePath (Maybe FilePart)
8888 | ResponseBuilder H.Status H.ResponseHeaders Builder
8989 | ResponseSource H.Status H.ResponseHeaders (forall b. WithSource IO (C.Flush Builder) b)
90− | ResponseRaw (C.Source IO B.ByteString -> C.Sink B.ByteString IO () -> IO ()) Response
90+ | ResponseRaw (forall b. WithRawApp b) Response
9191 deriving Typeable
9292
93+type RawApp = C.Source IO B.ByteString -> C.Sink B.ByteString IO () -> IO ()
94+type WithRawApp b = (RawApp -> IO b) -> IO b
95+
9396-- | Auxiliary type for 'ResponseSource'.
9497type WithSource m a b = (C.Source m a -> m b) -> m b
9598
warp/Network/Wai/Handler/Warp/Response.hs+3−3
@@ -193,7 +193,7 @@ sendResponse conn ii restore req reqidxhdr leftover' response = restore $ do
193193data Rsp = RspFile FilePath (Maybe FilePart) (Maybe HeaderValue) (IO ())
194194 | RspBuilder Builder Bool
195195 | RspSource (forall b. WithSource IO (Flush Builder) b) Bool T.Handle
196− | RspRaw (Source IO ByteString -> Sink ByteString IO () -> IO ()) (Maybe ByteString)
196+ | RspRaw (forall b. WithRawApp b) (Maybe ByteString)
197197
198198----------------------------------------------------------------
199199
@@ -247,8 +247,8 @@ sendRsp conn ver s hs (RspSource withBodyFlush needsChunked th) = withBodyFlush
247247
248248----------------------------------------------------------------
249249
250−sendRsp conn _ _ _ (RspRaw app mbs) =
251− app src sink
250+sendRsp conn _ _ _ (RspRaw withApp mbs) =
251+ withApp $ \app -> app src sink
252252 where
253253 sink = CL.mapM_ (connSendAll conn)
254254 src = do