You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by gg...@apache.org on 2017/04/25 22:34:03 UTC

svn commit: r1792667 - in /httpcomponents/httpcore/trunk: httpcore5-h2/src/main/java/org/apache/hc/core5/http2/config/ httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/ httpcore5/src/main/java/org/apache/hc/core5/util/

Author: ggregory
Date: Tue Apr 25 22:34:03 2017
New Revision: 1792667

URL: http://svn.apache.org/viewvc?rev=1792667&view=rev
Log:
Refactor range validation into new Args util methods.

Modified:
    httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/config/H2Config.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/Args.java

Modified: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/config/H2Config.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/config/H2Config.java?rev=1792667&r1=1792666&r2=1792667&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/config/H2Config.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/config/H2Config.java Tue Apr 25 22:34:03 2017
@@ -165,9 +165,8 @@ public class H2Config {
         }
 
         public Builder setMaxFrameSize(final int maxFrameSize) {
-            Args.checkRange(maxFrameSize, FrameConsts.MIN_FRAME_SIZE, FrameConsts.MAX_FRAME_SIZE,
+            this.maxFrameSize = Args.checkRange(maxFrameSize, FrameConsts.MIN_FRAME_SIZE, FrameConsts.MAX_FRAME_SIZE,
                     "Invalid max frame size");
-            this.maxFrameSize = maxFrameSize;
             return this;
         }
 

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java?rev=1792667&r1=1792666&r2=1792667&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java Tue Apr 25 22:34:03 2017
@@ -201,8 +201,7 @@ public final class EntityUtils {
             return null;
         }
         try {
-            Args.checkContentLength(entity);
-            int i = (int)entity.getContentLength();
+            int i = (int) Args.checkContentLength(entity);
             if (i < 0) {
                 i = 4096;
             }
@@ -226,8 +225,7 @@ public final class EntityUtils {
             return null;
         }
         try {
-            Args.checkContentLength(entity);
-            int i = (int) entity.getContentLength();
+            int i = (int) Args.checkContentLength(entity);
             if (i < 0) {
                 i = 4096;
             }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/Args.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/Args.java?rev=1792667&r1=1792666&r2=1792667&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/Args.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/Args.java Tue Apr 25 22:34:03 2017
@@ -51,27 +51,29 @@ public class Args {
         }
     }
 
-    public static void checkRange(final int value, final int lowInclusive, final int highInclusive,
+    public static long checkContentLength(final EntityDetails entityDetails) {
+        // -1 is a special value
+        // 0 is allowed as well
+        return checkRange(entityDetails.getContentLength(), -1, Integer.MAX_VALUE,
+                "HTTP entity too large to be buffered in memory)");
+    }
+
+    public static int checkRange(final int value, final int lowInclusive, final int highInclusive,
             final String message) {
         if (value < lowInclusive || value > highInclusive) {
             throw new IllegalArgumentException(String.format("%s: %,d is out of range [%,d, %,d]", message,
                     Integer.valueOf(value), Integer.valueOf(lowInclusive), Integer.valueOf(highInclusive)));
         }
+        return value;
     }
 
-    public static void checkRange(final long value, final long lowInclusive, final long highInclusive,
+    public static long checkRange(final long value, final long lowInclusive, final long highInclusive,
             final String message) {
         if (value < lowInclusive || value > highInclusive) {
             throw new IllegalArgumentException(String.format("%s: %,d is out of range [%,d, %,d]", message,
                     Long.valueOf(value), Long.valueOf(lowInclusive), Long.valueOf(highInclusive)));
         }
-    }
-
-    public static void checkContentLength(final EntityDetails entityDetails) {
-        // -1 is a special value
-        // 0 is allowed as well
-        checkRange(entityDetails.getContentLength(), -1, Integer.MAX_VALUE,
-                "HTTP entity too large to be buffered in memory)");
+        return value;
     }
 
     public static <T extends CharSequence> T containsNoBlanks(final T argument, final String name) {