def find_acceptable_charset(self):
        """Deduce acceptable charset for HTTP response."""
        request = cherrypy.serving.request
        response = cherrypy.serving.response

        if self.debug:
            cherrypy.log(
                'response.stream %r' % response.stream,
                'TOOLS.ENCODE',
            )
        if response.stream:
            encoder = self.encode_stream
        else:
            encoder = self.encode_string
            if 'Content-Length' in response.headers:
                # Delete Content-Length header so finalize() recalcs it.
                # Encoded strings may be of different lengths from their
                # unicode equivalents, and even from each other. For example:
                # >>> t = u"\u7007\u3040"
                # >>> len(t)
                # 2
                # >>> len(t.encode("UTF-8"))
                # 6
                # >>> len(t.encode("utf7"))
                # 8
                del response.headers['Content-Length']

        # Parse the Accept-Charset request header, and try to provide one
        # of the requested charsets (in order of user preference).
        encs = request.headers.elements('Accept-Charset')
        charsets = [enc.value.lower() for enc in encs]
        if self.debug:
            cherrypy.log('charsets %s' % repr(charsets), 'TOOLS.ENCODE')

        if self.encoding is not None:
            # If specified, force this encoding to be used, or fail.
            encoding = self.encoding.lower()
            if self.debug:
                cherrypy.log(
                    'Specified encoding %r' % encoding,
                    'TOOLS.ENCODE',
                )
            if (not charsets) or '*' in charsets or encoding in charsets:
                if self.debug:
                    cherrypy.log(
                        'Attempting encoding %r' % encoding,
                        'TOOLS.ENCODE',
                    )
                if encoder(encoding):
                    return encoding
        else:
            if not encs:
                if self.debug:
                    cherrypy.log(
                        'Attempting default encoding %r'
                        % self.default_encoding,
                        'TOOLS.ENCODE',
                    )
                # Any character-set is acceptable.
                if encoder(self.default_encoding):
                    return self.default_encoding
                else:
                    raise cherrypy.HTTPError(
                        500,
                        self.failmsg % self.default_encoding,
                    )
            else:
                for element in encs:
                    if element.qvalue > 0:
                        if element.value == '*':
                            # Matches any charset. Try our default.
                            if self.debug:
                                cherrypy.log(
                                    'Attempting default encoding due '
                                    'to %r' % element,
                                    'TOOLS.ENCODE',
                                )
                            if encoder(self.default_encoding):
                                return self.default_encoding
                        else:
                            encoding = element.value
                            if self.debug:
                                cherrypy.log(
                                    'Attempting encoding %s (qvalue >'
                                    '0)' % element,
                                    'TOOLS.ENCODE',
                                )
                            if encoder(encoding):
                                return encoding

                if '*' not in charsets:
                    # If no "*" is present in an Accept-Charset field, then all
                    # character sets not explicitly mentioned get a quality
                    # value of 0, except for ISO-8859-1, which gets a quality
                    # value of 1 if not explicitly mentioned.
                    iso = 'iso-8859-1'
                    if iso not in charsets:
                        if self.debug:
                            cherrypy.log(
                                'Attempting ISO-8859-1 encoding',
                                'TOOLS.ENCODE',
                            )
                        if encoder(iso):
                            return iso

        # No suitable encoding found.
        ac = request.headers.get('Accept-Charset')
        if ac is None:
            msg = 'Your client did not send an Accept-Charset header.'
        else:
            msg = 'Your client sent this Accept-Charset header: %s.' % ac
        _charsets = ', '.join(sorted(self.attempted_charsets))
        msg += ' We tried these charsets: %s.' % (_charsets,)
        raise cherrypy.HTTPError(406, msg)
Deduce acceptable charset for HTTP response.
cherrypy/lib/encoding.py