Skip to content

Commit 6850780

Browse files
committed
chore: Use http::Headers instead of custom consts
1 parent 51776b8 commit 6850780

3 files changed

Lines changed: 26 additions & 33 deletions

File tree

api/src/cors.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use axum::http::Method;
22
use http::HeaderMap;
3+
use http::HeaderName;
34
use http::HeaderValue;
45
use http::header;
56
use std::time::Duration;
@@ -12,10 +13,10 @@ const S3_DEFAULT_ALLOWED_HEADERS: &str = "authorization,content-type,content-md5
1213
const S3_EXPOSED_HEADERS: &str = "etag,content-range,accept-ranges,content-length,last-modified,\
1314
x-amz-request-id,x-amz-version-id,x-amz-delete-marker,aruna-source-content-type,\
1415
aruna-source-etag,aruna-source-last-modified,aruna-last-refresh";
15-
pub(crate) const S3_PREFLIGHT_VARY: &[&str] = &[
16-
"Origin",
17-
"Access-Control-Request-Method",
18-
"Access-Control-Request-Headers",
16+
pub(crate) const S3_PREFLIGHT_VARY: &[HeaderName] = &[
17+
header::ORIGIN,
18+
header::ACCESS_CONTROL_REQUEST_METHOD,
19+
header::ACCESS_CONTROL_REQUEST_HEADERS,
1920
];
2021

2122
/// Allowed cross-origin request origins, shared by the REST and S3 interfaces.
@@ -154,11 +155,11 @@ impl CorsConfig {
154155
header::ACCESS_CONTROL_EXPOSE_HEADERS,
155156
HeaderValue::from_static(S3_EXPOSED_HEADERS),
156157
);
157-
headers.append(header::VARY, HeaderValue::from_static("origin"));
158+
append_vary_headers(headers, &[header::ORIGIN]);
158159
}
159160
}
160161

161-
pub(crate) fn append_vary_headers(headers: &mut HeaderMap, values: &[&str]) {
162+
pub(crate) fn append_vary_headers(headers: &mut HeaderMap, values: &[HeaderName]) {
162163
let mut vary_values = headers
163164
.get(header::VARY)
164165
.and_then(|value| value.to_str().ok())
@@ -175,9 +176,9 @@ pub(crate) fn append_vary_headers(headers: &mut HeaderMap, values: &[&str]) {
175176
for value in values {
176177
if !vary_values
177178
.iter()
178-
.any(|existing| existing.eq_ignore_ascii_case(value))
179+
.any(|existing| existing.eq_ignore_ascii_case(value.as_str()))
179180
{
180-
vary_values.push((*value).to_string());
181+
vary_values.push(value.as_str().to_string());
181182
}
182183
}
183184

@@ -270,7 +271,7 @@ mod tests {
270271
);
271272
assert_eq!(
272273
headers.get(header::VARY).unwrap(),
273-
"Origin, Access-Control-Request-Method, Access-Control-Request-Headers"
274+
"origin, access-control-request-method, access-control-request-headers"
274275
);
275276
}
276277
}

api/src/s3/cors.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
use crate::cors::{S3_PREFLIGHT_VARY, append_vary_headers};
22
use aruna_core::structs::{BucketCorsConfiguration, BucketCorsRule};
3-
use http::header::{HeaderName, HeaderValue};
3+
use http::header::{self, HeaderName, HeaderValue};
44
use http::{Method, StatusCode};
55
use s3s::HttpResponse;
66
use s3s::dto::{CORSConfiguration, CORSRule, GetBucketCorsOutput};
77
use s3s::{S3Error, s3_error};
88

9-
pub(crate) const ORIGIN_HEADER: &str = "Origin";
10-
pub(crate) const REQUEST_METHOD_HEADER: &str = "Access-Control-Request-Method";
11-
pub(crate) const REQUEST_HEADERS_HEADER: &str = "Access-Control-Request-Headers";
12-
13-
pub(crate) const ALLOW_ORIGIN_HEADER: &str = "access-control-allow-origin";
14-
pub(crate) const ALLOW_METHODS_HEADER: &str = "access-control-allow-methods";
15-
pub(crate) const ALLOW_HEADERS_HEADER: &str = "access-control-allow-headers";
16-
pub(crate) const EXPOSE_HEADERS_HEADER: &str = "access-control-expose-headers";
17-
pub(crate) const MAX_AGE_HEADER: &str = "access-control-max-age";
18-
199
const WILDCARD: &str = "*";
2010
const VALID_CORS_METHODS: &[&str] = &["GET", "PUT", "HEAD", "POST", "DELETE"];
2111

@@ -271,14 +261,14 @@ pub(crate) fn build_preflight_response(matched_rule: MatchedCorsRule) -> HttpRes
271261
if !matched_rule.allow_headers.is_empty() {
272262
append_header(
273263
&mut response,
274-
HeaderName::from_static(ALLOW_HEADERS_HEADER),
264+
header::ACCESS_CONTROL_ALLOW_HEADERS,
275265
&matched_rule.allow_headers.join(", "),
276266
);
277267
}
278268
if let Some(max_age_seconds) = matched_rule.max_age_seconds {
279269
append_header(
280270
&mut response,
281-
HeaderName::from_static(MAX_AGE_HEADER),
271+
header::ACCESS_CONTROL_MAX_AGE,
282272
&max_age_seconds.to_string(),
283273
);
284274
}
@@ -301,22 +291,22 @@ pub(crate) fn inject_actual_cors_headers(
301291
if !matched_rule.expose_headers.is_empty() {
302292
append_header(
303293
response,
304-
HeaderName::from_static(EXPOSE_HEADERS_HEADER),
294+
header::ACCESS_CONTROL_EXPOSE_HEADERS,
305295
&matched_rule.expose_headers.join(", "),
306296
);
307297
}
308-
append_vary_headers(response.headers_mut(), &[ORIGIN_HEADER]);
298+
append_vary_headers(response.headers_mut(), &[header::ORIGIN]);
309299
}
310300

311301
fn append_allow_origin_and_methods(response: &mut HttpResponse, matched_rule: &MatchedCorsRule) {
312302
append_header(
313303
response,
314-
HeaderName::from_static(ALLOW_ORIGIN_HEADER),
304+
header::ACCESS_CONTROL_ALLOW_ORIGIN,
315305
&matched_rule.allow_origin,
316306
);
317307
append_header(
318308
response,
319-
HeaderName::from_static(ALLOW_METHODS_HEADER),
309+
header::ACCESS_CONTROL_ALLOW_METHODS,
320310
&matched_rule.allow_methods.join(", "),
321311
);
322312
}
@@ -477,7 +467,7 @@ mod tests {
477467
assert_eq!(response.headers()["access-control-max-age"], "60");
478468
assert_eq!(
479469
response.headers()[VARY],
480-
"Origin, Access-Control-Request-Method, Access-Control-Request-Headers"
470+
"origin, access-control-request-method, access-control-request-headers"
481471
);
482472
}
483473

@@ -494,7 +484,7 @@ mod tests {
494484
);
495485
assert_eq!(
496486
response.headers()[VARY],
497-
"Origin, Access-Control-Request-Method, Access-Control-Request-Headers"
487+
"origin, access-control-request-method, access-control-request-headers"
498488
);
499489
}
500490

