about summary refs log tree commit diff stats
path: root/src/web/responses.rs
blob: 00af35a035639a17c542678425c3f6b17e8bab5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Back - An extremely simple git bug visualization system. Inspired by TVL's
// panettone.
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// 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 <https://www.gnu.org/licenses/agpl.txt>.

use std::convert::Infallible;

use bytes::Bytes;
use git_bug::replica::entity::id::prefix::IdPrefix;
use http::{Response, StatusCode, Version};
use http_body_util::{BodyExt, Full, combinators::BoxBody};
use vy::{IntoHtml, h1, p, pre};

use super::generate::templates::make_page;
use crate::error;

pub(super) fn html_response<T: Into<Bytes>>(html_text: T) -> Response<BoxBody<Bytes, Infallible>> {
    html_response_status(html_text, StatusCode::OK)
}

pub(super) fn html_response_status<T: Into<Bytes>>(
    html_text: T,
    status: StatusCode,
) -> Response<BoxBody<Bytes, Infallible>> {
    html_response_status_content_type(html_text, status, "text/html")
}

pub(super) fn redirect(target: &str) -> Response<BoxBody<Bytes, Infallible>> {
    Response::builder()
        .status(StatusCode::MOVED_PERMANENTLY)
        .version(Version::HTTP_2)
        .header("Location", target)
        .body(full(""))
        .expect("This is hardcoded and will build")
}

pub(super) fn html_response_status_content_type<T: Into<Bytes>>(
    html_text: T,
    status: StatusCode,
    content_type: &str,
) -> Response<BoxBody<Bytes, Infallible>> {
    Response::builder()
        .status(status)
        .version(Version::HTTP_2)
        .header("Content-Type", format!("{content_type}; charset=utf-8"))
        .header("x-content-type-options", "nosniff")
        .header("x-frame-options", "SAMEORIGIN")
        .body(full(html_text))
        .expect("This will always build")
}

pub(super) fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, Infallible> {
    Full::new(chunk.into()).boxed()
}

impl error::Error {
    #[allow(clippy::too_many_lines)]
    pub fn into_response(self) -> Response<BoxBody<Bytes, Infallible>> {
        match self {
            error::Error::ConfigParse { .. }
            | error::Error::ProjectListRead { .. }
            | error::Error::ConfigRead { .. }
            | error::Error::RepoGetReferences(_)
            | error::Error::RepoIssueRead(_)
            | error::Error::RepoIdentityRead(_)
            | error::Error::RepoFind { .. }
            | error::Error::RepoRefsIter(_)
            | error::Error::RepoRefsPrefixed { .. }
            | error::Error::TcpBind { .. }
            | error::Error::RepoOpen { .. }
            | error::Error::TcpAccept { .. } => html_response_status(
                make_page(
                    (
                        h1!("Internal server error"),
                        pre!(format!("Error {}", self.to_string())),
                    ),
                    "Error page",
                    Some("Error | Back"),
                )
                .into_string(),
                StatusCode::INTERNAL_SERVER_ERROR,
            ),
            error::Error::NotGitBug { path } => html_response_status(
                make_page(
                    (
                        h1!("Expectation Failed"),
                        p!(
                            "Repository '",
                            path.display().to_string(),
                            "' has no git bug data to show."
                        ),
                    ),
                    "Error page",
                    Some("Error | Back"),
                )
                .into_string(),
                StatusCode::EXPECTATION_FAILED,
            ),
            error::Error::IssuesPrefixMissing { prefix, err } => html_response_status(
                make_page(
                    (
                        h1!("Expectation Failed"),
                        p!(
                            "There is no issue associated with the prefix ",
                            "'",
                            prefix.to_string(),
                            "': ",
                            err.to_string()
                        ),
                    ),
                    "Error page",
                    Some("Error | Back"),
                )
                .into_string(),
                StatusCode::EXPECTATION_FAILED,
            ),
            error::Error::IssuesPrefixParse { prefix, error } => html_response_status(
                make_page(
                    (
                        h1!("Expectation Failed"),
                        p!(
                            "The prefix '",
                            prefix,
                            "' cannot be interperted as a prefix",
                        ),
                        p!(error.to_string()),
                        p!(
                            "The prefix is composed of  ",
                            IdPrefix::REQUIRED_LENGTH,
                            " or more hex chars."
                        ),
                    ),
                    "Error page",
                    Some("Error | Back"),
                )
                .into_string(),
                StatusCode::EXPECTATION_FAILED,
            ),
            error::Error::NotFound(path_buf) => html_response_status(
                make_page(
                    (
                        h1!("Expectation Failed"),
                        p!(
                            "The path '",
                            path_buf.display().to_string(),
                            "' was unkonwn",
                        ),
                    ),
                    "Error page",
                    Some("Error | Back"),
                )
                .into_string(),
                StatusCode::EXPECTATION_FAILED,
            ),
            error::Error::InvalidQuery { err, query } => html_response_status(
                make_page(
                    (
                        h1!("Expectation Failed"),
                        p!("The query '", query, "' was invalid",),
                        p!(err.to_string()),
                    ),
                    "Error page",
                    Some("Error | Back"),
                )
                .into_string(),
                StatusCode::EXPECTATION_FAILED,
            ),
        }
    }
}