contrib: cargo: use cargo/vendored-openssl if needed
[vlc.git] / modules / access / gopher.c
blobc8ed71369c8764e978629691c8cdaee276a8b593
1 /*****************************************************************************
2 * gopher.c: gopher input module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VLC authors and VideoLAN
5 * Copyright (C) 2019-2020 Vincenzo "KatolaZ" Nicosia
7 * Authors: Vincenzo "KatolaZ" Nicosia <katolaz@freaknet.org>
9 * This module was adapted from tcp.c
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program 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
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_access.h>
33 #include <vlc_messages.h>
34 #include <vlc_url.h>
35 #include <vlc_tls.h>
37 #include <stdio.h>
38 #include <string.h>
40 /* Forward declarations */
41 static int Open(vlc_object_t *);
42 static void Close(vlc_object_t *);
44 /* Module descriptor */
45 vlc_module_begin ()
46 set_description( N_("Gopher input") )
47 set_capability( "access", 0 )
48 set_shortname( "gopher" )
49 set_category( CAT_INPUT )
50 set_subcategory( SUBCAT_INPUT_ACCESS )
51 add_shortcut( "gopher")
52 set_callbacks( Open, Close )
53 vlc_module_end ()
56 static ssize_t Read(stream_t *access, void *buf, size_t len)
58 return vlc_tls_Read(access->p_sys, buf, len, false);
61 static int Control( stream_t *p_access, int i_query, va_list args )
63 bool *pb_bool;
65 switch( i_query )
67 case STREAM_CAN_SEEK:
68 case STREAM_CAN_FASTSEEK:
69 pb_bool = va_arg( args, bool * );
70 *pb_bool = false;
71 break;
72 case STREAM_CAN_PAUSE:
73 pb_bool = va_arg( args, bool * );
74 *pb_bool = true; /* FIXME */
75 break;
76 case STREAM_CAN_CONTROL_PACE:
77 pb_bool = va_arg( args, bool * );
78 *pb_bool = true; /* FIXME */
79 break;
81 case STREAM_GET_PTS_DELAY:
82 *va_arg( args, vlc_tick_t * ) =
83 VLC_TICK_FROM_MS(var_InheritInteger( p_access, "network-caching" ));
84 break;
86 case STREAM_SET_PAUSE_STATE:
87 /* Nothing to do */
88 break;
90 default:
91 return VLC_EGENERIC;
93 return VLC_SUCCESS;
98 static int Open(vlc_object_t *obj)
100 char *psz_path = NULL;
101 stream_t *access = (stream_t *)obj;
102 vlc_tls_t *sock;
103 vlc_url_t url;
106 if (vlc_UrlParse(&url, access->psz_url) || url.psz_host == NULL)
108 msg_Err(access, "invalid location: %s", access->psz_location);
109 vlc_UrlClean(&url);
110 return VLC_EGENERIC;
113 if (url.i_port == 0)
115 url.i_port = 70;
117 sock = vlc_tls_SocketOpenTCP(obj, url.psz_host, url.i_port);
119 if (unlikely(sock == NULL))
121 msg_Err(access, "cannot connect to %s:%d", url.psz_host, url.i_port);
122 vlc_UrlClean(&url);
123 return VLC_EGENERIC;
126 if (url.psz_path == NULL || strlen(url.psz_path) <= 3)
128 /* If no resource type is specified, look for the root resource */
129 if (asprintf(&psz_path, "\r\n") == -1)
131 vlc_UrlClean(&url);
132 vlc_tls_SessionDelete(sock);
133 return VLC_EGENERIC;
135 msg_Info(access, "path set to root resource");
137 else { /* strip resource type from URL */
138 if(asprintf(&psz_path, "%s\r\n", url.psz_path+2) == -1)
140 vlc_UrlClean(&url);
141 vlc_tls_SessionDelete(sock);
142 return VLC_EGENERIC;
144 msg_Info(access, "stripped resource type from path");
146 vlc_UrlClean(&url);
148 access->p_sys = sock;
149 access->pf_read = Read;
150 access->pf_block = NULL;
151 access->pf_control = Control;
152 access->pf_seek = NULL;
154 msg_Dbg(access, "requesting resource: %s", psz_path);
155 if (vlc_tls_Write(access->p_sys, psz_path, strlen(psz_path)) < 0)
157 vlc_tls_SessionDelete(access->p_sys);
158 free(psz_path);
159 return VLC_EGENERIC;
162 free(psz_path);
163 return VLC_SUCCESS;
166 static void Close( vlc_object_t *p_this )
168 stream_t *access = (stream_t *)p_this;
170 vlc_tls_SessionDelete(access->p_sys);