@@ -18,6 +18,7 @@ module Network.Wai.Handler.SimpleServer
20import Network.Wai
20import Network.Wai
21+import Network.Wai.Handler.Helper
21import qualified System.IO
22import qualified System.IO
23import qualified Data.ByteString as BS
24import qualified Data.ByteString as BS
@@ -27,7 +28,7 @@ import Network
27 , withSocketsDo)
28 , withSocketsDo)
28import Control.Exception (bracket, finally, Exception)
29import Control.Exception (bracket, finally, Exception)
29import System.IO (Handle, hClose)
30import System.IO (Handle, hClose)
30−import Control.Concurrent
31+import Control.Concurrent (forkIO)
31import Control.Monad (unless)
32import Control.Monad (unless)
32import Data.Maybe (isJust, fromJust, fromMaybe)
33import Data.Maybe (isJust, fromJust, fromMaybe)
@@ -40,7 +41,6 @@ import qualified Web.Encodings.StringLike as SL
40import qualified Safe
41import qualified Safe
41import Network.Socket.SendFile
42import Network.Socket.SendFile
42import Control.Arrow (first)
43import Control.Arrow (first)
43−import Data.ByteString.Lazy.Internal (defaultChunkSize)
45run :: Port -> Application -> IO ()
45run :: Port -> Application -> IO ()
46run port = withSocketsDo .
46run port = withSocketsDo .
@@ -69,8 +69,8 @@ serveConnection port app conn remoteHost' =
70hParseRequest :: Port -> Handle -> String -> IO Request
70hParseRequest :: Port -> Handle -> String -> IO Request
71hParseRequest port conn remoteHost' = do
71hParseRequest port conn remoteHost' = do
72− responseHeaders' <- takeUntilBlank conn id
72+ headers' <- takeUntilBlank conn id
73− parseRequest port responseHeaders' conn remoteHost'
73+ parseRequest port headers' conn remoteHost'
75takeUntilBlank :: Handle
75takeUntilBlank :: Handle
76 -> ([BS.ByteString] -> [BS.ByteString])
76 -> ([BS.ByteString] -> [BS.ByteString])
@@ -95,7 +95,7 @@ data InvalidRequest =
95 deriving (Show, Typeable)
95 deriving (Show, Typeable)
96instance Exception InvalidRequest
96instance Exception InvalidRequest
98−-- | Parse a set of header lines and responseBody into a 'Request'.
98+-- | Parse a set of header lines and body into a 'Request'.
99parseRequest :: Port
99parseRequest :: Port
100 -> [BS.ByteString]
100 -> [BS.ByteString]
101 -> Handle
101 -> Handle
@@ -119,7 +119,6 @@ parseRequest port lines' handle remoteHost' = do
119 bs <- lookup ReqContentLength heads
119 bs <- lookup ReqContentLength heads
120 let str = SL.unpack bs
120 let str = SL.unpack bs
121 Safe.readMay str
121 Safe.readMay str
123 let (serverName', _) = SL.breakChar ':' host
122 let (serverName', _) = SL.breakChar ':' host
124 return $ Request
123 return $ Request
125 { requestMethod = method
124 { requestMethod = method
@@ -130,22 +129,11 @@ parseRequest port lines' handle remoteHost' = do
130 , serverPort = port
129 , serverPort = port
131 , requestHeaders = heads
130 , requestHeaders = heads
132 , urlScheme = HTTP
131 , urlScheme = HTTP
133− , requestBody = requestBodyHandle handle mlen
132+ , requestBody = Source len $ requestBodyHandle handle
134 , errorHandler = System.IO.hPutStr System.IO.stderr
133 , errorHandler = System.IO.hPutStr System.IO.stderr
135 , remoteHost = B8.pack remoteHost'
134 , remoteHost = B8.pack remoteHost'
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)
142− bs <- BS.hGet h $ min len defaultChunkSize
143− let newLen = len - BS.length bs
146− Left a' -> return (newLen, Left a')
147− Right a' -> helper a' newLen
149parseFirst :: (StringLike s, MonadFailure InvalidRequest m) =>
137parseFirst :: (StringLike s, MonadFailure InvalidRequest m) =>
151 -> m (s, s, s)
139 -> m (s, s, s)
@@ -169,7 +157,7 @@ sendResponse h res = do
169 BS.hPut h $ SL.pack "\r\n"
157 BS.hPut h $ SL.pack "\r\n"
170 case responseBody res of
158 case responseBody res of
171 Left fp -> unsafeSendFile h fp
159 Left fp -> unsafeSendFile h fp
172− Right enum -> enum myPut h >> return ()
160+ Right (Enumerator enum) -> enum myPut h >> return ()
174 myPut _ bs = do
162 myPut _ bs = do
175 BS.hPut h bs
163 BS.hPut h bs