@@ -41,6 +41,12 @@ import Data.Typeable (Typeable)
4141import Control.Arrow (first)
4242import Numeric (showHex)
4343
44+import Data.Enumerator (($$))
45+import qualified Data.Enumerator as E
46+import Data.Enumerator.IO (enumFile)
47+import Control.Monad.IO.Class (liftIO)
48+import Blaze.ByteString.Builder.Enumerator (builderToByteString)
49+
4450run :: Port -> Application -> IO ()
4551run port = withSocketsDo .
4652 bracket
@@ -158,15 +164,17 @@ sendResponse httpversion h res = do
158164 mapM_ putHeader $ responseHeaders res
159165 B.hPut h $ B.pack "Transfer-Encoding: chunked\r\n\r\n"
160166 case responseBody res of
161− ResponseFile fp -> do
162− -- FIXME this is lazy I/O
163− lbs <- L.readFile fp
164− mapM_ myPut $ L.toChunks lbs
165− ResponseEnumerator (Enumerator enum) ->
166− enum (const myPut) h >> return ()
167+ ResponseFile fp -> E.run_ $ enumFile fp $$ myIter
168+ ResponseEnumerator enum ->
169+ enum $ E.joinI $ builderToByteString $$ myIter
167170 ResponseLBS lbs -> mapM_ myPut $ L.toChunks lbs
168171 B.hPut h $ B.pack "0\r\n\r\n"
169172 where
173+ myIter = do
174+ mbs <- E.head
175+ case mbs of
176+ Nothing -> return ()
177+ Just bs -> liftIO (myPut bs) >> myIter
170178 myPut bs = do
171179 B.hPut h $ B.pack $ showHex (B.length bs) " \r\n"
172180 B.hPut h bs