Skip to content

Commit c36df62

Browse files
committed
Responses to handshake errors
Implemented correct responses for handshake errors (see TooTallNate#530) Removed possible Nullpointer in the toString() method
1 parent 026802d commit c36df62

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) {
260260
socketBuffer.reset();
261261
Handshakedata tmphandshake = d.translateHandshake( socketBuffer );
262262
if( !( tmphandshake instanceof ClientHandshake ) ) {
263-
flushAndClose( CloseFrame.PROTOCOL_ERROR, "wrong http function", false );
263+
closeConnectionDueToWrongHandshake( new InvalidDataException( CloseFrame.PROTOCOL_ERROR, "wrong http function" ));
264264
return false;
265265
}
266266
ClientHandshake handshake = ( ClientHandshake ) tmphandshake;
@@ -271,11 +271,11 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) {
271271
try {
272272
response = wsl.onWebsocketHandshakeReceivedAsServer( this, d, handshake );
273273
} catch ( InvalidDataException e ) {
274-
flushAndClose( e.getCloseCode(), e.getMessage(), false );
274+
closeConnectionDueToWrongHandshake( e );
275275
return false;
276276
} catch ( RuntimeException e ) {
277277
wsl.onWebsocketError( this, e );
278-
flushAndClose( CloseFrame.NEVER_CONNECTED, e.getMessage(), false );
278+
closeConnectionDueToInternalServerError( e );
279279
return false;
280280
}
281281
write( d.createHandshake( d.postProcessHandshakeResponseAsServer( handshake, response ), role ) );
@@ -288,7 +288,7 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) {
288288
}
289289
}
290290
if( draft == null ) {
291-
close( CloseFrame.PROTOCOL_ERROR, "no draft matches" );
291+
closeConnectionDueToWrongHandshake( new InvalidDataException( CloseFrame.PROTOCOL_ERROR, "no draft matches"));
292292
}
293293
return false;
294294
} else {
@@ -359,6 +359,42 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) {
359359
return false;
360360
}
361361

362+
/**
363+
* Close the connection if the received handshake was not correct
364+
* @param exception the InvalidDataException causing this problem
365+
*/
366+
private void closeConnectionDueToWrongHandshake( InvalidDataException exception ) {
367+
write(generateHttpResponseDueToError( 404 ));
368+
flushAndClose( exception.getCloseCode(), exception.getMessage(), false );
369+
}
370+
371+
/**
372+
* Close the connection if there was a server error by a RuntimeException
373+
* @param exception the RuntimeException causing this problem
374+
*/
375+
private void closeConnectionDueToInternalServerError(RuntimeException exception) {
376+
write(generateHttpResponseDueToError( 500 ));
377+
flushAndClose( CloseFrame.NEVER_CONNECTED, exception.getMessage(), false );
378+
}
379+
380+
/**
381+
* Generate a simple response for the corresponding endpoint to indicate some error
382+
* @param errorCode the http error code
383+
* @return the complete response as ByteBuffer
384+
*/
385+
private ByteBuffer generateHttpResponseDueToError(int errorCode) {
386+
String errorCodeDescription;
387+
switch (errorCode) {
388+
case 404:
389+
errorCodeDescription = "404 WebSocket Upgrade Failure";
390+
break;
391+
case 500:
392+
default:
393+
errorCodeDescription = "500 Internal Server Error";
394+
}
395+
return ByteBuffer.wrap( Charsetfunctions.asciiBytes( "HTTP/1.1 "+ errorCodeDescription +"\r\nContent-Type: text/html\nServer: TooTallNate Java-WebSocket\r\nContent-Length: " + (48 + errorCodeDescription.length()) +"\r\n\r\n<html><head></head><body><h1>" + errorCodeDescription + "</h1></body></html>" ));
396+
}
397+
362398
private void decodeFrames( ByteBuffer socketBuffer ) {
363399

364400
List<Framedata> frames;

src/main/java/org/java_websocket/drafts/Draft_6455.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ private String getServerTime() {
346346

347347
@Override
348348
public String toString() {
349-
return super.toString() + " extension: " + getExtension().toString();
349+
String result = super.toString();
350+
if( getExtension() != null )
351+
result += " extension: " + getExtension().toString();
352+
return result;
350353
}
351354
}

0 commit comments

Comments
 (0)