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