Skip to content

Commit

Permalink
Correct charset determination in HttpResponseBody
Browse files Browse the repository at this point in the history
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:
 - zaproxy#2935 - Wrong charset used in response body if no charset set
 - zaproxy#2941 - Attempt to determine (String) body's charset
  • Loading branch information
thc202 committed Oct 25, 2016
1 parent d987bb3 commit 067c719
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/org/zaproxy/zap/network/HttpResponseBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 067c719

Please sign in to comment.