You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2017/05/09 19:58:20 UTC
[28/50] [abbrv] httpcomponents-core git commit: Refactor range
validation into new Args util methods.
Refactor range validation into new Args util methods.
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk@1792665 13f79535-47bb-0310-9956-ffa450edef68
Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/65b1b583
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/65b1b583
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/65b1b583
Branch: refs/heads/trunk
Commit: 65b1b583fd7259dd24cb8ee74b575a356261b6f8
Parents: 0346029
Author: Gary D. Gregory <gg...@apache.org>
Authored: Tue Apr 25 22:12:38 2017 +0000
Committer: Gary D. Gregory <gg...@apache.org>
Committed: Tue Apr 25 22:12:38 2017 +0000
----------------------------------------------------------------------
.../apache/hc/core5/http2/config/H2Config.java | 3 +-
.../http/impl/EnglishReasonPhraseCatalog.java | 2 +-
.../hc/core5/http/io/entity/EntityUtils.java | 10 ++--
.../java/org/apache/hc/core5/util/Args.java | 25 +++++++++
.../java/org/apache/hc/core5/util/TestArgs.java | 58 ++++++++++++++++++++
5 files changed, 90 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/65b1b583/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/config/H2Config.java
----------------------------------------------------------------------
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/config/H2Config.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/config/H2Config.java
index 56ce70d..b7b5e45 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/config/H2Config.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/config/H2Config.java
@@ -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;
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/65b1b583/httpcore5/src/main/java/org/apache/hc/core5/http/impl/EnglishReasonPhraseCatalog.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/EnglishReasonPhraseCatalog.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/EnglishReasonPhraseCatalog.java
index 1514804..39b3a43 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/EnglishReasonPhraseCatalog.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/EnglishReasonPhraseCatalog.java
@@ -74,7 +74,7 @@ public class EnglishReasonPhraseCatalog implements ReasonPhraseCatalog {
*/
@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;
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/65b1b583/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java b/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java
index 5c5203b..975befa 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java
@@ -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) {
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/65b1b583/httpcore5/src/main/java/org/apache/hc/core5/util/Args.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/util/Args.java b/httpcore5/src/main/java/org/apache/hc/core5/util/Args.java
index f6fc05f..98d8ad8 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/util/Args.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/util/Args.java
@@ -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");
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/65b1b583/httpcore5/src/test/java/org/apache/hc/core5/util/TestArgs.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/util/TestArgs.java b/httpcore5/src/test/java/org/apache/hc/core5/util/TestArgs.java
index 430d1cb..b957e1e 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/util/TestArgs.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/util/TestArgs.java
@@ -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");
+ }
+
}