org.http4s.server.middleware

Type members

Classlikes

object AutoSlash

Removes a trailing slash from Request path

Removes a trailing slash from Request path

If a route exists with a file style Uri, eg "/foo", this middleware will cause Requests with uri = "/foo" and uri = "/foo/" to match the route.

Source:
AutoSlash.scala
object BodyCache

Middleware for caching the request body for multiple compilations

Middleware for caching the request body for multiple compilations

As the body of the request is the fs2.Stream of bytes, compiling it several times (e.g. with middlewares) is unsafe. This middleware forbids such behaviour, compiling the body only once. It does so only for the "inner" middlewares:

val route = AMiddleware(BodyCache(SomeOtherMiddleware(myRoute)))

In this example only myRoute & SomeOtherMiddleware will receive cached request body, while the AMiddleware will get the raw one.

As the entire request body will be allocated in memory, there is a possibility of OOM error with a large body. Because of that, using the EntityLimiter middleware is strongly advised.

Note:

This middleware has nothing to do with the HTTP caching mechanism and it does not cache bodies between multiple requests.

Source:
BodyCache.scala

Middelwares which allow for bracketing on a Request/Response, including the completion of the Response body stream.

Middelwares which allow for bracketing on a Request/Response, including the completion of the Response body stream.

These are analogous to cats.effect.Bracket and fs2.Stream.bracket. The reason that they exist is because due to the full termination of a Response being a function of the termination of the fs2.Stream which backs the response body, you can't actually use either cats.effect.Bracket or fs2.Stream.bracket directly.

Source:
BracketRequestResponse.scala
object CORS

Implements the CORS protocol. The actual middleware is a CORSPolicy, which can be obtained via policy.

Implements the CORS protocol. The actual middleware is a CORSPolicy, which can be obtained via policy.

See also:
Source:
CORS.scala
sealed class CORSPolicy(allowOrigin: AllowOrigin, allowCredentials: AllowCredentials, exposeHeaders: ExposeHeaders, allowMethods: AllowMethods, allowHeaders: AllowHeaders, maxAge: MaxAge)

A middleware that applies the CORS protocol to any Http value. Obtain a reference to a CORSPolicy via the CORS object, which represents a default policy.

A middleware that applies the CORS protocol to any Http value. Obtain a reference to a CORSPolicy via the CORS object, which represents a default policy.

Requests with an Origin header will receive the appropriate CORS headers. More headers are available for "pre-flight" requests, those whose method is OPTIONS and has an Access-Control-Request-Method header.

Requests without the required headers, or requests that fail a CORS origin, method, or headers check are passed through to the underlying Http function, but do not receive any CORS headers in the response. The user agent will then block sharing the resource across origins according to the CORS protocol.

Companion:
object
Source:
CORS.scala
object CORSPolicy
Companion:
class
Source:
CORS.scala
object Caching

Caching contains middlewares to support caching functionality.

Caching contains middlewares to support caching functionality.

Helper functions to support Caching.cache can be found in Caching.Helpers

Source:
Caching.scala

Generic middleware to aggregate chunked response into memory.

Generic middleware to aggregate chunked response into memory.

This middleware wraps any function that may produce an Http response. Upon receiving a chunk-encoded response, the composed function will pull and assemble the full response into memory before emitting the whole response. It also removes the "Chunked encoding" headers, and may add a Content-Length header for the whole payload.

Reference: "Chunked Transfer Encoding", Section 4.1 of RFC 7230 https://datatracker.ietf.org/doc/html/rfc7230#section-4.1

Source:
ChunkAggregator.scala

Middlewares for tracking the quantity of concurrent requests.

Middlewares for tracking the quantity of concurrent requests.

These are generalized middlewares and can be used to implement metrics, logging, max concurrent requests, etc.

Note:

The concurrent request count is decremented on the completion of the Response body, or in the event of any error, and is guaranteed to only occur once.

Source:
ConcurrentRequests.scala
object Date

Date Middleware, adds the Date Header to All Responses generated by the service.

Date Middleware, adds the Date Header to All Responses generated by the service.

Source:
Date.scala

Handles HEAD requests as a GET without a body.

Handles HEAD requests as a GET without a body.

If the service returns the fallthrough response, the request is resubmitted as a GET. The resulting response's body is killed, but all headers are preserved. This is a naive, but correct, implementation of HEAD. Routes requiring more optimization should implement their own HEAD handler.

