]> git.sesse.net Git - vlc/blobdiff - modules/control/http/util.c
Use var_InheritString for --decklink-video-connection.
[vlc] / modules / control / http / util.c
index d9699ccde25fe2d11ae8b2ee3c0431c1efa60706..565c100b1120c3d630a89c8a889dc4e65477b3b7 100644 (file)
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
 #include "http.h"
-#include "vlc_strings.h"
+#include <vlc_strings.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <vlc_fs.h>
 
 /****************************************************************************
  * File and directory functions
  ****************************************************************************/
 
 /* ToUrl: create a good name for an url from filename */
-char *FileToUrl( char *name, bool *pb_index )
+static char *FileToUrl( const char *name, bool *pb_index )
 {
-    char *url, *p;
-
-    url = p = malloc( strlen( name ) + 1 );
-
     *pb_index = false;
-    if( !url || !p )
-    {
+
+    char *url = malloc( strlen( name ) + 2 );
+    if( unlikely(url == NULL) )
         return NULL;
-    }
 
-#ifdef WIN32
-    while( *name == '\\' || *name == '/' )
+#if (DIR_SEP_CHAR == '/')
+    name += strspn( name, "/" );
 #else
-    while( *name == '/' )
+    name += strspn( name, "/"DIR_SEP );
 #endif
-    {
-        name++;
-    }
+    *url = '/';
+    strcpy( url + 1, name );
 
-    *p++ = '/';
-    strcpy( p, name );
-
-#ifdef WIN32
+#if (DIR_SEP_CHAR != '/')
     /* convert '\\' into '/' */
-    name = p;
-    while( *name )
-    {
-        if( *name == '\\' )
-            *name = '/';
-        name++;
-    }
+    for( char *ptr = url; *ptr; ptr++ )
+        if( *ptr == DIR_SEP_CHAR )
+            *ptr = '/';
 #endif
 
     /* index.* -> / */
-    if( ( p = strrchr( url, '/' ) ) != NULL )
+    char *p = strrchr( url, '/' );
+    if( p != NULL && !strncmp( p, "/index.", 7 ) )
     {
-        if( !strncmp( p, "/index.", 7 ) )
-        {
-            p[1] = '\0';
-            *pb_index = true;
-        }
+        p[1] = '\0';
+        *pb_index = true;
     }
     return url;
 }
@@ -90,11 +80,12 @@ int FileLoad( FILE *f, char **pp_data, int *pi_data )
 
     /* just load the file */
     *pi_data = 0;
-    *pp_data = malloc( 1025 );  /* +1 for \0 */
+    *pp_data = xmalloc( 1025 );  /* +1 for \0 */
+
     while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
     {
         *pi_data += 1024;
-        *pp_data = realloc( *pp_data, *pi_data  + 1025 );
+        *pp_data = xrealloc( *pp_data, *pi_data  + 1025 );
     }
     if( i_read > 0 )
     {
@@ -120,15 +111,7 @@ int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
 
     int           i_dirlen;
 
-    char sep;
-
-#if defined( WIN32 )
-    sep = '\\';
-#else
-    sep = '/';
-#endif
-
-    if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
+    if( ( p_dir = vlc_opendir( psz_dir ) ) == NULL )
     {
         if( errno != ENOENT && errno != ENOTDIR )
             msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
@@ -145,8 +128,8 @@ int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
 
     msg_Dbg( p_intf, "dir=%s", psz_dir );
 
-    snprintf( dir, sizeof( dir ), "%s%c.access", psz_dir, sep );
-    if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
+    snprintf( dir, sizeof( dir ), "%s"DIR_SEP".access", psz_dir );
+    if( ( file = vlc_fopen( dir, "r" ) ) != NULL )
     {
         char line[1024];
         int  i_size;
@@ -173,17 +156,25 @@ int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
                 password = strdup( p );
             }
         }
-        msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
-                 user, password, i_size );
+        msg_Dbg( p_intf, "using user=%s (read=%d)", user, i_size );
 
         fclose( file );
     }
 
