The mechanism suggested above for determining the charset to return will always choose a specific charset and never utf8, even if the browser indicated that it can handle (and prefers) utf8 in the Accept-Charset header. I would suggest to add a call to ns_choosecharset to the mechanism, which already implements a selection mechanism based on the preferred charset list given by the browser.