Source:
DefaultHead.scala
object GZip
Source:
GZip.scala
object HSTS

Middleware to add HTTP Strict Transport Security (HSTS) support adding the Strict Transport Security headers

Middleware to add HTTP Strict Transport Security (HSTS) support adding the Strict Transport Security headers

Source:
HSTS.scala

Middleware to redirect http traffic to https. Inspects X-Forwarded-Proto header and if it is set to http, redirects to Host with same URL with https schema; otherwise does nothing. This middleware is useful when a service is deployed behind a load balancer which does not support such redirect feature, e.g. Heroku.

Middleware to redirect http traffic to https. Inspects X-Forwarded-Proto header and if it is set to http, redirects to Host with same URL with https schema; otherwise does nothing. This middleware is useful when a service is deployed behind a load balancer which does not support such redirect feature, e.g. Heroku.

Source:
HttpsRedirect.scala
object Jsonp

Middleware to support wrapping json responses in jsonp.

Middleware to support wrapping json responses in jsonp.

Jsonp wrapping occurs when the request contains a parameter with the given name and the request Content-Type is application/json.

If the wrapping is done, the response Content-Type is changed into application/javascript and the appropriate jsonp callback is applied.

Source:
Jsonp.scala
object Logger

Simple Middleware for Logging All Requests and Responses

Simple Middleware for Logging All Requests and Responses

Source:
Logger.scala
object Metrics

Server middleware to record metrics for the http4s server.

Server middleware to record metrics for the http4s server.

This middleware will record:

  • Number of active requests
  • Time duration to send the response headers
  • Time duration to send the whole response body
  • Time duration of errors and other abnormal terminations

This middleware can be extended to support any metrics ecosystem by implementing the org.http4s.metrics.MetricsOps type

Source:
Metrics.scala
object RequestId

Propagate a X-Request-Id header to the response, generate a UUID when the X-Request-Id header is unset. https://devcenter.heroku.com/articles/http-request-id

Propagate a X-Request-Id header to the response, generate a UUID when the X-Request-Id header is unset. https://devcenter.heroku.com/articles/http-request-id

Source:
RequestId.scala

Simple Middleware for Logging Requests As They Are Processed

Simple Middleware for Logging Requests As They Are Processed

Source:
RequestLogger.scala

Simple middleware for logging responses as they are processed

Simple middleware for logging responses as they are processed

Source:
ResponseLogger.scala

Simple middleware for adding a static set of headers to responses returned by a kleisli.

Simple middleware for adding a static set of headers to responses returned by a kleisli.

Source:
StaticHeaders.scala
object Throttle

Transform a service to reject any calls the go over a given rate.

Transform a service to reject any calls the go over a given rate.

Source:
Throttle.scala
object Timeout

Removes the given prefix from the beginning of the path of the Request.

Removes the given prefix from the beginning of the path of the Request.

Source:
TranslateUri.scala

Middleware for lifting application/x-www-form-urlencoded bodies into the request query params.

Middleware for lifting application/x-www-form-urlencoded bodies into the request query params.

The params are merged into the existing paras after the existing query params. This means that if the query already contains the pair "foo" -> Some("bar"), parameters on the body must be acessed through multiParams.

Source:
UrlFormLifter.scala

Middleware for virtual host mapping

Middleware for virtual host mapping

The VirtualHost middleware allows multiple services to be mapped based on the org.http4s.headers.Host header of the org.http4s.Request.

Source:
VirtualHost.scala

Deprecated classlikes

@deprecated("Deficient. See https://github.com/http4s/http4s/security/advisories/GHSA-52cf-226f-rhr6.", "0.21.27")
final class CORSConfig

CORS middleware config options. You can give an instance of this class to the CORS middleware, to specify its behavior

CORS middleware config options. You can give an instance of this class to the CORS middleware, to specify its behavior

Companion:
object
Deprecated
Source:
CORS.scala
@deprecated("Deficient. See https://github.com/http4s/http4s/security/advisories/GHSA-52cf-226f-rhr6.", "0.21.27")
object CORSConfig
Companion:
class
Deprecated
Source:
CORS.scala
@deprecated("Obsolete. Not implemented by any backends.", "0.23.12")
Deprecated
Source:
PushSupport.scala