-
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathfuture.rs
More file actions
163 lines (149 loc) · 5.56 KB
/
Copy pathfuture.rs
File metadata and controls
163 lines (149 loc) · 5.56 KB
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
use std::{
future::Future,
pin::Pin,
str,
task::{Context, Poll, ready},
};
use futures_util::future::Either;
use http::{
Extensions, HeaderMap, HeaderValue, Method, Request, Response, StatusCode, Uri, Version,
header::{CONTENT_ENCODING, CONTENT_LENGTH, CONTENT_TYPE, LOCATION, TRANSFER_ENCODING},
};
use http_body::Body;
use iri_string::types::{UriAbsoluteString, UriReferenceStr};
use pin_project_lite::pin_project;
use tower::{Service, util::Oneshot};
use super::{
BodyRepr, RequestUri,
policy::{Action, Attempt, Policy},
};
pin_project! {
/// Response future for [`FollowRedirectLayer`].
#[project = ResponseFutureProj]
pub enum ResponseFuture<S, B, P>
where
S: Service<Request<B>>,
{
Redirect {
#[pin]
future: Either<S::Future, Oneshot<S, Request<B>>>,
service: S,
policy: P,
method: Method,
uri: Uri,
version: Version,
headers: HeaderMap<HeaderValue>,
extensions: Extensions,
body: BodyRepr<B>,
},
Direct {
#[pin]
future: S::Future,
},
}
}
impl<S, ReqBody, ResBody, P> Future for ResponseFuture<S, ReqBody, P>
where
S: Service<Request<ReqBody>, Response = Response<ResBody>> + Clone,
ReqBody: Body + Default,
P: Policy<ReqBody, S::Error>,
{
type Output = Result<Response<ResBody>, S::Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.project() {
ResponseFutureProj::Redirect {
mut future,
service,
policy,
method,
uri,
version,
headers,
extensions,
body,
} => {
let mut res = ready!(future.as_mut().poll(cx)?);
res.extensions_mut().insert(RequestUri(uri.clone()));
let drop_payload_headers = |headers: &mut HeaderMap| {
for header in &[
CONTENT_TYPE,
CONTENT_LENGTH,
CONTENT_ENCODING,
TRANSFER_ENCODING,
] {
headers.remove(header);
}
};
match res.status() {
StatusCode::MOVED_PERMANENTLY | StatusCode::FOUND => {
// User agents MAY change the request method from POST to GET
// (RFC 7231 section 6.4.2. and 6.4.3.).
if *method == Method::POST {
*method = Method::GET;
*body = BodyRepr::Empty;
drop_payload_headers(headers);
}
}
StatusCode::SEE_OTHER => {
// A user agent can perform a GET or HEAD request (RFC 7231 section 6.4.4.).
if *method != Method::HEAD {
*method = Method::GET;
}
*body = BodyRepr::Empty;
drop_payload_headers(headers);
}
StatusCode::TEMPORARY_REDIRECT | StatusCode::PERMANENT_REDIRECT => {}
_ => return Poll::Ready(Ok(res)),
};
let take_body = if let Some(body) = body.take() {
body
} else {
return Poll::Ready(Ok(res));
};
let location = res
.headers()
.get(&LOCATION)
.and_then(|loc| resolve_uri(str::from_utf8(loc.as_bytes()).ok()?, uri));
let location = if let Some(loc) = location {
loc
} else {
return Poll::Ready(Ok(res));
};
let attempt = Attempt {
status: res.status(),
headers: res.headers(),
location: &location,
previous: uri,
};
match policy.redirect(&attempt)? {
Action::Follow => {
*uri = location;
body.try_clone_from(&take_body, &policy);
let mut req = Request::new(take_body);
*req.uri_mut() = uri.clone();
*req.method_mut() = method.clone();
*req.version_mut() = *version;
*req.headers_mut() = headers.clone();
*req.extensions_mut() = extensions.clone();
policy.on_request(&mut req);
future.set(Either::Right(Oneshot::new(service.clone(), req)));
cx.waker().wake_by_ref();
Poll::Pending
}
Action::Stop => Poll::Ready(Ok(res)),
}
}
ResponseFutureProj::Direct { mut future } => {
let res = ready!(future.as_mut().poll(cx)?);
Poll::Ready(Ok(res))
}
}
}
}
/// Try to resolve a URI reference `relative` against a base URI `base`.
fn resolve_uri(relative: &str, base: &Uri) -> Option<Uri> {
let relative = UriReferenceStr::new(relative).ok()?;
let base = UriAbsoluteString::try_from(base.to_string()).ok()?;
let uri = relative.resolve_against(&base).to_string();
Uri::try_from(uri).ok()
}