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.

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