Cleanups to WebSocket CloseStatus

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-11-09 19:46:08 +11:00
parent 448d700f5c
commit d62831fd6d
1 changed files with 19 additions and 23 deletions

View File

@ -38,6 +38,7 @@ public class CloseStatus
public static final int SHUTDOWN = 1001; public static final int SHUTDOWN = 1001;
public static final int PROTOCOL = 1002; public static final int PROTOCOL = 1002;
public static final int BAD_DATA = 1003; 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_CODE = 1005;
public static final int NO_CLOSE = 1006; public static final int NO_CLOSE = 1006;
public static final int BAD_PAYLOAD = 1007; public static final int BAD_PAYLOAD = 1007;
@ -45,6 +46,9 @@ public class CloseStatus
public static final int MESSAGE_TOO_LARGE = 1009; public static final int MESSAGE_TOO_LARGE = 1009;
public static final int EXTENSION_ERROR = 1010; public static final int EXTENSION_ERROR = 1010;
public static final int SERVER_ERROR = 1011; 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 int FAILED_TLS_HANDSHAKE = 1015;
public static final CloseStatus NO_CODE_STATUS = new CloseStatus(NO_CODE); public static final CloseStatus NO_CODE_STATUS = new CloseStatus(NO_CODE);
@ -189,7 +193,6 @@ public class CloseStatus
this.code = statusCode; this.code = statusCode;
this.reason = null; this.reason = null;
return;
} }
public static CloseStatus getCloseStatus(Frame frame) public static CloseStatus getCloseStatus(Frame frame)
@ -244,8 +247,7 @@ public class CloseStatus
{ {
byte[] utf8Bytes = reason.getBytes(StandardCharsets.UTF_8); byte[] utf8Bytes = reason.getBytes(StandardCharsets.UTF_8);
reasonBytes = truncateToFit(utf8Bytes, CloseStatus.MAX_REASON_PHRASE); reasonBytes = truncateToFit(utf8Bytes, CloseStatus.MAX_REASON_PHRASE);
if (reasonBytes.length > 0)
if ((reasonBytes != null) && (reasonBytes.length > 0))
len += reasonBytes.length; len += reasonBytes.length;
} }
@ -291,27 +293,13 @@ public class CloseStatus
*/ */
public static boolean isTransmittableStatusCode(int statusCode) public static boolean isTransmittableStatusCode(int statusCode)
{ {
// Outside of range? // Transmittable status codes pre-defined by RFC6455 and IANA.
if ((statusCode <= 999) || (statusCode >= 5000)) if ((statusCode >= 1000 && statusCode <= 1003) || (statusCode >= 1007 && statusCode <= 1014))
{ return true;
return false;
}
// Specifically called out as not-transmittable? // Codes 3000-3999 reserved for libraries, frameworks, and applications.
if ((statusCode == NO_CODE) || // Codes 4000-4999 reserved for private use.
(statusCode == NO_CLOSE) || return statusCode >= 3000 && statusCode < 5000;
(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);
// All others are allowed
} }
public Frame toFrame() public Frame toFrame()
@ -343,6 +331,8 @@ public class CloseStatus
return "PROTOCOL"; return "PROTOCOL";
case BAD_DATA: case BAD_DATA:
return "BAD_DATA"; return "BAD_DATA";
case RESERVED:
return "RESERVED";
case NO_CODE: case NO_CODE:
return "NO_CODE"; return "NO_CODE";
case NO_CLOSE: case NO_CLOSE:
@ -357,6 +347,12 @@ public class CloseStatus
return "EXTENSION_ERROR"; return "EXTENSION_ERROR";
case SERVER_ERROR: case SERVER_ERROR:
return "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: case FAILED_TLS_HANDSHAKE:
return "FAILED_TLS_HANDSHAKE"; return "FAILED_TLS_HANDSHAKE";
default: default: