aboutsummaryrefslogtreecommitdiff
path: root/src/download.c
blob: 3b3692c27dec5e3a5b7b7578518d350d19dd225d (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "download.h"
#include "buffer.h"
#include "program.h"
#include "fallback.h"
#include <stdio.h>

int download_to_buffer(const char *url, Buffer *buffer, fallback *fall) {
    char new_url[2048];
    snprintf(new_url, sizeof(new_url), "%s", url);

    fallback_item *fall_item = NULL;
    if(fall) {
        fall_item = fallback_get_from_url(fall, url);
        if(fall_item && fall_item->source_to_use)
            fallback_item_replace_url_with_fallback_url(fall_item, url, new_url, sizeof(new_url));
    }

    const char *args[] = {
        "curl", "-s", "-L", "-f",
        "-H", "user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36",
        "-H", "Accept-Language: en-US,en;q=0.5",
        "--compressed",
        "--",
        new_url,
        NULL
    };

    int result = program_exec(args, program_buffer_write_callback, buffer);
    if(result == 0) {
        buffer_append(buffer, "\0", 1);
        return 0;
    }

    if(!fall || !fall_item || fall_item->source_to_use)
        return result;

    const char **fallbacks_it = buffer_begin(&fall_item->fallbacks);
    const char **fallbacks_end = buffer_end(&fall_item->fallbacks);
    for(; fallbacks_it != fallbacks_end; ++fallbacks_it) {
        buffer_clear(buffer);
        fallback_replace_url_with_fallback_url(url, *fallbacks_it, new_url, sizeof(new_url));
        if(download_to_buffer(new_url, buffer, NULL) == 0) {
            fprintf(stderr, "Download failed for url %s, replacing domain with %s for this sync session\n", url, *fallbacks_it);
            fall_item->source_to_use = *fallbacks_it;
            return 0;
        }
    }

    return -1;
}

int is_header_response_ok(const char *url, fallback *fall) {
    char new_url[2048];
    snprintf(new_url, sizeof(new_url), "%s", url);

    fallback_item *fall_item = NULL;
    if(fall) {
        fall_item = fallback_get_from_url(fall, url);
        if(fall_item && fall_item->source_to_use)
            fallback_item_replace_url_with_fallback_url(fall_item, url, new_url, sizeof(new_url));
    }

    const char *args[] = {
        "curl", "-s", "-L", "-f", "-I",
        "-H", "user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36",
        "-H", "Accept-Language: en-US,en;q=0.5",
        "--",
        new_url,
        NULL
    };

    int result = program_exec(args, NULL, NULL);
    if(result == 0)
        return 0;

    if(!fall || !fall_item || fall_item->source_to_use)
        return result;

    const char **fallbacks_it = buffer_begin(&fall_item->fallbacks);
    const char **fallbacks_end = buffer_end(&fall_item->fallbacks);
    for(; fallbacks_it != fallbacks_end; ++fallbacks_it) {
        fallback_replace_url_with_fallback_url(url, *fallbacks_it, new_url, sizeof(new_url));
        if(is_header_response_ok(new_url, NULL) == 0) {
            fprintf(stderr, "Download failed for url %s, replacing domain with %s for this sync session\n", url, *fallbacks_it);
            fall_item->source_to_use = *fallbacks_it;
            return 0;
        }
    }

    return -1;
}