mirror of
https://github.com/chatmail/core.git
synced 2026-05-02 21:06:31 +03:00
Follow http redirects for autoconfigure (#3208)
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## Unreleased
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
|
||||||
|
- Further improve finding the correct server after logging in #3208
|
||||||
|
|
||||||
## 1.77.0
|
## 1.77.0
|
||||||
|
|
||||||
### API changes
|
### API changes
|
||||||
|
|||||||
@@ -699,11 +699,11 @@ pub enum Error {
|
|||||||
error: quick_xml::Error,
|
error: quick_xml::Error,
|
||||||
},
|
},
|
||||||
|
|
||||||
#[error("Failed to get URL: {0}")]
|
|
||||||
ReadUrl(#[from] self::read_url::Error),
|
|
||||||
|
|
||||||
#[error("Number of redirection is exceeded")]
|
#[error("Number of redirection is exceeded")]
|
||||||
Redirection,
|
Redirection,
|
||||||
|
|
||||||
|
#[error("{0:#}")]
|
||||||
|
Other(#[from] anyhow::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -1,20 +1,40 @@
|
|||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
use anyhow::format_err;
|
||||||
pub enum Error {
|
use anyhow::Context as _;
|
||||||
#[error("URL request error")]
|
|
||||||
GetError(surf::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn read_url(context: &Context, url: &str) -> Result<String, Error> {
|
pub async fn read_url(context: &Context, url: &str) -> anyhow::Result<String> {
|
||||||
info!(context, "Requesting URL {}", url);
|
match read_url_inner(context, url).await {
|
||||||
|
Ok(s) => {
|
||||||
match surf::get(url).recv_string().await {
|
info!(context, "Successfully read url {}", url);
|
||||||
Ok(res) => Ok(res),
|
Ok(s)
|
||||||
Err(err) => {
|
}
|
||||||
info!(context, "Can\'t read URL {}: {}", url, err);
|
Err(e) => {
|
||||||
|
info!(context, "Can't read URL {}: {:#}", url, e);
|
||||||
Err(Error::GetError(err))
|
Err(format_err!("Can't read URL {}: {:#}", url, e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn read_url_inner(context: &Context, mut url: &str) -> anyhow::Result<String> {
|
||||||
|
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"))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user