Support website publishing #7
3 changed files with 38 additions and 39 deletions
|
@ -9,9 +9,9 @@ use garage_util::config::*;
|
|||
use garage_util::error::Error;
|
||||
|
||||
use garage_api::api_server;
|
||||
use garage_web::web_server;
|
||||
use garage_model::garage::Garage;
|
||||
use garage_rpc::rpc_server::RpcServer;
|
||||
use garage_web::web_server;
|
||||
|
||||
use crate::admin_rpc::*;
|
||||
|
||||
|
|
|
@ -2,4 +2,3 @@
|
|||
extern crate log;
|
||||
|
||||
pub mod web_server;
|
||||
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
use std::sync::Arc;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
|
||||
use futures::future::Future;
|
||||
|
||||
use hyper::server::conn::AddrStream;
|
||||
use hyper::{Body,Request,Response,Server,Uri};
|
||||
use hyper::header::HOST;
|
||||
use hyper::server::conn::AddrStream;
|
||||
use hyper::service::{make_service_fn, service_fn};
|
||||
use hyper::{Body, Request, Response, Server, Uri};
|
||||
|
||||
use garage_util::error::Error;
|
||||
use garage_model::garage::Garage;
|
||||
use garage_util::error::Error;
|
||||
|
||||
pub async fn run_web_server(
|
||||
garage: Arc<Garage>,
|
||||
|
@ -42,7 +42,6 @@ async fn handler(
|
|||
req: Request<Body>,
|
||||
addr: SocketAddr,
|
||||
) -> Result<Response<Body>, Error> {
|
||||
|
||||
// Get http authority string (eg. [::1]:3902 or garage.tld:80)
|
||||
let authority = req
|
||||
.headers()
|
||||
|
@ -78,12 +77,14 @@ fn authority_to_host(authority: &str) -> Result<String, Error> {
|
|||
|
||||
match uri_str.parse::<Uri>() {
|
||||
Ok(uri) => {
|
||||
let host = uri
|
||||
.host()
|
||||
.ok_or(Error::BadRequest(format!("Unable to extract host from authority")))?;
|
||||
let host = uri.host().ok_or(Error::BadRequest(format!(
|
||||
"Unable to extract host from authority"
|
||||
)))?;
|
||||
Ok(String::from(host))
|
||||
}
|
||||
_ => Err(Error::BadRequest(format!("Unable to parse authority (host HTTP header)"))),
|
||||
_ => Err(Error::BadRequest(format!(
|
||||
"Unable to parse authority (host HTTP header)"
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -94,12 +95,14 @@ fn host_to_bucket<'a>(host: &'a str, root: &str) -> &'a str {
|
|||
|
||||
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 };
|
||||
let cursor = if missing_starting_dot {
|
||||
len_diff - 1
|
||||
} else {
|
||||
len_diff
|
||||
};
|
||||
&host[..cursor]
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -130,26 +133,23 @@ mod tests {
|
|||
fn host_to_bucket_test() {
|
||||
assert_eq!(
|
||||
host_to_bucket("john.doe.garage.tld", ".garage.tld"),
|
||||
"john.doe");
|
||||
"john.doe"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
host_to_bucket("john.doe.garage.tld", "garage.tld"),
|
||||
"john.doe");
|
||||
"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("john.doe.com", ".garage.tld"),
|
||||
"john.doe.com");
|
||||
"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");
|
||||
|
||||
assert_eq!(
|
||||
host_to_bucket("garage.tld", ".garage.tld"),
|
||||
"garage.tld");
|
||||
assert_eq!(host_to_bucket("garage.tld", ".garage.tld"), "garage.tld");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue