On Sat, Feb 24, 2018 at 6:48 PM, Bindul Bhowmik <[email protected]> wrote:
> On Sat, Feb 24, 2018 at 8:46 AM, <[email protected]> wrote: > > Repository: httpcomponents-core > > Updated Branches: > > refs/heads/master 286ec375d -> 458d7c628 > > > > > > [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/458d7c62 > > Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/ > tree/458d7c62 > > Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/ > diff/458d7c62 > > > > Branch: refs/heads/master > > Commit: 458d7c628eaf8371c53ba906316ac3077235e40c > > Parents: 286ec37 > > Author: Gary Gregory <[email protected]> > > Authored: Sat Feb 24 08:46:06 2018 -0700 > > Committer: Gary Gregory <[email protected]> > > Committed: Sat Feb 24 08:46:06 2018 -0700 > > > > ---------------------------------------------------------------------- > > .../core5/http/ConnectionClosedException.java | 2 +- > > .../org/apache/hc/core5/http/HttpException.java | 42 ++++++++- > > .../hc/core5/http/NoHttpResponseException.java | 2 +- > > .../hc/core5/http/TestHttpExceptions.java | 89 > ++++++++++++++++++++ > > 4 files changed, 131 insertions(+), 4 deletions(-) > > ---------------------------------------------------------------------- > > > > > > http://git-wip-us.apache.org/repos/asf/httpcomponents-core/ > blob/458d7c62/httpcore5/src/main/java/org/apache/hc/core5/http/ > ConnectionClosedException.java > > ---------------------------------------------------------------------- > > diff --git > > a/httpcore5/src/main/java/org/apache/hc/core5/http/ConnectionClosedException.java > b/httpcore5/src/main/java/org/apache/hc/core5/http/ > ConnectionClosedException.java > > index 257fc21..c0fdefa 100644 > > --- a/httpcore5/src/main/java/org/apache/hc/core5/http/ > ConnectionClosedException.java > > +++ b/httpcore5/src/main/java/org/apache/hc/core5/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/458d7c62/httpcore5/src/main/java/org/apache/hc/core5/ > http/HttpException.java > > ---------------------------------------------------------------------- > > 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 bd1e4ab..207f72e 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 > > @@ -34,9 +34,47 @@ package org.apache.hc.core5.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(); > > + } > > Gary, > > Wouldn't it be safer to use the java.lang.Character#isISOControl(int) > method in this case? 0x7F - 0x9F are also not printable. > These are part of "Extended ASCII" which can be printable. See https://www.ascii-code.com/ Gary > > Bindul > > > + > > + /** > > * Creates a new HttpException with a {@code null} detail message. > > */ > > public HttpException() { > > @@ -49,7 +87,7 @@ public class HttpException extends Exception { > > * @param message the exception detail message > > */ > > public HttpException(final String message) { > > - super(message); > > + super(clean(message)); > > } > > > > /** > > @@ -60,7 +98,7 @@ public class HttpException extends Exception { > > * 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/458d7c62/httpcore5/src/main/java/org/apache/hc/core5/ > http/NoHttpResponseException.java > > ---------------------------------------------------------------------- > > diff --git > > a/httpcore5/src/main/java/org/apache/hc/core5/http/NoHttpResponseException.java > b/httpcore5/src/main/java/org/apache/hc/core5/http/ > NoHttpResponseException.java > > index f2f3cdf..5fc880e 100644 > > --- a/httpcore5/src/main/java/org/apache/hc/core5/http/ > NoHttpResponseException.java > > +++ b/httpcore5/src/main/java/org/apache/hc/core5/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/458d7c62/httpcore5/src/test/java/org/apache/hc/core5/ > http/TestHttpExceptions.java > > ---------------------------------------------------------------------- > > 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 > > new file mode 100644 > > index 0000000..5de7b1c > > --- /dev/null > > +++ b/httpcore5/src/test/java/org/apache/hc/core5/http/ > TestHttpExceptions.java > > @@ -0,0 +1,89 @@ > > +/* > > + * ==================================================================== > > + * Licensed to the Apache Software Foundation (ASF) under one > > + * or more contributor license agreements. See the NOTICE file > > + * distributed with this work for additional information > > + * regarding copyright ownership. The ASF licenses this file > > + * to you under the Apache License, Version 2.0 (the > > + * "License"); you may not use this file except in compliance > > + * with the License. You may obtain a copy of the License at > > + * > > + * http://www.apache.org/licenses/LICENSE-2.0 > > + * > > + * Unless required by applicable law or agreed to in writing, > > + * software distributed under the License is distributed on an > > + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY > > + * KIND, either express or implied. See the License for the > > + * specific language governing permissions and limitations > > + * under the License. > > + * ==================================================================== > > + * > > + * This software consists of voluntary contributions made by many > > + * individuals on behalf of the Apache Software Foundation. For more > > + * information on the Apache Software Foundation, please see > > + * <http://www.apache.org/>. > > + * > > + */ > > + > > +package org.apache.hc.core5.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(); > > + new HttpException(); > > + new HttpException("Oppsie"); > > + new HttpException("Oppsie", cause); > > + new ProtocolException(); > > + new ProtocolException("Oppsie"); > > + new ProtocolException("Oppsie", cause); > > + new NoHttpResponseException("Oppsie"); > > + new ConnectionClosedException("Oppsie"); > > + new MethodNotSupportedException("Oppsie"); > > + new MethodNotSupportedException("Oppsie", cause); > > + new UnsupportedHttpVersionException(); > > + 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 testNonPrintableCharactersInUn > supportedHttpVersionException() { > > + Assert.assertEquals(CLEAN_MESSAGE, new > UnsupportedHttpVersionException(nonPrintableMessage).getMessage()); > > + } > > + > > +} > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > >
