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"
34 /** @name Logging context. */
36 static const char *urlcontext_to_name(void *ptr)
38 URLContext *h = (URLContext *)ptr;
45 static void *urlcontext_child_next(void *obj, void *prev)
48 if (!prev && h->priv_data && h->prot->priv_data_class)
53 #define OFFSET(x) offsetof(URLContext,x)
54 #define E AV_OPT_FLAG_ENCODING_PARAM
55 #define D AV_OPT_FLAG_DECODING_PARAM
56 static const AVOption options[] = {
57 {"protocol_whitelist", "List of protocols that are allowed to be used", OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, D },
58 {"protocol_blacklist", "List of protocols that are not allowed to be used", OFFSET(protocol_blacklist), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, D },
59 {"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 },
63 const AVClass ffurl_context_class = {
64 .class_name = "URLContext",
65 .item_name = urlcontext_to_name,
67 .version = LIBAVUTIL_VERSION_INT,
68 .child_next = urlcontext_child_next,
69 .child_class_next = ff_urlcontext_child_class_next,
73 static int url_alloc_for_protocol(URLContext **puc, const URLProtocol *up,
74 const char *filename, int flags,
75 const AVIOInterruptCB *int_cb)
81 if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
84 if ((flags & AVIO_FLAG_READ) && !up->url_read) {
85 av_log(NULL, AV_LOG_ERROR,
86 "Impossible to open the '%s' protocol for reading\n", up->name);
89 if ((flags & AVIO_FLAG_WRITE) && !up->url_write) {
90 av_log(NULL, AV_LOG_ERROR,
91 "Impossible to open the '%s' protocol for writing\n", up->name);
94 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
96 err = AVERROR(ENOMEM);
99 uc->av_class = &ffurl_context_class;
100 uc->filename = (char *)&uc[1];
101 strcpy(uc->filename, filename);
104 uc->is_streamed = 0; /* default = not streamed */
105 uc->max_packet_size = 0; /* default: stream file */
106 if (up->priv_data_size) {
107 uc->priv_data = av_mallocz(up->priv_data_size);
108 if (!uc->priv_data) {
109 err = AVERROR(ENOMEM);
112 if (up->priv_data_class) {
113 int proto_len= strlen(up->name);
114 char *start = strchr(uc->filename, ',');
115 *(const AVClass **)uc->priv_data = up->priv_data_class;
116 av_opt_set_defaults(uc->priv_data);
117 if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
124 if (strcmp(up->name, "subfile"))
125 ret = AVERROR(EINVAL);
127 while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
129 if (strcmp(p, "start") && strcmp(p, "end")) {
130 ret = AVERROR_OPTION_NOT_FOUND;
132 ret= av_opt_set(uc->priv_data, p, key+1, 0);
133 if (ret == AVERROR_OPTION_NOT_FOUND)
134 av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
139 av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
140 av_freep(&uc->priv_data);
142 err = AVERROR(EINVAL);
145 memmove(start, key+1, strlen(key));
150 uc->interrupt_callback = *int_cb;
157 av_freep(&uc->priv_data);
160 if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
166 int ffurl_connect(URLContext *uc, AVDictionary **options)
169 AVDictionary *tmp_opts = NULL;
170 AVDictionaryEntry *e;
175 // Check that URLContext was initialized correctly and lists are matching if set
176 av_assert0(!(e=av_dict_get(*options, "protocol_whitelist", NULL, 0)) ||
177 (uc->protocol_whitelist && !strcmp(uc->protocol_whitelist, e->value)));
178 av_assert0(!(e=av_dict_get(*options, "protocol_blacklist", NULL, 0)) ||
179 (uc->protocol_blacklist && !strcmp(uc->protocol_blacklist, e->value)));
181 if (uc->protocol_whitelist && av_match_list(uc->prot->name, uc->protocol_whitelist, ',') <= 0) {
182 av_log(uc, AV_LOG_ERROR, "Protocol not on whitelist \'%s\'!\n", uc->protocol_whitelist);
183 return AVERROR(EINVAL);
186 if (uc->protocol_blacklist && av_match_list(uc->prot->name, uc->protocol_blacklist, ',') > 0) {
187 av_log(uc, AV_LOG_ERROR, "Protocol blacklisted \'%s\'!\n", uc->protocol_blacklist);
188 return AVERROR(EINVAL);
191 if (!uc->protocol_whitelist && uc->prot->default_whitelist) {
192 av_log(uc, AV_LOG_DEBUG, "Setting default whitelist '%s'\n", uc->prot->default_whitelist);
193 uc->protocol_whitelist = av_strdup(uc->prot->default_whitelist);
194 if (!uc->protocol_whitelist) {
195 return AVERROR(ENOMEM);
197 } else if (!uc->protocol_whitelist)
198 av_log(uc, AV_LOG_DEBUG, "No default whitelist set\n"); // This should be an error once all declare a default whitelist
200 if ((err = av_dict_set(options, "protocol_whitelist", uc->protocol_whitelist, 0)) < 0)
202 if ((err = av_dict_set(options, "protocol_blacklist", uc->protocol_blacklist, 0)) < 0)
206 uc->prot->url_open2 ? uc->prot->url_open2(uc,
210 uc->prot->url_open(uc, uc->filename, uc->flags);
212 av_dict_set(options, "protocol_whitelist", NULL, 0);
213 av_dict_set(options, "protocol_blacklist", NULL, 0);
217 uc->is_connected = 1;
218 /* We must be careful here as ffurl_seek() could be slow,
219 * for example for http */
220 if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
221 if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
226 int ffurl_accept(URLContext *s, URLContext **c)
229 if (s->prot->url_accept)
230 return s->prot->url_accept(s, c);
231 return AVERROR(EBADF);
234 int ffurl_handshake(URLContext *c)
237 if (c->prot->url_handshake) {
238 ret = c->prot->url_handshake(c);
246 #define URL_SCHEME_CHARS \
247 "abcdefghijklmnopqrstuvwxyz" \
248 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
251 static const struct URLProtocol *url_find_protocol(const char *filename)
253 const URLProtocol **protocols;
254 char proto_str[128], proto_nested[128], *ptr;
255 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
258 if (filename[proto_len] != ':' &&
259 (strncmp(filename, "subfile,", 8) || !strchr(filename + proto_len + 1, ':')) ||
260 is_dos_path(filename))
261 strcpy(proto_str, "file");
263 av_strlcpy(proto_str, filename,
264 FFMIN(proto_len + 1, sizeof(proto_str)));
266 if ((ptr = strchr(proto_str, ',')))
268 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
269 if ((ptr = strchr(proto_nested, '+')))
272 protocols = ffurl_get_protocols(NULL, NULL);
273 for (i = 0; protocols[i]; i++) {
274 const URLProtocol *up = protocols[i];
275 if (!strcmp(proto_str, up->name)) {
276 av_freep(&protocols);
279 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
280 !strcmp(proto_nested, up->name)) {
281 av_freep(&protocols);
289 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
290 const AVIOInterruptCB *int_cb)
292 const URLProtocol *p = NULL;
294 p = url_find_protocol(filename);
296 return url_alloc_for_protocol(puc, p, filename, flags, int_cb);
299 if (av_strstart(filename, "https:", NULL))
300 av_log(NULL, AV_LOG_WARNING, "https protocol not found, recompile FFmpeg with "
302 "or securetransport enabled.\n");
303 return AVERROR_PROTOCOL_NOT_FOUND;
306 int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags,
307 const AVIOInterruptCB *int_cb, AVDictionary **options,
308 const char *whitelist, const char* blacklist)
310 AVDictionary *tmp_opts = NULL;
311 AVDictionaryEntry *e;
312 int ret = ffurl_alloc(puc, filename, flags, int_cb);
316 (ret = av_opt_set_dict(*puc, options)) < 0)
318 if (options && (*puc)->prot->priv_data_class &&
319 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
325 av_assert0(!whitelist ||
326 !(e=av_dict_get(*options, "protocol_whitelist", NULL, 0)) ||
327 !strcmp(whitelist, e->value));
328 av_assert0(!blacklist ||
329 !(e=av_dict_get(*options, "protocol_blacklist", NULL, 0)) ||
330 !strcmp(blacklist, e->value));
332 if ((ret = av_dict_set(options, "protocol_whitelist", whitelist, 0)) < 0)
335 if ((ret = av_dict_set(options, "protocol_blacklist", blacklist, 0)) < 0)
338 if ((ret = av_opt_set_dict(*puc, options)) < 0)
341 ret = ffurl_connect(*puc, options);
351 int ffurl_open(URLContext **puc, const char *filename, int flags,
352 const AVIOInterruptCB *int_cb, AVDictionary **options)
354 return ffurl_open_whitelist(puc, filename, flags,
355 int_cb, options, NULL, NULL);
358 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
359 int size, int size_min,
360 int (*transfer_func)(URLContext *h,
365 int fast_retries = 5;
366 int64_t wait_since = 0;
369 while (len < size_min) {
370 if (ff_check_interrupt(&h->interrupt_callback))
372 ret = transfer_func(h, buf + len, size - len);
373 if (ret == AVERROR(EINTR))
375 if (h->flags & AVIO_FLAG_NONBLOCK)
377 if (ret == AVERROR(EAGAIN)) {
384 wait_since = av_gettime_relative();
385 else if (av_gettime_relative() > wait_since + h->rw_timeout)
391 return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
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->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)
629 if (!h->prot->url_get_multi_file_handle) {
630 if (!h->prot->url_get_file_handle)
631 return AVERROR(ENOSYS);
632 *handles = av_malloc(sizeof(**handles));
634 return AVERROR(ENOMEM);
636 *handles[0] = h->prot->url_get_file_handle(h);
639 return h->prot->url_get_multi_file_handle(h, handles, numhandles);
642 int ffurl_shutdown(URLContext *h, int flags)
644 if (!h->prot->url_shutdown)
645 return AVERROR(EINVAL);
646 return h->prot->url_shutdown(h, flags);
649 int ff_check_interrupt(AVIOInterruptCB *cb)
652 if (cb && cb->callback && (ret = cb->callback(cb->opaque)))