'Get encoding from HTTP headers or content.
:returns: encoding or `None`
:rtype: ``unicode`` or ``None``'

def _get_encoding(self):

    headers = self.raw.info()
    encoding = None
    if headers.getparam('charset'):
        encoding = headers.getparam('charset')
    for param in headers.getplist():
        if param.startswith('charset='):
            encoding = param[8:]
            break
    if (not self.stream):
        if (self.mimetype == 'text/html'):
            m = re.search('<meta.+charset=["\']{0,1}(.+?)["\'].*>', self.content)
            if m:
                encoding = m.group(1)
                print(('sniffed    HTML    encoding=%r' % encoding))
        elif ((self.mimetype.startswith('application/') or self.mimetype.startswith('text/')) and ('xml' in self.mimetype)):
            m = re.search('<?xml.+encoding=["\'](.+?)["\'][^>]*\\?>', self.content)
            if m:
                encoding = m.group(1)
    if ((self.mimetype == 'application/json') and (not encoding)):
        encoding = 'utf-8'
    elif ((self.mimetype == 'application/xml') and (not encoding)):
        encoding = 'utf-8'
    if encoding:
        encoding = encoding.lower()
    return encoding
