use std::borrow::Cow;
use std::collections::btree_map::Entry;
use std::collections::BTreeMap;
use std::fmt;
use std::str;
use std::time::Duration;
use base64;
use bytes::Bytes;
use hex;
use hmac::{Hmac, Mac};
use md5;
use sha2::{Digest, Sha256};
use time::now_utc;
use time::Tm;
use url::percent_encoding::{percent_decode, utf8_percent_encode, EncodeSet};
use crate::credential::AwsCredentials;
use crate::param::{Params, ServiceParams};
use crate::region::Region;
use crate::stream::ByteStream;
pub static UNSIGNED_PAYLOAD: &str = "UNSIGNED-PAYLOAD";
pub static EMPTY_SHA256_HASH: &str =
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
pub enum SignedRequestPayload {
Buffer(Bytes),
Stream(ByteStream),
}
impl fmt::Debug for SignedRequestPayload {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
SignedRequestPayload::Buffer(ref buf) => {
write!(f, "SignedRequestPayload::Buffer(len = {})", buf.len())
}
SignedRequestPayload::Stream(ref stream) => write!(
f,
"SignedRequestPayload::Stream(size_hint = {:?})",
stream.size_hint()
),
}
}
}
#[derive(Debug)]
pub struct SignedRequest {
pub method: String,
pub service: String,
pub region: Region,
pub path: String,
pub headers: BTreeMap<String, Vec<Vec<u8>>>,
pub params: Params,
pub scheme: Option<String>,
pub hostname: Option<String>,
pub payload: Option<SignedRequestPayload>,
pub canonical_query_string: String,
pub canonical_uri: String,
}
impl SignedRequest {
pub fn new(method: &str, service: &str, region: &Region, path: &str) -> SignedRequest {
SignedRequest {
method: method.to_string(),
service: service.to_string(),
region: region.clone(),
path: path.to_string(),
headers: BTreeMap::new(),
params: Params::new(),
scheme: None,
hostname: None,
payload: None,
canonical_query_string: String::new(),
canonical_uri: String::new(),
}
}
pub fn set_content_type(&mut self, content_type: String) {
self.add_header("content-type", &content_type);
}
pub fn set_hostname(&mut self, hostname: Option<String>) {
self.hostname = hostname;
}
pub fn set_endpoint_prefix(&mut self, endpoint_prefix: String) {
self.hostname = Some(build_hostname(&endpoint_prefix, &self.region));
}
pub fn set_payload<B: Into<Bytes>>(&mut self, payload: Option<B>) {
self.payload = payload.map(|chunk| SignedRequestPayload::Buffer(chunk.into()));
}
pub fn set_payload_stream(&mut self, stream: ByteStream) {
self.payload = Some(SignedRequestPayload::Stream(stream));
}
pub fn set_content_md5_header(&mut self) {
let digest;
if let Some(SignedRequestPayload::Buffer(ref payload)) = self.payload {
digest = Some(md5::compute(payload));
} else {
digest = None;
}
if let Some(digest) = digest {
self.add_header("Content-MD5", &base64::encode(&(*digest)));
}
}
pub fn method(&self) -> &str {
&self.method
}
pub fn path(&self) -> &str {
&self.path
}
pub fn canonical_path(&self) -> String {
canonical_uri(&self.path, &self.region)
}
pub fn canonical_uri(&self) -> &str {
&self.canonical_uri
}
pub fn canonical_query_string(&self) -> &str {
&self.canonical_query_string
}
pub fn headers(&self) -> &BTreeMap<String, Vec<Vec<u8>>> {
&self.headers
}
pub fn scheme(&self) -> String {
match self.scheme {
Some(ref p) => p.to_string(),
None => match self.region {
Region::Custom { ref endpoint, .. } => {
if endpoint.starts_with("http://") {
"http".to_owned()
} else {
"https".to_owned()
}
}
_ => "https".to_owned(),
},
}
}
pub fn hostname(&self) -> String {
match self.hostname {
Some(ref h) => h.to_string(),
None => build_hostname(&self.service, &self.region),
}
}
pub fn remove_header(&mut self, key: &str) {
let key_lower = key.to_ascii_lowercase().to_string();
self.headers.remove(&key_lower);
}
pub fn add_header<K: ToString>(&mut self, key: K, value: &str) {
let key_lower = key.to_string().to_ascii_lowercase();
let value_vec = value.as_bytes().to_vec();
match self.headers.entry(key_lower) {
Entry::Vacant(entry) => {
let mut values = Vec::new();
values.push(value_vec);
entry.insert(values);
}
Entry::Occupied(entry) => {
entry.into_mut().push(value_vec);
}
}
}
pub fn add_param<S>(&mut self, key: S, value: S)
where
S: Into<String>,
{
self.params.insert(key.into(), Some(value.into()));
}
pub fn set_params(&mut self, params: Params) {
self.params = params;
}
pub fn generate_presigned_url(
&mut self,
creds: &AwsCredentials,
expires_in: &Duration,
should_sha256_sign_payload: bool,
) -> String {
debug!("Presigning request URL");
self.sign(creds);
let hostname = match self.hostname {
Some(ref h) => h.to_string(),
None => build_hostname(&self.service, &self.region),
};
let current_time = now_utc();
let current_time_fmted = current_time.strftime("%Y%m%dT%H%M%SZ").unwrap();
let current_time_fmted = format!("{}", ¤t_time_fmted);
let current_date = current_time.strftime("%Y%m%d").unwrap();
self.remove_header("X-Amz-Content-Sha256");
self.remove_header("X-Amz-Date");
self.remove_header("Content-Type");
if let Some(ref token) = *creds.token() {
self.remove_header("X-Amz-Security-Token");
self.params
.put("X-Amz-Security-Token", encode_uri_strict(token));
}
self.remove_header("X-Amz-Algorithm");
self.params.put("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
self.remove_header("X-Amz-Credential");
self.params.put(
"X-Amz-Credential",
format!(
"{}/{}/{}/{}/aws4_request",
&creds.aws_access_key_id(),
¤t_date,
self.region.name(),
self.service
),
);
self.remove_header("X-Amz-Expires");
let expiration_time = format!("{}", expires_in.as_secs());
self.params.put("X-Amz-Expires", expiration_time);
self.canonical_uri = canonical_uri(&self.path, &self.region);
let canonical_headers = canonical_headers(&self.headers);
let signed_headers = signed_headers(&self.headers);
self.params.put("X-Amz-SignedHeaders", &signed_headers);
self.params.put("X-Amz-Date", current_time_fmted);
self.canonical_query_string = build_canonical_query_string(&self.params);
debug!("canonical_uri: {:?}", self.canonical_uri);
debug!("canonical_headers: {:?}", canonical_headers);
debug!("signed_headers: {:?}", signed_headers);
debug!("canonical_query_string: {:?}", self.canonical_query_string);
let payload = if should_sha256_sign_payload {
match self.payload {
None => {
Cow::Borrowed(EMPTY_SHA256_HASH)
}
Some(SignedRequestPayload::Buffer(ref payload)) => {
let (digest, _len) = digest_payload(&payload);
Cow::Owned(digest)
}
Some(SignedRequestPayload::Stream(ref _stream)) => {
Cow::Borrowed(UNSIGNED_PAYLOAD)
}
}
} else {
Cow::Borrowed(UNSIGNED_PAYLOAD)
};
let canonical_request = format!(
"{}\n{}\n{}\n{}\n{}\n{}",
&self.method,
self.canonical_uri,
self.canonical_query_string,
canonical_headers,
&signed_headers,
payload
);
debug!("canonical_request: {:?}", canonical_request);
let hashed_canonical_request = to_hexdigest(&canonical_request);
debug!("hashed_canonical_request: {:?}", hashed_canonical_request);
let scope = format!(
"{}/{}/{}/aws4_request",
current_date,
self.region.name(),
&self.service
);
debug!("scope: {}", scope);
let string_to_sign = string_to_sign(current_time, &hashed_canonical_request, &scope);
debug!("string_to_sign: {}", string_to_sign);
let signature = sign_string(
&string_to_sign,
creds.aws_secret_access_key(),
current_time,
&self.region.name(),
&self.service,
);
self.params.put("X-Amz-Signature", signature);
format!(
"{}://{}{}?{}",
self.scheme(),
hostname,
self.canonical_uri,
build_canonical_query_string(&self.params)
)
}
pub fn sign(&mut self, creds: &AwsCredentials) {
self.sign_with_plus(creds, false)
}
pub fn sign_with_plus(&mut self, creds: &AwsCredentials, should_treat_plus_literally: bool) {
debug!("Creating request to send to AWS.");
let hostname = match self.hostname {
Some(ref h) => h.to_string(),
None => build_hostname(&self.service, &self.region),
};
self.remove_header("host");
self.add_header("host", &hostname);
if let Some(ref token) = *creds.token() {
self.remove_header("X-Amz-Security-Token");
self.add_header("X-Amz-Security-Token", token);
}
self.canonical_query_string =
build_canonical_query_string_with_plus(&self.params, should_treat_plus_literally);
let date = now_utc();
self.remove_header("x-amz-date");
self.add_header(
"x-amz-date",
&date.strftime("%Y%m%dT%H%M%SZ").unwrap().to_string(),
);
if let Entry::Vacant(entry) = self.headers.entry("content-type".to_owned()) {
let mut values = Vec::new();
values.push(b"application/octet-stream".to_vec());
entry.insert(values);
}
let signed_headers = signed_headers(&self.headers);
self.canonical_uri = canonical_uri(&self.path, &self.region);
let canonical_uri = if &self.service != "s3" {
utf8_percent_encode(&self.canonical_uri, StrictPathEncodeSet).collect::<String>()
} else {
self.canonical_uri.clone()
};
let canonical_headers = canonical_headers(&self.headers);
let canonical_request: String;
let (digest, len) = match self.payload {
None => {
canonical_request = format!(
"{}\n{}\n{}\n{}\n{}\n{}",
&self.method,
canonical_uri,
self.canonical_query_string,
canonical_headers,
signed_headers,
EMPTY_SHA256_HASH
);
(Some(Cow::Borrowed(EMPTY_SHA256_HASH)), Some(0))
}
Some(SignedRequestPayload::Buffer(ref payload)) => {
let (digest, len) = digest_payload(&payload);
canonical_request = format!(
"{}\n{}\n{}\n{}\n{}\n{}",
&self.method,
canonical_uri,
self.canonical_query_string,
canonical_headers,
signed_headers,
&digest
);
(Some(Cow::Owned(digest)), Some(len))
}
Some(SignedRequestPayload::Stream(ref stream)) => {
canonical_request = format!(
"{}\n{}\n{}\n{}\n{}\n{}",
&self.method,
canonical_uri,
self.canonical_query_string,
canonical_headers,
signed_headers,
UNSIGNED_PAYLOAD
);
(Some(Cow::Borrowed(UNSIGNED_PAYLOAD)), stream.size_hint())
}
};
if let Some(digest) = digest {
self.remove_header("x-amz-content-sha256");
self.add_header("x-amz-content-sha256", &digest);
}
if let Some(len) = len {
self.remove_header("content-length");
self.add_header("content-length", &format!("{}", len));
}
let hashed_canonical_request = to_hexdigest(&canonical_request);
let scope = format!(
"{}/{}/{}/aws4_request",
date.strftime("%Y%m%d").unwrap(),
self.region.name(),
&self.service
);
let string_to_sign = string_to_sign(date, &hashed_canonical_request, &scope);
let signature = sign_string(
&string_to_sign,
creds.aws_secret_access_key(),
date,
&self.region.name(),
&self.service,
);
let auth_header = format!(
"AWS4-HMAC-SHA256 Credential={}/{}, SignedHeaders={}, Signature={}",
&creds.aws_access_key_id(),
scope,
signed_headers,
signature
);
self.remove_header("authorization");
self.add_header("authorization", &auth_header);
}
}
fn digest_payload(payload: &[u8]) -> (String, usize) {
let digest = to_hexdigest(payload);
let len = payload.len();
(digest, len)
}
#[inline]
fn hmac(secret: &[u8], message: &[u8]) -> Hmac<Sha256> {
let mut hmac = Hmac::<Sha256>::new(secret).expect("failed to create hmac");
hmac.input(message);
hmac
}
fn sign_string(
string_to_sign: &str,
secret: &str,
date: Tm,
region: &str,
service: &str,
) -> String {
let date_str = date.strftime("%Y%m%d").unwrap().to_string();
let date_hmac = hmac(format!("AWS4{}", secret).as_bytes(), date_str.as_bytes())
.result()
.code();
let region_hmac = hmac(date_hmac.as_ref(), region.as_bytes()).result().code();
let service_hmac = hmac(region_hmac.as_ref(), service.as_bytes())
.result()
.code();
let signing_hmac = hmac(service_hmac.as_ref(), b"aws4_request").result().code();
hex::encode(
hmac(signing_hmac.as_ref(), string_to_sign.as_bytes())
.result()
.code()
.as_ref(),
)
}
pub fn string_to_sign(date: Tm, hashed_canonical_request: &str, scope: &str) -> String {
format!(
"AWS4-HMAC-SHA256\n{}\n{}\n{}",
date.strftime("%Y%m%dT%H%M%SZ").unwrap(),
scope,
hashed_canonical_request
)
}
fn signed_headers(headers: &BTreeMap<String, Vec<Vec<u8>>>) -> String {
let mut signed = String::new();
headers
.iter()
.filter(|&(ref key, _)| !skipped_headers(&key))
.for_each(|(key, _)| {
if !signed.is_empty() {
signed.push(';');
}
signed.push_str(key);
});
signed
}
fn canonical_headers(headers: &BTreeMap<String, Vec<Vec<u8>>>) -> String {
let mut canonical = String::new();
for (key, value) in headers.iter() {
if skipped_headers(key) {
continue;
}
canonical.push_str(format!("{}:{}\n", key, canonical_values(value)).as_ref());
}
canonical
}
fn canonical_values(values: &[Vec<u8>]) -> String {
let mut st = String::new();
for v in values {
let s = str::from_utf8(v).unwrap();
if !st.is_empty() {
st.push(',')
}
if s.starts_with('\"') {
st.push_str(s);
} else {
st.push_str(s.replace(" ", " ").trim());
}
}
st
}
fn skipped_headers(header: &str) -> bool {
["authorization", "content-length", "user-agent"].contains(&header)
}
fn canonical_uri(path: &str, region: &Region) -> String {
let endpoint_path = match region {
Region::Custom { ref endpoint, .. } => extract_endpoint_path(endpoint),
_ => None,
};
match (endpoint_path, path) {
(Some(prefix), "") => prefix.to_string(),
(None, "") => "/".to_string(),
(Some(prefix), _) => encode_uri_path(&(prefix.to_owned() + path)),
_ => encode_uri_path(path),
}
}
fn build_canonical_query_string(params: &Params) -> String {
build_canonical_query_string_with_plus(params, false)
}
fn build_canonical_query_string_with_plus(
params: &Params,
should_treat_plus_literally: bool,
) -> String {
if params.is_empty() {
return String::new();
}
let mut output = String::new();
for (key, val) in params.iter() {
if !output.is_empty() {
output.push_str("&");
}
if should_treat_plus_literally {
output.push_str(&encode_uri_strict(&key));
} else {
output.push_str(&encode_uri_strict(&key.replace("+", " ")));
}
output.push_str("=");
if let Some(ref unwrapped_val) = *val {
if should_treat_plus_literally {
output.push_str(&encode_uri_strict(&unwrapped_val));
} else {
output.push_str(&encode_uri_strict(&unwrapped_val.replace("+", " ")));
}
}
}
output
}
#[derive(Clone)]
pub struct StrictEncodeSet;
impl EncodeSet for StrictEncodeSet {
#[inline]
fn contains(&self, byte: u8) -> bool {
let upper = byte >= 0x41 && byte <= 0x5a;
let lower = byte >= 0x61 && byte <= 0x7a;
let numeric = byte >= 0x30 && byte <= 0x39;
let hyphen = byte == 0x2d;
let underscore = byte == 0x5f;
let tilde = byte == 0x7e;
let period = byte == 0x2e;
!(upper || lower || numeric || hyphen || underscore || tilde || period)
}
}
#[derive(Clone)]
pub struct StrictPathEncodeSet;
impl EncodeSet for StrictPathEncodeSet {
#[inline]
fn contains(&self, byte: u8) -> bool {
let slash = byte == b'/';
!slash && StrictEncodeSet.contains(byte)
}
}
#[inline]
#[doc(hidden)]
pub fn encode_uri_path(uri: &str) -> String {
utf8_percent_encode(uri, StrictPathEncodeSet).collect::<String>()
}
#[inline]
fn encode_uri_strict(uri: &str) -> String {
utf8_percent_encode(&decode_uri(uri), StrictEncodeSet).collect::<String>()
}
#[inline]
#[doc(hidden)]
pub fn decode_uri(uri: &str) -> String {
let decoder = percent_decode(uri.as_bytes());
if let Ok(decoded) = decoder.decode_utf8() {
decoded.to_string()
} else {
uri.to_owned()
}
}
fn to_hexdigest<T: AsRef<[u8]>>(t: T) -> String {
let h = Sha256::digest(t.as_ref());
hex::encode(h.as_ref())
}
fn extract_endpoint_path(endpoint: &str) -> Option<&str> {
extract_endpoint_components(endpoint).1
}
fn extract_endpoint_components(endpoint: &str) -> (&str, Option<&str>) {
let unschemed = endpoint
.find("://")
.map(|p| &endpoint[p + 3..])
.unwrap_or(endpoint);
unschemed
.find('/')
.map(|p| (&unschemed[..p], Some(&unschemed[p..])))
.unwrap_or((unschemed, None))
}
fn extract_hostname(endpoint: &str) -> &str {
extract_endpoint_components(endpoint).0
}
fn build_hostname(service: &str, region: &Region) -> String {
match service {
"iam" => match *region {
Region::Custom { ref endpoint, .. } => extract_hostname(endpoint).to_owned(),
Region::CnNorth1 | Region::CnNorthwest1 => {
format!("{}.{}.amazonaws.com.cn", service, region.name())
}
_ => format!("{}.amazonaws.com", service),
},
"chime" => match *region {
Region::Custom { ref endpoint, .. } => extract_hostname(endpoint).to_owned(),
_ => format!("service.{}.aws.amazon.com", service),
},
"cloudfront" => match *region {
Region::Custom { ref endpoint, .. } => extract_hostname(endpoint).to_owned(),
_ => format!("{}.amazonaws.com", service),
},
"importexport" => match *region {
Region::Custom { ref endpoint, .. } => extract_hostname(endpoint).to_owned(),
_ => "importexport.amazonaws.com".to_owned(),
},
"s3" => match *region {
Region::Custom { ref endpoint, .. } => extract_hostname(endpoint).to_owned(),
Region::UsEast1 => "s3.amazonaws.com".to_string(),
Region::CnNorth1 | Region::CnNorthwest1 => {
format!("s3.{}.amazonaws.com.cn", region.name())
}
_ => format!("s3-{}.amazonaws.com", region.name()),
},
"route53" => match *region {
Region::Custom { ref endpoint, .. } => extract_hostname(endpoint).to_owned(),
_ => "route53.amazonaws.com".to_owned(),
},
"sdb" => match *region {
Region::Custom { ref endpoint, .. } => extract_hostname(endpoint).to_owned(),
Region::UsEast1 => "sdb.amazonaws.com".to_string(),
_ => format!("sdb.{}.amazonaws.com", region.name()),
},
_ => match *region {
Region::Custom { ref endpoint, .. } => extract_hostname(endpoint).to_owned(),
Region::CnNorth1 | Region::CnNorthwest1 => {
format!("{}.{}.amazonaws.com.cn", service, region.name())
}
_ => format!("{}.{}.amazonaws.com", service, region.name()),
},
}
}
#[cfg(test)]
mod tests {
use futures::Future;
use std::collections::BTreeMap;
use time::empty_tm;
use crate::credential::{ProfileProvider, ProvideAwsCredentials};
use crate::param::Params;
use crate::Region;
use super::{build_canonical_query_string, SignedRequest};
#[test]
fn get_hostname_none_present() {
let request = SignedRequest::new("POST", "sqs", &Region::UsEast1, "/");
assert_eq!("sqs.us-east-1.amazonaws.com", request.hostname());
}
#[test]
fn path_percent_encoded() {
let provider = ProfileProvider::with_configuration(
"test_resources/multiple_profile_credentials",
"foo",
);
let mut request = SignedRequest::new(
"GET",
"s3",
&Region::UsEast1,
"/path with spaces: the sequel",
);
request.sign(provider.credentials().wait().as_ref().unwrap());
assert_eq!(
"/path%20with%20spaces%3A%20the%20sequel",
request.canonical_uri()
);
}
#[test]
fn query_encoding_escaped_chars() {
query_encoding_escaped_chars_range(0u8, 45u8);
query_encoding_escaped_chars_range(47u8, 48u8);
query_encoding_escaped_chars_range(58u8, 65u8);
query_encoding_escaped_chars_range(91u8, 95u8);
query_encoding_escaped_chars_range(96u8, 97u8);
query_encoding_escaped_chars_range(123u8, 126u8);
query_encoding_escaped_chars_range(127u8, 128u8);
}
fn query_encoding_escaped_chars_range(start: u8, end: u8) {
let mut params = Params::new();
for code in start..end {
params.insert("k".to_owned(), Some((code as char).to_string()));
let enc = build_canonical_query_string(¶ms);
let expected = if (code as char) == '+' {
"k=%20".to_owned()
} else {
format!("k=%{:02X}", code)
};
assert_eq!(expected, enc);
}
}
#[test]
fn query_string_encoding_outliers() {
let mut request = SignedRequest::new(
"GET",
"s3",
&Region::UsEast1,
"/pathwith%20already%20existing%20encoding and some not encoded values",
);
request.add_param("arg1%7B", "arg1%7B");
request.add_param("arg2%7B+%2B", "+%2B");
assert_eq!(
super::build_canonical_query_string(&request.params),
"arg1%7B=arg1%7B&arg2%7B%20%2B=%20%2B"
);
assert_eq!(
super::canonical_uri(&request.path, &Region::default()),
"/pathwith%2520already%2520existing%2520encoding%20and%20some%20not%20encoded%20values"
);
}
#[test]
fn query_percent_encoded() {
let mut request = SignedRequest::new(
"GET",
"s3",
&Region::UsEast1,
"/path with spaces: the sequel++",
);
request.add_param(
"key:with@funny&characters",
"value with/funny%characters/Рускии",
);
let canonical_query_string = super::build_canonical_query_string(&request.params);
assert_eq!("key%3Awith%40funny%26characters=value%20with%2Ffunny%25characters%2F%D0%A0%D1%83%D1%81%D0%BA%D0%B8%D0%B8",
canonical_query_string);
let canonical_uri_string = super::canonical_uri(&request.path, &Region::default());
assert_eq!(
"/path%20with%20spaces%3A%20the%20sequel%2B%2B",
canonical_uri_string
);
}
#[test]
fn signature_generation() {
let signature_foo = super::sign_string(
"foo",
"wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
empty_tm(),
"us-west-1",
"s3",
);
assert_eq!(
signature_foo,
"29673d1d856a7684ff6f0f53c542bae0bfbb1e564f531aff7568be9fd206383b".to_string()
);
let signature_bar = super::sign_string(
"bar",
"wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
empty_tm(),
"us-west-1",
"s3",
);
assert_eq!(
signature_bar,
"2ba6879cd9e769d73df721dc90aafdaa843005d23f5b6c91d0744f804962e44f".to_string()
);
}
#[test]
fn signed_headers_unsigned_first() {
let mut headers = BTreeMap::new();
headers.insert("content-length".to_owned(), vec![vec![]]);
headers.insert("content-type".to_owned(), vec![vec![]]);
headers.insert("x-amz-date".to_owned(), vec![vec![]]);
assert_eq!(super::signed_headers(&headers), "content-type;x-amz-date");
}
#[test]
fn signed_headers_unsigned_in_center() {
let mut headers = BTreeMap::new();
headers.insert("cache-control".to_owned(), vec![vec![]]);
headers.insert("content-length".to_owned(), vec![vec![]]);
headers.insert("content-type".to_owned(), vec![vec![]]);
headers.insert("host".to_owned(), vec![vec![]]);
headers.insert("x-amz-date".to_owned(), vec![vec![]]);
assert_eq!(
super::signed_headers(&headers),
"cache-control;content-type;host;x-amz-date"
);
}
#[test]
fn signed_headers_unsigned_last() {
let mut headers = BTreeMap::new();
headers.insert("cache-control".to_owned(), vec![vec![]]);
headers.insert("content-length".to_owned(), vec![vec![]]);
assert_eq!(super::signed_headers(&headers), "cache-control");
}
#[test]
fn canonical_uri_combos() {
assert_eq!(super::canonical_uri("", &Region::default()), "/");
assert_eq!(super::canonical_uri("/foo", &Region::default()), "/foo");
assert_eq!(
super::canonical_uri(
"",
&Region::Custom {
name: Region::UsEast1.name().into(),
endpoint: "http://localhost:8000/path".into()
}
),
"/path"
);
assert_eq!(
super::canonical_uri(
"/foo",
&Region::Custom {
name: Region::UsEast1.name().into(),
endpoint: "http://localhost:8000/path".into()
}
),
"/path/foo"
);
assert_eq!(
super::canonical_uri(
"/foo",
&Region::Custom {
name: Region::UsEast1.name().into(),
endpoint: "http://localhost:8000".into()
}
),
"/foo"
);
}
#[test]
fn extract_hostname() {
assert_eq!(
super::extract_hostname("hostname.with.no.scheme"),
"hostname.with.no.scheme"
);
assert_eq!(
super::extract_hostname("http://hostname.with.scheme"),
"hostname.with.scheme"
);
assert_eq!(
super::extract_hostname("https://hostname.with.scheme"),
"hostname.with.scheme"
);
assert_eq!(
super::extract_hostname("hostname.with.no.scheme/test"),
"hostname.with.no.scheme"
);
assert_eq!(
super::extract_hostname("http://hostname.with.scheme/test"),
"hostname.with.scheme"
);
assert_eq!(
super::extract_hostname("https://hostname.with.scheme/test"),
"hostname.with.scheme"
);
}
}