From 78cd45d68a873232c1ef3937cbfdfe192c2443cb Mon Sep 17 00:00:00 2001 From: xenofem Date: Thu, 16 Jun 2022 19:48:10 -0400 Subject: [PATCH] use askama_actix responders so we get the correct content types/other headers for free --- Cargo.lock | 12 +++++++++++ Cargo.toml | 1 + src/main.rs | 58 ++++++++++++++++++++++------------------------------- 3 files changed, 37 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 451fec0..7fd809b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -320,6 +320,17 @@ dependencies = [ "askama_shared", ] +[[package]] +name = "askama_actix" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c52f74f8382a142ecfc052100b21abc33f2c069e20fe345808e7ed914b179449" +dependencies = [ + "actix-web", + "askama", + "askama_shared", +] + [[package]] name = "askama_derive" version = "0.11.2" @@ -1582,6 +1593,7 @@ dependencies = [ "actix-web", "actix-web-actors", "askama", + "askama_actix", "bytes", "bytesize", "crc32fast", diff --git a/Cargo.toml b/Cargo.toml index 6d1940b..5c71047 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ actix-http = "3.0.4" actix-web = "4.0.1" actix-web-actors = "4.1.0" askama = "0.11.1" +askama_actix = "0.13" bytes = "1.1.0" bytesize = "1.1.0" crc32fast = "1.3.2" diff --git a/src/main.rs b/src/main.rs index 97b4876..cf591ab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,12 +6,13 @@ mod zip; use std::{fmt::Debug, path::PathBuf, str::FromStr}; +use actix_http::StatusCode; use actix_web::{ error::InternalError, get, middleware::Logger, post, web, App, HttpRequest, HttpResponse, HttpServer, Responder, }; use actix_web_actors::ws; -use askama::Template; +use askama_actix::{Template, TemplateToResponse}; use bytesize::ByteSize; use log::{error, warn}; use serde::{Deserialize, Serialize}; @@ -55,19 +56,15 @@ pub fn log_auth_failure(ip_addr: &str) { #[derive(Template)] #[template(path = "index.html")] -struct IndexPage<'a> { - cachebuster: &'a str, +struct IndexPage { + cachebuster: String, } #[get("/")] async fn index(data: web::Data) -> impl Responder { - HttpResponse::Ok().body( - IndexPage { - cachebuster: &data.config.cachebuster, - } - .render() - .unwrap(), - ) + IndexPage { + cachebuster: data.config.cachebuster.clone(), + } } #[derive(Deserialize)] @@ -129,19 +126,16 @@ async fn handle_download( .into_response(&req)) } else { let offsets = info.contents.as_deref().map(zip::file_data_offsets); - Ok(HttpResponse::Ok().body( - DownloadPage { - info: DownloadInfo { - file: info, - code: code.clone(), - available: file.metadata().await?.len(), - offsets, - }, - cachebuster: &data.config.cachebuster, - } - .render() - .unwrap(), - )) + Ok(DownloadPage { + info: DownloadInfo { + file: info, + code: code.clone(), + available: file.metadata().await?.len(), + offsets, + }, + cachebuster: &data.config.cachebuster, + } + .to_response()) } } @@ -188,17 +182,13 @@ fn not_found(req: HttpRequest, data: web::Data, report: bool) -> ac let ip_addr = get_ip_addr(&req, data.config.reverse_proxy); log_auth_failure(&ip_addr); } - Err(InternalError::from_response( - "Download not found", - HttpResponse::NotFound().body( - NotFoundPage { - cachebuster: &data.config.cachebuster, - } - .render() - .unwrap(), - ), - ) - .into()) + let mut resp = NotFoundPage { + cachebuster: &data.config.cachebuster, + } + .to_response(); + *resp.status_mut() = StatusCode::NOT_FOUND; + + Err(InternalError::from_response("Download not found", resp).into()) } #[get("/upload")]