[vlc-devel] [PATCH] access: add gopher input module

Vincenzo Nicosia katolaz at freaknet.org
Sun Jan 5 07:25:41 CET 2020


On Sat, Jan 04, 2020 at 05:05:29PM +0100, Francois Cartegnie wrote:

Hi Francois,

thanks for your comments. Please find my replies and an amended patch
below.

[cut]

> > + * Copyright (C) 2019 Vincenzo "KatolaZ" Nicosia
> > + *
> > + * Authors: Vincenzo "KatolaZ" Nicosia <katolaz at freaknet.org>
> > + *
> > + * This module was adapted from tcp.c
> 
> If you reuse code, then your header is wrong.
>

You are right. I added the copyright statement I found in tcp.c.

> > +    set_shortname( N_( "gopher" ) )
> 
> Is there really a need for translation ?

Nope. Removed the call to N_().

[cut]

> > +    psz_path = malloc((strlen(url.psz_path) + 6) * sizeof(char));
> 
> I can think of funny things here with string max size, but you're saved
> by the scheme/host length.
>

It looks like this comment is not relevant any more, since the second
version of the patch I sent in reply to Remi's comments uses asprintf
instead of explicit calls to malloc. Nevertheless, there was an issue
with NULL url.psz_path, which has been addressed in the amended patch
below.

Regards

---
 modules/access/Makefile.am |   5 ++
 modules/access/gopher.c    | 166 +++++++++++++++++++++++++++++++++++++
 src/input/item.c           |   1 +
 3 files changed, 172 insertions(+)
 create mode 100644 modules/access/gopher.c

diff --git a/modules/access/Makefile.am b/modules/access/Makefile.am
index 756ca5cf59..9e5353b94c 100644
--- a/modules/access/Makefile.am
+++ b/modules/access/Makefile.am
@@ -326,6 +326,11 @@ libhttp_plugin_la_SOURCES = access/http.c
 libhttp_plugin_la_LIBADD = $(SOCKET_LIBS)
 access_LTLIBRARIES += libhttp_plugin.la
 
+libgopher_plugin_la_SOURCES = access/gopher.c
+libgopher_plugin_la_LIBADD = $(SOCKET_LIBS)
+access_LTLIBRARIES += libgopher_plugin.la
+
+
 liblive555_plugin_la_SOURCES = access/live555.cpp access/mms/asf.c access/mms/buffer.c \
                                access/live555_dtsgen.h
 liblive555_plugin_la_CXXFLAGS = $(AM_CXXFLAGS) $(CXXFLAGS_live555)
