Skip to content

Commit 1bc342b

Browse files
committed
Drop NettyResponseHeaders, don't buffer response headers there, but in ResponseBuilder
1 parent 13eda89 commit 1bc342b

File tree

6 files changed

+27
-124
lines changed

6 files changed

+27
-124
lines changed

client/src/main/java/org/asynchttpclient/HttpResponseHeaders.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,28 @@
1717

1818
import io.netty.handler.codec.http.HttpHeaders;
1919

20-
2120
/**
2221
* A class that represent the HTTP headers.
2322
*/
24-
public abstract class HttpResponseHeaders {
23+
public class HttpResponseHeaders {
2524

26-
private final boolean traillingHeaders;
25+
private final HttpHeaders headers;
26+
private final boolean trailling;
2727

28-
public HttpResponseHeaders() {
29-
this.traillingHeaders = false;
28+
public HttpResponseHeaders(HttpHeaders headers) {
29+
this(headers, false);
3030
}
3131

32-
public HttpResponseHeaders(boolean traillingHeaders) {
33-
this.traillingHeaders = traillingHeaders;
32+
public HttpResponseHeaders(HttpHeaders headers, boolean trailling) {
33+
this.headers = headers;
34+
this.trailling = trailling;
3435
}
3536

36-
/**
37-
* Return the HTTP header
38-
*
39-
* @return an {@link HttpHeaders}
40-
*/
41-
abstract public HttpHeaders getHeaders();
37+
public HttpHeaders getHeaders() {
38+
return headers;
39+
}
4240

43-
/**
44-
* Return true is headers has been received after the response body.
45-
*
46-
* @return true is headers has been received after the response body.
47-
*/
48-
public boolean isTraillingHeadersReceived() {
49-
return traillingHeaders;
41+
public boolean isTrailling() {
42+
return trailling;
5043
}
5144
}

client/src/main/java/org/asynchttpclient/Response.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ public interface Response {
7070
/**
7171
* Return the entire response body as a String.
7272
*
73-
* @param charset
74-
* the charset to use when decoding the stream
73+
* @param charset the charset to use when decoding the stream
7574
* @return the entire response body as a String.
7675
*/
7776
String getResponseBody(Charset charset);
@@ -158,19 +157,17 @@ public interface Response {
158157
/**
159158
* Get remote address client initiated request to.
160159
*
161-
* @return remote address client initiated request to, may be {@code null}
162-
* if asynchronous provider is unable to provide the remote address
160+
* @return remote address client initiated request to, may be {@code null} if asynchronous provider is unable to provide the remote address
163161
*/
164162
SocketAddress getRemoteAddress();
165163

166164
/**
167165
* Get local address client initiated request from.
168166
*
169-
* @return local address client initiated request from, may be {@code null}
170-
* if asynchronous provider is unable to provide the local address
167+
* @return local address client initiated request from, may be {@code null} if asynchronous provider is unable to provide the local address
171168
*/
172169
SocketAddress getLocalAddress();
173-
170+
174171
class ResponseBuilder {
175172
private final List<HttpResponseBodyPart> bodyParts = new ArrayList<>();
176173
private HttpResponseStatus status;
@@ -182,13 +179,12 @@ public ResponseBuilder accumulate(HttpResponseStatus status) {
182179
}
183180

184181
public ResponseBuilder accumulate(HttpResponseHeaders headers) {
185-
this.headers = headers;
182+
this.headers = this.headers == null ? headers : new HttpResponseHeaders(this.headers.getHeaders().add(headers.getHeaders()), true);
186183
return this;
187184
}
188185

189186
/**
190-
* @param bodyPart
191-
* a body part (possibly empty, but will be filtered out)
187+
* @param bodyPart a body part (possibly empty, but will be filtered out)
192188
* @return this
193189
*/
194190
public ResponseBuilder accumulate(HttpResponseBodyPart bodyPart) {

client/src/main/java/org/asynchttpclient/netty/NettyResponseFuture.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import static org.asynchttpclient.util.DateUtils.millisTime;
1717
import static org.asynchttpclient.util.MiscUtils.getCause;
1818
import io.netty.channel.Channel;
19-
import io.netty.handler.codec.http.HttpHeaders;
2019

2120
import java.net.SocketAddress;
2221
import java.util.concurrent.CancellationException;
@@ -84,7 +83,6 @@ public final class NettyResponseFuture<V> extends AbstractListenableFuture<V> {
8483
private Request targetRequest;
8584
private Request currentRequest;
8685
private NettyRequest nettyRequest;
87-
private HttpHeaders httpHeaders;
8886
private AsyncHandler<V> asyncHandler;
8987
private boolean streamWasAlreadyConsumed;
9088
private boolean reuseChannel;
@@ -321,14 +319,6 @@ public final void setKeepAlive(final boolean keepAlive) {
321319
this.keepAlive = keepAlive;
322320
}
323321

324-
public final HttpHeaders getHttpHeaders() {
325-
return httpHeaders;
326-
}
327-
328-
public final void setHttpHeaders(HttpHeaders httpHeaders) {
329-
this.httpHeaders = httpHeaders;
330-
}
331-
332322
public int incrementAndGetCurrentRedirectCount() {
333323
return redirectCount.incrementAndGet();
334324
}
@@ -477,7 +467,6 @@ public String toString() {
477467
",\n\tcontent=" + content + //
478468
",\n\turi=" + getUri() + //
479469
",\n\tkeepAlive=" + keepAlive + //
480-
",\n\thttpHeaders=" + httpHeaders + //
481470
",\n\texEx=" + exEx + //
482471
",\n\tredirectCount=" + redirectCount + //
483472
",\n\ttimeoutsHolder=" + timeoutsHolder + //

client/src/main/java/org/asynchttpclient/netty/NettyResponseHeaders.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.asynchttpclient.netty.Callback;
3939
import org.asynchttpclient.netty.NettyResponseBodyPart;
4040
import org.asynchttpclient.netty.NettyResponseFuture;
41-
import org.asynchttpclient.netty.NettyResponseHeaders;
4241
import org.asynchttpclient.netty.NettyResponseStatus;
4342
import org.asynchttpclient.netty.channel.ChannelManager;
4443
import org.asynchttpclient.netty.channel.Channels;
@@ -84,14 +83,13 @@ public void call() throws Exception {
8483
Request request = future.getCurrentRequest();
8584

8685
HttpResponseStatus status = new NettyResponseStatus(future.getUri(), config, response, channel);
87-
HttpResponseHeaders responseHeaders = new NettyResponseHeaders(response.headers());
86+
HttpResponseHeaders responseHeaders = new HttpResponseHeaders(response.headers());
8887
Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm();
8988

9089
if (exitAfterProcessingFilters(channel, future, handler, status, responseHeaders)) {
9190
return;
9291
}
9392

94-
future.setHttpHeaders(response.headers());
9593
if (exitAfterHandlingRedirect(channel, future, response, request, response.getStatus().code(), realm))
9694
return;
9795

client/src/test/java/org/asynchttpclient/netty/NettyAsyncResponseTest.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,8 @@ public void testCookieParseExpires() {
3838
Date date = new Date(System.currentTimeMillis() + 60000);
3939
final String cookieDef = String.format("efmembercheck=true; expires=%s; path=/; domain=.eclipse.org", sdf.format(date));
4040

41-
NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null, null), new HttpResponseHeaders() {
42-
@Override
43-
public HttpHeaders getHeaders() {
44-
return new DefaultHttpHeaders().add(HttpHeaders.Names.SET_COOKIE, cookieDef);
45-
}
46-
}, null);
41+
HttpResponseHeaders responseHeaders = new HttpResponseHeaders(new DefaultHttpHeaders().add(HttpHeaders.Names.SET_COOKIE, cookieDef));
42+
NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null, null), responseHeaders, null);
4743

4844
List<Cookie> cookies = response.getCookies();
4945
assertEquals(cookies.size(), 1);
@@ -55,12 +51,9 @@ public HttpHeaders getHeaders() {
5551
@Test(groups = "standalone")
5652
public void testCookieParseMaxAge() {
5753
final String cookieDef = "efmembercheck=true; max-age=60; path=/; domain=.eclipse.org";
58-
NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null, null), new HttpResponseHeaders() {
59-
@Override
60-
public HttpHeaders getHeaders() {
61-
return new DefaultHttpHeaders().add(HttpHeaders.Names.SET_COOKIE, cookieDef);
62-
}
63-
}, null);
54+
55+
HttpResponseHeaders responseHeaders = new HttpResponseHeaders(new DefaultHttpHeaders().add(HttpHeaders.Names.SET_COOKIE, cookieDef));
56+
NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null, null), responseHeaders, null);
6457
List<Cookie> cookies = response.getCookies();
6558
assertEquals(cookies.size(), 1);
6659

@@ -71,12 +64,8 @@ public HttpHeaders getHeaders() {
7164
@Test(groups = "standalone")
7265
public void testCookieParseWeirdExpiresValue() {
7366
final String cookieDef = "efmembercheck=true; expires=60; path=/; domain=.eclipse.org";
74-
NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null, null), new HttpResponseHeaders() {
75-
@Override
76-
public HttpHeaders getHeaders() {
77-
return new DefaultHttpHeaders().add(HttpHeaders.Names.SET_COOKIE, cookieDef);
78-
}
79-
}, null);
67+
HttpResponseHeaders responseHeaders = new HttpResponseHeaders(new DefaultHttpHeaders().add(HttpHeaders.Names.SET_COOKIE, cookieDef));
68+
NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null, null), responseHeaders, null);
8069

8170
List<Cookie> cookies = response.getCookies();
8271
assertEquals(cookies.size(), 1);

0 commit comments

Comments
 (0)