mirror of
https://github.com/redlib-org/redlib.git
synced 2025-05-15 06:22:50 +00:00
Handle 4 more unwraps
This commit is contained in:
parent
2091f26bda
commit
a606e48435
4 changed files with 58 additions and 22 deletions
27
src/proxy.rs
27
src/proxy.rs
|
@ -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()),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue