Skip to content

Commit b64a2dc

Browse files
committed
Improve fixed for AsyncHttpClient#110. Also fixes AsyncHttpClient#156 by properly invoking the AsyncHandler/WebSocketListener
1 parent e7de518 commit b64a2dc

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed

api/src/test/java/org/asynchttpclient/websocket/CloseCodeReasonMessageTest.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ public void onError(Throwable t) {
102102
}
103103
}
104104

105-
106105
@Test(timeOut = 60000)
107106
public void wrongStatusCode() throws Throwable {
108107
AsyncHttpClient c = getAsyncHttpClient(null);
@@ -142,4 +141,44 @@ public void onError(Throwable t) {
142141
c.close();
143142
}
144143
}
144+
145+
@Test(timeOut = 60000)
146+
public void wrongProtocolCode() throws Throwable {
147+
AsyncHttpClient c = getAsyncHttpClient(null);
148+
try {
149+
final CountDownLatch latch = new CountDownLatch(1);
150+
final AtomicReference<Throwable> throwable = new AtomicReference<Throwable>();
151+
152+
WebSocket websocket = c.prepareGet("ws://www.google.com").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {
153+
154+
@Override
155+
public void onMessage(String message) {
156+
}
157+
158+
@Override
159+
public void onFragment(String fragment, boolean last) {
160+
}
161+
162+
@Override
163+
public void onOpen(WebSocket websocket) {
164+
}
165+
166+
@Override
167+
public void onClose(WebSocket websocket) {
168+
}
169+
170+
@Override
171+
public void onError(Throwable t) {
172+
throwable.set(t);
173+
latch.countDown();
174+
}
175+
}).build()).get();
176+
177+
latch.await();
178+
assertNotNull(throwable.get());
179+
assertEquals(throwable.get().getClass(), IllegalStateException.class);
180+
} finally {
181+
c.close();
182+
}
183+
}
145184
}

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/EventHandler.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import org.glassfish.grizzly.http.util.Header;
4343
import org.glassfish.grizzly.http.util.HttpStatus;
4444
import org.glassfish.grizzly.utils.IdleTimeoutFilter;
45-
import org.glassfish.grizzly.websockets.HandshakeException;
4645
import org.glassfish.grizzly.websockets.SimpleWebSocket;
4746
import org.glassfish.grizzly.websockets.WebSocketHolder;
4847

@@ -201,7 +200,12 @@ public void onInitialLineParsed(HttpHeader httpHeader,
201200
context.setCurrentState(handler.onStatusReceived(responseStatus));
202201
if (context.isWSRequest() && context.getCurrentState() == ABORT) {
203202
httpHeader.setSkipRemainder(true);
204-
context.abort(new HandshakeException("Upgrade failed"));
203+
try {
204+
context.result(handler.onCompleted());
205+
context.done();
206+
} catch (Exception e) {
207+
context.abort(e);
208+
}
205209
}
206210
}
207211
} catch (Exception e) {

providers/netty/src/main/java/org/asynchttpclient/providers/netty/handler/WebSocketProtocol.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.asynchttpclient.providers.netty.handler;
1717

18-
import static io.netty.handler.codec.http.HttpResponseStatus.SWITCHING_PROTOCOLS;
1918
import io.netty.buffer.ByteBuf;
2019
import io.netty.channel.ChannelHandlerContext;
2120
import io.netty.handler.codec.http.HttpHeaders;
@@ -24,10 +23,6 @@
2423
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
2524
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
2625
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
27-
28-
import java.io.IOException;
29-
import java.util.Locale;
30-
3126
import org.asynchttpclient.AsyncHandler.STATE;
3227
import org.asynchttpclient.AsyncHttpClientConfig;
3328
import org.asynchttpclient.HttpResponseHeaders;
@@ -48,6 +43,11 @@
4843
import org.slf4j.Logger;
4944
import org.slf4j.LoggerFactory;
5045

46+
import java.io.IOException;
47+
import java.util.Locale;
48+
49+
import static io.netty.handler.codec.http.HttpResponseStatus.SWITCHING_PROTOCOLS;
50+
5151
final class WebSocketProtocol extends Protocol {
5252

5353
private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketProtocol.class);
@@ -98,16 +98,25 @@ public void handle(ChannelHandlerContext ctx, NettyResponseFuture<?> future, Obj
9898
status = new ResponseStatus(future.getURI(), response, config);
9999
final boolean statusReceived = h.onStatusReceived(status) == STATE.UPGRADE;
100100

101+
if (!statusReceived) {
102+
try {
103+
h.onCompleted();
104+
} finally {
105+
future.done();
106+
}
107+
return;
108+
}
109+
101110
final boolean headerOK = h.onHeadersReceived(responseHeaders) == STATE.CONTINUE;
102-
if (!headerOK || !validStatus || !validUpgrade || !validConnection || !statusReceived) {
111+
if (!headerOK || !validStatus || !validUpgrade || !validConnection) {
103112
channels.abort(future, new IOException("Invalid handshake response"));
104113
return;
105114
}
106115

107116
String accept = response.headers().get(HttpHeaders.Names.SEC_WEBSOCKET_ACCEPT);
108117
String key = WebSocketUtil.getAcceptKey(future.getNettyRequest().getHttpRequest().headers().get(HttpHeaders.Names.SEC_WEBSOCKET_KEY));
109118
if (accept == null || !accept.equals(key)) {
110-
throw new IOException(String.format("Invalid challenge. Actual: %s. Expected: %s", accept, key));
119+
channels.abort(future, new IOException(String.format("Invalid challenge. Actual: %s. Expected: %s", accept, key)));
111120
}
112121

113122
Channels.upgradePipelineForWebSockets(ctx);

0 commit comments

Comments
 (0)