Skip to content

Commit ad3d043

Browse files
committed
Fix WebSocketServer sometimes missing GET request
On Ubuntu 22.04 with Linux 6.5, it was observed that when the server gets the SSL records containing the client handshake finished message and the first HTTP GET request in ONE read operation, the latter SSL record is never processed. Commit 89eaf41 should have fixed this, but it turned out that when SSLSocketChannel2#processHandshake() is called from SSLSocketChannel2#write(), the second SSL record containing the HTTP GET request is stashed away, but never retrieved, since the calling code in WebSocketServer#doWrite() has no provisions for this, only WebSocketServer#doRead() does. Change SSLSocketChannel2#processHandshake() to only read from the socket when called from SSLSocketChannel#read(), to ensure that when two SSL records are read, the second one is processed as well. This fixes issue #1418.
1 parent c793f34 commit ad3d043

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

src/main/java/org/java_websocket/SSLSocketChannel2.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public SSLSocketChannel2(SocketChannel channel, SSLEngine sslEngine, ExecutorSer
126126
createBuffers(sslEngine.getSession());
127127
// kick off handshake
128128
socketChannel.write(wrap(emptybuffer));// initializes res
129-
processHandshake();
129+
processHandshake(false);
130130
}
131131

132132
private void consumeFutureUninterruptible(Future<?> f) {
@@ -148,7 +148,7 @@ private void consumeFutureUninterruptible(Future<?> f) {
148148
* This method will do whatever necessary to process the sslEngine handshake. Thats why it's
149149
* called both from the {@link #read(ByteBuffer)} and {@link #write(ByteBuffer)}
150150
**/
151-
private synchronized void processHandshake() throws IOException {
151+
private synchronized void processHandshake(boolean isReading) throws IOException {
152152
if (sslEngine.getHandshakeStatus() == HandshakeStatus.NOT_HANDSHAKING) {
153153
return; // since this may be called either from a reading or a writing thread and because this method is synchronized it is necessary to double check if we are still handshaking.
154154
}
@@ -167,7 +167,7 @@ private synchronized void processHandshake() throws IOException {
167167
}
168168
}
169169

170-
if (sslEngine.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.NEED_UNWRAP) {
170+
if (isReading && sslEngine.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.NEED_UNWRAP) {
171171
if (!isBlocking() || readEngineResult.getStatus() == Status.BUFFER_UNDERFLOW) {
172172
inCrypt.compact();
173173
int read = socketChannel.read(inCrypt);
@@ -273,7 +273,7 @@ protected void createBuffers(SSLSession session) {
273273

274274
public int write(ByteBuffer src) throws IOException {
275275
if (!isHandShakeComplete()) {
276-
processHandshake();
276+
processHandshake(false);
277277
return 0;
278278
}
279279
// assert(bufferallocations > 1); // see #190
@@ -303,10 +303,10 @@ public int read(ByteBuffer dst) throws IOException {
303303
if (!isHandShakeComplete()) {
304304
if (isBlocking()) {
305305
while (!isHandShakeComplete()) {
306-
processHandshake();
306+
processHandshake(true);
307307
}
308308
} else {
309-
processHandshake();
309+
processHandshake(true);
310310
if (!isHandShakeComplete()) {
311311
return 0;
312312
}

0 commit comments

Comments
 (0)