ffmpeg / libavformat / gopher.c @ f87b1b37
History | View | Annotate | Download (3.15 KB)
1 | 865780ae | Toshimitsu Kimura | /*
|
---|---|---|---|
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 | 2912e87a | Mans Rullgard | * This file is part of Libav.
|
9 | 865780ae | Toshimitsu Kimura | *
|
10 | 2912e87a | Mans Rullgard | * Libav is free software; you can redistribute it and/or
|
11 | 865780ae | Toshimitsu Kimura | * 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 | 2912e87a | Mans Rullgard | * Libav is distributed in the hope that it will be useful,
|
16 | 865780ae | Toshimitsu Kimura | * 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 | 2912e87a | Mans Rullgard | * License along with Libav; if not, write to the Free Software
|
22 | 865780ae | Toshimitsu Kimura | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
23 | */
|
||
24 | |||
25 | #include "libavutil/avstring.h" |
||
26 | #include "avformat.h" |
||
27 | e4a9e3cc | Aurelien Jacobs | #include "internal.h" |
28 | 865780ae | Toshimitsu Kimura | #include "network.h" |
29 | 0589da0a | Anton Khirnov | #include "url.h" |
30 | 865780ae | Toshimitsu Kimura | |
31 | typedef struct { |
||
32 | URLContext *hd; |
||
33 | } GopherContext; |
||
34 | |||
35 | 27241cbf | Martin Storsjö | static int gopher_write(URLContext *h, const uint8_t *buf, int size) |
36 | 865780ae | Toshimitsu Kimura | { |
37 | GopherContext *s = h->priv_data; |
||
38 | 925e908b | Anton Khirnov | return ffurl_write(s->hd, buf, size);
|
39 | 865780ae | Toshimitsu Kimura | } |
40 | |||
41 | static int gopher_connect(URLContext *h, const char *path) |
||
42 | { |
||
43 | char buffer[1024]; |
||
44 | |||
45 | if (!*path) return AVERROR(EINVAL); |
||
46 | switch (*++path) {
|
||
47 | case '5': |
||
48 | case '9': |
||
49 | path = strchr(path, '/');
|
||
50 | if (!path) return AVERROR(EINVAL); |
||
51 | break;
|
||
52 | default:
|
||
53 | av_log(NULL, AV_LOG_WARNING,
|
||
54 | "Gopher protocol type '%c' not supported yet!\n",
|
||
55 | *path); |
||
56 | return AVERROR(EINVAL);
|
||
57 | } |
||
58 | |||
59 | /* send gopher sector */
|
||
60 | snprintf(buffer, sizeof(buffer), "%s\r\n", path); |
||
61 | |||
62 | if (gopher_write(h, buffer, strlen(buffer)) < 0) |
||
63 | return AVERROR(EIO);
|
||
64 | |||
65 | return 0; |
||
66 | } |
||
67 | |||
68 | static int gopher_close(URLContext *h) |
||
69 | { |
||
70 | GopherContext *s = h->priv_data; |
||
71 | if (s->hd) {
|
||
72 | e52a9145 | Anton Khirnov | ffurl_close(s->hd); |
73 | 865780ae | Toshimitsu Kimura | s->hd = NULL;
|
74 | } |
||
75 | av_freep(&h->priv_data); |
||
76 | return 0; |
||
77 | } |
||
78 | |||
79 | static int gopher_open(URLContext *h, const char *uri, int flags) |
||
80 | { |
||
81 | GopherContext *s; |
||
82 | char hostname[1024], auth[1024], path[1024], buf[1024]; |
||
83 | int port, err;
|
||
84 | |||
85 | h->is_streamed = 1;
|
||
86 | |||
87 | s = av_malloc(sizeof(GopherContext));
|
||
88 | if (!s) {
|
||
89 | return AVERROR(ENOMEM);
|
||
90 | } |
||
91 | h->priv_data = s; |
||
92 | |||
93 | /* needed in any case to build the host string */
|
||
94 | f3bfe388 | Måns Rullgård | av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, |
95 | f984dcf6 | Martin Storsjö | path, sizeof(path), uri);
|
96 | 865780ae | Toshimitsu Kimura | |
97 | if (port < 0) |
||
98 | port = 70;
|
||
99 | |||
100 | 57b5555c | Martin Storsjö | ff_url_join(buf, sizeof(buf), "tcp", NULL, hostname, port, NULL); |
101 | 865780ae | Toshimitsu Kimura | |
102 | s->hd = NULL;
|
||
103 | f87b1b37 | Anton Khirnov | err = ffurl_open(&s->hd, buf, AVIO_RDWR); |
104 | 865780ae | Toshimitsu Kimura | if (err < 0) |
105 | goto fail;
|
||
106 | |||
107 | if ((err = gopher_connect(h, path)) < 0) |
||
108 | goto fail;
|
||
109 | return 0; |
||
110 | fail:
|
||
111 | gopher_close(h); |
||
112 | return err;
|
||
113 | } |
||
114 | |||
115 | static int gopher_read(URLContext *h, uint8_t *buf, int size) |
||
116 | { |
||
117 | GopherContext *s = h->priv_data; |
||
118 | bc371aca | Anton Khirnov | int len = ffurl_read(s->hd, buf, size);
|
119 | 865780ae | Toshimitsu Kimura | return len;
|
120 | } |
||
121 | |||
122 | |||
123 | c6610a21 | Diego Elio Pettenò | URLProtocol ff_gopher_protocol = { |
124 | 865780ae | Toshimitsu Kimura | "gopher",
|
125 | gopher_open, |
||
126 | gopher_read, |
||
127 | gopher_write, |
||
128 | NULL, /*seek*/ |
||
129 | gopher_close, |
||
130 | }; |