@@ -1,6 +1,5 @@
11{-# LANGUAGE Rank2Types #-}
22{-# LANGUAGE CPP #-}
3−{-# LANGUAGE ImpredicativeTypes #-}
43{-|
54
65This module defines a generic web application interface. It is a common
@@ -78,7 +77,6 @@ module Network.Wai
7877
7978import Blaze.ByteString.Builder (Builder, fromLazyByteString)
8079import Blaze.ByteString.Builder (fromByteString)
81−import Control.Exception (bracket, bracketOnError)
8280import Control.Monad (unless)
8381import qualified Data.ByteString as B
8482import qualified Data.ByteString.Lazy as L
@@ -171,12 +169,23 @@ responseHeaders (ResponseRaw _ res) = responseHeaders res
171169responseToStream :: Response
172170 -> ( H.Status
173171 , H.ResponseHeaders
174− , forall a. (StreamingBody -> IO a) -> IO a
172+ , (StreamingBody -> IO a) -> IO a
175173 )
176174responseToStream (ResponseStream s h b) = (s, h, ($ b))
177175responseToStream (ResponseFile s h fp (Just part)) =
178− error "FIXME responseToStream"
179− --(s, h, \f -> IO.withFile fp IO.ReadMode $ \handle -> f $ sourceFilePart handle part C.$= CL.map (C.Chunk . fromByteString))
176+ ( s
177+ , h
178+ , \withBody -> IO.withFile fp IO.ReadMode $ \handle -> withBody $ \sendChunk _flush -> do
179+ IO.hSeek handle IO.AbsoluteSeek $ filePartOffset part
180+ let loop remaining | remaining <= 0 = return ()
181+ loop remaining = do
182+ bs <- B.hGetSome handle defaultChunkSize
183+ unless (B.null bs) $ do
184+ let x = B.take remaining bs
185+ sendChunk $ fromByteString x
186+ loop $ remaining - B.length x
187+ loop $ fromIntegral $ filePartByteCount part
188+ )
180189responseToStream (ResponseFile s h fp Nothing) =
181190 ( s
182191 , h
@@ -191,12 +200,6 @@ responseToStream (ResponseBuilder s h b) =
191200 (s, h, \withBody -> withBody $ \sendChunk _flush -> sendChunk b)
192201responseToStream (ResponseRaw _ res) = responseToStream res
193202
194−{- FIXME
195−sourceFilePart :: IO.Handle -> FilePart -> C.Source IO B.ByteString
196−sourceFilePart handle (FilePart offset count _) =
197− CB.sourceHandleRange handle (Just offset) (Just count)
198−-}
199−
200203----------------------------------------------------------------
201204
202205-- | The WAI application.