warp-1.3.9: A fast, light-weight web server for WAI applications.

Safe Haskell | None

Network.Wai.Handler.Warp

Contents

- Run a Warp server
- Settings
  - Data types
- Connection
- Datatypes
- Internal (Manager)
- Internal
- Misc

Description

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

Synopsis

- run :: Port -> Application -> IO ()
- runSettings :: Settings -> Application -> IO ()
- runSettingsSocket :: Settings -> Socket -> Application -> IO ()
- data Settings
- defaultSettings :: Settings
- settingsPort :: Settings -> Int
- settingsHost :: Settings -> HostPreference
- settingsOnException :: Settings -> SomeException -> IO ()
- settingsOnOpen :: Settings -> IO ()
- settingsOnClose :: Settings -> IO ()
- settingsTimeout :: Settings -> Int
- settingsIntercept :: Settings -> Request -> Maybe (Source (ResourceT IO) ByteString -> Connection -> ResourceT IO ())
- settingsManager :: Settings -> Maybe Manager
- settingsFdCacheDuration :: Settings -> Int
- settingsResourceTPerRequest :: Settings -> Bool
- settingsBeforeMainLoop :: Settings -> IO ()
- settingsServerName :: Settings -> ByteString
- data HostPreference
  - = HostAny
  - | HostIPv4
  - | HostIPv6
  - | Host String
- data Connection = Connection {
  - connSendMany :: [ByteString] -> IO ()
  - connSendAll :: ByteString -> IO ()
  - connSendFile :: FilePath -> Integer -> Integer -> IO () -> [ByteString] -> Cleaner -> IO ()
  - connClose :: IO ()
  - connRecv :: IO ByteString
  }
- runSettingsConnection :: Settings -> IO (Connection, SockAddr) -> Application -> IO ()
- runSettingsConnectionMaker :: Settings -> IO (IO Connection, SockAddr) -> Application -> IO ()
- type Port = Int
- data InvalidRequest
  - = NotEnoughLines [String]
  - | BadFirstLine String
  - | NonHttp
  - | IncompleteHeaders
  - | ConnectionClosedByPeer
  - | OverLargeHeader
- data Manager
- data Handle
- initialize :: Int -> IO Manager
- withManager :: Int -> (Manager -> IO a) -> IO a
- register :: Manager -> IO () -> IO Handle
- registerKillThread :: Manager -> IO Handle
- pause :: Handle -> IO ()
- resume :: Handle -> IO ()
- cancel :: Handle -> IO ()
- parseRequest :: Connection -> Port -> SockAddr -> Source (ResourceT IO) ByteString -> ResourceT IO (Request, IO (ResumableSource (ResourceT IO) ByteString))
- sendResponse :: Settings -> Cleaner -> Request -> Connection -> Response -> ResourceT IO Bool
- dummyCleaner :: Cleaner
- socketConnection :: Socket -> Connection
- warpVersion :: String

# Run a Warp server

run :: Port -> Application -> IO ()Source

Run an `Application` on the given port. This calls `runSettings` with `defaultSettings`.

runSettings :: Settings -> Application -> IO ()Source

Run a Warp server with the given settings.

runSettingsSocket :: Settings -> Socket -> Application -> IO ()Source

Same as `runSettings`, but uses a user-supplied socket instead of opening one. This allows the user to provide, for example, Unix named socket, which can be used when reverse HTTP proxying into your application.

Note that the `settingsPort` will still be passed to `Application`s via the `serverPort` record.

# Settings

data Settings Source

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 record syntax to modify individual records. For example:

     defaultSettings { settingsTimeout = 20 }

defaultSettings :: Settings Source

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

settingsPort :: Settings -> Int Source

Port to listen on. Default value: 3000

settingsHost :: Settings -> HostPreference Source

Default value: HostIPv4

settingsOnException :: Settings -> SomeException -> IO ()Source

What to do with exceptions thrown by either the application or server. Default: ignore server-generated exceptions (see `InvalidRequest`) and print application-generated applications to stderr.

settingsOnOpen :: Settings -> IO ()Source

What to do when a connection is open. Default: do nothing.

settingsOnClose :: Settings -> IO ()Source

What to do when a connection is close. Default: do nothing.

