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"
43 #ifdef HAVE_SYS_STAT_H
44 # include <sys/stat.h>
55 #elif defined( WIN32 ) && !defined( UNDER_CE )
59 #if (!defined( WIN32 ) || defined(__MINGW32__))
60 /* Mingw has its own version of dirent */
64 /*****************************************************************************
66 *****************************************************************************/
67 static int Open ( vlc_object_t * );
68 static void Close( vlc_object_t * );
70 #define HOST_TEXT N_( "Host address" )
71 #define HOST_LONGTEXT N_( \
72 "You can set the address and port the http interface will bind to." )
73 #define SRC_TEXT N_( "Source directory" )
74 #define SRC_LONGTEXT N_( "Source directory" )
77 set_description( _("HTTP remote control interface") );
78 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
79 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
80 set_capability( "interface", 0 );
81 set_callbacks( Open, Close );
85 /*****************************************************************************
87 *****************************************************************************/
88 static void Run ( intf_thread_t *p_intf );
90 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
93 static int DirectoryCheck( char *psz_dir )
97 #ifdef HAVE_SYS_STAT_H
98 struct stat stat_info;
100 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
106 if( ( p_dir = opendir( psz_dir ) ) == NULL )
115 static int HttpCallback( httpd_file_sys_t *p_args,
118 uint8_t **pp_data, int *pi_data );
120 static char *uri_extract_value( char *psz_uri, const char *psz_name,
121 char *psz_value, int i_value_max );
122 static int uri_test_param( char *psz_uri, const char *psz_name );
124 static void uri_decode_url_encoded( char *psz );
126 static char *Find_end_MRL( char *psz );
128 static playlist_item_t * parse_MRL( intf_thread_t * , char *psz );
130 /*****************************************************************************
132 *****************************************************************************/
133 typedef struct mvar_s
139 struct mvar_s **field;
142 #define STACK_MAX 100
145 char *stack[STACK_MAX];
149 struct httpd_file_sys_t
151 intf_thread_t *p_intf;
152 httpd_file_t *p_file;
153 httpd_redirect_t *p_redir;
160 /* inited for each access */
167 httpd_host_t *p_httpd_host;
170 httpd_file_sys_t **pp_files;
172 playlist_t *p_playlist;
173 input_thread_t *p_input;
179 /*****************************************************************************
180 * Activate: initialize and create stuff
181 *****************************************************************************/
182 static int Open( vlc_object_t *p_this )
184 intf_thread_t *p_intf = (intf_thread_t*)p_this;
187 char *psz_address = "";
191 psz_host = config_GetPsz( p_intf, "http-host" );
195 psz_address = psz_host;
197 psz_parser = strchr( psz_host, ':' );
200 *psz_parser++ = '\0';
201 i_port = atoi( psz_parser );
209 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
210 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
213 return( VLC_ENOMEM );
215 p_sys->p_playlist = NULL;
216 p_sys->p_input = NULL;
219 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_intf), psz_address, i_port );
220 if( p_sys->p_httpd_host == NULL )
222 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
233 p_sys->pp_files = NULL;
235 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
236 ( defined(WIN32) && !defined(UNDER_CE ) )
237 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
239 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
240 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
246 sprintf( psz_src, "%s/http", psz_vlcpath);
248 sprintf( psz_src, "%s/share/http", psz_vlcpath);
252 psz_src = config_GetPsz( p_intf, "http-src" );
254 if( !psz_src || *psz_src == '\0' )
256 if( !DirectoryCheck( "share/http" ) )
258 psz_src = strdup( "share/http" );
260 else if( !DirectoryCheck( DATA_PATH "/http" ) )
262 psz_src = strdup( DATA_PATH "/http" );
267 if( !psz_src || *psz_src == '\0' )
269 msg_Err( p_intf, "invalid src dir" );
273 /* remove trainling \ or / */
274 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
275 psz_src[strlen( psz_src ) - 1] == '/' )
277 psz_src[strlen( psz_src ) - 1] = '\0';
280 ParseDirectory( p_intf, psz_src, psz_src );
283 if( p_sys->i_files <= 0 )
285 msg_Err( p_intf, "cannot find any files (%s)", psz_src );
288 p_intf->pf_run = Run;
294 if( psz_src ) free( psz_src );
295 if( p_sys->pp_files )
297 free( p_sys->pp_files );
299 httpd_HostDelete( p_sys->p_httpd_host );
304 /*****************************************************************************
305 * CloseIntf: destroy interface
306 *****************************************************************************/
307 void Close ( vlc_object_t *p_this )
309 intf_thread_t *p_intf = (intf_thread_t *)p_this;
310 intf_sys_t *p_sys = p_intf->p_sys;
316 vlm_Delete( p_sys->p_vlm );
319 for( i = 0; i < p_sys->i_files; i++ )
321 httpd_FileDelete( p_sys->pp_files[i]->p_file );
322 if( p_sys->pp_files[i]->p_redir )
324 httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
327 free( p_sys->pp_files[i]->file );
328 free( p_sys->pp_files[i]->name );
329 free( p_sys->pp_files[i] );
331 if( p_sys->pp_files )
333 free( p_sys->pp_files );
335 httpd_HostDelete( p_sys->p_httpd_host );
340 /*****************************************************************************
341 * Run: http interface thread
342 *****************************************************************************/
343 static void Run( intf_thread_t *p_intf )
345 intf_sys_t *p_sys = p_intf->p_sys;
347 while( !p_intf->b_die )
349 /* get the playlist */
350 if( p_sys->p_playlist == NULL )
352 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
355 /* Manage the input part */
356 if( p_sys->p_input == NULL )
358 if( p_sys->p_playlist )
361 vlc_object_find( p_sys->p_playlist,
366 else if( p_sys->p_input->b_dead )
368 vlc_object_release( p_sys->p_input );
369 p_sys->p_input = NULL;
374 msleep( INTF_IDLE_SLEEP );
379 vlc_object_release( p_sys->p_input );
380 p_sys->p_input = NULL;
383 if( p_sys->p_playlist )
385 vlc_object_release( p_sys->p_playlist );
386 p_sys->p_playlist = NULL;
391 /*****************************************************************************
393 *****************************************************************************/
394 #define MAX_DIR_SIZE 10240
396 /****************************************************************************
397 * FileToUrl: create a good name for an url from filename
398 ****************************************************************************/
399 static char *FileToUrl( char *name )
403 url = p = malloc( strlen( name ) + 1 );
411 while( *name == '\\' || *name == '/' )
413 while( *name == '/' )
423 /* convert '\\' into '/' */
436 if( ( p = strrchr( url, '/' ) ) != NULL )
438 if( !strncmp( p, "/index.", 7 ) )
446 /****************************************************************************
447 * ParseDirectory: parse recursively a directory, adding each file
448 ****************************************************************************/
449 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
452 intf_sys_t *p_sys = p_intf->p_sys;
453 char dir[MAX_DIR_SIZE];
454 #ifdef HAVE_SYS_STAT_H
455 struct stat stat_info;
458 struct dirent *p_dir_content;
462 char *password = NULL;
464 #ifdef HAVE_SYS_STAT_H
465 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
471 if( ( p_dir = opendir( psz_dir ) ) == NULL )
473 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
477 msg_Dbg( p_intf, "dir=%s", psz_dir );
479 sprintf( dir, "%s/.access", psz_dir );
480 if( ( file = fopen( dir, "r" ) ) != NULL )
485 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
487 i_size = fread( line, 1, 1023, file );
491 while( i_size > 0 && ( line[i_size-1] == '\n' ||
492 line[i_size-1] == '\r' ) )
499 p = strchr( line, ':' );
503 user = strdup( line );
504 password = strdup( p );
507 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
508 user, password, i_size );
515 /* parse psz_src dir */
516 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
521 if( p_dir_content->d_name[0] == '.' )
525 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
526 if( ParseDirectory( p_intf, psz_root, dir ) )
528 httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
533 f->file = strdup( dir );
534 f->name = FileToUrl( &dir[strlen( psz_root )] );
535 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
539 msg_Err( p_intf , "unable to parse directory" );
542 return( VLC_ENOMEM );
544 msg_Dbg( p_intf, "file=%s (url=%s)",
547 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
548 f->name, f->b_html ? "text/html" : NULL,
554 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
556 /* For rep/ add a redir from rep to rep/ */
557 if( f && f->name[strlen(f->name) - 1] == '/' )
559 char *psz_redir = strdup( f->name );
560 psz_redir[strlen( psz_redir ) - 1] = '\0';
562 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
563 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
583 /****************************************************************************
584 * var and set handling
585 ****************************************************************************/
587 static mvar_t *mvar_New( char *name, char *value )
589 mvar_t *v = malloc( sizeof( mvar_t ) );
591 if( !v ) return NULL;
592 v->name = strdup( name );
593 v->value = strdup( value ? value : "" );
596 v->field = malloc( sizeof( mvar_t * ) );
602 static void mvar_Delete( mvar_t *v )
609 for( i = 0; i < v->i_field; i++ )
611 mvar_Delete( v->field[i] );
617 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
619 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
620 v->field[v->i_field] = f;
624 static mvar_t *mvar_Duplicate( mvar_t *v )
629 n = mvar_New( v->name, v->value );
630 for( i = 0; i < v->i_field; i++ )
632 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
638 static void mvar_PushVar( mvar_t *v, mvar_t *f )
640 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
643 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
649 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
652 for( i = 0; i < v->i_field; i++ )
654 if( v->field[i] == f )
659 if( i >= v->i_field )
664 if( i + 1 < v->i_field )
666 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
669 /* FIXME should do a realloc */
672 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
675 char base[512], *field, *p;
678 /* format: name[index].field */
680 field = strchr( name, '.' );
683 int i = field - name;
684 strncpy( base, name, i );
690 strcpy( base, name );
693 if( ( p = strchr( base, '[' ) ) )
696 sscanf( p, "%d]", &i_index );
707 for( i = 0; i < s->i_field; i++ )
709 if( !strcmp( s->field[i]->name, base ) )
719 return mvar_GetVar( s->field[i], field );
733 static char *mvar_GetValue( mvar_t *v, char *field )
741 mvar_t *f = mvar_GetVar( v, field );
753 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
755 mvar_t *f = mvar_New( name, value );
756 mvar_PushVar( vars, f );
759 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
761 mvar_t *f = mvar_New( name, value );
762 mvar_AppendVar( vars, f );
766 /* arg= start[:stop[:step]],.. */
767 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
769 char *dup = strdup( arg );
771 mvar_t *s = mvar_New( name, "set" );
773 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
778 int i_start,i_stop,i_step;
781 p = strchr( str, ',' );
788 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
789 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
796 else if( i_match == 2 )
798 i_step = i_start < i_stop ? 1 : -1;
805 if( ( i_start < i_stop && i_step > 0 ) ||
806 ( i_start > i_stop && i_step < 0 ) )
808 for( i = i_start; ; i += i_step )
812 if( ( i_step > 0 && i > i_stop ) ||
813 ( i_step < 0 && i < i_stop ) )
818 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
819 sprintf( value, "%d", i );
821 mvar_PushNewVar( s, name, value );
832 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
834 mvar_t *s = mvar_New( name, "set" );
837 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
839 vlc_mutex_lock( &p_pl->object_lock );
840 for( i = 0; i < p_pl->i_size; i++ )
842 mvar_t *itm = mvar_New( name, "set" );
845 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
846 mvar_AppendNewVar( itm, "current", value );
848 sprintf( value, "%d", i );
849 mvar_AppendNewVar( itm, "index", value );
851 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->input.psz_name );
853 mvar_AppendNewVar( itm, "uri", p_pl->pp_items[i]->input.psz_uri );
855 sprintf( value, "%d", p_pl->pp_items[i]->i_group );
856 mvar_AppendNewVar( itm, "group", value );
858 mvar_AppendVar( s, itm );
860 vlc_mutex_unlock( &p_pl->object_lock );
865 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
867 mvar_t *s = mvar_New( name, "set" );
870 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
871 if( p_input == NULL )
876 vlc_mutex_lock( &p_input->input.p_item->lock );
877 for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
879 info_category_t *p_category = p_input->input.p_item->pp_categories[i];
880 mvar_t *cat = mvar_New( name, "set" );
881 mvar_t *iset = mvar_New( "info", "set" );
883 mvar_AppendNewVar( cat, "name", p_category->psz_name );
884 mvar_AppendVar( cat, iset );
886 for ( j = 0; j < p_category->i_infos; j++ )
888 info_t *p_info = p_category->pp_infos[j];
889 mvar_t *info = mvar_New( "info", "" );
891 msg_Dbg( p_input, "adding info name=%s value=%s",
892 p_info->psz_name, p_info->psz_value );
893 mvar_AppendNewVar( info, "name", p_info->psz_name );
894 mvar_AppendNewVar( info, "value", p_info->psz_value );
895 mvar_AppendVar( iset, info );
897 mvar_AppendVar( s, cat );
899 vlc_mutex_unlock( &p_input->input.p_item->lock );
904 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
906 mvar_t *s = mvar_New( name, "set" );
910 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
911 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
913 for( i= 0; i < info.i_count; )
917 inf = mvar_New( name, "set" );
920 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
921 info.info[i].psz_name, info.info[i].psz_value ); */
922 mvar_AppendNewVar( inf,
923 info.info[i].psz_name,
924 info.info[i].psz_value );
926 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
927 mvar_AppendVar( s, inf );
932 for( i = 0; i < info.i_count; i++ )
934 free( info.info[i].psz_name );
935 free( info.info[i].psz_value );
937 if( info.i_count > 0 )
946 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
948 mvar_t *s = mvar_New( name, "set" );
949 char tmp[MAX_DIR_SIZE], *p, *src;
950 #ifdef HAVE_SYS_STAT_H
951 struct stat stat_info;
954 struct dirent *p_dir_content;
957 /* convert all / to native separator */
959 while( (p = strchr( psz_dir, '/' )) )
968 /* remove trailling separator */
969 while( strlen( psz_dir ) > 1 &&
971 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
973 psz_dir[strlen( psz_dir ) -1 ] == sep )
975 psz_dir[strlen( psz_dir ) -1 ] ='\0';
977 /* remove double separator */
978 for( p = src = psz_dir; *src != '\0'; src++, p++ )
980 if( src[0] == sep && src[1] == sep )
988 if( *psz_dir == '\0' )
992 /* first fix all .. dir */
996 if( src[0] == '.' && src[1] == '.' )
999 if( p <= &psz_dir[1] )
1006 while( p > &psz_dir[1] && *p != sep )
1011 else if( *src == sep )
1013 if( p > psz_dir && p[-1] == sep )
1027 } while( *src && *src != sep );
1032 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1034 #ifdef HAVE_SYS_STAT_H
1035 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1041 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1043 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1047 /* remove traling / or \ */
1048 for( p = &psz_dir[strlen( psz_dir) - 1]; p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1057 /* parse psz_src dir */
1058 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1062 if( !strcmp( p_dir_content->d_name, "." ) )
1067 #if defined( WIN32 )
1068 sprintf( tmp, "%s\\%s", psz_dir, p_dir_content->d_name );
1070 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1073 #ifdef HAVE_SYS_STAT_H
1074 if( stat( tmp, &stat_info ) == -1 )
1079 f = mvar_New( name, "set" );
1080 mvar_AppendNewVar( f, "name", tmp );
1081 #ifdef HAVE_SYS_STAT_H
1082 if( S_ISDIR( stat_info.st_mode ) )
1084 mvar_AppendNewVar( f, "type", "directory" );
1086 else if( S_ISREG( stat_info.st_mode ) )
1088 mvar_AppendNewVar( f, "type", "file" );
1092 mvar_AppendNewVar( f, "type", "unknown" );
1095 sprintf( tmp, "%lld", stat_info.st_size );
1096 mvar_AppendNewVar( f, "size", tmp );
1098 /* FIXME memory leak FIXME */
1100 ctime_r( &stat_info.st_mtime, tmp );
1101 mvar_AppendNewVar( f, "date", tmp );
1103 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1107 mvar_AppendNewVar( f, "type", "unknown" );
1108 mvar_AppendNewVar( f, "size", "unknown" );
1109 mvar_AppendNewVar( f, "date", "unknown" );
1111 mvar_AppendVar( s, f );
1117 static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
1119 mvar_t *s = mvar_New( name, "set" );
1123 /* fprintf( stderr," mvar_VlmSetNew: name=`%s'\n", name ); */
1126 if( vlm_ExecuteCommand( vlm, "show", &msg ) )
1131 for( i = 0; i < msg->i_child; i++ )
1133 /* Over media, schedule */
1134 vlm_message_t *ch = msg->child[i];
1137 for( j = 0; j < ch->i_child; j++ )
1140 vlm_message_t *el = ch->child[j];
1141 vlm_message_t *inf, *desc;
1146 sprintf( psz, "show %s", el->psz_name );
1147 if( vlm_ExecuteCommand( vlm, psz, &inf ) )
1149 desc = inf->child[0];
1151 /* Add a node with name and info */
1152 set = mvar_New( name, "set" );
1153 mvar_AppendNewVar( set, "name", el->psz_name );
1155 /* fprintf( stderr, "#### name=%s\n", el->psz_name ); */
1157 for( k = 0; k < desc->i_child; k++ )
1159 vlm_message_t *ch = desc->child[k];
1160 if( ch->i_child > 0 )
1163 mvar_t *n = mvar_New( ch->psz_name, "set" );
1165 /* fprintf( stderr, " child=%s [%d]\n", ch->psz_name, ch->i_child ); */
1166 for( c = 0; c < ch->i_child; c++ )
1168 mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
1169 mvar_AppendVar( n, in );
1171 /* fprintf( stderr, " sub=%s\n", ch->child[c]->psz_name );*/
1173 mvar_AppendVar( set, n );
1177 /* fprintf( stderr, " child=%s->%s\n", ch->psz_name, ch->psz_value ); */
1178 mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
1181 vlm_MessageDelete( inf );
1183 mvar_AppendVar( s, set );
1186 vlm_MessageDelete( msg );
1192 static void SSInit( rpn_stack_t * );
1193 static void SSClean( rpn_stack_t * );
1194 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1196 static void SSPush ( rpn_stack_t *, char * );
1197 static char *SSPop ( rpn_stack_t * );
1199 static void SSPushN ( rpn_stack_t *, int );
1200 static int SSPopN ( rpn_stack_t *, mvar_t * );
1203 /****************************************************************************
1205 ****************************************************************************/
1213 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1217 /* just load the file */
1219 *pp_data = malloc( 1025 ); /* +1 for \0 */
1220 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1223 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1229 (*pp_data)[*pi_data] = '\0';
1234 static int MacroParse( macro_t *m, uint8_t *psz_src )
1236 uint8_t *dup = strdup( psz_src );
1241 #define EXTRACT( name, l ) \
1243 p = strchr( src, '"' ); \
1248 m->name = strdup( src ); \
1269 if( !strncmp( src, "id=\"", 4 ) )
1273 else if( !strncmp( src, "param1=\"", 8 ) )
1275 EXTRACT( param1, 8 );
1277 else if( !strncmp( src, "param2=\"", 8 ) )
1279 EXTRACT( param2, 8 );
1286 if( strstr( src, "/>" ) )
1288 src = strstr( src, "/>" ) + 2;
1292 src += strlen( src );
1297 m->id = strdup( "" );
1299 if( m->param1 == NULL )
1301 m->param1 = strdup( "" );
1303 if( m->param2 == NULL )
1305 m->param2 = strdup( "" );
1314 static void MacroClean( macro_t *m )
1372 StrToMacroTypeTab [] =
1374 { "control", MVLC_CONTROL },
1375 /* player control */
1376 { "play", MVLC_PLAY },
1377 { "stop", MVLC_STOP },
1378 { "pause", MVLC_PAUSE },
1379 { "next", MVLC_NEXT },
1380 { "previous", MVLC_PREVIOUS },
1381 { "seek", MVLC_SEEK },
1382 { "keep", MVLC_KEEP },
1383 { "fullscreen", MVLC_FULLSCREEN },
1384 { "volume", MVLC_VOLUME },
1386 /* playlist management */
1387 { "add", MVLC_ADD },
1388 { "delete", MVLC_DEL },
1389 { "empty", MVLC_EMPTY },
1390 { "sort", MVLC_SORT },
1391 { "move", MVLC_MOVE },
1394 { "close", MVLC_CLOSE },
1395 { "shutdown", MVLC_SHUTDOWN },
1398 { "vlm_new", MVLC_VLM_NEW },
1399 { "vlm_setup", MVLC_VLM_SETUP },
1400 { "vlm_del", MVLC_VLM_DEL },
1401 { "vlm_play", MVLC_VLM_PLAY },
1402 { "vlm_pause", MVLC_VLM_PAUSE },
1403 { "vlm_stop", MVLC_VLM_STOP },
1404 { "vlm_seek", MVLC_VLM_SEEK },
1405 { "vlm_load", MVLC_VLM_LOAD },
1406 { "vlm_save", MVLC_VLM_SAVE },
1408 { "rpn", MVLC_RPN },
1410 { "foreach", MVLC_FOREACH },
1411 { "value", MVLC_VALUE },
1414 { "else", MVLC_ELSE },
1415 { "end", MVLC_END },
1416 { "get", MVLC_GET },
1417 { "set", MVLC_SET },
1418 { "int", MVLC_INT },
1419 { "float", MVLC_FLOAT },
1420 { "string", MVLC_STRING },
1423 { NULL, MVLC_UNKNOWN }
1426 static int StrToMacroType( char *name )
1430 if( !name || *name == '\0')
1432 return MVLC_UNKNOWN;
1434 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1436 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1438 return StrToMacroTypeTab[i].i_type;
1441 return MVLC_UNKNOWN;
1444 static void MacroDo( httpd_file_sys_t *p_args,
1446 uint8_t *p_request, int i_request,
1447 uint8_t **pp_data, int *pi_data,
1450 intf_thread_t *p_intf = p_args->p_intf;
1451 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1454 #define ALLOC( l ) \
1456 int __i__ = *pp_dst - *pp_data; \
1458 *pp_data = realloc( *pp_data, *pi_data ); \
1459 *pp_dst = (*pp_data) + __i__; \
1461 #define PRINT( str ) \
1462 ALLOC( strlen( str ) + 1 ); \
1463 *pp_dst += sprintf( *pp_dst, str );
1465 #define PRINTS( str, s ) \
1466 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1468 char * psz_cur = *pp_dst; \
1469 *pp_dst += sprintf( *pp_dst, str, s ); \
1470 while( psz_cur && *psz_cur ) \
1472 /* Prevent script injection */ \
1473 if( *psz_cur == '<' ) *psz_cur = '*'; \
1474 if( *psz_cur == '>' ) *psz_cur = '*'; \
1479 switch( StrToMacroType( m->id ) )
1482 if( i_request <= 0 )
1486 uri_extract_value( p_request, "control", control, 512 );
1487 if( *m->param1 && !strstr( m->param1, control ) )
1489 msg_Warn( p_intf, "unauthorized control=%s", control );
1492 switch( StrToMacroType( control ) )
1499 uri_extract_value( p_request, "item", item, 512 );
1500 i_item = atoi( item );
1501 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
1502 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1506 playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1507 msg_Dbg( p_intf, "requested playlist stop" );
1510 playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1511 msg_Dbg( p_intf, "requested playlist pause" );
1514 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1515 p_sys->p_playlist->i_index + 1 );
1516 msg_Dbg( p_intf, "requested playlist next" );
1519 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1520 p_sys->p_playlist->i_index - 1 );
1521 msg_Dbg( p_intf, "requested playlist next" );
1523 case MVLC_FULLSCREEN:
1525 if( p_sys->p_input )
1527 vout_thread_t *p_vout;
1528 p_vout = vlc_object_find( p_sys->p_input,
1529 VLC_OBJECT_VOUT, FIND_CHILD );
1533 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1534 vlc_object_release( p_vout );
1535 msg_Dbg( p_intf, "requested fullscreen toggle" );
1549 #define POSITION_ABSOLUTE 12
1550 #define POSITION_REL_FOR 13
1551 #define POSITION_REL_BACK 11
1552 #define TIME_ABSOLUTE 0
1553 #define TIME_REL_FOR 1
1554 #define TIME_REL_BACK -1
1555 if( p_sys->p_input )
1557 uri_extract_value( p_request, "seek_value", value, 20 );
1558 uri_decode_url_encoded( value );
1560 var_Get( p_sys->p_input, "length", &val);
1561 i_length = val.i_time;
1563 while( p_value[0] != '\0' )
1569 i_relative = TIME_REL_FOR;
1575 i_relative = TIME_REL_BACK;
1579 case '0': case '1': case '2': case '3': case '4':
1580 case '5': case '6': case '7': case '8': case '9':
1582 i_stock = strtol( p_value , &p_value , 10 );
1585 case '%': /* for percentage ie position */
1587 i_relative += POSITION_ABSOLUTE;
1595 i_value = 60 * (i_value + i_stock) ;
1600 case 'h': case 'H': /* hours */
1602 i_value += 3600 * i_stock;
1604 /* other characters which are not numbers are not important */
1605 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1611 case 'm': case 'M': case '\'': /* minutes */
1613 i_value += 60 * i_stock;
1616 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1622 case 's': case 'S': case '"': /* seconds */
1626 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1640 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1647 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1648 val.i_time = (uint64_t)( i_value ) * 1000000;
1650 val.i_time = i_length;
1652 var_Set( p_sys->p_input, "time", val );
1653 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1658 var_Get( p_sys->p_input, "time", &val );
1659 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1661 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1664 val.i_time = i_length;
1666 var_Set( p_sys->p_input, "time", val );
1667 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1672 var_Get( p_sys->p_input, "time", &val );
1673 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1678 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1680 var_Set( p_sys->p_input, "time", val );
1681 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1684 case POSITION_ABSOLUTE:
1686 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1687 var_Set( p_sys->p_input, "position", val );
1688 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1691 case POSITION_REL_FOR:
1693 var_Get( p_sys->p_input, "position", &val );
1694 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1695 var_Set( p_sys->p_input, "position", val );
1696 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1699 case POSITION_REL_BACK:
1701 var_Get( p_sys->p_input, "position", &val );
1702 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1703 var_Set( p_sys->p_input, "position", val );
1704 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
1709 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1714 #undef POSITION_ABSOLUTE
1715 #undef POSITION_REL_FOR
1716 #undef POSITION_REL_BACK
1717 #undef TIME_ABSOLUTE
1719 #undef TIME_REL_BACK
1725 audio_volume_t i_volume;
1728 uri_extract_value( p_request, "value", vol, 8 );
1729 aout_VolumeGet( p_intf, &i_volume );
1730 uri_decode_url_encoded( vol );
1734 i_value = atoi( vol + 1 );
1735 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1737 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1738 msg_Dbg( p_intf, "requested volume set: max" );
1741 aout_VolumeSet( p_intf , (i_volume + i_value) );
1742 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1747 i_value = atoi( vol + 1 );
1748 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1750 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1751 msg_Dbg( p_intf, "requested volume set: min" );
1754 aout_VolumeSet( p_intf , (i_volume - i_value) );
1755 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1759 i_value = atoi( vol );
1760 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1762 aout_VolumeSet( p_intf , atoi( vol ) );
1763 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1769 /* playlist management */
1773 playlist_item_t * p_item;
1775 uri_extract_value( p_request, "mrl", mrl, 512 );
1776 uri_decode_url_encoded( mrl );
1777 p_item = parse_MRL( p_intf, mrl );
1779 if( !p_item || !p_item->input.psz_uri ||
1780 !*p_item->input.psz_uri )
1782 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1785 playlist_AddItem( p_sys->p_playlist , p_item ,
1786 PLAYLIST_APPEND, PLAYLIST_END );
1787 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1794 int i_item, *p_items = NULL, i_nb_items = 0;
1795 char item[512], *p_parser = p_request;
1797 /* Get the list of items to delete */
1799 uri_extract_value( p_parser, "item", item, 512 )) )
1801 if( !*item ) continue;
1803 i_item = atoi( item );
1804 p_items = realloc( p_items, (i_nb_items + 1) *
1806 p_items[i_nb_items] = i_item;
1810 /* The items need to be deleted from in reversed order */
1814 for( i = 0; i < i_nb_items; i++ )
1817 for( j = 0; j < i_nb_items; j++ )
1819 if( p_items[j] > p_items[i_index] )
1823 playlist_Delete( p_sys->p_playlist,
1825 msg_Dbg( p_intf, "requested playlist delete: %d",
1827 p_items[i_index] = -1;
1831 if( p_items ) free( p_items );
1836 int i_item, *p_items = NULL, i_nb_items = 0;
1837 char item[512], *p_parser = p_request;
1840 /* Get the list of items to keep */
1842 uri_extract_value( p_parser, "item", item, 512 )) )
1844 if( !*item ) continue;
1846 i_item = atoi( item );
1847 p_items = realloc( p_items, (i_nb_items + 1) *
1849 p_items[i_nb_items] = i_item;
1853 /* The items need to be deleted from in reversed order */
1854 for( i = p_sys->p_playlist->i_size - 1; i >= 0 ; i-- )
1856 /* Check if the item is in the keep list */
1857 for( j = 0 ; j < i_nb_items ; j++ )
1859 if( p_items[j] == i ) break;
1861 if( j == i_nb_items )
1863 playlist_Delete( p_sys->p_playlist, i );
1864 msg_Dbg( p_intf, "requested playlist delete: %d",
1869 if( p_items ) free( p_items );
1874 while( p_sys->p_playlist->i_size > 0 )
1876 playlist_Delete( p_sys->p_playlist, 0 );
1878 msg_Dbg( p_intf, "requested playlist empty" );
1887 uri_extract_value( p_request, "type", type, 12 );
1888 uri_extract_value( p_request, "order", order, 2 );
1890 if( order[0] == '0' ) i_order = ORDER_NORMAL;
1891 else i_order = ORDER_REVERSE;
1893 if( !strcmp( type , "title" ) )
1895 playlist_SortTitle( p_sys->p_playlist , i_order );
1896 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
1897 } else if( !strcmp( type , "group" ) )
1899 playlist_SortGroup( p_sys->p_playlist , i_order );
1900 msg_Dbg( p_intf, "requested playlist sort by group (%d)" , i_order );
1901 } else if( !strcmp( type , "author" ) )
1903 playlist_SortAuthor( p_sys->p_playlist , i_order );
1904 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
1905 } else if( !strcmp( type , "shuffle" ) )
1907 playlist_Sort( p_sys->p_playlist , SORT_RANDOM, ORDER_NORMAL );
1908 msg_Dbg( p_intf, "requested playlist shuffle");
1919 uri_extract_value( p_request, "psz_pos", psz_pos, 6 );
1920 uri_extract_value( p_request, "psz_newpos", psz_newpos, 6 );
1921 i_pos = atoi( psz_pos );
1922 i_newpos = atoi( psz_newpos );
1923 if ( i_pos < i_newpos )
1925 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
1927 playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
1929 msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
1933 /* admin function */
1937 uri_extract_value( p_request, "id", id, 512 );
1938 msg_Dbg( p_intf, "requested close id=%s", id );
1940 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
1942 msg_Warn( p_intf, "close failed for id=%s", id );
1949 msg_Dbg( p_intf, "requested shutdown" );
1950 p_intf->p_vlc->b_die = VLC_TRUE;
1955 case MVLC_VLM_SETUP:
1957 static const char *vlm_properties[11] =
1960 "enabled", "disabled", "loop", "unloop",
1962 "input", "output", "option", "date", "period", "repeat", "append",
1964 vlm_message_t *vlm_answer;
1966 char *psz = malloc( strlen( p_request ) + 1000 );
1971 if( p_intf->p_sys->p_vlm == NULL )
1972 p_intf->p_sys->p_vlm = vlm_New( p_intf );
1974 uri_extract_value( p_request, "name", name, 512 );
1975 if( StrToMacroType( control ) == MVLC_VLM_NEW )
1978 uri_extract_value( p_request, "type", type, 20 );
1979 p += sprintf( psz, "new %s %s", name, type );
1983 p += sprintf( psz, "setup %s", name );
1985 /* Parse the request */
1986 for( i = 0; i < 11; i++ )
1989 uri_extract_value( p_request, vlm_properties[i], val, 512 );
1990 uri_decode_url_encoded( val );
1991 if( strlen( val ) > 0 && i >= 4 )
1993 p += sprintf( p, " %s %s", vlm_properties[i], val );
1995 else if( uri_test_param( p_request, vlm_properties[i] ) && i < 4 )
1997 p += sprintf( p, " %s", vlm_properties[i] );
2000 fprintf( stderr, "vlm_ExecuteCommand: %s\n", psz );
2001 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2002 if( vlm_answer->psz_value == NULL ) /* there is no error */
2004 vlm_error = strdup( "" );
2008 vlm_error = malloc( strlen(vlm_answer->psz_name) +
2009 strlen(vlm_answer->psz_value) +
2010 strlen( " : ") + 1 );
2011 sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
2012 vlm_answer->psz_value );
2015 mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
2017 vlm_MessageDelete( vlm_answer );
2025 vlm_message_t *vlm_answer;
2028 if( p_intf->p_sys->p_vlm == NULL )
2029 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2031 uri_extract_value( p_request, "name", name, 512 );
2032 sprintf( psz, "del %s", name );
2034 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2035 /* FIXME do a vlm_answer -> var stack conversion */
2036 vlm_MessageDelete( vlm_answer );
2041 case MVLC_VLM_PAUSE:
2045 vlm_message_t *vlm_answer;
2048 if( p_intf->p_sys->p_vlm == NULL )
2049 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2051 uri_extract_value( p_request, "name", name, 512 );
2052 if( StrToMacroType( control ) == MVLC_VLM_PLAY )
2053 sprintf( psz, "control %s play", name );
2054 else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
2055 sprintf( psz, "control %s pause", name );
2056 else if( StrToMacroType( control ) == MVLC_VLM_STOP )
2057 sprintf( psz, "control %s stop", name );
2058 else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
2061 uri_extract_value( p_request, "percent", percent, 512 );
2062 sprintf( psz, "control %s seek %s", name, percent );
2065 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2066 /* FIXME do a vlm_answer -> var stack conversion */
2067 vlm_MessageDelete( vlm_answer );
2073 vlm_message_t *vlm_answer;
2077 if( p_intf->p_sys->p_vlm == NULL )
2078 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2080 uri_extract_value( p_request, "file", file, 512 );
2081 uri_decode_url_encoded( file );
2083 if( StrToMacroType( control ) == MVLC_VLM_LOAD )
2084 sprintf( psz, "load %s", file );
2086 sprintf( psz, "save %s", file );
2088 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2089 /* FIXME do a vlm_answer -> var stack conversion */
2090 vlm_MessageDelete( vlm_answer );
2097 PRINTS( "<!-- control param(%s) unsuported -->", control );
2109 if( i_request <= 0 ||
2110 *m->param1 == '\0' ||
2111 strstr( p_request, m->param1 ) == NULL )
2115 uri_extract_value( p_request, m->param1, value, 512 );
2116 uri_decode_url_encoded( value );
2118 switch( StrToMacroType( m->param2 ) )
2122 config_PutInt( p_intf, m->param1, i );
2126 config_PutFloat( p_intf, m->param1, f );
2129 config_PutPsz( p_intf, m->param1, value );
2132 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
2143 if( *m->param1 == '\0' )
2148 switch( StrToMacroType( m->param2 ) )
2151 i = config_GetInt( p_intf, m->param1 );
2152 sprintf( value, "%i", i );
2155 f = config_GetFloat( p_intf, m->param1 );
2156 sprintf( value, "%f", f );
2159 psz = config_GetPsz( p_intf, m->param1 );
2160 sprintf( value, "%s", psz ? psz : "" );
2161 if( psz ) free( psz );
2164 sprintf( value, "invalid type(%s) in set", m->param2 );
2167 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
2168 PRINTS( "%s", value );
2177 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2178 s = SSPop( &p_args->stack );
2179 v = mvar_GetValue( p_args->vars, s );
2183 v = s = SSPop( &p_args->stack );
2191 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2196 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2197 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2205 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
2212 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
2217 i_skip = MacroParse( &m, src );
2219 i_id = StrToMacroType( m.id );
2236 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2237 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2239 return src + ( b_after ? i_skip : 0 );
2241 else if( i_level < 0 )
2257 static void Execute( httpd_file_sys_t *p_args,
2258 uint8_t *p_request, int i_request,
2259 uint8_t **pp_data, int *pi_data,
2261 uint8_t *_src, uint8_t *_end )
2263 intf_thread_t *p_intf = p_args->p_intf;
2265 uint8_t *src, *dup, *end;
2266 uint8_t *dst = *pp_dst;
2268 src = dup = malloc( _end - _src + 1 );
2269 end = src +( _end - _src );
2271 memcpy( src, _src, _end - _src );
2274 /* we parse searching <vlc */
2280 p = strstr( src, "<vlc" );
2281 if( p < end && p == src )
2285 src += MacroParse( &m, src );
2287 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2289 switch( StrToMacroType( m.id ) )
2296 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2297 if( SSPopN( &p_args->stack, p_args->vars ) )
2305 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2309 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2313 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2316 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2320 else if( i_test == 1 )
2323 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2325 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2329 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2338 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2339 uint8_t *start = src;
2340 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2347 if( !strcmp( m.param2, "integer" ) )
2349 char *arg = SSPop( &p_args->stack );
2350 index = mvar_IntegerSetNew( m.param1, arg );
2353 else if( !strcmp( m.param2, "directory" ) )
2355 char *arg = SSPop( &p_args->stack );
2356 index = mvar_FileSetNew( m.param1, arg );
2359 else if( !strcmp( m.param2, "playlist" ) )
2361 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2363 else if( !strcmp( m.param2, "informations" ) )
2365 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2367 else if( !strcmp( m.param2, "vlm" ) )
2369 if( p_intf->p_sys->p_vlm == NULL )
2371 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2373 index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
2376 else if( !strcmp( m.param2, "hosts" ) )
2378 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2380 else if( !strcmp( m.param2, "urls" ) )
2382 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2384 else if( !strcmp( m.param2, "connections" ) )
2386 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2389 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2391 index = mvar_Duplicate( v );
2395 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2400 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2402 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2404 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2407 f->name = strdup( m.param1 );
2410 mvar_PushVar( p_args->vars, f );
2411 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2412 mvar_RemoveVar( p_args->vars, f );
2416 mvar_Delete( index );
2423 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2431 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2434 int i_index = dst - *pp_data;
2437 *pp_data = realloc( *pp_data, *pi_data );
2438 dst = (*pp_data) + i_index;
2440 memcpy( dst, src, i_copy );
2450 /****************************************************************************
2452 ****************************************************************************
2453 * a file with b_html is parsed and all "macro" replaced
2454 * <vlc id="macro name" [param1="" [param2=""]] />
2457 ****************************************************************************/
2458 static int HttpCallback( httpd_file_sys_t *p_args,
2459 httpd_file_t *p_file,
2461 uint8_t **pp_data, int *pi_data )
2463 int i_request = p_request ? strlen( p_request ) : 0;
2467 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2469 p = *pp_data = malloc( 10240 );
2472 return VLC_EGENERIC;
2474 p += sprintf( p, "<html>\n" );
2475 p += sprintf( p, "<head>\n" );
2476 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2477 p += sprintf( p, "</head>\n" );
2478 p += sprintf( p, "<body>\n" );
2479 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2480 p += sprintf( p, "<hr />\n" );
2481 p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
2482 p += sprintf( p, "</body>\n" );
2483 p += sprintf( p, "</html>\n" );
2485 *pi_data = strlen( *pp_data );
2490 if( !p_args->b_html )
2492 FileLoad( f, pp_data, pi_data );
2500 char position[4]; /* percentage */
2501 char time[12]; /* in seconds */
2502 char length[12]; /* in seconds */
2503 audio_volume_t i_volume;
2507 #define p_sys p_args->p_intf->p_sys
2508 if( p_sys->p_input )
2510 var_Get( p_sys->p_input, "position", &val);
2511 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2512 var_Get( p_sys->p_input, "time", &val);
2513 sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2514 var_Get( p_sys->p_input, "length", &val);
2515 sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2517 var_Get( p_sys->p_input, "state", &val );
2518 if( val.i_int == PLAYING_S )
2520 sprintf( state, "playing" );
2521 } else if( val.i_int == PAUSE_S )
2523 sprintf( state, "paused" );
2526 sprintf( state, "stop" );
2530 sprintf( position, "%d", 0 );
2531 sprintf( time, "%d", 0 );
2532 sprintf( length, "%d", 0 );
2533 sprintf( state, "stop" );
2537 aout_VolumeGet( p_args->p_intf , &i_volume );
2538 sprintf( volume , "%d" , (int)i_volume );
2540 p_args->vars = mvar_New( "variables", "" );
2541 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2542 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2543 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
2544 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2545 mvar_AppendNewVar( p_args->vars, "stream_position", position );
2546 mvar_AppendNewVar( p_args->vars, "stream_time", time );
2547 mvar_AppendNewVar( p_args->vars, "stream_length", length );
2548 mvar_AppendNewVar( p_args->vars, "volume", volume );
2549 mvar_AppendNewVar( p_args->vars, "stream_state", state );
2551 SSInit( &p_args->stack );
2553 /* first we load in a temporary buffer */
2554 FileLoad( f, &p_buffer, &i_buffer );
2556 /* allocate output */
2557 *pi_data = i_buffer + 1000;
2558 dst = *pp_data = malloc( *pi_data );
2560 /* we parse executing all <vlc /> macros */
2561 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2564 *pi_data = dst - *pp_data;
2566 SSClean( &p_args->stack );
2567 mvar_Delete( p_args->vars );
2576 /****************************************************************************
2578 ****************************************************************************/
2579 static int uri_test_param( char *psz_uri, const char *psz_name )
2583 while( (p = strstr( p, psz_name )) )
2585 /* Verify that we are dealing with a post/get argument */
2586 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2595 static char *uri_extract_value( char *psz_uri, const char *psz_name,
2596 char *psz_value, int i_value_max )
2600 while( (p = strstr( p, psz_name )) )
2602 /* Verify that we are dealing with a post/get argument */
2603 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2612 p += strlen( psz_name );
2613 if( *p == '=' ) p++;
2615 if( strchr( p, '&' ) )
2617 i_len = strchr( p, '&' ) - p;
2621 /* for POST method */
2622 if( strchr( p, '\n' ) )
2624 i_len = strchr( p, '\n' ) - p;
2625 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2629 i_len = strlen( p );
2632 i_len = __MIN( i_value_max - 1, i_len );
2635 strncpy( psz_value, p, i_len );
2636 psz_value[i_len] = '\0';
2640 strncpy( psz_value, "", i_value_max );
2646 strncpy( psz_value, "", i_value_max );
2652 static void uri_decode_url_encoded( char *psz )
2654 char *dup = strdup( psz );
2672 *psz++ = strtol( val, NULL, 16 );
2674 else if( *p == '+' )
2688 /****************************************************************************
2689 * Light RPN evaluator
2690 ****************************************************************************/
2691 static void SSInit( rpn_stack_t *st )
2696 static void SSClean( rpn_stack_t *st )
2698 while( st->i_stack > 0 )
2700 free( st->stack[--st->i_stack] );
2704 static void SSPush( rpn_stack_t *st, char *s )
2706 if( st->i_stack < STACK_MAX )
2708 st->stack[st->i_stack++] = strdup( s );
2712 static char * SSPop( rpn_stack_t *st )
2714 if( st->i_stack <= 0 )
2716 return strdup( "" );
2720 return st->stack[--st->i_stack];
2724 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2733 i = strtol( name, &end, 0 );
2736 value = mvar_GetValue( vars, name );
2744 static void SSPushN( rpn_stack_t *st, int i )
2748 sprintf( v, "%d", i );
2752 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2759 while( *exp == ' ' )
2766 /* extract string */
2769 while( *exp && *exp != '\'' )
2780 p = strchr( exp, ' ' );
2785 exp += strlen( exp );
2790 strncpy( s, exp, i );
2801 /* 1. Integer function */
2802 if( !strcmp( s, "!" ) )
2804 SSPushN( st, ~SSPopN( st, vars ) );
2806 else if( !strcmp( s, "^" ) )
2808 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2810 else if( !strcmp( s, "&" ) )
2812 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2814 else if( !strcmp( s, "|" ) )
2816 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2818 else if( !strcmp( s, "+" ) )
2820 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2822 else if( !strcmp( s, "-" ) )
2824 int j = SSPopN( st, vars );
2825 int i = SSPopN( st, vars );
2826 SSPushN( st, i - j );
2828 else if( !strcmp( s, "*" ) )
2830 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2832 else if( !strcmp( s, "/" ) )
2836 j = SSPopN( st, vars );
2837 i = SSPopN( st, vars );
2839 SSPushN( st, j != 0 ? i / j : 0 );
2841 else if( !strcmp( s, "%" ) )
2845 j = SSPopN( st, vars );
2846 i = SSPopN( st, vars );
2848 SSPushN( st, j != 0 ? i % j : 0 );
2850 /* 2. integer tests */
2851 else if( !strcmp( s, "=" ) )
2853 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2855 else if( !strcmp( s, "<" ) )
2857 int j = SSPopN( st, vars );
2858 int i = SSPopN( st, vars );
2860 SSPushN( st, i < j ? -1 : 0 );
2862 else if( !strcmp( s, ">" ) )
2864 int j = SSPopN( st, vars );
2865 int i = SSPopN( st, vars );
2867 SSPushN( st, i > j ? -1 : 0 );
2869 else if( !strcmp( s, "<=" ) )
2871 int j = SSPopN( st, vars );
2872 int i = SSPopN( st, vars );
2874 SSPushN( st, i <= j ? -1 : 0 );
2876 else if( !strcmp( s, ">=" ) )
2878 int j = SSPopN( st, vars );
2879 int i = SSPopN( st, vars );
2881 SSPushN( st, i >= j ? -1 : 0 );
2883 /* 3. string functions */
2884 else if( !strcmp( s, "strcat" ) )
2886 char *s2 = SSPop( st );
2887 char *s1 = SSPop( st );
2888 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2898 else if( !strcmp( s, "strcmp" ) )
2900 char *s2 = SSPop( st );
2901 char *s1 = SSPop( st );
2903 SSPushN( st, strcmp( s1, s2 ) );
2907 else if( !strcmp( s, "strncmp" ) )
2909 int n = SSPopN( st, vars );
2910 char *s2 = SSPop( st );
2911 char *s1 = SSPop( st );
2913 SSPushN( st, strncmp( s1, s2 , n ) );
2917 else if( !strcmp( s, "strsub" ) )
2919 int n = SSPopN( st, vars );
2920 int m = SSPopN( st, vars );
2922 char *s = SSPop( st );
2934 str = malloc( i_len + 1 );
2936 memcpy( str, s + m - 1, i_len );
2937 str[ i_len ] = '\0';
2943 else if( !strcmp( s, "strlen" ) )
2945 char *str = SSPop( st );
2947 SSPushN( st, strlen( str ) );
2950 /* 4. stack functions */
2951 else if( !strcmp( s, "dup" ) )
2953 char *str = SSPop( st );
2958 else if( !strcmp( s, "drop" ) )
2960 char *str = SSPop( st );
2963 else if( !strcmp( s, "swap" ) )
2965 char *s1 = SSPop( st );
2966 char *s2 = SSPop( st );
2973 else if( !strcmp( s, "flush" ) )
2978 else if( !strcmp( s, "store" ) )
2980 char *value = SSPop( st );
2981 char *name = SSPop( st );
2983 mvar_PushNewVar( vars, name, value );
2987 else if( !strcmp( s, "value" ) )
2989 char *name = SSPop( st );
2990 char *value = mvar_GetValue( vars, name );
2992 SSPush( st, value );
2996 else if( !strcmp( s, "url_extract" ) )
2998 char *url = mvar_GetValue( vars, "url_value" );
2999 char *name = SSPop( st );
3002 uri_extract_value( url, name, value, 512 );
3003 uri_decode_url_encoded( value );
3004 SSPush( st, value );
3013 /**********************************************************************
3014 * Find_end_MRL: Find the end of the sentence :
3015 * this function parses the string psz and find the end of the item
3016 * and/or option with detecting the " and ' problems.
3017 * returns NULL if an error is detected, otherwise, returns a pointer
3018 * of the end of the sentence (after the last character)
3019 **********************************************************************/
3020 static char *Find_end_MRL( char *psz )
3030 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
3032 if( *s_sent == '\'' )
3034 s_sent = Find_end_MRL( s_sent );
3036 if( s_sent == NULL )
3046 if( *s_sent == '\"' )
3050 } else /* *s_sent == '\0' , which means the number of " is incorrect */
3060 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
3062 if( *s_sent == '\"' )
3064 s_sent = Find_end_MRL( s_sent );
3066 if( s_sent == NULL )
3076 if( *s_sent == '\'' )
3080 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
3086 default: /* now we can look for spaces */
3088 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
3090 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
3092 s_sent = Find_end_MRL( s_sent );
3103 /**********************************************************************
3104 * parse_MRL: parse the MRL, find the mrl string and the options,
3105 * create an item with all informations in it, and return the item.
3106 * return NULL if there is an error.
3107 **********************************************************************/
3108 playlist_item_t * parse_MRL( intf_thread_t *p_intf, char *psz )
3110 char **ppsz_options = NULL;
3117 playlist_item_t * p_item = NULL;
3119 /* In case there is spaces before the mrl */
3120 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
3125 /* extract the mrl */
3126 s_temp = strstr( s_mrl , " :" );
3127 if( s_temp == NULL )
3129 s_temp = s_mrl + strlen( s_mrl );
3132 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
3139 /* if the mrl is between " or ', we must remove them */
3140 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
3142 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
3143 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
3144 mrl[ s_temp - s_mrl - 2 ] = '\0';
3147 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
3148 strncpy( mrl , s_mrl , s_temp - s_mrl );
3149 mrl[ s_temp - s_mrl ] = '\0';
3154 /* now we can take care of the options */
3155 while( (*s_mrl != '\0') && (i_error == 0) )
3164 case ':': /* an option */
3166 s_temp = Find_end_MRL( s_mrl );
3168 if( s_temp == NULL )
3175 ppsz_options = realloc( ppsz_options , i_options *
3177 ppsz_options[ i_options - 1 ] =
3178 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
3180 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
3183 /* don't forget to finish the string with a '\0' */
3184 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
3204 /* now create an item */
3205 p_item = playlist_ItemNew( p_intf, mrl, mrl);
3206 for( i = 0 ; i< i_options ; i++ )
3208 playlist_ItemAddOption( p_item, ppsz_options[i] );
3212 for( i = 0 ; i < i_options ; i++ )
3214 free( ppsz_options[i] );
3216 free( ppsz_options );