Exemple #1
0
def parse_header_value(name, val):
    if not is_pure_ascii(val):
        if parametrized.is_parametrized(name, val):
            # PHOENIX HACK!!!
            # FIXME: making the function return a completely different type on
            # parameterized headers is wrong.
            val = val.decode('utf-8', errors='replace')
            val = val.split(";", 1)
            if len(val) == 2:
                val, params = val

                rparams = {}
                for p in params.split(";"):
                    k,v = p.split("=", 1)
                    rparams[k.strip()] = v.split('"')[1]

                return val, rparams
            return val[0], {}

        return to_unicode(val)
    else:
        if parametrized.is_parametrized(name, val):
            val, params = parametrized.decode(val)
            if name == 'Content-Type':
                main, sub = parametrized.fix_content_type(val)
                return ContentType(main, sub, params)
            else:
                return WithParams(val, params)
        else:
            return val
Exemple #2
0
def parse_header_value(name, val):
    if not is_pure_ascii(val):
        if parametrized.is_parametrized(name, val):
            raise DecodingError("Unsupported value in content- header")
        return to_unicode(val)
    else:
        if parametrized.is_parametrized(name, val):
            val, params = parametrized.decode(val)
            if name == 'Content-Type':
                main, sub = parametrized.fix_content_type(val)
                return ContentType(main, sub, params)
            else:
                return WithParams(val, params)
        else:
            return val
Exemple #3
0
def parse_header_value(name, val):
    if not is_pure_ascii(val):
        if parametrized.is_parametrized(name, val):
            raise DecodingError("Unsupported value in content- header")
        return to_unicode(val)
    else:
        if parametrized.is_parametrized(name, val):
            val, params = parametrized.decode(val)
            if name == 'Content-Type':
                main, sub = parametrized.fix_content_type(val)
                return ContentType(main, sub, params)
            else:
                return WithParams(val, params)
        else:
            return val
Exemple #4
0
def encode(name, value):
    try:
        if parametrized.is_parametrized(name, value):
            value, params = value
            return encode_parametrized(name, value, params)
        else:
            return encode_unstructured(name, value)
    except Exception:
        log.exception("Failed to encode %s %s" % (name, value))
        raise
Exemple #5
0
def parse_header_value(name, val):
    if parametrized.is_parametrized(name, val):
        val, params = parametrized.decode(val)
        if name == 'Content-Type':
            main, sub = parametrized.fix_content_type(val)
            return ContentType(main, sub, params)
        else:
            return WithParams(val, params)
    else:
        return val if is_pure_ascii(val) else to_unicode(val)
Exemple #6
0
def parse_header_value(name, val):
    if parametrized.is_parametrized(name, val):
        val, params = parametrized.decode(val)
        if name == 'Content-Type':
            main, sub = parametrized.fix_content_type(val)
            return ContentType(main, sub, params)
        else:
            return WithParams(val, params)
    else:
        return val if is_pure_ascii(val) else to_unicode(val)
Exemple #7
0
def encode(name, value):
    try:
        if parametrized.is_parametrized(name, value):
            value, params = value
            return _encode_parametrized(name, value, params)

        return _encode_unstructured(name, value)
    except Exception:
        _log.exception('Failed to encode %s %s' % (name, value))
        raise
Exemple #8
0
def parse_header_value(name, val):
    if not is_pure_ascii(val):
        val = to_unicode(val)
    if parametrized.is_parametrized(name, val):
        val, params = parametrized.decode(val)
        if val is not None and not is_pure_ascii(val):
            raise DecodingError('Non-ascii content header value')
        if name == 'Content-Type':
            main, sub = parametrized.fix_content_type(val)
            return ContentType(main, sub, params)

        return WithParams(val, params)

    return val
Exemple #9
0
def parse_header_value(name, val):
    if not is_pure_ascii(val):
        val = to_unicode(val)
    if parametrized.is_parametrized(name, val):
        val, params = parametrized.decode(val)
        if val is not None and not is_pure_ascii(val):
            raise DecodingError('Non-ascii content header value')
        if name == 'Content-Type':
            main, sub = parametrized.fix_content_type(val)
            return ContentType(main, sub, params)

        return WithParams(val, params)

    return val