use core::ops;
pub mod complete;
#[doc(hidden)]
pub mod decode;
pub mod fixed;
mod range;
use o3::buffer::pool;
use range::{Authority, Source, Stage};
use crate::{
field::{
self, head,
metadata::{
block, lower,
map::record,
packed,
section::{self, rules, storage},
},
},
request::{Target, query::Entries},
uri,
};
#[derive(Debug)]
pub struct RetainedHead<B, S, C = block::Plan> {
fields: B,
pick: S,
control: C,
}
#[repr(transparent)]
pub struct Selected<P: section::Policy> {
value: P::Selection,
}
impl<P: section::Policy> Selected<P> {
pub fn into_inner(self) -> P::Selection {
self.value
}
}
impl<P: section::Policy> AsRef<P::Selection> for Selected<P> {
fn as_ref(&self) -> &P::Selection {
&self.value
}
}
impl<B, S, C> RetainedHead<B, S, C> {
pub fn split(self) -> (RetainedHead<B, (), C>, S) {
(
RetainedHead {
fields: self.fields,
pick: (),
control: self.control,
},
self.pick,
)
}
pub fn fields(&self) -> &B {
&self.fields
}
pub fn control(&self) -> &C {
&self.control
}
pub fn selection(&self) -> &S {
&self.pick
}
pub fn into_parts(self) -> (B, S, C) {
(self.fields, self.pick, self.control)
}
pub fn discard_control(self) -> RetainedHead<B, S, ()> {
self.bind(())
}
fn bind<T>(self, control: T) -> RetainedHead<B, S, T> {
RetainedHead {
fields: self.fields,
pick: self.pick,
control,
}
}
}
impl<const PREFIX: usize, S: rules::RawSelection> field::Source
for RetainedHead<
packed::Decoded<pool::Span, PREFIX>,
Selected<rules::RetainRaw<PREFIX, S>>,
block::Plan,
>
{
fn emit<'field, Out: field::Sink<'field>>(
&'field self,
mut sink: Out,
) -> Result<Out, Out::Error> {
for field in self.fields.iter() {
sink = sink.entry(field::Entry::from_record(field))?;
}
Ok(sink)
}
}
#[doc(hidden)]
pub struct Decoder<P: section::Policy, C: block::Initial = block::Plan> {
core: Core<C>,
policy: P,
}
struct Core<C: block::Initial> {
control: block::Gate<C>,
authority: Authority,
}
impl<P: section::Policy, C: block::Initial> rules::Validate for Decoder<P, C> {
fn value(&mut self, check: block::Validation) {
self.core.value(check);
}
}
impl<C: block::Initial> Core<C> {
fn value(&mut self, check: block::Validation) {
match check {
block::Validation::Valid => {}
block::Validation::Malformed => self.control.reject(),
block::Validation::RejectPush => self.control.reject_push(),
}
}
fn new(kind: block::Kind, connect: block::Connect, version: block::Version) -> Self {
Self {
control: block::Gate::new(kind, connect, version),
authority: Authority::absent(),
}
}
const fn is_checked(known: head::Name) -> bool {
matches!(
known,
head::Name::Method
| head::Name::Status
| head::Name::Scheme
| head::Name::Path
| head::Name::Authority
| head::Name::Protocol
| head::Name::Host
| head::Name::Te
| head::Name::ContentLength
)
}
fn is_te_list(value: &record::Span<'_>) -> bool {
let mut matched = 0;
for &byte in value.iter() {
match byte {
b',' if matched == 0 || matched == b"trailers".len() => matched = 0,
b' ' | b'\t' if matched == 0 || matched == b"trailers".len() => {}
_ if b"trailers"
.get(matched)
.is_some_and(|expected| expected.eq_ignore_ascii_case(&byte)) =>
{
matched += 1;
}
_ => return false,
}
}
matched == 0 || matched == b"trailers".len()
}
fn check_host(
&mut self,
value: record::Span<'_>,
retained: &[u8],
scratch: &[u8],
) -> block::Validation {
let incoming = match uri::Parsed::parse(value) {
Ok(value) => value,
Err(_) => return block::Validation::Malformed,
};
if !self.control.is_request() {
return block::Validation::Valid;
}
let (saved, owner) = match self.authority.source() {
Source::Absent => return block::Validation::Valid,
Source::Retained(saved) => (saved, retained),
Source::Staged(saved) => (saved, scratch),
};
let Some(value) = owner.get(saved.range()) else {
return block::Validation::Malformed;
};
let saved = match uri::Authority::parse(value) {
Ok(saved) => saved,
Err(_) => return block::Validation::Malformed,
};
if saved.spanned().is_same(incoming, self.control.scheme()) {
block::Validation::Valid
} else {
block::Validation::Malformed
}
}
fn check<A: rules::Push>(
&mut self,
known: head::Name,
field: record::View<'_>,
retained: &[u8],
scratch: &[u8],
) -> rules::Retention {
if self.control.validation() != block::Validation::Valid {
return rules::Retention::Discard;
}
let push = self.control.kind() == block::Kind::PushPromise;
let mut status = if known == head::Name::Host {
self.check_host(field.value, retained, scratch)
} else {
self.control.accept_value(known, field.value)
};
if status == block::Validation::Valid {
status = match known {
head::Name::Scheme if push && !A::is_scheme(field.value) => {
block::Validation::RejectPush
}
head::Name::Authority if push && !A::is_authority(field.value) => {
block::Validation::RejectPush
}
head::Name::ContentLength
if push && self.control.framing().content_length() != Some(0) =>
{
block::Validation::RejectPush
}
head::Name::Te
if !field.value.is_equal(b"trailers") && !Self::is_te_list(&field.value) =>
{
block::Validation::Malformed
}
head::Name::Invalid
| head::Name::Method
| head::Name::Scheme
| head::Name::Path
| head::Name::Status
| head::Name::Authority
| head::Name::Protocol
| head::Name::Host
| head::Name::Connection
| head::Name::ContentLength
| head::Name::TransferEncoding
| head::Name::Expect
| head::Name::AcceptEncoding
| head::Name::Te
| head::Name::KeepAlive
| head::Name::ProxyConnection
| head::Name::Upgrade
| head::Name::Regular => block::Validation::Valid,
};
}
self.value(status);
match status {
block::Validation::Valid => rules::Retention::Retain,
block::Validation::Malformed | block::Validation::RejectPush => {
rules::Retention::Discard
}
}
}
}
impl<P: rules::Plan, C: block::Initial> Decoder<P, C> {
pub(super) fn finish_block(
mut self,
mut writer: storage::Writer<P::Block>,
scratch: &mut storage::Scratch,
) -> Result<decode::Final<P, C>, lower::Error> {
let outcome = 'seal: {
match self.core.authority.stage() {
Stage::Absent | Stage::Retained => {}
Stage::Staged(saved) => {
let Some(value) = scratch.authority().get(saved.range()) else {
self.core.control.reject();
scratch.clear();
break 'seal block::Outcome::Rejected(self.core.control.validation());
};
let mut outcome = C::finish(&self.core.control);
if matches!(outcome, block::Outcome::Initial(control) if control.is_connect()) {
let prefix = storage::Tag::AUTHORITY
.prefix_with(value.len(), scratch.indexing)
.ok_or(lower::Error::Prefix(storage::Tag::AUTHORITY, value.len()))?;
let start = writer.lease.len();
let append = pool::Append::try_prefixed(&mut writer.lease, prefix, [value])
.map_err(lower::Error::Write)?;
let value_at = start + storage::Tag::PREFIX_LEN;
append.commit();
let value = value_at..writer.lease.len();
let status = P::commit(rules::Commit::new(
&mut self.policy,
storage::Tag::AUTHORITY,
value.clone(),
&writer.lease,
));
self.core.value(status);
match Authority::retained(value) {
Some(saved) => self.core.authority = saved,
None => self.core.control.reject(),
}
let check = self.core.control.validation();
if check != block::Validation::Valid {
outcome = block::Outcome::Rejected(check);
}
} else {
self.core.authority = Authority::absent();
}
scratch.clear();
break 'seal outcome;
}
}
C::finish(&self.core.control)
};
Ok(decode::Final::new(
writer.finish(),
self.policy.finish(),
self.core.control.validation(),
outcome,
self.core.control.framing(),
))
}
pub(in crate::field::metadata) fn decide(
&mut self,
name: record::Span<'_>,
proof: block::Name,
) -> rules::Decision<P::Inspect> {
let known = proof.head();
let check = self.core.control.accept_name(known);
self.decision(name, known, check)
}
fn decision(
&mut self,
name: record::Span<'_>,
known: head::Name,
check: block::Validation,
) -> rules::Decision<P::Inspect> {
if check != block::Validation::Valid {
return rules::Decision::Skip;
}
let action = P::disposition(rules::Candidate::new(&mut self.policy, name, known));
match action {
rules::Disposition::Discard
if self.core.control.is_request() && known == head::Name::Authority =>
{
rules::Decision::Scratch
}
rules::Disposition::Discard if P::SKIP_DISCARDED && !Core::<C>::is_checked(known) => {
rules::Decision::Skip
}
rules::Disposition::Discard => rules::Decision::Discard(known),
rules::Disposition::Inspect(token) => rules::Decision::Inspect(token, known),
rules::Disposition::Tagged(tag) => rules::Decision::Tagged(known, tag),
}
}
pub(in crate::field::metadata) fn inspect<A: rules::Push>(
&mut self,
token: P::Inspect,
known: head::Name,
field: record::View<'_>,
retained: &pool::Span,
scratch: &[u8],
) {
if self
.core
.check::<A>(known, field, retained.as_slice(), scratch)
== rules::Retention::Retain
{
let status = P::inspect(
token,
rules::Observation::new(&mut self.policy, field, retained),
);
self.core.value(status);
}
}
pub(in crate::field::metadata) fn retain<A: rules::Push>(
&mut self,
known: head::Name,
field: record::View<'_>,
retained: &[u8],
scratch: &[u8],
) -> rules::Retention {
self.core.check::<A>(known, field, retained, scratch)
}
pub(in crate::field::metadata) fn path(
&mut self,
value: record::Span<'_>,
start: usize,
) -> rules::Retention {
if self.core.control.validation() != block::Validation::Valid {
return rules::Retention::Discard;
}
let Some(end) = self.core.control.accept_path(value) else {
return rules::Retention::Discard;
};
if let Some(bytes) = value.as_slice()
&& Target::check_start(start).is_ok()
{
return self.policy.path(&Entries::decoded(bytes, start), end);
}
rules::Retention::Retain
}
pub(in crate::field::metadata) fn store(
&mut self,
known: head::Name,
store: rules::Store,
range: ops::Range<usize>,
retained: &pool::Span,
) {
match store {
rules::Store::Scratch => {
if known == head::Name::Authority && self.core.control.is_request() {
match Authority::staged(range) {
Some(saved) => self.core.authority = saved,
None => self.core.control.reject(),
}
}
}
rules::Store::Tagged(tag) => {
if known == head::Name::Authority && self.core.control.is_request() {
match Authority::retained(range.clone()) {
Some(saved) => self.core.authority = saved,
None => self.core.control.reject(),
}
}
let status = P::commit(rules::Commit::new(&mut self.policy, tag, range, retained));
self.core.value(status);
}
}
}
}
const _: () = {
assert!(core::mem::size_of::<Core<block::Plan>>() == 24);
assert!(core::mem::size_of::<Core<block::Response>>() == 24);
};