gopher.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "network.h"
  27. typedef struct {
  28. URLContext *hd;
  29. } GopherContext;
  30. static int gopher_write(URLContext *h, uint8_t *buf, int size)
  31. {
  32. GopherContext *s = h->priv_data;
  33. return url_write(s->hd, buf, size);
  34. }
  35. static int gopher_connect(URLContext *h, const char *path)
  36. {
  37. char buffer[1024];
  38. if (!*path) return AVERROR(EINVAL);
  39. switch (*++path) {
  40. case '5':
  41. case '9':
  42. path = strchr(path, '/');
  43. if (!path) return AVERROR(EINVAL);
  44. break;
  45. default:
  46. av_log(NULL, AV_LOG_WARNING,
  47. "Gopher protocol type '%c' not supported yet!\n",
  48. *path);
  49. return AVERROR(EINVAL);
  50. }
  51. /* send gopher sector */
  52. snprintf(buffer, sizeof(buffer), "%s\r\n", path);
  53. if (gopher_write(h, buffer, strlen(buffer)) < 0)
  54. return AVERROR(EIO);
  55. return 0;
  56. }
  57. static int gopher_close(URLContext *h)
  58. {
  59. GopherContext *s = h->priv_data;
  60. if (s->hd) {
  61. url_close(s->hd);
  62. s->hd = NULL;
  63. }
  64. av_freep(&h->priv_data);
  65. return 0;
  66. }
  67. static int gopher_open(URLContext *h, const char *uri, int flags)
  68. {
  69. GopherContext *s;
  70. char hostname[1024], auth[1024], path[1024], buf[1024];
  71. int port, err;
  72. h->is_streamed = 1;
  73. s = av_malloc(sizeof(GopherContext));
  74. if (!s) {
  75. return AVERROR(ENOMEM);
  76. }
  77. h->priv_data = s;
  78. /* needed in any case to build the host string */
  79. url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
  80. path, sizeof(path), uri);
  81. if (port < 0)
  82. port = 70;
  83. snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
  84. s->hd = NULL;
  85. err = url_open(&s->hd, buf, URL_RDWR);
  86. if (err < 0)
  87. goto fail;
  88. if ((err = gopher_connect(h, path)) < 0)
  89. goto fail;
  90. return 0;
  91. fail:
  92. gopher_close(h);
  93. return err;
  94. }
  95. static int gopher_read(URLContext *h, uint8_t *buf, int size)
  96. {
  97. GopherContext *s = h->priv_data;
  98. int len = url_read(s->hd, buf, size);
  99. return len;
  100. }
  101. URLProtocol gopher_protocol = {
  102. "gopher",
  103. gopher_open,
  104. gopher_read,
  105. gopher_write,
  106. NULL, /*seek*/
  107. gopher_close,
  108. };