-    snprintf( dir, sizeof( dir ), "%s%c.hosts", psz_dir, sep );
+    snprintf( dir, sizeof( dir ), "%s"DIR_SEP".hosts", psz_dir );
     p_acl = ACL_Create( p_intf, false );
     if( ACL_LoadFile( p_acl, dir ) )
     {
         ACL_Destroy( p_acl );
+
+        struct stat st;
+        if( vlc_stat( dir, &st ) == 0 )
+        {
+            free( user );
+            free( password );
+            closedir( p_dir );
+            return VLC_EGENERIC;
+        }
         p_acl = NULL;
     }
 
@@ -191,7 +182,7 @@ int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
     {
         char *psz_filename;
         /* parse psz_src dir */
-        if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
+        if( ( psz_filename = vlc_readdir( p_dir ) ) == NULL )
         {
             break;
         }
@@ -203,7 +194,7 @@ int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
             continue;
         }
 
-        snprintf( dir, sizeof( dir ), "%s%c%s", psz_dir, sep, psz_filename );
+        snprintf( dir, sizeof( dir ), "%s"DIR_SEP"%s", psz_dir, psz_filename );
         free( psz_filename );
 
         if( ParseDirectory( p_intf, psz_root, dir ) )
@@ -232,7 +223,7 @@ int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
             }
             if( f == NULL )
             {
-                f = malloc( sizeof( httpd_file_sys_t ) );
+                f = xmalloc( sizeof( httpd_file_sys_t ) );
                 f->b_handler = false;
             }
 
@@ -308,13 +299,14 @@ int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
 
                 if( b_index && ( p = strstr( f->file, "index." ) ) )
                 {
-                    asprintf( &psz_redir, "%s%s", f->name, p );
-
-                    msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
-                    f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
-                                                     f->name, psz_redir );
+                    if( asprintf( &psz_redir, "%s%s", f->name, p ) != -1 )
+                    {
+                        msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
+                        f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
+                                                         f->name, psz_redir );
 
-                    free( psz_redir );
+                        free( psz_redir );
+                    }
                 }
             }
         }
