aboutsummaryrefslogtreecommitdiff
path: root/src/lib/core/download.c
blob: c3f8dca43a248a3147d015720ea5e9a6b0143c68 (plain) (blame)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// Created by jhunk on 10/5/23.
//

#include "download.h"
#include "core.h"

size_t download_writer(void *fp, size_t size, size_t nmemb, void *stream) {
    size_t bytes = fwrite(fp, size, nmemb, (FILE *) stream);
    return bytes;
}

long download(char *url, const char *filename, char **errmsg) {
    long http_code = -1;
    char user_agent[20];
    sprintf(user_agent, "stasis/%s", VERSION);
    long timeout = 30L;
    char *timeout_str = getenv("STASIS_DOWNLOAD_TIMEOUT");

    curl_global_init(CURL_GLOBAL_ALL);
    CURL *c = curl_easy_init();
    curl_easy_setopt(c, CURLOPT_URL, url);
    curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, download_writer);
    FILE *fp = fopen(filename, "wb");
    if (!fp) {
        return -1;
    }

    curl_easy_setopt(c, CURLOPT_VERBOSE, 0L);
    curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(c, CURLOPT_USERAGENT, user_agent);
    curl_easy_setopt(c, CURLOPT_NOPROGRESS, 0L);
    curl_easy_setopt(c, CURLOPT_WRITEDATA, fp);

    if (timeout_str) {
        timeout = strtol(timeout_str, NULL, 10);
    }
    curl_easy_setopt(c, CURLOPT_CONNECTTIMEOUT, timeout);

    SYSDEBUG("curl_easy_perform(): \n\turl=%s\n\tfilename=%s\n\tuser agent=%s\n\ttimeout=%zu", url, filename, user_agent, timeout);
    CURLcode curl_code = curl_easy_perform(c);
    SYSDEBUG("curl status code: %d", curl_code);
    if (curl_code != CURLE_OK) {
        if (errmsg) {
            strcpy(*errmsg, curl_easy_strerror(curl_code));
        } else {
            fprintf(stderr, "\nCURL ERROR: %s\n", curl_easy_strerror(curl_code));
        }
        goto failed;
    }
    curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &http_code);
    failed:
    SYSDEBUG("HTTP code: %li", http_code);
    fclose(fp);
    curl_easy_cleanup(c);
    curl_global_cleanup();
    return http_code;
}