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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use std::{io, result, str, string};
use crate::protocol::{frame::coding::Data, Message};
use http::Response;
use thiserror::Error;
pub type Result<T> = result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
#[error("Connection closed normally")]
ConnectionClosed,
#[error("Trying to work with closed connection")]
AlreadyClosed,
#[error("IO error: {0}")]
Io(#[from] io::Error),
#[error("TLS error: {0}")]
Tls(#[from] TlsError),
#[error("Space limit exceeded: {0}")]
Capacity(CapacityError),
#[error("WebSocket protocol error: {0}")]
Protocol(ProtocolError),
#[error("Send queue is full")]
SendQueueFull(Message),
#[error("UTF-8 encoding error")]
Utf8,
#[error("URL error: {0}")]
Url(UrlError),
#[error("HTTP error: {}", .0.status())]
Http(Response<Option<String>>),
#[error("HTTP format error: {0}")]
HttpFormat(#[from] http::Error),
}
impl From<str::Utf8Error> for Error {
fn from(_: str::Utf8Error) -> Self {
Error::Utf8
}
}
impl From<string::FromUtf8Error> for Error {
fn from(_: string::FromUtf8Error) -> Self {
Error::Utf8
}
}
impl From<http::header::InvalidHeaderValue> for Error {
fn from(err: http::header::InvalidHeaderValue) -> Self {
Error::HttpFormat(err.into())
}
}
impl From<http::header::InvalidHeaderName> for Error {
fn from(err: http::header::InvalidHeaderName) -> Self {
Error::HttpFormat(err.into())
}
}
impl From<http::header::ToStrError> for Error {
fn from(_: http::header::ToStrError) -> Self {
Error::Utf8
}
}
impl From<http::uri::InvalidUri> for Error {
fn from(err: http::uri::InvalidUri) -> Self {
Error::HttpFormat(err.into())
}
}
impl From<http::status::InvalidStatusCode> for Error {
fn from(err: http::status::InvalidStatusCode) -> Self {
Error::HttpFormat(err.into())
}
}
impl From<httparse::Error> for Error {
fn from(err: httparse::Error) -> Self {
match err {
httparse::Error::TooManyHeaders => Error::Capacity(CapacityError::TooManyHeaders),
e => Error::Protocol(ProtocolError::HttparseError(e)),
}
}
}
#[derive(Error, Debug, PartialEq, Eq, Clone, Copy)]
pub enum CapacityError {
#[error("Too many headers")]
TooManyHeaders,
#[error("Header too long")]
HeaderTooLong,
#[error("Message too long: {size} > {max_size}")]
MessageTooLong {
size: usize,
max_size: usize,
},
#[error("Incoming TCP buffer is full")]
TcpBufferFull,
}
#[derive(Error, Debug, PartialEq, Eq, Clone, Copy)]
pub enum ProtocolError {
#[error("Unsupported HTTP method used - only GET is allowed")]
WrongHttpMethod,
#[error("HTTP version must be 1.1 or higher")]
WrongHttpVersion,
#[error("No \"Connection: upgrade\" header")]
MissingConnectionUpgradeHeader,
#[error("No \"Upgrade: websocket\" header")]
MissingUpgradeWebSocketHeader,
#[error("No \"Sec-WebSocket-Version: 13\" header")]
MissingSecWebSocketVersionHeader,
#[error("No \"Sec-WebSocket-Key\" header")]
MissingSecWebSocketKey,
#[error("Key mismatch in \"Sec-WebSocket-Accept\" header")]
SecWebSocketAcceptKeyMismatch,
#[error("Junk after client request")]
JunkAfterRequest,
#[error("Custom response must not be successful")]
CustomResponseSuccessful,
#[error("Handshake not finished")]
HandshakeIncomplete,
#[error("httparse error: {0}")]
HttparseError(#[from] httparse::Error),
#[error("Sending after closing is not allowed")]
SendAfterClosing,
#[error("Remote sent after having closed")]
ReceivedAfterClosing,
#[error("Reserved bits are non-zero")]
NonZeroReservedBits,
#[error("Received an unmasked frame from client")]
UnmaskedFrameFromClient,
#[error("Received a masked frame from server")]
MaskedFrameFromServer,
#[error("Fragmented control frame")]
FragmentedControlFrame,
#[error("Control frame too big (payload must be 125 bytes or less)")]
ControlFrameTooBig,
#[error("Unknown control frame type: {0}")]
UnknownControlFrameType(u8),
#[error("Unknown data frame type: {0}")]
UnknownDataFrameType(u8),
#[error("Continue frame but nothing to continue")]
UnexpectedContinueFrame,
#[error("While waiting for more fragments received: {0}")]
ExpectedFragment(Data),
#[error("Connection reset without closing handshake")]
ResetWithoutClosingHandshake,
#[error("Encountered invalid opcode: {0}")]
InvalidOpcode(u8),
#[error("Invalid close sequence")]
InvalidCloseSequence,
}
#[derive(Error, Debug, PartialEq, Eq)]
pub enum UrlError {
#[error("TLS support not compiled in")]
TlsFeatureNotEnabled,
#[error("No host name in the URL")]
NoHostName,
#[error("Unable to connect to {0}")]
UnableToConnect(String),
#[error("URL scheme not supported")]
UnsupportedUrlScheme,
#[error("URL contains empty host name")]
EmptyHostName,
#[error("No path/query in URL")]
NoPathOrQuery,
}
#[allow(missing_copy_implementations)]
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum TlsError {
#[cfg(feature = "native-tls")]
#[error("native-tls error: {0}")]
Native(#[from] native_tls_crate::Error),
#[cfg(feature = "rustls-tls")]
#[error("rustls error: {0}")]
Rustls(#[from] rustls::TLSError),
#[cfg(feature = "rustls-tls")]
#[error("Invalid DNS name: {0}")]
Dns(#[from] webpki::InvalidDNSNameError),
}