Allow post requests

This commit is contained in:
Christoph Haas 2018-04-07 12:49:12 +02:00
parent e5467333aa
commit 86c489f573
3 changed files with 71 additions and 27 deletions

View File

@ -29,6 +29,7 @@ interval=60
[custlog] [custlog]
;id=example_device_id ;id=example_device_id
;url=https://api.mygclog.com/log ;url=https://api.mygclog.com/log
;type=GET
;param_id=id ;param_id=id
;param_cpm=cpm ;param_cpm=cpm
;param_temp=temp ;param_temp=temp

View File

@ -37,6 +37,7 @@ typedef struct
char* safecast_device; char* safecast_device;
char* netc_id; char* netc_id;
char* custlog_url; char* custlog_url;
char* custlog_type;
char* custlog_id; char* custlog_id;
char* custlog_param_id; char* custlog_param_id;
char* custlog_param_cpm; char* custlog_param_cpm;

View File

@ -30,6 +30,8 @@ static int confighandler(void* config, const char* section, const char* name, co
pconfig->dev_interval = atoi(value); pconfig->dev_interval = atoi(value);
} else if (MATCH("custlog", "url")) { } else if (MATCH("custlog", "url")) {
pconfig->custlog_url = strdup(value); pconfig->custlog_url = strdup(value);
} else if (MATCH("custlog", "type")) {
pconfig->custlog_type = strdup(value);
} else if (MATCH("custlog", "id")) { } else if (MATCH("custlog", "id")) {
pconfig->custlog_id = strdup(value); pconfig->custlog_id = strdup(value);
} else if (MATCH("custlog", "param_id")) { } else if (MATCH("custlog", "param_id")) {
@ -70,6 +72,7 @@ void init_configuration(configuration* config) {
config->dev_longitude = 0.0; config->dev_longitude = 0.0;
config->dev_interval = 60; config->dev_interval = 60;
config->custlog_url = ""; config->custlog_url = "";
config->custlog_type = "GET";
config->custlog_id = ""; config->custlog_id = "";
config->custlog_param_id = "id"; config->custlog_param_id = "id";
config->custlog_param_cpm = "cpm"; config->custlog_param_cpm = "cpm";
@ -98,42 +101,76 @@ bool send_custlog(const configuration config, int cpm, float temperature, const
CURL *curl; CURL *curl;
CURLcode res; CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init(); curl = curl_easy_init();
if(curl) { if(curl) {
char *url_buffer; char *url_buffer = NULL;
size_t url_size; size_t url_size;
// first get size of final url char *post_buffer = NULL;
url_size = snprintf(NULL, 0, "%s?%s=%s&%s=%d&%s=%f&%s=%s&%s=%ld&%s=%f&%s=%f&%s=%s", size_t post_size;
config.custlog_url,
config.custlog_param_id, config.custlog_id,
config.custlog_param_cpm, cpm,
config.custlog_param_temp, temperature,
config.custlog_param_version, version,
config.custlog_param_time, mktime(tm),
config.custlog_param_lng, config.dev_longitude,
config.custlog_param_lat, config.dev_latitude,
config.custlog_param_loc, config.dev_location);
// now allocate buffer and build url if(strncmp(config.custlog_type, "GET", 3) == 0) {
url_buffer = (char *)malloc(url_size + 1); // first get size of final url
snprintf(url_buffer, url_size+1,"%s?%s=%s&%s=%d&%s=%f&%s=%s&%s=%ld&%s=%f&%s=%f&%s=%s", url_size = snprintf(NULL, 0, "%s?%s=%s&%s=%d&%s=%f&%s=%s&%s=%ld&%s=%f&%s=%f&%s=%s",
config.custlog_url, config.custlog_url,
config.custlog_param_id, config.custlog_id, config.custlog_param_id, config.custlog_id,
config.custlog_param_cpm, cpm, config.custlog_param_cpm, cpm,
config.custlog_param_temp, temperature, config.custlog_param_temp, temperature,
config.custlog_param_version, version, config.custlog_param_version, version,
config.custlog_param_time, mktime(tm), config.custlog_param_time, mktime(tm),
config.custlog_param_lng, config.dev_longitude, config.custlog_param_lng, config.dev_longitude,
config.custlog_param_lat, config.dev_latitude, config.custlog_param_lat, config.dev_latitude,
config.custlog_param_loc, config.dev_location); config.custlog_param_loc, config.dev_location);
printf("final url: %s\n", url_buffer); // now allocate buffer and build url
url_buffer = (char *)malloc(url_size + 1);
snprintf(url_buffer, url_size+1,"%s?%s=%s&%s=%d&%s=%f&%s=%s&%s=%ld&%s=%f&%s=%f&%s=%s",
config.custlog_url,
config.custlog_param_id, config.custlog_id,
config.custlog_param_cpm, cpm,
config.custlog_param_temp, temperature,
config.custlog_param_version, version,
config.custlog_param_time, mktime(tm),
config.custlog_param_lng, config.dev_longitude,
config.custlog_param_lat, config.dev_latitude,
config.custlog_param_loc, config.dev_location);
if(config.debug) {
printf("Final url: %s\n", url_buffer);
}
} else {
url_buffer = strdup(config.custlog_url);
}
curl_easy_setopt(curl, CURLOPT_URL, url_buffer); curl_easy_setopt(curl, CURLOPT_URL, url_buffer);
if(strncmp(config.custlog_type, "POST", 4) == 0) {
// first get size of final url
post_size = snprintf(NULL, 0, "%s=%s&%s=%d&%s=%f&%s=%s&%s=%ld&%s=%f&%s=%f&%s=%s",
config.custlog_param_id, config.custlog_id,
config.custlog_param_cpm, cpm,
config.custlog_param_temp, temperature,
config.custlog_param_version, version,
config.custlog_param_time, mktime(tm),
config.custlog_param_lng, config.dev_longitude,
config.custlog_param_lat, config.dev_latitude,
config.custlog_param_loc, config.dev_location);
// now allocate buffer and build url
post_buffer = (char *)malloc(post_size + 1);
snprintf(post_buffer, post_size+1,"%s=%s&%s=%d&%s=%f&%s=%s&%s=%ld&%s=%f&%s=%f&%s=%s",
config.custlog_param_id, config.custlog_id,
config.custlog_param_cpm, cpm,
config.custlog_param_temp, temperature,
config.custlog_param_version, version,
config.custlog_param_time, mktime(tm),
config.custlog_param_lng, config.dev_longitude,
config.custlog_param_lat, config.dev_latitude,
config.custlog_param_loc, config.dev_location);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_buffer);
}
#ifdef SKIP_PEER_VERIFICATION #ifdef SKIP_PEER_VERIFICATION
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif #endif
@ -149,6 +186,8 @@ bool send_custlog(const configuration config, int cpm, float temperature, const
printf("curl_easy_perform() ok!"); printf("curl_easy_perform() ok!");
free(url_buffer); free(url_buffer);
if(post_buffer != NULL)
free(post_buffer);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
return true; return true;
@ -196,6 +235,9 @@ int main(int argc, char *argv[]) {
signal(SIGQUIT, signal_handler); signal(SIGQUIT, signal_handler);
signal(SIGHUP, signal_handler); signal(SIGHUP, signal_handler);
// setup curl
curl_global_init(CURL_GLOBAL_DEFAULT);
// parse config file // parse config file
init_configuration(&config); init_configuration(&config);
while ((opt = getopt(argc, argv, "c:d")) != -1) { while ((opt = getopt(argc, argv, "c:d")) != -1) {