Network.Wai
Contents
Description
This module defines a generic web application interface. It is a common protocol between web servers and web applications.
The overriding design principles here are performance and generality . To
address performance, this library is built on Source
for the request body and
Enumerator
for the response bodies. The advantages of this approach over lazy
IO have been debated elsewhere.
Nonetheless, many people find these data structures difficult to work with. For that reason, this library includes the Network.Wai.Enumerator module to provide more familiar abstractions, including lazy IO.
Generality is achieved by removing many variables commonly found in similar
projects that are not universal to all servers. The goal is that the Request
object contains only data which is meaningful in all circumstances.
A final note: please remember when using this package that, while your application may compile without a hitch against many different servers, there are other considerations to be taken when moving to a new backend. For example, if you transfer from a CGI application to a FastCGI one, you might suddenly find you have a memory leak. Conversely, a FastCGI application would be well served to preload all templates from disk when first starting; this would kill the performance of a CGI application.
- type Method = ByteString
- type HttpVersion = ByteString
- http09 :: HttpVersion
- http10 :: HttpVersion
- http11 :: HttpVersion
- data CIByteString = CIByteString {}
- mkCIByteString :: ByteString -> CIByteString
- type RequestHeader = CIByteString
- type RequestHeaders = [(RequestHeader, ByteString)]
- type ResponseHeader = CIByteString
- type ResponseHeaders = [(ResponseHeader, ByteString)]
- data Status = Status {}
- status200 :: Status
- status301 :: Status
- status302 :: Status
- status303 :: Status
- status400 :: Status
- status401 :: Status
- status403 :: Status
- status404 :: Status
- status405 :: Status
- status500 :: Status
- data Request = Request {
- requestMethod :: Method
- httpVersion :: HttpVersion
- pathInfo :: ByteString
- queryString :: ByteString
- serverName :: ByteString
- serverPort :: Int
- requestHeaders :: [(RequestHeader, ByteString)]
- isSecure :: Bool
- errorHandler :: String -> IO ()
- remoteHost :: SockAddr
- data Response
- type ResponseEnumerator a = (Status -> ResponseHeaders -> Iteratee Builder IO a) -> IO a
- responseEnumerator :: Response -> ResponseEnumerator a
- type Application = Request -> Iteratee ByteString IO Response
- type Middleware = Application -> Application
- responseLBS :: Status -> ResponseHeaders -> ByteString -> Response
Data types
Request method
type Method = ByteStringSource
HTTP request method. Since the HTTP protocol allows arbitrary request
methods, we leave this open as a ByteString
. Please note the request
methods are case-sensitive.
HTTP protocol versions
type HttpVersion = ByteStringSource
Version of HTTP protocol used in current request. The value given here should be everything following the "HTTP/" line in a request. In other words, HTTP/1.1 -> "1.1", HTTP/1.0 -> "1.0".
HTTP/0.9
HTTP/1.0
HTTP/1.1
Case-insensitive byte strings
data CIByteString Source
A case insensitive bytestring, where the Eq
and Ord
instances do
comparisons based on the lower-cased version of this string. For efficiency,
this datatype contains both the original and lower-case version of the
string; this means there is no need to lower-case the bytestring for every
comparison.
Please note that this datatype has an IsString
instance, which can allow
for very concise code when using the OverloadedStrings language extension.
Constructors
CIByteString | |
Fields
|
mkCIByteString :: ByteString -> CIByteStringSource
Convert a regular bytestring to a case-insensitive bytestring.
Request header names
type RequestHeader = CIByteStringSource
Headers sent from the client to the server. Note that this is a case-insensitive string, as the HTTP spec specifies.
type RequestHeaders = [(RequestHeader, ByteString)]Source
Response header names
type ResponseHeader = CIByteStringSource
Headers sent from the server to the client. Note that this is a case-insensitive string, as the HTTP spec specifies.
type ResponseHeaders = [(ResponseHeader, ByteString)]Source
Response status code
HTTP status code; a combination of the integral code and a status message. Equality is determined solely on the basis of the integral code.
Constructors
Status | |
Fields
|
WAI interface
Information on the request sent by the client. This abstracts away the details of the underlying implementation.
Constructors
Request | |
Fields
|
Constructors
ResponseFile Status ResponseHeaders FilePath | |
ResponseBuilder Status ResponseHeaders Builder | |
ResponseEnumerator (forall a. ResponseEnumerator a) |
type ResponseEnumerator a = (Status -> ResponseHeaders -> Iteratee Builder IO a) -> IO aSource
type Application = Request -> Iteratee ByteString IO ResponseSource
type Middleware = Application -> ApplicationSource
Middleware is a component that sits between the server and application. It can do such tasks as GZIP encoding or response caching. What follows is the general definition of middleware, though a middleware author should feel free to modify this.
As an example of an alternate type for middleware, suppose you write a function to load up session information. The session information is simply a string map [(String, String)]. A logical type signatures for this middleware might be:
loadSession :: ([(String, String)] -> Application) -> Application
Here, instead of taking a standard Application
as its first argument, the
middleware takes a function which consumes the session information as well.
Response body smart constructors
responseLBS :: Status -> ResponseHeaders -> ByteString -> ResponseSource