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.

548 lines
15KB

  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. static URLProtocol *first_protocol = NULL;
  33. URLProtocol *ffurl_protocol_next(const URLProtocol *prev)
  34. {
  35. return prev ? prev->next : first_protocol;
  36. }
  37. /** @name Logging context. */
  38. /*@{*/
  39. static const char *urlcontext_to_name(void *ptr)
  40. {
  41. URLContext *h = (URLContext *)ptr;
  42. if (h->prot)
  43. return h->prot->name;
  44. else
  45. return "NULL";
  46. }
  47. static void *urlcontext_child_next(void *obj, void *prev)
  48. {
  49. URLContext *h = obj;
  50. if (!prev && h->priv_data && h->prot->priv_data_class)
  51. return h->priv_data;
  52. return NULL;
  53. }
  54. static const AVClass *urlcontext_child_class_next(const AVClass *prev)
  55. {
  56. URLProtocol *p = NULL;
  57. /* find the protocol that corresponds to prev */
  58. while (prev && (p = ffurl_protocol_next(p)))
  59. if (p->priv_data_class == prev)
  60. break;
  61. /* find next protocol with priv options */
  62. while (p = ffurl_protocol_next(p))
  63. if (p->priv_data_class)
  64. return p->priv_data_class;
  65. return NULL;
  66. }
  67. static const AVOption options[] = { { NULL } };
  68. const AVClass ffurl_context_class = {
  69. .class_name = "URLContext",
  70. .item_name = urlcontext_to_name,
  71. .option = options,
  72. .version = LIBAVUTIL_VERSION_INT,
  73. .child_next = urlcontext_child_next,
  74. .child_class_next = urlcontext_child_class_next,
  75. };
  76. /*@}*/
  77. const char *avio_enum_protocols(void **opaque, int output)
  78. {
  79. URLProtocol *p;
  80. *opaque = ffurl_protocol_next(*opaque);
  81. if (!(p = *opaque))
  82. return NULL;
  83. if ((output && p->url_write) || (!output && p->url_read))
  84. return p->name;
  85. return avio_enum_protocols(opaque, output);
  86. }
  87. int ffurl_register_protocol(URLProtocol *protocol)
  88. {
  89. URLProtocol **p;
  90. p = &first_protocol;
  91. while (*p)
  92. p = &(*p)->next;
  93. *p = protocol;
  94. protocol->next = NULL;
  95. return 0;
  96. }
  97. static int url_alloc_for_protocol(URLContext **puc, struct URLProtocol *up,
  98. const char *filename, int flags,
  99. const AVIOInterruptCB *int_cb)
  100. {
  101. URLContext *uc;
  102. int err;
  103. #if CONFIG_NETWORK
  104. if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
  105. return AVERROR(EIO);
  106. #endif
  107. if ((flags & AVIO_FLAG_READ) && !up->url_read) {
  108. av_log(NULL, AV_LOG_ERROR,
  109. "Impossible to open the '%s' protocol for reading\n", up->name);
  110. return AVERROR(EIO);
  111. }
  112. if ((flags & AVIO_FLAG_WRITE) && !up->url_write) {
  113. av_log(NULL, AV_LOG_ERROR,
  114. "Impossible to open the '%s' protocol for writing\n", up->name);
  115. return AVERROR(EIO);
  116. }
  117. uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
  118. if (!uc) {
  119. err = AVERROR(ENOMEM);
  120. goto fail;
  121. }
  122. uc->av_class = &ffurl_context_class;
  123. uc->filename = (char *)&uc[1];
  124. strcpy(uc->filename, filename);
  125. uc->prot = up;
  126. uc->flags = flags;
  127. uc->is_streamed = 0; /* default = not streamed */
  128. uc->max_packet_size = 0; /* default: stream file */
  129. if (up->priv_data_size) {
  130. uc->priv_data = av_mallocz(up->priv_data_size);
  131. if (!uc->priv_data) {
  132. err = AVERROR(ENOMEM);
  133. goto fail;
  134. }
  135. if (up->priv_data_class) {
  136. int proto_len= strlen(up->name);
  137. char *start = strchr(uc->filename, ',');
  138. *(const AVClass **)uc->priv_data = up->priv_data_class;
  139. av_opt_set_defaults(uc->priv_data);
  140. if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
  141. int ret= 0;
  142. char *p= start;
  143. char sep= *++p;
  144. char *key, *val;
  145. p++;
  146. while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
  147. *val= *key= 0;
  148. ret= av_opt_set(uc->priv_data, p, key+1, 0);
  149. if (ret == AVERROR_OPTION_NOT_FOUND)
  150. av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
  151. *val= *key= sep;
  152. p= val+1;
  153. }
  154. if(ret<0 || p!=key){
  155. av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
  156. av_freep(&uc->priv_data);
  157. av_freep(&uc);
  158. err = AVERROR(EINVAL);
  159. goto fail;
  160. }
  161. memmove(start, key+1, strlen(key));
  162. }
  163. }
  164. }
  165. if (int_cb)
  166. uc->interrupt_callback = *int_cb;
  167. *puc = uc;
  168. return 0;
  169. fail:
  170. *puc = NULL;
  171. if (uc)
  172. av_freep(&uc->priv_data);
  173. av_freep(&uc);
  174. #if CONFIG_NETWORK
  175. if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
  176. ff_network_close();
  177. #endif
  178. return err;
  179. }
  180. int ffurl_connect(URLContext *uc, AVDictionary **options)
  181. {
  182. int err =
  183. uc->prot->url_open2 ? uc->prot->url_open2(uc,
  184. uc->filename,
  185. uc->flags,
  186. options) :
  187. uc->prot->url_open(uc, uc->filename, uc->flags);
  188. if (err)
  189. return err;
  190. uc->is_connected = 1;
  191. /* We must be careful here as ffurl_seek() could be slow,
  192. * for example for http */
  193. if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
  194. if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
  195. uc->is_streamed = 1;
  196. return 0;
  197. }
  198. #define URL_SCHEME_CHARS \
  199. "abcdefghijklmnopqrstuvwxyz" \
  200. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  201. "0123456789+-."
  202. static struct URLProtocol *url_find_protocol(const char *filename)
  203. {
  204. URLProtocol *up = NULL;
  205. char proto_str[128], proto_nested[128], *ptr;
  206. size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
  207. if (filename[proto_len] != ':' &&
  208. (filename[proto_len] != ',' || !strchr(filename + proto_len + 1, ':')) ||
  209. is_dos_path(filename))
  210. strcpy(proto_str, "file");
  211. else
  212. av_strlcpy(proto_str, filename,
  213. FFMIN(proto_len + 1, sizeof(proto_str)));
  214. if ((ptr = strchr(proto_str, ',')))
  215. *ptr = '\0';
  216. av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
  217. if ((ptr = strchr(proto_nested, '+')))
  218. *ptr = '\0';
  219. while (up = ffurl_protocol_next(up)) {
  220. if (!strcmp(proto_str, up->name))
  221. break;
  222. if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
  223. !strcmp(proto_nested, up->name))
  224. break;
  225. }
  226. return up;
  227. }
  228. int ffurl_alloc(URLContext **puc, const char *filename, int flags,
  229. const AVIOInterruptCB *int_cb)
  230. {
  231. URLProtocol *p = NULL;
  232. if (!first_protocol) {
  233. av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
  234. "Missing call to av_register_all()?\n");
  235. }
  236. p = url_find_protocol(filename);
  237. if (p)
  238. return url_alloc_for_protocol(puc, p, filename, flags, int_cb);
  239. *puc = NULL;
  240. if (av_strstart(filename, "https:", NULL))
  241. av_log(NULL, AV_LOG_WARNING, "https protocol not found, recompile with "
  242. "openssl, gnutls,\n"
  243. "or securetransport enabled.\n");
  244. return AVERROR_PROTOCOL_NOT_FOUND;
  245. }
  246. int ffurl_open(URLContext **puc, const char *filename, int flags,
  247. const AVIOInterruptCB *int_cb, AVDictionary **options)
  248. {
  249. int ret = ffurl_alloc(puc, filename, flags, int_cb);
  250. if (ret < 0)
  251. return ret;
  252. if (options && (*puc)->prot->priv_data_class &&
  253. (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
  254. goto fail;
  255. if ((ret = av_opt_set_dict(*puc, options)) < 0)
  256. goto fail;
  257. ret = ffurl_connect(*puc, options);
  258. if (!ret)
  259. return 0;
  260. fail:
  261. ffurl_close(*puc);
  262. *puc = NULL;
  263. return ret;
  264. }
  265. static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
  266. int size, int size_min,
  267. int (*transfer_func)(URLContext *h,
  268. uint8_t *buf,
  269. int size))
  270. {
  271. int ret, len;
  272. int fast_retries = 5;
  273. int64_t wait_since = 0;
  274. len = 0;
  275. while (len < size_min) {
  276. if (ff_check_interrupt(&h->interrupt_callback))
  277. return AVERROR_EXIT;
  278. ret = transfer_func(h, buf + len, size - len);
  279. if (ret == AVERROR(EINTR))
  280. continue;
  281. if (h->flags & AVIO_FLAG_NONBLOCK)
  282. return ret;
  283. if (ret == AVERROR(EAGAIN)) {
  284. ret = 0;
  285. if (fast_retries) {
  286. fast_retries--;
  287. } else {
  288. if (h->rw_timeout) {
  289. if (!wait_since)
  290. wait_since = av_gettime_relative();
  291. else if (av_gettime_relative() > wait_since + h->rw_timeout)
  292. return AVERROR(EIO);
  293. }
  294. av_usleep(1000);
  295. }
  296. } else if (ret < 1)
  297. return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
  298. if (ret)
  299. fast_retries = FFMAX(fast_retries, 2);
  300. len += ret;
  301. }
  302. return len;
  303. }
  304. int ffurl_read(URLContext *h, unsigned char *buf, int size)
  305. {
  306. if (!(h->flags & AVIO_FLAG_READ))
  307. return AVERROR(EIO);
  308. return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
  309. }
  310. int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
  311. {
  312. if (!(h->flags & AVIO_FLAG_READ))
  313. return AVERROR(EIO);
  314. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
  315. }
  316. int ffurl_write(URLContext *h, const unsigned char *buf, int size)
  317. {
  318. if (!(h->flags & AVIO_FLAG_WRITE))
  319. return AVERROR(EIO);
  320. /* avoid sending too big packets */
  321. if (h->max_packet_size && size > h->max_packet_size)
  322. return AVERROR(EIO);
  323. return retry_transfer_wrapper(h, (unsigned char *)buf, size, size,
  324. (int (*)(struct URLContext *, uint8_t *, int))
  325. h->prot->url_write);
  326. }
  327. int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
  328. {
  329. int64_t ret;
  330. if (!h->prot->url_seek)
  331. return AVERROR(ENOSYS);
  332. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  333. return ret;
  334. }
  335. int ffurl_closep(URLContext **hh)
  336. {
  337. URLContext *h= *hh;
  338. int ret = 0;
  339. if (!h)
  340. return 0; /* can happen when ffurl_open fails */
  341. if (h->is_connected && h->prot->url_close)
  342. ret = h->prot->url_close(h);
  343. #if CONFIG_NETWORK
  344. if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
  345. ff_network_close();
  346. #endif
  347. if (h->prot->priv_data_size) {
  348. if (h->prot->priv_data_class)
  349. av_opt_free(h->priv_data);
  350. av_freep(&h->priv_data);
  351. }
  352. av_freep(hh);
  353. return ret;
  354. }
  355. int ffurl_close(URLContext *h)
  356. {
  357. return ffurl_closep(&h);
  358. }
  359. const char *avio_find_protocol_name(const char *url)
  360. {
  361. URLProtocol *p = url_find_protocol(url);
  362. return p ? p->name : NULL;
  363. }
  364. int avio_check(const char *url, int flags)
  365. {
  366. URLContext *h;
  367. int ret = ffurl_alloc(&h, url, flags, NULL);
  368. if (ret < 0)
  369. return ret;
  370. if (h->prot->url_check) {
  371. ret = h->prot->url_check(h, flags);
  372. } else {
  373. ret = ffurl_connect(h, NULL);
  374. if (ret >= 0)
  375. ret = flags;
  376. }
  377. ffurl_close(h);
  378. return ret;
  379. }
  380. int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options)
  381. {
  382. URLContext *h = NULL;
  383. AVIODirContext *ctx = NULL;
  384. int ret;
  385. av_assert0(s);
  386. ctx = av_mallocz(sizeof(*ctx));
  387. if (!ctx) {
  388. ret = AVERROR(ENOMEM);
  389. goto fail;
  390. }
  391. if ((ret = ffurl_alloc(&h, url, AVIO_FLAG_READ, NULL)) < 0)
  392. goto fail;
  393. if (h->prot->url_open_dir && h->prot->url_read_dir && h->prot->url_close_dir) {
  394. if (options && h->prot->priv_data_class &&
  395. (ret = av_opt_set_dict(h->priv_data, options)) < 0)
  396. goto fail;
  397. ret = h->prot->url_open_dir(h);
  398. } else
  399. ret = AVERROR(ENOSYS);
  400. if (ret < 0)
  401. goto fail;
  402. ctx->url_context = h;
  403. *s = ctx;
  404. return 0;
  405. fail:
  406. av_free(ctx);
  407. *s = NULL;
  408. ffurl_close(h);
  409. return ret;
  410. }
  411. int avio_read_dir(AVIODirContext *s, AVIODirEntry **next)
  412. {
  413. URLContext *h;
  414. int ret;
  415. if (!s || !s->url_context)
  416. return AVERROR(EINVAL);
  417. h = s->url_context;
  418. if ((ret = h->prot->url_read_dir(h, next)) < 0)
  419. avio_free_directory_entry(next);
  420. return ret;
  421. }
  422. int avio_close_dir(AVIODirContext **s)
  423. {
  424. URLContext *h;
  425. av_assert0(s);
  426. if (!(*s) || !(*s)->url_context)
  427. return AVERROR(EINVAL);
  428. h = (*s)->url_context;
  429. h->prot->url_close_dir(h);
  430. ffurl_close(h);
  431. av_freep(s);
  432. *s = NULL;
  433. return 0;
  434. }
  435. void avio_free_directory_entry(AVIODirEntry **entry)
  436. {
  437. if (!entry || !*entry)
  438. return;
  439. av_free((*entry)->name);
  440. av_freep(entry);
  441. }
  442. int64_t ffurl_size(URLContext *h)
  443. {
  444. int64_t pos, size;
  445. size = ffurl_seek(h, 0, AVSEEK_SIZE);
  446. if (size < 0) {
  447. pos = ffurl_seek(h, 0, SEEK_CUR);
  448. if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
  449. return size;
  450. size++;
  451. ffurl_seek(h, pos, SEEK_SET);
  452. }
  453. return size;
  454. }
  455. int ffurl_get_file_handle(URLContext *h)
  456. {
  457. if (!h->prot->url_get_file_handle)
  458. return -1;
  459. return h->prot->url_get_file_handle(h);
  460. }
  461. int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
  462. {
  463. if (!h->prot->url_get_multi_file_handle) {
  464. if (!h->prot->url_get_file_handle)
  465. return AVERROR(ENOSYS);
  466. *handles = av_malloc(sizeof(**handles));
  467. if (!*handles)
  468. return AVERROR(ENOMEM);
  469. *numhandles = 1;
  470. *handles[0] = h->prot->url_get_file_handle(h);
  471. return 0;
  472. }
  473. return h->prot->url_get_multi_file_handle(h, handles, numhandles);
  474. }
  475. int ffurl_shutdown(URLContext *h, int flags)
  476. {
  477. if (!h->prot->url_shutdown)
  478. return AVERROR(EINVAL);
  479. return h->prot->url_shutdown(h, flags);
  480. }
  481. int ff_check_interrupt(AVIOInterruptCB *cb)
  482. {
  483. int ret;
  484. if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
  485. return ret;
  486. return 0;
  487. }