R/mw-etag.R

Defines functions mw_etag

Documented in mw_etag

#' Middleware that add an `Etag` header to the response
#'
#' @param algorithm Checksum algorithm to use. Only `"crc32"` is
#' implemented currently.
#'
#' @return Handler function.
#'
#' @family middleware
#' @export
#' @examples
#' app <- new_app()
#' app$use(mw_etag())
#' app

mw_etag <- function(algorithm = "crc32") {
  if (algorithm != "crc32") {
    stop("Only the 'crc32' algorithm is implemented in `mw_etag()`")
  }
  function(req, res) {
    do <- function(req, res) {
      etag <- paste0("\"", crc32(res$.body), "\"")
      res$set_header("Etag", etag)
    }
    res$on_response(do)

    "next"
  }
}

Try the presser package in your browser

Any scripts or data that you put into this service are public.

presser documentation built on July 1, 2020, 5:49 p.m.