sark-http 0.39.0

HTTP wire types, semantics, codecs, and ownership invariants for Sark
Documentation
use std::ops::ControlFlow;

use o3::{
    buffer::{self, bytes::window::Snapshot, storage, write},
    mem::Rejected,
};

use crate::{
    h1::{
        head::line::{Request, Version, error},
        target,
    },
    method,
    request::span,
};

#[derive(Clone, Copy)]
pub struct Parts {
    pub(super) target: span::Span,
    pub(super) plan: target::Target,
    pub(super) version: Version,
    pub(super) matched: Option<method::Known>,
}

impl Parts {
    pub const fn target_span(&self) -> span::Span {
        self.target
    }

    pub const fn plan(&self) -> target::Target {
        self.plan
    }

    pub const fn version(&self) -> Version {
        self.version
    }

    pub const fn matched(&self) -> Option<method::Known> {
        self.matched
    }

    pub const fn end(&self) -> usize {
        self.target.end() + 11
    }
}

/// An append-only input retaining the exact request line already admitted.
pub struct Owned<const MAX: usize> {
    pub(super) source: Snapshot<MAX>,
    pub(super) parts: Parts,
}

impl<const MAX: usize> Owned<MAX> {
    /// Parses once and binds the admitted line to this unchanged input.
    pub fn try_parse(
        source: Snapshot<MAX>,
        methods: u8,
    ) -> Result<ControlFlow<Snapshot<MAX>, Self>, Rejected<Snapshot<MAX>, error::Request>> {
        let parts = match Request::parse_with(source.as_ref(), methods) {
            Ok(Some(request)) => request.parts,
            Ok(None) => return Ok(ControlFlow::Break(source)),
            Err(error) => return Err(Rejected::new(source, error)),
        };
        Ok(ControlFlow::Continue(Self { source, parts }))
    }

    /// Borrows the admitted line from its exact retained input.
    pub fn request(&self) -> Request<'_, &Parts> {
        Request {
            source: self.source.as_ref(),
            parts: &self.parts,
        }
    }

    pub(in crate::h1::head) fn into_source(self) -> Snapshot<MAX> {
        self.source
    }

    /// Borrows the same source as the admitted line.
    pub fn view(&self) -> buffer::View<'_, Snapshot<MAX>> {
        buffer::View::new(&self.source)
    }
}

impl<const MAX: usize> AsRef<[u8]> for Owned<MAX> {
    fn as_ref(&self) -> &[u8] {
        self.source.as_ref()
    }
}

impl<const MAX: usize> write::Sink for Owned<MAX> {
    type Error = storage::AllocError;

    fn write_slice(&mut self, bytes: &[u8]) -> Result<(), Self::Error> {
        self.source.write_slice(bytes)
    }

    fn write_slices<const N: usize>(&mut self, slices: [&[u8]; N]) -> Result<(), Self::Error> {
        self.source.write_slices(slices)
    }
}

// SAFETY: the private Snapshot retains the exact initialized source; shared access
// preserves its prefix, and every write::Sink operation only appends.
unsafe impl<const MAX: usize> buffer::raw::Source for Owned<MAX> {}

impl<const MAX: usize> std::borrow::Borrow<Parts> for Owned<MAX> {
    fn borrow(&self) -> &Parts {
        &self.parts
    }
}

impl<const MAX: usize> From<Owned<MAX>> for Parts {
    fn from(owner: Owned<MAX>) -> Self {
        let Owned { source, parts } = owner;
        drop(source);
        parts
    }
}

impl<'a, 'b> From<(&'a Request<'b>, std::num::NonZero<usize>)> for Parts {
    fn from((line, _): (&'a Request<'b>, std::num::NonZero<usize>)) -> Self {
        *line.metadata()
    }
}

impl<'a, 'b, const MAX: usize> TryFrom<(&'a Request<'b>, std::num::NonZero<usize>)>
    for Owned<MAX>
{
    type Error = storage::AllocError;

    fn try_from(
        (line, capacity): (&'a Request<'b>, std::num::NonZero<usize>),
    ) -> Result<Self, Self::Error> {
        line.try_line_snapshot(capacity)
    }
}