diff --git a/CHANGELOG.md b/CHANGELOG.md index 44c9388a1..6c8f4b4b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Changes + +- Further improve finding the correct server after logging in #3208 + ## 1.77.0 ### API changes diff --git a/src/configure.rs b/src/configure.rs index 4cb61d35e..97ed05884 100644 --- a/src/configure.rs +++ b/src/configure.rs @@ -699,11 +699,11 @@ pub enum Error { error: quick_xml::Error, }, - #[error("Failed to get URL: {0}")] - ReadUrl(#[from] self::read_url::Error), - #[error("Number of redirection is exceeded")] Redirection, + + #[error("{0:#}")] + Other(#[from] anyhow::Error), } #[cfg(test)] diff --git a/src/configure/read_url.rs b/src/configure/read_url.rs index dfd860c12..0f20d7a7f 100644 --- a/src/configure/read_url.rs +++ b/src/configure/read_url.rs @@ -1,20 +1,40 @@ use crate::context::Context; -#[derive(Debug, thiserror::Error)] -pub enum Error { - #[error("URL request error")] - GetError(surf::Error), -} +use anyhow::format_err; +use anyhow::Context as _; -pub async fn read_url(context: &Context, url: &str) -> Result { - info!(context, "Requesting URL {}", url); - - match surf::get(url).recv_string().await { - Ok(res) => Ok(res), - Err(err) => { - info!(context, "Can\'t read URL {}: {}", url, err); - - Err(Error::GetError(err)) +pub async fn read_url(context: &Context, url: &str) -> anyhow::Result { + match read_url_inner(context, url).await { + Ok(s) => { + info!(context, "Successfully read url {}", url); + Ok(s) + } + Err(e) => { + info!(context, "Can't read URL {}: {:#}", url, e); + Err(format_err!("Can't read URL {}: {:#}", url, e)) } } } + +pub async fn read_url_inner(context: &Context, mut url: &str) -> anyhow::Result { + let mut _temp; // For the borrow checker + + // Follow up to 10 http-redirects + for _i in 0..10 { + let mut response = surf::get(url).send().await.map_err(|e| e.into_inner())?; + if response.status().is_redirection() { + _temp = response + .header("location") + .context("Redirection doesn't have a target location")? + .last() + .to_string(); + info!(context, "Following redirect to {}", _temp); + url = &_temp; + continue; + } + + return response.body_string().await.map_err(|e| e.into_inner()); + } + + Err(format_err!("Followed 10 redirections")) +}