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.

145 lines
4.0KB

  1. /*
  2. * Gopher protocol
  3. *
  4. * Copyright (c) 2009 Toshimitsu Kimura
  5. * Copyright (c) 2021 parazyd <parazyd@dyne.org>
  6. *
  7. * based on libavformat/http.c, Copyright (c) 2000, 2001 Fabrice Bellard
  8. *
  9. * This file is part of FFmpeg.
  10. *
  11. * FFmpeg is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * FFmpeg is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with FFmpeg; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include "config.h"
  26. #include "libavutil/avstring.h"
  27. #include "avformat.h"
  28. #include "internal.h"
  29. #include "network.h"
  30. #include "url.h"
  31. typedef struct GopherContext {
  32. URLContext *hd;
  33. } GopherContext;
  34. static int gopher_write(URLContext *h, const uint8_t *buf, int size)
  35. {
  36. GopherContext *s = h->priv_data;
  37. return ffurl_write(s->hd, buf, size);
  38. }
  39. static int gopher_connect(URLContext *h, const char *path)
  40. {
  41. char buffer[1024];
  42. if (!*path) return AVERROR(EINVAL);
  43. switch (*++path) {
  44. case '5':
  45. case '9':
  46. path = strchr(path, '/');
  47. if (!path) return AVERROR(EINVAL);
  48. break;
  49. default:
  50. av_log(h, AV_LOG_WARNING,
  51. "Gopher protocol type '%c' not supported yet!\n",
  52. *path);
  53. return AVERROR(EINVAL);
  54. }
  55. /* send gopher sector */
  56. snprintf(buffer, sizeof(buffer), "%s\r\n", path);
  57. if (gopher_write(h, buffer, strlen(buffer)) < 0)
  58. return AVERROR(EIO);
  59. return 0;
  60. }
  61. static int gopher_close(URLContext *h)
  62. {
  63. GopherContext *s = h->priv_data;
  64. ffurl_closep(&s->hd);
  65. return 0;
  66. }
  67. static int gopher_open(URLContext *h, const char *uri, int flags)
  68. {
  69. GopherContext *s = h->priv_data;
  70. char proto[10], hostname[1024], auth[1024], path[1024], buf[1024];
  71. int port, err;
  72. const char *lower_proto = "tcp";
  73. h->is_streamed = 1;
  74. /* needed in any case to build the host string */
  75. av_url_split(proto, sizeof(proto), auth, sizeof(auth),
  76. hostname, sizeof(hostname), &port, path, sizeof(path), uri);
  77. if (port < 0)
  78. port = 70;
  79. if (!strcmp(proto, "gophers"))
  80. lower_proto = "tls";
  81. ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
  82. s->hd = NULL;
  83. err = ffurl_open_whitelist(&s->hd, buf, AVIO_FLAG_READ_WRITE,
  84. &h->interrupt_callback, NULL, h->protocol_whitelist, h->protocol_blacklist, h);
  85. if (err < 0)
  86. goto fail;
  87. if ((err = gopher_connect(h, path)) < 0)
  88. goto fail;
  89. return 0;
  90. fail:
  91. gopher_close(h);
  92. return err;
  93. }
  94. static int gopher_read(URLContext *h, uint8_t *buf, int size)
  95. {
  96. GopherContext *s = h->priv_data;
  97. int len = ffurl_read(s->hd, buf, size);
  98. return len;
  99. }
  100. #if CONFIG_GOPHER_PROTOCOL
  101. const URLProtocol ff_gopher_protocol = {
  102. .name = "gopher",
  103. .url_open = gopher_open,
  104. .url_read = gopher_read,
  105. .url_write = gopher_write,
  106. .url_close = gopher_close,
  107. .priv_data_size = sizeof(GopherContext),
  108. .flags = URL_PROTOCOL_FLAG_NETWORK,
  109. .default_whitelist = "gopher,tcp"
  110. };
  111. #endif /* CONFIG_GOPHER_PROTOCOL */
  112. #if CONFIG_GOPHERS_PROTOCOL
  113. const URLProtocol ff_gophers_protocol = {
  114. .name = "gophers",
  115. .url_open = gopher_open,
  116. .url_read = gopher_read,
  117. .url_write = gopher_write,
  118. .url_close = gopher_close,
  119. .priv_data_size = sizeof(GopherContext),
  120. .flags = URL_PROTOCOL_FLAG_NETWORK,
  121. .default_whitelist = "gopher,gophers,tcp,tls"
  122. };
  123. #endif /* CONFIG_GOPHERS_PROTOCOL */