]> git.sesse.net Git - vlc/blobdiff - modules/control/http.c
string review.
[vlc] / modules / control / http.c
index 6eceff2a359df49127315433e8e2efde03bf1af5..c108c46b676bd1843106b09dc038df9ff0305bd0 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * http.c :  http mini-server ;)
  *****************************************************************************
- * Copyright (C) 2001 VideoLAN
- * $Id: http.c,v 1.32 2003/11/12 02:43:33 garf Exp $
+ * Copyright (C) 2001-2004 VideoLAN
+ * $Id: http.c,v 1.49 2004/01/18 07:35:31 fenrir Exp $
  *
  * Authors: Gildas Bazin <gbazin@netcourrier.com>
  *          Laurent Aimar <fenrir@via.ecp.fr>
@@ -297,10 +297,12 @@ static int Activate( vlc_object_t *p_this )
         goto failed;
     }
     p_intf->pf_run = Run;
+    free( psz_src );
 
     return VLC_SUCCESS;
 
 failed:
+    if( psz_src ) free( psz_src );
     free( p_sys->pp_files );
     p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
                                         p_sys->p_httpd_host );
@@ -585,6 +587,7 @@ static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
             if( !f )
             {
                 msg_Err( p_intf, "Out of memory" );
+                closedir( p_dir );
                 return( VLC_ENOMEM );
             }
             f->p_intf  = p_intf;
@@ -595,6 +598,8 @@ static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
             if( !f->name || !f->mime )
             {
                 msg_Err( p_intf , "Unable to parse directory" );
+                closedir( p_dir );
+                free( f );
                 return( VLC_ENOMEM );
             }
             msg_Dbg( p_intf, "file=%s (url=%s mime=%s)",
@@ -622,6 +627,7 @@ static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
                 if( !f )
                 {
                     msg_Err( p_intf, "Out of memory" );
+                    closedir( p_dir );
                     return( VLC_ENOMEM );
                 }
                 f->p_intf  = p_intf;
@@ -659,6 +665,9 @@ static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
     {
         free( password );
     }
+
+    closedir( p_dir );
+
     return VLC_SUCCESS;
 }
 
@@ -932,6 +941,9 @@ static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
 
         mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->psz_name );
 
+        sprintf( value, "%d", p_pl->pp_items[i]->i_group );
+        mvar_AppendNewVar( itm, "group", value );
+
         mvar_AppendVar( s, itm );
     }
     vlc_mutex_unlock( &p_pl->object_lock );
@@ -1014,6 +1026,10 @@ static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
         free( info.info[i].psz_name );
         free( info.info[i].psz_value );
     }
+    if( info.i_count > 0 )
+    {
+        free( info.info );
+    }
 
     return s;
 }
