1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
38 #include <vlc/vout.h> /* for fullscreen */
40 #include "vlc_httpd.h"
44 #ifdef HAVE_SYS_STAT_H
45 # include <sys/stat.h>
56 #elif defined( WIN32 ) && !defined( UNDER_CE )
60 #if (!defined( WIN32 ) || defined(__MINGW32__))
61 /* Mingw has its own version of dirent */
65 /* stat() support for large files on win32 */
66 #if defined( WIN32 ) && !defined( UNDER_CE )
67 # define stat _stati64
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Open ( vlc_object_t * );
74 static void Close( vlc_object_t * );
76 #define HOST_TEXT N_( "Host address" )
77 #define HOST_LONGTEXT N_( \
78 "You can set the address and port the http interface will bind to." )
79 #define SRC_TEXT N_( "Source directory" )
80 #define SRC_LONGTEXT N_( "Source directory" )
81 #define CERT_TEXT N_( "Certificate file" )
82 #define CERT_LONGTEXT N_( "HTTP interface x509 PEM certificate file (enables SSL)" )
83 #define KEY_TEXT N_( "Private key file" )
84 #define KEY_LONGTEXT N_( "HTTP interface x509 PEM private key file" )
85 #define CA_TEXT N_( "Root CA file" )
86 #define CA_LONGTEXT N_( "HTTP interface x509 PEM trusted root CA certificates file" )
87 #define CRL_TEXT N_( "CRL file" )
88 #define CRL_LONGTEXT N_( "HTTP interace Certificates Revocation List file" )
91 set_description( _("HTTP remote control interface") );
92 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
93 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
94 add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
95 add_string ( "http-intf-key", NULL, NULL, KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
96 add_string ( "http-intf-ca", NULL, NULL, CA_TEXT, CA_LONGTEXT, VLC_TRUE );
97 add_string ( "http-intf-crl", NULL, NULL, CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
98 set_capability( "interface", 0 );
99 set_callbacks( Open, Close );
103 /*****************************************************************************
105 *****************************************************************************/
106 static void Run ( intf_thread_t *p_intf );
108 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
111 static int DirectoryCheck( char *psz_dir )
115 #ifdef HAVE_SYS_STAT_H
116 struct stat stat_info;
118 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
124 if( ( p_dir = opendir( psz_dir ) ) == NULL )
133 static int HttpCallback( httpd_file_sys_t *p_args,
136 uint8_t **pp_data, int *pi_data );
138 static char *uri_extract_value( char *psz_uri, const char *psz_name,
139 char *psz_value, int i_value_max );
140 static int uri_test_param( char *psz_uri, const char *psz_name );
142 static void uri_decode_url_encoded( char *psz );
144 static char *Find_end_MRL( char *psz );
146 static playlist_item_t * parse_MRL( intf_thread_t * , char *psz );
148 /*****************************************************************************
150 *****************************************************************************/
151 typedef struct mvar_s
157 struct mvar_s **field;
160 #define STACK_MAX 100
163 char *stack[STACK_MAX];
167 struct httpd_file_sys_t
169 intf_thread_t *p_intf;
170 httpd_file_t *p_file;
171 httpd_redirect_t *p_redir;
178 /* inited for each access */
185 httpd_host_t *p_httpd_host;
188 httpd_file_sys_t **pp_files;
190 playlist_t *p_playlist;
191 input_thread_t *p_input;
197 /*****************************************************************************
198 * Activate: initialize and create stuff
199 *****************************************************************************/
200 static int Open( vlc_object_t *p_this )
202 intf_thread_t *p_intf = (intf_thread_t*)p_this;
205 char *psz_address = "";
206 const char *psz_cert;
211 psz_host = config_GetPsz( p_intf, "http-host" );
215 psz_address = psz_host;
217 psz_parser = strchr( psz_host, ':' );
220 *psz_parser++ = '\0';
221 i_port = atoi( psz_parser );
225 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
228 return( VLC_ENOMEM );
230 p_sys->p_playlist = NULL;
231 p_sys->p_input = NULL;
234 psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
235 if ( psz_cert != NULL )
239 msg_Dbg( p_intf, "enablind TLS for HTTP interface (cert file: %s)",
241 psz_pem = config_GetPsz( p_intf, "http-intf-key" );
243 p_tls = tls_ServerCreate( p_this, psz_cert, psz_pem );
246 msg_Err( p_intf, "TLS initialization error" );
251 psz_pem = config_GetPsz( p_intf, "http-intf-ca" );
252 if ( ( psz_pem != NULL) && tls_ServerAddCA( p_tls, psz_pem ) )
254 msg_Err( p_intf, "TLS CA error" );
255 tls_ServerDelete( p_tls );
260 psz_pem = config_GetPsz( p_intf, "http-intf-crl" );
261 if ( ( psz_pem != NULL) && tls_ServerAddCRL( p_tls, psz_pem ) )
263 msg_Err( p_intf, "TLS CRL error" );
264 tls_ServerDelete( p_tls );
279 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
281 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_intf), psz_address,
283 if( p_sys->p_httpd_host == NULL )
285 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
287 tls_ServerDelete( p_tls );
298 p_sys->pp_files = NULL;
300 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
301 ( defined(WIN32) && !defined(UNDER_CE ) )
302 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
304 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
305 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
311 sprintf( psz_src, "%s/http", psz_vlcpath);
313 sprintf( psz_src, "%s/share/http", psz_vlcpath);
317 psz_src = config_GetPsz( p_intf, "http-src" );
319 if( !psz_src || *psz_src == '\0' )
321 if( !DirectoryCheck( "share/http" ) )
323 psz_src = strdup( "share/http" );
325 else if( !DirectoryCheck( DATA_PATH "/http" ) )
327 psz_src = strdup( DATA_PATH "/http" );
332 if( !psz_src || *psz_src == '\0' )
334 msg_Err( p_intf, "invalid src dir" );
338 /* remove trainling \ or / */
339 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
340 psz_src[strlen( psz_src ) - 1] == '/' )
342 psz_src[strlen( psz_src ) - 1] = '\0';
345 ParseDirectory( p_intf, psz_src, psz_src );
348 if( p_sys->i_files <= 0 )
350 msg_Err( p_intf, "cannot find any files (%s)", psz_src );
353 p_intf->pf_run = Run;
359 if( psz_src ) free( psz_src );
360 if( p_sys->pp_files )
362 free( p_sys->pp_files );
364 httpd_HostDelete( p_sys->p_httpd_host );
369 /*****************************************************************************
370 * CloseIntf: destroy interface
371 *****************************************************************************/
372 void Close ( vlc_object_t *p_this )
374 intf_thread_t *p_intf = (intf_thread_t *)p_this;
375 intf_sys_t *p_sys = p_intf->p_sys;
381 vlm_Delete( p_sys->p_vlm );
383 for( i = 0; i < p_sys->i_files; i++ )
385 httpd_FileDelete( p_sys->pp_files[i]->p_file );
386 if( p_sys->pp_files[i]->p_redir )
388 httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
391 free( p_sys->pp_files[i]->file );
392 free( p_sys->pp_files[i]->name );
393 free( p_sys->pp_files[i] );
395 if( p_sys->pp_files )
397 free( p_sys->pp_files );
399 httpd_HostDelete( p_sys->p_httpd_host );
404 /*****************************************************************************
405 * Run: http interface thread
406 *****************************************************************************/
407 static void Run( intf_thread_t *p_intf )
409 intf_sys_t *p_sys = p_intf->p_sys;
411 while( !p_intf->b_die )
413 /* get the playlist */
414 if( p_sys->p_playlist == NULL )
416 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
419 /* Manage the input part */
420 if( p_sys->p_input == NULL )
422 if( p_sys->p_playlist )
425 vlc_object_find( p_sys->p_playlist,
430 else if( p_sys->p_input->b_dead )
432 vlc_object_release( p_sys->p_input );
433 p_sys->p_input = NULL;
438 msleep( INTF_IDLE_SLEEP );
443 vlc_object_release( p_sys->p_input );
444 p_sys->p_input = NULL;
447 if( p_sys->p_playlist )
449 vlc_object_release( p_sys->p_playlist );
450 p_sys->p_playlist = NULL;
455 /*****************************************************************************
457 *****************************************************************************/
458 #define MAX_DIR_SIZE 10240
460 /****************************************************************************
461 * FileToUrl: create a good name for an url from filename
462 ****************************************************************************/
463 static char *FileToUrl( char *name )
467 url = p = malloc( strlen( name ) + 1 );
475 while( *name == '\\' || *name == '/' )
477 while( *name == '/' )
487 /* convert '\\' into '/' */
500 if( ( p = strrchr( url, '/' ) ) != NULL )
502 if( !strncmp( p, "/index.", 7 ) )
510 /****************************************************************************
511 * ParseDirectory: parse recursively a directory, adding each file
512 ****************************************************************************/
513 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
516 intf_sys_t *p_sys = p_intf->p_sys;
517 char dir[MAX_DIR_SIZE];
518 #ifdef HAVE_SYS_STAT_H
519 struct stat stat_info;
522 struct dirent *p_dir_content;
526 char *password = NULL;
528 #ifdef HAVE_SYS_STAT_H
529 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
535 if( ( p_dir = opendir( psz_dir ) ) == NULL )
537 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
541 msg_Dbg( p_intf, "dir=%s", psz_dir );
543 sprintf( dir, "%s/.access", psz_dir );
544 if( ( file = fopen( dir, "r" ) ) != NULL )
549 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
551 i_size = fread( line, 1, 1023, file );
555 while( i_size > 0 && ( line[i_size-1] == '\n' ||
556 line[i_size-1] == '\r' ) )
563 p = strchr( line, ':' );
567 user = strdup( line );
568 password = strdup( p );
571 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
572 user, password, i_size );
579 /* parse psz_src dir */
580 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
585 if( p_dir_content->d_name[0] == '.' )
589 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
590 if( ParseDirectory( p_intf, psz_root, dir ) )
592 httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
597 f->file = strdup( dir );
598 f->name = FileToUrl( &dir[strlen( psz_root )] );
599 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
603 msg_Err( p_intf , "unable to parse directory" );
606 return( VLC_ENOMEM );
608 msg_Dbg( p_intf, "file=%s (url=%s)",
611 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
612 f->name, f->b_html ? "text/html" : NULL,
618 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
620 /* For rep/ add a redir from rep to rep/ */
621 if( f && f->name[strlen(f->name) - 1] == '/' )
623 char *psz_redir = strdup( f->name );
624 psz_redir[strlen( psz_redir ) - 1] = '\0';
626 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
627 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
647 /****************************************************************************
648 * var and set handling
649 ****************************************************************************/
651 static mvar_t *mvar_New( char *name, char *value )
653 mvar_t *v = malloc( sizeof( mvar_t ) );
655 if( !v ) return NULL;
656 v->name = strdup( name );
657 v->value = strdup( value ? value : "" );
660 v->field = malloc( sizeof( mvar_t * ) );
666 static void mvar_Delete( mvar_t *v )
673 for( i = 0; i < v->i_field; i++ )
675 mvar_Delete( v->field[i] );
681 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
683 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
684 v->field[v->i_field] = f;
688 static mvar_t *mvar_Duplicate( mvar_t *v )
693 n = mvar_New( v->name, v->value );
694 for( i = 0; i < v->i_field; i++ )
696 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
702 static void mvar_PushVar( mvar_t *v, mvar_t *f )
704 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
707 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
713 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
716 for( i = 0; i < v->i_field; i++ )
718 if( v->field[i] == f )
723 if( i >= v->i_field )
728 if( i + 1 < v->i_field )
730 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
733 /* FIXME should do a realloc */
736 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
739 char base[512], *field, *p;
742 /* format: name[index].field */
744 field = strchr( name, '.' );
747 int i = field - name;
748 strncpy( base, name, i );
754 strcpy( base, name );
757 if( ( p = strchr( base, '[' ) ) )
760 sscanf( p, "%d]", &i_index );
771 for( i = 0; i < s->i_field; i++ )
773 if( !strcmp( s->field[i]->name, base ) )
783 return mvar_GetVar( s->field[i], field );
797 static char *mvar_GetValue( mvar_t *v, char *field )
805 mvar_t *f = mvar_GetVar( v, field );
817 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
819 mvar_t *f = mvar_New( name, value );
820 mvar_PushVar( vars, f );
823 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
825 mvar_t *f = mvar_New( name, value );
826 mvar_AppendVar( vars, f );
830 /* arg= start[:stop[:step]],.. */
831 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
833 char *dup = strdup( arg );
835 mvar_t *s = mvar_New( name, "set" );
837 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
842 int i_start,i_stop,i_step;
845 p = strchr( str, ',' );
852 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
853 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
860 else if( i_match == 2 )
862 i_step = i_start < i_stop ? 1 : -1;
869 if( ( i_start < i_stop && i_step > 0 ) ||
870 ( i_start > i_stop && i_step < 0 ) )
872 for( i = i_start; ; i += i_step )
876 if( ( i_step > 0 && i > i_stop ) ||
877 ( i_step < 0 && i < i_stop ) )
882 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
883 sprintf( value, "%d", i );
885 mvar_PushNewVar( s, name, value );
896 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
898 mvar_t *s = mvar_New( name, "set" );
901 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
903 vlc_mutex_lock( &p_pl->object_lock );
904 for( i = 0; i < p_pl->i_size; i++ )
906 mvar_t *itm = mvar_New( name, "set" );
909 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
910 mvar_AppendNewVar( itm, "current", value );
912 sprintf( value, "%d", i );
913 mvar_AppendNewVar( itm, "index", value );
915 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->input.psz_name );
917 mvar_AppendNewVar( itm, "uri", p_pl->pp_items[i]->input.psz_uri );
919 sprintf( value, "%d", p_pl->pp_items[i]->i_group );
920 mvar_AppendNewVar( itm, "group", value );
922 mvar_AppendVar( s, itm );
924 vlc_mutex_unlock( &p_pl->object_lock );
929 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
931 mvar_t *s = mvar_New( name, "set" );
934 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
935 if( p_input == NULL )
940 vlc_mutex_lock( &p_input->input.p_item->lock );
941 for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
943 info_category_t *p_category = p_input->input.p_item->pp_categories[i];
944 mvar_t *cat = mvar_New( name, "set" );
945 mvar_t *iset = mvar_New( "info", "set" );
947 mvar_AppendNewVar( cat, "name", p_category->psz_name );
948 mvar_AppendVar( cat, iset );
950 for ( j = 0; j < p_category->i_infos; j++ )
952 info_t *p_info = p_category->pp_infos[j];
953 mvar_t *info = mvar_New( "info", "" );
955 msg_Dbg( p_input, "adding info name=%s value=%s",
956 p_info->psz_name, p_info->psz_value );
957 mvar_AppendNewVar( info, "name", p_info->psz_name );
958 mvar_AppendNewVar( info, "value", p_info->psz_value );
959 mvar_AppendVar( iset, info );
961 mvar_AppendVar( s, cat );
963 vlc_mutex_unlock( &p_input->input.p_item->lock );
968 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
970 mvar_t *s = mvar_New( name, "set" );
974 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
975 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
977 for( i= 0; i < info.i_count; )
981 inf = mvar_New( name, "set" );
984 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
985 info.info[i].psz_name, info.info[i].psz_value ); */
986 mvar_AppendNewVar( inf,
987 info.info[i].psz_name,
988 info.info[i].psz_value );
990 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
991 mvar_AppendVar( s, inf );
996 for( i = 0; i < info.i_count; i++ )
998 free( info.info[i].psz_name );
999 free( info.info[i].psz_value );
1001 if( info.i_count > 0 )
1010 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1012 mvar_t *s = mvar_New( name, "set" );
1013 char tmp[MAX_DIR_SIZE], *p, *src;
1014 #ifdef HAVE_SYS_STAT_H
1015 struct stat stat_info;
1018 struct dirent *p_dir_content;
1021 /* convert all / to native separator */
1022 #if defined( WIN32 )
1023 while( (p = strchr( psz_dir, '/' )) )
1032 /* remove trailling separator */
1033 while( strlen( psz_dir ) > 1 &&
1034 #if defined( WIN32 )
1035 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1037 psz_dir[strlen( psz_dir ) -1 ] == sep )
1039 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1041 /* remove double separator */
1042 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1044 if( src[0] == sep && src[1] == sep )
1052 if( *psz_dir == '\0' )
1056 /* first fix all .. dir */
1060 if( src[0] == '.' && src[1] == '.' )
1063 if( p <= &psz_dir[1] )
1070 while( p > &psz_dir[1] && *p != sep )
1075 else if( *src == sep )
1077 if( p > psz_dir && p[-1] == sep )
1091 } while( *src && *src != sep );
1096 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1098 #ifdef HAVE_SYS_STAT_H
1099 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1105 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1107 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1111 /* remove traling / or \ */
1112 for( p = &psz_dir[strlen( psz_dir) - 1];
1113 p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1122 /* parse psz_src dir */
1123 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1127 if( !strcmp( p_dir_content->d_name, "." ) )
1132 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1134 #ifdef HAVE_SYS_STAT_H
1135 if( stat( tmp, &stat_info ) == -1 )
1140 f = mvar_New( name, "set" );
1141 mvar_AppendNewVar( f, "name", tmp );
1143 #ifdef HAVE_SYS_STAT_H
1144 if( S_ISDIR( stat_info.st_mode ) )
1146 mvar_AppendNewVar( f, "type", "directory" );
1148 else if( S_ISREG( stat_info.st_mode ) )
1150 mvar_AppendNewVar( f, "type", "file" );
1154 mvar_AppendNewVar( f, "type", "unknown" );
1157 sprintf( tmp, I64Fd, (int64_t)stat_info.st_size );
1158 mvar_AppendNewVar( f, "size", tmp );
1160 /* FIXME memory leak FIXME */
1162 ctime_r( &stat_info.st_mtime, tmp );
1163 mvar_AppendNewVar( f, "date", tmp );
1165 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1169 mvar_AppendNewVar( f, "type", "unknown" );
1170 mvar_AppendNewVar( f, "size", "unknown" );
1171 mvar_AppendNewVar( f, "date", "unknown" );
1173 mvar_AppendVar( s, f );
1179 static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
1181 mvar_t *s = mvar_New( name, "set" );
1185 /* fprintf( stderr," mvar_VlmSetNew: name=`%s'\n", name ); */
1186 if( vlm == NULL ) return s;
1188 if( vlm_ExecuteCommand( vlm, "show", &msg ) )
1193 for( i = 0; i < msg->i_child; i++ )
1195 /* Over media, schedule */
1196 vlm_message_t *ch = msg->child[i];
1199 for( j = 0; j < ch->i_child; j++ )
1202 vlm_message_t *el = ch->child[j];
1203 vlm_message_t *inf, *desc;
1208 sprintf( psz, "show %s", el->psz_name );
1209 if( vlm_ExecuteCommand( vlm, psz, &inf ) )
1211 desc = inf->child[0];
1213 /* Add a node with name and info */
1214 set = mvar_New( name, "set" );
1215 mvar_AppendNewVar( set, "name", el->psz_name );
1217 /* fprintf( stderr, "#### name=%s\n", el->psz_name ); */
1219 for( k = 0; k < desc->i_child; k++ )
1221 vlm_message_t *ch = desc->child[k];
1222 if( ch->i_child > 0 )
1225 mvar_t *n = mvar_New( ch->psz_name, "set" );
1227 /* fprintf( stderr, " child=%s [%d]\n", ch->psz_name, ch->i_child ); */
1228 for( c = 0; c < ch->i_child; c++ )
1230 mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
1231 mvar_AppendVar( n, in );
1233 /* fprintf( stderr, " sub=%s\n", ch->child[c]->psz_name );*/
1235 mvar_AppendVar( set, n );
1239 /* fprintf( stderr, " child=%s->%s\n", ch->psz_name, ch->psz_value ); */
1240 mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
1243 vlm_MessageDelete( inf );
1245 mvar_AppendVar( s, set );
1248 vlm_MessageDelete( msg );
1254 static void SSInit( rpn_stack_t * );
1255 static void SSClean( rpn_stack_t * );
1256 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1258 static void SSPush ( rpn_stack_t *, char * );
1259 static char *SSPop ( rpn_stack_t * );
1261 static void SSPushN ( rpn_stack_t *, int );
1262 static int SSPopN ( rpn_stack_t *, mvar_t * );
1265 /****************************************************************************
1267 ****************************************************************************/
1275 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1279 /* just load the file */
1281 *pp_data = malloc( 1025 ); /* +1 for \0 */
1282 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1285 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1291 (*pp_data)[*pi_data] = '\0';
1296 static int MacroParse( macro_t *m, uint8_t *psz_src )
1298 uint8_t *dup = strdup( psz_src );
1303 #define EXTRACT( name, l ) \
1305 p = strchr( src, '"' ); \
1310 m->name = strdup( src ); \
1331 if( !strncmp( src, "id=\"", 4 ) )
1335 else if( !strncmp( src, "param1=\"", 8 ) )
1337 EXTRACT( param1, 8 );
1339 else if( !strncmp( src, "param2=\"", 8 ) )
1341 EXTRACT( param2, 8 );
1348 if( strstr( src, "/>" ) )
1350 src = strstr( src, "/>" ) + 2;
1354 src += strlen( src );
1359 m->id = strdup( "" );
1361 if( m->param1 == NULL )
1363 m->param1 = strdup( "" );
1365 if( m->param2 == NULL )
1367 m->param2 = strdup( "" );
1376 static void MacroClean( macro_t *m )
1434 StrToMacroTypeTab [] =
1436 { "control", MVLC_CONTROL },
1437 /* player control */
1438 { "play", MVLC_PLAY },
1439 { "stop", MVLC_STOP },
1440 { "pause", MVLC_PAUSE },
1441 { "next", MVLC_NEXT },
1442 { "previous", MVLC_PREVIOUS },
1443 { "seek", MVLC_SEEK },
1444 { "keep", MVLC_KEEP },
1445 { "fullscreen", MVLC_FULLSCREEN },
1446 { "volume", MVLC_VOLUME },
1448 /* playlist management */
1449 { "add", MVLC_ADD },
1450 { "delete", MVLC_DEL },
1451 { "empty", MVLC_EMPTY },
1452 { "sort", MVLC_SORT },
1453 { "move", MVLC_MOVE },
1456 { "close", MVLC_CLOSE },
1457 { "shutdown", MVLC_SHUTDOWN },
1460 { "vlm_new", MVLC_VLM_NEW },
1461 { "vlm_setup", MVLC_VLM_SETUP },
1462 { "vlm_del", MVLC_VLM_DEL },
1463 { "vlm_play", MVLC_VLM_PLAY },
1464 { "vlm_pause", MVLC_VLM_PAUSE },
1465 { "vlm_stop", MVLC_VLM_STOP },
1466 { "vlm_seek", MVLC_VLM_SEEK },
1467 { "vlm_load", MVLC_VLM_LOAD },
1468 { "vlm_save", MVLC_VLM_SAVE },
1470 { "rpn", MVLC_RPN },
1472 { "foreach", MVLC_FOREACH },
1473 { "value", MVLC_VALUE },
1476 { "else", MVLC_ELSE },
1477 { "end", MVLC_END },
1478 { "get", MVLC_GET },
1479 { "set", MVLC_SET },
1480 { "int", MVLC_INT },
1481 { "float", MVLC_FLOAT },
1482 { "string", MVLC_STRING },
1485 { NULL, MVLC_UNKNOWN }
1488 static int StrToMacroType( char *name )
1492 if( !name || *name == '\0')
1494 return MVLC_UNKNOWN;
1496 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1498 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1500 return StrToMacroTypeTab[i].i_type;
1503 return MVLC_UNKNOWN;
1506 static void MacroDo( httpd_file_sys_t *p_args,
1508 uint8_t *p_request, int i_request,
1509 uint8_t **pp_data, int *pi_data,
1512 intf_thread_t *p_intf = p_args->p_intf;
1513 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1516 #define ALLOC( l ) \
1518 int __i__ = *pp_dst - *pp_data; \
1520 *pp_data = realloc( *pp_data, *pi_data ); \
1521 *pp_dst = (*pp_data) + __i__; \
1523 #define PRINT( str ) \
1524 ALLOC( strlen( str ) + 1 ); \
1525 *pp_dst += sprintf( *pp_dst, str );
1527 #define PRINTS( str, s ) \
1528 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1530 char * psz_cur = *pp_dst; \
1531 *pp_dst += sprintf( *pp_dst, str, s ); \
1532 while( psz_cur && *psz_cur ) \
1534 /* Prevent script injection */ \
1535 if( *psz_cur == '<' ) *psz_cur = '*'; \
1536 if( *psz_cur == '>' ) *psz_cur = '*'; \
1541 switch( StrToMacroType( m->id ) )
1544 if( i_request <= 0 )
1548 uri_extract_value( p_request, "control", control, 512 );
1549 if( *m->param1 && !strstr( m->param1, control ) )
1551 msg_Warn( p_intf, "unauthorized control=%s", control );
1554 switch( StrToMacroType( control ) )
1561 uri_extract_value( p_request, "item", item, 512 );
1562 i_item = atoi( item );
1563 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
1564 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1568 playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1569 msg_Dbg( p_intf, "requested playlist stop" );
1572 playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1573 msg_Dbg( p_intf, "requested playlist pause" );
1576 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1577 p_sys->p_playlist->i_index + 1 );
1578 msg_Dbg( p_intf, "requested playlist next" );
1581 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1582 p_sys->p_playlist->i_index - 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 , "group" ) )
1969 playlist_SortGroup( p_sys->p_playlist , i_order );
1970 msg_Dbg( p_intf, "requested playlist sort by group (%d)" , i_order );
1971 } else if( !strcmp( type , "author" ) )
1973 playlist_SortAuthor( p_sys->p_playlist , i_order );
1974 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
1975 } else if( !strcmp( type , "shuffle" ) )
1977 playlist_Sort( p_sys->p_playlist , SORT_RANDOM, ORDER_NORMAL );
1978 msg_Dbg( p_intf, "requested playlist shuffle");
1989 uri_extract_value( p_request, "psz_pos", psz_pos, 6 );
1990 uri_extract_value( p_request, "psz_newpos", psz_newpos, 6 );
1991 i_pos = atoi( psz_pos );
1992 i_newpos = atoi( psz_newpos );
1993 if ( i_pos < i_newpos )
1995 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
1997 playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
1999 msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
2003 /* admin function */
2007 uri_extract_value( p_request, "id", id, 512 );
2008 msg_Dbg( p_intf, "requested close id=%s", id );
2010 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
2012 msg_Warn( p_intf, "close failed for id=%s", id );
2019 msg_Dbg( p_intf, "requested shutdown" );
2020 p_intf->p_vlc->b_die = VLC_TRUE;
2025 case MVLC_VLM_SETUP:
2027 static const char *vlm_properties[11] =
2030 "enabled", "disabled", "loop", "unloop",
2032 "input", "output", "option", "date", "period", "repeat", "append",
2034 vlm_message_t *vlm_answer;
2036 char *psz = malloc( strlen( p_request ) + 1000 );
2041 if( p_intf->p_sys->p_vlm == NULL )
2042 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2044 if( p_intf->p_sys->p_vlm == NULL ) break;
2046 uri_extract_value( p_request, "name", name, 512 );
2047 if( StrToMacroType( control ) == MVLC_VLM_NEW )
2050 uri_extract_value( p_request, "type", type, 20 );
2051 p += sprintf( psz, "new %s %s", name, type );
2055 p += sprintf( psz, "setup %s", name );
2057 /* Parse the request */
2058 for( i = 0; i < 11; i++ )
2061 uri_extract_value( p_request, vlm_properties[i], val, 512 );
2062 uri_decode_url_encoded( val );
2063 if( strlen( val ) > 0 && i >= 4 )
2065 p += sprintf( p, " %s %s", vlm_properties[i], val );
2067 else if( uri_test_param( p_request, vlm_properties[i] ) && i < 4 )
2069 p += sprintf( p, " %s", vlm_properties[i] );
2072 fprintf( stderr, "vlm_ExecuteCommand: %s\n", psz );
2073 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2074 if( vlm_answer->psz_value == NULL ) /* there is no error */
2076 vlm_error = strdup( "" );
2080 vlm_error = malloc( strlen(vlm_answer->psz_name) +
2081 strlen(vlm_answer->psz_value) +
2082 strlen( " : ") + 1 );
2083 sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
2084 vlm_answer->psz_value );
2087 mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
2089 vlm_MessageDelete( vlm_answer );
2097 vlm_message_t *vlm_answer;
2100 if( p_intf->p_sys->p_vlm == NULL )
2101 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2103 if( p_intf->p_sys->p_vlm == NULL ) break;
2105 uri_extract_value( p_request, "name", name, 512 );
2106 sprintf( psz, "del %s", name );
2108 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2109 /* FIXME do a vlm_answer -> var stack conversion */
2110 vlm_MessageDelete( vlm_answer );
2115 case MVLC_VLM_PAUSE:
2119 vlm_message_t *vlm_answer;
2122 if( p_intf->p_sys->p_vlm == NULL )
2123 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2125 if( p_intf->p_sys->p_vlm == NULL ) break;
2127 uri_extract_value( p_request, "name", name, 512 );
2128 if( StrToMacroType( control ) == MVLC_VLM_PLAY )
2129 sprintf( psz, "control %s play", name );
2130 else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
2131 sprintf( psz, "control %s pause", name );
2132 else if( StrToMacroType( control ) == MVLC_VLM_STOP )
2133 sprintf( psz, "control %s stop", name );
2134 else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
2137 uri_extract_value( p_request, "percent", percent, 512 );
2138 sprintf( psz, "control %s seek %s", name, percent );
2141 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2142 /* FIXME do a vlm_answer -> var stack conversion */
2143 vlm_MessageDelete( vlm_answer );
2149 vlm_message_t *vlm_answer;
2153 if( p_intf->p_sys->p_vlm == NULL )
2154 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2156 if( p_intf->p_sys->p_vlm == NULL ) break;
2158 uri_extract_value( p_request, "file", file, 512 );
2159 uri_decode_url_encoded( file );
2161 if( StrToMacroType( control ) == MVLC_VLM_LOAD )
2162 sprintf( psz, "load %s", file );
2164 sprintf( psz, "save %s", file );
2166 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2167 /* FIXME do a vlm_answer -> var stack conversion */
2168 vlm_MessageDelete( vlm_answer );
2175 PRINTS( "<!-- control param(%s) unsuported -->", control );
2187 if( i_request <= 0 ||
2188 *m->param1 == '\0' ||
2189 strstr( p_request, m->param1 ) == NULL )
2193 uri_extract_value( p_request, m->param1, value, 512 );
2194 uri_decode_url_encoded( value );
2196 switch( StrToMacroType( m->param2 ) )
2200 config_PutInt( p_intf, m->param1, i );
2204 config_PutFloat( p_intf, m->param1, f );
2207 config_PutPsz( p_intf, m->param1, value );
2210 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
2221 if( *m->param1 == '\0' )
2226 switch( StrToMacroType( m->param2 ) )
2229 i = config_GetInt( p_intf, m->param1 );
2230 sprintf( value, "%i", i );
2233 f = config_GetFloat( p_intf, m->param1 );
2234 sprintf( value, "%f", f );
2237 psz = config_GetPsz( p_intf, m->param1 );
2238 sprintf( value, "%s", psz ? psz : "" );
2239 if( psz ) free( psz );
2242 sprintf( value, "invalid type(%s) in set", m->param2 );
2245 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
2246 PRINTS( "%s", value );
2255 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2256 s = SSPop( &p_args->stack );
2257 v = mvar_GetValue( p_args->vars, s );
2261 v = s = SSPop( &p_args->stack );
2269 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2274 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2275 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2283 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
2290 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
2295 i_skip = MacroParse( &m, src );
2297 i_id = StrToMacroType( m.id );
2314 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2315 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2317 return src + ( b_after ? i_skip : 0 );
2319 else if( i_level < 0 )
2335 static void Execute( httpd_file_sys_t *p_args,
2336 uint8_t *p_request, int i_request,
2337 uint8_t **pp_data, int *pi_data,
2339 uint8_t *_src, uint8_t *_end )
2341 intf_thread_t *p_intf = p_args->p_intf;
2343 uint8_t *src, *dup, *end;
2344 uint8_t *dst = *pp_dst;
2346 src = dup = malloc( _end - _src + 1 );
2347 end = src +( _end - _src );
2349 memcpy( src, _src, _end - _src );
2352 /* we parse searching <vlc */
2358 p = strstr( src, "<vlc" );
2359 if( p < end && p == src )
2363 src += MacroParse( &m, src );
2365 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2367 switch( StrToMacroType( m.id ) )
2374 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2375 if( SSPopN( &p_args->stack, p_args->vars ) )
2383 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2387 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2391 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2394 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2398 else if( i_test == 1 )
2401 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2403 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2407 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2416 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2417 uint8_t *start = src;
2418 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2425 if( !strcmp( m.param2, "integer" ) )
2427 char *arg = SSPop( &p_args->stack );
2428 index = mvar_IntegerSetNew( m.param1, arg );
2431 else if( !strcmp( m.param2, "directory" ) )
2433 char *arg = SSPop( &p_args->stack );
2434 index = mvar_FileSetNew( m.param1, arg );
2437 else if( !strcmp( m.param2, "playlist" ) )
2439 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2441 else if( !strcmp( m.param2, "information" ) )
2443 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2445 else if( !strcmp( m.param2, "vlm" ) )
2447 if( p_intf->p_sys->p_vlm == NULL )
2448 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2449 index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
2452 else if( !strcmp( m.param2, "hosts" ) )
2454 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2456 else if( !strcmp( m.param2, "urls" ) )
2458 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2460 else if( !strcmp( m.param2, "connections" ) )
2462 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2465 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2467 index = mvar_Duplicate( v );
2471 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2476 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2478 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2480 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2483 f->name = strdup( m.param1 );
2486 mvar_PushVar( p_args->vars, f );
2487 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2488 mvar_RemoveVar( p_args->vars, f );
2492 mvar_Delete( index );
2499 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2507 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2510 int i_index = dst - *pp_data;
2513 *pp_data = realloc( *pp_data, *pi_data );
2514 dst = (*pp_data) + i_index;
2516 memcpy( dst, src, i_copy );
2526 /****************************************************************************
2528 ****************************************************************************
2529 * a file with b_html is parsed and all "macro" replaced
2530 * <vlc id="macro name" [param1="" [param2=""]] />
2533 ****************************************************************************/
2534 static int HttpCallback( httpd_file_sys_t *p_args,
2535 httpd_file_t *p_file,
2537 uint8_t **pp_data, int *pi_data )
2539 int i_request = p_request ? strlen( p_request ) : 0;
2543 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2545 p = *pp_data = malloc( 10240 );
2548 return VLC_EGENERIC;
2550 p += sprintf( p, "<html>\n" );
2551 p += sprintf( p, "<head>\n" );
2552 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2553 p += sprintf( p, "</head>\n" );
2554 p += sprintf( p, "<body>\n" );
2555 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2556 p += sprintf( p, "<hr />\n" );
2557 p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
2558 p += sprintf( p, "</body>\n" );
2559 p += sprintf( p, "</html>\n" );
2561 *pi_data = strlen( *pp_data );
2566 if( !p_args->b_html )
2568 FileLoad( f, pp_data, pi_data );
2576 char position[4]; /* percentage */
2577 char time[12]; /* in seconds */
2578 char length[12]; /* in seconds */
2579 audio_volume_t i_volume;
2583 #define p_sys p_args->p_intf->p_sys
2584 if( p_sys->p_input )
2586 var_Get( p_sys->p_input, "position", &val);
2587 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2588 var_Get( p_sys->p_input, "time", &val);
2589 sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2590 var_Get( p_sys->p_input, "length", &val);
2591 sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2593 var_Get( p_sys->p_input, "state", &val );
2594 if( val.i_int == PLAYING_S )
2596 sprintf( state, "playing" );
2597 } else if( val.i_int == PAUSE_S )
2599 sprintf( state, "paused" );
2602 sprintf( state, "stop" );
2606 sprintf( position, "%d", 0 );
2607 sprintf( time, "%d", 0 );
2608 sprintf( length, "%d", 0 );
2609 sprintf( state, "stop" );
2613 aout_VolumeGet( p_args->p_intf , &i_volume );
2614 sprintf( volume , "%d" , (int)i_volume );
2616 p_args->vars = mvar_New( "variables", "" );
2617 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2618 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2619 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
2620 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2621 mvar_AppendNewVar( p_args->vars, "stream_position", position );
2622 mvar_AppendNewVar( p_args->vars, "stream_time", time );
2623 mvar_AppendNewVar( p_args->vars, "stream_length", length );
2624 mvar_AppendNewVar( p_args->vars, "volume", volume );
2625 mvar_AppendNewVar( p_args->vars, "stream_state", state );
2627 SSInit( &p_args->stack );
2629 /* first we load in a temporary buffer */
2630 FileLoad( f, &p_buffer, &i_buffer );
2632 /* allocate output */
2633 *pi_data = i_buffer + 1000;
2634 dst = *pp_data = malloc( *pi_data );
2636 /* we parse executing all <vlc /> macros */
2637 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2640 *pi_data = dst - *pp_data;
2642 SSClean( &p_args->stack );
2643 mvar_Delete( p_args->vars );
2652 /****************************************************************************
2654 ****************************************************************************/
2655 static int uri_test_param( char *psz_uri, const char *psz_name )
2659 while( (p = strstr( p, psz_name )) )
2661 /* Verify that we are dealing with a post/get argument */
2662 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2671 static char *uri_extract_value( char *psz_uri, const char *psz_name,
2672 char *psz_value, int i_value_max )
2676 while( (p = strstr( p, psz_name )) )
2678 /* Verify that we are dealing with a post/get argument */
2679 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2688 p += strlen( psz_name );
2689 if( *p == '=' ) p++;
2691 if( strchr( p, '&' ) )
2693 i_len = strchr( p, '&' ) - p;
2697 /* for POST method */
2698 if( strchr( p, '\n' ) )
2700 i_len = strchr( p, '\n' ) - p;
2701 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2705 i_len = strlen( p );
2708 i_len = __MIN( i_value_max - 1, i_len );
2711 strncpy( psz_value, p, i_len );
2712 psz_value[i_len] = '\0';
2716 strncpy( psz_value, "", i_value_max );
2722 strncpy( psz_value, "", i_value_max );
2728 static void uri_decode_url_encoded( char *psz )
2730 char *dup = strdup( psz );
2748 *psz++ = strtol( val, NULL, 16 );
2750 else if( *p == '+' )
2764 /****************************************************************************
2765 * Light RPN evaluator
2766 ****************************************************************************/
2767 static void SSInit( rpn_stack_t *st )
2772 static void SSClean( rpn_stack_t *st )
2774 while( st->i_stack > 0 )
2776 free( st->stack[--st->i_stack] );
2780 static void SSPush( rpn_stack_t *st, char *s )
2782 if( st->i_stack < STACK_MAX )
2784 st->stack[st->i_stack++] = strdup( s );
2788 static char * SSPop( rpn_stack_t *st )
2790 if( st->i_stack <= 0 )
2792 return strdup( "" );
2796 return st->stack[--st->i_stack];
2800 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2809 i = strtol( name, &end, 0 );
2812 value = mvar_GetValue( vars, name );
2820 static void SSPushN( rpn_stack_t *st, int i )
2824 sprintf( v, "%d", i );
2828 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2835 while( *exp == ' ' )
2842 /* extract string */
2845 while( *exp && *exp != '\'' )
2856 p = strchr( exp, ' ' );
2861 exp += strlen( exp );
2866 strncpy( s, exp, i );
2877 /* 1. Integer function */
2878 if( !strcmp( s, "!" ) )
2880 SSPushN( st, ~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 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2898 else if( !strcmp( s, "-" ) )
2900 int j = SSPopN( st, vars );
2901 int i = SSPopN( st, vars );
2902 SSPushN( st, i - j );
2904 else if( !strcmp( s, "*" ) )
2906 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2908 else if( !strcmp( s, "/" ) )
2912 j = SSPopN( st, vars );
2913 i = SSPopN( st, vars );
2915 SSPushN( st, j != 0 ? i / j : 0 );
2917 else if( !strcmp( s, "%" ) )
2921 j = SSPopN( st, vars );
2922 i = SSPopN( st, vars );
2924 SSPushN( st, j != 0 ? i % j : 0 );
2926 /* 2. integer tests */
2927 else if( !strcmp( s, "=" ) )
2929 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2931 else if( !strcmp( s, "<" ) )
2933 int j = SSPopN( st, vars );
2934 int i = SSPopN( st, vars );
2936 SSPushN( st, i < j ? -1 : 0 );
2938 else if( !strcmp( s, ">" ) )
2940 int j = SSPopN( st, vars );
2941 int i = SSPopN( st, vars );
2943 SSPushN( st, i > j ? -1 : 0 );
2945 else if( !strcmp( s, "<=" ) )
2947 int j = SSPopN( st, vars );
2948 int i = SSPopN( st, vars );
2950 SSPushN( st, i <= j ? -1 : 0 );
2952 else if( !strcmp( s, ">=" ) )
2954 int j = SSPopN( st, vars );
2955 int i = SSPopN( st, vars );
2957 SSPushN( st, i >= j ? -1 : 0 );
2959 /* 3. string functions */
2960 else if( !strcmp( s, "strcat" ) )
2962 char *s2 = SSPop( st );
2963 char *s1 = SSPop( st );
2964 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2974 else if( !strcmp( s, "strcmp" ) )
2976 char *s2 = SSPop( st );
2977 char *s1 = SSPop( st );
2979 SSPushN( st, strcmp( s1, s2 ) );
2983 else if( !strcmp( s, "strncmp" ) )
2985 int n = SSPopN( st, vars );
2986 char *s2 = SSPop( st );
2987 char *s1 = SSPop( st );
2989 SSPushN( st, strncmp( s1, s2 , n ) );
2993 else if( !strcmp( s, "strsub" ) )
2995 int n = SSPopN( st, vars );
2996 int m = SSPopN( st, vars );
2998 char *s = SSPop( st );
3010 str = malloc( i_len + 1 );
3012 memcpy( str, s + m - 1, i_len );
3013 str[ i_len ] = '\0';
3019 else if( !strcmp( s, "strlen" ) )
3021 char *str = SSPop( st );
3023 SSPushN( st, strlen( str ) );
3026 /* 4. stack functions */
3027 else if( !strcmp( s, "dup" ) )
3029 char *str = SSPop( st );
3034 else if( !strcmp( s, "drop" ) )
3036 char *str = SSPop( st );
3039 else if( !strcmp( s, "swap" ) )
3041 char *s1 = SSPop( st );
3042 char *s2 = SSPop( st );
3049 else if( !strcmp( s, "flush" ) )
3054 else if( !strcmp( s, "store" ) )
3056 char *value = SSPop( st );
3057 char *name = SSPop( st );
3059 mvar_PushNewVar( vars, name, value );
3063 else if( !strcmp( s, "value" ) )
3065 char *name = SSPop( st );
3066 char *value = mvar_GetValue( vars, name );
3068 SSPush( st, value );
3072 else if( !strcmp( s, "url_extract" ) )
3074 char *url = mvar_GetValue( vars, "url_value" );
3075 char *name = SSPop( st );
3078 uri_extract_value( url, name, value, 512 );
3079 uri_decode_url_encoded( value );
3080 SSPush( st, value );
3089 /**********************************************************************
3090 * Find_end_MRL: Find the end of the sentence :
3091 * this function parses the string psz and find the end of the item
3092 * and/or option with detecting the " and ' problems.
3093 * returns NULL if an error is detected, otherwise, returns a pointer
3094 * of the end of the sentence (after the last character)
3095 **********************************************************************/
3096 static char *Find_end_MRL( char *psz )
3106 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
3108 if( *s_sent == '\'' )
3110 s_sent = Find_end_MRL( s_sent );
3112 if( s_sent == NULL )
3122 if( *s_sent == '\"' )
3126 } else /* *s_sent == '\0' , which means the number of " is incorrect */
3136 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
3138 if( *s_sent == '\"' )
3140 s_sent = Find_end_MRL( s_sent );
3142 if( s_sent == NULL )
3152 if( *s_sent == '\'' )
3156 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
3162 default: /* now we can look for spaces */
3164 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
3166 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
3168 s_sent = Find_end_MRL( s_sent );
3179 /**********************************************************************
3180 * parse_MRL: parse the MRL, find the mrl string and the options,
3181 * create an item with all information in it, and return the item.
3182 * return NULL if there is an error.
3183 **********************************************************************/
3184 playlist_item_t * parse_MRL( intf_thread_t *p_intf, char *psz )
3186 char **ppsz_options = NULL;
3193 playlist_item_t * p_item = NULL;
3195 /* In case there is spaces before the mrl */
3196 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
3201 /* extract the mrl */
3202 s_temp = strstr( s_mrl , " :" );
3203 if( s_temp == NULL )
3205 s_temp = s_mrl + strlen( s_mrl );
3208 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
3215 /* if the mrl is between " or ', we must remove them */
3216 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
3218 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
3219 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
3220 mrl[ s_temp - s_mrl - 2 ] = '\0';
3223 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
3224 strncpy( mrl , s_mrl , s_temp - s_mrl );
3225 mrl[ s_temp - s_mrl ] = '\0';
3230 /* now we can take care of the options */
3231 while( (*s_mrl != '\0') && (i_error == 0) )
3240 case ':': /* an option */
3242 s_temp = Find_end_MRL( s_mrl );
3244 if( s_temp == NULL )
3251 ppsz_options = realloc( ppsz_options , i_options *
3253 ppsz_options[ i_options - 1 ] =
3254 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
3256 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
3259 /* don't forget to finish the string with a '\0' */
3260 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
3280 /* now create an item */
3281 p_item = playlist_ItemNew( p_intf, mrl, mrl);
3282 for( i = 0 ; i< i_options ; i++ )
3284 playlist_ItemAddOption( p_item, ppsz_options[i] );
3288 for( i = 0 ; i < i_options ; i++ )
3290 free( ppsz_options[i] );
3292 free( ppsz_options );