Skip to content

Commit 9c34195

Browse files
committed
Incremental fix for AsyncHttpClient#101 [websocket] onError must not be called when status code is != 101. Fix Netty side
1 parent 0fbabf0 commit 9c34195

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2346,7 +2346,13 @@ public void handle(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
23462346
s = new ResponseStatus(future.getURI(), response, NettyAsyncHttpProvider.this);
23472347
final boolean statusReceived = h.onStatusReceived(s) == STATE.UPGRADE;
23482348

2349-
if (!validStatus || !validUpgrade || !validConnection || !statusReceived) {
2349+
if (!statusReceived) {
2350+
h.onClose(new NettyWebSocket(ctx.getChannel()), 1002, "Bad response status " + response.getStatus().getCode());
2351+
future.done(null);
2352+
return;
2353+
}
2354+
2355+
if (!validStatus || !validUpgrade || !validConnection) {
23502356
throw new IOException("Invalid handshake response");
23512357
}
23522358

src/main/java/com/ning/http/client/websocket/WebSocketUpgradeHandler.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public final STATE onStatusReceived(HttpResponseStatus responseStatus) throws Ex
6464
if (responseStatus.getStatusCode() == 101) {
6565
return STATE.UPGRADE;
6666
} else {
67-
throw new IllegalStateException("Invalid upgrade protocol, status should be 101 but was " + responseStatus.getStatusCode());
67+
return STATE.ABORT;
6868
}
6969
}
7070

@@ -113,6 +113,21 @@ public final void onFailure(Throwable t) {
113113
}
114114
}
115115

116+
public final void onClose(WebSocket webSocket, int status, String reasonPhrase) {
117+
// Connect failure
118+
if (this.webSocket == null) this.webSocket = webSocket;
119+
120+
for (WebSocketListener w : l) {
121+
if (webSocket != null) {
122+
webSocket.addWebSocketListener(w);
123+
}
124+
w.onClose(webSocket);
125+
if (WebSocketCloseCodeReasonListener.class.isAssignableFrom(w.getClass())) {
126+
WebSocketCloseCodeReasonListener.class.cast(w).onClose(webSocket, status, reasonPhrase);
127+
}
128+
}
129+
}
130+
116131
/**
117132
* Build a {@link WebSocketUpgradeHandler}
118133
*/

0 commit comments

Comments
 (0)