From ad35b7d52b0ee467e6d5e519b529622d48611d48 Mon Sep 17 00:00:00 2001 From: UIJM Date: Sun, 26 Jul 2026 11:56:18 +0800 Subject: [PATCH] fix: parse_curl: strip '\n' from tokens when shlex.split mangles backslash-newline MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When copying cURL from browser DevTools (Copy as cURL), multi-line \\\n continuations cause shlex.split to produce tokens like "\n-H" instead of "-H". These tokens fail both t in ("-H", "--header") and t.startswith("-"), causing the entire header block to be silently dropped and cURL import to fail with "未从 cURL 提取到 Token". Strip only leading '\n' (not arbitrary whitespace) from each parsed token, then drop empty tokens before the main parsing loop. Fixes #17 --- proxy.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/proxy.py b/proxy.py index 3518dff..74fc620 100644 --- a/proxy.py +++ b/proxy.py @@ -1438,6 +1438,13 @@ def parse_curl(curl: str) -> dict: tokens = shlex.split(curl) except ValueError: tokens = curl.replace("\\\n", " ").split() + # When cURL uses \\\n (backslash-newline) continuation (e.g. Chrome + # DevTools "Copy as cURL" on macOS), shlex.split preserves '\n' as + # the first character of the following token, producing "\n-H" or + # "\n--data-raw". Strip only leading '\n' — not all whitespace — + # so intentional whitespace in values is preserved. + tokens = [t.lstrip('\n') for t in tokens] + tokens = [t for t in tokens if t] out = {"url": "", "headers": {}, "body": ""} i = 0 while i < len(tokens):