@@ -1751,7 +1767,7 @@ static void MacroDo( httpd_file_callback_args_t *p_args,
                     uri_decode_url_encoded( mrl );
                     p_item = parse_MRL( mrl );
 
-                    if( p_item == NULL )
+                    if( !p_item || !p_item->psz_uri || !*p_item->psz_uri )
                     {
                         msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
                     } else
@@ -1809,7 +1825,7 @@ static void MacroDo( httpd_file_callback_args_t *p_args,
                 {
                     int i_item, *p_items = NULL, i_nb_items = 0;
                     char item[512], *p_parser = p_request;
-                    int i,j,temp;
+                    int i,j;
 
                     /* Get the list of items to keep */
                     while( (p_parser =
@@ -1818,40 +1834,27 @@ static void MacroDo( httpd_file_callback_args_t *p_args,
                         if( !*item ) continue;
 
                         i_item = atoi( item );
-                        p_items = realloc( p_items, (i_nb_items + 3) *
+                        p_items = realloc( p_items, (i_nb_items + 1) *
                                            sizeof(int) );
-                        p_items[i_nb_items + 1] = i_item;
+                        p_items[i_nb_items] = i_item;
                         i_nb_items++;
                     }
 
-                    /* sort item list */
-                    for( i=1 ; i < (i_nb_items + 1) ; i++)
+                    /* The items need to be deleted from in reversed order */
+                    for( i = p_sys->p_playlist->i_size - 1; i >= 0 ; i-- )
                     {
-                        for( j=(i+1) ; j < (i_nb_items + 1) ; j++)
+                        /* Check if the item is in the keep list */
+                        for( j = 0 ; j < i_nb_items ; j++ )
                         {
-                            if( p_items[j] > p_items[i] )
-                            {
-                                temp = p_items[j];
-                                p_items[j] = p_items[i];
-                                p_items[i] = temp;
-                            }
+                            if( p_items[j] == i ) break;
                         }
-                    }
-
-                    p_items[0] = p_sys->p_playlist->i_size;
-                    p_items[ i_nb_items + 1 ] = -1;
-
-                    /* The items need to be deleted from in reversed order */
-                        for( i=0 ; i <= i_nb_items ; i++ )
+                        if( j == i_nb_items )
                         {
-                            for( j = (p_items[i] - 1) ; j > p_items[i + 1] ; j-- )
-                            {
-                                playlist_Delete( p_sys->p_playlist,
-                                                 j );
-                                msg_Dbg( p_intf, "requested playlist delete: %d",
-                                         j );
-                            }
+                            playlist_Delete( p_sys->p_playlist, i );
+                            msg_Dbg( p_intf, "requested playlist delete: %d",
+                                     i );
                         }
+                    }
 
                     if( p_items ) free( p_items );
                     break;
@@ -1874,8 +1877,8 @@ static void MacroDo( httpd_file_callback_args_t *p_args,
                     uri_extract_value( p_request, "type", type, 12 );
                     uri_extract_value( p_request, "order", order, 2 );
 
-                    if( order[0] == '0' ) i_order = SORT_NORMAL;
-                    else i_order = SORT_REVERSE;
+                    if( order[0] == '0' ) i_order = ORDER_NORMAL;
+                    else i_order = ORDER_REVERSE;
 
                     if( !strcmp( type , "title" ) )
                     {
@@ -1976,6 +1979,7 @@ static void MacroDo( httpd_file_callback_args_t *p_args,
                 case MVLC_STRING:
                     psz = config_GetPsz( p_intf, m->param1 );
                     sprintf( value, "%s", psz ? psz : "" );
+                    if( psz ) free( psz );
                     break;
                 default:
                     sprintf( value, "invalid type(%s) in set", m->param2 );
@@ -2047,6 +2051,8 @@ static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t
                     break;
             }
 
+            MacroClean( &m );
+
             if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
                 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
             {
@@ -2280,11 +2286,11 @@ static int  http_get( httpd_file_callback_args_t *p_args,
         p += sprintf( p, "<body>\n" );
         p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
         p += sprintf( p, "<hr />\n" );
-        p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
+        p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
         p += sprintf( p, "</body>\n" );
         p += sprintf( p, "</html>\n" );
 
-        *pi_data = strlen( *pp_data ) + 1;
+        *pi_data = strlen( *pp_data );
 
         return VLC_SUCCESS;
     }
@@ -2362,11 +2368,12 @@ static int  http_get( httpd_file_callback_args_t *p_args,
         /* we parse executing all  <vlc /> macros */
         Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
 
-        *dst++ = '\0';
+        *dst     = '\0';
         *pi_data = dst - *pp_data;
 
         SSClean( &p_args->stack );
         mvar_Delete( p_args->vars );
+        free( p_buffer );
     }
 
     fclose( f );
@@ -2380,9 +2387,16 @@ static int  http_get( httpd_file_callback_args_t *p_args,
 static char *uri_extract_value( char *psz_uri, char *psz_name,
                                 char *psz_value, int i_value_max )
 {
-    char *p;
+    char *p = psz_uri;
+
+    while( (p = strstr( p, psz_name )) )
+    {
+        /* Verify that we are dealing with a post/get argument */
+        if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
+            break;
+        p++;
+    }
 
-    p = strstr( psz_uri, psz_name );
     if( p )
     {
         int i_len;
@@ -2599,8 +2613,8 @@ static void  EvaluateRPN( mvar_t  *vars, rpn_stack_t *st, char *exp )
         }
         else if( !strcmp( s, "-" ) )
         {
-            int i = SSPopN( st, vars );
             int j = SSPopN( st, vars );
+            int i = SSPopN( st, vars );
             SSPushN( st, i - j );
         }
         else if( !strcmp( s, "*" ) )
@@ -2611,8 +2625,8 @@ static void  EvaluateRPN( mvar_t  *vars, rpn_stack_t *st, char *exp )
         {
             int i, j;
 
-            i = SSPopN( st, vars );
             j = SSPopN( st, vars );
+            i = SSPopN( st, vars );
 
             SSPushN( st, j != 0 ? i / j : 0 );
         }
@@ -2620,8 +2634,8 @@ static void  EvaluateRPN( mvar_t  *vars, rpn_stack_t *st, char *exp )
         {
             int i, j;
 
-            i = SSPopN( st, vars );
             j = SSPopN( st, vars );
+            i = SSPopN( st, vars );
 
             SSPushN( st, j != 0 ? i % j : 0 );
         }
@@ -2632,37 +2646,37 @@ static void  EvaluateRPN( mvar_t  *vars, rpn_stack_t *st, char *exp )
         }
         else if( !strcmp( s, "<" ) )
         {
-            int i = SSPopN( st, vars );
             int j = SSPopN( st, vars );
+            int i = SSPopN( st, vars );
 
             SSPushN( st, i < j ? -1 : 0 );
         }
         else if( !strcmp( s, ">" ) )
         {
-            int i = SSPopN( st, vars );
             int j = SSPopN( st, vars );
+            int i = SSPopN( st, vars );
 
             SSPushN( st, i > j ? -1 : 0 );
         }
         else if( !strcmp( s, "<=" ) )
         {
-            int i = SSPopN( st, vars );
             int j = SSPopN( st, vars );
+            int i = SSPopN( st, vars );
 
             SSPushN( st, i <= j ? -1 : 0 );
         }
         else if( !strcmp( s, ">=" ) )
         {
-            int i = SSPopN( st, vars );
             int j = SSPopN( st, vars );
+            int i = SSPopN( st, vars );
 
             SSPushN( st, i >= j ? -1 : 0 );
         }
         /* 3. string functions */
         else if( !strcmp( s, "strcat" ) )
         {
-            char *s1 = SSPop( st );
             char *s2 = SSPop( st );
+            char *s1 = SSPop( st );
             char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
 
             strcpy( str, s1 );
@@ -2675,8 +2689,8 @@ static void  EvaluateRPN( mvar_t  *vars, rpn_stack_t *st, char *exp )
         }
         else if( !strcmp( s, "strcmp" ) )
         {
-            char *s1 = SSPop( st );
             char *s2 = SSPop( st );
+            char *s1 = SSPop( st );
 
             SSPushN( st, strcmp( s1, s2 ) );
             free( s1 );
@@ -2684,9 +2698,9 @@ static void  EvaluateRPN( mvar_t  *vars, rpn_stack_t *st, char *exp )
         }
         else if( !strcmp( s, "strncmp" ) )
         {
-            char *s1 = SSPop( st );
-            char *s2 = SSPop( st );
             int n = SSPopN( st, vars );
+            char *s2 = SSPop( st );
+            char *s1 = SSPop( st );
 
             SSPushN( st, strncmp( s1, s2 , n ) );
             free( s1 );
@@ -2729,8 +2743,8 @@ static void  EvaluateRPN( mvar_t  *vars, rpn_stack_t *st, char *exp )
         }
         else if( !strcmp( s, "store" ) )
         {
-            char *name  = SSPop( st );
             char *value = SSPop( st );
+            char *name  = SSPop( st );
 
             mvar_PushNewVar( vars, name, value );
             free( name );
@@ -2866,7 +2880,7 @@ playlist_item_t * parse_MRL( char *psz )
     char *s_temp;
     int i = 0;
     int i_options = 0;
-    playlist_item_t * p_item;
+    playlist_item_t * p_item = NULL;
 
     /* In case there is spaces before the mrl */
     while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
@@ -2875,11 +2889,17 @@ playlist_item_t * parse_MRL( char *psz )
     }
 
     /* extract the mrl */
-    s_temp = Find_end_MRL( s_mrl );
-
+    s_temp = strstr( s_mrl , " :" );
     if( s_temp == NULL )
     {
-        return NULL;
+        s_temp = s_mrl + strlen( s_mrl );
+    } else
+    {
+        while( (*s_temp == ' ') && (s_temp != s_mrl ) )
+        {
+            s_temp--;
+        }
+        s_temp++;
     }
 
     /* if the mrl is between " or ', we must remove them */
@@ -2914,13 +2934,18 @@ playlist_item_t * parse_MRL( char *psz )
                 if( s_temp == NULL )
                 {
                     i_error = 1;
-                } else
+                }
+                else
                 {
                     i_options++;
-                    ppsz_options = (char **)realloc( ppsz_options , i_options * sizeof(char *) );
-                    ppsz_options[ i_options - 1 ] = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
+                    ppsz_options = realloc( ppsz_options , i_options *
+                                            sizeof(char *) );
+                    ppsz_options[ i_options - 1 ] =
+                        malloc( (s_temp - s_mrl + 1) * sizeof(char) );
+
+                    strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
+                             s_temp - s_mrl );
 
-                    strncpy( ppsz_options[ i_options - 1 ] , s_mrl , s_temp - s_mrl );
                     /* don't forget to finish the string with a '\0' */
                     (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
 
@@ -2939,35 +2964,29 @@ playlist_item_t * parse_MRL( char *psz )
     if( i_error != 0 )
     {
         free( mrl );
-        for( i = 0 ; i < i_options ; i++ )
-        {
-            free( ppsz_options[i] );
-        }
-        free( ppsz_options );
-        return NULL;
-    } else
+    }
+    else
     {
         /* now create an item */
         p_item = malloc( sizeof( playlist_item_t ) );
-
+        memset( p_item, 0, sizeof( playlist_item_t ) );
         p_item->psz_name   = mrl;
-        p_item->psz_uri    = mrl;
-        p_item->psz_author = strdup( "" );
+        p_item->psz_uri    = strdup( mrl );
         p_item->i_duration = -1;
-        p_item->i_type = 0;
-        p_item->i_status = 0;
-        p_item->b_autodeletion = VLC_FALSE;
         p_item->b_enabled = VLC_TRUE;
         p_item->i_group = PLAYLIST_TYPE_MANUAL;
 
-        p_item->ppsz_options = NULL;
-        p_item->i_options = i_options;
-
-        if( i_options )
+        for( i = 0 ; i< i_options ; i++ )
         {
-            p_item->ppsz_options = ppsz_options;
+            playlist_AddItemOption( p_item, ppsz_options[i] );
         }
+    }
 
-        return p_item;
+    for( i = 0 ; i < i_options ; i++ )
+    {
+        free( ppsz_options[i] );
     }
+    free( ppsz_options );
+
+    return p_item;
 }