Mercurial > libavformat.hg
annotate gopher.c @ 5109:293c03f0d47f libavformat
Add handling of EAGAIN at packet boundaries. See "[PATCH] RTSP-MS 14/15:
ASF packet parsing" thread for discussion / reasoning.
author | rbultje |
---|---|
date | Sun, 26 Jul 2009 19:09:35 +0000 |
parents | 1842e64fe89a |
children | 7c7fe75728dd |
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" |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
27 #include "network.h" |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
28 |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
29 typedef struct { |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
30 URLContext *hd; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
31 } GopherContext; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
32 |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
33 static int gopher_write(URLContext *h, uint8_t *buf, int size) |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
34 { |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
35 GopherContext *s = h->priv_data; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
36 return url_write(s->hd, buf, size); |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
37 } |
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 static int gopher_connect(URLContext *h, const char *path) |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
40 { |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
41 char buffer[1024]; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
42 |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
43 if (!*path) return AVERROR(EINVAL); |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
44 switch (*++path) { |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
45 case '5': |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
46 case '9': |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
47 path = strchr(path, '/'); |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
48 if (!path) return AVERROR(EINVAL); |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
49 break; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
50 default: |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
51 av_log(NULL, AV_LOG_WARNING, |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
52 "Gopher protocol type '%c' not supported yet!\n", |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
53 *path); |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
54 return AVERROR(EINVAL); |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
55 } |
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 /* send gopher sector */ |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
58 snprintf(buffer, sizeof(buffer), "%s\r\n", path); |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
59 |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
60 if (gopher_write(h, buffer, strlen(buffer)) < 0) |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
61 return AVERROR(EIO); |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
62 |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
63 return 0; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
64 } |
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 static int gopher_close(URLContext *h) |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
67 { |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
68 GopherContext *s = h->priv_data; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
69 if (s->hd) { |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
70 url_close(s->hd); |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
71 s->hd = NULL; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
72 } |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
73 av_freep(&h->priv_data); |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
74 return 0; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
75 } |
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 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
|
78 { |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
79 GopherContext *s; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
80 char hostname[1024], auth[1024], path[1024], buf[1024]; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
81 int port, err; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
82 |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
83 h->is_streamed = 1; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
84 |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
85 s = av_malloc(sizeof(GopherContext)); |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
86 if (!s) { |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
87 return AVERROR(ENOMEM); |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
88 } |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
89 h->priv_data = s; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
90 |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
91 /* needed in any case to build the host string */ |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
92 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
93 path, sizeof(path), uri); |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
94 |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
95 if (port < 0) |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
96 port = 70; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
97 |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
98 snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port); |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
99 |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
100 s->hd = NULL; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
101 err = url_open(&s->hd, buf, URL_RDWR); |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
102 if (err < 0) |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
103 goto fail; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
104 |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
105 if ((err = gopher_connect(h, path)) < 0) |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
106 goto fail; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
107 return 0; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
108 fail: |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
109 gopher_close(h); |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
110 return err; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
111 } |
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 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
|
114 { |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
115 GopherContext *s = h->priv_data; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
116 int len = url_read(s->hd, buf, size); |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
117 return len; |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
118 } |
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 URLProtocol gopher_protocol = { |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
122 "gopher", |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
123 gopher_open, |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
124 gopher_read, |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
125 gopher_write, |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
126 NULL, /*seek*/ |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
127 gopher_close, |
1842e64fe89a
Gopher protocol, patch by Toshimitsu Kimura, lovesyao gmail com
diego
parents:
diff
changeset
|
128 }; |