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, char *psz_name,
121 char *psz_value, int i_value_max );
122 static int uri_test_param( char *psz_uri, 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]->psz_name );
853 mvar_AppendNewVar( itm, "uri", p_pl->pp_items[i]->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" );
869 input_info_category_t * p_category;
870 input_info_t * p_info;
872 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
873 if( p_input == NULL )
878 vlc_mutex_lock( &p_input->stream.stream_lock );
879 p_category = p_input->stream.p_info;
882 mvar_t *cat = mvar_New( name, "set" );
883 mvar_t *iset = mvar_New( "info", "set" );
885 mvar_AppendNewVar( cat, "name", p_category->psz_name );
886 mvar_AppendVar( cat, iset );
888 p_info = p_category->p_info;
891 mvar_t *info = mvar_New( "info", "" );
893 msg_Dbg( p_input, "adding info name=%s value=%s", p_info->psz_name, p_info->psz_value );
894 mvar_AppendNewVar( info, "name", p_info->psz_name );
895 mvar_AppendNewVar( info, "value", p_info->psz_value );
896 mvar_AppendVar( iset, info );
897 p_info = p_info->p_next;
899 mvar_AppendVar( s, cat );
900 p_category = p_category->p_next;
902 vlc_mutex_unlock( &p_input->stream.stream_lock );
907 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
909 mvar_t *s = mvar_New( name, "set" );
913 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
914 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
916 for( i= 0; i < info.i_count; )
920 inf = mvar_New( name, "set" );
923 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
924 info.info[i].psz_name, info.info[i].psz_value ); */
925 mvar_AppendNewVar( inf,
926 info.info[i].psz_name,
927 info.info[i].psz_value );
929 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
930 mvar_AppendVar( s, inf );
935 for( i = 0; i < info.i_count; i++ )
937 free( info.info[i].psz_name );
938 free( info.info[i].psz_value );
940 if( info.i_count > 0 )
949 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
951 mvar_t *s = mvar_New( name, "set" );
952 char tmp[MAX_DIR_SIZE], *p, *src;
953 #ifdef HAVE_SYS_STAT_H
954 struct stat stat_info;
957 struct dirent *p_dir_content;
960 /* convert all / to native separator */
962 while( (p = strchr( psz_dir, '/' )) )
971 /* remove trailling separator */
972 while( strlen( psz_dir ) > 1 &&
974 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
976 psz_dir[strlen( psz_dir ) -1 ] == sep )
978 psz_dir[strlen( psz_dir ) -1 ] ='\0';
980 /* remove double separator */
981 for( p = src = psz_dir; *src != '\0'; src++, p++ )
983 if( src[0] == sep && src[1] == sep )
991 if( *psz_dir == '\0' )
995 /* first fix all .. dir */
999 if( src[0] == '.' && src[1] == '.' )
1002 if( p <= &psz_dir[1] )
1009 while( p > &psz_dir[1] && *p != sep )
1014 else if( *src == sep )
1016 if( p > psz_dir && p[-1] == sep )
1030 } while( *src && *src != sep );
1035 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1037 #ifdef HAVE_SYS_STAT_H
1038 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1044 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1046 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1050 /* remove traling / or \ */
1051 for( p = &psz_dir[strlen( psz_dir) - 1]; p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1060 /* parse psz_src dir */
1061 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1065 if( !strcmp( p_dir_content->d_name, "." ) )
1070 #if defined( WIN32 )
1071 sprintf( tmp, "%s\\%s", psz_dir, p_dir_content->d_name );
1073 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1076 #ifdef HAVE_SYS_STAT_H
1077 if( stat( tmp, &stat_info ) == -1 )
1082 f = mvar_New( name, "set" );
1083 mvar_AppendNewVar( f, "name", tmp );
1084 #ifdef HAVE_SYS_STAT_H
1085 if( S_ISDIR( stat_info.st_mode ) )
1087 mvar_AppendNewVar( f, "type", "directory" );
1089 else if( S_ISREG( stat_info.st_mode ) )
1091 mvar_AppendNewVar( f, "type", "file" );
1095 mvar_AppendNewVar( f, "type", "unknown" );
1098 sprintf( tmp, "%lld", stat_info.st_size );
1099 mvar_AppendNewVar( f, "size", tmp );
1101 /* FIXME memory leak FIXME */
1103 ctime_r( &stat_info.st_mtime, tmp );
1104 mvar_AppendNewVar( f, "date", tmp );
1106 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1110 mvar_AppendNewVar( f, "type", "unknown" );
1111 mvar_AppendNewVar( f, "size", "unknown" );
1112 mvar_AppendNewVar( f, "date", "unknown" );
1114 mvar_AppendVar( s, f );
1120 static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
1122 mvar_t *s = mvar_New( name, "set" );
1126 fprintf( stderr," mvar_VlmSetNew: name=`%s'\n", name );
1129 if( vlm_ExecuteCommand( vlm, "show", &msg ) )
1134 for( i = 0; i < msg->i_child; i++ )
1136 /* Over media, schedule */
1137 vlm_message_t *ch = msg->child[i];
1140 for( j = 0; j < ch->i_child; j++ )
1143 vlm_message_t *el = ch->child[j];
1144 vlm_message_t *inf, *desc;
1149 sprintf( psz, "show %s", el->psz_name );
1150 if( vlm_ExecuteCommand( vlm, psz, &inf ) )
1152 desc = inf->child[0];
1154 /* Add a node with name and info */
1155 set = mvar_New( name, "set" );
1156 mvar_AppendNewVar( set, "name", el->psz_name );
1158 for( k = 0; k < desc->i_child; k++ )
1160 mvar_AppendNewVar( set, desc->child[k]->psz_name, desc->child[k]->psz_value );
1162 vlm_MessageDelete( inf );
1164 mvar_AppendVar( s, set );
1167 vlm_MessageDelete( msg );
1173 static void SSInit( rpn_stack_t * );
1174 static void SSClean( rpn_stack_t * );
1175 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1177 static void SSPush ( rpn_stack_t *, char * );
1178 static char *SSPop ( rpn_stack_t * );
1180 static void SSPushN ( rpn_stack_t *, int );
1181 static int SSPopN ( rpn_stack_t *, mvar_t * );
1184 /****************************************************************************
1186 ****************************************************************************/
1194 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1198 /* just load the file */
1200 *pp_data = malloc( 1025 ); /* +1 for \0 */
1201 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1204 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1210 (*pp_data)[*pi_data] = '\0';
1215 static int MacroParse( macro_t *m, uint8_t *psz_src )
1217 uint8_t *dup = strdup( psz_src );
1222 #define EXTRACT( name, l ) \
1224 p = strchr( src, '"' ); \
1229 m->name = strdup( src ); \
1250 if( !strncmp( src, "id=\"", 4 ) )
1254 else if( !strncmp( src, "param1=\"", 8 ) )
1256 EXTRACT( param1, 8 );
1258 else if( !strncmp( src, "param2=\"", 8 ) )
1260 EXTRACT( param2, 8 );
1267 if( strstr( src, "/>" ) )
1269 src = strstr( src, "/>" ) + 2;
1273 src += strlen( src );
1278 m->id = strdup( "" );
1280 if( m->param1 == NULL )
1282 m->param1 = strdup( "" );
1284 if( m->param2 == NULL )
1286 m->param2 = strdup( "" );
1295 static void MacroClean( macro_t *m )
1351 StrToMacroTypeTab [] =
1353 { "control", MVLC_CONTROL },
1354 /* player control */
1355 { "play", MVLC_PLAY },
1356 { "stop", MVLC_STOP },
1357 { "pause", MVLC_PAUSE },
1358 { "next", MVLC_NEXT },
1359 { "previous", MVLC_PREVIOUS },
1360 { "seek", MVLC_SEEK },
1361 { "keep", MVLC_KEEP },
1362 { "fullscreen", MVLC_FULLSCREEN },
1363 { "volume", MVLC_VOLUME },
1365 /* playlist management */
1366 { "add", MVLC_ADD },
1367 { "delete", MVLC_DEL },
1368 { "empty", MVLC_EMPTY },
1369 { "sort", MVLC_SORT },
1370 { "move", MVLC_MOVE },
1373 { "close", MVLC_CLOSE },
1374 { "shutdown", MVLC_SHUTDOWN },
1377 { "vlm_new", MVLC_VLM_NEW },
1378 { "vlm_setup", MVLC_VLM_SETUP },
1379 { "vlm_del", MVLC_VLM_DEL },
1380 { "vlm_play", MVLC_VLM_PLAY },
1381 { "vlm_pause", MVLC_VLM_PAUSE },
1382 { "vlm_stop", MVLC_VLM_STOP },
1383 { "vlm_seek", MVLC_VLM_SEEK },
1385 { "rpn", MVLC_RPN },
1387 { "foreach", MVLC_FOREACH },
1388 { "value", MVLC_VALUE },
1391 { "else", MVLC_ELSE },
1392 { "end", MVLC_END },
1393 { "get", MVLC_GET },
1394 { "set", MVLC_SET },
1395 { "int", MVLC_INT },
1396 { "float", MVLC_FLOAT },
1397 { "string", MVLC_STRING },
1400 { NULL, MVLC_UNKNOWN }
1403 static int StrToMacroType( char *name )
1407 if( !name || *name == '\0')
1409 return MVLC_UNKNOWN;
1411 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1413 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1415 return StrToMacroTypeTab[i].i_type;
1418 return MVLC_UNKNOWN;
1421 static void MacroDo( httpd_file_sys_t *p_args,
1423 uint8_t *p_request, int i_request,
1424 uint8_t **pp_data, int *pi_data,
1427 intf_thread_t *p_intf = p_args->p_intf;
1428 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1431 #define ALLOC( l ) \
1433 int __i__ = *pp_dst - *pp_data; \
1435 *pp_data = realloc( *pp_data, *pi_data ); \
1436 *pp_dst = (*pp_data) + __i__; \
1438 #define PRINT( str ) \
1439 ALLOC( strlen( str ) + 1 ); \
1440 *pp_dst += sprintf( *pp_dst, str );
1442 #define PRINTS( str, s ) \
1443 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1445 char * psz_cur = *pp_dst; \
1446 *pp_dst += sprintf( *pp_dst, str, s ); \
1447 while( psz_cur && *psz_cur ) \
1449 /* Prevent script injection */ \
1450 if( *psz_cur == '<' ) *psz_cur = '*'; \
1451 if( *psz_cur == '>' ) *psz_cur = '*'; \
1456 switch( StrToMacroType( m->id ) )
1459 if( i_request <= 0 )
1463 uri_extract_value( p_request, "control", control, 512 );
1464 if( *m->param1 && !strstr( m->param1, control ) )
1466 msg_Warn( p_intf, "unauthorized control=%s", control );
1469 switch( StrToMacroType( control ) )
1476 uri_extract_value( p_request, "item", item, 512 );
1477 i_item = atoi( item );
1478 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
1479 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1483 playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1484 msg_Dbg( p_intf, "requested playlist stop" );
1487 playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1488 msg_Dbg( p_intf, "requested playlist pause" );
1491 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1492 p_sys->p_playlist->i_index + 1 );
1493 msg_Dbg( p_intf, "requested playlist next" );
1496 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1497 p_sys->p_playlist->i_index - 1 );
1498 msg_Dbg( p_intf, "requested playlist next" );
1500 case MVLC_FULLSCREEN:
1502 if( p_sys->p_input )
1504 vout_thread_t *p_vout;
1505 p_vout = vlc_object_find( p_sys->p_input,
1506 VLC_OBJECT_VOUT, FIND_CHILD );
1510 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1511 vlc_object_release( p_vout );
1512 msg_Dbg( p_intf, "requested fullscreen toggle" );
1526 #define POSITION_ABSOLUTE 12
1527 #define POSITION_REL_FOR 13
1528 #define POSITION_REL_BACK 11
1529 #define TIME_ABSOLUTE 0
1530 #define TIME_REL_FOR 1
1531 #define TIME_REL_BACK -1
1532 if( p_sys->p_input )
1534 uri_extract_value( p_request, "seek_value", value, 20 );
1535 uri_decode_url_encoded( value );
1537 var_Get( p_sys->p_input, "length", &val);
1538 i_length = val.i_time;
1540 while( p_value[0] != '\0' )
1546 i_relative = TIME_REL_FOR;
1552 i_relative = TIME_REL_BACK;
1556 case '0': case '1': case '2': case '3': case '4':
1557 case '5': case '6': case '7': case '8': case '9':
1559 i_stock = strtol( p_value , &p_value , 10 );
1562 case '%': /* for percentage ie position */
1564 i_relative += POSITION_ABSOLUTE;
1572 i_value = 60 * (i_value + i_stock) ;
1577 case 'h': case 'H': /* hours */
1579 i_value += 3600 * i_stock;
1581 /* other characters which are not numbers are not important */
1582 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1588 case 'm': case 'M': case '\'': /* minutes */
1590 i_value += 60 * i_stock;
1593 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1599 case 's': case 'S': case '"': /* seconds */
1603 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1617 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1624 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1625 val.i_time = (uint64_t)( i_value ) * 1000000;
1627 val.i_time = i_length;
1629 var_Set( p_sys->p_input, "time", val );
1630 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1635 var_Get( p_sys->p_input, "time", &val );
1636 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1638 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1641 val.i_time = i_length;
1643 var_Set( p_sys->p_input, "time", val );
1644 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1649 var_Get( p_sys->p_input, "time", &val );
1650 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1655 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1657 var_Set( p_sys->p_input, "time", val );
1658 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1661 case POSITION_ABSOLUTE:
1663 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1664 var_Set( p_sys->p_input, "position", val );
1665 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1668 case POSITION_REL_FOR:
1670 var_Get( p_sys->p_input, "position", &val );
1671 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1672 var_Set( p_sys->p_input, "position", val );
1673 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1676 case POSITION_REL_BACK:
1678 var_Get( p_sys->p_input, "position", &val );
1679 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1680 var_Set( p_sys->p_input, "position", val );
1681 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
1686 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1691 #undef POSITION_ABSOLUTE
1692 #undef POSITION_REL_FOR
1693 #undef POSITION_REL_BACK
1694 #undef TIME_ABSOLUTE
1696 #undef TIME_REL_BACK
1702 audio_volume_t i_volume;
1705 uri_extract_value( p_request, "value", vol, 8 );
1706 aout_VolumeGet( p_intf, &i_volume );
1707 uri_decode_url_encoded( vol );
1711 i_value = atoi( vol + 1 );
1712 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1714 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1715 msg_Dbg( p_intf, "requested volume set: max" );
1718 aout_VolumeSet( p_intf , (i_volume + i_value) );
1719 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1724 i_value = atoi( vol + 1 );
1725 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1727 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1728 msg_Dbg( p_intf, "requested volume set: min" );
1731 aout_VolumeSet( p_intf , (i_volume - i_value) );
1732 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1736 i_value = atoi( vol );
1737 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1739 aout_VolumeSet( p_intf , atoi( vol ) );
1740 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1746 /* playlist management */
1750 playlist_item_t * p_item;
1752 uri_extract_value( p_request, "mrl", mrl, 512 );
1753 uri_decode_url_encoded( mrl );
1754 p_item = parse_MRL( p_intf, mrl );
1756 if( !p_item || !p_item->psz_uri || !*p_item->psz_uri )
1758 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1761 playlist_AddItem( p_sys->p_playlist , p_item ,
1762 PLAYLIST_APPEND, PLAYLIST_END );
1763 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1770 int i_item, *p_items = NULL, i_nb_items = 0;
1771 char item[512], *p_parser = p_request;
1773 /* Get the list of items to delete */
1775 uri_extract_value( p_parser, "item", item, 512 )) )
1777 if( !*item ) continue;
1779 i_item = atoi( item );
1780 p_items = realloc( p_items, (i_nb_items + 1) *
1782 p_items[i_nb_items] = i_item;
1786 /* The items need to be deleted from in reversed order */
1790 for( i = 0; i < i_nb_items; i++ )
1793 for( j = 0; j < i_nb_items; j++ )
1795 if( p_items[j] > p_items[i_index] )
1799 playlist_Delete( p_sys->p_playlist,
1801 msg_Dbg( p_intf, "requested playlist delete: %d",
1803 p_items[i_index] = -1;
1807 if( p_items ) free( p_items );
1812 int i_item, *p_items = NULL, i_nb_items = 0;
1813 char item[512], *p_parser = p_request;
1816 /* Get the list of items to keep */
1818 uri_extract_value( p_parser, "item", item, 512 )) )
1820 if( !*item ) continue;
1822 i_item = atoi( item );
1823 p_items = realloc( p_items, (i_nb_items + 1) *
1825 p_items[i_nb_items] = i_item;
1829 /* The items need to be deleted from in reversed order */
1830 for( i = p_sys->p_playlist->i_size - 1; i >= 0 ; i-- )
1832 /* Check if the item is in the keep list */
1833 for( j = 0 ; j < i_nb_items ; j++ )
1835 if( p_items[j] == i ) break;
1837 if( j == i_nb_items )
1839 playlist_Delete( p_sys->p_playlist, i );
1840 msg_Dbg( p_intf, "requested playlist delete: %d",
1845 if( p_items ) free( p_items );
1850 while( p_sys->p_playlist->i_size > 0 )
1852 playlist_Delete( p_sys->p_playlist, 0 );
1854 msg_Dbg( p_intf, "requested playlist empty" );
1863 uri_extract_value( p_request, "type", type, 12 );
1864 uri_extract_value( p_request, "order", order, 2 );
1866 if( order[0] == '0' ) i_order = ORDER_NORMAL;
1867 else i_order = ORDER_REVERSE;
1869 if( !strcmp( type , "title" ) )
1871 playlist_SortTitle( p_sys->p_playlist , i_order );
1872 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
1873 } else if( !strcmp( type , "group" ) )
1875 playlist_SortGroup( p_sys->p_playlist , i_order );
1876 msg_Dbg( p_intf, "requested playlist sort by group (%d)" , i_order );
1877 } else if( !strcmp( type , "author" ) )
1879 playlist_SortAuthor( p_sys->p_playlist , i_order );
1880 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
1881 } else if( !strcmp( type , "shuffle" ) )
1883 playlist_Sort( p_sys->p_playlist , SORT_RANDOM, ORDER_NORMAL );
1884 msg_Dbg( p_intf, "requested playlist shuffle");
1895 uri_extract_value( p_request, "psz_pos", psz_pos, 6 );
1896 uri_extract_value( p_request, "psz_newpos", psz_newpos, 6 );
1897 i_pos = atoi( psz_pos );
1898 i_newpos = atoi( psz_newpos );
1899 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
1900 msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
1904 /* admin function */
1908 uri_extract_value( p_request, "id", id, 512 );
1909 msg_Dbg( p_intf, "requested close id=%s", id );
1911 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
1913 msg_Warn( p_intf, "close failed for id=%s", id );
1920 msg_Dbg( p_intf, "requested shutdown" );
1921 p_intf->p_vlc->b_die = VLC_TRUE;
1926 case MVLC_VLM_SETUP:
1928 static const char *vlm_properties[11] =
1930 "input", "output", "option", "enabled", "disabled",
1931 "loop", "unloop", "append", "date", "period", "repeat",
1933 vlm_message_t *vlm_answer;
1935 char *psz = malloc( strlen( p_request ) + 1000 );
1939 if( p_intf->p_sys->p_vlm == NULL )
1940 p_intf->p_sys->p_vlm = vlm_New( p_intf );
1942 uri_extract_value( p_request, "name", name, 512 );
1943 if( StrToMacroType( control ) == MVLC_VLM_NEW )
1946 uri_extract_value( p_request, "type", type, 20 );
1947 p += sprintf( psz, "new %s %s", name, type );
1951 p += sprintf( psz, "setup %s", name );
1953 /* Parse the request */
1954 for( i = 0; i < 11; i++ )
1957 uri_extract_value( p_request, vlm_properties[i], val, 512 );
1958 if( strlen( val ) > 0 )
1960 p += sprintf( p, " %s %s", vlm_properties[i], val );
1962 else if( uri_test_param( p_request, vlm_properties[i] ) )
1964 p += sprintf( p, " %s", vlm_properties[i] );
1967 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
1968 /* FIXME do a vlm_answer -> var stack conversion */
1969 vlm_MessageDelete( vlm_answer );
1976 vlm_message_t *vlm_answer;
1979 if( p_intf->p_sys->p_vlm == NULL )
1980 p_intf->p_sys->p_vlm = vlm_New( p_intf );
1982 uri_extract_value( p_request, "name", name, 512 );
1983 sprintf( psz, "del %s", name );
1985 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
1986 /* FIXME do a vlm_answer -> var stack conversion */
1987 vlm_MessageDelete( vlm_answer );
1992 case MVLC_VLM_PAUSE:
1996 vlm_message_t *vlm_answer;
1999 if( p_intf->p_sys->p_vlm == NULL )
2000 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2002 uri_extract_value( p_request, "name", name, 512 );
2003 if( StrToMacroType( control ) == MVLC_VLM_PLAY )
2004 sprintf( psz, "control %s play", name );
2005 else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
2006 sprintf( psz, "control %s pause", name );
2007 else if( StrToMacroType( control ) == MVLC_VLM_STOP )
2008 sprintf( psz, "control %s stop", name );
2009 else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
2012 uri_extract_value( p_request, "percent", percent, 512 );
2013 sprintf( psz, "control %s seek %s", name, percent );
2016 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2017 /* FIXME do a vlm_answer -> var stack conversion */
2018 vlm_MessageDelete( vlm_answer );
2023 PRINTS( "<!-- control param(%s) unsuported -->", control );
2034 if( i_request <= 0 ||
2035 *m->param1 == '\0' ||
2036 strstr( p_request, m->param1 ) == NULL )
2040 uri_extract_value( p_request, m->param1, value, 512 );
2041 uri_decode_url_encoded( value );
2043 switch( StrToMacroType( m->param2 ) )
2047 config_PutInt( p_intf, m->param1, i );
2051 config_PutFloat( p_intf, m->param1, f );
2054 config_PutPsz( p_intf, m->param1, value );
2057 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
2068 if( *m->param1 == '\0' )
2073 switch( StrToMacroType( m->param2 ) )
2076 i = config_GetInt( p_intf, m->param1 );
2077 sprintf( value, "%i", i );
2080 f = config_GetFloat( p_intf, m->param1 );
2081 sprintf( value, "%f", f );
2084 psz = config_GetPsz( p_intf, m->param1 );
2085 sprintf( value, "%s", psz ? psz : "" );
2086 if( psz ) free( psz );
2089 sprintf( value, "invalid type(%s) in set", m->param2 );
2092 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
2093 PRINTS( "%s", value );
2102 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2103 s = SSPop( &p_args->stack );
2104 v = mvar_GetValue( p_args->vars, s );
2108 v = s = SSPop( &p_args->stack );
2116 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2121 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2122 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2130 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
2137 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
2142 i_skip = MacroParse( &m, src );
2144 i_id = StrToMacroType( m.id );
2161 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2162 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2164 return src + ( b_after ? i_skip : 0 );
2166 else if( i_level < 0 )
2182 static void Execute( httpd_file_sys_t *p_args,
2183 uint8_t *p_request, int i_request,
2184 uint8_t **pp_data, int *pi_data,
2186 uint8_t *_src, uint8_t *_end )
2188 intf_thread_t *p_intf = p_args->p_intf;
2190 uint8_t *src, *dup, *end;
2191 uint8_t *dst = *pp_dst;
2193 src = dup = malloc( _end - _src + 1 );
2194 end = src +( _end - _src );
2196 memcpy( src, _src, _end - _src );
2199 /* we parse searching <vlc */
2205 p = strstr( src, "<vlc" );
2206 if( p < end && p == src )
2210 src += MacroParse( &m, src );
2212 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2214 switch( StrToMacroType( m.id ) )
2221 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2222 if( SSPopN( &p_args->stack, p_args->vars ) )
2230 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2234 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2238 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2241 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2245 else if( i_test == 1 )
2248 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2250 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2254 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2263 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2264 uint8_t *start = src;
2265 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2272 if( !strcmp( m.param2, "integer" ) )
2274 char *arg = SSPop( &p_args->stack );
2275 index = mvar_IntegerSetNew( m.param1, arg );
2278 else if( !strcmp( m.param2, "directory" ) )
2280 char *arg = SSPop( &p_args->stack );
2281 index = mvar_FileSetNew( m.param1, arg );
2284 else if( !strcmp( m.param2, "playlist" ) )
2286 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2288 else if( !strcmp( m.param2, "informations" ) )
2290 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2292 else if( !strcmp( m.param2, "vlm" ) )
2294 if( p_intf->p_sys->p_vlm == NULL )
2296 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2298 index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
2301 else if( !strcmp( m.param2, "hosts" ) )
2303 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2305 else if( !strcmp( m.param2, "urls" ) )
2307 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2309 else if( !strcmp( m.param2, "connections" ) )
2311 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2314 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2316 index = mvar_Duplicate( v );
2320 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2325 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2327 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2329 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2332 f->name = strdup( m.param1 );
2335 mvar_PushVar( p_args->vars, f );
2336 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2337 mvar_RemoveVar( p_args->vars, f );
2341 mvar_Delete( index );
2348 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2356 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2359 int i_index = dst - *pp_data;
2362 *pp_data = realloc( *pp_data, *pi_data );
2363 dst = (*pp_data) + i_index;
2365 memcpy( dst, src, i_copy );
2375 /****************************************************************************
2377 ****************************************************************************
2378 * a file with b_html is parsed and all "macro" replaced
2379 * <vlc id="macro name" [param1="" [param2=""]] />
2382 ****************************************************************************/
2383 static int HttpCallback( httpd_file_sys_t *p_args,
2384 httpd_file_t *p_file,
2386 uint8_t **pp_data, int *pi_data )
2388 int i_request = p_request ? strlen( p_request ) : 0;
2392 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2394 p = *pp_data = malloc( 10240 );
2397 return VLC_EGENERIC;
2399 p += sprintf( p, "<html>\n" );
2400 p += sprintf( p, "<head>\n" );
2401 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2402 p += sprintf( p, "</head>\n" );
2403 p += sprintf( p, "<body>\n" );
2404 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2405 p += sprintf( p, "<hr />\n" );
2406 p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
2407 p += sprintf( p, "</body>\n" );
2408 p += sprintf( p, "</html>\n" );
2410 *pi_data = strlen( *pp_data );
2415 if( !p_args->b_html )
2417 FileLoad( f, pp_data, pi_data );
2425 char position[4]; /* percentage */
2426 char time[12]; /* in seconds */
2427 char length[12]; /* in seconds */
2428 audio_volume_t i_volume;
2432 #define p_sys p_args->p_intf->p_sys
2433 if( p_sys->p_input )
2435 var_Get( p_sys->p_input, "position", &val);
2436 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2437 var_Get( p_sys->p_input, "time", &val);
2438 sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2439 var_Get( p_sys->p_input, "length", &val);
2440 sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2442 var_Get( p_sys->p_input, "state", &val );
2443 if( val.i_int == PLAYING_S )
2445 sprintf( state, "playing" );
2446 } else if( val.i_int == PAUSE_S )
2448 sprintf( state, "paused" );
2451 sprintf( state, "stop" );
2455 sprintf( position, "%d", 0 );
2456 sprintf( time, "%d", 0 );
2457 sprintf( length, "%d", 0 );
2458 sprintf( state, "stop" );
2462 aout_VolumeGet( p_args->p_intf , &i_volume );
2463 sprintf( volume , "%d" , (int)i_volume );
2465 p_args->vars = mvar_New( "variables", "" );
2466 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2467 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2468 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
2469 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2470 mvar_AppendNewVar( p_args->vars, "stream_position", position );
2471 mvar_AppendNewVar( p_args->vars, "stream_time", time );
2472 mvar_AppendNewVar( p_args->vars, "stream_length", length );
2473 mvar_AppendNewVar( p_args->vars, "volume", volume );
2474 mvar_AppendNewVar( p_args->vars, "stream_state", state );
2476 SSInit( &p_args->stack );
2478 /* first we load in a temporary buffer */
2479 FileLoad( f, &p_buffer, &i_buffer );
2481 /* allocate output */
2482 *pi_data = i_buffer + 1000;
2483 dst = *pp_data = malloc( *pi_data );
2485 /* we parse executing all <vlc /> macros */
2486 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2489 *pi_data = dst - *pp_data;
2491 SSClean( &p_args->stack );
2492 mvar_Delete( p_args->vars );
2501 /****************************************************************************
2503 ****************************************************************************/
2504 static int uri_test_param( char *psz_uri, char *psz_name )
2508 while( (p = strstr( p, psz_name )) )
2510 /* Verify that we are dealing with a post/get argument */
2511 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2520 static char *uri_extract_value( char *psz_uri, char *psz_name,
2521 char *psz_value, int i_value_max )
2525 while( (p = strstr( p, psz_name )) )
2527 /* Verify that we are dealing with a post/get argument */
2528 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2537 p += strlen( psz_name );
2538 if( *p == '=' ) p++;
2540 if( strchr( p, '&' ) )
2542 i_len = strchr( p, '&' ) - p;
2546 /* for POST method */
2547 if( strchr( p, '\n' ) )
2549 i_len = strchr( p, '\n' ) - p;
2550 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2554 i_len = strlen( p );
2557 i_len = __MIN( i_value_max - 1, i_len );
2560 strncpy( psz_value, p, i_len );
2561 psz_value[i_len] = '\0';
2565 strncpy( psz_value, "", i_value_max );
2571 strncpy( psz_value, "", i_value_max );
2577 static void uri_decode_url_encoded( char *psz )
2579 char *dup = strdup( psz );
2597 *psz++ = strtol( val, NULL, 16 );
2599 else if( *p == '+' )
2613 /****************************************************************************
2614 * Light RPN evaluator
2615 ****************************************************************************/
2616 static void SSInit( rpn_stack_t *st )
2621 static void SSClean( rpn_stack_t *st )
2623 while( st->i_stack > 0 )
2625 free( st->stack[--st->i_stack] );
2629 static void SSPush( rpn_stack_t *st, char *s )
2631 if( st->i_stack < STACK_MAX )
2633 st->stack[st->i_stack++] = strdup( s );
2637 static char * SSPop( rpn_stack_t *st )
2639 if( st->i_stack <= 0 )
2641 return strdup( "" );
2645 return st->stack[--st->i_stack];
2649 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2658 i = strtol( name, &end, 0 );
2661 value = mvar_GetValue( vars, name );
2669 static void SSPushN( rpn_stack_t *st, int i )
2673 sprintf( v, "%d", i );
2677 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2684 while( *exp == ' ' )
2691 /* extract string */
2694 while( *exp && *exp != '\'' )
2705 p = strchr( exp, ' ' );
2710 exp += strlen( exp );
2715 strncpy( s, exp, i );
2726 /* 1. Integer function */
2727 if( !strcmp( s, "!" ) )
2729 SSPushN( st, ~SSPopN( st, vars ) );
2731 else if( !strcmp( s, "^" ) )
2733 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2735 else if( !strcmp( s, "&" ) )
2737 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2739 else if( !strcmp( s, "|" ) )
2741 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2743 else if( !strcmp( s, "+" ) )
2745 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2747 else if( !strcmp( s, "-" ) )
2749 int j = SSPopN( st, vars );
2750 int i = SSPopN( st, vars );
2751 SSPushN( st, i - j );
2753 else if( !strcmp( s, "*" ) )
2755 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2757 else if( !strcmp( s, "/" ) )
2761 j = SSPopN( st, vars );
2762 i = SSPopN( st, vars );
2764 SSPushN( st, j != 0 ? i / j : 0 );
2766 else if( !strcmp( s, "%" ) )
2770 j = SSPopN( st, vars );
2771 i = SSPopN( st, vars );
2773 SSPushN( st, j != 0 ? i % j : 0 );
2775 /* 2. integer tests */
2776 else if( !strcmp( s, "=" ) )
2778 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2780 else if( !strcmp( s, "<" ) )
2782 int j = SSPopN( st, vars );
2783 int i = SSPopN( st, vars );
2785 SSPushN( st, i < j ? -1 : 0 );
2787 else if( !strcmp( s, ">" ) )
2789 int j = SSPopN( st, vars );
2790 int i = SSPopN( st, vars );
2792 SSPushN( st, i > j ? -1 : 0 );
2794 else if( !strcmp( s, "<=" ) )
2796 int j = SSPopN( st, vars );
2797 int i = SSPopN( st, vars );
2799 SSPushN( st, i <= j ? -1 : 0 );
2801 else if( !strcmp( s, ">=" ) )
2803 int j = SSPopN( st, vars );
2804 int i = SSPopN( st, vars );
2806 SSPushN( st, i >= j ? -1 : 0 );
2808 /* 3. string functions */
2809 else if( !strcmp( s, "strcat" ) )
2811 char *s2 = SSPop( st );
2812 char *s1 = SSPop( st );
2813 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2823 else if( !strcmp( s, "strcmp" ) )
2825 char *s2 = SSPop( st );
2826 char *s1 = SSPop( st );
2828 SSPushN( st, strcmp( s1, s2 ) );
2832 else if( !strcmp( s, "strncmp" ) )
2834 int n = SSPopN( st, vars );
2835 char *s2 = SSPop( st );
2836 char *s1 = SSPop( st );
2838 SSPushN( st, strncmp( s1, s2 , n ) );
2842 else if( !strcmp( s, "strsub" ) )
2844 int n = SSPopN( st, vars );
2845 int m = SSPopN( st, vars );
2847 char *s = SSPop( st );
2859 str = malloc( i_len + 1 );
2861 memcpy( str, s + m - 1, i_len );
2862 str[ i_len ] = '\0';
2868 else if( !strcmp( s, "strlen" ) )
2870 char *str = SSPop( st );
2872 SSPushN( st, strlen( str ) );
2875 /* 4. stack functions */
2876 else if( !strcmp( s, "dup" ) )
2878 char *str = SSPop( st );
2883 else if( !strcmp( s, "drop" ) )
2885 char *str = SSPop( st );
2888 else if( !strcmp( s, "swap" ) )
2890 char *s1 = SSPop( st );
2891 char *s2 = SSPop( st );
2898 else if( !strcmp( s, "flush" ) )
2903 else if( !strcmp( s, "store" ) )
2905 char *value = SSPop( st );
2906 char *name = SSPop( st );
2908 mvar_PushNewVar( vars, name, value );
2912 else if( !strcmp( s, "value" ) )
2914 char *name = SSPop( st );
2915 char *value = mvar_GetValue( vars, name );
2917 SSPush( st, value );
2921 else if( !strcmp( s, "url_extract" ) )
2923 char *url = mvar_GetValue( vars, "url_value" );
2924 char *name = SSPop( st );
2927 uri_extract_value( url, name, value, 512 );
2928 uri_decode_url_encoded( value );
2929 SSPush( st, value );
2938 /**********************************************************************
2939 * Find_end_MRL: Find the end of the sentence :
2940 * this function parses the string psz and find the end of the item
2941 * and/or option with detecting the " and ' problems.
2942 * returns NULL if an error is detected, otherwise, returns a pointer
2943 * of the end of the sentence (after the last character)
2944 **********************************************************************/
2945 static char *Find_end_MRL( char *psz )
2955 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
2957 if( *s_sent == '\'' )
2959 s_sent = Find_end_MRL( s_sent );
2961 if( s_sent == NULL )
2971 if( *s_sent == '\"' )
2975 } else /* *s_sent == '\0' , which means the number of " is incorrect */
2985 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
2987 if( *s_sent == '\"' )
2989 s_sent = Find_end_MRL( s_sent );
2991 if( s_sent == NULL )
3001 if( *s_sent == '\'' )
3005 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
3011 default: /* now we can look for spaces */
3013 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
3015 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
3017 s_sent = Find_end_MRL( s_sent );
3028 /**********************************************************************
3029 * parse_MRL: parse the MRL, find the mrl string and the options,
3030 * create an item with all informations in it, and return the item.
3031 * return NULL if there is an error.
3032 **********************************************************************/
3033 playlist_item_t * parse_MRL( intf_thread_t *p_intf, char *psz )
3035 char **ppsz_options = NULL;
3042 playlist_item_t * p_item = NULL;
3044 /* In case there is spaces before the mrl */
3045 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
3050 /* extract the mrl */
3051 s_temp = strstr( s_mrl , " :" );
3052 if( s_temp == NULL )
3054 s_temp = s_mrl + strlen( s_mrl );
3057 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
3064 /* if the mrl is between " or ', we must remove them */
3065 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
3067 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
3068 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
3069 mrl[ s_temp - s_mrl - 2 ] = '\0';
3072 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
3073 strncpy( mrl , s_mrl , s_temp - s_mrl );
3074 mrl[ s_temp - s_mrl ] = '\0';
3079 /* now we can take care of the options */
3080 while( (*s_mrl != '\0') && (i_error == 0) )
3089 case ':': /* an option */
3091 s_temp = Find_end_MRL( s_mrl );
3093 if( s_temp == NULL )
3100 ppsz_options = realloc( ppsz_options , i_options *
3102 ppsz_options[ i_options - 1 ] =
3103 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
3105 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
3108 /* don't forget to finish the string with a '\0' */
3109 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
3129 /* now create an item */
3130 p_item = playlist_ItemNew( p_intf, mrl, mrl);
3131 for( i = 0 ; i< i_options ; i++ )
3133 playlist_ItemAddOption( p_item, ppsz_options[i] );
3137 for( i = 0 ; i < i_options ; i++ )
3139 free( ppsz_options[i] );
3141 free( ppsz_options );