You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

660 lines
19KB

  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. #include "libavutil/avstring.h"
  22. #include "libavutil/dict.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/time.h"
  25. #include "libavutil/avassert.h"
  26. #include "os_support.h"
  27. #include "avformat.h"
  28. #if CONFIG_NETWORK
  29. #include "network.h"
  30. #endif
  31. #include "url.h"
  32. /** @name Logging context. */
  33. /*@{*/
  34. static const char *urlcontext_to_name(void *ptr)
  35. {
  36. URLContext *h = (URLContext *)ptr;
  37. if (h->prot)
  38. return h->prot->name;
  39. else
  40. return "NULL";
  41. }
  42. static void *urlcontext_child_next(void *obj, void *prev)
  43. {
  44. URLContext *h = obj;
  45. if (!prev && h->priv_data && h->prot->priv_data_class)
  46. return h->priv_data;
  47. return NULL;
  48. }
  49. #define OFFSET(x) offsetof(URLContext,x)
  50. #define E AV_OPT_FLAG_ENCODING_PARAM
  51. #define D AV_OPT_FLAG_DECODING_PARAM
  52. static const AVOption options[] = {
  53. {"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 },
  54. {"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 },
  55. {"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 },
  56. { NULL }
  57. };
  58. const AVClass ffurl_context_class = {
  59. .class_name = "URLContext",
  60. .item_name = urlcontext_to_name,
  61. .option = options,
  62. .version = LIBAVUTIL_VERSION_INT,
  63. .child_next = urlcontext_child_next,
  64. .child_class_next = ff_urlcontext_child_class_next,
  65. };
  66. /*@}*/
  67. static int url_alloc_for_protocol(URLContext **puc, const URLProtocol *up,
  68. const char *filename, int flags,
  69. const AVIOInterruptCB *int_cb)
  70. {
  71. URLContext *uc;
  72. int err;
  73. #if CONFIG_NETWORK
  74. if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
  75. return AVERROR(EIO);
  76. #endif
  77. if ((flags & AVIO_FLAG_READ) && !up->url_read) {
  78. av_log(NULL, AV_LOG_ERROR,
  79. "Impossible to open the '%s' protocol for reading\n", up->name);
  80. return AVERROR(EIO);
  81. }
  82. if ((flags & AVIO_FLAG_WRITE) && !up->url_write) {
  83. av_log(NULL, AV_LOG_ERROR,
  84. "Impossible to open the '%s' protocol for writing\n", up->name);
  85. return AVERROR(EIO);
  86. }
  87. uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
  88. if (!uc) {
  89. err = AVERROR(ENOMEM);
  90. goto fail;
  91. }
  92. uc->av_class = &ffurl_context_class;
  93. uc->filename = (char *)&uc[1];
  94. strcpy(uc->filename, filename);
  95. uc->prot = up;
  96. uc->flags = flags;
  97. uc->is_streamed = 0; /* default = not streamed */
  98. uc->max_packet_size = 0; /* default: stream file */
  99. if (up->priv_data_size) {
  100. uc->priv_data = av_mallocz(up->priv_data_size);
  101. if (!uc->priv_data) {
  102. err = AVERROR(ENOMEM);
  103. goto fail;
  104. }
  105. if (up->priv_data_class) {
  106. int proto_len= strlen(up->name);
  107. char *start = strchr(uc->filename, ',');
  108. *(const AVClass **)uc->priv_data = up->priv_data_class;
  109. av_opt_set_defaults(uc->priv_data);
  110. if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
  111. int ret= 0;
  112. char *p= start;
  113. char sep= *++p;
  114. char *key, *val;
  115. p++;
  116. if (strcmp(up->name, "subfile"))
  117. ret = AVERROR(EINVAL);
  118. while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
  119. *val= *key= 0;
  120. if (strcmp(p, "start") && strcmp(p, "end")) {
  121. ret = AVERROR_OPTION_NOT_FOUND;
  122. } else
  123. ret= av_opt_set(uc->priv_data, p, key+1, 0);
  124. if (ret == AVERROR_OPTION_NOT_FOUND)
  125. av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
  126. *val= *key= sep;
  127. p= val+1;
  128. }
  129. if(ret<0 || p!=key){
  130. av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
  131. av_freep(&uc->priv_data);
  132. av_freep(&uc);
  133. err = AVERROR(EINVAL);
  134. goto fail;
  135. }
  136. memmove(start, key+1, strlen(key));
  137. }
  138. }
  139. }
  140. if (int_cb)
  141. uc->interrupt_callback = *int_cb;
  142. *puc = uc;
  143. return 0;
  144. fail:
  145. *puc = NULL;
  146. if (uc)
  147. av_freep(&uc->priv_data);
  148. av_freep(&uc);
  149. #if CONFIG_NETWORK
  150. if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
  151. ff_network_close();
  152. #endif
  153. return err;
  154. }
  155. int ffurl_connect(URLContext *uc, AVDictionary **options)
  156. {
  157. int err;
  158. AVDictionary *tmp_opts = NULL;
  159. AVDictionaryEntry *e;
  160. if (!options)
  161. options = &tmp_opts;
  162. // Check that URLContext was initialized correctly and lists are matching if set
  163. av_assert0(!(e=av_dict_get(*options, "protocol_whitelist", NULL, 0)) ||
  164. (uc->protocol_whitelist && !strcmp(uc->protocol_whitelist, e->value)));
  165. av_assert0(!(e=av_dict_get(*options, "protocol_blacklist", NULL, 0)) ||
  166. (uc->protocol_blacklist && !strcmp(uc->protocol_blacklist, e->value)));
  167. if (uc->protocol_whitelist && av_match_list(uc->prot->name, uc->protocol_whitelist, ',') <= 0) {
  168. av_log(uc, AV_LOG_ERROR, "Protocol not on whitelist \'%s\'!\n", uc->protocol_whitelist);
  169. return AVERROR(EINVAL);
  170. }
  171. if (uc->protocol_blacklist && av_match_list(uc->prot->name, uc->protocol_blacklist, ',') > 0) {
  172. av_log(uc, AV_LOG_ERROR, "Protocol blacklisted \'%s\'!\n", uc->protocol_blacklist);
  173. return AVERROR(EINVAL);
  174. }
  175. if (!uc->protocol_whitelist && uc->prot->default_whitelist) {
  176. av_log(uc, AV_LOG_DEBUG, "Setting default whitelist '%s'\n", uc->prot->default_whitelist);
  177. uc->protocol_whitelist = av_strdup(uc->prot->default_whitelist);
  178. if (!uc->protocol_whitelist) {
  179. return AVERROR(ENOMEM);
  180. }
  181. } else if (!uc->protocol_whitelist)
  182. av_log(uc, AV_LOG_DEBUG, "No default whitelist set\n"); // This should be an error once all declare a default whitelist
  183. if ((err = av_dict_set(options, "protocol_whitelist", uc->protocol_whitelist, 0)) < 0)
  184. return err;
  185. if ((err = av_dict_set(options, "protocol_blacklist", uc->protocol_blacklist, 0)) < 0)
  186. return err;
  187. err =
  188. uc->prot->url_open2 ? uc->prot->url_open2(uc,
  189. uc->filename,
  190. uc->flags,
  191. options) :
  192. uc->prot->url_open(uc, uc->filename, uc->flags);
  193. av_dict_set(options, "protocol_whitelist", NULL, 0);
  194. av_dict_set(options, "protocol_blacklist", NULL, 0);
  195. if (err)
  196. return err;
  197. uc->is_connected = 1;
  198. /* We must be careful here as ffurl_seek() could be slow,
  199. * for example for http */
  200. if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
  201. if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
  202. uc->is_streamed = 1;
  203. return 0;
  204. }
  205. int ffurl_accept(URLContext *s, URLContext **c)
  206. {
  207. av_assert0(!*c);
  208. if (s->prot->url_accept)
  209. return s->prot->url_accept(s, c);
  210. return AVERROR(EBADF);
  211. }
  212. int ffurl_handshake(URLContext *c)
  213. {
  214. int ret;
  215. if (c->prot->url_handshake) {
  216. ret = c->prot->url_handshake(c);
  217. if (ret)
  218. return ret;
  219. }
  220. c->is_connected = 1;
  221. return 0;
  222. }
  223. #define URL_SCHEME_CHARS \
  224. "abcdefghijklmnopqrstuvwxyz" \
  225. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  226. "0123456789+-."
  227. static const struct URLProtocol *url_find_protocol(const char *filename)
  228. {
  229. const URLProtocol **protocols;
  230. char proto_str[128], proto_nested[128], *ptr;
  231. size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
  232. int i;
  233. if (filename[proto_len] != ':' &&
  234. (strncmp(filename, "subfile,", 8) || !strchr(filename + proto_len + 1, ':')) ||
  235. is_dos_path(filename))
  236. strcpy(proto_str, "file");
  237. else
  238. av_strlcpy(proto_str, filename,
  239. FFMIN(proto_len + 1, sizeof(proto_str)));
  240. if ((ptr = strchr(proto_str, ',')))
  241. *ptr = '\0';
  242. av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
  243. if ((ptr = strchr(proto_nested, '+')))
  244. *ptr = '\0';
  245. protocols = ffurl_get_protocols(NULL, NULL);
  246. for (i = 0; protocols[i]; i++) {
  247. const URLProtocol *up = protocols[i];
  248. if (!strcmp(proto_str, up->name)) {
  249. av_freep(&protocols);
  250. return up;
  251. }
  252. if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
  253. !strcmp(proto_nested, up->name)) {
  254. av_freep(&protocols);
  255. return up;
  256. }
  257. }
  258. av_freep(&protocols);
  259. return NULL;
  260. }
  261. int ffurl_alloc(URLContext **puc, const char *filename, int flags,
  262. const AVIOInterruptCB *int_cb)
  263. {
  264. const URLProtocol *p = NULL;
  265. p = url_find_protocol(filename);
  266. if (p)
  267. return url_alloc_for_protocol(puc, p, filename, flags, int_cb);
  268. *puc = NULL;
  269. if (av_strstart(filename, "https:", NULL))
  270. av_log(NULL, AV_LOG_WARNING, "https protocol not found, recompile FFmpeg with "
  271. "openssl, gnutls "
  272. "or securetransport enabled.\n");
  273. return AVERROR_PROTOCOL_NOT_FOUND;
  274. }
  275. int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags,
  276. const AVIOInterruptCB *int_cb, AVDictionary **options,
  277. const char *whitelist, const char* blacklist,
  278. URLContext *parent)
  279. {
  280. AVDictionary *tmp_opts = NULL;
  281. AVDictionaryEntry *e;
  282. int ret = ffurl_alloc(puc, filename, flags, int_cb);
  283. if (ret < 0)
  284. return ret;
  285. if (parent)
  286. av_opt_copy(*puc, parent);
  287. if (options &&
  288. (ret = av_opt_set_dict(*puc, options)) < 0)
  289. goto fail;
  290. if (options && (*puc)->prot->priv_data_class &&
  291. (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
  292. goto fail;
  293. if (!options)
  294. options = &tmp_opts;
  295. av_assert0(!whitelist ||
  296. !(e=av_dict_get(*options, "protocol_whitelist", NULL, 0)) ||
  297. !strcmp(whitelist, e->value));
  298. av_assert0(!blacklist ||
  299. !(e=av_dict_get(*options, "protocol_blacklist", NULL, 0)) ||
  300. !strcmp(blacklist, e->value));
  301. if ((ret = av_dict_set(options, "protocol_whitelist", whitelist, 0)) < 0)
  302. goto fail;
  303. if ((ret = av_dict_set(options, "protocol_blacklist", blacklist, 0)) < 0)
  304. goto fail;
  305. if ((ret = av_opt_set_dict(*puc, options)) < 0)
  306. goto fail;
  307. ret = ffurl_connect(*puc, options);
  308. if (!ret)
  309. return 0;
  310. fail:
  311. ffurl_close(*puc);
  312. *puc = NULL;
  313. return ret;
  314. }
  315. int ffurl_open(URLContext **puc, const char *filename, int flags,
  316. const AVIOInterruptCB *int_cb, AVDictionary **options)
  317. {
  318. return ffurl_open_whitelist(puc, filename, flags,
  319. int_cb, options, NULL, NULL, NULL);
  320. }
  321. static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
  322. int size, int size_min,
  323. int (*transfer_func)(URLContext *h,
  324. uint8_t *buf,
  325. int size))
  326. {
  327. int ret, len;
  328. int fast_retries = 5;
  329. int64_t wait_since = 0;
  330. len = 0;
  331. while (len < size_min) {
  332. if (ff_check_interrupt(&h->interrupt_callback))
  333. return AVERROR_EXIT;
  334. ret = transfer_func(h, buf + len, size - len);
  335. if (ret == AVERROR(EINTR))
  336. continue;
  337. if (h->flags & AVIO_FLAG_NONBLOCK)
  338. return ret;
  339. if (ret == AVERROR(EAGAIN)) {
  340. ret = 0;
  341. if (fast_retries) {
  342. fast_retries--;
  343. } else {
  344. if (h->rw_timeout) {
  345. if (!wait_since)
  346. wait_since = av_gettime_relative();
  347. else if (av_gettime_relative() > wait_since + h->rw_timeout)
  348. return AVERROR(EIO);
  349. }
  350. av_usleep(1000);
  351. }
  352. } else if (ret < 1)
  353. return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
  354. if (ret) {
  355. fast_retries = FFMAX(fast_retries, 2);
  356. wait_since = 0;
  357. }
  358. len += ret;
  359. }
  360. return len;
  361. }
  362. int ffurl_read(URLContext *h, unsigned char *buf, int size)
  363. {
  364. if (!(h->flags & AVIO_FLAG_READ))
  365. return AVERROR(EIO);
  366. return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
  367. }
  368. int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
  369. {
  370. if (!(h->flags & AVIO_FLAG_READ))
  371. return AVERROR(EIO);
  372. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
  373. }
  374. int ffurl_write(URLContext *h, const unsigned char *buf, int size)
  375. {
  376. if (!(h->flags & AVIO_FLAG_WRITE))
  377. return AVERROR(EIO);
  378. /* avoid sending too big packets */
  379. if (h->max_packet_size && size > h->max_packet_size)
  380. return AVERROR(EIO);
  381. return retry_transfer_wrapper(h, (unsigned char *)buf, size, size,
  382. (int (*)(struct URLContext *, uint8_t *, int))
  383. h->prot->url_write);
  384. }
  385. int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
  386. {
  387. int64_t ret;
  388. if (!h->prot->url_seek)
  389. return AVERROR(ENOSYS);
  390. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  391. return ret;
  392. }
  393. int ffurl_closep(URLContext **hh)
  394. {
  395. URLContext *h= *hh;
  396. int ret = 0;
  397. if (!h)
  398. return 0; /* can happen when ffurl_open fails */
  399. if (h->is_connected && h->prot->url_close)
  400. ret = h->prot->url_close(h);
  401. #if CONFIG_NETWORK
  402. if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
  403. ff_network_close();
  404. #endif
  405. if (h->prot->priv_data_size) {
  406. if (h->prot->priv_data_class)
  407. av_opt_free(h->priv_data);
  408. av_freep(&h->priv_data);
  409. }
  410. av_opt_free(h);
  411. av_freep(hh);
  412. return ret;
  413. }
  414. int ffurl_close(URLContext *h)
  415. {
  416. return ffurl_closep(&h);
  417. }
  418. const char *avio_find_protocol_name(const char *url)
  419. {
  420. const URLProtocol *p = url_find_protocol(url);
  421. return p ? p->name : NULL;
  422. }
  423. int avio_check(const char *url, int flags)
  424. {
  425. URLContext *h;
  426. int ret = ffurl_alloc(&h, url, flags, NULL);
  427. if (ret < 0)
  428. return ret;
  429. if (h->prot->url_check) {
  430. ret = h->prot->url_check(h, flags);
  431. } else {
  432. ret = ffurl_connect(h, NULL);
  433. if (ret >= 0)
  434. ret = flags;
  435. }
  436. ffurl_close(h);
  437. return ret;
  438. }
  439. int avpriv_io_move(const char *url_src, const char *url_dst)
  440. {
  441. URLContext *h_src, *h_dst;
  442. int ret = ffurl_alloc(&h_src, url_src, AVIO_FLAG_READ_WRITE, NULL);
  443. if (ret < 0)
  444. return ret;
  445. ret = ffurl_alloc(&h_dst, url_dst, AVIO_FLAG_WRITE, NULL);
  446. if (ret < 0) {
  447. ffurl_close(h_src);
  448. return ret;
  449. }
  450. if (h_src->prot == h_dst->prot && h_src->prot->url_move)
  451. ret = h_src->prot->url_move(h_src, h_dst);
  452. else
  453. ret = AVERROR(ENOSYS);
  454. ffurl_close(h_src);
  455. ffurl_close(h_dst);
  456. return ret;
  457. }
  458. int avpriv_io_delete(const char *url)
  459. {
  460. URLContext *h;
  461. int ret = ffurl_alloc(&h, url, AVIO_FLAG_WRITE, NULL);
  462. if (ret < 0)
  463. return ret;
  464. if (h->prot->url_delete)
  465. ret = h->prot->url_delete(h);
  466. else
  467. ret = AVERROR(ENOSYS);
  468. ffurl_close(h);
  469. return ret;
  470. }
  471. int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options)
  472. {
  473. URLContext *h = NULL;
  474. AVIODirContext *ctx = NULL;
  475. int ret;
  476. av_assert0(s);
  477. ctx = av_mallocz(sizeof(*ctx));
  478. if (!ctx) {
  479. ret = AVERROR(ENOMEM);
  480. goto fail;
  481. }
  482. if ((ret = ffurl_alloc(&h, url, AVIO_FLAG_READ, NULL)) < 0)
  483. goto fail;
  484. if (h->prot->url_open_dir && h->prot->url_read_dir && h->prot->url_close_dir) {
  485. if (options && h->prot->priv_data_class &&
  486. (ret = av_opt_set_dict(h->priv_data, options)) < 0)
  487. goto fail;
  488. ret = h->prot->url_open_dir(h);
  489. } else
  490. ret = AVERROR(ENOSYS);
  491. if (ret < 0)
  492. goto fail;
  493. h->is_connected = 1;
  494. ctx->url_context = h;
  495. *s = ctx;
  496. return 0;
  497. fail:
  498. av_free(ctx);
  499. *s = NULL;
  500. ffurl_close(h);
  501. return ret;
  502. }
  503. int avio_read_dir(AVIODirContext *s, AVIODirEntry **next)
  504. {
  505. URLContext *h;
  506. int ret;
  507. if (!s || !s->url_context)
  508. return AVERROR(EINVAL);
  509. h = s->url_context;
  510. if ((ret = h->prot->url_read_dir(h, next)) < 0)
  511. avio_free_directory_entry(next);
  512. return ret;
  513. }
  514. int avio_close_dir(AVIODirContext **s)
  515. {
  516. URLContext *h;
  517. av_assert0(s);
  518. if (!(*s) || !(*s)->url_context)
  519. return AVERROR(EINVAL);
  520. h = (*s)->url_context;
  521. h->prot->url_close_dir(h);
  522. ffurl_close(h);
  523. av_freep(s);
  524. *s = NULL;
  525. return 0;
  526. }
  527. void avio_free_directory_entry(AVIODirEntry **entry)
  528. {
  529. if (!entry || !*entry)
  530. return;
  531. av_free((*entry)->name);
  532. av_freep(entry);
  533. }
  534. int64_t ffurl_size(URLContext *h)
  535. {
  536. int64_t pos, size;
  537. size = ffurl_seek(h, 0, AVSEEK_SIZE);
  538. if (size < 0) {
  539. pos = ffurl_seek(h, 0, SEEK_CUR);
  540. if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
  541. return size;
  542. size++;
  543. ffurl_seek(h, pos, SEEK_SET);
  544. }
  545. return size;
  546. }
  547. int ffurl_get_file_handle(URLContext *h)
  548. {
  549. if (!h->prot->url_get_file_handle)
  550. return -1;
  551. return h->prot->url_get_file_handle(h);
  552. }
  553. int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
  554. {
  555. if (!h->prot->url_get_multi_file_handle) {
  556. if (!h->prot->url_get_file_handle)
  557. return AVERROR(ENOSYS);
  558. *handles = av_malloc(sizeof(**handles));
  559. if (!*handles)
  560. return AVERROR(ENOMEM);
  561. *numhandles = 1;
  562. *handles[0] = h->prot->url_get_file_handle(h);
  563. return 0;
  564. }
  565. return h->prot->url_get_multi_file_handle(h, handles, numhandles);
  566. }
  567. int ffurl_shutdown(URLContext *h, int flags)
  568. {
  569. if (!h->prot->url_shutdown)
  570. return AVERROR(EINVAL);
  571. return h->prot->url_shutdown(h, flags);
  572. }
  573. int ff_check_interrupt(AVIOInterruptCB *cb)
  574. {
  575. int ret;
  576. if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
  577. return ret;
  578. return 0;
  579. }