curlconverter / curlconverter

Transpile curl commands into Python, JavaScript and 27 other languages
https://curlconverter.com
MIT License
7.17k stars 867 forks source link

Add most arguments to C #630

Closed verhovsky closed 3 months ago

verhovsky commented 3 months ago
import sys
import re
import difflib
import subprocess

# copied from curl manpage
args = [
    ["--tls-max", "1.2"],
    ["--tls13-ciphers", "TLS_AES_128_GCM_SHA256"],
    ["--tlsauthtype", "SRP"],
    ["--tlspassword", "pwd"],
    ["--tlsuser", "user"],
    ["--tlsv1.0"],
    ["--tlsv1.1"],
    ["--tlsv1.2"],
    ["--tlsv1.3"],
    ["--tlsv1"],
    ["--socks4", "hostname:4096"],
    ["--socks4a", "hostname:4096"],
    ["--socks5-basic"],
    ["--socks5-gssapi-nec"],
    ["--socks5-gssapi-service", "sockd"],
    ["--socks5-gssapi"],
    ["--socks5-hostname", "proxy.example:7000"],
    ["--socks5", "proxy.example:7000"],
    ["--ssl-allow-beast"],
    ["--ssl-auto-client-cert"],
    ["--ssl-no-revoke"],
    ["--ssl-reqd"],
    ["--ssl-revoke-best-effort"],
    ["--ssl"],
    ["--sslv2"],
    ["--sslv3"],
    # ["--proxy-anyauth"],
    # ["--proxy-basic"],
    # ["--proxy-digest"],
    # ["--proxy-negotiate"],
    # ["--proxy-ntlm"],
    # ["--proxy-ca-native"],
    # ["--proxy-cacert", "CA-file.txt"],
    # ["--proxy-capath", "/local/directory"],
    # ["--proxy-cert-type", "PEM"],
    # ["--proxy-cert", "file"],
    # ["--proxy-ciphers", "ECDHE-ECDSA-AES256-CCM8"],
    # ["--proxy-crlfile", "rejects.txt"],
    # ["--proxy-header", "X-First-Name: Joe"],
    # ["--proxy-header", "Host:"],
    # ["--proxy-http2"],
    # ["--proxy-insecure"],
    # ["--proxy-key", "here"],
    # ["--proxy-key-type", "DER"],
    # ["--proxy-pass", "secret"],
    # ["--proxy-pinnedpubkey", "keyfile"],
    # ["--proxy-pinnedpubkey", "sha256//ce118b51897f4452dc"],
    # ["--proxy-service-name", "shrubbery"],
    # ["--proxy-ssl-allow-beast"],
    # ["--proxy-ssl-auto-client-cert"],
    # ["--proxy-tls13-ciphers", "TLS_AES_128_GCM_SHA256"],
    # ["--proxy-tlsauthtype", "SRP"],
    # ["--proxy-tlspassword", "passwd"],
    # ["--proxy-tlsuser", "smith"],
    # ["--proxy-tlsv1"],
    # ["--proxy-user", "name:pwd"],
    # ["--proxytunnel"],
]

common_args = [
    "curl",
    "example.com",
    "--silent",
    "--output",
    "/dev/null",
    # "-x", "http://proxy",
    "--libcurl",
    "-",
]

def run(args):
    result = subprocess.run(
        [
            *common_args,
            *args,
        ],
        text=True,
        capture_output=True,
    ).stdout
    if not result:
        print("Error", file=sys.stderr)
    return result.splitlines()

d = difflib.Differ()
without = run([])

def replace_with_upper(match):
    # Extract the character after the dash and convert it to uppercase
    char = match.group(1).upper()
    return char

for arg in args:
    code_arg = re.sub(r"-(.)", replace_with_upper, arg[0].strip("-"))
    is_bool = len(arg) == 1
    print(code_arg, arg, file=sys.stderr)

    result = subprocess.run(
        [*common_args, *arg],
        text=True,
        capture_output=True,
    ).stdout
    if not result:
        print("Error", file=sys.stderr)
        continue
    with_arg = result.splitlines()

    diff = list(d.compare(without, with_arg))
    changed_lines = [
        line.removeprefix("+ ")
        for line in diff
        if line.startswith("+ ") or line.startswith("- ")
    ]

    if not changed_lines:
        print("No difference", file=sys.stderr)
        continue

    print("\n".join(changed_lines), file=sys.stderr)
    print(file=sys.stderr)

    print("if (request." + code_arg + ") {")
    for line in changed_lines:
        line = line + "\n"
        if is_bool:
            print("  code += " + repr(line) + ";")
        else:
            other_arg = arg[1]
            other_arg_raw = '"' + other_arg + '"'
            if other_arg_raw in line:
                start, end = line.split(other_arg_raw, 1)
                print(
                    "  const "
                    + code_arg
                    + " = repr(request."
                    + code_arg
                    + ", imports);"
                )
                print(
                    "  code += "
                    + repr(start)
                    + " + "
                    + code_arg
                    + " + "
                    + repr(end)
                    + ";"
                )
            else:
                print("arg value missing", file=sys.stderr)
                print("  code += " + repr(line) + ";")
    print("}")
    print(file=sys.stderr)
    print(file=sys.stderr)

print()
print()
print()

for arg in args:
    config_code_arg = arg[0].lstrip("-")
    code_arg = re.sub(r"-(.)", replace_with_upper, arg[0].strip("-"))
    is_bool = len(arg) == 1
    if is_bool:
        print(
            "if (Object.prototype.hasOwnProperty.call(config, '"
            + config_code_arg
            + "')) {"
        )
    else:
        print("if (config['" + config_code_arg + "']) {")
    print("  request." + code_arg + " = config['" + config_code_arg + "'];")
    print("}")

print()
print()
print()
for arg in args:
    code_arg = re.sub(r"-(.)", replace_with_upper, arg[0].strip("-"))
    is_bool = len(arg) == 1
    print(repr(code_arg) + "?: " + ("boolean" if is_bool else "Word") + ";")