hpx_fastwebsockets/close.rs
1// Mostly copied from https://github.com/snapview/tungstenite-rs/blob/42b8797e8b7f39efb7d9322dc8af3e9089db4f7d/src/protocol/frame/coding.rs#L117
2//
3// Copyright (c) 2017 Alexey Galakhov
4// Copyright (c) 2016 Jason Housley
5// Dual licensed under MIT and Apache 2.0
6// ---
7// Copyright 2023 Divy Srivastava <dj.srivastava23@gmail.com>
8//
9// Licensed under the Apache License, Version 2.0 (the "License");
10// you may not use this file except in compliance with the License.
11// You may obtain a copy of the License at
12//
13// http://www.apache.org/licenses/LICENSE-2.0
14//
15// Unless required by applicable law or agreed to in writing, software
16// distributed under the License is distributed on an "AS IS" BASIS,
17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18// See the License for the specific language governing permissions and
19// limitations under the License.
20
21use self::CloseCode::*;
22/// Status code used to indicate why an endpoint is closing the WebSocket connection.
23#[derive(Debug, Eq, PartialEq, Clone, Copy)]
24pub enum CloseCode {
25 /// Indicates a normal closure, meaning that the purpose for
26 /// which the connection was established has been fulfilled.
27 Normal,
28 /// Indicates that an endpoint is "going away", such as a server
29 /// going down or a browser having navigated away from a page.
30 Away,
31 /// Indicates that an endpoint is terminating the connection due
32 /// to a protocol error.
33 Protocol,
34 /// Indicates that an endpoint is terminating the connection
35 /// because it has received a type of data it cannot accept (e.g., an
36 /// endpoint that understands only text data MAY send this if it
37 /// receives a binary message).
38 Unsupported,
39 /// Indicates that no status code was included in a closing frame. This
40 /// close code makes it possible to use a single method, `on_close` to
41 /// handle even cases where no close code was provided.
42 Status,
43 /// Indicates an abnormal closure. If the abnormal closure was due to an
44 /// error, this close code will not be used. Instead, the `on_error` method
45 /// of the handler will be called with the error. However, if the connection
46 /// is simply dropped, without an error, this close code will be sent to the
47 /// handler.
48 Abnormal,
49 /// Indicates that an endpoint is terminating the connection
50 /// because it has received data within a message that was not
51 /// consistent with the type of the message (e.g., non-UTF-8 \[RFC3629\]
52 /// data within a text message).
53 Invalid,
54 /// Indicates that an endpoint is terminating the connection
55 /// because it has received a message that violates its policy. This
56 /// is a generic status code that can be returned when there is no
57 /// other more suitable status code (e.g., Unsupported or Size) or if there
58 /// is a need to hide specific details about the policy.
59 Policy,
60 /// Indicates that an endpoint is terminating the connection
61 /// because it has received a message that is too big for it to
62 /// process.
63 Size,
64 /// Indicates that an endpoint (client) is terminating the
65 /// connection because it has expected the server to negotiate one or
66 /// more extension, but the server didn't return them in the response
67 /// message of the WebSocket handshake. The list of extensions that
68 /// are needed should be given as the reason for closing.
69 /// Note that this status code is not used by the server, because it
70 /// can fail the WebSocket handshake instead.
71 Extension,
72 /// Indicates that a server is terminating the connection because
73 /// it encountered an unexpected condition that prevented it from
74 /// fulfilling the request.
75 Error,
76 /// Indicates that the server is restarting. A client may choose to reconnect,
77 /// and if it does, it should use a randomized delay of 5-30 seconds between attempts.
78 Restart,
79 /// Indicates that the server is overloaded and the client should either connect
80 /// to a different IP (when multiple targets exist), or reconnect to the same IP
81 /// when a user has performed an action.
82 Again,
83 #[doc(hidden)]
84 Tls,
85 #[doc(hidden)]
86 Reserved(u16),
87 #[doc(hidden)]
88 Iana(u16),
89 #[doc(hidden)]
90 Library(u16),
91 #[doc(hidden)]
92 Bad(u16),
93}
94
95impl CloseCode {
96 /// Check if this CloseCode is allowed.
97 pub fn is_allowed(self) -> bool {
98 !matches!(self, Bad(_) | Reserved(_) | Status | Abnormal | Tls)
99 }
100}
101
102impl From<u16> for CloseCode {
103 fn from(code: u16) -> CloseCode {
104 match code {
105 1000 => Normal,
106 1001 => Away,
107 1002 => Protocol,
108 1003 => Unsupported,
109 1005 => Status,
110 1006 => Abnormal,
111 1007 => Invalid,
112 1008 => Policy,
113 1009 => Size,
114 1010 => Extension,
115 1011 => Error,
116 1012 => Restart,
117 1013 => Again,
118 1015 => Tls,
119 1..=999 => Bad(code),
120 1016..=2999 => Reserved(code),
121 3000..=3999 => Iana(code),
122 4000..=4999 => Library(code),
123 _ => Bad(code),
124 }
125 }
126}
127
128impl From<CloseCode> for u16 {
129 fn from(code: CloseCode) -> u16 {
130 match code {
131 Normal => 1000,
132 Away => 1001,
133 Protocol => 1002,
134 Unsupported => 1003,
135 Status => 1005,
136 Abnormal => 1006,
137 Invalid => 1007,
138 Policy => 1008,
139 Size => 1009,
140 Extension => 1010,
141 Error => 1011,
142 Restart => 1012,
143 Again => 1013,
144 Tls => 1015,
145 Reserved(code) => code,
146 Iana(code) => code,
147 Library(code) => code,
148 Bad(code) => code,
149 }
150 }
151}