Handle 4 more unwraps

This commit is contained in:
spikecodes 2021-02-20 12:14:32 -08:00
parent 2091f26bda
commit a606e48435
No known key found for this signature in database
GPG key ID: 004CECFF9B463BCB
4 changed files with 58 additions and 22 deletions

View file

@ -13,17 +13,20 @@ pub async fn handler(req: Request<()>, format: &str, params: Vec<&str>) -> tide:
}
async fn request(url: String) -> tide::Result {
let http = surf::get(url).await.unwrap();
match surf::get(url).await {
Ok(res) => {
let content_length = res.header("Content-Length").map(|v| v.to_string()).unwrap_or_default();
let content_type = res.content_type().map(|m| m.to_string()).unwrap_or_default();
let content_length = http.header("Content-Length").map(|v| v.to_string()).unwrap_or_default();
let content_type = http.content_type().map(|m| m.to_string()).unwrap_or_default();
Ok(
Response::builder(http.status())
.body(Body::from_reader(http, None))
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
.header("Content-Length", content_length)
.header("Content-Type", content_type)
.build(),
)
Ok(
Response::builder(res.status())
.body(Body::from_reader(res, None))
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
.header("Content-Length", content_length)
.header("Content-Type", content_type)
.build(),
)
}
Err(e) => Ok(Response::builder(503).body(e.to_string()).build()),
}
}

View file

@ -222,8 +222,10 @@ pub fn format_url(url: &str) -> String {
// Rewrite Reddit links to Libreddit in body of text
pub fn rewrite_urls(text: &str) -> String {
let re = Regex::new(r#"href="(https|http|)://(www.|old.|np.|)(reddit).(com)/"#).unwrap();
re.replace_all(text, r#"href="/"#).to_string()
match Regex::new(r#"href="(https|http|)://(www.|old.|np.|)(reddit).(com)/"#) {
Ok(re) => re.replace_all(text, r#"href="/"#).to_string(),
Err(_) => String::new(),
}
}
// Append `m` and `k` for millions and thousands respectively