on master help sign in
remoteHost is SockAddr; including getFileSize
commit0567731e996be12eadfd4d94896311317bebdb4e authorMichael <michael@snoyman.com> dateThu 6 Jan 2011, 00:09 (16 years ago) parentbbd67c1
3 files changed+37−12
Network/Wai/Handler/Warp.hs+6−12 Network/Wai/Handler/Warp/Util.hs+26 warp.cabal+5
Network/Wai/Handler/Warp.hs+6−12
@@ -33,7 +33,7 @@ import Network
3333 ( listenOn, sClose, PortID(PortNumber), Socket
3434 , withSocketsDo)
3535import Network.Socket
36− ( accept
36+ ( accept, SockAddr
3737 )
3838import qualified Network.Socket.ByteString as Sock
3939import Control.Exception (bracket, finally, Exception, SomeException, catch)
@@ -72,16 +72,10 @@ type Port = Int
7272serveConnections :: Port -> Application -> Socket -> IO ()
7373serveConnections port app socket = do
7474 (conn, sa) <- accept socket
75− let remoteHost' = stripPort $ show sa -- FIXME
76− _ <- forkIO $ serveConnection port app conn remoteHost'
75+ _ <- forkIO $ serveConnection port app conn sa
7776 serveConnections port app socket
78− where
79− stripPort s =
80− case break (== ':') $ reverse s of
81− (_, ':' : rest) -> reverse rest
82− _ -> s
8377
84−serveConnection :: Port -> Application -> Socket -> String -> IO ()
78+serveConnection :: Port -> Application -> Socket -> SockAddr -> IO ()
8579serveConnection port app conn remoteHost' = do
8680 catch
8781 (finally
@@ -98,7 +92,7 @@ serveConnection port app conn remoteHost' = do
9892 keepAlive <- liftIO $ sendResponse env (httpVersion env) conn res
9993 when keepAlive serveConnection'
10094
101−parseRequest :: Port -> String -> E.Iteratee S.ByteString IO (E.Enumeratee ByteString ByteString IO a, Request)
95+parseRequest :: Port -> SockAddr -> E.Iteratee S.ByteString IO (E.Enumeratee ByteString ByteString IO a, Request)
10296parseRequest port remoteHost' = do
10397 headers' <- takeUntilBlank 0 id
10498 parseRequest' port headers' remoteHost'
@@ -156,7 +150,7 @@ instance Exception InvalidRequest
156150-- | Parse a set of header lines and body into a 'Request'.
157151parseRequest' :: Port
158152 -> [ByteString]
159− -> String
153+ -> SockAddr
160154 -> E.Iteratee S.ByteString IO (E.Enumeratee S.ByteString S.ByteString IO a, Request)
161155parseRequest' port lines' remoteHost' = do
162156 (firstLine, otherLines) <-
@@ -186,7 +180,7 @@ parseRequest' port lines' remoteHost' = do
186180 , requestHeaders = heads
187181 , isSecure = False
188182 , errorHandler = System.IO.hPutStr System.IO.stderr
189− , remoteHost = B.pack remoteHost'
183+ , remoteHost = remoteHost'
190184 })
191185
192186parseFirst :: ByteString
Network/Wai/Handler/Warp/Util.hsnew file+26
@@ -0,0 +1,26 @@
1+{-# LANGUAGE CPP #-}
2+module Network.Wai.Handler.Warp.Util
3+ ( getFileSize
4+ ) where
5+
6+
7+#if defined(mingw32_HOST_OS) || defined(__MINGW32__)
8+
9+import System.Win32.File
10+
11+getFileSize :: FilePath -> IO Integer
12+getFileSize path = do
13+ hnd <- createFile path
14+ gENERIC_READ fILE_SHARE_READ Nothing oPEN_EXISTING 0 Nothing
15+ size <- fmap bhfiSize $ getFileInformationByHandle hnd
16+ closeHandle hnd
17+ return $ fromIntegral size
18+
19+#else
20+
21+import System.Posix.Files
22+
23+getFileSize :: FilePath -> IO Integer
24+getFileSize = fmap (fromIntegral . fileSize) . getFileStatus
25+
26+#endif
warp.cabal+5
@@ -30,7 +30,12 @@ Library
3030 , network-bytestring >= 0.1.3 && < 0.1.4
3131 else
3232 build-depends: network >= 2.3 && < 2.4
33+ if os(windows)
34+ Build-Depends: Win32 >= 2.2 && < 2.3
35+ else
36+ Build-Depends: unix >= 2.4 && < 2.5
3337 Exposed-modules: Network.Wai.Handler.Warp
38+ Network.Wai.Handler.Warp.Util
3439 ghc-options: -Wall
3540 if !flag(timeout-protection)
3641 Cpp-options: -DNO_TIMEOUT_PROTECTION