001/*
002 * Copyright (C) 2015-2017 Neo Visionaries Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package NeoVisionaries.WebSockets;
017
018
019/**
020 * WebSocket error codes.
021 * 
022 * <EMBED CLASS='external-html' DATA-FILE-ID=LICENSE>
023 *
024 * @see WebSocketException#getError()
025 */
026public enum WebSocketError
027{
028    /**
029     * The current state of the WebSocket is not CREATED.
030     *
031     * <p>
032     * This error occurs if {@link WebSocket#connect()} is called
033     * when the state of the WebSocket is not {@link
034     * WebSocketState#CREATED CREATED}.
035     * </p>
036     */
037    NOT_IN_CREATED_STATE,
038
039
040    /**
041     * Failed to get the input stream of the raw socket.
042     */
043    SOCKET_INPUT_STREAM_FAILURE,
044
045
046    /**
047     * Failed to get the output stream of the raw socket.
048     */
049    SOCKET_OUTPUT_STREAM_FAILURE,
050
051
052    /**
053     * Failed to send an opening handshake request to the server.
054     */
055    OPENING_HAHDSHAKE_REQUEST_FAILURE,
056
057
058    /**
059     * Failed to read an opening handshake response from the server.
060     */
061    OPENING_HANDSHAKE_RESPONSE_FAILURE,
062
063
064    /**
065     * The status line of the opening handshake response is empty.
066     */
067    STATUS_LINE_EMPTY,
068
069
070    /**
071     * The status line of the opening handshake response is badly formatted.
072     */
073    STATUS_LINE_BAD_FORMAT,
074
075
076    /**
077     * The status code of the opening handshake response is not {@code 101 Switching Protocols}.
078     */
079    NOT_SWITCHING_PROTOCOLS,
080
081
082    /**
083     * An error occurred while HTTP header section was being read.
084     */
085    HTTP_HEADER_FAILURE,
086
087
088    /**
089     * The opening handshake response does not contain {@code Upgrade} header.
090     */
091    NO_UPGRADE_HEADER,
092
093
094    /**
095     * {@code websocket} was not found in {@code Upgrade} header.
096     */
097    NO_WEBSOCKET_IN_UPGRADE_HEADER,
098
099
100    /**
101     * The opening handshake response does not contain {@code Connection} header.
102     */
103    NO_CONNECTION_HEADER,
104
105
106    /**
107     * {@code Upgrade} was not found in {@code Connection} header.
108     */
109    NO_UPGRADE_IN_CONNECTION_HEADER,
110
111
112    /**
113     * The opening handshake response does not contain {@code Sec-WebSocket-Accept} header.
114     */
115    NO_SEC_WEBSOCKET_ACCEPT_HEADER,
116
117
118    /**
119     * The value of {@code Sec-WebSocket-Accept} header is different from the expected one.
120     */
121    UNEXPECTED_SEC_WEBSOCKET_ACCEPT_HEADER,
122
123
124    /**
125     * The value in {@code Sec-WebSocket-Extensions} failed to be parsed.
126     */
127    EXTENSION_PARSE_ERROR,
128
129
130    /**
131     * The extension contained in {@code Sec-WebSocket-Extensions} header is not supported.
132     */
133    UNSUPPORTED_EXTENSION,
134
135
136    /**
137     * The combination of the extensions contained in {@code Sec-WebSocket-Extensions} header
138     * causes conflicts.
139     *
140     * @since 1.15
141     */
142    EXTENSIONS_CONFLICT,
143
144
145    /**
146     * The protocol contained in {@code Sec-WebSocket-Protocol} header is not supported.
147     */
148    UNSUPPORTED_PROTOCOL,
149
150
151    /**
152     * The end of the stream has been reached unexpectedly.
153     */
154    INSUFFICENT_DATA,
155
156
157    /**
158     * The payload length of a frame is invalid.
159     */
160    INVALID_PAYLOAD_LENGTH,
161
162
163    /**
164     * The payload length of a frame exceeds the maximum array size in Java.
165     */
166    TOO_LONG_PAYLOAD,
167
168
169    /**
170     * {@link OutOfMemoryError} occurred during a trial to allocate a memory area for a frame's payload.
171     */
172    INSUFFICIENT_MEMORY_FOR_PAYLOAD,
173
174
175    /**
176     * Interruption occurred while a frame was being read from the WebSocket.
177     */
178    INTERRUPTED_IN_READING,
179
180
181    /**
182     * An I/O error occurred while a frame was being read from the WebSocket.
183     */
184    IO_ERROR_IN_READING,
185
186
187    /**
188     * An I/O error occurred when a frame was tried to be sent.
189     */
190    IO_ERROR_IN_WRITING,
191
192
193    /**
194     * Flushing frames to the server failed.
195     */
196    FLUSH_ERROR,
197
198
199    /**
200     * At least one of the reserved bits of a frame is set.
201     *
202     * <blockquote>
203     * <p>From RFC 6455, <a href="http://tools.ietf.org/html/rfc6455#section-5.2"
204     * >5.2 Base Framing Protocol</a>; RSV1, RSV2, RSV3:</p>
205     * <p><i>
206     * MUST be 0 unless an extension is negotiated that defines meanings
207     * for non-zero values.  If a nonzero value is received and none of
208     * the negotiated extensions defines the meaning of such a nonzero
209     * value, the receiving endpoint MUST Fail the WebSocket Connection.
210     * </i></p>
211     * </blockquote>
212     *
213     * <p>
214     * By calling {@link WebSocket#setExtended(boolean) WebSocket.setExtended}{@code
215     * (true)}, you can skip the validity check of the RSV1/RSV2/RSV3 bits.
216     * </p>
217     *
218     * <p>
219     * This error code is not used in version 1.15 and after.
220     * </p>
221     */
222    NON_ZERO_RESERVED_BITS,
223
224
225    /**
226     * A reserved bit of a frame has an unexpected value.
227     *
228     * <blockquote>
229     * <p>From RFC 6455, <a href="http://tools.ietf.org/html/rfc6455#section-5.2"
230     * >5.2 Base Framing Protocol</a>; RSV1, RSV2, RSV3:</p>
231     * <p><i>
232     * MUST be 0 unless an extension is negotiated that defines meanings
233     * for non-zero values.  If a nonzero value is received and none of
234     * the negotiated extensions defines the meaning of such a nonzero
235     * value, the receiving endpoint MUST Fail the WebSocket Connection.
236     * </i></p>
237     * </blockquote>
238     *
239     * <p>
240     * By calling {@link WebSocket#setExtended(boolean) WebSocket.setExtended}{@code
241     * (true)}, you can skip the validity check of the RSV1/RSV2/RSV3 bits.
242     * </p>
243     *
244     * @since 1.15
245     */
246    UNEXPECTED_RESERVED_BIT,
247
248
249    /**
250     * A frame from the server is masked.
251     *
252     * <blockquote>
253     * <p>From RFC 6455, <a href="http://tools.ietf.org/html/rfc6455#section-5.1"
254     * >5.1. Overview</a>:</p>
255     * <p><i>
256     * A server MUST NOT mask any frames that it sends to the client.
257     * A client MUST close a connection if it detects a masked frame.
258     * </i></p>
259     * </blockquote>
260     */
261    FRAME_MASKED,
262
263
264    /**
265     * A frame has an unknown opcode.
266     *
267     * <p>
268     * By calling {@link WebSocket#setExtended(boolean) WebSocket.setExtended}{@code
269     * (true)}, you can accept frames which have an unknown opcode.
270     * </p>
271     */
272    UNKNOWN_OPCODE,
273
274
275    /**
276     * A control frame is fragmented.
277     *
278     * <blockquote>
279     * <p>From RFC 6455, <a href="http://tools.ietf.org/html/rfc6455#section-5.4"
280     * >5.4. Fragmentation</a>:</p>
281     * <p><i>
282     * Control frames (see Section 5.5) MAY be injected in the middle of
283     * a fragmented message.  Control frames themselves MUST NOT be fragmented.
284     * </i></p>
285     * </blockquote>
286     */
287    FRAGMENTED_CONTROL_FRAME,
288
289
290    /**
291     * A continuation frame was detected although a continuation had not started.
292     */
293    UNEXPECTED_CONTINUATION_FRAME,
294
295
296    /**
297     * A non-control frame was detected although the existing continuation had not been closed.
298     */
299    CONTINUATION_NOT_CLOSED,
300
301
302    /**
303     * The payload size of a control frame exceeds the maximum size (125 bytes).
304     *
305     * <blockquote>
306     * <p>From RFC 6455, <a href="http://tools.ietf.org/html/rfc6455#section-5.5"
307     * >5.5. Control Frames</a>:</p>
308     * <p><i>
309     * All control frames MUST have a payload length of 125 bytes or less and
310     * MUST NOT be fragmented.
311     * </i></p>
312     * </blockquote>
313     */
314    TOO_LONG_CONTROL_FRAME_PAYLOAD,
315
316
317    /**
318     * Failed to concatenate payloads of multiple frames to construct a message.
319     */
320    MESSAGE_CONSTRUCTION_ERROR,
321
322
323    /**
324     * Failed to convert payload data into a string.
325     */
326    TEXT_MESSAGE_CONSTRUCTION_ERROR,
327
328
329    /**
330     * An uncaught throwable was detected in the reading thread (which reads
331     * frames from the server).
332     */
333    UNEXPECTED_ERROR_IN_READING_THREAD,
334
335
336    /**
337     * An uncaught throwable was detected in the writing thread (which sends
338     * frames to the server).
339     */
340    UNEXPECTED_ERROR_IN_WRITING_THREAD,
341
342
343    /**
344     * {@code permessage-deflate} extension contains an unsupported parameter.
345     *
346     * <p>
347     * See <a href="https://tools.ietf.org/html/rfc7692#section-7">7. The
348     * "permessage-deflate" Extension</a> in
349     * <a href="https://tools.ietf.org/html/rfc7692">RFC 7692</a> for details.
350     * </p>
351     *
352     * @since 1.15
353     */
354    PERMESSAGE_DEFLATE_UNSUPPORTED_PARAMETER,
355
356
357    /**
358     * The value of {@code server_max_window_bits} parameter or {@code
359     * client_max_window_bits} parameter of {@code permessage-deflate}
360     * extension is invalid.
361     *
362     * <p>
363     * See <a href="https://tools.ietf.org/html/rfc7692#section-7.1.2">7.1.2.
364     * Limiting the LZ77 Sliding Window Size</a> in
365     * <a href="https://tools.ietf.org/html/rfc7692">RFC 7692</a> for details.
366     * </p>
367     *
368     * @since 1.15
369     */
370    PERMESSAGE_DEFLATE_INVALID_MAX_WINDOW_BITS,
371
372
373    /**
374     * Compression failed.
375     *
376     * @since 1.17
377     */
378    COMPRESSION_ERROR,
379
380
381    /**
382     * Decompression failed.
383     *
384     * @since 1.16
385     */
386    DECOMPRESSION_ERROR,
387
388
389    /**
390     * {@link java.net.Socket#connect(java.net.SocketAddress, int)
391     * Socket.connect()} failed.
392     *
393     * @since 1.20
394     */
395    SOCKET_CONNECT_ERROR,
396
397
398    /**
399     * Handshake with a proxy server failed.
400     *
401     * @since 1.20
402     */
403    PROXY_HANDSHAKE_ERROR,
404
405
406    /**
407     * Failed to overlay an existing socket.
408     *
409     * @since 1.20
410     */
411    SOCKET_OVERLAY_ERROR,
412
413
414    /**
415     * SSL handshake with a WebSocket endpoint failed.
416     *
417     * @since 1.20
418     */
419    SSL_HANDSHAKE_ERROR,
420
421
422    /**
423     * No more frame can be read because the end of the input stream has been reached.
424     *
425     * <p>
426     * This happens when the WebSocket connection is closed without receiving a
427     * <a href="https://tools.ietf.org/html/rfc6455#section-5.5.1">close frame</a>
428     * from the WebSocket server. Strictly speaking, it is a violation against
429     * <a href="https://tools.ietf.org/html/rfc6455">RFC 6455</a>, but it seems some
430     * server implementations sometimes close a connection without sending a close
431     * frame.
432     * </p>
433     *
434     * @since 1.29
435     */
436    NO_MORE_FRAME,
437
438
439    /**
440     * The certificate of the peer does not match the expected hostname.
441     *
442     * <p>
443     * When {@link WebSocketException#getError()} returns this error code, the
444     * {@link WebSocketException} can be cast to {@link HostnameUnverifiedException}
445     * through which you can get the
446     * </p>
447     *
448     * <p>
449     * See <a href='https://github.com/TakahikoKawasaki/nv-websocket-client/pull/107'
450     * >Verify that certificate is valid for server hostname (#107)</a>.
451     * </p>
452     *
453     * @since 2.1
454     */
455    HOSTNAME_UNVERIFIED,
456    ;
457}