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) {
117 int proto_len= strlen(up->name);
118 char *start = strchr(uc->filename, ',');
119 *(const AVClass **)uc->priv_data = up->priv_data_class;
120 av_opt_set_defaults(uc->priv_data);
121 if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
128 if (strcmp(up->name, "subfile"))
129 ret = AVERROR(EINVAL);
131 while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
133 if (strcmp(p, "start") && strcmp(p, "end")) {
134 ret = AVERROR_OPTION_NOT_FOUND;
136 ret= av_opt_set(uc->priv_data, p, key+1, 0);
137 if (ret == AVERROR_OPTION_NOT_FOUND)
138 av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
143 av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
144 av_freep(&uc->priv_data);
146 err = AVERROR(EINVAL);
149 memmove(start, key+1, strlen(key));
154 uc->interrupt_callback = *int_cb;
161 av_freep(&uc->priv_data);
164 if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
170 int ffurl_connect(URLContext *uc, AVDictionary **options)
173 AVDictionary *tmp_opts = NULL;
174 AVDictionaryEntry *e;
179 // Check that URLContext was initialized correctly and lists are matching if set
180 av_assert0(!(e=av_dict_get(*options, "protocol_whitelist", NULL, 0)) ||
181 (uc->protocol_whitelist && !strcmp(uc->protocol_whitelist, e->value)));
182 av_assert0(!(e=av_dict_get(*options, "protocol_blacklist", NULL, 0)) ||
183 (uc->protocol_blacklist && !strcmp(uc->protocol_blacklist, e->value)));
185 if (uc->protocol_whitelist && av_match_list(uc->prot->name, uc->protocol_whitelist, ',') <= 0) {
186 av_log(uc, AV_LOG_ERROR, "Protocol '%s' not on whitelist '%s'!\n", uc->prot->name, uc->protocol_whitelist);
187 return AVERROR(EINVAL);
190 if (uc->protocol_blacklist && av_match_list(uc->prot->name, uc->protocol_blacklist, ',') > 0) {
191 av_log(uc, AV_LOG_ERROR, "Protocol '%s' on blacklist '%s'!\n", uc->prot->name, uc->protocol_blacklist);
192 return AVERROR(EINVAL);
195 if (!uc->protocol_whitelist && uc->prot->default_whitelist) {
196 av_log(uc, AV_LOG_DEBUG, "Setting default whitelist '%s'\n", uc->prot->default_whitelist);
197 uc->protocol_whitelist = av_strdup(uc->prot->default_whitelist);
198 if (!uc->protocol_whitelist) {
199 return AVERROR(ENOMEM);
201 } else if (!uc->protocol_whitelist)
202 av_log(uc, AV_LOG_DEBUG, "No default whitelist set\n"); // This should be an error once all declare a default whitelist
204 if ((err = av_dict_set(options, "protocol_whitelist", uc->protocol_whitelist, 0)) < 0)
206 if ((err = av_dict_set(options, "protocol_blacklist", uc->protocol_blacklist, 0)) < 0)
210 uc->prot->url_open2 ? uc->prot->url_open2(uc,
214 uc->prot->url_open(uc, uc->filename, uc->flags);
216 av_dict_set(options, "protocol_whitelist", NULL, 0);
217 av_dict_set(options, "protocol_blacklist", NULL, 0);
221 uc->is_connected = 1;
222 /* We must be careful here as ffurl_seek() could be slow,
223 * for example for http */
224 if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
225 if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
230 int ffurl_accept(URLContext *s, URLContext **c)
233 if (s->prot->url_accept)
234 return s->prot->url_accept(s, c);
235 return AVERROR(EBADF);
238 int ffurl_handshake(URLContext *c)
241 if (c->prot->url_handshake) {
242 ret = c->prot->url_handshake(c);
250 #define URL_SCHEME_CHARS \
251 "abcdefghijklmnopqrstuvwxyz" \
252 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
255 static const struct URLProtocol *url_find_protocol(const char *filename)
257 const URLProtocol **protocols;
258 char proto_str[128], proto_nested[128], *ptr;
259 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
262 if (filename[proto_len] != ':' &&
263 (strncmp(filename, "subfile,", 8) || !strchr(filename + proto_len + 1, ':')) ||
264 is_dos_path(filename))
265 strcpy(proto_str, "file");
267 av_strlcpy(proto_str, filename,
268 FFMIN(proto_len + 1, sizeof(proto_str)));
270 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
271 if ((ptr = strchr(proto_nested, '+')))
274 protocols = ffurl_get_protocols(NULL, NULL);
277 for (i = 0; protocols[i]; i++) {
278 const URLProtocol *up = protocols[i];
279 if (!strcmp(proto_str, up->name)) {
280 av_freep(&protocols);
283 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
284 !strcmp(proto_nested, up->name)) {
285 av_freep(&protocols);
289 av_freep(&protocols);
290 if (av_strstart(filename, "https:", NULL) || av_strstart(filename, "tls:", NULL))
291 av_log(NULL, AV_LOG_WARNING, "https protocol not found, recompile FFmpeg with "
292 "openssl, gnutls or securetransport enabled.\n");
297 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
298 const AVIOInterruptCB *int_cb)
300 const URLProtocol *p = NULL;
302 p = url_find_protocol(filename);
304 return url_alloc_for_protocol(puc, p, filename, flags, int_cb);
307 return AVERROR_PROTOCOL_NOT_FOUND;
310 int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags,
311 const AVIOInterruptCB *int_cb, AVDictionary **options,
312 const char *whitelist, const char* blacklist,
315 AVDictionary *tmp_opts = NULL;
316 AVDictionaryEntry *e;
317 int ret = ffurl_alloc(puc, filename, flags, int_cb);
321 av_opt_copy(*puc, parent);
323 (ret = av_opt_set_dict(*puc, options)) < 0)
325 if (options && (*puc)->prot->priv_data_class &&
326 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
332 av_assert0(!whitelist ||
333 !(e=av_dict_get(*options, "protocol_whitelist", NULL, 0)) ||
334 !strcmp(whitelist, e->value));
335 av_assert0(!blacklist ||
336 !(e=av_dict_get(*options, "protocol_blacklist", NULL, 0)) ||
337 !strcmp(blacklist, e->value));
339 if ((ret = av_dict_set(options, "protocol_whitelist", whitelist, 0)) < 0)
342 if ((ret = av_dict_set(options, "protocol_blacklist", blacklist, 0)) < 0)
345 if ((ret = av_opt_set_dict(*puc, options)) < 0)
348 ret = ffurl_connect(*puc, options);
357 int ffurl_open(URLContext **puc, const char *filename, int flags,
358 const AVIOInterruptCB *int_cb, AVDictionary **options)
360 return ffurl_open_whitelist(puc, filename, flags,
361 int_cb, options, NULL, NULL, NULL);
364 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
365 int size, int size_min,
366 int (*transfer_func)(URLContext *h,
371 int fast_retries = 5;
372 int64_t wait_since = 0;
375 while (len < size_min) {
376 if (ff_check_interrupt(&h->interrupt_callback))
378 ret = transfer_func(h, buf + len, size - len);
379 if (ret == AVERROR(EINTR))
381 if (h->flags & AVIO_FLAG_NONBLOCK)
383 if (ret == AVERROR(EAGAIN)) {
390 wait_since = av_gettime_relative();
391 else if (av_gettime_relative() > wait_since + h->rw_timeout)
396 } else if (ret == AVERROR_EOF)
397 return (len > 0) ? len : AVERROR_EOF;
401 fast_retries = FFMAX(fast_retries, 2);
409 int ffurl_read(URLContext *h, unsigned char *buf, int size)
411 if (!(h->flags & AVIO_FLAG_READ))
413 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
416 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
418 if (!(h->flags & AVIO_FLAG_READ))
420 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
423 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
425 if (!(h->flags & AVIO_FLAG_WRITE))
427 /* avoid sending too big packets */
428 if (h->max_packet_size && size > h->max_packet_size)
431 return retry_transfer_wrapper(h, (unsigned char *)buf, size, size,
432 (int (*)(struct URLContext *, uint8_t *, int))
436 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
440 if (!h->prot->url_seek)
441 return AVERROR(ENOSYS);
442 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
446 int ffurl_closep(URLContext **hh)
451 return 0; /* can happen when ffurl_open fails */
453 if (h->is_connected && h->prot->url_close)
454 ret = h->prot->url_close(h);
456 if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
459 if (h->prot->priv_data_size) {
460 if (h->prot->priv_data_class)
461 av_opt_free(h->priv_data);
462 av_freep(&h->priv_data);
469 int ffurl_close(URLContext *h)
471 return ffurl_closep(&h);
475 const char *avio_find_protocol_name(const char *url)
477 const URLProtocol *p = url_find_protocol(url);
479 return p ? p->name : NULL;
482 int avio_check(const char *url, int flags)
485 int ret = ffurl_alloc(&h, url, flags, NULL);
489 if (h->prot->url_check) {
490 ret = h->prot->url_check(h, flags);
492 ret = ffurl_connect(h, NULL);
501 int avpriv_io_move(const char *url_src, const char *url_dst)
503 URLContext *h_src, *h_dst;
504 int ret = ffurl_alloc(&h_src, url_src, AVIO_FLAG_READ_WRITE, NULL);
507 ret = ffurl_alloc(&h_dst, url_dst, AVIO_FLAG_WRITE, NULL);
513 if (h_src->prot == h_dst->prot && h_src->prot->url_move)
514 ret = h_src->prot->url_move(h_src, h_dst);
516 ret = AVERROR(ENOSYS);
523 int avpriv_io_delete(const char *url)
526 int ret = ffurl_alloc(&h, url, AVIO_FLAG_WRITE, NULL);
530 if (h->prot->url_delete)
531 ret = h->prot->url_delete(h);
533 ret = AVERROR(ENOSYS);
539 int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options)
541 URLContext *h = NULL;
542 AVIODirContext *ctx = NULL;
546 ctx = av_mallocz(sizeof(*ctx));
548 ret = AVERROR(ENOMEM);
552 if ((ret = ffurl_alloc(&h, url, AVIO_FLAG_READ, NULL)) < 0)
555 if (h->prot->url_open_dir && h->prot->url_read_dir && h->prot->url_close_dir) {
556 if (options && h->prot->priv_data_class &&
557 (ret = av_opt_set_dict(h->priv_data, options)) < 0)
559 ret = h->prot->url_open_dir(h);
561 ret = AVERROR(ENOSYS);
566 ctx->url_context = h;
577 int avio_read_dir(AVIODirContext *s, AVIODirEntry **next)
582 if (!s || !s->url_context)
583 return AVERROR(EINVAL);
585 if ((ret = h->prot->url_read_dir(h, next)) < 0)
586 avio_free_directory_entry(next);
590 int avio_close_dir(AVIODirContext **s)
595 if (!(*s) || !(*s)->url_context)
596 return AVERROR(EINVAL);
597 h = (*s)->url_context;
598 h->prot->url_close_dir(h);
605 void avio_free_directory_entry(AVIODirEntry **entry)
607 if (!entry || !*entry)
609 av_free((*entry)->name);
613 int64_t ffurl_size(URLContext *h)
617 size = ffurl_seek(h, 0, AVSEEK_SIZE);
619 pos = ffurl_seek(h, 0, SEEK_CUR);
620 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
623 ffurl_seek(h, pos, SEEK_SET);
628 int ffurl_get_file_handle(URLContext *h)
630 if (!h || !h->prot || !h->prot->url_get_file_handle)
632 return h->prot->url_get_file_handle(h);
635 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
638 return AVERROR(ENOSYS);
639 if (!h->prot->url_get_multi_file_handle) {
640 if (!h->prot->url_get_file_handle)
641 return AVERROR(ENOSYS);
642 *handles = av_malloc(sizeof(**handles));
644 return AVERROR(ENOMEM);
646 *handles[0] = h->prot->url_get_file_handle(h);
649 return h->prot->url_get_multi_file_handle(h, handles, numhandles);
652 int ffurl_get_short_seek(URLContext *h)
654 if (!h || !h->prot || !h->prot->url_get_short_seek)
655 return AVERROR(ENOSYS);
656 return h->prot->url_get_short_seek(h);
659 int ffurl_shutdown(URLContext *h, int flags)
661 if (!h || !h->prot || !h->prot->url_shutdown)
662 return AVERROR(ENOSYS);
663 return h->prot->url_shutdown(h, flags);
666 int ff_check_interrupt(AVIOInterruptCB *cb)
668 if (cb && cb->callback)
669 return cb->callback(cb->opaque);
673 int ff_rename(const char *url_src, const char *url_dst, void *logctx)
675 int ret = avpriv_io_move(url_src, url_dst);
677 av_log(logctx, AV_LOG_ERROR, "failed to rename file %s to %s: %s\n", url_src, url_dst, av_err2str(ret));