@@ -3,53 +3,50 @@
33{-# LANGUAGE OverloadedStrings #-}
44{-# LANGUAGE PackageImports #-}
55{-# LANGUAGE PatternGuards #-}
6−{-# LANGUAGE RecordWildCards #-}
76{-# LANGUAGE ScopedTypeVariables #-}
87{-# LANGUAGE TupleSections #-}
9−{-# LANGUAGE ViewPatterns #-}
108{-# OPTIONS_GHC -fno-warn-deprecations #-}
119
1210module Network.Wai.Handler.Warp.Run where
1311
14−import "iproute" Data.IP (toHostAddress, toHostAddress6)
12+#if __GLASGOW_HASKELL__ < 709
13+import Control.Applicative ((<$>))
14+#endif
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)
1820import qualified Data.ByteString as S
1921import Data.Char (chr)
22+import "iproute" Data.IP (toHostAddress, toHostAddress6)
2023import Data.IORef (IORef, newIORef, readIORef, writeIORef)
2124import Data.Streaming.Network (bindPortTCP)
2225import Foreign.C.Error (Errno(..), eCONNABORTED)
2326import GHC.IO.Exception (IOException(..))
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
27+import Network (Socket)
28+import Network.Socket (close, accept, withSocketsDo, SockAddr(SockAddrInet, SockAddrInet6), setSocketOption, SocketOption(..))
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−
3631import Network.Wai.Handler.Warp.Buffer
3732import Network.Wai.Handler.Warp.Counter
3833import qualified Network.Wai.Handler.Warp.Date as D
3934import qualified Network.Wai.Handler.Warp.FdCache as F
4035import qualified Network.Wai.Handler.Warp.FileInfoCache as I
41−import Network.Wai.Handler.Warp.HTTP2 (http2)
42−import Network.Wai.Handler.Warp.HTTP2.Types (isHTTP2)
36+import Network.Wai.Handler.Warp.HTTP2 (http2, isHTTP2)
4337import Network.Wai.Handler.Warp.Header
44−import Network.Wai.Handler.Warp.Imports hiding (readInt)
38+import Network.Wai.Handler.Warp.IORef
4539import Network.Wai.Handler.Warp.ReadInt
4640import Network.Wai.Handler.Warp.Recv
4741import Network.Wai.Handler.Warp.Request
4842import Network.Wai.Handler.Warp.Response
4943import Network.Wai.Handler.Warp.SendFile
5044import Network.Wai.Handler.Warp.Settings
45+import qualified Network.Wai.Handler.Warp.Timeout as T
5146import Network.Wai.Handler.Warp.Types
52−
47+import Network.Wai.Internal (ResponseReceived (ResponseReceived))
48+import System.Environment (getEnvironment)
49+import System.Timeout (timeout)
5350
5451#if WINDOWS
5552import Network.Wai.Handler.Warp.Windows
@@ -58,40 +55,28 @@ import Network.Socket (fdSocket)
5855#endif
5956
6057-- | Creating 'Connection' for plain HTTP based on a given socket.
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
58+socketConnection :: Socket -> IO Connection
59+socketConnection s = do
6760 bufferPool <- newBufferPool
6861 writeBuf <- allocateBuffer bufferSize
6962 let sendall = Sock.sendAll s
70− isH2 <- newIORef False -- HTTP/1.x
7163 return Connection {
7264 connSendMany = Sock.sendMany s
7365 , connSendAll = sendall
7466 , 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
8567 , connClose = close s
86−#endif
8768 , connFree = freeBuffer writeBuf
8869 , connRecv = receive s bufferPool
8970 , connRecvBuf = receiveBuf s
9071 , connWriteBuffer = writeBuf
9172 , connBufferSize = bufferSize
92− , connHTTP2 = isH2
9373 }
9474
75+#if __GLASGOW_HASKELL__ < 702
76+allowInterrupt :: IO ()
77+allowInterrupt = unblock $ return ()
78+#endif
79+
9580-- | Run an 'Application' on the given port.
9681-- This calls 'runSettings' with 'defaultSettings'.
9782run :: Port -> Application -> IO ()
@@ -104,7 +89,7 @@ run p = runSettings defaultSettings { settingsPort = p }
10489-- Since 3.0.9
10590runEnv :: Port -> Application -> IO ()
10691runEnv p app = do
107− mp <- lookupEnv "PORT"
92+ mp <- lookup "PORT" <$> getEnvironment
10893
10994 maybe (run p app) runReadPort mp
11095
@@ -151,7 +136,7 @@ runSettingsSocket set socket app = do
151136 setSocketCloseOnExec s
152137 -- NoDelay causes an error for AF_UNIX.
153138 setSocketOption s NoDelay 1 `E.catch` \(E.SomeException _) -> return ()
154− conn <- socketConnection set s
139+ conn <- socketConnection s
155140 return (conn, sa)
156141
157142 closeListenSocket = close socket
@@ -192,15 +177,15 @@ runSettingsConnectionMakerSecure :: Settings -> IO (IO (Connection, Transport),
192177runSettingsConnectionMakerSecure set getConnMaker app = do
193178 settingsBeforeMainLoop set
194179 counter <- newCounter
195− withII $ acceptConnection set getConnMaker app counter
180+ withII0 $ acceptConnection set getConnMaker app counter
196181 where
197− withII action =
182+ withII0 action =
198183 withTimeoutManager $ \tm ->
199184 D.withDateCache $ \dc ->
200185 F.withFdCache fdCacheDurationInSeconds $ \fdc ->
201186 I.withFileInfoCache fdFileInfoDurationInSeconds $ \fic -> do
202− let ii = InternalInfo tm dc fdc fic
203− action ii
187+ let ii0 = InternalInfo0 tm dc fdc fic
188+ action ii0
204189
205190 !fdCacheDurationInSeconds = settingsFdCacheDuration set * 1000000
206191 !fdFileInfoDurationInSeconds = settingsFileInfoCacheDuration set * 1000000
@@ -229,18 +214,13 @@ acceptConnection :: Settings
229214 -> IO (IO (Connection, Transport), SockAddr)
230215 -> Application
231216 -> Counter
232− -> InternalInfo
217+ -> InternalInfo0
233218 -> IO ()
234−acceptConnection set getConnMaker app counter ii = do
219+acceptConnection set getConnMaker app counter ii0 = do
235220 -- First mask all exceptions in acceptLoop. This is necessary to
236221 -- ensure that no async exception is throw between the call to
237222 -- acceptNewConnection and the registering of connClose.
238− --
239− -- acceptLoop can be broken by closing the listing socket.
240223 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.
244224 gracefulShutdown set counter
245225 where
246226 acceptLoop = do
@@ -258,7 +238,7 @@ acceptConnection set getConnMaker app counter ii = do
258238 case mx of
259239 Nothing -> return ()
260240 Just (mkConn, addr) -> do
261− fork set mkConn addr app counter ii
241+ fork set mkConn addr app counter ii0
262242 acceptLoop
263243
264244 acceptNewConnection = do
@@ -281,55 +261,64 @@ fork :: Settings
281261 -> SockAddr
282262 -> Application
283263 -> Counter
284− -> InternalInfo
264+ -> InternalInfo0
285265 -> IO ()
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)
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
301304 where
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
305+ withClosedRef inner = newIORef False >>= inner
306+
307+ closeConn ref conn = do
308+ isClosed <- atomicModifyIORef' ref $ \x -> (True, x)
309+ unless isClosed $ connClose conn
320310
321311 onOpen adr = increase counter >> settingsOnOpen set adr
322312 onClose adr _ = decrease counter >> settingsOnClose set adr
323313
324314serveConnection :: Connection
325− -> InternalInfo
326− -> T.Handle
315+ -> InternalInfo1
327316 -> SockAddr
328317 -> Transport
329318 -> Settings
330319 -> Application
331320 -> IO ()
332−serveConnection conn ii th origAddr transport settings app = do
321+serveConnection conn ii1 origAddr transport settings app = do
333322 -- fixme: Upgrading to HTTP/2 should be supported.
334323 (h2,bs) <- if isHTTP2 transport then
335324 return (True, "")
@@ -339,39 +328,19 @@ serveConnection conn ii th origAddr transport settings app = do
339328 return (True, bs0)
340329 else
341330 return (False, bs0)
342− istatus <- newIORef False
343331 if settingsHTTP2Enabled settings && h2 then do
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
332+ recvN <- makeReceiveN bs (connRecv conn) (connRecvBuf conn)
354333 -- fixme: origAddr
355− checkTLS
356− setConnHTTP2 conn True
357− http2 conn transport ii origAddr settings recvN sendBS app
334+ http2 conn ii1 origAddr transport settings recvN app
358335 else do
336+ istatus <- newIORef False
359337 src <- mkSource (wrappedRecv conn th istatus (settingsSlowlorisSize settings))
360338 writeIORef istatus True
361339 leftoverSource src bs
362340 addr <- getProxyProtocolAddr src
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−
341+ http1 addr istatus src `E.catch` \e -> do
342+ sendErrorResponse addr istatus e
343+ throwIO (e :: SomeException)
375344 where
376345 getProxyProtocolAddr src =
377346 case settingsProxyProtocol settings of
@@ -413,40 +382,36 @@ serveConnection conn ii th origAddr transport settings app = do
413382
414383 decodeAscii = map (chr . fromEnum) . S.unpack
415384
385+ th = threadHandle1 ii1
386+
416387 shouldSendErrorResponse se
417388 | Just ConnectionClosedByPeer <- fromException se = False
418389 | otherwise = True
419390
420− sendErrorResponse req istatus e = do
391+ sendErrorResponse addr istatus e = do
421392 status <- readIORef istatus
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
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)
426397
427398 dummyreq addr = defaultRequest { remoteHost = addr }
428399
429400 errorResponse e = settingsOnExceptionResponse settings e
430401
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
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
434406 `E.catch` \e -> do
407+ -- Call the user-supplied exception handlers, passing the request.
408+ sendErrorResponse addr istatus e
435409 settingsOnException settings (Just req) e
436410 -- Don't throw the error again to prevent calling settingsOnException twice.
437411 return False
412+ when keepAlive $ http1 addr istatus src
438413
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
414+ processRequest istatus src req mremainingRef idxhdr nextBodyFlush ii = do
450415 -- Let the application run for as long as it wants
451416 T.pause th
452417
@@ -454,38 +419,30 @@ serveConnection conn ii th origAddr transport settings app = do
454419 -- creating the request, we need to make sure that we don't get
455420 -- an async exception before calling the ResponseSource.
456421 keepAliveRef <- newIORef $ error "keepAliveRef not filled"
457− r <- E.try $ app req $ \res -> do
422+ _ <- app req $ \res -> do
458423 T.resume th
459424 -- FIXME consider forcing evaluation of the res here to
460425 -- send more meaningful error messages to the user.
461426 -- However, it may affect performance.
462427 writeIORef istatus False
463− keepAlive <- sendResponse settings conn ii th req idxhdr (readSource src) res
428+ keepAlive <- sendResponse settings conn ii req idxhdr (readSource src) res
464429 writeIORef keepAliveRef keepAlive
465430 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−
475431 keepAlive <- readIORef keepAliveRef
476432
477433 -- We just send a Response and it takes a time to
478434 -- receive a Request again. If we immediately call recv,
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
435+ -- it is likely to fail and the IO manager works.
436+ -- It is very costly. So, we yield to another Haskell
481437 -- thread hoping that the next Request will arrive
482438 -- when this Haskell thread will be re-scheduled.
483439 -- This improves performance at least when
484440 -- the number of cores is small.
485441 Conc.yield
486442
487− if keepAlive
488− then
443+ if not keepAlive then
444+ return False
445+ else
489446 -- If there is an unknown or large amount of data to still be read
490447 -- from the request body, simple drop this connection instead of
491448 -- reading it all in to satisfy a keep-alive request.
@@ -511,21 +468,6 @@ serveConnection conn ii th origAddr transport settings app = do
511468 else
512469 return False
513470 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
529471
530472flushEntireBody :: IO ByteString -> IO ()
531473flushEntireBody src =
@@ -558,35 +500,12 @@ wrappedRecv Connection { connRecv = recv } th istatus slowlorisSize = do
558500 when (S.length bs >= slowlorisSize) $ T.tickle th
559501 return bs
560502
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−--
576503-- Copied from: https://github.com/mzero/plush/blob/master/src/Plush/Server/Warp.hs
577−--
578−-- @since 3.2.17
579504setSocketCloseOnExec :: Socket -> IO ()
580505#if WINDOWS
581506setSocketCloseOnExec _ = return ()
582507#else
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
508+setSocketCloseOnExec socket = F.setFileCloseOnExec $ fromIntegral $ fdSocket socket
590509#endif
591510
592511gracefulShutdown :: Settings -> Counter -> IO ()