Skip to content

Commit 3f9a596

Browse files
committed
Don't leak connection on websocket redirect
1 parent 51f1994 commit 3f9a596

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.netty.channel.Channel;
2020
import io.netty.handler.codec.http.HttpHeaderNames;
2121
import io.netty.handler.codec.http.HttpHeaderValues;
22+
import io.netty.handler.codec.http.HttpRequest;
2223
import io.netty.handler.codec.http.HttpResponse;
2324
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
2425
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
@@ -69,29 +70,22 @@ private class UpgradeCallback extends Callback {
6970

7071
private final Channel channel;
7172
private final HttpResponse response;
73+
private final WebSocketUpgradeHandler handler;
74+
private final HttpResponseStatus status;
75+
private final HttpResponseHeaders responseHeaders;
7276

73-
public UpgradeCallback(NettyResponseFuture<?> future, Channel channel, HttpResponse response) {
77+
public UpgradeCallback(NettyResponseFuture<?> future, Channel channel, HttpResponse response, WebSocketUpgradeHandler handler, HttpResponseStatus status, HttpResponseHeaders responseHeaders) {
7478
super(future);
7579
this.channel = channel;
7680
this.response = response;
81+
this.handler = handler;
82+
this.status = status;
83+
this.responseHeaders = responseHeaders;
7784
}
7885

7986
@Override
8087
public void call() throws Exception {
8188

82-
WebSocketUpgradeHandler handler = WebSocketUpgradeHandler.class.cast(future.getAsyncHandler());
83-
Request request = future.getCurrentRequest();
84-
85-
HttpResponseStatus status = new NettyResponseStatus(future.getUri(), config, response, channel);
86-
HttpResponseHeaders responseHeaders = new HttpResponseHeaders(response.headers());
87-
Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm();
88-
89-
if (exitAfterProcessingFilters(channel, future, handler, status, responseHeaders)) {
90-
return;
91-
}
92-
93-
if (REDIRECT_STATUSES.contains(status.getStatusCode()) && exitAfterHandlingRedirect(channel, future, response, request, response.status().code(), realm))
94-
return;
9589

9690
boolean validStatus = response.status().equals(SWITCHING_PROTOCOLS);
9791
boolean validUpgrade = response.headers().get(HttpHeaderNames.UPGRADE) != null;
@@ -137,7 +131,26 @@ public void handle(Channel channel, NettyResponseFuture<?> future, Object e) thr
137131

138132
if (e instanceof HttpResponse) {
139133
HttpResponse response = (HttpResponse) e;
140-
Channels.setAttribute(channel, new UpgradeCallback(future, channel, response));
134+
if (logger.isDebugEnabled()) {
135+
HttpRequest httpRequest = future.getNettyRequest().getHttpRequest();
136+
logger.debug("\n\nRequest {}\n\nResponse {}\n", httpRequest, response);
137+
}
138+
139+
WebSocketUpgradeHandler handler = WebSocketUpgradeHandler.class.cast(future.getAsyncHandler());
140+
HttpResponseStatus status = new NettyResponseStatus(future.getUri(), config, response, channel);
141+
HttpResponseHeaders responseHeaders = new HttpResponseHeaders(response.headers());
142+
143+
Request request = future.getCurrentRequest();
144+
Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm();
145+
146+
if (exitAfterProcessingFilters(channel, future, handler, status, responseHeaders)) {
147+
return;
148+
}
149+
150+
if (REDIRECT_STATUSES.contains(status.getStatusCode()) && exitAfterHandlingRedirect(channel, future, response, request, response.status().code(), realm))
151+
return;
152+
153+
Channels.setAttribute(channel, new UpgradeCallback(future, channel, response, handler, status, responseHeaders));
141154

142155
} else if (e instanceof WebSocketFrame) {
143156

0 commit comments

Comments
 (0)