diff --git a/modules/access/gopher.c b/modules/access/gopher.c
new file mode 100644
index 0000000000..9a29d5ec32
--- /dev/null
+++ b/modules/access/gopher.c
@@ -0,0 +1,166 @@
+/*****************************************************************************
+ * gopher.c: gopher input module
+ *****************************************************************************
+ * Copyright (C) 2003-2004 VLC authors and VideoLAN
+ * Copyright (C) 2019-2020 Vincenzo "KatolaZ" Nicosia
+ *
+ * Authors: Vincenzo "KatolaZ" Nicosia <katolaz at freaknet.org>
+ *
+ * This module was adapted from tcp.c
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_access.h>
+#include <vlc_messages.h>
+#include <vlc_url.h>
+#include <vlc_tls.h>
+
+#include <stdlib.h>
+#include <string.h>
+
+/* Forward declarations */
+static int Open(vlc_object_t *);
+static void Close(vlc_object_t *);
+
+/* Module descriptor */
+vlc_module_begin ()
+    set_description( N_("Gopher input") )
+    set_capability( "access", 0 )
+    set_shortname( "gopher" )
+    set_category( CAT_INPUT )
+    set_subcategory( SUBCAT_INPUT_ACCESS )
+    add_shortcut( "gopher")
+    set_callbacks( Open, Close )
+vlc_module_end ()
+
+
+static ssize_t Read(stream_t *access, void *buf, size_t len)
+{
+    return vlc_tls_Read(access->p_sys, buf, len, false);
+}
+
+static int Control( stream_t *p_access, int i_query, va_list args )
+{
+    bool    *pb_bool;
+
+    switch( i_query )
+    {
+        case STREAM_CAN_SEEK:
+        case STREAM_CAN_FASTSEEK:
+            pb_bool = va_arg( args, bool * );
+            *pb_bool = false;
+            break;
+        case STREAM_CAN_PAUSE:
+            pb_bool = va_arg( args, bool * );
+            *pb_bool = true;    /* FIXME */
+            break;
+        case STREAM_CAN_CONTROL_PACE:
+            pb_bool = va_arg( args, bool * );
+            *pb_bool = true;    /* FIXME */
+            break;
+
+        case STREAM_GET_PTS_DELAY:
+            *va_arg( args, vlc_tick_t * ) =
+                VLC_TICK_FROM_MS(var_InheritInteger( p_access, "network-caching" ));
+            break;
+
+        case STREAM_SET_PAUSE_STATE:
+            /* Nothing to do */
+            break;
+
+        default:
+            return VLC_EGENERIC;
+    }
+    return VLC_SUCCESS;
+}
+
+
+
+static int Open(vlc_object_t *obj)
+{
+    char *psz_path = NULL;
+    stream_t *access = (stream_t *)obj;
+    vlc_tls_t *sock;
+    vlc_url_t url;
+
+
+    if (vlc_UrlParse(&url, access->psz_url) || url.psz_host == NULL){
+        msg_Err(access, "invalid location: %s", access->psz_location);
+        vlc_UrlClean(&url);
+        return VLC_EGENERIC;
+    }
+
+    if (url.i_port <= 0){
+        url.i_port = 70;
+    }
+    sock = vlc_tls_SocketOpenTCP(obj, url.psz_host, url.i_port);
+
+    if (unlikely(sock == NULL)){
+        msg_Err(access, "cannot connect to %s:%d", url.psz_host, url.i_port);
+        vlc_UrlClean(&url);
+        return VLC_EGENERIC;
+    }
+
+    if (url.psz_path == NULL || strlen(url.psz_path) <= 3){
+        /* If no resource type is specified, look for the root resource */
+        if (asprintf(&psz_path, "\r\n") == -1){
+            vlc_UrlClean(&url);
+            vlc_tls_SessionDelete(sock);
+            return VLC_EGENERIC;
+        }
+        msg_Info(access, "path set to root resource");
+    }
+    else { /* strip resource type from URL */
+        if(asprintf(&psz_path, "%s\r\n", url.psz_path+2) == -1){
+            vlc_UrlClean(&url);
+            vlc_tls_SessionDelete(sock);
+            return VLC_EGENERIC;
+        }
+        msg_Info(access, "stripped resource type from path");
+    }
+    vlc_UrlClean(&url);
+
+    access->p_sys = sock;
+    access->pf_read = Read;
+    access->pf_block = NULL;
+    access->pf_control = Control;
+    access->pf_seek = NULL;
+
+    msg_Info(access, "requesting resource: %s", psz_path);
+    if (vlc_tls_Write(access->p_sys, psz_path, strlen(psz_path)) < 0){
+        vlc_tls_SessionDelete(access->p_sys);
+        free(psz_path);
+        return VLC_EGENERIC;
+    }
+
+    free(psz_path);
+    return VLC_SUCCESS;
+}
+
+static void Close( vlc_object_t *p_this )
+{
+    stream_t *access = (stream_t *)p_this;
+
+    vlc_tls_SessionDelete(access->p_sys);
+}
+
+
diff --git a/src/input/item.c b/src/input/item.c
index 9c3442d5c7..1a8a2bdb3f 100644
--- a/src/input/item.c
+++ b/src/input/item.c
@@ -1187,6 +1187,7 @@ static enum input_item_type_e GuessType( const input_item_t *p_item, bool *p_net
         { "fd",     ITEM_TYPE_UNKNOWN, false },
         { "file",   ITEM_TYPE_FILE, false },
         { "ftp",    ITEM_TYPE_FILE, true },
+        { "gopher", ITEM_TYPE_STREAM, true },
         { "http",   ITEM_TYPE_FILE, true },
         { "icyx",   ITEM_TYPE_STREAM, true },
         { "imem",   ITEM_TYPE_UNKNOWN, false },
-- 
2.20.1



More information about the vlc-devel mailing list