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 2014/08/13 10:39:36 UTC

svn commit: r1617685 - in /httpcomponents/httpcore/trunk/httpcore/src: main/java/org/apache/http/util/EntityUtils.java test/java/org/apache/http/util/TestEntityUtils.java

Author: olegk
Date: Wed Aug 13 08:39:36 2014
New Revision: 1617685

URL: http://svn.apache.org/r1617685
Log:
Make EntityUtils.toString more lenient towards invalid character sets.

e.g. one website is returning "Content-Type	text/html; charset=EN-AU"

Modified:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestEntityUtils.java

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java?rev=1617685&r1=1617684&r2=1617685&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java Wed Aug 13 08:39:36 2014
@@ -229,7 +229,9 @@ public final class EntityUtils {
                     charset = contentType.getCharset();
                 }
             } catch (final UnsupportedCharsetException ex) {
-                throw new UnsupportedEncodingException(ex.getMessage());
+                if (defaultCharset == null) {
+                    throw new UnsupportedEncodingException(ex.getMessage());
+                }
             }
             if (charset == null) {
                 charset = defaultCharset;

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestEntityUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestEntityUtils.java?rev=1617685&r1=1617684&r2=1617685&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestEntityUtils.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/util/TestEntityUtils.java Wed Aug 13 08:39:36 2014
@@ -204,6 +204,16 @@ public class TestEntityUtils {
         final String s = EntityUtils.toString(httpentity, "ISO-8859-1");
         Assert.assertEquals(content, s);
     }
+    @Test
+    public void testContentWithInvalidContentTypeToString() throws Exception {
+        final String content = constructString(RUSSIAN_HELLO);
+        final byte[] bytes = content.getBytes("UTF-8");
+        final BasicHttpEntity httpentity = new BasicHttpEntity();
+        httpentity.setContent(new ByteArrayInputStream(bytes));
+        httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; charset=nosuchcharset"));
+        final String s = EntityUtils.toString(httpentity, "UTF-8");
+        Assert.assertEquals(content, s);
+    }
 
     /**
      * Helper class that returns <code>null</code> as the content.