diff --git a/src/main/java/org/java_websocket/WebSocketImpl.java b/src/main/java/org/java_websocket/WebSocketImpl.java index aad172127..37523859c 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 Authorization Required"; + 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; + } + }