Ejemplo n.º 1
0
def url_param2value(param):
    """
    CONVERT URL QUERY PARAMETERS INTO DICT
    """
    if param == None:
        return Null
    if param == None:
        return Null

    def _decode(v):
        output = []
        i = 0
        while i < len(v):
            c = v[i]
            if c == "%":
                d = hex2chr(v[i + 1:i + 3])
                output.append(d)
                i += 3
            else:
                output.append(c)
                i += 1

        output = text("".join(output))
        try:
            from mo_json import json2value

            return json2value(output)
        except Exception:
            pass
        return output

    query = Data()
    for p in param.split('&'):
        if not p:
            continue
        if p.find("=") == -1:
            k = p
            v = True
        else:
            k, v = p.split("=")
            v = _decode(v)

        u = query.get(k)
        if u is None:
            query[k] = v
        elif is_list(u):
            u += [v]
        else:
            query[k] = [u, v]

    return query
Ejemplo n.º 2
0
def url_param2value(param):
    """
    CONVERT URL QUERY PARAMETERS INTO DICT
    """
    if param == None:
        return Null
    if param == None:
        return Null

    def _decode(v):
        output = []
        i = 0
        while i < len(v):
            c = v[i]
            if c == "%":
                d = hex2chr(v[i + 1:i + 3])
                output.append(d)
                i += 3
            else:
                output.append(c)
                i += 1

        output = text_type("".join(output))
        try:
            return json2value(output)
        except Exception:
            pass
        return output

    query = Data()
    for p in param.split('&'):
        if not p:
            continue
        if p.find("=") == -1:
            k = p
            v = True
        else:
            k, v = p.split("=")
            v = _decode(v)

        u = query.get(k)
        if u is None:
            query[k] = v
        elif is_list(u):
            u += [v]
        else:
            query[k] = [u, v]

    return query
Ejemplo n.º 3
0
def url_param2value(param):
    """
    CONVERT URL QUERY PARAMETERS INTO DICT
    """
    if isinstance(param, unicode):
        param = param.encode("ascii")

    def _decode(v):
        output = []
        i = 0
        while i < len(v):
            c = v[i]
            if c == "%":
                d = (v[i + 1:i + 3]).decode("hex")
                output.append(d)
                i += 3
            else:
                output.append(c)
                i += 1

        output = (b"".join(output)).decode("latin1")
        try:
            if not _Log:
                _late_import()
            return _json2value(output)
        except Exception:
            pass
        return output

    query = Data()
    for p in param.split(b'&'):
        if not p:
            continue
        if p.find(b"=") == -1:
            k = p
            v = True
        else:
            k, v = p.split(b"=")
            v = _decode(v)

        u = query.get(k)
        if u is None:
            query[k] = v
        elif isinstance(u, list):
            u += [v]
        else:
            query[k] = [u, v]

    return query
Ejemplo n.º 4
0
def url_param2value(param):
    """
    CONVERT URL QUERY PARAMETERS INTO DICT
    """
    def _decode(v):
        output = []
        i = 0
        while i < len(v):
            c = v[i]
            if c == "%":
                d = _url_to_str[v[i:i + 3]]
                output.append(d)
                i += 3
            else:
                output.append(c)
                i += 1

        output = ("".join(output))
        try:
            if not _Log:
                _late_import()
            return _json2value(output)
        except Exception:
            pass
        return output

    query = Data()
    for p in param.split("&"):
        if not p:
            continue
        if p.find("=") == -1:
            k = p
            v = True
        else:
            k, v = p.split("=")
            v = _decode(v)

        u = query.get(k)
        if u is None:
            query[k] = v
        elif isinstance(u, list):
            u += [v]
        else:
            query[k] = [u, v]

    return query
Ejemplo n.º 5
0
def url_param2value(param):
    """
    CONVERT URL QUERY PARAMETERS INTO DICT
    """
    if param == None:
        return Null
    if param == None:
        return Null

    def _decode(vs):
        from mo_json import json2value

        results = []
        for v in vs.split(","):
            output = []
            i = 0
            # WE MUST TRACK THE STATE OF UTF* DECODING, IF ILLEGITIMATE ENCODING
            # THEN ASSUME LATIN1
            utf_remaining = 0
            start = 0
            while i < len(v):
                c = v[i]
                if utf_remaining:
                    if c == "%":
                        try:
                            hex = v[i + 1 : i + 3]
                            if hex.strip() == hex:
                                d = int(v[i + 1 : i + 3], 16)
                                if d & 0xC0 == 0x80:  # 10XX XXXX
                                    utf_remaining -= 1
                                    b = bytes([d])
                                    output.append(b)
                                    i += 3
                                    continue
                        except Exception:
                            pass
                    # missing continuation byte (# 10XX XXXX), try again
                    output = output[:-utf_remaining]
                    utf_remaining = 0
                    i = start
                    output.append(b"%")
                    i += 1
                else:
                    if c == "+":
                        output.append(b" ")
                        i += 1
                    elif c == "%":
                        try:
                            hex_pair = v[i + 1 : i + 3]
                            if hex_pair.strip() != hex_pair:
                                output.append(b"%")
                                i += 1
                                continue

                            d = int(hex_pair, 16)
                            if d & 0xE0 == 0xC0:  # 110X XXXX
                                utf_remaining = 1
                                start = i
                                b = bytes([d])
                                output.append(b)
                                i += 3
                            elif d & 0xF0 == 0xE0:  # 1110 XXXX
                                utf_remaining = 2
                                start = i
                                b = bytes([d])
                                output.append(b)
                                i += 3
                            elif d & 0xF8 == 0xF0:  # 1111 0xxx
                                utf_remaining = 3
                                start = i
                                b = bytes([d])
                                output.append(b)
                                i += 3
                            elif d & 0x80 == 0x80:
                                output.append(b"%")
                                i += 1
                            else:
                                b = bytes([d])
                                output.append(b)
                                i += 3
                        except Exception:
                            output.append(b"%")
                            i += 1
                    else:
                        try:
                            output.append(c.encode("latin1"))
                        except Exception:
                            # WE EXPECT BYTES, BUT SOMEONE WILL GIVE US UNICODE STRINGS
                            output.append(c.encode("utf8"))
                        i += 1

            if utf_remaining:
                # missing continuation byte, try again
                output = output[:-utf_remaining] + [v[start:].encode("latin1")]

            output = b"".join(output).decode("utf8")
            try:
                output = json2value(output)
            except Exception:
                pass
            results.append(output)
        return unwraplist(results)

    query = Data()
    for p in param.split("&"):
        if not p:
            continue
        if p.find("=") == -1:
            k = p
            v = True
        else:
            k, v = p.split("=")
            k = _decode(k)
            v = _decode(v)

        u = query.get(k)
        if u is None:
            query[k] = v
        elif is_list(u):
            u += [v]
        else:
            query[k] = [u, v]

    return query