From 9721bda961db35aee1be16069a5ea4e83bf53e4b Mon Sep 17 00:00:00 2001 From: Pierce Thompson Date: Sun, 5 May 2024 10:51:18 -0400 Subject: [PATCH] Fix error pages returning a 200 status code Closes https://github.com/Insprill/intellectual/issues/31. --- src/errors.rs | 16 +++++++++------- src/templates.rs | 14 ++++++++++---- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index 3a1d6dc..8b956c4 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -6,12 +6,12 @@ use actix_web::{ HttpResponse, Result, }; use askama::Template; -use awc::error::HeaderValue; +use awc::{error::HeaderValue}; use log::error; use crate::{ settings::{settings_from_req, Settings}, - templates::template, + templates::{template_with_res}, }; pub fn render_500(res: ServiceResponse) -> Result> { @@ -20,8 +20,9 @@ pub fn render_500(res: ServiceResponse) -> Result> error!("{}", str); } - let new_response = template( + let new_response = template_with_res( res.request(), + HttpResponse::InternalServerError(), InternalErrorTemplate { settings: settings_from_req(res.request()), err, @@ -31,8 +32,9 @@ pub fn render_500(res: ServiceResponse) -> Result> } pub fn render_404(res: ServiceResponse) -> Result> { - let new_response = template( + let new_response = template_with_res( res.request(), + HttpResponse::NotFound(), NotFoundTemplate { settings: settings_from_req(res.request()), }, @@ -41,8 +43,9 @@ pub fn render_404(res: ServiceResponse) -> Result> } pub fn render_400(res: ServiceResponse) -> Result> { - let new_response = template( + let new_response = template_with_res( res.request(), + HttpResponse::BadRequest(), BadRequestTemplate { settings: settings_from_req(res.request()), err: get_err_str(&res), @@ -53,9 +56,8 @@ pub fn render_400(res: ServiceResponse) -> Result> fn create( res: ServiceResponse, - new_response: HttpResponse, + mut new_response: HttpResponse, ) -> Result> { - let mut new_response = new_response; new_response .headers_mut() .append(header::CACHE_CONTROL, HeaderValue::from_static("no-store")); diff --git a/src/templates.rs b/src/templates.rs index fb9aee8..caec3ef 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -1,12 +1,18 @@ -use actix_web::{HttpRequest, HttpResponse}; +use actix_web::{HttpRequest, HttpResponse, HttpResponseBuilder}; use askama::Template; use crate::settings::SETTINGS_KEY; pub fn template(req: &HttpRequest, t: impl Template) -> HttpResponse { - let mut res = HttpResponse::Ok(); - let res = res // rust moment - .append_header(("Content-Type", "text/html; charset=utf-8")) + template_with_res(req, HttpResponse::Ok(), t) +} + +pub fn template_with_res( + req: &HttpRequest, + mut res: HttpResponseBuilder, + t: impl Template, +) -> HttpResponse { + res.append_header(("Content-Type", "text/html; charset=utf-8")) // Caching Setup // Since Cloudflare ignores Vary headers, we can't publically cache all pages since only // the last-cached theme would be shown to users. Instead, we privately cache all pages in the