| 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 neqo_common::{Decoder, Encoder}; |
| 8 |
|
| 9 |
use super::hframe::HFrameType; |
| 10 |
use crate::{frames::reader::FrameDecoder, Error, Res}; |
| 11 |
|
| 12 |
pub type WebTransportFrameType = u64; |
| 13 |
|
| 14 |
const WT_FRAME_CLOSE_SESSION: WebTransportFrameType = 0x2843; |
| 15 |
const WT_FRAME_CLOSE_MAX_MESSAGE_SIZE: u64 = 1024; |
| 16 |
|
| 17 |
#[derive(PartialEq, Eq, Debug)] |
| 18 |
pub enum WebTransportFrame { |
| 19 |
CloseSession { error: u32, message: String }, |
| 20 |
} |
| 21 |
|
| 22 |
impl WebTransportFrame { |
| 23 |
pub fn encode(&self, enc: &mut Encoder) { |
| 24 |
enc.encode_varint(WT_FRAME_CLOSE_SESSION); |
| 25 |
let Self::CloseSession { error, message } = &self; |
| 26 |
enc.encode_varint(4 + message.len() as u64); |
| 27 |
enc.encode_uint(4, *error); |
| 28 |
enc.encode(message.as_bytes()); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
impl FrameDecoder<Self> for WebTransportFrame { |
| 33 |
fn decode(frame_type: HFrameType, frame_len: u64, data: Option<&[u8]>) -> Res<Option<Self>> { |
| 34 |
if let Some(payload) = data { |
| 35 |
let mut dec = Decoder::from(payload); |
| 36 |
if frame_type == HFrameType(WT_FRAME_CLOSE_SESSION) { |
| 37 |
if frame_len > WT_FRAME_CLOSE_MAX_MESSAGE_SIZE + 4 { |
| 38 |
return Err(Error::HttpMessageError); |
| 39 |
} |
| 40 |
let error = dec.decode_uint().ok_or(Error::HttpMessageError)?; |
| 41 |
let Ok(message) = String::from_utf8(dec.decode_remainder().to_vec()) else { |
| 42 |
return Err(Error::HttpMessageError); |
| 43 |
}; |
| 44 |
Ok(Some(Self::CloseSession { error, message })) |
| 45 |
} else { |
| 46 |
Ok(None) |
| 47 |
} |
| 48 |
} else { |
| 49 |
Ok(None) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
fn is_known_type(frame_type: HFrameType) -> bool { |
| 54 |
frame_type == HFrameType(WT_FRAME_CLOSE_SESSION) |
| 55 |
} |
| 56 |
} |