@@ -41,6 +41,12 @@ import Data.Typeable (Typeable)
41import Control.Arrow (first)
41import Control.Arrow (first)
42import Numeric (showHex)
42import Numeric (showHex)
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)
44run :: Port -> Application -> IO ()
50run :: Port -> Application -> IO ()
45run port = withSocketsDo .
51run port = withSocketsDo .
@@ -158,15 +164,17 @@ sendResponse httpversion h res = do
158 mapM_ putHeader $ responseHeaders res
164 mapM_ putHeader $ responseHeaders res
159 B.hPut h $ B.pack "Transfer-Encoding: chunked\r\n\r\n"
165 B.hPut h $ B.pack "Transfer-Encoding: chunked\r\n\r\n"
160 case responseBody res of
166 case responseBody res of
161− ResponseFile fp -> do
167+ ResponseFile fp -> E.run_ $ enumFile fp $$ myIter
162− -- FIXME this is lazy I/O
163− lbs <- L.readFile fp
164− mapM_ myPut $ L.toChunks lbs
165− ResponseEnumerator (Enumerator enum) ->
168+ ResponseEnumerator enum ->
166− enum (const myPut) h >> return ()
169+ enum $ E.joinI $ builderToByteString $$ myIter
167 ResponseLBS lbs -> mapM_ myPut $ L.toChunks lbs
170 ResponseLBS lbs -> mapM_ myPut $ L.toChunks lbs
168 B.hPut h $ B.pack "0\r\n\r\n"
171 B.hPut h $ B.pack "0\r\n\r\n"
176+ Nothing -> return ()
177+ Just bs -> liftIO (myPut bs) >> myIter
170 myPut bs = do
178 myPut bs = do
171 B.hPut h $ B.pack $ showHex (B.length bs) " \r\n"
179 B.hPut h $ B.pack $ showHex (B.length bs) " \r\n"
172 B.hPut h bs
180 B.hPut h bs