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
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::task::{Context, Poll};
type BoxErr = Box<dyn std::error::Error + Send + Sync>;
pub struct OneshotService<Pr, C = HttpConnector, B = Body> {
client: Client<C, B>,
scheme: Scheme,
authority: Authority,
path: Pr,
}
impl<Pr: Clone, C: Clone, B> Clone for OneshotService<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> OneshotService<Pr, C, B> {
pub fn from<S, A>(
client: 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<Pr, B> OneshotService<Pr, HttpConnector, B>
where
B: HttpBody + Send,
B::Data: Send,
{
pub fn http_default<A>(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: client::http_default(),
scheme: Scheme::HTTP,
authority,
path,
})
}
}
#[cfg(feature = "https")]
impl<Pr, B> OneshotService<Pr, HttpsConnector<HttpConnector>, B>
where
B: HttpBody + Send,
B::Data: Send,
{
#[cfg_attr(docsrs, doc(cfg(feature = "https")))]
pub fn https_default<A>(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: client::https_default(),
scheme: Scheme::HTTPS,
authority,
path,
})
}
}
impl<C, B, Pr> Service<Request<B>> for OneshotService<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() -> OneshotService<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 = OneshotService::from(
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;
}
}