1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
38 #include <vlc/vout.h> /* for fullscreen */
40 #include "vlc_httpd.h"
44 #ifdef HAVE_SYS_STAT_H
45 # include <sys/stat.h>
56 #elif defined( WIN32 ) && !defined( UNDER_CE )
60 #if (!defined( WIN32 ) || defined(__MINGW32__))
61 /* Mingw has its own version of dirent */
65 /* stat() support for large files on win32 */
66 #if defined( WIN32 ) && !defined( UNDER_CE )
67 # define stat _stati64
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Open ( vlc_object_t * );
74 static void Close( vlc_object_t * );
76 #define HOST_TEXT N_( "Host address" )
77 #define HOST_LONGTEXT N_( \
78 "You can set the address and port the http interface will bind to." )
79 #define SRC_TEXT N_( "Source directory" )
80 #define SRC_LONGTEXT N_( "Source directory" )
81 #define CERT_TEXT N_( "Certificate file" )
82 #define CERT_LONGTEXT N_( "HTTP interface x509 PEM certificate file " \
84 #define KEY_TEXT N_( "Private key file" )
85 #define KEY_LONGTEXT N_( "HTTP interface x509 PEM private key file" )
86 #define CA_TEXT N_( "Root CA file" )
87 #define CA_LONGTEXT N_( "HTTP interface x509 PEM trusted root CA " \
89 #define CRL_TEXT N_( "CRL file" )
90 #define CRL_LONGTEXT N_( "HTTP interace Certificates Revocation List file" )
93 set_description( _("HTTP remote control interface") );
94 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
95 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
96 add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
97 add_string ( "http-intf-key", NULL, NULL, KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
98 add_string ( "http-intf-ca", NULL, NULL, CA_TEXT, CA_LONGTEXT, VLC_TRUE );
99 add_string ( "http-intf-crl", NULL, NULL, CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
100 set_capability( "interface", 0 );
101 set_callbacks( Open, Close );
105 /*****************************************************************************
107 *****************************************************************************/
108 static void Run ( intf_thread_t *p_intf );
110 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
113 static int DirectoryCheck( char *psz_dir )
117 #ifdef HAVE_SYS_STAT_H
118 struct stat stat_info;
120 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
126 if( ( p_dir = opendir( psz_dir ) ) == NULL )
135 static int HttpCallback( httpd_file_sys_t *p_args,
138 uint8_t **pp_data, int *pi_data );
140 static char *uri_extract_value( char *psz_uri, const char *psz_name,
141 char *psz_value, int i_value_max );
142 static int uri_test_param( char *psz_uri, const char *psz_name );
144 static void uri_decode_url_encoded( char *psz );
146 static char *Find_end_MRL( char *psz );
148 static playlist_item_t * parse_MRL( intf_thread_t * , char *psz );
150 /*****************************************************************************
152 *****************************************************************************/
153 typedef struct mvar_s
159 struct mvar_s **field;
162 #define STACK_MAX 100
165 char *stack[STACK_MAX];
169 struct httpd_file_sys_t
171 intf_thread_t *p_intf;
172 httpd_file_t *p_file;
173 httpd_redirect_t *p_redir;
180 /* inited for each access */
187 httpd_host_t *p_httpd_host;
190 httpd_file_sys_t **pp_files;
192 playlist_t *p_playlist;
193 input_thread_t *p_input;
199 /*****************************************************************************
200 * Activate: initialize and create stuff
201 *****************************************************************************/
202 static int Open( vlc_object_t *p_this )
204 intf_thread_t *p_intf = (intf_thread_t*)p_this;
207 char *psz_address = "";
208 const char *psz_cert;
213 psz_host = config_GetPsz( p_intf, "http-host" );
217 psz_address = psz_host;
219 psz_parser = strchr( psz_host, ':' );
222 *psz_parser++ = '\0';
223 i_port = atoi( psz_parser );
227 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
230 return( VLC_ENOMEM );
232 p_sys->p_playlist = NULL;
233 p_sys->p_input = NULL;
236 psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
237 if ( psz_cert != NULL )
241 msg_Dbg( p_intf, "enablind TLS for HTTP interface (cert file: %s)",
243 psz_pem = config_GetPsz( p_intf, "http-intf-key" );
245 p_tls = tls_ServerCreate( p_this, psz_cert, psz_pem );
248 msg_Err( p_intf, "TLS initialization error" );
253 psz_pem = config_GetPsz( p_intf, "http-intf-ca" );
254 if ( ( psz_pem != NULL) && tls_ServerAddCA( p_tls, psz_pem ) )
256 msg_Err( p_intf, "TLS CA error" );
257 tls_ServerDelete( p_tls );
262 psz_pem = config_GetPsz( p_intf, "http-intf-crl" );
263 if ( ( psz_pem != NULL) && tls_ServerAddCRL( p_tls, psz_pem ) )
265 msg_Err( p_intf, "TLS CRL error" );
266 tls_ServerDelete( p_tls );
281 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
283 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_intf), psz_address,
285 if( p_sys->p_httpd_host == NULL )
287 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
289 tls_ServerDelete( p_tls );
300 p_sys->pp_files = NULL;
302 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
303 ( defined(WIN32) && !defined(UNDER_CE ) )
304 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
306 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
307 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
313 sprintf( psz_src, "%s/http", psz_vlcpath);
315 sprintf( psz_src, "%s/share/http", psz_vlcpath);
319 psz_src = config_GetPsz( p_intf, "http-src" );
321 if( !psz_src || *psz_src == '\0' )
323 if( !DirectoryCheck( "share/http" ) )
325 psz_src = strdup( "share/http" );
327 else if( !DirectoryCheck( DATA_PATH "/http" ) )
329 psz_src = strdup( DATA_PATH "/http" );
334 if( !psz_src || *psz_src == '\0' )
336 msg_Err( p_intf, "invalid src dir" );
340 /* remove trainling \ or / */
341 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
342 psz_src[strlen( psz_src ) - 1] == '/' )
344 psz_src[strlen( psz_src ) - 1] = '\0';
347 ParseDirectory( p_intf, psz_src, psz_src );
350 if( p_sys->i_files <= 0 )
352 msg_Err( p_intf, "cannot find any files (%s)", psz_src );
355 p_intf->pf_run = Run;
361 if( psz_src ) free( psz_src );
362 if( p_sys->pp_files )
364 free( p_sys->pp_files );
366 httpd_HostDelete( p_sys->p_httpd_host );
371 /*****************************************************************************
372 * CloseIntf: destroy interface
373 *****************************************************************************/
374 void Close ( vlc_object_t *p_this )
376 intf_thread_t *p_intf = (intf_thread_t *)p_this;
377 intf_sys_t *p_sys = p_intf->p_sys;
383 vlm_Delete( p_sys->p_vlm );
385 for( i = 0; i < p_sys->i_files; i++ )
387 httpd_FileDelete( p_sys->pp_files[i]->p_file );
388 if( p_sys->pp_files[i]->p_redir )
390 httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
393 free( p_sys->pp_files[i]->file );
394 free( p_sys->pp_files[i]->name );
395 free( p_sys->pp_files[i] );
397 if( p_sys->pp_files )
399 free( p_sys->pp_files );
401 httpd_HostDelete( p_sys->p_httpd_host );
406 /*****************************************************************************
407 * Run: http interface thread
408 *****************************************************************************/
409 static void Run( intf_thread_t *p_intf )
411 intf_sys_t *p_sys = p_intf->p_sys;
413 while( !p_intf->b_die )
415 /* get the playlist */
416 if( p_sys->p_playlist == NULL )
418 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
421 /* Manage the input part */
422 if( p_sys->p_input == NULL )
424 if( p_sys->p_playlist )
427 vlc_object_find( p_sys->p_playlist,
432 else if( p_sys->p_input->b_dead )
434 vlc_object_release( p_sys->p_input );
435 p_sys->p_input = NULL;
440 msleep( INTF_IDLE_SLEEP );
445 vlc_object_release( p_sys->p_input );
446 p_sys->p_input = NULL;
449 if( p_sys->p_playlist )
451 vlc_object_release( p_sys->p_playlist );
452 p_sys->p_playlist = NULL;
457 /*****************************************************************************
459 *****************************************************************************/
460 #define MAX_DIR_SIZE 10240
462 /****************************************************************************
463 * FileToUrl: create a good name for an url from filename
464 ****************************************************************************/
465 static char *FileToUrl( char *name )
469 url = p = malloc( strlen( name ) + 1 );
477 while( *name == '\\' || *name == '/' )
479 while( *name == '/' )
489 /* convert '\\' into '/' */
502 if( ( p = strrchr( url, '/' ) ) != NULL )
504 if( !strncmp( p, "/index.", 7 ) )
512 /****************************************************************************
513 * ParseDirectory: parse recursively a directory, adding each file
514 ****************************************************************************/
515 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
518 intf_sys_t *p_sys = p_intf->p_sys;
519 char dir[MAX_DIR_SIZE];
520 #ifdef HAVE_SYS_STAT_H
521 struct stat stat_info;
524 struct dirent *p_dir_content;
528 char *password = NULL;
530 #ifdef HAVE_SYS_STAT_H
531 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
537 if( ( p_dir = opendir( psz_dir ) ) == NULL )
539 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
543 msg_Dbg( p_intf, "dir=%s", psz_dir );
545 sprintf( dir, "%s/.access", psz_dir );
546 if( ( file = fopen( dir, "r" ) ) != NULL )
551 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
553 i_size = fread( line, 1, 1023, file );
557 while( i_size > 0 && ( line[i_size-1] == '\n' ||
558 line[i_size-1] == '\r' ) )
565 p = strchr( line, ':' );
569 user = strdup( line );
570 password = strdup( p );
573 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
574 user, password, i_size );
581 /* parse psz_src dir */
582 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
587 if( p_dir_content->d_name[0] == '.' )
591 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
592 if( ParseDirectory( p_intf, psz_root, dir ) )
594 httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
599 f->file = strdup( dir );
600 f->name = FileToUrl( &dir[strlen( psz_root )] );
601 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
605 msg_Err( p_intf , "unable to parse directory" );
608 return( VLC_ENOMEM );
610 msg_Dbg( p_intf, "file=%s (url=%s)",
613 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
614 f->name, f->b_html ? "text/html" : NULL,
620 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
622 /* For rep/ add a redir from rep to rep/ */
623 if( f && f->name[strlen(f->name) - 1] == '/' )
625 char *psz_redir = strdup( f->name );
626 psz_redir[strlen( psz_redir ) - 1] = '\0';
628 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
629 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
649 /****************************************************************************
650 * var and set handling
651 ****************************************************************************/
653 static mvar_t *mvar_New( char *name, char *value )
655 mvar_t *v = malloc( sizeof( mvar_t ) );
657 if( !v ) return NULL;
658 v->name = strdup( name );
659 v->value = strdup( value ? value : "" );
662 v->field = malloc( sizeof( mvar_t * ) );
668 static void mvar_Delete( mvar_t *v )
675 for( i = 0; i < v->i_field; i++ )
677 mvar_Delete( v->field[i] );
683 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
685 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
686 v->field[v->i_field] = f;
690 static mvar_t *mvar_Duplicate( mvar_t *v )
695 n = mvar_New( v->name, v->value );
696 for( i = 0; i < v->i_field; i++ )
698 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
704 static void mvar_PushVar( mvar_t *v, mvar_t *f )
706 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
709 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
715 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
718 for( i = 0; i < v->i_field; i++ )
720 if( v->field[i] == f )
725 if( i >= v->i_field )
730 if( i + 1 < v->i_field )
732 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
735 /* FIXME should do a realloc */
738 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
741 char base[512], *field, *p;
744 /* format: name[index].field */
746 field = strchr( name, '.' );
749 int i = field - name;
750 strncpy( base, name, i );
756 strcpy( base, name );
759 if( ( p = strchr( base, '[' ) ) )
762 sscanf( p, "%d]", &i_index );
773 for( i = 0; i < s->i_field; i++ )
775 if( !strcmp( s->field[i]->name, base ) )
785 return mvar_GetVar( s->field[i], field );
799 static char *mvar_GetValue( mvar_t *v, char *field )
807 mvar_t *f = mvar_GetVar( v, field );
819 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
821 mvar_t *f = mvar_New( name, value );
822 mvar_PushVar( vars, f );
825 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
827 mvar_t *f = mvar_New( name, value );
828 mvar_AppendVar( vars, f );
832 /* arg= start[:stop[:step]],.. */
833 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
835 char *dup = strdup( arg );
837 mvar_t *s = mvar_New( name, "set" );
839 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
844 int i_start,i_stop,i_step;
847 p = strchr( str, ',' );
854 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
855 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
862 else if( i_match == 2 )
864 i_step = i_start < i_stop ? 1 : -1;
871 if( ( i_start < i_stop && i_step > 0 ) ||
872 ( i_start > i_stop && i_step < 0 ) )
874 for( i = i_start; ; i += i_step )
878 if( ( i_step > 0 && i > i_stop ) ||
879 ( i_step < 0 && i < i_stop ) )
884 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
885 sprintf( value, "%d", i );
887 mvar_PushNewVar( s, name, value );
898 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
900 mvar_t *s = mvar_New( name, "set" );
903 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
905 vlc_mutex_lock( &p_pl->object_lock );
906 for( i = 0; i < p_pl->i_size; i++ )
908 mvar_t *itm = mvar_New( name, "set" );
911 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
912 mvar_AppendNewVar( itm, "current", value );
914 sprintf( value, "%d", i );
915 mvar_AppendNewVar( itm, "index", value );
917 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->input.psz_name );
919 mvar_AppendNewVar( itm, "uri", p_pl->pp_items[i]->input.psz_uri );
921 sprintf( value, "%d", p_pl->pp_items[i]->i_group );
922 mvar_AppendNewVar( itm, "group", value );
924 mvar_AppendVar( s, itm );
926 vlc_mutex_unlock( &p_pl->object_lock );
931 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
933 mvar_t *s = mvar_New( name, "set" );
936 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
937 if( p_input == NULL )
942 vlc_mutex_lock( &p_input->input.p_item->lock );
943 for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
945 info_category_t *p_category = p_input->input.p_item->pp_categories[i];
946 mvar_t *cat = mvar_New( name, "set" );
947 mvar_t *iset = mvar_New( "info", "set" );
949 mvar_AppendNewVar( cat, "name", p_category->psz_name );
950 mvar_AppendVar( cat, iset );
952 for ( j = 0; j < p_category->i_infos; j++ )
954 info_t *p_info = p_category->pp_infos[j];
955 mvar_t *info = mvar_New( "info", "" );
957 msg_Dbg( p_input, "adding info name=%s value=%s",
958 p_info->psz_name, p_info->psz_value );
959 mvar_AppendNewVar( info, "name", p_info->psz_name );
960 mvar_AppendNewVar( info, "value", p_info->psz_value );
961 mvar_AppendVar( iset, info );
963 mvar_AppendVar( s, cat );
965 vlc_mutex_unlock( &p_input->input.p_item->lock );
970 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
972 mvar_t *s = mvar_New( name, "set" );
976 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
977 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
979 for( i= 0; i < info.i_count; )
983 inf = mvar_New( name, "set" );
986 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
987 info.info[i].psz_name, info.info[i].psz_value ); */
988 mvar_AppendNewVar( inf,
989 info.info[i].psz_name,
990 info.info[i].psz_value );
992 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
993 mvar_AppendVar( s, inf );
998 for( i = 0; i < info.i_count; i++ )
1000 free( info.info[i].psz_name );
1001 free( info.info[i].psz_value );
1003 if( info.i_count > 0 )
1012 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1014 mvar_t *s = mvar_New( name, "set" );
1015 char tmp[MAX_DIR_SIZE], *p, *src;
1016 #ifdef HAVE_SYS_STAT_H
1017 struct stat stat_info;
1020 struct dirent *p_dir_content;
1023 /* convert all / to native separator */
1024 #if defined( WIN32 )
1025 while( (p = strchr( psz_dir, '/' )) )
1034 /* remove trailling separator */
1035 while( strlen( psz_dir ) > 1 &&
1036 #if defined( WIN32 )
1037 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1039 psz_dir[strlen( psz_dir ) -1 ] == sep )
1041 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1043 /* remove double separator */
1044 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1046 if( src[0] == sep && src[1] == sep )
1054 if( *psz_dir == '\0' )
1058 /* first fix all .. dir */
1062 if( src[0] == '.' && src[1] == '.' )
1065 if( p <= &psz_dir[1] )
1072 while( p > &psz_dir[1] && *p != sep )
1077 else if( *src == sep )
1079 if( p > psz_dir && p[-1] == sep )
1093 } while( *src && *src != sep );
1098 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1100 #ifdef HAVE_SYS_STAT_H
1101 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1107 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1109 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1113 /* remove traling / or \ */
1114 for( p = &psz_dir[strlen( psz_dir) - 1];
1115 p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1124 /* parse psz_src dir */
1125 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1129 if( !strcmp( p_dir_content->d_name, "." ) )
1134 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1136 #ifdef HAVE_SYS_STAT_H
1137 if( stat( tmp, &stat_info ) == -1 )
1142 f = mvar_New( name, "set" );
1143 mvar_AppendNewVar( f, "name", tmp );
1145 #ifdef HAVE_SYS_STAT_H
1146 if( S_ISDIR( stat_info.st_mode ) )
1148 mvar_AppendNewVar( f, "type", "directory" );
1150 else if( S_ISREG( stat_info.st_mode ) )
1152 mvar_AppendNewVar( f, "type", "file" );
1156 mvar_AppendNewVar( f, "type", "unknown" );
1159 sprintf( tmp, I64Fd, (int64_t)stat_info.st_size );
1160 mvar_AppendNewVar( f, "size", tmp );
1162 /* FIXME memory leak FIXME */
1164 ctime_r( &stat_info.st_mtime, tmp );
1165 mvar_AppendNewVar( f, "date", tmp );
1167 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1171 mvar_AppendNewVar( f, "type", "unknown" );
1172 mvar_AppendNewVar( f, "size", "unknown" );
1173 mvar_AppendNewVar( f, "date", "unknown" );
1175 mvar_AppendVar( s, f );
1181 static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
1183 mvar_t *s = mvar_New( name, "set" );
1187 /* fprintf( stderr," mvar_VlmSetNew: name=`%s'\n", name ); */
1188 if( vlm == NULL ) return s;
1190 if( vlm_ExecuteCommand( vlm, "show", &msg ) )
1195 for( i = 0; i < msg->i_child; i++ )
1197 /* Over media, schedule */
1198 vlm_message_t *ch = msg->child[i];
1201 for( j = 0; j < ch->i_child; j++ )
1204 vlm_message_t *el = ch->child[j];
1205 vlm_message_t *inf, *desc;
1210 sprintf( psz, "show %s", el->psz_name );
1211 if( vlm_ExecuteCommand( vlm, psz, &inf ) )
1213 desc = inf->child[0];
1215 /* Add a node with name and info */
1216 set = mvar_New( name, "set" );
1217 mvar_AppendNewVar( set, "name", el->psz_name );
1219 /* fprintf( stderr, "#### name=%s\n", el->psz_name ); */
1221 for( k = 0; k < desc->i_child; k++ )
1223 vlm_message_t *ch = desc->child[k];
1224 if( ch->i_child > 0 )
1227 mvar_t *n = mvar_New( ch->psz_name, "set" );
1229 /* fprintf( stderr, " child=%s [%d]\n", ch->psz_name, ch->i_child ); */
1230 for( c = 0; c < ch->i_child; c++ )
1232 mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
1233 mvar_AppendVar( n, in );
1235 /* fprintf( stderr, " sub=%s\n", ch->child[c]->psz_name );*/
1237 mvar_AppendVar( set, n );
1241 /* fprintf( stderr, " child=%s->%s\n", ch->psz_name, ch->psz_value ); */
1242 mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
1245 vlm_MessageDelete( inf );
1247 mvar_AppendVar( s, set );
1250 vlm_MessageDelete( msg );
1256 static void SSInit( rpn_stack_t * );
1257 static void SSClean( rpn_stack_t * );
1258 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1260 static void SSPush ( rpn_stack_t *, char * );
1261 static char *SSPop ( rpn_stack_t * );
1263 static void SSPushN ( rpn_stack_t *, int );
1264 static int SSPopN ( rpn_stack_t *, mvar_t * );
1267 /****************************************************************************
1269 ****************************************************************************/
1277 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1281 /* just load the file */
1283 *pp_data = malloc( 1025 ); /* +1 for \0 */
1284 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1287 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1293 (*pp_data)[*pi_data] = '\0';
1298 static int MacroParse( macro_t *m, uint8_t *psz_src )
1300 uint8_t *dup = strdup( psz_src );
1305 #define EXTRACT( name, l ) \
1307 p = strchr( src, '"' ); \
1312 m->name = strdup( src ); \
1333 if( !strncmp( src, "id=\"", 4 ) )
1337 else if( !strncmp( src, "param1=\"", 8 ) )
1339 EXTRACT( param1, 8 );
1341 else if( !strncmp( src, "param2=\"", 8 ) )
1343 EXTRACT( param2, 8 );
1350 if( strstr( src, "/>" ) )
1352 src = strstr( src, "/>" ) + 2;
1356 src += strlen( src );
1361 m->id = strdup( "" );
1363 if( m->param1 == NULL )
1365 m->param1 = strdup( "" );
1367 if( m->param2 == NULL )
1369 m->param2 = strdup( "" );
1378 static void MacroClean( macro_t *m )
1436 StrToMacroTypeTab [] =
1438 { "control", MVLC_CONTROL },
1439 /* player control */
1440 { "play", MVLC_PLAY },
1441 { "stop", MVLC_STOP },
1442 { "pause", MVLC_PAUSE },
1443 { "next", MVLC_NEXT },
1444 { "previous", MVLC_PREVIOUS },
1445 { "seek", MVLC_SEEK },
1446 { "keep", MVLC_KEEP },
1447 { "fullscreen", MVLC_FULLSCREEN },
1448 { "volume", MVLC_VOLUME },
1450 /* playlist management */
1451 { "add", MVLC_ADD },
1452 { "delete", MVLC_DEL },
1453 { "empty", MVLC_EMPTY },
1454 { "sort", MVLC_SORT },
1455 { "move", MVLC_MOVE },
1458 { "close", MVLC_CLOSE },
1459 { "shutdown", MVLC_SHUTDOWN },
1462 { "vlm_new", MVLC_VLM_NEW },
1463 { "vlm_setup", MVLC_VLM_SETUP },
1464 { "vlm_del", MVLC_VLM_DEL },
1465 { "vlm_play", MVLC_VLM_PLAY },
1466 { "vlm_pause", MVLC_VLM_PAUSE },
1467 { "vlm_stop", MVLC_VLM_STOP },
1468 { "vlm_seek", MVLC_VLM_SEEK },
1469 { "vlm_load", MVLC_VLM_LOAD },
1470 { "vlm_save", MVLC_VLM_SAVE },
1472 { "rpn", MVLC_RPN },
1474 { "foreach", MVLC_FOREACH },
1475 { "value", MVLC_VALUE },
1478 { "else", MVLC_ELSE },
1479 { "end", MVLC_END },
1480 { "get", MVLC_GET },
1481 { "set", MVLC_SET },
1482 { "int", MVLC_INT },
1483 { "float", MVLC_FLOAT },
1484 { "string", MVLC_STRING },
1487 { NULL, MVLC_UNKNOWN }
1490 static int StrToMacroType( char *name )
1494 if( !name || *name == '\0')
1496 return MVLC_UNKNOWN;
1498 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1500 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1502 return StrToMacroTypeTab[i].i_type;
1505 return MVLC_UNKNOWN;
1508 static void MacroDo( httpd_file_sys_t *p_args,
1510 uint8_t *p_request, int i_request,
1511 uint8_t **pp_data, int *pi_data,
1514 intf_thread_t *p_intf = p_args->p_intf;
1515 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1518 #define ALLOC( l ) \
1520 int __i__ = *pp_dst - *pp_data; \
1522 *pp_data = realloc( *pp_data, *pi_data ); \
1523 *pp_dst = (*pp_data) + __i__; \
1525 #define PRINT( str ) \
1526 ALLOC( strlen( str ) + 1 ); \
1527 *pp_dst += sprintf( *pp_dst, str );
1529 #define PRINTS( str, s ) \
1530 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1532 char * psz_cur = *pp_dst; \
1533 *pp_dst += sprintf( *pp_dst, str, s ); \
1534 while( psz_cur && *psz_cur ) \
1536 /* Prevent script injection */ \
1537 if( *psz_cur == '<' ) *psz_cur = '*'; \
1538 if( *psz_cur == '>' ) *psz_cur = '*'; \
1543 switch( StrToMacroType( m->id ) )
1546 if( i_request <= 0 )
1550 uri_extract_value( p_request, "control", control, 512 );
1551 if( *m->param1 && !strstr( m->param1, control ) )
1553 msg_Warn( p_intf, "unauthorized control=%s", control );
1556 switch( StrToMacroType( control ) )
1563 uri_extract_value( p_request, "item", item, 512 );
1564 i_item = atoi( item );
1565 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
1566 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1570 playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1571 msg_Dbg( p_intf, "requested playlist stop" );
1574 playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1575 msg_Dbg( p_intf, "requested playlist pause" );
1578 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1579 p_sys->p_playlist->i_index + 1 );
1580 msg_Dbg( p_intf, "requested playlist next" );
1583 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1584 p_sys->p_playlist->i_index - 1 );
1585 msg_Dbg( p_intf, "requested playlist next" );
1587 case MVLC_FULLSCREEN:
1589 if( p_sys->p_input )
1591 vout_thread_t *p_vout;
1592 p_vout = vlc_object_find( p_sys->p_input,
1593 VLC_OBJECT_VOUT, FIND_CHILD );
1597 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1598 vlc_object_release( p_vout );
1599 msg_Dbg( p_intf, "requested fullscreen toggle" );
1613 #define POSITION_ABSOLUTE 12
1614 #define POSITION_REL_FOR 13
1615 #define POSITION_REL_BACK 11
1616 #define TIME_ABSOLUTE 0
1617 #define TIME_REL_FOR 1
1618 #define TIME_REL_BACK -1
1619 if( p_sys->p_input )
1621 uri_extract_value( p_request, "seek_value", value, 20 );
1622 uri_decode_url_encoded( value );
1624 var_Get( p_sys->p_input, "length", &val);
1625 i_length = val.i_time;
1627 while( p_value[0] != '\0' )
1633 i_relative = TIME_REL_FOR;
1639 i_relative = TIME_REL_BACK;
1643 case '0': case '1': case '2': case '3': case '4':
1644 case '5': case '6': case '7': case '8': case '9':
1646 i_stock = strtol( p_value , &p_value , 10 );
1649 case '%': /* for percentage ie position */
1651 i_relative += POSITION_ABSOLUTE;
1659 i_value = 60 * (i_value + i_stock) ;
1664 case 'h': case 'H': /* hours */
1666 i_value += 3600 * i_stock;
1668 /* other characters which are not numbers are not important */
1669 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1675 case 'm': case 'M': case '\'': /* minutes */
1677 i_value += 60 * i_stock;
1680 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1686 case 's': case 'S': case '"': /* seconds */
1690 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1704 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1711 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1712 val.i_time = (uint64_t)( i_value ) * 1000000;
1714 val.i_time = i_length;
1716 var_Set( p_sys->p_input, "time", val );
1717 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1722 var_Get( p_sys->p_input, "time", &val );
1723 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1725 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1728 val.i_time = i_length;
1730 var_Set( p_sys->p_input, "time", val );
1731 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1736 var_Get( p_sys->p_input, "time", &val );
1737 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1742 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1744 var_Set( p_sys->p_input, "time", val );
1745 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1748 case POSITION_ABSOLUTE:
1750 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1751 var_Set( p_sys->p_input, "position", val );
1752 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1755 case POSITION_REL_FOR:
1757 var_Get( p_sys->p_input, "position", &val );
1758 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1759 var_Set( p_sys->p_input, "position", val );
1760 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1763 case POSITION_REL_BACK:
1765 var_Get( p_sys->p_input, "position", &val );
1766 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1767 var_Set( p_sys->p_input, "position", val );
1768 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
1773 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1778 #undef POSITION_ABSOLUTE
1779 #undef POSITION_REL_FOR
1780 #undef POSITION_REL_BACK
1781 #undef TIME_ABSOLUTE
1783 #undef TIME_REL_BACK
1789 audio_volume_t i_volume;
1792 uri_extract_value( p_request, "value", vol, 8 );
1793 aout_VolumeGet( p_intf, &i_volume );
1794 uri_decode_url_encoded( vol );
1798 i_value = atoi( vol + 1 );
1799 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1801 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1802 msg_Dbg( p_intf, "requested volume set: max" );
1805 aout_VolumeSet( p_intf , (i_volume + i_value) );
1806 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1811 i_value = atoi( vol + 1 );
1812 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1814 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1815 msg_Dbg( p_intf, "requested volume set: min" );
1818 aout_VolumeSet( p_intf , (i_volume - i_value) );
1819 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1822 if( strstr(vol, "%") != NULL )
1824 i_value = atoi( vol );
1825 if( (i_value <= 100) && (i_value>=0) ){
1826 aout_VolumeSet( p_intf, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/100+AOUT_VOLUME_MIN);
1827 msg_Dbg( p_intf, "requested volume set: %i%%", atoi( vol ));
1831 i_value = atoi( vol );
1832 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1834 aout_VolumeSet( p_intf , atoi( vol ) );
1835 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1841 /* playlist management */
1845 playlist_item_t * p_item;
1847 uri_extract_value( p_request, "mrl", mrl, 512 );
1848 uri_decode_url_encoded( mrl );
1849 p_item = parse_MRL( p_intf, mrl );
1851 if( !p_item || !p_item->input.psz_uri ||
1852 !*p_item->input.psz_uri )
1854 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1857 playlist_AddItem( p_sys->p_playlist , p_item ,
1858 PLAYLIST_APPEND, PLAYLIST_END );
1859 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1866 int i_item, *p_items = NULL, i_nb_items = 0;
1867 char item[512], *p_parser = p_request;
1869 /* Get the list of items to delete */
1871 uri_extract_value( p_parser, "item", item, 512 )) )
1873 if( !*item ) continue;
1875 i_item = atoi( item );
1876 p_items = realloc( p_items, (i_nb_items + 1) *
1878 p_items[i_nb_items] = i_item;
1882 /* The items need to be deleted from in reversed order */
1886 for( i = 0; i < i_nb_items; i++ )
1889 for( j = 0; j < i_nb_items; j++ )
1891 if( p_items[j] > p_items[i_index] )
1895 playlist_Delete( p_sys->p_playlist,
1897 msg_Dbg( p_intf, "requested playlist delete: %d",
1899 p_items[i_index] = -1;
1903 if( p_items ) free( p_items );
1908 int i_item, *p_items = NULL, i_nb_items = 0;
1909 char item[512], *p_parser = p_request;
1912 /* Get the list of items to keep */
1914 uri_extract_value( p_parser, "item", item, 512 )) )
1916 if( !*item ) continue;
1918 i_item = atoi( item );
1919 p_items = realloc( p_items, (i_nb_items + 1) *
1921 p_items[i_nb_items] = i_item;
1925 /* The items need to be deleted from in reversed order */
1926 for( i = p_sys->p_playlist->i_size - 1; i >= 0 ; i-- )
1928 /* Check if the item is in the keep list */
1929 for( j = 0 ; j < i_nb_items ; j++ )
1931 if( p_items[j] == i ) break;
1933 if( j == i_nb_items )
1935 playlist_Delete( p_sys->p_playlist, i );
1936 msg_Dbg( p_intf, "requested playlist delete: %d",
1941 if( p_items ) free( p_items );
1946 while( p_sys->p_playlist->i_size > 0 )
1948 playlist_Delete( p_sys->p_playlist, 0 );
1950 msg_Dbg( p_intf, "requested playlist empty" );
1959 uri_extract_value( p_request, "type", type, 12 );
1960 uri_extract_value( p_request, "order", order, 2 );
1962 if( order[0] == '0' ) i_order = ORDER_NORMAL;
1963 else i_order = ORDER_REVERSE;
1965 if( !strcmp( type , "title" ) )
1967 playlist_SortTitle( p_sys->p_playlist , i_order );
1968 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
1969 } else if( !strcmp( type , "group" ) )
1971 playlist_SortGroup( p_sys->p_playlist , i_order );
1972 msg_Dbg( p_intf, "requested playlist sort by group (%d)" , i_order );
1973 } else if( !strcmp( type , "author" ) )
1975 playlist_SortAuthor( p_sys->p_playlist , i_order );
1976 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
1977 } else if( !strcmp( type , "shuffle" ) )
1979 playlist_Sort( p_sys->p_playlist , SORT_RANDOM, ORDER_NORMAL );
1980 msg_Dbg( p_intf, "requested playlist shuffle");
1991 uri_extract_value( p_request, "psz_pos", psz_pos, 6 );
1992 uri_extract_value( p_request, "psz_newpos", psz_newpos, 6 );
1993 i_pos = atoi( psz_pos );
1994 i_newpos = atoi( psz_newpos );
1995 if ( i_pos < i_newpos )
1997 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
1999 playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
2001 msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
2005 /* admin function */
2009 uri_extract_value( p_request, "id", id, 512 );
2010 msg_Dbg( p_intf, "requested close id=%s", id );
2012 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
2014 msg_Warn( p_intf, "close failed for id=%s", id );
2021 msg_Dbg( p_intf, "requested shutdown" );
2022 p_intf->p_vlc->b_die = VLC_TRUE;
2027 case MVLC_VLM_SETUP:
2029 static const char *vlm_properties[11] =
2032 "enabled", "disabled", "loop", "unloop",
2034 "input", "output", "option", "date", "period", "repeat", "append",
2036 vlm_message_t *vlm_answer;
2038 char *psz = malloc( strlen( p_request ) + 1000 );
2043 if( p_intf->p_sys->p_vlm == NULL )
2044 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2046 if( p_intf->p_sys->p_vlm == NULL ) break;
2048 uri_extract_value( p_request, "name", name, 512 );
2049 if( StrToMacroType( control ) == MVLC_VLM_NEW )
2052 uri_extract_value( p_request, "type", type, 20 );
2053 p += sprintf( psz, "new %s %s", name, type );
2057 p += sprintf( psz, "setup %s", name );
2059 /* Parse the request */
2060 for( i = 0; i < 11; i++ )
2063 uri_extract_value( p_request, vlm_properties[i], val, 512 );
2064 uri_decode_url_encoded( val );
2065 if( strlen( val ) > 0 && i >= 4 )
2067 p += sprintf( p, " %s %s", vlm_properties[i], val );
2069 else if( uri_test_param( p_request, vlm_properties[i] ) && i < 4 )
2071 p += sprintf( p, " %s", vlm_properties[i] );
2074 fprintf( stderr, "vlm_ExecuteCommand: %s\n", psz );
2075 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2076 if( vlm_answer->psz_value == NULL ) /* there is no error */
2078 vlm_error = strdup( "" );
2082 vlm_error = malloc( strlen(vlm_answer->psz_name) +
2083 strlen(vlm_answer->psz_value) +
2084 strlen( " : ") + 1 );
2085 sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
2086 vlm_answer->psz_value );
2089 mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
2091 vlm_MessageDelete( vlm_answer );
2099 vlm_message_t *vlm_answer;
2102 if( p_intf->p_sys->p_vlm == NULL )
2103 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2105 if( p_intf->p_sys->p_vlm == NULL ) break;
2107 uri_extract_value( p_request, "name", name, 512 );
2108 sprintf( psz, "del %s", name );
2110 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2111 /* FIXME do a vlm_answer -> var stack conversion */
2112 vlm_MessageDelete( vlm_answer );
2117 case MVLC_VLM_PAUSE:
2121 vlm_message_t *vlm_answer;
2124 if( p_intf->p_sys->p_vlm == NULL )
2125 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2127 if( p_intf->p_sys->p_vlm == NULL ) break;
2129 uri_extract_value( p_request, "name", name, 512 );
2130 if( StrToMacroType( control ) == MVLC_VLM_PLAY )
2131 sprintf( psz, "control %s play", name );
2132 else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
2133 sprintf( psz, "control %s pause", name );
2134 else if( StrToMacroType( control ) == MVLC_VLM_STOP )
2135 sprintf( psz, "control %s stop", name );
2136 else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
2139 uri_extract_value( p_request, "percent", percent, 512 );
2140 sprintf( psz, "control %s seek %s", name, percent );
2143 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2144 /* FIXME do a vlm_answer -> var stack conversion */
2145 vlm_MessageDelete( vlm_answer );
2151 vlm_message_t *vlm_answer;
2155 if( p_intf->p_sys->p_vlm == NULL )
2156 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2158 if( p_intf->p_sys->p_vlm == NULL ) break;
2160 uri_extract_value( p_request, "file", file, 512 );
2161 uri_decode_url_encoded( file );
2163 if( StrToMacroType( control ) == MVLC_VLM_LOAD )
2164 sprintf( psz, "load %s", file );
2166 sprintf( psz, "save %s", file );
2168 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2169 /* FIXME do a vlm_answer -> var stack conversion */
2170 vlm_MessageDelete( vlm_answer );
2177 PRINTS( "<!-- control param(%s) unsuported -->", control );
2189 if( i_request <= 0 ||
2190 *m->param1 == '\0' ||
2191 strstr( p_request, m->param1 ) == NULL )
2195 uri_extract_value( p_request, m->param1, value, 512 );
2196 uri_decode_url_encoded( value );
2198 switch( StrToMacroType( m->param2 ) )
2202 config_PutInt( p_intf, m->param1, i );
2206 config_PutFloat( p_intf, m->param1, f );
2209 config_PutPsz( p_intf, m->param1, value );
2212 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
2223 if( *m->param1 == '\0' )
2228 switch( StrToMacroType( m->param2 ) )
2231 i = config_GetInt( p_intf, m->param1 );
2232 sprintf( value, "%i", i );
2235 f = config_GetFloat( p_intf, m->param1 );
2236 sprintf( value, "%f", f );
2239 psz = config_GetPsz( p_intf, m->param1 );
2240 sprintf( value, "%s", psz ? psz : "" );
2241 if( psz ) free( psz );
2244 sprintf( value, "invalid type(%s) in set", m->param2 );
2247 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
2248 PRINTS( "%s", value );
2257 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2258 s = SSPop( &p_args->stack );
2259 v = mvar_GetValue( p_args->vars, s );
2263 v = s = SSPop( &p_args->stack );
2271 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2276 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2277 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2285 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
2292 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
2297 i_skip = MacroParse( &m, src );
2299 i_id = StrToMacroType( m.id );
2316 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2317 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2319 return src + ( b_after ? i_skip : 0 );
2321 else if( i_level < 0 )
2337 static void Execute( httpd_file_sys_t *p_args,
2338 uint8_t *p_request, int i_request,
2339 uint8_t **pp_data, int *pi_data,
2341 uint8_t *_src, uint8_t *_end )
2343 intf_thread_t *p_intf = p_args->p_intf;
2345 uint8_t *src, *dup, *end;
2346 uint8_t *dst = *pp_dst;
2348 src = dup = malloc( _end - _src + 1 );
2349 end = src +( _end - _src );
2351 memcpy( src, _src, _end - _src );
2354 /* we parse searching <vlc */
2360 p = strstr( src, "<vlc" );
2361 if( p < end && p == src )
2365 src += MacroParse( &m, src );
2367 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2369 switch( StrToMacroType( m.id ) )
2376 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2377 if( SSPopN( &p_args->stack, p_args->vars ) )
2385 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2389 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2393 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2396 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2400 else if( i_test == 1 )
2403 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2405 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2409 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2418 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2419 uint8_t *start = src;
2420 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2427 if( !strcmp( m.param2, "integer" ) )
2429 char *arg = SSPop( &p_args->stack );
2430 index = mvar_IntegerSetNew( m.param1, arg );
2433 else if( !strcmp( m.param2, "directory" ) )
2435 char *arg = SSPop( &p_args->stack );
2436 index = mvar_FileSetNew( m.param1, arg );
2439 else if( !strcmp( m.param2, "playlist" ) )
2441 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2443 else if( !strcmp( m.param2, "information" ) )
2445 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2447 else if( !strcmp( m.param2, "vlm" ) )
2449 if( p_intf->p_sys->p_vlm == NULL )
2450 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2451 index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
2454 else if( !strcmp( m.param2, "hosts" ) )
2456 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2458 else if( !strcmp( m.param2, "urls" ) )
2460 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2462 else if( !strcmp( m.param2, "connections" ) )
2464 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2467 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2469 index = mvar_Duplicate( v );
2473 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2478 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2480 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2482 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2485 f->name = strdup( m.param1 );
2488 mvar_PushVar( p_args->vars, f );
2489 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2490 mvar_RemoveVar( p_args->vars, f );
2494 mvar_Delete( index );
2501 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2509 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2512 int i_index = dst - *pp_data;
2515 *pp_data = realloc( *pp_data, *pi_data );
2516 dst = (*pp_data) + i_index;
2518 memcpy( dst, src, i_copy );
2528 /****************************************************************************
2530 ****************************************************************************
2531 * a file with b_html is parsed and all "macro" replaced
2532 * <vlc id="macro name" [param1="" [param2=""]] />
2535 ****************************************************************************/
2536 static int HttpCallback( httpd_file_sys_t *p_args,
2537 httpd_file_t *p_file,
2539 uint8_t **pp_data, int *pi_data )
2541 int i_request = p_request ? strlen( p_request ) : 0;
2545 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2547 p = *pp_data = malloc( 10240 );
2550 return VLC_EGENERIC;
2552 p += sprintf( p, "<html>\n" );
2553 p += sprintf( p, "<head>\n" );
2554 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2555 p += sprintf( p, "</head>\n" );
2556 p += sprintf( p, "<body>\n" );
2557 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2558 p += sprintf( p, "<hr />\n" );
2559 p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
2560 p += sprintf( p, "</body>\n" );
2561 p += sprintf( p, "</html>\n" );
2563 *pi_data = strlen( *pp_data );
2568 if( !p_args->b_html )
2570 FileLoad( f, pp_data, pi_data );
2578 char position[4]; /* percentage */
2579 char time[12]; /* in seconds */
2580 char length[12]; /* in seconds */
2581 audio_volume_t i_volume;
2585 #define p_sys p_args->p_intf->p_sys
2586 if( p_sys->p_input )
2588 var_Get( p_sys->p_input, "position", &val);
2589 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2590 var_Get( p_sys->p_input, "time", &val);
2591 sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2592 var_Get( p_sys->p_input, "length", &val);
2593 sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2595 var_Get( p_sys->p_input, "state", &val );
2596 if( val.i_int == PLAYING_S )
2598 sprintf( state, "playing" );
2599 } else if( val.i_int == PAUSE_S )
2601 sprintf( state, "paused" );
2604 sprintf( state, "stop" );
2608 sprintf( position, "%d", 0 );
2609 sprintf( time, "%d", 0 );
2610 sprintf( length, "%d", 0 );
2611 sprintf( state, "stop" );
2615 aout_VolumeGet( p_args->p_intf , &i_volume );
2616 sprintf( volume , "%d" , (int)i_volume );
2618 p_args->vars = mvar_New( "variables", "" );
2619 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2620 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2621 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
2622 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2623 mvar_AppendNewVar( p_args->vars, "stream_position", position );
2624 mvar_AppendNewVar( p_args->vars, "stream_time", time );
2625 mvar_AppendNewVar( p_args->vars, "stream_length", length );
2626 mvar_AppendNewVar( p_args->vars, "volume", volume );
2627 mvar_AppendNewVar( p_args->vars, "stream_state", state );
2629 SSInit( &p_args->stack );
2631 /* first we load in a temporary buffer */
2632 FileLoad( f, &p_buffer, &i_buffer );
2634 /* allocate output */
2635 *pi_data = i_buffer + 1000;
2636 dst = *pp_data = malloc( *pi_data );
2638 /* we parse executing all <vlc /> macros */
2639 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2642 *pi_data = dst - *pp_data;
2644 SSClean( &p_args->stack );
2645 mvar_Delete( p_args->vars );
2654 /****************************************************************************
2656 ****************************************************************************/
2657 static int uri_test_param( char *psz_uri, const char *psz_name )
2661 while( (p = strstr( p, psz_name )) )
2663 /* Verify that we are dealing with a post/get argument */
2664 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2673 static char *uri_extract_value( char *psz_uri, const char *psz_name,
2674 char *psz_value, int i_value_max )
2678 while( (p = strstr( p, psz_name )) )
2680 /* Verify that we are dealing with a post/get argument */
2681 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2690 p += strlen( psz_name );
2691 if( *p == '=' ) p++;
2693 if( strchr( p, '&' ) )
2695 i_len = strchr( p, '&' ) - p;
2699 /* for POST method */
2700 if( strchr( p, '\n' ) )
2702 i_len = strchr( p, '\n' ) - p;
2703 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2707 i_len = strlen( p );
2710 i_len = __MIN( i_value_max - 1, i_len );
2713 strncpy( psz_value, p, i_len );
2714 psz_value[i_len] = '\0';
2718 strncpy( psz_value, "", i_value_max );
2724 strncpy( psz_value, "", i_value_max );
2730 static void uri_decode_url_encoded( char *psz )
2732 char *dup = strdup( psz );
2750 *psz++ = strtol( val, NULL, 16 );
2752 else if( *p == '+' )
2766 /****************************************************************************
2767 * Light RPN evaluator
2768 ****************************************************************************/
2769 static void SSInit( rpn_stack_t *st )
2774 static void SSClean( rpn_stack_t *st )
2776 while( st->i_stack > 0 )
2778 free( st->stack[--st->i_stack] );
2782 static void SSPush( rpn_stack_t *st, char *s )
2784 if( st->i_stack < STACK_MAX )
2786 st->stack[st->i_stack++] = strdup( s );
2790 static char * SSPop( rpn_stack_t *st )
2792 if( st->i_stack <= 0 )
2794 return strdup( "" );
2798 return st->stack[--st->i_stack];
2802 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2811 i = strtol( name, &end, 0 );
2814 value = mvar_GetValue( vars, name );
2822 static void SSPushN( rpn_stack_t *st, int i )
2826 sprintf( v, "%d", i );
2830 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2837 while( *exp == ' ' )
2844 /* extract string */
2847 while( *exp && *exp != '\'' )
2858 p = strchr( exp, ' ' );
2863 exp += strlen( exp );
2868 strncpy( s, exp, i );
2879 /* 1. Integer function */
2880 if( !strcmp( s, "!" ) )
2882 SSPushN( st, ~SSPopN( st, vars ) );
2884 else if( !strcmp( s, "^" ) )
2886 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2888 else if( !strcmp( s, "&" ) )
2890 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2892 else if( !strcmp( s, "|" ) )
2894 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2896 else if( !strcmp( s, "+" ) )
2898 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2900 else if( !strcmp( s, "-" ) )
2902 int j = SSPopN( st, vars );
2903 int i = SSPopN( st, vars );
2904 SSPushN( st, i - j );
2906 else if( !strcmp( s, "*" ) )
2908 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2910 else if( !strcmp( s, "/" ) )
2914 j = SSPopN( st, vars );
2915 i = SSPopN( st, vars );
2917 SSPushN( st, j != 0 ? i / j : 0 );
2919 else if( !strcmp( s, "%" ) )
2923 j = SSPopN( st, vars );
2924 i = SSPopN( st, vars );
2926 SSPushN( st, j != 0 ? i % j : 0 );
2928 /* 2. integer tests */
2929 else if( !strcmp( s, "=" ) )
2931 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2933 else if( !strcmp( s, "<" ) )
2935 int j = SSPopN( st, vars );
2936 int i = SSPopN( st, vars );
2938 SSPushN( st, i < j ? -1 : 0 );
2940 else if( !strcmp( s, ">" ) )
2942 int j = SSPopN( st, vars );
2943 int i = SSPopN( st, vars );
2945 SSPushN( st, i > j ? -1 : 0 );
2947 else if( !strcmp( s, "<=" ) )
2949 int j = SSPopN( st, vars );
2950 int i = SSPopN( st, vars );
2952 SSPushN( st, i <= j ? -1 : 0 );
2954 else if( !strcmp( s, ">=" ) )
2956 int j = SSPopN( st, vars );
2957 int i = SSPopN( st, vars );
2959 SSPushN( st, i >= j ? -1 : 0 );
2961 /* 3. string functions */
2962 else if( !strcmp( s, "strcat" ) )
2964 char *s2 = SSPop( st );
2965 char *s1 = SSPop( st );
2966 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2976 else if( !strcmp( s, "strcmp" ) )
2978 char *s2 = SSPop( st );
2979 char *s1 = SSPop( st );
2981 SSPushN( st, strcmp( s1, s2 ) );
2985 else if( !strcmp( s, "strncmp" ) )
2987 int n = SSPopN( st, vars );
2988 char *s2 = SSPop( st );
2989 char *s1 = SSPop( st );
2991 SSPushN( st, strncmp( s1, s2 , n ) );
2995 else if( !strcmp( s, "strsub" ) )
2997 int n = SSPopN( st, vars );
2998 int m = SSPopN( st, vars );
3000 char *s = SSPop( st );
3012 str = malloc( i_len + 1 );
3014 memcpy( str, s + m - 1, i_len );
3015 str[ i_len ] = '\0';
3021 else if( !strcmp( s, "strlen" ) )
3023 char *str = SSPop( st );
3025 SSPushN( st, strlen( str ) );
3028 /* 4. stack functions */
3029 else if( !strcmp( s, "dup" ) )
3031 char *str = SSPop( st );
3036 else if( !strcmp( s, "drop" ) )
3038 char *str = SSPop( st );
3041 else if( !strcmp( s, "swap" ) )
3043 char *s1 = SSPop( st );
3044 char *s2 = SSPop( st );
3051 else if( !strcmp( s, "flush" ) )
3056 else if( !strcmp( s, "store" ) )
3058 char *value = SSPop( st );
3059 char *name = SSPop( st );
3061 mvar_PushNewVar( vars, name, value );
3065 else if( !strcmp( s, "value" ) )
3067 char *name = SSPop( st );
3068 char *value = mvar_GetValue( vars, name );
3070 SSPush( st, value );
3074 else if( !strcmp( s, "url_extract" ) )
3076 char *url = mvar_GetValue( vars, "url_value" );
3077 char *name = SSPop( st );
3080 uri_extract_value( url, name, value, 512 );
3081 uri_decode_url_encoded( value );
3082 SSPush( st, value );
3091 /**********************************************************************
3092 * Find_end_MRL: Find the end of the sentence :
3093 * this function parses the string psz and find the end of the item
3094 * and/or option with detecting the " and ' problems.
3095 * returns NULL if an error is detected, otherwise, returns a pointer
3096 * of the end of the sentence (after the last character)
3097 **********************************************************************/
3098 static char *Find_end_MRL( char *psz )
3108 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
3110 if( *s_sent == '\'' )
3112 s_sent = Find_end_MRL( s_sent );
3114 if( s_sent == NULL )
3124 if( *s_sent == '\"' )
3128 } else /* *s_sent == '\0' , which means the number of " is incorrect */
3138 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
3140 if( *s_sent == '\"' )
3142 s_sent = Find_end_MRL( s_sent );
3144 if( s_sent == NULL )
3154 if( *s_sent == '\'' )
3158 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
3164 default: /* now we can look for spaces */
3166 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
3168 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
3170 s_sent = Find_end_MRL( s_sent );
3181 /**********************************************************************
3182 * parse_MRL: parse the MRL, find the mrl string and the options,
3183 * create an item with all information in it, and return the item.
3184 * return NULL if there is an error.
3185 **********************************************************************/
3186 playlist_item_t * parse_MRL( intf_thread_t *p_intf, char *psz )
3188 char **ppsz_options = NULL;
3195 playlist_item_t * p_item = NULL;
3197 /* In case there is spaces before the mrl */
3198 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
3203 /* extract the mrl */
3204 s_temp = strstr( s_mrl , " :" );
3205 if( s_temp == NULL )
3207 s_temp = s_mrl + strlen( s_mrl );
3210 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
3217 /* if the mrl is between " or ', we must remove them */
3218 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
3220 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
3221 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
3222 mrl[ s_temp - s_mrl - 2 ] = '\0';
3225 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
3226 strncpy( mrl , s_mrl , s_temp - s_mrl );
3227 mrl[ s_temp - s_mrl ] = '\0';
3232 /* now we can take care of the options */
3233 while( (*s_mrl != '\0') && (i_error == 0) )
3242 case ':': /* an option */
3244 s_temp = Find_end_MRL( s_mrl );
3246 if( s_temp == NULL )
3253 ppsz_options = realloc( ppsz_options , i_options *
3255 ppsz_options[ i_options - 1 ] =
3256 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
3258 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
3261 /* don't forget to finish the string with a '\0' */
3262 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
3282 /* now create an item */
3283 p_item = playlist_ItemNew( p_intf, mrl, mrl);
3284 for( i = 0 ; i< i_options ; i++ )
3286 playlist_ItemAddOption( p_item, ppsz_options[i] );
3290 for( i = 0 ; i < i_options ; i++ )
3292 free( ppsz_options[i] );
3294 free( ppsz_options );