@@ -18,6 +18,7 @@ module Network.Wai.Handler.SimpleServer
1818 ) where
1919
2020import Network.Wai
21+import Network.Wai.Handler.Helper
2122import qualified System.IO
2223
2324import qualified Data.ByteString as BS
@@ -27,7 +28,7 @@ import Network
2728 , withSocketsDo)
2829import Control.Exception (bracket, finally, Exception)
2930import System.IO (Handle, hClose)
30−import Control.Concurrent
31+import Control.Concurrent (forkIO)
3132import Control.Monad (unless)
3233import Data.Maybe (isJust, fromJust, fromMaybe)
3334
@@ -40,7 +41,6 @@ import qualified Web.Encodings.StringLike as SL
4041import qualified Safe
4142import Network.Socket.SendFile
4243import Control.Arrow (first)
43−import Data.ByteString.Lazy.Internal (defaultChunkSize)
4444
4545run :: Port -> Application -> IO ()
4646run port = withSocketsDo .
@@ -69,8 +69,8 @@ serveConnection port app conn remoteHost' =
6969
7070hParseRequest :: Port -> Handle -> String -> IO Request
7171hParseRequest port conn remoteHost' = do
72− responseHeaders' <- takeUntilBlank conn id
73− parseRequest port responseHeaders' conn remoteHost'
72+ headers' <- takeUntilBlank conn id
73+ parseRequest port headers' conn remoteHost'
7474
7575takeUntilBlank :: Handle
7676 -> ([BS.ByteString] -> [BS.ByteString])
@@ -95,7 +95,7 @@ data InvalidRequest =
9595 deriving (Show, Typeable)
9696instance Exception InvalidRequest
9797
98−-- | Parse a set of header lines and responseBody into a 'Request'.
98+-- | Parse a set of header lines and body into a 'Request'.
9999parseRequest :: Port
100100 -> [BS.ByteString]
101101 -> Handle
@@ -119,7 +119,6 @@ parseRequest port lines' handle remoteHost' = do
119119 bs <- lookup ReqContentLength heads
120120 let str = SL.unpack bs
121121 Safe.readMay str
122− mlen <- newMVar len
123122 let (serverName', _) = SL.breakChar ':' host
124123 return $ Request
125124 { requestMethod = method
@@ -130,22 +129,11 @@ parseRequest port lines' handle remoteHost' = do
130129 , serverPort = port
131130 , requestHeaders = heads
132131 , urlScheme = HTTP
133− , requestBody = requestBodyHandle handle mlen
132+ , requestBody = Source len $ requestBodyHandle handle
134133 , errorHandler = System.IO.hPutStr System.IO.stderr
135134 , remoteHost = B8.pack remoteHost'
136135 }
137136
138−requestBodyHandle :: Handle -> MVar Int -> Enumerator a
139−requestBodyHandle h mlen iter accum = modifyMVar mlen (helper accum) where
140− helper a 0 = return (0, Right a)
141− helper a len = do
142− bs <- BS.hGet h $ min len defaultChunkSize
143− let newLen = len - BS.length bs
144− ea' <- iter a bs
145− case ea' of
146− Left a' -> return (newLen, Left a')
147− Right a' -> helper a' newLen
148−
149137parseFirst :: (StringLike s, MonadFailure InvalidRequest m) =>
150138 s
151139 -> m (s, s, s)
@@ -169,7 +157,7 @@ sendResponse h res = do
169157 BS.hPut h $ SL.pack "\r\n"
170158 case responseBody res of
171159 Left fp -> unsafeSendFile h fp
172− Right enum -> enum myPut h >> return ()
160+ Right (Enumerator enum) -> enum myPut h >> return ()
173161 where
174162 myPut _ bs = do
175163 BS.hPut h bs