Skip to content

Commit 2a07103

Browse files
authored
Merge pull request #572 from marci4/master
InvalidHandshakeException on invalid status line
2 parents 1bc60f7 + 6566b06 commit 2a07103

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/main/example/ExampleClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void onMessage( String message ) {
5757
@Override
5858
public void onClose( int code, String reason, boolean remote ) {
5959
// The codecodes are documented in class org.java_websocket.framing.CloseFrame
60-
System.out.println( "Connection closed by " + ( remote ? "remote peer" : "us" ) );
60+
System.out.println( "Connection closed by " + ( remote ? "remote peer" : "us" ) + " Code: " + code + " Reason: " + reason );
6161
}
6262

6363
@Override
@@ -67,7 +67,7 @@ public void onError( Exception ex ) {
6767
}
6868

6969
public static void main( String[] args ) throws URISyntaxException {
70-
ExampleClient c = new ExampleClient( new URI( "ws://localhost:8887" ), new Draft_6455() ); // more about drafts here: http://github.com/TooTallNate/Java-WebSocket/wiki/Drafts
70+
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
7171
c.connect();
7272
}
7373

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,24 @@ public static HandshakeBuilder translateHandshakeHttp( ByteBuffer buf, Role role
119119

120120
if( role == Role.CLIENT ) {
121121
// translating/parsing the response from the SERVER
122+
if (!"HTTP/1.1".equalsIgnoreCase(firstLineTokens[0])) {
123+
throw new InvalidHandshakeException( "Invalid status line received: " + firstLineTokens[0] );
124+
}
125+
if (!"101".equals(firstLineTokens[1])) {
126+
throw new InvalidHandshakeException( "Invalid status code received: " + firstLineTokens[1] );
127+
}
122128
handshake = new HandshakeImpl1Server();
123129
ServerHandshakeBuilder serverhandshake = (ServerHandshakeBuilder) handshake;
124130
serverhandshake.setHttpStatus( Short.parseShort( firstLineTokens[ 1 ] ) );
125131
serverhandshake.setHttpStatusMessage( firstLineTokens[ 2 ] );
126132
} else {
127133
// translating/parsing the request from the CLIENT
134+
if (!"GET".equalsIgnoreCase(firstLineTokens[0])) {
135+
throw new InvalidHandshakeException( "Invalid request method received: " + firstLineTokens[0] );
136+
}
137+
if (!"HTTP/1.1".equalsIgnoreCase(firstLineTokens[2])) {
138+
throw new InvalidHandshakeException( "Invalid status line received: " + firstLineTokens[2] );
139+
}
128140
ClientHandshakeBuilder clienthandshake = new HandshakeImpl1Client();
129141
clienthandshake.setResourceDescriptor( firstLineTokens[ 1 ] );
130142
handshake = clienthandshake;

0 commit comments

Comments
 (0)