annotate gopher.c @ 6290:a1500faf3a7e libavformat

Check the status code of each server responses, and fail if it indicates a problem. Patch by Zhentan Feng <spyfeng gmail com>.
author rbultje
date Tue, 20 Jul 2010 15:01:06 +0000
parents 4fc5e0e4e1cd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4452
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
1 /*
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
2 * Gopher protocol
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
3 *
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
4 * Copyright (c) 2009 Toshimitsu Kimura
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
5 *
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
6 * based on libavformat/http.c, Copyright (c) 2000, 2001 Fabrice Bellard
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
7 *
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
8 * This file is part of FFmpeg.
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
9 *
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
10 * FFmpeg is free software; you can redistribute it and/or
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
11 * modify it under the terms of the GNU Lesser General Public
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
12 * License as published by the Free Software Foundation; either
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
13 * version 2.1 of the License, or (at your option) any later version.
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
14 *
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
15 * FFmpeg is distributed in the hope that it will be useful,
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
18 * Lesser General Public License for more details.
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
19 *
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
20 * You should have received a copy of the GNU Lesser General Public
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
21 * License along with FFmpeg; if not, write to the Free Software
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
23 */
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
24
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
25 #include "libavutil/avstring.h"
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
26 #include "avformat.h"
5837
d605f589f0be move ff_url_split() and ff_url_join() declarations to internal.h
aurel
parents: 5776
diff changeset
27 #include "internal.h"
4452
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
28 #include "network.h"
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
29
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
30 typedef struct {
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
31 URLContext *hd;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
32 } GopherContext;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
33
6068
7fdda2416684 Declare the url_write buffer parameter as const
mstorsjo
parents: 5837
diff changeset
34 static int gopher_write(URLContext *h, const uint8_t *buf, int size)
4452
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
35 {
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
36 GopherContext *s = h->priv_data;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
37 return url_write(s->hd, buf, size);
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
38 }
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
39
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
40 static int gopher_connect(URLContext *h, const char *path)
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
41 {
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
42 char buffer[1024];
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
43
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
44 if (!*path) return AVERROR(EINVAL);
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
45 switch (*++path) {
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
46 case '5':
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
47 case '9':
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
48 path = strchr(path, '/');
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
49 if (!path) return AVERROR(EINVAL);
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
50 break;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
51 default:
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
52 av_log(NULL, AV_LOG_WARNING,
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
53 "Gopher protocol type '%c' not supported yet!\n",
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
54 *path);
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
55 return AVERROR(EINVAL);
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
56 }
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
57
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
58 /* send gopher sector */
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
59 snprintf(buffer, sizeof(buffer), "%s\r\n", path);
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
60
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
61 if (gopher_write(h, buffer, strlen(buffer)) < 0)
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
62 return AVERROR(EIO);
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
63
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
64 return 0;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
65 }
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
66
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
67 static int gopher_close(URLContext *h)
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
68 {
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
69 GopherContext *s = h->priv_data;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
70 if (s->hd) {
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
71 url_close(s->hd);
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
72 s->hd = NULL;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
73 }
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
74 av_freep(&h->priv_data);
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
75 return 0;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
76 }
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
77
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
78 static int gopher_open(URLContext *h, const char *uri, int flags)
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
79 {
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
80 GopherContext *s;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
81 char hostname[1024], auth[1024], path[1024], buf[1024];
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
82 int port, err;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
83
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
84 h->is_streamed = 1;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
85
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
86 s = av_malloc(sizeof(GopherContext));
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
87 if (!s) {
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
88 return AVERROR(ENOMEM);
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
89 }
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
90 h->priv_data = s;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
91
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
92 /* needed in any case to build the host string */
6182
4fc5e0e4e1cd Make ff_url_split() public
mru
parents: 6068
diff changeset
93 av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
5776
d34f985d6e8f Reindent
mstorsjo
parents: 5775
diff changeset
94 path, sizeof(path), uri);
4452
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
95
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
96 if (port < 0)
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
97 port = 70;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
98
5756
7c7fe75728dd Use ff_url_join for assembling URLs, instead of snprintf
mstorsjo
parents: 4452
diff changeset
99 ff_url_join(buf, sizeof(buf), "tcp", NULL, hostname, port, NULL);
4452
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
100
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
101 s->hd = NULL;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
102 err = url_open(&s->hd, buf, URL_RDWR);
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
103 if (err < 0)
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
104 goto fail;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
105
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
106 if ((err = gopher_connect(h, path)) < 0)
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
107 goto fail;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
108 return 0;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
109 fail:
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
110 gopher_close(h);
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
111 return err;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
112 }
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
113
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
114 static int gopher_read(URLContext *h, uint8_t *buf, int size)
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
115 {
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
116 GopherContext *s = h->priv_data;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
117 int len = url_read(s->hd, buf, size);
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
118 return len;
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
119 }
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
120
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
121
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
122 URLProtocol gopher_protocol = {
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
123 "gopher",
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
124 gopher_open,
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
125 gopher_read,
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
126 gopher_write,
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
127 NULL, /*seek*/
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
128 gopher_close,
1842e64fe89a Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff changeset
129 };