Skip to content

Commit e91a0ea

Browse files
committed
Fix for 621
Added a check in decode() to make sure that the connection is still open.
1 parent 57f11ae commit e91a0ea

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

src/main/java/org/java_websocket/WebSocketImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public void decode( ByteBuffer socketBuffer ) {
206206
decodeFrames( socketBuffer );
207207
}
208208
} else {
209-
if( decodeHandshake( socketBuffer ) ) {
209+
if( decodeHandshake( socketBuffer ) && (!isClosing() && !isClosed())) {
210210
assert ( tmpHandshakeBytes.hasRemaining() != socketBuffer.hasRemaining() || !socketBuffer.hasRemaining() ); // the buffers will never have remaining bytes at the same time
211211

212212
if( socketBuffer.hasRemaining() ) {

src/test/java/org/java_websocket/issues/AllIssueTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
@RunWith(Suite.class)
3232
@Suite.SuiteClasses({
3333
org.java_websocket.issues.Issue609Test.class,
34+
org.java_websocket.issues.Issue621Test.class,
3435
org.java_websocket.issues.Issue580Test.class
3536
})
3637
/**
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (c) 2010-2017 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package org.java_websocket.issues;
27+
28+
import org.java_websocket.WebSocket;
29+
import org.java_websocket.client.WebSocketClient;
30+
import org.java_websocket.handshake.ClientHandshake;
31+
import org.java_websocket.handshake.ServerHandshake;
32+
import org.java_websocket.server.WebSocketServer;
33+
import org.java_websocket.util.SocketUtil;
34+
import org.junit.Test;
35+
36+
import java.io.OutputStream;
37+
import java.io.PrintStream;
38+
import java.net.InetSocketAddress;
39+
import java.net.URI;
40+
import java.util.concurrent.CountDownLatch;
41+
42+
import static org.junit.Assert.assertTrue;
43+
44+
public class Issue621Test {
45+
46+
CountDownLatch countDownLatch = new CountDownLatch( 1 );
47+
CountDownLatch countServerDownLatch = new CountDownLatch( 1 );
48+
49+
boolean wasError = false;
50+
51+
class TestPrintStream extends PrintStream {
52+
public TestPrintStream( OutputStream out ) {
53+
super( out );
54+
}
55+
56+
@Override
57+
public void println( Object o ) {
58+
wasError = true;
59+
super.println( o );
60+
}
61+
}
62+
63+
@Test
64+
public void testIssue() throws Exception {
65+
System.setErr( new TestPrintStream( System.err ) );
66+
int port = SocketUtil.getAvailablePort();
67+
WebSocketClient webSocket = new WebSocketClient( new URI( "ws://localhost:" + port ) ) {
68+
@Override
69+
public void onOpen( ServerHandshake handshakedata ) {
70+
71+
}
72+
73+
@Override
74+
public void onMessage( String message ) {
75+
76+
}
77+
78+
@Override
79+
public void onClose( int code, String reason, boolean remote ) {
80+
countDownLatch.countDown();
81+
}
82+
83+
@Override
84+
public void onError( Exception ex ) {
85+
86+
}
87+
};
88+
WebSocketServer server = new WebSocketServer( new InetSocketAddress( port ) ) {
89+
@Override
90+
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
91+
conn.close();
92+
}
93+
94+
@Override
95+
public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
96+
}
97+
98+
@Override
99+
public void onMessage( WebSocket conn, String message ) {
100+
101+
}
102+
103+
@Override
104+
public void onError( WebSocket conn, Exception ex ) {
105+
106+
}
107+
108+
@Override
109+
public void onStart() {
110+
countServerDownLatch.countDown();
111+
}
112+
};
113+
server.start();
114+
countServerDownLatch.await();
115+
webSocket.connectBlocking();
116+
countDownLatch.await();
117+
assertTrue( "There was an error using System.err", !wasError );
118+
server.stop();
119+
}
120+
}

0 commit comments

Comments
 (0)