]> git.sesse.net Git - vlc/blobdiff - modules/control/http/http.c
DBus: remove unused macro, check memory allocation
[vlc] / modules / control / http / http.c
index db38c6a0e2a39ea2bf2e406c4797dbef8dbbc2b6..ff46c7d4d5d54c6379420265d31f573f693f33fe 100644 (file)
@@ -22,6 +22,9 @@
  * 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 "http.h"
 
@@ -39,9 +42,6 @@ static void Close( vlc_object_t * );
     "machine, enter 127.0.0.1" )
 #define SRC_TEXT N_( "Source directory" )
 #define SRC_LONGTEXT N_( "Source directory" )
-#define CHARSET_TEXT N_( "Charset" )
-#define CHARSET_LONGTEXT N_( \
-        "Charset declared in Content-Type header (default UTF-8)." )
 #define HANDLERS_TEXT N_( "Handlers" )
 #define HANDLERS_LONGTEXT N_( \
         "List of handler extensions and executable paths (for instance: " \
@@ -68,11 +68,11 @@ vlc_module_begin();
     set_subcategory( SUBCAT_INTERFACE_MAIN );
         add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
         add_string ( "http-src",  NULL, NULL, SRC_TEXT,  SRC_LONGTEXT,  VLC_TRUE );
-        add_string ( "http-charset", "UTF-8", NULL, CHARSET_TEXT, CHARSET_LONGTEXT, VLC_TRUE );
+        add_obsolete_string ( "http-charset" );
 #if defined( HAVE_FORK ) || defined( WIN32 )
         add_string ( "http-handlers", NULL, NULL, HANDLERS_TEXT, HANDLERS_LONGTEXT, VLC_TRUE );
 #endif
-        add_bool   ( "http-album-art", 1, NULL, ART_TEXT, ART_LONGTEXT, VLC_TRUE );
+        add_bool   ( "http-album-art", VLC_FALSE, NULL, ART_TEXT, ART_LONGTEXT, VLC_TRUE );
         set_section( N_("HTTP SSL" ), 0 );
         add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
         add_string ( "http-intf-key",  NULL, NULL, KEY_TEXT,  KEY_LONGTEXT,  VLC_TRUE );
@@ -86,7 +86,6 @@ vlc_module_end();
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-static void Run          ( intf_thread_t *p_intf );
 int  E_(ArtCallback)( httpd_handler_sys_t *p_args,
                           httpd_handler_t *p_handler, char *_p_url,
                           uint8_t *_p_request, int i_type,
@@ -134,14 +133,9 @@ static int Open( vlc_object_t *p_this )
     const char    *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
                   *psz_crl = NULL;
     int           i_port       = 0;
-    char          *psz_src;
+    char          *psz_src = NULL;
 
-    var_Create(p_intf->p_libvlc, "http-host", VLC_VAR_STRING );
-    psz_address = var_GetString(p_intf->p_libvlc, "http-host");
-    if( !psz_address || !*psz_address )
-    {
-        psz_address = config_GetPsz( p_intf, "http-host" );
-    }
+    psz_address = var_CreateGetNonEmptyString( p_intf, "http-host" );
     if( psz_address != NULL )
     {
         char *psz_parser = strchr( psz_address, ':' );
@@ -159,56 +153,13 @@ static int Open( vlc_object_t *p_this )
     {
         return( VLC_ENOMEM );
     }
-    p_sys->p_playlist = NULL;
+
+    p_sys->p_playlist = pl_Yield( p_this );
     p_sys->p_input    = NULL;
     p_sys->p_vlm      = NULL;
     p_sys->psz_address = psz_address;
     p_sys->i_port     = i_port;
-
-    /* determine Content-Type value for HTML pages */
-    psz_src = config_GetPsz( p_intf, "http-charset" );
-    if( psz_src == NULL || !*psz_src )
-    {
-        if( psz_src != NULL ) free( psz_src );
-        psz_src = strdup("UTF-8");
-    }
-
-    p_sys->psz_html_type = malloc( 20 + strlen( psz_src ) );
-    if( p_sys->psz_html_type == NULL )
-    {
-        free( p_sys->psz_address );
-        free( p_sys );
-        free( psz_src );
-        return VLC_ENOMEM ;
-    }
-    sprintf( p_sys->psz_html_type, "text/html; charset=%s", psz_src );
-    msg_Dbg( p_intf, "using charset=%s", psz_src );
-
-    if( strcmp( psz_src, "UTF-8" ) )
-    {
-        char psz_encoding[strlen( psz_src ) + sizeof( "//translit")];
-        sprintf( psz_encoding, "%s//translit", psz_src);
-
-        p_sys->iconv_from_utf8 = vlc_iconv_open( psz_encoding, "UTF-8" );
-        if( p_sys->iconv_from_utf8 == (vlc_iconv_t)-1 )
-            msg_Warn( p_intf, "unable to perform charset conversion to %s",
-                      psz_encoding );
-        else
-        {
-            p_sys->iconv_to_utf8 = vlc_iconv_open( "UTF-8", psz_src );
-            if( p_sys->iconv_to_utf8 == (vlc_iconv_t)-1 )
-                msg_Warn( p_intf,
-                          "unable to perform charset conversion from %s",
-                          psz_src );
-        }
-    }
-    else
-    {
-        p_sys->iconv_from_utf8 = p_sys->iconv_to_utf8 = (vlc_iconv_t)-1;
-    }
-
-    p_sys->psz_charset = psz_src;
-    psz_src = NULL;
+    p_sys->p_art_handler = NULL;
 
     /* determine file handler associations */
     p_sys->i_handlers = 0;
@@ -250,8 +201,7 @@ static int Open( vlc_object_t *p_this )
             TAB_APPEND( p_sys->i_handlers, p_sys->pp_handlers, p_handler );
         }
     }
