fix: Properly parse CURL commands for api request (#8592)

* fix: Properly parse CURL commands for api request

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Eric Hare 2025-06-17 10:07:56 -07:00 committed by GitHub
commit cb6ec940f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -122,24 +122,29 @@ def parse_curl_command(curl_command):
def parse_context(curl_command):
method = "get"
if not curl_command:
if not curl_command or not curl_command.strip():
return ParsedContext(
method=method, url="", data=None, headers={}, cookies={}, verify=True, auth=None, proxy=None
)
# Strip whitespace to handle formatting issues
curl_command = curl_command.strip()
parsed_args: ParsedArgs = parse_curl_command(curl_command)
post_data = parsed_args.data or parsed_args.data_binary
# Safeguard against missing parsed_args attributes
post_data = getattr(parsed_args, "data", None) or getattr(parsed_args, "data_binary", None)
if post_data:
method = "post"
if parsed_args.method:
# Prioritize explicit method from -X flag
if getattr(parsed_args, "method", None):
method = parsed_args.method.lower()
cookie_dict = OrderedDict()
quoted_headers = OrderedDict()
for curl_header in parsed_args.headers:
# Process headers safely
for curl_header in getattr(parsed_args, "headers", []):
if curl_header.startswith(":"):
occurrence = [m.start() for m in re.finditer(r":", curl_header)]
header_key, header_value = curl_header[: occurrence[1]], curl_header[occurrence[1] + 1 :]
@ -153,20 +158,19 @@ def parse_context(curl_command):
else:
quoted_headers[header_key] = header_value.strip()
# add auth
user = parsed_args.user
if parsed_args.user:
# Add auth
user = getattr(parsed_args, "user", None)
if user:
user = tuple(user.split(":"))
# add proxy and its authentication if it's available.
proxies = parsed_args.proxy
# proxy_auth = parsed_args.proxy_user
if parsed_args.proxy and parsed_args.proxy_user:
# Add proxy and its authentication if available
proxies = getattr(parsed_args, "proxy", None)
if proxies and getattr(parsed_args, "proxy_user", None):
proxies = {
"http": f"http://{parsed_args.proxy_user}@{parsed_args.proxy}/",
"https": f"http://{parsed_args.proxy_user}@{parsed_args.proxy}/",
}
elif parsed_args.proxy:
elif proxies:
proxies = {
"http": f"http://{parsed_args.proxy}/",
"https": f"http://{parsed_args.proxy}/",
@ -174,11 +178,11 @@ def parse_context(curl_command):
return ParsedContext(
method=method,
url=parsed_args.url,
url=getattr(parsed_args, "url", ""), # Default to empty string if URL is missing
data=post_data,
headers=quoted_headers,
cookies=cookie_dict,
verify=parsed_args.insecure,
verify=getattr(parsed_args, "insecure", True), # Default to True if missing
auth=user,
proxy=proxies,
)