-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patherror.rs
More file actions
236 lines (224 loc) · 7.22 KB
/
Copy patherror.rs
File metadata and controls
236 lines (224 loc) · 7.22 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//! Crate's error module.
//!
//! Contains all error types used throughout the crate.
use core::convert::Infallible;
/// Error decoding a WebSocket frame.
#[derive(Debug, thiserror::Error)]
pub enum FrameDecodeError {
/// Reserved bits are not zero.
#[error("Reserved bits must be zero")]
ReservedBitsNotZero,
/// Unmasked frame received from client.
///
/// The server must close the connection when an unmasked frame is received.
#[error("Received an unmasked frame from client")]
UnmaskedFrameFromClient,
/// Masked frame received from server.
///
/// The client must close the connection when a masked frame is received.
#[error("Received a masked frame from server")]
MaskedFrameFromServer,
/// Invalid opcode.
#[error("Invalid opcode")]
InvalidOpCode,
/// Payload length is too large.
// XXX: The payload length comes as a u64, converting it to usize might fail on 32-bit systems
#[error("Payload too large")]
PayloadTooLarge,
/// Control frame fragmented.
///
/// Control frames must not be fragmented.
#[error("Control frame fragmented")]
ControlFrameFragmented,
/// Control frame too large.
///
/// Control frames must have a payload length of 125 bytes or less.
#[error("Control frame too large")]
ControlFrameTooLarge,
}
/// Error encoding a WebSocket frame.
#[derive(Debug, thiserror::Error)]
pub enum FrameEncodeError {
/// Write buffer is too small to hold the encoded frame.
#[error("Buffer too small")]
BufferTooSmall,
}
/// Error decoding an HTTP request/response.
#[derive(Debug, thiserror::Error)]
pub enum HttpDecodeError {
/// Error parsing the HTTP request/response.
#[error("Parse error: {0}")]
Parse(httparse::Error),
}
impl From<httparse::Error> for HttpDecodeError {
fn from(err: httparse::Error) -> Self {
HttpDecodeError::Parse(err)
}
}
/// Error encoding an HTTP request/response.
#[derive(Debug, thiserror::Error)]
pub enum HttpEncodeError {
/// Write buffer is too small to hold the encoded HTTP request/response.
#[error("Buffer too small")]
BufferTooSmall,
}
/// Protocol specific errors/violations.
#[derive(Debug, thiserror::Error)]
pub enum ProtocolError {
/// Close frame is invalid.
#[error("Invalid close frame")]
InvalidCloseFrame,
/// Close code is invalid.
#[error("Invalid close code")]
InvalidCloseCode,
/// Text message contains invalid UTF-8.
#[error("Invalid UTF-8")]
InvalidUTF8,
/// Fragment is invalid.
///
/// This happens when a final fragment is received without any prior fragments.
#[error("Invalid fragment")]
InvalidFragment,
/// Continuation frame is invalid.
///
/// This happens when a continuation frame is received without an ongoing fragmented message.
#[error("Invalid continuation frame")]
InvalidContinuationFrame,
}
/// Error reading from a WebSocket connection.
#[derive(Debug, thiserror::Error)]
pub enum ReadError<I> {
/// Error reading a WebSocket frame from the underlying I/O.
#[error("Read frame error: {0}")]
ReadFrame(
#[source]
#[from]
framez::ReadError<I, FrameDecodeError>,
),
/// Error reading an HTTP request/response from the underlying I/O.
#[error("Read http error: {0}")]
ReadHttp(
#[source]
#[from]
framez::ReadError<I, HttpDecodeError>,
),
/// Protocol error.
#[error("Protocol error: {0}")]
Protocol(
#[source]
#[from]
ProtocolError,
),
/// Fragments buffer is too small to read a frame.
#[error("Fragments buffer too small to read a frame")]
FragmentsBufferTooSmall,
}
/// Error writing to a WebSocket connection.
#[derive(Debug, thiserror::Error)]
pub enum WriteError<I> {
/// Websocket connection is closed.
///
/// To close the TCP connection, you should drop/close the underlying I/O instance.
#[error("Connection closed")]
ConnectionClosed,
/// Error writing a WebSocket frame to the underlying I/O.
#[error("Write frame error: {0}")]
WriteFrame(
#[source]
#[from]
framez::WriteError<I, FrameEncodeError>,
),
/// Error writing an HTTP request/response to the underlying I/O.
#[error("Write http error: {0}")]
WriteHttp(
#[source]
#[from]
framez::WriteError<I, HttpEncodeError>,
),
}
/// Error establishing a WebSocket handshake.
///
/// # Generic Parameters
/// `E`: User-defined error type for custom errors during the handshake.
#[derive(Debug, thiserror::Error)]
pub enum HandshakeError<E = Infallible> {
/// Use of the wrong HTTP method (the WebSocket protocol requires the GET method to be used).
#[error("Unsupported HTTP method used - only GET is allowed")]
WrongHttpMethod,
/// Wrong HTTP version used (the WebSocket protocol requires version 1.1 or higher).
#[error("HTTP version must be 1.1 or higher")]
WrongHttpVersion,
/// Connection was closed during the handshake.
#[error("Connection closed during handshake")]
ConnectionClosed,
/// Invalid status code. (Should be 101 for switching protocols.)
#[error("Invalid status code")]
InvalidStatusCode,
/// Missing or invalid (`Upgrade`: `websocket`) header.
#[error("Missing or invalid upgrade header")]
MissingOrInvalidUpgrade,
/// Missing or invalid (`Connection`: `upgrade`) header.
#[error("Missing or invalid connection header")]
MissingOrInvalidConnection,
/// Missing or invalid (`Sec-WebSocket-Accept`) header.
#[error("Missing or invalid sec websocket accept header")]
MissingOrInvalidAccept,
/// Missing or invalid (`Sec-WebSocket-Version`) header.
#[error("Missing or invalid sec websocket version header")]
MissingOrInvalidSecVersion,
/// Missing (`Sec-WebSocket-Key`) header.
#[error("Missing sec websocket key header")]
MissingSecKey,
/// Other error.
///
/// User-defined error type.
#[error("Other: {0}")]
Other(E),
}
/// Fragmentation error.
#[derive(Debug, thiserror::Error)]
pub enum FragmentationError {
/// Fragment size is zero.
#[error("Fragment size must be greater than 0")]
InvalidFragmentSize,
/// Error indicating that a message type that cannot be fragmented was attempted to be fragmented.
///
/// Only text and binary messages can be fragmented.
#[error("Only text and binary messages can be fragmented")]
CanNotBeFragmented,
}
/// General WebSocket error type.
///
/// # Generic Parameters
/// `E`: User-defined error type for custom errors during the handshake.
#[derive(Debug, thiserror::Error)]
pub enum Error<I, E = Infallible> {
/// Error reading from the WebSocket connection.
#[error("Read error: {0}")]
Read(
#[from]
#[source]
ReadError<I>,
),
/// Error writing to the WebSocket connection.
#[error("Write error: {0}")]
Write(
#[from]
#[source]
WriteError<I>,
),
/// Handshake error.
#[error("Handshake error: {0}")]
Handshake(
#[from]
#[source]
HandshakeError<E>,
),
/// Fragmentation error.
#[error("Fragment error: {0}")]
Fragmentation(
#[from]
#[source]
FragmentationError,
),
}