use crate::errors::{error, nil};
use crate::net::http::body::Body;
use crate::net::http::request::Header;
use crate::types::{byte, int, int64, string};
pub struct Response {
pub Status: string, pub StatusCode: int, pub Proto: string, pub Header: Header,
pub Body: Body,
pub ContentLength: int64,
pub Request: Option<Box<crate::net::http::Request>>,
}
impl Response {
pub(crate) fn empty(code: int) -> Response {
Response {
Status: format!("{} {}", code, crate::net::http::StatusText(code)).into(),
StatusCode: code,
Proto: "HTTP/1.1".into(),
Header: Header::new(),
Body: Body::empty(),
ContentLength: 0,
Request: None,
}
}
}
pub struct ResponseWriter {
pub(crate) header: Header,
pub(crate) status: int,
pub(crate) body: Vec<byte>,
pub(crate) wrote_header: bool,
}
impl ResponseWriter {
pub(crate) fn new() -> Self {
ResponseWriter {
header: Header::new(),
status: 200,
body: Vec::new(),
wrote_header: false,
}
}
#[allow(non_snake_case)]
pub fn Header(&mut self) -> &mut Header {
&mut self.header
}
#[allow(non_snake_case)]
pub fn WriteHeader(&mut self, code: int) {
if !self.wrote_header {
self.status = code;
self.wrote_header = true;
}
}
#[allow(non_snake_case)]
pub fn Write(&mut self, p: &[byte]) -> (int, error) {
if !self.wrote_header { self.wrote_header = true; }
self.body.extend_from_slice(p);
(p.len() as int, nil)
}
}
impl std::io::Write for ResponseWriter {
fn write(&mut self, p: &[u8]) -> std::io::Result<usize> {
if !self.wrote_header { self.wrote_header = true; }
self.body.extend_from_slice(p);
Ok(p.len())
}
fn flush(&mut self) -> std::io::Result<()> { Ok(()) }
}