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 2018/02/24 15:34:49 UTC
httpcomponents-core git commit: [HTTPCORE-514] Exceptions defined by
HttpCore should clean message strings when built to replace non-printable
characters with hex values.
Repository: httpcomponents-core
Updated Branches:
refs/heads/4.4.x 890aa8da9 -> 4d97c9d87
[HTTPCORE-514] Exceptions defined by HttpCore should clean message
strings when built to replace non-printable characters with hex values.
Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/4d97c9d8
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/4d97c9d8
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/4d97c9d8
Branch: refs/heads/4.4.x
Commit: 4d97c9d8701ee39e5ca981df4e60fac857590e7e
Parents: 890aa8d
Author: Gary Gregory <ga...@gmail.com>
Authored: Sat Feb 24 08:34:46 2018 -0700
Committer: Gary Gregory <ga...@gmail.com>
Committed: Sat Feb 24 08:34:46 2018 -0700
----------------------------------------------------------------------
RELEASE_NOTES.txt | 3 ++
.../apache/http/ConnectionClosedException.java | 2 +-
.../java/org/apache/http/HttpException.java | 54 +++++++++++++++++---
.../apache/http/NoHttpResponseException.java | 2 +-
.../org/apache/http/TestHttpExceptions.java | 37 +++++++++++++-
5 files changed, 88 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/4d97c9d8/RELEASE_NOTES.txt
----------------------------------------------------------------------
diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt
index 1e28a5f..f1a7d82 100644
--- a/RELEASE_NOTES.txt
+++ b/RELEASE_NOTES.txt
@@ -15,6 +15,9 @@ Changelog
* HTTPCORE-509: AVAIL_PROCS is auto-configured based on core count.
Contributed by Gary Gregory <ggregory at apache.org>
+* HTTPCORE-514: Exceptions defined by HttpCore should clean message strings when built to replace non-printable characters with hex values.
+ Contributed by Gary Gregory <ggregory at apache.org>
+
Release 4.4.9
-------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/4d97c9d8/httpcore/src/main/java/org/apache/http/ConnectionClosedException.java
----------------------------------------------------------------------
diff --git a/httpcore/src/main/java/org/apache/http/ConnectionClosedException.java b/httpcore/src/main/java/org/apache/http/ConnectionClosedException.java
index 2743590..303a07c 100644
--- a/httpcore/src/main/java/org/apache/http/ConnectionClosedException.java
+++ b/httpcore/src/main/java/org/apache/http/ConnectionClosedException.java
@@ -44,7 +44,7 @@ public class ConnectionClosedException extends IOException {
* @param message The exception detail message
*/
public ConnectionClosedException(final String message) {
- super(message);
+ super(HttpException.clean(message));
}
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/4d97c9d8/httpcore/src/main/java/org/apache/http/HttpException.java
----------------------------------------------------------------------
diff --git a/httpcore/src/main/java/org/apache/http/HttpException.java b/httpcore/src/main/java/org/apache/http/HttpException.java
index c02c52c..035f19f 100644
--- a/httpcore/src/main/java/org/apache/http/HttpException.java
+++ b/httpcore/src/main/java/org/apache/http/HttpException.java
@@ -34,9 +34,47 @@ package org.apache.http;
*/
public class HttpException extends Exception {
+ private static final int FIRST_VALID_CHAR = 32;
private static final long serialVersionUID = -5437299376222011036L;
/**
+ * Converts characters < 32 to hex.
+ *
+ * @param message
+ * the source string.
+ * @return a converted string.
+ */
+ static String clean(final String message) {
+ final char[] chars = message.toCharArray();
+ int i;
+ // First check to see if need to allocate a new StringBuilder
+ for (i = 0; i < chars.length; i++) {
+ if (chars[i] < FIRST_VALID_CHAR) {
+ break;
+ }
+ }
+ if (i == chars.length) {
+ return message;
+ }
+ final StringBuilder builder = new StringBuilder(chars.length * 2);
+ for (i = 0; i < chars.length; i++) {
+ final char ch = chars[i];
+ if (ch < FIRST_VALID_CHAR) {
+ builder.append("[0x");
+ final String hexString = Integer.toHexString(i);
+ if (hexString.length() == 1) {
+ builder.append("0");
+ }
+ builder.append(hexString);
+ builder.append("]");
+ } else {
+ builder.append(ch);
+ }
+ }
+ return builder.toString();
+ }
+
+ /**
* Creates a new HttpException with a {@code null} detail message.
*/
public HttpException() {
@@ -46,21 +84,25 @@ public class HttpException extends Exception {
/**
* Creates a new HttpException with the specified detail message.
*
- * @param message the exception detail message
+ * @param message
+ * the exception detail message
*/
public HttpException(final String message) {
- super(message);
+ super(clean(message));
}
/**
* Creates a new HttpException with the specified detail message and cause.
*
- * @param message the exception detail message
- * @param cause the {@code Throwable} that caused this exception, or {@code null}
- * if the cause is unavailable, unknown, or not a {@code Throwable}
+ * @param message
+ * the exception detail message
+ * @param cause
+ * the {@code Throwable} that caused this exception, or
+ * {@code null} if the cause is unavailable, unknown, or not a
+ * {@code Throwable}
*/
public HttpException(final String message, final Throwable cause) {
- super(message);
+ super(clean(message));
initCause(cause);
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/4d97c9d8/httpcore/src/main/java/org/apache/http/NoHttpResponseException.java
----------------------------------------------------------------------
diff --git a/httpcore/src/main/java/org/apache/http/NoHttpResponseException.java b/httpcore/src/main/java/org/apache/http/NoHttpResponseException.java
index ac17c48..29e8839 100644
--- a/httpcore/src/main/java/org/apache/http/NoHttpResponseException.java
+++ b/httpcore/src/main/java/org/apache/http/NoHttpResponseException.java
@@ -44,7 +44,7 @@ public class NoHttpResponseException extends IOException {
* @param message exception message
*/
public NoHttpResponseException(final String message) {
- super(message);
+ super(HttpException.clean(message));
}
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/4d97c9d8/httpcore/src/test/java/org/apache/http/TestHttpExceptions.java
----------------------------------------------------------------------
diff --git a/httpcore/src/test/java/org/apache/http/TestHttpExceptions.java b/httpcore/src/test/java/org/apache/http/TestHttpExceptions.java
index e5f4867..e0874f0 100644
--- a/httpcore/src/test/java/org/apache/http/TestHttpExceptions.java
+++ b/httpcore/src/test/java/org/apache/http/TestHttpExceptions.java
@@ -27,15 +27,18 @@
package org.apache.http;
+import org.junit.Assert;
import org.junit.Test;
/**
* Simple tests for various HTTP exception classes.
- *
- *
*/
public class TestHttpExceptions {
+ private static final String CLEAN_MESSAGE = "[0x00]Hello[0x06][0x07][0x08][0x09][0x0a][0x0b][0x0c][0x0d][0x0e][0x0f]World";
+ private static String nonPrintableMessage = String.valueOf(
+ new char[] { 1, 'H', 'e', 'l', 'l', 'o', 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 'W', 'o', 'r', 'l', 'd' });
+
@Test
public void testConstructor() {
final Throwable cause = new Exception();
@@ -53,4 +56,34 @@ public class TestHttpExceptions {
new UnsupportedHttpVersionException("Oppsie");
}
+ @Test
+ public void testNonPrintableCharactersInConnectionClosedException() {
+ Assert.assertEquals(CLEAN_MESSAGE, new ConnectionClosedException(nonPrintableMessage).getMessage());
+ }
+
+ @Test
+ public void testNonPrintableCharactersInHttpException() {
+ Assert.assertEquals(CLEAN_MESSAGE, new HttpException(nonPrintableMessage).getMessage());
+ }
+
+ @Test
+ public void testNonPrintableCharactersInMethodNotSupportedException() {
+ Assert.assertEquals(CLEAN_MESSAGE, new MethodNotSupportedException(nonPrintableMessage).getMessage());
+ }
+
+ @Test
+ public void testNonPrintableCharactersInNoHttpResponseException() {
+ Assert.assertEquals(CLEAN_MESSAGE, new NoHttpResponseException(nonPrintableMessage).getMessage());
+ }
+
+ @Test
+ public void testNonPrintableCharactersInProtocolException() {
+ Assert.assertEquals(CLEAN_MESSAGE, new ProtocolException(nonPrintableMessage).getMessage());
+ }
+
+ @Test
+ public void testNonPrintableCharactersInUnsupportedHttpVersionException() {
+ Assert.assertEquals(CLEAN_MESSAGE, new UnsupportedHttpVersionException(nonPrintableMessage).getMessage());
+ }
+
}