@@ -513,6 +503,6 @@ mod tests {
513503
"https://example.org"
514504
);
515505
assert_eq!(response.headers()["access-control-expose-headers"], "etag");
516-
assert_eq!(response.headers()[VARY], "Accept-Encoding, Origin");
506+
assert_eq!(response.headers()[VARY], "Accept-Encoding, origin");
517507
}
518508
}

api/src/s3/s3_server.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::auth::AuthProvider;
22
use super::cors::{
3-
ORIGIN_HEADER, REQUEST_HEADERS_HEADER, REQUEST_METHOD_HEADER,
43
build_preflight_forbidden_response, build_preflight_response, inject_actual_cors_headers,
54
match_actual_rule, match_preflight_rule, parse_requested_headers,
65
};
@@ -157,17 +156,20 @@ impl Service<Request<Incoming>> for WrappingService {
157156
.get(header::HOST)
158157
.and_then(|value| value.to_str().ok());
159158
let bucket = extract_bucket_name(host, &path, &self.domain);
160-
let origin_header = parts.headers.get(ORIGIN_HEADER).cloned();
159+
let origin_header = parts.headers.get(header::ORIGIN).cloned();
161160
let origin = origin_header
162161
.as_ref()
163162
.and_then(|value| value.to_str().ok())
164163
.map(str::to_owned);
165164
let requested_method = parts
166165
.headers
167-
.get(REQUEST_METHOD_HEADER)
166+
.get(header::ACCESS_CONTROL_REQUEST_METHOD)
168167
.and_then(|value| value.to_str().ok())
169168
.map(str::to_owned);
170-
let requested_headers_value = parts.headers.get(REQUEST_HEADERS_HEADER).cloned();
169+
let requested_headers_value = parts
170+
.headers
171+
.get(header::ACCESS_CONTROL_REQUEST_HEADERS)
172+
.cloned();
171173
let requested_headers = requested_headers_value
172174
.as_ref()
173175
.and_then(|value| value.to_str().ok())

0 commit comments

Comments
 (0)