From 02158ee666390b0da964af09ce2e894e13e29fc5 Mon Sep 17 00:00:00 2001 From: Trinity Pointard Date: Thu, 11 Nov 2021 14:12:22 +0100 Subject: fix issue where list on vhost-bucket would list bucket instead of bucket content --- src/api/api_server.rs | 102 ++++++++++++++------------------------------------ 1 file changed, 29 insertions(+), 73 deletions(-) diff --git a/src/api/api_server.rs b/src/api/api_server.rs index 2217be1a..ab690c30 100644 --- a/src/api/api_server.rs +++ b/src/api/api_server.rs @@ -84,9 +84,6 @@ async fn handler_inner(garage: Arc, req: Request) -> Result, req: Request) -> Result api_key.allow_read(bucket), _ => api_key.allow_write(bucket), @@ -152,7 +155,7 @@ async fn handler_inner(garage: Arc, req: Request) -> Result, req: Request) -> Result( path: &'a str, - host: Option<&'a str>, - root: Option<&str>, + host_bucket: Option<&'a str>, ) -> Result<(&'a str, Option<&'a str>), Error> { let path = path.trim_start_matches('/'); - if host.and(root).is_some() { - if let Some(bucket) = host_to_bucket(host.unwrap(), root.unwrap()) { - if !path.is_empty() { - return Ok((bucket, Some(path))); - } else { - return Ok((bucket, None)); - } + if let Some(bucket) = host_bucket { + if !path.is_empty() { + return Ok((bucket, Some(path))); + } else { + return Ok((bucket, None)); } } @@ -304,7 +304,7 @@ mod tests { #[test] fn parse_bucket_containing_a_key() -> Result<(), Error> { - let (bucket, key) = parse_bucket_key("/my_bucket/a/super/file.jpg", None, None)?; + let (bucket, key) = parse_bucket_key("/my_bucket/a/super/file.jpg", None)?; assert_eq!(bucket, "my_bucket"); assert_eq!(key.expect("key must be set"), "a/super/file.jpg"); Ok(()) @@ -312,10 +312,10 @@ mod tests { #[test] fn parse_bucket_containing_no_key() -> Result<(), Error> { - let (bucket, key) = parse_bucket_key("/my_bucket/", None, None)?; + let (bucket, key) = parse_bucket_key("/my_bucket/", None)?; assert_eq!(bucket, "my_bucket"); assert!(key.is_none()); - let (bucket, key) = parse_bucket_key("/my_bucket", None, None)?; + let (bucket, key) = parse_bucket_key("/my_bucket", None)?; assert_eq!(bucket, "my_bucket"); assert!(key.is_none()); Ok(()) @@ -323,74 +323,30 @@ mod tests { #[test] fn parse_bucket_containing_no_bucket() { - let parsed = parse_bucket_key("", None, None); + let parsed = parse_bucket_key("", None); assert!(parsed.is_err()); - let parsed = parse_bucket_key("/", None, None); + let parsed = parse_bucket_key("/", None); assert!(parsed.is_err()); - let parsed = parse_bucket_key("////", None, None); + let parsed = parse_bucket_key("////", None); assert!(parsed.is_err()); } #[test] fn parse_bucket_with_vhost_and_key() -> Result<(), Error> { - let (bucket, key) = parse_bucket_key( - "/a/super/file.jpg", - Some("my-bucket.garage.tld"), - Some("garage.tld"), - )?; + let (bucket, key) = parse_bucket_key("/a/super/file.jpg", Some("my-bucket"))?; assert_eq!(bucket, "my-bucket"); assert_eq!(key.expect("key must be set"), "a/super/file.jpg"); - - let (bucket, key) = parse_bucket_key( - "/my_bucket/a/super/file.jpg", - Some("not-garage.tld"), - Some("garage.tld"), - )?; - assert_eq!(bucket, "my_bucket"); - assert_eq!(key.expect("key must be set"), "a/super/file.jpg"); Ok(()) } #[test] fn parse_bucket_with_vhost_no_key() -> Result<(), Error> { - let (bucket, key) = parse_bucket_key("", Some("my-bucket.garage.tld"), Some("garage.tld"))?; + let (bucket, key) = parse_bucket_key("", Some("my-bucket"))?; assert_eq!(bucket, "my-bucket"); assert!(key.is_none()); - let (bucket, key) = - parse_bucket_key("/", Some("my-bucket.garage.tld"), Some("garage.tld"))?; + let (bucket, key) = parse_bucket_key("/", Some("my-bucket"))?; assert_eq!(bucket, "my-bucket"); assert!(key.is_none()); Ok(()) } - - #[test] - fn parse_bucket_missmatch_vhost() { - let test_vec = [ - "/my_bucket/a/super/file.jpg", - "/my_bucket/", - "/my_bucket", - "", - "/", - "////", - ]; - let eq = |l, r| match (l, r) { - (Ok(l), Ok(r)) => l == r, - (Err(_), Err(_)) => true, - _ => false, - }; - for test in test_vec { - assert!(eq( - parse_bucket_key(test, None, None), - parse_bucket_key(test, Some("bucket.garage.tld"), None) - )); - assert!(eq( - parse_bucket_key(test, None, None), - parse_bucket_key(test, None, Some("garage.tld")) - )); - assert!(eq( - parse_bucket_key(test, None, None), - parse_bucket_key(test, Some("not-garage.tld"), Some("garage.tld")) - )); - } - } } -- cgit v1.2.3