@@ -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 \n Content-Type: text/html\n Server: TooTallNate Java-WebSocket\r \n Content-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 ;
0 commit comments