@@ -337,95 +329,145 @@ void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl,
                            playlist_item_t *p_node, char *name, mvar_t *s,
                            int i_depth )
 {
-    if( p_node != NULL )
+    if( !p_node || !p_node->p_input )
+        return;
+
+    if( p_node->i_children == -1 )
     {
-        if( p_node->i_children == -1 )
-        {
-            char value[512];
-            char *psz;
-            mvar_t *itm = mvar_New( name, "set" );
+        char value[512];
+        char *psz;
+        playlist_item_t * p_item = playlist_CurrentPlayingItem( p_pl );
+        if( !p_item || !p_item->p_input )
+            return;
+
+        mvar_t *itm = mvar_New( name, "set" );
+        if( p_item->p_input == p_node->p_input )
+            mvar_AppendNewVar( itm, "current", "1" );
+        else
+            mvar_AppendNewVar( itm, "current", "0" );
 
-            if( p_pl->status.p_item && p_node &&
-                p_pl->status.p_item->p_input && p_node->p_input &&
-                p_pl->status.p_item->p_input->i_id == p_node->p_input->i_id )
-            {
-                mvar_AppendNewVar( itm, "current", "1" );
-            }
-            else
-            {
-                mvar_AppendNewVar( itm, "current", "0" );
-            }
+        sprintf( value, "%d", p_node->i_id );
+        mvar_AppendNewVar( itm, "index", value );
 
-            sprintf( value, "%d", p_node->i_id );
-            mvar_AppendNewVar( itm, "index", value );
+        psz = input_item_GetName( p_node->p_input );
+        mvar_AppendNewVar( itm, "name", psz );
+        free( psz );
 
-            psz = input_item_GetName( p_node->p_input );
-            mvar_AppendNewVar( itm, "name", psz );
-            free( psz );
+        psz = input_item_GetURI( p_node->p_input );
+        mvar_AppendNewVar( itm, "uri", psz );
+        free( psz );
 
-            psz = input_item_GetURI( p_node->p_input );
-            mvar_AppendNewVar( itm, "uri", psz );
-            free( psz );
+        mvar_AppendNewVar( itm, "type", "Item" );
 
-            sprintf( value, "Item");
-            mvar_AppendNewVar( itm, "type", value );
+        sprintf( value, "%d", i_depth );
+        mvar_AppendNewVar( itm, "depth", value );
 
-            sprintf( value, "%d", i_depth );
-            mvar_AppendNewVar( itm, "depth", value );
+        if( p_node->i_flags & PLAYLIST_RO_FLAG )
+            mvar_AppendNewVar( itm, "ro", "ro" );
+        else
+            mvar_AppendNewVar( itm, "ro", "rw" );
 
-            if( p_node->i_flags & PLAYLIST_RO_FLAG )
-            {
-                mvar_AppendNewVar( itm, "ro", "ro" );
-            }
-            else
-            {
-                mvar_AppendNewVar( itm, "ro", "rw" );
-            }
+        sprintf( value, "%"PRId64, input_item_GetDuration( p_node->p_input ) );
+        mvar_AppendNewVar( itm, "duration", value );
 
-            sprintf( value, "%ld",
-                    (long) input_item_GetDuration( p_node->p_input ) );
-            mvar_AppendNewVar( itm, "duration", value );
+        //Adding extra meta-information to each playlist item
 
-            mvar_AppendVar( s, itm );
-        }
-        else
-        {
-            char value[512];
-            int i_child;
-            mvar_t *itm = mvar_New( name, "set" );
+        psz = input_item_GetTitle( p_node->p_input );
+        mvar_AppendNewVar( itm, "title", psz );
+        free( psz );
 
-            mvar_AppendNewVar( itm, "name", p_node->p_input->psz_name );
-            mvar_AppendNewVar( itm, "uri", p_node->p_input->psz_name );
+        psz = input_item_GetArtist( p_node->p_input );
+        mvar_AppendNewVar( itm, "artist", psz );
+        free( psz );
 
-            sprintf( value, "Node" );
-            mvar_AppendNewVar( itm, "type", value );
+        psz = input_item_GetGenre( p_node->p_input );
+        mvar_AppendNewVar( itm, "genre", psz );
+        free( psz );
 
-            sprintf( value, "%d", p_node->i_id );
-            mvar_AppendNewVar( itm, "index", value );
+        psz = input_item_GetCopyright( p_node->p_input );
+        mvar_AppendNewVar( itm, "copyright", psz );
+        free( psz );
 
-            sprintf( value, "%d", p_node->i_children);
-            mvar_AppendNewVar( itm, "i_children", value );
+        psz = input_item_GetAlbum( p_node->p_input );
+        mvar_AppendNewVar( itm, "album", psz );
+        free( psz );
 
-            sprintf( value, "%d", i_depth );
-            mvar_AppendNewVar( itm, "depth", value );
+        psz = input_item_GetTrackNum( p_node->p_input );
+        mvar_AppendNewVar( itm, "track", psz );
+        free( psz );
 
-            if( p_node->i_flags & PLAYLIST_RO_FLAG )
-            {
-                mvar_AppendNewVar( itm, "ro", "ro" );
-            }
-            else
-            {
-                mvar_AppendNewVar( itm, "ro", "rw" );
-            }
+        psz = input_item_GetDescription( p_node->p_input );
+        mvar_AppendNewVar( itm, "description", psz );
+        free( psz );
 
-            mvar_AppendVar( s, itm );
+        psz = input_item_GetRating( p_node->p_input );
+        mvar_AppendNewVar( itm, "rating", psz );
+        free( psz );
 
-            for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
-                PlaylistListNode( p_intf, p_pl,
-                                      p_node->pp_children[i_child],
-                                      name, s, i_depth + 1);
+        psz = input_item_GetDate( p_node->p_input );
+        mvar_AppendNewVar( itm, "date", psz );
+        free( psz );
 
-        }
+        psz = input_item_GetURL( p_node->p_input );
+        mvar_AppendNewVar( itm, "url", psz );
+        free( psz );
+
+        psz = input_item_GetLanguage( p_node->p_input );
+        mvar_AppendNewVar( itm, "language", psz );
+        free( psz );
+
+        psz = input_item_GetNowPlaying( p_node->p_input );
+        mvar_AppendNewVar( itm, "now_playing", psz );
+        free( psz );
+
+        psz = input_item_GetPublisher( p_node->p_input );
+        mvar_AppendNewVar( itm, "publisher", psz );
+        free( psz );
+
+        psz = input_item_GetEncodedBy( p_node->p_input );
+        mvar_AppendNewVar( itm, "encoded_by", psz );
+        free( psz );
+
+        psz = input_item_GetArtURL( p_node->p_input );
+        mvar_AppendNewVar( itm, "art_url", psz );
+        free( psz );
+
+        psz = input_item_GetTrackID( p_node->p_input );
+        mvar_AppendNewVar( itm, "track_id", psz );
+        free( psz );
+
+        mvar_AppendVar( s, itm );
+    }
+    else
+    {
+        char value[512];
+        int i_child;
+        mvar_t *itm = mvar_New( name, "set" );
+
+        mvar_AppendNewVar( itm, "name", p_node->p_input->psz_name );
+        mvar_AppendNewVar( itm, "uri", p_node->p_input->psz_name );
+
+        mvar_AppendNewVar( itm, "type", "Node" );
+
+        sprintf( value, "%d", p_node->i_id );
+        mvar_AppendNewVar( itm, "index", value );
+
+        sprintf( value, "%d", p_node->i_children);
+        mvar_AppendNewVar( itm, "i_children", value );
+
+        sprintf( value, "%d", i_depth );
+        mvar_AppendNewVar( itm, "depth", value );
+
+        if( p_node->i_flags & PLAYLIST_RO_FLAG )
+            mvar_AppendNewVar( itm, "ro", "ro" );
+        else
+            mvar_AppendNewVar( itm, "ro", "rw" );
+
+        mvar_AppendVar( s, itm );
+
+        for( i_child = 0 ; i_child < p_node->i_children ; i_child++ )
+             PlaylistListNode( p_intf, p_pl, p_node->pp_children[i_child],
+                               name, s, i_depth + 1);
     }
 }
 
