From 32b8637c7e96745147c1b504c55996cd5fb4ea42 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Thu, 14 Jan 2021 14:56:28 -0800 Subject: [PATCH] Handle failed redirects --- src/utils.rs | 14 +++++++++----- templates/utils.html | 5 ++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index d6acf27..cfab28e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -133,9 +133,10 @@ pub fn prefs(req: HttpRequest) -> Preferences { // Grab a query param from a url pub fn param(path: &str, value: &str) -> String { - let url = Url::parse(format!("https://libredd.it/{}", path).as_str()).unwrap(); - let pairs: HashMap<_, _> = url.query_pairs().into_owned().collect(); - pairs.get(value).unwrap_or(&String::new()).to_owned() + match Url::parse(format!("https://libredd.it/{}", path).as_str()) { + Ok(url) => url.query_pairs().into_owned().collect::>().get(value).unwrap_or(&String::new()).to_owned(), + _ => String::new(), + } } // Parse Cookie value from request @@ -360,9 +361,12 @@ pub async fn request(path: &str) -> Result { // Get first number of response HTTP status code match payload.status().to_string().chars().next() { // If success - Some('2') => Ok(String::from_utf8(payload.body().limit(20_000_000).await.unwrap().to_vec()).unwrap()), + Some('2') => Ok(String::from_utf8(payload.body().limit(20_000_000).await.unwrap_or_default().to_vec()).unwrap_or_default()), // If redirection - Some('3') => Err((true, payload.headers().get("location").unwrap().to_str().unwrap().to_string())), + Some('3') => match payload.headers().get("location") { + Some(location) => Err((true, location.to_str().unwrap_or_default().to_string())), + None => Err((false, "Page not found".to_string())) + } // Otherwise _ => Err((false, "Page not found".to_string())), } diff --git a/templates/utils.html b/templates/utils.html index 968f198..cb3c65a 100644 --- a/templates/utils.html +++ b/templates/utils.html @@ -28,5 +28,8 @@ {%- endmacro %} {% macro render_flair(flair) -%} -{% for flair_part in flair %}{% if flair_part.flair_part_type == "emoji" %}{% else if flair_part.flair_part_type == "text" %}{{ flair_part.value }}{% endif %}{% endfor %} +{% for flair_part in flair %} + {% if flair_part.flair_part_type == "emoji" %} + {% else if flair_part.flair_part_type == "text" %}{{ flair_part.value }}{% endif %} +{% endfor %} {%- endmacro %}