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
312
313
use crate::endpoint::error::InvalidEndpointError;
use crate::operation::error::BuildError;
use http::uri::{Authority, Uri};
use std::borrow::Cow;
use std::result::Result as StdResult;
use std::str::FromStr;
pub mod error;
pub mod middleware;
pub use error::ResolveEndpointError;
pub type Result = std::result::Result<aws_smithy_types::endpoint::Endpoint, ResolveEndpointError>;
pub trait ResolveEndpoint<Params>: Send + Sync {
fn resolve_endpoint(&self, params: &Params) -> Result;
}
impl<T> ResolveEndpoint<T> for &'static str {
fn resolve_endpoint(&self, _params: &T) -> Result {
Ok(aws_smithy_types::endpoint::Endpoint::builder()
.url(*self)
.build())
}
}
#[derive(Clone, Debug)]
#[deprecated(note = "Use `.endpoint_url(...)` directly instead")]
pub struct Endpoint {
uri: http::Uri,
immutable: bool,
}
#[allow(deprecated)]
impl<T> ResolveEndpoint<T> for Endpoint {
fn resolve_endpoint(&self, _params: &T) -> Result {
Ok(aws_smithy_types::endpoint::Endpoint::builder()
.url(self.uri.to_string())
.build())
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EndpointPrefix(String);
impl EndpointPrefix {
pub fn new(prefix: impl Into<String>) -> StdResult<Self, BuildError> {
let prefix = prefix.into();
match Authority::from_str(&prefix) {
Ok(_) => Ok(EndpointPrefix(prefix)),
Err(err) => Err(BuildError::invalid_uri(
prefix,
"invalid prefix".into(),
err,
)),
}
}
pub fn as_str(&self) -> &str {
&self.0
}
}
pub fn apply_endpoint(
uri: &mut Uri,
endpoint: &Uri,
prefix: Option<&EndpointPrefix>,
) -> StdResult<(), InvalidEndpointError> {
let prefix = prefix.map(|p| p.0.as_str()).unwrap_or("");
let authority = endpoint
.authority()
.as_ref()
.map(|auth| auth.as_str())
.unwrap_or("");
let authority = if !prefix.is_empty() {
Authority::from_str(&format!("{}{}", prefix, authority))
} else {
Authority::from_str(authority)
}
.map_err(InvalidEndpointError::failed_to_construct_authority)?;
let scheme = *endpoint
.scheme()
.as_ref()
.ok_or_else(InvalidEndpointError::endpoint_must_have_scheme)?;
let new_uri = Uri::builder()
.authority(authority)
.scheme(scheme.clone())
.path_and_query(merge_paths(endpoint, uri).as_ref())
.build()
.map_err(InvalidEndpointError::failed_to_construct_uri)?;
*uri = new_uri;
Ok(())
}
#[allow(deprecated)]
impl Endpoint {
pub fn mutable_uri(uri: Uri) -> StdResult<Self, InvalidEndpointError> {
Ok(Endpoint {
uri: Self::validate_endpoint(uri)?,
immutable: false,
})
}
pub fn mutable(uri: impl AsRef<str>) -> StdResult<Self, InvalidEndpointError> {
Self::mutable_uri(
Uri::try_from(uri.as_ref()).map_err(InvalidEndpointError::failed_to_construct_uri)?,
)
}
pub fn uri(&self) -> &Uri {
&self.uri
}
pub fn immutable_uri(uri: Uri) -> StdResult<Self, InvalidEndpointError> {
Ok(Endpoint {
uri: Self::validate_endpoint(uri)?,
immutable: true,
})
}
pub fn immutable(uri: impl AsRef<str>) -> StdResult<Self, InvalidEndpointError> {
Self::immutable_uri(
Uri::try_from(uri.as_ref()).map_err(InvalidEndpointError::failed_to_construct_uri)?,
)
}
pub fn set_endpoint(
&self,
uri: &mut http::Uri,
prefix: Option<&EndpointPrefix>,
) -> StdResult<(), InvalidEndpointError> {
let prefix = match self.immutable {
true => None,
false => prefix,
};
apply_endpoint(uri, &self.uri, prefix)
}
fn validate_endpoint(endpoint: Uri) -> StdResult<Uri, InvalidEndpointError> {
if endpoint.scheme().is_none() {
Err(InvalidEndpointError::endpoint_must_have_scheme())
} else {
Ok(endpoint)
}
}
}
fn merge_paths<'a>(endpoint: &'a Uri, uri: &'a Uri) -> Cow<'a, str> {
if let Some(query) = endpoint.path_and_query().and_then(|pq| pq.query()) {
tracing::warn!(query = %query, "query specified in endpoint will be ignored during endpoint resolution");
}
let endpoint_path = endpoint.path();
let uri_path_and_query = uri.path_and_query().map(|pq| pq.as_str()).unwrap_or("");
if endpoint_path.is_empty() {
Cow::Borrowed(uri_path_and_query)
} else {
let ep_no_slash = endpoint_path.strip_suffix('/').unwrap_or(endpoint_path);
let uri_path_no_slash = uri_path_and_query
.strip_prefix('/')
.unwrap_or(uri_path_and_query);
Cow::Owned(format!("{}/{}", ep_no_slash, uri_path_no_slash))
}
}
#[cfg(test)]
#[allow(deprecated)]
mod test {
use crate::endpoint::error::{InvalidEndpointError, InvalidEndpointErrorKind};
use crate::endpoint::{Endpoint, EndpointPrefix};
use http::Uri;
#[test]
fn prefix_endpoint() {
let ep = Endpoint::mutable("https://us-east-1.dynamo.amazonaws.com").unwrap();
let mut uri = Uri::from_static("/list_tables?k=v");
ep.set_endpoint(
&mut uri,
Some(&EndpointPrefix::new("subregion.").expect("valid prefix")),
)
.unwrap();
assert_eq!(
uri,
Uri::from_static("https://subregion.us-east-1.dynamo.amazonaws.com/list_tables?k=v")
);
}
#[test]
fn prefix_endpoint_custom_port() {
let ep = Endpoint::mutable("https://us-east-1.dynamo.amazonaws.com:6443").unwrap();
let mut uri = Uri::from_static("/list_tables?k=v");
ep.set_endpoint(
&mut uri,
Some(&EndpointPrefix::new("subregion.").expect("valid prefix")),
)
.unwrap();
assert_eq!(
uri,
Uri::from_static(
"https://subregion.us-east-1.dynamo.amazonaws.com:6443/list_tables?k=v"
)
);
}
#[test]
fn prefix_immutable_endpoint() {
let ep = Endpoint::immutable("https://us-east-1.dynamo.amazonaws.com").unwrap();
let mut uri = Uri::from_static("/list_tables?k=v");
ep.set_endpoint(
&mut uri,
Some(&EndpointPrefix::new("subregion.").expect("valid prefix")),
)
.unwrap();
assert_eq!(
uri,
Uri::from_static("https://us-east-1.dynamo.amazonaws.com/list_tables?k=v")
);
}
#[test]
fn endpoint_with_path() {
for uri in &[
"https://us-east-1.dynamo.amazonaws.com/private",
"https://us-east-1.dynamo.amazonaws.com/private/",
] {
let ep = Endpoint::immutable(uri).unwrap();
let mut uri = Uri::from_static("/list_tables?k=v");
ep.set_endpoint(
&mut uri,
Some(&EndpointPrefix::new("subregion.").expect("valid prefix")),
)
.unwrap();
assert_eq!(
uri,
Uri::from_static("https://us-east-1.dynamo.amazonaws.com/private/list_tables?k=v")
);
}
}
#[test]
fn set_endpoint_empty_path() {
let ep = Endpoint::immutable("http://localhost:8000").unwrap();
let mut uri = Uri::from_static("/");
ep.set_endpoint(&mut uri, None).unwrap();
assert_eq!(uri, Uri::from_static("http://localhost:8000/"))
}
#[test]
fn endpoint_construction_missing_scheme() {
assert!(matches!(
Endpoint::mutable("localhost:8000"),
Err(InvalidEndpointError {
kind: InvalidEndpointErrorKind::EndpointMustHaveScheme
})
));
assert!(matches!(
Endpoint::immutable("localhost:8000"),
Err(InvalidEndpointError {
kind: InvalidEndpointErrorKind::EndpointMustHaveScheme
})
));
}
}