1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
5 * $Id: http.c,v 1.50 2004/01/25 16:17:03 anil 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 the http interface will bind to." )
75 #define SRC_TEXT N_( "Source directory" )
76 #define SRC_LONGTEXT N_( "Source directory" )
79 set_description( _("HTTP remote control interface") );
80 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
81 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
82 set_capability( "interface", 0 );
83 set_callbacks( Activate, Close );
87 /*****************************************************************************
89 *****************************************************************************/
90 static void Run ( intf_thread_t *p_intf );
92 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
95 static int DirectoryCheck( char *psz_dir )
99 #ifdef HAVE_SYS_STAT_H
100 struct stat stat_info;
102 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
108 if( ( p_dir = opendir( psz_dir ) ) == NULL )
118 static int http_get( httpd_file_callback_args_t *p_args,
119 uint8_t *p_request, int i_request,
120 uint8_t **pp_data, int *pi_data );
122 static char *uri_extract_value( char *psz_uri, char *psz_name,
123 char *psz_value, int i_value_max );
124 static void uri_decode_url_encoded( char *psz );
126 static char *Find_end_MRL( char *psz );
128 static playlist_item_t * parse_MRL( char *psz );
130 /*****************************************************************************
132 *****************************************************************************/
133 typedef struct mvar_s
139 struct mvar_s **field;
142 #define STACK_MAX 100
145 char *stack[STACK_MAX];
149 struct httpd_file_callback_args_t
151 intf_thread_t *p_intf;
152 httpd_file_t *p_file;
158 /* inited for each access */
166 httpd_host_t *p_httpd_host;
169 httpd_file_callback_args_t **pp_files;
171 playlist_t *p_playlist;
172 input_thread_t *p_input;
177 /*****************************************************************************
178 * Activate: initialize and create stuff
179 *****************************************************************************/
180 static int Activate( vlc_object_t *p_this )
182 intf_thread_t *p_intf = (intf_thread_t*)p_this;
185 char *psz_address = "";
189 psz_host = config_GetPsz( p_intf, "http-host" );
193 psz_address = psz_host;
195 psz_parser = strchr( psz_host, ':' );
198 *psz_parser++ = '\0';
199 i_port = atoi( psz_parser );
207 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
208 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
211 return( VLC_ENOMEM );
213 p_sys->p_playlist = NULL;
214 p_sys->p_input = NULL;
216 if( ( p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_intf), VLC_TRUE ) ) == NULL )
218 msg_Err( p_intf, "cannot create/find httpd" );
223 if( ( p_sys->p_httpd_host =
224 p_sys->p_httpd->pf_register_host( p_sys->p_httpd,
225 psz_address, i_port ) ) == NULL )
227 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
228 httpd_Release( p_sys->p_httpd );
239 p_sys->pp_files = malloc( sizeof( httpd_file_callback_args_t *) );
240 if( !p_sys->pp_files )
242 return( VLC_ENOMEM );
245 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
246 ( defined(WIN32) && !defined(UNDER_CE ) )
247 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
249 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
250 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
253 return( VLC_ENOMEM );
256 sprintf( psz_src, "%s/http", psz_vlcpath);
258 sprintf( psz_src, "%s/share/http", psz_vlcpath);
262 psz_src = config_GetPsz( p_intf, "http-src" );
264 if( !psz_src || *psz_src == '\0' )
266 if( !DirectoryCheck( "share/http" ) )
268 psz_src = strdup( "share/http" );
270 else if( !DirectoryCheck( DATA_PATH "/http" ) )
272 psz_src = strdup( DATA_PATH "/http" );
277 if( !psz_src || *psz_src == '\0' )
279 msg_Err( p_intf, "invalid src dir" );
283 /* remove trainling \ or / */
284 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
285 psz_src[strlen( psz_src ) - 1] == '/' )
287 psz_src[strlen( psz_src ) - 1] = '\0';
290 ParseDirectory( p_intf, psz_src, psz_src );
293 if( p_sys->i_files <= 0 )
295 msg_Err( p_intf, "cannot find any files (%s)", psz_src );
298 p_intf->pf_run = Run;
304 if( psz_src ) free( psz_src );
305 free( p_sys->pp_files );
306 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
307 p_sys->p_httpd_host );
308 httpd_Release( p_sys->p_httpd );
313 /*****************************************************************************
314 * CloseIntf: destroy interface
315 *****************************************************************************/
316 void Close ( vlc_object_t *p_this )
318 intf_thread_t *p_intf = (intf_thread_t *)p_this;
319 intf_sys_t *p_sys = p_intf->p_sys;
323 for( i = 0; i < p_sys->i_files; i++ )
325 p_sys->p_httpd->pf_unregister_file( p_sys->p_httpd,
326 p_sys->pp_files[i]->p_file );
327 /* do not free mime */
328 free( p_sys->pp_files[i]->file );
329 free( p_sys->pp_files[i]->name );
330 free( p_sys->pp_files[i] );
332 free( p_sys->pp_files );
333 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
334 p_sys->p_httpd_host );
335 httpd_Release( p_sys->p_httpd );
339 /*****************************************************************************
340 * Run: http interface thread
341 *****************************************************************************/
342 static void Run( intf_thread_t *p_intf )
344 intf_sys_t *p_sys = p_intf->p_sys;
346 while( !p_intf->b_die )
348 /* get the playlist */
349 if( p_sys->p_playlist == NULL )
351 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
354 /* Manage the input part */
355 if( p_sys->p_input == NULL )
357 if( p_sys->p_playlist )
360 vlc_object_find( p_sys->p_playlist,
365 else if( p_sys->p_input->b_dead )
367 vlc_object_release( p_sys->p_input );
368 p_sys->p_input = NULL;
373 msleep( INTF_IDLE_SLEEP );
378 vlc_object_release( p_sys->p_input );
379 p_sys->p_input = NULL;
382 if( p_sys->p_playlist )
384 vlc_object_release( p_sys->p_playlist );
385 p_sys->p_playlist = NULL;
390 /*****************************************************************************
392 *****************************************************************************/
393 #define MAX_DIR_SIZE 10240
395 /****************************************************************************
396 * FileToUrl: create a good name for an url from filename
397 ****************************************************************************/
398 static char *FileToUrl( char *name )
402 url = p = malloc( strlen( name ) + 1 );
410 while( *name == '\\' || *name == '/' )
412 while( *name == '/' )
422 /* convert '\\' into '/' */
435 if( ( p = strrchr( url, '/' ) ) != NULL )
437 if( !strncmp( p, "/index.", 7 ) )
445 /****************************************************************************
446 * FileToMime: XXX duplicated with modules/access_out/http.c
447 ****************************************************************************/
454 { ".htm", "text/html" },
455 { ".html", "text/html" },
457 { ".css", "text/css" },
460 { ".avi", "video/avi" },
461 { ".asf", "video/x-ms-asf" },
462 { ".m1a", "audio/mpeg" },
463 { ".m2a", "audio/mpeg" },
464 { ".m1v", "video/mpeg" },
465 { ".m2v", "video/mpeg" },
466 { ".mp2", "audio/mpeg" },
467 { ".mp3", "audio/mpeg" },
468 { ".mpa", "audio/mpeg" },
469 { ".mpg", "video/mpeg" },
470 { ".mpeg", "video/mpeg" },
471 { ".mpe", "video/mpeg" },
472 { ".mov", "video/quicktime" },
473 { ".moov", "video/quicktime" },
474 { ".ogg", "application/ogg" },
475 { ".ogm", "application/ogg" },
476 { ".wav", "audio/wav" },
482 static char *FileToMime( char *psz_name )
486 psz_ext = strrchr( psz_name, '.' );
491 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
493 if( !strcmp( http_mime[i].psz_ext, psz_ext ) )
495 return( http_mime[i].psz_mime );
499 return( "application/octet-stream" );
502 /****************************************************************************
503 * ParseDirectory: parse recursively a directory, adding each file
504 ****************************************************************************/
505 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
508 intf_sys_t *p_sys = p_intf->p_sys;
509 char dir[MAX_DIR_SIZE];
510 #ifdef HAVE_SYS_STAT_H
511 struct stat stat_info;
514 struct dirent *p_dir_content;
518 char *password = NULL;
520 #ifdef HAVE_SYS_STAT_H
521 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
527 if( ( p_dir = opendir( psz_dir ) ) == NULL )
529 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
533 msg_Dbg( p_intf, "dir=%s", psz_dir );
535 sprintf( dir, "%s/.access", psz_dir );
536 if( ( file = fopen( dir, "r" ) ) != NULL )
541 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
543 i_size = fread( line, 1, 1023, file );
547 while( i_size > 0 && ( line[i_size-1] == '\n' ||
548 line[i_size-1] == '\r' ) )
555 p = strchr( line, ':' );
559 user = strdup( line );
560 password = strdup( p );
563 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
564 user, password, i_size );
571 /* parse psz_src dir */
572 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
577 if( p_dir_content->d_name[0] == '.' )
581 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
582 if( ParseDirectory( p_intf, psz_root, dir ) )
584 #define f p_sys->pp_files[p_sys->i_files]
585 f = malloc( sizeof( httpd_file_callback_args_t ) );
588 msg_Err( p_intf, "out of memory" );
590 return( VLC_ENOMEM );
593 f->file = strdup( dir );
594 f->name = FileToUrl( &dir[strlen( psz_root )] );
595 f->mime = FileToMime( &dir[strlen( psz_root )] );
597 if( !f->name || !f->mime )
599 msg_Err( p_intf , "unable to parse directory" );
602 return( VLC_ENOMEM );
604 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)",
605 f->file, f->name, f->mime );
608 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
616 p_sys->pp_files = realloc( p_sys->pp_files,
617 (p_sys->i_files+1) * sizeof( httpd_file_callback_args_t ) );
619 #define fold p_sys->pp_files[p_sys->i_files-1]
621 /* FIXME for rep/ add rep (it would be better to do a redirection) */
622 if( p_sys->i_files && strlen(fold->name) > 1 &&
623 fold->name[strlen(fold->name) - 1] == '/' )
625 f = malloc( sizeof( httpd_file_callback_args_t ) );
628 msg_Err( p_intf, "out of memory" );
630 return( VLC_ENOMEM );
633 f->file = strdup( fold->file );
634 f->name = strdup( fold->name );
635 f->mime = fold->mime;
637 f->name[strlen(f->name) - 1] = '\0';
638 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)", f->file, f->name,
641 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
650 realloc( p_sys->pp_files, (p_sys->i_files+1) *
651 sizeof( httpd_file_callback_args_t ) );
673 /****************************************************************************
674 * var and set handling
675 ****************************************************************************/
677 static mvar_t *mvar_New( char *name, char *value )
679 mvar_t *v = malloc( sizeof( mvar_t ) );
681 if( !v ) return NULL;
682 v->name = strdup( name );
683 v->value = strdup( value ? value : "" );
686 v->field = malloc( sizeof( mvar_t * ) );
692 static void mvar_Delete( mvar_t *v )
699 for( i = 0; i < v->i_field; i++ )
701 mvar_Delete( v->field[i] );
707 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
709 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
710 v->field[v->i_field] = f;
714 static mvar_t *mvar_Duplicate( mvar_t *v )
719 n = mvar_New( v->name, v->value );
720 for( i = 0; i < v->i_field; i++ )
722 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
728 static void mvar_PushVar( mvar_t *v, mvar_t *f )
730 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
733 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
739 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
742 for( i = 0; i < v->i_field; i++ )
744 if( v->field[i] == f )
749 if( i >= v->i_field )
754 if( i + 1 < v->i_field )
756 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
759 /* FIXME should do a realloc */
762 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
765 char base[512], *field, *p;
768 /* format: name[index].field */
770 field = strchr( name, '.' );
773 int i = field - name;
774 strncpy( base, name, i );
780 strcpy( base, name );
783 if( ( p = strchr( base, '[' ) ) )
786 sscanf( p, "%d]", &i_index );
797 for( i = 0; i < s->i_field; i++ )
799 if( !strcmp( s->field[i]->name, base ) )
809 return mvar_GetVar( s->field[i], field );
823 static char *mvar_GetValue( mvar_t *v, char *field )
831 mvar_t *f = mvar_GetVar( v, field );
843 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
845 mvar_t *f = mvar_New( name, value );
846 mvar_PushVar( vars, f );
849 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
851 mvar_t *f = mvar_New( name, value );
852 mvar_AppendVar( vars, f );
856 /* arg= start[:stop[:step]],.. */
857 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
859 char *dup = strdup( arg );
861 mvar_t *s = mvar_New( name, "set" );
863 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
868 int i_start,i_stop,i_step;
871 p = strchr( str, ',' );
878 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
879 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
886 else if( i_match == 2 )
888 i_step = i_start < i_stop ? 1 : -1;
895 if( ( i_start < i_stop && i_step > 0 ) ||
896 ( i_start > i_stop && i_step < 0 ) )
898 for( i = i_start; ; i += i_step )
902 if( ( i_step > 0 && i > i_stop ) ||
903 ( i_step < 0 && i < i_stop ) )
908 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
909 sprintf( value, "%d", i );
911 mvar_PushNewVar( s, name, value );
922 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
924 mvar_t *s = mvar_New( name, "set" );
927 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
929 vlc_mutex_lock( &p_pl->object_lock );
930 for( i = 0; i < p_pl->i_size; i++ )
932 mvar_t *itm = mvar_New( name, "set" );
935 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
936 mvar_AppendNewVar( itm, "current", value );
938 sprintf( value, "%d", i );
939 mvar_AppendNewVar( itm, "index", value );
941 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->psz_name );
943 sprintf( value, "%d", p_pl->pp_items[i]->i_group );
944 mvar_AppendNewVar( itm, "group", value );
946 mvar_AppendVar( s, itm );
948 vlc_mutex_unlock( &p_pl->object_lock );
953 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
955 mvar_t *s = mvar_New( name, "set" );
957 input_info_category_t * p_category;
958 input_info_t * p_info;
960 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
961 if( p_input == NULL )
966 vlc_mutex_lock( &p_input->stream.stream_lock );
967 p_category = p_input->stream.p_info;
970 mvar_t *cat = mvar_New( name, "set" );
971 mvar_t *iset = mvar_New( "info", "set" );
973 mvar_AppendNewVar( cat, "name", p_category->psz_name );
974 mvar_AppendVar( cat, iset );
976 p_info = p_category->p_info;
979 mvar_t *info = mvar_New( "info", "" );
981 msg_Dbg( p_input, "adding info name=%s value=%s", p_info->psz_name, p_info->psz_value );
982 mvar_AppendNewVar( info, "name", p_info->psz_name );
983 mvar_AppendNewVar( info, "value", p_info->psz_value );
984 mvar_AppendVar( iset, info );
985 p_info = p_info->p_next;
987 mvar_AppendVar( s, cat );
988 p_category = p_category->p_next;
990 vlc_mutex_unlock( &p_input->stream.stream_lock );
995 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
997 mvar_t *s = mvar_New( name, "set" );
1001 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
1002 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
1004 for( i= 0; i < info.i_count; )
1008 inf = mvar_New( name, "set" );
1011 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
1012 info.info[i].psz_name, info.info[i].psz_value ); */
1013 mvar_AppendNewVar( inf,
1014 info.info[i].psz_name,
1015 info.info[i].psz_value );
1017 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
1018 mvar_AppendVar( s, inf );
1023 for( i = 0; i < info.i_count; i++ )
1025 free( info.info[i].psz_name );
1026 free( info.info[i].psz_value );
1028 if( info.i_count > 0 )
1035 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1037 mvar_t *s = mvar_New( name, "set" );
1038 char tmp[MAX_DIR_SIZE], *p, *src;
1039 #ifdef HAVE_SYS_STAT_H
1040 struct stat stat_info;
1043 struct dirent *p_dir_content;
1046 /* convert all / to native separator */
1047 #if defined( WIN32 )
1048 while( (p = strchr( psz_dir, '/' )) )
1057 /* remove trailling separator */
1058 while( strlen( psz_dir ) > 1 &&
1059 #if defined( WIN32 )
1060 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1062 psz_dir[strlen( psz_dir ) -1 ] == sep )
1064 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1066 /* remove double separator */
1067 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1069 if( src[0] == sep && src[1] == sep )
1077 if( *psz_dir == '\0' )
1081 /* first fix all .. dir */
1085 if( src[0] == '.' && src[1] == '.' )
1088 if( p <= &psz_dir[1] )
1095 while( p > &psz_dir[1] && *p != sep )
1100 else if( *src == sep )
1102 if( p > psz_dir && p[-1] == sep )
1116 } while( *src && *src != sep );
1121 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1123 #ifdef HAVE_SYS_STAT_H
1124 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1130 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1132 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1136 /* remove traling / or \ */
1137 for( p = &psz_dir[strlen( psz_dir) - 1]; p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1146 /* parse psz_src dir */
1147 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1151 if( !strcmp( p_dir_content->d_name, "." ) )
1156 #if defined( WIN32 )
1157 sprintf( tmp, "%s\\%s", psz_dir, p_dir_content->d_name );
1159 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1162 #ifdef HAVE_SYS_STAT_H
1163 if( stat( tmp, &stat_info ) == -1 )
1168 f = mvar_New( name, "set" );
1169 mvar_AppendNewVar( f, "name", tmp );
1170 #ifdef HAVE_SYS_STAT_H
1171 if( S_ISDIR( stat_info.st_mode ) )
1173 mvar_AppendNewVar( f, "type", "directory" );
1175 else if( S_ISREG( stat_info.st_mode ) )
1177 mvar_AppendNewVar( f, "type", "file" );
1181 mvar_AppendNewVar( f, "type", "unknown" );
1184 sprintf( tmp, "%lld", stat_info.st_size );
1185 mvar_AppendNewVar( f, "size", tmp );
1187 /* FIXME memory leak FIXME */
1189 ctime_r( &stat_info.st_mtime, tmp );
1190 mvar_AppendNewVar( f, "date", tmp );
1192 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1196 mvar_AppendNewVar( f, "type", "unknown" );
1197 mvar_AppendNewVar( f, "size", "unknown" );
1198 mvar_AppendNewVar( f, "date", "unknown" );
1200 mvar_AppendVar( s, f );
1206 static void SSInit( rpn_stack_t * );
1207 static void SSClean( rpn_stack_t * );
1208 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1210 static void SSPush ( rpn_stack_t *, char * );
1211 static char *SSPop ( rpn_stack_t * );
1213 static void SSPushN ( rpn_stack_t *, int );
1214 static int SSPopN ( rpn_stack_t *, mvar_t * );
1217 /****************************************************************************
1219 ****************************************************************************/
1227 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1231 /* just load the file */
1233 *pp_data = malloc( 1025 ); /* +1 for \0 */
1234 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1237 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1243 (*pp_data)[*pi_data] = '\0';
1248 static int MacroParse( macro_t *m, uint8_t *psz_src )
1250 uint8_t *dup = strdup( psz_src );
1255 #define EXTRACT( name, l ) \
1257 p = strchr( src, '"' ); \
1262 m->name = strdup( src ); \
1283 if( !strncmp( src, "id=\"", 4 ) )
1287 else if( !strncmp( src, "param1=\"", 8 ) )
1289 EXTRACT( param1, 8 );
1291 else if( !strncmp( src, "param2=\"", 8 ) )
1293 EXTRACT( param2, 8 );
1300 if( strstr( src, "/>" ) )
1302 src = strstr( src, "/>" ) + 2;
1306 src += strlen( src );
1311 m->id = strdup( "" );
1313 if( m->param1 == NULL )
1315 m->param1 = strdup( "" );
1317 if( m->param2 == NULL )
1319 m->param2 = strdup( "" );
1328 static void MacroClean( macro_t *m )
1374 StrToMacroTypeTab [] =
1376 { "control", MVLC_CONTROL },
1377 /* player control */
1378 { "play", MVLC_PLAY },
1379 { "stop", MVLC_STOP },
1380 { "pause", MVLC_PAUSE },
1381 { "next", MVLC_NEXT },
1382 { "previous", MVLC_PREVIOUS },
1383 { "seek", MVLC_SEEK },
1384 { "keep", MVLC_KEEP },
1385 { "fullscreen", MVLC_FULLSCREEN },
1386 { "volume", MVLC_VOLUME },
1388 /* playlist management */
1389 { "add", MVLC_ADD },
1390 { "delete", MVLC_DEL },
1391 { "empty", MVLC_EMPTY },
1392 { "sort", MVLC_SORT },
1395 { "close", MVLC_CLOSE },
1396 { "shutdown", MVLC_SHUTDOWN },
1398 { "rpn", MVLC_RPN },
1400 { "foreach", MVLC_FOREACH },
1401 { "value", MVLC_VALUE },
1404 { "else", MVLC_ELSE },
1405 { "end", MVLC_END },
1406 { "get", MVLC_GET },
1407 { "set", MVLC_SET },
1408 { "int", MVLC_INT },
1409 { "float", MVLC_FLOAT },
1410 { "string", MVLC_STRING },
1413 { NULL, MVLC_UNKNOWN }
1416 static int StrToMacroType( char *name )
1420 if( !name || *name == '\0')
1422 return MVLC_UNKNOWN;
1424 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1426 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1428 return StrToMacroTypeTab[i].i_type;
1431 return MVLC_UNKNOWN;
1434 static void MacroDo( httpd_file_callback_args_t *p_args,
1436 uint8_t *p_request, int i_request,
1437 uint8_t **pp_data, int *pi_data,
1440 intf_thread_t *p_intf = p_args->p_intf;
1441 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1444 #define ALLOC( l ) \
1446 int __i__ = *pp_dst - *pp_data; \
1448 *pp_data = realloc( *pp_data, *pi_data ); \
1449 *pp_dst = (*pp_data) + __i__; \
1451 #define PRINT( str ) \
1452 ALLOC( strlen( str ) + 1 ); \
1453 *pp_dst += sprintf( *pp_dst, str );
1455 #define PRINTS( str, s ) \
1456 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1458 char * psz_cur = *pp_dst; \
1459 *pp_dst += sprintf( *pp_dst, str, s ); \
1460 while( psz_cur && *psz_cur ) \
1462 /* Prevent script injection */ \
1463 if( *psz_cur == '<' ) *psz_cur = '*'; \
1464 if( *psz_cur == '>' ) *psz_cur = '*'; \
1469 switch( StrToMacroType( m->id ) )
1472 if( i_request <= 0 )
1476 uri_extract_value( p_request, "control", control, 512 );
1477 if( *m->param1 && !strstr( m->param1, control ) )
1479 msg_Warn( p_intf, "unauthorized control=%s", control );
1482 switch( StrToMacroType( control ) )
1489 uri_extract_value( p_request, "item", item, 512 );
1490 i_item = atoi( item );
1491 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
1492 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1496 playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1497 msg_Dbg( p_intf, "requested playlist stop" );
1500 playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1501 msg_Dbg( p_intf, "requested playlist pause" );
1504 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1505 p_sys->p_playlist->i_index + 1 );
1506 msg_Dbg( p_intf, "requested playlist next" );
1509 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1510 p_sys->p_playlist->i_index - 1 );
1511 msg_Dbg( p_intf, "requested playlist next" );
1513 case MVLC_FULLSCREEN:
1515 if( p_sys->p_input )
1517 vout_thread_t *p_vout;
1518 p_vout = vlc_object_find( p_sys->p_input,
1519 VLC_OBJECT_VOUT, FIND_CHILD );
1523 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1524 vlc_object_release( p_vout );
1525 msg_Dbg( p_intf, "requested fullscreen toggle" );
1539 #define POSITION_ABSOLUTE 12
1540 #define POSITION_REL_FOR 13
1541 #define POSITION_REL_BACK 11
1542 #define TIME_ABSOLUTE 0
1543 #define TIME_REL_FOR 1
1544 #define TIME_REL_BACK -1
1545 if( p_sys->p_input )
1547 uri_extract_value( p_request, "seek_value", value, 20 );
1548 uri_decode_url_encoded( value );
1550 var_Get( p_sys->p_input, "length", &val);
1551 i_length = val.i_time;
1553 while( p_value[0] != '\0' )
1559 i_relative = TIME_REL_FOR;
1565 i_relative = TIME_REL_BACK;
1569 case '0': case '1': case '2': case '3': case '4':
1570 case '5': case '6': case '7': case '8': case '9':
1572 i_stock = strtol( p_value , &p_value , 10 );
1575 case '%': /* for percentage ie position */
1577 i_relative += POSITION_ABSOLUTE;
1585 i_value = 60 * (i_value + i_stock) ;
1590 case 'h': case 'H': /* hours */
1592 i_value += 3600 * i_stock;
1594 /* other characters which are not numbers are not important */
1595 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1601 case 'm': case 'M': case '\'': /* minutes */
1603 i_value += 60 * i_stock;
1606 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1612 case 's': case 'S': case '"': /* seconds */
1616 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1630 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1637 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1638 val.i_time = (uint64_t)( i_value ) * 1000000;
1640 val.i_time = i_length;
1642 var_Set( p_sys->p_input, "time", val );
1643 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1648 var_Get( p_sys->p_input, "time", &val );
1649 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1651 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1654 val.i_time = i_length;
1656 var_Set( p_sys->p_input, "time", val );
1657 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1662 var_Get( p_sys->p_input, "time", &val );
1663 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1668 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1670 var_Set( p_sys->p_input, "time", val );
1671 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1674 case POSITION_ABSOLUTE:
1676 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1677 var_Set( p_sys->p_input, "position", val );
1678 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1681 case POSITION_REL_FOR:
1683 var_Get( p_sys->p_input, "position", &val );
1684 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1685 var_Set( p_sys->p_input, "position", val );
1686 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1689 case POSITION_REL_BACK:
1691 var_Get( p_sys->p_input, "position", &val );
1692 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1693 var_Set( p_sys->p_input, "position", val );
1694 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
1699 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1704 #undef POSITION_ABSOLUTE
1705 #undef POSITION_REL_FOR
1706 #undef POSITION_REL_BACK
1707 #undef TIME_ABSOLUTE
1709 #undef TIME_REL_BACK
1715 audio_volume_t i_volume;
1718 uri_extract_value( p_request, "value", vol, 8 );
1719 aout_VolumeGet( p_intf, &i_volume );
1720 uri_decode_url_encoded( vol );
1724 i_value = atoi( vol + 1 );
1725 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1727 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1728 msg_Dbg( p_intf, "requested volume set: max" );
1731 aout_VolumeSet( p_intf , (i_volume + i_value) );
1732 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1737 i_value = atoi( vol + 1 );
1738 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1740 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1741 msg_Dbg( p_intf, "requested volume set: min" );
1744 aout_VolumeSet( p_intf , (i_volume - i_value) );
1745 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1749 i_value = atoi( vol );
1750 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1752 aout_VolumeSet( p_intf , atoi( vol ) );
1753 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1759 /* playlist management */
1763 playlist_item_t * p_item;
1765 uri_extract_value( p_request, "mrl", mrl, 512 );
1766 uri_decode_url_encoded( mrl );
1767 p_item = parse_MRL( mrl );
1769 if( !p_item || !p_item->psz_uri || !*p_item->psz_uri )
1771 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1774 playlist_AddItem( p_sys->p_playlist , p_item ,
1775 PLAYLIST_APPEND, PLAYLIST_END );
1776 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1783 int i_item, *p_items = NULL, i_nb_items = 0;
1784 char item[512], *p_parser = p_request;
1786 /* Get the list of items to delete */
1788 uri_extract_value( p_parser, "item", item, 512 )) )
1790 if( !*item ) continue;
1792 i_item = atoi( item );
1793 p_items = realloc( p_items, (i_nb_items + 1) *
1795 p_items[i_nb_items] = i_item;
1799 /* The items need to be deleted from in reversed order */
1803 for( i = 0; i < i_nb_items; i++ )
1806 for( j = 0; j < i_nb_items; j++ )
1808 if( p_items[j] > p_items[i_index] )
1812 playlist_Delete( p_sys->p_playlist,
1814 msg_Dbg( p_intf, "requested playlist delete: %d",
1816 p_items[i_index] = -1;
1820 if( p_items ) free( p_items );
1825 int i_item, *p_items = NULL, i_nb_items = 0;
1826 char item[512], *p_parser = p_request;
1829 /* Get the list of items to keep */
1831 uri_extract_value( p_parser, "item", item, 512 )) )
1833 if( !*item ) continue;
1835 i_item = atoi( item );
1836 p_items = realloc( p_items, (i_nb_items + 1) *
1838 p_items[i_nb_items] = i_item;
1842 /* The items need to be deleted from in reversed order */
1843 for( i = p_sys->p_playlist->i_size - 1; i >= 0 ; i-- )
1845 /* Check if the item is in the keep list */
1846 for( j = 0 ; j < i_nb_items ; j++ )
1848 if( p_items[j] == i ) break;
1850 if( j == i_nb_items )
1852 playlist_Delete( p_sys->p_playlist, i );
1853 msg_Dbg( p_intf, "requested playlist delete: %d",
1858 if( p_items ) free( p_items );
1863 while( p_sys->p_playlist->i_size > 0 )
1865 playlist_Delete( p_sys->p_playlist, 0 );
1867 msg_Dbg( p_intf, "requested playlist empty" );
1876 uri_extract_value( p_request, "type", type, 12 );
1877 uri_extract_value( p_request, "order", order, 2 );
1879 if( order[0] == '0' ) i_order = ORDER_NORMAL;
1880 else i_order = ORDER_REVERSE;
1882 if( !strcmp( type , "title" ) )
1884 playlist_SortTitle( p_sys->p_playlist , i_order );
1885 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
1886 } else if( !strcmp( type , "group" ) )
1888 playlist_SortGroup( p_sys->p_playlist , i_order );
1889 msg_Dbg( p_intf, "requested playlist sort by group (%d)" , i_order );
1890 } else if( !strcmp( type , "author" ) )
1892 playlist_SortAuthor( p_sys->p_playlist , i_order );
1893 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
1899 /* admin function */
1903 uri_extract_value( p_request, "id", id, 512 );
1904 msg_Dbg( p_intf, "requested close id=%s", id );
1905 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
1907 msg_Warn( p_intf, "close failed for id=%s", id );
1913 msg_Dbg( p_intf, "requested shutdown" );
1914 p_intf->p_vlc->b_die = VLC_TRUE;
1918 PRINTS( "<!-- control param(%s) unsuported -->", control );
1929 if( i_request <= 0 ||
1930 *m->param1 == '\0' ||
1931 strstr( p_request, m->param1 ) == NULL )
1935 uri_extract_value( p_request, m->param1, value, 512 );
1936 uri_decode_url_encoded( value );
1938 switch( StrToMacroType( m->param2 ) )
1942 config_PutInt( p_intf, m->param1, i );
1946 config_PutFloat( p_intf, m->param1, f );
1949 config_PutPsz( p_intf, m->param1, value );
1952 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
1963 if( *m->param1 == '\0' )
1968 switch( StrToMacroType( m->param2 ) )
1971 i = config_GetInt( p_intf, m->param1 );
1972 sprintf( value, "%i", i );
1975 f = config_GetFloat( p_intf, m->param1 );
1976 sprintf( value, "%f", f );
1979 psz = config_GetPsz( p_intf, m->param1 );
1980 sprintf( value, "%s", psz ? psz : "" );
1981 if( psz ) free( psz );
1984 sprintf( value, "invalid type(%s) in set", m->param2 );
1987 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
1988 PRINTS( "%s", value );
1997 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1998 s = SSPop( &p_args->stack );
1999 v = mvar_GetValue( p_args->vars, s );
2003 v = s = SSPop( &p_args->stack );
2011 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2015 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2016 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2024 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
2031 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
2036 i_skip = MacroParse( &m, src );
2038 i_id = StrToMacroType( m.id );
2055 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2056 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2058 return src + ( b_after ? i_skip : 0 );
2060 else if( i_level < 0 )
2076 static void Execute( httpd_file_callback_args_t *p_args,
2077 uint8_t *p_request, int i_request,
2078 uint8_t **pp_data, int *pi_data,
2080 uint8_t *_src, uint8_t *_end )
2082 intf_thread_t *p_intf = p_args->p_intf;
2084 uint8_t *src, *dup, *end;
2085 uint8_t *dst = *pp_dst;
2087 src = dup = malloc( _end - _src + 1 );
2088 end = src +( _end - _src );
2090 memcpy( src, _src, _end - _src );
2093 /* we parse searching <vlc */
2099 p = strstr( src, "<vlc" );
2100 if( p < end && p == src )
2104 src += MacroParse( &m, src );
2106 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2108 switch( StrToMacroType( m.id ) )
2115 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2116 if( SSPopN( &p_args->stack, p_args->vars ) )
2124 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2128 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2132 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2135 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2139 else if( i_test == 1 )
2142 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2144 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2148 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2157 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2158 uint8_t *start = src;
2159 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2166 if( !strcmp( m.param2, "integer" ) )
2168 char *arg = SSPop( &p_args->stack );
2169 index = mvar_IntegerSetNew( m.param1, arg );
2172 else if( !strcmp( m.param2, "directory" ) )
2174 char *arg = SSPop( &p_args->stack );
2175 index = mvar_FileSetNew( m.param1, arg );
2178 else if( !strcmp( m.param2, "playlist" ) )
2180 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2182 else if( !strcmp( m.param2, "informations" ) )
2184 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2186 else if( !strcmp( m.param2, "hosts" ) )
2188 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2190 else if( !strcmp( m.param2, "urls" ) )
2192 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2194 else if( !strcmp( m.param2, "connections" ) )
2196 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2198 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2200 index = mvar_Duplicate( v );
2204 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2209 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2211 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2213 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2216 f->name = strdup( m.param1 );
2219 mvar_PushVar( p_args->vars, f );
2220 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2221 mvar_RemoveVar( p_args->vars, f );
2225 mvar_Delete( index );
2232 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2240 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2243 int i_index = dst - *pp_data;
2246 *pp_data = realloc( *pp_data, *pi_data );
2247 dst = (*pp_data) + i_index;
2249 memcpy( dst, src, i_copy );
2259 /****************************************************************************
2261 ****************************************************************************
2262 * a file with mime == text/html is parsed and all "macro" replaced
2263 * <vlc id="macro name" [param1="" [param2=""]] />
2266 ****************************************************************************/
2267 static int http_get( httpd_file_callback_args_t *p_args,
2268 uint8_t *p_request, int i_request,
2269 uint8_t **pp_data, int *pi_data )
2274 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2276 p = *pp_data = malloc( 10240 );
2279 return VLC_EGENERIC;
2281 p += sprintf( p, "<html>\n" );
2282 p += sprintf( p, "<head>\n" );
2283 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2284 p += sprintf( p, "</head>\n" );
2285 p += sprintf( p, "<body>\n" );
2286 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2287 p += sprintf( p, "<hr />\n" );
2288 p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
2289 p += sprintf( p, "</body>\n" );
2290 p += sprintf( p, "</html>\n" );
2292 *pi_data = strlen( *pp_data );
2297 if( strcmp( p_args->mime, "text/html" ) )
2299 FileLoad( f, pp_data, pi_data );
2307 char position[4]; /* percentage */
2308 char time[12]; /* in seconds */
2309 char length[12]; /* in seconds */
2310 audio_volume_t i_volume;
2314 #define p_sys p_args->p_intf->p_sys
2315 if( p_sys->p_input )
2317 var_Get( p_sys->p_input, "position", &val);
2318 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2319 var_Get( p_sys->p_input, "time", &val);
2320 sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2321 var_Get( p_sys->p_input, "length", &val);
2322 sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2324 var_Get( p_sys->p_input, "state", &val );
2325 if( val.i_int == PLAYING_S )
2327 sprintf( state, "playing" );
2328 } else if( val.i_int == PAUSE_S )
2330 sprintf( state, "paused" );
2333 sprintf( state, "stop" );
2337 sprintf( position, "%d", 0 );
2338 sprintf( time, "%d", 0 );
2339 sprintf( length, "%d", 0 );
2340 sprintf( state, "stop" );
2344 aout_VolumeGet( p_args->p_intf , &i_volume );
2345 sprintf( volume , "%d" , (int)i_volume );
2347 p_args->vars = mvar_New( "variables", "" );
2348 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2349 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2350 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
2351 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2352 mvar_AppendNewVar( p_args->vars, "stream_position", position );
2353 mvar_AppendNewVar( p_args->vars, "stream_time", time );
2354 mvar_AppendNewVar( p_args->vars, "stream_length", length );
2355 mvar_AppendNewVar( p_args->vars, "volume", volume );
2356 mvar_AppendNewVar( p_args->vars, "stream_state", state );
2358 SSInit( &p_args->stack );
2360 /* first we load in a temporary buffer */
2361 FileLoad( f, &p_buffer, &i_buffer );
2363 /* allocate output */
2364 *pi_data = i_buffer + 1000;
2365 dst = *pp_data = malloc( *pi_data );
2367 /* we parse executing all <vlc /> macros */
2368 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2371 *pi_data = dst - *pp_data;
2373 SSClean( &p_args->stack );
2374 mvar_Delete( p_args->vars );
2383 /****************************************************************************
2385 ****************************************************************************/
2386 static char *uri_extract_value( char *psz_uri, char *psz_name,
2387 char *psz_value, int i_value_max )
2391 while( (p = strstr( p, psz_name )) )
2393 /* Verify that we are dealing with a post/get argument */
2394 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2403 p += strlen( psz_name );
2404 if( *p == '=' ) p++;
2406 if( strchr( p, '&' ) )
2408 i_len = strchr( p, '&' ) - p;
2412 /* for POST method */
2413 if( strchr( p, '\n' ) )
2415 i_len = strchr( p, '\n' ) - p;
2416 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2420 i_len = strlen( p );
2423 i_len = __MIN( i_value_max - 1, i_len );
2426 strncpy( psz_value, p, i_len );
2427 psz_value[i_len] = '\0';
2431 strncpy( psz_value, "", i_value_max );
2437 strncpy( psz_value, "", i_value_max );
2443 static void uri_decode_url_encoded( char *psz )
2445 char *dup = strdup( psz );
2463 *psz++ = strtol( val, NULL, 16 );
2465 else if( *p == '+' )
2479 /****************************************************************************
2480 * Light RPN evaluator
2481 ****************************************************************************/
2482 static void SSInit( rpn_stack_t *st )
2487 static void SSClean( rpn_stack_t *st )
2489 while( st->i_stack > 0 )
2491 free( st->stack[--st->i_stack] );
2495 static void SSPush( rpn_stack_t *st, char *s )
2497 if( st->i_stack < STACK_MAX )
2499 st->stack[st->i_stack++] = strdup( s );
2503 static char * SSPop( rpn_stack_t *st )
2505 if( st->i_stack <= 0 )
2507 return strdup( "" );
2511 return st->stack[--st->i_stack];
2515 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2524 i = strtol( name, &end, 0 );
2527 value = mvar_GetValue( vars, name );
2535 static void SSPushN( rpn_stack_t *st, int i )
2539 sprintf( v, "%d", i );
2543 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2550 while( *exp == ' ' )
2557 /* extract string */
2560 while( *exp && *exp != '\'' )
2571 p = strchr( exp, ' ' );
2576 exp += strlen( exp );
2581 strncpy( s, exp, i );
2592 /* 1. Integer function */
2593 if( !strcmp( s, "!" ) )
2595 SSPushN( st, ~SSPopN( st, vars ) );
2597 else if( !strcmp( s, "^" ) )
2599 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2601 else if( !strcmp( s, "&" ) )
2603 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2605 else if( !strcmp( s, "|" ) )
2607 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2609 else if( !strcmp( s, "+" ) )
2611 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2613 else if( !strcmp( s, "-" ) )
2615 int j = SSPopN( st, vars );
2616 int i = SSPopN( st, vars );
2617 SSPushN( st, i - j );
2619 else if( !strcmp( s, "*" ) )
2621 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2623 else if( !strcmp( s, "/" ) )
2627 j = SSPopN( st, vars );
2628 i = SSPopN( st, vars );
2630 SSPushN( st, j != 0 ? i / j : 0 );
2632 else if( !strcmp( s, "%" ) )
2636 j = SSPopN( st, vars );
2637 i = SSPopN( st, vars );
2639 SSPushN( st, j != 0 ? i % j : 0 );
2641 /* 2. integer tests */
2642 else if( !strcmp( s, "=" ) )
2644 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2646 else if( !strcmp( s, "<" ) )
2648 int j = SSPopN( st, vars );
2649 int i = SSPopN( st, vars );
2651 SSPushN( st, i < j ? -1 : 0 );
2653 else if( !strcmp( s, ">" ) )
2655 int j = SSPopN( st, vars );
2656 int i = SSPopN( st, vars );
2658 SSPushN( st, i > j ? -1 : 0 );
2660 else if( !strcmp( s, "<=" ) )
2662 int j = SSPopN( st, vars );
2663 int i = SSPopN( st, vars );
2665 SSPushN( st, i <= j ? -1 : 0 );
2667 else if( !strcmp( s, ">=" ) )
2669 int j = SSPopN( st, vars );
2670 int i = SSPopN( st, vars );
2672 SSPushN( st, i >= j ? -1 : 0 );
2674 /* 3. string functions */
2675 else if( !strcmp( s, "strcat" ) )
2677 char *s2 = SSPop( st );
2678 char *s1 = SSPop( st );
2679 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2689 else if( !strcmp( s, "strcmp" ) )
2691 char *s2 = SSPop( st );
2692 char *s1 = SSPop( st );
2694 SSPushN( st, strcmp( s1, s2 ) );
2698 else if( !strcmp( s, "strncmp" ) )
2700 int n = SSPopN( st, vars );
2701 char *s2 = SSPop( st );
2702 char *s1 = SSPop( st );
2704 SSPushN( st, strncmp( s1, s2 , n ) );
2708 else if( !strcmp( s, "strlen" ) )
2710 char *str = SSPop( st );
2712 SSPushN( st, strlen( str ) );
2715 /* 4. stack functions */
2716 else if( !strcmp( s, "dup" ) )
2718 char *str = SSPop( st );
2723 else if( !strcmp( s, "drop" ) )
2725 char *str = SSPop( st );
2728 else if( !strcmp( s, "swap" ) )
2730 char *s1 = SSPop( st );
2731 char *s2 = SSPop( st );
2738 else if( !strcmp( s, "flush" ) )
2743 else if( !strcmp( s, "store" ) )
2745 char *value = SSPop( st );
2746 char *name = SSPop( st );
2748 mvar_PushNewVar( vars, name, value );
2752 else if( !strcmp( s, "value" ) )
2754 char *name = SSPop( st );
2755 char *value = mvar_GetValue( vars, name );
2757 SSPush( st, value );
2761 else if( !strcmp( s, "url_extract" ) )
2763 char *url = mvar_GetValue( vars, "url_value" );
2764 char *name = SSPop( st );
2767 uri_extract_value( url, name, value, 512 );
2768 uri_decode_url_encoded( value );
2769 SSPush( st, value );
2778 /**********************************************************************
2779 * Find_end_MRL: Find the end of the sentence :
2780 * this function parses the string psz and find the end of the item
2781 * and/or option with detecting the " and ' problems.
2782 * returns NULL if an error is detected, otherwise, returns a pointer
2783 * of the end of the sentence (after the last character)
2784 **********************************************************************/
2785 static char *Find_end_MRL( char *psz )
2795 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
2797 if( *s_sent == '\'' )
2799 s_sent = Find_end_MRL( s_sent );
2801 if( s_sent == NULL )
2811 if( *s_sent == '\"' )
2815 } else /* *s_sent == '\0' , which means the number of " is incorrect */
2825 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
2827 if( *s_sent == '\"' )
2829 s_sent = Find_end_MRL( s_sent );
2831 if( s_sent == NULL )
2841 if( *s_sent == '\'' )
2845 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
2851 default: /* now we can look for spaces */
2853 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
2855 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
2857 s_sent = Find_end_MRL( s_sent );
2868 /**********************************************************************
2869 * parse_MRL: parse the MRL, find the mrl string and the options,
2870 * create an item with all informations in it, and return the item.
2871 * return NULL if there is an error.
2872 **********************************************************************/
2873 playlist_item_t * parse_MRL( char *psz )
2875 char **ppsz_options = NULL;
2882 playlist_item_t * p_item = NULL;
2884 /* In case there is spaces before the mrl */
2885 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
2890 /* extract the mrl */
2891 s_temp = strstr( s_mrl , " :" );
2892 if( s_temp == NULL )
2894 s_temp = s_mrl + strlen( s_mrl );
2897 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
2904 /* if the mrl is between " or ', we must remove them */
2905 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
2907 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
2908 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
2909 mrl[ s_temp - s_mrl - 2 ] = '\0';
2912 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
2913 strncpy( mrl , s_mrl , s_temp - s_mrl );
2914 mrl[ s_temp - s_mrl ] = '\0';
2919 /* now we can take care of the options */
2920 while( (*s_mrl != '\0') && (i_error == 0) )
2929 case ':': /* an option */
2931 s_temp = Find_end_MRL( s_mrl );
2933 if( s_temp == NULL )
2940 ppsz_options = realloc( ppsz_options , i_options *
2942 ppsz_options[ i_options - 1 ] =
2943 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
2945 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
2948 /* don't forget to finish the string with a '\0' */
2949 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
2969 /* now create an item */
2970 p_item = malloc( sizeof( playlist_item_t ) );
2971 memset( p_item, 0, sizeof( playlist_item_t ) );
2972 p_item->psz_name = mrl;
2973 p_item->psz_uri = strdup( mrl );
2974 p_item->i_duration = -1;
2975 p_item->b_enabled = VLC_TRUE;
2976 p_item->i_group = PLAYLIST_TYPE_MANUAL;
2978 for( i = 0 ; i< i_options ; i++ )
2980 playlist_AddItemOption( p_item, ppsz_options[i] );
2984 for( i = 0 ; i < i_options ; i++ )
2986 free( ppsz_options[i] );
2988 free( ppsz_options );