Browse code

Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com

Originally committed as revision 17159 to svn://svn.ffmpeg.org/ffmpeg/trunk

Toshimitsu Kimura authored on 2009/02/12 01:28:46
Showing 5 changed files
... ...
@@ -149,6 +149,7 @@ version <next>
149 149
 - Electronic Arts TQI decoder
150 150
 - OpenJPEG based JPEG 2000 decoder
151 151
 - NC (NC4600) cameras file demuxer
152
+- Gopher client support
152 153
 
153 154
 version 0.4.9-pre1:
154 155
 
... ...
@@ -208,6 +208,7 @@ OBJS-$(CONFIG_VHOOK)                     += framehook.o
208 208
 OBJS+= avio.o aviobuf.o
209 209
 
210 210
 OBJS-$(CONFIG_FILE_PROTOCOL)             += file.o
211
+OBJS-$(CONFIG_GOPHER_PROTOCOL)           += gopher.o
211 212
 OBJS-$(CONFIG_HTTP_PROTOCOL)             += http.o
212 213
 OBJS-$(CONFIG_PIPE_PROTOCOL)             += file.o
213 214
 OBJS-$(CONFIG_RTP_PROTOCOL)              += rtpproto.o
... ...
@@ -199,6 +199,7 @@ void av_register_all(void)
199 199
 
200 200
     /* protocols */
201 201
     REGISTER_PROTOCOL (FILE, file);
202
+    REGISTER_PROTOCOL (GOPHER, gopher);
202 203
     REGISTER_PROTOCOL (HTTP, http);
203 204
     REGISTER_PROTOCOL (PIPE, pipe);
204 205
     REGISTER_PROTOCOL (RTP, rtp);
... ...
@@ -22,7 +22,7 @@
22 22
 #define AVFORMAT_AVFORMAT_H
23 23
 
24 24
 #define LIBAVFORMAT_VERSION_MAJOR 52
25
-#define LIBAVFORMAT_VERSION_MINOR 26
25
+#define LIBAVFORMAT_VERSION_MINOR 27
26 26
 #define LIBAVFORMAT_VERSION_MICRO  0
27 27
 
28 28
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
29 29
new file mode 100644
... ...
@@ -0,0 +1,128 @@
0
+/*
1
+ * Gopher protocol
2
+ *
3
+ * Copyright (c) 2009 Toshimitsu Kimura
4
+ *
5
+ * based on libavformat/http.c, Copyright (c) 2000, 2001 Fabrice Bellard
6
+ *
7
+ * This file is part of FFmpeg.
8
+ *
9
+ * FFmpeg is free software; you can redistribute it and/or
10
+ * modify it under the terms of the GNU Lesser General Public
11
+ * License as published by the Free Software Foundation; either
12
+ * version 2.1 of the License, or (at your option) any later version.
13
+ *
14
+ * FFmpeg is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
+ * Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with FFmpeg; if not, write to the Free Software
21
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+ */
23
+
24
+#include "libavutil/avstring.h"
25
+#include "avformat.h"
26
+#include "network.h"
27
+
28
+typedef struct {
29
+    URLContext *hd;
30
+} GopherContext;
31
+
32
+static int gopher_write(URLContext *h, uint8_t *buf, int size)
33
+{
34
+    GopherContext *s = h->priv_data;
35
+    return url_write(s->hd, buf, size);
36
+}
37
+
38
+static int gopher_connect(URLContext *h, const char *path)
39
+{
40
+    char buffer[1024];
41
+
42
+    if (!*path) return AVERROR(EINVAL);
43
+    switch (*++path) {
44
+        case '5':
45
+        case '9':
46
+            path = strchr(path, '/');
47
+            if (!path) return AVERROR(EINVAL);
48
+            break;
49
+        default:
50
+            av_log(NULL, AV_LOG_WARNING,
51
+                   "Gopher protocol type '%c' not supported yet!\n",
52
+                   *path);
53
+            return AVERROR(EINVAL);
54
+    }
55
+
56
+    /* send gopher sector */
57
+    snprintf(buffer, sizeof(buffer), "%s\r\n", path);
58
+
59
+    if (gopher_write(h, buffer, strlen(buffer)) < 0)
60
+        return AVERROR(EIO);
61
+
62
+    return 0;
63
+}
64
+
65
+static int gopher_close(URLContext *h)
66
+{
67
+    GopherContext *s = h->priv_data;
68
+    if (s->hd) {
69
+        url_close(s->hd);
70
+        s->hd = NULL;
71
+    }
72
+    av_freep(&h->priv_data);
73
+    return 0;
74
+}
75
+
76
+static int gopher_open(URLContext *h, const char *uri, int flags)
77
+{
78
+    GopherContext *s;
79
+    char hostname[1024], auth[1024], path[1024], buf[1024];
80
+    int port, err;
81
+
82
+    h->is_streamed = 1;
83
+
84
+    s = av_malloc(sizeof(GopherContext));
85
+    if (!s) {
86
+        return AVERROR(ENOMEM);
87
+    }
88
+    h->priv_data = s;
89
+
90
+    /* needed in any case to build the host string */
91
+    url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
92
+              path, sizeof(path), uri);
93
+
94
+    if (port < 0)
95
+        port = 70;
96
+
97
+    snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
98
+
99
+    s->hd = NULL;
100
+    err = url_open(&s->hd, buf, URL_RDWR);
101
+    if (err < 0)
102
+        goto fail;
103
+
104
+    if ((err = gopher_connect(h, path)) < 0)
105
+        goto fail;
106
+    return 0;
107
+ fail:
108
+    gopher_close(h);
109
+    return err;
110
+}
111
+
112
+static int gopher_read(URLContext *h, uint8_t *buf, int size)
113
+{
114
+    GopherContext *s = h->priv_data;
115
+    int len = url_read(s->hd, buf, size);
116
+    return len;
117
+}
118
+
119
+
120
+URLProtocol gopher_protocol = {
121
+    "gopher",
122
+    gopher_open,
123
+    gopher_read,
124
+    gopher_write,
125
+    NULL, /*seek*/
126
+    gopher_close,
127
+};