Permalink
Browse files

- If content-length is not set but we have enough information to set …

…the content-length, check to see if compression is enabled. If so, do not set the content-length.
  • Loading branch information...
1 parent 2be790b commit 71d54d86e360cca65bb253987e326b1dcd262f2c Ryan Lubke committed Sep 16, 2011
Showing with 26 additions and 10 deletions.
  1. +26 −10 src/main/java/com/ning/http/client/providers/grizzly/GrizzlyAsyncHttpProvider.java
@@ -123,6 +123,8 @@
private final Attribute<HttpTransactionContext> REQUEST_STATE_ATTR =
Grizzly.DEFAULT_ATTRIBUTE_BUILDER.createAttribute(HttpTransactionContext.class.getName());
+ private final BodyHandlerFactory bodyHandlerFactory = new BodyHandlerFactory();
+
private final TCPNIOTransport clientTransport;
private final AsyncHttpClientConfig clientConfig;
private final ConnectionManager connectionManager;
@@ -131,6 +133,7 @@
private DelayedExecutor timeoutExecutor;
+
// ------------------------------------------------------------ Constructors
@@ -420,7 +423,7 @@ private void sendRequest(final FilterChainContext ctx,
if (requestHasEntityBody(request)) {
final HttpTransactionContext context = getHttpTransactionContext(ctx.getConnection());
- BodyHandler handler = BodyHandlerFactory.getBodyHandler(request);
+ BodyHandler handler = bodyHandlerFactory.getBodyHandler(request);
if (requestPacket.getHeaders().contains(Header.Expect)
&& requestPacket.getHeaders().getValue(1).equalsIgnoreCase("100-Continue")) {
handler = new ExpectHandler(handler);
@@ -1453,9 +1456,9 @@ void doHandle(final FilterChainContext ctx,
} // END BodyHandler
- private static final class BodyHandlerFactory {
+ private final class BodyHandlerFactory {
- private static final BodyHandler[] HANDLERS = new BodyHandler[] {
+ private final BodyHandler[] HANDLERS = new BodyHandler[] {
new StringBodyHandler(),
new ByteArrayBodyHandler(),
new ParamsBodyHandler(),
@@ -1466,7 +1469,7 @@ void doHandle(final FilterChainContext ctx,
new BodyGeneratorBodyHandler()
};
- public static BodyHandler getBodyHandler(final Request request) {
+ public BodyHandler getBodyHandler(final Request request) {
for (final BodyHandler h : HANDLERS) {
if (h.handlesBodyType(request)) {
return h;
@@ -1515,7 +1518,7 @@ public void finish(final FilterChainContext ctx) throws IOException {
} // END ContinueHandler
- private static final class ByteArrayBodyHandler implements BodyHandler {
+ private final class ByteArrayBodyHandler implements BodyHandler {
// -------------------------------------------- Methods from BodyHandler
@@ -1537,15 +1540,19 @@ public void doHandle(final FilterChainContext ctx,
final byte[] data = new String(request.getByteData(), charset).getBytes(charset);
final MemoryManager mm = ctx.getMemoryManager();
final Buffer gBuffer = Buffers.wrap(mm, data);
- requestPacket.setContentLengthLong(data.length);
+ if (requestPacket.getContentLength() == -1) {
+ if (!clientConfig.isCompressionEnabled()) {
+ requestPacket.setContentLengthLong(data.length);
+ }
+ }
final HttpContent content = requestPacket.httpContentBuilder().content(gBuffer).build();
content.setLast(true);
ctx.write(content, ((!requestPacket.isCommitted()) ? ctx.getTransportContext().getCompletionHandler() : null));
}
}
- private static final class StringBodyHandler implements BodyHandler {
+ private final class StringBodyHandler implements BodyHandler {
// -------------------------------------------- Methods from BodyHandler
@@ -1568,7 +1575,11 @@ public void doHandle(final FilterChainContext ctx,
final byte[] data = request.getStringData().getBytes(charset);
final MemoryManager mm = ctx.getMemoryManager();
final Buffer gBuffer = Buffers.wrap(mm, data);
- requestPacket.setContentLengthLong(data.length);
+ if (requestPacket.getContentLength() == -1) {
+ if (!clientConfig.isCompressionEnabled()) {
+ requestPacket.setContentLengthLong(data.length);
+ }
+ }
final HttpContent content = requestPacket.httpContentBuilder().content(gBuffer).build();
content.setLast(true);
ctx.write(content, ((!requestPacket.isCommitted()) ? ctx.getTransportContext().getCompletionHandler() : null));
@@ -1601,7 +1612,8 @@ public void doHandle(final FilterChainContext ctx,
} // END StringBodyHandler
- private static final class ParamsBodyHandler implements BodyHandler {
+ private final class ParamsBodyHandler implements BodyHandler {
+
// -------------------------------------------- Methods from BodyHandler
@@ -1646,7 +1658,11 @@ public void doHandle(final FilterChainContext ctx,
final MemoryManager mm = ctx.getMemoryManager();
final Buffer gBuffer = Buffers.wrap(mm, data);
final HttpContent content = requestPacket.httpContentBuilder().content(gBuffer).build();
- requestPacket.setContentLengthLong(data.length);
+ if (requestPacket.getContentLength() == -1) {
+ if (!clientConfig.isCompressionEnabled()) {
+ requestPacket.setContentLengthLong(data.length);
+ }
+ }
content.setLast(true);
ctx.write(content, ((!requestPacket.isCommitted()) ? ctx.getTransportContext().getCompletionHandler() : null));
}

0 comments on commit 71d54d8

Please sign in to comment.