1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use http::{Request, Uri};
use std::task::{Context, Poll};
use tower_service::Service;
#[derive(Debug)]
pub(crate) struct AddOrigin<T> {
inner: T,
origin: Uri,
}
impl<T> AddOrigin<T> {
pub(crate) fn new(inner: T, origin: Uri) -> Self {
Self { inner, origin }
}
}
impl<T, ReqBody> Service<Request<ReqBody>> for AddOrigin<T>
where
T: Service<Request<ReqBody>>,
{
type Response = T::Response;
type Error = T::Error;
type Future = T::Future;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
let (mut head, body) = req.into_parts();
let mut uri: http::uri::Parts = head.uri.into();
let set_uri = self.origin.clone().into_parts();
uri.scheme = Some(set_uri.scheme.expect("expected scheme").clone());
uri.authority = Some(set_uri.authority.expect("expected authority").clone());
head.uri = http::Uri::from_parts(uri).expect("valid uri");
let request = Request::from_parts(head, body);
self.inner.call(request)
}
}