Initialize the MultiPartParser object. :META: The standard ``META`` dictionary in Django request objects. :input_data: The raw post data, as a file-like object. :upload_handlers: A list of UploadHandler instances that perform oper
(self, META, input_data, upload_handlers, encoding=None)
| 56 | boundary_re = _lazy_re_compile(r"[ -~]{0,200}[!-~]") |
| 57 | |
| 58 | def __init__(self, META, input_data, upload_handlers, encoding=None): |
| 59 | """ |
| 60 | Initialize the MultiPartParser object. |
| 61 | |
| 62 | :META: |
| 63 | The standard ``META`` dictionary in Django request objects. |
| 64 | :input_data: |
| 65 | The raw post data, as a file-like object. |
| 66 | :upload_handlers: |
| 67 | A list of UploadHandler instances that perform operations on the |
| 68 | uploaded data. |
| 69 | :encoding: |
| 70 | The encoding with which to treat the incoming data. |
| 71 | """ |
| 72 | # Content-Type should contain multipart and the boundary information. |
| 73 | content_type = META.get("CONTENT_TYPE", "") |
| 74 | if not content_type.startswith("multipart/"): |
| 75 | raise MultiPartParserError("Invalid Content-Type: %s" % content_type) |
| 76 | |
| 77 | try: |
| 78 | content_type.encode("ascii") |
| 79 | except UnicodeEncodeError: |
| 80 | raise MultiPartParserError( |
| 81 | "Invalid non-ASCII Content-Type in multipart: %s" |
| 82 | % force_str(content_type) |
| 83 | ) |
| 84 | |
| 85 | # Parse the header to get the boundary to split the parts. |
| 86 | _, opts = parse_header_parameters(content_type) |
| 87 | boundary = opts.get("boundary") |
| 88 | if not boundary or not self.boundary_re.fullmatch(boundary): |
| 89 | raise MultiPartParserError( |
| 90 | "Invalid boundary in multipart: %s" % force_str(boundary) |
| 91 | ) |
| 92 | |
| 93 | # Content-Length should contain the length of the body we are about |
| 94 | # to receive. |
| 95 | try: |
| 96 | content_length = int(META.get("CONTENT_LENGTH", 0)) |
| 97 | except (ValueError, TypeError): |
| 98 | content_length = 0 |
| 99 | |
| 100 | if content_length < 0: |
| 101 | # This means we shouldn't continue...raise an error. |
| 102 | raise MultiPartParserError("Invalid content length: %r" % content_length) |
| 103 | |
| 104 | self._boundary = boundary.encode("ascii") |
| 105 | self._input_data = input_data |
| 106 | |
| 107 | # For compatibility with low-level network APIs (with 32-bit integers), |
| 108 | # the chunk size should be < 2^31, but still divisible by 4. |
| 109 | possible_sizes = [x.chunk_size for x in upload_handlers if x.chunk_size] |
| 110 | self._chunk_size = min([2**31 - 4, *possible_sizes]) |
| 111 | |
| 112 | self._meta = META |
| 113 | self._encoding = encoding or settings.DEFAULT_CHARSET |
| 114 | self._content_length = content_length |
| 115 | self._upload_handlers = upload_handlers |
nothing calls this directly
no test coverage detected