]> git.sesse.net Git - vlc/blobdiff - modules/control/http.c
* modules/mux/mpeg/ts.c: More sensible defaults for configuration options.
[vlc] / modules / control / http.c
index ca87006d72cb85232238d6733e9263dc5aa89bfc..54b8280f9e851212d9df8f4798efdf4b7a232f6f 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * http.c :  http mini-server ;)
  *****************************************************************************
- * Copyright (C) 2001-2004 VideoLAN
+ * Copyright (C) 2001-2005 VideoLAN
  * $Id$
  *
  * Authors: Gildas Bazin <gbazin@netcourrier.com>
@@ -40,6 +40,7 @@
 #include "vlc_httpd.h"
 #include "vlc_vlm.h"
 #include "vlc_tls.h"
+#include "charset.h"
 
 #ifdef HAVE_SYS_STAT_H
 #   include <sys/stat.h>
@@ -57,8 +58,7 @@
 #   include <io.h>
 #endif
 
-#if (!defined( WIN32 ) || defined(__MINGW32__))
-/* Mingw has its own version of dirent */
+#ifdef HAVE_DIRENT_H
 #   include <dirent.h>
 #endif
 
@@ -90,11 +90,13 @@ static void Close( vlc_object_t * );
 #define CRL_LONGTEXT N_( "HTTP interace Certificates Revocation List file" )
 
 vlc_module_begin();
+    set_shortname( _("HTTP"));
     set_description( _("HTTP remote control interface") );
     set_category( CAT_INTERFACE );
     set_subcategory( SUBCAT_INTERFACE_GENERAL );
         add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
         add_string ( "http-src",  NULL, NULL, SRC_TEXT,  SRC_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 );
         add_string ( "http-intf-ca",   NULL, NULL, CA_TEXT,   CA_LONGTEXT,   VLC_TRUE );
@@ -146,8 +148,7 @@ static int uri_test_param( char *psz_uri, const char *psz_name );
 static void uri_decode_url_encoded( char *psz );
 
 static char *Find_end_MRL( char *psz );
-
-static playlist_item_t * parse_MRL( intf_thread_t * , char *psz );
+static playlist_item_t *parse_MRL( intf_thread_t * , char *psz );
 
 /*****************************************************************************
  *
@@ -173,6 +174,7 @@ struct httpd_file_sys_t
     intf_thread_t    *p_intf;
     httpd_file_t     *p_file;
     httpd_redirect_t *p_redir;
+    httpd_redirect_t *p_redir2;
 
     char          *file;
     char          *name;
@@ -194,6 +196,7 @@ struct intf_sys_t
     playlist_t          *p_playlist;
     input_thread_t      *p_input;
     vlm_t               *p_vlm;
+    char                *psz_html_type;
 };
 
 
@@ -235,6 +238,24 @@ static int Open( vlc_object_t *p_this )
     p_sys->p_input    = NULL;
     p_sys->p_vlm      = NULL;
 
+    /* determine Content-Type value for HTML pages */
+    vlc_current_charset(&psz_src);
+    if( psz_src == NULL )
+    {
+        free( p_sys );
+        return VLC_ENOMEM;
+    }
+    p_sys->psz_html_type = malloc( 20 + strlen( psz_src ) );
+    if( p_sys->psz_html_type == NULL )
+    {
+        free( p_sys );
+        free( psz_src );
+        return VLC_ENOMEM ;
+    }
+    sprintf( p_sys->psz_html_type, "text/html; charset=%s", psz_src );
+    free( psz_src );
+
+    /* determine SSL configuration */
     psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
     if ( psz_cert != NULL )
     {
@@ -248,6 +269,7 @@ static int Open( vlc_object_t *p_this )
         if ( p_tls == NULL )
         {
             msg_Err( p_intf, "TLS initialization error" );
+            free( p_sys->psz_html_type );
             free( p_sys );
             return VLC_EGENERIC;
         }
@@ -257,6 +279,7 @@ static int Open( vlc_object_t *p_this )
         {
             msg_Err( p_intf, "TLS CA error" );
             tls_ServerDelete( p_tls );
+            free( p_sys->psz_html_type );
             free( p_sys );
             return VLC_EGENERIC;
         }
@@ -266,6 +289,7 @@ static int Open( vlc_object_t *p_this )
         {
             msg_Err( p_intf, "TLS CRL error" );
             tls_ServerDelete( p_tls );
+            free( p_sys->psz_html_type );
             free( p_sys );
             return VLC_EGENERIC;
         }
@@ -289,6 +313,8 @@ static int Open( vlc_object_t *p_this )
         msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
         if ( p_tls != NULL )
             tls_ServerDelete( p_tls );
+
+        free( p_sys->psz_html_type );
         free( p_sys );
         return VLC_EGENERIC;
     }
