Skip to content

Commit 4a1f689

Browse files
committed
Handle Netty 4's HttpObject.getDecoderResult().cause(), close AsyncHttpClient#1072
1 parent bd03b19 commit 4a1f689

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.netty.channel.ChannelHandler.Sharable;
1919
import io.netty.handler.codec.http.HttpContent;
2020
import io.netty.handler.codec.http.HttpHeaders;
21+
import io.netty.handler.codec.http.HttpObject;
2122
import io.netty.handler.codec.http.HttpRequest;
2223
import io.netty.handler.codec.http.HttpResponse;
2324
import io.netty.handler.codec.http.LastHttpContent;
@@ -68,7 +69,7 @@ private boolean updateBodyAndInterrupt(NettyResponseFuture<?> future, AsyncHandl
6869
return interrupt;
6970
}
7071

71-
private boolean exitAfterHandler(Channel channel, NettyResponseFuture<?> future, HttpResponse response, AsyncHandler<?> handler, NettyResponseStatus status,
72+
private void notifyHandler(Channel channel, NettyResponseFuture<?> future, HttpResponse response, AsyncHandler<?> handler, NettyResponseStatus status,
7273
HttpRequest httpRequest, HttpResponseHeaders responseHeaders) throws IOException, Exception {
7374

7475
boolean exit = exitAfterHandlingStatus(channel, future, response, handler, status, httpRequest) || //
@@ -77,8 +78,6 @@ private boolean exitAfterHandler(Channel channel, NettyResponseFuture<?> future,
7778

7879
if (exit)
7980
finishUpdate(future, channel, HttpHeaders.isTransferEncodingChunked(httpRequest) || HttpHeaders.isTransferEncodingChunked(response));
80-
81-
return exit;
8281
}
8382

8483
private boolean exitAfterHandlingStatus(//
@@ -118,7 +117,7 @@ private boolean exitAfterHandlingReactiveStreams(//
118117
return false;
119118
}
120119

121-
private boolean handleHttpResponse(final HttpResponse response, final Channel channel, final NettyResponseFuture<?> future, AsyncHandler<?> handler) throws Exception {
120+
private void handleHttpResponse(final HttpResponse response, final Channel channel, final NettyResponseFuture<?> future, AsyncHandler<?> handler) throws Exception {
122121

123122
HttpRequest httpRequest = future.getNettyRequest().getHttpRequest();
124123
logger.debug("\n\nRequest {}\n\nResponse {}\n", httpRequest, response);
@@ -128,8 +127,9 @@ private boolean handleHttpResponse(final HttpResponse response, final Channel ch
128127
NettyResponseStatus status = new NettyResponseStatus(future.getUri(), config, response, channel);
129128
HttpResponseHeaders responseHeaders = new HttpResponseHeaders(response.headers());
130129

131-
return interceptors.intercept(channel, future, handler, response, status, responseHeaders)
132-
|| exitAfterHandler(channel, future, response, handler, status, httpRequest, responseHeaders);
130+
if (!interceptors.exitAfterIntercept(channel, future, handler, response, status, responseHeaders)) {
131+
notifyHandler(channel, future, response, handler, status, httpRequest, responseHeaders);
132+
}
133133
}
134134

135135
private void handleChunk(HttpContent chunk,//
@@ -173,9 +173,17 @@ public void handleRead(final Channel channel, final NettyResponseFuture<?> futur
173173

174174
AsyncHandler<?> handler = future.getAsyncHandler();
175175
try {
176-
if (e instanceof HttpResponse) {
177-
if (handleHttpResponse((HttpResponse) e, channel, future, handler))
176+
if (e instanceof HttpObject) {
177+
HttpObject object = (HttpObject) e;
178+
Throwable t = object.getDecoderResult().cause();
179+
if (t != null) {
180+
readFailed(channel, future, t);
178181
return;
182+
}
183+
}
184+
185+
if (e instanceof HttpResponse) {
186+
handleHttpResponse((HttpResponse) e, channel, future, handler);
179187

180188
} else if (e instanceof HttpContent) {
181189
handleChunk((HttpContent) e, channel, future, handler);
@@ -189,16 +197,20 @@ public void handleRead(final Channel channel, final NettyResponseFuture<?> futur
189197
return;
190198
}
191199

192-
try {
193-
requestSender.abort(channel, future, t);
194-
} catch (Exception abortException) {
195-
logger.debug("Abort failed", abortException);
196-
} finally {
197-
finishUpdate(future, channel, false);
198-
}
200+
readFailed(channel, future, t);
199201
throw t;
200202
}
201203
}
204+
205+
private void readFailed(Channel channel, NettyResponseFuture<?> future, Throwable t) throws Exception {
206+
try {
207+
requestSender.abort(channel, future, t);
208+
} catch (Exception abortException) {
209+
logger.debug("Abort failed", abortException);
210+
} finally {
211+
finishUpdate(future, channel, false);
212+
}
213+
}
202214

203215
@Override
204216
public void handleException(NettyResponseFuture<?> future, Throwable error) {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,10 @@ public void handleRead(Channel channel, NettyResponseFuture<?> future, Object e)
140140
HttpResponseStatus status = new NettyResponseStatus(future.getUri(), config, response, channel);
141141
HttpResponseHeaders responseHeaders = new HttpResponseHeaders(response.headers());
142142

143-
if (interceptors.intercept(channel, future, handler, response, status, responseHeaders)) {
144-
return;
143+
if (!interceptors.exitAfterIntercept(channel, future, handler, response, status, responseHeaders)) {
144+
Channels.setAttribute(channel, new UpgradeCallback(future, channel, response, handler, status, responseHeaders));
145145
}
146146

147-
Channels.setAttribute(channel, new UpgradeCallback(future, channel, response, handler, status, responseHeaders));
148147

149148
} else if (e instanceof WebSocketFrame) {
150149

client/src/main/java/org/asynchttpclient/netty/handler/intercept/Interceptors.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Interceptors(//
5555
hasResponseFilters = !config.getResponseFilters().isEmpty();
5656
}
5757

58-
public boolean intercept(//
58+
public boolean exitAfterIntercept(//
5959
Channel channel,//
6060
NettyResponseFuture<?> future,//
6161
AsyncHandler<?> handler,//

0 commit comments

Comments
 (0)