gopher.c 3.2 KB

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