Skip to content
This repository was archived by the owner on Nov 23, 2022. It is now read-only.

Commit 31f32bf

Browse files
Boilerplate until the let_chains feature is fixed
rust-lang/rust#92145
1 parent 7a82f6e commit 31f32bf

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

cdn_server/src/download.rs

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,54 @@ use crate::node::get_node_ip;
2121
pub async fn download(
2222
Path((node, filename)): Path<(String, String)>,
2323
) -> Result<Response<BoxBody>, CdnError> {
24-
let mut content_type: String;
25-
26-
let mut decoded: Vec<u8>;
27-
28-
let mut status: StatusCode;
24+
if *CACHE {
25+
if let Some(file_) = get_from_cache(&filename) {
26+
let content_type = tree_magic::from_u8(&file_);
27+
let decoded = file_;
28+
29+
let resp = Response::builder()
30+
.status(StatusCode::FOUND)
31+
.header(
32+
CONTENT_TYPE,
33+
HeaderValue::from_str(content_type.as_str())
34+
.unwrap_or(HeaderValue::from_static("application/octet-stream")),
35+
)
36+
.body(body::boxed(body::Full::from(decoded)))
37+
.unwrap_or_else(|e| {
38+
// this should only be reachable if a invalid HTTP code is passed in
39+
unreachable!(
40+
"got an error while attempting to construct HTTP response for ServerError: {}",
41+
e
42+
)
43+
});
44+
45+
return Ok(resp);
46+
}
47+
}
2948

30-
if *CACHE && let Some(file_) = get_from_cache(&filename) {
31-
content_type = tree_magic::from_u8(&file_);
32-
decoded = file_;
33-
status = StatusCode::FOUND;
34-
} else {
35-
let node_ip = get_node_ip(node).await?;
49+
let node_ip = get_node_ip(node).await?;
3650

37-
let file = get_file(node_ip, filename).await?;
51+
let file = get_file(node_ip, filename.clone()).await?;
3852

39-
let mut decoder = ZstdDecoder::new(Vec::new());
40-
decoder
41-
.write_all(&file)
42-
.await
43-
.map_err(|e| CdnError::FailedToDeCompress(e))?;
44-
decoder
45-
.shutdown()
46-
.await
47-
.map_err(|e| CdnError::FailedToDeCompress(e))?;
53+
let mut decoder = ZstdDecoder::new(Vec::new());
54+
decoder
55+
.write_all(&file)
56+
.await
57+
.map_err(|e| CdnError::FailedToDeCompress(e))?;
58+
decoder
59+
.shutdown()
60+
.await
61+
.map_err(|e| CdnError::FailedToDeCompress(e))?;
4862

49-
decoded = decoder.into_inner();
50-
content_type = tree_magic::from_u8(&decoded);
51-
status = StatusCode::OK;
63+
let decoded = decoder.into_inner();
64+
let content_type = tree_magic::from_u8(&decoded);
5265

53-
if *CACHE {
54-
insert_into_cache(filename, decoded.clone(), decoded.len() as i64).await;
55-
}
66+
if *CACHE {
67+
insert_into_cache(filename, decoded.clone(), decoded.len() as i64).await;
5668
}
5769

5870
let resp = Response::builder()
59-
.status(status)
71+
.status(StatusCode::OK)
6072
.header(
6173
CONTENT_TYPE,
6274
HeaderValue::from_str(content_type.as_str())

cdn_server/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![feature(async_closure)]
22
#![feature(once_cell)]
3-
#![feature(let_chains)]
43

54
#[macro_use]
65
extern crate lazy_static;

0 commit comments

Comments
 (0)