]>
Commit | Line | Data |
---|---|---|
769ce76d AG |
1 | /* |
2 | * QEMU Block driver for CURL images | |
3 | * | |
4 | * Copyright (c) 2009 Alexander Graf <agraf@suse.de> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
452fcdbc | 24 | |
80c71a24 | 25 | #include "qemu/osdep.h" |
da34e65c | 26 | #include "qapi/error.h" |
796a060b | 27 | #include "qemu/error-report.h" |
0b8fa32f | 28 | #include "qemu/module.h" |
922a01a0 | 29 | #include "qemu/option.h" |
e2c1c34f | 30 | #include "block/block-io.h" |
737e150e | 31 | #include "block/block_int.h" |
452fcdbc | 32 | #include "qapi/qmp/qdict.h" |
d49b6836 | 33 | #include "qapi/qmp/qstring.h" |
1bff9606 | 34 | #include "crypto/secret.h" |
769ce76d | 35 | #include <curl/curl.h> |
f348b6d1 | 36 | #include "qemu/cutils.h" |
ed2a66de | 37 | #include "trace.h" |
769ce76d | 38 | |
769ce76d AG |
39 | // #define DEBUG_VERBOSE |
40 | ||
fb6d1bbd | 41 | #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \ |
23dce387 | 42 | CURLPROTO_FTP | CURLPROTO_FTPS) |
fb6d1bbd | 43 | |
769ce76d AG |
44 | #define CURL_NUM_STATES 8 |
45 | #define CURL_NUM_ACB 8 | |
f76faeda | 46 | #define CURL_TIMEOUT_MAX 10000 |
769ce76d | 47 | |
e3542c67 MB |
48 | #define CURL_BLOCK_OPT_URL "url" |
49 | #define CURL_BLOCK_OPT_READAHEAD "readahead" | |
97a3ea57 | 50 | #define CURL_BLOCK_OPT_SSLVERIFY "sslverify" |
212aefaa | 51 | #define CURL_BLOCK_OPT_TIMEOUT "timeout" |
a94f83d9 | 52 | #define CURL_BLOCK_OPT_COOKIE "cookie" |
327c8ebd | 53 | #define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret" |
1bff9606 DB |
54 | #define CURL_BLOCK_OPT_USERNAME "username" |
55 | #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret" | |
56 | #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username" | |
57 | #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret" | |
e3542c67 | 58 | |
712b64e8 HR |
59 | #define CURL_BLOCK_OPT_READAHEAD_DEFAULT (256 * 1024) |
60 | #define CURL_BLOCK_OPT_SSLVERIFY_DEFAULT true | |
61 | #define CURL_BLOCK_OPT_TIMEOUT_DEFAULT 5 | |
62 | ||
769ce76d | 63 | struct BDRVCURLState; |
04878616 | 64 | struct CURLState; |
769ce76d | 65 | |
2d25964d JC |
66 | static bool libcurl_initialized; |
67 | ||
769ce76d | 68 | typedef struct CURLAIOCB { |
28256d82 | 69 | Coroutine *co; |
769ce76d | 70 | QEMUIOVector *qiov; |
363c3c85 | 71 | |
2125e5ea PB |
72 | uint64_t offset; |
73 | uint64_t bytes; | |
28256d82 | 74 | int ret; |
363c3c85 | 75 | |
769ce76d AG |
76 | size_t start; |
77 | size_t end; | |
78 | } CURLAIOCB; | |
79 | ||
ff5ca166 HR |
80 | typedef struct CURLSocket { |
81 | int fd; | |
3663dca4 | 82 | struct BDRVCURLState *s; |
ff5ca166 HR |
83 | } CURLSocket; |
84 | ||
769ce76d AG |
85 | typedef struct CURLState |
86 | { | |
87 | struct BDRVCURLState *s; | |
88 | CURLAIOCB *acb[CURL_NUM_ACB]; | |
89 | CURL *curl; | |
90 | char *orig_buf; | |
2125e5ea | 91 | uint64_t buf_start; |
769ce76d AG |
92 | size_t buf_off; |
93 | size_t buf_len; | |
94 | char range[128]; | |
95 | char errmsg[CURL_ERROR_SIZE]; | |
96 | char in_use; | |
97 | } CURLState; | |
98 | ||
99 | typedef struct BDRVCURLState { | |
100 | CURLM *multi; | |
031fd1be | 101 | QEMUTimer timer; |
2125e5ea | 102 | uint64_t len; |
769ce76d | 103 | CURLState states[CURL_NUM_STATES]; |
0f418a20 | 104 | GHashTable *sockets; /* GINT_TO_POINTER(fd) -> socket */ |
769ce76d | 105 | char *url; |
c76f4952 | 106 | size_t readahead_size; |
97a3ea57 | 107 | bool sslverify; |
f76faeda | 108 | uint64_t timeout; |
a94f83d9 | 109 | char *cookie; |
3494d650 | 110 | bool accept_range; |
63f0f45f | 111 | AioContext *aio_context; |
ba3186c4 | 112 | QemuMutex mutex; |
709f2132 | 113 | CoQueue free_state_waitq; |
1bff9606 DB |
114 | char *username; |
115 | char *password; | |
116 | char *proxyusername; | |
117 | char *proxypassword; | |
769ce76d AG |
118 | } BDRVCURLState; |
119 | ||
120 | static void curl_clean_state(CURLState *s); | |
121 | static void curl_multi_do(void *arg); | |
122 | ||
0f418a20 HR |
123 | static gboolean curl_drop_socket(void *key, void *value, void *opaque) |
124 | { | |
125 | CURLSocket *socket = value; | |
126 | BDRVCURLState *s = socket->s; | |
127 | ||
128 | aio_set_fd_handler(s->aio_context, socket->fd, false, | |
826cc324 | 129 | NULL, NULL, NULL, NULL, NULL); |
0f418a20 HR |
130 | return true; |
131 | } | |
132 | ||
133 | static void curl_drop_all_sockets(GHashTable *sockets) | |
134 | { | |
135 | g_hash_table_foreach_remove(sockets, curl_drop_socket, NULL); | |
136 | } | |
137 | ||
34db05e7 | 138 | /* Called from curl_multi_do_locked, with s->mutex held. */ |
031fd1be PM |
139 | static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque) |
140 | { | |
141 | BDRVCURLState *s = opaque; | |
142 | ||
ed2a66de | 143 | trace_curl_timer_cb(timeout_ms); |
031fd1be PM |
144 | if (timeout_ms == -1) { |
145 | timer_del(&s->timer); | |
146 | } else { | |
147 | int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000; | |
148 | timer_mod(&s->timer, | |
149 | qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns); | |
150 | } | |
151 | return 0; | |
152 | } | |
031fd1be | 153 | |
34db05e7 | 154 | /* Called from curl_multi_do_locked, with s->mutex held. */ |
769ce76d | 155 | static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action, |
63f0f45f | 156 | void *userp, void *sp) |
769ce76d | 157 | { |
63f0f45f | 158 | BDRVCURLState *s; |
838ef602 | 159 | CURLState *state = NULL; |
ff5ca166 HR |
160 | CURLSocket *socket; |
161 | ||
838ef602 | 162 | curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state); |
63f0f45f | 163 | s = state->s; |
838ef602 | 164 | |
0f418a20 | 165 | socket = g_hash_table_lookup(s->sockets, GINT_TO_POINTER(fd)); |
ff5ca166 HR |
166 | if (!socket) { |
167 | socket = g_new0(CURLSocket, 1); | |
168 | socket->fd = fd; | |
3663dca4 | 169 | socket->s = s; |
0f418a20 | 170 | g_hash_table_insert(s->sockets, GINT_TO_POINTER(fd), socket); |
ff5ca166 | 171 | } |
ff5ca166 | 172 | |
ed2a66de | 173 | trace_curl_sock_cb(action, (int)fd); |
769ce76d AG |
174 | switch (action) { |
175 | case CURL_POLL_IN: | |
dca21ef2 | 176 | aio_set_fd_handler(s->aio_context, fd, false, |
826cc324 | 177 | curl_multi_do, NULL, NULL, NULL, socket); |
769ce76d AG |
178 | break; |
179 | case CURL_POLL_OUT: | |
dca21ef2 | 180 | aio_set_fd_handler(s->aio_context, fd, false, |
826cc324 | 181 | NULL, curl_multi_do, NULL, NULL, socket); |
769ce76d AG |
182 | break; |
183 | case CURL_POLL_INOUT: | |
dca21ef2 | 184 | aio_set_fd_handler(s->aio_context, fd, false, |
826cc324 SH |
185 | curl_multi_do, curl_multi_do, |
186 | NULL, NULL, socket); | |
769ce76d AG |
187 | break; |
188 | case CURL_POLL_REMOVE: | |
dca21ef2 | 189 | aio_set_fd_handler(s->aio_context, fd, false, |
826cc324 | 190 | NULL, NULL, NULL, NULL, NULL); |
769ce76d AG |
191 | break; |
192 | } | |
193 | ||
007f339b | 194 | if (action == CURL_POLL_REMOVE) { |
0f418a20 | 195 | g_hash_table_remove(s->sockets, GINT_TO_POINTER(fd)); |
007f339b HR |
196 | } |
197 | ||
769ce76d AG |
198 | return 0; |
199 | } | |
200 | ||
34db05e7 | 201 | /* Called from curl_multi_do_locked, with s->mutex held. */ |
3494d650 | 202 | static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) |
769ce76d | 203 | { |
3494d650 | 204 | BDRVCURLState *s = opaque; |
769ce76d | 205 | size_t realsize = size * nmemb; |
7788a319 DE |
206 | const char *header = (char *)ptr; |
207 | const char *end = header + realsize; | |
69032253 | 208 | const char *accept_ranges = "accept-ranges:"; |
7788a319 | 209 | const char *bytes = "bytes"; |
769ce76d | 210 | |
7788a319 | 211 | if (realsize >= strlen(accept_ranges) |
69032253 DE |
212 | && g_ascii_strncasecmp(header, accept_ranges, |
213 | strlen(accept_ranges)) == 0) { | |
7788a319 DE |
214 | |
215 | char *p = strchr(header, ':') + 1; | |
216 | ||
217 | /* Skip whitespace between the header name and value. */ | |
218 | while (p < end && *p && g_ascii_isspace(*p)) { | |
219 | p++; | |
220 | } | |
221 | ||
222 | if (end - p >= strlen(bytes) | |
223 | && strncmp(p, bytes, strlen(bytes)) == 0) { | |
224 | ||
225 | /* Check that there is nothing but whitespace after the value. */ | |
226 | p += strlen(bytes); | |
227 | while (p < end && *p && g_ascii_isspace(*p)) { | |
228 | p++; | |
229 | } | |
230 | ||
231 | if (p == end || !*p) { | |
232 | s->accept_range = true; | |
233 | } | |
234 | } | |
0bfcd599 | 235 | } |
769ce76d AG |
236 | |
237 | return realsize; | |
238 | } | |
239 | ||
34db05e7 | 240 | /* Called from curl_multi_do_locked, with s->mutex held. */ |
769ce76d AG |
241 | static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) |
242 | { | |
243 | CURLState *s = ((CURLState*)opaque); | |
244 | size_t realsize = size * nmemb; | |
769ce76d | 245 | |
ed2a66de | 246 | trace_curl_read_cb(realsize); |
769ce76d | 247 | |
4e767657 HR |
248 | if (!s || !s->orig_buf) { |
249 | goto read_end; | |
250 | } | |
769ce76d | 251 | |
6d4b9e55 FZ |
252 | if (s->buf_off >= s->buf_len) { |
253 | /* buffer full, read nothing */ | |
4e767657 | 254 | goto read_end; |
6d4b9e55 FZ |
255 | } |
256 | realsize = MIN(realsize, s->buf_len - s->buf_off); | |
769ce76d AG |
257 | memcpy(s->orig_buf + s->buf_off, ptr, realsize); |
258 | s->buf_off += realsize; | |
259 | ||
4e767657 HR |
260 | read_end: |
261 | /* curl will error out if we do not return this value */ | |
262 | return size * nmemb; | |
769ce76d AG |
263 | } |
264 | ||
456af346 | 265 | /* Called with s->mutex held. */ |
28256d82 PB |
266 | static bool curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len, |
267 | CURLAIOCB *acb) | |
769ce76d AG |
268 | { |
269 | int i; | |
2125e5ea PB |
270 | uint64_t end = start + len; |
271 | uint64_t clamped_end = MIN(end, s->len); | |
272 | uint64_t clamped_len = clamped_end - start; | |
769ce76d AG |
273 | |
274 | for (i=0; i<CURL_NUM_STATES; i++) { | |
275 | CURLState *state = &s->states[i]; | |
2125e5ea PB |
276 | uint64_t buf_end = (state->buf_start + state->buf_off); |
277 | uint64_t buf_fend = (state->buf_start + state->buf_len); | |
769ce76d AG |
278 | |
279 | if (!state->orig_buf) | |
280 | continue; | |
281 | if (!state->buf_off) | |
282 | continue; | |
283 | ||
284 | // Does the existing buffer cover our section? | |
285 | if ((start >= state->buf_start) && | |
286 | (start <= buf_end) && | |
4e504535 HR |
287 | (clamped_end >= state->buf_start) && |
288 | (clamped_end <= buf_end)) | |
769ce76d AG |
289 | { |
290 | char *buf = state->orig_buf + (start - state->buf_start); | |
291 | ||
4e504535 HR |
292 | qemu_iovec_from_buf(acb->qiov, 0, buf, clamped_len); |
293 | if (clamped_len < len) { | |
294 | qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped_len); | |
295 | } | |
28256d82 PB |
296 | acb->ret = 0; |
297 | return true; | |
769ce76d AG |
298 | } |
299 | ||
300 | // Wait for unfinished chunks | |
b7079df4 MB |
301 | if (state->in_use && |
302 | (start >= state->buf_start) && | |
769ce76d | 303 | (start <= buf_fend) && |
4e504535 HR |
304 | (clamped_end >= state->buf_start) && |
305 | (clamped_end <= buf_fend)) | |
769ce76d AG |
306 | { |
307 | int j; | |
308 | ||
309 | acb->start = start - state->buf_start; | |
4e504535 | 310 | acb->end = acb->start + clamped_len; |
769ce76d AG |
311 | |
312 | for (j=0; j<CURL_NUM_ACB; j++) { | |
313 | if (!state->acb[j]) { | |
314 | state->acb[j] = acb; | |
28256d82 | 315 | return true; |
769ce76d AG |
316 | } |
317 | } | |
318 | } | |
319 | } | |
320 | ||
28256d82 | 321 | return false; |
769ce76d AG |
322 | } |
323 | ||
ba3186c4 | 324 | /* Called with s->mutex held. */ |
838ef602 | 325 | static void curl_multi_check_completion(BDRVCURLState *s) |
769ce76d | 326 | { |
769ce76d AG |
327 | int msgs_in_queue; |
328 | ||
769ce76d AG |
329 | /* Try to find done transfers, so we can free the easy |
330 | * handle again. */ | |
1f2cead3 | 331 | for (;;) { |
769ce76d AG |
332 | CURLMsg *msg; |
333 | msg = curl_multi_info_read(s->multi, &msgs_in_queue); | |
334 | ||
1f2cead3 | 335 | /* Quit when there are no more completions */ |
769ce76d AG |
336 | if (!msg) |
337 | break; | |
769ce76d | 338 | |
1f2cead3 | 339 | if (msg->msg == CURLMSG_DONE) { |
bfb23b48 | 340 | int i; |
1f2cead3 | 341 | CURLState *state = NULL; |
bfb23b48 HR |
342 | bool error = msg->data.result != CURLE_OK; |
343 | ||
1f2cead3 MB |
344 | curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, |
345 | (char **)&state); | |
346 | ||
bfb23b48 | 347 | if (error) { |
796a060b RJ |
348 | static int errcount = 100; |
349 | ||
350 | /* Don't lose the original error message from curl, since | |
351 | * it contains extra data. | |
352 | */ | |
353 | if (errcount > 0) { | |
354 | error_report("curl: %s", state->errmsg); | |
355 | if (--errcount == 0) { | |
356 | error_report("curl: further errors suppressed"); | |
357 | } | |
358 | } | |
bfb23b48 | 359 | } |
796a060b | 360 | |
bfb23b48 HR |
361 | for (i = 0; i < CURL_NUM_ACB; i++) { |
362 | CURLAIOCB *acb = state->acb[i]; | |
1f2cead3 | 363 | |
bfb23b48 HR |
364 | if (acb == NULL) { |
365 | continue; | |
366 | } | |
367 | ||
368 | if (!error) { | |
369 | /* Assert that we have read all data */ | |
370 | assert(state->buf_off >= acb->end); | |
371 | ||
372 | qemu_iovec_from_buf(acb->qiov, 0, | |
373 | state->orig_buf + acb->start, | |
374 | acb->end - acb->start); | |
f785a5ae | 375 | |
bfb23b48 HR |
376 | if (acb->end - acb->start < acb->bytes) { |
377 | size_t offset = acb->end - acb->start; | |
378 | qemu_iovec_memset(acb->qiov, offset, 0, | |
379 | acb->bytes - offset); | |
380 | } | |
1f2cead3 | 381 | } |
bfb23b48 HR |
382 | |
383 | acb->ret = error ? -EIO : 0; | |
384 | state->acb[i] = NULL; | |
385 | qemu_mutex_unlock(&s->mutex); | |
386 | aio_co_wake(acb->co); | |
387 | qemu_mutex_lock(&s->mutex); | |
769ce76d | 388 | } |
1f2cead3 MB |
389 | |
390 | curl_clean_state(state); | |
391 | break; | |
769ce76d | 392 | } |
1f2cead3 | 393 | } |
769ce76d AG |
394 | } |
395 | ||
ba3186c4 | 396 | /* Called with s->mutex held. */ |
9abaf9fc | 397 | static void curl_multi_do_locked(CURLSocket *socket) |
031fd1be | 398 | { |
3663dca4 | 399 | BDRVCURLState *s = socket->s; |
031fd1be PM |
400 | int running; |
401 | int r; | |
402 | ||
9abaf9fc | 403 | if (!s->multi) { |
031fd1be PM |
404 | return; |
405 | } | |
406 | ||
9abaf9fc HR |
407 | do { |
408 | r = curl_multi_socket_action(s->multi, socket->fd, 0, &running); | |
409 | } while (r == CURLM_CALL_MULTI_PERFORM); | |
838ef602 MB |
410 | } |
411 | ||
9d456654 PB |
412 | static void curl_multi_do(void *arg) |
413 | { | |
9dbad87d | 414 | CURLSocket *socket = arg; |
3663dca4 | 415 | BDRVCURLState *s = socket->s; |
9d456654 | 416 | |
9dbad87d HR |
417 | qemu_mutex_lock(&s->mutex); |
418 | curl_multi_do_locked(socket); | |
419 | curl_multi_check_completion(s); | |
420 | qemu_mutex_unlock(&s->mutex); | |
031fd1be PM |
421 | } |
422 | ||
423 | static void curl_multi_timeout_do(void *arg) | |
424 | { | |
031fd1be PM |
425 | BDRVCURLState *s = (BDRVCURLState *)arg; |
426 | int running; | |
427 | ||
428 | if (!s->multi) { | |
429 | return; | |
430 | } | |
431 | ||
ba3186c4 | 432 | qemu_mutex_lock(&s->mutex); |
031fd1be PM |
433 | curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); |
434 | ||
838ef602 | 435 | curl_multi_check_completion(s); |
ba3186c4 | 436 | qemu_mutex_unlock(&s->mutex); |
031fd1be PM |
437 | } |
438 | ||
456af346 | 439 | /* Called with s->mutex held. */ |
3ce6a729 | 440 | static CURLState *curl_find_state(BDRVCURLState *s) |
769ce76d AG |
441 | { |
442 | CURLState *state = NULL; | |
3ce6a729 | 443 | int i; |
769ce76d | 444 | |
3ce6a729 PB |
445 | for (i = 0; i < CURL_NUM_STATES; i++) { |
446 | if (!s->states[i].in_use) { | |
769ce76d AG |
447 | state = &s->states[i]; |
448 | state->in_use = 1; | |
449 | break; | |
450 | } | |
3ce6a729 PB |
451 | } |
452 | return state; | |
453 | } | |
769ce76d | 454 | |
3ce6a729 PB |
455 | static int curl_init_state(BDRVCURLState *s, CURLState *state) |
456 | { | |
9e550b32 MB |
457 | if (!state->curl) { |
458 | state->curl = curl_easy_init(); | |
459 | if (!state->curl) { | |
3ce6a729 | 460 | return -EIO; |
9e550b32 | 461 | } |
b0ea6c98 PM |
462 | if (curl_easy_setopt(state->curl, CURLOPT_URL, s->url) || |
463 | curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER, | |
464 | (long) s->sslverify) || | |
465 | curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYHOST, | |
466 | s->sslverify ? 2L : 0L)) { | |
467 | goto err; | |
468 | } | |
a94f83d9 | 469 | if (s->cookie) { |
b0ea6c98 PM |
470 | if (curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie)) { |
471 | goto err; | |
472 | } | |
473 | } | |
474 | if (curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout) || | |
475 | curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, | |
476 | (void *)curl_read_cb) || | |
477 | curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state) || | |
478 | curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state) || | |
479 | curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1) || | |
480 | curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1) || | |
481 | curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1) || | |
482 | curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg) || | |
483 | curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1)) { | |
484 | goto err; | |
a94f83d9 | 485 | } |
1bff9606 | 486 | if (s->username) { |
b0ea6c98 PM |
487 | if (curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username)) { |
488 | goto err; | |
489 | } | |
1bff9606 DB |
490 | } |
491 | if (s->password) { | |
b0ea6c98 PM |
492 | if (curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password)) { |
493 | goto err; | |
494 | } | |
1bff9606 DB |
495 | } |
496 | if (s->proxyusername) { | |
b0ea6c98 PM |
497 | if (curl_easy_setopt(state->curl, |
498 | CURLOPT_PROXYUSERNAME, s->proxyusername)) { | |
499 | goto err; | |
500 | } | |
1bff9606 DB |
501 | } |
502 | if (s->proxypassword) { | |
b0ea6c98 PM |
503 | if (curl_easy_setopt(state->curl, |
504 | CURLOPT_PROXYPASSWORD, s->proxypassword)) { | |
505 | goto err; | |
506 | } | |
1bff9606 DB |
507 | } |
508 | ||
9e550b32 MB |
509 | /* Restrict supported protocols to avoid security issues in the more |
510 | * obscure protocols. For example, do not allow POP3/SMTP/IMAP see | |
511 | * CVE-2013-0249. | |
512 | * | |
513 | * Restricting protocols is only supported from 7.19.4 upwards. | |
514 | */ | |
8a8f5840 | 515 | #if LIBCURL_VERSION_NUM >= 0x071304 |
b0ea6c98 PM |
516 | if (curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS) || |
517 | curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS)) { | |
518 | goto err; | |
519 | } | |
8a8f5840 | 520 | #endif |
fb6d1bbd | 521 | |
769ce76d | 522 | #ifdef DEBUG_VERBOSE |
b0ea6c98 PM |
523 | if (curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1)) { |
524 | goto err; | |
525 | } | |
769ce76d | 526 | #endif |
9e550b32 | 527 | } |
769ce76d AG |
528 | |
529 | state->s = s; | |
530 | ||
3ce6a729 | 531 | return 0; |
b0ea6c98 PM |
532 | |
533 | err: | |
534 | curl_easy_cleanup(state->curl); | |
535 | state->curl = NULL; | |
536 | return -EIO; | |
769ce76d AG |
537 | } |
538 | ||
456af346 | 539 | /* Called with s->mutex held. */ |
769ce76d AG |
540 | static void curl_clean_state(CURLState *s) |
541 | { | |
675a7756 PB |
542 | int j; |
543 | for (j = 0; j < CURL_NUM_ACB; j++) { | |
544 | assert(!s->acb[j]); | |
545 | } | |
546 | ||
769ce76d AG |
547 | if (s->s->multi) |
548 | curl_multi_remove_handle(s->s->multi, s->curl); | |
ff5ca166 | 549 | |
769ce76d | 550 | s->in_use = 0; |
2bb5c936 | 551 | |
709f2132 | 552 | qemu_co_enter_next(&s->s->free_state_waitq, &s->s->mutex); |
769ce76d AG |
553 | } |
554 | ||
8e6d58cd KW |
555 | static void curl_parse_filename(const char *filename, QDict *options, |
556 | Error **errp) | |
769ce76d | 557 | { |
46f5ac20 | 558 | qdict_put_str(options, CURL_BLOCK_OPT_URL, filename); |
8e6d58cd KW |
559 | } |
560 | ||
63f0f45f SH |
561 | static void curl_detach_aio_context(BlockDriverState *bs) |
562 | { | |
563 | BDRVCURLState *s = bs->opaque; | |
564 | int i; | |
565 | ||
f5056b70 | 566 | WITH_QEMU_LOCK_GUARD(&s->mutex) { |
0f418a20 | 567 | curl_drop_all_sockets(s->sockets); |
f5056b70 GQ |
568 | for (i = 0; i < CURL_NUM_STATES; i++) { |
569 | if (s->states[i].in_use) { | |
570 | curl_clean_state(&s->states[i]); | |
571 | } | |
572 | if (s->states[i].curl) { | |
573 | curl_easy_cleanup(s->states[i].curl); | |
574 | s->states[i].curl = NULL; | |
575 | } | |
576 | g_free(s->states[i].orig_buf); | |
577 | s->states[i].orig_buf = NULL; | |
63f0f45f | 578 | } |
f5056b70 GQ |
579 | if (s->multi) { |
580 | curl_multi_cleanup(s->multi); | |
581 | s->multi = NULL; | |
63f0f45f | 582 | } |
63f0f45f SH |
583 | } |
584 | ||
585 | timer_del(&s->timer); | |
586 | } | |
587 | ||
588 | static void curl_attach_aio_context(BlockDriverState *bs, | |
589 | AioContext *new_context) | |
590 | { | |
591 | BDRVCURLState *s = bs->opaque; | |
592 | ||
593 | aio_timer_init(new_context, &s->timer, | |
594 | QEMU_CLOCK_REALTIME, SCALE_NS, | |
595 | curl_multi_timeout_do, s); | |
596 | ||
597 | assert(!s->multi); | |
598 | s->multi = curl_multi_init(); | |
599 | s->aio_context = new_context; | |
600 | curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb); | |
63f0f45f SH |
601 | curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s); |
602 | curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb); | |
63f0f45f SH |
603 | } |
604 | ||
8e6d58cd KW |
605 | static QemuOptsList runtime_opts = { |
606 | .name = "curl", | |
607 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
608 | .desc = { | |
609 | { | |
e3542c67 | 610 | .name = CURL_BLOCK_OPT_URL, |
8e6d58cd KW |
611 | .type = QEMU_OPT_STRING, |
612 | .help = "URL to open", | |
613 | }, | |
614 | { | |
e3542c67 | 615 | .name = CURL_BLOCK_OPT_READAHEAD, |
8e6d58cd KW |
616 | .type = QEMU_OPT_SIZE, |
617 | .help = "Readahead size", | |
618 | }, | |
97a3ea57 MB |
619 | { |
620 | .name = CURL_BLOCK_OPT_SSLVERIFY, | |
621 | .type = QEMU_OPT_BOOL, | |
622 | .help = "Verify SSL certificate" | |
623 | }, | |
212aefaa DHB |
624 | { |
625 | .name = CURL_BLOCK_OPT_TIMEOUT, | |
626 | .type = QEMU_OPT_NUMBER, | |
627 | .help = "Curl timeout" | |
628 | }, | |
a94f83d9 RJ |
629 | { |
630 | .name = CURL_BLOCK_OPT_COOKIE, | |
631 | .type = QEMU_OPT_STRING, | |
632 | .help = "Pass the cookie or list of cookies with each request" | |
633 | }, | |
327c8ebd PK |
634 | { |
635 | .name = CURL_BLOCK_OPT_COOKIE_SECRET, | |
636 | .type = QEMU_OPT_STRING, | |
637 | .help = "ID of secret used as cookie passed with each request" | |
638 | }, | |
1bff9606 DB |
639 | { |
640 | .name = CURL_BLOCK_OPT_USERNAME, | |
641 | .type = QEMU_OPT_STRING, | |
642 | .help = "Username for HTTP auth" | |
643 | }, | |
644 | { | |
645 | .name = CURL_BLOCK_OPT_PASSWORD_SECRET, | |
646 | .type = QEMU_OPT_STRING, | |
647 | .help = "ID of secret used as password for HTTP auth", | |
648 | }, | |
649 | { | |
650 | .name = CURL_BLOCK_OPT_PROXY_USERNAME, | |
651 | .type = QEMU_OPT_STRING, | |
652 | .help = "Username for HTTP proxy auth" | |
653 | }, | |
654 | { | |
655 | .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET, | |
656 | .type = QEMU_OPT_STRING, | |
657 | .help = "ID of secret used as password for HTTP proxy auth", | |
658 | }, | |
8e6d58cd KW |
659 | { /* end of list */ } |
660 | }, | |
661 | }; | |
662 | ||
1bff9606 | 663 | |
015a1036 HR |
664 | static int curl_open(BlockDriverState *bs, QDict *options, int flags, |
665 | Error **errp) | |
8e6d58cd KW |
666 | { |
667 | BDRVCURLState *s = bs->opaque; | |
668 | CURLState *state = NULL; | |
669 | QemuOpts *opts; | |
8e6d58cd | 670 | const char *file; |
a94f83d9 | 671 | const char *cookie; |
327c8ebd | 672 | const char *cookie_secret; |
8e6d58cd | 673 | double d; |
1bff9606 | 674 | const char *secretid; |
34634ca2 | 675 | const char *protocol_delimiter; |
2d25964d | 676 | int ret; |
8e6d58cd | 677 | |
6ceef36a KW |
678 | ret = bdrv_apply_auto_read_only(bs, "curl driver does not support writes", |
679 | errp); | |
680 | if (ret < 0) { | |
681 | return ret; | |
a7cea2ba RJ |
682 | } |
683 | ||
2d25964d JC |
684 | if (!libcurl_initialized) { |
685 | ret = curl_global_init(CURL_GLOBAL_ALL); | |
686 | if (ret) { | |
687 | error_setg(errp, "libcurl initialization failed with %d", ret); | |
688 | return -EIO; | |
689 | } | |
690 | libcurl_initialized = true; | |
691 | } | |
692 | ||
456af346 | 693 | qemu_mutex_init(&s->mutex); |
87ea75d5 | 694 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
668f62ec | 695 | if (!qemu_opts_absorb_qdict(opts, options, errp)) { |
8e6d58cd KW |
696 | goto out_noclean; |
697 | } | |
698 | ||
e3542c67 | 699 | s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD, |
712b64e8 | 700 | CURL_BLOCK_OPT_READAHEAD_DEFAULT); |
c76f4952 | 701 | if ((s->readahead_size & 0x1ff) != 0) { |
2a94fee3 PB |
702 | error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512", |
703 | s->readahead_size); | |
c76f4952 N |
704 | goto out_noclean; |
705 | } | |
706 | ||
212aefaa | 707 | s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT, |
712b64e8 | 708 | CURL_BLOCK_OPT_TIMEOUT_DEFAULT); |
f76faeda RJ |
709 | if (s->timeout > CURL_TIMEOUT_MAX) { |
710 | error_setg(errp, "timeout parameter is too large or negative"); | |
711 | goto out_noclean; | |
712 | } | |
212aefaa | 713 | |
712b64e8 HR |
714 | s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, |
715 | CURL_BLOCK_OPT_SSLVERIFY_DEFAULT); | |
97a3ea57 | 716 | |
a94f83d9 | 717 | cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE); |
327c8ebd PK |
718 | cookie_secret = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE_SECRET); |
719 | ||
720 | if (cookie && cookie_secret) { | |
721 | error_setg(errp, | |
722 | "curl driver cannot handle both cookie and cookie secret"); | |
723 | goto out_noclean; | |
724 | } | |
725 | ||
726 | if (cookie_secret) { | |
727 | s->cookie = qcrypto_secret_lookup_as_utf8(cookie_secret, errp); | |
728 | if (!s->cookie) { | |
729 | goto out_noclean; | |
730 | } | |
731 | } else { | |
732 | s->cookie = g_strdup(cookie); | |
733 | } | |
a94f83d9 | 734 | |
e3542c67 | 735 | file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL); |
8e6d58cd | 736 | if (file == NULL) { |
2a94fee3 | 737 | error_setg(errp, "curl block driver requires an 'url' option"); |
8e6d58cd KW |
738 | goto out_noclean; |
739 | } | |
740 | ||
34634ca2 HR |
741 | if (!strstart(file, bs->drv->protocol_name, &protocol_delimiter) || |
742 | !strstart(protocol_delimiter, "://", NULL)) | |
743 | { | |
744 | error_setg(errp, "%s curl driver cannot handle the URL '%s' (does not " | |
745 | "start with '%s://')", bs->drv->protocol_name, file, | |
746 | bs->drv->protocol_name); | |
747 | goto out_noclean; | |
748 | } | |
749 | ||
1bff9606 DB |
750 | s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME)); |
751 | secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET); | |
752 | ||
753 | if (secretid) { | |
754 | s->password = qcrypto_secret_lookup_as_utf8(secretid, errp); | |
755 | if (!s->password) { | |
756 | goto out_noclean; | |
757 | } | |
758 | } | |
759 | ||
760 | s->proxyusername = g_strdup( | |
761 | qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME)); | |
762 | secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET); | |
763 | if (secretid) { | |
764 | s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp); | |
765 | if (!s->proxypassword) { | |
766 | goto out_noclean; | |
767 | } | |
768 | } | |
769 | ||
ed2a66de | 770 | trace_curl_open(file); |
709f2132 | 771 | qemu_co_queue_init(&s->free_state_waitq); |
63f0f45f | 772 | s->aio_context = bdrv_get_aio_context(bs); |
8e6d58cd | 773 | s->url = g_strdup(file); |
0f418a20 | 774 | s->sockets = g_hash_table_new_full(NULL, NULL, NULL, g_free); |
456af346 | 775 | qemu_mutex_lock(&s->mutex); |
3ce6a729 | 776 | state = curl_find_state(s); |
456af346 | 777 | qemu_mutex_unlock(&s->mutex); |
3ce6a729 | 778 | if (!state) { |
769ce76d | 779 | goto out_noclean; |
3ce6a729 | 780 | } |
769ce76d AG |
781 | |
782 | // Get file size | |
783 | ||
3ce6a729 | 784 | if (curl_init_state(s, state) < 0) { |
2ea7dfcd PM |
785 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, |
786 | "curl library initialization failed."); | |
3ce6a729 PB |
787 | goto out; |
788 | } | |
789 | ||
3494d650 | 790 | s->accept_range = false; |
b0ea6c98 PM |
791 | if (curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1) || |
792 | curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, curl_header_cb) || | |
793 | curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s)) { | |
794 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, | |
795 | "curl library initialization failed."); | |
796 | goto out; | |
797 | } | |
769ce76d AG |
798 | if (curl_easy_perform(state->curl)) |
799 | goto out; | |
a41c4578 | 800 | if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) { |
769ce76d | 801 | goto out; |
a41c4578 TG |
802 | } |
803 | /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not | |
804 | * know or the size is zero. From 7.19.4 CURL returns -1 if size is not | |
50d6a8a3 | 805 | * known and zero if it is really zero-length file. */ |
a41c4578 TG |
806 | #if LIBCURL_VERSION_NUM >= 0x071304 |
807 | if (d < 0) { | |
808 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, | |
809 | "Server didn't report file size."); | |
810 | goto out; | |
811 | } | |
812 | #else | |
813 | if (d <= 0) { | |
814 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, | |
815 | "Unknown file size or zero-length file."); | |
816 | goto out; | |
817 | } | |
818 | #endif | |
819 | ||
2125e5ea | 820 | s->len = d; |
a41c4578 | 821 | |
3494d650 FZ |
822 | if ((!strncasecmp(s->url, "http://", strlen("http://")) |
823 | || !strncasecmp(s->url, "https://", strlen("https://"))) | |
824 | && !s->accept_range) { | |
825 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, | |
826 | "Server does not support 'range' (byte ranges)."); | |
827 | goto out; | |
828 | } | |
ed2a66de | 829 | trace_curl_open_size(s->len); |
769ce76d | 830 | |
456af346 | 831 | qemu_mutex_lock(&s->mutex); |
769ce76d | 832 | curl_clean_state(state); |
456af346 | 833 | qemu_mutex_unlock(&s->mutex); |
769ce76d AG |
834 | curl_easy_cleanup(state->curl); |
835 | state->curl = NULL; | |
836 | ||
63f0f45f | 837 | curl_attach_aio_context(bs, bdrv_get_aio_context(bs)); |
769ce76d | 838 | |
8e6d58cd | 839 | qemu_opts_del(opts); |
769ce76d AG |
840 | return 0; |
841 | ||
842 | out: | |
acd7fdc6 | 843 | error_setg(errp, "CURL: Error opening file: %s", state->errmsg); |
769ce76d AG |
844 | curl_easy_cleanup(state->curl); |
845 | state->curl = NULL; | |
846 | out_noclean: | |
456af346 | 847 | qemu_mutex_destroy(&s->mutex); |
a94f83d9 | 848 | g_free(s->cookie); |
8e6d58cd | 849 | g_free(s->url); |
996922de JC |
850 | g_free(s->username); |
851 | g_free(s->proxyusername); | |
852 | g_free(s->proxypassword); | |
0f418a20 HR |
853 | curl_drop_all_sockets(s->sockets); |
854 | g_hash_table_destroy(s->sockets); | |
8e6d58cd | 855 | qemu_opts_del(opts); |
769ce76d AG |
856 | return -EINVAL; |
857 | } | |
858 | ||
9bae2aca | 859 | static void coroutine_fn curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb) |
769ce76d | 860 | { |
769ce76d | 861 | CURLState *state; |
b69cdef8 | 862 | int running; |
769ce76d | 863 | |
1919631e | 864 | BDRVCURLState *s = bs->opaque; |
769ce76d | 865 | |
2125e5ea PB |
866 | uint64_t start = acb->offset; |
867 | uint64_t end; | |
769ce76d | 868 | |
ba3186c4 | 869 | qemu_mutex_lock(&s->mutex); |
1919631e | 870 | |
769ce76d AG |
871 | // In case we have the requested data already (e.g. read-ahead), |
872 | // we can just call the callback and be done. | |
28256d82 PB |
873 | if (curl_find_buf(s, start, acb->bytes, acb)) { |
874 | goto out; | |
769ce76d AG |
875 | } |
876 | ||
877 | // No cache found, so let's start a new request | |
3ce6a729 PB |
878 | for (;;) { |
879 | state = curl_find_state(s); | |
880 | if (state) { | |
881 | break; | |
882 | } | |
709f2132 | 883 | qemu_co_queue_wait(&s->free_state_waitq, &s->mutex); |
3ce6a729 PB |
884 | } |
885 | ||
886 | if (curl_init_state(s, state) < 0) { | |
887 | curl_clean_state(state); | |
28256d82 | 888 | acb->ret = -EIO; |
1919631e | 889 | goto out; |
363c3c85 | 890 | } |
769ce76d AG |
891 | |
892 | acb->start = 0; | |
2125e5ea | 893 | acb->end = MIN(acb->bytes, s->len - start); |
769ce76d AG |
894 | |
895 | state->buf_off = 0; | |
f7047c2d | 896 | g_free(state->orig_buf); |
769ce76d | 897 | state->buf_start = start; |
4e504535 HR |
898 | state->buf_len = MIN(acb->end + s->readahead_size, s->len - start); |
899 | end = start + state->buf_len - 1; | |
8dc7a772 KW |
900 | state->orig_buf = g_try_malloc(state->buf_len); |
901 | if (state->buf_len && state->orig_buf == NULL) { | |
902 | curl_clean_state(state); | |
28256d82 | 903 | acb->ret = -ENOMEM; |
1919631e | 904 | goto out; |
8dc7a772 | 905 | } |
769ce76d AG |
906 | state->acb[0] = acb; |
907 | ||
2125e5ea | 908 | snprintf(state->range, 127, "%" PRIu64 "-%" PRIu64, start, end); |
ed2a66de | 909 | trace_curl_setup_preadv(acb->bytes, start, state->range); |
b0ea6c98 PM |
910 | if (curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range) || |
911 | curl_multi_add_handle(s->multi, state->curl) != CURLM_OK) { | |
c34dc07f HR |
912 | state->acb[0] = NULL; |
913 | acb->ret = -EIO; | |
914 | ||
915 | curl_clean_state(state); | |
916 | goto out; | |
917 | } | |
769ce76d | 918 | |
b69cdef8 MB |
919 | /* Tell curl it needs to kick things off */ |
920 | curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); | |
1919631e PB |
921 | |
922 | out: | |
ba3186c4 | 923 | qemu_mutex_unlock(&s->mutex); |
363c3c85 NT |
924 | } |
925 | ||
28256d82 | 926 | static int coroutine_fn curl_co_preadv(BlockDriverState *bs, |
f7ef38dd VSO |
927 | int64_t offset, int64_t bytes, QEMUIOVector *qiov, |
928 | BdrvRequestFlags flags) | |
363c3c85 | 929 | { |
28256d82 PB |
930 | CURLAIOCB acb = { |
931 | .co = qemu_coroutine_self(), | |
932 | .ret = -EINPROGRESS, | |
933 | .qiov = qiov, | |
934 | .offset = offset, | |
935 | .bytes = bytes | |
936 | }; | |
937 | ||
938 | curl_setup_preadv(bs, &acb); | |
939 | while (acb.ret == -EINPROGRESS) { | |
940 | qemu_coroutine_yield(); | |
941 | } | |
942 | return acb.ret; | |
769ce76d AG |
943 | } |
944 | ||
769ce76d AG |
945 | static void curl_close(BlockDriverState *bs) |
946 | { | |
947 | BDRVCURLState *s = bs->opaque; | |
769ce76d | 948 | |
ed2a66de | 949 | trace_curl_close(); |
63f0f45f | 950 | curl_detach_aio_context(bs); |
ba3186c4 | 951 | qemu_mutex_destroy(&s->mutex); |
031fd1be | 952 | |
0f418a20 | 953 | g_hash_table_destroy(s->sockets); |
a94f83d9 | 954 | g_free(s->cookie); |
45724d6d | 955 | g_free(s->url); |
996922de JC |
956 | g_free(s->username); |
957 | g_free(s->proxyusername); | |
958 | g_free(s->proxypassword); | |
769ce76d AG |
959 | } |
960 | ||
c86422c5 | 961 | static int64_t coroutine_fn curl_co_getlength(BlockDriverState *bs) |
769ce76d AG |
962 | { |
963 | BDRVCURLState *s = bs->opaque; | |
964 | return s->len; | |
965 | } | |
966 | ||
937c007b HR |
967 | static void curl_refresh_filename(BlockDriverState *bs) |
968 | { | |
969 | BDRVCURLState *s = bs->opaque; | |
970 | ||
971 | /* "readahead" and "timeout" do not change the guest-visible data, | |
972 | * so ignore them */ | |
973 | if (s->sslverify != CURL_BLOCK_OPT_SSLVERIFY_DEFAULT || | |
974 | s->cookie || s->username || s->password || s->proxyusername || | |
975 | s->proxypassword) | |
976 | { | |
977 | return; | |
978 | } | |
979 | ||
980 | pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), s->url); | |
981 | } | |
982 | ||
983 | ||
2654267c HR |
984 | static const char *const curl_strong_runtime_opts[] = { |
985 | CURL_BLOCK_OPT_URL, | |
986 | CURL_BLOCK_OPT_SSLVERIFY, | |
987 | CURL_BLOCK_OPT_COOKIE, | |
988 | CURL_BLOCK_OPT_COOKIE_SECRET, | |
989 | CURL_BLOCK_OPT_USERNAME, | |
990 | CURL_BLOCK_OPT_PASSWORD_SECRET, | |
991 | CURL_BLOCK_OPT_PROXY_USERNAME, | |
992 | CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET, | |
993 | ||
994 | NULL | |
995 | }; | |
996 | ||
769ce76d | 997 | static BlockDriver bdrv_http = { |
63f0f45f SH |
998 | .format_name = "http", |
999 | .protocol_name = "http", | |
1000 | ||
1001 | .instance_size = sizeof(BDRVCURLState), | |
1002 | .bdrv_parse_filename = curl_parse_filename, | |
1003 | .bdrv_file_open = curl_open, | |
1004 | .bdrv_close = curl_close, | |
c86422c5 | 1005 | .bdrv_co_getlength = curl_co_getlength, |
769ce76d | 1006 | |
28256d82 | 1007 | .bdrv_co_preadv = curl_co_preadv, |
769ce76d | 1008 | |
63f0f45f SH |
1009 | .bdrv_detach_aio_context = curl_detach_aio_context, |
1010 | .bdrv_attach_aio_context = curl_attach_aio_context, | |
2654267c | 1011 | |
937c007b | 1012 | .bdrv_refresh_filename = curl_refresh_filename, |
2654267c | 1013 | .strong_runtime_opts = curl_strong_runtime_opts, |
769ce76d AG |
1014 | }; |
1015 | ||
1016 | static BlockDriver bdrv_https = { | |
63f0f45f SH |
1017 | .format_name = "https", |
1018 | .protocol_name = "https", | |
769ce76d | 1019 | |
63f0f45f SH |
1020 | .instance_size = sizeof(BDRVCURLState), |
1021 | .bdrv_parse_filename = curl_parse_filename, | |
1022 | .bdrv_file_open = curl_open, | |
1023 | .bdrv_close = curl_close, | |
c86422c5 | 1024 | .bdrv_co_getlength = curl_co_getlength, |
769ce76d | 1025 | |
28256d82 | 1026 | .bdrv_co_preadv = curl_co_preadv, |
63f0f45f SH |
1027 | |
1028 | .bdrv_detach_aio_context = curl_detach_aio_context, | |
1029 | .bdrv_attach_aio_context = curl_attach_aio_context, | |
2654267c | 1030 | |
937c007b | 1031 | .bdrv_refresh_filename = curl_refresh_filename, |
2654267c | 1032 | .strong_runtime_opts = curl_strong_runtime_opts, |
769ce76d AG |
1033 | }; |
1034 | ||
1035 | static BlockDriver bdrv_ftp = { | |
63f0f45f SH |
1036 | .format_name = "ftp", |
1037 | .protocol_name = "ftp", | |
1038 | ||
1039 | .instance_size = sizeof(BDRVCURLState), | |
1040 | .bdrv_parse_filename = curl_parse_filename, | |
1041 | .bdrv_file_open = curl_open, | |
1042 | .bdrv_close = curl_close, | |
c86422c5 | 1043 | .bdrv_co_getlength = curl_co_getlength, |
769ce76d | 1044 | |
28256d82 | 1045 | .bdrv_co_preadv = curl_co_preadv, |
769ce76d | 1046 | |
63f0f45f SH |
1047 | .bdrv_detach_aio_context = curl_detach_aio_context, |
1048 | .bdrv_attach_aio_context = curl_attach_aio_context, | |
2654267c | 1049 | |
937c007b | 1050 | .bdrv_refresh_filename = curl_refresh_filename, |
2654267c | 1051 | .strong_runtime_opts = curl_strong_runtime_opts, |
769ce76d AG |
1052 | }; |
1053 | ||
1054 | static BlockDriver bdrv_ftps = { | |
63f0f45f SH |
1055 | .format_name = "ftps", |
1056 | .protocol_name = "ftps", | |
769ce76d | 1057 | |
63f0f45f SH |
1058 | .instance_size = sizeof(BDRVCURLState), |
1059 | .bdrv_parse_filename = curl_parse_filename, | |
1060 | .bdrv_file_open = curl_open, | |
1061 | .bdrv_close = curl_close, | |
c86422c5 | 1062 | .bdrv_co_getlength = curl_co_getlength, |
769ce76d | 1063 | |
28256d82 | 1064 | .bdrv_co_preadv = curl_co_preadv, |
63f0f45f SH |
1065 | |
1066 | .bdrv_detach_aio_context = curl_detach_aio_context, | |
1067 | .bdrv_attach_aio_context = curl_attach_aio_context, | |
2654267c | 1068 | |
937c007b | 1069 | .bdrv_refresh_filename = curl_refresh_filename, |
2654267c | 1070 | .strong_runtime_opts = curl_strong_runtime_opts, |
769ce76d AG |
1071 | }; |
1072 | ||
769ce76d AG |
1073 | static void curl_block_init(void) |
1074 | { | |
1075 | bdrv_register(&bdrv_http); | |
1076 | bdrv_register(&bdrv_https); | |
1077 | bdrv_register(&bdrv_ftp); | |
1078 | bdrv_register(&bdrv_ftps); | |
769ce76d AG |
1079 | } |
1080 | ||
1081 | block_init(curl_block_init); |