use crate::{HttpError, HttpRequestLine, HttpStatus, Version};
#[doc = crate::_tags!(network protocol)]
#[doc = crate::_doc_meta!{location("sys/net/http")}]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum HttpVersion {
Http10,
Http11,
Http2,
Http3,
}
impl HttpVersion {
#[must_use]
pub const fn label(self) -> &'static str {
match self {
Self::Http10 => "HTTP/1.0",
Self::Http11 => "HTTP/1.1",
Self::Http2 => "HTTP/2",
Self::Http3 => "HTTP/3",
}
}
#[must_use]
pub const fn parts(self) -> (u8, u8) {
(self.major(), self.minor())
}
#[must_use]
pub const fn major(self) -> u8 {
match self {
Self::Http10 | Self::Http11 => 1,
Self::Http2 => 2,
Self::Http3 => 3,
}
}
#[must_use]
pub const fn minor(self) -> u8 {
match self {
Self::Http11 => 1,
Self::Http10 | Self::Http2 | Self::Http3 => 0,
}
}
#[must_use]
pub const fn is_http1(self) -> bool {
matches!(self, Self::Http10 | Self::Http11)
}
#[must_use]
pub const fn http1_token(self) -> Option<&'static str> {
match self {
Self::Http10 => Some("HTTP/1.0"),
Self::Http11 => Some("HTTP/1.1"),
Self::Http2 | Self::Http3 => None,
}
}
#[must_use]
pub const fn parse_http1_token(token: &[u8]) -> Option<Self> {
if token.len() != 8 {
return None;
}
if token[0] != b'H'
|| token[1] != b'T'
|| token[2] != b'T'
|| token[3] != b'P'
|| token[4] != b'/'
|| token[5] != b'1'
|| token[6] != b'.'
{
return None;
}
match token[7] {
b'0' => Some(Self::Http10),
b'1' => Some(Self::Http11),
_ => None,
}
}
}