Skip to content

Commit d23b727

Browse files
committed
Instantiate header/cookie collections lazily.
1 parent 2918d8f commit d23b727

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

api/src/main/java/org/asynchttpclient/Request.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ public static interface EntityWriter {
8686
*/
8787
public FluentCaseInsensitiveStringsMap getHeaders();
8888

89+
/**
90+
* @return return <code>true</code> if request headers have been added,
91+
* otherwise, returns <code>false</code>.
92+
*
93+
* @since 2.0
94+
*/
95+
boolean hasHeaders();
96+
8997
/**
9098
* Return Coookie.
9199
*

api/src/main/java/org/asynchttpclient/RequestBuilderBase.java

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ private static final class RequestImpl implements Request {
5454
private URI rawUri;
5555
private InetAddress address;
5656
private InetAddress localAddress;
57-
private FluentCaseInsensitiveStringsMap headers = new FluentCaseInsensitiveStringsMap();
58-
private Collection<Cookie> cookies = new ArrayList<Cookie>();
57+
private FluentCaseInsensitiveStringsMap headers;
58+
private Collection<Cookie> cookies;
5959
private byte[] byteData;
6060
private String stringData;
6161
private InputStream streamData;
@@ -213,12 +213,23 @@ private URI toURI(boolean encode) {
213213

214214
/* @Override */
215215
public FluentCaseInsensitiveStringsMap getHeaders() {
216+
if (headers == null) {
217+
headers = new FluentCaseInsensitiveStringsMap();
218+
}
216219
return headers;
217220
}
218221

222+
@Override
223+
public boolean hasHeaders() {
224+
return headers != null && !headers.isEmpty();
225+
}
226+
219227
/* @Override */
220228
public Collection<Cookie> getCookies() {
221-
return Collections.unmodifiableCollection(cookies);
229+
if (cookies == null) {
230+
cookies = Collections.unmodifiableCollection(Collections.<Cookie>emptyList());
231+
}
232+
return cookies;
222233
}
223234

224235
/* @Override */
@@ -322,12 +333,13 @@ public String toString() {
322333
sb.append("\t");
323334
sb.append(method);
324335
sb.append("\theaders:");
325-
if (headers != null) {
326-
for (String name : headers.keySet()) {
336+
final FluentCaseInsensitiveStringsMap headersLocal = getHeaders();
337+
if (headersLocal != null) {
338+
for (String name : headersLocal.keySet()) {
327339
sb.append("\t");
328340
sb.append(name);
329341
sb.append(":");
330-
sb.append(headers.getJoinedValue(name, ", "));
342+
sb.append(headersLocal.getJoinedValue(name, ", "));
331343
}
332344
}
333345
sb.append("\tparams:");
@@ -436,7 +448,7 @@ public T setVirtualHost(String virtualHost) {
436448
}
437449

438450
public T setHeader(String name, String value) {
439-
request.headers.replace(name, value);
451+
request.getHeaders().replace(name, value);
440452
return derived.cast(this);
441453
}
442454

@@ -446,17 +458,21 @@ public T addHeader(String name, String value) {
446458
value = "";
447459
}
448460

449-
request.headers.add(name, value);
461+
request.getHeaders().add(name, value);
450462
return derived.cast(this);
451463
}
452464

453465
public T setHeaders(FluentCaseInsensitiveStringsMap headers) {
454-
request.headers = (headers == null ? new FluentCaseInsensitiveStringsMap() : new FluentCaseInsensitiveStringsMap(headers));
466+
if (headers != null) {
467+
request.headers = new FluentCaseInsensitiveStringsMap(headers);
468+
}
455469
return derived.cast(this);
456470
}
457471

458472
public T setHeaders(Map<String, Collection<String>> headers) {
459-
request.headers = (headers == null ? new FluentCaseInsensitiveStringsMap() : new FluentCaseInsensitiveStringsMap(headers));
473+
if (headers != null) {
474+
request.headers = new FluentCaseInsensitiveStringsMap(headers);
475+
}
460476
return derived.cast(this);
461477
}
462478

@@ -466,6 +482,9 @@ public T setContentLength(int length) {
466482
}
467483

468484
public T addCookie(Cookie cookie) {
485+
if (request.cookies == null) {
486+
request.cookies = new ArrayList<Cookie>();
487+
}
469488
request.cookies.add(cookie);
470489
return derived.cast(this);
471490
}
@@ -639,7 +658,10 @@ public T setConnectionPoolKeyStrategy(ConnectionPoolKeyStrategy connectionPoolKe
639658
public Request build() {
640659
if ((request.length < 0) && (request.streamData == null) && allowBody(request.getMethod())) {
641660
// can't concatenate content-length
642-
String contentLength = request.headers.getFirstValue("Content-Length");
661+
String contentLength = null;
662+
if (request.headers != null && request.headers.isEmpty()) {
663+
contentLength = request.headers.getFirstValue("Content-Length");
664+
}
643665

644666
if (contentLength != null) {
645667
try {
@@ -649,6 +671,9 @@ public Request build() {
649671
}
650672
}
651673
}
674+
if (request.cookies != null) {
675+
request.cookies = Collections.unmodifiableCollection(request.cookies);
676+
}
652677
return request;
653678
}
654679

@@ -660,6 +685,11 @@ public T addOrReplaceCookie(Cookie cookie) {
660685
String cookieKey = cookie.getName();
661686
boolean replace = false;
662687
int index = 0;
688+
if (request.cookies == null) {
689+
request.cookies = new ArrayList<Cookie>();
690+
request.cookies.add(cookie);
691+
return derived.cast(this);
692+
}
663693
for (Cookie c : request.cookies) {
664694
if (c.getName().equals(cookieKey)) {
665695
replace = true;

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/filters/AsyncHttpClientFilter.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,12 @@ private static FilterChainContext checkAndHandleFilterChainUpdate(final FilterCh
285285
private static void initTransferCompletionHandler(final Request request,
286286
final AsyncHandler h)
287287
throws IOException {
288-
if (h instanceof TransferCompletionHandler) {
289-
final FluentCaseInsensitiveStringsMap map = new FluentCaseInsensitiveStringsMap(request.getHeaders());
290-
TransferCompletionHandler.class.cast(h).transferAdapter(new GrizzlyTransferAdapter(map));
291-
}
288+
if (h instanceof TransferCompletionHandler) {
289+
final FluentCaseInsensitiveStringsMap map =
290+
new FluentCaseInsensitiveStringsMap(request.getHeaders());
291+
TransferCompletionHandler.class.cast(h)
292+
.transferAdapter(new GrizzlyTransferAdapter(map));
293+
}
292294
}
293295

294296
private static boolean checkHandshakeError(final FilterChainContext ctx,
@@ -369,14 +371,15 @@ private static void convertToUpgradeRequest(final HttpTransactionContext ctx) {
369371
private void addGeneralHeaders(final Request request,
370372
final HttpRequestPacket requestPacket) {
371373

372-
final FluentCaseInsensitiveStringsMap map = request.getHeaders();
373-
if (isNonEmpty(map)) {
374+
if (request.hasHeaders()) {
375+
final FluentCaseInsensitiveStringsMap map = request.getHeaders();
374376
for (final Map.Entry<String, List<String>> entry : map.entrySet()) {
375377
final String headerName = entry.getKey();
376378
final List<String> headerValues = entry.getValue();
377379
if (isNonEmpty(headerValues)) {
378380
for (int i = 0, len = headerValues.size(); i < len; i++) {
379-
requestPacket.addHeader(headerName, headerValues.get(i));
381+
requestPacket.addHeader(headerName,
382+
headerValues.get(i));
380383
}
381384
}
382385
}

0 commit comments

Comments
 (0)