POST method handler.
(self)
| 175 | self.send_error(404, f"File Not Found: {path}") |
| 176 | |
| 177 | def do_POST(self): |
| 178 | """POST method handler.""" |
| 179 | try: |
| 180 | remaining_bytes = int(self.headers.get("content-length", 0)) |
| 181 | contents = self.rfile.read(remaining_bytes).decode("utf-8") |
| 182 | |
| 183 | path = self.path[1:].split("?")[0] |
| 184 | |
| 185 | if path == "echo_body": |
| 186 | self._send_response("text/plain") |
| 187 | self.wfile.write(contents.encode("utf-8")) |
| 188 | return |
| 189 | |
| 190 | fn_match = re.search(r'Content-Disposition.*name="upload"; filename="(.*)"', contents) |
| 191 | if not fn_match: |
| 192 | self.send_error(500, f"File not found in content. {contents}") |
| 193 | return |
| 194 | self._send_response("text/html") |
| 195 | self.wfile.write( |
| 196 | f"""<!doctype html> |
| 197 | {contents} |
| 198 | <script>window.top.window.onUploadDone();</script> |
| 199 | """.encode() |
| 200 | ) |
| 201 | except Exception as e: |
| 202 | self.send_error(500, f"Error found: {e}") |
| 203 | |
| 204 | def log_message(self, format, *args): |
| 205 | """Override default to avoid trashing stderr.""" |