-    if( psz_src != NULL )
-        free( psz_src );
+    free( psz_src );
 #endif
 
     /* determine SSL configuration */
@@ -260,9 +210,9 @@ static int Open( vlc_object_t *p_this )
     {
         msg_Dbg( p_intf, "enabling TLS for HTTP interface (cert file: %s)",
                  psz_cert );
-        psz_key = config_GetPsz( p_intf, "http-intf-key" );
-        psz_ca = config_GetPsz( p_intf, "http-intf-ca" );
-        psz_crl = config_GetPsz( p_intf, "http-intf-crl" );
+        psz_key = var_GetNonEmptyString( p_intf, "http-intf-key" );
+        psz_ca = var_GetNonEmptyString( p_intf, "http-intf-ca" );
+        psz_crl = var_GetNonEmptyString( p_intf, "http-intf-crl" );
 
         if( i_port <= 0 )
             i_port = 8443;
@@ -281,7 +231,7 @@ static int Open( vlc_object_t *p_this )
     if( p_sys->p_httpd_host == NULL )
     {
         msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
-        free( p_sys->psz_html_type );
+        pl_Release( p_this );
         free( p_sys->psz_address );
         free( p_sys );
         return VLC_EGENERIC;
@@ -292,6 +242,7 @@ static int Open( vlc_object_t *p_this )
 
         /* Ugly hack to run several HTTP servers on different ports */
         snprintf( psz_tmp, sizeof (psz_tmp), "%s:%d", psz_address, i_port + 1 );
+        var_Create(p_intf->p_libvlc, "http-host", VLC_VAR_STRING );
         var_SetString( p_intf->p_libvlc, "http-host", psz_tmp );
     }
 
@@ -311,19 +262,20 @@ static int Open( vlc_object_t *p_this )
 
     if( ( psz_src == NULL ) || ( *psz_src == '\0' ) )
     {
-        static char const* ppsz_paths[] = {
+        const char *data_path = config_GetDataDir ();
+        char buf[strlen (data_path) + sizeof ("/http")];
+        snprintf (buf, sizeof (buf), "%s/http", data_path);
+
+        const char const* ppsz_paths[] = {
             "share/http",
             "../share/http",
-            DATA_PATH"/http",
+            buf,
             NULL
         };
         unsigned i;
 
-        if( psz_src != NULL )
-        {
-            free( psz_src );
-            psz_src = NULL;
-        }
+        free( psz_src );
+        psz_src = NULL;
 
         for( i = 0; ppsz_paths[i] != NULL; i++ )
             if( !DirectoryCheck( ppsz_paths[i] ) )
@@ -348,6 +300,13 @@ static int Open( vlc_object_t *p_this )
     }
 
     E_(ParseDirectory)( p_intf, psz_src, psz_src );
+    if( p_sys->i_files <= 0 )
+    {
+        msg_Err( p_intf, "cannot find any file in directory %s", psz_src );
+        goto failed;
+    }
+
+    free( psz_src );
 
     if( config_GetInt( p_intf, "http-album-art" ) )
     {
@@ -365,33 +324,18 @@ static int Open( vlc_object_t *p_this )
         h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
                                          "/art", NULL, NULL, NULL,
                                          E_(ArtCallback), h );
-        TAB_APPEND( p_sys->i_handlers, p_sys->pp_handlers, h->p_handler );
+        p_sys->p_art_handler = h->p_handler;
     }
 
-    if( p_sys->i_files <= 0 )
-    {
-        msg_Err( p_intf, "cannot find any file in directory %s", psz_src );
-        goto failed;
-    }
-    p_intf->pf_run = Run;
-    free( psz_src );
-
     return VLC_SUCCESS;
 
 failed:
-    if( psz_src ) free( psz_src );
-    if( p_sys->pp_files )
-    {
-        free( p_sys->pp_files );
-    }
+    free( psz_src );
+    free( p_sys->pp_files );
     httpd_HostDelete( p_sys->p_httpd_host );
     free( p_sys->psz_address );
-    free( p_sys->psz_html_type );
-    if( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
-        vlc_iconv_close( p_sys->iconv_from_utf8 );
-    if( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
-        vlc_iconv_close( p_sys->iconv_to_utf8 );
     free( p_sys );
+    pl_Release( p_this );
     return VLC_EGENERIC;
 }
 
@@ -424,10 +368,7 @@ static void Close ( vlc_object_t *p_this )
         free( p_sys->pp_files[i]->name );
         free( p_sys->pp_files[i] );
     }
-    if( p_sys->pp_files )
-    {
-        free( p_sys->pp_files );
-    }
+    free( p_sys->pp_files );
     for( i = 0; i < p_sys->i_handlers; i++ )
     {
         http_association_t *p_handler = p_sys->pp_handlers[i];
@@ -441,61 +382,12 @@ static void Close ( vlc_object_t *p_this )
     }
     if( p_sys->i_handlers )
         free( p_sys->pp_handlers );
+    if( p_sys->p_art_handler )
+        httpd_HandlerDelete( p_sys->p_art_handler );
     httpd_HostDelete( p_sys->p_httpd_host );
     free( p_sys->psz_address );
-    free( p_sys->psz_html_type );
-
-    if( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
-        vlc_iconv_close( p_sys->iconv_from_utf8 );
-    if( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
-        vlc_iconv_close( p_sys->iconv_to_utf8 );
     free( p_sys );
-}
-
-/*****************************************************************************
- * Run: http interface thread
- *****************************************************************************/
-static void Run( intf_thread_t *p_intf )
-{
-    intf_sys_t     *p_sys = p_intf->p_sys;
-
-    while( !intf_ShouldDie( p_intf ) )
-    {
-        /* get the playlist */
-        if( p_sys->p_playlist == NULL )
-        {
-            p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
-        }
-
-        /* Manage the input part */
-        if( p_sys->p_input == NULL )
-        {
-            if( p_sys->p_playlist )
-            {
-                p_sys->p_input = p_sys->p_playlist->p_input;
-            }
-        }
-        else if( p_sys->p_input->b_dead || p_sys->p_input->b_die )
-        {
-            p_sys->p_input = NULL;
-        }
-
-
-        /* Wait a bit */
-        msleep( INTF_IDLE_SLEEP );
-    }
-
-    if( p_sys->p_input )
-    {
-        vlc_object_release( p_sys->p_input );
-        p_sys->p_input = NULL;
-    }
-
-    if( p_sys->p_playlist )
-    {
-        vlc_object_release( p_sys->p_playlist );
-        p_sys->p_playlist = NULL;
-    }
+    pl_Release( p_this );
 }
 
 /****************************************************************************
@@ -529,6 +421,7 @@ static void ParseExecute( httpd_file_sys_t *p_args, char *p_buffer,
                           int i_buffer, char *p_request,
                           char **pp_data, int *pi_data )
 {
+    intf_sys_t *p_sys = p_args->p_intf->p_sys;
     int i_request = p_request != NULL ? strlen( p_request ) : 0;
     char *dst;
     vlc_value_t val;
@@ -540,9 +433,12 @@ static void ParseExecute( httpd_file_sys_t *p_args, char *p_buffer,
     const char *state;
     char stats[20];
 
-#define p_sys p_args->p_intf->p_sys
+    assert( p_sys->p_input == NULL );
+    /* FIXME: proper locking anyone? */
+    p_sys->p_input = p_sys->p_playlist->p_input;
     if( p_sys->p_input )
     {
+        vlc_object_yield( p_sys->p_input );
         var_Get( p_sys->p_input, "position", &val);
         sprintf( position, "%d" , (int)((val.f_float) * 100.0));
         var_Get( p_sys->p_input, "time", &val);
@@ -579,7 +475,6 @@ static void ParseExecute( httpd_file_sys_t *p_args, char *p_buffer,
         sprintf( length, "%d", 0 );
         state = "stop";
     }
-#undef p_sys
 
     aout_VolumeGet( p_args->p_intf, &i_volume );
     sprintf( volume, "%d", (int)i_volume );
@@ -602,10 +497,9 @@ static void ParseExecute( httpd_file_sys_t *p_args, char *p_buffer,
     E_(mvar_AppendNewVar)( p_args->vars, "stream_length", length );
     E_(mvar_AppendNewVar)( p_args->vars, "volume", volume );
     E_(mvar_AppendNewVar)( p_args->vars, "stream_state", state );
-    E_(mvar_AppendNewVar)( p_args->vars, "charset", ((intf_sys_t *)p_args->p_intf->p_sys)->psz_charset );
+    E_(mvar_AppendNewVar)( p_args->vars, "charset", "UTF-8" );
 
     /* Stats */
-#define p_sys p_args->p_intf->p_sys
     if( p_sys->p_input )
     {
         /* FIXME: Workarround a stupid assert in input_GetItem */
@@ -638,7 +532,6 @@ static void ParseExecute( httpd_file_sys_t *p_args, char *p_buffer,
             vlc_mutex_unlock( &p_item->p_stats->lock );
         }
     }
-#undef p_sys
 
     E_(SSInit)( &p_args->stack );
 
@@ -653,6 +546,11 @@ static void ParseExecute( httpd_file_sys_t *p_args, char *p_buffer,
     *dst     = '\0';
     *pi_data = dst - *pp_data;
 
+    if( p_sys->p_input != NULL )
+    {
+        vlc_object_release( p_sys->p_input );
+        p_sys->p_input = NULL;
+    }
     E_(SSClean)( &p_args->stack );
     E_(mvar_Delete)( p_args->vars );
 }
@@ -662,6 +560,7 @@ int  E_(HttpCallback)( httpd_file_sys_t *p_args,
                        uint8_t *_p_request,
                        uint8_t **_pp_data, int *pi_data )
 {
+    VLC_UNUSED(p_file);
     char *p_request = (char *)_p_request;
     char **pp_data = (char **)_pp_data;
     FILE *f;
@@ -706,6 +605,7 @@ int  E_(HandlerCallback)( httpd_handler_sys_t *p_args,
                           char *psz_remote_addr, char *psz_remote_host,
                           uint8_t **_pp_data, int *pi_data )
 {
+    VLC_UNUSED(p_handler); VLC_UNUSED(_p_in);
     char *p_url = (char *)_p_url;
     char *p_request = (char *)_p_request;
     char **pp_data = (char **)_pp_data;
@@ -716,7 +616,7 @@ int  E_(HandlerCallback)( httpd_handler_sys_t *p_args,
     char **ppsz_env = NULL;
     char *psz_tmp;
     char sep;
-    int  i_buffer;
+    size_t i_buffer;
     char *p_buffer;
     char *psz_cwd, *psz_file = NULL;
     int i_ret;
@@ -887,8 +787,7 @@ int  E_(HandlerCallback)( httpd_handler_sys_t *p_args,
                 NULL );
     TAB_REMOVE( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
                 psz_file );
-    if( psz_cwd != NULL )
-        free( psz_cwd );
+    free( psz_cwd );
     while( i_env )
         TAB_REMOVE( i_env, ppsz_env, ppsz_env[0] );
 
@@ -933,8 +832,10 @@ int  E_(ArtCallback)( httpd_handler_sys_t *p_args,
                           char *psz_remote_addr, char *psz_remote_host,
                           uint8_t **pp_data, int *pi_data )
 {
-    uint8_t *p_data = NULL;
-    int i_data = 0;
+    VLC_UNUSED(p_handler); VLC_UNUSED(_p_url); VLC_UNUSED(i_type); 
+    VLC_UNUSED(p_in); VLC_UNUSED(i_in); VLC_UNUSED(psz_remote_addr); 
+    VLC_UNUSED(psz_remote_host); 
+
     char *psz_art = NULL;
     intf_thread_t *p_intf = p_args->file.p_intf;
     intf_sys_t *p_sys = p_intf->p_sys;
@@ -944,7 +845,7 @@ int  E_(ArtCallback)( httpd_handler_sys_t *p_args,
 
     psz_id[0] = '\0';
     if( p_request )
-        E_(ExtractURIValue)( p_request, "id", psz_id, 15 );
+        E_(ExtractURIValue)( (char *)p_request, "id", psz_id, 15 );
     i_id = atoi( psz_id );
     if( i_id )
     {
@@ -970,7 +871,8 @@ int  E_(ArtCallback)( httpd_handler_sys_t *p_args,
         FILE *f;
         char *psz_ext;
         char *psz_header;
-        int i_header_size;
+        char *p_data = NULL;
+        int i_header_size, i_data;
 
         if( ( f = utf8_fopen( psz_art + strlen( "file://" ), "r" ) ) == NULL )
         {