1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-11-21 23:00:50 +03:00

Make HTTP request layers convert to Bytes

Add layer to convert a request's Bytes to a Body.
This commit is contained in:
Kévin Commaille
2022-08-19 18:01:16 +02:00
committed by Quentin Gliech
parent 669a1867b7
commit 348044afdc
8 changed files with 102 additions and 22 deletions

View File

@@ -20,7 +20,8 @@ use tower::{layer::util::Stack, Service, ServiceBuilder};
use tower_http::cors::CorsLayer;
use crate::layers::{
body_to_bytes::{BodyToBytes, BodyToBytesLayer},
body_to_bytes_response::{BodyToBytesResponse, BodyToBytesResponseLayer},
bytes_to_body_request::{BytesToBodyRequest, BytesToBodyRequestLayer},
catch_http_codes::{CatchHttpCodes, CatchHttpCodesLayer},
form_urlencoded_request::{FormUrlencodedRequest, FormUrlencodedRequestLayer},
json_request::{JsonRequest, JsonRequestLayer},
@@ -69,8 +70,12 @@ impl CorsLayerExt for CorsLayer {
}
pub trait ServiceExt<Body>: Sized {
fn response_body_to_bytes(self) -> BodyToBytes<Self> {
BodyToBytes::new(self)
fn request_bytes_to_body(self) -> BytesToBodyRequest<Self> {
BytesToBodyRequest::new(self)
}
fn response_body_to_bytes(self) -> BodyToBytesResponse<Self> {
BodyToBytesResponse::new(self)
}
fn json_response<T>(self) -> JsonResponse<Self, T> {
@@ -104,7 +109,8 @@ pub trait ServiceExt<Body>: Sized {
impl<S, B> ServiceExt<B> for S where S: Service<Request<B>> {}
pub trait ServiceBuilderExt<L>: Sized {
fn response_body_to_bytes(self) -> ServiceBuilder<Stack<BodyToBytesLayer, L>>;
fn request_bytes_to_body(self) -> ServiceBuilder<Stack<BytesToBodyRequestLayer, L>>;
fn response_body_to_bytes(self) -> ServiceBuilder<Stack<BodyToBytesResponseLayer, L>>;
fn json_response<T>(self) -> ServiceBuilder<Stack<JsonResponseLayer<T>, L>>;
fn json_request<T>(self) -> ServiceBuilder<Stack<JsonRequestLayer<T>, L>>;
fn form_urlencoded_request<T>(self) -> ServiceBuilder<Stack<FormUrlencodedRequestLayer<T>, L>>;
@@ -131,8 +137,12 @@ pub trait ServiceBuilderExt<L>: Sized {
}
impl<L> ServiceBuilderExt<L> for ServiceBuilder<L> {
fn response_body_to_bytes(self) -> ServiceBuilder<Stack<BodyToBytesLayer, L>> {
self.layer(BodyToBytesLayer::default())
fn request_bytes_to_body(self) -> ServiceBuilder<Stack<BytesToBodyRequestLayer, L>> {
self.layer(BytesToBodyRequestLayer::default())
}
fn response_body_to_bytes(self) -> ServiceBuilder<Stack<BodyToBytesResponseLayer, L>> {
self.layer(BodyToBytesResponseLayer::default())
}
fn json_response<T>(self) -> ServiceBuilder<Stack<JsonResponseLayer<T>, L>> {