@@ -301,16 +327,12 @@ static int Open( vlc_object_t *p_this )
     p_sys->i_files  = 0;
     p_sys->pp_files = NULL;
 
-#if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
-        ( defined(WIN32) && !defined(UNDER_CE ) )
+#if defined(SYS_DARWIN) || defined(SYS_BEOS) || defined(WIN32)
     if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
     {
         char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
         psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
-        if( !psz_src )
-        {
-            return VLC_ENOMEM;
-        }
+        if( !psz_src ) return VLC_ENOMEM;
 #if defined(WIN32)
         sprintf( psz_src, "%s/http", psz_vlcpath);
 #else
@@ -366,6 +388,7 @@ failed:
         free( p_sys->pp_files );
     }
     httpd_HostDelete( p_sys->p_httpd_host );
+    free( p_sys->psz_html_type );
     free( p_sys );
     return VLC_EGENERIC;
 }
@@ -388,9 +411,9 @@ void Close ( vlc_object_t *p_this )
     {
        httpd_FileDelete( p_sys->pp_files[i]->p_file );
        if( p_sys->pp_files[i]->p_redir )
-       {
            httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
-       }
+       if( p_sys->pp_files[i]->p_redir2 )
+           httpd_RedirectDelete( p_sys->pp_files[i]->p_redir2 );
 
        free( p_sys->pp_files[i]->file );
        free( p_sys->pp_files[i]->name );
@@ -402,6 +425,7 @@ void Close ( vlc_object_t *p_this )
     }
     httpd_HostDelete( p_sys->p_httpd_host );
 
+    free( p_sys->psz_html_type );
     free( p_sys );
 }
 
@@ -464,7 +488,7 @@ static void Run( intf_thread_t *p_intf )
 /****************************************************************************
  * FileToUrl: create a good name for an url from filename
  ****************************************************************************/
-static char *FileToUrl( char *name )
+static char *FileToUrl( char *name, vlc_bool_t *pb_index )
 {
     char *url, *p;
 
@@ -500,12 +524,14 @@ static char *FileToUrl( char *name )
     }
 #endif
 
+    *pb_index = VLC_FALSE;
     /* index.* -> / */
     if( ( p = strrchr( url, '/' ) ) != NULL )
     {
         if( !strncmp( p, "/index.", 7 ) )
         {
             p[1] = '\0';
+            *pb_index = VLC_TRUE;
         }
     }
     return url;
@@ -594,12 +620,14 @@ static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
         if( ParseDirectory( p_intf, psz_root, dir ) )
         {
             httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
+            vlc_bool_t b_index;
 
             f->p_intf  = p_intf;
             f->p_file = NULL;
             f->p_redir = NULL;
+            f->p_redir2 = NULL;
             f->file = strdup( dir );
-            f->name = FileToUrl( &dir[strlen( psz_root )] );
+            f->name = FileToUrl( &dir[strlen( psz_root )], &b_index );
             f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
 
             if( !f->name )
@@ -613,7 +641,8 @@ static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
                      f->file, f->name );
 
             f->p_file = httpd_FileNew( p_sys->p_httpd_host,
-                                       f->name, f->b_html ? "text/html" : NULL,
+                                       f->name,
+                                       f->b_html ? p_sys->psz_html_type : NULL,
                                        user, password,
                                        HttpCallback, f );
 
