hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject httpcomponents-core git commit: HTTPCORE-491 Make BasicAsyncRequest|ResponseConsumer more paranoid
Date Wed, 27 Sep 2017 15:05:45 GMT
Repository: httpcomponents-core
Updated Branches:
  refs/heads/4.4.x f4c2bdb0b -> 47411d22b


HTTPCORE-491 Make BasicAsyncRequest|ResponseConsumer more paranoid

BasicAsyncRequestConsumer and BasicAsyncResponseConsumer used to
blindly pre-allocate a buffer of Content-Length. If the request / response
is crafted to have a very large Content-Length header, this can result in
OutOfMemoryErrors.

This limits the initial buffer size to 256kb, while still allowing the
buffer to grow if a larger message is read.


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/47411d22
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/47411d22
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/47411d22

Branch: refs/heads/4.4.x
Commit: 47411d22b3150010ac794a166b7c284be22b82cf
Parents: f4c2bdb
Author: Michael Heemskerk <mheemskerk@atlassian.com>
Authored: Wed Sep 27 16:00:30 2017 +0200
Committer: Oleg Kalnichevski <olegk@apache.org>
Committed: Wed Sep 27 17:00:26 2017 +0200

----------------------------------------------------------------------
 .../org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java | 5 ++++-
 .../apache/http/nio/protocol/BasicAsyncResponseConsumer.java    | 5 ++++-
 2 files changed, 8 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/47411d22/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java
----------------------------------------------------------------------
diff --git a/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java
b/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java
index aca38da..9508506 100644
--- a/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java
+++ b/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java
@@ -50,6 +50,8 @@ import org.apache.http.util.Asserts;
  */
 public class BasicAsyncRequestConsumer extends AbstractAsyncRequestConsumer<HttpRequest>
{
 
+    private static final int MAX_INITIAL_BUFFER_SIZE = 256 * 1024;
+
     private volatile HttpRequest request;
     private volatile SimpleInputBuffer buf;
 
@@ -72,7 +74,8 @@ public class BasicAsyncRequestConsumer extends AbstractAsyncRequestConsumer<Http
         if (len < 0) {
             len = 4096;
         }
-        this.buf = new SimpleInputBuffer((int) len, new HeapByteBufferAllocator());
+        final int initialBufferSize = Math.min((int) len, MAX_INITIAL_BUFFER_SIZE);
+        this.buf = new SimpleInputBuffer(initialBufferSize, new HeapByteBufferAllocator());
         ((HttpEntityEnclosingRequest) this.request).setEntity(
                 new ContentBufferEntity(entity, this.buf));
     }

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/47411d22/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java
----------------------------------------------------------------------
diff --git a/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java
b/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java
index 10c3c40..a42aaa6 100644
--- a/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java
+++ b/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java
@@ -49,6 +49,8 @@ import org.apache.http.util.Asserts;
  */
 public class BasicAsyncResponseConsumer extends AbstractAsyncResponseConsumer<HttpResponse>
{
 
+    private static final int MAX_INITIAL_BUFFER_SIZE = 256 * 1024;
+
     private volatile HttpResponse response;
     private volatile SimpleInputBuffer buf;
 
@@ -71,7 +73,8 @@ public class BasicAsyncResponseConsumer extends AbstractAsyncResponseConsumer<Ht
         if (len < 0) {
             len = 4096;
         }
-        this.buf = new SimpleInputBuffer((int) len, new HeapByteBufferAllocator());
+        final int initialBufferSize = Math.min((int) len, MAX_INITIAL_BUFFER_SIZE);
+        this.buf = new SimpleInputBuffer(initialBufferSize, new HeapByteBufferAllocator());
         this.response.setEntity(new ContentBufferEntity(entity, this.buf));
     }
 


Mime
View raw message