"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 plume_models::{posts::Post, Connection, CONFIG, ITEMS_PER_PAGE};
use rocket::{ use rocket::{
http::{ http::{
hyper::header::{CacheControl, CacheDirective, ETag, EntityTag},
uri::{FromUriParam, Query}, uri::{FromUriParam, Query},
RawStr, Status, Header, RawStr, Status,
}, },
request::{self, FromFormValue, FromRequest, Request}, request::{self, FromFormValue, FromRequest, Request},
response::{self, Flash, NamedFile, Redirect, Responder, Response}, response::{self, Flash, NamedFile, Redirect, Responder, Response},
@ -208,7 +207,7 @@ pub mod well_known;
#[response()] #[response()]
pub struct CachedFile { pub struct CachedFile {
inner: NamedFile, inner: NamedFile,
cache_control: CacheControl, cache_control: Header<'static>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -228,12 +227,12 @@ impl<'r> Responder<'r> for ThemeFile {
{ {
Response::build() Response::build()
.status(Status::NotModified) .status(Status::NotModified)
.header(ETag(EntityTag::strong(etag))) .header("ETag", etag)
.ok() .ok()
} else { } else {
Response::build() Response::build()
.merge(self.0.respond_to(r)?) .merge(self.0.respond_to(r)?)
.header(ETag(EntityTag::strong(etag))) .header("ETag", etag)
.ok() .ok()
} }
} }
@ -256,7 +255,7 @@ pub fn plume_media_files(file: PathBuf) -> Option<CachedFile> {
.ok() .ok()
.map(|f| CachedFile { .map(|f| CachedFile {
inner: f, 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)] #[get("/static/<file..>", rank = 3)]
@ -265,6 +264,6 @@ pub fn static_files(file: PathBuf) -> Option<CachedFile> {
.ok() .ok()
.map(|f| CachedFile { .map(|f| CachedFile {
inner: f, 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 plume_models::{notifications::*, users::User, Connection, PlumeRocket};
use crate::templates::Html; use crate::templates::Html;
use rocket::http::hyper::header::{ETag, EntityTag};
use rocket::http::{Method, Status}; use rocket::http::{Method, Status};
use rocket::request::Request; use rocket::request::Request;
use rocket::response::{self, content::Html as HtmlCt, Responder, Response}; use rocket::response::{self, content::Html as HtmlCt, Responder, Response};
@ -67,12 +66,12 @@ impl<'r> Responder<'r> for Ructe {
{ {
Response::build() Response::build()
.status(Status::NotModified) .status(Status::NotModified)
.header(ETag(EntityTag::strong(etag))) .header("ETag", etag)
.ok() .ok()
} else { } else {
Response::build() Response::build()
.merge(HtmlCt(self.0).respond_to(r)?) .merge(HtmlCt(self.0).respond_to(r)?)
.header(ETag(EntityTag::strong(etag))) .header("ETag", etag)
.ok() .ok()
} }
} }