diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/HttpException.java b/httpcore5/src/main/java/org/apache/hc/core5/http/HttpException.java index 54f8f5ba1..5327597bd 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/HttpException.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/HttpException.java @@ -60,7 +60,7 @@ static String clean(final String message) { final char ch = chars[i]; if (ch < FIRST_VALID_CHAR) { builder.append("[0x"); - final String hexString = Integer.toHexString(i); + final String hexString = Integer.toHexString(ch); if (hexString.length() == 1) { builder.append("0"); } diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/TestHttpExceptions.java b/httpcore5/src/test/java/org/apache/hc/core5/http/TestHttpExceptions.java index 29da8ab79..4731c84fd 100644 --- a/httpcore5/src/test/java/org/apache/hc/core5/http/TestHttpExceptions.java +++ b/httpcore5/src/test/java/org/apache/hc/core5/http/TestHttpExceptions.java @@ -35,7 +35,7 @@ */ class TestHttpExceptions { - private static final String CLEAN_MESSAGE = "[0x00]Hello[0x06][0x07][0x08][0x09][0x0a][0x0b][0x0c][0x0d][0x0e][0x0f]World"; + private static final String CLEAN_MESSAGE = "[0x01]Hello[0x01][0x02][0x03][0x04][0x05][0x06][0x07][0x08][0x09][0x00]World"; private static final 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' }); @@ -86,4 +86,18 @@ void testNonPrintableCharactersInUnsupportedHttpVersionException() { Assertions.assertEquals(CLEAN_MESSAGE, new UnsupportedHttpVersionException(nonPrintableMessage).getMessage()); } + + @Test + void testControlCharacterEscaping() { + final HttpException exception = new HttpException("0123456789abcdef\nsuffix"); + + Assertions.assertEquals("0123456789abcdef[0x0a]suffix", exception.getMessage()); + } + + @Test + void testControlCharacterEncodingIndependentOfPosition() { + Assertions.assertEquals("a[0x0a]b", new HttpException("a\nb").getMessage()); + Assertions.assertEquals("0123456789abcdef[0x0a]b", new HttpException("0123456789abcdef\nb").getMessage()); + } + }