Skip to content

Commit

Permalink
Cleanups to WebSocket CloseStatus
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Nov 9, 2020
1 parent 448d700 commit d62831f
Showing 1 changed file with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ public class CloseStatus
public static final int SHUTDOWN = 1001;
public static final int PROTOCOL = 1002;
public static final int BAD_DATA = 1003;
public static final int RESERVED = 1004;
public static final int NO_CODE = 1005;
public static final int NO_CLOSE = 1006;
public static final int BAD_PAYLOAD = 1007;
public static final int POLICY_VIOLATION = 1008;
public static final int MESSAGE_TOO_LARGE = 1009;
public static final int EXTENSION_ERROR = 1010;
public static final int SERVER_ERROR = 1011;
public static final int SERVICE_RESTART = 1012;
public static final int TRY_AGAIN_LATER = 1013;
public static final int BAD_GATEWAY = 1014;
public static final int FAILED_TLS_HANDSHAKE = 1015;

public static final CloseStatus NO_CODE_STATUS = new CloseStatus(NO_CODE);
Expand Down Expand Up @@ -189,7 +193,6 @@ public CloseStatus(ByteBuffer payload)

this.code = statusCode;
this.reason = null;
return;
}

public static CloseStatus getCloseStatus(Frame frame)
Expand Down Expand Up @@ -244,8 +247,7 @@ public static ByteBuffer asPayloadBuffer(int statusCode, String reason)
{
byte[] utf8Bytes = reason.getBytes(StandardCharsets.UTF_8);
reasonBytes = truncateToFit(utf8Bytes, CloseStatus.MAX_REASON_PHRASE);

if ((reasonBytes != null) && (reasonBytes.length > 0))
if (reasonBytes.length > 0)
len += reasonBytes.length;
}

Expand Down Expand Up @@ -291,27 +293,13 @@ private static byte[] truncateToFit(byte[] bytes, int maxBytes)
*/
public static boolean isTransmittableStatusCode(int statusCode)
{
// Outside of range?
if ((statusCode <= 999) || (statusCode >= 5000))
{
return false;
}

// Specifically called out as not-transmittable?
if ((statusCode == NO_CODE) ||
(statusCode == NO_CLOSE) ||
(statusCode == FAILED_TLS_HANDSHAKE))
{
return false;
}

// Reserved / not yet allocated
// RFC6455 Not allowed to be used for any purpose
return (statusCode != 1004) && // Reserved in RFC6455 (might be defined in the future)
((statusCode < 1016) || (statusCode > 2999)) && // Reserved in RFC6455 (for future revisions, and extensions)
(statusCode < 5000);
// Transmittable status codes pre-defined by RFC6455 and IANA.
if ((statusCode >= 1000 && statusCode <= 1003) || (statusCode >= 1007 && statusCode <= 1014))
return true;

// All others are allowed
// Codes 3000-3999 reserved for libraries, frameworks, and applications.
// Codes 4000-4999 reserved for private use.
return statusCode >= 3000 && statusCode < 5000;
}

public Frame toFrame()
Expand Down Expand Up @@ -343,6 +331,8 @@ public static String codeString(int closeStatus)
return "PROTOCOL";
case BAD_DATA:
return "BAD_DATA";
case RESERVED:
return "RESERVED";
case NO_CODE:
return "NO_CODE";
case NO_CLOSE:
Expand All @@ -357,6 +347,12 @@ public static String codeString(int closeStatus)
return "EXTENSION_ERROR";
case SERVER_ERROR:
return "SERVER_ERROR";
case SERVICE_RESTART:
return "SERVICE_RESTART";
case TRY_AGAIN_LATER:
return "TRY_AGAIN_LATER";
case BAD_GATEWAY:
return "BAD_GATEWAY";
case FAILED_TLS_HANDSHAKE:
return "FAILED_TLS_HANDSHAKE";
default:
Expand Down

0 comments on commit d62831f

Please sign in to comment.