aboutsummaryrefslogtreecommitdiffstats
path: root/net/api.c
blob: 9da0e8ef1e92f00880bc49fc1c828255c839d21f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <curl/curl.h>

extern char *token;

int http_get(char *url)
{
    int pipefd[2];
    if(pipe(pipefd) < 0)
        return -errno;

    FILE *write_end = fdopen(pipefd[1], "w");

    int ret = pipefd[0];

    CURL *job = curl_easy_init();
    if(job == 0) {
        close(pipefd[0]);
        ret = -1;
        goto close_writepipe;
    }

    curl_easy_setopt(job, CURLOPT_URL, url);
    curl_easy_setopt(job, CURLOPT_WRITEDATA, write_end);
    curl_easy_setopt(job, CURLOPT_HTTPHEADER,
            curl_slist_append(NULL, token));
    CURLcode res = curl_easy_perform(job);

    if(res > 0) {
        ret = -res;
        close(pipefd[0]);
        goto cleanup;
    }

cleanup:
    curl_easy_cleanup(job);
close_writepipe:
    fclose(write_end);
    return ret;
}