sark-http 0.40.0

HTTP wire types, semantics, codecs, and ownership invariants for Sark
Documentation
use crate::field::{
    head::Name,
    metadata::block::{Initial, Kind, Validation},
};

pub(super) const METHOD: u8 = 1;
pub(super) const SCHEME: u8 = 1 << 1;
pub(super) const PATH: u8 = 1 << 2;
pub(super) const STATUS: u8 = 1 << 3;
pub(super) const AUTHORITY: u8 = 1 << 4;
pub(super) const HOST: u8 = 1 << 5;
pub(super) const PROTOCOL: u8 = 1 << 6;
const REGULAR: u8 = 1 << 7;

#[derive(Clone, Copy, PartialEq, Eq)]
pub(in crate::field::metadata) struct Names {
    pub(super) seen: u8,
    pub(in crate::field::metadata) check: Validation,
}

impl Names {
    pub(super) const EMPTY: Self = Self {
        seen: 0,
        check: Validation::Valid,
    };

    pub(super) fn with<C: Initial>(mut self, kind: Kind, name: Name) -> Self {
        if self.check != Validation::Valid {
            return self;
        }
        let request = C::REQUEST && kind == Kind::Initial;
        let target = request || kind == Kind::PushPromise;
        let bit = match name {
            Name::Regular => REGULAR,
            Name::ContentLength if kind != Kind::Trailers => REGULAR,
            Name::Te if target => REGULAR,
            Name::Host => HOST | REGULAR,
            Name::Method if target => METHOD,
            Name::Scheme if target => SCHEME,
            Name::Path if target => PATH,
            Name::Authority if target => AUTHORITY,
            Name::Protocol if request => PROTOCOL,
            Name::Status if !C::REQUEST && kind == Kind::Initial => STATUS,
            _ => 0,
        };
        if bit == 0
            || bit & REGULAR == 0 && self.seen & REGULAR != 0
            || self.seen & bit & !REGULAR != 0
        {
            self.check = Validation::Malformed;
        } else {
            self.seen |= bit;
        }
        self
    }
}

const _: () = assert!(size_of::<Names>() == 2);