From 1b3c169a9c3b1bd28c1510b5974b7da7d3c395fb Mon Sep 17 00:00:00 2001 From: luccioman Date: Mon, 3 Jul 2017 13:51:14 +0200 Subject: [PATCH] URL Viewer : decode raw text using the eventual response charset. When provided, or decode as UTF-8 as previously done. --- htroot/ViewFile.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/htroot/ViewFile.java b/htroot/ViewFile.java index 9a7262ac2e..bfa6c4a65a 100644 --- a/htroot/ViewFile.java +++ b/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());