@@ -3,49 +3,53 @@
33{-# LANGUAGE OverloadedStrings #-}
44{-# LANGUAGE PackageImports #-}
55{-# LANGUAGE PatternGuards #-}
6+{-# LANGUAGE RecordWildCards #-}
67{-# LANGUAGE ScopedTypeVariables #-}
78{-# LANGUAGE TupleSections #-}
9+{-# LANGUAGE ViewPatterns #-}
810{-# OPTIONS_GHC -fno-warn-deprecations #-}
911
1012module Network.Wai.Handler.Warp.Run where
1113
12−#if __GLASGOW_HASKELL__ < 709
13−import Control.Applicative ((<$>))
14−#endif
14+import "iproute" Data.IP (toHostAddress, toHostAddress6)
1515import Control.Arrow (first)
1616import qualified Control.Concurrent as Conc (yield)
1717import Control.Exception as E
18−import Control.Monad (when, unless, void)
19−import Data.ByteString (ByteString)
2018import qualified Data.ByteString as S
2119import Data.Char (chr)
22−import "iproute" Data.IP (toHostAddress, toHostAddress6)
2320import Data.IORef (IORef, newIORef, readIORef, writeIORef)
2421import Data.Streaming.Network (bindPortTCP)
2522import Foreign.C.Error (Errno(..), eCONNABORTED)
2623import GHC.IO.Exception (IOException(..))
27−import Network (sClose, Socket)
28−import Network.Socket (accept, withSocketsDo, SockAddr(SockAddrInet, SockAddrInet6), setSocketOption, SocketOption(..))
24+import qualified Network.HTTP2 as H2
25+import Network.Socket (Socket, close, accept, withSocketsDo, SockAddr(SockAddrInet, SockAddrInet6), setSocketOption, SocketOption(..))
26+#if MIN_VERSION_network(3,1,1)
27+import Network.Socket (gracefulClose)
28+#endif
2929import qualified Network.Socket.ByteString as Sock
3030import Network.Wai
31+import Network.Wai.Internal (ResponseReceived (ResponseReceived))
32+import System.Environment (lookupEnv)
33+import qualified System.TimeManager as T
34+import System.Timeout (timeout)
35+
3136import Network.Wai.Handler.Warp.Buffer
3237import Network.Wai.Handler.Warp.Counter
3338import qualified Network.Wai.Handler.Warp.Date as D
3439import qualified Network.Wai.Handler.Warp.FdCache as F
3540import qualified Network.Wai.Handler.Warp.FileInfoCache as I
36−import Network.Wai.Handler.Warp.HTTP2 (http2, isHTTP2)
41+import Network.Wai.Handler.Warp.HTTP2 (http2)
42+import Network.Wai.Handler.Warp.HTTP2.Types (isHTTP2)
3743import Network.Wai.Handler.Warp.Header
44+import Network.Wai.Handler.Warp.Imports hiding (readInt)
3845import Network.Wai.Handler.Warp.ReadInt
3946import Network.Wai.Handler.Warp.Recv
4047import Network.Wai.Handler.Warp.Request
4148import Network.Wai.Handler.Warp.Response
4249import Network.Wai.Handler.Warp.SendFile
4350import Network.Wai.Handler.Warp.Settings
44−import qualified Network.Wai.Handler.Warp.Timeout as T
4551import Network.Wai.Handler.Warp.Types
46−import Network.Wai.Internal (ResponseReceived (ResponseReceived))
47−import System.Environment (getEnvironment)
48−import System.Timeout (timeout)
52+
4953
5054#if WINDOWS
5155import Network.Wai.Handler.Warp.Windows
@@ -54,27 +58,40 @@ import Network.Socket (fdSocket)
5458#endif
5559
5660-- | Creating 'Connection' for plain HTTP based on a given socket.
57−socketConnection :: Socket -> IO Connection
58−socketConnection s = do
61+socketConnection :: Settings -> Socket -> IO Connection
62+#if MIN_VERSION_network(3,1,1)
63+socketConnection set s = do
64+#else
65+socketConnection _ s = do
66+#endif
5967 bufferPool <- newBufferPool
6068 writeBuf <- allocateBuffer bufferSize
6169 let sendall = Sock.sendAll s
70+ isH2 <- newIORef False -- HTTP/1.x
6271 return Connection {
6372 connSendMany = Sock.sendMany s
6473 , connSendAll = sendall
6574 , connSendFile = sendFile s writeBuf bufferSize sendall
66− , connClose = sClose s >> freeBuffer writeBuf
75+#if MIN_VERSION_network(3,1,1)
76+ , connClose = do
77+ h2 <- readIORef isH2
78+ let tm = if h2 then settingsGracefulCloseTimeout2 set
79+ else settingsGracefulCloseTimeout1 set
80+ if tm == 0 then
81+ close s
82+ else
83+ gracefulClose s tm `E.catch` \(E.SomeException _) -> return ()
84+#else
85+ , connClose = close s
86+#endif
87+ , connFree = freeBuffer writeBuf
6788 , connRecv = receive s bufferPool
6889 , connRecvBuf = receiveBuf s
6990 , connWriteBuffer = writeBuf
7091 , connBufferSize = bufferSize
92+ , connHTTP2 = isH2
7193 }
7294
73−#if __GLASGOW_HASKELL__ < 702
74−allowInterrupt :: IO ()
75−allowInterrupt = unblock $ return ()
76−#endif
77−
7895-- | Run an 'Application' on the given port.
7996-- This calls 'runSettings' with 'defaultSettings'.
8097run :: Port -> Application -> IO ()
@@ -87,7 +104,7 @@ run p = runSettings defaultSettings { settingsPort = p }
87104-- Since 3.0.9
88105runEnv :: Port -> Application -> IO ()
89106runEnv p app = do
90− mp <- lookup "PORT" <$> getEnvironment
107+ mp <- lookupEnv "PORT"
91108
92109 maybe (run p app) runReadPort mp
93110
@@ -104,7 +121,7 @@ runSettings :: Settings -> Application -> IO ()
104121runSettings set app = withSocketsDo $
105122 bracket
106123 (bindPortTCP (settingsPort set) (settingsHost set))
107− sClose
124+ close
108125 (\socket -> do
109126 setSocketCloseOnExec socket
110127 runSettingsSocket set socket app)
@@ -134,10 +151,10 @@ runSettingsSocket set socket app = do
134151 setSocketCloseOnExec s
135152 -- NoDelay causes an error for AF_UNIX.
136153 setSocketOption s NoDelay 1 `E.catch` \(E.SomeException _) -> return ()
137− conn <- socketConnection s
154+ conn <- socketConnection set s
138155 return (conn, sa)
139156
140− closeListenSocket = sClose socket
157+ closeListenSocket = close socket
141158
142159-- | The connection setup action would be expensive. A good example
143160-- is initialization of TLS.
@@ -175,15 +192,15 @@ runSettingsConnectionMakerSecure :: Settings -> IO (IO (Connection, Transport),
175192runSettingsConnectionMakerSecure set getConnMaker app = do
176193 settingsBeforeMainLoop set
177194 counter <- newCounter
178− withII0 $ acceptConnection set getConnMaker app counter
195+ withII $ acceptConnection set getConnMaker app counter
179196 where
180− withII0 action =
197+ withII action =
181198 withTimeoutManager $ \tm ->
182199 D.withDateCache $ \dc ->
183200 F.withFdCache fdCacheDurationInSeconds $ \fdc ->
184201 I.withFileInfoCache fdFileInfoDurationInSeconds $ \fic -> do
185− let ii0 = InternalInfo0 tm dc fdc fic
186− action ii0
202+ let ii = InternalInfo tm dc fdc fic
203+ action ii
187204
188205 !fdCacheDurationInSeconds = settingsFdCacheDuration set * 1000000
189206 !fdFileInfoDurationInSeconds = settingsFileInfoCacheDuration set * 1000000
@@ -212,13 +229,18 @@ acceptConnection :: Settings
212229 -> IO (IO (Connection, Transport), SockAddr)
213230 -> Application
214231 -> Counter
215− -> InternalInfo0
232+ -> InternalInfo
216233 -> IO ()
217−acceptConnection set getConnMaker app counter ii0 = do
234+acceptConnection set getConnMaker app counter ii = do
218235 -- First mask all exceptions in acceptLoop. This is necessary to
219236 -- ensure that no async exception is throw between the call to
220237 -- acceptNewConnection and the registering of connClose.
238+ --
239+ -- acceptLoop can be broken by closing the listing socket.
221240 void $ mask_ acceptLoop
241+ -- In some cases, we want to stop Warp here without graceful shutdown.
242+ -- So, async exceptions are allowed here.
243+ -- That's why `finally` is not used.
222244 gracefulShutdown set counter
223245 where
224246 acceptLoop = do
@@ -236,7 +258,7 @@ acceptConnection set getConnMaker app counter ii0 = do
236258 case mx of
237259 Nothing -> return ()
238260 Just (mkConn, addr) -> do
239− fork set mkConn addr app counter ii0
261+ fork set mkConn addr app counter ii
240262 acceptLoop
241263
242264 acceptNewConnection = do
@@ -259,54 +281,55 @@ fork :: Settings
259281 -> SockAddr
260282 -> Application
261283 -> Counter
262− -> InternalInfo0
284+ -> InternalInfo
263285 -> IO ()
264−fork set mkConn addr app counter ii0 = settingsFork set $ \ unmask ->
265− -- Run the connection maker to get a new connection, and ensure
266− -- that the connection is closed. If the mkConn call throws an
267− -- exception, we will leak the connection. If the mkConn call is
268− -- vulnerable to attacks (e.g., Slowloris), we do nothing to
269− -- protect the server. It is therefore vital that mkConn is well
270− -- vetted.
271− --
272− -- We grab the connection before registering timeouts since the
273− -- timeouts will be useless during connection creation, due to the
274− -- fact that async exceptions are still masked.
275− bracket mkConn closeConn $ \(conn, transport) ->
286+fork set mkConn addr app counter ii = settingsFork set $ \unmask ->
287+ -- Call the user-supplied on exception code if any
288+ -- exceptions are thrown.
289+ handle (settingsOnException set Nothing) $
290+ -- Run the connection maker to get a new connection, and ensure
291+ -- that the connection is closed. If the mkConn call throws an
292+ -- exception, we will leak the connection. If the mkConn call is
293+ -- vulnerable to attacks (e.g., Slowloris), we do nothing to
294+ -- protect the server. It is therefore vital that mkConn is well
295+ -- vetted.
296+ --
297+ -- We grab the connection before registering timeouts since the
298+ -- timeouts will be useless during connection creation, due to the
299+ -- fact that async exceptions are still masked.
300+ bracket mkConn cleanUp (serve unmask)
301+ where
302+ cleanUp (conn, _) = connClose conn `finally` connFree conn
276303
277304 -- We need to register a timeout handler for this thread, and
278305 -- cancel that handler as soon as we exit.
279− bracket (T.registerKillThread (timeoutManager0 ii0)) T.cancel $ \th ->
280−
281− let ii1 = toInternalInfo1 ii0 th
282− -- We now have fully registered a connection close handler
283− -- in the case of all exceptions, so it is safe to one
284− -- again allow async exceptions.
285− in unmask .
286− -- Call the user-supplied on exception code if any
287− -- exceptions are thrown.
288− handle (settingsOnException set Nothing) .
289−
290− -- Call the user-supplied code for connection open and close events
291− bracket (onOpen addr) (onClose addr) $ \goingon ->
292−
293− -- Actually serve this connection.
294− -- bracket with closeConn above ensures the connection is closed.
295− when goingon $ serveConnection conn ii1 addr transport set app
296− where
297− closeConn (conn, _transport) = connClose conn
306+ serve unmask (conn, transport) = bracket register cancel $ \th -> do
307+ -- We now have fully registered a connection close handler in
308+ -- the case of all exceptions, so it is safe to once again
309+ -- allow async exceptions.
310+ unmask .
311+ -- Call the user-supplied code for connection open and
312+ -- close events
313+ bracket (onOpen addr) (onClose addr) $ \goingon ->
314+ -- Actually serve this connection. bracket with closeConn
315+ -- above ensures the connection is closed.
316+ when goingon $ serveConnection conn ii th addr transport set app
317+ where
318+ register = T.registerKillThread (timeoutManager ii) (connClose conn)
319+ cancel = T.cancel
298320
299321 onOpen adr = increase counter >> settingsOnOpen set adr
300322 onClose adr _ = decrease counter >> settingsOnClose set adr
301323
302324serveConnection :: Connection
303− -> InternalInfo1
325+ -> InternalInfo
326+ -> T.Handle
304327 -> SockAddr
305328 -> Transport
306329 -> Settings
307330 -> Application
308331 -> IO ()
309−serveConnection conn ii1 origAddr transport settings app = do
332+serveConnection conn ii th origAddr transport settings app = do
310333 -- fixme: Upgrading to HTTP/2 should be supported.
311334 (h2,bs) <- if isHTTP2 transport then
312335 return (True, "")
@@ -316,19 +339,39 @@ serveConnection conn ii1 origAddr transport settings app = do
316339 return (True, bs0)
317340 else
318341 return (False, bs0)
342+ istatus <- newIORef False
319343 if settingsHTTP2Enabled settings && h2 then do
320− recvN <- makeReceiveN bs (connRecv conn) (connRecvBuf conn)
344+ rawRecvN <- makeReceiveN bs (connRecv conn) (connRecvBuf conn)
345+ -- This thread becomes the sender in http2 library.
346+ -- In the case of event source, one request comes and one
347+ -- worker gets busy. But it is likely that the receiver does
348+ -- not receive any data at all while the sender is sending
349+ -- output data from the worker. It's not good enough to tickle
350+ -- the time handler in the receiver only. So, we should tickle
351+ -- the time handler in both the receiver and the sender.
352+ let recvN = wrappedRecvN th istatus (settingsSlowlorisSize settings) rawRecvN
353+ sendBS x = connSendAll conn x >> T.tickle th
321354 -- fixme: origAddr
322− http2 conn ii1 origAddr transport settings recvN app
355+ checkTLS
356+ setConnHTTP2 conn True
357+ http2 conn transport ii origAddr settings recvN sendBS app
323358 else do
324− istatus <- newIORef False
325359 src <- mkSource (wrappedRecv conn th istatus (settingsSlowlorisSize settings))
326360 writeIORef istatus True
327361 leftoverSource src bs
328362 addr <- getProxyProtocolAddr src
329− http1 addr istatus src `E.catch` \e -> do
330− sendErrorResponse addr istatus e
331− throwIO (e :: SomeException)
363+ http1 True addr istatus src `E.catch` \e ->
364+ case () of
365+ ()
366+ -- See comment below referencing
367+ -- https://github.com/yesodweb/wai/issues/618
368+ | Just NoKeepAliveRequest <- fromException e -> return ()
369+ -- No valid request
370+ | Just (BadFirstLine _) <- fromException e -> return ()
371+ | otherwise -> do
372+ _ <- sendErrorResponse (dummyreq addr) istatus e
373+ throwIO e
374+
332375 where
333376 getProxyProtocolAddr src =
334377 case settingsProxyProtocol settings of
@@ -370,36 +413,40 @@ serveConnection conn ii1 origAddr transport settings app = do
370413
371414 decodeAscii = map (chr . fromEnum) . S.unpack
372415
373− th = threadHandle1 ii1
374−
375416 shouldSendErrorResponse se
376417 | Just ConnectionClosedByPeer <- fromException se = False
377418 | otherwise = True
378419
379− sendErrorResponse addr istatus e = do
420+ sendErrorResponse req istatus e = do
380421 status <- readIORef istatus
381− when (shouldSendErrorResponse e && status) $ do
382− let ii = toInternalInfo ii1 0 -- dummy
383− dreq = dummyreq addr
384− void $ sendResponse settings conn ii dreq defaultIndexRequestHeader (return S.empty) (errorResponse e)
422+ if shouldSendErrorResponse e && status
423+ then do
424+ sendResponse settings conn ii th req defaultIndexRequestHeader (return S.empty) (errorResponse e)
425+ else return False
385426
386427 dummyreq addr = defaultRequest { remoteHost = addr }
387428
388429 errorResponse e = settingsOnExceptionResponse settings e
389430
390− http1 addr istatus src = do
391− (req', mremainingRef, idxhdr, nextBodyFlush, ii) <- recvRequest settings conn ii1 addr src
392− let req = req' { isSecure = isTransportSecure transport }
393− keepAlive <- processRequest istatus src req mremainingRef idxhdr nextBodyFlush ii
431+ http1 firstRequest addr istatus src = do
432+ (req, mremainingRef, idxhdr, nextBodyFlush) <- recvRequest firstRequest settings conn ii th addr src transport
433+ keepAlive <- processRequest istatus src req mremainingRef idxhdr nextBodyFlush
394434 `E.catch` \e -> do
395− -- Call the user-supplied exception handlers, passing the request.
396− sendErrorResponse addr istatus e
397435 settingsOnException settings (Just req) e
398436 -- Don't throw the error again to prevent calling settingsOnException twice.
399437 return False
400− when keepAlive $ http1 addr istatus src
401438
402− processRequest istatus src req mremainingRef idxhdr nextBodyFlush ii = do
439+ -- When doing a keep-alive connection, the other side may just
440+ -- close the connection. We don't want to treat that as an
441+ -- exceptional situation, so we pass in False to http1 (which
442+ -- in turn passes in False to recvRequest), indicating that
443+ -- this is not the first request. If, when trying to read the
444+ -- request headers, no data is available, recvRequest will
445+ -- throw a NoKeepAliveRequest exception, which we catch here
446+ -- and ignore. See: https://github.com/yesodweb/wai/issues/618
447+ when keepAlive $ http1 False addr istatus src
448+
449+ processRequest istatus src req mremainingRef idxhdr nextBodyFlush = do
403450 -- Let the application run for as long as it wants
404451 T.pause th
405452
@@ -407,30 +454,38 @@ serveConnection conn ii1 origAddr transport settings app = do
407454 -- creating the request, we need to make sure that we don't get
408455 -- an async exception before calling the ResponseSource.
409456 keepAliveRef <- newIORef $ error "keepAliveRef not filled"
410− _ <- app req $ \res -> do
457+ r <- E.try $ app req $ \res -> do
411458 T.resume th
412459 -- FIXME consider forcing evaluation of the res here to
413460 -- send more meaningful error messages to the user.
414461 -- However, it may affect performance.
415462 writeIORef istatus False
416− keepAlive <- sendResponse settings conn ii req idxhdr (readSource src) res
463+ keepAlive <- sendResponse settings conn ii th req idxhdr (readSource src) res
417464 writeIORef keepAliveRef keepAlive
418465 return ResponseReceived
466+ case r of
467+ Right ResponseReceived -> return ()
468+ Left e@(SomeException _)
469+ | Just (ExceptionInsideResponseBody e') <- fromException e -> throwIO e'
470+ | otherwise -> do
471+ keepAlive <- sendErrorResponse req istatus e
472+ settingsOnException settings (Just req) e
473+ writeIORef keepAliveRef keepAlive
474+
419475 keepAlive <- readIORef keepAliveRef
420476
421477 -- We just send a Response and it takes a time to
422478 -- receive a Request again. If we immediately call recv,
423− -- it is likely to fail and the IO manager works.
424− -- It is very costly. So, we yield to another Haskell
479+ -- it is likely to fail and cause the IO manager to do some work.
480+ -- It is very costly, so we yield to another Haskell
425481 -- thread hoping that the next Request will arrive
426482 -- when this Haskell thread will be re-scheduled.
427483 -- This improves performance at least when
428484 -- the number of cores is small.
429485 Conc.yield
430486
431− if not keepAlive then
432− return False
433− else
487+ if keepAlive
488+ then
434489 -- If there is an unknown or large amount of data to still be read
435490 -- from the request body, simple drop this connection instead of
436491 -- reading it all in to satisfy a keep-alive request.
@@ -456,6 +511,21 @@ serveConnection conn ii1 origAddr transport settings app = do
456511 else
457512 return False
458513 Nothing -> tryKeepAlive
514+ else
515+ return False
516+
517+ checkTLS = case transport of
518+ TCP -> return () -- direct
519+ tls -> unless (tls12orLater tls) $ goaway conn H2.InadequateSecurity "Weak TLS"
520+ tls12orLater tls = tlsMajorVersion tls == 3 && tlsMinorVersion tls >= 3
521+
522+-- connClose must not be called here since Run:fork calls it
523+goaway :: Connection -> H2.ErrorCodeId -> ByteString -> IO ()
524+goaway Connection{..} etype debugmsg = connSendAll bytestream
525+ where
526+ einfo = H2.encodeInfo id 0
527+ frame = H2.GoAwayFrame 0 etype debugmsg
528+ bytestream = H2.encodeFrame einfo frame
459529
460530flushEntireBody :: IO ByteString -> IO ()
461531flushEntireBody src =
@@ -488,12 +558,35 @@ wrappedRecv Connection { connRecv = recv } th istatus slowlorisSize = do
488558 when (S.length bs >= slowlorisSize) $ T.tickle th
489559 return bs
490560
561+wrappedRecvN :: T.Handle -> IORef Bool -> Int -> (BufSize -> IO ByteString) -> (BufSize -> IO ByteString)
562+wrappedRecvN th istatus slowlorisSize readN bufsize = do
563+ bs <- readN bufsize
564+ unless (S.null bs) $ do
565+ writeIORef istatus True
566+ -- TODO: think about the slowloris protection in HTTP2: current code
567+ -- might open a slow-loris attack vector. Rather than timing we should
568+ -- consider limiting the per-client connections assuming that in HTTP2
569+ -- we should allow only few connections per host (real-world
570+ -- deployments with large NATs may be trickier).
571+ when (S.length bs >= slowlorisSize || bufsize <= slowlorisSize) $ T.tickle th
572+ return bs
573+
574+-- | Set flag FileCloseOnExec flag on a socket (on Unix)
575+--
491576-- Copied from: https://github.com/mzero/plush/blob/master/src/Plush/Server/Warp.hs
577+--
578+-- @since 3.2.17
492579setSocketCloseOnExec :: Socket -> IO ()
493580#if WINDOWS
494581setSocketCloseOnExec _ = return ()
495582#else
496−setSocketCloseOnExec socket = F.setFileCloseOnExec $ fromIntegral $ fdSocket socket
583+setSocketCloseOnExec socket = do
584+#if MIN_VERSION_network(3,0,0)
585+ fd <- fdSocket socket
586+#else
587+ let fd = fdSocket socket
588+#endif
589+ F.setFileCloseOnExec $ fromIntegral fd
497590#endif
498591
499592gracefulShutdown :: Settings -> Counter -> IO ()