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 other http error codes #1

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion src/main/java/org/java_websocket/WebSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class InvalidDataException extends Exception {
*/
private final int closecode;

private Integer httpErrorCode;

/**
* constructor for a InvalidDataException
*
Expand All @@ -49,6 +51,17 @@ public InvalidDataException(int closecode) {
this.closecode = closecode;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constructor should initialize httpErrorCode = null


/**
* 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.
*
Expand All @@ -60,6 +73,19 @@ public InvalidDataException(int closecode, String s) {
this.closecode = closecode;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constructor should initialize httpErrorCode = null

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not required as I used Integer so if not initialized it will be null


/**
* 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.
*
Expand Down Expand Up @@ -92,4 +118,13 @@ public int getCloseCode() {
return closecode;
}

/**
* Getter httpErrorCode
*
* @return the httpErrorCode
*/
public Integer getHttpErrorCode() {
return httpErrorCode;
}

}