1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
38 #include <vlc/vout.h> /* for fullscreen */
40 #include "vlc_httpd.h"
44 #ifdef HAVE_SYS_STAT_H
45 # include <sys/stat.h>
56 #elif defined( WIN32 ) && !defined( UNDER_CE )
60 #if (!defined( WIN32 ) || defined(__MINGW32__))
61 /* Mingw has its own version of dirent */
65 /* stat() support for large files on win32 */
66 #if defined( WIN32 ) && !defined( UNDER_CE )
67 # define stat _stati64
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Open ( vlc_object_t * );
74 static void Close( vlc_object_t * );
76 #define HOST_TEXT N_( "Host address" )
77 #define HOST_LONGTEXT N_( \
78 "You can set the address and port the http interface will bind to." )
79 #define SRC_TEXT N_( "Source directory" )
80 #define SRC_LONGTEXT N_( "Source directory" )
81 #define CERT_TEXT N_( "Certificate file" )
82 #define CERT_LONGTEXT N_( "HTTP interface x509 PEM certificate file " \
84 #define KEY_TEXT N_( "Private key file" )
85 #define KEY_LONGTEXT N_( "HTTP interface x509 PEM private key file" )
86 #define CA_TEXT N_( "Root CA file" )
87 #define CA_LONGTEXT N_( "HTTP interface x509 PEM trusted root CA " \
89 #define CRL_TEXT N_( "CRL file" )
90 #define CRL_LONGTEXT N_( "HTTP interace Certificates Revocation List file" )
93 set_description( _("HTTP remote control interface") );
94 set_category( CAT_INTERFACE );
95 set_subcategory( SUBCAT_INTERFACE_CONTROL );
96 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
97 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
98 add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
99 add_string ( "http-intf-key", NULL, NULL, KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
100 add_string ( "http-intf-ca", NULL, NULL, CA_TEXT, CA_LONGTEXT, VLC_TRUE );
101 add_string ( "http-intf-crl", NULL, NULL, CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
102 set_capability( "interface", 0 );
103 set_callbacks( Open, Close );
107 /*****************************************************************************
109 *****************************************************************************/
110 static void Run ( intf_thread_t *p_intf );
112 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
115 static int DirectoryCheck( char *psz_dir )
119 #ifdef HAVE_SYS_STAT_H
120 struct stat stat_info;
122 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
128 if( ( p_dir = opendir( psz_dir ) ) == NULL )
137 static int HttpCallback( httpd_file_sys_t *p_args,
140 uint8_t **pp_data, int *pi_data );
142 static char *uri_extract_value( char *psz_uri, const char *psz_name,
143 char *psz_value, int i_value_max );
144 static int uri_test_param( char *psz_uri, const char *psz_name );
146 static void uri_decode_url_encoded( char *psz );
148 static char *Find_end_MRL( char *psz );
150 static playlist_item_t * parse_MRL( intf_thread_t * , char *psz );
152 /*****************************************************************************
154 *****************************************************************************/
155 typedef struct mvar_s
161 struct mvar_s **field;
164 #define STACK_MAX 100
167 char *stack[STACK_MAX];
171 struct httpd_file_sys_t
173 intf_thread_t *p_intf;
174 httpd_file_t *p_file;
175 httpd_redirect_t *p_redir;
182 /* inited for each access */
189 httpd_host_t *p_httpd_host;
192 httpd_file_sys_t **pp_files;
194 playlist_t *p_playlist;
195 input_thread_t *p_input;
201 /*****************************************************************************
202 * Activate: initialize and create stuff
203 *****************************************************************************/
204 static int Open( vlc_object_t *p_this )
206 intf_thread_t *p_intf = (intf_thread_t*)p_this;
209 char *psz_address = "";
210 const char *psz_cert;
215 psz_host = config_GetPsz( p_intf, "http-host" );
219 psz_address = psz_host;
221 psz_parser = strchr( psz_host, ':' );
224 *psz_parser++ = '\0';
225 i_port = atoi( psz_parser );
229 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
232 return( VLC_ENOMEM );
234 p_sys->p_playlist = NULL;
235 p_sys->p_input = NULL;
238 psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
239 if ( psz_cert != NULL )
243 msg_Dbg( p_intf, "enablind TLS for HTTP interface (cert file: %s)",
245 psz_pem = config_GetPsz( p_intf, "http-intf-key" );
247 p_tls = tls_ServerCreate( p_this, psz_cert, psz_pem );
250 msg_Err( p_intf, "TLS initialization error" );
255 psz_pem = config_GetPsz( p_intf, "http-intf-ca" );
256 if ( ( psz_pem != NULL) && tls_ServerAddCA( p_tls, psz_pem ) )
258 msg_Err( p_intf, "TLS CA error" );
259 tls_ServerDelete( p_tls );
264 psz_pem = config_GetPsz( p_intf, "http-intf-crl" );
265 if ( ( psz_pem != NULL) && tls_ServerAddCRL( p_tls, psz_pem ) )
267 msg_Err( p_intf, "TLS CRL error" );
268 tls_ServerDelete( p_tls );
283 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
285 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_intf), psz_address,
287 if( p_sys->p_httpd_host == NULL )
289 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
291 tls_ServerDelete( p_tls );
302 p_sys->pp_files = NULL;
304 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
305 ( defined(WIN32) && !defined(UNDER_CE ) )
306 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
308 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
309 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
315 sprintf( psz_src, "%s/http", psz_vlcpath);
317 sprintf( psz_src, "%s/share/http", psz_vlcpath);
321 psz_src = config_GetPsz( p_intf, "http-src" );
323 if( !psz_src || *psz_src == '\0' )
325 if( !DirectoryCheck( "share/http" ) )
327 psz_src = strdup( "share/http" );
329 else if( !DirectoryCheck( DATA_PATH "/http" ) )
331 psz_src = strdup( DATA_PATH "/http" );
336 if( !psz_src || *psz_src == '\0' )
338 msg_Err( p_intf, "invalid src dir" );
342 /* remove trainling \ or / */
343 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
344 psz_src[strlen( psz_src ) - 1] == '/' )
346 psz_src[strlen( psz_src ) - 1] = '\0';
349 ParseDirectory( p_intf, psz_src, psz_src );
352 if( p_sys->i_files <= 0 )
354 msg_Err( p_intf, "cannot find any files (%s)", psz_src );
357 p_intf->pf_run = Run;
363 if( psz_src ) free( psz_src );
364 if( p_sys->pp_files )
366 free( p_sys->pp_files );
368 httpd_HostDelete( p_sys->p_httpd_host );
373 /*****************************************************************************
374 * CloseIntf: destroy interface
375 *****************************************************************************/
376 void Close ( vlc_object_t *p_this )
378 intf_thread_t *p_intf = (intf_thread_t *)p_this;
379 intf_sys_t *p_sys = p_intf->p_sys;
385 vlm_Delete( p_sys->p_vlm );
387 for( i = 0; i < p_sys->i_files; i++ )
389 httpd_FileDelete( p_sys->pp_files[i]->p_file );
390 if( p_sys->pp_files[i]->p_redir )
392 httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
395 free( p_sys->pp_files[i]->file );
396 free( p_sys->pp_files[i]->name );
397 free( p_sys->pp_files[i] );
399 if( p_sys->pp_files )
401 free( p_sys->pp_files );
403 httpd_HostDelete( p_sys->p_httpd_host );
408 /*****************************************************************************
409 * Run: http interface thread
410 *****************************************************************************/
411 static void Run( intf_thread_t *p_intf )
413 intf_sys_t *p_sys = p_intf->p_sys;
415 while( !p_intf->b_die )
417 /* get the playlist */
418 if( p_sys->p_playlist == NULL )
420 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
423 /* Manage the input part */
424 if( p_sys->p_input == NULL )
426 if( p_sys->p_playlist )
429 vlc_object_find( p_sys->p_playlist,
434 else if( p_sys->p_input->b_dead )
436 vlc_object_release( p_sys->p_input );
437 p_sys->p_input = NULL;
442 msleep( INTF_IDLE_SLEEP );
447 vlc_object_release( p_sys->p_input );
448 p_sys->p_input = NULL;
451 if( p_sys->p_playlist )
453 vlc_object_release( p_sys->p_playlist );
454 p_sys->p_playlist = NULL;
459 /*****************************************************************************
461 *****************************************************************************/
462 #define MAX_DIR_SIZE 10240
464 /****************************************************************************
465 * FileToUrl: create a good name for an url from filename
466 ****************************************************************************/
467 static char *FileToUrl( char *name )
471 url = p = malloc( strlen( name ) + 1 );
479 while( *name == '\\' || *name == '/' )
481 while( *name == '/' )
491 /* convert '\\' into '/' */
504 if( ( p = strrchr( url, '/' ) ) != NULL )
506 if( !strncmp( p, "/index.", 7 ) )
514 /****************************************************************************
515 * ParseDirectory: parse recursively a directory, adding each file
516 ****************************************************************************/
517 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
520 intf_sys_t *p_sys = p_intf->p_sys;
521 char dir[MAX_DIR_SIZE];
522 #ifdef HAVE_SYS_STAT_H
523 struct stat stat_info;
526 struct dirent *p_dir_content;
530 char *password = NULL;
532 #ifdef HAVE_SYS_STAT_H
533 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
539 if( ( p_dir = opendir( psz_dir ) ) == NULL )
541 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
545 msg_Dbg( p_intf, "dir=%s", psz_dir );
547 sprintf( dir, "%s/.access", psz_dir );
548 if( ( file = fopen( dir, "r" ) ) != NULL )
553 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
555 i_size = fread( line, 1, 1023, file );
559 while( i_size > 0 && ( line[i_size-1] == '\n' ||
560 line[i_size-1] == '\r' ) )
567 p = strchr( line, ':' );
571 user = strdup( line );
572 password = strdup( p );
575 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
576 user, password, i_size );
583 /* parse psz_src dir */
584 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
589 if( p_dir_content->d_name[0] == '.' )
593 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
594 if( ParseDirectory( p_intf, psz_root, dir ) )
596 httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
601 f->file = strdup( dir );
602 f->name = FileToUrl( &dir[strlen( psz_root )] );
603 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
607 msg_Err( p_intf , "unable to parse directory" );
610 return( VLC_ENOMEM );
612 msg_Dbg( p_intf, "file=%s (url=%s)",
615 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
616 f->name, f->b_html ? "text/html" : NULL,
622 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
624 /* For rep/ add a redir from rep to rep/ */
625 if( f && f->name[strlen(f->name) - 1] == '/' )
627 char *psz_redir = strdup( f->name );
628 psz_redir[strlen( psz_redir ) - 1] = '\0';
630 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
631 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
651 /****************************************************************************
652 * var and set handling
653 ****************************************************************************/
655 static mvar_t *mvar_New( char *name, char *value )
657 mvar_t *v = malloc( sizeof( mvar_t ) );
659 if( !v ) return NULL;
660 v->name = strdup( name );
661 v->value = strdup( value ? value : "" );
664 v->field = malloc( sizeof( mvar_t * ) );
670 static void mvar_Delete( mvar_t *v )
677 for( i = 0; i < v->i_field; i++ )
679 mvar_Delete( v->field[i] );
685 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
687 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
688 v->field[v->i_field] = f;
692 static mvar_t *mvar_Duplicate( mvar_t *v )
697 n = mvar_New( v->name, v->value );
698 for( i = 0; i < v->i_field; i++ )
700 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
706 static void mvar_PushVar( mvar_t *v, mvar_t *f )
708 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
711 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
717 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
720 for( i = 0; i < v->i_field; i++ )
722 if( v->field[i] == f )
727 if( i >= v->i_field )
732 if( i + 1 < v->i_field )
734 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
737 /* FIXME should do a realloc */
740 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
743 char base[512], *field, *p;
746 /* format: name[index].field */
748 field = strchr( name, '.' );
751 int i = field - name;
752 strncpy( base, name, i );
758 strcpy( base, name );
761 if( ( p = strchr( base, '[' ) ) )
764 sscanf( p, "%d]", &i_index );
775 for( i = 0; i < s->i_field; i++ )
777 if( !strcmp( s->field[i]->name, base ) )
787 return mvar_GetVar( s->field[i], field );
801 static char *mvar_GetValue( mvar_t *v, char *field )
809 mvar_t *f = mvar_GetVar( v, field );
821 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
823 mvar_t *f = mvar_New( name, value );
824 mvar_PushVar( vars, f );
827 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
829 mvar_t *f = mvar_New( name, value );
830 mvar_AppendVar( vars, f );
834 /* arg= start[:stop[:step]],.. */
835 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
837 char *dup = strdup( arg );
839 mvar_t *s = mvar_New( name, "set" );
841 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
846 int i_start,i_stop,i_step;
849 p = strchr( str, ',' );
856 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
857 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
864 else if( i_match == 2 )
866 i_step = i_start < i_stop ? 1 : -1;
873 if( ( i_start < i_stop && i_step > 0 ) ||
874 ( i_start > i_stop && i_step < 0 ) )
876 for( i = i_start; ; i += i_step )
880 if( ( i_step > 0 && i > i_stop ) ||
881 ( i_step < 0 && i < i_stop ) )
886 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
887 sprintf( value, "%d", i );
889 mvar_PushNewVar( s, name, value );
900 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
902 mvar_t *s = mvar_New( name, "set" );
905 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
907 vlc_mutex_lock( &p_pl->object_lock );
908 for( i = 0; i < p_pl->i_size; i++ )
910 mvar_t *itm = mvar_New( name, "set" );
913 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
914 mvar_AppendNewVar( itm, "current", value );
916 sprintf( value, "%d", i );
917 mvar_AppendNewVar( itm, "index", value );
919 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->input.psz_name );
921 mvar_AppendNewVar( itm, "uri", p_pl->pp_items[i]->input.psz_uri );
923 mvar_AppendVar( s, itm );
925 vlc_mutex_unlock( &p_pl->object_lock );
930 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
932 mvar_t *s = mvar_New( name, "set" );
935 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
936 if( p_input == NULL )
941 vlc_mutex_lock( &p_input->input.p_item->lock );
942 for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
944 info_category_t *p_category = p_input->input.p_item->pp_categories[i];
945 mvar_t *cat = mvar_New( name, "set" );
946 mvar_t *iset = mvar_New( "info", "set" );
948 mvar_AppendNewVar( cat, "name", p_category->psz_name );
949 mvar_AppendVar( cat, iset );
951 for ( j = 0; j < p_category->i_infos; j++ )
953 info_t *p_info = p_category->pp_infos[j];
954 mvar_t *info = mvar_New( "info", "" );
956 msg_Dbg( p_input, "adding info name=%s value=%s",
957 p_info->psz_name, p_info->psz_value );
958 mvar_AppendNewVar( info, "name", p_info->psz_name );
959 mvar_AppendNewVar( info, "value", p_info->psz_value );
960 mvar_AppendVar( iset, info );
962 mvar_AppendVar( s, cat );
964 vlc_mutex_unlock( &p_input->input.p_item->lock );
969 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
971 mvar_t *s = mvar_New( name, "set" );
975 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
976 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
978 for( i= 0; i < info.i_count; )
982 inf = mvar_New( name, "set" );
985 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
986 info.info[i].psz_name, info.info[i].psz_value ); */
987 mvar_AppendNewVar( inf,
988 info.info[i].psz_name,
989 info.info[i].psz_value );
991 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
992 mvar_AppendVar( s, inf );
997 for( i = 0; i < info.i_count; i++ )
999 free( info.info[i].psz_name );
1000 free( info.info[i].psz_value );
1002 if( info.i_count > 0 )
1011 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1013 mvar_t *s = mvar_New( name, "set" );
1014 char tmp[MAX_DIR_SIZE], *p, *src;
1015 #ifdef HAVE_SYS_STAT_H
1016 struct stat stat_info;
1019 struct dirent *p_dir_content;
1022 /* convert all / to native separator */
1023 #if defined( WIN32 )
1024 while( (p = strchr( psz_dir, '/' )) )
1033 /* remove trailling separator */
1034 while( strlen( psz_dir ) > 1 &&
1035 #if defined( WIN32 )
1036 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1038 psz_dir[strlen( psz_dir ) -1 ] == sep )
1040 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1042 /* remove double separator */
1043 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1045 if( src[0] == sep && src[1] == sep )
1053 if( *psz_dir == '\0' )
1057 /* first fix all .. dir */
1061 if( src[0] == '.' && src[1] == '.' )
1064 if( p <= &psz_dir[1] )
1071 while( p > &psz_dir[1] && *p != sep )
1076 else if( *src == sep )
1078 if( p > psz_dir && p[-1] == sep )
1092 } while( *src && *src != sep );
1097 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1099 #ifdef HAVE_SYS_STAT_H
1100 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1106 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1108 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1112 /* remove traling / or \ */
1113 for( p = &psz_dir[strlen( psz_dir) - 1];
1114 p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1123 /* parse psz_src dir */
1124 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1128 if( !strcmp( p_dir_content->d_name, "." ) )
1133 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1135 #ifdef HAVE_SYS_STAT_H
1136 if( stat( tmp, &stat_info ) == -1 )
1141 f = mvar_New( name, "set" );
1142 mvar_AppendNewVar( f, "name", tmp );
1144 #ifdef HAVE_SYS_STAT_H
1145 if( S_ISDIR( stat_info.st_mode ) )
1147 mvar_AppendNewVar( f, "type", "directory" );
1149 else if( S_ISREG( stat_info.st_mode ) )
1151 mvar_AppendNewVar( f, "type", "file" );
1155 mvar_AppendNewVar( f, "type", "unknown" );
1158 sprintf( tmp, I64Fd, (int64_t)stat_info.st_size );
1159 mvar_AppendNewVar( f, "size", tmp );
1161 /* FIXME memory leak FIXME */
1163 ctime_r( &stat_info.st_mtime, tmp );
1164 mvar_AppendNewVar( f, "date", tmp );
1166 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1170 mvar_AppendNewVar( f, "type", "unknown" );
1171 mvar_AppendNewVar( f, "size", "unknown" );
1172 mvar_AppendNewVar( f, "date", "unknown" );
1174 mvar_AppendVar( s, f );
1180 static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
1182 mvar_t *s = mvar_New( name, "set" );
1186 /* fprintf( stderr," mvar_VlmSetNew: name=`%s'\n", name ); */
1187 if( vlm == NULL ) return s;
1189 if( vlm_ExecuteCommand( vlm, "show", &msg ) )
1194 for( i = 0; i < msg->i_child; i++ )
1196 /* Over media, schedule */
1197 vlm_message_t *ch = msg->child[i];
1200 for( j = 0; j < ch->i_child; j++ )
1203 vlm_message_t *el = ch->child[j];
1204 vlm_message_t *inf, *desc;
1209 sprintf( psz, "show %s", el->psz_name );
1210 if( vlm_ExecuteCommand( vlm, psz, &inf ) )
1212 desc = inf->child[0];
1214 /* Add a node with name and info */
1215 set = mvar_New( name, "set" );
1216 mvar_AppendNewVar( set, "name", el->psz_name );
1218 /* fprintf( stderr, "#### name=%s\n", el->psz_name ); */
1220 for( k = 0; k < desc->i_child; k++ )
1222 vlm_message_t *ch = desc->child[k];
1223 if( ch->i_child > 0 )
1226 mvar_t *n = mvar_New( ch->psz_name, "set" );
1228 /* fprintf( stderr, " child=%s [%d]\n", ch->psz_name, ch->i_child ); */
1229 for( c = 0; c < ch->i_child; c++ )
1231 mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
1232 mvar_AppendVar( n, in );
1234 /* fprintf( stderr, " sub=%s\n", ch->child[c]->psz_name );*/
1236 mvar_AppendVar( set, n );
1240 /* fprintf( stderr, " child=%s->%s\n", ch->psz_name, ch->psz_value ); */
1241 mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
1244 vlm_MessageDelete( inf );
1246 mvar_AppendVar( s, set );
1249 vlm_MessageDelete( msg );
1255 static void SSInit( rpn_stack_t * );
1256 static void SSClean( rpn_stack_t * );
1257 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1259 static void SSPush ( rpn_stack_t *, char * );
1260 static char *SSPop ( rpn_stack_t * );
1262 static void SSPushN ( rpn_stack_t *, int );
1263 static int SSPopN ( rpn_stack_t *, mvar_t * );
1266 /****************************************************************************
1268 ****************************************************************************/
1276 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1280 /* just load the file */
1282 *pp_data = malloc( 1025 ); /* +1 for \0 */
1283 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1286 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1292 (*pp_data)[*pi_data] = '\0';
1297 static int MacroParse( macro_t *m, uint8_t *psz_src )
1299 uint8_t *dup = strdup( psz_src );
1304 #define EXTRACT( name, l ) \
1306 p = strchr( src, '"' ); \
1311 m->name = strdup( src ); \
1332 if( !strncmp( src, "id=\"", 4 ) )
1336 else if( !strncmp( src, "param1=\"", 8 ) )
1338 EXTRACT( param1, 8 );
1340 else if( !strncmp( src, "param2=\"", 8 ) )
1342 EXTRACT( param2, 8 );
1349 if( strstr( src, "/>" ) )
1351 src = strstr( src, "/>" ) + 2;
1355 src += strlen( src );
1360 m->id = strdup( "" );
1362 if( m->param1 == NULL )
1364 m->param1 = strdup( "" );
1366 if( m->param2 == NULL )
1368 m->param2 = strdup( "" );
1377 static void MacroClean( macro_t *m )
1435 StrToMacroTypeTab [] =
1437 { "control", MVLC_CONTROL },
1438 /* player control */
1439 { "play", MVLC_PLAY },
1440 { "stop", MVLC_STOP },
1441 { "pause", MVLC_PAUSE },
1442 { "next", MVLC_NEXT },
1443 { "previous", MVLC_PREVIOUS },
1444 { "seek", MVLC_SEEK },
1445 { "keep", MVLC_KEEP },
1446 { "fullscreen", MVLC_FULLSCREEN },
1447 { "volume", MVLC_VOLUME },
1449 /* playlist management */
1450 { "add", MVLC_ADD },
1451 { "delete", MVLC_DEL },
1452 { "empty", MVLC_EMPTY },
1453 { "sort", MVLC_SORT },
1454 { "move", MVLC_MOVE },
1457 { "close", MVLC_CLOSE },
1458 { "shutdown", MVLC_SHUTDOWN },
1461 { "vlm_new", MVLC_VLM_NEW },
1462 { "vlm_setup", MVLC_VLM_SETUP },
1463 { "vlm_del", MVLC_VLM_DEL },
1464 { "vlm_play", MVLC_VLM_PLAY },
1465 { "vlm_pause", MVLC_VLM_PAUSE },
1466 { "vlm_stop", MVLC_VLM_STOP },
1467 { "vlm_seek", MVLC_VLM_SEEK },
1468 { "vlm_load", MVLC_VLM_LOAD },
1469 { "vlm_save", MVLC_VLM_SAVE },
1471 { "rpn", MVLC_RPN },
1473 { "foreach", MVLC_FOREACH },
1474 { "value", MVLC_VALUE },
1477 { "else", MVLC_ELSE },
1478 { "end", MVLC_END },
1479 { "get", MVLC_GET },
1480 { "set", MVLC_SET },
1481 { "int", MVLC_INT },
1482 { "float", MVLC_FLOAT },
1483 { "string", MVLC_STRING },
1486 { NULL, MVLC_UNKNOWN }
1489 static int StrToMacroType( char *name )
1493 if( !name || *name == '\0')
1495 return MVLC_UNKNOWN;
1497 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1499 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1501 return StrToMacroTypeTab[i].i_type;
1504 return MVLC_UNKNOWN;
1507 static void MacroDo( httpd_file_sys_t *p_args,
1509 uint8_t *p_request, int i_request,
1510 uint8_t **pp_data, int *pi_data,
1513 intf_thread_t *p_intf = p_args->p_intf;
1514 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1517 #define ALLOC( l ) \
1519 int __i__ = *pp_dst - *pp_data; \
1521 *pp_data = realloc( *pp_data, *pi_data ); \
1522 *pp_dst = (*pp_data) + __i__; \
1524 #define PRINT( str ) \
1525 ALLOC( strlen( str ) + 1 ); \
1526 *pp_dst += sprintf( *pp_dst, str );
1528 #define PRINTS( str, s ) \
1529 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1531 char * psz_cur = *pp_dst; \
1532 *pp_dst += sprintf( *pp_dst, str, s ); \
1533 while( psz_cur && *psz_cur ) \
1535 /* Prevent script injection */ \
1536 if( *psz_cur == '<' ) *psz_cur = '*'; \
1537 if( *psz_cur == '>' ) *psz_cur = '*'; \
1542 switch( StrToMacroType( m->id ) )
1545 if( i_request <= 0 )
1549 uri_extract_value( p_request, "control", control, 512 );
1550 if( *m->param1 && !strstr( m->param1, control ) )
1552 msg_Warn( p_intf, "unauthorized control=%s", control );
1555 switch( StrToMacroType( control ) )
1562 uri_extract_value( p_request, "item", item, 512 );
1563 i_item = atoi( item );
1564 playlist_Control( p_sys->p_playlist, PLAYLIST_GOTO,
1566 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1570 playlist_Control( p_sys->p_playlist, PLAYLIST_STOP);
1571 msg_Dbg( p_intf, "requested playlist stop" );
1574 playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE );
1575 msg_Dbg( p_intf, "requested playlist pause" );
1578 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, 1 );
1579 msg_Dbg( p_intf, "requested playlist next" );
1582 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, -1);
1583 msg_Dbg( p_intf, "requested playlist next" );
1585 case MVLC_FULLSCREEN:
1587 if( p_sys->p_input )
1589 vout_thread_t *p_vout;
1590 p_vout = vlc_object_find( p_sys->p_input,
1591 VLC_OBJECT_VOUT, FIND_CHILD );
1595 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1596 vlc_object_release( p_vout );
1597 msg_Dbg( p_intf, "requested fullscreen toggle" );
1611 #define POSITION_ABSOLUTE 12
1612 #define POSITION_REL_FOR 13
1613 #define POSITION_REL_BACK 11
1614 #define TIME_ABSOLUTE 0
1615 #define TIME_REL_FOR 1
1616 #define TIME_REL_BACK -1
1617 if( p_sys->p_input )
1619 uri_extract_value( p_request, "seek_value", value, 20 );
1620 uri_decode_url_encoded( value );
1622 var_Get( p_sys->p_input, "length", &val);
1623 i_length = val.i_time;
1625 while( p_value[0] != '\0' )
1631 i_relative = TIME_REL_FOR;
1637 i_relative = TIME_REL_BACK;
1641 case '0': case '1': case '2': case '3': case '4':
1642 case '5': case '6': case '7': case '8': case '9':
1644 i_stock = strtol( p_value , &p_value , 10 );
1647 case '%': /* for percentage ie position */
1649 i_relative += POSITION_ABSOLUTE;
1657 i_value = 60 * (i_value + i_stock) ;
1662 case 'h': case 'H': /* hours */
1664 i_value += 3600 * i_stock;
1666 /* other characters which are not numbers are not important */
1667 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1673 case 'm': case 'M': case '\'': /* minutes */
1675 i_value += 60 * i_stock;
1678 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1684 case 's': case 'S': case '"': /* seconds */
1688 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1702 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1709 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1710 val.i_time = (uint64_t)( i_value ) * 1000000;
1712 val.i_time = i_length;
1714 var_Set( p_sys->p_input, "time", val );
1715 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1720 var_Get( p_sys->p_input, "time", &val );
1721 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1723 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1726 val.i_time = i_length;
1728 var_Set( p_sys->p_input, "time", val );
1729 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1734 var_Get( p_sys->p_input, "time", &val );
1735 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1740 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1742 var_Set( p_sys->p_input, "time", val );
1743 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1746 case POSITION_ABSOLUTE:
1748 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1749 var_Set( p_sys->p_input, "position", val );
1750 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1753 case POSITION_REL_FOR:
1755 var_Get( p_sys->p_input, "position", &val );
1756 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1757 var_Set( p_sys->p_input, "position", val );
1758 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1761 case POSITION_REL_BACK:
1763 var_Get( p_sys->p_input, "position", &val );
1764 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1765 var_Set( p_sys->p_input, "position", val );
1766 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
1771 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1776 #undef POSITION_ABSOLUTE
1777 #undef POSITION_REL_FOR
1778 #undef POSITION_REL_BACK
1779 #undef TIME_ABSOLUTE
1781 #undef TIME_REL_BACK
1787 audio_volume_t i_volume;
1790 uri_extract_value( p_request, "value", vol, 8 );
1791 aout_VolumeGet( p_intf, &i_volume );
1792 uri_decode_url_encoded( vol );
1796 i_value = atoi( vol + 1 );
1797 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1799 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1800 msg_Dbg( p_intf, "requested volume set: max" );
1803 aout_VolumeSet( p_intf , (i_volume + i_value) );
1804 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1809 i_value = atoi( vol + 1 );
1810 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1812 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1813 msg_Dbg( p_intf, "requested volume set: min" );
1816 aout_VolumeSet( p_intf , (i_volume - i_value) );
1817 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1820 if( strstr(vol, "%") != NULL )
1822 i_value = atoi( vol );
1823 if( (i_value <= 100) && (i_value>=0) ){
1824 aout_VolumeSet( p_intf, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/100+AOUT_VOLUME_MIN);
1825 msg_Dbg( p_intf, "requested volume set: %i%%", atoi( vol ));
1829 i_value = atoi( vol );
1830 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1832 aout_VolumeSet( p_intf , atoi( vol ) );
1833 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1839 /* playlist management */
1843 playlist_item_t * p_item;
1845 uri_extract_value( p_request, "mrl", mrl, 512 );
1846 uri_decode_url_encoded( mrl );
1847 p_item = parse_MRL( p_intf, mrl );
1849 if( !p_item || !p_item->input.psz_uri ||
1850 !*p_item->input.psz_uri )
1852 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1855 playlist_AddItem( p_sys->p_playlist , p_item ,
1856 PLAYLIST_APPEND, PLAYLIST_END );
1857 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1864 int i_item, *p_items = NULL, i_nb_items = 0;
1865 char item[512], *p_parser = p_request;
1867 /* Get the list of items to delete */
1869 uri_extract_value( p_parser, "item", item, 512 )) )
1871 if( !*item ) continue;
1873 i_item = atoi( item );
1874 p_items = realloc( p_items, (i_nb_items + 1) *
1876 p_items[i_nb_items] = i_item;
1880 /* The items need to be deleted from in reversed order */
1884 for( i = 0; i < i_nb_items; i++ )
1887 for( j = 0; j < i_nb_items; j++ )
1889 if( p_items[j] > p_items[i_index] )
1893 playlist_Delete( p_sys->p_playlist,
1895 msg_Dbg( p_intf, "requested playlist delete: %d",
1897 p_items[i_index] = -1;
1901 if( p_items ) free( p_items );
1906 int i_item, *p_items = NULL, i_nb_items = 0;
1907 char item[512], *p_parser = p_request;
1910 /* Get the list of items to keep */
1912 uri_extract_value( p_parser, "item", item, 512 )) )
1914 if( !*item ) continue;
1916 i_item = atoi( item );
1917 p_items = realloc( p_items, (i_nb_items + 1) *
1919 p_items[i_nb_items] = i_item;
1923 /* The items need to be deleted from in reversed order */
1924 for( i = p_sys->p_playlist->i_size - 1; i >= 0 ; i-- )
1926 /* Check if the item is in the keep list */
1927 for( j = 0 ; j < i_nb_items ; j++ )
1929 if( p_items[j] == i ) break;
1931 if( j == i_nb_items )
1933 playlist_Delete( p_sys->p_playlist, i );
1934 msg_Dbg( p_intf, "requested playlist delete: %d",
1939 if( p_items ) free( p_items );
1944 while( p_sys->p_playlist->i_size > 0 )
1946 playlist_Delete( p_sys->p_playlist, 0 );
1948 msg_Dbg( p_intf, "requested playlist empty" );
1957 uri_extract_value( p_request, "type", type, 12 );
1958 uri_extract_value( p_request, "order", order, 2 );
1960 if( order[0] == '0' ) i_order = ORDER_NORMAL;
1961 else i_order = ORDER_REVERSE;
1963 if( !strcmp( type , "title" ) )
1965 playlist_SortTitle( p_sys->p_playlist , i_order );
1966 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
1967 } else if( !strcmp( type , "author" ) )
1969 playlist_SortAuthor( p_sys->p_playlist , i_order );
1970 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
1971 } else if( !strcmp( type , "shuffle" ) )
1973 playlist_Sort( p_sys->p_playlist , SORT_RANDOM, ORDER_NORMAL );
1974 msg_Dbg( p_intf, "requested playlist shuffle");
1985 uri_extract_value( p_request, "psz_pos", psz_pos, 6 );
1986 uri_extract_value( p_request, "psz_newpos", psz_newpos, 6 );
1987 i_pos = atoi( psz_pos );
1988 i_newpos = atoi( psz_newpos );
1989 if ( i_pos < i_newpos )
1991 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
1993 playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
1995 msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
1999 /* admin function */
2003 uri_extract_value( p_request, "id", id, 512 );
2004 msg_Dbg( p_intf, "requested close id=%s", id );
2006 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
2008 msg_Warn( p_intf, "close failed for id=%s", id );
2015 msg_Dbg( p_intf, "requested shutdown" );
2016 p_intf->p_vlc->b_die = VLC_TRUE;
2021 case MVLC_VLM_SETUP:
2023 static const char *vlm_properties[11] =
2026 "enabled", "disabled", "loop", "unloop",
2028 "input", "output", "option", "date", "period", "repeat", "append",
2030 vlm_message_t *vlm_answer;
2032 char *psz = malloc( strlen( p_request ) + 1000 );
2037 if( p_intf->p_sys->p_vlm == NULL )
2038 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2040 if( p_intf->p_sys->p_vlm == NULL ) break;
2042 uri_extract_value( p_request, "name", name, 512 );
2043 if( StrToMacroType( control ) == MVLC_VLM_NEW )
2046 uri_extract_value( p_request, "type", type, 20 );
2047 p += sprintf( psz, "new %s %s", name, type );
2051 p += sprintf( psz, "setup %s", name );
2053 /* Parse the request */
2054 for( i = 0; i < 11; i++ )
2057 uri_extract_value( p_request, vlm_properties[i], val, 512 );
2058 uri_decode_url_encoded( val );
2059 if( strlen( val ) > 0 && i >= 4 )
2061 p += sprintf( p, " %s %s", vlm_properties[i], val );
2063 else if( uri_test_param( p_request, vlm_properties[i] ) && i < 4 )
2065 p += sprintf( p, " %s", vlm_properties[i] );
2068 fprintf( stderr, "vlm_ExecuteCommand: %s\n", psz );
2069 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2070 if( vlm_answer->psz_value == NULL ) /* there is no error */
2072 vlm_error = strdup( "" );
2076 vlm_error = malloc( strlen(vlm_answer->psz_name) +
2077 strlen(vlm_answer->psz_value) +
2078 strlen( " : ") + 1 );
2079 sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
2080 vlm_answer->psz_value );
2083 mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
2085 vlm_MessageDelete( vlm_answer );
2093 vlm_message_t *vlm_answer;
2096 if( p_intf->p_sys->p_vlm == NULL )
2097 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2099 if( p_intf->p_sys->p_vlm == NULL ) break;
2101 uri_extract_value( p_request, "name", name, 512 );
2102 sprintf( psz, "del %s", name );
2104 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2105 /* FIXME do a vlm_answer -> var stack conversion */
2106 vlm_MessageDelete( vlm_answer );
2111 case MVLC_VLM_PAUSE:
2115 vlm_message_t *vlm_answer;
2118 if( p_intf->p_sys->p_vlm == NULL )
2119 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2121 if( p_intf->p_sys->p_vlm == NULL ) break;
2123 uri_extract_value( p_request, "name", name, 512 );
2124 if( StrToMacroType( control ) == MVLC_VLM_PLAY )
2125 sprintf( psz, "control %s play", name );
2126 else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
2127 sprintf( psz, "control %s pause", name );
2128 else if( StrToMacroType( control ) == MVLC_VLM_STOP )
2129 sprintf( psz, "control %s stop", name );
2130 else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
2133 uri_extract_value( p_request, "percent", percent, 512 );
2134 sprintf( psz, "control %s seek %s", name, percent );
2137 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2138 /* FIXME do a vlm_answer -> var stack conversion */
2139 vlm_MessageDelete( vlm_answer );
2145 vlm_message_t *vlm_answer;
2149 if( p_intf->p_sys->p_vlm == NULL )
2150 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2152 if( p_intf->p_sys->p_vlm == NULL ) break;
2154 uri_extract_value( p_request, "file", file, 512 );
2155 uri_decode_url_encoded( file );
2157 if( StrToMacroType( control ) == MVLC_VLM_LOAD )
2158 sprintf( psz, "load %s", file );
2160 sprintf( psz, "save %s", file );
2162 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2163 /* FIXME do a vlm_answer -> var stack conversion */
2164 vlm_MessageDelete( vlm_answer );
2171 PRINTS( "<!-- control param(%s) unsuported -->", control );
2183 if( i_request <= 0 ||
2184 *m->param1 == '\0' ||
2185 strstr( p_request, m->param1 ) == NULL )
2189 uri_extract_value( p_request, m->param1, value, 512 );
2190 uri_decode_url_encoded( value );
2192 switch( StrToMacroType( m->param2 ) )
2196 config_PutInt( p_intf, m->param1, i );
2200 config_PutFloat( p_intf, m->param1, f );
2203 config_PutPsz( p_intf, m->param1, value );
2206 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
2217 if( *m->param1 == '\0' )
2222 switch( StrToMacroType( m->param2 ) )
2225 i = config_GetInt( p_intf, m->param1 );
2226 sprintf( value, "%i", i );
2229 f = config_GetFloat( p_intf, m->param1 );
2230 sprintf( value, "%f", f );
2233 psz = config_GetPsz( p_intf, m->param1 );
2234 sprintf( value, "%s", psz ? psz : "" );
2235 if( psz ) free( psz );
2238 sprintf( value, "invalid type(%s) in set", m->param2 );
2241 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
2242 PRINTS( "%s", value );
2251 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2252 s = SSPop( &p_args->stack );
2253 v = mvar_GetValue( p_args->vars, s );
2257 v = s = SSPop( &p_args->stack );
2265 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2270 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2271 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2279 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
2286 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
2291 i_skip = MacroParse( &m, src );
2293 i_id = StrToMacroType( m.id );
2310 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2311 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2313 return src + ( b_after ? i_skip : 0 );
2315 else if( i_level < 0 )
2331 static void Execute( httpd_file_sys_t *p_args,
2332 uint8_t *p_request, int i_request,
2333 uint8_t **pp_data, int *pi_data,
2335 uint8_t *_src, uint8_t *_end )
2337 intf_thread_t *p_intf = p_args->p_intf;
2339 uint8_t *src, *dup, *end;
2340 uint8_t *dst = *pp_dst;
2342 src = dup = malloc( _end - _src + 1 );
2343 end = src +( _end - _src );
2345 memcpy( src, _src, _end - _src );
2348 /* we parse searching <vlc */
2354 p = strstr( src, "<vlc" );
2355 if( p < end && p == src )
2359 src += MacroParse( &m, src );
2361 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2363 switch( StrToMacroType( m.id ) )
2370 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2371 if( SSPopN( &p_args->stack, p_args->vars ) )
2379 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2383 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2387 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2390 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2394 else if( i_test == 1 )
2397 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2399 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2403 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2412 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2413 uint8_t *start = src;
2414 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2421 if( !strcmp( m.param2, "integer" ) )
2423 char *arg = SSPop( &p_args->stack );
2424 index = mvar_IntegerSetNew( m.param1, arg );
2427 else if( !strcmp( m.param2, "directory" ) )
2429 char *arg = SSPop( &p_args->stack );
2430 index = mvar_FileSetNew( m.param1, arg );
2433 else if( !strcmp( m.param2, "playlist" ) )
2435 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2437 else if( !strcmp( m.param2, "information" ) )
2439 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2441 else if( !strcmp( m.param2, "vlm" ) )
2443 if( p_intf->p_sys->p_vlm == NULL )
2444 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2445 index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
2448 else if( !strcmp( m.param2, "hosts" ) )
2450 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2452 else if( !strcmp( m.param2, "urls" ) )
2454 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2456 else if( !strcmp( m.param2, "connections" ) )
2458 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2461 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2463 index = mvar_Duplicate( v );
2467 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2472 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2474 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2476 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2479 f->name = strdup( m.param1 );
2482 mvar_PushVar( p_args->vars, f );
2483 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2484 mvar_RemoveVar( p_args->vars, f );
2488 mvar_Delete( index );
2495 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2503 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2506 int i_index = dst - *pp_data;
2509 *pp_data = realloc( *pp_data, *pi_data );
2510 dst = (*pp_data) + i_index;
2512 memcpy( dst, src, i_copy );
2522 /****************************************************************************
2524 ****************************************************************************
2525 * a file with b_html is parsed and all "macro" replaced
2526 * <vlc id="macro name" [param1="" [param2=""]] />
2529 ****************************************************************************/
2530 static int HttpCallback( httpd_file_sys_t *p_args,
2531 httpd_file_t *p_file,
2533 uint8_t **pp_data, int *pi_data )
2535 int i_request = p_request ? strlen( p_request ) : 0;
2539 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2541 p = *pp_data = malloc( 10240 );
2544 return VLC_EGENERIC;
2546 p += sprintf( p, "<html>\n" );
2547 p += sprintf( p, "<head>\n" );
2548 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2549 p += sprintf( p, "</head>\n" );
2550 p += sprintf( p, "<body>\n" );
2551 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2552 p += sprintf( p, "<hr />\n" );
2553 p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
2554 p += sprintf( p, "</body>\n" );
2555 p += sprintf( p, "</html>\n" );
2557 *pi_data = strlen( *pp_data );
2562 if( !p_args->b_html )
2564 FileLoad( f, pp_data, pi_data );
2572 char position[4]; /* percentage */
2573 char time[12]; /* in seconds */
2574 char length[12]; /* in seconds */
2575 audio_volume_t i_volume;
2579 #define p_sys p_args->p_intf->p_sys
2580 if( p_sys->p_input )
2582 var_Get( p_sys->p_input, "position", &val);
2583 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2584 var_Get( p_sys->p_input, "time", &val);
2585 sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2586 var_Get( p_sys->p_input, "length", &val);
2587 sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2589 var_Get( p_sys->p_input, "state", &val );
2590 if( val.i_int == PLAYING_S )
2592 sprintf( state, "playing" );
2593 } else if( val.i_int == PAUSE_S )
2595 sprintf( state, "paused" );
2598 sprintf( state, "stop" );
2602 sprintf( position, "%d", 0 );
2603 sprintf( time, "%d", 0 );
2604 sprintf( length, "%d", 0 );
2605 sprintf( state, "stop" );
2609 aout_VolumeGet( p_args->p_intf , &i_volume );
2610 sprintf( volume , "%d" , (int)i_volume );
2612 p_args->vars = mvar_New( "variables", "" );
2613 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2614 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2615 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
2616 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2617 mvar_AppendNewVar( p_args->vars, "stream_position", position );
2618 mvar_AppendNewVar( p_args->vars, "stream_time", time );
2619 mvar_AppendNewVar( p_args->vars, "stream_length", length );
2620 mvar_AppendNewVar( p_args->vars, "volume", volume );
2621 mvar_AppendNewVar( p_args->vars, "stream_state", state );
2623 SSInit( &p_args->stack );
2625 /* first we load in a temporary buffer */
2626 FileLoad( f, &p_buffer, &i_buffer );
2628 /* allocate output */
2629 *pi_data = i_buffer + 1000;
2630 dst = *pp_data = malloc( *pi_data );
2632 /* we parse executing all <vlc /> macros */
2633 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2636 *pi_data = dst - *pp_data;
2638 SSClean( &p_args->stack );
2639 mvar_Delete( p_args->vars );
2648 /****************************************************************************
2650 ****************************************************************************/
2651 static int uri_test_param( char *psz_uri, const char *psz_name )
2655 while( (p = strstr( p, psz_name )) )
2657 /* Verify that we are dealing with a post/get argument */
2658 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2667 static char *uri_extract_value( char *psz_uri, const char *psz_name,
2668 char *psz_value, int i_value_max )
2672 while( (p = strstr( p, psz_name )) )
2674 /* Verify that we are dealing with a post/get argument */
2675 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2684 p += strlen( psz_name );
2685 if( *p == '=' ) p++;
2687 if( strchr( p, '&' ) )
2689 i_len = strchr( p, '&' ) - p;
2693 /* for POST method */
2694 if( strchr( p, '\n' ) )
2696 i_len = strchr( p, '\n' ) - p;
2697 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2701 i_len = strlen( p );
2704 i_len = __MIN( i_value_max - 1, i_len );
2707 strncpy( psz_value, p, i_len );
2708 psz_value[i_len] = '\0';
2712 strncpy( psz_value, "", i_value_max );
2718 strncpy( psz_value, "", i_value_max );
2724 static void uri_decode_url_encoded( char *psz )
2726 char *dup = strdup( psz );
2744 *psz++ = strtol( val, NULL, 16 );
2746 else if( *p == '+' )
2760 /****************************************************************************
2761 * Light RPN evaluator
2762 ****************************************************************************/
2763 static void SSInit( rpn_stack_t *st )
2768 static void SSClean( rpn_stack_t *st )
2770 while( st->i_stack > 0 )
2772 free( st->stack[--st->i_stack] );
2776 static void SSPush( rpn_stack_t *st, char *s )
2778 if( st->i_stack < STACK_MAX )
2780 st->stack[st->i_stack++] = strdup( s );
2784 static char * SSPop( rpn_stack_t *st )
2786 if( st->i_stack <= 0 )
2788 return strdup( "" );
2792 return st->stack[--st->i_stack];
2796 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2805 i = strtol( name, &end, 0 );
2808 value = mvar_GetValue( vars, name );
2816 static void SSPushN( rpn_stack_t *st, int i )
2820 sprintf( v, "%d", i );
2824 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2831 while( *exp == ' ' )
2838 /* extract string */
2841 while( *exp && *exp != '\'' )
2852 p = strchr( exp, ' ' );
2857 exp += strlen( exp );
2862 strncpy( s, exp, i );
2873 /* 1. Integer function */
2874 if( !strcmp( s, "!" ) )
2876 SSPushN( st, ~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 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2894 else if( !strcmp( s, "-" ) )
2896 int j = SSPopN( st, vars );
2897 int i = SSPopN( st, vars );
2898 SSPushN( st, i - j );
2900 else if( !strcmp( s, "*" ) )
2902 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2904 else if( !strcmp( s, "/" ) )
2908 j = SSPopN( st, vars );
2909 i = SSPopN( st, vars );
2911 SSPushN( st, j != 0 ? i / j : 0 );
2913 else if( !strcmp( s, "%" ) )
2917 j = SSPopN( st, vars );
2918 i = SSPopN( st, vars );
2920 SSPushN( st, j != 0 ? i % j : 0 );
2922 /* 2. integer tests */
2923 else if( !strcmp( s, "=" ) )
2925 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2927 else if( !strcmp( s, "<" ) )
2929 int j = SSPopN( st, vars );
2930 int i = SSPopN( st, vars );
2932 SSPushN( st, i < j ? -1 : 0 );
2934 else if( !strcmp( s, ">" ) )
2936 int j = SSPopN( st, vars );
2937 int i = SSPopN( st, vars );
2939 SSPushN( st, i > j ? -1 : 0 );
2941 else if( !strcmp( s, "<=" ) )
2943 int j = SSPopN( st, vars );
2944 int i = SSPopN( st, vars );
2946 SSPushN( st, i <= j ? -1 : 0 );
2948 else if( !strcmp( s, ">=" ) )
2950 int j = SSPopN( st, vars );
2951 int i = SSPopN( st, vars );
2953 SSPushN( st, i >= j ? -1 : 0 );
2955 /* 3. string functions */
2956 else if( !strcmp( s, "strcat" ) )
2958 char *s2 = SSPop( st );
2959 char *s1 = SSPop( st );
2960 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2970 else if( !strcmp( s, "strcmp" ) )
2972 char *s2 = SSPop( st );
2973 char *s1 = SSPop( st );
2975 SSPushN( st, strcmp( s1, s2 ) );
2979 else if( !strcmp( s, "strncmp" ) )
2981 int n = SSPopN( st, vars );
2982 char *s2 = SSPop( st );
2983 char *s1 = SSPop( st );
2985 SSPushN( st, strncmp( s1, s2 , n ) );
2989 else if( !strcmp( s, "strsub" ) )
2991 int n = SSPopN( st, vars );
2992 int m = SSPopN( st, vars );
2994 char *s = SSPop( st );
3006 str = malloc( i_len + 1 );
3008 memcpy( str, s + m - 1, i_len );
3009 str[ i_len ] = '\0';
3015 else if( !strcmp( s, "strlen" ) )
3017 char *str = SSPop( st );
3019 SSPushN( st, strlen( str ) );
3022 /* 4. stack functions */
3023 else if( !strcmp( s, "dup" ) )
3025 char *str = SSPop( st );
3030 else if( !strcmp( s, "drop" ) )
3032 char *str = SSPop( st );
3035 else if( !strcmp( s, "swap" ) )
3037 char *s1 = SSPop( st );
3038 char *s2 = SSPop( st );
3045 else if( !strcmp( s, "flush" ) )
3050 else if( !strcmp( s, "store" ) )
3052 char *value = SSPop( st );
3053 char *name = SSPop( st );
3055 mvar_PushNewVar( vars, name, value );
3059 else if( !strcmp( s, "value" ) )
3061 char *name = SSPop( st );
3062 char *value = mvar_GetValue( vars, name );
3064 SSPush( st, value );
3068 else if( !strcmp( s, "url_extract" ) )
3070 char *url = mvar_GetValue( vars, "url_value" );
3071 char *name = SSPop( st );
3074 uri_extract_value( url, name, value, 512 );
3075 uri_decode_url_encoded( value );
3076 SSPush( st, value );
3085 /**********************************************************************
3086 * Find_end_MRL: Find the end of the sentence :
3087 * this function parses the string psz and find the end of the item
3088 * and/or option with detecting the " and ' problems.
3089 * returns NULL if an error is detected, otherwise, returns a pointer
3090 * of the end of the sentence (after the last character)
3091 **********************************************************************/
3092 static char *Find_end_MRL( char *psz )
3102 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
3104 if( *s_sent == '\'' )
3106 s_sent = Find_end_MRL( s_sent );
3108 if( s_sent == NULL )
3118 if( *s_sent == '\"' )
3122 } else /* *s_sent == '\0' , which means the number of " is incorrect */
3132 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
3134 if( *s_sent == '\"' )
3136 s_sent = Find_end_MRL( s_sent );
3138 if( s_sent == NULL )
3148 if( *s_sent == '\'' )
3152 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
3158 default: /* now we can look for spaces */
3160 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
3162 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
3164 s_sent = Find_end_MRL( s_sent );
3175 /**********************************************************************
3176 * parse_MRL: parse the MRL, find the mrl string and the options,
3177 * create an item with all information in it, and return the item.
3178 * return NULL if there is an error.
3179 **********************************************************************/
3180 playlist_item_t * parse_MRL( intf_thread_t *p_intf, char *psz )
3182 char **ppsz_options = NULL;
3189 playlist_item_t * p_item = NULL;
3191 /* In case there is spaces before the mrl */
3192 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
3197 /* extract the mrl */
3198 s_temp = strstr( s_mrl , " :" );
3199 if( s_temp == NULL )
3201 s_temp = s_mrl + strlen( s_mrl );
3204 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
3211 /* if the mrl is between " or ', we must remove them */
3212 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
3214 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
3215 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
3216 mrl[ s_temp - s_mrl - 2 ] = '\0';
3219 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
3220 strncpy( mrl , s_mrl , s_temp - s_mrl );
3221 mrl[ s_temp - s_mrl ] = '\0';
3226 /* now we can take care of the options */
3227 while( (*s_mrl != '\0') && (i_error == 0) )
3236 case ':': /* an option */
3238 s_temp = Find_end_MRL( s_mrl );
3240 if( s_temp == NULL )
3247 ppsz_options = realloc( ppsz_options , i_options *
3249 ppsz_options[ i_options - 1 ] =
3250 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
3252 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
3255 /* don't forget to finish the string with a '\0' */
3256 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
3276 /* now create an item */
3277 p_item = playlist_ItemNew( p_intf, mrl, mrl);
3278 for( i = 0 ; i< i_options ; i++ )
3280 playlist_ItemAddOption( p_item, ppsz_options[i] );
3284 for( i = 0 ; i < i_options ; i++ )
3286 free( ppsz_options[i] );
3288 free( ppsz_options );