mod method;
use core::num::NonZero;
pub use method::*;
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub enum H11TransferEncoding {
#[default]
None,
Chunked,
}
impl H11TransferEncoding {
pub fn as_bytes_out(&self) -> &[u8] {
match self {
Self::None => &[],
Self::Chunked => "chunked".as_bytes(),
}
}
}
#[derive(Debug, Default, PartialEq)]
pub enum H11Version {
#[default]
Unknown,
Http11,
}
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub enum H11Connection {
#[default]
Close,
KeepAlive,
}
impl H11Connection {
pub fn as_bytes_out(&self) -> &[u8] {
match self {
Self::Close => "Close",
Self::KeepAlive => "Keep-Alive",
}
.as_bytes()
}
}
#[derive(Debug, Default, PartialEq)]
pub enum H11TransferCompression {
#[default]
None,
Compress,
Deflate,
Gzip,
Br,
Zstd,
}
#[derive(Debug, PartialEq)]
pub enum RequestBodySize {
Fixed(NonZero<usize>),
Chunked,
Dynamic,
Empty,
}
impl RequestBodySize {
#[inline]
pub const fn is_empty(&self) -> bool {
matches!(self, RequestBodySize::Empty)
}
}
#[derive(Debug, Default, PartialEq)]
pub struct H11RequestMeta {
pub(crate) method: H11Method,
pub(crate) target_loc: Option<(usize, usize)>,
pub(crate) version: H11Version,
pub(crate) transfer_encoding: H11TransferEncoding,
pub(crate) transfer_compression: H11TransferCompression,
pub(crate) body_length: Option<usize>,
pub(crate) headers_end: Option<usize>,
}
impl H11RequestMeta {
#[inline]
pub fn status_complete(&self) -> bool {
self.method != H11Method::Unknown
&& self.target_loc.is_some()
&& self.version != H11Version::Unknown
}
#[inline]
pub fn headers_complete(&self) -> bool {
self.headers_end.is_some()
}
pub fn body_size(&self) -> RequestBodySize {
let l = match self.body_length {
Some(s) => match s {
0 => RequestBodySize::Empty,
_ => RequestBodySize::Fixed(NonZero::new(s).expect("Bug")),
},
None => RequestBodySize::Empty,
};
if let H11TransferEncoding::Chunked = self.transfer_encoding {
return RequestBodySize::Chunked;
}
return l;
}
pub fn method(&self) -> H11Method {
self.method
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum H11Header<'h> {
Connection(H11Connection),
ContentLength(H11MaybeValue<'h>),
SetCookie(H11MaybeValue<'h>),
Host(H11MaybeValue<'h>),
TransferEncoding(H11TransferEncoding),
Unknown(H11UnknownField<'h>, H11MaybeValue<'h>),
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct H11UnknownField<'h>(pub &'h [u8]);
impl<'h> H11UnknownField<'h> {
pub fn as_bytes_out(&self) -> &[u8] {
self.0
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum H11MaybeValue<'h> {
Integer(&'h [u8]),
Bytes(&'h [u8]),
}
impl<'h> H11MaybeValue<'h> {
pub fn as_bytes_out(&self) -> &[u8] {
match self {
Self::Integer(i) => i,
Self::Bytes(b) => b,
}
}
}
use crate::{H11Error, HeaderValidationError};
impl<'h> TryFrom<H11MaybeValue<'h>> for usize {
type Error = H11Error;
fn try_from(from: H11MaybeValue<'h>) -> Result<usize, Self::Error> {
match from {
H11MaybeValue::Integer(i) => {
let s = match core::str::from_utf8(i) {
Ok(s) => s,
Err(_) => {
return Err(H11Error::InvalidHeaderValueNoPos(
HeaderValidationError::ExpectedInteger,
))
}
};
use core::str::FromStr;
match usize::from_str(s) {
Ok(s) => Ok(s),
Err(_) => Err(H11Error::InvalidHeaderValueNoPos(
HeaderValidationError::ExpectedInteger,
)),
}
}
_ => Err(H11Error::InvalidHeaderValueNoPos(
HeaderValidationError::ExpectedInteger,
)),
}
}
}
impl<'h> H11Header<'h> {
pub fn as_bytes_out(&self) -> (&[u8], &[u8]) {
let (field, value) = match self {
Self::Connection(c) => ("Connection".as_bytes(), c.as_bytes_out()),
Self::ContentLength(l) => ("Content-Length".as_bytes(), l.as_bytes_out()),
Self::SetCookie(c) => ("Set-Cookie".as_bytes(), c.as_bytes_out()),
Self::Host(h) => ("Host".as_bytes(), h.as_bytes_out()),
Self::TransferEncoding(te) => ("Transfer-Encoding".as_bytes(), te.as_bytes_out()),
Self::Unknown(field, value) => (field.as_bytes_out(), value.as_bytes_out()),
};
(field, value)
}
}