1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
38 #include <vlc/vout.h> /* for fullscreen */
40 #include "vlc_httpd.h"
44 #ifdef HAVE_SYS_STAT_H
45 # include <sys/stat.h>
56 #elif defined( WIN32 ) && !defined( UNDER_CE )
60 #if (!defined( WIN32 ) || defined(__MINGW32__))
61 /* Mingw has its own version of dirent */
65 /* stat() support for large files on win32 */
66 #if defined( WIN32 ) && !defined( UNDER_CE )
67 # define stat _stati64
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Open ( vlc_object_t * );
74 static void Close( vlc_object_t * );
76 #define HOST_TEXT N_( "Host address" )
77 #define HOST_LONGTEXT N_( \
78 "You can set the address and port the http interface will bind to." )
79 #define SRC_TEXT N_( "Source directory" )
80 #define SRC_LONGTEXT N_( "Source directory" )
81 #define CERT_TEXT N_( "Certificate file" )
82 #define CERT_LONGTEXT N_( "x509 PEM certificates path file" )
83 #define KEY_TEXT N_( "Private key file" )
84 #define KEY_LONGTEXT N_( "x509 PEM private key file" )
85 #define CA_TEXT N_( "Root CA file" )
86 #define CA_LONGTEXT N_( "x509 PEM trusted root CA certificates file" )
87 #define CRL_TEXT N_( "CRL file" )
88 #define CRL_LONGTEXT N_( "Certificates revocation list file" )
91 set_description( _("HTTP remote control interface") );
92 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
93 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
94 add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
95 add_string ( "http-intf-key", NULL, NULL, KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
96 add_string ( "http-intf-ca", NULL, NULL, CA_TEXT, CA_LONGTEXT, VLC_TRUE );
97 add_string ( "http-intf-crl", NULL, NULL, CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
98 set_capability( "interface", 0 );
99 set_callbacks( Open, Close );
103 /*****************************************************************************
105 *****************************************************************************/
106 static void Run ( intf_thread_t *p_intf );
108 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
111 static int DirectoryCheck( char *psz_dir )
115 #ifdef HAVE_SYS_STAT_H
116 struct stat stat_info;
118 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
124 if( ( p_dir = opendir( psz_dir ) ) == NULL )
133 static int HttpCallback( httpd_file_sys_t *p_args,
136 uint8_t **pp_data, int *pi_data );
138 static char *uri_extract_value( char *psz_uri, const char *psz_name,
139 char *psz_value, int i_value_max );
140 static int uri_test_param( char *psz_uri, const char *psz_name );
142 static void uri_decode_url_encoded( char *psz );
144 static char *Find_end_MRL( char *psz );
146 static playlist_item_t * parse_MRL( intf_thread_t * , char *psz );
148 /*****************************************************************************
150 *****************************************************************************/
151 typedef struct mvar_s
157 struct mvar_s **field;
160 #define STACK_MAX 100
163 char *stack[STACK_MAX];
167 struct httpd_file_sys_t
169 intf_thread_t *p_intf;
170 httpd_file_t *p_file;
171 httpd_redirect_t *p_redir;
178 /* inited for each access */
185 httpd_host_t *p_httpd_host;
188 httpd_file_sys_t **pp_files;
190 playlist_t *p_playlist;
191 input_thread_t *p_input;
198 /*****************************************************************************
199 * Activate: initialize and create stuff
200 *****************************************************************************/
201 static int Open( vlc_object_t *p_this )
203 intf_thread_t *p_intf = (intf_thread_t*)p_this;
206 char *psz_address = "";
207 const char *psz_cert;
212 psz_host = config_GetPsz( p_intf, "http-host" );
216 psz_address = psz_host;
218 psz_parser = strchr( psz_host, ':' );
221 *psz_parser++ = '\0';
222 i_port = atoi( psz_parser );
226 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
229 return( VLC_ENOMEM );
231 p_sys->p_playlist = NULL;
232 p_sys->p_input = NULL;
235 /* TODO: avoid possible code duplication in other modules */
236 psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
237 if ( psz_cert != NULL )
241 p_sys->p_tls = vlc_object_create( p_this, VLC_OBJECT_TLS );
242 vlc_object_attach( p_sys->p_tls, p_this );
244 p_sys->p_tls->p_module = module_Need( p_sys->p_tls, "tls", 0, 0 );
245 if( p_sys->p_tls->p_module == NULL )
247 msg_Err( p_this, "cannot find TLS/SSL provider" );
248 vlc_object_detach( p_sys->p_tls );
249 vlc_object_destroy( p_sys->p_tls );
254 msg_Dbg( p_intf, "enablind TLS for HTTP interface (cert file: %s)",
256 psz_pem = config_GetPsz( p_intf, "http-intf-key" );
257 if ( psz_pem == NULL )
260 p_tls = tls_ServerCreate( p_sys->p_tls, psz_cert, psz_pem );
263 msg_Err( p_intf, "TLS initialization error" );
268 psz_pem = config_GetPsz( p_intf, "http-intf-ca" );
269 if ( ( psz_pem != NULL) && tls_ServerAddCA( p_tls, psz_pem ) )
271 msg_Err( p_intf, "TLS CA error" );
272 tls_ServerDelete( p_tls );
277 psz_pem = config_GetPsz( p_intf, "http-intf-crl" );
278 if ( ( psz_pem != NULL) && tls_ServerAddCRL( p_tls, psz_pem ) )
280 msg_Err( p_intf, "TLS CRL error" );
281 tls_ServerDelete( p_tls );
297 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
299 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_intf), psz_address,
301 if( p_sys->p_httpd_host == NULL )
303 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
305 tls_ServerDelete( p_tls );
316 p_sys->pp_files = NULL;
318 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
319 ( defined(WIN32) && !defined(UNDER_CE ) )
320 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
322 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
323 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
329 sprintf( psz_src, "%s/http", psz_vlcpath);
331 sprintf( psz_src, "%s/share/http", psz_vlcpath);
335 psz_src = config_GetPsz( p_intf, "http-src" );
337 if( !psz_src || *psz_src == '\0' )
339 if( !DirectoryCheck( "share/http" ) )
341 psz_src = strdup( "share/http" );
343 else if( !DirectoryCheck( DATA_PATH "/http" ) )
345 psz_src = strdup( DATA_PATH "/http" );
350 if( !psz_src || *psz_src == '\0' )
352 msg_Err( p_intf, "invalid src dir" );
356 /* remove trainling \ or / */
357 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
358 psz_src[strlen( psz_src ) - 1] == '/' )
360 psz_src[strlen( psz_src ) - 1] = '\0';
363 ParseDirectory( p_intf, psz_src, psz_src );
366 if( p_sys->i_files <= 0 )
368 msg_Err( p_intf, "cannot find any files (%s)", psz_src );
371 p_intf->pf_run = Run;
377 if( psz_src ) free( psz_src );
378 if( p_sys->pp_files )
380 free( p_sys->pp_files );
382 httpd_HostDelete( p_sys->p_httpd_host );
387 /*****************************************************************************
388 * CloseIntf: destroy interface
389 *****************************************************************************/
390 void Close ( vlc_object_t *p_this )
392 intf_thread_t *p_intf = (intf_thread_t *)p_this;
393 intf_sys_t *p_sys = p_intf->p_sys;
399 vlm_Delete( p_sys->p_vlm );
401 for( i = 0; i < p_sys->i_files; i++ )
403 httpd_FileDelete( p_sys->pp_files[i]->p_file );
404 if( p_sys->pp_files[i]->p_redir )
406 httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
409 free( p_sys->pp_files[i]->file );
410 free( p_sys->pp_files[i]->name );
411 free( p_sys->pp_files[i] );
413 if( p_sys->pp_files )
415 free( p_sys->pp_files );
417 httpd_HostDelete( p_sys->p_httpd_host );
418 /* TODO: do this in the httpd code to avoid code duplication */
419 if( p_sys->p_tls != NULL )
421 module_Unneed( p_sys->p_tls, p_sys->p_tls->p_module );
422 vlc_object_detach( p_sys->p_tls );
423 vlc_object_destroy( p_sys->p_tls );
429 /*****************************************************************************
430 * Run: http interface thread
431 *****************************************************************************/
432 static void Run( intf_thread_t *p_intf )
434 intf_sys_t *p_sys = p_intf->p_sys;
436 while( !p_intf->b_die )
438 /* get the playlist */
439 if( p_sys->p_playlist == NULL )
441 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
444 /* Manage the input part */
445 if( p_sys->p_input == NULL )
447 if( p_sys->p_playlist )
450 vlc_object_find( p_sys->p_playlist,
455 else if( p_sys->p_input->b_dead )
457 vlc_object_release( p_sys->p_input );
458 p_sys->p_input = NULL;
463 msleep( INTF_IDLE_SLEEP );
468 vlc_object_release( p_sys->p_input );
469 p_sys->p_input = NULL;
472 if( p_sys->p_playlist )
474 vlc_object_release( p_sys->p_playlist );
475 p_sys->p_playlist = NULL;
480 /*****************************************************************************
482 *****************************************************************************/
483 #define MAX_DIR_SIZE 10240
485 /****************************************************************************
486 * FileToUrl: create a good name for an url from filename
487 ****************************************************************************/
488 static char *FileToUrl( char *name )
492 url = p = malloc( strlen( name ) + 1 );
500 while( *name == '\\' || *name == '/' )
502 while( *name == '/' )
512 /* convert '\\' into '/' */
525 if( ( p = strrchr( url, '/' ) ) != NULL )
527 if( !strncmp( p, "/index.", 7 ) )
535 /****************************************************************************
536 * ParseDirectory: parse recursively a directory, adding each file
537 ****************************************************************************/
538 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
541 intf_sys_t *p_sys = p_intf->p_sys;
542 char dir[MAX_DIR_SIZE];
543 #ifdef HAVE_SYS_STAT_H
544 struct stat stat_info;
547 struct dirent *p_dir_content;
551 char *password = NULL;
553 #ifdef HAVE_SYS_STAT_H
554 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
560 if( ( p_dir = opendir( psz_dir ) ) == NULL )
562 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
566 msg_Dbg( p_intf, "dir=%s", psz_dir );
568 sprintf( dir, "%s/.access", psz_dir );
569 if( ( file = fopen( dir, "r" ) ) != NULL )
574 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
576 i_size = fread( line, 1, 1023, file );
580 while( i_size > 0 && ( line[i_size-1] == '\n' ||
581 line[i_size-1] == '\r' ) )
588 p = strchr( line, ':' );
592 user = strdup( line );
593 password = strdup( p );
596 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
597 user, password, i_size );
604 /* parse psz_src dir */
605 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
610 if( p_dir_content->d_name[0] == '.' )
614 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
615 if( ParseDirectory( p_intf, psz_root, dir ) )
617 httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
622 f->file = strdup( dir );
623 f->name = FileToUrl( &dir[strlen( psz_root )] );
624 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
628 msg_Err( p_intf , "unable to parse directory" );
631 return( VLC_ENOMEM );
633 msg_Dbg( p_intf, "file=%s (url=%s)",
636 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
637 f->name, f->b_html ? "text/html" : NULL,
643 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
645 /* For rep/ add a redir from rep to rep/ */
646 if( f && f->name[strlen(f->name) - 1] == '/' )
648 char *psz_redir = strdup( f->name );
649 psz_redir[strlen( psz_redir ) - 1] = '\0';
651 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
652 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
672 /****************************************************************************
673 * var and set handling
674 ****************************************************************************/
676 static mvar_t *mvar_New( char *name, char *value )
678 mvar_t *v = malloc( sizeof( mvar_t ) );
680 if( !v ) return NULL;
681 v->name = strdup( name );
682 v->value = strdup( value ? value : "" );
685 v->field = malloc( sizeof( mvar_t * ) );
691 static void mvar_Delete( mvar_t *v )
698 for( i = 0; i < v->i_field; i++ )
700 mvar_Delete( v->field[i] );
706 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
708 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
709 v->field[v->i_field] = f;
713 static mvar_t *mvar_Duplicate( mvar_t *v )
718 n = mvar_New( v->name, v->value );
719 for( i = 0; i < v->i_field; i++ )
721 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
727 static void mvar_PushVar( mvar_t *v, mvar_t *f )
729 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
732 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
738 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
741 for( i = 0; i < v->i_field; i++ )
743 if( v->field[i] == f )
748 if( i >= v->i_field )
753 if( i + 1 < v->i_field )
755 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
758 /* FIXME should do a realloc */
761 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
764 char base[512], *field, *p;
767 /* format: name[index].field */
769 field = strchr( name, '.' );
772 int i = field - name;
773 strncpy( base, name, i );
779 strcpy( base, name );
782 if( ( p = strchr( base, '[' ) ) )
785 sscanf( p, "%d]", &i_index );
796 for( i = 0; i < s->i_field; i++ )
798 if( !strcmp( s->field[i]->name, base ) )
808 return mvar_GetVar( s->field[i], field );
822 static char *mvar_GetValue( mvar_t *v, char *field )
830 mvar_t *f = mvar_GetVar( v, field );
842 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
844 mvar_t *f = mvar_New( name, value );
845 mvar_PushVar( vars, f );
848 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
850 mvar_t *f = mvar_New( name, value );
851 mvar_AppendVar( vars, f );
855 /* arg= start[:stop[:step]],.. */
856 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
858 char *dup = strdup( arg );
860 mvar_t *s = mvar_New( name, "set" );
862 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
867 int i_start,i_stop,i_step;
870 p = strchr( str, ',' );
877 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
878 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
885 else if( i_match == 2 )
887 i_step = i_start < i_stop ? 1 : -1;
894 if( ( i_start < i_stop && i_step > 0 ) ||
895 ( i_start > i_stop && i_step < 0 ) )
897 for( i = i_start; ; i += i_step )
901 if( ( i_step > 0 && i > i_stop ) ||
902 ( i_step < 0 && i < i_stop ) )
907 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
908 sprintf( value, "%d", i );
910 mvar_PushNewVar( s, name, value );
921 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
923 mvar_t *s = mvar_New( name, "set" );
926 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
928 vlc_mutex_lock( &p_pl->object_lock );
929 for( i = 0; i < p_pl->i_size; i++ )
931 mvar_t *itm = mvar_New( name, "set" );
934 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
935 mvar_AppendNewVar( itm, "current", value );
937 sprintf( value, "%d", i );
938 mvar_AppendNewVar( itm, "index", value );
940 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->input.psz_name );
942 mvar_AppendNewVar( itm, "uri", p_pl->pp_items[i]->input.psz_uri );
944 mvar_AppendVar( s, itm );
946 vlc_mutex_unlock( &p_pl->object_lock );
951 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
953 mvar_t *s = mvar_New( name, "set" );
956 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
957 if( p_input == NULL )
962 vlc_mutex_lock( &p_input->input.p_item->lock );
963 for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
965 info_category_t *p_category = p_input->input.p_item->pp_categories[i];
966 mvar_t *cat = mvar_New( name, "set" );
967 mvar_t *iset = mvar_New( "info", "set" );
969 mvar_AppendNewVar( cat, "name", p_category->psz_name );
970 mvar_AppendVar( cat, iset );
972 for ( j = 0; j < p_category->i_infos; j++ )
974 info_t *p_info = p_category->pp_infos[j];
975 mvar_t *info = mvar_New( "info", "" );
977 msg_Dbg( p_input, "adding info name=%s value=%s",
978 p_info->psz_name, p_info->psz_value );
979 mvar_AppendNewVar( info, "name", p_info->psz_name );
980 mvar_AppendNewVar( info, "value", p_info->psz_value );
981 mvar_AppendVar( iset, info );
983 mvar_AppendVar( s, cat );
985 vlc_mutex_unlock( &p_input->input.p_item->lock );
990 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
992 mvar_t *s = mvar_New( name, "set" );
996 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
997 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
999 for( i= 0; i < info.i_count; )
1003 inf = mvar_New( name, "set" );
1006 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
1007 info.info[i].psz_name, info.info[i].psz_value ); */
1008 mvar_AppendNewVar( inf,
1009 info.info[i].psz_name,
1010 info.info[i].psz_value );
1012 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
1013 mvar_AppendVar( s, inf );
1018 for( i = 0; i < info.i_count; i++ )
1020 free( info.info[i].psz_name );
1021 free( info.info[i].psz_value );
1023 if( info.i_count > 0 )
1032 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1034 mvar_t *s = mvar_New( name, "set" );
1035 char tmp[MAX_DIR_SIZE], *p, *src;
1036 #ifdef HAVE_SYS_STAT_H
1037 struct stat stat_info;
1040 struct dirent *p_dir_content;
1043 /* convert all / to native separator */
1044 #if defined( WIN32 )
1045 while( (p = strchr( psz_dir, '/' )) )
1054 /* remove trailling separator */
1055 while( strlen( psz_dir ) > 1 &&
1056 #if defined( WIN32 )
1057 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1059 psz_dir[strlen( psz_dir ) -1 ] == sep )
1061 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1063 /* remove double separator */
1064 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1066 if( src[0] == sep && src[1] == sep )
1074 if( *psz_dir == '\0' )
1078 /* first fix all .. dir */
1082 if( src[0] == '.' && src[1] == '.' )
1085 if( p <= &psz_dir[1] )
1092 while( p > &psz_dir[1] && *p != sep )
1097 else if( *src == sep )
1099 if( p > psz_dir && p[-1] == sep )
1113 } while( *src && *src != sep );
1118 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1120 #ifdef HAVE_SYS_STAT_H
1121 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1127 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1129 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1133 /* remove traling / or \ */
1134 for( p = &psz_dir[strlen( psz_dir) - 1];
1135 p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1144 /* parse psz_src dir */
1145 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1149 if( !strcmp( p_dir_content->d_name, "." ) )
1154 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1156 #ifdef HAVE_SYS_STAT_H
1157 if( stat( tmp, &stat_info ) == -1 )
1162 f = mvar_New( name, "set" );
1163 mvar_AppendNewVar( f, "name", tmp );
1165 #ifdef HAVE_SYS_STAT_H
1166 if( S_ISDIR( stat_info.st_mode ) )
1168 mvar_AppendNewVar( f, "type", "directory" );
1170 else if( S_ISREG( stat_info.st_mode ) )
1172 mvar_AppendNewVar( f, "type", "file" );
1176 mvar_AppendNewVar( f, "type", "unknown" );
1179 sprintf( tmp, I64Fd, (int64_t)stat_info.st_size );
1180 mvar_AppendNewVar( f, "size", tmp );
1182 /* FIXME memory leak FIXME */
1184 ctime_r( &stat_info.st_mtime, tmp );
1185 mvar_AppendNewVar( f, "date", tmp );
1187 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1191 mvar_AppendNewVar( f, "type", "unknown" );
1192 mvar_AppendNewVar( f, "size", "unknown" );
1193 mvar_AppendNewVar( f, "date", "unknown" );
1195 mvar_AppendVar( s, f );
1201 static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
1203 mvar_t *s = mvar_New( name, "set" );
1207 /* fprintf( stderr," mvar_VlmSetNew: name=`%s'\n", name ); */
1208 if( vlm == NULL ) return s;
1210 if( vlm_ExecuteCommand( vlm, "show", &msg ) )
1215 for( i = 0; i < msg->i_child; i++ )
1217 /* Over media, schedule */
1218 vlm_message_t *ch = msg->child[i];
1221 for( j = 0; j < ch->i_child; j++ )
1224 vlm_message_t *el = ch->child[j];
1225 vlm_message_t *inf, *desc;
1230 sprintf( psz, "show %s", el->psz_name );
1231 if( vlm_ExecuteCommand( vlm, psz, &inf ) )
1233 desc = inf->child[0];
1235 /* Add a node with name and info */
1236 set = mvar_New( name, "set" );
1237 mvar_AppendNewVar( set, "name", el->psz_name );
1239 /* fprintf( stderr, "#### name=%s\n", el->psz_name ); */
1241 for( k = 0; k < desc->i_child; k++ )
1243 vlm_message_t *ch = desc->child[k];
1244 if( ch->i_child > 0 )
1247 mvar_t *n = mvar_New( ch->psz_name, "set" );
1249 /* fprintf( stderr, " child=%s [%d]\n", ch->psz_name, ch->i_child ); */
1250 for( c = 0; c < ch->i_child; c++ )
1252 mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
1253 mvar_AppendVar( n, in );
1255 /* fprintf( stderr, " sub=%s\n", ch->child[c]->psz_name );*/
1257 mvar_AppendVar( set, n );
1261 /* fprintf( stderr, " child=%s->%s\n", ch->psz_name, ch->psz_value ); */
1262 mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
1265 vlm_MessageDelete( inf );
1267 mvar_AppendVar( s, set );
1270 vlm_MessageDelete( msg );
1276 static void SSInit( rpn_stack_t * );
1277 static void SSClean( rpn_stack_t * );
1278 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1280 static void SSPush ( rpn_stack_t *, char * );
1281 static char *SSPop ( rpn_stack_t * );
1283 static void SSPushN ( rpn_stack_t *, int );
1284 static int SSPopN ( rpn_stack_t *, mvar_t * );
1287 /****************************************************************************
1289 ****************************************************************************/
1297 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1301 /* just load the file */
1303 *pp_data = malloc( 1025 ); /* +1 for \0 */
1304 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1307 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1313 (*pp_data)[*pi_data] = '\0';
1318 static int MacroParse( macro_t *m, uint8_t *psz_src )
1320 uint8_t *dup = strdup( psz_src );
1325 #define EXTRACT( name, l ) \
1327 p = strchr( src, '"' ); \
1332 m->name = strdup( src ); \
1353 if( !strncmp( src, "id=\"", 4 ) )
1357 else if( !strncmp( src, "param1=\"", 8 ) )
1359 EXTRACT( param1, 8 );
1361 else if( !strncmp( src, "param2=\"", 8 ) )
1363 EXTRACT( param2, 8 );
1370 if( strstr( src, "/>" ) )
1372 src = strstr( src, "/>" ) + 2;
1376 src += strlen( src );
1381 m->id = strdup( "" );
1383 if( m->param1 == NULL )
1385 m->param1 = strdup( "" );
1387 if( m->param2 == NULL )
1389 m->param2 = strdup( "" );
1398 static void MacroClean( macro_t *m )
1456 StrToMacroTypeTab [] =
1458 { "control", MVLC_CONTROL },
1459 /* player control */
1460 { "play", MVLC_PLAY },
1461 { "stop", MVLC_STOP },
1462 { "pause", MVLC_PAUSE },
1463 { "next", MVLC_NEXT },
1464 { "previous", MVLC_PREVIOUS },
1465 { "seek", MVLC_SEEK },
1466 { "keep", MVLC_KEEP },
1467 { "fullscreen", MVLC_FULLSCREEN },
1468 { "volume", MVLC_VOLUME },
1470 /* playlist management */
1471 { "add", MVLC_ADD },
1472 { "delete", MVLC_DEL },
1473 { "empty", MVLC_EMPTY },
1474 { "sort", MVLC_SORT },
1475 { "move", MVLC_MOVE },
1478 { "close", MVLC_CLOSE },
1479 { "shutdown", MVLC_SHUTDOWN },
1482 { "vlm_new", MVLC_VLM_NEW },
1483 { "vlm_setup", MVLC_VLM_SETUP },
1484 { "vlm_del", MVLC_VLM_DEL },
1485 { "vlm_play", MVLC_VLM_PLAY },
1486 { "vlm_pause", MVLC_VLM_PAUSE },
1487 { "vlm_stop", MVLC_VLM_STOP },
1488 { "vlm_seek", MVLC_VLM_SEEK },
1489 { "vlm_load", MVLC_VLM_LOAD },
1490 { "vlm_save", MVLC_VLM_SAVE },
1492 { "rpn", MVLC_RPN },
1494 { "foreach", MVLC_FOREACH },
1495 { "value", MVLC_VALUE },
1498 { "else", MVLC_ELSE },
1499 { "end", MVLC_END },
1500 { "get", MVLC_GET },
1501 { "set", MVLC_SET },
1502 { "int", MVLC_INT },
1503 { "float", MVLC_FLOAT },
1504 { "string", MVLC_STRING },
1507 { NULL, MVLC_UNKNOWN }
1510 static int StrToMacroType( char *name )
1514 if( !name || *name == '\0')
1516 return MVLC_UNKNOWN;
1518 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1520 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1522 return StrToMacroTypeTab[i].i_type;
1525 return MVLC_UNKNOWN;
1528 static void MacroDo( httpd_file_sys_t *p_args,
1530 uint8_t *p_request, int i_request,
1531 uint8_t **pp_data, int *pi_data,
1534 intf_thread_t *p_intf = p_args->p_intf;
1535 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1538 #define ALLOC( l ) \
1540 int __i__ = *pp_dst - *pp_data; \
1542 *pp_data = realloc( *pp_data, *pi_data ); \
1543 *pp_dst = (*pp_data) + __i__; \
1545 #define PRINT( str ) \
1546 ALLOC( strlen( str ) + 1 ); \
1547 *pp_dst += sprintf( *pp_dst, str );
1549 #define PRINTS( str, s ) \
1550 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1552 char * psz_cur = *pp_dst; \
1553 *pp_dst += sprintf( *pp_dst, str, s ); \
1554 while( psz_cur && *psz_cur ) \
1556 /* Prevent script injection */ \
1557 if( *psz_cur == '<' ) *psz_cur = '*'; \
1558 if( *psz_cur == '>' ) *psz_cur = '*'; \
1563 switch( StrToMacroType( m->id ) )
1566 if( i_request <= 0 )
1570 uri_extract_value( p_request, "control", control, 512 );
1571 if( *m->param1 && !strstr( m->param1, control ) )
1573 msg_Warn( p_intf, "unauthorized control=%s", control );
1576 switch( StrToMacroType( control ) )
1583 uri_extract_value( p_request, "item", item, 512 );
1584 i_item = atoi( item );
1585 playlist_Control( p_sys->p_playlist, PLAYLIST_GOTO,
1587 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1591 playlist_Control( p_sys->p_playlist, PLAYLIST_STOP);
1592 msg_Dbg( p_intf, "requested playlist stop" );
1595 playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE );
1596 msg_Dbg( p_intf, "requested playlist pause" );
1599 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, 1 );
1600 msg_Dbg( p_intf, "requested playlist next" );
1603 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, -1);
1604 msg_Dbg( p_intf, "requested playlist next" );
1606 case MVLC_FULLSCREEN:
1608 if( p_sys->p_input )
1610 vout_thread_t *p_vout;
1611 p_vout = vlc_object_find( p_sys->p_input,
1612 VLC_OBJECT_VOUT, FIND_CHILD );
1616 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1617 vlc_object_release( p_vout );
1618 msg_Dbg( p_intf, "requested fullscreen toggle" );
1632 #define POSITION_ABSOLUTE 12
1633 #define POSITION_REL_FOR 13
1634 #define POSITION_REL_BACK 11
1635 #define TIME_ABSOLUTE 0
1636 #define TIME_REL_FOR 1
1637 #define TIME_REL_BACK -1
1638 if( p_sys->p_input )
1640 uri_extract_value( p_request, "seek_value", value, 20 );
1641 uri_decode_url_encoded( value );
1643 var_Get( p_sys->p_input, "length", &val);
1644 i_length = val.i_time;
1646 while( p_value[0] != '\0' )
1652 i_relative = TIME_REL_FOR;
1658 i_relative = TIME_REL_BACK;
1662 case '0': case '1': case '2': case '3': case '4':
1663 case '5': case '6': case '7': case '8': case '9':
1665 i_stock = strtol( p_value , &p_value , 10 );
1668 case '%': /* for percentage ie position */
1670 i_relative += POSITION_ABSOLUTE;
1678 i_value = 60 * (i_value + i_stock) ;
1683 case 'h': case 'H': /* hours */
1685 i_value += 3600 * i_stock;
1687 /* other characters which are not numbers are not important */
1688 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1694 case 'm': case 'M': case '\'': /* minutes */
1696 i_value += 60 * i_stock;
1699 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1705 case 's': case 'S': case '"': /* seconds */
1709 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1723 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1730 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1731 val.i_time = (uint64_t)( i_value ) * 1000000;
1733 val.i_time = i_length;
1735 var_Set( p_sys->p_input, "time", val );
1736 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1741 var_Get( p_sys->p_input, "time", &val );
1742 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1744 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1747 val.i_time = i_length;
1749 var_Set( p_sys->p_input, "time", val );
1750 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1755 var_Get( p_sys->p_input, "time", &val );
1756 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1761 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1763 var_Set( p_sys->p_input, "time", val );
1764 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1767 case POSITION_ABSOLUTE:
1769 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1770 var_Set( p_sys->p_input, "position", val );
1771 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1774 case POSITION_REL_FOR:
1776 var_Get( p_sys->p_input, "position", &val );
1777 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1778 var_Set( p_sys->p_input, "position", val );
1779 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1782 case POSITION_REL_BACK:
1784 var_Get( p_sys->p_input, "position", &val );
1785 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1786 var_Set( p_sys->p_input, "position", val );
1787 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
1792 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1797 #undef POSITION_ABSOLUTE
1798 #undef POSITION_REL_FOR
1799 #undef POSITION_REL_BACK
1800 #undef TIME_ABSOLUTE
1802 #undef TIME_REL_BACK
1808 audio_volume_t i_volume;
1811 uri_extract_value( p_request, "value", vol, 8 );
1812 aout_VolumeGet( p_intf, &i_volume );
1813 uri_decode_url_encoded( vol );
1817 i_value = atoi( vol + 1 );
1818 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1820 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1821 msg_Dbg( p_intf, "requested volume set: max" );
1824 aout_VolumeSet( p_intf , (i_volume + i_value) );
1825 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1830 i_value = atoi( vol + 1 );
1831 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1833 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1834 msg_Dbg( p_intf, "requested volume set: min" );
1837 aout_VolumeSet( p_intf , (i_volume - i_value) );
1838 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1841 if( strstr(vol, "%") != NULL )
1843 i_value = atoi( vol );
1844 if( (i_value <= 100) && (i_value>=0) ){
1845 aout_VolumeSet( p_intf, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/100+AOUT_VOLUME_MIN);
1846 msg_Dbg( p_intf, "requested volume set: %i%%", atoi( vol ));
1850 i_value = atoi( vol );
1851 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1853 aout_VolumeSet( p_intf , atoi( vol ) );
1854 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1860 /* playlist management */
1864 playlist_item_t * p_item;
1866 uri_extract_value( p_request, "mrl", mrl, 512 );
1867 uri_decode_url_encoded( mrl );
1868 p_item = parse_MRL( p_intf, mrl );
1870 if( !p_item || !p_item->input.psz_uri ||
1871 !*p_item->input.psz_uri )
1873 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1876 playlist_AddItem( p_sys->p_playlist , p_item ,
1877 PLAYLIST_APPEND, PLAYLIST_END );
1878 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1885 int i_item, *p_items = NULL, i_nb_items = 0;
1886 char item[512], *p_parser = p_request;
1888 /* Get the list of items to delete */
1890 uri_extract_value( p_parser, "item", item, 512 )) )
1892 if( !*item ) continue;
1894 i_item = atoi( item );
1895 p_items = realloc( p_items, (i_nb_items + 1) *
1897 p_items[i_nb_items] = i_item;
1901 /* The items need to be deleted from in reversed order */
1905 for( i = 0; i < i_nb_items; i++ )
1908 for( j = 0; j < i_nb_items; j++ )
1910 if( p_items[j] > p_items[i_index] )
1914 playlist_Delete( p_sys->p_playlist,
1916 msg_Dbg( p_intf, "requested playlist delete: %d",
1918 p_items[i_index] = -1;
1922 if( p_items ) free( p_items );
1927 int i_item, *p_items = NULL, i_nb_items = 0;
1928 char item[512], *p_parser = p_request;
1931 /* Get the list of items to keep */
1933 uri_extract_value( p_parser, "item", item, 512 )) )
1935 if( !*item ) continue;
1937 i_item = atoi( item );
1938 p_items = realloc( p_items, (i_nb_items + 1) *
1940 p_items[i_nb_items] = i_item;
1944 /* The items need to be deleted from in reversed order */
1945 for( i = p_sys->p_playlist->i_size - 1; i >= 0 ; i-- )
1947 /* Check if the item is in the keep list */
1948 for( j = 0 ; j < i_nb_items ; j++ )
1950 if( p_items[j] == i ) break;
1952 if( j == i_nb_items )
1954 playlist_Delete( p_sys->p_playlist, i );
1955 msg_Dbg( p_intf, "requested playlist delete: %d",
1960 if( p_items ) free( p_items );
1965 while( p_sys->p_playlist->i_size > 0 )
1967 playlist_Delete( p_sys->p_playlist, 0 );
1969 msg_Dbg( p_intf, "requested playlist empty" );
1978 uri_extract_value( p_request, "type", type, 12 );
1979 uri_extract_value( p_request, "order", order, 2 );
1981 if( order[0] == '0' ) i_order = ORDER_NORMAL;
1982 else i_order = ORDER_REVERSE;
1984 if( !strcmp( type , "title" ) )
1986 playlist_SortTitle( p_sys->p_playlist , i_order );
1987 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
1988 } else if( !strcmp( type , "author" ) )
1990 playlist_SortAuthor( p_sys->p_playlist , i_order );
1991 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
1992 } else if( !strcmp( type , "shuffle" ) )
1994 playlist_Sort( p_sys->p_playlist , SORT_RANDOM, ORDER_NORMAL );
1995 msg_Dbg( p_intf, "requested playlist shuffle");
2006 uri_extract_value( p_request, "psz_pos", psz_pos, 6 );
2007 uri_extract_value( p_request, "psz_newpos", psz_newpos, 6 );
2008 i_pos = atoi( psz_pos );
2009 i_newpos = atoi( psz_newpos );
2010 if ( i_pos < i_newpos )
2012 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
2014 playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
2016 msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
2020 /* admin function */
2024 uri_extract_value( p_request, "id", id, 512 );
2025 msg_Dbg( p_intf, "requested close id=%s", id );
2027 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
2029 msg_Warn( p_intf, "close failed for id=%s", id );
2036 msg_Dbg( p_intf, "requested shutdown" );
2037 p_intf->p_vlc->b_die = VLC_TRUE;
2042 case MVLC_VLM_SETUP:
2044 static const char *vlm_properties[11] =
2047 "enabled", "disabled", "loop", "unloop",
2049 "input", "output", "option", "date", "period", "repeat", "append",
2051 vlm_message_t *vlm_answer;
2053 char *psz = malloc( strlen( p_request ) + 1000 );
2058 if( p_intf->p_sys->p_vlm == NULL )
2059 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2061 if( p_intf->p_sys->p_vlm == NULL ) break;
2063 uri_extract_value( p_request, "name", name, 512 );
2064 if( StrToMacroType( control ) == MVLC_VLM_NEW )
2067 uri_extract_value( p_request, "type", type, 20 );
2068 p += sprintf( psz, "new %s %s", name, type );
2072 p += sprintf( psz, "setup %s", name );
2074 /* Parse the request */
2075 for( i = 0; i < 11; i++ )
2078 uri_extract_value( p_request, vlm_properties[i], val, 512 );
2079 uri_decode_url_encoded( val );
2080 if( strlen( val ) > 0 && i >= 4 )
2082 p += sprintf( p, " %s %s", vlm_properties[i], val );
2084 else if( uri_test_param( p_request, vlm_properties[i] ) && i < 4 )
2086 p += sprintf( p, " %s", vlm_properties[i] );
2089 fprintf( stderr, "vlm_ExecuteCommand: %s\n", psz );
2090 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2091 if( vlm_answer->psz_value == NULL ) /* there is no error */
2093 vlm_error = strdup( "" );
2097 vlm_error = malloc( strlen(vlm_answer->psz_name) +
2098 strlen(vlm_answer->psz_value) +
2099 strlen( " : ") + 1 );
2100 sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
2101 vlm_answer->psz_value );
2104 mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
2106 vlm_MessageDelete( vlm_answer );
2114 vlm_message_t *vlm_answer;
2117 if( p_intf->p_sys->p_vlm == NULL )
2118 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2120 if( p_intf->p_sys->p_vlm == NULL ) break;
2122 uri_extract_value( p_request, "name", name, 512 );
2123 sprintf( psz, "del %s", name );
2125 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2126 /* FIXME do a vlm_answer -> var stack conversion */
2127 vlm_MessageDelete( vlm_answer );
2132 case MVLC_VLM_PAUSE:
2136 vlm_message_t *vlm_answer;
2139 if( p_intf->p_sys->p_vlm == NULL )
2140 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2142 if( p_intf->p_sys->p_vlm == NULL ) break;
2144 uri_extract_value( p_request, "name", name, 512 );
2145 if( StrToMacroType( control ) == MVLC_VLM_PLAY )
2146 sprintf( psz, "control %s play", name );
2147 else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
2148 sprintf( psz, "control %s pause", name );
2149 else if( StrToMacroType( control ) == MVLC_VLM_STOP )
2150 sprintf( psz, "control %s stop", name );
2151 else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
2154 uri_extract_value( p_request, "percent", percent, 512 );
2155 sprintf( psz, "control %s seek %s", name, percent );
2158 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2159 /* FIXME do a vlm_answer -> var stack conversion */
2160 vlm_MessageDelete( vlm_answer );
2166 vlm_message_t *vlm_answer;
2170 if( p_intf->p_sys->p_vlm == NULL )
2171 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2173 if( p_intf->p_sys->p_vlm == NULL ) break;
2175 uri_extract_value( p_request, "file", file, 512 );
2176 uri_decode_url_encoded( file );
2178 if( StrToMacroType( control ) == MVLC_VLM_LOAD )
2179 sprintf( psz, "load %s", file );
2181 sprintf( psz, "save %s", file );
2183 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2184 /* FIXME do a vlm_answer -> var stack conversion */
2185 vlm_MessageDelete( vlm_answer );
2192 PRINTS( "<!-- control param(%s) unsuported -->", control );
2204 if( i_request <= 0 ||
2205 *m->param1 == '\0' ||
2206 strstr( p_request, m->param1 ) == NULL )
2210 uri_extract_value( p_request, m->param1, value, 512 );
2211 uri_decode_url_encoded( value );
2213 switch( StrToMacroType( m->param2 ) )
2217 config_PutInt( p_intf, m->param1, i );
2221 config_PutFloat( p_intf, m->param1, f );
2224 config_PutPsz( p_intf, m->param1, value );
2227 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
2238 if( *m->param1 == '\0' )
2243 switch( StrToMacroType( m->param2 ) )
2246 i = config_GetInt( p_intf, m->param1 );
2247 sprintf( value, "%i", i );
2250 f = config_GetFloat( p_intf, m->param1 );
2251 sprintf( value, "%f", f );
2254 psz = config_GetPsz( p_intf, m->param1 );
2255 sprintf( value, "%s", psz ? psz : "" );
2256 if( psz ) free( psz );
2259 sprintf( value, "invalid type(%s) in set", m->param2 );
2262 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
2263 PRINTS( "%s", value );
2272 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2273 s = SSPop( &p_args->stack );
2274 v = mvar_GetValue( p_args->vars, s );
2278 v = s = SSPop( &p_args->stack );
2286 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2291 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2292 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2300 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
2307 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
2312 i_skip = MacroParse( &m, src );
2314 i_id = StrToMacroType( m.id );
2331 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2332 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2334 return src + ( b_after ? i_skip : 0 );
2336 else if( i_level < 0 )
2352 static void Execute( httpd_file_sys_t *p_args,
2353 uint8_t *p_request, int i_request,
2354 uint8_t **pp_data, int *pi_data,
2356 uint8_t *_src, uint8_t *_end )
2358 intf_thread_t *p_intf = p_args->p_intf;
2360 uint8_t *src, *dup, *end;
2361 uint8_t *dst = *pp_dst;
2363 src = dup = malloc( _end - _src + 1 );
2364 end = src +( _end - _src );
2366 memcpy( src, _src, _end - _src );
2369 /* we parse searching <vlc */
2375 p = strstr( src, "<vlc" );
2376 if( p < end && p == src )
2380 src += MacroParse( &m, src );
2382 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2384 switch( StrToMacroType( m.id ) )
2391 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2392 if( SSPopN( &p_args->stack, p_args->vars ) )
2400 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2404 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2408 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2411 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2415 else if( i_test == 1 )
2418 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2420 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2424 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2433 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2434 uint8_t *start = src;
2435 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2442 if( !strcmp( m.param2, "integer" ) )
2444 char *arg = SSPop( &p_args->stack );
2445 index = mvar_IntegerSetNew( m.param1, arg );
2448 else if( !strcmp( m.param2, "directory" ) )
2450 char *arg = SSPop( &p_args->stack );
2451 index = mvar_FileSetNew( m.param1, arg );
2454 else if( !strcmp( m.param2, "playlist" ) )
2456 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2458 else if( !strcmp( m.param2, "information" ) )
2460 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2462 else if( !strcmp( m.param2, "vlm" ) )
2464 if( p_intf->p_sys->p_vlm == NULL )
2465 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2466 index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
2469 else if( !strcmp( m.param2, "hosts" ) )
2471 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2473 else if( !strcmp( m.param2, "urls" ) )
2475 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2477 else if( !strcmp( m.param2, "connections" ) )
2479 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2482 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2484 index = mvar_Duplicate( v );
2488 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2493 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2495 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2497 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2500 f->name = strdup( m.param1 );
2503 mvar_PushVar( p_args->vars, f );
2504 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2505 mvar_RemoveVar( p_args->vars, f );
2509 mvar_Delete( index );
2516 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2524 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2527 int i_index = dst - *pp_data;
2530 *pp_data = realloc( *pp_data, *pi_data );
2531 dst = (*pp_data) + i_index;
2533 memcpy( dst, src, i_copy );
2543 /****************************************************************************
2545 ****************************************************************************
2546 * a file with b_html is parsed and all "macro" replaced
2547 * <vlc id="macro name" [param1="" [param2=""]] />
2550 ****************************************************************************/
2551 static int HttpCallback( httpd_file_sys_t *p_args,
2552 httpd_file_t *p_file,
2554 uint8_t **pp_data, int *pi_data )
2556 int i_request = p_request ? strlen( p_request ) : 0;
2560 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2562 p = *pp_data = malloc( 10240 );
2565 return VLC_EGENERIC;
2567 p += sprintf( p, "<html>\n" );
2568 p += sprintf( p, "<head>\n" );
2569 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2570 p += sprintf( p, "</head>\n" );
2571 p += sprintf( p, "<body>\n" );
2572 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2573 p += sprintf( p, "<hr />\n" );
2574 p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
2575 p += sprintf( p, "</body>\n" );
2576 p += sprintf( p, "</html>\n" );
2578 *pi_data = strlen( *pp_data );
2583 if( !p_args->b_html )
2585 FileLoad( f, pp_data, pi_data );
2593 char position[4]; /* percentage */
2594 char time[12]; /* in seconds */
2595 char length[12]; /* in seconds */
2596 audio_volume_t i_volume;
2600 #define p_sys p_args->p_intf->p_sys
2601 if( p_sys->p_input )
2603 var_Get( p_sys->p_input, "position", &val);
2604 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2605 var_Get( p_sys->p_input, "time", &val);
2606 sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2607 var_Get( p_sys->p_input, "length", &val);
2608 sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2610 var_Get( p_sys->p_input, "state", &val );
2611 if( val.i_int == PLAYING_S )
2613 sprintf( state, "playing" );
2614 } else if( val.i_int == PAUSE_S )
2616 sprintf( state, "paused" );
2619 sprintf( state, "stop" );
2623 sprintf( position, "%d", 0 );
2624 sprintf( time, "%d", 0 );
2625 sprintf( length, "%d", 0 );
2626 sprintf( state, "stop" );
2630 aout_VolumeGet( p_args->p_intf , &i_volume );
2631 sprintf( volume , "%d" , (int)i_volume );
2633 p_args->vars = mvar_New( "variables", "" );
2634 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2635 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2636 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
2637 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2638 mvar_AppendNewVar( p_args->vars, "stream_position", position );
2639 mvar_AppendNewVar( p_args->vars, "stream_time", time );
2640 mvar_AppendNewVar( p_args->vars, "stream_length", length );
2641 mvar_AppendNewVar( p_args->vars, "volume", volume );
2642 mvar_AppendNewVar( p_args->vars, "stream_state", state );
2644 SSInit( &p_args->stack );
2646 /* first we load in a temporary buffer */
2647 FileLoad( f, &p_buffer, &i_buffer );
2649 /* allocate output */
2650 *pi_data = i_buffer + 1000;
2651 dst = *pp_data = malloc( *pi_data );
2653 /* we parse executing all <vlc /> macros */
2654 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2657 *pi_data = dst - *pp_data;
2659 SSClean( &p_args->stack );
2660 mvar_Delete( p_args->vars );
2669 /****************************************************************************
2671 ****************************************************************************/
2672 static int uri_test_param( char *psz_uri, const char *psz_name )
2676 while( (p = strstr( p, psz_name )) )
2678 /* Verify that we are dealing with a post/get argument */
2679 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2688 static char *uri_extract_value( char *psz_uri, const char *psz_name,
2689 char *psz_value, int i_value_max )
2693 while( (p = strstr( p, psz_name )) )
2695 /* Verify that we are dealing with a post/get argument */
2696 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2705 p += strlen( psz_name );
2706 if( *p == '=' ) p++;
2708 if( strchr( p, '&' ) )
2710 i_len = strchr( p, '&' ) - p;
2714 /* for POST method */
2715 if( strchr( p, '\n' ) )
2717 i_len = strchr( p, '\n' ) - p;
2718 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2722 i_len = strlen( p );
2725 i_len = __MIN( i_value_max - 1, i_len );
2728 strncpy( psz_value, p, i_len );
2729 psz_value[i_len] = '\0';
2733 strncpy( psz_value, "", i_value_max );
2739 strncpy( psz_value, "", i_value_max );
2745 static void uri_decode_url_encoded( char *psz )
2747 char *dup = strdup( psz );
2765 *psz++ = strtol( val, NULL, 16 );
2767 else if( *p == '+' )
2781 /****************************************************************************
2782 * Light RPN evaluator
2783 ****************************************************************************/
2784 static void SSInit( rpn_stack_t *st )
2789 static void SSClean( rpn_stack_t *st )
2791 while( st->i_stack > 0 )
2793 free( st->stack[--st->i_stack] );
2797 static void SSPush( rpn_stack_t *st, char *s )
2799 if( st->i_stack < STACK_MAX )
2801 st->stack[st->i_stack++] = strdup( s );
2805 static char * SSPop( rpn_stack_t *st )
2807 if( st->i_stack <= 0 )
2809 return strdup( "" );
2813 return st->stack[--st->i_stack];
2817 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2826 i = strtol( name, &end, 0 );
2829 value = mvar_GetValue( vars, name );
2837 static void SSPushN( rpn_stack_t *st, int i )
2841 sprintf( v, "%d", i );
2845 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2852 while( *exp == ' ' )
2859 /* extract string */
2862 while( *exp && *exp != '\'' )
2873 p = strchr( exp, ' ' );
2878 exp += strlen( exp );
2883 strncpy( s, exp, i );
2894 /* 1. Integer function */
2895 if( !strcmp( s, "!" ) )
2897 SSPushN( st, ~SSPopN( st, vars ) );
2899 else if( !strcmp( s, "^" ) )
2901 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2903 else if( !strcmp( s, "&" ) )
2905 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2907 else if( !strcmp( s, "|" ) )
2909 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2911 else if( !strcmp( s, "+" ) )
2913 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2915 else if( !strcmp( s, "-" ) )
2917 int j = SSPopN( st, vars );
2918 int i = SSPopN( st, vars );
2919 SSPushN( st, i - j );
2921 else if( !strcmp( s, "*" ) )
2923 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2925 else if( !strcmp( s, "/" ) )
2929 j = SSPopN( st, vars );
2930 i = SSPopN( st, vars );
2932 SSPushN( st, j != 0 ? i / j : 0 );
2934 else if( !strcmp( s, "%" ) )
2938 j = SSPopN( st, vars );
2939 i = SSPopN( st, vars );
2941 SSPushN( st, j != 0 ? i % j : 0 );
2943 /* 2. integer tests */
2944 else if( !strcmp( s, "=" ) )
2946 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2948 else if( !strcmp( s, "<" ) )
2950 int j = SSPopN( st, vars );
2951 int i = SSPopN( st, vars );
2953 SSPushN( st, i < j ? -1 : 0 );
2955 else if( !strcmp( s, ">" ) )
2957 int j = SSPopN( st, vars );
2958 int i = SSPopN( st, vars );
2960 SSPushN( st, i > j ? -1 : 0 );
2962 else if( !strcmp( s, "<=" ) )
2964 int j = SSPopN( st, vars );
2965 int i = SSPopN( st, vars );
2967 SSPushN( st, i <= j ? -1 : 0 );
2969 else if( !strcmp( s, ">=" ) )
2971 int j = SSPopN( st, vars );
2972 int i = SSPopN( st, vars );
2974 SSPushN( st, i >= j ? -1 : 0 );
2976 /* 3. string functions */
2977 else if( !strcmp( s, "strcat" ) )
2979 char *s2 = SSPop( st );
2980 char *s1 = SSPop( st );
2981 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2991 else if( !strcmp( s, "strcmp" ) )
2993 char *s2 = SSPop( st );
2994 char *s1 = SSPop( st );
2996 SSPushN( st, strcmp( s1, s2 ) );
3000 else if( !strcmp( s, "strncmp" ) )
3002 int n = SSPopN( st, vars );
3003 char *s2 = SSPop( st );
3004 char *s1 = SSPop( st );
3006 SSPushN( st, strncmp( s1, s2 , n ) );
3010 else if( !strcmp( s, "strsub" ) )
3012 int n = SSPopN( st, vars );
3013 int m = SSPopN( st, vars );
3015 char *s = SSPop( st );
3027 str = malloc( i_len + 1 );
3029 memcpy( str, s + m - 1, i_len );
3030 str[ i_len ] = '\0';
3036 else if( !strcmp( s, "strlen" ) )
3038 char *str = SSPop( st );
3040 SSPushN( st, strlen( str ) );
3043 /* 4. stack functions */
3044 else if( !strcmp( s, "dup" ) )
3046 char *str = SSPop( st );
3051 else if( !strcmp( s, "drop" ) )
3053 char *str = SSPop( st );
3056 else if( !strcmp( s, "swap" ) )
3058 char *s1 = SSPop( st );
3059 char *s2 = SSPop( st );
3066 else if( !strcmp( s, "flush" ) )
3071 else if( !strcmp( s, "store" ) )
3073 char *value = SSPop( st );
3074 char *name = SSPop( st );
3076 mvar_PushNewVar( vars, name, value );
3080 else if( !strcmp( s, "value" ) )
3082 char *name = SSPop( st );
3083 char *value = mvar_GetValue( vars, name );
3085 SSPush( st, value );
3089 else if( !strcmp( s, "url_extract" ) )
3091 char *url = mvar_GetValue( vars, "url_value" );
3092 char *name = SSPop( st );
3095 uri_extract_value( url, name, value, 512 );
3096 uri_decode_url_encoded( value );
3097 SSPush( st, value );
3106 /**********************************************************************
3107 * Find_end_MRL: Find the end of the sentence :
3108 * this function parses the string psz and find the end of the item
3109 * and/or option with detecting the " and ' problems.
3110 * returns NULL if an error is detected, otherwise, returns a pointer
3111 * of the end of the sentence (after the last character)
3112 **********************************************************************/
3113 static char *Find_end_MRL( char *psz )
3123 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
3125 if( *s_sent == '\'' )
3127 s_sent = Find_end_MRL( s_sent );
3129 if( s_sent == NULL )
3139 if( *s_sent == '\"' )
3143 } else /* *s_sent == '\0' , which means the number of " is incorrect */
3153 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
3155 if( *s_sent == '\"' )
3157 s_sent = Find_end_MRL( s_sent );
3159 if( s_sent == NULL )
3169 if( *s_sent == '\'' )
3173 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
3179 default: /* now we can look for spaces */
3181 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
3183 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
3185 s_sent = Find_end_MRL( s_sent );
3196 /**********************************************************************
3197 * parse_MRL: parse the MRL, find the mrl string and the options,
3198 * create an item with all information in it, and return the item.
3199 * return NULL if there is an error.
3200 **********************************************************************/
3201 playlist_item_t * parse_MRL( intf_thread_t *p_intf, char *psz )
3203 char **ppsz_options = NULL;
3210 playlist_item_t * p_item = NULL;
3212 /* In case there is spaces before the mrl */
3213 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
3218 /* extract the mrl */
3219 s_temp = strstr( s_mrl , " :" );
3220 if( s_temp == NULL )
3222 s_temp = s_mrl + strlen( s_mrl );
3225 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
3232 /* if the mrl is between " or ', we must remove them */
3233 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
3235 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
3236 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
3237 mrl[ s_temp - s_mrl - 2 ] = '\0';
3240 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
3241 strncpy( mrl , s_mrl , s_temp - s_mrl );
3242 mrl[ s_temp - s_mrl ] = '\0';
3247 /* now we can take care of the options */
3248 while( (*s_mrl != '\0') && (i_error == 0) )
3257 case ':': /* an option */
3259 s_temp = Find_end_MRL( s_mrl );
3261 if( s_temp == NULL )
3268 ppsz_options = realloc( ppsz_options , i_options *
3270 ppsz_options[ i_options - 1 ] =
3271 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
3273 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
3276 /* don't forget to finish the string with a '\0' */
3277 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
3297 /* now create an item */
3298 p_item = playlist_ItemNew( p_intf, mrl, mrl);
3299 for( i = 0 ; i< i_options ; i++ )
3301 playlist_ItemAddOption( p_item, ppsz_options[i] );
3305 for( i = 0 ; i < i_options ; i++ )
3307 free( ppsz_options[i] );
3309 free( ppsz_options );