From c8c4c12717272f2cf255e3d965b78665784938a8 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 23 Aug 2022 18:11:57 +0200 Subject: [PATCH] Change request for checking for redirects from get to head Signed-off-by: trivernis --- src/utils/reddit.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/utils/reddit.rs b/src/utils/reddit.rs index d999313..06c75d7 100644 --- a/src/utils/reddit.rs +++ b/src/utils/reddit.rs @@ -78,10 +78,10 @@ async fn get_post(url: &str) -> Result { // url cleanup // add trailing slash and remove path params + if let Some((left, right)) = url.rsplit_once('?') { + url = left.to_string(); + } if !url.ends_with('/') { - if let Some((left, right)) = url.rsplit_once('?') { - url = left.to_string(); - } url.push('/'); } let mut response: Vec = reqwest::get(format!("{}.json", url)).await?.json().await?; @@ -103,16 +103,21 @@ async fn get_post(url: &str) -> Result { #[tracing::instrument(level = "debug")] async fn resolve_redirects(url: &str) -> Result { if is_resolved(url) { + tracing::debug!("Url already resolved."); return Ok(url.to_string()); } let client = reqwest::Client::builder() .redirect(Policy::none()) .build()?; - let response = client.get(url).send().await?; + let response = client.head(url).send().await?; + if let Some(location) = response.headers().get("location") { - return Ok(location.to_str().unwrap().to_string()); + tracing::debug!("Redirect to {location:?} found"); + Ok(location.to_str().unwrap().to_string()) + } else { + tracing::debug!("No redirect found."); + Ok(response.url().as_str().to_string()) } - Ok(url.to_string()) } /// Checks if the url is already in a format that can be used for retrieving information