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.

546 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 openssl or gnutls enabled.\n");
  242. return AVERROR_PROTOCOL_NOT_FOUND;
  243. }
  244. int ffurl_open(URLContext **puc, const char *filename, int flags,
  245. const AVIOInterruptCB *int_cb, AVDictionary **options)
  246. {
  247. int ret = ffurl_alloc(puc, filename, flags, int_cb);
  248. if (ret < 0)
  249. return ret;
  250. if (options && (*puc)->prot->priv_data_class &&
  251. (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
  252. goto fail;
  253. if ((ret = av_opt_set_dict(*puc, options)) < 0)
  254. goto fail;
  255. ret = ffurl_connect(*puc, options);
  256. if (!ret)
  257. return 0;
  258. fail:
  259. ffurl_close(*puc);
  260. *puc = NULL;
  261. return ret;
  262. }
  263. static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
  264. int size, int size_min,
  265. int (*transfer_func)(URLContext *h,
  266. uint8_t *buf,
  267. int size))
  268. {
  269. int ret, len;
  270. int fast_retries = 5;
  271. int64_t wait_since = 0;
  272. len = 0;
  273. while (len < size_min) {
  274. if (ff_check_interrupt(&h->interrupt_callback))
  275. return AVERROR_EXIT;
  276. ret = transfer_func(h, buf + len, size - len);
  277. if (ret == AVERROR(EINTR))
  278. continue;
  279. if (h->flags & AVIO_FLAG_NONBLOCK)
  280. return ret;
  281. if (ret == AVERROR(EAGAIN)) {
  282. ret = 0;
  283. if (fast_retries) {
  284. fast_retries--;
  285. } else {
  286. if (h->rw_timeout) {
  287. if (!wait_since)
  288. wait_since = av_gettime_relative();
  289. else if (av_gettime_relative() > wait_since + h->rw_timeout)
  290. return AVERROR(EIO);
  291. }
  292. av_usleep(1000);
  293. }
  294. } else if (ret < 1)
  295. return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
  296. if (ret)
  297. fast_retries = FFMAX(fast_retries, 2);
  298. len += ret;
  299. }
  300. return len;
  301. }
  302. int ffurl_read(URLContext *h, unsigned char *buf, int size)
  303. {
  304. if (!(h->flags & AVIO_FLAG_READ))
  305. return AVERROR(EIO);
  306. return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
  307. }
  308. int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
  309. {
  310. if (!(h->flags & AVIO_FLAG_READ))
  311. return AVERROR(EIO);
  312. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
  313. }
  314. int ffurl_write(URLContext *h, const unsigned char *buf, int size)
  315. {
  316. if (!(h->flags & AVIO_FLAG_WRITE))
  317. return AVERROR(EIO);
  318. /* avoid sending too big packets */
  319. if (h->max_packet_size && size > h->max_packet_size)
  320. return AVERROR(EIO);
  321. return retry_transfer_wrapper(h, (unsigned char *)buf, size, size,
  322. (int (*)(struct URLContext *, uint8_t *, int))
  323. h->prot->url_write);
  324. }
  325. int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
  326. {
  327. int64_t ret;
  328. if (!h->prot->url_seek)
  329. return AVERROR(ENOSYS);
  330. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  331. return ret;
  332. }
  333. int ffurl_closep(URLContext **hh)
  334. {
  335. URLContext *h= *hh;
  336. int ret = 0;
  337. if (!h)
  338. return 0; /* can happen when ffurl_open fails */
  339. if (h->is_connected && h->prot->url_close)
  340. ret = h->prot->url_close(h);
  341. #if CONFIG_NETWORK
  342. if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
  343. ff_network_close();
  344. #endif
  345. if (h->prot->priv_data_size) {
  346. if (h->prot->priv_data_class)
  347. av_opt_free(h->priv_data);
  348. av_freep(&h->priv_data);
  349. }
  350. av_freep(hh);
  351. return ret;
  352. }
  353. int ffurl_close(URLContext *h)
  354. {
  355. return ffurl_closep(&h);
  356. }
  357. const char *avio_find_protocol_name(const char *url)
  358. {
  359. URLProtocol *p = url_find_protocol(url);
  360. return p ? p->name : NULL;
  361. }
  362. int avio_check(const char *url, int flags)
  363. {
  364. URLContext *h;
  365. int ret = ffurl_alloc(&h, url, flags, NULL);
  366. if (ret < 0)
  367. return ret;
  368. if (h->prot->url_check) {
  369. ret = h->prot->url_check(h, flags);
  370. } else {
  371. ret = ffurl_connect(h, NULL);
  372. if (ret >= 0)
  373. ret = flags;
  374. }
  375. ffurl_close(h);
  376. return ret;
  377. }
  378. int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options)
  379. {
  380. URLContext *h = NULL;
  381. AVIODirContext *ctx = NULL;
  382. int ret;
  383. av_assert0(s);
  384. ctx = av_mallocz(sizeof(*ctx));
  385. if (!ctx) {
  386. ret = AVERROR(ENOMEM);
  387. goto fail;
  388. }
  389. if ((ret = ffurl_alloc(&h, url, AVIO_FLAG_READ, NULL)) < 0)
  390. goto fail;
  391. if (h->prot->url_open_dir && h->prot->url_read_dir && h->prot->url_close_dir) {
  392. if (options && h->prot->priv_data_class &&
  393. (ret = av_opt_set_dict(h->priv_data, options)) < 0)
  394. goto fail;
  395. ret = h->prot->url_open_dir(h);
  396. } else
  397. ret = AVERROR(ENOSYS);
  398. if (ret < 0)
  399. goto fail;
  400. ctx->url_context = h;
  401. *s = ctx;
  402. return 0;
  403. fail:
  404. av_free(ctx);
  405. *s = NULL;
  406. ffurl_close(h);
  407. return ret;
  408. }
  409. int avio_read_dir(AVIODirContext *s, AVIODirEntry **next)
  410. {
  411. URLContext *h;
  412. int ret;
  413. if (!s || !s->url_context)
  414. return AVERROR(EINVAL);
  415. h = s->url_context;
  416. if ((ret = h->prot->url_read_dir(h, next)) < 0)
  417. avio_free_directory_entry(next);
  418. return ret;
  419. }
  420. int avio_close_dir(AVIODirContext **s)
  421. {
  422. URLContext *h;
  423. av_assert0(s);
  424. if (!(*s) || !(*s)->url_context)
  425. return AVERROR(EINVAL);
  426. h = (*s)->url_context;
  427. h->prot->url_close_dir(h);
  428. ffurl_close(h);
  429. av_freep(s);
  430. *s = NULL;
  431. return 0;
  432. }
  433. void avio_free_directory_entry(AVIODirEntry **entry)
  434. {
  435. if (!entry || !*entry)
  436. return;
  437. av_free((*entry)->name);
  438. av_freep(entry);
  439. }
  440. int64_t ffurl_size(URLContext *h)
  441. {
  442. int64_t pos, size;
  443. size = ffurl_seek(h, 0, AVSEEK_SIZE);
  444. if (size < 0) {
  445. pos = ffurl_seek(h, 0, SEEK_CUR);
  446. if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
  447. return size;
  448. size++;
  449. ffurl_seek(h, pos, SEEK_SET);
  450. }
  451. return size;
  452. }
  453. int ffurl_get_file_handle(URLContext *h)
  454. {
  455. if (!h->prot->url_get_file_handle)
  456. return -1;
  457. return h->prot->url_get_file_handle(h);
  458. }
  459. int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
  460. {
  461. if (!h->prot->url_get_multi_file_handle) {
  462. if (!h->prot->url_get_file_handle)
  463. return AVERROR(ENOSYS);
  464. *handles = av_malloc(sizeof(**handles));
  465. if (!*handles)
  466. return AVERROR(ENOMEM);
  467. *numhandles = 1;
  468. *handles[0] = h->prot->url_get_file_handle(h);
  469. return 0;
  470. }
  471. return h->prot->url_get_multi_file_handle(h, handles, numhandles);
  472. }
  473. int ffurl_shutdown(URLContext *h, int flags)
  474. {
  475. if (!h->prot->url_shutdown)
  476. return AVERROR(EINVAL);
  477. return h->prot->url_shutdown(h, flags);
  478. }
  479. int ff_check_interrupt(AVIOInterruptCB *cb)
  480. {
  481. int ret;
  482. if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
  483. return ret;
  484. return 0;
  485. }