def parse_header_value()

in http/get_compressed/python/server/server.py [0:0]


def parse_header_value(header_name, header_value):
    """
    Parse the Accept or Accept-Encoding request header values.

    Returns
    -------
    list of (str, dict)
        The list of lowercase tokens and their parameters in the order they
        appear in the header. The parameters are stored in a dictionary where
        the keys are the parameter names and the values are the parameter
        values. If a parameter is not followed by an equal sign and a value,
        the value is None.
    """

    def unexpected(label, value):
        msg = f"Malformed {header_name} header: unexpected {label} at {value!r}"
        return ValueError(msg)

    def tokenize():
        for mo in re.finditer(TOKENIZER_PAT, header_value):
            kind = mo.lastgroup
            if kind == "SKIP":
                continue
            elif kind == "MISMATCH":
                raise unexpected("character", mo.group())
            yield (kind, mo.group())

    tokens = tokenize()

    def expect(expected_kind):
        kind, text = next(tokens)
        if kind != expected_kind:
            raise unexpected("token", text)
        return text

    accepted = []
    while True:
        try:
            name, params = None, {}
            name = expect("TOK").lower()
            kind, text = next(tokens)
            while True:
                if kind == "COMMA":
                    accepted.append((name, params))
                    break
                if kind == "SEMI":
                    ident = expect("TOK")
                    params[ident] = None  # init param to None
                    kind, text = next(tokens)
                    if kind != "EQ":
                        continue
                    kind, text = next(tokens)
                    if kind in ["TOK", "QUOTED"]:
                        if kind == "QUOTED":
                            text = text[1:-1]  # remove the quotes
                        params[ident] = text  # set param to value
                        kind, text = next(tokens)
                        continue
                raise unexpected("token", text)
        except StopIteration:
            break
    if name is not None:
        # any unfinished ;param=value sequence or trailing separators are ignored
        accepted.append((name, params))
    return accepted