3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/avstring.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/time.h"
26 #include "libavutil/avassert.h"
27 #include "os_support.h"
35 /** @name Logging context. */
37 static const char *urlcontext_to_name(void *ptr)
39 URLContext *h = (URLContext *)ptr;
46 static void *urlcontext_child_next(void *obj, void *prev)
49 if (!prev && h->priv_data && h->prot->priv_data_class)
54 #define OFFSET(x) offsetof(URLContext,x)
55 #define E AV_OPT_FLAG_ENCODING_PARAM
56 #define D AV_OPT_FLAG_DECODING_PARAM
57 static const AVOption options[] = {
58 {"protocol_whitelist", "List of protocols that are allowed to be used", OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
59 {"protocol_blacklist", "List of protocols that are not allowed to be used", OFFSET(protocol_blacklist), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
60 {"rw_timeout", "Timeout for IO operations (in microseconds)", offsetof(URLContext, rw_timeout), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_DECODING_PARAM },
64 const AVClass ffurl_context_class = {
65 .class_name = "URLContext",
66 .item_name = urlcontext_to_name,
68 .version = LIBAVUTIL_VERSION_INT,
69 .child_next = urlcontext_child_next,
70 #if FF_API_CHILD_CLASS_NEXT
71 .child_class_next = ff_urlcontext_child_class_next,
73 .child_class_iterate = ff_urlcontext_child_class_iterate,
77 static int url_alloc_for_protocol(URLContext **puc, const URLProtocol *up,
78 const char *filename, int flags,
79 const AVIOInterruptCB *int_cb)
85 if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
88 if ((flags & AVIO_FLAG_READ) && !up->url_read) {
89 av_log(NULL, AV_LOG_ERROR,
90 "Impossible to open the '%s' protocol for reading\n", up->name);
93 if ((flags & AVIO_FLAG_WRITE) && !up->url_write) {
94 av_log(NULL, AV_LOG_ERROR,
95 "Impossible to open the '%s' protocol for writing\n", up->name);
98 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
100 err = AVERROR(ENOMEM);
103 uc->av_class = &ffurl_context_class;
104 uc->filename = (char *)&uc[1];
105 strcpy(uc->filename, filename);
108 uc->is_streamed = 0; /* default = not streamed */
109 uc->max_packet_size = 0; /* default: stream file */
110 if (up->priv_data_size) {
111 uc->priv_data = av_mallocz(up->priv_data_size);
112 if (!uc->priv_data) {
113 err = AVERROR(ENOMEM);
116 if (up->priv_data_class) {
118 *(const AVClass **)uc->priv_data = up->priv_data_class;
119 av_opt_set_defaults(uc->priv_data);
120 if (av_strstart(uc->filename, up->name, (const char**)&start) && *start == ',') {
127 if (strcmp(up->name, "subfile"))
128 ret = AVERROR(EINVAL);
130 while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
132 if (strcmp(p, "start") && strcmp(p, "end")) {
133 ret = AVERROR_OPTION_NOT_FOUND;
135 ret= av_opt_set(uc->priv_data, p, key+1, 0);
136 if (ret == AVERROR_OPTION_NOT_FOUND)
137 av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
142 av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
143 av_freep(&uc->priv_data);
145 err = AVERROR(EINVAL);
148 memmove(start, key+1, strlen(key));
153 uc->interrupt_callback = *int_cb;
160 av_freep(&uc->priv_data);
163 if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
169 int ffurl_connect(URLContext *uc, AVDictionary **options)
172 AVDictionary *tmp_opts = NULL;
173 AVDictionaryEntry *e;
178 // Check that URLContext was initialized correctly and lists are matching if set
179 av_assert0(!(e=av_dict_get(*options, "protocol_whitelist", NULL, 0)) ||
180 (uc->protocol_whitelist && !strcmp(uc->protocol_whitelist, e->value)));
181 av_assert0(!(e=av_dict_get(*options, "protocol_blacklist", NULL, 0)) ||
182 (uc->protocol_blacklist && !strcmp(uc->protocol_blacklist, e->value)));
184 if (uc->protocol_whitelist && av_match_list(uc->prot->name, uc->protocol_whitelist, ',') <= 0) {
185 av_log(uc, AV_LOG_ERROR, "Protocol '%s' not on whitelist '%s'!\n", uc->prot->name, uc->protocol_whitelist);
186 return AVERROR(EINVAL);
189 if (uc->protocol_blacklist && av_match_list(uc->prot->name, uc->protocol_blacklist, ',') > 0) {
190 av_log(uc, AV_LOG_ERROR, "Protocol '%s' on blacklist '%s'!\n", uc->prot->name, uc->protocol_blacklist);
191 return AVERROR(EINVAL);
194 if (!uc->protocol_whitelist && uc->prot->default_whitelist) {
195 av_log(uc, AV_LOG_DEBUG, "Setting default whitelist '%s'\n", uc->prot->default_whitelist);
196 uc->protocol_whitelist = av_strdup(uc->prot->default_whitelist);
197 if (!uc->protocol_whitelist) {
198 return AVERROR(ENOMEM);
200 } else if (!uc->protocol_whitelist)
201 av_log(uc, AV_LOG_DEBUG, "No default whitelist set\n"); // This should be an error once all declare a default whitelist
203 if ((err = av_dict_set(options, "protocol_whitelist", uc->protocol_whitelist, 0)) < 0)
205 if ((err = av_dict_set(options, "protocol_blacklist", uc->protocol_blacklist, 0)) < 0)
209 uc->prot->url_open2 ? uc->prot->url_open2(uc,
213 uc->prot->url_open(uc, uc->filename, uc->flags);
215 av_dict_set(options, "protocol_whitelist", NULL, 0);
216 av_dict_set(options, "protocol_blacklist", NULL, 0);
220 uc->is_connected = 1;
221 /* We must be careful here as ffurl_seek() could be slow,
222 * for example for http */
223 if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
224 if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
229 int ffurl_accept(URLContext *s, URLContext **c)
232 if (s->prot->url_accept)
233 return s->prot->url_accept(s, c);
234 return AVERROR(EBADF);
237 int ffurl_handshake(URLContext *c)
240 if (c->prot->url_handshake) {
241 ret = c->prot->url_handshake(c);
249 #define URL_SCHEME_CHARS \
250 "abcdefghijklmnopqrstuvwxyz" \
251 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
254 static const struct URLProtocol *url_find_protocol(const char *filename)
256 const URLProtocol **protocols;
257 char proto_str[128], proto_nested[128], *ptr;
258 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
261 if (filename[proto_len] != ':' &&
262 (strncmp(filename, "subfile,", 8) || !strchr(filename + proto_len + 1, ':')) ||
263 is_dos_path(filename))
264 strcpy(proto_str, "file");
266 av_strlcpy(proto_str, filename,
267 FFMIN(proto_len + 1, sizeof(proto_str)));
269 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
270 if ((ptr = strchr(proto_nested, '+')))
273 protocols = ffurl_get_protocols(NULL, NULL);
276 for (i = 0; protocols[i]; i++) {
277 const URLProtocol *up = protocols[i];
278 if (!strcmp(proto_str, up->name)) {
279 av_freep(&protocols);
282 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
283 !strcmp(proto_nested, up->name)) {
284 av_freep(&protocols);
288 av_freep(&protocols);
289 if (av_strstart(filename, "https:", NULL) || av_strstart(filename, "tls:", NULL))
290 av_log(NULL, AV_LOG_WARNING, "https protocol not found, recompile FFmpeg with "
291 "openssl, gnutls or securetransport enabled.\n");
296 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
297 const AVIOInterruptCB *int_cb)
299 const URLProtocol *p = NULL;
301 p = url_find_protocol(filename);
303 return url_alloc_for_protocol(puc, p, filename, flags, int_cb);
306 return AVERROR_PROTOCOL_NOT_FOUND;
309 int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags,
310 const AVIOInterruptCB *int_cb, AVDictionary **options,
311 const char *whitelist, const char* blacklist,
314 AVDictionary *tmp_opts = NULL;
315 AVDictionaryEntry *e;
316 int ret = ffurl_alloc(puc, filename, flags, int_cb);
320 av_opt_copy(*puc, parent);
322 (ret = av_opt_set_dict(*puc, options)) < 0)
324 if (options && (*puc)->prot->priv_data_class &&
325 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
331 av_assert0(!whitelist ||
332 !(e=av_dict_get(*options, "protocol_whitelist", NULL, 0)) ||
333 !strcmp(whitelist, e->value));
334 av_assert0(!blacklist ||
335 !(e=av_dict_get(*options, "protocol_blacklist", NULL, 0)) ||
336 !strcmp(blacklist, e->value));
338 if ((ret = av_dict_set(options, "protocol_whitelist", whitelist, 0)) < 0)
341 if ((ret = av_dict_set(options, "protocol_blacklist", blacklist, 0)) < 0)
344 if ((ret = av_opt_set_dict(*puc, options)) < 0)
347 ret = ffurl_connect(*puc, options);
356 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
357 int size, int size_min,
358 int (*transfer_func)(URLContext *h,
363 int fast_retries = 5;
364 int64_t wait_since = 0;
367 while (len < size_min) {
368 if (ff_check_interrupt(&h->interrupt_callback))
370 ret = transfer_func(h, buf + len, size - len);
371 if (ret == AVERROR(EINTR))
373 if (h->flags & AVIO_FLAG_NONBLOCK)
375 if (ret == AVERROR(EAGAIN)) {
382 wait_since = av_gettime_relative();
383 else if (av_gettime_relative() > wait_since + h->rw_timeout)
388 } else if (ret == AVERROR_EOF)
389 return (len > 0) ? len : AVERROR_EOF;
393 fast_retries = FFMAX(fast_retries, 2);
401 int ffurl_read(URLContext *h, unsigned char *buf, int size)
403 if (!(h->flags & AVIO_FLAG_READ))
405 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
408 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
410 if (!(h->flags & AVIO_FLAG_READ))
412 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
415 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
417 if (!(h->flags & AVIO_FLAG_WRITE))
419 /* avoid sending too big packets */
420 if (h->max_packet_size && size > h->max_packet_size)
423 return retry_transfer_wrapper(h, (unsigned char *)buf, size, size,
424 (int (*)(struct URLContext *, uint8_t *, int))
428 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
432 if (!h->prot->url_seek)
433 return AVERROR(ENOSYS);
434 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
438 int ffurl_closep(URLContext **hh)
443 return 0; /* can happen when ffurl_open fails */
445 if (h->is_connected && h->prot->url_close)
446 ret = h->prot->url_close(h);
448 if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
451 if (h->prot->priv_data_size) {
452 if (h->prot->priv_data_class)
453 av_opt_free(h->priv_data);
454 av_freep(&h->priv_data);
461 int ffurl_close(URLContext *h)
463 return ffurl_closep(&h);
467 const char *avio_find_protocol_name(const char *url)
469 const URLProtocol *p = url_find_protocol(url);
471 return p ? p->name : NULL;
474 int avio_check(const char *url, int flags)
477 int ret = ffurl_alloc(&h, url, flags, NULL);
481 if (h->prot->url_check) {
482 ret = h->prot->url_check(h, flags);
484 ret = ffurl_connect(h, NULL);
493 int avpriv_io_move(const char *url_src, const char *url_dst)
495 URLContext *h_src, *h_dst;
496 int ret = ffurl_alloc(&h_src, url_src, AVIO_FLAG_READ_WRITE, NULL);
499 ret = ffurl_alloc(&h_dst, url_dst, AVIO_FLAG_WRITE, NULL);
505 if (h_src->prot == h_dst->prot && h_src->prot->url_move)
506 ret = h_src->prot->url_move(h_src, h_dst);
508 ret = AVERROR(ENOSYS);
515 int avpriv_io_delete(const char *url)
518 int ret = ffurl_alloc(&h, url, AVIO_FLAG_WRITE, NULL);
522 if (h->prot->url_delete)
523 ret = h->prot->url_delete(h);
525 ret = AVERROR(ENOSYS);
531 int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options)
533 URLContext *h = NULL;
534 AVIODirContext *ctx = NULL;
538 ctx = av_mallocz(sizeof(*ctx));
540 ret = AVERROR(ENOMEM);
544 if ((ret = ffurl_alloc(&h, url, AVIO_FLAG_READ, NULL)) < 0)
547 if (h->prot->url_open_dir && h->prot->url_read_dir && h->prot->url_close_dir) {
548 if (options && h->prot->priv_data_class &&
549 (ret = av_opt_set_dict(h->priv_data, options)) < 0)
551 ret = h->prot->url_open_dir(h);
553 ret = AVERROR(ENOSYS);
558 ctx->url_context = h;
569 int avio_read_dir(AVIODirContext *s, AVIODirEntry **next)
574 if (!s || !s->url_context)
575 return AVERROR(EINVAL);
577 if ((ret = h->prot->url_read_dir(h, next)) < 0)
578 avio_free_directory_entry(next);
582 int avio_close_dir(AVIODirContext **s)
587 if (!(*s) || !(*s)->url_context)
588 return AVERROR(EINVAL);
589 h = (*s)->url_context;
590 h->prot->url_close_dir(h);
597 void avio_free_directory_entry(AVIODirEntry **entry)
599 if (!entry || !*entry)
601 av_free((*entry)->name);
605 int64_t ffurl_size(URLContext *h)
609 size = ffurl_seek(h, 0, AVSEEK_SIZE);
611 pos = ffurl_seek(h, 0, SEEK_CUR);
612 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
615 ffurl_seek(h, pos, SEEK_SET);
620 int ffurl_get_file_handle(URLContext *h)
622 if (!h || !h->prot || !h->prot->url_get_file_handle)
624 return h->prot->url_get_file_handle(h);
627 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
630 return AVERROR(ENOSYS);
631 if (!h->prot->url_get_multi_file_handle) {
632 if (!h->prot->url_get_file_handle)
633 return AVERROR(ENOSYS);
634 *handles = av_malloc(sizeof(**handles));
636 return AVERROR(ENOMEM);
638 *handles[0] = h->prot->url_get_file_handle(h);
641 return h->prot->url_get_multi_file_handle(h, handles, numhandles);
644 int ffurl_get_short_seek(URLContext *h)
646 if (!h || !h->prot || !h->prot->url_get_short_seek)
647 return AVERROR(ENOSYS);
648 return h->prot->url_get_short_seek(h);
651 int ffurl_shutdown(URLContext *h, int flags)
653 if (!h || !h->prot || !h->prot->url_shutdown)
654 return AVERROR(ENOSYS);
655 return h->prot->url_shutdown(h, flags);
658 int ff_check_interrupt(AVIOInterruptCB *cb)
660 if (cb && cb->callback)
661 return cb->callback(cb->opaque);
665 int ff_rename(const char *url_src, const char *url_dst, void *logctx)
667 int ret = avpriv_io_move(url_src, url_dst);
669 av_log(logctx, AV_LOG_ERROR, "failed to rename file %s to %s: %s\n", url_src, url_dst, av_err2str(ret));