aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQuentin <quentin@deuxfleurs.fr>2020-11-10 09:57:07 +0100
committerQuentin <quentin@deuxfleurs.fr>2020-11-10 09:57:07 +0100
commit4093833ae854df16bc893a21617b0902a5beae47 (patch)
treee606270e791e2a5f68530839c58f7dedcb38b939
parent09137fd6b5ff30639addcac837bc1c6e6ff78fcf (diff)
downloadgarage-4093833ae854df16bc893a21617b0902a5beae47.tar.gz
garage-4093833ae854df16bc893a21617b0902a5beae47.zip
Extract bucket
-rw-r--r--Cargo.lock1
-rw-r--r--src/util/config.rs3
-rw-r--r--src/web/Cargo.toml1
-rw-r--r--src/web/web_server.rs55
4 files changed, 54 insertions, 6 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 6e1c97ae..9a06d31d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -661,6 +661,7 @@ dependencies = [
"http",
"hyper",
"log",
+ "percent-encoding",
"rand",
"rmp-serde",
"roxmltree",
diff --git a/src/util/config.rs b/src/util/config.rs
index a5fbe4b4..72f7c319 100644
--- a/src/util/config.rs
+++ b/src/util/config.rs
@@ -54,7 +54,8 @@ pub struct ApiConfig {
#[derive(Deserialize, Debug, Clone)]
pub struct WebConfig {
- pub web_bind_addr: SocketAddr,
+ pub bind_addr: SocketAddr,
+ pub root_domain: String,
}
fn default_max_concurrent_rpc_requests() -> usize {
diff --git a/src/web/Cargo.toml b/src/web/Cargo.toml
index 796478ae..8eddf047 100644
--- a/src/web/Cargo.toml
+++ b/src/web/Cargo.toml
@@ -36,6 +36,7 @@ tokio = { version = "0.2", default-features = false, features = ["rt-core", "rt-
http = "0.2"
hyper = "0.13"
+percent-encoding = "2.1.0"
rustls = "0.17"
webpki = "0.21"
diff --git a/src/web/web_server.rs b/src/web/web_server.rs
index a712a5bd..432d9752 100644
--- a/src/web/web_server.rs
+++ b/src/web/web_server.rs
@@ -15,7 +15,7 @@ pub async fn run_web_server(
garage: Arc<Garage>,
shutdown_signal: impl Future<Output = ()>,
) -> Result<(), Error> {
- let addr = &garage.config.s3_web.web_bind_addr;
+ let addr = &garage.config.s3_web.bind_addr;
let service = make_service_fn(|conn: &AddrStream| {
let garage = garage.clone();
@@ -43,17 +43,23 @@ async fn handler(
addr: SocketAddr,
) -> Result<Response<Body>, Error> {
- // Get http authority string (eg. [::1]:3902)
+ // Get http authority string (eg. [::1]:3902 or garage.tld:80)
let authority = req
.headers()
.get(HOST)
.ok_or(Error::BadRequest(format!("HOST header required")))?
.to_str()?;
- info!("authority is {}", authority);
- // Get HTTP domain/ip from host
+ // Get bucket
let host = authority_to_host(authority)?;
- info!("host is {}", host);
+ let root = &garage.config.s3_web.root_domain;
+ let bucket = host_to_bucket(&host, root);
+
+ // Get path
+ let path = req.uri().path().to_string();
+ let key = percent_encoding::percent_decode_str(&path).decode_utf8()?;
+
+ info!("host: {}, bucket: {}, key: {}", host, bucket, key);
Ok(Response::new(Body::from("hello world\n")))
}
@@ -81,6 +87,18 @@ fn authority_to_host(authority: &str) -> Result<String, Error> {
}
}
+fn host_to_bucket<'a>(host: &'a str, root: &str) -> &'a str {
+ if root.len() >= host.len() || !host.ends_with(root) {
+ return host;
+ }
+
+ let len_diff = host.len() - root.len();
+ let missing_starting_dot = root.chars().next() != Some('.');
+ let cursor = if missing_starting_dot { len_diff - 1 } else { len_diff };
+ &host[..cursor]
+}
+
+
#[cfg(test)]
mod tests {
@@ -107,4 +125,31 @@ mod tests {
assert_eq!(domain3, "127.0.0.1");
Ok(())
}
+
+ #[test]
+ fn host_to_bucket_test() {
+ assert_eq!(
+ host_to_bucket("john.doe.garage.tld", ".garage.tld"),
+ "john.doe");
+
+ assert_eq!(
+ host_to_bucket("john.doe.garage.tld", "garage.tld"),
+ "john.doe");
+
+ assert_eq!(
+ host_to_bucket("john.doe.com", "garage.tld"),
+ "john.doe.com");
+
+ assert_eq!(
+ host_to_bucket("john.doe.com", ".garage.tld"),
+ "john.doe.com");
+
+ assert_eq!(
+ host_to_bucket("garage.tld", "garage.tld"),
+ "garage.tld");
+
+ assert_eq!(
+ host_to_bucket("garage.tld", ".garage.tld"),
+ "garage.tld");
+ }
}