1 /*
2 * unbuffered I/O
3 * Copyright (c) 2001 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
29 #if CONFIG_NETWORK
31 #endif
33
34 /** @name Logging context. */
35 /*@{*/
37 {
41 else
42 return "NULL";
43 }
44
46 {
51 }
52
53 #define OFFSET(x) offsetof(URLContext,x)
54 #define E AV_OPT_FLAG_ENCODING_PARAM
55 #define D AV_OPT_FLAG_DECODING_PARAM
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 },
61 };
62
70 };
71 /*@}*/
72
74 const char *filename,
int flags,
76 {
78 int err;
79
80 #if CONFIG_NETWORK
83 #endif
86 "Impossible to open the '%s' protocol for reading\n", up->
name);
88 }
91 "Impossible to open the '%s' protocol for writing\n", up->
name);
93 }
95 if (!uc) {
98 }
111 }
113 int proto_len= strlen(up->
name);
118 int ret= 0;
120 char sep= *++p;
122 p++;
123
124 if (strcmp(up->
name,
"subfile"))
126
127 while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
128 *val= *key= 0;
129 if (strcmp(p, "start") && strcmp(p, "end")) {
131 } else
135 *val= *key= sep;
136 p= val+1;
137 }
138 if(ret<0 || p!=key){
144 }
145 memmove(start, key+1, strlen(key));
146 }
147 }
148 }
149 if (int_cb)
151
152 *puc = uc;
153 return 0;
156 if (uc)
159 #if CONFIG_NETWORK
162 #endif
163 return err;
164 }
165
167 {
168 int err;
171
172 if (!options)
173 options = &tmp_opts;
174
175 // Check that URLContext was initialized correctly and lists are matching if set
180
184 }
185
189 }
190
196 }
198 av_log(uc,
AV_LOG_DEBUG,
"No default whitelist set\n");
// This should be an error once all declare a default whitelist
199
201 return err;
203 return err;
204
205 err =
209 options) :
211
214
215 if (err)
216 return err;
218 /* We must be careful here as ffurl_seek() could be slow,
219 * for example for http */
223 return 0;
224 }
225
227 {
232 }
233
235 {
236 int ret;
239 if (ret)
240 return ret;
241 }
243 return 0;
244 }
245
246 #define URL_SCHEME_CHARS \
247 "abcdefghijklmnopqrstuvwxyz" \
248 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
249 "0123456789+-."
250
252 {
254 char proto_str[128], proto_nested[128], *ptr;
256 int i;
257
258 if (filename[proto_len] != ':' &&
259 (strncmp(filename, "subfile,", 8) || !strchr(filename + proto_len + 1, ':')) ||
261 strcpy(proto_str, "file");
262 else
264 FFMIN(proto_len + 1,
sizeof(proto_str)));
265
266 av_strlcpy(proto_nested, proto_str,
sizeof(proto_nested));
267 if ((ptr = strchr(proto_nested, '+')))
268 *ptr = '0円';
269
271 if (!protocols)
273 for (i = 0; protocols[i]; i++) {
275 if (!strcmp(proto_str, up->
name)) {
277 return up;
278 }
280 !strcmp(proto_nested, up->
name)) {
282 return up;
283 }
284 }
286
288 }
289
292 {
294
296 if (p)
298
302 "openssl, gnutls "
303 "or securetransport enabled.\n");
305 }
306
309 const char *whitelist, const char* blacklist,
311 {
314 int ret =
ffurl_alloc(puc, filename, flags, int_cb);
315 if (ret < 0)
316 return ret;
317 if (parent)
319 if (options &&
322 if (options && (*puc)->prot->priv_data_class &&
325
326 if (!options)
327 options = &tmp_opts;
328
331 !strcmp(whitelist, e->
value));
334 !strcmp(blacklist, e->
value));
335
336 if ((ret =
av_dict_set(options,
"protocol_whitelist", whitelist, 0)) < 0)
338
339 if ((ret =
av_dict_set(options,
"protocol_blacklist", blacklist, 0)) < 0)
341
344
346
347 if (!ret)
348 return 0;
352 return ret;
353 }
354
357 {
360 }
361
363 int size,
int size_min,
366 int size))
367 {
369 int fast_retries = 5;
370 int64_t wait_since = 0;
371
372 len = 0;
373 while (len < size_min) {
376 ret = transfer_func(h, buf + len, size - len);
378 continue;
380 return ret;
382 ret = 0;
383 if (fast_retries) {
384 fast_retries--;
385 } else {
387 if (!wait_since)
391 }
393 }
396 else if (ret < 0)
397 return ret;
398 if (ret) {
399 fast_retries =
FFMAX(fast_retries, 2);
400 wait_since = 0;
401 }
402 len += ret;
403 }
405 }
406
408 {
412 }
413
415 {
419 }
420
422 {
425 /* avoid sending too big packets */
428
432 }
433
435 {
436 int64_t ret;
437
441 return ret;
442 }
443
445 {
447 int ret = 0;
448 if (!h)
449 return 0; /* can happen when ffurl_open fails */
450
453 #if CONFIG_NETWORK
456 #endif
461 }
464 return ret;
465 }
466
468 {
470 }
471
472
474 {
476
478 }
479
481 {
484 if (ret < 0)
485 return ret;
486
489 } else {
491 if (ret >= 0)
493 }
494
496 return ret;
497 }
498
500 {
503 if (ret < 0)
504 return ret;
506 if (ret < 0) {
508 return ret;
509 }
510
513 else
515
518 return ret;
519 }
520
522 {
525 if (ret < 0)
526 return ret;
527
530 else
532
534 return ret;
535 }
536
538 {
541 int ret;
543
545 if (!ctx) {
548 }
549
552
558 } else
560 if (ret < 0)
562
566 return 0;
567
572 return ret;
573 }
574
576 {
578 int ret;
579
585 return ret;
586 }
587
589 {
591
593 if (!(*s) || !(*s)->url_context)
595 h = (*s)->url_context;
600 return 0;
601 }
602
604 {
605 if (!entry || !*entry)
606 return;
609 }
610
612 {
614
616 if (size < 0) {
620 size++;
622 }
624 }
625
627 {
629 return -1;
631 }
632
634 {
641 if (!*handles)
643 *numhandles = 1;
645 return 0;
646 }
648 }
649
651 {
655 }
656
658 {
662 }
663
665 {
666 int ret;
668 return ret;
669 return 0;
670 }
int(* url_open_dir)(URLContext *h)
const char const char void * val
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
#define URL_PROTOCOL_FLAG_NETWORK
int(* url_close_dir)(URLContext *h)
int(* url_check)(URLContext *h, int mask)
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
int is_streamed
true if streamed (no seek possible), default = false
int ffurl_connect(URLContext *uc, AVDictionary **options)
Connect an URLContext that has been allocated by ffurl_alloc.
AVIOInterruptCB interrupt_callback
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Describes single entry of the directory.
#define AVIO_FLAG_READ
read-only
int64_t rw_timeout
maximum time to wait for (network) read/write operation completion, in mcs
#define AVIO_FLAG_WRITE
write-only
void ff_network_close(void)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
int av_usleep(unsigned usec)
Sleep for a period of time.
static void * urlcontext_child_next(void *obj, void *prev)
static int url_alloc_for_protocol(URLContext **puc, const URLProtocol *up, const char *filename, int flags, const AVIOInterruptCB *int_cb)
int ffurl_shutdown(URLContext *h, int flags)
Signal the URLContext that we are done reading or writing the stream.
int avpriv_io_move(const char *url_src, const char *url_dst)
Move or rename a resource.
const AVClass * priv_data_class
int avio_check(const char *url, int flags)
Return AVIO_FLAG_* access flags corresponding to the access permissions of the resource in url...
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static double cb(void *priv, double x, double y)
int ff_network_init(void)
miscellaneous OS support macros and functions.
int avio_read_dir(AVIODirContext *s, AVIODirEntry **next)
Get next directory entry.
int(* url_get_file_handle)(URLContext *h)
#define AVERROR_PROTOCOL_NOT_FOUND
Protocol not found.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
const AVClass * ff_urlcontext_child_class_next(const AVClass *prev)
int(* url_get_multi_file_handle)(URLContext *h, int **handles, int *numhandles)
#define AVERROR_EOF
End of file.
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
int(* url_move)(URLContext *h_src, URLContext *h_dst)
Callback for checking whether to abort blocking functions.
int ffurl_alloc(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb)
Create a URLContext for accessing to the resource indicated by url, but do not initiate the connectio...
int(* url_open2)(URLContext *h, const char *url, int flags, AVDictionary **options)
This callback is to be used by protocols which open further nested protocols.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
Return the file descriptors associated with this URL.
const AVIOInterruptCB int_cb
const char * protocol_whitelist
int av_match_list(const char *name, const char *list, char separator)
Check if a name is in a list.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define URL_PROTOCOL_FLAG_NESTED_SCHEME
simple assert() macros that are a bit more flexible than ISO C assert().
int(* url_open)(URLContext *h, const char *url, int flags)
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
static const char * urlcontext_to_name(void *ptr)
void avio_free_directory_entry(AVIODirEntry **entry)
Free entry allocated by avio_read_dir().
int(* url_write)(URLContext *h, const unsigned char *buf, int size)
int(* url_read)(URLContext *h, unsigned char *buf, int size)
Read data from the protocol.
static const AVOption options[]
static int retry_transfer_wrapper(URLContext *h, uint8_t *buf, int size, int size_min, int(*transfer_func)(URLContext *h, uint8_t *buf, int size))
int ffurl_get_short_seek(URLContext *h)
Return the current short seek threshold value for this URL.
int ffurl_handshake(URLContext *c)
Perform one step of the protocol handshake to accept a new client.
int64_t(* url_seek)(URLContext *h, int64_t pos, int whence)
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
int ffurl_accept(URLContext *s, URLContext **c)
Accept an URLContext c on an URLContext s.
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
char * av_strdup(const char *s)
Duplicate a string.
static int is_dos_path(const char *path)
const char * protocol_blacklist
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrupt a blocking function associated with cb.
int av_opt_set_dict(void *obj, AVDictionary **options)
Set all the options from a given dictionary on an object.
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
const char * default_whitelist
int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options)
Open directory for reading.
int64_t ffurl_size(URLContext *h)
Return the filesize of the resource accessed by h, AVERROR(ENOSYS) if the operation is not supported ...
#define AVIO_FLAG_READ_WRITE
read-write pseudo flag
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Describe the class of an AVClass context structure.
#define AVSEEK_FORCE
Passing this flag as the "whence" parameter to a seek function causes it to seek by any means (like r...
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
int(* url_handshake)(URLContext *c)
int(* url_read_dir)(URLContext *h, AVIODirEntry **next)
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
const AVClass * av_class
information for av_log().
int ffurl_close(URLContext *h)
int(* url_accept)(URLContext *s, URLContext **c)
const AVClass ffurl_context_class
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
void av_opt_free(void *obj)
Free all allocated objects in obj.
int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
Change the position that will be used by the next read/write operation on the resource accessed by h...
const struct URLProtocol * prot
int ffurl_open(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
int(* url_delete)(URLContext *h)
int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
Read as many bytes as possible (up to size), calling the read function multiple times if necessary...
char * filename
specified URL
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
#define AVSEEK_SIZE
ORing this as the "whence" parameter to a seek function causes it to return the filesize without seek...
int avio_close_dir(AVIODirContext **s)
Close directory.
#define AVERROR_OPTION_NOT_FOUND
Option not found.
int avpriv_io_delete(const char *url)
Delete a resource.
int max_packet_size
if non zero, the stream is packetized with this max packet size
int(* url_close)(URLContext *h)
unbuffered private I/O API
int(* url_get_short_seek)(URLContext *h)
static const struct URLProtocol * url_find_protocol(const char *filename)
int(* url_shutdown)(URLContext *h, int flags)
struct URLContext * url_context
const URLProtocol ** ffurl_get_protocols(const char *whitelist, const char *blacklist)
Construct a list of protocols matching a given whitelist and/or blacklist.
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf...