scheme

scheme

§Description

Rejects a request if its Uri scheme does not match a given one.

The scheme directive can be used to match requests by their Uri scheme, only passing through requests that match the specified scheme and rejecting all others.

A typical use case for the scheme directive would be to reject requests coming in over http instead of https, or to redirect such requests to the matching https URI with a MovedPermanently.

For simply extracting the scheme name, see the extractScheme directive.

§Example

  1. final Route route = route(
  2. scheme("http", ()->
  3. extract((ctx) -> ctx.getRequest().getUri(), (uri)->
  4. redirect(uri.scheme("https"), StatusCodes.MOVED_PERMANENTLY)
  5. )
  6. ),
  7. scheme("https", ()->
  8. complete("Safe and secure!")
  9. )
  10. );
  11.  
  12. testRoute(route).run(HttpRequest.GET("http://www.example.com/hello"))
  13. .assertStatusCode(StatusCodes.MOVED_PERMANENTLY)
  14. .assertHeaderExists(Location.create("https://www.example.com/hello"))
  15. ;
  16.  
  17. testRoute(route).run(HttpRequest.GET("https://www.example.com/hello"))
  18. .assertEntity("Safe and secure!");