@@ -621,15 +650,29 @@ static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
             {
                 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
             }
-            /* For rep/ add a redir from rep to rep/ */
+            /* for url that ends by / add
+             *  - a redirect from rep to rep/
+             *  - in case of index.* rep/index.html to rep/ */
             if( f && f->name[strlen(f->name) - 1] == '/' )
             {
                 char *psz_redir = strdup( f->name );
+                char *p;
                 psz_redir[strlen( psz_redir ) - 1] = '\0';
 
                 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
                 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
                 free( psz_redir );
+
+                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 );
+
+                    free( psz_redir );
+                }
             }
         }
     }
@@ -838,7 +881,6 @@ static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
     char *str = dup;
     mvar_t *s = mvar_New( name, "set" );
 
-    fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
 
     while( str )
     {
@@ -854,7 +896,6 @@ static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
 
         i_step = 0;
         i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
-        fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
 
         if( i_match == 1 )
         {
@@ -883,7 +924,6 @@ static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
                         break;
                     }
 
-                    fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
                     sprintf( value, "%d", i );
 
                     mvar_PushNewVar( s, name, value );
@@ -930,7 +970,6 @@ void PlaylistListNode( playlist_t *p_pl, playlist_item_t *p_node,
             char value[512];
             int i_child;
             mvar_t *itm = mvar_New( name, "set" );
-            mvar_t *itm_end = mvar_New( name, "set" );
 
             mvar_AppendNewVar( itm, "name", p_node->input.psz_name );
             mvar_AppendNewVar( itm, "uri", p_node->input.psz_name );
@@ -961,7 +1000,6 @@ static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
     playlist_view_t *p_view;
     mvar_t *s = mvar_New( name, "set" );
 
-    fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
 
     vlc_mutex_lock( &p_pl->object_lock );
 
@@ -980,7 +1018,6 @@ static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
     mvar_t *s = mvar_New( name, "set" );
     int i, j;
 
-    fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
     if( p_input == NULL )
     {
         return s;
@@ -1020,7 +1057,6 @@ static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
     httpd_info_t info;
     int          i;
 
-    fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
     if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
     {
         for( i= 0; i < info.i_count; )
@@ -1142,7 +1178,6 @@ static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
     }
     *p = '\0';
 
-    fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
 
 #ifdef HAVE_SYS_STAT_H
     if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
@@ -1231,7 +1266,6 @@ static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
     vlm_message_t *msg;
     int    i;
 
-    /* fprintf( stderr," mvar_VlmSetNew: name=`%s'\n", name ); */
     if( vlm == NULL ) return s;
 
     if( vlm_ExecuteCommand( vlm, "show", &msg ) )
@@ -1263,8 +1297,6 @@ static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
             set = mvar_New( name, "set" );
             mvar_AppendNewVar( set, "name", el->psz_name );
 
-            /* fprintf( stderr, "#### name=%s\n", el->psz_name ); */
-
             for( k = 0; k < desc->i_child; k++ )
             {
                 vlm_message_t *ch = desc->child[k];
@@ -1273,19 +1305,22 @@ static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
                     int c;
                     mvar_t *n = mvar_New( ch->psz_name, "set" );
 
-                    /* fprintf( stderr, "        child=%s [%d]\n", ch->psz_name, ch->i_child ); */
                     for( c = 0; c < ch->i_child; c++ )
                     {
-                        mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
-                        mvar_AppendVar( n, in );
-
-                        /* fprintf( stderr, "            sub=%s\n", ch->child[c]->psz_name );*/
+                        if( ch->child[c]->psz_value )
+                        {
+                            mvar_AppendNewVar( n, ch->child[c]->psz_name, ch->child[c]->psz_value );
+                        }
+                        else
+                        {
+                            mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
+                            mvar_AppendVar( n, in );
+                        }
                     }
                     mvar_AppendVar( set, n );
                 }
                 else
                 {
-                    /* fprintf( stderr, "        child=%s->%s\n", ch->psz_name, ch->psz_value ); */
                     mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
                 }
             }
@@ -1662,9 +1697,9 @@ static void MacroDo( httpd_file_sys_t *p_args,
 #define POSITION_ABSOLUTE 12
 #define POSITION_REL_FOR 13
 #define POSITION_REL_BACK 11
-#define TIME_ABSOLUTE 0
-#define TIME_REL_FOR 1
-#define TIME_REL_BACK -1
+#define VL_TIME_ABSOLUTE 0
+#define VL_TIME_REL_FOR 1
+#define VL_TIME_REL_BACK -1
                     if( p_sys->p_input )
                     {
                         uri_extract_value( p_request, "seek_value", value, 20 );
@@ -1679,13 +1714,13 @@ static void MacroDo( httpd_file_sys_t *p_args,
                             {
                                 case '+':
                                 {
-                                    i_relative = TIME_REL_FOR;
+                                    i_relative = VL_TIME_REL_FOR;
                                     p_value++;
                                     break;
                                 }
                                 case '-':
                                 {
-                                    i_relative = TIME_REL_BACK;
+                                    i_relative = VL_TIME_REL_BACK;
                                     p_value++;
                                     break;
                                 }
@@ -1755,7 +1790,7 @@ static void MacroDo( httpd_file_sys_t *p_args,
 
                         switch(i_relative)
                         {
-                            case TIME_ABSOLUTE:
+                            case VL_TIME_ABSOLUTE:
                             {
                                 if( (uint64_t)( i_value ) * 1000000 <= i_length )
                                     val.i_time = (uint64_t)( i_value ) * 1000000;
@@ -1766,7 +1801,7 @@ static void MacroDo( httpd_file_sys_t *p_args,
                                 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
                                 break;
                             }
-                            case TIME_REL_FOR:
+                            case VL_TIME_REL_FOR:
                             {
                                 var_Get( p_sys->p_input, "time", &val );
                                 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
@@ -1780,7 +1815,7 @@ static void MacroDo( httpd_file_sys_t *p_args,
                                 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
                                 break;
                             }
-                            case TIME_REL_BACK:
+                            case VL_TIME_REL_BACK:
                             {
                                 var_Get( p_sys->p_input, "time", &val );
                                 if( (int64_t)( i_value ) * 1000000 > val.i_time )
@@ -1827,9 +1862,9 @@ static void MacroDo( httpd_file_sys_t *p_args,
 #undef POSITION_ABSOLUTE
 #undef POSITION_REL_FOR
 #undef POSITION_REL_BACK
-#undef TIME_ABSOLUTE
-#undef TIME_REL_FOR
-#undef TIME_REL_BACK
+#undef VL_TIME_ABSOLUTE
+#undef VL_TIME_REL_FOR
+#undef VL_TIME_REL_BACK
                     break;
                 }
                 case MVLC_VOLUME:
@@ -2117,7 +2152,6 @@ static void MacroDo( httpd_file_sys_t *p_args,
                             p += sprintf( p, " %s", vlm_properties[i] );
                         }
                     }
-                    fprintf( stderr, "vlm_ExecuteCommand: %s\n", psz );
                     vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
                     if( vlm_answer->psz_value == NULL ) /* there is no error */
                     {
@@ -3243,7 +3277,7 @@ static char *Find_end_MRL( char *psz )
  * create an item with all information in it, and return the item.
  * return NULL if there is an error.
  **********************************************************************/
-playlist_item_t * parse_MRL( intf_thread_t *p_intf, char *psz )
+static playlist_item_t *parse_MRL( intf_thread_t *p_intf, char *psz )
 {
     char **ppsz_options = NULL;
     char *mrl;
@@ -3347,11 +3381,8 @@ playlist_item_t * parse_MRL( intf_thread_t *p_intf, char *psz )
         }
     }
 
-    for( i = 0 ; i < i_options ; i++ )
-    {
-        free( ppsz_options[i] );
-    }
-    free( ppsz_options );
+    for( i = 0; i < i_options; i++ ) free( ppsz_options[i] );
+    if( i_options ) free( ppsz_options );
 
     return p_item;
 }