-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathfile-updater.c
145 lines (117 loc) · 3.58 KB
/
file-updater.c
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <curl/curl.h>
#include <util/threading.h>
#include <util/platform.h>
#include <util/darray.h>
#include <util/dstr.h>
#include <obs-data.h>
#include "file-updater.h"
#define warn(msg, ...) blog(LOG_WARNING, "%s" msg, info->log_prefix, ##__VA_ARGS__)
#define info(msg, ...) blog(LOG_WARNING, "%s" msg, info->log_prefix, ##__VA_ARGS__)
struct update_info {
char error[CURL_ERROR_SIZE];
struct curl_slist *header;
DARRAY(uint8_t) file_data;
char *user_agent;
CURL *curl;
char *url;
confirm_file_callback_t callback;
void *param;
pthread_t thread;
bool thread_created;
char *log_prefix;
};
void update_info_destroy(struct update_info *info)
{
if (!info)
return;
if (info->thread_created)
pthread_join(info->thread, NULL);
da_free(info->file_data);
bfree(info->log_prefix);
bfree(info->user_agent);
bfree(info->url);
if (info->header)
curl_slist_free_all(info->header);
if (info->curl)
curl_easy_cleanup(info->curl);
bfree(info);
}
static size_t http_write(void *ptr, size_t size, size_t nmemb, void *uinfo)
{
size_t total = size * nmemb;
struct update_info *info = (struct update_info *)uinfo;
if (total)
da_push_back_array(info->file_data, ptr, total);
return total;
}
static bool do_http_request(struct update_info *info, const char *url, long *response_code)
{
CURLcode code;
uint8_t null_terminator = 0;
da_resize(info->file_data, 0);
curl_easy_setopt(info->curl, CURLOPT_URL, url);
curl_easy_setopt(info->curl, CURLOPT_HTTPHEADER, info->header);
curl_easy_setopt(info->curl, CURLOPT_ERRORBUFFER, info->error);
curl_easy_setopt(info->curl, CURLOPT_WRITEFUNCTION, http_write);
curl_easy_setopt(info->curl, CURLOPT_WRITEDATA, info);
curl_easy_setopt(info->curl, CURLOPT_FAILONERROR, true);
curl_easy_setopt(info->curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(info->curl, CURLOPT_ACCEPT_ENCODING, "");
curl_easy_setopt(info->curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_REVOKE_BEST_EFFORT);
code = curl_easy_perform(info->curl);
if (code != CURLE_OK) {
warn("Remote update of URL \"%s\" failed: %s", url, info->error);
return false;
}
if (curl_easy_getinfo(info->curl, CURLINFO_RESPONSE_CODE, response_code) != CURLE_OK)
return false;
if (*response_code >= 400) {
warn("Remote update of URL \"%s\" failed: HTTP/%ld", url, *response_code);
return false;
}
da_push_back(info->file_data, &null_terminator);
return true;
}
struct file_update_data {
const char *name;
int version;
bool newer;
bool found;
};
static void *single_file_thread(void *data)
{
struct update_info *info = data;
struct file_download_data download_data;
long response_code;
info->curl = curl_easy_init();
if (!info->curl) {
warn("Could not initialize Curl");
return NULL;
}
if (!do_http_request(info, info->url, &response_code))
return NULL;
if (!info->file_data.array || !info->file_data.array[0])
return NULL;
download_data.name = info->url;
download_data.version = 0;
download_data.buffer.da = info->file_data.da;
info->callback(info->param, &download_data);
info->file_data.da = download_data.buffer.da;
return NULL;
}
update_info_t *update_info_create_single(const char *log_prefix, const char *user_agent, const char *file_url,
confirm_file_callback_t confirm_callback, void *param)
{
struct update_info *info;
if (!log_prefix)
log_prefix = "";
info = bzalloc(sizeof(*info));
info->log_prefix = bstrdup(log_prefix);
info->user_agent = bstrdup(user_agent);
info->url = bstrdup(file_url);
info->callback = confirm_callback;
info->param = param;
if (pthread_create(&info->thread, NULL, single_file_thread, info) == 0)
info->thread_created = true;
return info;
}