hotaru_http 0.8.4

HTTP/1.1 implementation for the Hotaru web framework
Documentation
use std::fmt::Write;

use hotaru_core::connection::{HotaruBufRead, HotaruWrite};
use hotaru_core::connection::error::ConnectionError;

use crate::message::body::HttpBody;
use crate::message::meta::HttpMeta;
use crate::security::safety::HttpSafety;

pub async fn parse_lazy<R: HotaruBufRead<Error = std::io::Error> + Unpin + Send>(
    stream: &mut R,
    config: &HttpSafety,
    is_request: bool,
    print_raw: bool,
) -> Result<(HttpMeta, HttpBody), ConnectionError> {
    // Create one BufReader up-front, pass this throughout.
    let mut meta = HttpMeta::from_stream(stream, config, print_raw, is_request).await?;

    let body = HttpBody::read_buffer(stream, &mut meta, config).await?;

    Ok((meta, body))
}

pub async fn send<W: HotaruWrite<Error = std::io::Error> + Unpin + Send>(
    mut meta: HttpMeta,
    body: HttpBody,
    writer: &mut W,
) -> std::io::Result<()> {
    let mut headers = String::with_capacity(256);

    // Add the values such as content length into header
    let bin = body.into_static(&mut meta).await;
    write!(&mut headers, "{}", meta.represent())
        .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;

    writer.write_all(headers.as_bytes()).await?;
    writer.write_all(&bin).await?;

    // println!("{:?}, {:?}", headers, bin);
    writer.flush().await?;

    Ok(())
}