{-# LANGUAGE OverloadedStrings, ScopedTypeVariables, ViewPatterns #-}
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE ImpredicativeTypes, CPP #-}
{-# LANGUAGE MagicHash, UnboxedTuples #-}
module Network.Wai.Handler.Warp.Settings where
import GHC.IO (unsafeUnmask, IO (IO))
import GHC.Prim (fork#)
import UnliftIO (SomeException, fromException)
import qualified Data.ByteString.Char8 as C8
import qualified Data.ByteString.Builder as Builder
import Data.Streaming.Network (HostPreference)
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import Data.Version (showVersion)
import GHC.IO.Exception (IOErrorType(..), AsyncException (ThreadKilled))
import qualified Network.HTTP.Types as H
import Network.Socket (Socket, SockAddr, accept)
import Network.Wai
import qualified Paths_warp
import System.IO (stderr)
import System.IO.Error (ioeGetErrorType)
import System.TimeManager
import Network.Wai.Handler.Warp.Imports
import Network.Wai.Handler.Warp.Types
#if WINDOWS
import Network.Wai.Handler.Warp.Windows (windowsThreadBlockHack)
#endif
data Settings = Settings
{ Settings -> Int
settingsPort :: Port
, Settings -> HostPreference
settingsHost :: HostPreference
, Settings -> Maybe Request -> SomeException -> IO ()
settingsOnException :: Maybe Request -> SomeException -> IO ()
, Settings -> SomeException -> Response
settingsOnExceptionResponse :: SomeException -> Response
, Settings -> SockAddr -> IO Bool
settingsOnOpen :: SockAddr -> IO Bool
, Settings -> SockAddr -> IO ()
settingsOnClose :: SockAddr -> IO ()
, Settings -> Int
settingsTimeout :: Int
, Settings -> Maybe Manager
settingsManager :: Maybe Manager
, Settings -> Int
settingsFdCacheDuration :: Int
, Settings -> Int
settingsFileInfoCacheDuration :: Int
, Settings -> IO ()
settingsBeforeMainLoop :: IO ()
, Settings -> ((forall a. IO a -> IO a) -> IO ()) -> IO ()
settingsFork :: ((forall a. IO a -> IO a) -> IO ()) -> IO ()
, Settings -> Socket -> IO (Socket, SockAddr)
settingsAccept :: Socket -> IO (Socket, SockAddr)
, Settings -> Bool
settingsNoParsePath :: Bool
, Settings -> IO () -> IO ()
settingsInstallShutdownHandler :: IO () -> IO ()
, Settings -> ByteString
settingsServerName :: ByteString
, Settings -> Maybe Int
settingsMaximumBodyFlush :: Maybe Int
, Settings -> ProxyProtocol
settingsProxyProtocol :: ProxyProtocol
, Settings -> Int
settingsSlowlorisSize :: Int
, Settings -> Bool
settingsHTTP2Enabled :: Bool
, Settings -> Request -> Status -> Maybe Integer -> IO ()
settingsLogger :: Request -> H.Status -> Maybe Integer -> IO ()
, Settings -> Request -> ByteString -> Integer -> IO ()
settingsServerPushLogger :: Request -> ByteString -> Integer -> IO ()
, Settings -> Maybe Int
settingsGracefulShutdownTimeout :: Maybe Int
, Settings -> Int
settingsGracefulCloseTimeout1 :: Int
, Settings -> Int
settingsGracefulCloseTimeout2 :: Int
, :: Int
, Settings -> Maybe ByteString
settingsAltSvc :: Maybe ByteString
, Settings -> Int
settingsMaxBuilderResponseBufferSize :: Int
}
data ProxyProtocol = ProxyProtocolNone
| ProxyProtocolRequired
| ProxyProtocolOptional
defaultSettings :: Settings
defaultSettings :: Settings
defaultSettings = Settings
{ settingsPort :: Int
settingsPort = Int
3000
, settingsHost :: HostPreference
settingsHost = HostPreference
"*4"
, settingsOnException :: Maybe Request -> SomeException -> IO ()
settingsOnException = Maybe Request -> SomeException -> IO ()
defaultOnException
, settingsOnExceptionResponse :: SomeException -> Response
settingsOnExceptionResponse = SomeException -> Response
defaultOnExceptionResponse
, settingsOnOpen :: SockAddr -> IO Bool
settingsOnOpen = forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
, settingsOnClose :: SockAddr -> IO ()
settingsOnClose = forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return ()
, settingsTimeout :: Int
settingsTimeout = Int
30
, settingsManager :: Maybe Manager
settingsManager = forall a. Maybe a
Nothing
, settingsFdCacheDuration :: Int
settingsFdCacheDuration = Int
0
, settingsFileInfoCacheDuration :: Int
settingsFileInfoCacheDuration = Int
0
, settingsBeforeMainLoop :: IO ()
settingsBeforeMainLoop = forall (m :: * -> *) a. Monad m => a -> m a
return ()
, settingsFork :: ((forall a. IO a -> IO a) -> IO ()) -> IO ()
settingsFork = ((forall a. IO a -> IO a) -> IO ()) -> IO ()
defaultFork
, settingsAccept :: Socket -> IO (Socket, SockAddr)
settingsAccept = Socket -> IO (Socket, SockAddr)
defaultAccept
, settingsNoParsePath :: Bool
settingsNoParsePath = Bool
False
, settingsInstallShutdownHandler :: IO () -> IO ()
settingsInstallShutdownHandler = forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return ()
, settingsServerName :: ByteString
settingsServerName = [Char] -> ByteString
C8.pack forall a b. (a -> b) -> a -> b
$ [Char]
"Warp/" forall a. [a] -> [a] -> [a]
++ Version -> [Char]
showVersion Version
Paths_warp.version
, settingsMaximumBodyFlush :: Maybe Int
settingsMaximumBodyFlush = forall a. a -> Maybe a
Just Int
8192
, settingsProxyProtocol :: ProxyProtocol
settingsProxyProtocol = ProxyProtocol
ProxyProtocolNone
, settingsSlowlorisSize :: Int
settingsSlowlorisSize = Int
2048
, settingsHTTP2Enabled :: Bool
settingsHTTP2Enabled = Bool
True
, settingsLogger :: Request -> Status -> Maybe Integer -> IO ()
settingsLogger = \Request
_ Status
_ Maybe Integer
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
, settingsServerPushLogger :: Request -> ByteString -> Integer -> IO ()
settingsServerPushLogger = \Request
_ ByteString
_ Integer
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
, settingsGracefulShutdownTimeout :: Maybe Int
settingsGracefulShutdownTimeout = forall a. Maybe a
Nothing
, settingsGracefulCloseTimeout1 :: Int
settingsGracefulCloseTimeout1 = Int
0
, settingsGracefulCloseTimeout2 :: Int
settingsGracefulCloseTimeout2 = Int
2000
, settingsMaxTotalHeaderLength :: Int
settingsMaxTotalHeaderLength = Int
50 forall a. Num a => a -> a -> a
* Int
1024
, settingsAltSvc :: Maybe ByteString
settingsAltSvc = forall a. Maybe a
Nothing
, settingsMaxBuilderResponseBufferSize :: Int
settingsMaxBuilderResponseBufferSize = Int
1049000000
}
defaultShouldDisplayException :: SomeException -> Bool
defaultShouldDisplayException :: SomeException -> Bool
defaultShouldDisplayException SomeException
se
| Just AsyncException
ThreadKilled <- forall e. Exception e => SomeException -> Maybe e
fromException SomeException
se = Bool
False
| Just (InvalidRequest
_ :: InvalidRequest) <- forall e. Exception e => SomeException -> Maybe e
fromException SomeException
se = Bool
False
| Just (IOError -> IOErrorType
ioeGetErrorType -> IOErrorType
et) <- forall e. Exception e => SomeException -> Maybe e
fromException SomeException
se
, IOErrorType
et forall a. Eq a => a -> a -> Bool
== IOErrorType
ResourceVanished Bool -> Bool -> Bool
|| IOErrorType
et forall a. Eq a => a -> a -> Bool
== IOErrorType
InvalidArgument = Bool
False
| Just TimeoutThread
TimeoutThread <- forall e. Exception e => SomeException -> Maybe e
fromException SomeException
se = Bool
False
| Bool
otherwise = Bool
True
defaultOnException :: Maybe Request -> SomeException -> IO ()
defaultOnException :: Maybe Request -> SomeException -> IO ()
defaultOnException Maybe Request
_ SomeException
e =
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (SomeException -> Bool
defaultShouldDisplayException SomeException
e)
forall a b. (a -> b) -> a -> b
$ Handle -> Text -> IO ()
TIO.hPutStrLn Handle
stderr forall a b. (a -> b) -> a -> b
$ [Char] -> Text
T.pack forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> [Char]
show SomeException
e
defaultOnExceptionResponse :: SomeException -> Response
defaultOnExceptionResponse :: SomeException -> Response
defaultOnExceptionResponse SomeException
e
| Just InvalidRequest
PayloadTooLarge <-
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
e = Status -> ResponseHeaders -> ByteString -> Response
responseLBS Status
H.status413
[(HeaderName
H.hContentType, ByteString
"text/plain; charset=utf-8")]
ByteString
"Payload too large"
| Just InvalidRequest
RequestHeaderFieldsTooLarge <-
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
e = Status -> ResponseHeaders -> ByteString -> Response
responseLBS Status
H.status431
[(HeaderName
H.hContentType, ByteString
"text/plain; charset=utf-8")]
ByteString
"Request header fields too large"
| Just (InvalidRequest
_ :: InvalidRequest) <-
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
e = Status -> ResponseHeaders -> ByteString -> Response
responseLBS Status
H.badRequest400
[(HeaderName
H.hContentType, ByteString
"text/plain; charset=utf-8")]
ByteString
"Bad Request"
| Bool
otherwise = Status -> ResponseHeaders -> ByteString -> Response
responseLBS Status
H.internalServerError500
[(HeaderName
H.hContentType, ByteString
"text/plain; charset=utf-8")]
ByteString
"Something went wrong"
exceptionResponseForDebug :: SomeException -> Response
exceptionResponseForDebug :: SomeException -> Response
exceptionResponseForDebug SomeException
e =
Status -> ResponseHeaders -> Builder -> Response
responseBuilder Status
H.internalServerError500
[(HeaderName
H.hContentType, ByteString
"text/plain; charset=utf-8")]
forall a b. (a -> b) -> a -> b
$ Builder
"Exception: " forall a. Semigroup a => a -> a -> a
<> [Char] -> Builder
Builder.stringUtf8 (forall a. Show a => a -> [Char]
show SomeException
e)
defaultFork :: ((forall a. IO a -> IO a) -> IO ()) -> IO ()
defaultFork :: ((forall a. IO a -> IO a) -> IO ()) -> IO ()
defaultFork (forall a. IO a -> IO a) -> IO ()
io =
#if __GLASGOW_HASKELL__ >= 904
IO $ \s0 ->
case io unsafeUnmask of
IO io' ->
case (fork# io' s0) of
(# s1, _tid #) ->
(# s1, () #)
#else
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s0 ->
case (forall a.
a -> State# RealWorld -> (# State# RealWorld, ThreadId# #)
fork# ((forall a. IO a -> IO a) -> IO ()
io forall a. IO a -> IO a
unsafeUnmask) State# RealWorld
s0) of
(# State# RealWorld
s1, ThreadId#
_tid #) ->
(# State# RealWorld
s1, () #)
#endif
defaultAccept :: Socket -> IO (Socket, SockAddr)
defaultAccept :: Socket -> IO (Socket, SockAddr)
defaultAccept =
#if WINDOWS
windowsThreadBlockHack . accept
#else
Socket -> IO (Socket, SockAddr)
accept
#endif