From a606e48435963cc08aa79c17a4ff9d49f51b847e Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Sat, 20 Feb 2021 12:14:32 -0800 Subject: [PATCH] Handle 4 more unwraps --- Cargo.lock | 43 +++++++++++++++++++++++++++++++++++++------ src/proxy.rs | 27 +++++++++++++++------------ src/utils.rs | 6 ++++-- templates/utils.html | 4 ++-- 4 files changed, 58 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9105b29..1b45d2c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -297,7 +297,7 @@ dependencies = [ "async-io", "async-lock", "async-process", - "crossbeam-utils 0.8.1", + "crossbeam-utils 0.8.2", "futures-channel", "futures-core", "futures-io", @@ -591,13 +591,14 @@ dependencies = [ [[package]] name = "crossbeam-utils" -version = "0.8.1" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02d96d1e189ef58269ebe5b97953da3274d83a93af647c2ddd6f9dab28cedb8d" +checksum = "bae8f328835f8f5a6ceb6a7842a7f2d0c03692adb5c889347235d59194731fe3" dependencies = [ "autocfg", "cfg-if 1.0.0", "lazy_static", + "loom", ] [[package]] @@ -893,6 +894,19 @@ dependencies = [ "slab", ] +[[package]] +name = "generator" +version = "0.6.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cdc09201b2e8ca1b19290cf7e65de2246b8e91fb6874279722189c4de7b94dc" +dependencies = [ + "cc", + "libc", + "log", + "rustc_version", + "winapi", +] + [[package]] name = "generic-array" version = "0.14.4" @@ -1065,7 +1079,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2948a0ce43e2c2ef11d7edf6816508998d99e13badd1150be0914205df9388a" dependencies = [ "bytes 0.5.6", - "crossbeam-utils 0.8.1", + "crossbeam-utils 0.8.2", "curl", "curl-sys", "flume", @@ -1188,6 +1202,17 @@ dependencies = [ "value-bag", ] +[[package]] +name = "loom" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d44c73b4636e497b4917eb21c33539efa3816741a2d3ff26c6316f1b529481a4" +dependencies = [ + "cfg-if 1.0.0", + "generator", + "scoped-tls", +] + [[package]] name = "matches" version = "0.1.8" @@ -1498,6 +1523,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "scoped-tls" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" + [[package]] name = "scopeguard" version = "1.1.0" @@ -1595,9 +1626,9 @@ dependencies = [ [[package]] name = "signal-hook" -version = "0.3.4" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "780f5e3fe0c66f67197236097d89de1e86216f1f6fdeaf47c442f854ab46c240" +checksum = "8a7f3f92a1da3d6b1d32245d0cbcbbab0cfc45996d8df619c42bccfa6d2bbb5f" dependencies = [ "libc", "signal-hook-registry", diff --git a/src/proxy.rs b/src/proxy.rs index 5da6ddf..5cff7cf 100644 --- a/src/proxy.rs +++ b/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()), + } } diff --git a/src/utils.rs b/src/utils.rs index 12f0c1a..a052420 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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 diff --git a/templates/utils.html b/templates/utils.html index 72634b2..afbf388 100644 --- a/templates/utils.html +++ b/templates/utils.html @@ -1,7 +1,7 @@ {% macro options(current, values, default) -%} {% for value in values %} {% endfor %} {%- endmacro %} @@ -9,7 +9,7 @@ {% macro sort(root, methods, selected) -%} {% for method in methods %} - {{ format!("{}{}", method.get(0..1).unwrap().to_uppercase(), method.get(1..).unwrap()) }} + {{ format!("{}{}", method.get(0..1).unwrap_or_default().to_uppercase(), method.get(1..).unwrap_or_default()) }} {% endfor %} {%- endmacro %}