175 lines
3.9 KiB
C
175 lines
3.9 KiB
C
#include <libgen.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
|
|
#include <curl/curl.h>
|
|
#include <magic.h>
|
|
|
|
#include "landerctl.h"
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|