From ed70b07d8184a6e5c3bb048af15386fb68b647c6 Mon Sep 17 00:00:00 2001 From: James Hurst Date: Mon, 9 Nov 2020 20:45:58 -0500 Subject: [PATCH 1/2] Return 404 instead of fallback icon --- src/api/icons.rs | 26 ++++++++++++-------------- src/static/fallback-icon.png | Bin 344 -> 0 bytes 2 files changed, 12 insertions(+), 14 deletions(-) delete mode 100644 src/static/fallback-icon.png diff --git a/src/api/icons.rs b/src/api/icons.rs index 5653bb1..f1916c3 100644 --- a/src/api/icons.rs +++ b/src/api/icons.rs @@ -17,8 +17,6 @@ pub fn routes() -> Vec { routes![icon] } -const FALLBACK_ICON: &[u8; 344] = include_bytes!("../static/fallback-icon.png"); - const ALLOWED_CHARS: &str = "_-."; static CLIENT: Lazy = Lazy::new(|| { @@ -52,15 +50,15 @@ fn is_valid_domain(domain: &str) -> bool { } #[get("//icon.png")] -fn icon(domain: String) -> Cached>> { - let icon_type = ContentType::new("image", "x-icon"); - +fn icon(domain: String) -> Option>>> { if !is_valid_domain(&domain) { warn!("Invalid domain: {:#?}", domain); - return Cached::long(Content(icon_type, FALLBACK_ICON.to_vec())); + return None; } - Cached::long(Content(icon_type, get_icon(&domain))) + get_icon(&domain).map(|icon| { + Cached::long(Content(ContentType::new("image", "x-icon"), icon)) + }) } /// TODO: This is extracted from IpAddr::is_global, which is unstable: @@ -190,29 +188,29 @@ fn check_icon_domain_is_blacklisted(domain: &str) -> bool { is_blacklisted } -fn get_icon(domain: &str) -> Vec { +fn get_icon(domain: &str) -> Option> { let path = format!("{}/{}.png", CONFIG.icon_cache_folder(), domain); if let Some(icon) = get_cached_icon(&path) { - return icon; + return Some(icon); } if CONFIG.disable_icon_download() { - return FALLBACK_ICON.to_vec(); + return None; } - // Get the icon, or fallback in case of error + // Get the icon, or None in case of error match download_icon(&domain) { Ok(icon) => { save_icon(&path, &icon); - icon + Some(icon) } Err(e) => { error!("Error downloading icon: {:?}", e); let miss_indicator = path + ".miss"; let empty_icon = Vec::new(); save_icon(&miss_indicator, &empty_icon); - FALLBACK_ICON.to_vec() + None } } } @@ -220,7 +218,7 @@ fn get_icon(domain: &str) -> Vec { fn get_cached_icon(path: &str) -> Option> { // Check for expiration of negatively cached copy if icon_is_negcached(path) { - return Some(FALLBACK_ICON.to_vec()); + return None; } // Check for expiration of successfully cached copy diff --git a/src/static/fallback-icon.png b/src/static/fallback-icon.png deleted file mode 100644 index 7ba709fd1ae2a7f467f948e9c3a9d69c2da3afef..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 344 zcmV-e0jK_nP)e( zze@u_5QX2JLgEilk#L|QLO>A_1jR~8Zy{(asnSHW{~8NRI}t2`h5taX3)l%_pGG7g zB4Tj}M+&hB62e$yx$MPP&CGuL_RUI8gzQqIMa%=IL>d;$B((XYj?uv|t{L%xLyR&r zjFV@TI}F2kAwO_1)?7V-(V;+{9>zUEKSRQa=Qzg=fy5TS7zdIlVZ5f6k_aWHsnW$r z2*fRhksFF%ae0hL#2Cgx1~^LrV_R;;52oC4K!I-$DT!xSHdv9kq)W{MjCFCIHa~>y zP|g6xq$qlAlYJr{y~$Jm^+x7aiz^mca*>ODBu|ooz9fk;VvYw(&hq9VRYFR%A^TOM qT%<@$k9RioU*y>2hzfhm`}03sXXm+tt9(EJ0000 Date: Mon, 9 Nov 2020 21:50:35 -0500 Subject: [PATCH 2/2] Fix for negcached icons --- src/api/icons.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/api/icons.rs b/src/api/icons.rs index f1916c3..f4e4edf 100644 --- a/src/api/icons.rs +++ b/src/api/icons.rs @@ -191,6 +191,11 @@ fn check_icon_domain_is_blacklisted(domain: &str) -> bool { fn get_icon(domain: &str) -> Option> { let path = format!("{}/{}.png", CONFIG.icon_cache_folder(), domain); + // Check for expiration of negatively cached copy + if icon_is_negcached(&path) { + return None; + } + if let Some(icon) = get_cached_icon(&path) { return Some(icon); } @@ -216,11 +221,6 @@ fn get_icon(domain: &str) -> Option> { } fn get_cached_icon(path: &str) -> Option> { - // Check for expiration of negatively cached copy - if icon_is_negcached(path) { - return None; - } - // Check for expiration of successfully cached copy if icon_is_expired(path) { return None;