settingsTimeout :: Settings -> Int Source

Timeout value in seconds. Default value: 30

settingsIntercept :: Settings -> Request -> Maybe (Source (ResourceT IO) ByteString -> Connection -> ResourceT IO ())Source

settingsManager :: Settings -> Maybe Manager Source

Use an existing timeout manager instead of spawning a new one. If used, `settingsTimeout` is ignored. Default is `Nothing`

settingsFdCacheDuration :: Settings -> Int Source

Cache duratoin time of file descriptors in seconds. 0 means that the cache mechanism is not used. Default value: 10

settingsResourceTPerRequest :: Settings -> Bool Source

If `True`, each request/response pair will run in a separate `ResourceT`. This provides more intuitive behavior for dynamic code, but can hinder performance in high-throughput cases. File servers can safely set to `False` for increased performance. Default is `True`.

settingsBeforeMainLoop :: Settings -> IO ()Source

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 1.3.6

settingsServerName :: Settings -> ByteString Source

Server name to be sent in the Server header.

Default: Warp/version

Since 1.3.8

## Data types

data HostPreference

Which host to bind.

Note: The `IsString` instance recognizes the following special values:

- `*` means `HostAny`
- `*4` means `HostIPv4`
- `*6` means `HostIPv6`

Constructors

HostAny |
HostIPv4 |
HostIPv6 |
Host String |

Instances

Eq HostPreference |
Ord HostPreference |
Read HostPreference |
Show HostPreference |
IsString HostPreference |

# Connection

data Connection Source

In order to provide slowloris protection, Warp provides timeout handlers. We follow these rules:

- A timeout is created when a connection is opened.
- When all request headers are read, the timeout is tickled.
- Every time at least 2048 bytes of the request body are read, the timeout is tickled.
- The timeout is paused while executing user code. This will apply to both the application itself, and a ResponseSource response. The timeout is resumed as soon as we return from user code.
- Every time data is successfully sent to the client, the timeout is tickled.

Constructors

Connection |

Fields

connSendMany :: [ByteString] -> IO ()
connSendAll :: ByteString -> IO ()
connSendFile :: FilePath -> Integer -> Integer -> IO () -> [ByteString] -> Cleaner -> IO ()

  offset, length

connClose :: IO ()
connRecv :: IO ByteString

runSettingsConnection :: Settings -> IO (Connection, SockAddr) -> Application -> IO ()Source

runSettingsConnectionMaker :: Settings -> IO (IO Connection, SockAddr) -> Application -> IO ()Source

Allows you to provide a function which will return a `Connection`. In cases where creating the `Connection` can be expensive, this allows the expensive computations to be performed in a separate thread instead of the main server loop.

Since 1.3.5

# Datatypes

type Port = Int Source

TCP port number

data InvalidRequest Source

Constructors

NotEnoughLines [String] |
BadFirstLine String |
NonHttp |
IncompleteHeaders |
ConnectionClosedByPeer |
OverLargeHeader |

Instances

Eq InvalidRequest |
Show InvalidRequest |
Typeable InvalidRequest |
Exception InvalidRequest |

# Internal (Manager)

data Manager Source

A timeout manager

data Handle Source

A handle used by `Manager`

First field is action to be performed on timeout.

initialize :: Int -> IO Manager Source

withManager Source

Arguments

:: Int | timeout in microseconds
-> (Manager -> IO a) |
-> IO a |

Call the inner function with a timeout manager.

register :: Manager -> IO () -> IO Handle Source

registerKillThread :: Manager -> IO Handle Source

pause :: Handle -> IO ()Source

resume :: Handle -> IO ()Source

cancel :: Handle -> IO ()Source

# Internal

parseRequest :: Connection -> Port -> SockAddr -> Source (ResourceT IO) ByteString -> ResourceT IO (Request, IO (ResumableSource (ResourceT IO) ByteString))Source

sendResponse :: Settings -> Cleaner -> Request -> Connection -> Response -> ResourceT IO Bool Source

dummyCleaner :: CleanerSource

A dummy `Cleaner`, intended for applications making use of the low-level request parsing and rendering functions.

Since 1.3.4

socketConnection :: Socket -> Connection Source

Default action value for `Connection`

# Misc

warpVersion :: String Source