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:12:38 UTC
svn commit: r1792665 - in /httpcomponents/httpcore/trunk:
httpcore5-h2/src/main/java/org/apache/hc/core5/http2/config/
httpcore5/src/main/java/org/apache/hc/core5/http/impl/
httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/
httpcore5/src/main...
Author: ggregory
Date: Tue Apr 25 22:12:38 2017
New Revision: 1792665
URL: http://svn.apache.org/viewvc?rev=1792665&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/impl/EnglishReasonPhraseCatalog.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
httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestArgs.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=1792665&r1=1792664&r2=1792665&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:12:38 2017
@@ -165,7 +165,8 @@ public class H2Config {
}
public Builder setMaxFrameSize(final int maxFrameSize) {
- Args.check(maxFrameSize >= FrameConsts.MIN_FRAME_SIZE && maxFrameSize <= FrameConsts.MAX_FRAME_SIZE, "Invalid max frame size");
+ 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/impl/EnglishReasonPhraseCatalog.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/EnglishReasonPhraseCatalog.java?rev=1792665&r1=1792664&r2=1792665&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/EnglishReasonPhraseCatalog.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/EnglishReasonPhraseCatalog.java Tue Apr 25 22:12:38 2017
@@ -74,7 +74,7 @@ public class EnglishReasonPhraseCatalog
*/
@Override
public String getReason(final int status, final Locale loc) {
- Args.check(status >= 100 && status < 600, "Unknown category for status code " + status);
+ Args.checkRange(status, 100, 599, "Unknown category for status code");
final int category = status / 100;
final int subcode = status - 100*category;
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=1792665&r1=1792664&r2=1792665&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:12:38 2017
@@ -201,8 +201,7 @@ public final class EntityUtils {
return null;
}
try {
- Args.check(entity.getContentLength() <= Integer.MAX_VALUE,
- "HTTP entity too large to be buffered in memory");
+ Args.checkContentLength(entity);
int i = (int)entity.getContentLength();
if (i < 0) {
i = 4096;
@@ -227,9 +226,8 @@ public final class EntityUtils {
return null;
}
try {
- Args.check(entity.getContentLength() <= Integer.MAX_VALUE,
- "HTTP entity too large to be buffered in memory");
- int i = (int)entity.getContentLength();
+ Args.checkContentLength(entity);
+ int i = (int) entity.getContentLength();
if (i < 0) {
i = 4096;
}
@@ -351,7 +349,7 @@ public final class EntityUtils {
return Collections.emptyList();
}
final long len = entity.getContentLength();
- Args.check(len <= Integer.MAX_VALUE, "HTTP entity is too large");
+ Args.checkRange(len, 0, Integer.MAX_VALUE, "HTTP entity is too large");
final Charset charset = contentType.getCharset() != null ? contentType.getCharset() : StandardCharsets.ISO_8859_1;
final InputStream instream = entity.getContent();
if (instream == null) {
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=1792665&r1=1792664&r2=1792665&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:12:38 2017
@@ -29,6 +29,8 @@ package org.apache.hc.core5.util;
import java.util.Collection;
+import org.apache.hc.core5.http.EntityDetails;
+
public class Args {
public static void check(final boolean expression, final String message) {
@@ -49,6 +51,29 @@ public class Args {
}
}
+ public static void 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)));
+ }
+ }
+
+ public static void 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)");
+ }
+
public static <T extends CharSequence> T containsNoBlanks(final T argument, final String name) {
if (argument == null) {
throw new IllegalArgumentException(name + " may not be null");
Modified: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestArgs.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestArgs.java?rev=1792665&r1=1792664&r2=1792665&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestArgs.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestArgs.java Tue Apr 25 22:12:38 2017
@@ -167,4 +167,62 @@ public class TestArgs {
Args.notNegative(-1L, "Number");
}
+ //
+ public void testIntSmallestRangeOK() {
+ Args.checkRange(0, 0, 0, "Number");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testIntSmallestRangeFailLow() {
+ Args.checkRange(-1, 0, 0, "Number");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testIntRangeFailLow() {
+ Args.checkRange(-101, -100, 100, "Number");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testIntRangeFailHigh() {
+ Args.checkRange(101, -100, 100, "Number");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testIntSmallestRangeFailHigh() {
+ Args.checkRange(1, 0, 0, "Number");
+ }
+
+ public void testIntFullRangeOK() {
+ Args.checkRange(0, Integer.MIN_VALUE, Integer.MAX_VALUE, "Number");
+ }
+
+ //
+ public void testLongSmallestRangeOK() {
+ Args.checkRange(0L, 0L, 0L, "Number");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testLongSmallestRangeFailLow() {
+ Args.checkRange(-1L, 0L, 0L, "Number");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testLongRangeFailLow() {
+ Args.checkRange(-101L, -100L, 100L, "Number");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testLongRangeFailHigh() {
+ Args.checkRange(101L, -100L, 100L, "Number");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testLongSmallestRangeFailHigh() {
+ Args.checkRange(1L, 0L, 0L, "Number");
+ }
+
+ public void testLongFullRangeOK() {
+ Args.checkRange(0L, Long.MIN_VALUE, Long.MAX_VALUE, "Number");
+ }
+
}