web_server.rs: handle error properly and refactor

This commit is contained in:
Alex 2023-08-28 12:05:14 +02:00
parent 63da1d2443
commit c5cafa0000

View file

@ -169,21 +169,15 @@ impl WebServer {
} }
} }
async fn check_key_exists(self: &Arc<Self>, bucket_id: Uuid, key: &str) -> Option<()> { async fn check_key_exists(self: &Arc<Self>, bucket_id: Uuid, key: &str) -> Result<bool, Error> {
self.garage let exists = self
.garage
.object_table .object_table
.get(&bucket_id, &key.to_string()) .get(&bucket_id, &key.to_string())
.await .await?
.ok() .map(|object| object.versions().iter().any(|v| v.is_data()))
.flatten() .unwrap_or(false);
.and_then(|object| { Ok(exists)
object
.versions()
.iter()
.rev()
.find(|v| v.is_data())
.and(Some(()))
})
} }
async fn serve_file(self: &Arc<Self>, req: &Request<Body>) -> Result<Response<Body>, Error> { async fn serve_file(self: &Arc<Self>, req: &Request<Body>) -> Result<Response<Body>, Error> {
@ -242,10 +236,7 @@ impl WebServer {
// Try implicit redirect on error // Try implicit redirect on error
let ret_doc_with_redir = match (&ret_doc, may_redirect) { let ret_doc_with_redir = match (&ret_doc, may_redirect) {
(Err(ApiError::NoSuchKey), ImplicitRedirect::To { key, url }) (Err(ApiError::NoSuchKey), ImplicitRedirect::To { key, url })
if self if self.check_key_exists(bucket_id, key.as_str()).await? =>
.check_key_exists(bucket_id, key.as_str())
.await
.is_some() =>
{ {
Ok(Response::builder() Ok(Response::builder()
.status(StatusCode::FOUND) .status(StatusCode::FOUND)
@ -358,13 +349,12 @@ enum ImplicitRedirect {
fn path_to_keys<'a>(path: &'a str, index: &str) -> Result<(String, ImplicitRedirect), Error> { fn path_to_keys<'a>(path: &'a str, index: &str) -> Result<(String, ImplicitRedirect), Error> {
let path_utf8 = percent_encoding::percent_decode_str(path).decode_utf8()?; let path_utf8 = percent_encoding::percent_decode_str(path).decode_utf8()?;
if !path_utf8.starts_with('/') { let base_key = match path_utf8.strip_prefix("/") {
return Err(Error::BadRequest("Path must start with a / (slash)".into())); Some(bk) => bk,
} None => return Err(Error::BadRequest("Path must start with a / (slash)".into())),
};
let base_key = &path_utf8[1..];
let is_bucket_root = base_key.len() == 0; let is_bucket_root = base_key.len() == 0;
let is_trailing_slash = path_utf8.chars().last().map(|v| v == '/').unwrap_or(false); let is_trailing_slash = path_utf8.ends_with("/");
match (is_bucket_root, is_trailing_slash) { match (is_bucket_root, is_trailing_slash) {
// It is not possible to store something at the root of the bucket (ie. empty key), // It is not possible to store something at the root of the bucket (ie. empty key),