FFmpeg  4.4
gopher.c
Go to the documentation of this file.
1 /*
2  * Gopher protocol
3  *
4  * Copyright (c) 2009 Toshimitsu Kimura
5  * Copyright (c) 2021 parazyd <parazyd@dyne.org>
6  *
7  * based on libavformat/http.c, Copyright (c) 2000, 2001 Fabrice Bellard
8  *
9  * This file is part of FFmpeg.
10  *
11  * FFmpeg is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * FFmpeg is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with FFmpeg; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 #include "config.h"
27 
28 #include "libavutil/avstring.h"
29 #include "avformat.h"
30 #include "internal.h"
31 #include "network.h"
32 #include "url.h"
33 
34 typedef struct GopherContext {
37 
38 static int gopher_write(URLContext *h, const uint8_t *buf, int size)
39 {
40  GopherContext *s = h->priv_data;
41  return ffurl_write(s->hd, buf, size);
42 }
43 
44 static int gopher_connect(URLContext *h, const char *path)
45 {
46  char buffer[1024];
47 
48  if (!*path) return AVERROR(EINVAL);
49  switch (*++path) {
50  case '5':
51  case '9':
52  path = strchr(path, '/');
53  if (!path) return AVERROR(EINVAL);
54  break;
55  default:
57  "Gopher protocol type '%c' not supported yet!\n",
58  *path);
59  return AVERROR(EINVAL);
60  }
61 
62  /* send gopher sector */
63  snprintf(buffer, sizeof(buffer), "%s\r\n", path);
64 
65  if (gopher_write(h, buffer, strlen(buffer)) < 0)
66  return AVERROR(EIO);
67 
68  return 0;
69 }
70 
72 {
73  GopherContext *s = h->priv_data;
74  ffurl_closep(&s->hd);
75  return 0;
76 }
77 
78 static int gopher_open(URLContext *h, const char *uri, int flags)
79 {
80  GopherContext *s = h->priv_data;
81  char proto[10], hostname[1024], auth[1024], path[1024], buf[1024];
82  int port, err;
83  const char *lower_proto = "tcp";
84 
85  h->is_streamed = 1;
86 
87  /* needed in any case to build the host string */
88  av_url_split(proto, sizeof(proto), auth, sizeof(auth),
89  hostname, sizeof(hostname), &port, path, sizeof(path), uri);
90 
91  if (port < 0)
92  port = 70;
93 
94  if (!strcmp(proto, "gophers"))
95  lower_proto = "tls";
96 
97  ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
98 
99  s->hd = NULL;
101  &h->interrupt_callback, NULL, h->protocol_whitelist, h->protocol_blacklist, h);
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 
113 static int gopher_read(URLContext *h, uint8_t *buf, int size)
114 {
115  GopherContext *s = h->priv_data;
116  int len = ffurl_read(s->hd, buf, size);
117  return len;
118 }
119 
120 #if CONFIG_GOPHER_PROTOCOL
122  .name = "gopher",
123  .url_open = gopher_open,
124  .url_read = gopher_read,
125  .url_write = gopher_write,
126  .url_close = gopher_close,
127  .priv_data_size = sizeof(GopherContext),
129  .default_whitelist = "gopher,tcp"
130 };
131 #endif /* CONFIG_GOPHER_PROTOCOL */
132 
133 #if CONFIG_GOPHERS_PROTOCOL
135  .name = "gophers",
136  .url_open = gopher_open,
137  .url_read = gopher_read,
138  .url_write = gopher_write,
139  .url_close = gopher_close,
140  .priv_data_size = sizeof(GopherContext),
142  .default_whitelist = "gopher,gophers,tcp,tls"
143 };
144 #endif /* CONFIG_GOPHERS_PROTOCOL */
uint8_t
Main libavformat public API header.
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf.
Definition: avio.c:401
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it.
Definition: avio.c:309
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:438
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition: avio.c:415
#define AVIO_FLAG_READ_WRITE
read-write pseudo flag
Definition: avio.h:676
#define flags(name, subs,...)
Definition: cbs_av1.c:561
#define s(width, name)
Definition: cbs_vp9.c:257
#define fail()
Definition: checkasm.h:133
#define NULL
Definition: coverity.c:32
static int gopher_read(URLContext *h, uint8_t *buf, int size)
Definition: gopher.c:113
static int gopher_close(URLContext *h)
Definition: gopher.c:71
static int gopher_open(URLContext *h, const char *uri, int flags)
Definition: gopher.c:78
static int gopher_connect(URLContext *h, const char *path)
Definition: gopher.c:44
static int gopher_write(URLContext *h, const uint8_t *buf, int size)
Definition: gopher.c:38
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
Definition: utils.c:4795
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
common internal API header
const URLProtocol ff_gophers_protocol
const URLProtocol ff_gopher_protocol
static char buffer[20]
Definition: seek.c:32
#define snprintf
Definition: snprintf.h:34
URLContext * hd
Definition: gopher.c:35
Definition: url.h:38
const char * name
Definition: url.h:55
#define av_log(a,...)
int size
int ff_url_join(char *str, int size, const char *proto, const char *authorization, const char *hostname, int port, const char *fmt,...)
Definition: url.c:38
unbuffered private I/O API
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:34
int len