Provide Cache-Control Headers
max-age = 1 week for everything stale-while-revalidate = 1 day for everything except raw pastes immutable for raw pastes Most likely fixes #2, unless I forgot something. Suggested-by: Leonora Tindall <ltindall@fastly.com> Signed-off-by: Gunwant Jain <mail@wantguns.dev>
This commit is contained in:
@@ -8,7 +8,8 @@ use std::time::SystemTime;
|
|||||||
|
|
||||||
pub enum ResponseWrapper<R> {
|
pub enum ResponseWrapper<R> {
|
||||||
MetaInterfaceResponse(R),
|
MetaInterfaceResponse(R),
|
||||||
PasteContentResponse(R, SystemTime),
|
PrettyPasteContentResponse(R, SystemTime),
|
||||||
|
RawPasteContentResponse(R, SystemTime),
|
||||||
Redirect(Box<Redirect>),
|
Redirect(Box<Redirect>),
|
||||||
NotFound(String),
|
NotFound(String),
|
||||||
ServerError(String),
|
ServerError(String),
|
||||||
@@ -19,8 +20,12 @@ impl<'r, 'o: 'r, R: Responder<'r, 'o>> ResponseWrapper<R> {
|
|||||||
Self::MetaInterfaceResponse(responder)
|
Self::MetaInterfaceResponse(responder)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn paste_response(responder: R, modified: SystemTime) -> Self {
|
pub fn pretty_paste_response(responder: R, modified: SystemTime) -> Self {
|
||||||
Self::PasteContentResponse(responder, modified)
|
Self::PrettyPasteContentResponse(responder, modified)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn raw_paste_response(responder: R, modified: SystemTime) -> Self {
|
||||||
|
Self::RawPasteContentResponse(responder, modified)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn redirect(redirect: Redirect) -> Self {
|
pub fn redirect(redirect: Redirect) -> Self {
|
||||||
@@ -51,15 +56,31 @@ impl<'r, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o>
|
|||||||
MetaInterfaceResponse(sup) => response
|
MetaInterfaceResponse(sup) => response
|
||||||
.join(sup.respond_to(request)?)
|
.join(sup.respond_to(request)?)
|
||||||
.raw_header("ETag", &*crate::BINARY_ETAG)
|
.raw_header("ETag", &*crate::BINARY_ETAG)
|
||||||
|
.raw_header(
|
||||||
|
"Cache-Control",
|
||||||
|
"max-age=604800, stale-while-revalidate=86400",
|
||||||
|
)
|
||||||
.ok(),
|
.ok(),
|
||||||
PasteContentResponse(sup, modified) => response
|
|
||||||
|
PrettyPasteContentResponse(sup, modified) => response
|
||||||
.join(sup.respond_to(request)?)
|
.join(sup.respond_to(request)?)
|
||||||
.raw_header("Last-Modified", http_strftime(modified))
|
.raw_header("Last-Modified", http_strftime(modified))
|
||||||
|
.raw_header(
|
||||||
|
"Cache-Control",
|
||||||
|
"max-age=604800, stale-while-revalidate=86400",
|
||||||
|
)
|
||||||
.ok(),
|
.ok(),
|
||||||
|
|
||||||
|
RawPasteContentResponse(sup, modified) => response
|
||||||
|
.join(sup.respond_to(request)?)
|
||||||
|
.raw_header("Last-Modified", http_strftime(modified))
|
||||||
|
.raw_header("Cache-Control", "max-age=604800, immutable")
|
||||||
|
.ok(),
|
||||||
|
|
||||||
Redirect(sup) => response.join(sup.respond_to(request)?).ok(),
|
Redirect(sup) => response.join(sup.respond_to(request)?).ok(),
|
||||||
|
|
||||||
NotFound(s) => {
|
NotFound(s) => {
|
||||||
let body = format!("Unable to find entity '{}'", s);
|
let body = format!("Unable to find entity '{}'", s);
|
||||||
|
|
||||||
response
|
response
|
||||||
.sized_body(body.len(), Cursor::new(body))
|
.sized_body(body.len(), Cursor::new(body))
|
||||||
.status(Status::NotFound)
|
.status(Status::NotFound)
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ use rocket_dyn_templates::Template;
|
|||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use crate::models::response_wrapper::ResponseWrapper;
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
pub async fn index() -> Option<Template> {
|
pub async fn index() -> ResponseWrapper<Template> {
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
map.insert("title", "bin");
|
map.insert("title", "bin");
|
||||||
Some(Template::render("index.html", &map))
|
ResponseWrapper::meta_response(Template::render("index.html", &map))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,7 +66,9 @@ pub async fn pretter_retrieve_inner(
|
|||||||
let rendered = Template::render("pretty.html", &map);
|
let rendered = Template::render("pretty.html", &map);
|
||||||
|
|
||||||
match tree_magic::match_filepath("text/plain", &filepath) {
|
match tree_magic::match_filepath("text/plain", &filepath) {
|
||||||
true => ResponseWrapper::paste_response(rendered, modified_date),
|
true => {
|
||||||
|
ResponseWrapper::pretty_paste_response(rendered, modified_date)
|
||||||
|
}
|
||||||
false => ResponseWrapper::server_error("media type unacceptable"),
|
false => ResponseWrapper::server_error("media type unacceptable"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,5 +42,5 @@ pub async fn retrieve_inner(id: &str) -> ResponseWrapper<File> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ResponseWrapper::paste_response(file, modified_date)
|
ResponseWrapper::raw_paste_response(file, modified_date)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user