// This file is part of Germ . // Copyright (C) 2022-2022 Fuwn // // 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 . // // Copyright (C) 2022-2022 Fuwn // SPDX-License-Identifier: GPL-3.0-only use { crate::request::Status, rustls::SupportedCipherSuite, std::{borrow::Cow, fmt::Write}, }; #[derive(Debug, Clone, PartialEq)] pub struct Response { status: Status, meta: String, content: Option, size: usize, suite: Option, } impl Response { pub(crate) fn new(data: &[u8], suite: Option) -> Self { let string_form = String::from_utf8_lossy(data).to_string(); let mut content = None; let header; if string_form.ends_with("\r\n") { header = string_form; } else { let mut string_split = string_form.split("\r\n"); header = string_split.next().unwrap_or("").to_string(); content = Some(string_split.fold(String::new(), |mut output, s| { let _ = write!(output, "{s}\r\n"); output })); } let header_split = header.split_at(2); Self { status: Status::from(header_split.0.parse::().unwrap_or(0)), meta: header_split.1.trim_start().to_string(), content, size: data.len(), suite, } } #[must_use] pub const fn status(&self) -> &Status { &self.status } #[allow(clippy::missing_const_for_fn)] #[must_use] pub fn meta(&self) -> Cow<'_, str> { Cow::Borrowed(&self.meta) } #[must_use] pub const fn content(&self) -> &Option { &self.content } #[must_use] pub const fn size(&self) -> &usize { &self.size } #[must_use] pub const fn suite(&self) -> &Option { &self.suite } }