From 6566b0637154f75107e6a30180f876b3298ae929 Mon Sep 17 00:00:00 2001 From: marci4 Date: Thu, 12 Oct 2017 19:02:32 +0200 Subject: [PATCH] InvalidHandshakeException on invalid status line Fix for #390 --- src/main/example/ExampleClient.java | 4 ++-- src/main/java/org/java_websocket/drafts/Draft.java | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/example/ExampleClient.java b/src/main/example/ExampleClient.java index 0362c821..f7610c23 100644 --- a/src/main/example/ExampleClient.java +++ b/src/main/example/ExampleClient.java @@ -57,7 +57,7 @@ public void onMessage( String message ) { @Override public void onClose( int code, String reason, boolean remote ) { // The codecodes are documented in class org.java_websocket.framing.CloseFrame - System.out.println( "Connection closed by " + ( remote ? "remote peer" : "us" ) ); + System.out.println( "Connection closed by " + ( remote ? "remote peer" : "us" ) + " Code: " + code + " Reason: " + reason ); } @Override @@ -67,7 +67,7 @@ public void onError( Exception ex ) { } public static void main( String[] args ) throws URISyntaxException { - ExampleClient c = new ExampleClient( new URI( "ws://localhost:8887" ), new Draft_6455() ); // more about drafts here: http://github.com/TooTallNate/Java-WebSocket/wiki/Drafts + ExampleClient c = new ExampleClient( new URI( "ws://echo.websocket.org/asdf" ), new Draft_6455() ); // more about drafts here: http://github.com/TooTallNate/Java-WebSocket/wiki/Drafts c.connect(); } diff --git a/src/main/java/org/java_websocket/drafts/Draft.java b/src/main/java/org/java_websocket/drafts/Draft.java index 7a151af2..5d135aa3 100644 --- a/src/main/java/org/java_websocket/drafts/Draft.java +++ b/src/main/java/org/java_websocket/drafts/Draft.java @@ -119,12 +119,24 @@ public static HandshakeBuilder translateHandshakeHttp( ByteBuffer buf, Role role if( role == Role.CLIENT ) { // translating/parsing the response from the SERVER + if (!"HTTP/1.1".equalsIgnoreCase(firstLineTokens[0])) { + throw new InvalidHandshakeException( "Invalid status line received: " + firstLineTokens[0] ); + } + if (!"101".equals(firstLineTokens[1])) { + throw new InvalidHandshakeException( "Invalid status code received: " + firstLineTokens[1] ); + } handshake = new HandshakeImpl1Server(); ServerHandshakeBuilder serverhandshake = (ServerHandshakeBuilder) handshake; serverhandshake.setHttpStatus( Short.parseShort( firstLineTokens[ 1 ] ) ); serverhandshake.setHttpStatusMessage( firstLineTokens[ 2 ] ); } else { // translating/parsing the request from the CLIENT + if (!"GET".equalsIgnoreCase(firstLineTokens[0])) { + throw new InvalidHandshakeException( "Invalid request method received: " + firstLineTokens[0] ); + } + if (!"HTTP/1.1".equalsIgnoreCase(firstLineTokens[2])) { + throw new InvalidHandshakeException( "Invalid status line received: " + firstLineTokens[2] ); + } ClientHandshakeBuilder clienthandshake = new HandshakeImpl1Client(); clienthandshake.setResourceDescriptor( firstLineTokens[ 1 ] ); handshake = clienthandshake;