From 086ee8900b81c9162707803ed50b8f951b001c71 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 3 Oct 2024 09:30:35 +0200 Subject: [PATCH 1/6] feat(landerctl): implement -S flag using restructured codebase --- Makefile | 1 + landerctl/include/landerctl.h | 20 +++ landerctl/src/args.c | 93 +++++++++++ landerctl/src/commands.c | 149 +++++++++++++++++ landerctl/src/main.c | 297 ++++++++++++++++++---------------- 5 files changed, 423 insertions(+), 137 deletions(-) create mode 100644 landerctl/src/args.c create mode 100644 landerctl/src/commands.c diff --git a/Makefile b/Makefile index 9cd57fe..b020898 100644 --- a/Makefile +++ b/Makefile @@ -141,6 +141,7 @@ clean: bear: clean bear -- make bear --append -- make build-test + bear --append -- make -C landerctl # Make make aware of the .d files diff --git a/landerctl/include/landerctl.h b/landerctl/include/landerctl.h index 75da26e..b6e0d8c 100644 --- a/landerctl/include/landerctl.h +++ b/landerctl/include/landerctl.h @@ -50,6 +50,23 @@ typedef struct landerctl_ctx { FILE *data_file; } landerctl_ctx; +typedef struct landerctl_args { + bool secure; + bool verbose; + landerctl_cfg cfg; + struct { + char **arr; + int len; + } args; + landerctl_mode mode; +} landerctl_args; + +typedef struct landerctl_curl { + CURL *curl; + struct curl_slist *headers; + char err_msg[CURL_ERROR_SIZE]; +} landerctl_curl; + const char *landerctl_err_msg(landerctl_err err); void landerctl_set_common(landerctl_ctx *ctx); @@ -57,4 +74,7 @@ landerctl_err landerctl_post_short(landerctl_ctx *ctx); landerctl_err landerctl_post_paste(landerctl_ctx *ctx); landerctl_err landerctl_post_file(landerctl_ctx *ctx); +int landerctl_cmd_short(landerctl_args *args); +int landerctl_parse_args(landerctl_args *out, int argc, char **argv); + #endif diff --git a/landerctl/src/args.c b/landerctl/src/args.c new file mode 100644 index 0000000..9538a27 --- /dev/null +++ b/landerctl/src/args.c @@ -0,0 +1,93 @@ +#include +#include +#include + +#include "landerctl.h" + +const char *cfg_file_name = ".landerrc"; +const char *usage = "%s [-SPFsv] [-c CONFIG_FILE] [ARGS]\n"; + +int landerctl_parse_config(landerctl_cfg *cfg, const char *path) { + // Parse default file location instead + if (path == NULL) { + const char *home_dir = getenv("HOME"); + + if (home_dir == NULL) { + path = cfg_file_name; + } else { + // This is a blatant memleak if a custom config file is set, but it really + // doesn't matter for a short-lived CLI tool + char *buf = malloc(strlen(home_dir) + strlen(cfg_file_name) + 2); + sprintf(buf, "%s/%s", home_dir, cfg_file_name); + path = buf; + } + } + + char *err_msg = NULL; + landerctl_cfg_err parse_res = landerctl_cfg_parse(cfg, path); + + switch (parse_res) { + case landerctl_cfg_err_ok: + break; + case landerctl_cfg_err_not_found: + err_msg = "Config file not found"; + break; + case landerctl_cfg_err_invalid: + err_msg = "Invalid config file"; + break; + case landerctl_cfg_err_incomplete: + err_msg = "Incomplete config file"; + break; + } + + if (err_msg != NULL) { + fprintf(stderr, "%s\n", err_msg); + + return 1; + } + + return 0; +} + +int landerctl_parse_args(landerctl_args *args, int argc, char **argv) { + optind = 1; + int c; + + const char *cfg_path = NULL; + + while ((c = getopt(argc, argv, "SPFsvc:")) != -1) { + switch (c) { + case 'S': + args->mode = landerctl_mode_short; + break; + case 'P': + args->mode = landerctl_mode_paste; + break; + case 'F': + args->mode = landerctl_mode_file; + break; + case 's': + args->secure = true; + break; + case 'v': + args->verbose = true; + break; + case 'c': + cfg_path = optarg; + break; + case '?': + fprintf(stderr, usage, argv[0]); + return 2; + } + } + + int res; + if ((res = landerctl_parse_config(&args->cfg, cfg_path))) { + return res; + } + + args->args.arr = &argv[optind]; + args->args.len = argc - optind; + + return 0; +} diff --git a/landerctl/src/commands.c b/landerctl/src/commands.c new file mode 100644 index 0000000..4ab4c8b --- /dev/null +++ b/landerctl/src/commands.c @@ -0,0 +1,149 @@ +#include + +#include + +#include "landerctl.h" + +int landerctl_curl_init(landerctl_curl *out) { + curl_global_init(CURL_GLOBAL_ALL); + CURL *curl = curl_easy_init(); + + if (curl == NULL) { + fprintf(stderr, "Failed to initialize cURL client.\n"); + + return 8; + } + + out->curl = curl; + out->headers = NULL; + + return 0; +} + +int landerctl_curl_set_common(landerctl_curl *curl, landerctl_args *args, + const char *key) { + size_t url_len = strlen(args->cfg.server_url) + 4; + + if (key != NULL) { + url_len += strlen(key); + } + + char mode_char; + + switch (args->mode) { + case landerctl_mode_short: + mode_char = 's'; + break; + case landerctl_mode_paste: + mode_char = 'p'; + break; + case landerctl_mode_file: + mode_char = 'f'; + break; + // Shouldn't be able to happen + default: + return 10; + } + + char url[url_len + 1]; + + if (key == NULL) { + sprintf(url, "%s/%c%s/", args->cfg.server_url, mode_char, + args->secure ? "l" : ""); + } else { + sprintf(url, "%s/%c%s/%s", args->cfg.server_url, mode_char, + args->secure ? "l" : "", key); + } + + curl_easy_setopt(curl->curl, CURLOPT_URL, url); + + // Add API key header + char api_key_header[strlen(args->cfg.api_key) + 12]; + sprintf(api_key_header, "X-Api-Key: %s", args->cfg.api_key); + + curl->headers = curl_slist_append(curl->headers, api_key_header); + + curl_easy_setopt(curl->curl, CURLOPT_USERAGENT, + "landerctl/" LANDER_VERSION ""); + + if (args->verbose) { + curl_easy_setopt(curl->curl, CURLOPT_VERBOSE, 1L); + } + + curl_easy_setopt(curl->curl, CURLOPT_CAINFO, args->cfg.ca_certs_bundle); + curl_easy_setopt(curl->curl, CURLOPT_ERRORBUFFER, curl->err_msg); + + return 0; +} + +int landerctl_curl_perform(landerctl_curl *curl) { + curl_easy_setopt(curl->curl, CURLOPT_HTTPHEADER, curl->headers); + + int res = curl_easy_perform(curl->curl); + + if (res != CURLE_OK) { + fprintf(stderr, "Libcurl encountered an error (code %i): %s\n", res, + curl->err_msg); + } + + return res; +} + +int landerctl_curl_inspect(landerctl_curl *curl, landerctl_args *args) { + long response_code; + curl_easy_getinfo(curl->curl, CURLINFO_RESPONSE_CODE, &response_code); + + if (response_code < 200 || response_code > 299) { + fprintf(stderr, "HTTP status code %li\n", response_code); + + return 3; + } else { + struct curl_header *location_header; + + if (curl_easy_header(curl->curl, "Location", 0, CURLH_HEADER, -1, + &location_header) == CURLHE_OK) { + printf("%s%s\n", args->cfg.server_url, location_header->value); + } else { + fprintf(stderr, "Server returned a 2xx without a Location header.\n"); + + return 5; + } + } + + return 0; +} + +void landerctl_curl_cleanup(landerctl_curl *curl) { + curl_easy_cleanup(curl->curl); + curl_slist_free_all(curl->headers); +} + +int landerctl_cmd_short(landerctl_args *args) { + // TODO argument count check + int res; + landerctl_curl curl; + + if ((res = landerctl_curl_init(&curl))) { + return res; + } + + const char *key = args->args.len == 2 ? args->args.arr[1] : NULL; + if ((res = landerctl_curl_set_common(&curl, args, key))) { + return res; + } + + const char *url = args->args.arr[0]; + + curl_easy_setopt(curl.curl, CURLOPT_POSTFIELDSIZE, strlen(url)); + curl_easy_setopt(curl.curl, CURLOPT_POSTFIELDS, url); + + res = landerctl_curl_perform(&curl); + + if (res == 0) { + landerctl_curl_inspect(&curl, args); + } + + landerctl_curl_cleanup(&curl); + + return res; +} diff --git a/landerctl/src/main.c b/landerctl/src/main.c index fd53944..0566648 100644 --- a/landerctl/src/main.c +++ b/landerctl/src/main.c @@ -9,162 +9,185 @@ #include "landerctl.h" -const char *cfg_file_name = ".landerrc"; -const char *usage = "%s [-SPFsv] [-c CONFIG_FILE] arg [key]\n"; - int main(int argc, char **argv) { - landerctl_ctx ctx = {0}; + landerctl_args args = {0}; - const char *home_dir = getenv("HOME"); - const char *cfg_path; - - if (home_dir == NULL) { - cfg_path = cfg_file_name; - } else { - // This is a blatant memleak if a custom config file is set, but it really - // doesn't matter for a short-lived CLI tool - char *buf = malloc(strlen(home_dir) + strlen(cfg_file_name) + 2); - sprintf(buf, "%s/%s", home_dir, cfg_file_name); - cfg_path = buf; + int res; + if ((res = landerctl_parse_args(&args, argc, argv))) { + return res; } - opterr = 0; - int c; - - while ((c = getopt(argc, argv, "SPFsvc:")) != -1) { - switch (c) { - case 'S': - ctx.mode = landerctl_mode_short; - break; - case 'P': - ctx.mode = landerctl_mode_paste; - break; - case 'F': - ctx.mode = landerctl_mode_file; - break; - case 's': - ctx.secure = true; - break; - case 'v': - ctx.verbose = true; - break; - case 'c': - cfg_path = optarg; - break; - case '?': - printf(usage, argv[0]); - exit(2); - } - } - - char *err_msg = NULL; - landerctl_cfg_err parse_res = landerctl_cfg_parse(&ctx.cfg, cfg_path); - - switch (parse_res) { - case landerctl_cfg_err_ok: - break; - case landerctl_cfg_err_not_found: - err_msg = "Config file not found"; - break; - case landerctl_cfg_err_invalid: - err_msg = "Invalid config file"; - break; - case landerctl_cfg_err_incomplete: - err_msg = "Incomplete config file"; - break; - } - - if (err_msg != NULL) { - fprintf(stderr, "%s\n", err_msg); - exit(1); - } - - if (ctx.mode == landerctl_mode_none) { - printf("No mode specified.\n\n"); - printf(usage, argv[0]); - exit(2); - } - - if (optind == argc || (argc - optind > 2)) { - printf(usage, argv[0]); - exit(2); - } - - ctx.arg = argv[optind]; - ctx.key = argc - optind == 2 ? argv[optind + 1] : NULL; - - curl_global_init(CURL_GLOBAL_ALL); - ctx.curl = curl_easy_init(); - - if (ctx.curl == NULL) { - exit(255); - } - - landerctl_set_common(&ctx); - landerctl_err res; - - switch (ctx.mode) { + switch (args.mode) { case landerctl_mode_short: - res = landerctl_post_short(&ctx); - break; - case landerctl_mode_paste: - res = landerctl_post_paste(&ctx); - break; - case landerctl_mode_file: - res = landerctl_post_file(&ctx); + res = landerctl_cmd_short(&args); break; + /* case landerctl_mode_paste: */ + /* res = landerctl_post_paste(&ctx); */ + /* break; */ + /* case landerctl_mode_file: */ + /* res = landerctl_post_file(&ctx); */ + /* break; */ default: - return 7; + res = 7; + break; } - if (res != landerctl_err_ok) { - printf("%s\n", landerctl_err_msg(res)); - exit(6); - } + return res; - if (ctx.verbose) { - curl_easy_setopt(ctx.curl, CURLOPT_VERBOSE, 1L); - } + /* landerctl_ctx ctx = {0}; */ - curl_easy_setopt(ctx.curl, CURLOPT_HTTPHEADER, ctx.headers); - curl_easy_setopt(ctx.curl, CURLOPT_CAINFO, ctx.cfg.ca_certs_bundle); + /* const char *home_dir = getenv("HOME"); */ + /* const char *cfg_path; */ - char curl_err_msg[CURL_ERROR_SIZE]; - curl_easy_setopt(ctx.curl, CURLOPT_ERRORBUFFER, curl_err_msg); + /* if (home_dir == NULL) { */ + /* cfg_path = cfg_file_name; */ + /* } else { */ + /* // This is a blatant memleak if a custom config file is set, but it + * really */ + /* // doesn't matter for a short-lived CLI tool */ + /* char *buf = malloc(strlen(home_dir) + strlen(cfg_file_name) + 2); */ + /* sprintf(buf, "%s/%s", home_dir, cfg_file_name); */ + /* cfg_path = buf; */ + /* } */ - int exit_code = 0; + /* opterr = 0; */ + /* int c; */ - if (curl_easy_perform(ctx.curl) == CURLE_OK) { - long response_code; - curl_easy_getinfo(ctx.curl, CURLINFO_RESPONSE_CODE, &response_code); + /* while ((c = getopt(argc, argv, "SPFsvc:")) != -1) { */ + /* switch (c) { */ + /* case 'S': */ + /* ctx.mode = landerctl_mode_short; */ + /* break; */ + /* case 'P': */ + /* ctx.mode = landerctl_mode_paste; */ + /* break; */ + /* case 'F': */ + /* ctx.mode = landerctl_mode_file; */ + /* break; */ + /* case 's': */ + /* ctx.secure = true; */ + /* break; */ + /* case 'v': */ + /* ctx.verbose = true; */ + /* break; */ + /* case 'c': */ + /* cfg_path = optarg; */ + /* break; */ + /* case '?': */ + /* printf(usage, argv[0]); */ + /* exit(2); */ + /* } */ + /* } */ - if (response_code < 200 || response_code > 299) { - fprintf(stderr, "HTTP status code %li\n", response_code); - exit_code = 3; - } else { - struct curl_header *location_header; + /* char *err_msg = NULL; */ + /* landerctl_cfg_err parse_res = landerctl_cfg_parse(&ctx.cfg, cfg_path); */ - if (curl_easy_header(ctx.curl, "Location", 0, CURLH_HEADER, -1, - &location_header) == CURLHE_OK) { - printf("%s%s\n", ctx.cfg.server_url, location_header->value); - } else { - fprintf(stderr, "Server returned a 2xx without a Location header.\n"); - exit_code = 5; - } - } - } else { - fprintf(stderr, "Libcurl encountered an error: %s\n", curl_err_msg); - exit_code = 4; - } + /* switch (parse_res) { */ + /* case landerctl_cfg_err_ok: */ + /* break; */ + /* case landerctl_cfg_err_not_found: */ + /* err_msg = "Config file not found"; */ + /* break; */ + /* case landerctl_cfg_err_invalid: */ + /* err_msg = "Invalid config file"; */ + /* break; */ + /* case landerctl_cfg_err_incomplete: */ + /* err_msg = "Incomplete config file"; */ + /* break; */ + /* } */ - curl_easy_cleanup(ctx.curl); - curl_slist_free_all(ctx.headers); + /* if (err_msg != NULL) { */ + /* fprintf(stderr, "%s\n", err_msg); */ + /* exit(1); */ + /* } */ - if (ctx.data_file != NULL) { - fclose(ctx.data_file); - } + /* if (ctx.mode == landerctl_mode_none) { */ + /* printf("No mode specified.\n\n"); */ + /* printf(usage, argv[0]); */ + /* exit(2); */ + /* } */ - return exit_code; + /* if (optind == argc || (argc - optind > 2)) { */ + /* printf(usage, argv[0]); */ + /* exit(2); */ + /* } */ + + /* ctx.arg = argv[optind]; */ + /* ctx.key = argc - optind == 2 ? argv[optind + 1] : NULL; */ + + /* curl_global_init(CURL_GLOBAL_ALL); */ + /* ctx.curl = curl_easy_init(); */ + + /* if (ctx.curl == NULL) { */ + /* exit(255); */ + /* } */ + + /* landerctl_set_common(&ctx); */ + /* landerctl_err res; */ + + /* switch (ctx.mode) { */ + /* case landerctl_mode_short: */ + /* res = landerctl_post_short(&ctx); */ + /* break; */ + /* case landerctl_mode_paste: */ + /* res = landerctl_post_paste(&ctx); */ + /* break; */ + /* case landerctl_mode_file: */ + /* res = landerctl_post_file(&ctx); */ + /* break; */ + /* default: */ + /* return 7; */ + /* } */ + + /* if (res != landerctl_err_ok) { */ + /* printf("%s\n", landerctl_err_msg(res)); */ + /* exit(6); */ + /* } */ + + /* if (ctx.verbose) { */ + /* curl_easy_setopt(ctx.curl, CURLOPT_VERBOSE, 1L); */ + /* } */ + + /* curl_easy_setopt(ctx.curl, CURLOPT_HTTPHEADER, ctx.headers); */ + /* curl_easy_setopt(ctx.curl, CURLOPT_CAINFO, ctx.cfg.ca_certs_bundle); */ + + /* char curl_err_msg[CURL_ERROR_SIZE]; */ + /* curl_easy_setopt(ctx.curl, CURLOPT_ERRORBUFFER, curl_err_msg); */ + + /* int exit_code = 0; */ + + /* if (curl_easy_perform(ctx.curl) == CURLE_OK) { */ + /* long response_code; */ + /* curl_easy_getinfo(ctx.curl, CURLINFO_RESPONSE_CODE, &response_code); */ + + /* if (response_code < 200 || response_code > 299) { */ + /* fprintf(stderr, "HTTP status code %li\n", response_code); */ + /* exit_code = 3; */ + /* } else { */ + /* struct curl_header *location_header; */ + + /* if (curl_easy_header(ctx.curl, "Location", 0, CURLH_HEADER, -1, */ + /* &location_header) == CURLHE_OK) { */ + /* printf("%s%s\n", ctx.cfg.server_url, location_header->value); */ + /* } else { */ + /* fprintf(stderr, "Server returned a 2xx without a Location + * header.\n"); */ + /* exit_code = 5; */ + /* } */ + /* } */ + /* } else { */ + /* fprintf(stderr, "Libcurl encountered an error: %s\n", curl_err_msg); */ + /* exit_code = 4; */ + /* } */ + + /* curl_easy_cleanup(ctx.curl); */ + /* curl_slist_free_all(ctx.headers); */ + + /* if (ctx.data_file != NULL) { */ + /* fclose(ctx.data_file); */ + /* } */ + + /* return exit_code; */ /* struct stat sb; */ From 8b7ce0085e13b7b9e679374aa1cd1612a0485c43 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 3 Oct 2024 09:44:00 +0200 Subject: [PATCH 2/6] feat(landerctl): re-implement paste & file functionality --- landerctl/include/landerctl.h | 2 + landerctl/src/commands.c | 112 ++++++++++++++++++++++++++++++++++ landerctl/src/main.c | 6 ++ 3 files changed, 120 insertions(+) diff --git a/landerctl/include/landerctl.h b/landerctl/include/landerctl.h index b6e0d8c..55acf66 100644 --- a/landerctl/include/landerctl.h +++ b/landerctl/include/landerctl.h @@ -75,6 +75,8 @@ landerctl_err landerctl_post_paste(landerctl_ctx *ctx); landerctl_err landerctl_post_file(landerctl_ctx *ctx); int landerctl_cmd_short(landerctl_args *args); +int landerctl_cmd_paste(landerctl_args *args); +int landerctl_cmd_file(landerctl_args *args); int landerctl_parse_args(landerctl_args *out, int argc, char **argv); #endif diff --git a/landerctl/src/commands.c b/landerctl/src/commands.c index 4ab4c8b..3b91fa4 100644 --- a/landerctl/src/commands.c +++ b/landerctl/src/commands.c @@ -1,6 +1,9 @@ +#include #include +#include #include +#include #include "landerctl.h" @@ -147,3 +150,112 @@ int landerctl_cmd_short(landerctl_args *args) { return res; } + +int landerctl_cmd_paste(landerctl_args *args) { + // TODO argument count check + int res; + landerctl_curl curl; + + if ((res = landerctl_curl_init(&curl))) { + return res; + } + + const char *key = args->args.len == 2 ? args->args.arr[1] : NULL; + if ((res = landerctl_curl_set_common(&curl, args, key))) { + return res; + } + + const char *data_path = args->args.arr[0]; + FILE *f = fopen(data_path, "rb"); + + if (f == NULL) { + fprintf(stderr, "Failed to open data file %s\n", args->args.arr[0]); + + return 1; + } + + struct stat sb; + stat(data_path, &sb); + + curl_easy_setopt(curl.curl, CURLOPT_POST, 1L); + curl_easy_setopt(curl.curl, CURLOPT_READDATA, f); + curl_easy_setopt(curl.curl, CURLOPT_POSTFIELDSIZE, sb.st_size); + + res = landerctl_curl_perform(&curl); + + if (res == 0) { + landerctl_curl_inspect(&curl, args); + } + + landerctl_curl_cleanup(&curl); + + return res; +} + +int landerctl_cmd_file(landerctl_args *args) { + // TODO argument count check + int res; + landerctl_curl curl; + + if ((res = landerctl_curl_init(&curl))) { + return res; + } + + const char *key = args->args.len == 2 ? args->args.arr[1] : NULL; + if ((res = landerctl_curl_set_common(&curl, args, key))) { + return res; + } + + const char *data_path = args->args.arr[0]; + FILE *f = fopen(data_path, "rb"); + + if (f == NULL) { + fprintf(stderr, "Failed to open data file %s\n", args->args.arr[0]); + + return 1; + } + + struct stat sb; + stat(data_path, &sb); + + curl_easy_setopt(curl.curl, CURLOPT_POST, 1L); + curl_easy_setopt(curl.curl, CURLOPT_READDATA, f); + curl_easy_setopt(curl.curl, CURLOPT_POSTFIELDSIZE_LARGE, sb.st_size); + curl_easy_setopt(curl.curl, CURLOPT_NOPROGRESS, 0L); + + magic_t cookie = magic_open(MAGIC_MIME_TYPE); + + if (magic_load(cookie, NULL) == 0) { + const char *mime_type = magic_file(cookie, data_path); + + if (mime_type != NULL) { + char content_type_header[strlen(mime_type) + 24]; + sprintf(content_type_header, "X-Lander-Content-Type: %s", mime_type); + + curl.headers = curl_slist_append(curl.headers, content_type_header); + } else { + printf("Couldn't determine mime type; skipping Content-Type header\n"); + } + } else { + printf("Couldn't load magic file; skipping Content-Type header\n"); + } + + char s[strlen(data_path) + 1]; + strcpy(s, data_path); + const char *base_name = basename(s); + + char filename_header[strlen(base_name) + 20]; + sprintf(filename_header, "X-Lander-Filename: %s", base_name); + + curl.headers = curl_slist_append(curl.headers, filename_header); + + res = landerctl_curl_perform(&curl); + + if (res == 0) { + landerctl_curl_inspect(&curl, args); + } + + landerctl_curl_cleanup(&curl); + + return res; +} diff --git a/landerctl/src/main.c b/landerctl/src/main.c index 0566648..b1dab2f 100644 --- a/landerctl/src/main.c +++ b/landerctl/src/main.c @@ -21,6 +21,12 @@ int main(int argc, char **argv) { case landerctl_mode_short: res = landerctl_cmd_short(&args); break; + case landerctl_mode_paste: + res = landerctl_cmd_paste(&args); + break; + case landerctl_mode_file: + res = landerctl_cmd_file(&args); + break; /* case landerctl_mode_paste: */ /* res = landerctl_post_paste(&ctx); */ /* break; */ From 16a7af6865c18304cb2c78fc680fcc6187eb7bd3 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 3 Oct 2024 10:00:44 +0200 Subject: [PATCH 3/6] refactor(landerctl): split some files --- landerctl/include/landerctl.h | 10 ++- landerctl/src/commands.c | 114 ----------------------------- landerctl/src/commands_common.c | 122 ++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 115 deletions(-) create mode 100644 landerctl/src/commands_common.c diff --git a/landerctl/include/landerctl.h b/landerctl/include/landerctl.h index 55acf66..fc37649 100644 --- a/landerctl/include/landerctl.h +++ b/landerctl/include/landerctl.h @@ -74,9 +74,17 @@ landerctl_err landerctl_post_short(landerctl_ctx *ctx); landerctl_err landerctl_post_paste(landerctl_ctx *ctx); landerctl_err landerctl_post_file(landerctl_ctx *ctx); +int landerctl_parse_args(landerctl_args *out, int argc, char **argv); + int landerctl_cmd_short(landerctl_args *args); int landerctl_cmd_paste(landerctl_args *args); int landerctl_cmd_file(landerctl_args *args); -int landerctl_parse_args(landerctl_args *out, int argc, char **argv); + +int landerctl_curl_init(landerctl_curl *out); +int landerctl_curl_set_common(landerctl_curl *curl, landerctl_args *args, + const char *key); +int landerctl_curl_perform(landerctl_curl *curl); +int landerctl_curl_inspect(landerctl_curl *curl, landerctl_args *args); +void landerctl_curl_cleanup(landerctl_curl *curl); #endif diff --git a/landerctl/src/commands.c b/landerctl/src/commands.c index 3b91fa4..00b1f29 100644 --- a/landerctl/src/commands.c +++ b/landerctl/src/commands.c @@ -7,120 +7,6 @@ #include "landerctl.h" -int landerctl_curl_init(landerctl_curl *out) { - curl_global_init(CURL_GLOBAL_ALL); - CURL *curl = curl_easy_init(); - - if (curl == NULL) { - fprintf(stderr, "Failed to initialize cURL client.\n"); - - return 8; - } - - out->curl = curl; - out->headers = NULL; - - return 0; -} - -int landerctl_curl_set_common(landerctl_curl *curl, landerctl_args *args, - const char *key) { - size_t url_len = strlen(args->cfg.server_url) + 4; - - if (key != NULL) { - url_len += strlen(key); - } - - char mode_char; - - switch (args->mode) { - case landerctl_mode_short: - mode_char = 's'; - break; - case landerctl_mode_paste: - mode_char = 'p'; - break; - case landerctl_mode_file: - mode_char = 'f'; - break; - // Shouldn't be able to happen - default: - return 10; - } - - char url[url_len + 1]; - - if (key == NULL) { - sprintf(url, "%s/%c%s/", args->cfg.server_url, mode_char, - args->secure ? "l" : ""); - } else { - sprintf(url, "%s/%c%s/%s", args->cfg.server_url, mode_char, - args->secure ? "l" : "", key); - } - - curl_easy_setopt(curl->curl, CURLOPT_URL, url); - - // Add API key header - char api_key_header[strlen(args->cfg.api_key) + 12]; - sprintf(api_key_header, "X-Api-Key: %s", args->cfg.api_key); - - curl->headers = curl_slist_append(curl->headers, api_key_header); - - curl_easy_setopt(curl->curl, CURLOPT_USERAGENT, - "landerctl/" LANDER_VERSION ""); - - if (args->verbose) { - curl_easy_setopt(curl->curl, CURLOPT_VERBOSE, 1L); - } - - curl_easy_setopt(curl->curl, CURLOPT_CAINFO, args->cfg.ca_certs_bundle); - curl_easy_setopt(curl->curl, CURLOPT_ERRORBUFFER, curl->err_msg); - - return 0; -} - -int landerctl_curl_perform(landerctl_curl *curl) { - curl_easy_setopt(curl->curl, CURLOPT_HTTPHEADER, curl->headers); - - int res = curl_easy_perform(curl->curl); - - if (res != CURLE_OK) { - fprintf(stderr, "Libcurl encountered an error (code %i): %s\n", res, - curl->err_msg); - } - - return res; -} - -int landerctl_curl_inspect(landerctl_curl *curl, landerctl_args *args) { - long response_code; - curl_easy_getinfo(curl->curl, CURLINFO_RESPONSE_CODE, &response_code); - - if (response_code < 200 || response_code > 299) { - fprintf(stderr, "HTTP status code %li\n", response_code); - - return 3; - } else { - struct curl_header *location_header; - - if (curl_easy_header(curl->curl, "Location", 0, CURLH_HEADER, -1, - &location_header) == CURLHE_OK) { - printf("%s%s\n", args->cfg.server_url, location_header->value); - } else { - fprintf(stderr, "Server returned a 2xx without a Location header.\n"); - - return 5; - } - } - - return 0; -} - -void landerctl_curl_cleanup(landerctl_curl *curl) { - curl_easy_cleanup(curl->curl); - curl_slist_free_all(curl->headers); -} - int landerctl_cmd_short(landerctl_args *args) { // TODO argument count check int res; diff --git a/landerctl/src/commands_common.c b/landerctl/src/commands_common.c new file mode 100644 index 0000000..e735aa7 --- /dev/null +++ b/landerctl/src/commands_common.c @@ -0,0 +1,122 @@ +#include +#include +#include + +#include +#include + +#include "landerctl.h" + +int landerctl_curl_init(landerctl_curl *out) { + curl_global_init(CURL_GLOBAL_ALL); + CURL *curl = curl_easy_init(); + + if (curl == NULL) { + fprintf(stderr, "Failed to initialize cURL client.\n"); + + return 8; + } + + out->curl = curl; + out->headers = NULL; + + return 0; +} + +int landerctl_curl_set_common(landerctl_curl *curl, landerctl_args *args, + const char *key) { + size_t url_len = strlen(args->cfg.server_url) + 4; + + if (key != NULL) { + url_len += strlen(key); + } + + char mode_char; + + switch (args->mode) { + case landerctl_mode_short: + mode_char = 's'; + break; + case landerctl_mode_paste: + mode_char = 'p'; + break; + case landerctl_mode_file: + mode_char = 'f'; + break; + // Shouldn't be able to happen + default: + return 10; + } + + char url[url_len + 1]; + + if (key == NULL) { + sprintf(url, "%s/%c%s/", args->cfg.server_url, mode_char, + args->secure ? "l" : ""); + } else { + sprintf(url, "%s/%c%s/%s", args->cfg.server_url, mode_char, + args->secure ? "l" : "", key); + } + + curl_easy_setopt(curl->curl, CURLOPT_URL, url); + + // Add API key header + char api_key_header[strlen(args->cfg.api_key) + 12]; + sprintf(api_key_header, "X-Api-Key: %s", args->cfg.api_key); + + curl->headers = curl_slist_append(curl->headers, api_key_header); + + curl_easy_setopt(curl->curl, CURLOPT_USERAGENT, + "landerctl/" LANDER_VERSION ""); + + if (args->verbose) { + curl_easy_setopt(curl->curl, CURLOPT_VERBOSE, 1L); + } + + curl_easy_setopt(curl->curl, CURLOPT_CAINFO, args->cfg.ca_certs_bundle); + curl_easy_setopt(curl->curl, CURLOPT_ERRORBUFFER, curl->err_msg); + + return 0; +} + +int landerctl_curl_perform(landerctl_curl *curl) { + curl_easy_setopt(curl->curl, CURLOPT_HTTPHEADER, curl->headers); + + int res = curl_easy_perform(curl->curl); + + if (res != CURLE_OK) { + fprintf(stderr, "Libcurl encountered an error (code %i): %s\n", res, + curl->err_msg); + } + + return res; +} + +int landerctl_curl_inspect(landerctl_curl *curl, landerctl_args *args) { + long response_code; + curl_easy_getinfo(curl->curl, CURLINFO_RESPONSE_CODE, &response_code); + + if (response_code < 200 || response_code > 299) { + fprintf(stderr, "HTTP status code %li\n", response_code); + + return 3; + } else { + struct curl_header *location_header; + + if (curl_easy_header(curl->curl, "Location", 0, CURLH_HEADER, -1, + &location_header) == CURLHE_OK) { + printf("%s%s\n", args->cfg.server_url, location_header->value); + } else { + fprintf(stderr, "Server returned a 2xx without a Location header.\n"); + + return 5; + } + } + + return 0; +} + +void landerctl_curl_cleanup(landerctl_curl *curl) { + curl_easy_cleanup(curl->curl); + curl_slist_free_all(curl->headers); +} From 40a895673c6b0dd3b29f123bfe5711fe84185144 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 3 Oct 2024 10:08:52 +0200 Subject: [PATCH 4/6] chore(landerctl): remove dead code --- landerctl/.landerrc | 2 - landerctl/include/landerctl.h | 52 +++++---- landerctl/src/main.c | 213 ---------------------------------- landerctl/src/post.c | 131 --------------------- 4 files changed, 28 insertions(+), 370 deletions(-) delete mode 100644 landerctl/.landerrc delete mode 100644 landerctl/src/post.c diff --git a/landerctl/.landerrc b/landerctl/.landerrc deleted file mode 100644 index 964d3f2..0000000 --- a/landerctl/.landerrc +++ /dev/null @@ -1,2 +0,0 @@ -api_key = test -server_url = http://localhost:18080 diff --git a/landerctl/include/landerctl.h b/landerctl/include/landerctl.h index fc37649..9ad264e 100644 --- a/landerctl/include/landerctl.h +++ b/landerctl/include/landerctl.h @@ -5,6 +5,9 @@ #include +/** + * Represents a parsed config file + */ typedef struct landerctl_cfg { const char *api_key; const char *server_url; @@ -33,23 +36,9 @@ typedef enum landerctl_mode { landerctl_mode_file, } landerctl_mode; -typedef enum landerctl_err { - landerctl_err_ok = 0, - landerctl_err_not_found -} landerctl_err; - -typedef struct landerctl_ctx { - landerctl_cfg cfg; - landerctl_mode mode; - bool secure; - bool verbose; - const char *arg; - const char *key; - CURL *curl; - struct curl_slist *headers; - FILE *data_file; -} landerctl_ctx; - +/** + * Represents parsed CLI arguments + */ typedef struct landerctl_args { bool secure; bool verbose; @@ -61,30 +50,45 @@ typedef struct landerctl_args { landerctl_mode mode; } landerctl_args; +/** + * Convenience wrapper around a CURL object + */ typedef struct landerctl_curl { CURL *curl; struct curl_slist *headers; char err_msg[CURL_ERROR_SIZE]; } landerctl_curl; -const char *landerctl_err_msg(landerctl_err err); - -void landerctl_set_common(landerctl_ctx *ctx); -landerctl_err landerctl_post_short(landerctl_ctx *ctx); -landerctl_err landerctl_post_paste(landerctl_ctx *ctx); -landerctl_err landerctl_post_file(landerctl_ctx *ctx); - int landerctl_parse_args(landerctl_args *out, int argc, char **argv); int landerctl_cmd_short(landerctl_args *args); int landerctl_cmd_paste(landerctl_args *args); int landerctl_cmd_file(landerctl_args *args); +/** + * Initialize a CURL object + */ int landerctl_curl_init(landerctl_curl *out); + +/** + * Set common configurations for the CURL shared across all commands + */ int landerctl_curl_set_common(landerctl_curl *curl, landerctl_args *args, const char *key); + +/** + * Execute the HTTP request configured in the CURL object + */ int landerctl_curl_perform(landerctl_curl *curl); + +/** + * Inspect the response code and Location header of a successful HTTP request + */ int landerctl_curl_inspect(landerctl_curl *curl, landerctl_args *args); + +/** + * Deallocate the CURL object + */ void landerctl_curl_cleanup(landerctl_curl *curl); #endif diff --git a/landerctl/src/main.c b/landerctl/src/main.c index b1dab2f..7609fa9 100644 --- a/landerctl/src/main.c +++ b/landerctl/src/main.c @@ -1,6 +1,4 @@ #include -#include -#include #include #include @@ -27,221 +25,10 @@ int main(int argc, char **argv) { case landerctl_mode_file: res = landerctl_cmd_file(&args); break; - /* case landerctl_mode_paste: */ - /* res = landerctl_post_paste(&ctx); */ - /* break; */ - /* case landerctl_mode_file: */ - /* res = landerctl_post_file(&ctx); */ - /* break; */ default: res = 7; break; } return res; - - /* landerctl_ctx ctx = {0}; */ - - /* const char *home_dir = getenv("HOME"); */ - /* const char *cfg_path; */ - - /* if (home_dir == NULL) { */ - /* cfg_path = cfg_file_name; */ - /* } else { */ - /* // This is a blatant memleak if a custom config file is set, but it - * really */ - /* // doesn't matter for a short-lived CLI tool */ - /* char *buf = malloc(strlen(home_dir) + strlen(cfg_file_name) + 2); */ - /* sprintf(buf, "%s/%s", home_dir, cfg_file_name); */ - /* cfg_path = buf; */ - /* } */ - - /* opterr = 0; */ - /* int c; */ - - /* while ((c = getopt(argc, argv, "SPFsvc:")) != -1) { */ - /* switch (c) { */ - /* case 'S': */ - /* ctx.mode = landerctl_mode_short; */ - /* break; */ - /* case 'P': */ - /* ctx.mode = landerctl_mode_paste; */ - /* break; */ - /* case 'F': */ - /* ctx.mode = landerctl_mode_file; */ - /* break; */ - /* case 's': */ - /* ctx.secure = true; */ - /* break; */ - /* case 'v': */ - /* ctx.verbose = true; */ - /* break; */ - /* case 'c': */ - /* cfg_path = optarg; */ - /* break; */ - /* case '?': */ - /* printf(usage, argv[0]); */ - /* exit(2); */ - /* } */ - /* } */ - - /* char *err_msg = NULL; */ - /* landerctl_cfg_err parse_res = landerctl_cfg_parse(&ctx.cfg, cfg_path); */ - - /* switch (parse_res) { */ - /* case landerctl_cfg_err_ok: */ - /* break; */ - /* case landerctl_cfg_err_not_found: */ - /* err_msg = "Config file not found"; */ - /* break; */ - /* case landerctl_cfg_err_invalid: */ - /* err_msg = "Invalid config file"; */ - /* break; */ - /* case landerctl_cfg_err_incomplete: */ - /* err_msg = "Incomplete config file"; */ - /* break; */ - /* } */ - - /* if (err_msg != NULL) { */ - /* fprintf(stderr, "%s\n", err_msg); */ - /* exit(1); */ - /* } */ - - /* if (ctx.mode == landerctl_mode_none) { */ - /* printf("No mode specified.\n\n"); */ - /* printf(usage, argv[0]); */ - /* exit(2); */ - /* } */ - - /* if (optind == argc || (argc - optind > 2)) { */ - /* printf(usage, argv[0]); */ - /* exit(2); */ - /* } */ - - /* ctx.arg = argv[optind]; */ - /* ctx.key = argc - optind == 2 ? argv[optind + 1] : NULL; */ - - /* curl_global_init(CURL_GLOBAL_ALL); */ - /* ctx.curl = curl_easy_init(); */ - - /* if (ctx.curl == NULL) { */ - /* exit(255); */ - /* } */ - - /* landerctl_set_common(&ctx); */ - /* landerctl_err res; */ - - /* switch (ctx.mode) { */ - /* case landerctl_mode_short: */ - /* res = landerctl_post_short(&ctx); */ - /* break; */ - /* case landerctl_mode_paste: */ - /* res = landerctl_post_paste(&ctx); */ - /* break; */ - /* case landerctl_mode_file: */ - /* res = landerctl_post_file(&ctx); */ - /* break; */ - /* default: */ - /* return 7; */ - /* } */ - - /* if (res != landerctl_err_ok) { */ - /* printf("%s\n", landerctl_err_msg(res)); */ - /* exit(6); */ - /* } */ - - /* if (ctx.verbose) { */ - /* curl_easy_setopt(ctx.curl, CURLOPT_VERBOSE, 1L); */ - /* } */ - - /* curl_easy_setopt(ctx.curl, CURLOPT_HTTPHEADER, ctx.headers); */ - /* curl_easy_setopt(ctx.curl, CURLOPT_CAINFO, ctx.cfg.ca_certs_bundle); */ - - /* char curl_err_msg[CURL_ERROR_SIZE]; */ - /* curl_easy_setopt(ctx.curl, CURLOPT_ERRORBUFFER, curl_err_msg); */ - - /* int exit_code = 0; */ - - /* if (curl_easy_perform(ctx.curl) == CURLE_OK) { */ - /* long response_code; */ - /* curl_easy_getinfo(ctx.curl, CURLINFO_RESPONSE_CODE, &response_code); */ - - /* if (response_code < 200 || response_code > 299) { */ - /* fprintf(stderr, "HTTP status code %li\n", response_code); */ - /* exit_code = 3; */ - /* } else { */ - /* struct curl_header *location_header; */ - - /* if (curl_easy_header(ctx.curl, "Location", 0, CURLH_HEADER, -1, */ - /* &location_header) == CURLHE_OK) { */ - /* printf("%s%s\n", ctx.cfg.server_url, location_header->value); */ - /* } else { */ - /* fprintf(stderr, "Server returned a 2xx without a Location - * header.\n"); */ - /* exit_code = 5; */ - /* } */ - /* } */ - /* } else { */ - /* fprintf(stderr, "Libcurl encountered an error: %s\n", curl_err_msg); */ - /* exit_code = 4; */ - /* } */ - - /* curl_easy_cleanup(ctx.curl); */ - /* curl_slist_free_all(ctx.headers); */ - - /* if (ctx.data_file != NULL) { */ - /* fclose(ctx.data_file); */ - /* } */ - - /* return exit_code; */ - - /* struct stat sb; */ - - /* stat(argv[1], &sb); */ - - /* printf("file size: %lu\n", sb.st_size); */ - - /* FILE *f = fopen(argv[1], "rb"); */ - - /* if (f == NULL) { */ - /* printf("Couldn't open file.\n"); */ - /* exit(1); */ - /* } */ - - /* CURL *curl = curl_easy_init(); */ - - /* if (curl == NULL) { */ - /* exit(1); */ - /* } */ - - /* curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:18080/f/"); */ - /* curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); */ - /* curl_easy_setopt(curl, CURLOPT_READDATA, f); */ - - /* curl_off_t file_size = sb.st_size; */ - /* /1* curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, file_size); *1/ */ - /* curl_easy_setopt(curl, CURLOPT_POST, 1L); */ - /* curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, file_size); */ - - /* magic_t cookie = magic_open(MAGIC_MIME_TYPE); */ - /* magic_load(cookie, NULL); */ - /* const char *mime_type = magic_file(cookie, argv[1]); */ - - /* char content_type_header[strlen(mime_type) + 24]; */ - /* sprintf(content_type_header, "X-Lander-Content-Type: %s", mime_type); */ - - /* char content_length_header[32]; */ - /* sprintf(content_length_header, "Content-Length: %lu", sb.st_size); */ - - /* struct curl_slist *list = NULL; */ - /* list = curl_slist_append(list, content_type_header); */ - /* list = curl_slist_append(list, content_length_header); */ - /* list = curl_slist_append(list, "X-Api-Key: test"); */ - - /* curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); */ - - /* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); */ - /* curl_easy_perform(curl); */ - - /* curl_slist_free_all(list); */ } diff --git a/landerctl/src/post.c b/landerctl/src/post.c deleted file mode 100644 index 16ebac4..0000000 --- a/landerctl/src/post.c +++ /dev/null @@ -1,131 +0,0 @@ -#include -#include -#include - -#include -#include - -#include "landerctl.h" - -const char *landerctl_err_msg(landerctl_err err) { - switch (err) { - case landerctl_err_not_found: - return "File not found"; - default: - return ""; - } -} - -void landerctl_set_common(landerctl_ctx *ctx) { - size_t url_len = strlen(ctx->cfg.server_url) + 4; - - if (ctx->key != NULL) { - url_len += strlen(ctx->key); - } - - char mode_char; - - switch (ctx->mode) { - case landerctl_mode_short: - mode_char = 's'; - break; - case landerctl_mode_paste: - mode_char = 'p'; - break; - case landerctl_mode_file: - mode_char = 'f'; - break; - // Shouldn't be able to happen - default: - return; - } - - char url[url_len + 1]; - - if (ctx->key == NULL) { - sprintf(url, "%s/%c%s/", ctx->cfg.server_url, mode_char, - ctx->secure ? "l" : ""); - } else { - sprintf(url, "%s/%c%s/%s", ctx->cfg.server_url, mode_char, - ctx->secure ? "l" : "", ctx->key); - } - - curl_easy_setopt(ctx->curl, CURLOPT_URL, url); - - // Add API key header - char api_key_header[strlen(ctx->cfg.api_key) + 12]; - sprintf(api_key_header, "X-Api-Key: %s", ctx->cfg.api_key); - - ctx->headers = curl_slist_append(NULL, api_key_header); - - curl_easy_setopt(ctx->curl, CURLOPT_USERAGENT, - "landerctl/" LANDER_VERSION ""); -} - -landerctl_err landerctl_post_short(landerctl_ctx *ctx) { - curl_easy_setopt(ctx->curl, CURLOPT_POSTFIELDSIZE, strlen(ctx->arg)); - curl_easy_setopt(ctx->curl, CURLOPT_POSTFIELDS, ctx->arg); - - return landerctl_err_ok; -} - -landerctl_err landerctl_post_paste(landerctl_ctx *ctx) { - ctx->data_file = fopen(ctx->arg, "rb"); - - if (ctx->data_file == NULL) { - return landerctl_err_not_found; - } - - struct stat sb; - stat(ctx->arg, &sb); - - curl_easy_setopt(ctx->curl, CURLOPT_POST, 1L); - curl_easy_setopt(ctx->curl, CURLOPT_READDATA, ctx->data_file); - curl_easy_setopt(ctx->curl, CURLOPT_POSTFIELDSIZE, sb.st_size); - - return landerctl_err_ok; -} - -landerctl_err landerctl_post_file(landerctl_ctx *ctx) { - ctx->data_file = fopen(ctx->arg, "rb"); - - if (ctx->data_file == NULL) { - return landerctl_err_not_found; - } - - struct stat sb; - stat(ctx->arg, &sb); - - curl_easy_setopt(ctx->curl, CURLOPT_POST, 1L); - curl_easy_setopt(ctx->curl, CURLOPT_READDATA, ctx->data_file); - curl_easy_setopt(ctx->curl, CURLOPT_POSTFIELDSIZE_LARGE, sb.st_size); - curl_easy_setopt(ctx->curl, CURLOPT_NOPROGRESS, 0L); - - magic_t cookie = magic_open(MAGIC_MIME_TYPE); - - if (magic_load(cookie, NULL) == 0) { - const char *mime_type = magic_file(cookie, ctx->arg); - - if (mime_type != NULL) { - char content_type_header[strlen(mime_type) + 24]; - sprintf(content_type_header, "X-Lander-Content-Type: %s", mime_type); - - ctx->headers = curl_slist_append(ctx->headers, content_type_header); - } else { - printf("Couldn't determine mime type; skipping Content-Type header\n"); - } - } else { - printf("Couldn't load magic file; skipping Content-Type header\n"); - } - - char s[strlen(ctx->arg) + 1]; - strcpy(s, ctx->arg); - const char *base_name = basename(s); - - char filename_header[strlen(base_name) + 20]; - sprintf(filename_header, "X-Lander-Filename: %s", base_name); - - ctx->headers = curl_slist_append(ctx->headers, filename_header); - - return landerctl_err_ok; -} From b833d0ed745ddec8bd421e64d5762229a62ad689 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 3 Oct 2024 10:12:57 +0200 Subject: [PATCH 5/6] fix(lander): use correct secure route for placeholders --- src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index a6727a6..980e9f7 100644 --- a/src/main.c +++ b/src/main.c @@ -100,7 +100,7 @@ lnm_http_loop *loop_init(lander_gctx *gctx, const char *api_key) { lnm_http_route_step_append(route, lander_post_placeholder, false); lnm_http_route_step_append(route, lander_commit_entry, true); - lnm_http_router_add(&route, router, lnm_http_method_post, "/hs/"); + lnm_http_router_add(&route, router, lnm_http_method_post, "/hl/"); lnm_http_route_step_append(route, lnm_http_loop_step_auth, false); lnm_http_route_step_append(route, lander_post_placeholder_secure, false); lnm_http_route_step_append(route, lander_commit_entry, true); From bdbc750f7fec6f8680489b2342bc78d40e904503 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 3 Oct 2024 10:18:57 +0200 Subject: [PATCH 6/6] feat(landerctl): implement placeholder requests --- landerctl/include/landerctl.h | 2 ++ landerctl/src/args.c | 5 ++++- landerctl/src/commands.c | 27 +++++++++++++++++++++++++++ landerctl/src/commands_common.c | 3 +++ landerctl/src/main.c | 3 +++ 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/landerctl/include/landerctl.h b/landerctl/include/landerctl.h index 9ad264e..872a11e 100644 --- a/landerctl/include/landerctl.h +++ b/landerctl/include/landerctl.h @@ -34,6 +34,7 @@ typedef enum landerctl_mode { landerctl_mode_short, landerctl_mode_paste, landerctl_mode_file, + landerctl_mode_placeholder, } landerctl_mode; /** @@ -64,6 +65,7 @@ int landerctl_parse_args(landerctl_args *out, int argc, char **argv); int landerctl_cmd_short(landerctl_args *args); int landerctl_cmd_paste(landerctl_args *args); int landerctl_cmd_file(landerctl_args *args); +int landerctl_cmd_placeholder(landerctl_args *args); /** * Initialize a CURL object diff --git a/landerctl/src/args.c b/landerctl/src/args.c index 9538a27..6043247 100644 --- a/landerctl/src/args.c +++ b/landerctl/src/args.c @@ -55,7 +55,7 @@ int landerctl_parse_args(landerctl_args *args, int argc, char **argv) { const char *cfg_path = NULL; - while ((c = getopt(argc, argv, "SPFsvc:")) != -1) { + while ((c = getopt(argc, argv, "SPFHsvc:")) != -1) { switch (c) { case 'S': args->mode = landerctl_mode_short; @@ -66,6 +66,9 @@ int landerctl_parse_args(landerctl_args *args, int argc, char **argv) { case 'F': args->mode = landerctl_mode_file; break; + case 'H': + args->mode = landerctl_mode_placeholder; + break; case 's': args->secure = true; break; diff --git a/landerctl/src/commands.c b/landerctl/src/commands.c index 00b1f29..9ec3ebc 100644 --- a/landerctl/src/commands.c +++ b/landerctl/src/commands.c @@ -145,3 +145,30 @@ int landerctl_cmd_file(landerctl_args *args) { return res; } + +int landerctl_cmd_placeholder(landerctl_args *args) { + // TODO argument count check + int res; + landerctl_curl curl; + + if ((res = landerctl_curl_init(&curl))) { + return res; + } + + const char *key = args->args.arr[0]; + if ((res = landerctl_curl_set_common(&curl, args, key))) { + return res; + } + + curl_easy_setopt(curl.curl, CURLOPT_POST, 1L); + + res = landerctl_curl_perform(&curl); + + if (res == 0) { + landerctl_curl_inspect(&curl, args); + } + + landerctl_curl_cleanup(&curl); + + return res; +} diff --git a/landerctl/src/commands_common.c b/landerctl/src/commands_common.c index e735aa7..8cf4e5e 100644 --- a/landerctl/src/commands_common.c +++ b/landerctl/src/commands_common.c @@ -43,6 +43,9 @@ int landerctl_curl_set_common(landerctl_curl *curl, landerctl_args *args, case landerctl_mode_file: mode_char = 'f'; break; + case landerctl_mode_placeholder: + mode_char = 'h'; + break; // Shouldn't be able to happen default: return 10; diff --git a/landerctl/src/main.c b/landerctl/src/main.c index 7609fa9..db8155a 100644 --- a/landerctl/src/main.c +++ b/landerctl/src/main.c @@ -25,6 +25,9 @@ int main(int argc, char **argv) { case landerctl_mode_file: res = landerctl_cmd_file(&args); break; + case landerctl_mode_placeholder: + res = landerctl_cmd_placeholder(&args); + break; default: res = 7; break;