follow redirects

This commit is contained in:
Sam Denty 2022-07-24 15:57:12 +01:00
parent 21e584be0d
commit 05372c8b70
No known key found for this signature in database
GPG key ID: 7B4EAF7B9E291B79
4 changed files with 12 additions and 4 deletions

2
Cargo.lock generated
View file

@ -1494,7 +1494,7 @@ checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de"
[[package]] [[package]]
name = "site_icons" name = "site_icons"
version = "0.1.11" version = "0.1.12"
dependencies = [ dependencies = [
"byteorder", "byteorder",
"clap", "clap",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "site_icons" name = "site_icons"
version = "0.1.11" version = "0.1.12"
authors = ["Sam Denty <sam@samdenty.com>"] authors = ["Sam Denty <sam@samdenty.com>"]
edition = "2018" edition = "2018"
license = "GPL-3.0" license = "GPL-3.0"

View file

@ -30,7 +30,10 @@ pub enum IconInfo {
} }
impl IconInfo { impl IconInfo {
pub async fn load(url: Url, sizes: Option<String>) -> Result<IconInfo, Box<dyn Error + Send + Sync>> { pub async fn load(
url: Url,
sizes: Option<String>,
) -> Result<IconInfo, Box<dyn Error + Send + Sync>> {
let sizes = sizes.as_ref().and_then(|s| IconSizes::from_str(s).ok()); let sizes = sizes.as_ref().and_then(|s| IconSizes::from_str(s).ok());
let (mime, mut body): (_, Box<dyn AsyncRead + Unpin + Send + Sync>) = match url.scheme() { let (mime, mut body): (_, Box<dyn AsyncRead + Unpin + Send + Sync>) = match url.scheme() {

View file

@ -38,13 +38,18 @@ pub use icons::*;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use reqwest::{ use reqwest::{
header::{HeaderMap, HeaderValue, USER_AGENT}, header::{HeaderMap, HeaderValue, USER_AGENT},
redirect::Policy,
Client, Client,
}; };
static CLIENT: Lazy<Client> = Lazy::new(|| { static CLIENT: Lazy<Client> = Lazy::new(|| {
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
headers.insert(USER_AGENT, HeaderValue::from_str("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36").unwrap()); headers.insert(USER_AGENT, HeaderValue::from_str("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36").unwrap());
Client::builder().default_headers(headers).build().unwrap() Client::builder()
.default_headers(headers)
.redirect(Policy::limited(20))
.build()
.unwrap()
}); });
#[cfg(test)] #[cfg(test)]