aboutsummaryrefslogtreecommitdiff
path: root/src/api/s3_cors.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2022-02-28 11:15:53 +0100
committerAlex Auvolat <alex@adnab.me>2022-02-28 12:22:39 +0100
commit8129a9829165c8543b2ab2d32e8f20ed54ced9e5 (patch)
tree5a1eae4279761a82722dc1224b1261a72aba96da /src/api/s3_cors.rs
parent54e02b4c3ba466988c50984aba9b60dc51b6cdce (diff)
downloadgarage-8129a9829165c8543b2ab2d32e8f20ed54ced9e5.tar.gz
garage-8129a9829165c8543b2ab2d32e8f20ed54ced9e5.zip
Process CORS earlier in pipeline
Diffstat (limited to 'src/api/s3_cors.rs')
-rw-r--r--src/api/s3_cors.rs28
1 files changed, 27 insertions, 1 deletions
diff --git a/src/api/s3_cors.rs b/src/api/s3_cors.rs
index cde66079..7dc48d8e 100644
--- a/src/api/s3_cors.rs
+++ b/src/api/s3_cors.rs
@@ -100,7 +100,33 @@ pub async fn handle_put_cors(
.body(Body::empty())?)
}
-pub async fn handle_options(req: &Request<Body>, bucket: &Bucket) -> Result<Response<Body>, Error> {
+pub async fn handle_options(
+ garage: Arc<Garage>,
+ req: &Request<Body>,
+ bucket_name: Option<String>,
+) -> Result<Response<Body>, Error> {
+ let bucket = if let Some(bn) = bucket_name {
+ let helper = garage.bucket_helper();
+ let bucket_id = helper
+ .resolve_global_bucket_name(&bn)
+ .await?
+ .ok_or(Error::NoSuchBucket)?;
+ garage
+ .bucket_table
+ .get(&EmptyKey, &bucket_id)
+ .await?
+ .filter(|b| !b.state.is_deleted())
+ .ok_or(Error::NoSuchBucket)?
+ } else {
+ // The only supported API call that doesn't use a bucket name is ListBuckets,
+ // which we want to allow in all cases
+ return Ok(Response::builder()
+ .header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
+ .header(ACCESS_CONTROL_ALLOW_METHODS, "GET")
+ .status(StatusCode::OK)
+ .body(Body::empty())?);
+ };
+
let origin = req
.headers()
.get("Origin")