@@ -642,10 +684,10 @@ int TestURIParam( char *psz_uri, const char *psz_name )
     return false;
 }
 
-static char *FindURIValue( char *psz_uri, const char *restrict psz_name,
+static const char *FindURIValue( const char *psz_uri, const char *restrict psz_name,
                            size_t *restrict p_len )
 {
-    char *p = psz_uri, *end;
+    const char *p = psz_uri, *end;
     size_t len;
 
     while( (p = strstr( p, psz_name )) )
@@ -687,13 +729,13 @@ static char *FindURIValue( char *psz_uri, const char *restrict psz_name,
     return p;
 }
 
-char *ExtractURIValue( char *restrict psz_uri,
+const char *ExtractURIValue( const char *restrict psz_uri,
                            const char *restrict psz_name,
                            char *restrict psz_buf, size_t bufsize )
 {
     size_t len;
-    char *psz_value = FindURIValue( psz_uri, psz_name, &len );
-    char *psz_next;
+    const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
+    const char *psz_next;
 
     if( psz_value == NULL )
     {
@@ -715,11 +757,11 @@ char *ExtractURIValue( char *restrict psz_uri,
     return psz_next;
 }
 
-char *ExtractURIString( char *restrict psz_uri,
+char *ExtractURIString( const char *restrict psz_uri,
                             const char *restrict psz_name )
 {
     size_t len;
-    char *psz_value = FindURIValue( psz_uri, psz_name, &len );
+    const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
 
     if( psz_value == NULL )
         return NULL;
@@ -825,14 +867,12 @@ static char *FirstOption( char *psz, char *new )
         return NULL;
 }
 
-input_item_t *MRLParse( intf_thread_t *p_intf, char *_psz,
+input_item_t *MRLParse( intf_thread_t *p_intf, const char *mrl,
                                    char *psz_name )
 {
-    char *psz = strdup( _psz );
-    char *s_mrl = psz;
-    char *s_temp;
-    input_item_t * p_input = NULL;
-
+    char *psz = strdup( mrl ), *s_mrl = psz, *s_temp;
+    if( psz == NULL )
+        return NULL;
     /* extract the mrl */
     s_temp = FirstOption( s_mrl, s_mrl );
     if( s_temp == NULL )
@@ -840,7 +880,9 @@ input_item_t *MRLParse( intf_thread_t *p_intf, char *_psz,
         s_temp = s_mrl + strlen( s_mrl );
     }
 
-    p_input = input_ItemNew( p_intf, s_mrl, psz_name );
+    input_item_t *p_input = input_item_New( p_intf, s_mrl, psz_name );
+    if( p_input == NULL )
+        return NULL;
     s_mrl = s_temp;
 
     /* now we can take care of the options */
@@ -853,29 +895,27 @@ input_item_t *MRLParse( intf_thread_t *p_intf, char *_psz,
         {
             s_temp = s_mrl + strlen( s_mrl );
         }
-        input_ItemAddOption( p_input, s_mrl );
+        input_item_AddOption( p_input, s_mrl, VLC_INPUT_OPTION_TRUSTED );
         s_mrl = s_temp;
     }
 
-    free( psz );
     return p_input;
 }
 
 /**********************************************************************
  * RealPath: parse ../, ~ and path stuff
  **********************************************************************/
-char *RealPath( intf_thread_t *p_intf, const char *psz_src )
+char *RealPath( const char *psz_src )
 {
     char *psz_dir;
     char *p;
     int i_len = strlen(psz_src);
-    const char sep = DIR_SEP_CHAR;
 
-    psz_dir = malloc( i_len + 2 );
+    psz_dir = xmalloc( i_len + 2 );
     strcpy( psz_dir, psz_src );
 
     /* Add a trailing sep to ease the .. step */
-    psz_dir[i_len] = sep;
+    psz_dir[i_len] = DIR_SEP_CHAR;
     psz_dir[i_len + 1] = '\0';
 
 #if (DIR_SEP_CHAR != '/')
@@ -883,18 +923,18 @@ char *RealPath( intf_thread_t *p_intf, const char *psz_src )
     p = psz_dir;
     while( (p = strchr( p, '/' )) != NULL )
     {
-        *p = sep;
+        *p = DIR_SEP_CHAR;
     }
 #endif
 
     /* FIXME: this could be O(N) rather than O(N²)... */
     /* Remove multiple separators and /./ */
     p = psz_dir;
-    while( (p = strchr( p, sep )) != NULL )
+    while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
     {
-        if( p[1] == sep )
+        if( p[1] == DIR_SEP_CHAR )
             memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
-        else if( p[1] == '.' && p[2] == sep )
+        else if( p[1] == '.' && p[2] == DIR_SEP_CHAR )
             memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
         else
             p++;
@@ -902,23 +942,26 @@ char *RealPath( intf_thread_t *p_intf, const char *psz_src )
 
     if( psz_dir[0] == '~' )
     {
-        char *dir;
-        asprintf( &dir, "%s%s", config_GetHomeDir(), psz_dir + 1 );
-        free( psz_dir );
-        psz_dir = dir;
+        char *home = config_GetUserDir( VLC_HOME_DIR ), *dir;
+        if( asprintf( &dir, "%s%s", home, psz_dir + 1 ) != -1 )
+        {
+            free( psz_dir );
+            psz_dir = dir;
+        }
+        free( home );
     }
 
     if( strlen(psz_dir) > 2 )
     {
         /* Fix all .. dir */
         p = psz_dir + 3;
-        while( (p = strchr( p, sep )) != NULL )
+        while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
         {
-            if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
+            if( p[-1] == '.' && p[-2] == '.' && p[-3] == DIR_SEP_CHAR )
             {
                 char *q;
                 p[-3] = '\0';
-                if( (q = strrchr( psz_dir, sep )) != NULL )
+                if( (q = strrchr( psz_dir, DIR_SEP_CHAR )) != NULL )
                 {
                     memmove( q + 1, p + 1, strlen(p + 1) + 1 );
                     p = q + 1;
@@ -936,8 +979,8 @@ char *RealPath( intf_thread_t *p_intf, const char *psz_src )
 
     /* Remove trailing sep if there are at least 2 sep in the string
      * (handles the C:\ stuff) */
-    p = strrchr( psz_dir, sep );
-    if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )
+    p = strrchr( psz_dir, DIR_SEP_CHAR );
+    if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, DIR_SEP_CHAR ) )
         *p = '\0';
 
     return psz_dir;