Merge pull request #1223 from LemmyNet/remove_cache_headers

Remove cache headers. Fixes #1222
This commit is contained in:
Dessalines 2020-10-22 14:56:43 -04:00 committed by GitHub
commit 52c679a3cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 40 deletions

View file

@ -78,6 +78,4 @@ lazy_static! {
Settings::get().hostname Settings::get().hostname
)) ))
.unwrap(); .unwrap();
pub static ref CACHE_CONTROL_REGEX: Regex =
Regex::new("^((text|image)/.+|application/javascript)$").unwrap();
} }

View file

@ -2,38 +2,23 @@
extern crate diesel_migrations; extern crate diesel_migrations;
use actix::prelude::*; use actix::prelude::*;
use actix_web::{ use actix_web::*;
body::Body,
dev::{Service, ServiceRequest, ServiceResponse},
http::{
header::{CACHE_CONTROL, CONTENT_TYPE},
HeaderValue,
},
*,
};
use diesel::{ use diesel::{
r2d2::{ConnectionManager, Pool}, r2d2::{ConnectionManager, Pool},
PgConnection, PgConnection,
}; };
use lazy_static::lazy_static;
use lemmy_api::match_websocket_operation; use lemmy_api::match_websocket_operation;
use lemmy_apub::activity_queue::create_activity_queue; use lemmy_apub::activity_queue::create_activity_queue;
use lemmy_db::get_database_url_from_env; use lemmy_db::get_database_url_from_env;
use lemmy_rate_limit::{rate_limiter::RateLimiter, RateLimit}; use lemmy_rate_limit::{rate_limiter::RateLimiter, RateLimit};
use lemmy_server::{code_migrations::run_advanced_migrations, routes::*}; use lemmy_server::{code_migrations::run_advanced_migrations, routes::*};
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{settings::Settings, LemmyError, CACHE_CONTROL_REGEX}; use lemmy_utils::{settings::Settings, LemmyError};
use lemmy_websocket::{chat_server::ChatServer, LemmyContext}; use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
use reqwest::Client; use reqwest::Client;
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::Mutex; use tokio::sync::Mutex;
lazy_static! {
// static ref CACHE_CONTROL_VALUE: String = format!("public, max-age={}", 365 * 24 * 60 * 60);
// Test out 1 hour here, this is breaking some things
static ref CACHE_CONTROL_VALUE: String = format!("public, max-age={}", 60 * 60);
}
embed_migrations!(); embed_migrations!();
#[actix_web::main] #[actix_web::main]
@ -90,7 +75,6 @@ async fn main() -> Result<(), LemmyError> {
); );
let rate_limiter = rate_limiter.clone(); let rate_limiter = rate_limiter.clone();
App::new() App::new()
.wrap_fn(add_cache_headers)
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.data(context) .data(context)
// The routes // The routes
@ -108,23 +92,3 @@ async fn main() -> Result<(), LemmyError> {
Ok(()) Ok(())
} }
fn add_cache_headers<S>(
req: ServiceRequest,
srv: &mut S,
) -> impl Future<Output = Result<ServiceResponse, Error>>
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<Body>, Error = Error>,
{
let fut = srv.call(req);
async move {
let mut res = fut.await?;
if let Some(content_type) = res.headers().get(CONTENT_TYPE) {
if CACHE_CONTROL_REGEX.is_match(content_type.to_str().unwrap()) {
let header_val = HeaderValue::from_static(&CACHE_CONTROL_VALUE);
res.headers_mut().insert(CACHE_CONTROL, header_val);
}
}
Ok(res)
}
}