Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for close code 1012-1014 #651

Merged
merged 2 commits into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/main/java/org/java_websocket/framing/CloseFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,31 @@ public class CloseFrame extends ControlFrame {
* fulfilling the request.
**/
public static final int UNEXPECTED_CONDITION = 1011;
/**
* 1012 indicates that the service is restarted.
* A client may reconnect, and if it choses to do, should reconnect using a randomized delay of 5 - 30s.
* See https://www.ietf.org/mail-archive/web/hybi/current/msg09670.html for more information.
*
* @since 1.3.8
**/
public static final int SERVICE_RESTART = 1012;
/**
* 1013 indicates that the service is experiencing overload.
* A client should only connect to a different IP (when there are multiple for the target)
* or reconnect to the same IP upon user action.
* See https://www.ietf.org/mail-archive/web/hybi/current/msg09670.html for more information.
*
* @since 1.3.8
**/
public static final int TRY_AGAIN_LATER = 1013;
/**
* 1014 indicates that the server was acting as a gateway or proxy and received an
* invalid response from the upstream server. This is similar to 502 HTTP Status Code
* See https://www.ietf.org/mail-archive/web/hybi/current/msg10748.html fore more information.
*
* @since 1.3.8
**/
public static final int BAD_GATEWAY = 1014;
/**
* 1015 is a reserved value and MUST NOT be set as a status code in a
* Close control frame by an endpoint. It is designated for use in
Expand Down Expand Up @@ -216,7 +241,7 @@ public void isValid() throws InvalidDataException {
throw new InvalidDataException(PROTOCOL_ERROR, "A close frame must have a closecode if it has a reason");
}
//Intentional check for code != CloseFrame.TLS_ERROR just to make sure even if the code earlier changes
if ((code > CloseFrame.UNEXPECTED_CONDITION && code < 3000 && code != CloseFrame.TLS_ERROR)) {
if ((code > CloseFrame.TLS_ERROR && code < 3000)) {
throw new InvalidDataException(PROTOCOL_ERROR, "Trying to send an illegal close code!");
}
if (code == CloseFrame.ABNORMAL_CLOSE || code == CloseFrame.TLS_ERROR || code == CloseFrame.NOCODE || code > 4999 || code < 1000 || code == 1004) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1970,15 +1970,7 @@ public void test7_9_5() {
assertEquals( "OK", testResult.get( "behaviorClose" ) );
Assume.assumeTrue("Duration: " + testResult.getInt( "duration" ), testResult.getInt( "duration" ) < 10 );
}

@Test
public void test7_9_6() {
JSONObject testResult = jsonObject.getJSONObject( "7.9.6" );
assertEquals( "OK", testResult.get( "behavior" ) );
assertEquals( "OK", testResult.get( "behaviorClose" ) );
Assume.assumeTrue("Duration: " + testResult.getInt( "duration" ), testResult.getInt( "duration" ) < 10 );
}


@Test
public void test7_9_7() {
JSONObject testResult = jsonObject.getJSONObject( "7.9.7" );
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/java_websocket/framing/CloseFrameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ public void testIsValid() {
} catch (InvalidDataException e) {
fail("InvalidDataException should not be thrown");
}
frame.setCode(CloseFrame.SERVICE_RESTART);
try {
frame.isValid();
} catch (InvalidDataException e) {
fail("InvalidDataException should not be thrown");
}
frame.setCode(CloseFrame.TRY_AGAIN_LATER);
try {
frame.isValid();
} catch (InvalidDataException e) {
fail("InvalidDataException should not be thrown");
}
frame.setCode(CloseFrame.BAD_GATEWAY);
try {
frame.isValid();
} catch (InvalidDataException e) {
fail("InvalidDataException should not be thrown");
}
frame.setCode(CloseFrame.TLS_ERROR);
try {
frame.isValid();
Expand Down