happstack-server-6.2.5: Web related tools and services.

Happstack.Server.Routing

Contents

Description

Route an incoming Request to a handler. For more in-depth documentation see this section of the Happstack Crash Course: http://happstack.com/docs/crashcourse/RouteFilters.html

Synopsis

Route by request method

methodM :: (ServerMonad m, MonadPlus m, MatchMethod method) => method -> m ()

Guard against the method. This function also guards against *any remaining path segments*. See methodOnly for the version that guards only by method.

Example:

 handler :: ServerPart Response
 handler =
     do methodM [GET, HEAD]
        ...

methodOnly :: (ServerMonad m, MonadPlus m, MatchMethod method) => method -> m ()

Guard against the method only (as opposed to methodM).

Example:

 handler :: ServerPart Response
 handler =
     do methodOnly [GET, HEAD]
        ...

methodSP :: (ServerMonad m, MonadPlus m, MatchMethod method) => method -> m b -> m b

Guard against the method. Note, this function also guards against any remaining path segments. Similar to methodM but with a different type signature.

Example:

 handler :: ServerPart Response
 handler = methodSP [GET, HEAD] $ subHandler

method :: (MatchMethod method, Monad m) => method -> WebT m a -> ServerPartT m a

Guard against the method. Note, this function also guards against any remaining path segments.

DEPRECATED: Use methodSP, methodM, or methodOnly

class MatchMethod m where

instances of this class provide a variety of ways to match on the Request method.

Examples

 methodM GET                  -- match GET
 methodM [HEAD, GET]          -- match HEAD or GET
 methodM (not . (==) DELETE)  -- match any method except DELETE
 methodM ()                   -- match any method

Methods

matchMethod :: m -> Method -> Bool

Route by pathInfo

dir :: (ServerMonad m, MonadPlus m) => String -> m a -> m a

Pop a path element and run the supplied handler if it matches the given string.

 handler :: ServerPart Response
 handler = dir "foo" $ dir "bar" $ subHandler

The path element can not contain '/'. See also dirs.

dirs :: (ServerMonad m, MonadPlus m) => FilePath -> m a -> m a

Guard against a FilePath. Unlike dir the FilePath may contain '/'. If the guard succeeds, the matched elements will be popped from the directory stack.

 dirs "foo/bar" $ ...

See also: dir.

nullDir :: (ServerMonad m, MonadPlus m) => m ()

guard which only succeeds if there are no remaining path segments

Often used if you want to explicitly assign a route for /

trailingSlash :: (ServerMonad m, MonadPlus m) => m ()

Guard which checks that the Request URI ends in '/'. Useful for distinguishing between foo and foo/

anyPath :: (ServerMonad m, MonadPlus m) => m r -> m r

Pop any path element and run the handler.

Succeeds if a path component was popped. Fails is the remaining path was empty.

class FromReqURI a where

This class is used by path to parse a path component into a value.

The instances for number types (Int, Float, etc) use readM to parse the path component.

The instance for String, on the other hand, returns the unmodified path component.

See the following section of the Happstack Crash Course for detailed instructions using and extending FromReqURI:

http://www.happstack.com/docs/crashcourse/RouteFilters.html#FromReqURI

Methods

fromReqURI :: String -> Maybe a

path :: (FromReqURI a, MonadPlus m, ServerMonad m) => (a -> m b) -> m b

Pop a path element and parse it using the fromReqURI in the FromReqURI class.

uriRest :: ServerMonad m => (String -> m a) -> m a

Grab the rest of the URL (dirs + query) and passes it to your handler.

Route by host

host :: (ServerMonad m, MonadPlus m) => String -> m a -> m a

Guard against the host.

This matches against the host header specified in the incoming Request.

Can be used to support virtual hosting, http://en.wikipedia.org/wiki/Virtual_hosting

see also: withHost

withHost :: (ServerMonad m, MonadPlus m) => (String -> m a) -> m a

Lookup the host header in the incoming request and pass it to the handler.

see also: host

Route by (Request -> Bool)

guardRq :: (ServerMonad m, MonadPlus m) => (Request -> Bool) -> m ()

Guard using an arbitrary function on the Request.