@@ -3,53 +3,49 @@
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 (sClose, Socket)
28+import Network.Socket (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)
4538import Network.Wai.Handler.Warp.ReadInt
4639import Network.Wai.Handler.Warp.Recv
4740import Network.Wai.Handler.Warp.Request
4841import Network.Wai.Handler.Warp.Response
4942import Network.Wai.Handler.Warp.SendFile
5043import Network.Wai.Handler.Warp.Settings
44+import qualified Network.Wai.Handler.Warp.Timeout as T
5145import Network.Wai.Handler.Warp.Types
52−
46+import Network.Wai.Internal (ResponseReceived (ResponseReceived))
47+import System.Environment (getEnvironment)
48+import System.Timeout (timeout)
5349
5450#if WINDOWS
5551import Network.Wai.Handler.Warp.Windows
@@ -58,40 +54,27 @@ import Network.Socket (fdSocket)
5854#endif
5955
6056-- | 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
57+socketConnection :: Socket -> IO Connection
58+socketConnection s = do
6759 bufferPool <- newBufferPool
6860 writeBuf <- allocateBuffer bufferSize
6961 let sendall = Sock.sendAll s
70− isH2 <- newIORef False -- HTTP/1.x
7162 return Connection {
7263 connSendMany = Sock.sendMany s
7364 , connSendAll = sendall
7465 , 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
85− , connClose = close s
86−#endif
87− , connFree = freeBuffer writeBuf
66+ , connClose = sClose s >> freeBuffer writeBuf
8867 , connRecv = receive s bufferPool
8968 , connRecvBuf = receiveBuf s
9069 , connWriteBuffer = writeBuf
9170 , connBufferSize = bufferSize
92− , connHTTP2 = isH2
9371 }
9472
73+#if __GLASGOW_HASKELL__ < 702
74+allowInterrupt :: IO ()
75+allowInterrupt = unblock $ return ()
76+#endif
77+
9578-- | Run an 'Application' on the given port.
9679-- This calls 'runSettings' with 'defaultSettings'.
9780run :: Port -> Application -> IO ()
@@ -104,7 +87,7 @@ run p = runSettings defaultSettings { settingsPort = p }
10487-- Since 3.0.9
10588runEnv :: Port -> Application -> IO ()
10689runEnv p app = do
107− mp <- lookupEnv "PORT"
90+ mp <- lookup "PORT" <$> getEnvironment
10891
10992 maybe (run p app) runReadPort mp
11093
@@ -121,7 +104,7 @@ runSettings :: Settings -> Application -> IO ()
121104runSettings set app = withSocketsDo $
122105 bracket
123106 (bindPortTCP (settingsPort set) (settingsHost set))
124− close
107+ sClose
125108 (\socket -> do
126109 setSocketCloseOnExec socket
127110 runSettingsSocket set socket app)
@@ -151,10 +134,10 @@ runSettingsSocket set socket app = do
151134 setSocketCloseOnExec s
152135 -- NoDelay causes an error for AF_UNIX.
153136 setSocketOption s NoDelay 1 `E.catch` \(E.SomeException _) -> return ()
154− conn <- socketConnection set s
137+ conn <- socketConnection s
155138 return (conn, sa)
156139
157− closeListenSocket = close socket
140+ closeListenSocket = sClose socket
158141
159142-- | The connection setup action would be expensive. A good example
160143-- is initialization of TLS.
@@ -192,15 +175,15 @@ runSettingsConnectionMakerSecure :: Settings -> IO (IO (Connection, Transport),
192175runSettingsConnectionMakerSecure set getConnMaker app = do
193176 settingsBeforeMainLoop set
194177 counter <- newCounter
195− withII $ acceptConnection set getConnMaker app counter
178+ withII0 $ acceptConnection set getConnMaker app counter
196179 where
197− withII action =
180+ withII0 action =
198181 withTimeoutManager $ \tm ->
199182 D.withDateCache $ \dc ->
200183 F.withFdCache fdCacheDurationInSeconds $ \fdc ->
201184 I.withFileInfoCache fdFileInfoDurationInSeconds $ \fic -> do
202− let ii = InternalInfo tm dc fdc fic
203− action ii
185+ let ii0 = InternalInfo0 tm dc fdc fic
186+ action ii0
204187
205188 !fdCacheDurationInSeconds = settingsFdCacheDuration set * 1000000
206189 !fdFileInfoDurationInSeconds = settingsFileInfoCacheDuration set * 1000000
@@ -229,18 +212,13 @@ acceptConnection :: Settings
229212 -> IO (IO (Connection, Transport), SockAddr)
230213 -> Application
231214 -> Counter
232− -> InternalInfo
215+ -> InternalInfo0
233216 -> IO ()
234−acceptConnection set getConnMaker app counter ii = do
217+acceptConnection set getConnMaker app counter ii0 = do
235218 -- First mask all exceptions in acceptLoop. This is necessary to
236219 -- ensure that no async exception is throw between the call to
237220 -- acceptNewConnection and the registering of connClose.
238− --
239− -- acceptLoop can be broken by closing the listing socket.
240221 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.
244222 gracefulShutdown set counter
245223 where
246224 acceptLoop = do
@@ -258,7 +236,7 @@ acceptConnection set getConnMaker app counter ii = do
258236 case mx of
259237 Nothing -> return ()
260238 Just (mkConn, addr) -> do
261− fork set mkConn addr app counter ii
239+ fork set mkConn addr app counter ii0
262240 acceptLoop
263241
264242 acceptNewConnection = do
@@ -281,55 +259,54 @@ fork :: Settings
281259 -> SockAddr
282260 -> Application
283261 -> Counter
284− -> InternalInfo
262+ -> InternalInfo0
285263 -> 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)
301− where
302− cleanUp (conn, _) = connClose conn `finally` connFree conn
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) ->
303276
304277 -- We need to register a timeout handler for this thread, and
305278 -- 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
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
320298
321299 onOpen adr = increase counter >> settingsOnOpen set adr
322300 onClose adr _ = decrease counter >> settingsOnClose set adr
323301
324302serveConnection :: Connection
325− -> InternalInfo
326− -> T.Handle
303+ -> InternalInfo1
327304 -> SockAddr
328305 -> Transport
329306 -> Settings
330307 -> Application
331308 -> IO ()
332−serveConnection conn ii th origAddr transport settings app = do
309+serveConnection conn ii1 origAddr transport settings app = do
333310 -- fixme: Upgrading to HTTP/2 should be supported.
334311 (h2,bs) <- if isHTTP2 transport then
335312 return (True, "")
@@ -339,39 +316,19 @@ serveConnection conn ii th origAddr transport settings app = do
339316 return (True, bs0)
340317 else
341318 return (False, bs0)
342− istatus <- newIORef False
343319 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
320+ recvN <- makeReceiveN bs (connRecv conn) (connRecvBuf conn)
354321 -- fixme: origAddr
355− checkTLS
356− setConnHTTP2 conn True
357− http2 conn transport ii origAddr settings recvN sendBS app
322+ http2 conn ii1 origAddr transport settings recvN app
358323 else do
324+ istatus <- newIORef False
359325 src <- mkSource (wrappedRecv conn th istatus (settingsSlowlorisSize settings))
360326 writeIORef istatus True
361327 leftoverSource src bs
362328 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−
329+ http1 addr istatus src `E.catch` \e -> do
330+ sendErrorResponse addr istatus e
331+ throwIO (e :: SomeException)
375332 where
376333 getProxyProtocolAddr src =
377334 case settingsProxyProtocol settings of
@@ -413,40 +370,36 @@ serveConnection conn ii th origAddr transport settings app = do
413370
414371 decodeAscii = map (chr . fromEnum) . S.unpack
415372
373+ th = threadHandle1 ii1
374+
416375 shouldSendErrorResponse se
417376 | Just ConnectionClosedByPeer <- fromException se = False
418377 | otherwise = True
419378
420− sendErrorResponse req istatus e = do
379+ sendErrorResponse addr istatus e = do
421380 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
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)
426385
427386 dummyreq addr = defaultRequest { remoteHost = addr }
428387
429388 errorResponse e = settingsOnExceptionResponse settings e
430389
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
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
434394 `E.catch` \e -> do
395+ -- Call the user-supplied exception handlers, passing the request.
396+ sendErrorResponse addr istatus e
435397 settingsOnException settings (Just req) e
436398 -- Don't throw the error again to prevent calling settingsOnException twice.
437399 return False
400+ when keepAlive $ http1 addr istatus src
438401
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
402+ processRequest istatus src req mremainingRef idxhdr nextBodyFlush ii = do
450403 -- Let the application run for as long as it wants
451404 T.pause th
452405
@@ -454,38 +407,30 @@ serveConnection conn ii th origAddr transport settings app = do
454407 -- creating the request, we need to make sure that we don't get
455408 -- an async exception before calling the ResponseSource.
456409 keepAliveRef <- newIORef $ error "keepAliveRef not filled"
457− r <- E.try $ app req $ \res -> do
410+ _ <- app req $ \res -> do
458411 T.resume th
459412 -- FIXME consider forcing evaluation of the res here to
460413 -- send more meaningful error messages to the user.
461414 -- However, it may affect performance.
462415 writeIORef istatus False
463− keepAlive <- sendResponse settings conn ii th req idxhdr (readSource src) res
416+ keepAlive <- sendResponse settings conn ii req idxhdr (readSource src) res
464417 writeIORef keepAliveRef keepAlive
465418 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−
475419 keepAlive <- readIORef keepAliveRef
476420
477421 -- We just send a Response and it takes a time to
478422 -- 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
423+ -- it is likely to fail and the IO manager works.
424+ -- It is very costly. So, we yield to another Haskell
481425 -- thread hoping that the next Request will arrive
482426 -- when this Haskell thread will be re-scheduled.
483427 -- This improves performance at least when
484428 -- the number of cores is small.
485429 Conc.yield
486430
487− if keepAlive
488− then
431+ if not keepAlive then
432+ return False
433+ else
489434 -- If there is an unknown or large amount of data to still be read
490435 -- from the request body, simple drop this connection instead of
491436 -- reading it all in to satisfy a keep-alive request.
@@ -511,21 +456,6 @@ serveConnection conn ii th origAddr transport settings app = do
511456 else
512457 return False
513458 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
529459
530460flushEntireBody :: IO ByteString -> IO ()
531461flushEntireBody src =
@@ -558,35 +488,12 @@ wrappedRecv Connection { connRecv = recv } th istatus slowlorisSize = do
558488 when (S.length bs >= slowlorisSize) $ T.tickle th
559489 return bs
560490
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−--
576491-- Copied from: https://github.com/mzero/plush/blob/master/src/Plush/Server/Warp.hs
577−--
578−-- @since 3.2.17
579492setSocketCloseOnExec :: Socket -> IO ()
580493#if WINDOWS
581494setSocketCloseOnExec _ = return ()
582495#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
496+setSocketCloseOnExec socket = F.setFileCloseOnExec $ fromIntegral $ fdSocket socket
590497#endif
591498
592499gracefulShutdown :: Settings -> Counter -> IO ()