Parses a form request body. Supports ``application/x-www-form-urlencoded`` and ``multipart/form-data``. The ``content_type`` parameter should be a string and ``body`` should be a byte string. The ``arguments`` and ``files`` parameters are dictionaries that will be updated with
(
content_type: str,
body: bytes,
arguments: Dict[str, List[bytes]],
files: Dict[str, List[HTTPFile]],
headers: Optional[HTTPHeaders] = None,
)
| 743 | |
| 744 | |
| 745 | def parse_body_arguments( |
| 746 | content_type: str, |
| 747 | body: bytes, |
| 748 | arguments: Dict[str, List[bytes]], |
| 749 | files: Dict[str, List[HTTPFile]], |
| 750 | headers: Optional[HTTPHeaders] = None, |
| 751 | ) -> None: |
| 752 | """Parses a form request body. |
| 753 | |
| 754 | Supports ``application/x-www-form-urlencoded`` and |
| 755 | ``multipart/form-data``. The ``content_type`` parameter should be |
| 756 | a string and ``body`` should be a byte string. The ``arguments`` |
| 757 | and ``files`` parameters are dictionaries that will be updated |
| 758 | with the parsed contents. |
| 759 | """ |
| 760 | if content_type.startswith("application/x-www-form-urlencoded"): |
| 761 | if headers and "Content-Encoding" in headers: |
| 762 | gen_log.warning( |
| 763 | "Unsupported Content-Encoding: %s", headers["Content-Encoding"] |
| 764 | ) |
| 765 | return |
| 766 | try: |
| 767 | # real charset decoding will happen in RequestHandler.decode_argument() |
| 768 | uri_arguments = parse_qs_bytes(body, keep_blank_values=True) |
| 769 | except Exception as e: |
| 770 | gen_log.warning("Invalid x-www-form-urlencoded body: %s", e) |
| 771 | uri_arguments = {} |
| 772 | for name, values in uri_arguments.items(): |
| 773 | if values: |
| 774 | arguments.setdefault(name, []).extend(values) |
| 775 | elif content_type.startswith("multipart/form-data"): |
| 776 | if headers and "Content-Encoding" in headers: |
| 777 | gen_log.warning( |
| 778 | "Unsupported Content-Encoding: %s", headers["Content-Encoding"] |
| 779 | ) |
| 780 | return |
| 781 | try: |
| 782 | fields = content_type.split(";") |
| 783 | for field in fields: |
| 784 | k, sep, v = field.strip().partition("=") |
| 785 | if k == "boundary" and v: |
| 786 | parse_multipart_form_data(utf8(v), body, arguments, files) |
| 787 | break |
| 788 | else: |
| 789 | raise ValueError("multipart boundary not found") |
| 790 | except Exception as e: |
| 791 | gen_log.warning("Invalid multipart/form-data: %s", e) |
| 792 | |
| 793 | |
| 794 | def parse_multipart_form_data( |
no test coverage detected