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 Status Code in HTTP Response for WebSocket Handshake #1424

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 20 additions & 9 deletions src/main/java/org/java_websocket/WebSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,10 @@ private void decodeFrames(ByteBuffer socketBuffer) {
* @param exception the InvalidDataException causing this problem
*/
private void closeConnectionDueToWrongHandshake(InvalidDataException exception) {
write(generateHttpResponseDueToError(404));
flushAndClose(exception.getCloseCode(), exception.getMessage(), false);
int closeCode = exception.getCloseCode();
String message = exception.getMessage();
write(generateHttpResponseDueToError(closeCode, message));
flushAndClose(closeCode, message, false);
}

/**
Expand All @@ -434,25 +436,34 @@ private void closeConnectionDueToWrongHandshake(InvalidDataException exception)
* @param exception the RuntimeException causing this problem
*/
private void closeConnectionDueToInternalServerError(RuntimeException exception) {
write(generateHttpResponseDueToError(500));
flushAndClose(CloseFrame.NEVER_CONNECTED, exception.getMessage(), false);
String message = exception.getMessage();
write(generateHttpResponseDueToError(500, message));
flushAndClose(CloseFrame.NEVER_CONNECTED, message, false);
}

/**
* Generate a simple response for the corresponding endpoint to indicate some error
*
* @param errorCode the http error code
* @param errorMessage the http error message
* @return the complete response as ByteBuffer
*/
private ByteBuffer generateHttpResponseDueToError(int errorCode) {
private ByteBuffer generateHttpResponseDueToError(int errorCode, String errorMessage) {
String errorCodeDescription;
switch (errorCode) {
case 404:
errorCodeDescription = "404 WebSocket Upgrade Failure";
break;
case 500:
if (errorMessage == null || errorMessage.isEmpty()) {
errorCodeDescription = "500 Internal Server Error";
} else {
errorCodeDescription = String.format("500 %s", errorMessage);
}
break;
default:
errorCodeDescription = "500 Internal Server Error";
if (errorMessage == null || errorMessage.isEmpty()) {
errorCodeDescription = String.format("%d WebSocket Upgrade Failure", errorCode);
} else {
errorCodeDescription = String.format("%d %s", errorCode, errorMessage);
}
}
return ByteBuffer.wrap(Charsetfunctions.asciiBytes("HTTP/1.1 " + errorCodeDescription
+ "\r\nContent-Type: text/html\r\nServer: TooTallNate Java-WebSocket\r\nContent-Length: "
Expand Down