1 /*
2 * http_download.c
3 * vim: expandtab:ts=4:sts=4:sw=4
4 *
5 * Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
6 * Copyright (C) 2020 William Wennerström <william@wstrm.dev>
7 *
8 * This file is part of Profanity.
9 *
10 * Profanity is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Profanity is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Profanity. If not, see <https://www.gnu.org/licenses/>.
22 *
23 * In addition, as a special exception, the copyright holders give permission to
24 * link the code of portions of this program with the OpenSSL library under
25 * certain conditions as described in each individual source file, and
26 * distribute linked combinations including the two.
27 *
28 * You must obey the GNU General Public License in all respects for all of the
29 * code used other than OpenSSL. If you modify file(s) with this exception, you
30 * may extend this exception to your version of the file(s), but you are not
31 * obligated to do so. If you do not wish to do so, delete this exception
32 * statement from your version. If you delete this exception statement from all
33 * source files in the program, then also delete it here.
34 *
35 */
36
37 #include "config.h"
38
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <sys/stat.h>
43 #include <sys/types.h>
44 #include <curl/curl.h>
45 #include <gio/gio.h>
46 #include <pthread.h>
47 #include <assert.h>
48 #include <errno.h>
49
50 #include "profanity.h"
51 #include "event/client_events.h"
52 #include "tools/http_download.h"
53 #include "config/preferences.h"
54 #include "ui/ui.h"
55 #include "ui/window.h"
56 #include "common.h"
57
58 GSList* download_processes = NULL;
59
60 static int
_xferinfo(void * userdata,curl_off_t dltotal,curl_off_t dlnow,curl_off_t ultotal,curl_off_t ulnow)61 _xferinfo(void* userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
62 {
63 HTTPDownload* download = (HTTPDownload*)userdata;
64
65 pthread_mutex_lock(&lock);
66
67 if (download->cancel) {
68 pthread_mutex_unlock(&lock);
69 return 1;
70 }
71
72 if (download->bytes_received == dlnow) {
73 pthread_mutex_unlock(&lock);
74 return 0;
75 } else {
76 download->bytes_received = dlnow;
77 }
78
79 unsigned int dlperc = 0;
80 if (dltotal != 0) {
81 dlperc = (100 * dlnow) / dltotal;
82 }
83
84 http_print_transfer_update(download->window, download->url,
85 "Downloading '%s': %d%%", download->url, dlperc);
86
87 pthread_mutex_unlock(&lock);
88
89 return 0;
90 }
91
92 #if LIBCURL_VERSION_NUM < 0x072000
93 static int
_older_progress(void * p,double dltotal,double dlnow,double ultotal,double ulnow)94 _older_progress(void* p, double dltotal, double dlnow, double ultotal, double ulnow)
95 {
96 return _xferinfo(p, (curl_off_t)dltotal, (curl_off_t)dlnow, (curl_off_t)ultotal, (curl_off_t)ulnow);
97 }
98 #endif
99
100 void*
http_file_get(void * userdata)101 http_file_get(void* userdata)
102 {
103 HTTPDownload* download = (HTTPDownload*)userdata;
104
105 char* err = NULL;
106
107 CURL* curl;
108 CURLcode res;
109
110 download->cancel = 0;
111 download->bytes_received = 0;
112
113 pthread_mutex_lock(&lock);
114 http_print_transfer(download->window, download->url,
115 "Downloading '%s': 0%%", download->url);
116
117 FILE* outfh = fopen(download->filename, "wb");
118 if (outfh == NULL) {
119 http_print_transfer_update(download->window, download->url,
120 "Downloading '%s' failed: Unable to open "
121 "output file at '%s' for writing (%s).",
122 download->url, download->filename,
123 g_strerror(errno));
124 goto out;
125 }
126
127 char* cert_path = prefs_get_string(PREF_TLS_CERTPATH);
128 pthread_mutex_unlock(&lock);
129
130 curl_global_init(CURL_GLOBAL_ALL);
131 curl = curl_easy_init();
132
133 curl_easy_setopt(curl, CURLOPT_URL, download->url);
134
135 #if LIBCURL_VERSION_NUM >= 0x072000
136 curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, _xferinfo);
137 curl_easy_setopt(curl, CURLOPT_XFERINFODATA, download);
138 #else
139 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, _older_progress);
140 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, download);
141 #endif
142 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
143
144 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)outfh);
145
146 curl_easy_setopt(curl, CURLOPT_USERAGENT, "profanity");
147
148 if (cert_path) {
149 curl_easy_setopt(curl, CURLOPT_CAPATH, cert_path);
150 }
151
152 if ((res = curl_easy_perform(curl)) != CURLE_OK) {
153 err = strdup(curl_easy_strerror(res));
154 }
155
156 curl_easy_cleanup(curl);
157 curl_global_cleanup();
158
159 if (fclose(outfh) == EOF) {
160 err = strdup(g_strerror(errno));
161 }
162
163 pthread_mutex_lock(&lock);
164 g_free(cert_path);
165 if (err) {
166 if (download->cancel) {
167 http_print_transfer_update(download->window, download->url,
168 "Downloading '%s' failed: "
169 "Download was canceled",
170 download->url);
171 } else {
172 http_print_transfer_update(download->window, download->url,
173 "Downloading '%s' failed: %s",
174 download->url, err);
175 }
176 free(err);
177 } else {
178 if (!download->cancel) {
179 http_print_transfer_update(download->window, download->url,
180 "Downloading '%s': done",
181 download->url);
182 win_mark_received(download->window, download->url);
183 }
184 }
185
186 if (download->cmd_template != NULL) {
187 gchar** argv = format_call_external_argv(download->cmd_template,
188 download->url,
189 download->filename);
190
191 // TODO: Log the error.
192 if (!call_external(argv, NULL, NULL)) {
193 http_print_transfer_update(download->window, download->url,
194 "Downloading '%s' failed: Unable to call "
195 "command '%s' with file at '%s' (%s).",
196 download->url,
197 download->cmd_template,
198 download->filename,
199 "TODO: Log the error");
200 }
201
202 g_strfreev(argv);
203 free(download->cmd_template);
204 }
205
206 out:
207
208 download_processes = g_slist_remove(download_processes, download);
209 pthread_mutex_unlock(&lock);
210
211 free(download->url);
212 free(download->filename);
213 free(download);
214
215 return NULL;
216 }
217
218 void
http_download_cancel_processes(ProfWin * window)219 http_download_cancel_processes(ProfWin* window)
220 {
221 GSList* download_process = download_processes;
222 while (download_process) {
223 HTTPDownload* download = download_process->data;
224 if (download->window == window) {
225 download->cancel = 1;
226 break;
227 }
228 download_process = g_slist_next(download_process);
229 }
230 }
231
232 void
http_download_add_download(HTTPDownload * download)233 http_download_add_download(HTTPDownload* download)
234 {
235 download_processes = g_slist_append(download_processes, download);
236 }
237