1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
38 #include <vlc/vout.h> /* for fullscreen */
40 #include "vlc_httpd.h"
44 #ifdef HAVE_SYS_STAT_H
45 # include <sys/stat.h>
56 #elif defined( WIN32 ) && !defined( UNDER_CE )
60 #if (!defined( WIN32 ) || defined(__MINGW32__))
61 /* Mingw has its own version of dirent */
65 /* stat() support for large files on win32 */
66 #if defined( WIN32 ) && !defined( UNDER_CE )
67 # define stat _stati64
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Open ( vlc_object_t * );
74 static void Close( vlc_object_t * );
76 #define HOST_TEXT N_( "Host address" )
77 #define HOST_LONGTEXT N_( \
78 "You can set the address and port the http interface will bind to." )
79 #define SRC_TEXT N_( "Source directory" )
80 #define SRC_LONGTEXT N_( "Source directory" )
81 #define CERT_TEXT N_( "Certificate file" )
82 #define CERT_LONGTEXT N_( "HTTP interface x509 PEM certificate file (enables SSL)" )
83 #define KEY_TEXT N_( "Private key file" )
84 #define KEY_LONGTEXT N_( "HTTP interface x509 PEM private key file" )
85 #define CA_TEXT N_( "Root CA file" )
86 #define CA_LONGTEXT N_( "HTTP interface x509 PEM trusted root CA certificates file" )
87 #define CRL_TEXT N_( "CRL file" )
88 #define CRL_LONGTEXT N_( "HTTP interace Certificates Revocation List file" )
91 set_description( _("HTTP remote control interface") );
92 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
93 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
94 add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
95 add_string ( "http-intf-key", NULL, NULL, KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
96 add_string ( "http-intf-ca", NULL, NULL, CA_TEXT, CA_LONGTEXT, VLC_TRUE );
97 add_string ( "http-intf-crl", NULL, NULL, CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
98 set_capability( "interface", 0 );
99 set_callbacks( Open, Close );
103 /*****************************************************************************
105 *****************************************************************************/
106 static void Run ( intf_thread_t *p_intf );
108 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
111 static int DirectoryCheck( char *psz_dir )
115 #ifdef HAVE_SYS_STAT_H
116 struct stat stat_info;
118 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
124 if( ( p_dir = opendir( psz_dir ) ) == NULL )
133 static int HttpCallback( httpd_file_sys_t *p_args,
136 uint8_t **pp_data, int *pi_data );
138 static char *uri_extract_value( char *psz_uri, const char *psz_name,
139 char *psz_value, int i_value_max );
140 static int uri_test_param( char *psz_uri, const char *psz_name );
142 static void uri_decode_url_encoded( char *psz );
144 static char *Find_end_MRL( char *psz );
146 static playlist_item_t * parse_MRL( intf_thread_t * , char *psz );
148 /*****************************************************************************
150 *****************************************************************************/
151 typedef struct mvar_s
157 struct mvar_s **field;
160 #define STACK_MAX 100
163 char *stack[STACK_MAX];
167 struct httpd_file_sys_t
169 intf_thread_t *p_intf;
170 httpd_file_t *p_file;
171 httpd_redirect_t *p_redir;
178 /* inited for each access */
185 httpd_host_t *p_httpd_host;
188 httpd_file_sys_t **pp_files;
190 playlist_t *p_playlist;
191 input_thread_t *p_input;
197 /*****************************************************************************
198 * Activate: initialize and create stuff
199 *****************************************************************************/
200 static int Open( vlc_object_t *p_this )
202 intf_thread_t *p_intf = (intf_thread_t*)p_this;
205 char *psz_address = "";
206 const char *psz_cert;
211 psz_host = config_GetPsz( p_intf, "http-host" );
215 psz_address = psz_host;
217 psz_parser = strchr( psz_host, ':' );
220 *psz_parser++ = '\0';
221 i_port = atoi( psz_parser );
225 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
228 return( VLC_ENOMEM );
230 p_sys->p_playlist = NULL;
231 p_sys->p_input = NULL;
234 psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
235 if ( psz_cert != NULL )
239 msg_Dbg( p_intf, "enablind TLS for HTTP interface (cert file: %s)",
241 psz_pem = config_GetPsz( p_intf, "http-intf-key" );
243 p_tls = tls_ServerCreate( p_this, psz_cert, psz_pem );
246 msg_Err( p_intf, "TLS initialization error" );
251 psz_pem = config_GetPsz( p_intf, "http-intf-ca" );
252 if ( ( psz_pem != NULL) && tls_ServerAddCA( p_tls, psz_pem ) )
254 msg_Err( p_intf, "TLS CA error" );
255 tls_ServerDelete( p_tls );
260 psz_pem = config_GetPsz( p_intf, "http-intf-crl" );
261 if ( ( psz_pem != NULL) && tls_ServerAddCRL( p_tls, psz_pem ) )
263 msg_Err( p_intf, "TLS CRL error" );
264 tls_ServerDelete( p_tls );
279 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
281 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_intf), psz_address,
283 if( p_sys->p_httpd_host == NULL )
285 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
287 tls_ServerDelete( p_tls );
298 p_sys->pp_files = NULL;
300 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
301 ( defined(WIN32) && !defined(UNDER_CE ) )
302 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
304 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
305 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
311 sprintf( psz_src, "%s/http", psz_vlcpath);
313 sprintf( psz_src, "%s/share/http", psz_vlcpath);
317 psz_src = config_GetPsz( p_intf, "http-src" );
319 if( !psz_src || *psz_src == '\0' )
321 if( !DirectoryCheck( "share/http" ) )
323 psz_src = strdup( "share/http" );
325 else if( !DirectoryCheck( DATA_PATH "/http" ) )
327 psz_src = strdup( DATA_PATH "/http" );
332 if( !psz_src || *psz_src == '\0' )
334 msg_Err( p_intf, "invalid src dir" );
338 /* remove trainling \ or / */
339 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
340 psz_src[strlen( psz_src ) - 1] == '/' )
342 psz_src[strlen( psz_src ) - 1] = '\0';
345 ParseDirectory( p_intf, psz_src, psz_src );
348 if( p_sys->i_files <= 0 )
350 msg_Err( p_intf, "cannot find any files (%s)", psz_src );
353 p_intf->pf_run = Run;
359 if( psz_src ) free( psz_src );
360 if( p_sys->pp_files )
362 free( p_sys->pp_files );
364 httpd_HostDelete( p_sys->p_httpd_host );
369 /*****************************************************************************
370 * CloseIntf: destroy interface
371 *****************************************************************************/
372 void Close ( vlc_object_t *p_this )
374 intf_thread_t *p_intf = (intf_thread_t *)p_this;
375 intf_sys_t *p_sys = p_intf->p_sys;
381 vlm_Delete( p_sys->p_vlm );
383 for( i = 0; i < p_sys->i_files; i++ )
385 httpd_FileDelete( p_sys->pp_files[i]->p_file );
386 if( p_sys->pp_files[i]->p_redir )
388 httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
391 free( p_sys->pp_files[i]->file );
392 free( p_sys->pp_files[i]->name );
393 free( p_sys->pp_files[i] );
395 if( p_sys->pp_files )
397 free( p_sys->pp_files );
399 httpd_HostDelete( p_sys->p_httpd_host );
404 /*****************************************************************************
405 * Run: http interface thread
406 *****************************************************************************/
407 static void Run( intf_thread_t *p_intf )
409 intf_sys_t *p_sys = p_intf->p_sys;
411 while( !p_intf->b_die )
413 /* get the playlist */
414 if( p_sys->p_playlist == NULL )
416 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
419 /* Manage the input part */
420 if( p_sys->p_input == NULL )
422 if( p_sys->p_playlist )
425 vlc_object_find( p_sys->p_playlist,
430 else if( p_sys->p_input->b_dead )
432 vlc_object_release( p_sys->p_input );
433 p_sys->p_input = NULL;
438 msleep( INTF_IDLE_SLEEP );
443 vlc_object_release( p_sys->p_input );
444 p_sys->p_input = NULL;
447 if( p_sys->p_playlist )
449 vlc_object_release( p_sys->p_playlist );
450 p_sys->p_playlist = NULL;
455 /*****************************************************************************
457 *****************************************************************************/
458 #define MAX_DIR_SIZE 10240
460 /****************************************************************************
461 * FileToUrl: create a good name for an url from filename
462 ****************************************************************************/
463 static char *FileToUrl( char *name )
467 url = p = malloc( strlen( name ) + 1 );
475 while( *name == '\\' || *name == '/' )
477 while( *name == '/' )
487 /* convert '\\' into '/' */
500 if( ( p = strrchr( url, '/' ) ) != NULL )
502 if( !strncmp( p, "/index.", 7 ) )
510 /****************************************************************************
511 * ParseDirectory: parse recursively a directory, adding each file
512 ****************************************************************************/
513 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
516 intf_sys_t *p_sys = p_intf->p_sys;
517 char dir[MAX_DIR_SIZE];
518 #ifdef HAVE_SYS_STAT_H
519 struct stat stat_info;
522 struct dirent *p_dir_content;
526 char *password = NULL;
528 #ifdef HAVE_SYS_STAT_H
529 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
535 if( ( p_dir = opendir( psz_dir ) ) == NULL )
537 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
541 msg_Dbg( p_intf, "dir=%s", psz_dir );
543 sprintf( dir, "%s/.access", psz_dir );
544 if( ( file = fopen( dir, "r" ) ) != NULL )
549 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
551 i_size = fread( line, 1, 1023, file );
555 while( i_size > 0 && ( line[i_size-1] == '\n' ||
556 line[i_size-1] == '\r' ) )
563 p = strchr( line, ':' );
567 user = strdup( line );
568 password = strdup( p );
571 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
572 user, password, i_size );
579 /* parse psz_src dir */
580 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
585 if( p_dir_content->d_name[0] == '.' )
589 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
590 if( ParseDirectory( p_intf, psz_root, dir ) )
592 httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
597 f->file = strdup( dir );
598 f->name = FileToUrl( &dir[strlen( psz_root )] );
599 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
603 msg_Err( p_intf , "unable to parse directory" );
606 return( VLC_ENOMEM );
608 msg_Dbg( p_intf, "file=%s (url=%s)",
611 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
612 f->name, f->b_html ? "text/html" : NULL,
618 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
620 /* For rep/ add a redir from rep to rep/ */
621 if( f && f->name[strlen(f->name) - 1] == '/' )
623 char *psz_redir = strdup( f->name );
624 psz_redir[strlen( psz_redir ) - 1] = '\0';
626 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
627 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
647 /****************************************************************************
648 * var and set handling
649 ****************************************************************************/
651 static mvar_t *mvar_New( char *name, char *value )
653 mvar_t *v = malloc( sizeof( mvar_t ) );
655 if( !v ) return NULL;
656 v->name = strdup( name );
657 v->value = strdup( value ? value : "" );
660 v->field = malloc( sizeof( mvar_t * ) );
666 static void mvar_Delete( mvar_t *v )
673 for( i = 0; i < v->i_field; i++ )
675 mvar_Delete( v->field[i] );
681 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
683 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
684 v->field[v->i_field] = f;
688 static mvar_t *mvar_Duplicate( mvar_t *v )
693 n = mvar_New( v->name, v->value );
694 for( i = 0; i < v->i_field; i++ )
696 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
702 static void mvar_PushVar( mvar_t *v, mvar_t *f )
704 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
707 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
713 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
716 for( i = 0; i < v->i_field; i++ )
718 if( v->field[i] == f )
723 if( i >= v->i_field )
728 if( i + 1 < v->i_field )
730 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
733 /* FIXME should do a realloc */
736 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
739 char base[512], *field, *p;
742 /* format: name[index].field */
744 field = strchr( name, '.' );
747 int i = field - name;
748 strncpy( base, name, i );
754 strcpy( base, name );
757 if( ( p = strchr( base, '[' ) ) )
760 sscanf( p, "%d]", &i_index );
771 for( i = 0; i < s->i_field; i++ )
773 if( !strcmp( s->field[i]->name, base ) )
783 return mvar_GetVar( s->field[i], field );
797 static char *mvar_GetValue( mvar_t *v, char *field )
805 mvar_t *f = mvar_GetVar( v, field );
817 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
819 mvar_t *f = mvar_New( name, value );
820 mvar_PushVar( vars, f );
823 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
825 mvar_t *f = mvar_New( name, value );
826 mvar_AppendVar( vars, f );
830 /* arg= start[:stop[:step]],.. */
831 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
833 char *dup = strdup( arg );
835 mvar_t *s = mvar_New( name, "set" );
837 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
842 int i_start,i_stop,i_step;
845 p = strchr( str, ',' );
852 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
853 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
860 else if( i_match == 2 )
862 i_step = i_start < i_stop ? 1 : -1;
869 if( ( i_start < i_stop && i_step > 0 ) ||
870 ( i_start > i_stop && i_step < 0 ) )
872 for( i = i_start; ; i += i_step )
876 if( ( i_step > 0 && i > i_stop ) ||
877 ( i_step < 0 && i < i_stop ) )
882 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
883 sprintf( value, "%d", i );
885 mvar_PushNewVar( s, name, value );
896 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
898 mvar_t *s = mvar_New( name, "set" );
901 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
903 vlc_mutex_lock( &p_pl->object_lock );
904 for( i = 0; i < p_pl->i_size; i++ )
906 mvar_t *itm = mvar_New( name, "set" );
909 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
910 mvar_AppendNewVar( itm, "current", value );
912 sprintf( value, "%d", i );
913 mvar_AppendNewVar( itm, "index", value );
915 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->input.psz_name );
917 mvar_AppendNewVar( itm, "uri", p_pl->pp_items[i]->input.psz_uri );
919 mvar_AppendVar( s, itm );
921 vlc_mutex_unlock( &p_pl->object_lock );
926 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
928 mvar_t *s = mvar_New( name, "set" );
931 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
932 if( p_input == NULL )
937 vlc_mutex_lock( &p_input->input.p_item->lock );
938 for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
940 info_category_t *p_category = p_input->input.p_item->pp_categories[i];
941 mvar_t *cat = mvar_New( name, "set" );
942 mvar_t *iset = mvar_New( "info", "set" );
944 mvar_AppendNewVar( cat, "name", p_category->psz_name );
945 mvar_AppendVar( cat, iset );
947 for ( j = 0; j < p_category->i_infos; j++ )
949 info_t *p_info = p_category->pp_infos[j];
950 mvar_t *info = mvar_New( "info", "" );
952 msg_Dbg( p_input, "adding info name=%s value=%s",
953 p_info->psz_name, p_info->psz_value );
954 mvar_AppendNewVar( info, "name", p_info->psz_name );
955 mvar_AppendNewVar( info, "value", p_info->psz_value );
956 mvar_AppendVar( iset, info );
958 mvar_AppendVar( s, cat );
960 vlc_mutex_unlock( &p_input->input.p_item->lock );
965 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
967 mvar_t *s = mvar_New( name, "set" );
971 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
972 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
974 for( i= 0; i < info.i_count; )
978 inf = mvar_New( name, "set" );
981 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
982 info.info[i].psz_name, info.info[i].psz_value ); */
983 mvar_AppendNewVar( inf,
984 info.info[i].psz_name,
985 info.info[i].psz_value );
987 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
988 mvar_AppendVar( s, inf );
993 for( i = 0; i < info.i_count; i++ )
995 free( info.info[i].psz_name );
996 free( info.info[i].psz_value );
998 if( info.i_count > 0 )
1007 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1009 mvar_t *s = mvar_New( name, "set" );
1010 char tmp[MAX_DIR_SIZE], *p, *src;
1011 #ifdef HAVE_SYS_STAT_H
1012 struct stat stat_info;
1015 struct dirent *p_dir_content;
1018 /* convert all / to native separator */
1019 #if defined( WIN32 )
1020 while( (p = strchr( psz_dir, '/' )) )
1029 /* remove trailling separator */
1030 while( strlen( psz_dir ) > 1 &&
1031 #if defined( WIN32 )
1032 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1034 psz_dir[strlen( psz_dir ) -1 ] == sep )
1036 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1038 /* remove double separator */
1039 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1041 if( src[0] == sep && src[1] == sep )
1049 if( *psz_dir == '\0' )
1053 /* first fix all .. dir */
1057 if( src[0] == '.' && src[1] == '.' )
1060 if( p <= &psz_dir[1] )
1067 while( p > &psz_dir[1] && *p != sep )
1072 else if( *src == sep )
1074 if( p > psz_dir && p[-1] == sep )
1088 } while( *src && *src != sep );
1093 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1095 #ifdef HAVE_SYS_STAT_H
1096 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1102 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1104 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1108 /* remove traling / or \ */
1109 for( p = &psz_dir[strlen( psz_dir) - 1];
1110 p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1119 /* parse psz_src dir */
1120 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1124 if( !strcmp( p_dir_content->d_name, "." ) )
1129 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1131 #ifdef HAVE_SYS_STAT_H
1132 if( stat( tmp, &stat_info ) == -1 )
1137 f = mvar_New( name, "set" );
1138 mvar_AppendNewVar( f, "name", tmp );
1140 #ifdef HAVE_SYS_STAT_H
1141 if( S_ISDIR( stat_info.st_mode ) )
1143 mvar_AppendNewVar( f, "type", "directory" );
1145 else if( S_ISREG( stat_info.st_mode ) )
1147 mvar_AppendNewVar( f, "type", "file" );
1151 mvar_AppendNewVar( f, "type", "unknown" );
1154 sprintf( tmp, I64Fd, (int64_t)stat_info.st_size );
1155 mvar_AppendNewVar( f, "size", tmp );
1157 /* FIXME memory leak FIXME */
1159 ctime_r( &stat_info.st_mtime, tmp );
1160 mvar_AppendNewVar( f, "date", tmp );
1162 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1166 mvar_AppendNewVar( f, "type", "unknown" );
1167 mvar_AppendNewVar( f, "size", "unknown" );
1168 mvar_AppendNewVar( f, "date", "unknown" );
1170 mvar_AppendVar( s, f );
1176 static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
1178 mvar_t *s = mvar_New( name, "set" );
1182 /* fprintf( stderr," mvar_VlmSetNew: name=`%s'\n", name ); */
1183 if( vlm == NULL ) return s;
1185 if( vlm_ExecuteCommand( vlm, "show", &msg ) )
1190 for( i = 0; i < msg->i_child; i++ )
1192 /* Over media, schedule */
1193 vlm_message_t *ch = msg->child[i];
1196 for( j = 0; j < ch->i_child; j++ )
1199 vlm_message_t *el = ch->child[j];
1200 vlm_message_t *inf, *desc;
1205 sprintf( psz, "show %s", el->psz_name );
1206 if( vlm_ExecuteCommand( vlm, psz, &inf ) )
1208 desc = inf->child[0];
1210 /* Add a node with name and info */
1211 set = mvar_New( name, "set" );
1212 mvar_AppendNewVar( set, "name", el->psz_name );
1214 /* fprintf( stderr, "#### name=%s\n", el->psz_name ); */
1216 for( k = 0; k < desc->i_child; k++ )
1218 vlm_message_t *ch = desc->child[k];
1219 if( ch->i_child > 0 )
1222 mvar_t *n = mvar_New( ch->psz_name, "set" );
1224 /* fprintf( stderr, " child=%s [%d]\n", ch->psz_name, ch->i_child ); */
1225 for( c = 0; c < ch->i_child; c++ )
1227 mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
1228 mvar_AppendVar( n, in );
1230 /* fprintf( stderr, " sub=%s\n", ch->child[c]->psz_name );*/
1232 mvar_AppendVar( set, n );
1236 /* fprintf( stderr, " child=%s->%s\n", ch->psz_name, ch->psz_value ); */
1237 mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
1240 vlm_MessageDelete( inf );
1242 mvar_AppendVar( s, set );
1245 vlm_MessageDelete( msg );
1251 static void SSInit( rpn_stack_t * );
1252 static void SSClean( rpn_stack_t * );
1253 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1255 static void SSPush ( rpn_stack_t *, char * );
1256 static char *SSPop ( rpn_stack_t * );
1258 static void SSPushN ( rpn_stack_t *, int );
1259 static int SSPopN ( rpn_stack_t *, mvar_t * );
1262 /****************************************************************************
1264 ****************************************************************************/
1272 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1276 /* just load the file */
1278 *pp_data = malloc( 1025 ); /* +1 for \0 */
1279 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1282 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1288 (*pp_data)[*pi_data] = '\0';
1293 static int MacroParse( macro_t *m, uint8_t *psz_src )
1295 uint8_t *dup = strdup( psz_src );
1300 #define EXTRACT( name, l ) \
1302 p = strchr( src, '"' ); \
1307 m->name = strdup( src ); \
1328 if( !strncmp( src, "id=\"", 4 ) )
1332 else if( !strncmp( src, "param1=\"", 8 ) )
1334 EXTRACT( param1, 8 );
1336 else if( !strncmp( src, "param2=\"", 8 ) )
1338 EXTRACT( param2, 8 );
1345 if( strstr( src, "/>" ) )
1347 src = strstr( src, "/>" ) + 2;
1351 src += strlen( src );
1356 m->id = strdup( "" );
1358 if( m->param1 == NULL )
1360 m->param1 = strdup( "" );
1362 if( m->param2 == NULL )
1364 m->param2 = strdup( "" );
1373 static void MacroClean( macro_t *m )
1431 StrToMacroTypeTab [] =
1433 { "control", MVLC_CONTROL },
1434 /* player control */
1435 { "play", MVLC_PLAY },
1436 { "stop", MVLC_STOP },
1437 { "pause", MVLC_PAUSE },
1438 { "next", MVLC_NEXT },
1439 { "previous", MVLC_PREVIOUS },
1440 { "seek", MVLC_SEEK },
1441 { "keep", MVLC_KEEP },
1442 { "fullscreen", MVLC_FULLSCREEN },
1443 { "volume", MVLC_VOLUME },
1445 /* playlist management */
1446 { "add", MVLC_ADD },
1447 { "delete", MVLC_DEL },
1448 { "empty", MVLC_EMPTY },
1449 { "sort", MVLC_SORT },
1450 { "move", MVLC_MOVE },
1453 { "close", MVLC_CLOSE },
1454 { "shutdown", MVLC_SHUTDOWN },
1457 { "vlm_new", MVLC_VLM_NEW },
1458 { "vlm_setup", MVLC_VLM_SETUP },
1459 { "vlm_del", MVLC_VLM_DEL },
1460 { "vlm_play", MVLC_VLM_PLAY },
1461 { "vlm_pause", MVLC_VLM_PAUSE },
1462 { "vlm_stop", MVLC_VLM_STOP },
1463 { "vlm_seek", MVLC_VLM_SEEK },
1464 { "vlm_load", MVLC_VLM_LOAD },
1465 { "vlm_save", MVLC_VLM_SAVE },
1467 { "rpn", MVLC_RPN },
1469 { "foreach", MVLC_FOREACH },
1470 { "value", MVLC_VALUE },
1473 { "else", MVLC_ELSE },
1474 { "end", MVLC_END },
1475 { "get", MVLC_GET },
1476 { "set", MVLC_SET },
1477 { "int", MVLC_INT },
1478 { "float", MVLC_FLOAT },
1479 { "string", MVLC_STRING },
1482 { NULL, MVLC_UNKNOWN }
1485 static int StrToMacroType( char *name )
1489 if( !name || *name == '\0')
1491 return MVLC_UNKNOWN;
1493 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1495 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1497 return StrToMacroTypeTab[i].i_type;
1500 return MVLC_UNKNOWN;
1503 static void MacroDo( httpd_file_sys_t *p_args,
1505 uint8_t *p_request, int i_request,
1506 uint8_t **pp_data, int *pi_data,
1509 intf_thread_t *p_intf = p_args->p_intf;
1510 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1513 #define ALLOC( l ) \
1515 int __i__ = *pp_dst - *pp_data; \
1517 *pp_data = realloc( *pp_data, *pi_data ); \
1518 *pp_dst = (*pp_data) + __i__; \
1520 #define PRINT( str ) \
1521 ALLOC( strlen( str ) + 1 ); \
1522 *pp_dst += sprintf( *pp_dst, str );
1524 #define PRINTS( str, s ) \
1525 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1527 char * psz_cur = *pp_dst; \
1528 *pp_dst += sprintf( *pp_dst, str, s ); \
1529 while( psz_cur && *psz_cur ) \
1531 /* Prevent script injection */ \
1532 if( *psz_cur == '<' ) *psz_cur = '*'; \
1533 if( *psz_cur == '>' ) *psz_cur = '*'; \
1538 switch( StrToMacroType( m->id ) )
1541 if( i_request <= 0 )
1545 uri_extract_value( p_request, "control", control, 512 );
1546 if( *m->param1 && !strstr( m->param1, control ) )
1548 msg_Warn( p_intf, "unauthorized control=%s", control );
1551 switch( StrToMacroType( control ) )
1558 uri_extract_value( p_request, "item", item, 512 );
1559 i_item = atoi( item );
1560 playlist_Control( p_sys->p_playlist, PLAYLIST_GOTO,
1562 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1566 playlist_Control( p_sys->p_playlist, PLAYLIST_STOP);
1567 msg_Dbg( p_intf, "requested playlist stop" );
1570 playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE );
1571 msg_Dbg( p_intf, "requested playlist pause" );
1574 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, 1 );
1575 msg_Dbg( p_intf, "requested playlist next" );
1578 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, -1);
1579 msg_Dbg( p_intf, "requested playlist next" );
1581 case MVLC_FULLSCREEN:
1583 if( p_sys->p_input )
1585 vout_thread_t *p_vout;
1586 p_vout = vlc_object_find( p_sys->p_input,
1587 VLC_OBJECT_VOUT, FIND_CHILD );
1591 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1592 vlc_object_release( p_vout );
1593 msg_Dbg( p_intf, "requested fullscreen toggle" );
1607 #define POSITION_ABSOLUTE 12
1608 #define POSITION_REL_FOR 13
1609 #define POSITION_REL_BACK 11
1610 #define TIME_ABSOLUTE 0
1611 #define TIME_REL_FOR 1
1612 #define TIME_REL_BACK -1
1613 if( p_sys->p_input )
1615 uri_extract_value( p_request, "seek_value", value, 20 );
1616 uri_decode_url_encoded( value );
1618 var_Get( p_sys->p_input, "length", &val);
1619 i_length = val.i_time;
1621 while( p_value[0] != '\0' )
1627 i_relative = TIME_REL_FOR;
1633 i_relative = TIME_REL_BACK;
1637 case '0': case '1': case '2': case '3': case '4':
1638 case '5': case '6': case '7': case '8': case '9':
1640 i_stock = strtol( p_value , &p_value , 10 );
1643 case '%': /* for percentage ie position */
1645 i_relative += POSITION_ABSOLUTE;
1653 i_value = 60 * (i_value + i_stock) ;
1658 case 'h': case 'H': /* hours */
1660 i_value += 3600 * i_stock;
1662 /* other characters which are not numbers are not important */
1663 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1669 case 'm': case 'M': case '\'': /* minutes */
1671 i_value += 60 * i_stock;
1674 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1680 case 's': case 'S': case '"': /* seconds */
1684 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1698 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1705 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1706 val.i_time = (uint64_t)( i_value ) * 1000000;
1708 val.i_time = i_length;
1710 var_Set( p_sys->p_input, "time", val );
1711 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1716 var_Get( p_sys->p_input, "time", &val );
1717 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1719 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1722 val.i_time = i_length;
1724 var_Set( p_sys->p_input, "time", val );
1725 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1730 var_Get( p_sys->p_input, "time", &val );
1731 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1736 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1738 var_Set( p_sys->p_input, "time", val );
1739 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1742 case POSITION_ABSOLUTE:
1744 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1745 var_Set( p_sys->p_input, "position", val );
1746 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1749 case POSITION_REL_FOR:
1751 var_Get( p_sys->p_input, "position", &val );
1752 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1753 var_Set( p_sys->p_input, "position", val );
1754 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1757 case POSITION_REL_BACK:
1759 var_Get( p_sys->p_input, "position", &val );
1760 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1761 var_Set( p_sys->p_input, "position", val );
1762 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
1767 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1772 #undef POSITION_ABSOLUTE
1773 #undef POSITION_REL_FOR
1774 #undef POSITION_REL_BACK
1775 #undef TIME_ABSOLUTE
1777 #undef TIME_REL_BACK
1783 audio_volume_t i_volume;
1786 uri_extract_value( p_request, "value", vol, 8 );
1787 aout_VolumeGet( p_intf, &i_volume );
1788 uri_decode_url_encoded( vol );
1792 i_value = atoi( vol + 1 );
1793 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1795 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1796 msg_Dbg( p_intf, "requested volume set: max" );
1799 aout_VolumeSet( p_intf , (i_volume + i_value) );
1800 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1805 i_value = atoi( vol + 1 );
1806 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1808 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1809 msg_Dbg( p_intf, "requested volume set: min" );
1812 aout_VolumeSet( p_intf , (i_volume - i_value) );
1813 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1816 if( strstr(vol, "%") != NULL )
1818 i_value = atoi( vol );
1819 if( (i_value <= 100) && (i_value>=0) ){
1820 aout_VolumeSet( p_intf, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/100+AOUT_VOLUME_MIN);
1821 msg_Dbg( p_intf, "requested volume set: %i%%", atoi( vol ));
1825 i_value = atoi( vol );
1826 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1828 aout_VolumeSet( p_intf , atoi( vol ) );
1829 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1835 /* playlist management */
1839 playlist_item_t * p_item;
1841 uri_extract_value( p_request, "mrl", mrl, 512 );
1842 uri_decode_url_encoded( mrl );
1843 p_item = parse_MRL( p_intf, mrl );
1845 if( !p_item || !p_item->input.psz_uri ||
1846 !*p_item->input.psz_uri )
1848 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1851 playlist_AddItem( p_sys->p_playlist , p_item ,
1852 PLAYLIST_APPEND, PLAYLIST_END );
1853 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1860 int i_item, *p_items = NULL, i_nb_items = 0;
1861 char item[512], *p_parser = p_request;
1863 /* Get the list of items to delete */
1865 uri_extract_value( p_parser, "item", item, 512 )) )
1867 if( !*item ) continue;
1869 i_item = atoi( item );
1870 p_items = realloc( p_items, (i_nb_items + 1) *
1872 p_items[i_nb_items] = i_item;
1876 /* The items need to be deleted from in reversed order */
1880 for( i = 0; i < i_nb_items; i++ )
1883 for( j = 0; j < i_nb_items; j++ )
1885 if( p_items[j] > p_items[i_index] )
1889 playlist_Delete( p_sys->p_playlist,
1891 msg_Dbg( p_intf, "requested playlist delete: %d",
1893 p_items[i_index] = -1;
1897 if( p_items ) free( p_items );
1902 int i_item, *p_items = NULL, i_nb_items = 0;
1903 char item[512], *p_parser = p_request;
1906 /* Get the list of items to keep */
1908 uri_extract_value( p_parser, "item", item, 512 )) )
1910 if( !*item ) continue;
1912 i_item = atoi( item );
1913 p_items = realloc( p_items, (i_nb_items + 1) *
1915 p_items[i_nb_items] = i_item;
1919 /* The items need to be deleted from in reversed order */
1920 for( i = p_sys->p_playlist->i_size - 1; i >= 0 ; i-- )
1922 /* Check if the item is in the keep list */
1923 for( j = 0 ; j < i_nb_items ; j++ )
1925 if( p_items[j] == i ) break;
1927 if( j == i_nb_items )
1929 playlist_Delete( p_sys->p_playlist, i );
1930 msg_Dbg( p_intf, "requested playlist delete: %d",
1935 if( p_items ) free( p_items );
1940 while( p_sys->p_playlist->i_size > 0 )
1942 playlist_Delete( p_sys->p_playlist, 0 );
1944 msg_Dbg( p_intf, "requested playlist empty" );
1953 uri_extract_value( p_request, "type", type, 12 );
1954 uri_extract_value( p_request, "order", order, 2 );
1956 if( order[0] == '0' ) i_order = ORDER_NORMAL;
1957 else i_order = ORDER_REVERSE;
1959 if( !strcmp( type , "title" ) )
1961 playlist_SortTitle( p_sys->p_playlist , i_order );
1962 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
1963 } else if( !strcmp( type , "author" ) )
1965 playlist_SortAuthor( p_sys->p_playlist , i_order );
1966 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
1967 } else if( !strcmp( type , "shuffle" ) )
1969 playlist_Sort( p_sys->p_playlist , SORT_RANDOM, ORDER_NORMAL );
1970 msg_Dbg( p_intf, "requested playlist shuffle");
1981 uri_extract_value( p_request, "psz_pos", psz_pos, 6 );
1982 uri_extract_value( p_request, "psz_newpos", psz_newpos, 6 );
1983 i_pos = atoi( psz_pos );
1984 i_newpos = atoi( psz_newpos );
1985 if ( i_pos < i_newpos )
1987 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
1989 playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
1991 msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
1995 /* admin function */
1999 uri_extract_value( p_request, "id", id, 512 );
2000 msg_Dbg( p_intf, "requested close id=%s", id );
2002 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
2004 msg_Warn( p_intf, "close failed for id=%s", id );
2011 msg_Dbg( p_intf, "requested shutdown" );
2012 p_intf->p_vlc->b_die = VLC_TRUE;
2017 case MVLC_VLM_SETUP:
2019 static const char *vlm_properties[11] =
2022 "enabled", "disabled", "loop", "unloop",
2024 "input", "output", "option", "date", "period", "repeat", "append",
2026 vlm_message_t *vlm_answer;
2028 char *psz = malloc( strlen( p_request ) + 1000 );
2033 if( p_intf->p_sys->p_vlm == NULL )
2034 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2036 if( p_intf->p_sys->p_vlm == NULL ) break;
2038 uri_extract_value( p_request, "name", name, 512 );
2039 if( StrToMacroType( control ) == MVLC_VLM_NEW )
2042 uri_extract_value( p_request, "type", type, 20 );
2043 p += sprintf( psz, "new %s %s", name, type );
2047 p += sprintf( psz, "setup %s", name );
2049 /* Parse the request */
2050 for( i = 0; i < 11; i++ )
2053 uri_extract_value( p_request, vlm_properties[i], val, 512 );
2054 uri_decode_url_encoded( val );
2055 if( strlen( val ) > 0 && i >= 4 )
2057 p += sprintf( p, " %s %s", vlm_properties[i], val );
2059 else if( uri_test_param( p_request, vlm_properties[i] ) && i < 4 )
2061 p += sprintf( p, " %s", vlm_properties[i] );
2064 fprintf( stderr, "vlm_ExecuteCommand: %s\n", psz );
2065 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2066 if( vlm_answer->psz_value == NULL ) /* there is no error */
2068 vlm_error = strdup( "" );
2072 vlm_error = malloc( strlen(vlm_answer->psz_name) +
2073 strlen(vlm_answer->psz_value) +
2074 strlen( " : ") + 1 );
2075 sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
2076 vlm_answer->psz_value );
2079 mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
2081 vlm_MessageDelete( vlm_answer );
2089 vlm_message_t *vlm_answer;
2092 if( p_intf->p_sys->p_vlm == NULL )
2093 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2095 if( p_intf->p_sys->p_vlm == NULL ) break;
2097 uri_extract_value( p_request, "name", name, 512 );
2098 sprintf( psz, "del %s", name );
2100 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2101 /* FIXME do a vlm_answer -> var stack conversion */
2102 vlm_MessageDelete( vlm_answer );
2107 case MVLC_VLM_PAUSE:
2111 vlm_message_t *vlm_answer;
2114 if( p_intf->p_sys->p_vlm == NULL )
2115 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2117 if( p_intf->p_sys->p_vlm == NULL ) break;
2119 uri_extract_value( p_request, "name", name, 512 );
2120 if( StrToMacroType( control ) == MVLC_VLM_PLAY )
2121 sprintf( psz, "control %s play", name );
2122 else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
2123 sprintf( psz, "control %s pause", name );
2124 else if( StrToMacroType( control ) == MVLC_VLM_STOP )
2125 sprintf( psz, "control %s stop", name );
2126 else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
2129 uri_extract_value( p_request, "percent", percent, 512 );
2130 sprintf( psz, "control %s seek %s", name, percent );
2133 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2134 /* FIXME do a vlm_answer -> var stack conversion */
2135 vlm_MessageDelete( vlm_answer );
2141 vlm_message_t *vlm_answer;
2145 if( p_intf->p_sys->p_vlm == NULL )
2146 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2148 if( p_intf->p_sys->p_vlm == NULL ) break;
2150 uri_extract_value( p_request, "file", file, 512 );
2151 uri_decode_url_encoded( file );
2153 if( StrToMacroType( control ) == MVLC_VLM_LOAD )
2154 sprintf( psz, "load %s", file );
2156 sprintf( psz, "save %s", file );
2158 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2159 /* FIXME do a vlm_answer -> var stack conversion */
2160 vlm_MessageDelete( vlm_answer );
2167 PRINTS( "<!-- control param(%s) unsuported -->", control );
2179 if( i_request <= 0 ||
2180 *m->param1 == '\0' ||
2181 strstr( p_request, m->param1 ) == NULL )
2185 uri_extract_value( p_request, m->param1, value, 512 );
2186 uri_decode_url_encoded( value );
2188 switch( StrToMacroType( m->param2 ) )
2192 config_PutInt( p_intf, m->param1, i );
2196 config_PutFloat( p_intf, m->param1, f );
2199 config_PutPsz( p_intf, m->param1, value );
2202 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
2213 if( *m->param1 == '\0' )
2218 switch( StrToMacroType( m->param2 ) )
2221 i = config_GetInt( p_intf, m->param1 );
2222 sprintf( value, "%i", i );
2225 f = config_GetFloat( p_intf, m->param1 );
2226 sprintf( value, "%f", f );
2229 psz = config_GetPsz( p_intf, m->param1 );
2230 sprintf( value, "%s", psz ? psz : "" );
2231 if( psz ) free( psz );
2234 sprintf( value, "invalid type(%s) in set", m->param2 );
2237 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
2238 PRINTS( "%s", value );
2247 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2248 s = SSPop( &p_args->stack );
2249 v = mvar_GetValue( p_args->vars, s );
2253 v = s = SSPop( &p_args->stack );
2261 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2266 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2267 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2275 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
2282 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
2287 i_skip = MacroParse( &m, src );
2289 i_id = StrToMacroType( m.id );
2306 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2307 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2309 return src + ( b_after ? i_skip : 0 );
2311 else if( i_level < 0 )
2327 static void Execute( httpd_file_sys_t *p_args,
2328 uint8_t *p_request, int i_request,
2329 uint8_t **pp_data, int *pi_data,
2331 uint8_t *_src, uint8_t *_end )
2333 intf_thread_t *p_intf = p_args->p_intf;
2335 uint8_t *src, *dup, *end;
2336 uint8_t *dst = *pp_dst;
2338 src = dup = malloc( _end - _src + 1 );
2339 end = src +( _end - _src );
2341 memcpy( src, _src, _end - _src );
2344 /* we parse searching <vlc */
2350 p = strstr( src, "<vlc" );
2351 if( p < end && p == src )
2355 src += MacroParse( &m, src );
2357 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2359 switch( StrToMacroType( m.id ) )
2366 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2367 if( SSPopN( &p_args->stack, p_args->vars ) )
2375 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2379 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2383 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2386 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2390 else if( i_test == 1 )
2393 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2395 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2399 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2408 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2409 uint8_t *start = src;
2410 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2417 if( !strcmp( m.param2, "integer" ) )
2419 char *arg = SSPop( &p_args->stack );
2420 index = mvar_IntegerSetNew( m.param1, arg );
2423 else if( !strcmp( m.param2, "directory" ) )
2425 char *arg = SSPop( &p_args->stack );
2426 index = mvar_FileSetNew( m.param1, arg );
2429 else if( !strcmp( m.param2, "playlist" ) )
2431 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2433 else if( !strcmp( m.param2, "information" ) )
2435 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2437 else if( !strcmp( m.param2, "vlm" ) )
2439 if( p_intf->p_sys->p_vlm == NULL )
2440 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2441 index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
2444 else if( !strcmp( m.param2, "hosts" ) )
2446 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2448 else if( !strcmp( m.param2, "urls" ) )
2450 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2452 else if( !strcmp( m.param2, "connections" ) )
2454 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2457 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2459 index = mvar_Duplicate( v );
2463 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2468 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2470 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2472 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2475 f->name = strdup( m.param1 );
2478 mvar_PushVar( p_args->vars, f );
2479 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2480 mvar_RemoveVar( p_args->vars, f );
2484 mvar_Delete( index );
2491 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2499 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2502 int i_index = dst - *pp_data;
2505 *pp_data = realloc( *pp_data, *pi_data );
2506 dst = (*pp_data) + i_index;
2508 memcpy( dst, src, i_copy );
2518 /****************************************************************************
2520 ****************************************************************************
2521 * a file with b_html is parsed and all "macro" replaced
2522 * <vlc id="macro name" [param1="" [param2=""]] />
2525 ****************************************************************************/
2526 static int HttpCallback( httpd_file_sys_t *p_args,
2527 httpd_file_t *p_file,
2529 uint8_t **pp_data, int *pi_data )
2531 int i_request = p_request ? strlen( p_request ) : 0;
2535 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2537 p = *pp_data = malloc( 10240 );
2540 return VLC_EGENERIC;
2542 p += sprintf( p, "<html>\n" );
2543 p += sprintf( p, "<head>\n" );
2544 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2545 p += sprintf( p, "</head>\n" );
2546 p += sprintf( p, "<body>\n" );
2547 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2548 p += sprintf( p, "<hr />\n" );
2549 p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
2550 p += sprintf( p, "</body>\n" );
2551 p += sprintf( p, "</html>\n" );
2553 *pi_data = strlen( *pp_data );
2558 if( !p_args->b_html )
2560 FileLoad( f, pp_data, pi_data );
2568 char position[4]; /* percentage */
2569 char time[12]; /* in seconds */
2570 char length[12]; /* in seconds */
2571 audio_volume_t i_volume;
2575 #define p_sys p_args->p_intf->p_sys
2576 if( p_sys->p_input )
2578 var_Get( p_sys->p_input, "position", &val);
2579 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2580 var_Get( p_sys->p_input, "time", &val);
2581 sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2582 var_Get( p_sys->p_input, "length", &val);
2583 sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2585 var_Get( p_sys->p_input, "state", &val );
2586 if( val.i_int == PLAYING_S )
2588 sprintf( state, "playing" );
2589 } else if( val.i_int == PAUSE_S )
2591 sprintf( state, "paused" );
2594 sprintf( state, "stop" );
2598 sprintf( position, "%d", 0 );
2599 sprintf( time, "%d", 0 );
2600 sprintf( length, "%d", 0 );
2601 sprintf( state, "stop" );
2605 aout_VolumeGet( p_args->p_intf , &i_volume );
2606 sprintf( volume , "%d" , (int)i_volume );
2608 p_args->vars = mvar_New( "variables", "" );
2609 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2610 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2611 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
2612 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2613 mvar_AppendNewVar( p_args->vars, "stream_position", position );
2614 mvar_AppendNewVar( p_args->vars, "stream_time", time );
2615 mvar_AppendNewVar( p_args->vars, "stream_length", length );
2616 mvar_AppendNewVar( p_args->vars, "volume", volume );
2617 mvar_AppendNewVar( p_args->vars, "stream_state", state );
2619 SSInit( &p_args->stack );
2621 /* first we load in a temporary buffer */
2622 FileLoad( f, &p_buffer, &i_buffer );
2624 /* allocate output */
2625 *pi_data = i_buffer + 1000;
2626 dst = *pp_data = malloc( *pi_data );
2628 /* we parse executing all <vlc /> macros */
2629 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2632 *pi_data = dst - *pp_data;
2634 SSClean( &p_args->stack );
2635 mvar_Delete( p_args->vars );
2644 /****************************************************************************
2646 ****************************************************************************/
2647 static int uri_test_param( char *psz_uri, const char *psz_name )
2651 while( (p = strstr( p, psz_name )) )
2653 /* Verify that we are dealing with a post/get argument */
2654 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2663 static char *uri_extract_value( char *psz_uri, const char *psz_name,
2664 char *psz_value, int i_value_max )
2668 while( (p = strstr( p, psz_name )) )
2670 /* Verify that we are dealing with a post/get argument */
2671 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2680 p += strlen( psz_name );
2681 if( *p == '=' ) p++;
2683 if( strchr( p, '&' ) )
2685 i_len = strchr( p, '&' ) - p;
2689 /* for POST method */
2690 if( strchr( p, '\n' ) )
2692 i_len = strchr( p, '\n' ) - p;
2693 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2697 i_len = strlen( p );
2700 i_len = __MIN( i_value_max - 1, i_len );
2703 strncpy( psz_value, p, i_len );
2704 psz_value[i_len] = '\0';
2708 strncpy( psz_value, "", i_value_max );
2714 strncpy( psz_value, "", i_value_max );
2720 static void uri_decode_url_encoded( char *psz )
2722 char *dup = strdup( psz );
2740 *psz++ = strtol( val, NULL, 16 );
2742 else if( *p == '+' )
2756 /****************************************************************************
2757 * Light RPN evaluator
2758 ****************************************************************************/
2759 static void SSInit( rpn_stack_t *st )
2764 static void SSClean( rpn_stack_t *st )
2766 while( st->i_stack > 0 )
2768 free( st->stack[--st->i_stack] );
2772 static void SSPush( rpn_stack_t *st, char *s )
2774 if( st->i_stack < STACK_MAX )
2776 st->stack[st->i_stack++] = strdup( s );
2780 static char * SSPop( rpn_stack_t *st )
2782 if( st->i_stack <= 0 )
2784 return strdup( "" );
2788 return st->stack[--st->i_stack];
2792 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2801 i = strtol( name, &end, 0 );
2804 value = mvar_GetValue( vars, name );
2812 static void SSPushN( rpn_stack_t *st, int i )
2816 sprintf( v, "%d", i );
2820 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2827 while( *exp == ' ' )
2834 /* extract string */
2837 while( *exp && *exp != '\'' )
2848 p = strchr( exp, ' ' );
2853 exp += strlen( exp );
2858 strncpy( s, exp, i );
2869 /* 1. Integer function */
2870 if( !strcmp( s, "!" ) )
2872 SSPushN( st, ~SSPopN( st, vars ) );
2874 else if( !strcmp( s, "^" ) )
2876 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2878 else if( !strcmp( s, "&" ) )
2880 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2882 else if( !strcmp( s, "|" ) )
2884 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2886 else if( !strcmp( s, "+" ) )
2888 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2890 else if( !strcmp( s, "-" ) )
2892 int j = SSPopN( st, vars );
2893 int i = SSPopN( st, vars );
2894 SSPushN( st, i - j );
2896 else if( !strcmp( s, "*" ) )
2898 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2900 else if( !strcmp( s, "/" ) )
2904 j = SSPopN( st, vars );
2905 i = SSPopN( st, vars );
2907 SSPushN( st, j != 0 ? i / j : 0 );
2909 else if( !strcmp( s, "%" ) )
2913 j = SSPopN( st, vars );
2914 i = SSPopN( st, vars );
2916 SSPushN( st, j != 0 ? i % j : 0 );
2918 /* 2. integer tests */
2919 else if( !strcmp( s, "=" ) )
2921 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2923 else if( !strcmp( s, "<" ) )
2925 int j = SSPopN( st, vars );
2926 int i = SSPopN( st, vars );
2928 SSPushN( st, i < j ? -1 : 0 );
2930 else if( !strcmp( s, ">" ) )
2932 int j = SSPopN( st, vars );
2933 int i = SSPopN( st, vars );
2935 SSPushN( st, i > j ? -1 : 0 );
2937 else if( !strcmp( s, "<=" ) )
2939 int j = SSPopN( st, vars );
2940 int i = SSPopN( st, vars );
2942 SSPushN( st, i <= j ? -1 : 0 );
2944 else if( !strcmp( s, ">=" ) )
2946 int j = SSPopN( st, vars );
2947 int i = SSPopN( st, vars );
2949 SSPushN( st, i >= j ? -1 : 0 );
2951 /* 3. string functions */
2952 else if( !strcmp( s, "strcat" ) )
2954 char *s2 = SSPop( st );
2955 char *s1 = SSPop( st );
2956 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2966 else if( !strcmp( s, "strcmp" ) )
2968 char *s2 = SSPop( st );
2969 char *s1 = SSPop( st );
2971 SSPushN( st, strcmp( s1, s2 ) );
2975 else if( !strcmp( s, "strncmp" ) )
2977 int n = SSPopN( st, vars );
2978 char *s2 = SSPop( st );
2979 char *s1 = SSPop( st );
2981 SSPushN( st, strncmp( s1, s2 , n ) );
2985 else if( !strcmp( s, "strsub" ) )
2987 int n = SSPopN( st, vars );
2988 int m = SSPopN( st, vars );
2990 char *s = SSPop( st );
3002 str = malloc( i_len + 1 );
3004 memcpy( str, s + m - 1, i_len );
3005 str[ i_len ] = '\0';
3011 else if( !strcmp( s, "strlen" ) )
3013 char *str = SSPop( st );
3015 SSPushN( st, strlen( str ) );
3018 /* 4. stack functions */
3019 else if( !strcmp( s, "dup" ) )
3021 char *str = SSPop( st );
3026 else if( !strcmp( s, "drop" ) )
3028 char *str = SSPop( st );
3031 else if( !strcmp( s, "swap" ) )
3033 char *s1 = SSPop( st );
3034 char *s2 = SSPop( st );
3041 else if( !strcmp( s, "flush" ) )
3046 else if( !strcmp( s, "store" ) )
3048 char *value = SSPop( st );
3049 char *name = SSPop( st );
3051 mvar_PushNewVar( vars, name, value );
3055 else if( !strcmp( s, "value" ) )
3057 char *name = SSPop( st );
3058 char *value = mvar_GetValue( vars, name );
3060 SSPush( st, value );
3064 else if( !strcmp( s, "url_extract" ) )
3066 char *url = mvar_GetValue( vars, "url_value" );
3067 char *name = SSPop( st );
3070 uri_extract_value( url, name, value, 512 );
3071 uri_decode_url_encoded( value );
3072 SSPush( st, value );
3081 /**********************************************************************
3082 * Find_end_MRL: Find the end of the sentence :
3083 * this function parses the string psz and find the end of the item
3084 * and/or option with detecting the " and ' problems.
3085 * returns NULL if an error is detected, otherwise, returns a pointer
3086 * of the end of the sentence (after the last character)
3087 **********************************************************************/
3088 static char *Find_end_MRL( char *psz )
3098 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
3100 if( *s_sent == '\'' )
3102 s_sent = Find_end_MRL( s_sent );
3104 if( s_sent == NULL )
3114 if( *s_sent == '\"' )
3118 } else /* *s_sent == '\0' , which means the number of " is incorrect */
3128 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
3130 if( *s_sent == '\"' )
3132 s_sent = Find_end_MRL( s_sent );
3134 if( s_sent == NULL )
3144 if( *s_sent == '\'' )
3148 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
3154 default: /* now we can look for spaces */
3156 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
3158 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
3160 s_sent = Find_end_MRL( s_sent );
3171 /**********************************************************************
3172 * parse_MRL: parse the MRL, find the mrl string and the options,
3173 * create an item with all information in it, and return the item.
3174 * return NULL if there is an error.
3175 **********************************************************************/
3176 playlist_item_t * parse_MRL( intf_thread_t *p_intf, char *psz )
3178 char **ppsz_options = NULL;
3185 playlist_item_t * p_item = NULL;
3187 /* In case there is spaces before the mrl */
3188 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
3193 /* extract the mrl */
3194 s_temp = strstr( s_mrl , " :" );
3195 if( s_temp == NULL )
3197 s_temp = s_mrl + strlen( s_mrl );
3200 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
3207 /* if the mrl is between " or ', we must remove them */
3208 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
3210 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
3211 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
3212 mrl[ s_temp - s_mrl - 2 ] = '\0';
3215 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
3216 strncpy( mrl , s_mrl , s_temp - s_mrl );
3217 mrl[ s_temp - s_mrl ] = '\0';
3222 /* now we can take care of the options */
3223 while( (*s_mrl != '\0') && (i_error == 0) )
3232 case ':': /* an option */
3234 s_temp = Find_end_MRL( s_mrl );
3236 if( s_temp == NULL )
3243 ppsz_options = realloc( ppsz_options , i_options *
3245 ppsz_options[ i_options - 1 ] =
3246 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
3248 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
3251 /* don't forget to finish the string with a '\0' */
3252 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
3272 /* now create an item */
3273 p_item = playlist_ItemNew( p_intf, mrl, mrl);
3274 for( i = 0 ; i< i_options ; i++ )
3276 playlist_ItemAddOption( p_item, ppsz_options[i] );
3280 for( i = 0 ; i < i_options ; i++ )
3282 free( ppsz_options[i] );
3284 free( ppsz_options );