use o3::buffer::{
resident::bound::{AtMost, Unbounded},
segments, storage,
write::{Sink, consume},
};
use std::{borrow::Cow, convert::Infallible, marker::PhantomData};
use crate::{
field,
payload::{self, fixed},
};
use headers::{Application, List, Name};
use status::Code;
mod body;
pub mod bounded;
pub mod declared;
pub mod dispatch;
pub mod encoded;
pub mod exact;
pub mod headers;
pub mod status;
pub use body::Body;
pub trait Destination<'place, 'd, S, Ranges: Copy + 'd = ()> {
type Place;
type Stored;
fn empty(place: Self::Place) -> Self::Stored;
fn owned(place: Self::Place, body: storage::NonEmpty<Vec<u8>>) -> Self::Stored;
fn allocated(place: Self::Place, body: storage::NonEmpty<storage::Owned>) -> Self::Stored;
fn shared<const COPIED: bool>(
place: Self::Place,
body: storage::NonEmpty<storage::Shared>,
) -> Self::Stored;
fn retained<const INPUT: bool>(
place: Self::Place,
body: segments::raw::Data<'d, (), Ranges>,
) -> Self::Stored;
fn fixed<const DEFERRED: bool>(
place: Self::Place,
body: storage::NonEmpty<&'d [u8]>,
) -> Self::Stored;
}
pub struct Output<H, B, F = Fixed> {
pub status: Code,
pub fields: H,
pub body: B,
form: PhantomData<fn() -> F>,
}
impl<H: Copy> Copy for Output<H, fixed::Body<'_>, Static> {}
impl<H: Copy> Clone for Output<H, fixed::Body<'_>, Static> {
fn clone(&self) -> Self {
*self
}
}
impl<H, B, F> Output<H, B, F> {
pub fn new(status: Code, fields: H, body: B) -> Self {
Self {
status,
fields,
body,
form: PhantomData,
}
}
pub fn status(&self) -> Code {
self.status
}
pub fn body(&self) -> &B {
&self.body
}
}
pub enum Fixed {}
pub enum Static {}
pub struct Encoded<const EXACT: bool>;
impl<'req, 'd, Ranges: Copy + 'd, D, H: field::Source<Application>, B: Body<'d, Ranges>>
dispatch::Emit<'req, 'd, D, Ranges> for Output<H, B, Fixed>
{
type Kind = dispatch::Sync;
type Stream = dispatch::Never;
type Bound = Unbounded;
const BODY: payload::Kind = payload::Kind::Inline;
fn consume<W: dispatch::Transaction<'req, 'd, dispatch::Never, Ranges>>(
self,
writer: W,
) -> W::Done {
writer.fixed::<Code, _, _>(self.status, self.fields, self.body)
}
}
impl<'body: 'd, 'req, 'd, Ranges: Copy + 'd, D, H: field::Source<Application>>
dispatch::Emit<'req, 'd, D, Ranges> for Output<H, fixed::Body<'body>, Static>
{
type Kind = dispatch::Sync;
type Stream = dispatch::Never;
type Bound = AtMost<0>;
const BODY: payload::Kind = payload::Kind::Static;
fn consume<W: dispatch::Transaction<'req, 'd, dispatch::Never, Ranges>>(
self,
writer: W,
) -> W::Done {
writer.statics::<Code, _>(self.status, self.fields, self.body)
}
}
impl<'body: 'd, 'req, 'd, Ranges: Copy + 'd, D, H: field::Source<Application>, const N: usize>
dispatch::Emit<'req, 'd, D, Ranges> for Output<H, &'body [u8; N], Static>
{
type Kind = dispatch::Sync;
type Stream = dispatch::Never;
type Bound = AtMost<0>;
const BODY: payload::Kind = payload::Kind::Static;
fn consume<W: dispatch::Transaction<'req, 'd, dispatch::Never, Ranges>>(
self,
writer: W,
) -> W::Done {
writer.sized_static::<Code, _, N>(self.status, self.fields, self.body)
}
}
impl<
'req,
'd,
Ranges: Copy + 'd,
D,
H: field::Source<Application>,
B: encoded::Source<EXACT>,
const EXACT: bool,
> dispatch::Emit<'req, 'd, D, Ranges> for Output<H, B, Encoded<EXACT>>
{
type Kind = dispatch::Sync;
type Stream = dispatch::Never;
type Bound = Unbounded;
const BODY: payload::Kind = payload::Kind::Inline;
fn consume<W: dispatch::Transaction<'req, 'd, dispatch::Never, Ranges>>(
self,
writer: W,
) -> W::Done {
writer.encoded::<EXACT, Code>(self.status, self.fields, self.body)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum Method {
Other,
Head,
Connect,
}
impl Method {
pub fn from_bytes(method: &[u8]) -> Self {
match method.len() {
4 if method == b"HEAD" => Self::Head,
7 if method == b"CONNECT" => Self::Connect,
0..=3 | 5..=6 | 8.. => Self::Other,
4 | 7 => Self::Other,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Content {
Forbidden,
Suppressed,
Tunnel,
Empty,
Allowed,
}
impl Content {
pub const fn new(method: Method, status: u16) -> Self {
if matches!(method, Method::Connect) && status >= 200 && status < 300 {
Self::Tunnel
} else if status < 200 || status == 204 {
Self::Forbidden
} else if matches!(method, Method::Head) || status == 304 {
Self::Suppressed
} else if status == 205 {
Self::Empty
} else {
Self::Allowed
}
}
pub fn length<S: dispatch::Source<Kind>, Kind>(
self,
status: Code,
source: Option<&S>,
) -> Option<u64> {
if S::remaining(&mut S::Count::default()).is_some()
&& (self == Self::Allowed || self == Self::Suppressed && status != Code::RESET_CONTENT)
{
Some(source.map_or(0, S::length))
} else {
None
}
}
}
pub trait Encode: Sized {
const MAX_LEN: usize = usize::MAX;
fn encode<W>(self, out: &mut W) -> Result<(), consume::Error>
where
W: Sink<Error = consume::Error>;
}
impl Encode for Infallible {
fn encode<W: Sink<Error = consume::Error>>(self, _: &mut W) -> Result<(), consume::Error> {
match self {}
}
}
pub struct Response<'field, B = [u8; 0], T = (), S = Code, N = Name<Cow<'field, str>>> {
pub(crate) status: S,
pub(crate) headers: List<'field, N>,
pub(crate) trailers: T,
pub(crate) body: B,
}
impl<'field, B, T, S, N> Response<'field, B, T, S, N> {
pub fn status(&self) -> S
where
S: Copy,
{
self.status
}
pub fn headers(&self) -> &List<'field, N> {
&self.headers
}
pub fn body(&self) -> &B {
&self.body
}
pub fn body_mut(&mut self) -> &mut B {
&mut self.body
}
pub fn into_body(self) -> B {
self.body
}
pub fn with_body<U>(self, body: U) -> Response<'field, U, T, S, N> {
Response {
status: self.status,
headers: self.headers,
trailers: self.trailers,
body,
}
}
}
impl<'field, B, S, N> Response<'field, B, List<'field>, S, N> {
pub fn trailers(&self) -> &List<'field> {
&self.trailers
}
}
impl<'field> Response<'field, [u8; 0]> {
pub fn new(status: Code) -> Self {
Self {
status,
headers: List::new(),
trailers: (),
body: [],
}
}
}
impl<'field, 'req, 'd, Ranges: Copy + 'd, D, B> dispatch::Emit<'req, 'd, D, Ranges>
for Response<'field, B, (), Code>
where
B: Body<'d, Ranges>,
{
type Kind = dispatch::Sync;
type Stream = dispatch::Never;
type Bound = Unbounded;
const BODY: payload::Kind = payload::Kind::Inline;
fn consume<W: dispatch::Transaction<'req, 'd, dispatch::Never, Ranges>>(
self,
writer: W,
) -> W::Done {
let Self {
status,
headers,
trailers: (),
body,
} = self;
writer.fixed::<Code, _, _>(status, headers, body)
}
}
const _: () = assert!(
core::mem::size_of::<Response<'static, [u8; 0]>>()
< core::mem::size_of::<Response<'static, [u8; 0], List<'static>, status::Code>>()
);