@@ -23,6 +23,8 @@ module Network.Wai
2323import qualified Data.ByteString.Lazy as L
2424import qualified Data.ByteString as B
2525import qualified Data.ByteString.Char8 as B8
26+import System.IO (withBinaryFile, IOMode (ReadMode), hIsEOF)
27+import Control.Monad (unless)
2628
2729-- | Please do not use the Show and Read instances for anything other than
2830-- debugging purposes. Instead, the 'methodFromBS' and 'methodToBS' provide a
@@ -121,8 +123,13 @@ class ResponseBodyClass a where
121123 sendLazyByteString a bs = mapM_ (sendByteString a) $ L.toChunks bs
122124
123125 sendFile :: a -> FilePath -> IO ()
124− -- FIXME do not use lazy I/O
125− sendFile a fp = L.readFile fp >>= sendLazyByteString a
126+ sendFile a fp = withBinaryFile fp ReadMode helper where
127+ helper h = do
128+ eof <- hIsEOF h
129+ unless eof $ do
130+ b <- B.hGet h 1024 -- FIXME determine better block size
131+ sendByteString a b
132+ helper h
126133instance ResponseBodyClass ResponseBody where
127134 sendByteString (ResponseBody a) = sendByteString a
128135 sendLazyByteString (ResponseBody a) = sendLazyByteString a