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