Permalink
Browse files

URL Viewer : decode raw text using the eventual response charset.

When provided, or decode as UTF-8 as previously done.
  • Loading branch information...
luccioman committed Jul 3, 2017
1 parent 90a7c1a commit 1b3c169a9c3b1bd28c1510b5974b7da7d3c395fb
Showing with 17 additions and 1 deletion.
  1. +17 −1 htroot/ViewFile.java
@@ -29,6 +29,9 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
@@ -202,7 +205,20 @@ public static serverObjects respond(final RequestHeader header, final serverObje
// TODO: how to handle very large files here ?
String content;
try {
content = UTF8.String(response.getContent());
String charsetName = response.getCharacterEncoding();
try {
if(charsetName == null) {
/* Encoding is unknown from response headers : default decode using UTF-8 */
charsetName = StandardCharsets.UTF_8.name();
} else if(!Charset.isSupported(charsetName)) {
/* Encoding is known but not supported on this system : default decode using UTF-8 */
charsetName = StandardCharsets.UTF_8.name();
}
} catch(IllegalCharsetNameException e) {
/* Encoding is known but charset name is not valid : default decode using UTF-8 */
charsetName = StandardCharsets.UTF_8.name();
}
content = new String(response.getContent(), charsetName);
} catch (final Exception e) {
prop.put("error", "4");
prop.putHTML("error_errorText", e.getMessage());

0 comments on commit 1b3c169

Please sign in to comment.