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_( "HTTP interface x509 PEM certificate file " \
84 #define KEY_TEXT N_( "Private key file" )
85 #define KEY_LONGTEXT N_( "HTTP interface x509 PEM private key file" )
86 #define CA_TEXT N_( "Root CA file" )
87 #define CA_LONGTEXT N_( "HTTP interface x509 PEM trusted root CA " \
89 #define CRL_TEXT N_( "CRL file" )
90 #define CRL_LONGTEXT N_( "HTTP interace Certificates Revocation List file" )
93 set_description( _("HTTP remote control interface") );
94 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
95 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
96 add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
97 add_string ( "http-intf-key", NULL, NULL, KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
98 add_string ( "http-intf-ca", NULL, NULL, CA_TEXT, CA_LONGTEXT, VLC_TRUE );
99 add_string ( "http-intf-crl", NULL, NULL, CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
100 set_capability( "interface", 0 );
101 set_callbacks( Open, Close );
105 /*****************************************************************************
107 *****************************************************************************/
108 static void Run ( intf_thread_t *p_intf );
110 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
113 static int DirectoryCheck( char *psz_dir )
117 #ifdef HAVE_SYS_STAT_H
118 struct stat stat_info;
120 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
126 if( ( p_dir = opendir( psz_dir ) ) == NULL )
135 static int HttpCallback( httpd_file_sys_t *p_args,
138 uint8_t **pp_data, int *pi_data );
140 static char *uri_extract_value( char *psz_uri, const char *psz_name,
141 char *psz_value, int i_value_max );
142 static int uri_test_param( char *psz_uri, const char *psz_name );
144 static void uri_decode_url_encoded( char *psz );
146 static char *Find_end_MRL( char *psz );
148 static playlist_item_t * parse_MRL( intf_thread_t * , char *psz );
150 /*****************************************************************************
152 *****************************************************************************/
153 typedef struct mvar_s
159 struct mvar_s **field;
162 #define STACK_MAX 100
165 char *stack[STACK_MAX];
169 struct httpd_file_sys_t
171 intf_thread_t *p_intf;
172 httpd_file_t *p_file;
173 httpd_redirect_t *p_redir;
180 /* inited for each access */
187 httpd_host_t *p_httpd_host;
190 httpd_file_sys_t **pp_files;
192 playlist_t *p_playlist;
193 input_thread_t *p_input;
199 /*****************************************************************************
200 * Activate: initialize and create stuff
201 *****************************************************************************/
202 static int Open( vlc_object_t *p_this )
204 intf_thread_t *p_intf = (intf_thread_t*)p_this;
207 char *psz_address = "";
208 const char *psz_cert;
213 psz_host = config_GetPsz( p_intf, "http-host" );
217 psz_address = psz_host;
219 psz_parser = strchr( psz_host, ':' );
222 *psz_parser++ = '\0';
223 i_port = atoi( psz_parser );
227 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
230 return( VLC_ENOMEM );
232 p_sys->p_playlist = NULL;
233 p_sys->p_input = NULL;
236 psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
237 if ( psz_cert != NULL )
241 msg_Dbg( p_intf, "enablind TLS for HTTP interface (cert file: %s)",
243 psz_pem = config_GetPsz( p_intf, "http-intf-key" );
245 p_tls = tls_ServerCreate( p_this, psz_cert, psz_pem );
248 msg_Err( p_intf, "TLS initialization error" );
253 psz_pem = config_GetPsz( p_intf, "http-intf-ca" );
254 if ( ( psz_pem != NULL) && tls_ServerAddCA( p_tls, psz_pem ) )
256 msg_Err( p_intf, "TLS CA error" );
257 tls_ServerDelete( p_tls );
262 psz_pem = config_GetPsz( p_intf, "http-intf-crl" );
263 if ( ( psz_pem != NULL) && tls_ServerAddCRL( p_tls, psz_pem ) )
265 msg_Err( p_intf, "TLS CRL error" );
266 tls_ServerDelete( p_tls );
281 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
283 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_intf), psz_address,
285 if( p_sys->p_httpd_host == NULL )
287 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
289 tls_ServerDelete( p_tls );
300 p_sys->pp_files = NULL;
302 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
303 ( defined(WIN32) && !defined(UNDER_CE ) )
304 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
306 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
307 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
313 sprintf( psz_src, "%s/http", psz_vlcpath);
315 sprintf( psz_src, "%s/share/http", psz_vlcpath);
319 psz_src = config_GetPsz( p_intf, "http-src" );
321 if( !psz_src || *psz_src == '\0' )
323 if( !DirectoryCheck( "share/http" ) )
325 psz_src = strdup( "share/http" );
327 else if( !DirectoryCheck( DATA_PATH "/http" ) )
329 psz_src = strdup( DATA_PATH "/http" );
334 if( !psz_src || *psz_src == '\0' )
336 msg_Err( p_intf, "invalid src dir" );
340 /* remove trainling \ or / */
341 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
342 psz_src[strlen( psz_src ) - 1] == '/' )
344 psz_src[strlen( psz_src ) - 1] = '\0';
347 ParseDirectory( p_intf, psz_src, psz_src );
350 if( p_sys->i_files <= 0 )
352 msg_Err( p_intf, "cannot find any files (%s)", psz_src );
355 p_intf->pf_run = Run;
361 if( psz_src ) free( psz_src );
362 if( p_sys->pp_files )
364 free( p_sys->pp_files );
366 httpd_HostDelete( p_sys->p_httpd_host );
371 /*****************************************************************************
372 * CloseIntf: destroy interface
373 *****************************************************************************/
374 void Close ( vlc_object_t *p_this )
376 intf_thread_t *p_intf = (intf_thread_t *)p_this;
377 intf_sys_t *p_sys = p_intf->p_sys;
383 vlm_Delete( p_sys->p_vlm );
385 for( i = 0; i < p_sys->i_files; i++ )
387 httpd_FileDelete( p_sys->pp_files[i]->p_file );
388 if( p_sys->pp_files[i]->p_redir )
390 httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
393 free( p_sys->pp_files[i]->file );
394 free( p_sys->pp_files[i]->name );
395 free( p_sys->pp_files[i] );
397 if( p_sys->pp_files )
399 free( p_sys->pp_files );
401 httpd_HostDelete( p_sys->p_httpd_host );
406 /*****************************************************************************
407 * Run: http interface thread
408 *****************************************************************************/
409 static void Run( intf_thread_t *p_intf )
411 intf_sys_t *p_sys = p_intf->p_sys;
413 while( !p_intf->b_die )
415 /* get the playlist */
416 if( p_sys->p_playlist == NULL )
418 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
421 /* Manage the input part */
422 if( p_sys->p_input == NULL )
424 if( p_sys->p_playlist )
427 vlc_object_find( p_sys->p_playlist,
432 else if( p_sys->p_input->b_dead )
434 vlc_object_release( p_sys->p_input );
435 p_sys->p_input = NULL;
440 msleep( INTF_IDLE_SLEEP );
445 vlc_object_release( p_sys->p_input );
446 p_sys->p_input = NULL;
449 if( p_sys->p_playlist )
451 vlc_object_release( p_sys->p_playlist );
452 p_sys->p_playlist = NULL;
457 /*****************************************************************************
459 *****************************************************************************/
460 #define MAX_DIR_SIZE 10240
462 /****************************************************************************
463 * FileToUrl: create a good name for an url from filename
464 ****************************************************************************/
465 static char *FileToUrl( char *name )
469 url = p = malloc( strlen( name ) + 1 );
477 while( *name == '\\' || *name == '/' )
479 while( *name == '/' )
489 /* convert '\\' into '/' */
502 if( ( p = strrchr( url, '/' ) ) != NULL )
504 if( !strncmp( p, "/index.", 7 ) )
512 /****************************************************************************
513 * ParseDirectory: parse recursively a directory, adding each file
514 ****************************************************************************/
515 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
518 intf_sys_t *p_sys = p_intf->p_sys;
519 char dir[MAX_DIR_SIZE];
520 #ifdef HAVE_SYS_STAT_H
521 struct stat stat_info;
524 struct dirent *p_dir_content;
528 char *password = NULL;
530 #ifdef HAVE_SYS_STAT_H
531 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
537 if( ( p_dir = opendir( psz_dir ) ) == NULL )
539 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
543 msg_Dbg( p_intf, "dir=%s", psz_dir );
545 sprintf( dir, "%s/.access", psz_dir );
546 if( ( file = fopen( dir, "r" ) ) != NULL )
551 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
553 i_size = fread( line, 1, 1023, file );
557 while( i_size > 0 && ( line[i_size-1] == '\n' ||
558 line[i_size-1] == '\r' ) )
565 p = strchr( line, ':' );
569 user = strdup( line );
570 password = strdup( p );
573 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
574 user, password, i_size );
581 /* parse psz_src dir */
582 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
587 if( p_dir_content->d_name[0] == '.' )
591 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
592 if( ParseDirectory( p_intf, psz_root, dir ) )
594 httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
599 f->file = strdup( dir );
600 f->name = FileToUrl( &dir[strlen( psz_root )] );
601 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
605 msg_Err( p_intf , "unable to parse directory" );
608 return( VLC_ENOMEM );
610 msg_Dbg( p_intf, "file=%s (url=%s)",
613 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
614 f->name, f->b_html ? "text/html" : NULL,
620 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
622 /* For rep/ add a redir from rep to rep/ */
623 if( f && f->name[strlen(f->name) - 1] == '/' )
625 char *psz_redir = strdup( f->name );
626 psz_redir[strlen( psz_redir ) - 1] = '\0';
628 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
629 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
649 /****************************************************************************
650 * var and set handling
651 ****************************************************************************/
653 static mvar_t *mvar_New( char *name, char *value )
655 mvar_t *v = malloc( sizeof( mvar_t ) );
657 if( !v ) return NULL;
658 v->name = strdup( name );
659 v->value = strdup( value ? value : "" );
662 v->field = malloc( sizeof( mvar_t * ) );
668 static void mvar_Delete( mvar_t *v )
675 for( i = 0; i < v->i_field; i++ )
677 mvar_Delete( v->field[i] );
683 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
685 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
686 v->field[v->i_field] = f;
690 static mvar_t *mvar_Duplicate( mvar_t *v )
695 n = mvar_New( v->name, v->value );
696 for( i = 0; i < v->i_field; i++ )
698 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
704 static void mvar_PushVar( mvar_t *v, mvar_t *f )
706 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
709 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
715 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
718 for( i = 0; i < v->i_field; i++ )
720 if( v->field[i] == f )
725 if( i >= v->i_field )
730 if( i + 1 < v->i_field )
732 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
735 /* FIXME should do a realloc */
738 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
741 char base[512], *field, *p;
744 /* format: name[index].field */
746 field = strchr( name, '.' );
749 int i = field - name;
750 strncpy( base, name, i );
756 strcpy( base, name );
759 if( ( p = strchr( base, '[' ) ) )
762 sscanf( p, "%d]", &i_index );
773 for( i = 0; i < s->i_field; i++ )
775 if( !strcmp( s->field[i]->name, base ) )
785 return mvar_GetVar( s->field[i], field );
799 static char *mvar_GetValue( mvar_t *v, char *field )
807 mvar_t *f = mvar_GetVar( v, field );
819 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
821 mvar_t *f = mvar_New( name, value );
822 mvar_PushVar( vars, f );
825 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
827 mvar_t *f = mvar_New( name, value );
828 mvar_AppendVar( vars, f );
832 /* arg= start[:stop[:step]],.. */
833 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
835 char *dup = strdup( arg );
837 mvar_t *s = mvar_New( name, "set" );
839 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
844 int i_start,i_stop,i_step;
847 p = strchr( str, ',' );
854 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
855 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
862 else if( i_match == 2 )
864 i_step = i_start < i_stop ? 1 : -1;
871 if( ( i_start < i_stop && i_step > 0 ) ||
872 ( i_start > i_stop && i_step < 0 ) )
874 for( i = i_start; ; i += i_step )
878 if( ( i_step > 0 && i > i_stop ) ||
879 ( i_step < 0 && i < i_stop ) )
884 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
885 sprintf( value, "%d", i );
887 mvar_PushNewVar( s, name, value );
898 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
900 mvar_t *s = mvar_New( name, "set" );
903 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
905 vlc_mutex_lock( &p_pl->object_lock );
906 for( i = 0; i < p_pl->i_size; i++ )
908 mvar_t *itm = mvar_New( name, "set" );
911 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
912 mvar_AppendNewVar( itm, "current", value );
914 sprintf( value, "%d", i );
915 mvar_AppendNewVar( itm, "index", value );
917 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->input.psz_name );
919 mvar_AppendNewVar( itm, "uri", p_pl->pp_items[i]->input.psz_uri );
921 mvar_AppendVar( s, itm );
923 vlc_mutex_unlock( &p_pl->object_lock );
928 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
930 mvar_t *s = mvar_New( name, "set" );
933 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
934 if( p_input == NULL )
939 vlc_mutex_lock( &p_input->input.p_item->lock );
940 for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
942 info_category_t *p_category = p_input->input.p_item->pp_categories[i];
943 mvar_t *cat = mvar_New( name, "set" );
944 mvar_t *iset = mvar_New( "info", "set" );
946 mvar_AppendNewVar( cat, "name", p_category->psz_name );
947 mvar_AppendVar( cat, iset );
949 for ( j = 0; j < p_category->i_infos; j++ )
951 info_t *p_info = p_category->pp_infos[j];
952 mvar_t *info = mvar_New( "info", "" );
954 msg_Dbg( p_input, "adding info name=%s value=%s",
955 p_info->psz_name, p_info->psz_value );
956 mvar_AppendNewVar( info, "name", p_info->psz_name );
957 mvar_AppendNewVar( info, "value", p_info->psz_value );
958 mvar_AppendVar( iset, info );
960 mvar_AppendVar( s, cat );
962 vlc_mutex_unlock( &p_input->input.p_item->lock );
967 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
969 mvar_t *s = mvar_New( name, "set" );
973 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
974 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
976 for( i= 0; i < info.i_count; )
980 inf = mvar_New( name, "set" );
983 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
984 info.info[i].psz_name, info.info[i].psz_value ); */
985 mvar_AppendNewVar( inf,
986 info.info[i].psz_name,
987 info.info[i].psz_value );
989 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
990 mvar_AppendVar( s, inf );
995 for( i = 0; i < info.i_count; i++ )
997 free( info.info[i].psz_name );
998 free( info.info[i].psz_value );
1000 if( info.i_count > 0 )
1009 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1011 mvar_t *s = mvar_New( name, "set" );
1012 char tmp[MAX_DIR_SIZE], *p, *src;
1013 #ifdef HAVE_SYS_STAT_H
1014 struct stat stat_info;
1017 struct dirent *p_dir_content;
1020 /* convert all / to native separator */
1021 #if defined( WIN32 )
1022 while( (p = strchr( psz_dir, '/' )) )
1031 /* remove trailling separator */
1032 while( strlen( psz_dir ) > 1 &&
1033 #if defined( WIN32 )
1034 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1036 psz_dir[strlen( psz_dir ) -1 ] == sep )
1038 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1040 /* remove double separator */
1041 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1043 if( src[0] == sep && src[1] == sep )
1051 if( *psz_dir == '\0' )
1055 /* first fix all .. dir */
1059 if( src[0] == '.' && src[1] == '.' )
1062 if( p <= &psz_dir[1] )
1069 while( p > &psz_dir[1] && *p != sep )
1074 else if( *src == sep )
1076 if( p > psz_dir && p[-1] == sep )
1090 } while( *src && *src != sep );
1095 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1097 #ifdef HAVE_SYS_STAT_H
1098 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1104 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1106 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1110 /* remove traling / or \ */
1111 for( p = &psz_dir[strlen( psz_dir) - 1];
1112 p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1121 /* parse psz_src dir */
1122 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1126 if( !strcmp( p_dir_content->d_name, "." ) )
1131 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1133 #ifdef HAVE_SYS_STAT_H
1134 if( stat( tmp, &stat_info ) == -1 )
1139 f = mvar_New( name, "set" );
1140 mvar_AppendNewVar( f, "name", tmp );
1142 #ifdef HAVE_SYS_STAT_H
1143 if( S_ISDIR( stat_info.st_mode ) )
1145 mvar_AppendNewVar( f, "type", "directory" );
1147 else if( S_ISREG( stat_info.st_mode ) )
1149 mvar_AppendNewVar( f, "type", "file" );
1153 mvar_AppendNewVar( f, "type", "unknown" );
1156 sprintf( tmp, I64Fd, (int64_t)stat_info.st_size );
1157 mvar_AppendNewVar( f, "size", tmp );
1159 /* FIXME memory leak FIXME */
1161 ctime_r( &stat_info.st_mtime, tmp );
1162 mvar_AppendNewVar( f, "date", tmp );
1164 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1168 mvar_AppendNewVar( f, "type", "unknown" );
1169 mvar_AppendNewVar( f, "size", "unknown" );
1170 mvar_AppendNewVar( f, "date", "unknown" );
1172 mvar_AppendVar( s, f );
1178 static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
1180 mvar_t *s = mvar_New( name, "set" );
1184 /* fprintf( stderr," mvar_VlmSetNew: name=`%s'\n", name ); */
1185 if( vlm == NULL ) return s;
1187 if( vlm_ExecuteCommand( vlm, "show", &msg ) )
1192 for( i = 0; i < msg->i_child; i++ )
1194 /* Over media, schedule */
1195 vlm_message_t *ch = msg->child[i];
1198 for( j = 0; j < ch->i_child; j++ )
1201 vlm_message_t *el = ch->child[j];
1202 vlm_message_t *inf, *desc;
1207 sprintf( psz, "show %s", el->psz_name );
1208 if( vlm_ExecuteCommand( vlm, psz, &inf ) )
1210 desc = inf->child[0];
1212 /* Add a node with name and info */
1213 set = mvar_New( name, "set" );
1214 mvar_AppendNewVar( set, "name", el->psz_name );
1216 /* fprintf( stderr, "#### name=%s\n", el->psz_name ); */
1218 for( k = 0; k < desc->i_child; k++ )
1220 vlm_message_t *ch = desc->child[k];
1221 if( ch->i_child > 0 )
1224 mvar_t *n = mvar_New( ch->psz_name, "set" );
1226 /* fprintf( stderr, " child=%s [%d]\n", ch->psz_name, ch->i_child ); */
1227 for( c = 0; c < ch->i_child; c++ )
1229 mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
1230 mvar_AppendVar( n, in );
1232 /* fprintf( stderr, " sub=%s\n", ch->child[c]->psz_name );*/
1234 mvar_AppendVar( set, n );
1238 /* fprintf( stderr, " child=%s->%s\n", ch->psz_name, ch->psz_value ); */
1239 mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
1242 vlm_MessageDelete( inf );
1244 mvar_AppendVar( s, set );
1247 vlm_MessageDelete( msg );
1253 static void SSInit( rpn_stack_t * );
1254 static void SSClean( rpn_stack_t * );
1255 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1257 static void SSPush ( rpn_stack_t *, char * );
1258 static char *SSPop ( rpn_stack_t * );
1260 static void SSPushN ( rpn_stack_t *, int );
1261 static int SSPopN ( rpn_stack_t *, mvar_t * );
1264 /****************************************************************************
1266 ****************************************************************************/
1274 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1278 /* just load the file */
1280 *pp_data = malloc( 1025 ); /* +1 for \0 */
1281 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1284 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1290 (*pp_data)[*pi_data] = '\0';
1295 static int MacroParse( macro_t *m, uint8_t *psz_src )
1297 uint8_t *dup = strdup( psz_src );
1302 #define EXTRACT( name, l ) \
1304 p = strchr( src, '"' ); \
1309 m->name = strdup( src ); \
1330 if( !strncmp( src, "id=\"", 4 ) )
1334 else if( !strncmp( src, "param1=\"", 8 ) )
1336 EXTRACT( param1, 8 );
1338 else if( !strncmp( src, "param2=\"", 8 ) )
1340 EXTRACT( param2, 8 );
1347 if( strstr( src, "/>" ) )
1349 src = strstr( src, "/>" ) + 2;
1353 src += strlen( src );
1358 m->id = strdup( "" );
1360 if( m->param1 == NULL )
1362 m->param1 = strdup( "" );
1364 if( m->param2 == NULL )
1366 m->param2 = strdup( "" );
1375 static void MacroClean( macro_t *m )
1433 StrToMacroTypeTab [] =
1435 { "control", MVLC_CONTROL },
1436 /* player control */
1437 { "play", MVLC_PLAY },
1438 { "stop", MVLC_STOP },
1439 { "pause", MVLC_PAUSE },
1440 { "next", MVLC_NEXT },
1441 { "previous", MVLC_PREVIOUS },
1442 { "seek", MVLC_SEEK },
1443 { "keep", MVLC_KEEP },
1444 { "fullscreen", MVLC_FULLSCREEN },
1445 { "volume", MVLC_VOLUME },
1447 /* playlist management */
1448 { "add", MVLC_ADD },
1449 { "delete", MVLC_DEL },
1450 { "empty", MVLC_EMPTY },
1451 { "sort", MVLC_SORT },
1452 { "move", MVLC_MOVE },
1455 { "close", MVLC_CLOSE },
1456 { "shutdown", MVLC_SHUTDOWN },
1459 { "vlm_new", MVLC_VLM_NEW },
1460 { "vlm_setup", MVLC_VLM_SETUP },
1461 { "vlm_del", MVLC_VLM_DEL },
1462 { "vlm_play", MVLC_VLM_PLAY },
1463 { "vlm_pause", MVLC_VLM_PAUSE },
1464 { "vlm_stop", MVLC_VLM_STOP },
1465 { "vlm_seek", MVLC_VLM_SEEK },
1466 { "vlm_load", MVLC_VLM_LOAD },
1467 { "vlm_save", MVLC_VLM_SAVE },
1469 { "rpn", MVLC_RPN },
1471 { "foreach", MVLC_FOREACH },
1472 { "value", MVLC_VALUE },
1475 { "else", MVLC_ELSE },
1476 { "end", MVLC_END },
1477 { "get", MVLC_GET },
1478 { "set", MVLC_SET },
1479 { "int", MVLC_INT },
1480 { "float", MVLC_FLOAT },
1481 { "string", MVLC_STRING },
1484 { NULL, MVLC_UNKNOWN }
1487 static int StrToMacroType( char *name )
1491 if( !name || *name == '\0')
1493 return MVLC_UNKNOWN;
1495 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1497 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1499 return StrToMacroTypeTab[i].i_type;
1502 return MVLC_UNKNOWN;
1505 static void MacroDo( httpd_file_sys_t *p_args,
1507 uint8_t *p_request, int i_request,
1508 uint8_t **pp_data, int *pi_data,
1511 intf_thread_t *p_intf = p_args->p_intf;
1512 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1515 #define ALLOC( l ) \
1517 int __i__ = *pp_dst - *pp_data; \
1519 *pp_data = realloc( *pp_data, *pi_data ); \
1520 *pp_dst = (*pp_data) + __i__; \
1522 #define PRINT( str ) \
1523 ALLOC( strlen( str ) + 1 ); \
1524 *pp_dst += sprintf( *pp_dst, str );
1526 #define PRINTS( str, s ) \
1527 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1529 char * psz_cur = *pp_dst; \
1530 *pp_dst += sprintf( *pp_dst, str, s ); \
1531 while( psz_cur && *psz_cur ) \
1533 /* Prevent script injection */ \
1534 if( *psz_cur == '<' ) *psz_cur = '*'; \
1535 if( *psz_cur == '>' ) *psz_cur = '*'; \
1540 switch( StrToMacroType( m->id ) )
1543 if( i_request <= 0 )
1547 uri_extract_value( p_request, "control", control, 512 );
1548 if( *m->param1 && !strstr( m->param1, control ) )
1550 msg_Warn( p_intf, "unauthorized control=%s", control );
1553 switch( StrToMacroType( control ) )
1560 uri_extract_value( p_request, "item", item, 512 );
1561 i_item = atoi( item );
1562 playlist_Control( p_sys->p_playlist, PLAYLIST_GOTO,
1564 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1568 playlist_Control( p_sys->p_playlist, PLAYLIST_STOP);
1569 msg_Dbg( p_intf, "requested playlist stop" );
1572 playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE );
1573 msg_Dbg( p_intf, "requested playlist pause" );
1576 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, 1 );
1577 msg_Dbg( p_intf, "requested playlist next" );
1580 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, -1);
1581 msg_Dbg( p_intf, "requested playlist next" );
1583 case MVLC_FULLSCREEN:
1585 if( p_sys->p_input )
1587 vout_thread_t *p_vout;
1588 p_vout = vlc_object_find( p_sys->p_input,
1589 VLC_OBJECT_VOUT, FIND_CHILD );
1593 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1594 vlc_object_release( p_vout );
1595 msg_Dbg( p_intf, "requested fullscreen toggle" );
1609 #define POSITION_ABSOLUTE 12
1610 #define POSITION_REL_FOR 13
1611 #define POSITION_REL_BACK 11
1612 #define TIME_ABSOLUTE 0
1613 #define TIME_REL_FOR 1
1614 #define TIME_REL_BACK -1
1615 if( p_sys->p_input )
1617 uri_extract_value( p_request, "seek_value", value, 20 );
1618 uri_decode_url_encoded( value );
1620 var_Get( p_sys->p_input, "length", &val);
1621 i_length = val.i_time;
1623 while( p_value[0] != '\0' )
1629 i_relative = TIME_REL_FOR;
1635 i_relative = TIME_REL_BACK;
1639 case '0': case '1': case '2': case '3': case '4':
1640 case '5': case '6': case '7': case '8': case '9':
1642 i_stock = strtol( p_value , &p_value , 10 );
1645 case '%': /* for percentage ie position */
1647 i_relative += POSITION_ABSOLUTE;
1655 i_value = 60 * (i_value + i_stock) ;
1660 case 'h': case 'H': /* hours */
1662 i_value += 3600 * i_stock;
1664 /* other characters which are not numbers are not important */
1665 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1671 case 'm': case 'M': case '\'': /* minutes */
1673 i_value += 60 * i_stock;
1676 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1682 case 's': case 'S': case '"': /* seconds */
1686 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1700 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1707 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1708 val.i_time = (uint64_t)( i_value ) * 1000000;
1710 val.i_time = i_length;
1712 var_Set( p_sys->p_input, "time", val );
1713 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1718 var_Get( p_sys->p_input, "time", &val );
1719 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1721 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1724 val.i_time = i_length;
1726 var_Set( p_sys->p_input, "time", val );
1727 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1732 var_Get( p_sys->p_input, "time", &val );
1733 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1738 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1740 var_Set( p_sys->p_input, "time", val );
1741 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1744 case POSITION_ABSOLUTE:
1746 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1747 var_Set( p_sys->p_input, "position", val );
1748 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1751 case POSITION_REL_FOR:
1753 var_Get( p_sys->p_input, "position", &val );
1754 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1755 var_Set( p_sys->p_input, "position", val );
1756 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1759 case POSITION_REL_BACK:
1761 var_Get( p_sys->p_input, "position", &val );
1762 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1763 var_Set( p_sys->p_input, "position", val );
1764 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
1769 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1774 #undef POSITION_ABSOLUTE
1775 #undef POSITION_REL_FOR
1776 #undef POSITION_REL_BACK
1777 #undef TIME_ABSOLUTE
1779 #undef TIME_REL_BACK
1785 audio_volume_t i_volume;
1788 uri_extract_value( p_request, "value", vol, 8 );
1789 aout_VolumeGet( p_intf, &i_volume );
1790 uri_decode_url_encoded( vol );
1794 i_value = atoi( vol + 1 );
1795 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1797 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1798 msg_Dbg( p_intf, "requested volume set: max" );
1801 aout_VolumeSet( p_intf , (i_volume + i_value) );
1802 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1807 i_value = atoi( vol + 1 );
1808 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1810 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1811 msg_Dbg( p_intf, "requested volume set: min" );
1814 aout_VolumeSet( p_intf , (i_volume - i_value) );
1815 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1818 if( strstr(vol, "%") != NULL )
1820 i_value = atoi( vol );
1821 if( (i_value <= 100) && (i_value>=0) ){
1822 aout_VolumeSet( p_intf, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/100+AOUT_VOLUME_MIN);
1823 msg_Dbg( p_intf, "requested volume set: %i%%", atoi( vol ));
1827 i_value = atoi( vol );
1828 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1830 aout_VolumeSet( p_intf , atoi( vol ) );
1831 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1837 /* playlist management */
1841 playlist_item_t * p_item;
1843 uri_extract_value( p_request, "mrl", mrl, 512 );
1844 uri_decode_url_encoded( mrl );
1845 p_item = parse_MRL( p_intf, mrl );
1847 if( !p_item || !p_item->input.psz_uri ||
1848 !*p_item->input.psz_uri )
1850 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1853 playlist_AddItem( p_sys->p_playlist , p_item ,
1854 PLAYLIST_APPEND, PLAYLIST_END );
1855 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1862 int i_item, *p_items = NULL, i_nb_items = 0;
1863 char item[512], *p_parser = p_request;
1865 /* Get the list of items to delete */
1867 uri_extract_value( p_parser, "item", item, 512 )) )
1869 if( !*item ) continue;
1871 i_item = atoi( item );
1872 p_items = realloc( p_items, (i_nb_items + 1) *
1874 p_items[i_nb_items] = i_item;
1878 /* The items need to be deleted from in reversed order */
1882 for( i = 0; i < i_nb_items; i++ )
1885 for( j = 0; j < i_nb_items; j++ )
1887 if( p_items[j] > p_items[i_index] )
1891 playlist_Delete( p_sys->p_playlist,
1893 msg_Dbg( p_intf, "requested playlist delete: %d",
1895 p_items[i_index] = -1;
1899 if( p_items ) free( p_items );
1904 int i_item, *p_items = NULL, i_nb_items = 0;
1905 char item[512], *p_parser = p_request;
1908 /* Get the list of items to keep */
1910 uri_extract_value( p_parser, "item", item, 512 )) )
1912 if( !*item ) continue;
1914 i_item = atoi( item );
1915 p_items = realloc( p_items, (i_nb_items + 1) *
1917 p_items[i_nb_items] = i_item;
1921 /* The items need to be deleted from in reversed order */
1922 for( i = p_sys->p_playlist->i_size - 1; i >= 0 ; i-- )
1924 /* Check if the item is in the keep list */
1925 for( j = 0 ; j < i_nb_items ; j++ )
1927 if( p_items[j] == i ) break;
1929 if( j == i_nb_items )
1931 playlist_Delete( p_sys->p_playlist, i );
1932 msg_Dbg( p_intf, "requested playlist delete: %d",
1937 if( p_items ) free( p_items );
1942 while( p_sys->p_playlist->i_size > 0 )
1944 playlist_Delete( p_sys->p_playlist, 0 );
1946 msg_Dbg( p_intf, "requested playlist empty" );
1955 uri_extract_value( p_request, "type", type, 12 );
1956 uri_extract_value( p_request, "order", order, 2 );
1958 if( order[0] == '0' ) i_order = ORDER_NORMAL;
1959 else i_order = ORDER_REVERSE;
1961 if( !strcmp( type , "title" ) )
1963 playlist_SortTitle( p_sys->p_playlist , i_order );
1964 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
1965 } else if( !strcmp( type , "author" ) )
1967 playlist_SortAuthor( p_sys->p_playlist , i_order );
1968 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
1969 } else if( !strcmp( type , "shuffle" ) )
1971 playlist_Sort( p_sys->p_playlist , SORT_RANDOM, ORDER_NORMAL );
1972 msg_Dbg( p_intf, "requested playlist shuffle");
1983 uri_extract_value( p_request, "psz_pos", psz_pos, 6 );
1984 uri_extract_value( p_request, "psz_newpos", psz_newpos, 6 );
1985 i_pos = atoi( psz_pos );
1986 i_newpos = atoi( psz_newpos );
1987 if ( i_pos < i_newpos )
1989 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
1991 playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
1993 msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
1997 /* admin function */
2001 uri_extract_value( p_request, "id", id, 512 );
2002 msg_Dbg( p_intf, "requested close id=%s", id );
2004 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
2006 msg_Warn( p_intf, "close failed for id=%s", id );
2013 msg_Dbg( p_intf, "requested shutdown" );
2014 p_intf->p_vlc->b_die = VLC_TRUE;
2019 case MVLC_VLM_SETUP:
2021 static const char *vlm_properties[11] =
2024 "enabled", "disabled", "loop", "unloop",
2026 "input", "output", "option", "date", "period", "repeat", "append",
2028 vlm_message_t *vlm_answer;
2030 char *psz = malloc( strlen( p_request ) + 1000 );
2035 if( p_intf->p_sys->p_vlm == NULL )
2036 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2038 if( p_intf->p_sys->p_vlm == NULL ) break;
2040 uri_extract_value( p_request, "name", name, 512 );
2041 if( StrToMacroType( control ) == MVLC_VLM_NEW )
2044 uri_extract_value( p_request, "type", type, 20 );
2045 p += sprintf( psz, "new %s %s", name, type );
2049 p += sprintf( psz, "setup %s", name );
2051 /* Parse the request */
2052 for( i = 0; i < 11; i++ )
2055 uri_extract_value( p_request, vlm_properties[i], val, 512 );
2056 uri_decode_url_encoded( val );
2057 if( strlen( val ) > 0 && i >= 4 )
2059 p += sprintf( p, " %s %s", vlm_properties[i], val );
2061 else if( uri_test_param( p_request, vlm_properties[i] ) && i < 4 )
2063 p += sprintf( p, " %s", vlm_properties[i] );
2066 fprintf( stderr, "vlm_ExecuteCommand: %s\n", psz );
2067 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2068 if( vlm_answer->psz_value == NULL ) /* there is no error */
2070 vlm_error = strdup( "" );
2074 vlm_error = malloc( strlen(vlm_answer->psz_name) +
2075 strlen(vlm_answer->psz_value) +
2076 strlen( " : ") + 1 );
2077 sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
2078 vlm_answer->psz_value );
2081 mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
2083 vlm_MessageDelete( vlm_answer );
2091 vlm_message_t *vlm_answer;
2094 if( p_intf->p_sys->p_vlm == NULL )
2095 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2097 if( p_intf->p_sys->p_vlm == NULL ) break;
2099 uri_extract_value( p_request, "name", name, 512 );
2100 sprintf( psz, "del %s", name );
2102 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2103 /* FIXME do a vlm_answer -> var stack conversion */
2104 vlm_MessageDelete( vlm_answer );
2109 case MVLC_VLM_PAUSE:
2113 vlm_message_t *vlm_answer;
2116 if( p_intf->p_sys->p_vlm == NULL )
2117 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2119 if( p_intf->p_sys->p_vlm == NULL ) break;
2121 uri_extract_value( p_request, "name", name, 512 );
2122 if( StrToMacroType( control ) == MVLC_VLM_PLAY )
2123 sprintf( psz, "control %s play", name );
2124 else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
2125 sprintf( psz, "control %s pause", name );
2126 else if( StrToMacroType( control ) == MVLC_VLM_STOP )
2127 sprintf( psz, "control %s stop", name );
2128 else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
2131 uri_extract_value( p_request, "percent", percent, 512 );
2132 sprintf( psz, "control %s seek %s", name, percent );
2135 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2136 /* FIXME do a vlm_answer -> var stack conversion */
2137 vlm_MessageDelete( vlm_answer );
2143 vlm_message_t *vlm_answer;
2147 if( p_intf->p_sys->p_vlm == NULL )
2148 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2150 if( p_intf->p_sys->p_vlm == NULL ) break;
2152 uri_extract_value( p_request, "file", file, 512 );
2153 uri_decode_url_encoded( file );
2155 if( StrToMacroType( control ) == MVLC_VLM_LOAD )
2156 sprintf( psz, "load %s", file );
2158 sprintf( psz, "save %s", file );
2160 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2161 /* FIXME do a vlm_answer -> var stack conversion */
2162 vlm_MessageDelete( vlm_answer );
2169 PRINTS( "<!-- control param(%s) unsuported -->", control );
2181 if( i_request <= 0 ||
2182 *m->param1 == '\0' ||
2183 strstr( p_request, m->param1 ) == NULL )
2187 uri_extract_value( p_request, m->param1, value, 512 );
2188 uri_decode_url_encoded( value );
2190 switch( StrToMacroType( m->param2 ) )
2194 config_PutInt( p_intf, m->param1, i );
2198 config_PutFloat( p_intf, m->param1, f );
2201 config_PutPsz( p_intf, m->param1, value );
2204 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
2215 if( *m->param1 == '\0' )
2220 switch( StrToMacroType( m->param2 ) )
2223 i = config_GetInt( p_intf, m->param1 );
2224 sprintf( value, "%i", i );
2227 f = config_GetFloat( p_intf, m->param1 );
2228 sprintf( value, "%f", f );
2231 psz = config_GetPsz( p_intf, m->param1 );
2232 sprintf( value, "%s", psz ? psz : "" );
2233 if( psz ) free( psz );
2236 sprintf( value, "invalid type(%s) in set", m->param2 );
2239 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
2240 PRINTS( "%s", value );
2249 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2250 s = SSPop( &p_args->stack );
2251 v = mvar_GetValue( p_args->vars, s );
2255 v = s = SSPop( &p_args->stack );
2263 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2268 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2269 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2277 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
2284 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
2289 i_skip = MacroParse( &m, src );
2291 i_id = StrToMacroType( m.id );
2308 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2309 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2311 return src + ( b_after ? i_skip : 0 );
2313 else if( i_level < 0 )
2329 static void Execute( httpd_file_sys_t *p_args,
2330 uint8_t *p_request, int i_request,
2331 uint8_t **pp_data, int *pi_data,
2333 uint8_t *_src, uint8_t *_end )
2335 intf_thread_t *p_intf = p_args->p_intf;
2337 uint8_t *src, *dup, *end;
2338 uint8_t *dst = *pp_dst;
2340 src = dup = malloc( _end - _src + 1 );
2341 end = src +( _end - _src );
2343 memcpy( src, _src, _end - _src );
2346 /* we parse searching <vlc */
2352 p = strstr( src, "<vlc" );
2353 if( p < end && p == src )
2357 src += MacroParse( &m, src );
2359 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2361 switch( StrToMacroType( m.id ) )
2368 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2369 if( SSPopN( &p_args->stack, p_args->vars ) )
2377 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2381 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2385 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2388 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2392 else if( i_test == 1 )
2395 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2397 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2401 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2410 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2411 uint8_t *start = src;
2412 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2419 if( !strcmp( m.param2, "integer" ) )
2421 char *arg = SSPop( &p_args->stack );
2422 index = mvar_IntegerSetNew( m.param1, arg );
2425 else if( !strcmp( m.param2, "directory" ) )
2427 char *arg = SSPop( &p_args->stack );
2428 index = mvar_FileSetNew( m.param1, arg );
2431 else if( !strcmp( m.param2, "playlist" ) )
2433 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2435 else if( !strcmp( m.param2, "information" ) )
2437 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2439 else if( !strcmp( m.param2, "vlm" ) )
2441 if( p_intf->p_sys->p_vlm == NULL )
2442 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2443 index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
2446 else if( !strcmp( m.param2, "hosts" ) )
2448 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2450 else if( !strcmp( m.param2, "urls" ) )
2452 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2454 else if( !strcmp( m.param2, "connections" ) )
2456 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2459 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2461 index = mvar_Duplicate( v );
2465 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2470 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2472 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2474 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2477 f->name = strdup( m.param1 );
2480 mvar_PushVar( p_args->vars, f );
2481 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2482 mvar_RemoveVar( p_args->vars, f );
2486 mvar_Delete( index );
2493 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2501 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2504 int i_index = dst - *pp_data;
2507 *pp_data = realloc( *pp_data, *pi_data );
2508 dst = (*pp_data) + i_index;
2510 memcpy( dst, src, i_copy );
2520 /****************************************************************************
2522 ****************************************************************************
2523 * a file with b_html is parsed and all "macro" replaced
2524 * <vlc id="macro name" [param1="" [param2=""]] />
2527 ****************************************************************************/
2528 static int HttpCallback( httpd_file_sys_t *p_args,
2529 httpd_file_t *p_file,
2531 uint8_t **pp_data, int *pi_data )
2533 int i_request = p_request ? strlen( p_request ) : 0;
2537 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2539 p = *pp_data = malloc( 10240 );
2542 return VLC_EGENERIC;
2544 p += sprintf( p, "<html>\n" );
2545 p += sprintf( p, "<head>\n" );
2546 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2547 p += sprintf( p, "</head>\n" );
2548 p += sprintf( p, "<body>\n" );
2549 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2550 p += sprintf( p, "<hr />\n" );
2551 p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
2552 p += sprintf( p, "</body>\n" );
2553 p += sprintf( p, "</html>\n" );
2555 *pi_data = strlen( *pp_data );
2560 if( !p_args->b_html )
2562 FileLoad( f, pp_data, pi_data );
2570 char position[4]; /* percentage */
2571 char time[12]; /* in seconds */
2572 char length[12]; /* in seconds */
2573 audio_volume_t i_volume;
2577 #define p_sys p_args->p_intf->p_sys
2578 if( p_sys->p_input )
2580 var_Get( p_sys->p_input, "position", &val);
2581 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2582 var_Get( p_sys->p_input, "time", &val);
2583 sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2584 var_Get( p_sys->p_input, "length", &val);
2585 sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2587 var_Get( p_sys->p_input, "state", &val );
2588 if( val.i_int == PLAYING_S )
2590 sprintf( state, "playing" );
2591 } else if( val.i_int == PAUSE_S )
2593 sprintf( state, "paused" );
2596 sprintf( state, "stop" );
2600 sprintf( position, "%d", 0 );
2601 sprintf( time, "%d", 0 );
2602 sprintf( length, "%d", 0 );
2603 sprintf( state, "stop" );
2607 aout_VolumeGet( p_args->p_intf , &i_volume );
2608 sprintf( volume , "%d" , (int)i_volume );
2610 p_args->vars = mvar_New( "variables", "" );
2611 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2612 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2613 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
2614 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2615 mvar_AppendNewVar( p_args->vars, "stream_position", position );
2616 mvar_AppendNewVar( p_args->vars, "stream_time", time );
2617 mvar_AppendNewVar( p_args->vars, "stream_length", length );
2618 mvar_AppendNewVar( p_args->vars, "volume", volume );
2619 mvar_AppendNewVar( p_args->vars, "stream_state", state );
2621 SSInit( &p_args->stack );
2623 /* first we load in a temporary buffer */
2624 FileLoad( f, &p_buffer, &i_buffer );
2626 /* allocate output */
2627 *pi_data = i_buffer + 1000;
2628 dst = *pp_data = malloc( *pi_data );
2630 /* we parse executing all <vlc /> macros */
2631 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2634 *pi_data = dst - *pp_data;
2636 SSClean( &p_args->stack );
2637 mvar_Delete( p_args->vars );
2646 /****************************************************************************
2648 ****************************************************************************/
2649 static int uri_test_param( char *psz_uri, const char *psz_name )
2653 while( (p = strstr( p, psz_name )) )
2655 /* Verify that we are dealing with a post/get argument */
2656 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2665 static char *uri_extract_value( char *psz_uri, const char *psz_name,
2666 char *psz_value, int i_value_max )
2670 while( (p = strstr( p, psz_name )) )
2672 /* Verify that we are dealing with a post/get argument */
2673 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2682 p += strlen( psz_name );
2683 if( *p == '=' ) p++;
2685 if( strchr( p, '&' ) )
2687 i_len = strchr( p, '&' ) - p;
2691 /* for POST method */
2692 if( strchr( p, '\n' ) )
2694 i_len = strchr( p, '\n' ) - p;
2695 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2699 i_len = strlen( p );
2702 i_len = __MIN( i_value_max - 1, i_len );
2705 strncpy( psz_value, p, i_len );
2706 psz_value[i_len] = '\0';
2710 strncpy( psz_value, "", i_value_max );
2716 strncpy( psz_value, "", i_value_max );
2722 static void uri_decode_url_encoded( char *psz )
2724 char *dup = strdup( psz );
2742 *psz++ = strtol( val, NULL, 16 );
2744 else if( *p == '+' )
2758 /****************************************************************************
2759 * Light RPN evaluator
2760 ****************************************************************************/
2761 static void SSInit( rpn_stack_t *st )
2766 static void SSClean( rpn_stack_t *st )
2768 while( st->i_stack > 0 )
2770 free( st->stack[--st->i_stack] );
2774 static void SSPush( rpn_stack_t *st, char *s )
2776 if( st->i_stack < STACK_MAX )
2778 st->stack[st->i_stack++] = strdup( s );
2782 static char * SSPop( rpn_stack_t *st )
2784 if( st->i_stack <= 0 )
2786 return strdup( "" );
2790 return st->stack[--st->i_stack];
2794 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2803 i = strtol( name, &end, 0 );
2806 value = mvar_GetValue( vars, name );
2814 static void SSPushN( rpn_stack_t *st, int i )
2818 sprintf( v, "%d", i );
2822 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2829 while( *exp == ' ' )
2836 /* extract string */
2839 while( *exp && *exp != '\'' )
2850 p = strchr( exp, ' ' );
2855 exp += strlen( exp );
2860 strncpy( s, exp, i );
2871 /* 1. Integer function */
2872 if( !strcmp( s, "!" ) )
2874 SSPushN( st, ~SSPopN( st, vars ) );
2876 else if( !strcmp( s, "^" ) )
2878 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2880 else if( !strcmp( s, "&" ) )
2882 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2884 else if( !strcmp( s, "|" ) )
2886 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2888 else if( !strcmp( s, "+" ) )
2890 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2892 else if( !strcmp( s, "-" ) )
2894 int j = SSPopN( st, vars );
2895 int i = SSPopN( st, vars );
2896 SSPushN( st, i - j );
2898 else if( !strcmp( s, "*" ) )
2900 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2902 else if( !strcmp( s, "/" ) )
2906 j = SSPopN( st, vars );
2907 i = SSPopN( st, vars );
2909 SSPushN( st, j != 0 ? i / j : 0 );
2911 else if( !strcmp( s, "%" ) )
2915 j = SSPopN( st, vars );
2916 i = SSPopN( st, vars );
2918 SSPushN( st, j != 0 ? i % j : 0 );
2920 /* 2. integer tests */
2921 else if( !strcmp( s, "=" ) )
2923 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2925 else if( !strcmp( s, "<" ) )
2927 int j = SSPopN( st, vars );
2928 int i = SSPopN( st, vars );
2930 SSPushN( st, i < j ? -1 : 0 );
2932 else if( !strcmp( s, ">" ) )
2934 int j = SSPopN( st, vars );
2935 int i = SSPopN( st, vars );
2937 SSPushN( st, i > j ? -1 : 0 );
2939 else if( !strcmp( s, "<=" ) )
2941 int j = SSPopN( st, vars );
2942 int i = SSPopN( st, vars );
2944 SSPushN( st, i <= j ? -1 : 0 );
2946 else if( !strcmp( s, ">=" ) )
2948 int j = SSPopN( st, vars );
2949 int i = SSPopN( st, vars );
2951 SSPushN( st, i >= j ? -1 : 0 );
2953 /* 3. string functions */
2954 else if( !strcmp( s, "strcat" ) )
2956 char *s2 = SSPop( st );
2957 char *s1 = SSPop( st );
2958 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2968 else if( !strcmp( s, "strcmp" ) )
2970 char *s2 = SSPop( st );
2971 char *s1 = SSPop( st );
2973 SSPushN( st, strcmp( s1, s2 ) );
2977 else if( !strcmp( s, "strncmp" ) )
2979 int n = SSPopN( st, vars );
2980 char *s2 = SSPop( st );
2981 char *s1 = SSPop( st );
2983 SSPushN( st, strncmp( s1, s2 , n ) );
2987 else if( !strcmp( s, "strsub" ) )
2989 int n = SSPopN( st, vars );
2990 int m = SSPopN( st, vars );
2992 char *s = SSPop( st );
3004 str = malloc( i_len + 1 );
3006 memcpy( str, s + m - 1, i_len );
3007 str[ i_len ] = '\0';
3013 else if( !strcmp( s, "strlen" ) )
3015 char *str = SSPop( st );
3017 SSPushN( st, strlen( str ) );
3020 /* 4. stack functions */
3021 else if( !strcmp( s, "dup" ) )
3023 char *str = SSPop( st );
3028 else if( !strcmp( s, "drop" ) )
3030 char *str = SSPop( st );
3033 else if( !strcmp( s, "swap" ) )
3035 char *s1 = SSPop( st );
3036 char *s2 = SSPop( st );
3043 else if( !strcmp( s, "flush" ) )
3048 else if( !strcmp( s, "store" ) )
3050 char *value = SSPop( st );
3051 char *name = SSPop( st );
3053 mvar_PushNewVar( vars, name, value );
3057 else if( !strcmp( s, "value" ) )
3059 char *name = SSPop( st );
3060 char *value = mvar_GetValue( vars, name );
3062 SSPush( st, value );
3066 else if( !strcmp( s, "url_extract" ) )
3068 char *url = mvar_GetValue( vars, "url_value" );
3069 char *name = SSPop( st );
3072 uri_extract_value( url, name, value, 512 );
3073 uri_decode_url_encoded( value );
3074 SSPush( st, value );
3083 /**********************************************************************
3084 * Find_end_MRL: Find the end of the sentence :
3085 * this function parses the string psz and find the end of the item
3086 * and/or option with detecting the " and ' problems.
3087 * returns NULL if an error is detected, otherwise, returns a pointer
3088 * of the end of the sentence (after the last character)
3089 **********************************************************************/
3090 static char *Find_end_MRL( char *psz )
3100 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
3102 if( *s_sent == '\'' )
3104 s_sent = Find_end_MRL( s_sent );
3106 if( s_sent == NULL )
3116 if( *s_sent == '\"' )
3120 } else /* *s_sent == '\0' , which means the number of " is incorrect */
3130 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
3132 if( *s_sent == '\"' )
3134 s_sent = Find_end_MRL( s_sent );
3136 if( s_sent == NULL )
3146 if( *s_sent == '\'' )
3150 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
3156 default: /* now we can look for spaces */
3158 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
3160 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
3162 s_sent = Find_end_MRL( s_sent );
3173 /**********************************************************************
3174 * parse_MRL: parse the MRL, find the mrl string and the options,
3175 * create an item with all information in it, and return the item.
3176 * return NULL if there is an error.
3177 **********************************************************************/
3178 playlist_item_t * parse_MRL( intf_thread_t *p_intf, char *psz )
3180 char **ppsz_options = NULL;
3187 playlist_item_t * p_item = NULL;
3189 /* In case there is spaces before the mrl */
3190 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
3195 /* extract the mrl */
3196 s_temp = strstr( s_mrl , " :" );
3197 if( s_temp == NULL )
3199 s_temp = s_mrl + strlen( s_mrl );
3202 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
3209 /* if the mrl is between " or ', we must remove them */
3210 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
3212 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
3213 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
3214 mrl[ s_temp - s_mrl - 2 ] = '\0';
3217 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
3218 strncpy( mrl , s_mrl , s_temp - s_mrl );
3219 mrl[ s_temp - s_mrl ] = '\0';
3224 /* now we can take care of the options */
3225 while( (*s_mrl != '\0') && (i_error == 0) )
3234 case ':': /* an option */
3236 s_temp = Find_end_MRL( s_mrl );
3238 if( s_temp == NULL )
3245 ppsz_options = realloc( ppsz_options , i_options *
3247 ppsz_options[ i_options - 1 ] =
3248 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
3250 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
3253 /* don't forget to finish the string with a '\0' */
3254 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
3274 /* now create an item */
3275 p_item = playlist_ItemNew( p_intf, mrl, mrl);
3276 for( i = 0 ; i< i_options ; i++ )
3278 playlist_ItemAddOption( p_item, ppsz_options[i] );
3282 for( i = 0 ; i < i_options ; i++ )
3284 free( ppsz_options[i] );
3286 free( ppsz_options );