1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! The module contains the implementation of the `RST_STREAM` frame.
use std::io;

use http::{ErrorCode, StreamId};
use http::frame::{Frame, FrameIR, FrameBuilder, FrameHeader, RawFrame, NoFlag};

/// The total allowed size for the `RST_STREAM` frame payload.
pub const RST_STREAM_FRAME_LEN: u32 = 4;
/// The frame type of the `RST_STREAM` frame.
pub const RST_STREAM_FRAME_TYPE: u8 = 0x3;

/// The struct represents the `RST_STREAM` HTTP/2 frame.
#[derive(Clone, Debug, PartialEq)]
pub struct RstStreamFrame {
    raw_error_code: u32,
    stream_id: StreamId,
    flags: u8,
}

impl RstStreamFrame {
    /// Constructs a new `RstStreamFrame` with the given `ErrorCode`.
    pub fn new(stream_id: StreamId, error_code: ErrorCode) -> RstStreamFrame {
        RstStreamFrame {
            raw_error_code: error_code.into(),
            stream_id: stream_id,
            flags: 0,
        }
    }

    /// Constructs a new `RstStreamFrame` that will use the given `raw_error_code` for its payload.
    pub fn with_raw_error_code(stream_id: StreamId, raw_error_code: u32) -> RstStreamFrame {
        RstStreamFrame {
            raw_error_code: raw_error_code,
            stream_id: stream_id,
            flags: 0,
        }
    }

    /// Returns the interpreted error code of the frame. Any unknown error codes are mapped into
    /// the `InternalError` variant of the enum.
    pub fn error_code(&self) -> ErrorCode {
        self.raw_error_code.into()
    }

    /// Returns the original raw error code of the frame. If the code is unknown, it will not be
    /// changed.
    pub fn raw_error_code(&self) -> u32 {
        self.raw_error_code
    }
}

impl<'a> Frame<'a> for RstStreamFrame {
    type FlagType = NoFlag;

    fn from_raw(raw_frame: &'a RawFrame<'a>) -> Option<Self> {
        let (payload_len, frame_type, flags, stream_id) = raw_frame.header();
        if payload_len != RST_STREAM_FRAME_LEN {
            return None;
        }
        if frame_type != RST_STREAM_FRAME_TYPE {
            return None;
        }
        if stream_id == 0x0 {
            return None;
        }

        let error = unpack_octets_4!(raw_frame.payload(), 0, u32);

        Some(RstStreamFrame {
            raw_error_code: error,
            stream_id: stream_id,
            flags: flags,
        })
    }

    fn is_set(&self, _: NoFlag) -> bool {
        false
    }
    fn get_stream_id(&self) -> StreamId {
        self.stream_id
    }
    fn get_header(&self) -> FrameHeader {
        (RST_STREAM_FRAME_LEN,
         RST_STREAM_FRAME_TYPE,
         self.flags,
         self.stream_id)
    }
}

impl FrameIR for RstStreamFrame {
    fn serialize_into<B: FrameBuilder>(self, builder: &mut B) -> io::Result<()> {
        try!(builder.write_header(self.get_header()));
        try!(builder.write_u32(self.raw_error_code));
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::RstStreamFrame;

    use http::tests::common::serialize_frame;
    use http::ErrorCode;
    use http::frame::{pack_header, FrameHeader, Frame};

    /// A helper function that creates a new Vec containing the serialized representation of the
    /// given `FrameHeader` followed by the raw provided payload.
    fn prepare_frame_bytes(header: FrameHeader, payload: Vec<u8>) -> Vec<u8> {
        let mut buf = Vec::new();
        buf.extend(pack_header(&header).to_vec());
        buf.extend(payload);
        buf
    }

    #[test]
    fn test_parse_valid() {
        let raw = prepare_frame_bytes((4, 0x3, 0, 1), vec![0, 0, 0, 1]);
        let rst = RstStreamFrame::from_raw(&raw.into()).expect("Valid frame expected");
        assert_eq!(rst.error_code(), ErrorCode::ProtocolError);
        assert_eq!(rst.get_stream_id(), 1);
    }

    #[test]
    fn test_parse_valid_with_unknown_flags() {
        let raw = prepare_frame_bytes((4, 0x3, 0x80, 1), vec![0, 0, 0, 1]);
        let rst = RstStreamFrame::from_raw(&raw.into()).expect("Valid frame expected");
        assert_eq!(rst.error_code(), ErrorCode::ProtocolError);
        assert_eq!(rst.get_stream_id(), 1);
        // The raw flag set is correctly reported from the header, but the parsed frame itself does
        // not know how to interpret them.
        assert_eq!(rst.get_header().2, 0x80);
    }

    #[test]
    fn test_parse_unknown_error_code() {
        let raw = prepare_frame_bytes((4, 0x3, 0x80, 1), vec![1, 0, 0, 1]);
        let rst = RstStreamFrame::from_raw(&raw.into()).expect("Valid frame expected");
        // Unknown error codes are considered equivalent to an internal error.
        assert_eq!(rst.error_code(), ErrorCode::InternalError);
        // ...but the frame still surfaces the original raw code, so that clients can handle that,
        // if they so choose.
        assert_eq!(rst.raw_error_code(), 0x01000001);
    }

    #[test]
    fn test_parse_invalid_stream_id() {
        let raw = prepare_frame_bytes((4, 0x3, 0x80, 0), vec![0, 0, 0, 1]);
        assert!(RstStreamFrame::from_raw(&raw.into()).is_none());
    }

    #[test]
    fn test_parse_invalid_payload_size() {
        let raw = prepare_frame_bytes((5, 0x3, 0x00, 2), vec![0, 0, 0, 1, 0]);
        assert!(RstStreamFrame::from_raw(&raw.into()).is_none());
    }

    #[test]
    fn test_parse_invalid_id() {
        let raw = prepare_frame_bytes((4, 0x1, 0x00, 2), vec![0, 0, 0, 1, 0]);
        assert!(RstStreamFrame::from_raw(&raw.into()).is_none());
    }

    #[test]
    fn test_serialize_protocol_error() {
        let frame = RstStreamFrame::new(1, ErrorCode::ProtocolError);
        let raw = serialize_frame(&frame);
        assert_eq!(raw, prepare_frame_bytes((4, 0x3, 0, 1), vec![0, 0, 0, 1]));
    }

    #[test]
    fn test_serialize_stream_closed() {
        let frame = RstStreamFrame::new(2, ErrorCode::StreamClosed);
        let raw = serialize_frame(&frame);
        assert_eq!(raw, prepare_frame_bytes((4, 0x3, 0, 2), vec![0, 0, 0, 0x5]));
    }

    #[test]
    fn test_serialize_raw_error_code() {
        let frame = RstStreamFrame::with_raw_error_code(3, 1024);
        let raw = serialize_frame(&frame);
        assert_eq!(raw,
                   prepare_frame_bytes((4, 0x3, 0, 3), vec![0, 0, 0x04, 0]));
    }

    #[test]
    fn test_partial_eq() {
        let frame1 = RstStreamFrame::with_raw_error_code(3, 1);
        let frame2 = RstStreamFrame::new(3, ErrorCode::ProtocolError);
        assert_eq!(frame1, frame2);
    }
}