on master help sign in
enumFileRange from enumerator
commit2437258700a62f294a304d16f79fc28ffc73ba9d authorMichael Snoyman <michael@snoyman.com> dateMon 28 Mar 2011, 08:04 (16 years ago) parent6de3a32
2 files changed+7−38
Network/Wai.hs+6−37 wai.cabal+1−1
Network/Wai.hs+6−37
@@ -41,21 +41,15 @@ module Network.Wai
4141import qualified Data.ByteString as B
4242import qualified Data.ByteString.Lazy as L
4343import Data.Typeable (Typeable)
44−import Data.Enumerator
45− ( Enumerator, Iteratee (..), ($$), joinI, run_, returnI
46− , Stream (Chunks), (>>==), continue, Step (Continue)
47− , throwError
48− )
44+import Data.Enumerator (Enumerator, Iteratee (..), ($$), joinI, run_)
4945import qualified Data.Enumerator as E
50−import Data.Enumerator.Binary (enumFile)
46+import qualified Data.Enumerator.List as EL
47+import Data.Enumerator.Binary (enumFile, enumFileRange)
5148import Blaze.ByteString.Builder (Builder, fromByteString, fromLazyByteString)
5249import Network.Socket (SockAddr)
5350import qualified Network.HTTP.Types as H
5451import Data.Text (Text)
5552import Data.ByteString.Lazy.Char8 () -- makes it easier to use responseLBS
56−import qualified System.IO as IO
57−import qualified Control.Exception as Exc
58−import Control.Monad.IO.Class (liftIO)
5953
6054-- | Information on the request sent by the client. This abstracts away the
6155-- details of the underlying implementation.
@@ -104,39 +98,14 @@ responseEnumerator :: Response -> ResponseEnumerator a
10498responseEnumerator (ResponseEnumerator e) f = e f
10599responseEnumerator (ResponseFile s h fp mpart) f =
106100 run_ $ (maybe enumFile enumFilePart) mpart fp $$ joinI
107− $ E.map fromByteString $$ f s h
101+ $ EL.map fromByteString $$ f s h
108102responseEnumerator (ResponseBuilder s h b) f = run_ $ do
109103 E.yield () $ E.Chunks [b]
110104 f s h
111105
112−tryStep :: IO t -> (t -> Iteratee a IO b) -> Iteratee a IO b
113−tryStep get io = do
114− tried <- liftIO $ Exc.try get
115− case tried of
116− Right t -> io t
117− Left err -> throwError (err :: Exc.SomeException)
118−
119106enumFilePart :: FilePart -> FilePath -> Enumerator B.ByteString IO a
120−enumFilePart (FilePart offset count) fp step = withHandle $ \h -> do
121− liftIO $ IO.hSeek h IO.AbsoluteSeek offset
122− Iteratee $ Exc.finally
123− (runIteratee $ enumHandleCount count 4096 h step)
124− (IO.hClose h)
125− where
126− withHandle = tryStep $ IO.openBinaryFile fp IO.ReadMode
127−
128−
129−enumHandleCount :: Integer -> Integer -> IO.Handle -> Enumerator B.ByteString IO a
130−enumHandleCount count buff h (Continue k)
131− | count <= 0 = continue k
132− | otherwise = do
133− let toRead = min count buff
134− bs <- liftIO $ B.hGet h $ fromInteger toRead
135− if B.null bs
136− then continue k
137− else k (Chunks [bs]) >>==
138− enumHandleCount (count - fromIntegral (B.length bs)) buff h
139−enumHandleCount _ _ _ step = returnI step
107+enumFilePart (FilePart offset count) fp =
108+ enumFileRange fp (Just offset) (Just count)
140109
141110responseLBS :: H.Status -> H.ResponseHeaders -> L.ByteString -> Response
142111responseLBS s h = ResponseBuilder s h . fromLazyByteString
wai.cabal+1−1
@@ -20,7 +20,7 @@ Library
2020 Build-Depends: base >= 3 && < 5
2121 , bytestring >= 0.9 && < 0.10
2222 , blaze-builder >= 0.2 && < 0.4
23− , enumerator >= 0.4.7 && < 0.5
23+ , enumerator >= 0.4.8 && < 0.5
2424 , network >= 2.2 && < 2.4
2525 , http-types >= 0.6 && < 0.7
2626 , text >= 0.5 && < 1.0