use std::io::{self, Write};
const HEADERS: &str = "Cache-Control: no-store\r\n\
Content-Security-Policy: default-src 'none'; style-src 'self'; base-uri 'none'; \
form-action 'none'; frame-ancestors 'none'\r\n\
X-Content-Type-Options: nosniff\r\n\
Referrer-Policy: no-referrer\r\n\
X-Frame-Options: DENY\r\n\
Cross-Origin-Opener-Policy: same-origin\r\n\
Cross-Origin-Resource-Policy: same-origin\r\n\
Connection: close\r\n";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Response {
pub status: u16,
pub content_type: &'static str,
pub body: Vec<u8>,
extra: Option<&'static str>,
}
impl Response {
pub fn new(status: u16, content_type: &'static str, body: impl Into<Vec<u8>>) -> Self {
let extra = match status {
401 => Some("WWW-Authenticate: Bearer\r\n"),
405 => Some("Allow: GET\r\n"),
_ => None,
};
Self {
status,
content_type,
body: body.into(),
extra,
}
}
pub fn text(status: u16, text: &str) -> Self {
Self::new(status, "text/plain; charset=utf-8", format!("{text}\n"))
}
pub fn unauthorized() -> Self {
Self::text(
401,
"this needs the token from the link `openmoat serve` printed",
)
}
pub fn write_to(&self, out: &mut impl Write) -> io::Result<()> {
write!(
out,
"HTTP/1.1 {} {}\r\nContent-Type: {}\r\nContent-Length: {}\r\n{HEADERS}{}\r\n",
self.status,
reason(self.status),
self.content_type,
self.body.len(),
self.extra.unwrap_or_default(),
)?;
out.write_all(&self.body)?;
out.flush()
}
}
fn reason(status: u16) -> &'static str {
match status {
200 => "OK",
400 => "Bad Request",
401 => "Unauthorized",
403 => "Forbidden",
404 => "Not Found",
405 => "Method Not Allowed",
408 => "Request Timeout",
431 => "Request Header Fields Too Large",
_ => "Internal Server Error",
}
}