132 lines
3.3 KiB
C
132 lines
3.3 KiB
C
#include <libgen.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
|
|
#include <curl/curl.h>
|
|
#include <magic.h>
|
|
|
|
#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;
|
|
}
|