Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidHandshakeException on invalid status line #572

Merged
merged 1 commit into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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