HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulewarp-3.4.9Haskell2010

Network.Wai.Handler.Warp

A fast, light-weight HTTP server handler for WAI.

HTTP/1.0, HTTP/1.1 and HTTP/2 are supported. For HTTP/2, Warp supports direct and ALPN (in TLS) but not upgrade.

Note on slowloris timeouts: to prevent slowloris attacks, timeouts are used at various points in request receiving and response sending. One interesting corner case is partial request body consumption; in that case, Warp's timeout handling is still in effect, and the timeout will not be triggered again. Therefore, it is recommended that once you start consuming the request body, you either:

  • consume the entire body promptly

  • call the pauseTimeout function

For more information, see https://github.com/yesodweb/wai/issues/351.

  • 7 types
  • 67 values
  • Packagewarp-3.4.9
  • Exports74
  • LanguageHaskell2010
  • LicenceMIT
  • SourceWarp.hs

Run a Warp server

4 declarations

All of these automatically serve the same Application over HTTP/1, HTTP/1.1, and HTTP/2.

This installs a shutdown handler for the given socket and calls runSettingsConnection with the default connection setup action which handles plain (non-cipher) HTTP. When the listen socket in the second argument is closed, all live connections are gracefully shut down.

The supplied socket can be a Unix named socket, which can be used when reverse HTTP proxying into your application.

Note that the settingsPort will still be passed to Applications via the serverPort record.

Settings

2 declarations
datadata Settings
#

Various Warp server settings. This is purposely kept as an abstract data type so that new settings can be added without breaking backwards compatibility. In order to create a Settings value, use defaultSettings and the various 'set' functions to modify individual fields. For example:

setTimeout 20 defaultSettings

The default settings for the Warp server. See the individual settings for the default value.

Setters

A function to create a Response when an exception occurs. Default: defaultOnExceptionResponse

Note that an application can handle its own exceptions without interfering with Warp:

myApp :: Application
myApp request respond = innerApp `catch` onError
  where
    onError = respond . response500 request

response500 :: Request -> SomeException -> Response
response500 req someEx = responseLBS status500 -- ...

Since 2.1.0

valuesetOnOpen :: (SockAddr -> IO Bool) -> Settings -> Settings
#

What to do when a connection is opened. When False is returned, the connection is closed immediately. Otherwise, the connection is going on. Default: always returns True.

Since 2.1.0

valuesetTimeout :: Int -> Settings -> Settings
#

"Slow-loris" timeout lower-bound value in seconds. Connections where network progress is made less frequently than this may be closed. In practice many connections may be allowed to go without progress for up to twice this amount of time. Note that this timeout is not applied to application code, only network progress.

Default value: 30

Since 2.1.0

Cache duration time of file descriptors in seconds. 0 means that the cache mechanism is not used.

The FD cache is an optimization that is useful for servers dealing with static files. However, if files are being modified, it can cause incorrect results in some cases. Therefore, we disable it by default. If you know that your files will be static or you prefer performance to file consistency, it's recommended to turn this on; a reasonable value for those cases is 10. Enabling this cache results in drastic performance improvement for file transfers.

Default value: 0, was previously 10

Since 3.0.13

Cache duration time of file information in seconds. 0 means that the cache mechanism is not used.

The file information cache is an optimization that is useful for servers dealing with static files. However, if files are being modified, it can cause incorrect results in some cases. Therefore, we disable it by default. If you know that your files will be static or you prefer performance to file consistency, it's recommended to turn this on; a reasonable value for those cases is 10. Enabling this cache results in drastic performance improvement for file transfers.

Default value: 0

valuesetBeforeMainLoop :: IO () -> Settings -> Settings
#

Code to run after the listening socket is ready but before entering the main event loop. Useful for signaling to tests that they can start running, or to drop permissions after binding to a restricted port.

Default: do nothing.

Since 2.1.0

valuesetInstallShutdownHandler :: (IO () -> IO ()) -> Settings -> Settings
#

A code to install shutdown handler.

For instance, this code should set up a UNIX signal handler. The handler should call the first argument, which closes the listen socket, at shutdown.

Example usage:

settings :: IO () -> Settings
settings shutdownAction = setInstallShutdownHandler shutdownHandler defaultSettings
  where
    shutdownHandler closeSocket =
      void $ installHandler sigTERM (Catch $ shutdownAction >> closeSocket) Nothing

Note that by default, the graceful shutdown mode lasts indefinitely (see setGracefulShutdownTimeout). If you install a signal handler as above, upon receiving that signal, the custom shutdown action will run and all outstanding requests will be handled.

You may instead prefer to do one or both of the following:

  • Only wait a finite amount of time for outstanding requests to complete, using setGracefulShutdownTimeout.

  • Only catch one signal, so the second hard-kills the Warp server, using CatchOnce.

Default: does not install any code.

Since 3.0.1

Default server name to be sent as the "Server:" header if an application does not set one. If an empty string is set, the "Server:" header is not sent. This is true even if an application set one.

