Skip to content

Commit

Permalink
InvalidHandshakeException on invalid status line
Browse files Browse the repository at this point in the history
  • Loading branch information
marci4 committed Oct 12, 2017
1 parent 1bc60f7 commit 6566b06
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/example/ExampleClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/java_websocket/drafts/Draft.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6566b06

Please sign in to comment.