Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Commit 59f4b62

Browse files
author
christoffer.ahlbin
committed
Add callback to obtain handshake response as client in case of
handshake failure. E.g. if the server rejects the WebSocket upgrade request by issuing a HTTP response with 503 status code, then this HTTP response data can be obtained via WebSocketListener.onWebsocketHandshakeReceivedAsClientFailed(WebSocket, ClientHandshake, ServerHandshake)
1 parent 7c3b223 commit 59f4b62

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎src/main/java/org/java_websocket/WebSocketAdapter.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer( WebSocket co
3232
public void onWebsocketHandshakeReceivedAsClient( WebSocket conn, ClientHandshake request, ServerHandshake response ) throws InvalidDataException {
3333
}
3434

35+
/**
36+
* This default implementation does not do anything which will cause the connections to always progress.
37+
*
38+
* @see org.java_websocket.WebSocketListener#onWebsocketHandshakeReceivedAsClientFailed(WebSocket, ClientHandshake, ServerHandshake)
39+
*/
40+
@Override
41+
public void onWebsocketHandshakeReceivedAsClientFailed( WebSocket conn, ClientHandshake request, ServerHandshake response ) {
42+
43+
}
44+
3545
/**
3646
* This default implementation does not do anything which will cause the connections to always progress.
3747
*

‎src/main/java/org/java_websocket/WebSocketImpl.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,16 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) {
287287
open( handshake );
288288
return true;
289289
} else {
290+
if (handshake.getHttpStatus() != 101) {
291+
// if HTTP-status is not 101, let client get a chance to obtain the HTTP response
292+
// and take action upon that as per regular HTTP procedures.
293+
// (http://tools.ietf.org/html/rfc6455#section-4.1)
294+
try {
295+
wsl.onWebsocketHandshakeReceivedAsClientFailed(this, handshakerequest, handshake);
296+
} catch (RuntimeException e) {
297+
// do nothing, fall through and close socket
298+
}
299+
}
290300
close( CloseFrame.PROTOCOL_ERROR, "draft " + draft + " refuses handshake" );
291301
}
292302
}

‎src/main/java/org/java_websocket/WebSocketListener.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ public interface WebSocketListener {
5050
*/
5151
public void onWebsocketHandshakeReceivedAsClient( WebSocket conn, ClientHandshake request, ServerHandshake response ) throws InvalidDataException;
5252

53+
/**
54+
* Called on the client side when the socket connection is about to be established, but failed due to the handshake
55+
* could not be verified. E.g. the server rejected the request with a regular HTTP response, and the client
56+
* can then use this callback as a way of obtain that HTTP response, e.g. status code.
57+
*
58+
* @param conn The WebSocket related to this event
59+
* @param request The handshake initially send out to the server by this websocket.
60+
* @param response The handshake the server sent in response to the request.
61+
*/
62+
public void onWebsocketHandshakeReceivedAsClientFailed(WebSocket conn, ClientHandshake request, ServerHandshake response);
63+
64+
5365
/**
5466
* Called on the client side when the socket connection is first established, and the WebSocketImpl
5567
* handshake has just been sent.

0 commit comments

Comments
 (0)