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
use crate::s3::error::Error;
use crate::s3::utils::{to_query_string, Multimap};
use derivative::Derivative;
use hyper::http::Method;
use hyper::Uri;
use std::fmt;
#[derive(Derivative)]
#[derivative(Clone, Debug, Default)]
pub struct Url {
#[derivative(Default(value = "true"))]
pub https: bool,
pub host: String,
pub port: u16,
pub path: String,
pub query: Multimap,
}
impl Url {
pub fn host_header_value(&self) -> String {
if self.port > 0 {
return format!("{}:{}", self.host, self.port);
}
return self.host.clone();
}
}
impl fmt::Display for Url {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.host.is_empty() {
return Err(std::fmt::Error);
}
if self.https {
f.write_str("https://")?;
} else {
f.write_str("http://")?;
}
if self.port > 0 {
f.write_str(format!("{}:{}", self.host, self.port).as_str())?;
} else {
f.write_str(&self.host)?;
}
if !self.path.starts_with("/") {
f.write_str("/")?;
}
f.write_str(&self.path)?;
if !self.query.is_empty() {
f.write_str("?")?;
f.write_str(&to_query_string(&self.query))?;
}
Ok(())
}
}
fn extract_region(host: &str) -> String {
let tokens: Vec<&str> = host.split('.').collect();
let region = match tokens.get(1) {
Some(r) => match *r {
"dualstack" => match tokens.get(2) {
Some(t) => t,
_ => "",
},
"amazonaws" => "",
_ => r,
},
_ => "",
};
return region.to_string();
}
#[derive(Derivative)]
#[derivative(Clone, Debug, Default)]
pub struct BaseUrl {
#[derivative(Default(value = "true"))]
pub https: bool,
host: String,
port: u16,
pub region: String,
pub aws_host: bool,
accelerate_host: bool,
dualstack_host: bool,
virtual_style: bool,
}
impl BaseUrl {
pub fn build_url(
&self,
method: &Method,
region: &String,
query: &Multimap,
bucket_name: Option<&str>,
object_name: Option<&str>,
) -> Result<Url, Error> {
if !object_name.map_or(true, |v| v.is_empty()) {
if bucket_name.map_or(true, |v| v.is_empty()) {
return Err(Error::UrlBuildError(String::from(
"empty bucket name provided for object name",
)));
}
}
let mut url = Url::default();
url.https = self.https;
url.host = self.host.clone();
url.port = self.port;
url.query = query.clone();
if bucket_name.is_none() {
url.path.push_str("/");
if self.aws_host {
url.host = format!("s3.{}.{}", region, self.host);
}
return Ok(url);
}
let bucket = bucket_name.unwrap();
let enforce_path_style = true &&
(method == Method::PUT && object_name.is_none() && query.is_empty()) ||
query.contains_key("location") ||
(bucket.contains('.') && self.https);
if self.aws_host {
let mut s3_domain = "s3.".to_string();
if self.accelerate_host {
if bucket.contains('.') {
return Err(Error::UrlBuildError(String::from(
"bucket name with '.' is not allowed for accelerate endpoint",
)));
}
if !enforce_path_style {
s3_domain = "s3-accelerate.".to_string();
}
}
if self.dualstack_host {
s3_domain.push_str("dualstack.");
}
if enforce_path_style || !self.accelerate_host {
s3_domain.push_str(region);
s3_domain.push_str(".");
}
url.host = s3_domain + &url.host;
}
if enforce_path_style || !self.virtual_style {
url.path.push_str("/");
url.path.push_str(bucket);
} else {
url.host = format!("{}.{}", bucket, url.host);
}
if object_name.is_some() {
if object_name.unwrap().chars().nth(0) != Some('/') {
url.path.push_str("/");
}
url.path.push_str(object_name.unwrap());
}
return Ok(url);
}
pub fn from_string(s: String) -> Result<BaseUrl, Error> {
let url = s.parse::<Uri>()?;
let https = match url.scheme() {
None => true,
Some(scheme) => match scheme.as_str() {
"http" => false,
"https" => true,
_ => {
return Err(Error::InvalidBaseUrl(String::from(
"scheme must be http or https",
)))
}
},
};
let mut host = match url.host() {
Some(h) => h,
_ => {
return Err(Error::InvalidBaseUrl(String::from(
"valid host must be provided",
)))
}
};
let ipv6host = "[".to_string() + host + "]";
if host.parse::<std::net::Ipv6Addr>().is_ok() {
host = &ipv6host;
}
let mut port = match url.port() {
Some(p) => p.as_u16(),
_ => 0u16,
};
if (https && port == 443) || (!https && port == 80) {
port = 0u16;
}
if url.path() != "/" && url.path() != "" {
return Err(Error::InvalidBaseUrl(String::from(
"path must be empty for base URL",
)));
}
if !url.query().is_none() {
return Err(Error::InvalidBaseUrl(String::from(
"query must be none for base URL",
)));
}
let mut accelerate_host = host.starts_with("s3-accelerate.");
let aws_host = (host.starts_with("s3.") || accelerate_host)
&& (host.ends_with(".amazonaws.com") || host.ends_with(".amazonaws.com.cn"));
let virtual_style = aws_host || host.ends_with("aliyuncs.com");
let mut region = String::new();
let mut dualstack_host = false;
if aws_host {
let mut aws_domain = "amazonaws.com";
region = extract_region(host);
let is_aws_china_host = host.ends_with(".cn");
if is_aws_china_host {
aws_domain = "amazonaws.com.cn";
if region.is_empty() {
return Err(Error::InvalidBaseUrl(String::from(
"region must be provided in Amazon S3 China endpoint",
)));
}
}
dualstack_host = host.contains(".dualstack.");
host = aws_domain;
} else {
accelerate_host = false;
}
return Ok(BaseUrl {
https: https,
host: host.to_string(),
port: port,
region: region,
aws_host: aws_host,
accelerate_host: accelerate_host,
dualstack_host: dualstack_host,
virtual_style: virtual_style,
});
}
}