From 9b17b7482bf1a97cbe1d63a4d934410aec08ea3d Mon Sep 17 00:00:00 2001 From: "paula.zachacz" Date: Tue, 28 Nov 2023 15:22:19 +0100 Subject: [PATCH 1/2] Support other http error codes --- .../org/java_websocket/WebSocketImpl.java | 9 ++++- .../exceptions/InvalidDataException.java | 35 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/java_websocket/WebSocketImpl.java b/src/main/java/org/java_websocket/WebSocketImpl.java index aad172127..478016c6c 100644 --- a/src/main/java/org/java_websocket/WebSocketImpl.java +++ b/src/main/java/org/java_websocket/WebSocketImpl.java @@ -428,7 +428,11 @@ private void decodeFrames(ByteBuffer socketBuffer) { * @param exception the InvalidDataException causing this problem */ private void closeConnectionDueToWrongHandshake(InvalidDataException exception) { - write(generateHttpResponseDueToError(404)); + if (exception.getHttpErrorCode() != null) { + write(generateHttpResponseDueToError(exception.getHttpErrorCode())); + } else { + write(generateHttpResponseDueToError(404)); + } flushAndClose(exception.getCloseCode(), exception.getMessage(), false); } @@ -451,6 +455,9 @@ private void closeConnectionDueToInternalServerError(RuntimeException exception) private ByteBuffer generateHttpResponseDueToError(int errorCode) { String errorCodeDescription; switch (errorCode) { + case 401: + errorCodeDescription = "401 WebSocket Authentication Failure"; + break; case 404: errorCodeDescription = "404 WebSocket Upgrade Failure"; break; diff --git a/src/main/java/org/java_websocket/exceptions/InvalidDataException.java b/src/main/java/org/java_websocket/exceptions/InvalidDataException.java index c34c8c941..3419d4b40 100644 --- a/src/main/java/org/java_websocket/exceptions/InvalidDataException.java +++ b/src/main/java/org/java_websocket/exceptions/InvalidDataException.java @@ -40,6 +40,8 @@ public class InvalidDataException extends Exception { */ private final int closecode; + private Integer httpErrorCode; + /** * constructor for a InvalidDataException * @@ -49,6 +51,17 @@ public InvalidDataException(int closecode) { this.closecode = closecode; } + /** + * constructor for a InvalidDataException + * + * @param closecode the closecode which will be returned + * @param httpErrorCode the httpErrorCode which will be returned. + */ + public InvalidDataException(int closecode, int httpErrorCode) { + this.closecode = closecode; + this.httpErrorCode = httpErrorCode; + } + /** * constructor for a InvalidDataException. * @@ -60,6 +73,19 @@ public InvalidDataException(int closecode, String s) { this.closecode = closecode; } + /** + * constructor for a InvalidDataException. + * + * @param closecode the closecode which will be returned. + * @param s the detail message. + * @param httpErrorCode the httpErrorCode which will be returned. + */ + public InvalidDataException(int closecode, String s, int httpErrorCode) { + super(s); + this.closecode = closecode; + this.httpErrorCode = httpErrorCode; + } + /** * constructor for a InvalidDataException. * @@ -92,4 +118,13 @@ public int getCloseCode() { return closecode; } + /** + * Getter httpErrorCode + * + * @return the httpErrorCode + */ + public Integer getHttpErrorCode() { + return httpErrorCode; + } + } From 6d3c69b7422766742c67b6492131739a55532df0 Mon Sep 17 00:00:00 2001 From: "paula.zachacz" Date: Tue, 28 Nov 2023 17:32:58 +0100 Subject: [PATCH 2/2] update error description on comment --- src/main/java/org/java_websocket/WebSocketImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/java_websocket/WebSocketImpl.java b/src/main/java/org/java_websocket/WebSocketImpl.java index 478016c6c..37523859c 100644 --- a/src/main/java/org/java_websocket/WebSocketImpl.java +++ b/src/main/java/org/java_websocket/WebSocketImpl.java @@ -456,7 +456,7 @@ private ByteBuffer generateHttpResponseDueToError(int errorCode) { String errorCodeDescription; switch (errorCode) { case 401: - errorCodeDescription = "401 WebSocket Authentication Failure"; + errorCodeDescription = "401 Authorization Required"; break; case 404: errorCodeDescription = "404 WebSocket Upgrade Failure";