wai

Web Application Interface. https://github.com/yesodweb/wai

Version on this page:3.0.3.0
LTS Haskell 9.9:3.2.1.1
Stackage Nightly 2017-10-18:3.2.1.1
Latest on Hackage:3.2.1.1
MIT licensed by Michael Snoyman
Maintained by michael@snoyman.com

Module documentation for 3.0.3.0

WAI: Web Application Interface

Getting started

You want a minimal example? Here it is!

{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.HTTP.Types
import Network.Wai.Handler.Warp (run)

app :: Application
app _ respond = do
    putStrLn "I've done some IO here"
    respond $ responseLBS
        status200
        [("Content-Type", "text/plain")]
        "Hello, Web!"

main :: IO ()
main = do
    putStrLn $ "http://localhost:8080/"
    run 8080 app

Put that code into a file named hello.hs and install wai and warp from Hackage:

cabal install wai warp

Run it:

runhaskell hello.hs

Point your browser to:

http://localhost:8080/

Serving static content

We can modify our previous example to serve static content. For this create a file named index.html:

<p>Hello, Web!</p>

Now we redefine responseBody to refer to that file:

app2 :: Application
app2 _ respond = respond index

index :: Response
index = responseFile
    status200
    [("Content-Type", "text/html")]
    "index.html"
    Nothing

Basic dispatching

An Application maps Requests to Responses:

ghci> :info  Application
type Application = Request -> IO Response

Depending on the path info provided with each Request we can serve different Responses:

app3 :: Application
app3 request respond = respond $ case rawPathInfo request of
    "/"     -> index
    "/raw/" -> plainIndex
    _       -> notFound

plainIndex :: Response
plainIndex = responseFile
    status200
    [("Content-Type", "text/plain")]
    "index.html"
    Nothing

notFound :: Response
notFound = responseLBS
    status404
    [("Content-Type", "text/plain")]
    "404 - Not Found"

Doing without overloaded strings

For the sake of efficiency, WAI uses the bytestring package. We used GHCs overloaded strings to almost hide this fact. But we can easily do without. What follows is a more verbose definition of notFound, that works without GHC extensions:

import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy.Char8 as LB8
import           Data.CaseInsensitive (mk)

notFound = responseLBS
    status404
    [(mk $ B8.pack "Content-Type", B8.pack "text/plain")]
    (LB8.pack "404 - Not Found")

Changes

3.2.1.1

  • Relax upper bound on bytestring-builder

3.2.1

  • add mapResponseStatus #532

3.2.0.1

  • Add missing changelog entry

3.2.0

  • Major version up due to breaking changes. We chose 3.2.0, not 3.1.0 for consistency with Warp 3.2.0.
  • The Network.Wai.HTTP2 module was removed.
  • tryGetFileSize, hContentRange, hAcceptRanges, contentRangeHeader and chooseFilePart, adjustForFilePart and parseByteRanges were removed from the Network.Wai.Internal module.
  • New fields for Request: requestHeaderReferer and requestHeaderUserAgent.

3.0.5.0

  • Avoid using the IsString Builder instance

3.0.4.0

  • A new module Network.Wai.HTTP2 is exported.

3.0.3.0

  • mapResponseHeaders, ifRequest and modifyResponse are exported.

3.0.2.3

  • Allow blaze-builder 0.4

3.0.2.2

Used by 308 packages:
airbrake, airship, apiary, apiary-authenticate, apiary-cookie, apiary-helics, apiary-http-client, apiary-session, avers-server, bake, batchd, bimap-server, blank-canvas, blunt, breve, canteven-http, cerberus, cmdargs-browser, colorless-scotty, coltrane, curryer, debug-me, digestive-functors-scotty, dingo-core, diplomacy-server, duplo, ekg-wai, engine-io-growler, engine-io-wai, engine-io-yesod, eros-http, ethereum-analyzer-webui, eventsourced, fbmessenger-api, feed-translator, ficketed, filter-logger, firefly, firefly-example, fluid-idl-scotty, fn, fn-extra, gc-monitoring-wai, ghclive, git-annex, github-post-receive, google-oauth2-for-cli, gopher-proxy, growler, gssapi-wai, hack2-interface-wai, hackage-proxy, hails, hairy, hakyll, haskbot-core, haskell-kubernetes, haskell-tools-demo, haste-app, helics-wai, helix, herringbone-wai, hexpress, heyefi, hipbot, hledger-api, hledger-web, hoauth2, hoogle, hslogstash, hsoz, hspec-wai, hs-server-starter, http-pony-serve-wai, http-proxy, http-reverse-proxy, hums, hunt-server, hylide, instapaper-sender, IPv6DB, jsaddle-warp, juandelacosa, kawaii, keter, keysafe, lambdacms-core, legion, legion-discovery, linden, line, linklater, lio-simple, logentries, log-utils, ltiv1p1, magicbane, mangopay, marvin, mellon-web, memis, MFlow, mighttpd2, miku, mywatch, nero-wai, network-wai-router, nicovideo-translator, nomyx-api, notmuch-web, on-a-horse, pansite, paprika, paranoia, paypal-api, persona-idp, pipes-wai, postgrest, postgrest-ws, prefork, prometheus, prometheus-effect, prove-everywhere-server, purescript, quenya-verb, ratel-wai, raven-haskell-scotty, reload, reqcatcher, respond, rest-wai, schedule-planner, scottish, scotty, scotty-blaze, scotty-fay, scotty-hastache, scotty-resource, scotty-rest, scotty-session, scotty-tls, seacat, sensei, servant-auth-cookie, servant-auth-hmac, servant-auth-server, servant-auth-token, servant-checked-exceptions, servant-ekg, servant-examples, servant-github-webhook, servant-JuicyPixels, servant-mock, servant-multipart, servant-options, servant-py, servant-rawm, servant-server, servant-static-th, servant-subscriber, servant-websockets, servant-zeppelin-server, server-generic, serversession-frontend-wai, serversession-frontend-yesod, services, servius, serv-wai, simple, simple-session, skell, socketed, socketio, socketson, solga, space, Spock, Spock-core, Spock-digestive, sprinkles, sproxy2, sproxy-web, sssp, stm-firehose, streaming-wai, strelka-wai, sws, t3-server, tamarin-prover, test-sandbox-compose, thentos-cookie-session, tightrope, tkyprof, tpb, trasa-server, vigilance, wai-accept-language, wai-app-file-cgi, wai-app-static, wai-cli, wai-conduit, wai-cors, wai-devel, wai-digestive-functors, wai-dispatch, wai-eventsource, wai-extra, wai-frontend-monadcgi, wai-graceful, wai-handler-devel, wai-handler-fastcgi, wai-handler-launch, wai-handler-scgi, wai-handler-snap, wai-handler-webkit, wai-hastache, wai-hmac-auth, wai-http2-extra, wai-lens, wai-lite, wai-logger, wai-logger-prefork, wai-make-assets, wai-middleware-auth, wai-middleware-cache, wai-middleware-cache-redis, wai-middleware-caching, wai-middleware-caching-lru, wai-middleware-caching-redis, wai-middleware-catch, wai-middleware-consul, wai-middleware-content-type, wai-middleware-crowd, wai-middleware-etag, wai-middleware-gunzip, wai-middleware-headers, wai-middleware-hmac, wai-middleware-json-errors, wai-middleware-metrics, wai-middleware-preprocessor, wai-middleware-prometheus, wai-middleware-rollbar, wai-middleware-route, wai-middleware-static, wai-middleware-static-caching, wai-middleware-static-embedded, wai-middleware-throttle, wai-middleware-verbs, wai-predicates, wai-request-spec, wai-responsible, wai-route, wai-router, wai-routes, wai-routing, wai-secure-cookies, wai-session, wai-session-mysql, wai-session-postgresql, wai-slack-middleware, wai-static-cache, wai-static-pages, wai-test, wai-thrift, wai-throttler, waitra, wai-transformers, wai-util, wai-websockets, waldo, warp, warp-dynamic, warp-tls, warp-tls-uid, webapi, webapp, webcrank-wai, web-inv-route, web-routes-wai, webwire, welshy, werewolf-slack, Wheb, Yablog, yackage, yesod, yesod-auth, yesod-auth-basic, yesod-auth-fb, yesod-auth-zendesk, yesod-bin, yesod-comments, yesod-core, yesod-crud-persist, yesod-eventsource, yesod-fb, yesod-form, yesod-gitrepo, yesod-mangopay, yesod-platform, yesod-raml-bin, yesod-recaptcha, yesod-session-redis, yesod-static, yesod-test, yesod-test-json, yesod-tls, yesod-websockets
comments powered byDisqus