Parses a typical multi-valued and parametrised HTTP header (e.g. Accept headers) and returns a list of values and parameters. For non-standard or broken input, this implementation may return partial results. :param h: A header string (e.g. ``text/html,text/plain;q=0.9,*/*;q=0.8``) :
(h)
| 3404 | _hsplit = re.compile('(?:(?:"((?:[^"\\\\]|\\\\.)*)")|([^;,=]+))([;,=]?)').findall |
| 3405 | |
| 3406 | def _parse_http_header(h): |
| 3407 | """ Parses a typical multi-valued and parametrised HTTP header (e.g. Accept headers) and returns a list of values |
| 3408 | and parameters. For non-standard or broken input, this implementation may return partial results. |
| 3409 | :param h: A header string (e.g. ``text/html,text/plain;q=0.9,*/*;q=0.8``) |
| 3410 | :return: List of (value, params) tuples. The second element is a (possibly empty) dict. |
| 3411 | """ |
| 3412 | values = [] |
| 3413 | if '"' not in h: # INFO: Fast path without regexp (~2x faster) |
| 3414 | for value in h.split(','): |
| 3415 | parts = value.split(';') |
| 3416 | values.append((parts[0].strip(), {})) |
| 3417 | for attr in parts[1:]: |
| 3418 | name, value = attr.split('=', 1) |
| 3419 | values[-1][1][name.strip()] = value.strip() |
| 3420 | else: |
| 3421 | lop, key, attrs = ',', None, {} |
| 3422 | for quoted, plain, tok in _hsplit(h): |
| 3423 | value = plain.strip() if plain else quoted.replace('\\"', '"') |
| 3424 | if lop == ',': |
| 3425 | attrs = {} |
| 3426 | values.append((value, attrs)) |
| 3427 | elif lop == ';': |
| 3428 | if tok == '=': |
| 3429 | key = value |
| 3430 | else: |
| 3431 | attrs[value] = '' |
| 3432 | elif lop == '=' and key: |
| 3433 | attrs[key] = value |
| 3434 | key = None |
| 3435 | lop = tok |
| 3436 | return values |
| 3437 | |
| 3438 | |
| 3439 | def _parse_qsl(qs): |