From 703e790d63c7fb06212538f2c3f3671d59de4029 Mon Sep 17 00:00:00 2001 From: Vika Date: Mon, 26 Aug 2024 21:12:40 +0300 Subject: Use `Cache-Control: private` whenever the user is authorized --- src/frontend/mod.rs | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) (limited to 'src/frontend/mod.rs') diff --git a/src/frontend/mod.rs b/src/frontend/mod.rs index 69f6aa5..e142ee1 100644 --- a/src/frontend/mod.rs +++ b/src/frontend/mod.rs @@ -244,6 +244,11 @@ pub async fn homepage( // This is stupid, but there is no other way. let hcard_url: url::Url = format!("https://{}/", host).parse().unwrap(); let feed_path = format!("https://{}/feeds/main", host); + let mut headers = axum::http::HeaderMap::new(); + headers.insert( + axum::http::header::CONTENT_TYPE, + axum::http::HeaderValue::from_static(r#"text/html; charset="utf-8""#), + ); let user = session.as_deref().map(|s| &s.me); match tokio::try_join!( @@ -265,13 +270,16 @@ pub async fn homepage( db.get_channels(&hcard_url).map(|i| i.unwrap_or_default()) ); + if user.is_some() { + headers.insert( + axum::http::header::CACHE_CONTROL, + axum::http::HeaderValue::from_static("private") + ); + } // Render the homepage ( StatusCode::OK, - [( - axum::http::header::CONTENT_TYPE, - r#"text/html; charset="utf-8""#, - )], + headers, Template { title: blogname.as_ref(), blog_name: blogname.as_ref(), @@ -286,7 +294,7 @@ pub async fn homepage( .to_string(), } .to_string(), - ) + ).into_response() } Err(err) => { if err.code == StatusCode::NOT_FOUND { @@ -296,7 +304,7 @@ pub async fn homepage( StatusCode::FOUND, [(axum::http::header::LOCATION, "/.kittybox/onboarding")], String::default(), - ) + ).into_response() } else { error!("Error while fetching h-card and/or h-feed: {}", err); // Return the error @@ -308,11 +316,7 @@ pub async fn homepage( ); ( - err.code(), - [( - axum::http::header::CONTENT_TYPE, - r#"text/html; charset="utf-8""#, - )], + err.code(), headers, Template { title: blogname.as_ref(), blog_name: blogname.as_ref(), @@ -325,7 +329,7 @@ pub async fn homepage( .to_string(), } .to_string(), - ) + ).into_response() } } } @@ -354,13 +358,22 @@ pub async fn catchall( db.get_channels(&host).map(|i| i.unwrap_or_default()) ); + let mut headers = axum::http::HeaderMap::new(); + headers.insert( + axum::http::header::CONTENT_TYPE, + axum::http::HeaderValue::from_static(r#"text/html; charset="utf-8""#), + ); + if user.is_some() { + headers.insert( + axum::http::header::CACHE_CONTROL, + axum::http::HeaderValue::from_static("private") + ); + } + // Render the homepage ( StatusCode::OK, - [( - axum::http::header::CONTENT_TYPE, - r#"text/html; charset="utf-8""#, - )], + headers, Template { title: blogname.as_ref(), blog_name: blogname.as_ref(), @@ -376,7 +389,7 @@ pub async fn catchall( }, } .to_string(), - ) + ).into_response() } Err(err) => { let (blogname, channels) = tokio::join!( @@ -403,7 +416,7 @@ pub async fn catchall( .to_string(), } .to_string(), - ) + ).into_response() } } } -- cgit 1.4.1