@@ -3,50 +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 (Socket)
28−import Network.Socket (close, 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
38−import Network.Wai.Handler.Warp.IORef
44+import Network.Wai.Handler.Warp.Imports hiding (readInt)
3945import Network.Wai.Handler.Warp.ReadInt
4046import Network.Wai.Handler.Warp.Recv
4147import Network.Wai.Handler.Warp.Request
4248import Network.Wai.Handler.Warp.Response
4349import Network.Wai.Handler.Warp.SendFile
4450import Network.Wai.Handler.Warp.Settings
45−import qualified Network.Wai.Handler.Warp.Timeout as T
4651import Network.Wai.Handler.Warp.Types
47−import Network.Wai.Internal (ResponseReceived (ResponseReceived))
48−import System.Environment (getEnvironment)
49−import System.Timeout (timeout)
52+
5053
5154#if WINDOWS
5255import Network.Wai.Handler.Warp.Windows
@@ -55,28 +58,40 @@ import Network.Socket (fdSocket)
5558#endif
5659
5760-- | Creating 'Connection' for plain HTTP based on a given socket.
58−socketConnection :: Socket -> IO Connection
59−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
6067 bufferPool <- newBufferPool
6168 writeBuf <- allocateBuffer bufferSize
6269 let sendall = Sock.sendAll s
70+ isH2 <- newIORef False -- HTTP/1.x
6371 return Connection {
6472 connSendMany = Sock.sendMany s
6573 , connSendAll = sendall
6674 , connSendFile = sendFile s writeBuf bufferSize sendall
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
6785 , connClose = close s
86+#endif
6887 , connFree = freeBuffer writeBuf
6988 , connRecv = receive s bufferPool
7089 , connRecvBuf = receiveBuf s
7190 , connWriteBuffer = writeBuf
7291 , connBufferSize = bufferSize
92+ , connHTTP2 = isH2
7393 }
7494
75−#if __GLASGOW_HASKELL__ < 702
76−allowInterrupt :: IO ()
77−allowInterrupt = unblock $ return ()
78−#endif
79−
8095-- | Run an 'Application' on the given port.
8196-- This calls 'runSettings' with 'defaultSettings'.
8297run :: Port -> Application -> IO ()
@@ -89,7 +104,7 @@ run p = runSettings defaultSettings { settingsPort = p }
89104-- Since 3.0.9
90105runEnv :: Port -> Application -> IO ()
91106runEnv p app = do
92− mp <- lookup "PORT" <$> getEnvironment
107+ mp <- lookupEnv "PORT"
93108
94109 maybe (run p app) runReadPort mp
95110
@@ -136,7 +151,7 @@ runSettingsSocket set socket app = do
136151 setSocketCloseOnExec s
137152 -- NoDelay causes an error for AF_UNIX.
138153 setSocketOption s NoDelay 1 `E.catch` \(E.SomeException _) -> return ()
139− conn <- socketConnection s
154+ conn <- socketConnection set s
140155 return (conn, sa)
141156
142157 closeListenSocket = close socket
@@ -177,15 +192,15 @@ runSettingsConnectionMakerSecure :: Settings -> IO (IO (Connection, Transport),
177192runSettingsConnectionMakerSecure set getConnMaker app = do
178193 settingsBeforeMainLoop set
179194 counter <- newCounter
180− withII0 $ acceptConnection set getConnMaker app counter
195+ withII $ acceptConnection set getConnMaker app counter
181196 where
182− withII0 action =
197+ withII action =
183198 withTimeoutManager $ \tm ->
184199 D.withDateCache $ \dc ->
185200 F.withFdCache fdCacheDurationInSeconds $ \fdc ->
186201 I.withFileInfoCache fdFileInfoDurationInSeconds $ \fic -> do
187− let ii0 = InternalInfo0 tm dc fdc fic
188− action ii0
202+ let ii = InternalInfo tm dc fdc fic
203+ action ii
189204
190205 !fdCacheDurationInSeconds = settingsFdCacheDuration set * 1000000
191206 !fdFileInfoDurationInSeconds = settingsFileInfoCacheDuration set * 1000000
@@ -214,13 +229,18 @@ acceptConnection :: Settings
214229 -> IO (IO (Connection, Transport), SockAddr)
215230 -> Application
216231 -> Counter
217− -> InternalInfo0
232+ -> InternalInfo
218233 -> IO ()
219−acceptConnection set getConnMaker app counter ii0 = do
234+acceptConnection set getConnMaker app counter ii = do
220235 -- First mask all exceptions in acceptLoop. This is necessary to
221236 -- ensure that no async exception is throw between the call to
222237 -- acceptNewConnection and the registering of connClose.
238+ --
239+ -- acceptLoop can be broken by closing the listing socket.
223240 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.
224244 gracefulShutdown set counter
225245 where
226246 acceptLoop = do
@@ -238,7 +258,7 @@ acceptConnection set getConnMaker app counter ii0 = do
238258 case mx of
239259 Nothing -> return ()
240260 Just (mkConn, addr) -> do
241− fork set mkConn addr app counter ii0
261+ fork set mkConn addr app counter ii
242262 acceptLoop
243263
244264 acceptNewConnection = do
@@ -261,64 +281,55 @@ fork :: Settings
261281 -> SockAddr
262282 -> Application
263283 -> Counter
264− -> InternalInfo0
284+ -> InternalInfo
265285 -> IO ()
266−fork set mkConn addr app counter ii0 = settingsFork set $ \ unmask ->
267− -- Allocate a new IORef indicating whether the connection has been
268− -- closed, to avoid double-freeing a connection
269− withClosedRef $ \ref ->
270− let cleanUp (conn, _) = closeConn ref conn `finally` connFree conn
271− serve (conn, transport) =
272− -- We need to register a timeout handler for this thread, and
273− -- cancel that handler as soon as we exit. We additionally close
274− -- the connection immediately in case the child thread catches the
275− -- async exception or performs some long-running cleanup action.
276− bracket (T.registerKillThread (timeoutManager0 ii0) (closeConn ref conn))
277− T.cancel $ \th ->
278− let ii1 = toInternalInfo1 ii0 th
279− -- We now have fully registered a connection close handler
280− -- in the case of all exceptions, so it is safe to one
281− -- again allow async exceptions.
282− in unmask .
283− -- Call the user-supplied on exception code if any
284− -- exceptions are thrown.
285− handle (settingsOnException set Nothing) .
286−
287− -- Call the user-supplied code for connection open and close events
288− bracket (onOpen addr) (onClose addr) $ \goingon ->
289−
290− -- Actually serve this connection.
291− -- bracket with closeConn above ensures the connection is closed.
292− when goingon $ serveConnection conn ii1 addr transport set app
293− -- Run the connection maker to get a new connection, and ensure
294− -- that the connection is closed. If the mkConn call throws an
295− -- exception, we will leak the connection. If the mkConn call is
296− -- vulnerable to attacks (e.g., Slowloris), we do nothing to
297− -- protect the server. It is therefore vital that mkConn is well
298− -- vetted.
299− --
300− -- We grab the connection before registering timeouts since the
301− -- timeouts will be useless during connection creation, due to the
302− -- fact that async exceptions are still masked.
303− in bracket mkConn cleanUp serve `catch` settingsOnException set Nothing
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)
304301 where
305− withClosedRef inner = newIORef False >>= inner
306−
307− closeConn ref conn = do
308− isClosed <- atomicModifyIORef' ref $ \x -> (True, x)
309− unless isClosed $ connClose conn
302+ cleanUp (conn, _) = connClose conn `finally` connFree conn
303+
304+ -- We need to register a timeout handler for this thread, and
305+ -- cancel that handler as soon as we exit.
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
310320
311321 onOpen adr = increase counter >> settingsOnOpen set adr
312322 onClose adr _ = decrease counter >> settingsOnClose set adr
313323
314324serveConnection :: Connection
315− -> InternalInfo1
325+ -> InternalInfo
326+ -> T.Handle
316327 -> SockAddr
317328 -> Transport
318329 -> Settings
319330 -> Application
320331 -> IO ()
321−serveConnection conn ii1 origAddr transport settings app = do
332+serveConnection conn ii th origAddr transport settings app = do
322333 -- fixme: Upgrading to HTTP/2 should be supported.
323334 (h2,bs) <- if isHTTP2 transport then
324335 return (True, "")
@@ -328,19 +339,39 @@ serveConnection conn ii1 origAddr transport settings app = do
328339 return (True, bs0)
329340 else
330341 return (False, bs0)
342+ istatus <- newIORef False
331343 if settingsHTTP2Enabled settings && h2 then do
332− 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
333354 -- fixme: origAddr
334− http2 conn ii1 origAddr transport settings recvN app
355+ checkTLS
356+ setConnHTTP2 conn True
357+ http2 conn transport ii origAddr settings recvN sendBS app
335358 else do
336− istatus <- newIORef False
337359 src <- mkSource (wrappedRecv conn th istatus (settingsSlowlorisSize settings))
338360 writeIORef istatus True
339361 leftoverSource src bs
340362 addr <- getProxyProtocolAddr src
341− http1 addr istatus src `E.catch` \e -> do
342− sendErrorResponse addr istatus e
343− 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+
344375 where
345376 getProxyProtocolAddr src =
346377 case settingsProxyProtocol settings of
@@ -382,36 +413,40 @@ serveConnection conn ii1 origAddr transport settings app = do
382413
383414 decodeAscii = map (chr . fromEnum) . S.unpack
384415
385− th = threadHandle1 ii1
386−
387416 shouldSendErrorResponse se
388417 | Just ConnectionClosedByPeer <- fromException se = False
389418 | otherwise = True
390419
391− sendErrorResponse addr istatus e = do
420+ sendErrorResponse req istatus e = do
392421 status <- readIORef istatus
393− when (shouldSendErrorResponse e && status) $ do
394− let ii = toInternalInfo ii1 0 -- dummy
395− dreq = dummyreq addr
396− 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
397426
398427 dummyreq addr = defaultRequest { remoteHost = addr }
399428
400429 errorResponse e = settingsOnExceptionResponse settings e
401430
402− http1 addr istatus src = do
403− (req', mremainingRef, idxhdr, nextBodyFlush, ii) <- recvRequest settings conn ii1 addr src
404− let req = req' { isSecure = isTransportSecure transport }
405− 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
406434 `E.catch` \e -> do
407− -- Call the user-supplied exception handlers, passing the request.
408− sendErrorResponse addr istatus e
409435 settingsOnException settings (Just req) e
410436 -- Don't throw the error again to prevent calling settingsOnException twice.
411437 return False
412− when keepAlive $ http1 addr istatus src
413438
414− 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
415450 -- Let the application run for as long as it wants
416451 T.pause th
417452
@@ -419,30 +454,38 @@ serveConnection conn ii1 origAddr transport settings app = do
419454 -- creating the request, we need to make sure that we don't get
420455 -- an async exception before calling the ResponseSource.
421456 keepAliveRef <- newIORef $ error "keepAliveRef not filled"
422− _ <- app req $ \res -> do
457+ r <- E.try $ app req $ \res -> do
423458 T.resume th
424459 -- FIXME consider forcing evaluation of the res here to
425460 -- send more meaningful error messages to the user.
426461 -- However, it may affect performance.
427462 writeIORef istatus False
428− keepAlive <- sendResponse settings conn ii req idxhdr (readSource src) res
463+ keepAlive <- sendResponse settings conn ii th req idxhdr (readSource src) res
429464 writeIORef keepAliveRef keepAlive
430465 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+
431475 keepAlive <- readIORef keepAliveRef
432476
433477 -- We just send a Response and it takes a time to
434478 -- receive a Request again. If we immediately call recv,
435− -- it is likely to fail and the IO manager works.
436− -- 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
437481 -- thread hoping that the next Request will arrive
438482 -- when this Haskell thread will be re-scheduled.
439483 -- This improves performance at least when
440484 -- the number of cores is small.
441485 Conc.yield
442486
443− if not keepAlive then
444− return False
445− else
487+ if keepAlive
488+ then
446489 -- If there is an unknown or large amount of data to still be read
447490 -- from the request body, simple drop this connection instead of
448491 -- reading it all in to satisfy a keep-alive request.
@@ -468,6 +511,21 @@ serveConnection conn ii1 origAddr transport settings app = do
468511 else
469512 return False
470513 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
471529
472530flushEntireBody :: IO ByteString -> IO ()
473531flushEntireBody src =
@@ -500,12 +558,35 @@ wrappedRecv Connection { connRecv = recv } th istatus slowlorisSize = do
500558 when (S.length bs >= slowlorisSize) $ T.tickle th
501559 return bs
502560
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+--
503576-- Copied from: https://github.com/mzero/plush/blob/master/src/Plush/Server/Warp.hs
577+--
578+-- @since 3.2.17
504579setSocketCloseOnExec :: Socket -> IO ()
505580#if WINDOWS
506581setSocketCloseOnExec _ = return ()
507582#else
508−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
509590#endif
510591
511592gracefulShutdown :: Settings -> Counter -> IO ()