Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

declare const headers #117

Merged
merged 3 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dotenv = { version = "0.15.0", optional = true }
futures = "0.3.21"
hex-simd = "0.5.0"
hmac = "0.12.1"
http = "0.2.7"
httparse = "1.7.0"
hyper = { version = "0.14.18", features = ["server"] }
md-5 = "0.10.1"
Expand Down
8 changes: 3 additions & 5 deletions src/headers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Common Request Headers
#![allow(clippy::borrow_interior_mutable_const)] // See https://github.com/rust-lang/rust-clippy/issues/5812

mod amz_content_sha256;
mod amz_copy_source;
Expand All @@ -14,16 +15,13 @@ pub use self::range::Range;

pub use hyper::header::*;

// FIXME: declare const headers, see <https://github.com/hyperium/http/issues/264>

use once_cell::sync::Lazy;

/// declare http header names
macro_rules! declare_header_name{
{$($(#[$docs:meta])* $n:ident: $s:expr;)+} => {
$(
$(#[$docs])*
pub static $n: Lazy<HeaderName> = Lazy::new(||HeaderName::from_static($s));
#[allow(clippy::declare_interior_mutable_const)] // See https://github.com/rust-lang/rust-clippy/issues/5812
pub const $n: HeaderName = HeaderName::from_static($s);
)+

#[test]
Expand Down
12 changes: 6 additions & 6 deletions src/ops/complete_multipart_upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async fn extract(ctx: &mut ReqContext<'_>) -> S3Result<CompleteMultipartUploadRe
};

let h = &ctx.headers;
h.assign_str(&*X_AMZ_REQUEST_PAYER, &mut input.request_payer);
h.assign_str(X_AMZ_REQUEST_PAYER, &mut input.request_payer);

Ok(input)
}
Expand All @@ -76,14 +76,14 @@ impl S3Output for CompleteMultipartUploadOutput {
#[allow(clippy::shadow_unrelated)]
fn try_into_response(self) -> S3Result<Response> {
wrap_internal_error(|res| {
res.set_optional_header(&*X_AMZ_EXPIRATION, self.expiration)?;
res.set_optional_header(&*X_AMZ_SERVER_SIDE_ENCRYPTION, self.server_side_encryption)?;
res.set_optional_header(&*X_AMZ_VERSION_ID, self.version_id)?;
res.set_optional_header(X_AMZ_EXPIRATION, self.expiration)?;
res.set_optional_header(X_AMZ_SERVER_SIDE_ENCRYPTION, self.server_side_encryption)?;
res.set_optional_header(X_AMZ_VERSION_ID, self.version_id)?;
res.set_optional_header(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_AWS_KMS_KEY_ID,
X_AMZ_SERVER_SIDE_ENCRYPTION_AWS_KMS_KEY_ID,
self.ssekms_key_id,
)?;
res.set_optional_header(&*X_AMZ_REQUEST_CHARGED, self.request_charged)?;
res.set_optional_header(X_AMZ_REQUEST_CHARGED, self.request_charged)?;

let location = self.location;
let bucket = self.bucket;
Expand Down
81 changes: 39 additions & 42 deletions src/ops/copy_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl S3Handler for Handler {
fn is_match(&self, ctx: &'_ ReqContext<'_>) -> bool {
bool_try!(ctx.req.method() == Method::PUT);
bool_try!(ctx.path.is_object());
ctx.headers.get(&*X_AMZ_COPY_SOURCE).is_some()
ctx.headers.get(X_AMZ_COPY_SOURCE).is_some()
}

async fn handle(
Expand All @@ -51,7 +51,7 @@ impl S3Handler for Handler {
/// extract operation request
fn extract(ctx: &mut ReqContext<'_>) -> S3Result<CopyObjectRequest> {
let (bucket, key) = ctx.unwrap_object_path();
let copy_source = ctx.unwrap_header(&*X_AMZ_COPY_SOURCE);
let copy_source = ctx.unwrap_header(X_AMZ_COPY_SOURCE);

AmzCopySource::try_match(copy_source)
.map_err(|err| invalid_request!("Invalid header: x-amz-copy-source", err))?;
Expand All @@ -64,85 +64,82 @@ fn extract(ctx: &mut ReqContext<'_>) -> S3Result<CopyObjectRequest> {
};

let h = &ctx.headers;
h.assign_str(&*X_AMZ_ACL, &mut input.acl);
h.assign_str(X_AMZ_ACL, &mut input.acl);
h.assign_str(CACHE_CONTROL, &mut input.cache_control);
h.assign_str(CONTENT_DISPOSITION, &mut input.content_disposition);
h.assign_str(CONTENT_ENCODING, &mut input.content_encoding);
h.assign_str(CONTENT_LANGUAGE, &mut input.content_language);
h.assign_str(CONTENT_TYPE, &mut input.content_type);
h.assign_str(X_AMZ_COPY_SOURCE_IF_MATCH, &mut input.copy_source_if_match);
h.assign_str(
&*X_AMZ_COPY_SOURCE_IF_MATCH,
&mut input.copy_source_if_match,
);
h.assign_str(
&*X_AMZ_COPY_SOURCE_IF_MODIFIED_SINCE,
X_AMZ_COPY_SOURCE_IF_MODIFIED_SINCE,
&mut input.copy_source_if_modified_since,
);
h.assign_str(
&*X_AMZ_COPY_SOURCE_IF_NONE_MATCH,
X_AMZ_COPY_SOURCE_IF_NONE_MATCH,
&mut input.copy_source_if_none_match,
);
h.assign_str(
&*X_AMZ_COPY_SOURCE_IF_UNMODIFIED_SINCE,
X_AMZ_COPY_SOURCE_IF_UNMODIFIED_SINCE,
&mut input.copy_source_if_unmodified_since,
);
h.assign_str(EXPIRES, &mut input.expires);
h.assign_str(&*X_AMZ_GRANT_FULL_CONTROL, &mut input.grant_full_control);
h.assign_str(&*X_AMZ_GRANT_READ, &mut input.grant_read);
h.assign_str(&*X_AMZ_GRANT_READ_ACP, &mut input.grant_read_acp);
h.assign_str(&*X_AMZ_GRANT_WRITE_ACP, &mut input.grant_write_acp);
h.assign_str(&*X_AMZ_METADATA_DIRECTIVE, &mut input.metadata_directive);
h.assign_str(&*X_AMZ_TAGGING_DIRECTIVE, &mut input.tagging_directive);
h.assign_str(
&*X_AMZ_SERVER_SIDE_ENCRYPTION,
h.assign_str(X_AMZ_GRANT_FULL_CONTROL, &mut input.grant_full_control);
h.assign_str(X_AMZ_GRANT_READ, &mut input.grant_read);
h.assign_str(X_AMZ_GRANT_READ_ACP, &mut input.grant_read_acp);
h.assign_str(X_AMZ_GRANT_WRITE_ACP, &mut input.grant_write_acp);
h.assign_str(X_AMZ_METADATA_DIRECTIVE, &mut input.metadata_directive);
h.assign_str(X_AMZ_TAGGING_DIRECTIVE, &mut input.tagging_directive);
h.assign_str(
X_AMZ_SERVER_SIDE_ENCRYPTION,
&mut input.server_side_encryption,
);
h.assign_str(&*X_AMZ_STORAGE_CLASS, &mut input.storage_class);
h.assign_str(X_AMZ_STORAGE_CLASS, &mut input.storage_class);
h.assign_str(
&*X_AMZ_WEBSITE_REDIRECT_LOCATION,
X_AMZ_WEBSITE_REDIRECT_LOCATION,
&mut input.website_redirect_location,
);
h.assign_str(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM,
X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM,
&mut input.sse_customer_algorithm,
);
h.assign_str(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY,
X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY,
&mut input.sse_customer_key,
);
h.assign_str(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5,
X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5,
&mut input.sse_customer_key_md5,
);
h.assign_str(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_AWS_KMS_KEY_ID,
X_AMZ_SERVER_SIDE_ENCRYPTION_AWS_KMS_KEY_ID,
&mut input.ssekms_key_id,
);
h.assign_str(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_CONTEXT,
X_AMZ_SERVER_SIDE_ENCRYPTION_CONTEXT,
&mut input.ssekms_encryption_context,
);
h.assign_str(
&*X_AMZ_COPY_SOURCE_SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM,
X_AMZ_COPY_SOURCE_SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM,
&mut input.copy_source_sse_customer_algorithm,
);
h.assign_str(
&*X_AMZ_COPY_SOURCE_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY,
X_AMZ_COPY_SOURCE_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY,
&mut input.copy_source_sse_customer_key,
);
h.assign_str(
&*X_AMZ_COPY_SOURCE_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5,
X_AMZ_COPY_SOURCE_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5,
&mut input.copy_source_sse_customer_key_md5,
);
h.assign_str(&*X_AMZ_REQUEST_PAYER, &mut input.request_payer);
h.assign_str(&*X_AMZ_TAGGING, &mut input.tagging);
h.assign_str(&*X_AMZ_OBJECT_LOCK_MODE, &mut input.object_lock_mode);
h.assign_str(X_AMZ_REQUEST_PAYER, &mut input.request_payer);
h.assign_str(X_AMZ_TAGGING, &mut input.tagging);
h.assign_str(X_AMZ_OBJECT_LOCK_MODE, &mut input.object_lock_mode);
h.assign_str(
&*X_AMZ_OBJECT_LOCK_RETAIN_UNTIL_DATE,
X_AMZ_OBJECT_LOCK_RETAIN_UNTIL_DATE,
&mut input.object_lock_retain_until_date,
);
h.assign_str(
&*X_AMZ_OBJECT_LOCK_LEGAL_HOLD,
X_AMZ_OBJECT_LOCK_LEGAL_HOLD,
&mut input.object_lock_legal_hold_status,
);

Expand All @@ -153,27 +150,27 @@ impl S3Output for CopyObjectOutput {
#[allow(clippy::shadow_unrelated)]
fn try_into_response(self) -> S3Result<Response> {
wrap_internal_error(|res| {
res.set_optional_header(&*X_AMZ_EXPIRATION, self.expiration)?;
res.set_optional_header(&*X_AMZ_COPY_SOURCE_VERSION_ID, self.copy_source_version_id)?;
res.set_optional_header(&*X_AMZ_VERSION_ID, self.version_id)?;
res.set_optional_header(&*X_AMZ_SERVER_SIDE_ENCRYPTION, self.server_side_encryption)?;
res.set_optional_header(X_AMZ_EXPIRATION, self.expiration)?;
res.set_optional_header(X_AMZ_COPY_SOURCE_VERSION_ID, self.copy_source_version_id)?;
res.set_optional_header(X_AMZ_VERSION_ID, self.version_id)?;
res.set_optional_header(X_AMZ_SERVER_SIDE_ENCRYPTION, self.server_side_encryption)?;
res.set_optional_header(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM,
X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM,
self.sse_customer_algorithm,
)?;
res.set_optional_header(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5,
X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5,
self.sse_customer_key_md5,
)?;
res.set_optional_header(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_AWS_KMS_KEY_ID,
X_AMZ_SERVER_SIDE_ENCRYPTION_AWS_KMS_KEY_ID,
self.ssekms_key_id,
)?;
res.set_optional_header(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_CONTEXT,
X_AMZ_SERVER_SIDE_ENCRYPTION_CONTEXT,
self.ssekms_encryption_context,
)?;
res.set_optional_header(&*X_AMZ_REQUEST_CHARGED, self.request_charged)?;
res.set_optional_header(X_AMZ_REQUEST_CHARGED, self.request_charged)?;

let copy_object_result = self.copy_object_result;

Expand Down
14 changes: 7 additions & 7 deletions src/ops/create_bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ async fn extract(ctx: &mut ReqContext<'_>) -> S3Result<CreateBucketRequest> {
};

let h = &ctx.headers;
h.assign_str(&*X_AMZ_ACL, &mut input.acl);
h.assign_str(&*X_AMZ_GRANT_FULL_CONTROL, &mut input.grant_full_control);
h.assign_str(&*X_AMZ_GRANT_READ, &mut input.grant_read);
h.assign_str(&*X_AMZ_GRANT_READ_ACP, &mut input.grant_read_acp);
h.assign_str(&*X_AMZ_GRANT_WRITE, &mut input.grant_write);
h.assign_str(&*X_AMZ_GRANT_WRITE_ACP, &mut input.grant_write_acp);
h.assign_str(X_AMZ_ACL, &mut input.acl);
h.assign_str(X_AMZ_GRANT_FULL_CONTROL, &mut input.grant_full_control);
h.assign_str(X_AMZ_GRANT_READ, &mut input.grant_read);
h.assign_str(X_AMZ_GRANT_READ_ACP, &mut input.grant_read_acp);
h.assign_str(X_AMZ_GRANT_WRITE, &mut input.grant_write);
h.assign_str(X_AMZ_GRANT_WRITE_ACP, &mut input.grant_write_acp);
h.assign(
&*X_AMZ_BUCKET_OBJECT_LOCK_ENABLED,
X_AMZ_BUCKET_OBJECT_LOCK_ENABLED,
&mut input.object_lock_enabled_for_bucket,
)
.map_err(|err| invalid_request!("Invalid header: x-amz-bucket-object-lock-enabled", err))?;
Expand Down
52 changes: 26 additions & 26 deletions src/ops/create_multipart_upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,27 @@ impl S3Output for CreateMultipartUploadOutput {
#[allow(clippy::shadow_unrelated)]
fn try_into_response(self) -> S3Result<Response> {
wrap_internal_error(|res| {
res.set_optional_header(&*X_AMZ_ABORT_DATE, self.abort_date)?;
res.set_optional_header(&*X_AMZ_ABORT_RULE_ID, self.abort_rule_id)?;
res.set_optional_header(&*X_AMZ_SERVER_SIDE_ENCRYPTION, self.server_side_encryption)?;
res.set_optional_header(X_AMZ_ABORT_DATE, self.abort_date)?;
res.set_optional_header(X_AMZ_ABORT_RULE_ID, self.abort_rule_id)?;
res.set_optional_header(X_AMZ_SERVER_SIDE_ENCRYPTION, self.server_side_encryption)?;
res.set_optional_header(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM,
X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM,
self.sse_customer_algorithm,
)?;
res.set_optional_header(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5,
X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5,
self.sse_customer_key_md5,
)?;
res.set_optional_header(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_AWS_KMS_KEY_ID,
X_AMZ_SERVER_SIDE_ENCRYPTION_AWS_KMS_KEY_ID,
self.ssekms_key_id,
)?;
res.set_optional_header(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_CONTEXT,
X_AMZ_SERVER_SIDE_ENCRYPTION_CONTEXT,
self.ssekms_encryption_context,
)?;

res.set_optional_header(&*X_AMZ_REQUEST_CHARGED, self.request_charged)?;
res.set_optional_header(X_AMZ_REQUEST_CHARGED, self.request_charged)?;

let bucket = self.bucket;
let key = self.key;
Expand Down Expand Up @@ -106,55 +106,55 @@ fn extract(ctx: &mut ReqContext<'_>) -> S3Result<CreateMultipartUploadRequest> {
};

let h = &ctx.headers;
h.assign_str(&*X_AMZ_ACL, &mut input.acl);
h.assign_str(X_AMZ_ACL, &mut input.acl);
h.assign_str(CACHE_CONTROL, &mut input.cache_control);
h.assign_str(CONTENT_DISPOSITION, &mut input.content_disposition);
h.assign_str(CONTENT_ENCODING, &mut input.content_encoding);
h.assign_str(CONTENT_LANGUAGE, &mut input.content_language);
h.assign_str(CONTENT_TYPE, &mut input.content_type);
h.assign_str(EXPIRES, &mut input.expires);
h.assign_str(&*X_AMZ_GRANT_FULL_CONTROL, &mut input.grant_full_control);
h.assign_str(&*X_AMZ_GRANT_READ, &mut input.grant_read);
h.assign_str(&*X_AMZ_GRANT_READ_ACP, &mut input.grant_read_acp);
h.assign_str(&*X_AMZ_GRANT_WRITE_ACP, &mut input.grant_write_acp);
h.assign_str(X_AMZ_GRANT_FULL_CONTROL, &mut input.grant_full_control);
h.assign_str(X_AMZ_GRANT_READ, &mut input.grant_read);
h.assign_str(X_AMZ_GRANT_READ_ACP, &mut input.grant_read_acp);
h.assign_str(X_AMZ_GRANT_WRITE_ACP, &mut input.grant_write_acp);
h.assign_str(
&*X_AMZ_SERVER_SIDE_ENCRYPTION,
X_AMZ_SERVER_SIDE_ENCRYPTION,
&mut input.server_side_encryption,
);
h.assign_str(&*X_AMZ_STORAGE_CLASS, &mut input.storage_class);
h.assign_str(X_AMZ_STORAGE_CLASS, &mut input.storage_class);
h.assign_str(
&*X_AMZ_WEBSITE_REDIRECT_LOCATION,
X_AMZ_WEBSITE_REDIRECT_LOCATION,
&mut input.website_redirect_location,
);
h.assign_str(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM,
X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM,
&mut input.sse_customer_algorithm,
);
h.assign_str(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY,
X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY,
&mut input.sse_customer_key,
);
h.assign_str(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5,
X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5,
&mut input.sse_customer_key_md5,
);
h.assign_str(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_AWS_KMS_KEY_ID,
X_AMZ_SERVER_SIDE_ENCRYPTION_AWS_KMS_KEY_ID,
&mut input.ssekms_key_id,
);
h.assign_str(
&*X_AMZ_SERVER_SIDE_ENCRYPTION_CONTEXT,
X_AMZ_SERVER_SIDE_ENCRYPTION_CONTEXT,
&mut input.ssekms_encryption_context,
);
h.assign_str(&*X_AMZ_REQUEST_PAYER, &mut input.request_payer);
h.assign_str(&*X_AMZ_TAGGING, &mut input.tagging);
h.assign_str(&*X_AMZ_OBJECT_LOCK_MODE, &mut input.object_lock_mode);
h.assign_str(X_AMZ_REQUEST_PAYER, &mut input.request_payer);
h.assign_str(X_AMZ_TAGGING, &mut input.tagging);
h.assign_str(X_AMZ_OBJECT_LOCK_MODE, &mut input.object_lock_mode);
h.assign_str(
&*X_AMZ_OBJECT_LOCK_RETAIN_UNTIL_DATE,
X_AMZ_OBJECT_LOCK_RETAIN_UNTIL_DATE,
&mut input.object_lock_retain_until_date,
);
h.assign_str(
&*X_AMZ_OBJECT_LOCK_LEGAL_HOLD,
X_AMZ_OBJECT_LOCK_LEGAL_HOLD,
&mut input.object_lock_legal_hold_status,
);

Expand Down
2 changes: 1 addition & 1 deletion src/ops/delete_bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn extract(ctx: &mut ReqContext<'_>) -> S3Result<DeleteBucketRequest> {

let h = &ctx.headers;
h.assign_str(
&*X_AMZ_EXPECTED_BUCKET_OWNER,
X_AMZ_EXPECTED_BUCKET_OWNER,
&mut input.expected_bucket_owner,
);

Expand Down
Loading