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 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_access.h>
33 #include <vlc_messages.h>
40 /* Forward declarations */
41 static int Open(vlc_object_t
*);
42 static void Close(vlc_object_t
*);
44 /* Module descriptor */
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
)
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
)
68 case STREAM_CAN_FASTSEEK
:
69 pb_bool
= va_arg( args
, bool * );
72 case STREAM_CAN_PAUSE
:
73 pb_bool
= va_arg( args
, bool * );
74 *pb_bool
= true; /* FIXME */
76 case STREAM_CAN_CONTROL_PACE
:
77 pb_bool
= va_arg( args
, bool * );
78 *pb_bool
= true; /* FIXME */
81 case STREAM_GET_PTS_DELAY
:
82 *va_arg( args
, vlc_tick_t
* ) =
83 VLC_TICK_FROM_MS(var_InheritInteger( p_access
, "network-caching" ));
86 case STREAM_SET_PAUSE_STATE
:
98 static int Open(vlc_object_t
*obj
)
100 char *psz_path
= NULL
;
101 stream_t
*access
= (stream_t
*)obj
;
106 if (vlc_UrlParse(&url
, access
->psz_url
) || url
.psz_host
== NULL
)
108 msg_Err(access
, "invalid location: %s", access
->psz_location
);
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
);
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)
132 vlc_tls_SessionDelete(sock
);
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)
141 vlc_tls_SessionDelete(sock
);
144 msg_Info(access
, "stripped resource type from path");
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
);
166 static void Close( vlc_object_t
*p_this
)
168 stream_t
*access
= (stream_t
*)p_this
;
170 vlc_tls_SessionDelete(access
->p_sys
);