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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
use crate::client;
use crate::future::RevProxyFuture;
use crate::rewrite::PathRewriter;
use crate::Error;

use client::HttpConnector;
#[cfg(feature = "https")]
use client::HttpsConnector;

use http::uri::{Authority, Scheme};
use http::Error as HttpError;
use http::{Request, Response};

use hyper::body::{Body, HttpBody};
use hyper::client::{connect::Connect, Client};

use tower_service::Service;

use std::convert::Infallible;
use std::sync::Arc;
use std::task::{Context, Poll};

type BoxErr = Box<dyn std::error::Error + Send + Sync>;

/// The return type of [`builder()`], [`builder_http()`] and [`builder_https()`].
#[derive(Debug)]
pub struct Builder<C = HttpConnector, B = Body> {
    client: Arc<Client<C, B>>,
    scheme: Scheme,
    authority: Authority,
}

impl<C, B> Clone for Builder<C, B> {
    fn clone(&self) -> Self {
        Self {
            client: self.client.clone(),
            scheme: self.scheme.clone(),
            authority: self.authority.clone(),
        }
    }
}

impl<C, B> Builder<C, B> {
    pub fn build<Pr>(&self, path: Pr) -> ReusedService<Pr, C, B> {
        let Self {
            client,
            scheme,
            authority,
        } = Clone::clone(self);
        ReusedService {
            client,
            scheme,
            authority,
            path,
        }
    }
}

/// Builder of [`ReusedService`], with [`client::http_default()`].
///
/// For the meaning of "authority", refer to the documentation of [`Uri`](http::uri::Uri).
pub fn builder_http<B, A>(authority: A) -> Result<Builder<HttpConnector, B>, HttpError>
where
    B: HttpBody + Send,
    B::Data: Send,
    Authority: TryFrom<A>,
    <Authority as TryFrom<A>>::Error: Into<HttpError>,
{
    builder(client::http_default(), Scheme::HTTP, authority)
}

/// Builder of [`ReusedService`], with [`client::https_default()`].
///
/// For the meaning of "authority", refer to the documentation of [`Uri`](http::uri::Uri).
#[cfg(feature = "https")]
pub fn builder_https<B, A>(
    authority: A,
) -> Result<Builder<HttpsConnector<HttpConnector>, B>, HttpError>
where
    B: HttpBody + Send,
    B::Data: Send,
    Authority: TryFrom<A>,
    <Authority as TryFrom<A>>::Error: Into<HttpError>,
{
    builder(client::https_default(), Scheme::HTTP, authority)
}

/// Builder of [`ReusedService`].
///
/// For the meaning of "scheme" and "authority", refer to the documentation of
/// [`Uri`](http::uri::Uri).
pub fn builder<C, B, S, A>(
    client: Client<C, B>,
    scheme: S,
    authority: A,
) -> Result<Builder<C, B>, HttpError>
where
    Scheme: TryFrom<S>,
    <Scheme as TryFrom<S>>::Error: Into<HttpError>,
    Authority: TryFrom<A>,
    <Authority as TryFrom<A>>::Error: Into<HttpError>,
{
    let scheme = scheme.try_into().map_err(Into::into)?;
    let authority = authority.try_into().map_err(Into::into)?;
    Ok(Builder {
        client: Arc::new(client),
        scheme,
        authority,
    })
}

/// A [`Service<Request<B>>`] that sends a request and returns the response, sharing a [`Client`].
///
/// ```
/// # async fn run_test() {
/// # use reverse_proxy_service::ReusedService;
/// # use reverse_proxy_service::Static;
/// # use tower_service::Service;
/// # use hyper::body::Body;
/// # use http::Request;
/// let svc_builder = reverse_proxy_service::builder_http("example.com:1234").unwrap();
///
/// let mut svc1 = svc_builder.build(Static("bar"));
/// let mut svc2 = svc_builder.build(Static("baz"));
///
/// let req = Request::builder()
///     .uri("https://myserver.com/foo")
///     .body(Body::empty())
///     .unwrap();
/// // http://example.com:1234/bar
/// let _res = svc1.call(req).await.unwrap();
///
/// let req = Request::builder()
///     .uri("https://myserver.com/foo")
///     .body(Body::empty())
///     .unwrap();
/// // http://example.com:1234/baz
/// let _res = svc2.call(req).await.unwrap();
/// # }
/// ```
#[derive(Debug)]
pub struct ReusedService<Pr, C, B = Body> {
    client: Arc<Client<C, B>>,
    scheme: Scheme,
    authority: Authority,
    path: Pr,
}

