Portability | not portable, uses 2-rank types |
---|---|
Stability | experimental |
Maintainer | johan.tibell@gmail.com |
Defines the interface implemented by all web applications.
Example application:
{-# LANGUAGE Rank2Types, ImpredicativeTypes #-} module Main where
import qualified Data.ByteString as S import qualified Data.ByteString.Char8 as C (pack, unpack) import Hyena.Server import Network.Wai (Application, Enumerator, pathInfo) import System.Directory (getCurrentDirectory) import System.FilePath ((</>), makeRelative) import System.IO
sendFile :: FilePath -> IO Enumerator sendFile path = do h <- openBinaryFile path ReadMode let yieldBlock f z = do block <- S.hGetNonBlocking h 1024 if S.null block then hClose h >> return z else do z' <- f z block case z' of Left z'' -> hClose h >> return z'' Right z'' -> yieldBlock f z'' return yieldBlock
fileServer :: Application fileServer environ = do cwd <- getCurrentDirectory let path = (cwd </> makeRelative "/" (C.unpack $ pathInfo environ)) size <- getFileSize path -- Here you should add security checks, etc. let contentType = (C.pack "Content-Type", C.pack "text/plain") contentLength = (C.pack "Content-Length", C.pack (show size)) enumerator <- sendFile path return (200, C.pack "OK", [contentType,contentLength], enumerator)
getFileSize :: String -> IO Integer getFileSize fn = withFile fn ReadMode hFileSize
main :: IO () main = serve fileServer
- type Application = Environment -> IO (Int, ByteString, Headers, Enumerator)
- type Enumerator = forall a. (a -> ByteString -> IO (Either a a)) -> a -> IO a
- data Environment = Environment {
- requestMethod :: Method
- scriptName :: ByteString
- pathInfo :: ByteString
- queryString :: Maybe ByteString
- requestProtocol :: (Int, Int)
- headers :: Headers
- input :: Enumerator
- errors :: String -> IO ()
- type Headers = [(ByteString, ByteString)]
- data Method
The Application type
type Application = Environment -> IO (Int, ByteString, Headers, Enumerator)Source
An application takes an environment and returns a HTTP status
code, a sequence of headers and an Enumerator
containing the
response body.
type Enumerator = forall a. (a -> ByteString -> IO (Either a a)) -> a -> IO aSource
A left-fold enumerator.
data Environment Source
An environment providing information regarding the request.
Environment | |
|
type Headers = [(ByteString, ByteString)]Source
The HTTP request headers.