use crate::error::{DecodeError, EncodeError};
use crate::limits::Limits;
use crate::varint;
pub mod frame {
pub const DATA: u64 = 0x0;
pub const HEADERS: u64 = 0x1;
pub const CANCEL_PUSH: u64 = 0x3;
pub const SETTINGS: u64 = 0x4;
pub const PUSH_PROMISE: u64 = 0x5;
pub const GOAWAY: u64 = 0x7;
pub const MAX_PUSH_ID: u64 = 0xd;
}
#[must_use]
pub const fn is_http2_reserved(kind: u64) -> bool {
matches!(kind, 0x2 | 0x6 | 0x8 | 0x9)
}
pub mod stream {
pub const CONTROL: u64 = 0x0;
pub const PUSH: u64 = 0x1;
pub const QPACK_ENCODER: u64 = 0x2;
pub const QPACK_DECODER: u64 = 0x3;
}
pub mod setting {
pub const QPACK_MAX_TABLE_CAPACITY: u64 = 0x1;
pub const MAX_FIELD_SECTION_SIZE: u64 = 0x6;
pub const QPACK_BLOCKED_STREAMS: u64 = 0x7;
}
pub mod code {
pub const H3_NO_ERROR: u64 = 0x100;
pub const H3_GENERAL_PROTOCOL_ERROR: u64 = 0x101;
pub const H3_INTERNAL_ERROR: u64 = 0x102;
pub const H3_STREAM_CREATION_ERROR: u64 = 0x103;
pub const H3_CLOSED_CRITICAL_STREAM: u64 = 0x104;
pub const H3_FRAME_UNEXPECTED: u64 = 0x105;
pub const H3_FRAME_ERROR: u64 = 0x106;
pub const H3_EXCESSIVE_LOAD: u64 = 0x107;
pub const H3_ID_ERROR: u64 = 0x108;
pub const H3_SETTINGS_ERROR: u64 = 0x109;
pub const H3_MISSING_SETTINGS: u64 = 0x10a;
pub const H3_REQUEST_REJECTED: u64 = 0x10b;
pub const H3_REQUEST_CANCELLED: u64 = 0x10c;
pub const H3_REQUEST_INCOMPLETE: u64 = 0x10d;
pub const H3_MESSAGE_ERROR: u64 = 0x10e;
pub const H3_CONNECT_ERROR: u64 = 0x10f;
pub const H3_VERSION_FALLBACK: u64 = 0x110;
pub const QPACK_DECOMPRESSION_FAILED: u64 = 0x200;
pub const QPACK_ENCODER_STREAM_ERROR: u64 = 0x201;
pub const QPACK_DECODER_STREAM_ERROR: u64 = 0x202;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FrameHeader {
pub kind: u64,
pub len: u64,
}
pub fn decode_frame_header(input: &[u8]) -> Result<(FrameHeader, usize), DecodeError> {
let (kind, a) = varint::decode(input)?;
let (len, b) = varint::decode(&input[a..])?;
Ok((FrameHeader { kind, len }, a + b))
}
const fn malformed(what: &'static str) -> DecodeError {
DecodeError::Malformed { what }
}
pub fn decode_settings(payload: &[u8], limits: Limits) -> Result<Vec<(u64, u64)>, DecodeError> {
if payload.len() as u64 > u64::from(limits.max_frame_size) {
return Err(DecodeError::TooLarge {
what: "frame",
declared: payload.len() as u64,
cap: u64::from(limits.max_frame_size),
});
}
let mut out: Vec<(u64, u64)> = Vec::new();
let mut pos = 0;
while pos < payload.len() {
let (id, a) = varint::decode(&payload[pos..]).map_err(|_| malformed("SETTINGS"))?;
pos += a;
let (value, b) = varint::decode(&payload[pos..]).map_err(|_| malformed("SETTINGS"))?;
pos += b;
if (0x2..=0x5).contains(&id) {
return Err(malformed("HTTP/2 setting in HTTP/3"));
}
if out.iter().any(|&(seen, _)| seen == id) {
return Err(malformed("duplicate setting"));
}
out.push((id, value));
}
Ok(out)
}
pub fn decode_goaway(payload: &[u8]) -> Result<u64, DecodeError> {
match varint::decode(payload) {
Ok((id, used)) if used == payload.len() => Ok(id),
_ => Err(malformed("GOAWAY")),
}
}
fn header(kind: u64, len: u64, out: &mut Vec<u8>) -> Result<(), EncodeError> {
varint::encode(kind, out)?;
varint::encode(len, out)
}
pub fn encode_settings(settings: &[(u64, u64)], out: &mut Vec<u8>) -> Result<(), EncodeError> {
let mut payload = Vec::new();
for &(id, value) in settings {
varint::encode(id, &mut payload)?;
varint::encode(value, &mut payload)?;
}
header(frame::SETTINGS, payload.len() as u64, out)?;
out.extend_from_slice(&payload);
Ok(())
}
pub fn encode_data_header(len: u64, out: &mut Vec<u8>) -> Result<(), EncodeError> {
header(frame::DATA, len, out)
}
pub fn encode_headers(block: &[u8], out: &mut Vec<u8>) {
header(frame::HEADERS, block.len() as u64, out).expect("a slice length fits a varint");
out.extend_from_slice(block);
}
pub fn encode_goaway(id: u64, out: &mut Vec<u8>) -> Result<(), EncodeError> {
if id > varint::MAX {
return Err(EncodeError::TooLarge {
what: "varint",
len: id,
cap: varint::MAX,
});
}
header(frame::GOAWAY, varint::len(id) as u64, out)?;
varint::encode(id, out)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn settings_round_trip_and_refuse_by_name() {
let mut out = Vec::new();
encode_settings(
&[(setting::MAX_FIELD_SECTION_SIZE, 65_536), (0x21, 0)],
&mut out,
)
.unwrap();
let (head, used) = decode_frame_header(&out).unwrap();
assert_eq!(
head,
FrameHeader {
kind: frame::SETTINGS,
len: (out.len() - used) as u64
}
);
assert_eq!(
decode_settings(&out[used..], Limits::DEFAULT),
Ok(vec![(6, 65_536), (0x21, 0)])
);
let refuse = |p: &[u8]| match decode_settings(p, Limits::DEFAULT) {
Err(DecodeError::Malformed { what }) => what,
other => panic!("{other:?}"),
};
assert_eq!(refuse(&[0x06, 0x01, 0x06, 0x02]), "duplicate setting");
assert_eq!(refuse(&[0x04, 0x00]), "HTTP/2 setting in HTTP/3");
assert_eq!(refuse(&[0x06]), "SETTINGS");
let small = Limits {
max_frame_size: 1,
..Limits::DEFAULT
};
assert!(matches!(
decode_settings(&[0x06, 0x01], small),
Err(DecodeError::TooLarge { what: "frame", .. })
));
}
#[test]
fn goaway_is_one_integer() {
let mut out = Vec::new();
encode_goaway(400, &mut out).unwrap();
let (head, used) = decode_frame_header(&out).unwrap();
assert_eq!(head.kind, frame::GOAWAY);
assert_eq!(decode_goaway(&out[used..]), Ok(400));
assert_eq!(decode_goaway(&[0x01, 0x02]), Err(malformed("GOAWAY")));
}
#[test]
fn the_reserved_http2_types() {
assert!([0x2, 0x6, 0x8, 0x9].into_iter().all(is_http2_reserved));
assert!(!is_http2_reserved(frame::DATA));
assert!(!is_http2_reserved(frame::GOAWAY));
}
}