Skip to main content

net_wire/http1/
error.rs

1//! HTTP/1 parsing and construction errors.
2
3use core::fmt;
4
5/// Failure to parse HTTP/1 bytes.
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub enum Http1ParseError {
8    /// More bytes are required to complete the declared structure.
9    Incomplete {
10        /// Required bytes.
11        required: usize,
12        /// Available bytes.
13        available: usize,
14    },
15    /// The request or response start line is malformed.
16    InvalidStartLine,
17    /// A header or trailer field is malformed.
18    InvalidField,
19    /// A `Content-Length` value is invalid.
20    InvalidContentLength,
21    /// A `Content-Length` value exceeds `usize`.
22    ContentLengthOverflow,
23    /// Multiple `Content-Length` values disagree.
24    ConflictingContentLength,
25    /// `Transfer-Encoding` and `Content-Length` both occur.
26    TransferEncodingWithContentLength,
27    /// Transfer coding syntax is malformed.
28    InvalidTransferEncoding,
29    /// `chunked` is not final, is repeated, or has parameters.
30    InvalidChunkedTransferEncoding,
31    /// A chunk size is malformed.
32    InvalidChunkSize,
33    /// A chunk size exceeds `usize`.
34    ChunkSizeOverflow,
35    /// A chunk data terminator is malformed.
36    InvalidChunkTerminator,
37    /// A trailer field or trailer terminator is malformed.
38    InvalidTrailer,
39    /// The decoded chunk data total exceeds `usize`.
40    DecodedSizeOverflow,
41}
42
43impl fmt::Display for Http1ParseError {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        f.write_str(match self {
46            Self::Incomplete { .. } => "incomplete HTTP/1 input",
47            Self::InvalidStartLine => "invalid HTTP/1 start line",
48            Self::InvalidField => "invalid HTTP/1 field",
49            Self::InvalidContentLength => "invalid HTTP Content-Length",
50            Self::ContentLengthOverflow => "HTTP Content-Length exceeds platform limit",
51            Self::ConflictingContentLength => "conflicting HTTP Content-Length values",
52            Self::TransferEncodingWithContentLength => "HTTP Transfer-Encoding with Content-Length",
53            Self::InvalidTransferEncoding => "invalid HTTP Transfer-Encoding",
54            Self::InvalidChunkedTransferEncoding => "invalid HTTP chunked Transfer-Encoding",
55            Self::InvalidChunkSize => "invalid HTTP chunk size",
56            Self::ChunkSizeOverflow => "HTTP chunk size exceeds platform limit",
57            Self::InvalidChunkTerminator => "invalid HTTP chunk terminator",
58            Self::InvalidTrailer => "invalid HTTP trailer",
59            Self::DecodedSizeOverflow => "decoded HTTP chunk data exceeds platform limit",
60        })
61    }
62}
63
64/// Failure to build HTTP/1 bytes in caller-owned storage.
65#[derive(Clone, Copy, Debug, Eq, PartialEq)]
66pub enum Http1BuildError {
67    /// A start-line component or field is invalid.
68    InvalidValue,
69    /// The finished head length exceeds `usize`.
70    LengthTooLarge,
71    /// The caller buffer cannot contain the finished encoding.
72    BufferTooShort {
73        /// Required bytes.
74        required: usize,
75        /// Available bytes.
76        available: usize,
77    },
78}
79
80impl fmt::Display for Http1BuildError {
81    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82        match self {
83            Self::InvalidValue => f.write_str("invalid HTTP/1 builder value"),
84            Self::LengthTooLarge => f.write_str("HTTP/1 head length exceeds platform limit"),
85            Self::BufferTooShort {
86                required,
87                available,
88            } => write!(
89                f,
90                "HTTP/1 buffer is too short: need {required} bytes, have {available}"
91            ),
92        }
93    }
94}