fix: manual multipart parser (reliable)
Deploy contracts-flask / validate (push) Successful in 0s

This commit is contained in:
2026-06-29 18:08:34 +04:00
parent f5f8bf294b
commit e98bac278e
+23 -13
View File
@@ -223,24 +223,34 @@ class Handler(BaseHTTPRequestHandler):
raw = self.rfile.read(length) raw = self.rfile.read(length)
ctype = self.headers.get("Content-Type", "") ctype = self.headers.get("Content-Type", "")
# Простой multipart приём (как в upload.py) # Простой multipart приём
import cgi, io as io_mod import cgi, re, io as io_mod
_, params = cgi.parse_header(ctype) _, params = cgi.parse_header(ctype)
fs = cgi.FieldStorage( boundary = params.get("boundary", "")
fp=io_mod.BytesIO(raw), if not boundary:
environ={'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': ctype}, self._json({"ok": False, "error": "No boundary"}, 400)
keep_blank_values=True return
) # Ручной разбор multipart (надёжнее чем cgi.FieldStorage)
items = fs.getlist("files") if hasattr(fs, 'getlist') else ([fs["files"]] if "files" in fs else []) parts = raw.split(b"--" + boundary.encode())
files = [] files = []
for item in items: for part in parts:
if not getattr(item, 'filename', None): if b"Content-Disposition" not in part:
continue continue
name = os.path.basename(item.filename) try:
hdr_end = part.index(b"\r\n\r\n")
except ValueError:
continue
hdrs = part[:hdr_end].decode("latin-1", errors="replace")
body = part[hdr_end + 4:]
if body.endswith(b"\r\n"):
body = body[:-2]
m = re.search(r'filename="([^"]*)"', hdrs)
if not m:
continue
name = os.path.basename(m.group(1))
if not name or ".." in name or "/" in name or "\\" in name: if not name or ".." in name or "/" in name or "\\" in name:
continue continue
data = item.file.read() files.append((name, body, ""))
files.append((name, data, ""))
if not files: if not files:
self._json({"ok": False, "error": "Нет файлов"}, 400) self._json({"ok": False, "error": "Нет файлов"}, 400)