Skip to content

Commit 367206b

Browse files
committed
Decode debug_data in GOAWAY frames
1 parent ffbd87c commit 367206b

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/frame/go_away.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
use crate::frame::{self, Error, Head, Kind, Reason, StreamId};
1+
use std::fmt;
2+
3+
use bytes::{BufMut, Bytes};
24

3-
use bytes::BufMut;
5+
use crate::frame::{self, Error, Head, Kind, Reason, StreamId};
46

5-
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
7+
#[derive(Clone, Eq, PartialEq)]
68
pub struct GoAway {
79
last_stream_id: StreamId,
810
error_code: Reason,
11+
#[allow(unused)]
12+
debug_data: Bytes,
913
}
1014

1115
impl GoAway {
1216
pub fn new(last_stream_id: StreamId, reason: Reason) -> Self {
1317
GoAway {
1418
last_stream_id,
1519
error_code: reason,
20+
debug_data: Bytes::new(),
1621
}
1722
}
1823

@@ -24,17 +29,24 @@ impl GoAway {
2429
self.error_code
2530
}
2631

32+
#[cfg(feature = "unstable")]
33+
pub fn debug_data(&self) -> &[u8] {
34+
&self.debug_data
35+
}
36+
2737
pub fn load(payload: &[u8]) -> Result<GoAway, Error> {
2838
if payload.len() < 8 {
2939
return Err(Error::BadFrameSize);
3040
}
3141

3242
let (last_stream_id, _) = StreamId::parse(&payload[..4]);
3343
let error_code = unpack_octets_4!(payload, 4, u32);
44+
let debug_data = Bytes::from(&payload[8..]);
3445

3546
Ok(GoAway {
3647
last_stream_id,
3748
error_code: error_code.into(),
49+
debug_data,
3850
})
3951
}
4052

@@ -52,3 +64,17 @@ impl<B> From<GoAway> for frame::Frame<B> {
5264
frame::Frame::GoAway(src)
5365
}
5466
}
67+
68+
impl fmt::Debug for GoAway {
69+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70+
let mut builder = f.debug_struct("GoAway");
71+
builder.field("error_code", &self.error_code);
72+
builder.field("last_stream_id", &self.last_stream_id);
73+
74+
if !self.debug_data.is_empty() {
75+
builder.field("debug_data", &self.debug_data);
76+
}
77+
78+
builder.finish()
79+
}
80+
}

tests/h2-tests/tests/codec_read.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,26 @@ async fn update_max_frame_len_at_rest() {
214214
"frame with invalid size"
215215
);
216216
}
217+
218+
#[tokio::test]
219+
async fn read_goaway_with_debug_data() {
220+
let mut codec = raw_codec! {
221+
read => [
222+
// head
223+
0, 0, 22, 7, 0, 0, 0, 0, 0,
224+
// last_stream_id
225+
0, 0, 0, 1,
226+
// error_code
227+
0, 0, 0, 11,
228+
// debug_data
229+
"too_many_pings",
230+
];
231+
};
232+
233+
let data = poll_frame!(GoAway, codec);
234+
assert_eq!(data.reason(), Reason::ENHANCE_YOUR_CALM);
235+
assert_eq!(data.last_stream_id(), 1);
236+
assert_eq!(data.debug_data(), b"too_many_pings");
237+
238+
assert_closed!(codec);
239+
}

0 commit comments

Comments
 (0)