Skip to content

Commit 830a7ee

Browse files
committed
Merge pull request AsyncHttpClient#472 from grammati/1.8.x-websocket-bug
Fix bug in netty websockets that treated a close frame as text.
2 parents ad2971e + a1397e2 commit 830a7ee

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2366,7 +2366,7 @@ public void setContent(ChannelBuffer content) {
23662366
if (webSocket != null) {
23672367
if (pendingOpcode == OPCODE_BINARY) {
23682368
webSocket.onBinaryFragment(rp.getBodyPartBytes(), frame.isFinalFragment());
2369-
} else {
2369+
} else if (pendingOpcode == OPCODE_TEXT) {
23702370
webSocket.onTextFragment(frame.getBinaryData().toString(UTF8), frame.isFinalFragment());
23712371
}
23722372

src/test/java/com/ning/http/client/websocket/TextMessageTest.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ public void onClose(int i, String s) {
4545
@Override
4646
public void onMessage(String s) {
4747
try {
48-
connection.sendMessage(s);
48+
if (s.equals("CLOSE"))
49+
connection.close();
50+
else
51+
connection.sendMessage(s);
4952
} catch (IOException e) {
5053
try {
5154
connection.sendMessage("FAIL");
@@ -401,4 +404,53 @@ public void onError(Throwable t) {
401404
c.close();
402405
}
403406
}
407+
408+
@Test(timeOut = 60000)
409+
public void echoTextAndThenClose() throws Throwable {
410+
AsyncHttpClient c = getAsyncHttpClient(null);
411+
try {
412+
final CountDownLatch textLatch = new CountDownLatch(1);
413+
final CountDownLatch closeLatch = new CountDownLatch(1);
414+
final AtomicReference<String> text = new AtomicReference<String>("");
415+
416+
final WebSocket websocket = c.prepareGet(getTargetUrl()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {
417+
418+
@Override
419+
public void onMessage(String message) {
420+
text.set(text.get() + message);
421+
textLatch.countDown();
422+
}
423+
424+
@Override
425+
public void onFragment(String fragment, boolean last) {
426+
}
427+
428+
@Override
429+
public void onOpen(com.ning.http.client.websocket.WebSocket websocket) {
430+
}
431+
432+
@Override
433+
public void onClose(com.ning.http.client.websocket.WebSocket websocket) {
434+
closeLatch.countDown();
435+
}
436+
437+
@Override
438+
public void onError(Throwable t) {
439+
t.printStackTrace();
440+
closeLatch.countDown();
441+
}
442+
}).build()).get();
443+
444+
websocket.sendTextMessage("ECHO");
445+
textLatch.await();
446+
447+
websocket.sendTextMessage("CLOSE");
448+
closeLatch.await();
449+
450+
assertEquals(text.get(), "ECHO");
451+
} finally {
452+
c.close();
453+
}
454+
}
455+
404456
}

0 commit comments

Comments
 (0)