Since 3.0.2

The maximum number of bytes to flush from an unconsumed request body.

By default, Warp does not flush the request body so that, if a large body is present, the connection is simply terminated instead of wasting time and bandwidth on transmitting it. However, some clients do not deal with that situation well. You can either change this setting to Nothing to flush the entire body in all cases, or in your application ensure that you always consume the entire request body.

Default: 8192 bytes.

Since 3.0.3

valuesetFork
  1. :: ((forall a. IO a -> IO a) -> IO ()) -> IO ()
  2. -> Settings
  3. -> Settings
#

Code to fork a new thread to accept a connection.

This may be useful if you need OS bound threads, or if you wish to develop an alternative threading model.

Default: void . forkIOWithUnmask

Since 3.0.4

valuesetAccept :: (Socket -> IO (Socket, SockAddr)) -> Settings -> Settings
#

Code to accept a new connection.

Useful if you need to provide connected sockets from something other than a standard accept call.

Default: defaultAccept

Since 3.3.24

Require PROXY header.

This is for cases where a "dumb" TCP/SSL proxy is being used, which cannot add an X-Forwarded-For HTTP header field but has enabled support for the PROXY protocol.

See http://www.haproxy.org/download/1.5/doc/proxy-protocol.txt and http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/TerminologyandKeyConcepts.html#proxy-protocol.

Only the human-readable header format (version 1) is supported. The binary header format (version 2) is not supported.

Since 3.0.5

Use the PROXY header if it exists, but also accept connections without the header. See setProxyProtocolRequired.

WARNING: This is contrary to the PROXY protocol specification and using it can indicate a security problem with your architecture if the web server is directly accessible to the public, since it would allow easy IP address spoofing. However, it can be useful in some cases, such as if a load balancer health check uses regular HTTP without the PROXY header, but proxied connections do include the PROXY header.

Since 3.0.5

Set the graceful shutdown timeout. A timeout of Nothing will wait indefinitely, and a number, if provided, will be treated as seconds to wait for requests to finish, before shutting down the server entirely.

Graceful shutdown mode is entered when the server socket is closed; see setInstallShutdownHandler for an example of how this could be done in response to a UNIX signal.

Since 3.2.8

Getters

Exception handler

Exception response handler

Data types

3 declarations
datadata HostPreference
#

Which host to bind.

Note: The IsString instance recognizes the following special values:

  • * means HostAny - "any IPv4 or IPv6 hostname"

  • *4 means HostIPv4 - "any IPv4 or IPv6 hostname, IPv4 preferred"

  • !4 means HostIPv4Only - "any IPv4 hostname"

  • *6 means HostIPv6@ - "any IPv4 or IPv6 hostname, IPv6 preferred"

  • !6 means HostIPv6Only - "any IPv6 hostname"

Note that the permissive * values allow binding to an IPv4 or an IPv6 hostname, which means you might be able to successfully bind to a port more times than you expect (eg once on the IPv4 localhost 127.0.0.1 and again on the IPv6 localhost 0:0:0:0:0:0:0:1).

Any other value is treated as a hostname. As an example, to bind to the IPv4 local host only, use "127.0.0.1".

Instances5Eq, Ord, Read, Show, IsString
typetype Port = Int
#

TCP port number.

datadata InvalidRequest
#
Instances3Eq, Show, Exception

Utilities

9 declarations

Getting file information of the target file.

This function first uses a stat(2) or similar system call to obtain information of the target file, then registers it into the internal cache. From the next time, the information is obtained from the cache. This reduces the overhead to call the system call. The internal cache is refreshed every duration specified by setFileInfoCacheDuration.

This function throws an IO exception if the information is not available. For instance, the target file does not exist. If this function is used an a Request generated by a WAI backend besides Warp, it also throws an IO exception.

Since 3.1.10

valuetestWithApplication :: IO Application -> (Port -> IO a) -> IO a
#

Same as withApplication but with different exception handling: If the given Application throws an exception, testWithApplication will re-throw the exception to the calling thread, possibly interrupting the execution of the given operation.

This is handy for running tests against an Application over a real network port. When running tests, it's useful to let exceptions thrown by your Application propagate to the main thread of the test-suite.

The exception handling makes this function unsuitable for use in production. Use withApplication instead.

Version

1 declaration

HTTP/2

0 declarations

HTTP2 data

datadata HTTP2Data
#

HTTP/2 specific data.

Since: 3.2.7

Push promise

datadata PushPromise
#

HTTP/2 push promise or sever push. This allows files only for backward-compatibility while the HTTP/2 library supports other types.

Since: 3.2.7

Instances3Eq, Ord, Show
  • Eq PushPromiseDefined in warp-3.4.9 · Network.Wai.Handler.Warp.HTTP2.Types
  • Ord PushPromiseDefined in warp-3.4.9 · Network.Wai.Handler.Warp.HTTP2.Types
  • Show PushPromiseDefined in warp-3.4.9 · Network.Wai.Handler.Warp.HTTP2.Types