refactor: Optimizations to CLI

This commit is contained in:
Karim shoair
2025-06-29 19:10:52 +03:00
parent b28f90e854
commit 29e485bdf2
2 changed files with 51 additions and 66 deletions
+41 -36
View File
@@ -73,6 +73,46 @@ def _CookieParser(cookie_string):
yield key, morsel.value
def _ParseHeaders(
header_lines: List[str], parse_cookies: bool = True
) -> Tuple[Dict[str, str], Dict[str, str]]:
"""Parses headers into separate header and cookie dictionaries."""
header_dict = dict()
cookie_dict = dict()
for header_line in header_lines:
if ":" not in header_line:
if header_line.endswith(";"):
header_key = header_line[:-1].strip()
header_value = ""
header_dict[header_key] = header_value
else:
raise ValueError(
f"Could not parse header without colon: '{header_line}'."
)
else:
header_key, header_value = header_line.split(":", 1)
header_key = header_key.strip()
header_value = header_value.strip()
if parse_cookies:
if header_key.lower() == "cookie":
try:
cookie_dict = {
key: value for key, value in _CookieParser(header_value)
}
except Exception as e:
raise ValueError(
f"Could not parse cookie string from header '{header_value}': {e}"
)
else:
header_dict[header_key] = header_value
else:
header_dict[header_key] = header_value
return header_dict, cookie_dict
# Suppress exit on error to handle parsing errors gracefully
class NoExitArgumentParser(ArgumentParser):
def error(self, message):
@@ -142,41 +182,6 @@ class CurlParser:
self._supported_methods = ("get", "post", "put", "delete")
# --- Helper Functions ---
@staticmethod
def parse_headers(header_lines: List[str]) -> Tuple[Dict[str, str], Dict[str, str]]:
"""Parses -H headers into separate header and cookie dictionaries."""
header_dict = dict()
cookie_dict = dict()
for header_line in header_lines:
if ":" not in header_line:
if header_line.endswith(";"):
header_key = header_line[:-1].strip()
header_value = ""
header_dict[header_key] = header_value
else:
log.warning(
f"Could not parse header without colon: '{header_line}', skipping."
)
continue
else:
header_key, header_value = header_line.split(":", 1)
header_key = header_key.strip()
header_value = header_value.strip()
if header_key.lower() == "cookie":
try:
cookie_dict = {
key: value for key, value in _CookieParser(header_value)
}
except Exception as e:
raise ValueError(
f"Could not parse cookie string from -H '{header_value}': {e}"
)
else:
header_dict[header_key] = header_value
return header_dict, cookie_dict
# --- Main Parsing Logic ---
def parse(self, curl_command: str) -> Optional[Request]:
@@ -225,7 +230,7 @@ class CurlParser:
):
method = "post"
headers, cookies = self.parse_headers(parsed_args.header)
headers, cookies = _ParseHeaders(parsed_args.header)
if parsed_args.cookie:
# We are focusing on the string format from DevTools.