From bf83ee68f3f1557a05ed669eb42d461da6be32b4 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Fri, 20 Jan 2023 10:41:16 -0800 Subject: [PATCH] Serve favicon as PNG to Safari and SVG others (#1302) --- Cargo.lock | 39 +++++++++++++++++++++++++++++++++- Cargo.toml | 2 +- src/subcommand/server.rs | 45 +++++++++++++++++++++++++--------------- src/templates.rs | 2 -- templates/page.html | 2 -- 5 files changed, 67 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3fbf77a8d8..245e607ce9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -371,6 +371,7 @@ dependencies = [ "bitflags", "bytes", "futures-util", + "headers", "http", "http-body", "hyper", @@ -1349,6 +1350,31 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "headers" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3e372db8e5c0d213e0cd0b9be18be2aca3d44cf2fe30a9d46a65581cd454584" +dependencies = [ + "base64 0.13.1", + "bitflags", + "bytes", + "headers-core", + "http", + "httpdate", + "mime", + "sha1 0.10.5", +] + +[[package]] +name = "headers-core" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" +dependencies = [ + "http", +] + [[package]] name = "heck" version = "0.4.0" @@ -2866,6 +2892,17 @@ dependencies = [ "sha1_smol", ] +[[package]] +name = "sha1" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f04293dc80c3993519f2d7f6f511707ee7094fe0c6d3406feb330cdb3540eba3" +dependencies = [ + "cfg-if 1.0.0", + "cpufeatures", + "digest 0.10.6", +] + [[package]] name = "sha1_smol" version = "1.0.0" @@ -3035,7 +3072,7 @@ dependencies = [ "serde", "serde_derive", "serde_json", - "sha1", + "sha1 0.6.1", "syn", ] diff --git a/Cargo.toml b/Cargo.toml index 2063d3cac8..61ccc8c483 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ members = [".", "test-bitcoincore-rpc"] [dependencies] anyhow = { version = "1.0.56", features = ["backtrace"] } -axum = "0.6.1" +axum = { version = "0.6.1", features = ["headers"] } axum-server = "0.4.0" bech32 = "0.9.1" bip39 = "1.0.1" diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 2a2af4caf7..0c7f3bc4c6 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -12,10 +12,11 @@ use { axum::{ body, extract::{Extension, Path, Query}, + headers::UserAgent, http::{header, HeaderMap, HeaderValue, StatusCode, Uri}, response::{IntoResponse, Redirect, Response}, routing::get, - Router, + Router, TypedHeader, }, axum_server::Handle, rust_embed::RustEmbed, @@ -136,8 +137,7 @@ impl Server { .route("/clock", get(Self::clock)) .route("/content/:inscription_id", get(Self::content)) .route("/faq", get(Self::faq)) - .route("/favicon.ico", get(Self::favicon_ico)) - .route("/favicon.svg", get(Self::favicon_svg)) + .route("/favicon.ico", get(Self::favicon)) .route("/feed.xml", get(Self::feed)) .route("/input/:block/:transaction/:input", get(Self::input)) .route("/inscription/:inscription_id", get(Self::inscription)) @@ -555,21 +555,32 @@ impl Server { } } - async fn favicon_ico() -> ServerResult { - Self::static_asset(Path("/favicon.png".to_string())).await - } - - async fn favicon_svg() -> ServerResult { - Ok( - ( - [( - header::CONTENT_SECURITY_POLICY, - HeaderValue::from_static("default-src 'unsafe-inline'"), - )], - Self::static_asset(Path("/favicon.svg".to_string())).await?, + async fn favicon(user_agent: Option>) -> ServerResult { + if user_agent + .map(|user_agent| { + user_agent.as_str().contains("Safari/") + && !user_agent.as_str().contains("Chrome/") + && !user_agent.as_str().contains("Chromium/") + }) + .unwrap_or_default() + { + Ok( + Self::static_asset(Path("/favicon.png".to_string())) + .await + .into_response(), ) - .into_response(), - ) + } else { + Ok( + ( + [( + header::CONTENT_SECURITY_POLICY, + HeaderValue::from_static("default-src 'unsafe-inline'"), + )], + Self::static_asset(Path("/favicon.svg".to_string())).await?, + ) + .into_response(), + ) + } } async fn feed( diff --git a/src/templates.rs b/src/templates.rs index 6435077d8f..8ad882af8c 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -95,8 +95,6 @@ mod tests { Foo - - diff --git a/templates/page.html b/templates/page.html index 683cb2e05d..a8f01a74ee 100644 --- a/templates/page.html +++ b/templates/page.html @@ -9,8 +9,6 @@ %% } {{ self.content.title() }} - -