On this page:

2.17 Wrapping Requests & Responses🔗

The web-server/dispatchers/dispatch-wrap module provides a general-purpose wrapping dispatcher that allows one to intercept an incoming request as well as the response returned by other servlets


(make servlet req-trans res-trans)  dispatcher/c

  servlet : (-> request? response?)
  req-trans : (-> request? request?)
  res-trans : (-> response? response?)
Returns a dispatcher that wraps res-trans around servlet, which itself receives requests transformed by req-trans. Put differently, the servlet underlying the dispatcher returned by make is equivalent to (λ (r) (res-trans (servlet (req-trans r)))).

If you’re not interested in transforming requests, pass in identity (the identity function) for req-trans. Similarly, using identity for res-trans will cause responses to pass through unchanged. (Using identity for both req-trans and res-trans is equivalent to just using servlet as-is.)

A typical use case for this dispatcher would be to inject headers into requests or responses. Similarly, functionally updating existing headers also fits into this pattern. Since the entire request – not just its headers – is available to req-trans (and similarly for the response and res-trans), arbitrary rewriting of request/response bodies is possible. Side effects in req-trans and res-trans are permitted as long as make’s contracts are adhered to.

Changed in version 1.9 of package web-server-lib: First version of this dispatcher