diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/h2/src/frame/go_away.rs | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/h2/src/frame/go_away.rs')
-rw-r--r-- | third_party/rust/h2/src/frame/go_away.rs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/third_party/rust/h2/src/frame/go_away.rs b/third_party/rust/h2/src/frame/go_away.rs new file mode 100644 index 0000000000..91d9c4c6b5 --- /dev/null +++ b/third_party/rust/h2/src/frame/go_away.rs @@ -0,0 +1,79 @@ +use std::fmt; + +use bytes::{BufMut, Bytes}; + +use crate::frame::{self, Error, Head, Kind, Reason, StreamId}; + +#[derive(Clone, Eq, PartialEq)] +pub struct GoAway { + last_stream_id: StreamId, + error_code: Reason, + #[allow(unused)] + debug_data: Bytes, +} + +impl GoAway { + pub fn new(last_stream_id: StreamId, reason: Reason) -> Self { + GoAway { + last_stream_id, + error_code: reason, + debug_data: Bytes::new(), + } + } + + pub fn last_stream_id(&self) -> StreamId { + self.last_stream_id + } + + pub fn reason(&self) -> Reason { + self.error_code + } + + pub fn debug_data(&self) -> &Bytes { + &self.debug_data + } + + pub fn load(payload: &[u8]) -> Result<GoAway, Error> { + if payload.len() < 8 { + return Err(Error::BadFrameSize); + } + + let (last_stream_id, _) = StreamId::parse(&payload[..4]); + let error_code = unpack_octets_4!(payload, 4, u32); + let debug_data = Bytes::copy_from_slice(&payload[8..]); + + Ok(GoAway { + last_stream_id, + error_code: error_code.into(), + debug_data, + }) + } + + pub fn encode<B: BufMut>(&self, dst: &mut B) { + tracing::trace!("encoding GO_AWAY; code={:?}", self.error_code); + let head = Head::new(Kind::GoAway, 0, StreamId::zero()); + head.encode(8, dst); + dst.put_u32(self.last_stream_id.into()); + dst.put_u32(self.error_code.into()); + } +} + +impl<B> From<GoAway> for frame::Frame<B> { + fn from(src: GoAway) -> Self { + frame::Frame::GoAway(src) + } +} + +impl fmt::Debug for GoAway { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut builder = f.debug_struct("GoAway"); + builder.field("error_code", &self.error_code); + builder.field("last_stream_id", &self.last_stream_id); + + if !self.debug_data.is_empty() { + builder.field("debug_data", &self.debug_data); + } + + builder.finish() + } +} |