From 43a97878ce14b72f0981164f87f2e35e14151312 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:22:09 +0200 Subject: Adding upstream version 110.0.1. Signed-off-by: Daniel Baumann --- third_party/rust/h2/src/frame/go_away.rs | 79 ++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 third_party/rust/h2/src/frame/go_away.rs (limited to 'third_party/rust/h2/src/frame/go_away.rs') 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 { + 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(&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 From for frame::Frame { + 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() + } +} -- cgit v1.2.3