import subprocess
import shlex
from typing import Dict, Any
from shutil import which
class CurlError(Exception):
"""Custom exception for curl-related errors"""
pass
def run_curl(target: str, opts: str) -> Dict[str, Any]:
"""
Run a curl command against the given target with the provided options.
:param target: URL or hostname (e.g. "https://example.com")
:param opts: space-separated curl options (e.g. "-I -L")
:return: A dictionary containing status, stdout, and stderr
"""
try:
if which("curl") is None:
raise CurlError(
"curl not found in PATH. Please install curl or ensure it's in your system PATH."
)
cmd = ["curl"]
if opts:
cmd.extend(shlex.split(opts))
cmd.append(target)
completed = subprocess.run(
cmd,
text=True,
capture_output=True,
timeout=60
)
return {
"status": "success" if completed.returncode == 0 else "error",
"return_code": completed.returncode,
"stdout": completed.stdout,
"stderr": completed.stderr,
"command": " ".join(cmd)
}
except subprocess.TimeoutExpired:
raise CurlError(f"curl command timed out after 60 seconds for target: {target}")
except FileNotFoundError as e:
raise CurlError(f"curl executable not found: {e}")
except Exception as e:
raise CurlError(f"Error running curl against {target}: {str(e)}")