aboutsummaryrefslogtreecommitdiff
path: root/src/response.rs
blob: 4ab513e400f5d474bbbc561ef2a80a8f310e26d6 (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
// This file is part of Windmark <https://github.com/gemrest/windmark>.
// Copyright (C) 2022-2022 Fuwn <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only

//! Content and response handlers

macro_rules! response {
  ($name:ident, $status:expr) => {
    pub fn $name<S>(content: S) -> Self
    where S: AsRef<str> {
      Self::new($status, content.as_ref())
    }
  };
}

/// The content and response type a handler should reply with.
#[derive(Clone)]
pub struct Response {
  pub status:        i32,
  pub mime:          Option<String>,
  pub content:       String,
  pub character_set: Option<String>,
  pub language:      Option<String>,
}

impl Response {
  response!(input, 10);

  response!(sensitive_input, 11);

  response!(temporary_redirect, 30);

  response!(permanent_redirect, 31);

  response!(temporary_failure, 40);

  response!(server_unavailable, 41);

  response!(cgi_error, 42);

  response!(proxy_error, 43);

  response!(slow_down, 44);

  response!(permanent_failure, 50);

  response!(not_found, 51);

  response!(gone, 52);

  response!(proxy_refused, 53);

  response!(bad_request, 59);

  response!(client_certificate_required, 60);

  response!(certificate_not_authorised, 61);

  response!(certificate_not_valid, 62);

  pub fn success<S>(content: S) -> Self
  where S: AsRef<str> {
    Self::new(20, content.as_ref())
      .with_mime("text/gemini")
      .with_language("en")
      .with_character_set("utf-8")
      .clone()
  }

  #[must_use]
  pub fn binary_success(content: &[u8], mime: &str) -> Self {
    Self::new(21, &String::from_utf8_lossy(content))
      .with_mime(mime)
      .clone()
  }

  #[cfg(feature = "auto-deduce-mime")]
  #[must_use]
  pub fn binary_success_auto(content: &[u8]) -> Self {
    Self::new(22, &String::from_utf8_lossy(content))
      .with_mime(&tree_magic::from_u8(&*content))
      .clone()
  }

  #[must_use]
  pub fn new(status: i32, content: &str) -> Self {
    Self {
      status,
      mime: None,
      content: content.to_string(),
      character_set: None,
      language: None,
    }
  }

  pub fn with_mime(&mut self, mime: &str) -> &mut Self {
    self.mime = Some(mime.to_string());

    self
  }

  pub fn with_character_set(&mut self, character_set: &str) -> &mut Self {
    self.character_set = Some(character_set.to_string());

    self
  }

  pub fn with_language(&mut self, language: &str) -> &mut Self {
    self.language = Some(language.to_string());

    self
  }
}