impl<Pr: Clone, C, B> Clone for ReusedService<Pr, C, B> {
    #[inline]
    fn clone(&self) -> Self {
        Self {
            client: self.client.clone(),
            scheme: self.scheme.clone(),
            authority: self.authority.clone(),
            path: self.path.clone(),
        }
    }
}

impl<Pr, C, B> ReusedService<Pr, C, B> {
    pub fn from<S, A>(
        client: Arc<Client<C, B>>,
        scheme: S,
        authority: A,
        path: Pr,
    ) -> Result<Self, HttpError>
    where
        Scheme: TryFrom<S>,
        <Scheme as TryFrom<S>>::Error: Into<HttpError>,
        Authority: TryFrom<A>,
        <Authority as TryFrom<A>>::Error: Into<HttpError>,
    {
        let scheme = scheme.try_into().map_err(Into::into)?;
        let authority = authority.try_into().map_err(Into::into)?;
        Ok(Self {
            client,
            scheme,
            authority,
            path,
        })
    }
}

impl<B, Pr> ReusedService<Pr, HttpConnector, B>
where
    B: HttpBody + Send,
    B::Data: Send,
{
    pub fn http_default<A>(
        client: Arc<Client<HttpConnector, B>>,
        authority: A,
        path: Pr,
    ) -> Result<Self, HttpError>
    where
        Authority: TryFrom<A>,
        <Authority as TryFrom<A>>::Error: Into<HttpError>,
    {
        let authority = authority.try_into().map_err(Into::into)?;
        Ok(Self {
            client,
            scheme: Scheme::HTTP,
            authority,
            path,
        })
    }
}

#[cfg(feature = "https")]
impl<Pr, B> ReusedService<Pr, HttpsConnector<HttpConnector>, B>
where
    B: HttpBody + Send,
    B::Data: Send,
{
    pub fn https_default<A>(
        client: Arc<Client<HttpsConnector<HttpConnector>, B>>,
        authority: A,
        path: Pr,
    ) -> Result<Self, HttpError>
    where
        Authority: TryFrom<A>,
        <Authority as TryFrom<A>>::Error: Into<HttpError>,
    {
        let authority = authority.try_into().map_err(Into::into)?;
        Ok(Self {
            client,
            scheme: Scheme::HTTPS,
            authority,
            path,
        })
    }
}

impl<C, B, Pr> Service<Request<B>> for ReusedService<Pr, C, B>
where
    C: Connect + Clone + Send + Sync + 'static,
    B: HttpBody + Send + 'static,
    B::Data: Send,
    B::Error: Into<BoxErr>,
    Pr: PathRewriter,
{
    type Response = Result<Response<Body>, Error>;
    type Error = Infallible;
    type Future = RevProxyFuture;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: Request<B>) -> Self::Future {
        RevProxyFuture::new(
            &self.client,
            req,
            &self.scheme,
            &self.authority,
            &mut self.path,
        )
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::test_helper;
    use crate::ReplaceAll;

    use http::uri::{Parts, Uri};

    fn make_svc() -> ReusedService<ReplaceAll<'static>, HttpConnector, String> {
        let uri = Uri::try_from(&mockito::server_url());
        assert!(uri.is_ok());
        let uri = uri.unwrap();

        let Parts {
            scheme, authority, ..
        } = uri.into_parts();

        let svc = ReusedService::from(
            Arc::new(client::http_default()),
            scheme.unwrap(),
            authority.unwrap(),
            ReplaceAll("foo", "goo"),
        );
        assert!(svc.is_ok());
        svc.unwrap()
    }

    #[tokio::test]
    async fn match_path() {
        let mut svc = make_svc();
        test_helper::match_path(&mut svc).await;
    }

    #[tokio::test]
    async fn match_query() {
        let mut svc = make_svc();
        test_helper::match_query(&mut svc).await;
    }

    #[tokio::test]
    async fn match_post() {
        let mut svc = make_svc();
        test_helper::match_post(&mut svc).await;
    }

    #[tokio::test]
    async fn match_header() {
        let mut svc = make_svc();
        test_helper::match_header(&mut svc).await;
    }
}