From 067c71913e8b261427aab3ed8b29adfa66e85a26 Mon Sep 17 00:00:00 2001 From: thc202 Date: Tue, 25 Oct 2016 15:03:32 +0100 Subject: [PATCH] Correct charset determination in HttpResponseBody Remove use of platform's default charset when determining if the charset of the string is UTF-8, which was leading to wrong results if the platform's default charset was not UTF-8. Related to: - #2935 - Wrong charset used in response body if no charset set - #2941 - Attempt to determine (String) body's charset --- src/org/zaproxy/zap/network/HttpResponseBody.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/org/zaproxy/zap/network/HttpResponseBody.java b/src/org/zaproxy/zap/network/HttpResponseBody.java index 5d7ffea8983..9d3f459384c 100644 --- a/src/org/zaproxy/zap/network/HttpResponseBody.java +++ b/src/org/zaproxy/zap/network/HttpResponseBody.java @@ -90,12 +90,16 @@ protected Charset determineCharset(String contents) { } catch (IllegalArgumentException e) { log.warn("Unable to determine (valid) charset with the (X)HTML meta charset: " + e.getMessage()); } - } else if (contents.getBytes(StandardCharsets.UTF_8).length == contents.getBytes().length) { + } else if (isUtf8String(contents)) { return StandardCharsets.UTF_8; } return null; } + private static boolean isUtf8String(String string) { + return new String(string.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8).length() == string.length(); + } + @Override protected String createString(Charset currentCharset) { if (currentCharset != null) {