|
| 1 | +use bytes::Bytes; |
| 2 | +use http_body_util::{BodyExt, Full}; |
| 3 | +use motore::{layer::Layer, service::Service}; |
| 4 | + |
| 5 | +use crate::{body::Body, request::Request, response::Response}; |
| 6 | + |
| 7 | +pub struct RespBodyToFullLayer; |
| 8 | +pub struct RespBodyToFullService<S>(S); |
| 9 | + |
| 10 | +impl<S> Layer<S> for RespBodyToFullLayer { |
| 11 | + type Service = RespBodyToFullService<S>; |
| 12 | + |
| 13 | + fn layer(self, inner: S) -> Self::Service { |
| 14 | + RespBodyToFullService(inner) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +impl<Cx, ReqBody, RespBody, S> Service<Cx, Request<ReqBody>> for RespBodyToFullService<S> |
| 19 | +where |
| 20 | + Cx: Send, |
| 21 | + S: Service<Cx, Request<ReqBody>, Response = Response<RespBody>> + Sync, |
| 22 | + ReqBody: Send, |
| 23 | + RespBody: http_body::Body + Send, |
| 24 | + RespBody::Data: Send, |
| 25 | + RespBody::Error: std::fmt::Debug + Send, |
| 26 | +{ |
| 27 | + type Response = Response<Full<Bytes>>; |
| 28 | + type Error = S::Error; |
| 29 | + |
| 30 | + async fn call( |
| 31 | + &self, |
| 32 | + cx: &mut Cx, |
| 33 | + req: Request<ReqBody>, |
| 34 | + ) -> Result<Self::Response, Self::Error> { |
| 35 | + let resp = self.0.call(cx, req).await?; |
| 36 | + let (parts, body) = resp.into_parts(); |
| 37 | + let body = Full::new(BodyExt::collect(body).await.unwrap().to_bytes()); |
| 38 | + let resp = Response::from_parts(parts, body); |
| 39 | + Ok(resp) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// For request, and for testing, it should never implement `http_body::Body` |
| 44 | +pub struct AutoBody; |
| 45 | + |
| 46 | +pub struct AutoBodyLayer; |
| 47 | +pub struct AutoBodyService<S>(S); |
| 48 | + |
| 49 | +impl<S> Layer<S> for AutoBodyLayer { |
| 50 | + type Service = AutoBodyService<S>; |
| 51 | + |
| 52 | + fn layer(self, inner: S) -> Self::Service { |
| 53 | + AutoBodyService(inner) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl<Cx, RespBody, S> Service<Cx, Request<AutoBody>> for AutoBodyService<S> |
| 58 | +where |
| 59 | + Cx: Send, |
| 60 | + S: Service<Cx, Request, Response = Response<RespBody>> + Sync, |
| 61 | + RespBody: Send, |
| 62 | +{ |
| 63 | + type Response = S::Response; |
| 64 | + type Error = S::Error; |
| 65 | + |
| 66 | + async fn call( |
| 67 | + &self, |
| 68 | + cx: &mut Cx, |
| 69 | + req: Request<AutoBody>, |
| 70 | + ) -> Result<Self::Response, Self::Error> { |
| 71 | + let (parts, _) = req.into_parts(); |
| 72 | + let body = Body::from("Hello, World"); |
| 73 | + let req = Request::from_parts(parts, body); |
| 74 | + self.0.call(cx, req).await |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// For request, and for testing, it should never implement `http_body::Body` |
| 79 | +pub struct AutoFull; |
| 80 | + |
| 81 | +pub struct AutoFullLayer; |
| 82 | +pub struct AutoFullService<S>(S); |
| 83 | + |
| 84 | +impl<S> Layer<S> for AutoFullLayer { |
| 85 | + type Service = AutoFullService<S>; |
| 86 | + |
| 87 | + fn layer(self, inner: S) -> Self::Service { |
| 88 | + AutoFullService(inner) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl<Cx, RespBody, S> Service<Cx, Request<AutoFull>> for AutoFullService<S> |
| 93 | +where |
| 94 | + Cx: Send, |
| 95 | + S: Service<Cx, Request<Full<Bytes>>, Response = Response<RespBody>> + Sync, |
| 96 | + RespBody: Send, |
| 97 | +{ |
| 98 | + type Response = S::Response; |
| 99 | + type Error = S::Error; |
| 100 | + |
| 101 | + async fn call( |
| 102 | + &self, |
| 103 | + cx: &mut Cx, |
| 104 | + req: Request<AutoFull>, |
| 105 | + ) -> Result<Self::Response, Self::Error> { |
| 106 | + let (parts, _) = req.into_parts(); |
| 107 | + let body = Full::new(Bytes::new()); |
| 108 | + let req = Request::from_parts(parts, body); |
| 109 | + self.0.call(cx, req).await |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// For response, and for testing, it should never implement `http_body::Body` |
| 114 | +pub struct Nothing; |
| 115 | + |
| 116 | +pub struct DropBodyLayer; |
| 117 | +pub struct DropBodyService<S>(S); |
| 118 | + |
| 119 | +impl<S> Layer<S> for DropBodyLayer { |
| 120 | + type Service = DropBodyService<S>; |
| 121 | + |
| 122 | + fn layer(self, inner: S) -> Self::Service { |
| 123 | + DropBodyService(inner) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl<Cx, ReqBody, RespBody, S> Service<Cx, Request<ReqBody>> for DropBodyService<S> |
| 128 | +where |
| 129 | + Cx: Send, |
| 130 | + S: Service<Cx, Request<ReqBody>, Response = Response<RespBody>> + Sync, |
| 131 | + ReqBody: Send, |
| 132 | + RespBody: Send, |
| 133 | +{ |
| 134 | + type Response = Response<Nothing>; |
| 135 | + type Error = S::Error; |
| 136 | + |
| 137 | + async fn call( |
| 138 | + &self, |
| 139 | + cx: &mut Cx, |
| 140 | + req: Request<ReqBody>, |
| 141 | + ) -> Result<Self::Response, Self::Error> { |
| 142 | + let resp = self.0.call(cx, req).await?; |
| 143 | + let (parts, _) = resp.into_parts(); |
| 144 | + let body = Nothing; |
| 145 | + let resp = Response::from_parts(parts, body); |
| 146 | + Ok(resp) |
| 147 | + } |
| 148 | +} |
0 commit comments