lander/landerctl/src/post.c

61 lines
1.5 KiB
C

#include <curl/curl.h>
#include <string.h>
#include "landerctl.h"
struct curl_slist *landerctl_set_common(const landerctl_cfg *cfg, CURL *curl,
landerctl_mode mode, bool secure,
const char *key) {
size_t url_len = strlen(cfg->server_url) + 4;
if (key != NULL) {
url_len += strlen(key);
}
char mode_char;
switch (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 NULL;
}
char url[url_len + 1];
if (key == NULL) {
sprintf(url, "%s/%c%s/", cfg->server_url, mode_char, secure ? "l" : "");
} else {
sprintf(url, "%s/%c%s/%s", cfg->server_url, mode_char, secure ? "l" : "",
key);
}
curl_easy_setopt(curl, CURLOPT_URL, url);
// Add API key header
char api_key_header[strlen(cfg->api_key) + 12];
sprintf(api_key_header, "X-Api-Key: %s", cfg->api_key);
struct curl_slist *list = NULL;
list = curl_slist_append(list, api_key_header);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "landerctl/" LANDER_VERSION "");
return list;
}
void landerctl_post_short(CURL *curl, const char *url) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(url));
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, url);
}