Skip to content

Commit 8624440

Browse files
committed
updated close code list and fixed close code validation
1 parent 2ef1559 commit 8624440

File tree

2 files changed

+90
-72
lines changed

2 files changed

+90
-72
lines changed

src/main/java/org/java_websocket/framing/CloseFrame.java

Lines changed: 82 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,89 @@
44
import org.java_websocket.exceptions.InvalidFrameException;
55

66
public interface CloseFrame extends Framedata {
7-
/**
8-
* indicates a normal closure, meaning whatever purpose the
9-
* connection was established for has been fulfilled.
10-
*/
11-
public static final int NORMAL = 1000;
12-
/**
13-
* 1001 indicates that an endpoint is "going away", such as a server
14-
* going down, or a browser having navigated away from a page.
15-
*/
16-
public static final int GOING_AWAY = 1001;
17-
/**
18-
* 1002 indicates that an endpoint is terminating the connection due
19-
* to a protocol error.
20-
*/
21-
public static final int PROTOCOL_ERROR = 1002;
22-
/**
23-
* 1003 indicates that an endpoint is terminating the connection
24-
* because it has received a type of data it cannot accept (e.g. an
25-
* endpoint that understands only text data MAY send this if it
26-
* receives a binary message).
27-
*/
28-
public static final int REFUSE = 1003;
29-
/*1004: Reserved. The specific meaning might be defined in the future.*/
30-
/**
31-
* 1005 is a reserved value and MUST NOT be set as a status code in a
32-
* Close control frame by an endpoint. It is designated for use in
33-
* applications expecting a status code to indicate that no status
34-
* code was actually present.
35-
*/
36-
public static final int NOCODE = 1005;
37-
/**
38-
* 1006 is a reserved value and MUST NOT be set as a status code in a
39-
* Close control frame by an endpoint. It is designated for use in
40-
* applications expecting a status code to indicate that the
41-
* connection was closed abnormally, e.g. without sending or
42-
* receiving a Close control frame.
43-
*/
44-
public static final int ABNORMAL_CLOSE = 1006;
45-
/**
46-
* 1007 indicates that an endpoint is terminating the connection
47-
* because it has received data within a message that was not
48-
* consistent with the type of the message (e.g., non-UTF-8 [RFC3629]
49-
* data within a text message).
50-
*/
51-
public static final int NO_UTF8 = 1007;
52-
/**
53-
* 1008 indicates that an endpoint is terminating the connection
54-
* because it has received a message that violates its policy. This
55-
* is a generic status code that can be returned when there is no
56-
* other more suitable status code (e.g. 1003 or 1009), or if there
57-
* is a need to hide specific details about the policy.
58-
*/
59-
public static final int POLICY_VALIDATION = 1008;
60-
/**
61-
* 1009 indicates that an endpoint is terminating the connection
62-
* because it has received a message which is too big for it to
63-
* process.
64-
*/
65-
public static final int TOOBIG = 1009;
66-
/**
67-
* 1010 indicates that an endpoint (client) is terminating the
68-
* connection because it has expected the server to negotiate one or
69-
* more extension, but the server didn't return them in the response
70-
* message of the WebSocket handshake. The list of extensions which
71-
* are needed SHOULD appear in the /reason/ part of the Close frame.
72-
* Note that this status code is not used by the server, because it
73-
* can fail the WebSocket handshake instead.
74-
*/
7+
/**
8+
* indicates a normal closure, meaning whatever purpose the
9+
* connection was established for has been fulfilled.
10+
*/
11+
public static final int NORMAL = 1000;
12+
/**
13+
* 1001 indicates that an endpoint is "going away", such as a server
14+
* going down, or a browser having navigated away from a page.
15+
*/
16+
public static final int GOING_AWAY = 1001;
17+
/**
18+
* 1002 indicates that an endpoint is terminating the connection due
19+
* to a protocol error.
20+
*/
21+
public static final int PROTOCOL_ERROR = 1002;
22+
/**
23+
* 1003 indicates that an endpoint is terminating the connection
24+
* because it has received a type of data it cannot accept (e.g. an
25+
* endpoint that understands only text data MAY send this if it
26+
* receives a binary message).
27+
*/
28+
public static final int REFUSE = 1003;
29+
/*1004: Reserved. The specific meaning might be defined in the future.*/
30+
/**
31+
* 1005 is a reserved value and MUST NOT be set as a status code in a
32+
* Close control frame by an endpoint. It is designated for use in
33+
* applications expecting a status code to indicate that no status
34+
* code was actually present.
35+
*/
36+
public static final int NOCODE = 1005;
37+
/**
38+
* 1006 is a reserved value and MUST NOT be set as a status code in a
39+
* Close control frame by an endpoint. It is designated for use in
40+
* applications expecting a status code to indicate that the
41+
* connection was closed abnormally, e.g. without sending or
42+
* receiving a Close control frame.
43+
*/
44+
public static final int ABNORMAL_CLOSE = 1006;
45+
/**
46+
* 1007 indicates that an endpoint is terminating the connection
47+
* because it has received data within a message that was not
48+
* consistent with the type of the message (e.g., non-UTF-8 [RFC3629]
49+
* data within a text message).
50+
*/
51+
public static final int NO_UTF8 = 1007;
52+
/**
53+
* 1008 indicates that an endpoint is terminating the connection
54+
* because it has received a message that violates its policy. This
55+
* is a generic status code that can be returned when there is no
56+
* other more suitable status code (e.g. 1003 or 1009), or if there
57+
* is a need to hide specific details about the policy.
58+
*/
59+
public static final int POLICY_VALIDATION = 1008;
60+
/**
61+
* 1009 indicates that an endpoint is terminating the connection
62+
* because it has received a message which is too big for it to
63+
* process.
64+
*/
65+
public static final int TOOBIG = 1009;
66+
/**
67+
* 1010 indicates that an endpoint (client) is terminating the
68+
* connection because it has expected the server to negotiate one or
69+
* more extension, but the server didn't return them in the response
70+
* message of the WebSocket handshake. The list of extensions which
71+
* are needed SHOULD appear in the /reason/ part of the Close frame.
72+
* Note that this status code is not used by the server, because it
73+
* can fail the WebSocket handshake instead.
74+
*/
7575
public static final int EXTENSION = 1010;
76+
/**
77+
* 1011 indicates that a server is terminating the connection because
78+
* it encountered an unexpected condition that prevented it from
79+
* fulfilling the request.
80+
**/
81+
public static final int UNEXPECTED_CONDITION = 1011;
82+
/**
83+
* 1015 is a reserved value and MUST NOT be set as a status code in a
84+
* Close control frame by an endpoint. It is designated for use in
85+
* applications expecting a status code to indicate that the
86+
* connection was closed due to a failure to perform a TLS handshake
87+
* (e.g., the server certificate can't be verified).
88+
**/
89+
public static final int TLS_ERROR = 1015;
7690

7791
/** The connection had never been established */
7892
public static final int NEVERCONNECTED = -1;

src/main/java/org/java_websocket/framing/CloseFrameBuilder.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ private void setCodeAndMessage( int code, String m ) throws InvalidDataException
3434
if( m == null ) {
3535
m = "";
3636
}
37+
// CloseFrame.TLS_ERROR is not allowed to be transfered over the wire
38+
if( code == CloseFrame.TLS_ERROR ) {
39+
code = CloseFrame.NOCODE;
40+
m = "";
41+
}
3742
if( code == CloseFrame.NOCODE ) {
3843
if( !m.isEmpty() ) {
3944
throw new InvalidDataException( PROTOCOL_ERROR, "A close frame must have a closecode if it has a reason" );
@@ -62,10 +67,9 @@ private void initCloseCode() throws InvalidFrameException {
6267
bb.putShort( payload.getShort() );
6368
bb.position( 0 );
6469
code = bb.getInt();
65-
if( code < 0 || code > Short.MAX_VALUE )
66-
code = CloseFrame.NOCODE;
67-
if( code < CloseFrame.NORMAL || code > CloseFrame.EXTENSION || code == NOCODE || code == 1004 ) {
68-
throw new InvalidFrameException( "bad code " + code );
70+
71+
if( code == CloseFrame.ABNORMAL_CLOSE || code == CloseFrame.TLS_ERROR || code == CloseFrame.NOCODE || code > 4999 ) {
72+
throw new InvalidFrameException( "closecode must not be sent over the wire " + code );
6973
}
7074
}
7175
payload.reset();

0 commit comments

Comments
 (0)