Parse a query given as a string argument. Arguments: qs: percent-encoded query string to be parsed keep_blank_values: flag indicating whether blank values in percent-encoded queries should be treated as blank strings. A true value indicates
(qs, keep_blank_values=False, strict_parsing=False,
encoding='utf-8', errors='replace', max_num_fields=None, separator='&')
| 727 | |
| 728 | |
| 729 | def parse_qsl(qs, keep_blank_values=False, strict_parsing=False, |
| 730 | encoding='utf-8', errors='replace', max_num_fields=None, separator='&'): |
| 731 | """Parse a query given as a string argument. |
| 732 | |
| 733 | Arguments: |
| 734 | |
| 735 | qs: percent-encoded query string to be parsed |
| 736 | |
| 737 | keep_blank_values: flag indicating whether blank values in |
| 738 | percent-encoded queries should be treated as blank strings. |
| 739 | A true value indicates that blanks should be retained as blank |
| 740 | strings. The default false value indicates that blank values |
| 741 | are to be ignored and treated as if they were not included. |
| 742 | |
| 743 | strict_parsing: flag indicating what to do with parsing errors. If |
| 744 | false (the default), errors are silently ignored. If true, |
| 745 | errors raise a ValueError exception. |
| 746 | |
| 747 | encoding and errors: specify how to decode percent-encoded sequences |
| 748 | into Unicode characters, as accepted by the bytes.decode() method. |
| 749 | |
| 750 | max_num_fields: int. If set, then throws a ValueError |
| 751 | if there are more than n fields read by parse_qsl(). |
| 752 | |
| 753 | separator: str. The symbol to use for separating the query arguments. |
| 754 | Defaults to &. |
| 755 | |
| 756 | Returns a list, as G-d intended. |
| 757 | """ |
| 758 | |
| 759 | if not separator or not isinstance(separator, (str, bytes)): |
| 760 | raise ValueError("Separator must be of type string or bytes.") |
| 761 | if isinstance(qs, str): |
| 762 | if not isinstance(separator, str): |
| 763 | separator = str(separator, 'ascii') |
| 764 | eq = '=' |
| 765 | def _unquote(s): |
| 766 | return unquote_plus(s, encoding=encoding, errors=errors) |
| 767 | else: |
| 768 | if not qs: |
| 769 | return [] |
| 770 | # Use memoryview() to reject integers and iterables, |
| 771 | # acceptable by the bytes constructor. |
| 772 | qs = bytes(memoryview(qs)) |
| 773 | if isinstance(separator, str): |
| 774 | separator = bytes(separator, 'ascii') |
| 775 | eq = b'=' |
| 776 | def _unquote(s): |
| 777 | return unquote_to_bytes(s.replace(b'+', b' ')) |
| 778 | |
| 779 | if not qs: |
| 780 | return [] |
| 781 | |
| 782 | # If max_num_fields is defined then check that the number of fields |
| 783 | # is less than max_num_fields. This prevents a memory exhaustion DOS |
| 784 | # attack via post bodies with many fields. |
| 785 | if max_num_fields is not None: |
| 786 | num_fields = 1 + qs.count(separator) |