Rename fields and fix clippy

This commit is contained in:
Alex 2022-01-07 17:05:31 +01:00
parent f270df21c0
commit 1ca3691637
No known key found for this signature in database
GPG Key ID: EDABF9711E244EB1
3 changed files with 21 additions and 22 deletions

View File

@ -193,9 +193,9 @@ impl CorsRule {
GarageCorsRule {
id: self.id.as_ref().map(|x| x.0.to_owned()),
max_age_seconds: self.max_age_seconds.as_ref().map(|x| x.0 as u64),
allowed_origins: convert_vec(&self.allowed_origins),
allowed_methods: convert_vec(&self.allowed_methods),
allowed_headers: convert_vec(&self.allowed_headers),
allow_origins: convert_vec(&self.allowed_origins),
allow_methods: convert_vec(&self.allowed_methods),
allow_headers: convert_vec(&self.allowed_headers),
expose_headers: convert_vec(&self.expose_headers),
}
}
@ -209,9 +209,9 @@ impl CorsRule {
Self {
id: rule.id.as_ref().map(|x| Value(x.clone())),
max_age_seconds: rule.max_age_seconds.map(|x| IntValue(x as i64)),
allowed_origins: convert_vec(&rule.allowed_origins),
allowed_methods: convert_vec(&rule.allowed_methods),
allowed_headers: convert_vec(&rule.allowed_headers),
allowed_origins: convert_vec(&rule.allow_origins),
allowed_methods: convert_vec(&rule.allow_methods),
allowed_headers: convert_vec(&rule.allow_headers),
expose_headers: convert_vec(&rule.expose_headers),
}
}
@ -227,10 +227,10 @@ where
HI: Iterator<Item = S>,
S: AsRef<str>,
{
rule.allowed_origins.iter().any(|x| x == "*" || x == origin)
&& rule.allowed_methods.iter().any(|x| x == "*" || x == method)
rule.allow_origins.iter().any(|x| x == "*" || x == origin)
&& rule.allow_methods.iter().any(|x| x == "*" || x == method)
&& request_headers.all(|h| {
rule.allowed_headers
rule.allow_headers
.iter()
.any(|x| x == "*" || x == h.as_ref())
})
@ -243,15 +243,15 @@ pub fn add_cors_headers(
let h = resp.headers_mut();
h.insert(
ACCESS_CONTROL_ALLOW_ORIGIN,
rule.allowed_origins.join(", ").parse()?,
rule.allow_origins.join(", ").parse()?,
);
h.insert(
ACCESS_CONTROL_ALLOW_METHODS,
rule.allowed_methods.join(", ").parse()?,
rule.allow_methods.join(", ").parse()?,
);
h.insert(
ACCESS_CONTROL_ALLOW_HEADERS,
rule.allowed_headers.join(", ").parse()?,
rule.allow_headers.join(", ").parse()?,
);
h.insert(
ACCESS_CONTROL_EXPOSE_HEADERS,

View File

@ -57,9 +57,9 @@ pub struct WebsiteConfig {
pub struct CorsRule {
pub id: Option<String>,
pub max_age_seconds: Option<u64>,
pub allowed_origins: Vec<String>,
pub allowed_methods: Vec<String>,
pub allowed_headers: Vec<String>,
pub allow_origins: Vec<String>,
pub allow_methods: Vec<String>,
pub allow_headers: Vec<String>,
pub expose_headers: Vec<String>,
}

View File

@ -210,19 +210,19 @@ async fn serve_file(garage: Arc<Garage>, req: &Request<Body>) -> Result<Response
if let Some(origin) = req.headers().get("Origin") {
let origin = origin.to_str()?;
let request_headers = match req.headers().get(ACCESS_CONTROL_REQUEST_HEADERS) {
Some(h) => h.to_str()?.split(",").map(|h| h.trim()).collect::<Vec<_>>(),
Some(h) => h.to_str()?.split(',').map(|h| h.trim()).collect::<Vec<_>>(),
None => vec![],
};
let matching_rule = cors_config.iter().find(|rule| {
cors_rule_matches(
rule,
&origin,
origin,
&req.method().to_string(),
request_headers.iter(),
)
});
if let Some(rule) = matching_rule {
add_cors_headers(&mut resp, &rule)
add_cors_headers(&mut resp, rule)
.ok_or_internal_error("Invalid CORS configuration")?;
}
}
@ -244,21 +244,20 @@ fn handle_options(bucket: &Bucket, req: &Request<Body>) -> Result<Response<Body>
.ok_or_bad_request("Missing Access-Control-Request-Method header")?
.to_str()?;
let request_headers = match req.headers().get(ACCESS_CONTROL_REQUEST_HEADERS) {
Some(h) => h.to_str()?.split(",").map(|h| h.trim()).collect::<Vec<_>>(),
Some(h) => h.to_str()?.split(',').map(|h| h.trim()).collect::<Vec<_>>(),
None => vec![],
};
if let Some(cors_config) = bucket.params().unwrap().cors_config.get() {
let matching_rule = cors_config
.iter()
.find(|rule| cors_rule_matches(rule, &origin, &request_method, request_headers.iter()));
.find(|rule| cors_rule_matches(rule, origin, request_method, request_headers.iter()));
if let Some(rule) = matching_rule {
let mut resp = Response::builder()
.status(StatusCode::OK)
.body(Body::empty())
.map_err(ApiError::from)?;
add_cors_headers(&mut resp, &rule)
.ok_or_internal_error("Invalid CORS configuration")?;
add_cors_headers(&mut resp, rule).ok_or_internal_error("Invalid CORS configuration")?;
return Ok(resp);
}
}