canonical_request_string

Function canonical_request_string 

Source
pub fn canonical_request_string<S: AsRef<str>>(
    method: &str,
    url: &Url,
    canonical_headers: &[(S, &str)],
    payload_hash: &str,
) -> String
Expand description

Generate a canonical request. Assumes headers are already sorted and aws canonical.

NOTE: payload_hash might be “UNSIGNED-PAYLOAD” or sha256() of content, which can be different per request type.

canonical_request = method + ‘\n’ + canonical_uri + ‘\n’ + canonical_querystring + ‘\n’ + canonical_headers + ‘\n’ + signed_headers + ‘\n’ + payload_hash

§Examples

use url::Url;
use common_s3_headers::aws_canonical::to_canonical_headers;
use common_s3_headers::aws_format::canonical_request_string;

let url = Url::parse("https://examplebucket.s3.amazonaws.com/test.txt").unwrap();
let headers = vec![
 ("x-amz-date", "20130524T000000Z"),
 ("Range", "bytes=0-9"),
 ("Host", "examplebucket.s3.amazonaws.com"),
 ("x-amz-content-sha256", "UNSIGNED-PAYLOAD"),
];
let canonical_headers = to_canonical_headers(&headers);
let result = canonical_request_string("GET", &url, &canonical_headers, "UNSIGNED-PAYLOAD");
assert_eq!(
 result,
 "GET\n\
 /test.txt\n\
 \n\
 host:examplebucket.s3.amazonaws.com\n\
 range:bytes=0-9\n\
 x-amz-content-sha256:UNSIGNED-PAYLOAD\n\
 x-amz-date:20130524T000000Z\n\
 \n\
 host;range;x-amz-content-sha256;x-amz-date\n\
 UNSIGNED-PAYLOAD"
);

§See

  • https://docs.aws.amazon.com/AmazonS3/latest/userguide/RESTAuthentication.html#ConstructingTheAuthenticationHeader
  • https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
  • https://docs.aws.amazon.com/general/latest/gr/sigv4-create-string-to-sign.html
  • https://docs.aws.amazon.com/general/latest/gr/sigv4-add-signature-to-request.html