1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: http.c,v 1.31 2003/11/09 05:22:56 garf Exp $
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 *****************************************************************************/
41 #include <vlc/vout.h> /* for fullscreen */
45 #ifdef HAVE_SYS_STAT_H
46 # include <sys/stat.h>
57 #elif defined( WIN32 ) && !defined( UNDER_CE )
61 #if (!defined( WIN32 ) || defined(__MINGW32__))
62 /* Mingw has its own version of dirent */
66 /*****************************************************************************
68 *****************************************************************************/
69 static int Activate ( vlc_object_t * );
70 static void Close ( vlc_object_t * );
72 #define HOST_TEXT N_( "Host address" )
73 #define HOST_LONGTEXT N_( \
74 "You can set the address and port on which the http interface will bind" )
75 #define SRC_TEXT N_( "Source directory" )
76 #define SRC_LONGTEXT N_( "Source directory" )
79 set_description( _("HTTP remote control interface") );
80 add_category_hint( N_("HTTP remote control"), NULL, VLC_TRUE );
81 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
82 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
83 set_capability( "interface", 0 );
84 set_callbacks( Activate, Close );
88 /*****************************************************************************
90 *****************************************************************************/
91 static void Run ( intf_thread_t *p_intf );
93 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
96 static int DirectoryCheck( char *psz_dir )
100 #ifdef HAVE_SYS_STAT_H
101 struct stat stat_info;
103 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
109 if( ( p_dir = opendir( psz_dir ) ) == NULL )
119 static int http_get( httpd_file_callback_args_t *p_args,
120 uint8_t *p_request, int i_request,
121 uint8_t **pp_data, int *pi_data );
123 static char *uri_extract_value( char *psz_uri, char *psz_name,
124 char *psz_value, int i_value_max );
125 static void uri_decode_url_encoded( char *psz );
127 /*****************************************************************************
129 *****************************************************************************/
130 typedef struct mvar_s
136 struct mvar_s **field;
139 #define STACK_MAX 100
142 char *stack[STACK_MAX];
146 struct httpd_file_callback_args_t
148 intf_thread_t *p_intf;
149 httpd_file_t *p_file;
155 /* inited for each access */
163 httpd_host_t *p_httpd_host;
166 httpd_file_callback_args_t **pp_files;
168 playlist_t *p_playlist;
169 input_thread_t *p_input;
174 /*****************************************************************************
175 * Activate: initialize and create stuff
176 *****************************************************************************/
177 static int Activate( vlc_object_t *p_this )
179 intf_thread_t *p_intf = (intf_thread_t*)p_this;
182 char *psz_address = "";
186 psz_host = config_GetPsz( p_intf, "http-host" );
190 psz_address = psz_host;
192 psz_parser = strchr( psz_host, ':' );
195 *psz_parser++ = '\0';
196 i_port = atoi( psz_parser );
204 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
205 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
208 return( VLC_ENOMEM );
210 p_sys->p_playlist = NULL;
211 p_sys->p_input = NULL;
213 if( ( p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_intf), VLC_TRUE ) ) == NULL )
215 msg_Err( p_intf, "cannot create/find httpd" );
220 if( ( p_sys->p_httpd_host =
221 p_sys->p_httpd->pf_register_host( p_sys->p_httpd,
222 psz_address, i_port ) ) == NULL )
224 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
225 httpd_Release( p_sys->p_httpd );
236 p_sys->pp_files = malloc( sizeof( httpd_file_callback_args_t *) );
237 if( !p_sys->pp_files )
239 return( VLC_ENOMEM );
242 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
243 ( defined(WIN32) && !defined(UNDER_CE ) )
244 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
246 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
247 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
250 return( VLC_ENOMEM );
253 sprintf( psz_src, "%s/http", psz_vlcpath);
255 sprintf( psz_src, "%s/share/http", psz_vlcpath);
259 psz_src = config_GetPsz( p_intf, "http-src" );
261 if( !psz_src || *psz_src == '\0' )
263 if( !DirectoryCheck( "share/http" ) )
265 psz_src = strdup( "share/http" );
267 else if( !DirectoryCheck( DATA_PATH "/http" ) )
269 psz_src = strdup( DATA_PATH "/http" );
274 if( !psz_src || *psz_src == '\0' )
276 msg_Err( p_intf, "invalid src dir" );
280 /* remove trainling \ or / */
281 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
282 psz_src[strlen( psz_src ) - 1] == '/' )
284 psz_src[strlen( psz_src ) - 1] = '\0';
287 ParseDirectory( p_intf, psz_src, psz_src );
290 if( p_sys->i_files <= 0 )
292 msg_Err( p_intf, "cannot find any files (%s)", psz_src );
295 p_intf->pf_run = Run;
300 free( p_sys->pp_files );
301 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
302 p_sys->p_httpd_host );
303 httpd_Release( p_sys->p_httpd );
308 /*****************************************************************************
309 * CloseIntf: destroy interface
310 *****************************************************************************/
311 void Close ( vlc_object_t *p_this )
313 intf_thread_t *p_intf = (intf_thread_t *)p_this;
314 intf_sys_t *p_sys = p_intf->p_sys;
318 for( i = 0; i < p_sys->i_files; i++ )
320 p_sys->p_httpd->pf_unregister_file( p_sys->p_httpd,
321 p_sys->pp_files[i]->p_file );
322 /* do not free mime */
323 free( p_sys->pp_files[i]->file );
324 free( p_sys->pp_files[i]->name );
325 free( p_sys->pp_files[i] );
327 free( p_sys->pp_files );
328 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
329 p_sys->p_httpd_host );
330 httpd_Release( p_sys->p_httpd );
334 /*****************************************************************************
335 * Run: http interface thread
336 *****************************************************************************/
337 static void Run( intf_thread_t *p_intf )
339 intf_sys_t *p_sys = p_intf->p_sys;
341 while( !p_intf->b_die )
343 /* get the playlist */
344 if( p_sys->p_playlist == NULL )
346 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
349 /* Manage the input part */
350 if( p_sys->p_input == NULL )
352 if( p_sys->p_playlist )
355 vlc_object_find( p_sys->p_playlist,
360 else if( p_sys->p_input->b_dead )
362 vlc_object_release( p_sys->p_input );
363 p_sys->p_input = NULL;
368 msleep( INTF_IDLE_SLEEP );
373 vlc_object_release( p_sys->p_input );
374 p_sys->p_input = NULL;
377 if( p_sys->p_playlist )
379 vlc_object_release( p_sys->p_playlist );
380 p_sys->p_playlist = NULL;
385 /*****************************************************************************
387 *****************************************************************************/
388 #define MAX_DIR_SIZE 10240
390 /****************************************************************************
391 * FileToUrl: create a good name for an url from filename
392 ****************************************************************************/
393 static char *FileToUrl( char *name )
397 url = p = malloc( strlen( name ) + 1 );
405 while( *name == '\\' || *name == '/' )
407 while( *name == '/' )
417 /* convert '\\' into '/' */
430 if( ( p = strrchr( url, '/' ) ) != NULL )
432 if( !strncmp( p, "/index.", 7 ) )
440 /****************************************************************************
441 * FileToMime: XXX duplicated with modules/access_out/http.c
442 ****************************************************************************/
449 { ".htm", "text/html" },
450 { ".html", "text/html" },
452 { ".css", "text/css" },
455 { ".avi", "video/avi" },
456 { ".asf", "video/x-ms-asf" },
457 { ".m1a", "audio/mpeg" },
458 { ".m2a", "audio/mpeg" },
459 { ".m1v", "video/mpeg" },
460 { ".m2v", "video/mpeg" },
461 { ".mp2", "audio/mpeg" },
462 { ".mp3", "audio/mpeg" },
463 { ".mpa", "audio/mpeg" },
464 { ".mpg", "video/mpeg" },
465 { ".mpeg", "video/mpeg" },
466 { ".mpe", "video/mpeg" },
467 { ".mov", "video/quicktime" },
468 { ".moov", "video/quicktime" },
469 { ".ogg", "application/ogg" },
470 { ".ogm", "application/ogg" },
471 { ".wav", "audio/wav" },
477 static char *FileToMime( char *psz_name )
481 psz_ext = strrchr( psz_name, '.' );
486 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
488 if( !strcmp( http_mime[i].psz_ext, psz_ext ) )
490 return( http_mime[i].psz_mime );
494 return( "application/octet-stream" );
497 /****************************************************************************
498 * ParseDirectory: parse recursively a directory, adding each file
499 ****************************************************************************/
500 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
503 intf_sys_t *p_sys = p_intf->p_sys;
504 char dir[MAX_DIR_SIZE];
505 #ifdef HAVE_SYS_STAT_H
506 struct stat stat_info;
509 struct dirent *p_dir_content;
513 char *password = NULL;
515 #ifdef HAVE_SYS_STAT_H
516 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
522 if( ( p_dir = opendir( psz_dir ) ) == NULL )
524 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
528 msg_Dbg( p_intf, "dir=%s", psz_dir );
530 sprintf( dir, "%s/.access", psz_dir );
531 if( ( file = fopen( dir, "r" ) ) != NULL )
536 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
538 i_size = fread( line, 1, 1023, file );
542 while( i_size > 0 && ( line[i_size-1] == '\n' ||
543 line[i_size-1] == '\r' ) )
550 p = strchr( line, ':' );
554 user = strdup( line );
555 password = strdup( p );
558 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
559 user, password, i_size );
566 /* parse psz_src dir */
567 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
572 if( p_dir_content->d_name[0] == '.' )
576 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
577 if( ParseDirectory( p_intf, psz_root, dir ) )
579 #define f p_sys->pp_files[p_sys->i_files]
580 f = malloc( sizeof( httpd_file_callback_args_t ) );
583 msg_Err( p_intf, "Out of memory" );
584 return( VLC_ENOMEM );
587 f->file = strdup( dir );
588 f->name = FileToUrl( &dir[strlen( psz_root )] );
589 f->mime = FileToMime( &dir[strlen( psz_root )] );
591 if( !f->name || !f->mime )
593 msg_Err( p_intf , "Unable to parse directory" );
594 return( VLC_ENOMEM );
596 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)",
597 f->file, f->name, f->mime );
600 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
608 p_sys->pp_files = realloc( p_sys->pp_files,
609 (p_sys->i_files+1) * sizeof( httpd_file_callback_args_t ) );
611 #define fold p_sys->pp_files[p_sys->i_files-1]
613 /* FIXME for rep/ add rep (it would be better to do a redirection) */
614 if( p_sys->i_files && strlen(fold->name) > 1 &&
615 fold->name[strlen(fold->name) - 1] == '/' )
617 f = malloc( sizeof( httpd_file_callback_args_t ) );
620 msg_Err( p_intf, "Out of memory" );
621 return( VLC_ENOMEM );
624 f->file = strdup( fold->file );
625 f->name = strdup( fold->name );
626 f->mime = fold->mime;
628 f->name[strlen(f->name) - 1] = '\0';
629 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)", f->file, f->name,
632 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
641 realloc( p_sys->pp_files, (p_sys->i_files+1) *
642 sizeof( httpd_file_callback_args_t ) );
661 /****************************************************************************
662 * var and set handling
663 ****************************************************************************/
665 static mvar_t *mvar_New( char *name, char *value )
667 mvar_t *v = malloc( sizeof( mvar_t ) );
669 if( !v ) return NULL;
670 v->name = strdup( name );
671 v->value = strdup( value ? value : "" );
674 v->field = malloc( sizeof( mvar_t * ) );
680 static void mvar_Delete( mvar_t *v )
687 for( i = 0; i < v->i_field; i++ )
689 mvar_Delete( v->field[i] );
695 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
697 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
698 v->field[v->i_field] = f;
702 static mvar_t *mvar_Duplicate( mvar_t *v )
707 n = mvar_New( v->name, v->value );
708 for( i = 0; i < v->i_field; i++ )
710 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
716 static void mvar_PushVar( mvar_t *v, mvar_t *f )
718 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
721 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
727 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
730 for( i = 0; i < v->i_field; i++ )
732 if( v->field[i] == f )
737 if( i >= v->i_field )
742 if( i + 1 < v->i_field )
744 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
747 /* FIXME should do a realloc */
750 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
753 char base[512], *field, *p;
756 /* format: name[index].field */
758 field = strchr( name, '.' );
761 int i = field - name;
762 strncpy( base, name, i );
768 strcpy( base, name );
771 if( ( p = strchr( base, '[' ) ) )
774 sscanf( p, "%d]", &i_index );
785 for( i = 0; i < s->i_field; i++ )
787 if( !strcmp( s->field[i]->name, base ) )
797 return mvar_GetVar( s->field[i], field );
811 static char *mvar_GetValue( mvar_t *v, char *field )
819 mvar_t *f = mvar_GetVar( v, field );
831 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
833 mvar_t *f = mvar_New( name, value );
834 mvar_PushVar( vars, f );
837 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
839 mvar_t *f = mvar_New( name, value );
840 mvar_AppendVar( vars, f );
844 /* arg= start[:stop[:step]],.. */
845 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
847 char *dup = strdup( arg );
849 mvar_t *s = mvar_New( name, "set" );
851 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
856 int i_start,i_stop,i_step;
859 p = strchr( str, ',' );
866 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
867 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
874 else if( i_match == 2 )
876 i_step = i_start < i_stop ? 1 : -1;
883 if( ( i_start < i_stop && i_step > 0 ) ||
884 ( i_start > i_stop && i_step < 0 ) )
886 for( i = i_start; ; i += i_step )
890 if( ( i_step > 0 && i > i_stop ) ||
891 ( i_step < 0 && i < i_stop ) )
896 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
897 sprintf( value, "%d", i );
899 mvar_PushNewVar( s, name, value );
910 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
912 mvar_t *s = mvar_New( name, "set" );
915 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
917 vlc_mutex_lock( &p_pl->object_lock );
918 for( i = 0; i < p_pl->i_size; i++ )
920 mvar_t *itm = mvar_New( name, "set" );
923 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
924 mvar_AppendNewVar( itm, "current", value );
926 sprintf( value, "%d", i );
927 mvar_AppendNewVar( itm, "index", value );
929 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->psz_name );
931 mvar_AppendVar( s, itm );
933 vlc_mutex_unlock( &p_pl->object_lock );
938 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
940 mvar_t *s = mvar_New( name, "set" );
942 input_info_category_t * p_category;
943 input_info_t * p_info;
945 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
946 if( p_input == NULL )
951 vlc_mutex_lock( &p_input->stream.stream_lock );
952 p_category = p_input->stream.p_info;
955 mvar_t *cat = mvar_New( name, "set" );
956 mvar_t *iset = mvar_New( "info", "set" );
958 mvar_AppendNewVar( cat, "name", p_category->psz_name );
959 mvar_AppendVar( cat, iset );
961 p_info = p_category->p_info;
964 mvar_t *info = mvar_New( "info", "" );
966 msg_Dbg( p_input, "adding info name=%s value=%s", p_info->psz_name, p_info->psz_value );
967 mvar_AppendNewVar( info, "name", p_info->psz_name );
968 mvar_AppendNewVar( info, "value", p_info->psz_value );
969 mvar_AppendVar( iset, info );
970 p_info = p_info->p_next;
972 mvar_AppendVar( s, cat );
973 p_category = p_category->p_next;
975 vlc_mutex_unlock( &p_input->stream.stream_lock );
980 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
982 mvar_t *s = mvar_New( name, "set" );
986 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
987 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
989 for( i= 0; i < info.i_count; )
993 inf = mvar_New( name, "set" );
996 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
997 info.info[i].psz_name, info.info[i].psz_value ); */
998 mvar_AppendNewVar( inf,
999 info.info[i].psz_name,
1000 info.info[i].psz_value );
1002 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
1003 mvar_AppendVar( s, inf );
1008 for( i = 0; i < info.i_count; i++ )
1010 free( info.info[i].psz_name );
1011 free( info.info[i].psz_value );
1016 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1018 mvar_t *s = mvar_New( name, "set" );
1019 char tmp[MAX_DIR_SIZE], *p, *src;
1020 #ifdef HAVE_SYS_STAT_H
1021 struct stat stat_info;
1024 struct dirent *p_dir_content;
1027 /* convert all / to native separator */
1028 #if defined( WIN32 )
1029 while( (p = strchr( psz_dir, '/' )) )
1038 /* remove trailling separator */
1039 while( strlen( psz_dir ) > 1 &&
1040 #if defined( WIN32 )
1041 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1043 psz_dir[strlen( psz_dir ) -1 ] == sep )
1045 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1047 /* remove double separator */
1048 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1050 if( src[0] == sep && src[1] == sep )
1058 if( *psz_dir == '\0' )
1062 /* first fix all .. dir */
1066 if( src[0] == '.' && src[1] == '.' )
1069 if( p <= &psz_dir[1] )
1076 while( p > &psz_dir[1] && *p != sep )
1081 else if( *src == sep )
1083 if( p > psz_dir && p[-1] == sep )
1097 } while( *src && *src != sep );
1102 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1104 #ifdef HAVE_SYS_STAT_H
1105 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1111 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1113 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1117 /* remove traling / or \ */
1118 for( p = &psz_dir[strlen( psz_dir) - 1]; p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1127 /* parse psz_src dir */
1128 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1132 if( !strcmp( p_dir_content->d_name, "." ) )
1137 #if defined( WIN32 )
1138 sprintf( tmp, "%s\\%s", psz_dir, p_dir_content->d_name );
1140 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1143 #ifdef HAVE_SYS_STAT_H
1144 if( stat( tmp, &stat_info ) == -1 )
1149 f = mvar_New( name, "set" );
1150 mvar_AppendNewVar( f, "name", tmp );
1151 #ifdef HAVE_SYS_STAT_H
1152 if( S_ISDIR( stat_info.st_mode ) )
1154 mvar_AppendNewVar( f, "type", "directory" );
1156 else if( S_ISREG( stat_info.st_mode ) )
1158 mvar_AppendNewVar( f, "type", "file" );
1162 mvar_AppendNewVar( f, "type", "unknown" );
1165 sprintf( tmp, "%lld", stat_info.st_size );
1166 mvar_AppendNewVar( f, "size", tmp );
1168 /* FIXME memory leak FIXME */
1170 ctime_r( &stat_info.st_mtime, tmp );
1171 mvar_AppendNewVar( f, "date", tmp );
1173 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1177 mvar_AppendNewVar( f, "type", "unknown" );
1178 mvar_AppendNewVar( f, "size", "unknown" );
1179 mvar_AppendNewVar( f, "date", "unknown" );
1181 mvar_AppendVar( s, f );
1187 static void SSInit( rpn_stack_t * );
1188 static void SSClean( rpn_stack_t * );
1189 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1191 static void SSPush ( rpn_stack_t *, char * );
1192 static char *SSPop ( rpn_stack_t * );
1194 static void SSPushN ( rpn_stack_t *, int );
1195 static int SSPopN ( rpn_stack_t *, mvar_t * );
1198 /****************************************************************************
1200 ****************************************************************************/
1208 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1212 /* just load the file */
1214 *pp_data = malloc( 1025 ); /* +1 for \0 */
1215 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1218 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1224 (*pp_data)[*pi_data] = '\0';
1229 static int MacroParse( macro_t *m, uint8_t *psz_src )
1231 uint8_t *dup = strdup( psz_src );
1236 #define EXTRACT( name, l ) \
1238 p = strchr( src, '"' ); \
1243 m->name = strdup( src ); \
1264 if( !strncmp( src, "id=\"", 4 ) )
1268 else if( !strncmp( src, "param1=\"", 8 ) )
1270 EXTRACT( param1, 8 );
1272 else if( !strncmp( src, "param2=\"", 8 ) )
1274 EXTRACT( param2, 8 );
1281 if( strstr( src, "/>" ) )
1283 src = strstr( src, "/>" ) + 2;
1287 src += strlen( src );
1292 m->id = strdup( "" );
1294 if( m->param1 == NULL )
1296 m->param1 = strdup( "" );
1298 if( m->param2 == NULL )
1300 m->param2 = strdup( "" );
1309 static void MacroClean( macro_t *m )
1355 StrToMacroTypeTab [] =
1357 { "control", MVLC_CONTROL },
1358 /* player control */
1359 { "play", MVLC_PLAY },
1360 { "stop", MVLC_STOP },
1361 { "pause", MVLC_PAUSE },
1362 { "next", MVLC_NEXT },
1363 { "previous", MVLC_PREVIOUS },
1364 { "seek", MVLC_SEEK },
1365 { "keep", MVLC_KEEP },
1366 { "fullscreen", MVLC_FULLSCREEN },
1367 { "volume", MVLC_VOLUME },
1369 /* playlist management */
1370 { "add", MVLC_ADD },
1371 { "delete", MVLC_DEL },
1372 { "empty", MVLC_EMPTY },
1373 { "sort", MVLC_SORT },
1376 { "close", MVLC_CLOSE },
1377 { "shutdown", MVLC_SHUTDOWN },
1379 { "rpn", MVLC_RPN },
1381 { "foreach", MVLC_FOREACH },
1382 { "value", MVLC_VALUE },
1385 { "else", MVLC_ELSE },
1386 { "end", MVLC_END },
1387 { "get", MVLC_GET },
1388 { "set", MVLC_SET },
1389 { "int", MVLC_INT },
1390 { "float", MVLC_FLOAT },
1391 { "string", MVLC_STRING },
1394 { NULL, MVLC_UNKNOWN }
1397 static int StrToMacroType( char *name )
1401 if( !name || *name == '\0')
1403 return MVLC_UNKNOWN;
1405 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1407 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1409 return StrToMacroTypeTab[i].i_type;
1412 return MVLC_UNKNOWN;
1415 static void MacroDo( httpd_file_callback_args_t *p_args,
1417 uint8_t *p_request, int i_request,
1418 uint8_t **pp_data, int *pi_data,
1421 intf_thread_t *p_intf = p_args->p_intf;
1422 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1425 #define ALLOC( l ) \
1427 int __i__ = *pp_dst - *pp_data; \
1429 *pp_data = realloc( *pp_data, *pi_data ); \
1430 *pp_dst = (*pp_data) + __i__; \
1432 #define PRINT( str ) \
1433 ALLOC( strlen( str ) + 1 ); \
1434 *pp_dst += sprintf( *pp_dst, str );
1436 #define PRINTS( str, s ) \
1437 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1439 char * psz_cur = *pp_dst; \
1440 *pp_dst += sprintf( *pp_dst, str, s ); \
1441 while( psz_cur && *psz_cur ) \
1443 /* Prevent script injection */ \
1444 if( *psz_cur == '<' ) *psz_cur = '*'; \
1445 if( *psz_cur == '>' ) *psz_cur = '*'; \
1450 switch( StrToMacroType( m->id ) )
1453 if( i_request <= 0 )
1457 uri_extract_value( p_request, "control", control, 512 );
1458 if( *m->param1 && !strstr( m->param1, control ) )
1460 msg_Warn( p_intf, "unauthorized control=%s", control );
1463 switch( StrToMacroType( control ) )
1470 uri_extract_value( p_request, "item", item, 512 );
1471 i_item = atoi( item );
1472 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
1473 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1477 playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1478 msg_Dbg( p_intf, "requested playlist stop" );
1481 playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1482 msg_Dbg( p_intf, "requested playlist pause" );
1485 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1486 p_sys->p_playlist->i_index + 1 );
1487 msg_Dbg( p_intf, "requested playlist next" );
1490 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1491 p_sys->p_playlist->i_index - 1 );
1492 msg_Dbg( p_intf, "requested playlist next" );
1494 case MVLC_FULLSCREEN:
1496 if( p_sys->p_input )
1498 vout_thread_t *p_vout;
1499 p_vout = vlc_object_find( p_sys->p_input,
1500 VLC_OBJECT_VOUT, FIND_CHILD );
1504 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1505 vlc_object_release( p_vout );
1506 msg_Dbg( p_intf, "requested fullscreen toggle" );
1520 #define POSITION_ABSOLUTE 12
1521 #define POSITION_REL_FOR 13
1522 #define POSITION_REL_BACK 11
1523 #define TIME_ABSOLUTE 0
1524 #define TIME_REL_FOR 1
1525 #define TIME_REL_BACK -1
1526 if( p_sys->p_input )
1528 uri_extract_value( p_request, "seek_value", value, 20 );
1529 uri_decode_url_encoded( value );
1531 var_Get( p_sys->p_input, "length", &val);
1532 i_length = val.i_time;
1534 while( p_value[0] != '\0' )
1540 i_relative = TIME_REL_FOR;
1546 i_relative = TIME_REL_BACK;
1550 case '0': case '1': case '2': case '3': case '4':
1551 case '5': case '6': case '7': case '8': case '9':
1553 i_stock = strtol( p_value , &p_value , 10 );
1556 case '%': /* for percentage ie position */
1558 i_relative += POSITION_ABSOLUTE;
1566 i_value = 60 * (i_value + i_stock) ;
1571 case 'h': case 'H': /* hours */
1573 i_value += 3600 * i_stock;
1575 /* other characters which are not numbers are not important */
1576 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1582 case 'm': case 'M': case '\'': /* minutes */
1584 i_value += 60 * i_stock;
1587 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1593 case 's': case 'S': case '"': /* seconds */
1597 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1611 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1618 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1619 val.i_time = (uint64_t)( i_value ) * 1000000;
1621 val.i_time = i_length;
1623 var_Set( p_sys->p_input, "time", val );
1624 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1629 var_Get( p_sys->p_input, "time", &val );
1630 if( (uint64_t)( i_value ) * 1000 + val.i_time <= i_length )
1632 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1635 val.i_time = i_length;
1637 var_Set( p_sys->p_input, "time", val );
1638 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1643 var_Get( p_sys->p_input, "time", &val );
1644 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1649 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1651 var_Set( p_sys->p_input, "time", val );
1652 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1655 case POSITION_ABSOLUTE:
1657 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1658 var_Set( p_sys->p_input, "position", val );
1659 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1662 case POSITION_REL_FOR:
1664 var_Get( p_sys->p_input, "position", &val );
1665 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1666 var_Set( p_sys->p_input, "position", val );
1667 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1670 case POSITION_REL_BACK:
1672 var_Get( p_sys->p_input, "position", &val );
1673 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1674 var_Set( p_sys->p_input, "position", val );
1675 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
1680 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1685 #undef POSITION_ABSOLUTE
1686 #undef POSITION_REL_FOR
1687 #undef POSITION_REL_BACK
1688 #undef TIME_ABSOLUTE
1690 #undef TIME_REL_BACK
1696 audio_volume_t i_volume;
1699 uri_extract_value( p_request, "value", vol, 8 );
1700 aout_VolumeGet( p_intf, &i_volume );
1701 uri_decode_url_encoded( vol );
1705 i_value = atoi( vol + 1 );
1706 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1708 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1709 msg_Dbg( p_intf, "requested volume set: max" );
1712 aout_VolumeSet( p_intf , (i_volume + i_value) );
1713 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1718 i_value = atoi( vol + 1 );
1719 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1721 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1722 msg_Dbg( p_intf, "requested volume set: min" );
1725 aout_VolumeSet( p_intf , (i_volume - i_value) );
1726 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1730 i_value = atoi( vol );
1731 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1733 aout_VolumeSet( p_intf , atoi( vol ) );
1734 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1740 /* playlist management */
1744 uri_extract_value( p_request, "mrl", mrl, 512 );
1745 uri_decode_url_encoded( mrl );
1746 playlist_Add( p_sys->p_playlist, mrl, NULL, 0,
1747 PLAYLIST_APPEND, PLAYLIST_END );
1748 msg_Dbg( p_intf, "requested playlist add: %s", mrl );
1753 int i_item, *p_items = NULL, i_nb_items = 0;
1754 char item[512], *p_parser = p_request;
1756 /* Get the list of items to delete */
1758 uri_extract_value( p_parser, "item", item, 512 )) )
1760 if( !*item ) continue;
1762 i_item = atoi( item );
1763 p_items = realloc( p_items, (i_nb_items + 1) *
1765 p_items[i_nb_items] = i_item;
1769 /* The items need to be deleted from in reversed order */
1773 for( i = 0; i < i_nb_items; i++ )
1776 for( j = 0; j < i_nb_items; j++ )
1778 if( p_items[j] > p_items[i_index] )
1782 playlist_Delete( p_sys->p_playlist,
1784 msg_Dbg( p_intf, "requested playlist delete: %d",
1786 p_items[i_index] = -1;
1790 if( p_items ) free( p_items );
1795 int i_item, *p_items = NULL, i_nb_items = 0;
1796 char item[512], *p_parser = p_request;
1799 /* Get the list of items to keep */
1801 uri_extract_value( p_parser, "item", item, 512 )) )
1803 if( !*item ) continue;
1805 i_item = atoi( item );
1806 p_items = realloc( p_items, (i_nb_items + 3) *
1808 p_items[i_nb_items + 1] = i_item;
1812 /* sort item list */
1813 for( i=1 ; i < (i_nb_items + 1) ; i++)
1815 for( j=(i+1) ; j < (i_nb_items + 1) ; j++)
1817 if( p_items[j] > p_items[i] )
1820 p_items[j] = p_items[i];
1826 p_items[0] = p_sys->p_playlist->i_size;
1827 p_items[ i_nb_items + 1 ] = -1;
1829 /* The items need to be deleted from in reversed order */
1830 for( i=0 ; i <= i_nb_items ; i++ )
1832 for( j = (p_items[i] - 1) ; j > p_items[i + 1] ; j-- )
1834 playlist_Delete( p_sys->p_playlist,
1836 msg_Dbg( p_intf, "requested playlist delete: %d",
1841 if( p_items ) free( p_items );
1846 while( p_sys->p_playlist->i_size > 0 )
1848 playlist_Delete( p_sys->p_playlist, 0 );
1850 msg_Dbg( p_intf, "requested playlist empty" );
1859 uri_extract_value( p_request, "type", type, 12 );
1860 uri_extract_value( p_request, "order", order, 2 );
1862 if( order[0] == '0' ) i_order = SORT_NORMAL;
1863 else i_order = SORT_REVERSE;
1865 if( !strcmp( type , "title" ) )
1867 playlist_SortTitle( p_sys->p_playlist , i_order );
1868 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
1869 } else if( !strcmp( type , "group" ) )
1871 playlist_SortGroup( p_sys->p_playlist , i_order );
1872 msg_Dbg( p_intf, "requested playlist sort by group (%d)" , i_order );
1873 } else if( !strcmp( type , "author" ) )
1875 playlist_SortAuthor( p_sys->p_playlist , i_order );
1876 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
1882 /* admin function */
1886 uri_extract_value( p_request, "id", id, 512 );
1887 msg_Dbg( p_intf, "requested close id=%s", id );
1888 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
1890 msg_Warn( p_intf, "close failed for id=%s", id );
1896 msg_Dbg( p_intf, "requested shutdown" );
1897 p_intf->p_vlc->b_die = VLC_TRUE;
1901 PRINTS( "<!-- control param(%s) unsuported -->", control );
1912 if( i_request <= 0 ||
1913 *m->param1 == '\0' ||
1914 strstr( p_request, m->param1 ) == NULL )
1918 uri_extract_value( p_request, m->param1, value, 512 );
1919 uri_decode_url_encoded( value );
1921 switch( StrToMacroType( m->param2 ) )
1925 config_PutInt( p_intf, m->param1, i );
1929 config_PutFloat( p_intf, m->param1, f );
1932 config_PutPsz( p_intf, m->param1, value );
1935 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
1946 if( *m->param1 == '\0' )
1951 switch( StrToMacroType( m->param2 ) )
1954 i = config_GetInt( p_intf, m->param1 );
1955 sprintf( value, "%i", i );
1958 f = config_GetFloat( p_intf, m->param1 );
1959 sprintf( value, "%f", f );
1962 psz = config_GetPsz( p_intf, m->param1 );
1963 sprintf( value, "%s", psz ? psz : "" );
1966 sprintf( value, "invalid type(%s) in set", m->param2 );
1969 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
1970 PRINTS( "%s", value );
1979 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1980 s = SSPop( &p_args->stack );
1981 v = mvar_GetValue( p_args->vars, s );
1985 v = s = SSPop( &p_args->stack );
1993 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1997 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
1998 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2006 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
2013 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
2018 i_skip = MacroParse( &m, src );
2020 i_id = StrToMacroType( m.id );
2035 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2036 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2038 return src + ( b_after ? i_skip : 0 );
2040 else if( i_level < 0 )
2056 static void Execute( httpd_file_callback_args_t *p_args,
2057 uint8_t *p_request, int i_request,
2058 uint8_t **pp_data, int *pi_data,
2060 uint8_t *_src, uint8_t *_end )
2062 intf_thread_t *p_intf = p_args->p_intf;
2064 uint8_t *src, *dup, *end;
2065 uint8_t *dst = *pp_dst;
2067 src = dup = malloc( _end - _src + 1 );
2068 end = src +( _end - _src );
2070 memcpy( src, _src, _end - _src );
2073 /* we parse searching <vlc */
2079 p = strstr( src, "<vlc" );
2080 if( p < end && p == src )
2084 src += MacroParse( &m, src );
2086 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2088 switch( StrToMacroType( m.id ) )
2095 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2096 if( SSPopN( &p_args->stack, p_args->vars ) )
2104 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2108 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2112 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2115 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2119 else if( i_test == 1 )
2122 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2124 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2128 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2137 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2138 uint8_t *start = src;
2139 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2146 if( !strcmp( m.param2, "integer" ) )
2148 char *arg = SSPop( &p_args->stack );
2149 index = mvar_IntegerSetNew( m.param1, arg );
2152 else if( !strcmp( m.param2, "directory" ) )
2154 char *arg = SSPop( &p_args->stack );
2155 index = mvar_FileSetNew( m.param1, arg );
2158 else if( !strcmp( m.param2, "playlist" ) )
2160 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2162 else if( !strcmp( m.param2, "informations" ) )
2164 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2166 else if( !strcmp( m.param2, "hosts" ) )
2168 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2170 else if( !strcmp( m.param2, "urls" ) )
2172 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2174 else if( !strcmp( m.param2, "connections" ) )
2176 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2178 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2180 index = mvar_Duplicate( v );
2184 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2189 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2191 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2193 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2196 f->name = strdup( m.param1 );
2199 mvar_PushVar( p_args->vars, f );
2200 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2201 mvar_RemoveVar( p_args->vars, f );
2205 mvar_Delete( index );
2212 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2220 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2223 int i_index = dst - *pp_data;
2226 *pp_data = realloc( *pp_data, *pi_data );
2227 dst = (*pp_data) + i_index;
2229 memcpy( dst, src, i_copy );
2239 /****************************************************************************
2241 ****************************************************************************
2242 * a file with mime == text/html is parsed and all "macro" replaced
2243 * <vlc id="macro name" [param1="" [param2=""]] />
2246 ****************************************************************************/
2247 static int http_get( httpd_file_callback_args_t *p_args,
2248 uint8_t *p_request, int i_request,
2249 uint8_t **pp_data, int *pi_data )
2254 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2256 p = *pp_data = malloc( 10240 );
2259 return VLC_EGENERIC;
2261 p += sprintf( p, "<html>\n" );
2262 p += sprintf( p, "<head>\n" );
2263 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2264 p += sprintf( p, "</head>\n" );
2265 p += sprintf( p, "<body>\n" );
2266 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2267 p += sprintf( p, "<hr />\n" );
2268 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2269 p += sprintf( p, "</body>\n" );
2270 p += sprintf( p, "</html>\n" );
2272 *pi_data = strlen( *pp_data ) + 1;
2277 if( strcmp( p_args->mime, "text/html" ) )
2279 FileLoad( f, pp_data, pi_data );
2287 char position[4]; /* percentage */
2288 char time[12]; /* in seconds */
2289 char length[12]; /* in seconds */
2290 audio_volume_t i_volume;
2294 #define p_sys p_args->p_intf->p_sys
2295 if( p_sys->p_input )
2297 var_Get( p_sys->p_input, "position", &val);
2298 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2299 var_Get( p_sys->p_input, "time", &val);
2300 sprintf( time, "%d" , (int)(val.i_time / 1000) );
2301 var_Get( p_sys->p_input, "length", &val);
2302 sprintf( length, "%d" , (int)(val.i_time / 1000) );
2304 var_Get( p_sys->p_input, "state", &val );
2305 if( val.i_int == PLAYING_S )
2307 sprintf( state, "playing" );
2308 } else if( val.i_int == PAUSE_S )
2310 sprintf( state, "paused" );
2313 sprintf( state, "stop" );
2317 sprintf( position, "%d", 0 );
2318 sprintf( time, "%d", 0 );
2319 sprintf( length, "%d", 0 );
2320 sprintf( state, "stop" );
2324 aout_VolumeGet( p_args->p_intf , &i_volume );
2325 sprintf( volume , "%d" , (int)i_volume );
2327 p_args->vars = mvar_New( "variables", "" );
2328 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2329 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2330 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
2331 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2332 mvar_AppendNewVar( p_args->vars, "stream_position", position );
2333 mvar_AppendNewVar( p_args->vars, "stream_time", time );
2334 mvar_AppendNewVar( p_args->vars, "stream_length", length );
2335 mvar_AppendNewVar( p_args->vars, "volume", volume );
2336 mvar_AppendNewVar( p_args->vars, "stream_state", state );
2338 SSInit( &p_args->stack );
2340 /* first we load in a temporary buffer */
2341 FileLoad( f, &p_buffer, &i_buffer );
2343 /* allocate output */
2344 *pi_data = i_buffer + 1000;
2345 dst = *pp_data = malloc( *pi_data );
2347 /* we parse executing all <vlc /> macros */
2348 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2351 *pi_data = dst - *pp_data;
2353 SSClean( &p_args->stack );
2354 mvar_Delete( p_args->vars );
2362 /****************************************************************************
2364 ****************************************************************************/
2365 static char *uri_extract_value( char *psz_uri, char *psz_name,
2366 char *psz_value, int i_value_max )
2370 p = strstr( psz_uri, psz_name );
2375 p += strlen( psz_name );
2376 if( *p == '=' ) p++;
2378 if( strchr( p, '&' ) )
2380 i_len = strchr( p, '&' ) - p;
2384 /* for POST method */
2385 if( strchr( p, '\n' ) )
2387 i_len = strchr( p, '\n' ) - p;
2388 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2392 i_len = strlen( p );
2395 i_len = __MIN( i_value_max - 1, i_len );
2398 strncpy( psz_value, p, i_len );
2399 psz_value[i_len] = '\0';
2403 strncpy( psz_value, "", i_value_max );
2409 strncpy( psz_value, "", i_value_max );
2415 static void uri_decode_url_encoded( char *psz )
2417 char *dup = strdup( psz );
2435 *psz++ = strtol( val, NULL, 16 );
2437 else if( *p == '+' )
2451 /****************************************************************************
2452 * Light RPN evaluator
2453 ****************************************************************************/
2454 static void SSInit( rpn_stack_t *st )
2459 static void SSClean( rpn_stack_t *st )
2461 while( st->i_stack > 0 )
2463 free( st->stack[--st->i_stack] );
2467 static void SSPush( rpn_stack_t *st, char *s )
2469 if( st->i_stack < STACK_MAX )
2471 st->stack[st->i_stack++] = strdup( s );
2475 static char * SSPop( rpn_stack_t *st )
2477 if( st->i_stack <= 0 )
2479 return strdup( "" );
2483 return st->stack[--st->i_stack];
2487 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2496 i = strtol( name, &end, 0 );
2499 value = mvar_GetValue( vars, name );
2507 static void SSPushN( rpn_stack_t *st, int i )
2511 sprintf( v, "%d", i );
2515 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2522 while( *exp == ' ' )
2529 /* extract string */
2532 while( *exp && *exp != '\'' )
2543 p = strchr( exp, ' ' );
2548 exp += strlen( exp );
2553 strncpy( s, exp, i );
2564 /* 1. Integer function */
2565 if( !strcmp( s, "!" ) )
2567 SSPushN( st, ~SSPopN( st, vars ) );
2569 else if( !strcmp( s, "^" ) )
2571 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2573 else if( !strcmp( s, "&" ) )
2575 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2577 else if( !strcmp( s, "|" ) )
2579 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2581 else if( !strcmp( s, "+" ) )
2583 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2585 else if( !strcmp( s, "-" ) )
2587 int i = SSPopN( st, vars );
2588 int j = SSPopN( st, vars );
2589 SSPushN( st, i - j );
2591 else if( !strcmp( s, "*" ) )
2593 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2595 else if( !strcmp( s, "/" ) )
2599 i = SSPopN( st, vars );
2600 j = SSPopN( st, vars );
2602 SSPushN( st, j != 0 ? i / j : 0 );
2604 else if( !strcmp( s, "%" ) )
2608 i = SSPopN( st, vars );
2609 j = SSPopN( st, vars );
2611 SSPushN( st, j != 0 ? i % j : 0 );
2613 /* 2. integer tests */
2614 else if( !strcmp( s, "=" ) )
2616 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2618 else if( !strcmp( s, "<" ) )
2620 int i = SSPopN( st, vars );
2621 int j = SSPopN( st, vars );
2623 SSPushN( st, i < j ? -1 : 0 );
2625 else if( !strcmp( s, ">" ) )
2627 int i = SSPopN( st, vars );
2628 int j = SSPopN( st, vars );
2630 SSPushN( st, i > j ? -1 : 0 );
2632 else if( !strcmp( s, "<=" ) )
2634 int i = SSPopN( st, vars );
2635 int j = SSPopN( st, vars );
2637 SSPushN( st, i <= j ? -1 : 0 );
2639 else if( !strcmp( s, ">=" ) )
2641 int i = SSPopN( st, vars );
2642 int j = SSPopN( st, vars );
2644 SSPushN( st, i >= j ? -1 : 0 );
2646 /* 3. string functions */
2647 else if( !strcmp( s, "strcat" ) )
2649 char *s1 = SSPop( st );
2650 char *s2 = SSPop( st );
2651 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2661 else if( !strcmp( s, "strcmp" ) )
2663 char *s1 = SSPop( st );
2664 char *s2 = SSPop( st );
2666 SSPushN( st, strcmp( s1, s2 ) );
2670 else if( !strcmp( s, "strlen" ) )
2672 char *str = SSPop( st );
2674 SSPushN( st, strlen( str ) );
2677 /* 4. stack functions */
2678 else if( !strcmp( s, "dup" ) )
2680 char *str = SSPop( st );
2685 else if( !strcmp( s, "drop" ) )
2687 char *str = SSPop( st );
2690 else if( !strcmp( s, "swap" ) )
2692 char *s1 = SSPop( st );
2693 char *s2 = SSPop( st );
2700 else if( !strcmp( s, "flush" ) )
2705 else if( !strcmp( s, "store" ) )
2707 char *name = SSPop( st );
2708 char *value = SSPop( st );
2710 mvar_PushNewVar( vars, name, value );
2714 else if( !strcmp( s, "value" ) )
2716 char *name = SSPop( st );
2717 char *value = mvar_GetValue( vars, name );
2719 SSPush( st, value );
2723 else if( !strcmp( s, "url_extract" ) )
2725 char *url = mvar_GetValue( vars, "url_value" );
2726 char *name = SSPop( st );
2729 uri_extract_value( url, name, value, 512 );
2730 uri_decode_url_encoded( value );
2731 SSPush( st, value );