1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-11-24 23:01:05 +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

@@ -39,17 +39,17 @@ impl<S, B> Error<S, B> {
}
#[derive(Clone)]
pub struct BodyToBytes<S> {
pub struct BodyToBytesResponse<S> {
inner: S,
}
impl<S> BodyToBytes<S> {
impl<S> BodyToBytesResponse<S> {
pub const fn new(inner: S) -> Self {
Self { inner }
}
}
impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for BodyToBytes<S>
impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for BodyToBytesResponse<S>
where
S: Service<Request<ReqBody>, Response = Response<ResBody>>,
S::Future: Send + 'static,
@@ -85,12 +85,12 @@ where
}
#[derive(Default, Clone, Copy)]
pub struct BodyToBytesLayer;
pub struct BodyToBytesResponseLayer;
impl<S> Layer<S> for BodyToBytesLayer {
type Service = BodyToBytes<S>;
impl<S> Layer<S> for BodyToBytesResponseLayer {
type Service = BodyToBytesResponse<S>;
fn layer(&self, inner: S) -> Self::Service {
BodyToBytes::new(inner)
BodyToBytesResponse::new(inner)
}
}

View File

@@ -0,0 +1,66 @@
// Copyright 2022 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use bytes::Bytes;
use http::Request;
use http_body::Full;
use tower::{Layer, Service};
#[derive(Clone)]
pub struct BytesToBodyRequest<S> {
inner: S,
}
impl<S> BytesToBodyRequest<S> {
pub const fn new(inner: S) -> Self {
Self { inner }
}
}
impl<S> Service<Request<Bytes>> for BytesToBodyRequest<S>
where
S: Service<Request<Full<Bytes>>>,
S::Future: Send + 'static,
{
type Error = S::Error;
type Response = S::Response;
type Future = S::Future;
fn poll_ready(
&mut self,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, request: Request<Bytes>) -> Self::Future {
let (parts, body) = request.into_parts();
let body = Full::new(body);
let request = Request::from_parts(parts, body);
self.inner.call(request)
}
}
#[derive(Default, Clone, Copy)]
pub struct BytesToBodyRequestLayer;
impl<S> Layer<S> for BytesToBodyRequestLayer {
type Service = BytesToBodyRequest<S>;
fn layer(&self, inner: S) -> Self::Service {
BytesToBodyRequest::new(inner)
}
}

View File

@@ -21,7 +21,6 @@ use futures_util::{
};
use headers::{ContentType, HeaderMapExt};
use http::Request;
use http_body::Full;
use serde::Serialize;
use thiserror::Error;
use tower::{Layer, Service};
@@ -65,7 +64,7 @@ impl<S, T> FormUrlencodedRequest<S, T> {
impl<S, T> Service<Request<T>> for FormUrlencodedRequest<S, T>
where
S: Service<Request<Full<Bytes>>>,
S: Service<Request<Bytes>>,
S::Future: Send + 'static,
S::Error: 'static,
T: Serialize,
@@ -87,7 +86,7 @@ where
parts.headers.typed_insert(ContentType::form_url_encoded());
let body = match serde_urlencoded::to_string(&body) {
Ok(body) => Full::new(Bytes::from(body)),
Ok(body) => Bytes::from(body),
Err(err) => return std::future::ready(Err(Error::serialize(err))).left_future(),
};

View File

@@ -21,7 +21,6 @@ use futures_util::{
};
use headers::{ContentType, HeaderMapExt};
use http::Request;
use http_body::Full;
use serde::Serialize;
use thiserror::Error;
use tower::{Layer, Service};
@@ -65,7 +64,7 @@ impl<S, T> JsonRequest<S, T> {
impl<S, T> Service<Request<T>> for JsonRequest<S, T>
where
S: Service<Request<Full<Bytes>>>,
S: Service<Request<Bytes>>,
S::Future: Send + 'static,
S::Error: 'static,
T: Serialize,
@@ -87,7 +86,7 @@ where
parts.headers.typed_insert(ContentType::json());
let body = match serde_json::to_vec(&body) {
Ok(body) => Full::new(Bytes::from(body)),
Ok(body) => Bytes::from(body),
Err(err) => return std::future::ready(Err(Error::serialize(err))).left_future(),
};

View File

@@ -12,7 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
pub mod body_to_bytes;
pub mod body_to_bytes_response;
pub mod bytes_to_body_request;
pub mod catch_http_codes;
pub mod form_urlencoded_request;
pub mod json_request;