Skip to content

Commit 8edfd54

Browse files
author
oleksiys
committed
[master] fix issue AsyncHttpClient#564
AsyncHttpClient#564 "emtpy HTTP delete message is sent using chunked transfer-encoding for no reason"
1 parent 9024e4a commit 8edfd54

13 files changed

+126
-64
lines changed

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/Utils.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013 Sonatype, Inc. All rights reserved.
2+
* Copyright (c) 2013-2014 Sonatype, Inc. All rights reserved.
33
*
44
* This program is licensed to you under the Apache License Version 2.0,
55
* and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -13,12 +13,10 @@
1313

1414
package org.asynchttpclient.providers.grizzly;
1515

16-
import org.asynchttpclient.Request;
1716
import org.glassfish.grizzly.Connection;
1817
import org.glassfish.grizzly.Grizzly;
1918
import org.glassfish.grizzly.attributes.Attribute;
2019
import org.glassfish.grizzly.attributes.AttributeStorage;
21-
import org.glassfish.grizzly.http.Method;
2220

2321
import java.net.URI;
2422
import java.util.concurrent.atomic.AtomicInteger;
@@ -86,13 +84,4 @@ public static boolean isSpdyConnection(final Connection c) {
8684
Boolean result = SPDY.get(c);
8785
return result != null ? result : false;
8886
}
89-
90-
public static boolean requestHasEntityBody(final Request request) {
91-
92-
final String method = request.getMethod();
93-
return Method.POST.matchesMethod(method)//
94-
|| Method.PUT.matchesMethod(method)//
95-
|| Method.PATCH.matchesMethod(method)//
96-
|| Method.DELETE.matchesMethod(method);
97-
}
9887
}

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/bodyhandler/BodyGeneratorBodyHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013 Sonatype, Inc. All rights reserved.
2+
* Copyright (c) 2013-2014 Sonatype, Inc. All rights reserved.
33
*
44
* This program is licensed to you under the Apache License Version 2.0,
55
* and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -26,7 +26,7 @@
2626

2727
import java.io.IOException;
2828

29-
public final class BodyGeneratorBodyHandler implements BodyHandler {
29+
public final class BodyGeneratorBodyHandler extends BodyHandler {
3030

3131
// -------------------------------------------- Methods from BodyHandler
3232

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/bodyhandler/BodyHandler.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013 Sonatype, Inc. All rights reserved.
2+
* Copyright (c) 2013-2014 Sonatype, Inc. All rights reserved.
33
*
44
* This program is licensed to you under the Apache License Version 2.0,
55
* and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -19,11 +19,25 @@
1919

2020
import java.io.IOException;
2121

22-
public interface BodyHandler {
22+
public abstract class BodyHandler {
2323

24-
static int MAX_CHUNK_SIZE = 8192;
24+
public static int MAX_CHUNK_SIZE = 8192;
2525

26-
boolean handlesBodyType(final Request request);
26+
public abstract boolean handlesBodyType(final Request request);
2727

28-
boolean doHandle(final FilterChainContext ctx, final Request request, final HttpRequestPacket requestPacket) throws IOException;
28+
public abstract boolean doHandle(final FilterChainContext ctx,
29+
final Request request, final HttpRequestPacket requestPacket)
30+
throws IOException;
31+
32+
/**
33+
* Tries to predict request content-length based on the {@link Request}.
34+
* Not all the <tt>BodyHandler</tt>s can predict the content-length in advance.
35+
*
36+
* @param request
37+
* @return the content-length, or <tt>-1</tt> if the content-length can't be
38+
* predicted
39+
*/
40+
protected long getContentLength(final Request request) {
41+
return request.getContentLength();
42+
}
2943
}

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/bodyhandler/BodyHandlerFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013 Sonatype, Inc. All rights reserved.
2+
* Copyright (c) 2013-2014 Sonatype, Inc. All rights reserved.
33
*
44
* This program is licensed to you under the Apache License Version 2.0,
55
* and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -27,7 +27,7 @@ public BodyHandlerFactory(GrizzlyAsyncHttpProvider grizzlyAsyncHttpProvider) {
2727
new ParamsBodyHandler(grizzlyAsyncHttpProvider),//
2828
new StreamDataBodyHandler(),//
2929
new PartsBodyHandler(),//
30-
new FileBodyHandler(),//
30+
new FileBodyHandler(grizzlyAsyncHttpProvider),//
3131
new BodyGeneratorBodyHandler() //
3232
};
3333
}
@@ -39,7 +39,8 @@ public BodyHandler getBodyHandler(final Request request) {
3939
return h;
4040
}
4141
}
42-
return new NoBodyHandler();
42+
43+
return null;
4344
}
4445

