forked from Deuxfleurs/garage
refactor web_server.rs
This commit is contained in:
parent
47467df83e
commit
2aaba39ddc
1 changed files with 38 additions and 65 deletions
|
@ -253,13 +253,9 @@ impl WebServer {
|
||||||
.header("Location", url)
|
.header("Location", url)
|
||||||
.body(empty_body())
|
.body(empty_body())
|
||||||
.unwrap()),
|
.unwrap()),
|
||||||
(&Method::HEAD, Ok((key, code))) => {
|
(_, Ok((key, code))) => {
|
||||||
handle_head(self.garage.clone(), req, bucket_id, key, code).await
|
handle_inner(self.garage.clone(), req, bucket_id, key, code).await
|
||||||
}
|
}
|
||||||
(&Method::GET, Ok((key, code))) => {
|
|
||||||
handle_get(self.garage.clone(), req, bucket_id, key, code).await
|
|
||||||
}
|
|
||||||
_ => Err(ApiError::bad_request("HTTP method not supported")),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Try handling errors if bucket configuration provided fallbacks
|
// Try handling errors if bucket configuration provided fallbacks
|
||||||
|
@ -297,9 +293,7 @@ impl WebServer {
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
) => {
|
) => {
|
||||||
match *req.method() {
|
handle_inner(
|
||||||
Method::HEAD => {
|
|
||||||
handle_head(
|
|
||||||
self.garage.clone(),
|
self.garage.clone(),
|
||||||
req,
|
req,
|
||||||
bucket_id,
|
bucket_id,
|
||||||
|
@ -308,20 +302,6 @@ impl WebServer {
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
Method::GET => {
|
|
||||||
handle_get(
|
|
||||||
self.garage.clone(),
|
|
||||||
req,
|
|
||||||
bucket_id,
|
|
||||||
redirect_key,
|
|
||||||
*redirect_code,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
// we shouldn't ever reach here
|
|
||||||
_ => Err(ApiError::bad_request("HTTP method not supported")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => ret_doc,
|
_ => ret_doc,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -347,11 +327,12 @@ impl WebServer {
|
||||||
// We want to return the error document
|
// We want to return the error document
|
||||||
// Create a fake HTTP request with path = the error document
|
// Create a fake HTTP request with path = the error document
|
||||||
let req2 = Request::builder()
|
let req2 = Request::builder()
|
||||||
|
.method("GET")
|
||||||
.uri(format!("http://{}/{}", host, &error_document))
|
.uri(format!("http://{}/{}", host, &error_document))
|
||||||
.body(empty_body::<Infallible>())
|
.body(empty_body::<Infallible>())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
match handle_get(
|
match handle_inner(
|
||||||
self.garage.clone(),
|
self.garage.clone(),
|
||||||
&req2,
|
&req2,
|
||||||
bucket_id,
|
bucket_id,
|
||||||
|
@ -401,29 +382,7 @@ impl WebServer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_head(
|
async fn handle_inner(
|
||||||
garage: Arc<Garage>,
|
|
||||||
req: &Request<impl Body>,
|
|
||||||
bucket_id: Uuid,
|
|
||||||
key: &str,
|
|
||||||
status_code: StatusCode,
|
|
||||||
) -> Result<Response<ResBody>, ApiError> {
|
|
||||||
if status_code != StatusCode::OK {
|
|
||||||
// See comment in handle_get
|
|
||||||
let cleaned_req = Request::builder()
|
|
||||||
.uri(req.uri())
|
|
||||||
.body(empty_body::<Infallible>())
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let mut ret = handle_head_without_ctx(garage, &cleaned_req, bucket_id, key, None).await?;
|
|
||||||
*ret.status_mut() = status_code;
|
|
||||||
Ok(ret)
|
|
||||||
} else {
|
|
||||||
handle_head_without_ctx(garage, req, bucket_id, key, None).await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn handle_get(
|
|
||||||
garage: Arc<Garage>,
|
garage: Arc<Garage>,
|
||||||
req: &Request<impl Body>,
|
req: &Request<impl Body>,
|
||||||
bucket_id: Uuid,
|
bucket_id: Uuid,
|
||||||
|
@ -432,7 +391,7 @@ pub async fn handle_get(
|
||||||
) -> Result<Response<ResBody>, ApiError> {
|
) -> Result<Response<ResBody>, ApiError> {
|
||||||
if status_code != StatusCode::OK {
|
if status_code != StatusCode::OK {
|
||||||
// If we are returning an error document, discard all headers from
|
// If we are returning an error document, discard all headers from
|
||||||
// the original GET request that would have influenced the result:
|
// the original request that would have influenced the result:
|
||||||
// - Range header, we don't want to return a subrange of the error document
|
// - Range header, we don't want to return a subrange of the error document
|
||||||
// - Caching directives such as If-None-Match, etc, which are not relevant
|
// - Caching directives such as If-None-Match, etc, which are not relevant
|
||||||
let cleaned_req = Request::builder()
|
let cleaned_req = Request::builder()
|
||||||
|
@ -440,7 +399,12 @@ pub async fn handle_get(
|
||||||
.body(empty_body::<Infallible>())
|
.body(empty_body::<Infallible>())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut ret = handle_get_without_ctx(
|
let mut ret = match req.method() {
|
||||||
|
&Method::HEAD => {
|
||||||
|
handle_head_without_ctx(garage, &cleaned_req, bucket_id, key, None).await?
|
||||||
|
}
|
||||||
|
&Method::GET => {
|
||||||
|
handle_get_without_ctx(
|
||||||
garage,
|
garage,
|
||||||
&cleaned_req,
|
&cleaned_req,
|
||||||
bucket_id,
|
bucket_id,
|
||||||
|
@ -448,14 +412,23 @@ pub async fn handle_get(
|
||||||
None,
|
None,
|
||||||
Default::default(),
|
Default::default(),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?
|
||||||
|
}
|
||||||
|
_ => return Err(ApiError::bad_request("HTTP method not supported")),
|
||||||
|
};
|
||||||
|
|
||||||
*ret.status_mut() = status_code;
|
*ret.status_mut() = status_code;
|
||||||
|
|
||||||
Ok(ret)
|
Ok(ret)
|
||||||
} else {
|
} else {
|
||||||
|
match req.method() {
|
||||||
|
&Method::HEAD => handle_head_without_ctx(garage, req, bucket_id, key, None).await,
|
||||||
|
&Method::GET => {
|
||||||
handle_get_without_ctx(garage, req, bucket_id, key, None, Default::default()).await
|
handle_get_without_ctx(garage, req, bucket_id, key, None, Default::default()).await
|
||||||
}
|
}
|
||||||
|
_ => Err(ApiError::bad_request("HTTP method not supported")),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn error_to_res(e: Error) -> Response<BoxBody<Error>> {
|
fn error_to_res(e: Error) -> Response<BoxBody<Error>> {
|
||||||
|
|
Loading…
Reference in a new issue