4 * Copyright (c) 2009 Toshimitsu Kimura
6 * based on libavformat/http.c, Copyright (c) 2000, 2001 Fabrice Bellard
8 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/avstring.h"
34 static int gopher_write(URLContext *h, const uint8_t *buf, int size)
36 GopherContext *s = h->priv_data;
37 return url_write(s->hd, buf, size);
40 static int gopher_connect(URLContext *h, const char *path)
44 if (!*path) return AVERROR(EINVAL);
48 path = strchr(path, '/');
49 if (!path) return AVERROR(EINVAL);
52 av_log(NULL, AV_LOG_WARNING,
53 "Gopher protocol type '%c' not supported yet!\n",
55 return AVERROR(EINVAL);
58 /* send gopher sector */
59 snprintf(buffer, sizeof(buffer), "%s\r\n", path);
61 if (gopher_write(h, buffer, strlen(buffer)) < 0)
67 static int gopher_close(URLContext *h)
69 GopherContext *s = h->priv_data;
74 av_freep(&h->priv_data);
78 static int gopher_open(URLContext *h, const char *uri, int flags)
81 char hostname[1024], auth[1024], path[1024], buf[1024];
86 s = av_malloc(sizeof(GopherContext));
88 return AVERROR(ENOMEM);
92 /* needed in any case to build the host string */
93 av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
94 path, sizeof(path), uri);
99 ff_url_join(buf, sizeof(buf), "tcp", NULL, hostname, port, NULL);
102 err = url_open(&s->hd, buf, URL_RDWR);
106 if ((err = gopher_connect(h, path)) < 0)
114 static int gopher_read(URLContext *h, uint8_t *buf, int size)
116 GopherContext *s = h->priv_data;
117 int len = url_read(s->hd, buf, size);
122 URLProtocol ff_gopher_protocol = {