Safe Haskell | None |
---|---|
Language | Haskell98 |
Network.Wai.Handler.Warp
Contents
Description
A fast, light-weight HTTP server handler for WAI.
- run :: Port -> Application -> IO ()
- runSettings :: Settings -> Application -> IO ()
- runSettingsSocket :: Settings -> Socket -> Application -> IO ()
- runSettingsConnection :: Settings -> IO (Connection, SockAddr) -> Application -> IO ()
- runSettingsConnectionMaker :: Settings -> IO (IO Connection, SockAddr) -> Application -> IO ()
- runSettingsConnectionMakerSecure :: Settings -> IO (IO (Connection, Bool), SockAddr) -> Application -> IO ()
- data Settings
- defaultSettings :: Settings
- setPort :: Int -> Settings -> Settings
- setHost :: HostPreference -> Settings -> Settings
- setOnException :: (Maybe Request -> SomeException -> IO ()) -> Settings -> Settings
- setOnExceptionResponse :: (SomeException -> Response) -> Settings -> Settings
- setOnOpen :: (SockAddr -> IO Bool) -> Settings -> Settings
- setOnClose :: (SockAddr -> IO ()) -> Settings -> Settings
- setTimeout :: Int -> Settings -> Settings
- setManager :: Manager -> Settings -> Settings
- setFdCacheDuration :: Int -> Settings -> Settings
- setBeforeMainLoop :: IO () -> Settings -> Settings
- setNoParsePath :: Bool -> Settings -> Settings
- getPort :: Settings -> Int
- getHost :: Settings -> HostPreference
- settingsPort :: Settings -> Int
- settingsHost :: Settings -> HostPreference
- settingsOnException :: Settings -> Maybe Request -> SomeException -> IO ()
- settingsOnExceptionResponse :: Settings -> SomeException -> Response
- settingsOnOpen :: Settings -> SockAddr -> IO Bool
- settingsOnClose :: Settings -> SockAddr -> IO ()
- settingsTimeout :: Settings -> Int
- settingsManager :: Settings -> Maybe Manager
- settingsFdCacheDuration :: Settings -> Int
- settingsBeforeMainLoop :: Settings -> IO ()
- settingsNoParsePath :: Settings -> Bool
- exceptionResponseForDebug :: SomeException -> Response
- defaultShouldDisplayException :: SomeException -> Bool
- data HostPreference :: *
- type Port = Int
- data InvalidRequest
- data ConnSendFileOverride
- data Connection = Connection {
- connSendMany :: [ByteString] -> IO ()
- connSendAll :: ByteString -> IO ()
- connSendFile :: FilePath -> Integer -> Integer -> IO () -> [ByteString] -> IO ()
- connClose :: IO ()
- connRecv :: IO ByteString
- connReadBuffer :: Buffer
- connWriteBuffer :: Buffer
- connBufferSize :: BufSize
- connSendFileOverride :: ConnSendFileOverride
- socketConnection :: Socket -> IO Connection
- warpVersion :: String
- data InternalInfo = InternalInfo {}
- type HeaderValue = ByteString
- type IndexedHeader = Array Int (Maybe HeaderValue)
- requestMaxIndex :: Int
- module Network.Wai.Handler.Warp.Timeout
- withFdCache :: Int -> (Maybe MutableFdCache -> IO a) -> IO a
- getFd :: MutableFdCache -> FilePath -> IO (Fd, Refresh)
- data MutableFdCache
- type Refresh = IO ()
- withDateCache :: (DateCache -> IO a) -> IO a
- getDate :: DateCache -> IO GMTDate
- data DateCache
- type GMTDate = ByteString
- recvRequest :: Settings -> Connection -> InternalInfo -> SockAddr -> Source -> IO (Request, IndexedHeader)
- sendResponse :: Connection -> InternalInfo -> Request -> IndexedHeader -> IO ByteString -> Response -> IO Bool
Run a Warp server
run :: Port -> Application -> IO ()
Run an Application
on the given port. This calls runSettings
with
defaultSettings
.
runSettings :: Settings -> Application -> IO ()
Run an Application
with the given Settings
.
runSettingsSocket :: Settings -> Socket -> Application -> IO ()
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.
runSettingsConnection :: Settings -> IO (Connection, SockAddr) -> Application -> IO ()
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
runSettingsConnectionMaker :: Settings -> IO (IO Connection, SockAddr) -> Application -> IO ()
runSettingsConnectionMakerSecure :: Settings -> IO (IO (Connection, Bool), SockAddr) -> Application -> IO ()
Allows you to provide a function which will return a function
which will return Connection
.
Since 2.1.4
Settings
data 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
setHost :: HostPreference -> Settings -> Settings
Interface to bind to. Default value: HostIPv4
Since 2.1.0
setOnException :: (Maybe Request -> SomeException -> IO ()) -> Settings -> Settings
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.
Since 2.1.0
setOnExceptionResponse :: (SomeException -> Response) -> Settings -> Settings
A function to create a Response
when an exception occurs.
Default: 500, text/plain, "Something went wrong"
Since 2.1.0
setOnClose :: (SockAddr -> IO ()) -> Settings -> Settings
What to do when a connection is closed. Default: do nothing.
Since 2.1.0
setTimeout :: Int -> Settings -> Settings
Timeout value in seconds. Default value: 30
Since 2.1.0
setManager :: Manager -> Settings -> Settings
Use an existing timeout manager instead of spawning a new one. If used,
settingsTimeout
is ignored.
Since 2.1.0
setFdCacheDuration :: Int -> Settings -> Settings
Cache duration time of file descriptors in seconds. 0 means that the cache mechanism is not used. Default value: 10
setBeforeMainLoop :: 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
setNoParsePath :: Bool -> Settings -> Settings
Perform no parsing on the rawPathInfo.
This is useful for writing HTTP proxies.
Default: False
Since 2.1.0
Getters
getHost :: Settings -> HostPreference
Get the interface to bind to.
Since 2.1.1
Accessors
Note: these accessors are deprecated, please use the set
versions instead.
settingsPort :: Settings -> Int
Deprecated: Use setPort instead
Port to listen on. Default value: 3000
settingsHost :: Settings -> HostPreference
Deprecated: Use setHost instead
Default value: HostIPv4
settingsOnException :: Settings -> Maybe Request -> SomeException -> IO ()
Deprecated: Use setOnException instead
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.
settingsOnExceptionResponse :: Settings -> SomeException -> Response
Deprecated: Use setOnExceptionResponse instead
A function to create Response
when an exception occurs.
Default: 500, text/plain, "Something went wrong"
Since 2.0.3
settingsOnOpen :: Settings -> SockAddr -> IO Bool
settingsOnClose :: Settings -> SockAddr -> IO ()
Deprecated: Use setOnClose instead
What to do when a connection is close. Default: do nothing.
settingsTimeout :: Settings -> Int
Deprecated: Use setTimeout instead
Timeout value in seconds. Default value: 30
settingsManager :: Settings -> Maybe Manager
Deprecated: Use setManager instead
Use an existing timeout manager instead of spawning a new one. If used, settingsTimeout
is ignored. Default is Nothing
settingsFdCacheDuration :: Settings -> Int
Deprecated: Use setFdCacheDuration instead
Cache duratoin time of file descriptors in seconds. 0 means that the cache mechanism is not used. Default value: 10
settingsBeforeMainLoop :: Settings -> IO ()
Deprecated: Use setBeforeMainLoop instead
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
settingsNoParsePath :: Settings -> Bool
Deprecated: Use setNoParsePath instead
Perform no parsing on the rawPathInfo.
This is useful for writing HTTP proxies.
Default: False
Since 2.0.3
Debugging
exceptionResponseForDebug :: SomeException -> Response
Default implementation of settingsOnExceptionResponse
for the debugging purpose. 500, text/plain, a showed exception.
defaultShouldDisplayException :: SomeException -> Bool
Apply the logic provided by defaultExceptionHandler
to determine if an
exception should be shown or not. The goal is to hide exceptions which occur
under the normal course of the web server running.
Since 2.1.3
Data types
data HostPreference :: *
Which host to bind.
Note: The IsString
instance recognizes the following special values:
*
meansHostAny
*4
meansHostIPv4
!4
meansHostIPv4Only
*6
meansHostIPv6
!6
meansHostIPv6Only
data InvalidRequest
Error types for bad Request
.
data ConnSendFileOverride
Whether or not ConnSendFileOverride
in Connection
can be
overridden. This is a kind of hack to keep the signature of
Connection
clean.
Constructors
NotOverride | Don't override |
Override Socket | Override with this |
Connection
data Connection
Data type to manipulate IO actions for connections.
Constructors
Connection | |
Fields
|
socketConnection :: Socket -> IO Connection
Default action value for Connection
.
Internal
Version
The version of Warp.
Data types
type HeaderValue = ByteString
The type for header value used with HeaderName
.
type IndexedHeader = Array Int (Maybe HeaderValue)
Array for a set of HTTP headers.
The size for IndexedHeader
for HTTP Request.
From 0 to this corresponds to "Content-Length", "Transfer-Encoding",
"Expect", "Connection", "Range", and "Host".
Time out manager
File descriptor cache
withFdCache :: Int -> (Maybe MutableFdCache -> IO a) -> IO a
Creating MutableFdCache
and executing the action in the second
argument. The first argument is a cache duration in second.
data MutableFdCache
Mutable Fd cacher.
Date
withDateCache :: (DateCache -> IO a) -> IO a
Creating DateCache
and executing the action.
data DateCache
The type of the cache of the Date header value.
type GMTDate = ByteString
The type of the Date header value.
Request and response
Arguments
:: Settings | |
-> Connection | |
-> InternalInfo | |
-> SockAddr | Peer's address. |
-> Source | Where HTTP request comes from. |
-> IO (Request, IndexedHeader) |
|
Receiving a HTTP request from Connection
and parsing its header
to create Request
.
Arguments
:: Connection | |
-> InternalInfo | |
-> Request | HTTP request. |
-> IndexedHeader | Indexed header of HTTP request. |
-> IO ByteString | source from client, for raw response |
-> Response | HTTP response including status code and response header. |
-> IO Bool | Returing True if the connection is persistent. |
Sending a HTTP response to Connection
according to Response
.
Applications/middlewares MUST specify a proper ResponseHeaders
.
so that inconsistency does not happen.
No header is deleted by this function.
Especially, Applications/middlewares MUST take care of Content-Length, Content-Range, and Transfer-Encoding because they are inserted, when necessary, regardless they already exist. This function does not insert Content-Encoding. It's middleware's responsibility.
The Date and Server header is added if not exist in HTTP response header.
There are three basic APIs to create Response
:
responseFile
::Status
->ResponseHeaders
->FilePath
->Maybe
FilePart
->Response
- HTTP response body is sent by sendfile().
Applications are categorized into simple and sophisticated.
Simple applications should specify
Nothing
toMaybe
FilePart
. The size of the specified file is obtained by disk access. Then Range is handled. Sophisticated applications should specifyJust
toMaybe
FilePart
. They should treat Range (and If-Range) by thierselves. In both cases, Content-Length and Content-Range (if necessary) are automatically added into the HTTP response header. If Content-Length and Content-Range exist in the HTTP response header, they would cause inconsistency. Status is also changed to 206 if necessary. responseBuilder
::Status
->ResponseHeaders
->Builder
->Response
- HTTP response body is created from
Source
. Typically, Transfer-Encoding: chunked is used. If Content-Length is specified, Transfer-Encoding: chunked is not used. responseSource
::Status
->ResponseHeaders
->Source
IO
(Flush
Builder
) ->Response
- HTTP response body is created from
Builder
. Typically, Transfer-Encoding: chunked is used. If Content-Length is specified, Transfer-Encoding: chunked is not used.