'Decode the body based on the header (or metadata) encoding.
The implemented algorithm follows the encoding detection logic
used by FF:
1) First try to find a charset using the following search criteria:
a) Look in the CONTENT_TYPE HTTP header. Example:
content-type: text/html; charset=iso-8859-1
b) Look in the \'meta\' HTML header. Example:
<meta .* content="text/html; charset=utf-8" />
c) Determine the charset using the chardet module (TODO)
d) Use the DEFAULT_CHARSET
2) Try to decode the body using the found charset. If it fails,
then force it to use the DEFAULT_CHARSET
Finally return the unicode (decoded) body and the used charset.
Note: If the body is already a unicode string return it as it is.'

def _charset_handling(self):

    headers = self.get_headers()
    (content_type, _) = headers.iget(CONTENT_TYPE, None)
    charset = self._charset
    rawbody = self._raw_body
    if (type(rawbody) is unicode):
        _body = rawbody
        assert (charset is not None), 'HTTPResponse    objects    containing    unicode    body    must    have    an    associated    charset'
    elif (content_type is None):
        _body = rawbody
        charset = DEFAULT_CHARSET
        if len(_body):
            msg = 'The    remote    web    server    failed    to    send    the    CONTENT_TYPE    header    in    HTTP    response    with    id    %s'
            om.out.debug((msg % self.id))
    elif (not self.is_text_or_html()):
        _body = rawbody
        charset = (charset or DEFAULT_CHARSET)
    else:
        if (not charset):
            charset = self.guess_charset(rawbody, headers)
        try:
            _body = smart_unicode(rawbody, charset, errors=ESCAPED_CHAR, on_error_guess=False)
        except LookupError:
            msg = ('Charset    LookupError:    unknown    charset:    %s;    ignored    and    set    to    default:    %s' % (charset, DEFAULT_CHARSET))
            om.out.debug(msg)
            charset = DEFAULT_CHARSET
            _body = smart_unicode(rawbody, charset, errors=ESCAPED_CHAR, on_error_guess=False)
    return (_body, charset)
