"manually" create ETag and Cache-Control headers

This commit is contained in:
Igor Galić 2020-01-27 23:44:27 +01:00
parent b51551973a
commit 45c335e17b
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86
2 changed files with 8 additions and 10 deletions

View File

@ -7,9 +7,8 @@ use chrono::naive::NaiveDateTime;
use plume_models::{posts::Post, Connection, CONFIG, ITEMS_PER_PAGE};
use rocket::{
http::{
hyper::header::{CacheControl, CacheDirective, ETag, EntityTag},
uri::{FromUriParam, Query},
RawStr, Status,
Header, RawStr, Status,
},
request::{self, FromFormValue, FromRequest, Request},
response::{self, Flash, NamedFile, Redirect, Responder, Response},
@ -208,7 +207,7 @@ pub mod well_known;
#[response()]
pub struct CachedFile {
inner: NamedFile,
cache_control: CacheControl,
cache_control: Header<'static>,
}
#[derive(Debug)]
@ -228,12 +227,12 @@ impl<'r> Responder<'r> for ThemeFile {
{
Response::build()
.status(Status::NotModified)
.header(ETag(EntityTag::strong(etag)))
.header("ETag", etag)
.ok()
} else {
Response::build()
.merge(self.0.respond_to(r)?)
.header(ETag(EntityTag::strong(etag)))
.header("ETag", etag)
.ok()
}
}
@ -256,7 +255,7 @@ pub fn plume_media_files(file: PathBuf) -> Option<CachedFile> {
.ok()
.map(|f| CachedFile {
inner: f,
cache_control: CacheControl(vec![CacheDirective::MaxAge(60 * 60 * 24 * 30)]),
cache_control: Header::new("Cache-Control", format!("max-age={}", 60 * 60 * 24 * 30)),
})
}
#[get("/static/<file..>", rank = 3)]
@ -265,6 +264,6 @@ pub fn static_files(file: PathBuf) -> Option<CachedFile> {
.ok()
.map(|f| CachedFile {
inner: f,
cache_control: CacheControl(vec![CacheDirective::MaxAge(60 * 60 * 24 * 30)]),
cache_control: Header::new("Cache-Control", format!("max-age={}", 60 * 60 * 24 * 30)),
})
}

View File

@ -1,7 +1,6 @@
use plume_models::{notifications::*, users::User, Connection, PlumeRocket};
use crate::templates::Html;
use rocket::http::hyper::header::{ETag, EntityTag};
use rocket::http::{Method, Status};
use rocket::request::Request;
use rocket::response::{self, content::Html as HtmlCt, Responder, Response};
@ -67,12 +66,12 @@ impl<'r> Responder<'r> for Ructe {
{
Response::build()
.status(Status::NotModified)
.header(ETag(EntityTag::strong(etag)))
.header("ETag", etag)
.ok()
} else {
Response::build()
.merge(HtmlCt(self.0).respond_to(r)?)
.header(ETag(EntityTag::strong(etag)))
.header("ETag", etag)
.ok()
}
}