4546
} // END BodyHandlerFactory

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/bodyhandler/ByteArrayBodyHandler.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013 Sonatype, Inc. All rights reserved.
2+
* Copyright (c) 2013-2014 Sonatype, Inc. All rights reserved.
33
*
44
* This program is licensed to you under the Apache License Version 2.0,
55
* and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -24,11 +24,12 @@
2424

2525
import java.io.IOException;
2626

27-
public final class ByteArrayBodyHandler implements BodyHandler {
27+
public final class ByteArrayBodyHandler extends BodyHandler {
2828

2929
private final boolean compressionEnabled;
3030

31-
public ByteArrayBodyHandler(GrizzlyAsyncHttpProvider grizzlyAsyncHttpProvider) {
31+
public ByteArrayBodyHandler(
32+
final GrizzlyAsyncHttpProvider grizzlyAsyncHttpProvider) {
3233
compressionEnabled = grizzlyAsyncHttpProvider.getClientConfig().isCompressionEnabled();
3334
}
3435

@@ -39,7 +40,8 @@ public boolean handlesBodyType(final Request request) {
3940
}
4041

4142
@SuppressWarnings({ "unchecked" })
42-
public boolean doHandle(final FilterChainContext ctx, final Request request, final HttpRequestPacket requestPacket) throws IOException {
43+
public boolean doHandle(final FilterChainContext ctx, final Request request,
44+
final HttpRequestPacket requestPacket) throws IOException {
4345

4446
final byte[] data = request.getByteData();
4547
final MemoryManager mm = ctx.getMemoryManager();
@@ -54,4 +56,13 @@ public boolean doHandle(final FilterChainContext ctx, final Request request, fin
5456
ctx.write(content, ((!requestPacket.isCommitted()) ? ctx.getTransportContext().getCompletionHandler() : null));
5557
return true;
5658
}
59+
60+
@Override
61+
protected long getContentLength(final Request request) {
62+
if (request.getContentLength() >= 0) {
63+
return request.getContentLength();
64+
}
65+
66+
return compressionEnabled ? -1 : request.getByteData().length;
67+
}
5768
}

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/bodyhandler/ExpectHandler.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013 Sonatype, Inc. All rights reserved.
2+
* Copyright (c) 2013-2014 Sonatype, Inc. All rights reserved.
33
*
44
* This program is licensed to you under the Apache License Version 2.0,
55
* and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -19,7 +19,7 @@
1919

2020
import java.io.IOException;
2121

22-
public final class ExpectHandler implements BodyHandler {
22+
public final class ExpectHandler extends BodyHandler {
2323

2424
private final BodyHandler delegate;
2525
private Request request;
@@ -41,6 +41,13 @@ public boolean handlesBodyType(Request request) {
4141
public boolean doHandle(FilterChainContext ctx, Request request, HttpRequestPacket requestPacket) throws IOException {
4242
this.request = request;
4343
this.requestPacket = requestPacket;
44+
45+
// Set content-length if possible
46+
final long contentLength = delegate.getContentLength(request);
47+
if (contentLength != -1) {
48+
requestPacket.setContentLengthLong(contentLength);
49+
}
50+
4451
ctx.write(requestPacket, ((!requestPacket.isCommitted()) ? ctx.getTransportContext().getCompletionHandler() : null));
4552
return true;
4653
}

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/bodyhandler/FileBodyHandler.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013 Sonatype, Inc. All rights reserved.
2+
* Copyright (c) 2013-2014 Sonatype, Inc. All rights reserved.
33
*
44
* This program is licensed to you under the Apache License Version 2.0,
55
* and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -31,14 +31,22 @@
3131
import java.io.FileInputStream;
3232
import java.io.IOException;
3333
import java.util.concurrent.atomic.AtomicInteger;
34+
import org.asynchttpclient.providers.grizzly.GrizzlyAsyncHttpProvider;
3435

35-
public final class FileBodyHandler implements BodyHandler {
36+
public final class FileBodyHandler extends BodyHandler {
3637

3738
private static final boolean SEND_FILE_SUPPORT;
3839
static {
3940
SEND_FILE_SUPPORT = configSendFileSupport();
4041
}
4142

43+
private final boolean compressionEnabled;
44+
45+
public FileBodyHandler(
46+
final GrizzlyAsyncHttpProvider grizzlyAsyncHttpProvider) {
47+
compressionEnabled = grizzlyAsyncHttpProvider.getClientConfig().isCompressionEnabled();
48+
}
49+
4250
// ------------------------------------------------ Methods from BodyHandler
4351

4452
public boolean handlesBodyType(final Request request) {
@@ -51,7 +59,7 @@ public boolean doHandle(final FilterChainContext ctx, final Request request, fin
5159
final File f = request.getFile();
5260
requestPacket.setContentLengthLong(f.length());
5361
final HttpTxContext context = HttpTxContext.get(ctx);
54-
if (!SEND_FILE_SUPPORT || requestPacket.isSecure()) {
62+
if (compressionEnabled || !SEND_FILE_SUPPORT || requestPacket.isSecure()) {
5563
final FileInputStream fis = new FileInputStream(request.getFile());
5664
final MemoryManager mm = ctx.getMemoryManager();
5765
AtomicInteger written = new AtomicInteger();
@@ -98,6 +106,15 @@ public void completed(WriteResult result) {
98106
return true;
99107
}
100108

109+
@Override
110+
protected long getContentLength(final Request request) {
111+
if (request.getContentLength() >= 0) {
112+
return request.getContentLength();
113+
}
114+
115+
return compressionEnabled ? -1 : request.getFile().length();
116+
}
117+
101118
// --------------------------------------------------------- Private Methods
102119

103120
private static void notifyHandlerIfNeeded(final HttpTxContext context, final HttpRequestPacket requestPacket,

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/bodyhandler/NoBodyHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013 Sonatype, Inc. All rights reserved.
2+
* Copyright (c) 2013-2014 Sonatype, Inc. All rights reserved.
33
*
44
* This program is licensed to you under the Apache License Version 2.0,
55
* and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -21,7 +21,7 @@
2121

2222
import java.io.IOException;
2323

24-
public final class NoBodyHandler implements BodyHandler {
24+
public final class NoBodyHandler extends BodyHandler {
2525

2626
// -------------------------------------------- Methods from BodyHandler
2727

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/bodyhandler/ParamsBodyHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013 Sonatype, Inc. All rights reserved.
2+
* Copyright (c) 2013-2014 Sonatype, Inc. All rights reserved.
33
*
44
* This program is licensed to you under the Apache License Version 2.0,
55
* and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -31,7 +31,7 @@
3131
import java.util.List;
3232
import java.util.Map;
3333

34-
public final class ParamsBodyHandler implements BodyHandler {
34+
public final class ParamsBodyHandler extends BodyHandler {
3535

3636
private final boolean compressionEnabled;
3737

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/bodyhandler/PartsBodyHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013 Sonatype, Inc. All rights reserved.
2+
* Copyright (c) 2013-2014 Sonatype, Inc. All rights reserved.
33
*
44
* This program is licensed to you under the Apache License Version 2.0,
55
* and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -31,7 +31,7 @@
3131
import java.io.IOException;
3232
import java.util.List;
3333

34-
public final class PartsBodyHandler implements BodyHandler {
34+
public final class PartsBodyHandler extends BodyHandler {
3535

3636
// -------------------------------------------- Methods from BodyHandler
3737

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/bodyhandler/StreamDataBodyHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013 Sonatype, Inc. All rights reserved.
2+
* Copyright (c) 2013-2014 Sonatype, Inc. All rights reserved.
33
*
44
* This program is licensed to you under the Apache License Version 2.0,
55
* and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -25,7 +25,7 @@
2525
import java.io.IOException;
2626
import java.io.InputStream;
2727

28-
public final class StreamDataBodyHandler implements BodyHandler {
28+
public final class StreamDataBodyHandler extends BodyHandler {
2929

3030
// -------------------------------------------- Methods from BodyHandler
3131

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/bodyhandler/StringBodyHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013 Sonatype, Inc. All rights reserved.
2+
* Copyright (c) 2013-2014 Sonatype, Inc. All rights reserved.
33
*
44
* This program is licensed to you under the Apache License Version 2.0,
55
* and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -25,7 +25,7 @@
2525

2626
import java.io.IOException;
2727

28-
public final class StringBodyHandler implements BodyHandler {
28+
public final class StringBodyHandler extends BodyHandler {
2929
private final GrizzlyAsyncHttpProvider grizzlyAsyncHttpProvider;
3030

3131
public StringBodyHandler(GrizzlyAsyncHttpProvider grizzlyAsyncHttpProvider) {

0 commit comments

Comments
 (0)