Safe Haskell | None |
---|---|
Language | Haskell2010 |
- type RoutingApplication = Request -> (RouteResult Response -> IO ResponseReceived) -> IO ResponseReceived
- data RouteResult a
- = Fail ServantErr
- | FailFatal !ServantErr
- | Route !a
- toApplication :: RoutingApplication -> Application
- data Delayed env c where
- newtype DelayedIO a = DelayedIO {
- runDelayedIO :: Request -> IO (RouteResult a)
- emptyDelayed :: RouteResult a -> Delayed env a
- delayedFail :: ServantErr -> DelayedIO a
- delayedFailFatal :: ServantErr -> DelayedIO a
- withRequest :: (Request -> DelayedIO a) -> DelayedIO a
- addCapture :: Delayed env (a -> b) -> (captured -> DelayedIO a) -> Delayed (captured, env) b
- addMethodCheck :: Delayed env a -> DelayedIO () -> Delayed env a
- addAuthCheck :: Delayed env (a -> b) -> DelayedIO a -> Delayed env b
- addBodyCheck :: Delayed env (a -> b) -> DelayedIO a -> Delayed env b
- addAcceptCheck :: Delayed env a -> DelayedIO () -> Delayed env a
- passToServer :: Delayed env (a -> b) -> (Request -> a) -> Delayed env b
- runDelayed :: Delayed env a -> env -> Request -> IO (RouteResult a)
- runAction :: Delayed env (Handler a) -> env -> Request -> (RouteResult Response -> IO r) -> (a -> RouteResult Response) -> IO r
Documentation
type RoutingApplication Source #
= Request | the request, the field |
-> (RouteResult Response -> IO ResponseReceived) | |
-> IO ResponseReceived |
data RouteResult a Source #
The result of matching against a path in the route tree.
Fail ServantErr | Keep trying other paths. The |
FailFatal !ServantErr | Don't try other paths. |
Route !a |
Functor RouteResult Source # | |
Eq a => Eq (RouteResult a) Source # | |
Read a => Read (RouteResult a) Source # | |
Show a => Show (RouteResult a) Source # | |
data Delayed env c where Source #
A Delayed
is a representation of a handler with scheduled
delayed checks that can trigger errors.
Why would we want to delay checks?
There are two reasons:
- In a straight-forward implementation, the order in which we perform checks will determine the error we generate. This is because once an error occurs, we would abort and not perform any subsequent checks, but rather return the current error.
This is not a necessity: we could continue doing other checks, and choose the preferred error. However, that would in general mean more checking, which leads us to the other reason.
- We really want to avoid doing certain checks too early. For example, captures involve parsing, and are much more costly than static route matches. In particular, if several paths contain the "same" capture, we'd like as much as possible to avoid trying the same parse many times. Also tricky is the request body. Again, this involves parsing, but also, WAI makes obtaining the request body a side-effecting operation. We could/can work around this by manually caching the request body, but we'd rather keep the number of times we actually try to decode the request body to an absolute minimum.
We prefer to have the following relative priorities of error codes:
404 405 (bad method) 401 (unauthorized) 415 (unsupported media type) 400 (bad request) 406 (not acceptable)
Therefore, while routing, we delay most checks so that they will ultimately occur in the right order.
A Delayed
contains three delayed blocks of tests, and
the actual handler:
- Delayed captures. These can actually cause 404, and while they're costly, they should be done first among the delayed checks (at least as long as we do not decouple the check order from the error reporting, see above). Delayed captures can provide inputs to the actual handler.
- Method check(s). This can cause a 405. On success, it does not provide an input for the handler. Method checks are comparatively cheap.
- Body and accept header checks. The request body check can cause both 400 and 415. This provides an input to the handler. The accept header check can be performed as the final computation in this block. It can cause a 406.
Computations used in a Delayed
can depend on the
incoming Request
, may perform 'IO, and result in a
'RouteResult, meaning they can either suceed, fail
(with the possibility to recover), or fail fatally.
DelayedIO | |
|
emptyDelayed :: RouteResult a -> Delayed env a Source #
A Delayed
without any stored checks.
delayedFail :: ServantErr -> DelayedIO a Source #
Fail with the option to recover.
delayedFailFatal :: ServantErr -> DelayedIO a Source #
Fail fatally, i.e., without any option to recover.
addCapture :: Delayed env (a -> b) -> (captured -> DelayedIO a) -> Delayed (captured, env) b Source #
Add a capture to the end of the capture block.
addMethodCheck :: Delayed env a -> DelayedIO () -> Delayed env a Source #
Add a method check to the end of the method block.
addAuthCheck :: Delayed env (a -> b) -> DelayedIO a -> Delayed env b Source #
Add an auth check to the end of the auth block.
addBodyCheck :: Delayed env (a -> b) -> DelayedIO a -> Delayed env b Source #
Add a body check to the end of the body block.
addAcceptCheck :: Delayed env a -> DelayedIO () -> Delayed env a Source #
Add an accept header check to the beginning of the body block. There is a tradeoff here. In principle, we'd like to take a bad body (400) response take precedence over a failed accept check (406). BUT to allow streaming the body, we cannot run the body check and then still backtrack. We therefore do the accept check before the body check, when we can still backtrack. There are other solutions to this, but they'd be more complicated (such as delaying the body check further so that it can still be run in a situation where we'd otherwise report 406).
passToServer :: Delayed env (a -> b) -> (Request -> a) -> Delayed env b Source #
Many combinators extract information that is passed to
the handler without the possibility of failure. In such a
case, passToServer
can be used.
runDelayed :: Delayed env a -> env -> Request -> IO (RouteResult a) Source #
Run a delayed server. Performs all scheduled operations in order, and passes the results from the capture and body blocks on to the actual handler.
This should only be called once per request; otherwise the guarantees about effect and HTTP error ordering break down.
runAction :: Delayed env (Handler a) -> env -> Request -> (RouteResult Response -> IO r) -> (a -> RouteResult Response) -> IO r Source #
Runs a delayed server and the resulting action. Takes a continuation that lets us send a response. Also takes a continuation for how to turn the result of the delayed server into a response.