| 62 | self.files = {} |
| 63 | |
| 64 | def read(self, chunk): |
| 65 | if self._reached_end(): |
| 66 | return |
| 67 | |
| 68 | self._buffer += chunk |
| 69 | |
| 70 | self._read_bytes += len(chunk) |
| 71 | |
| 72 | if not self._reached_end() and len(self._buffer) < 128: |
| 73 | return |
| 74 | |
| 75 | count = 0 |
| 76 | |
| 77 | while self._buffer and count < 10000: |
| 78 | count += 1 |
| 79 | |
| 80 | if not self._current_part: |
| 81 | if b'\r\n\r\n' not in self._buffer: |
| 82 | break |
| 83 | |
| 84 | if not self._buffer.startswith(self._boundary): |
| 85 | raise Exception('Invalid buffer format: ' + str(self._buffer)) |
| 86 | |
| 87 | part = self._buffer[len(self._fields_separator):] |
| 88 | |
| 89 | (header, self._buffer) = part.split(b'\r\n\r\n', 1) |
| 90 | |
| 91 | self._current_part = _FormDataPart(header, self._output_files_path) |
| 92 | |
| 93 | if self._fields_separator not in self._buffer: |
| 94 | potential_separator_start = self._find_potential_separator_start(self._buffer) |
| 95 | |
| 96 | if potential_separator_start >= 0: |
| 97 | body = self._buffer[:potential_separator_start] |
| 98 | self._buffer = self._buffer[potential_separator_start:] |
| 99 | else: |
| 100 | body = self._buffer |
| 101 | self._buffer = b'' |
| 102 | |
| 103 | self._current_part.write(body) |
| 104 | break |
| 105 | |
| 106 | body_end = self._buffer.index(self._fields_separator) |
| 107 | body = self._buffer[:body_end] |
| 108 | self._buffer = self._buffer[body_end + 2:] |
| 109 | |
| 110 | self._current_part.write(body) |
| 111 | self._add_value(self._current_part) |
| 112 | self._current_part = None |
| 113 | |
| 114 | if self._reached_end(): |
| 115 | if self._current_part: |
| 116 | self._current_part.write(self._buffer) |
| 117 | self._buffer = b'' |
| 118 | self._add_value(self._current_part) |
| 119 | self._current_part = None |
| 120 | |
| 121 | def _reached_end(self): |