Response::new(401)
.with_headers(vec![("WWW-Authenticate".to_string(), "Bearer realm=\"oauth2\"".to_string())])
.with_body(r#"{ "error": "token was not present"}"#)
Stopping Request Execution
To intercept and stop a request, return a Response
object from your on_request
wrapped functions:
You can block or allow requests to reach the upstream service using a Flow
. A Flow
in an enum
value with two possible values:
-
Continue
: Defines an object that is forwarded to the response. -
Break
: Aborts the request and returns the provided response.
Create a Flow
as follows:
async fn request_filter(request_state: RequestState) -> Flow<()> {
let header_state = request_state.into_headers_state().await;
let handler = header_state.handler();
if handler.header("authorization").is_some() {
Flow::Continue(())
} else {
Flow::Break(Response::new(401)
.with_headers(vec![("WWW-Authenticate".to_string(), "Bearer realm=\"oauth2\"".to_string())])
.with_body(r#"{ "error": "token was not present"}"#))
}
}
Due to the streaming nature of To avoid early responses reaching the client, see Sharing Data Between Requests and Responses. |