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