Skip to content

Commit

Permalink
URL Viewer : decode raw text using the eventual response charset.
Browse files Browse the repository at this point in the history
When provided, or decode as UTF-8 as previously done.
  • Loading branch information
luccioman committed Jul 3, 2017
1 parent 90a7c1a commit 1b3c169
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion htroot/ViewFile.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 1b3c169

Please sign in to comment.