'Converts headers key/value to ascii/latin1 native strings.
`charset` must be \'ascii\' or \'latin-1\'. If `mime_encode` is True and
`value` value can\'t be represented in the given charset, MIME-encoding
is applied.'

def _convert_to_charset(self, value, charset, mime_encode=False):

    if (not isinstance(value, (bytes, six.text_type))):
        value = str(value)
    try:
        if six.PY3:
            if isinstance(value, str):
                value.encode(charset)
            else:
                value = value.decode(charset)
        elif isinstance(value, str):
            value.decode(charset)
        else:
            value = value.encode(charset)
    except UnicodeError as e:
        if mime_encode:
            value = str(Header(value, u'utf-8').encode())
        else:
            e.reason += (u',    HTTP    response    headers    must    be    in    %s    format' % charset)
            raise
    if ((str(u'\n') in value) or (str(u'\r') in value)):
        raise BadHeaderError((u"Header    values    can't    contain    newlines    (got    %r)" % value))
    return value
