From 1e4dff1995833538f436b381bc0450a7c0080bad Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Mon, 12 May 2025 12:39:10 +0200 Subject: chore: Initial commit Based on the version that was previously in `vhack.eu/nixos-server/pkgs/by-name/ba/back`. --- src/web/responses.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/web/responses.rs (limited to 'src/web/responses.rs') diff --git a/src/web/responses.rs b/src/web/responses.rs new file mode 100644 index 0000000..bcdcc0a --- /dev/null +++ b/src/web/responses.rs @@ -0,0 +1,61 @@ +// Back - An extremely simple git bug visualization system. Inspired by TVL's +// panettone. +// +// Copyright (C) 2025 Benedikt Peetz +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// This file is part of Back. +// +// You should have received a copy of the License along with this program. +// If not, see . + +use std::convert::Infallible; + +use bytes::Bytes; +use http::{Response, StatusCode, Version}; +use http_body_util::{BodyExt, Full, combinators::BoxBody}; + +use crate::{error, git_bug::format::HtmlString}; + +pub(super) fn html_response>(html_text: T) -> Response> { + html_response_status(html_text, StatusCode::OK) +} + +pub(super) fn html_response_status>( + html_text: T, + status: StatusCode, +) -> Response> { + html_response_status_content_type(html_text, status, "text/html") +} + +pub(super) fn html_response_status_content_type>( + html_text: T, + status: StatusCode, + content_type: &str, +) -> Response> { + Response::builder() + .status(status) + .version(Version::HTTP_2) + .header("Content-Type", format!("{}; charset=utf-8", content_type)) + .header("x-content-type-options", "nosniff") + .header("x-frame-options", "SAMEORIGIN") + .body(full(html_text)) + .expect("This will always build") +} + +fn full>(chunk: T) -> BoxBody { + Full::new(chunk.into()).boxed() +} + +// FIXME: Not all errors should return `INTERNAL_SERVER_ERROR`. <2025-03-08> +impl error::Error { + pub fn into_response(self) -> Response> { + html_response_status( + format!( + "

Internal server error.

Error: {}
", + HtmlString::from(self.to_string()) + ), + StatusCode::INTERNAL_SERVER_ERROR, + ) + } +} -- cgit 1.4.1