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
// Copyright 2022 - 2023 Wenmeng See the COPYRIGHT
// file at the top-level directory of this distribution.
// 
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// 
// Author: tickbh
// -----
// Created Date: 2023/08/22 10:03:26

use std::fmt;
use crate::{WebError};

use super::{DecoderError, HuffmanDecoderError};


#[derive(Debug)]
pub enum Http2Error {
    Decoder(DecoderError),
    Huffman(HuffmanDecoderError),
    /// A full frame header was not passed.
    Short,
    /// An unsupported value was set for the flag value.
    BadFlag(u8),

    /// An unsupported value was set for the frame kind.
    BadKind(u8),

    /// The padding length was larger than the frame-header-specified
    /// length of the payload.
    TooMuchPadding(u8),

    /// The payload length specified by the frame header was shorter than
    /// necessary for the parser settings specified and the frame type.
    ///
    /// This happens if, for instance, the priority flag is set and the
    /// header length is shorter than a stream dependency.
    ///
    /// `PayloadLengthTooShort` should be treated as a protocol error.
    PayloadLengthTooShort,

    /// The payload length specified by the frame header of a settings frame
    /// was not a round multiple of the size of a single setting.
    PartialSettingLength,

    /// The payload length specified by the frame header was not the
    /// value necessary for the specific frame type.
    InvalidPayloadLength,
    /// 无效的streamId, 比如setting只能以0的id来传送
    InvalidStreamId,
    /// 无效的设置值, 比如enable_push只能取0和1
    InvalidSettingValue,
    /// 无效的frame大小 
    BadFrameSize,
    /// 无效的窗口大小文件
    InvalidWindowUpdateValue,
    /// 无效的依赖StreamId
    InvalidDependencyId,
    /// 无效的报文信息
    MalformedMessage,
    /// 请求的头信息不全
    InvalidRequesetUrl,
}


impl Http2Error {
    #[inline]
    pub fn description_str(&self) -> &'static str {
        match *self {
            Self::Decoder(_) => "",
            Self::Huffman(_) => "",
            _ => "",
        }
    }

    pub fn into<E: Into<Http2Error>>(e: E) -> WebError {
        WebError::Http2(e.into())
    }
}

impl fmt::Display for Http2Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.description_str())
    }
}

impl From<DecoderError> for Http2Error {
    fn from(e: DecoderError) -> Self {
        Http2Error::Decoder(e)
    }
}

impl From<HuffmanDecoderError> for Http2Error {
    fn from(e: HuffmanDecoderError) -> Self {
        Http2Error::Huffman(e)
    }
}

impl Into<WebError> for Http2Error {
    fn into(self) -> WebError {
        WebError::Http2(self)
    }
}