| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
use std::fmt::{Debug, Write as _}; |
| 8 |
|
| 9 |
use neqo_common::{Decoder, Encoder}; |
| 10 |
use neqo_crypto::random; |
| 11 |
use neqo_transport::StreamId; |
| 12 |
|
| 13 |
use crate::{frames::reader::FrameDecoder, settings::HSettings, Error, Priority, PushId, Res}; |
| 14 |
|
| 15 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 16 |
pub struct HFrameType(pub u64); |
| 17 |
|
| 18 |
pub const H3_FRAME_TYPE_DATA: HFrameType = HFrameType(0x0); |
| 19 |
pub const H3_FRAME_TYPE_HEADERS: HFrameType = HFrameType(0x1); |
| 20 |
pub const H3_FRAME_TYPE_CANCEL_PUSH: HFrameType = HFrameType(0x3); |
| 21 |
pub const H3_FRAME_TYPE_SETTINGS: HFrameType = HFrameType(0x4); |
| 22 |
pub const H3_FRAME_TYPE_PUSH_PROMISE: HFrameType = HFrameType(0x5); |
| 23 |
pub const H3_FRAME_TYPE_GOAWAY: HFrameType = HFrameType(0x7); |
| 24 |
pub const H3_FRAME_TYPE_MAX_PUSH_ID: HFrameType = HFrameType(0xd); |
| 25 |
pub const H3_FRAME_TYPE_PRIORITY_UPDATE_REQUEST: HFrameType = HFrameType(0xf0700); |
| 26 |
pub const H3_FRAME_TYPE_PRIORITY_UPDATE_PUSH: HFrameType = HFrameType(0xf0701); |
| 27 |
|
| 28 |
pub const H3_RESERVED_FRAME_TYPES: &[HFrameType] = &[ |
| 29 |
HFrameType(0x2), |
| 30 |
HFrameType(0x6), |
| 31 |
HFrameType(0x8), |
| 32 |
HFrameType(0x9), |
| 33 |
]; |
| 34 |
|
| 35 |
impl From<HFrameType> for u64 { |
| 36 |
fn from(t: HFrameType) -> Self { |
| 37 |
t.0 |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// data for DATA frame is not read into HFrame::Data. |
| 42 |
#[derive(PartialEq, Eq, Debug)] |
| 43 |
pub enum HFrame { |
| 44 |
Data { |
| 45 |
len: u64, // length of the data |
| 46 |
}, |
| 47 |
Headers { |
| 48 |
header_block: Vec<u8>, |
| 49 |
}, |
| 50 |
CancelPush { |
| 51 |
push_id: PushId, |
| 52 |
}, |
| 53 |
Settings { |
| 54 |
settings: HSettings, |
| 55 |
}, |
| 56 |
PushPromise { |
| 57 |
push_id: PushId, |
| 58 |
header_block: Vec<u8>, |
| 59 |
}, |
| 60 |
Goaway { |
| 61 |
stream_id: StreamId, |
| 62 |
}, |
| 63 |
MaxPushId { |
| 64 |
push_id: PushId, |
| 65 |
}, |
| 66 |
Grease, |
| 67 |
PriorityUpdateRequest { |
| 68 |
element_id: u64, |
| 69 |
priority: Priority, |
| 70 |
}, |
| 71 |
PriorityUpdatePush { |
| 72 |
element_id: u64, |
| 73 |
priority: Priority, |
| 74 |
}, |
| 75 |
} |
| 76 |
|
| 77 |
impl HFrame { |
| 78 |
fn get_type(&self) -> HFrameType { |
| 79 |
match self { |
| 80 |
Self::Data { .. } => H3_FRAME_TYPE_DATA, |
| 81 |
Self::Headers { .. } => H3_FRAME_TYPE_HEADERS, |
| 82 |
Self::CancelPush { .. } => H3_FRAME_TYPE_CANCEL_PUSH, |
| 83 |
Self::Settings { .. } => H3_FRAME_TYPE_SETTINGS, |
| 84 |
Self::PushPromise { .. } => H3_FRAME_TYPE_PUSH_PROMISE, |
| 85 |
Self::Goaway { .. } => H3_FRAME_TYPE_GOAWAY, |
| 86 |
Self::MaxPushId { .. } => H3_FRAME_TYPE_MAX_PUSH_ID, |
| 87 |
Self::PriorityUpdateRequest { .. } => H3_FRAME_TYPE_PRIORITY_UPDATE_REQUEST, |
| 88 |
Self::PriorityUpdatePush { .. } => H3_FRAME_TYPE_PRIORITY_UPDATE_PUSH, |
| 89 |
Self::Grease => { |
| 90 |
let r = u64::from_ne_bytes(random::<8>()); |
| 91 |
// Zero out the top 7 bits: 2 for being a varint; 5 to account for the *0x1f. |
| 92 |
HFrameType((r >> 7) * 0x1f + 0x21) |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
pub fn encode(&self, enc: &mut Encoder) { |
| 98 |
enc.encode_varint(self.get_type()); |
| 99 |
|
| 100 |
match self { |
| 101 |
Self::Data { len } => { |
| 102 |
// DATA frame only encode the length here. |
| 103 |
enc.encode_varint(*len); |
| 104 |
} |
| 105 |
Self::Headers { header_block } => { |
| 106 |
enc.encode_vvec(header_block); |
| 107 |
} |
| 108 |
Self::CancelPush { push_id } => { |
| 109 |
enc.encode_vvec_with(|enc_inner| { |
| 110 |
enc_inner.encode_varint(*push_id); |
| 111 |
}); |
| 112 |
} |
| 113 |
Self::Settings { settings } => { |
| 114 |
settings.encode_frame_contents(enc); |
| 115 |
} |
| 116 |
Self::PushPromise { |
| 117 |
push_id, |
| 118 |
header_block, |
| 119 |
} => { |
| 120 |
enc.encode_varint( |
| 121 |
(header_block.len() + (Encoder::varint_len(u64::from(*push_id)))) as u64, |
| 122 |
); |
| 123 |
enc.encode_varint(*push_id); |
| 124 |
enc.encode(header_block); |
| 125 |
} |
| 126 |
Self::Goaway { stream_id } => { |
| 127 |
enc.encode_vvec_with(|enc_inner| { |
| 128 |
enc_inner.encode_varint(stream_id.as_u64()); |
| 129 |
}); |
| 130 |
} |
| 131 |
Self::MaxPushId { push_id } => { |
| 132 |
enc.encode_vvec_with(|enc_inner| { |
| 133 |
enc_inner.encode_varint(*push_id); |
| 134 |
}); |
| 135 |
} |
| 136 |
Self::Grease => { |
| 137 |
// Encode some number of random bytes. |
| 138 |
let r = random::<8>(); |
| 139 |
enc.encode_vvec(&r[1..usize::from(1 + (r[0] & 0x7))]); |
| 140 |
} |
| 141 |
Self::PriorityUpdateRequest { |
| 142 |
element_id, |
| 143 |
priority, |
| 144 |
} |
| 145 |
| Self::PriorityUpdatePush { |
| 146 |
element_id, |
| 147 |
priority, |
| 148 |
} => { |
| 149 |
enc.encode_vvec_with(|enc_inner| { |
| 150 |
enc_inner.encode_varint(*element_id); |
| 151 |
write!(enc_inner, "{priority}").expect("write OK"); |
| 152 |
}); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
impl FrameDecoder<Self> for HFrame { |
| 159 |
fn frame_type_allowed(frame_type: HFrameType) -> Res<()> { |
| 160 |
if H3_RESERVED_FRAME_TYPES.contains(&frame_type) { |
| 161 |
return Err(Error::HttpFrameUnexpected); |
| 162 |
} |
| 163 |
Ok(()) |
| 164 |
} |
| 165 |
|
| 166 |
fn decode(frame_type: HFrameType, frame_len: u64, data: Option<&[u8]>) -> Res<Option<Self>> { |
| 167 |
if frame_type == H3_FRAME_TYPE_DATA { |
| 168 |
Ok(Some(Self::Data { len: frame_len })) |
| 169 |
} else if let Some(payload) = data { |
| 170 |
let mut dec = Decoder::from(payload); |
| 171 |
Ok(match frame_type { |
| 172 |
H3_FRAME_TYPE_DATA => unreachable!("DATA frame has been handled already"), |
| 173 |
H3_FRAME_TYPE_HEADERS => Some(Self::Headers { |
| 174 |
header_block: dec.decode_remainder().to_vec(), |
| 175 |
}), |
| 176 |
H3_FRAME_TYPE_CANCEL_PUSH => Some(Self::CancelPush { |
| 177 |
push_id: dec.decode_varint().ok_or(Error::HttpFrame)?.into(), |
| 178 |
}), |
| 179 |
H3_FRAME_TYPE_SETTINGS => { |
| 180 |
let mut settings = HSettings::default(); |
| 181 |
settings.decode_frame_contents(&mut dec).map_err(|e| { |
| 182 |
if e == Error::HttpSettings { |
| 183 |
e |
| 184 |
} else { |
| 185 |
Error::HttpFrame |
| 186 |
} |
| 187 |
})?; |
| 188 |
Some(Self::Settings { settings }) |
| 189 |
} |
| 190 |
H3_FRAME_TYPE_PUSH_PROMISE => Some(Self::PushPromise { |
| 191 |
push_id: dec.decode_varint().ok_or(Error::HttpFrame)?.into(), |
| 192 |
header_block: dec.decode_remainder().to_vec(), |
| 193 |
}), |
| 194 |
H3_FRAME_TYPE_GOAWAY => Some(Self::Goaway { |
| 195 |
stream_id: StreamId::new(dec.decode_varint().ok_or(Error::HttpFrame)?), |
| 196 |
}), |
| 197 |
H3_FRAME_TYPE_MAX_PUSH_ID => Some(Self::MaxPushId { |
| 198 |
push_id: dec.decode_varint().ok_or(Error::HttpFrame)?.into(), |
| 199 |
}), |
| 200 |
H3_FRAME_TYPE_PRIORITY_UPDATE_REQUEST | H3_FRAME_TYPE_PRIORITY_UPDATE_PUSH => { |
| 201 |
let element_id = dec.decode_varint().ok_or(Error::HttpFrame)?; |
| 202 |
let priority = dec.decode_remainder(); |
| 203 |
let priority = Priority::from_bytes(priority)?; |
| 204 |
if frame_type == H3_FRAME_TYPE_PRIORITY_UPDATE_REQUEST { |
| 205 |
Some(Self::PriorityUpdateRequest { |
| 206 |
element_id, |
| 207 |
priority, |
| 208 |
}) |
| 209 |
} else { |
| 210 |
Some(Self::PriorityUpdatePush { |
| 211 |
element_id, |
| 212 |
priority, |
| 213 |
}) |
| 214 |
} |
| 215 |
} |
| 216 |
_ => None, |
| 217 |
}) |
| 218 |
} else { |
| 219 |
Ok(None) |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
fn is_known_type(frame_type: HFrameType) -> bool { |
| 224 |
matches!( |
| 225 |
frame_type, |
| 226 |
H3_FRAME_TYPE_DATA |
| 227 |
| H3_FRAME_TYPE_HEADERS |
| 228 |
| H3_FRAME_TYPE_CANCEL_PUSH |
| 229 |
| H3_FRAME_TYPE_SETTINGS |
| 230 |
| H3_FRAME_TYPE_PUSH_PROMISE |
| 231 |
| H3_FRAME_TYPE_GOAWAY |
| 232 |
| H3_FRAME_TYPE_MAX_PUSH_ID |
| 233 |
| H3_FRAME_TYPE_PRIORITY_UPDATE_REQUEST |
| 234 |
| H3_FRAME_TYPE_PRIORITY_UPDATE_PUSH |
| 235 |
) |
| 236 |
} |
| 237 |
} |