on master help sign in
remoteHost is SockAddr; including getFileSize
0567731Michael
Network/Wai/Handler/Warp.hs+6−12
@@ -33,7 +33,7 @@ import Network
33 ( listenOn, sClose, PortID(PortNumber), Socket
33 ( listenOn, sClose, PortID(PortNumber), Socket
34 , withSocketsDo)
34 , withSocketsDo)
35import Network.Socket
35import Network.Socket
36− ( accept
36+ ( accept, SockAddr
37 )
37 )
38import qualified Network.Socket.ByteString as Sock
38import qualified Network.Socket.ByteString as Sock
39import Control.Exception (bracket, finally, Exception, SomeException, catch)
39import Control.Exception (bracket, finally, Exception, SomeException, catch)
@@ -72,16 +72,10 @@ type Port = Int
72serveConnections :: Port -> Application -> Socket -> IO ()
72serveConnections :: Port -> Application -> Socket -> IO ()
73serveConnections port app socket = do
73serveConnections port app socket = do
74 (conn, sa) <- accept socket
74 (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
77 serveConnections port app socket
76 serveConnections port app socket
78− where
79− stripPort s =
80− case break (== ':') $ reverse s of
81− (_, ':' : rest) -> reverse rest
82− _ -> s
83
77
84−serveConnection :: Port -> Application -> Socket -> String -> IO ()
78+serveConnection :: Port -> Application -> Socket -> SockAddr -> IO ()
85serveConnection port app conn remoteHost' = do
79serveConnection port app conn remoteHost' = do
86 catch
80 catch
87 (finally
81 (finally
@@ -98,7 +92,7 @@ serveConnection port app conn remoteHost' = do
98 keepAlive <- liftIO $ sendResponse env (httpVersion env) conn res
92 keepAlive <- liftIO $ sendResponse env (httpVersion env) conn res
99 when keepAlive serveConnection'
93 when keepAlive serveConnection'
100
94
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)
102parseRequest port remoteHost' = do
96parseRequest port remoteHost' = do
103 headers' <- takeUntilBlank 0 id
97 headers' <- takeUntilBlank 0 id
104 parseRequest' port headers' remoteHost'
98 parseRequest' port headers' remoteHost'
@@ -156,7 +150,7 @@ instance Exception InvalidRequest
156-- | Parse a set of header lines and body into a 'Request'.
150-- | Parse a set of header lines and body into a 'Request'.
157parseRequest' :: Port
151parseRequest' :: Port
158 -> [ByteString]
152 -> [ByteString]
159− -> String
153+ -> SockAddr
160 -> E.Iteratee S.ByteString IO (E.Enumeratee S.ByteString S.ByteString IO a, Request)
154 -> E.Iteratee S.ByteString IO (E.Enumeratee S.ByteString S.ByteString IO a, Request)
161parseRequest' port lines' remoteHost' = do
155parseRequest' port lines' remoteHost' = do
162 (firstLine, otherLines) <-
156 (firstLine, otherLines) <-
@@ -186,7 +180,7 @@ parseRequest' port lines' remoteHost' = do
186 , requestHeaders = heads
180 , requestHeaders = heads
187 , isSecure = False
181 , isSecure = False
188 , errorHandler = System.IO.hPutStr System.IO.stderr
182 , errorHandler = System.IO.hPutStr System.IO.stderr
189− , remoteHost = B.pack remoteHost'
183+ , remoteHost = remoteHost'
190 })
184 })
191
185
192parseFirst :: ByteString
186parseFirst :: 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
30 , network-bytestring >= 0.1.3 && < 0.1.4
30 , network-bytestring >= 0.1.3 && < 0.1.4
31 else
31 else
32 build-depends: network >= 2.3 && < 2.4
32 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
33 Exposed-modules: Network.Wai.Handler.Warp
37 Exposed-modules: Network.Wai.Handler.Warp
38+ Network.Wai.Handler.Warp.Util
34 ghc-options: -Wall
39 ghc-options: -Wall
35 if !flag(timeout-protection)
40 if !flag(timeout-protection)
36 Cpp-options: -DNO_TIMEOUT_PROTECTION
41 Cpp-options: -DNO_TIMEOUT_PROTECTION