on master help sign in
Chunked transfer encoding for simple server
commit71b1dea733ca166add5594be84e2ce59adcffae6 authorMichael Snoyman <michael@snoyman.com> dateFri 24 Sep 2010, 10:37 (16 years ago) parent1807806
2 files changed+14−4
Network/Wai/Handler/SimpleServer.hs+13−3 wai-extra.cabal+1−1
Network/Wai/Handler/SimpleServer.hs+13−3
@@ -38,6 +38,7 @@ import Data.Typeable (Typeable)
3838
3939import Network.Socket.SendFile
4040import Control.Arrow (first)
41+import Numeric (showHex)
4142
4243run :: Port -> Application -> IO ()
4344run port = withSocketsDo .
@@ -154,14 +155,23 @@ sendResponse httpversion h res = do
154155 B.hPut h $ statusMessage $ status res
155156 B.hPut h $ B.pack "\r\n"
156157 mapM_ putHeader $ responseHeaders res
158+ case responseBody res of
159+ ResponseFile _ -> return ()
160+ _ -> B.hPut h $ B.pack "Transfer-Encoding: chunked\r\n"
157161 B.hPut h $ B.pack "\r\n"
158162 case responseBody res of
159163 ResponseFile fp -> unsafeSendFile h fp
160− ResponseEnumerator (Enumerator enum) -> enum myPut h >> return ()
161− ResponseLBS lbs -> L.hPut h lbs
164+ ResponseEnumerator (Enumerator enum) ->
165+ enum (const myPut) h >> return ()
166+ ResponseLBS lbs -> mapM_ myPut $ L.toChunks lbs
167+ case responseBody res of
168+ ResponseFile _ -> return ()
169+ _ -> B.hPut h $ B.pack "0\r\n"
162170 where
163− myPut _ bs = do
171+ myPut bs = do
172+ B.hPut h $ B.pack $ showHex (B.length bs) "\r\n"
164173 B.hPut h bs
174+ B.hPut h $ B.pack "\r\n"
165175 return (Right h)
166176 putHeader (x, y) = do
167177 B.hPut h $ ciOriginal x
wai-extra.cabal+1−1
@@ -1,5 +1,5 @@
11Name: wai-extra
2−Version: 0.2.2.2
2+Version: 0.2.2.3
33Synopsis: Provides some basic WAI handlers and middleware.
44Description: The goal here is to provide common features without many dependencies.
55License: BSD3