|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +use tower_service::Service; |
| 19 | + |
| 20 | +use super::Filter; |
| 21 | +use crate::invocation::Metadata; |
| 22 | +use crate::invocation::Request; |
| 23 | + |
| 24 | +#[derive(Clone)] |
| 25 | +pub struct FilterService<S, F> { |
| 26 | + inner: S, |
| 27 | + f: F, |
| 28 | +} |
| 29 | + |
| 30 | +impl<S, F> FilterService<S, F> { |
| 31 | + pub fn new(inner: S, f: F) -> Self |
| 32 | + where |
| 33 | + F: Filter, |
| 34 | + { |
| 35 | + Self { inner, f } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl<S, F, ReqBody> Service<http::Request<ReqBody>> for FilterService<S, F> |
| 40 | +where |
| 41 | + F: Filter, |
| 42 | + S: Service<http::Request<ReqBody>, Response = http::Response<crate::BoxBody>>, |
| 43 | + S::Error: Into<crate::Error>, |
| 44 | + S::Future: Send + 'static, |
| 45 | +{ |
| 46 | + type Response = http::Response<crate::BoxBody>; |
| 47 | + |
| 48 | + type Error = S::Error; |
| 49 | + |
| 50 | + type Future = crate::BoxFuture<Self::Response, Self::Error>; |
| 51 | + |
| 52 | + fn poll_ready( |
| 53 | + &mut self, |
| 54 | + cx: &mut std::task::Context<'_>, |
| 55 | + ) -> std::task::Poll<Result<(), Self::Error>> { |
| 56 | + self.inner.poll_ready(cx) |
| 57 | + } |
| 58 | + |
| 59 | + fn call(&mut self, req: http::Request<ReqBody>) -> Self::Future { |
| 60 | + let uri = req.uri().clone(); |
| 61 | + let method = req.method().clone(); |
| 62 | + let version = req.version(); |
| 63 | + let (parts, msg) = req.into_parts(); |
| 64 | + |
| 65 | + let res = self.f.call(Request::from_parts( |
| 66 | + Metadata::from_headers(parts.headers), |
| 67 | + (), |
| 68 | + )); |
| 69 | + match res { |
| 70 | + Ok(req) => { |
| 71 | + let (metadata, _) = req.into_parts(); |
| 72 | + let req = Request::from_parts(Metadata::from_headers(metadata.into_headers()), msg); |
| 73 | + let http_req = req.into_http(uri, method, version); |
| 74 | + |
| 75 | + let resp = self.inner.call(http_req); |
| 76 | + Box::pin(resp) |
| 77 | + } |
| 78 | + Err(err) => { |
| 79 | + let fut = async move { Ok(err.to_http()) }; |
| 80 | + Box::pin(fut) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments