1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
5 * $Id: http.c,v 1.49 2004/01/18 07:35:31 fenrir 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 static char *Find_end_MRL( char *psz );
129 static playlist_item_t * parse_MRL( char *psz );
131 /*****************************************************************************
133 *****************************************************************************/
134 typedef struct mvar_s
140 struct mvar_s **field;
143 #define STACK_MAX 100
146 char *stack[STACK_MAX];
150 struct httpd_file_callback_args_t
152 intf_thread_t *p_intf;
153 httpd_file_t *p_file;
159 /* inited for each access */
167 httpd_host_t *p_httpd_host;
170 httpd_file_callback_args_t **pp_files;
172 playlist_t *p_playlist;
173 input_thread_t *p_input;
178 /*****************************************************************************
179 * Activate: initialize and create stuff
180 *****************************************************************************/
181 static int Activate( vlc_object_t *p_this )
183 intf_thread_t *p_intf = (intf_thread_t*)p_this;
186 char *psz_address = "";
190 psz_host = config_GetPsz( p_intf, "http-host" );
194 psz_address = psz_host;
196 psz_parser = strchr( psz_host, ':' );
199 *psz_parser++ = '\0';
200 i_port = atoi( psz_parser );
208 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
209 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
212 return( VLC_ENOMEM );
214 p_sys->p_playlist = NULL;
215 p_sys->p_input = NULL;
217 if( ( p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_intf), VLC_TRUE ) ) == NULL )
219 msg_Err( p_intf, "cannot create/find httpd" );
224 if( ( p_sys->p_httpd_host =
225 p_sys->p_httpd->pf_register_host( p_sys->p_httpd,
226 psz_address, i_port ) ) == NULL )
228 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
229 httpd_Release( p_sys->p_httpd );
240 p_sys->pp_files = malloc( sizeof( httpd_file_callback_args_t *) );
241 if( !p_sys->pp_files )
243 return( VLC_ENOMEM );
246 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
247 ( defined(WIN32) && !defined(UNDER_CE ) )
248 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
250 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
251 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
254 return( VLC_ENOMEM );
257 sprintf( psz_src, "%s/http", psz_vlcpath);
259 sprintf( psz_src, "%s/share/http", psz_vlcpath);
263 psz_src = config_GetPsz( p_intf, "http-src" );
265 if( !psz_src || *psz_src == '\0' )
267 if( !DirectoryCheck( "share/http" ) )
269 psz_src = strdup( "share/http" );
271 else if( !DirectoryCheck( DATA_PATH "/http" ) )
273 psz_src = strdup( DATA_PATH "/http" );
278 if( !psz_src || *psz_src == '\0' )
280 msg_Err( p_intf, "invalid src dir" );
284 /* remove trainling \ or / */
285 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
286 psz_src[strlen( psz_src ) - 1] == '/' )
288 psz_src[strlen( psz_src ) - 1] = '\0';
291 ParseDirectory( p_intf, psz_src, psz_src );
294 if( p_sys->i_files <= 0 )
296 msg_Err( p_intf, "cannot find any files (%s)", psz_src );
299 p_intf->pf_run = Run;
305 if( psz_src ) free( psz_src );
306 free( p_sys->pp_files );
307 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
308 p_sys->p_httpd_host );
309 httpd_Release( p_sys->p_httpd );
314 /*****************************************************************************
315 * CloseIntf: destroy interface
316 *****************************************************************************/
317 void Close ( vlc_object_t *p_this )
319 intf_thread_t *p_intf = (intf_thread_t *)p_this;
320 intf_sys_t *p_sys = p_intf->p_sys;
324 for( i = 0; i < p_sys->i_files; i++ )
326 p_sys->p_httpd->pf_unregister_file( p_sys->p_httpd,
327 p_sys->pp_files[i]->p_file );
328 /* do not free mime */
329 free( p_sys->pp_files[i]->file );
330 free( p_sys->pp_files[i]->name );
331 free( p_sys->pp_files[i] );
333 free( p_sys->pp_files );
334 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
335 p_sys->p_httpd_host );
336 httpd_Release( p_sys->p_httpd );
340 /*****************************************************************************
341 * Run: http interface thread
342 *****************************************************************************/
343 static void Run( intf_thread_t *p_intf )
345 intf_sys_t *p_sys = p_intf->p_sys;
347 while( !p_intf->b_die )
349 /* get the playlist */
350 if( p_sys->p_playlist == NULL )
352 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
355 /* Manage the input part */
356 if( p_sys->p_input == NULL )
358 if( p_sys->p_playlist )
361 vlc_object_find( p_sys->p_playlist,
366 else if( p_sys->p_input->b_dead )
368 vlc_object_release( p_sys->p_input );
369 p_sys->p_input = NULL;
374 msleep( INTF_IDLE_SLEEP );
379 vlc_object_release( p_sys->p_input );
380 p_sys->p_input = NULL;
383 if( p_sys->p_playlist )
385 vlc_object_release( p_sys->p_playlist );
386 p_sys->p_playlist = NULL;
391 /*****************************************************************************
393 *****************************************************************************/
394 #define MAX_DIR_SIZE 10240
396 /****************************************************************************
397 * FileToUrl: create a good name for an url from filename
398 ****************************************************************************/
399 static char *FileToUrl( char *name )
403 url = p = malloc( strlen( name ) + 1 );
411 while( *name == '\\' || *name == '/' )
413 while( *name == '/' )
423 /* convert '\\' into '/' */
436 if( ( p = strrchr( url, '/' ) ) != NULL )
438 if( !strncmp( p, "/index.", 7 ) )
446 /****************************************************************************
447 * FileToMime: XXX duplicated with modules/access_out/http.c
448 ****************************************************************************/
455 { ".htm", "text/html" },
456 { ".html", "text/html" },
458 { ".css", "text/css" },
461 { ".avi", "video/avi" },
462 { ".asf", "video/x-ms-asf" },
463 { ".m1a", "audio/mpeg" },
464 { ".m2a", "audio/mpeg" },
465 { ".m1v", "video/mpeg" },
466 { ".m2v", "video/mpeg" },
467 { ".mp2", "audio/mpeg" },
468 { ".mp3", "audio/mpeg" },
469 { ".mpa", "audio/mpeg" },
470 { ".mpg", "video/mpeg" },
471 { ".mpeg", "video/mpeg" },
472 { ".mpe", "video/mpeg" },
473 { ".mov", "video/quicktime" },
474 { ".moov", "video/quicktime" },
475 { ".ogg", "application/ogg" },
476 { ".ogm", "application/ogg" },
477 { ".wav", "audio/wav" },
483 static char *FileToMime( char *psz_name )
487 psz_ext = strrchr( psz_name, '.' );
492 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
494 if( !strcmp( http_mime[i].psz_ext, psz_ext ) )
496 return( http_mime[i].psz_mime );
500 return( "application/octet-stream" );
503 /****************************************************************************
504 * ParseDirectory: parse recursively a directory, adding each file
505 ****************************************************************************/
506 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
509 intf_sys_t *p_sys = p_intf->p_sys;
510 char dir[MAX_DIR_SIZE];
511 #ifdef HAVE_SYS_STAT_H
512 struct stat stat_info;
515 struct dirent *p_dir_content;
519 char *password = NULL;
521 #ifdef HAVE_SYS_STAT_H
522 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
528 if( ( p_dir = opendir( psz_dir ) ) == NULL )
530 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
534 msg_Dbg( p_intf, "dir=%s", psz_dir );
536 sprintf( dir, "%s/.access", psz_dir );
537 if( ( file = fopen( dir, "r" ) ) != NULL )
542 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
544 i_size = fread( line, 1, 1023, file );
548 while( i_size > 0 && ( line[i_size-1] == '\n' ||
549 line[i_size-1] == '\r' ) )
556 p = strchr( line, ':' );
560 user = strdup( line );
561 password = strdup( p );
564 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
565 user, password, i_size );
572 /* parse psz_src dir */
573 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
578 if( p_dir_content->d_name[0] == '.' )
582 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
583 if( ParseDirectory( p_intf, psz_root, dir ) )
585 #define f p_sys->pp_files[p_sys->i_files]
586 f = malloc( sizeof( httpd_file_callback_args_t ) );
589 msg_Err( p_intf, "Out of memory" );
591 return( VLC_ENOMEM );
594 f->file = strdup( dir );
595 f->name = FileToUrl( &dir[strlen( psz_root )] );
596 f->mime = FileToMime( &dir[strlen( psz_root )] );
598 if( !f->name || !f->mime )
600 msg_Err( p_intf , "Unable to parse directory" );
603 return( VLC_ENOMEM );
605 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)",
606 f->file, f->name, f->mime );
609 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
617 p_sys->pp_files = realloc( p_sys->pp_files,
618 (p_sys->i_files+1) * sizeof( httpd_file_callback_args_t ) );
620 #define fold p_sys->pp_files[p_sys->i_files-1]
622 /* FIXME for rep/ add rep (it would be better to do a redirection) */
623 if( p_sys->i_files && strlen(fold->name) > 1 &&
624 fold->name[strlen(fold->name) - 1] == '/' )
626 f = malloc( sizeof( httpd_file_callback_args_t ) );
629 msg_Err( p_intf, "Out of memory" );
631 return( VLC_ENOMEM );
634 f->file = strdup( fold->file );
635 f->name = strdup( fold->name );
636 f->mime = fold->mime;
638 f->name[strlen(f->name) - 1] = '\0';
639 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)", f->file, f->name,
642 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
651 realloc( p_sys->pp_files, (p_sys->i_files+1) *
652 sizeof( httpd_file_callback_args_t ) );
674 /****************************************************************************
675 * var and set handling
676 ****************************************************************************/
678 static mvar_t *mvar_New( char *name, char *value )
680 mvar_t *v = malloc( sizeof( mvar_t ) );
682 if( !v ) return NULL;
683 v->name = strdup( name );
684 v->value = strdup( value ? value : "" );
687 v->field = malloc( sizeof( mvar_t * ) );
693 static void mvar_Delete( mvar_t *v )
700 for( i = 0; i < v->i_field; i++ )
702 mvar_Delete( v->field[i] );
708 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
710 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
711 v->field[v->i_field] = f;
715 static mvar_t *mvar_Duplicate( mvar_t *v )
720 n = mvar_New( v->name, v->value );
721 for( i = 0; i < v->i_field; i++ )
723 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
729 static void mvar_PushVar( mvar_t *v, mvar_t *f )
731 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
734 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
740 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
743 for( i = 0; i < v->i_field; i++ )
745 if( v->field[i] == f )
750 if( i >= v->i_field )
755 if( i + 1 < v->i_field )
757 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
760 /* FIXME should do a realloc */
763 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
766 char base[512], *field, *p;
769 /* format: name[index].field */
771 field = strchr( name, '.' );
774 int i = field - name;
775 strncpy( base, name, i );
781 strcpy( base, name );
784 if( ( p = strchr( base, '[' ) ) )
787 sscanf( p, "%d]", &i_index );
798 for( i = 0; i < s->i_field; i++ )
800 if( !strcmp( s->field[i]->name, base ) )
810 return mvar_GetVar( s->field[i], field );
824 static char *mvar_GetValue( mvar_t *v, char *field )
832 mvar_t *f = mvar_GetVar( v, field );
844 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
846 mvar_t *f = mvar_New( name, value );
847 mvar_PushVar( vars, f );
850 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
852 mvar_t *f = mvar_New( name, value );
853 mvar_AppendVar( vars, f );
857 /* arg= start[:stop[:step]],.. */
858 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
860 char *dup = strdup( arg );
862 mvar_t *s = mvar_New( name, "set" );
864 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
869 int i_start,i_stop,i_step;
872 p = strchr( str, ',' );
879 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
880 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
887 else if( i_match == 2 )
889 i_step = i_start < i_stop ? 1 : -1;
896 if( ( i_start < i_stop && i_step > 0 ) ||
897 ( i_start > i_stop && i_step < 0 ) )
899 for( i = i_start; ; i += i_step )
903 if( ( i_step > 0 && i > i_stop ) ||
904 ( i_step < 0 && i < i_stop ) )
909 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
910 sprintf( value, "%d", i );
912 mvar_PushNewVar( s, name, value );
923 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
925 mvar_t *s = mvar_New( name, "set" );
928 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
930 vlc_mutex_lock( &p_pl->object_lock );
931 for( i = 0; i < p_pl->i_size; i++ )
933 mvar_t *itm = mvar_New( name, "set" );
936 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
937 mvar_AppendNewVar( itm, "current", value );
939 sprintf( value, "%d", i );
940 mvar_AppendNewVar( itm, "index", value );
942 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->psz_name );
944 sprintf( value, "%d", p_pl->pp_items[i]->i_group );
945 mvar_AppendNewVar( itm, "group", value );
947 mvar_AppendVar( s, itm );
949 vlc_mutex_unlock( &p_pl->object_lock );
954 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
956 mvar_t *s = mvar_New( name, "set" );
958 input_info_category_t * p_category;
959 input_info_t * p_info;
961 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
962 if( p_input == NULL )
967 vlc_mutex_lock( &p_input->stream.stream_lock );
968 p_category = p_input->stream.p_info;
971 mvar_t *cat = mvar_New( name, "set" );
972 mvar_t *iset = mvar_New( "info", "set" );
974 mvar_AppendNewVar( cat, "name", p_category->psz_name );
975 mvar_AppendVar( cat, iset );
977 p_info = p_category->p_info;
980 mvar_t *info = mvar_New( "info", "" );
982 msg_Dbg( p_input, "adding info name=%s value=%s", p_info->psz_name, p_info->psz_value );
983 mvar_AppendNewVar( info, "name", p_info->psz_name );
984 mvar_AppendNewVar( info, "value", p_info->psz_value );
985 mvar_AppendVar( iset, info );
986 p_info = p_info->p_next;
988 mvar_AppendVar( s, cat );
989 p_category = p_category->p_next;
991 vlc_mutex_unlock( &p_input->stream.stream_lock );
996 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
998 mvar_t *s = mvar_New( name, "set" );
1002 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
1003 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
1005 for( i= 0; i < info.i_count; )
1009 inf = mvar_New( name, "set" );
1012 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
1013 info.info[i].psz_name, info.info[i].psz_value ); */
1014 mvar_AppendNewVar( inf,
1015 info.info[i].psz_name,
1016 info.info[i].psz_value );
1018 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
1019 mvar_AppendVar( s, inf );
1024 for( i = 0; i < info.i_count; i++ )
1026 free( info.info[i].psz_name );
1027 free( info.info[i].psz_value );
1029 if( info.i_count > 0 )
1036 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1038 mvar_t *s = mvar_New( name, "set" );
1039 char tmp[MAX_DIR_SIZE], *p, *src;
1040 #ifdef HAVE_SYS_STAT_H
1041 struct stat stat_info;
1044 struct dirent *p_dir_content;
1047 /* convert all / to native separator */
1048 #if defined( WIN32 )
1049 while( (p = strchr( psz_dir, '/' )) )
1058 /* remove trailling separator */
1059 while( strlen( psz_dir ) > 1 &&
1060 #if defined( WIN32 )
1061 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1063 psz_dir[strlen( psz_dir ) -1 ] == sep )
1065 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1067 /* remove double separator */
1068 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1070 if( src[0] == sep && src[1] == sep )
1078 if( *psz_dir == '\0' )
1082 /* first fix all .. dir */
1086 if( src[0] == '.' && src[1] == '.' )
1089 if( p <= &psz_dir[1] )
1096 while( p > &psz_dir[1] && *p != sep )
1101 else if( *src == sep )
1103 if( p > psz_dir && p[-1] == sep )
1117 } while( *src && *src != sep );
1122 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1124 #ifdef HAVE_SYS_STAT_H
1125 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1131 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1133 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1137 /* remove traling / or \ */
1138 for( p = &psz_dir[strlen( psz_dir) - 1]; p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1147 /* parse psz_src dir */
1148 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1152 if( !strcmp( p_dir_content->d_name, "." ) )
1157 #if defined( WIN32 )
1158 sprintf( tmp, "%s\\%s", psz_dir, p_dir_content->d_name );
1160 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1163 #ifdef HAVE_SYS_STAT_H
1164 if( stat( tmp, &stat_info ) == -1 )
1169 f = mvar_New( name, "set" );
1170 mvar_AppendNewVar( f, "name", tmp );
1171 #ifdef HAVE_SYS_STAT_H
1172 if( S_ISDIR( stat_info.st_mode ) )
1174 mvar_AppendNewVar( f, "type", "directory" );
1176 else if( S_ISREG( stat_info.st_mode ) )
1178 mvar_AppendNewVar( f, "type", "file" );
1182 mvar_AppendNewVar( f, "type", "unknown" );
1185 sprintf( tmp, "%lld", stat_info.st_size );
1186 mvar_AppendNewVar( f, "size", tmp );
1188 /* FIXME memory leak FIXME */
1190 ctime_r( &stat_info.st_mtime, tmp );
1191 mvar_AppendNewVar( f, "date", tmp );
1193 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1197 mvar_AppendNewVar( f, "type", "unknown" );
1198 mvar_AppendNewVar( f, "size", "unknown" );
1199 mvar_AppendNewVar( f, "date", "unknown" );
1201 mvar_AppendVar( s, f );
1207 static void SSInit( rpn_stack_t * );
1208 static void SSClean( rpn_stack_t * );
1209 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1211 static void SSPush ( rpn_stack_t *, char * );
1212 static char *SSPop ( rpn_stack_t * );
1214 static void SSPushN ( rpn_stack_t *, int );
1215 static int SSPopN ( rpn_stack_t *, mvar_t * );
1218 /****************************************************************************
1220 ****************************************************************************/
1228 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1232 /* just load the file */
1234 *pp_data = malloc( 1025 ); /* +1 for \0 */
1235 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1238 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1244 (*pp_data)[*pi_data] = '\0';
1249 static int MacroParse( macro_t *m, uint8_t *psz_src )
1251 uint8_t *dup = strdup( psz_src );
1256 #define EXTRACT( name, l ) \
1258 p = strchr( src, '"' ); \
1263 m->name = strdup( src ); \
1284 if( !strncmp( src, "id=\"", 4 ) )
1288 else if( !strncmp( src, "param1=\"", 8 ) )
1290 EXTRACT( param1, 8 );
1292 else if( !strncmp( src, "param2=\"", 8 ) )
1294 EXTRACT( param2, 8 );
1301 if( strstr( src, "/>" ) )
1303 src = strstr( src, "/>" ) + 2;
1307 src += strlen( src );
1312 m->id = strdup( "" );
1314 if( m->param1 == NULL )
1316 m->param1 = strdup( "" );
1318 if( m->param2 == NULL )
1320 m->param2 = strdup( "" );
1329 static void MacroClean( macro_t *m )
1375 StrToMacroTypeTab [] =
1377 { "control", MVLC_CONTROL },
1378 /* player control */
1379 { "play", MVLC_PLAY },
1380 { "stop", MVLC_STOP },
1381 { "pause", MVLC_PAUSE },
1382 { "next", MVLC_NEXT },
1383 { "previous", MVLC_PREVIOUS },
1384 { "seek", MVLC_SEEK },
1385 { "keep", MVLC_KEEP },
1386 { "fullscreen", MVLC_FULLSCREEN },
1387 { "volume", MVLC_VOLUME },
1389 /* playlist management */
1390 { "add", MVLC_ADD },
1391 { "delete", MVLC_DEL },
1392 { "empty", MVLC_EMPTY },
1393 { "sort", MVLC_SORT },
1396 { "close", MVLC_CLOSE },
1397 { "shutdown", MVLC_SHUTDOWN },
1399 { "rpn", MVLC_RPN },
1401 { "foreach", MVLC_FOREACH },
1402 { "value", MVLC_VALUE },
1405 { "else", MVLC_ELSE },
1406 { "end", MVLC_END },
1407 { "get", MVLC_GET },
1408 { "set", MVLC_SET },
1409 { "int", MVLC_INT },
1410 { "float", MVLC_FLOAT },
1411 { "string", MVLC_STRING },
1414 { NULL, MVLC_UNKNOWN }
1417 static int StrToMacroType( char *name )
1421 if( !name || *name == '\0')
1423 return MVLC_UNKNOWN;
1425 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1427 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1429 return StrToMacroTypeTab[i].i_type;
1432 return MVLC_UNKNOWN;
1435 static void MacroDo( httpd_file_callback_args_t *p_args,
1437 uint8_t *p_request, int i_request,
1438 uint8_t **pp_data, int *pi_data,
1441 intf_thread_t *p_intf = p_args->p_intf;
1442 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1445 #define ALLOC( l ) \
1447 int __i__ = *pp_dst - *pp_data; \
1449 *pp_data = realloc( *pp_data, *pi_data ); \
1450 *pp_dst = (*pp_data) + __i__; \
1452 #define PRINT( str ) \
1453 ALLOC( strlen( str ) + 1 ); \
1454 *pp_dst += sprintf( *pp_dst, str );
1456 #define PRINTS( str, s ) \
1457 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1459 char * psz_cur = *pp_dst; \
1460 *pp_dst += sprintf( *pp_dst, str, s ); \
1461 while( psz_cur && *psz_cur ) \
1463 /* Prevent script injection */ \
1464 if( *psz_cur == '<' ) *psz_cur = '*'; \
1465 if( *psz_cur == '>' ) *psz_cur = '*'; \
1470 switch( StrToMacroType( m->id ) )
1473 if( i_request <= 0 )
1477 uri_extract_value( p_request, "control", control, 512 );
1478 if( *m->param1 && !strstr( m->param1, control ) )
1480 msg_Warn( p_intf, "unauthorized control=%s", control );
1483 switch( StrToMacroType( control ) )
1490 uri_extract_value( p_request, "item", item, 512 );
1491 i_item = atoi( item );
1492 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
1493 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1497 playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1498 msg_Dbg( p_intf, "requested playlist stop" );
1501 playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1502 msg_Dbg( p_intf, "requested playlist pause" );
1505 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1506 p_sys->p_playlist->i_index + 1 );
1507 msg_Dbg( p_intf, "requested playlist next" );
1510 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1511 p_sys->p_playlist->i_index - 1 );
1512 msg_Dbg( p_intf, "requested playlist next" );
1514 case MVLC_FULLSCREEN:
1516 if( p_sys->p_input )
1518 vout_thread_t *p_vout;
1519 p_vout = vlc_object_find( p_sys->p_input,
1520 VLC_OBJECT_VOUT, FIND_CHILD );
1524 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1525 vlc_object_release( p_vout );
1526 msg_Dbg( p_intf, "requested fullscreen toggle" );
1540 #define POSITION_ABSOLUTE 12
1541 #define POSITION_REL_FOR 13
1542 #define POSITION_REL_BACK 11
1543 #define TIME_ABSOLUTE 0
1544 #define TIME_REL_FOR 1
1545 #define TIME_REL_BACK -1
1546 if( p_sys->p_input )
1548 uri_extract_value( p_request, "seek_value", value, 20 );
1549 uri_decode_url_encoded( value );
1551 var_Get( p_sys->p_input, "length", &val);
1552 i_length = val.i_time;
1554 while( p_value[0] != '\0' )
1560 i_relative = TIME_REL_FOR;
1566 i_relative = TIME_REL_BACK;
1570 case '0': case '1': case '2': case '3': case '4':
1571 case '5': case '6': case '7': case '8': case '9':
1573 i_stock = strtol( p_value , &p_value , 10 );
1576 case '%': /* for percentage ie position */
1578 i_relative += POSITION_ABSOLUTE;
1586 i_value = 60 * (i_value + i_stock) ;
1591 case 'h': case 'H': /* hours */
1593 i_value += 3600 * i_stock;
1595 /* other characters which are not numbers are not important */
1596 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1602 case 'm': case 'M': case '\'': /* minutes */
1604 i_value += 60 * i_stock;
1607 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1613 case 's': case 'S': case '"': /* seconds */
1617 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1631 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1638 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1639 val.i_time = (uint64_t)( i_value ) * 1000000;
1641 val.i_time = i_length;
1643 var_Set( p_sys->p_input, "time", val );
1644 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1649 var_Get( p_sys->p_input, "time", &val );
1650 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1652 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1655 val.i_time = i_length;
1657 var_Set( p_sys->p_input, "time", val );
1658 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1663 var_Get( p_sys->p_input, "time", &val );
1664 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1669 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1671 var_Set( p_sys->p_input, "time", val );
1672 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1675 case POSITION_ABSOLUTE:
1677 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1678 var_Set( p_sys->p_input, "position", val );
1679 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1682 case POSITION_REL_FOR:
1684 var_Get( p_sys->p_input, "position", &val );
1685 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1686 var_Set( p_sys->p_input, "position", val );
1687 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1690 case POSITION_REL_BACK:
1692 var_Get( p_sys->p_input, "position", &val );
1693 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1694 var_Set( p_sys->p_input, "position", val );
1695 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
1700 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1705 #undef POSITION_ABSOLUTE
1706 #undef POSITION_REL_FOR
1707 #undef POSITION_REL_BACK
1708 #undef TIME_ABSOLUTE
1710 #undef TIME_REL_BACK
1716 audio_volume_t i_volume;
1719 uri_extract_value( p_request, "value", vol, 8 );
1720 aout_VolumeGet( p_intf, &i_volume );
1721 uri_decode_url_encoded( vol );
1725 i_value = atoi( vol + 1 );
1726 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1728 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1729 msg_Dbg( p_intf, "requested volume set: max" );
1732 aout_VolumeSet( p_intf , (i_volume + i_value) );
1733 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1738 i_value = atoi( vol + 1 );
1739 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1741 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1742 msg_Dbg( p_intf, "requested volume set: min" );
1745 aout_VolumeSet( p_intf , (i_volume - i_value) );
1746 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1750 i_value = atoi( vol );
1751 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1753 aout_VolumeSet( p_intf , atoi( vol ) );
1754 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1760 /* playlist management */
1764 playlist_item_t * p_item;
1766 uri_extract_value( p_request, "mrl", mrl, 512 );
1767 uri_decode_url_encoded( mrl );
1768 p_item = parse_MRL( mrl );
1770 if( !p_item || !p_item->psz_uri || !*p_item->psz_uri )
1772 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1775 playlist_AddItem( p_sys->p_playlist , p_item ,
1776 PLAYLIST_APPEND, PLAYLIST_END );
1777 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1784 int i_item, *p_items = NULL, i_nb_items = 0;
1785 char item[512], *p_parser = p_request;
1787 /* Get the list of items to delete */
1789 uri_extract_value( p_parser, "item", item, 512 )) )
1791 if( !*item ) continue;
1793 i_item = atoi( item );
1794 p_items = realloc( p_items, (i_nb_items + 1) *
1796 p_items[i_nb_items] = i_item;
1800 /* The items need to be deleted from in reversed order */
1804 for( i = 0; i < i_nb_items; i++ )
1807 for( j = 0; j < i_nb_items; j++ )
1809 if( p_items[j] > p_items[i_index] )
1813 playlist_Delete( p_sys->p_playlist,
1815 msg_Dbg( p_intf, "requested playlist delete: %d",
1817 p_items[i_index] = -1;
1821 if( p_items ) free( p_items );
1826 int i_item, *p_items = NULL, i_nb_items = 0;
1827 char item[512], *p_parser = p_request;
1830 /* Get the list of items to keep */
1832 uri_extract_value( p_parser, "item", item, 512 )) )
1834 if( !*item ) continue;
1836 i_item = atoi( item );
1837 p_items = realloc( p_items, (i_nb_items + 1) *
1839 p_items[i_nb_items] = i_item;
1843 /* The items need to be deleted from in reversed order */
1844 for( i = p_sys->p_playlist->i_size - 1; i >= 0 ; i-- )
1846 /* Check if the item is in the keep list */
1847 for( j = 0 ; j < i_nb_items ; j++ )
1849 if( p_items[j] == i ) break;
1851 if( j == i_nb_items )
1853 playlist_Delete( p_sys->p_playlist, i );
1854 msg_Dbg( p_intf, "requested playlist delete: %d",
1859 if( p_items ) free( p_items );
1864 while( p_sys->p_playlist->i_size > 0 )
1866 playlist_Delete( p_sys->p_playlist, 0 );
1868 msg_Dbg( p_intf, "requested playlist empty" );
1877 uri_extract_value( p_request, "type", type, 12 );
1878 uri_extract_value( p_request, "order", order, 2 );
1880 if( order[0] == '0' ) i_order = ORDER_NORMAL;
1881 else i_order = ORDER_REVERSE;
1883 if( !strcmp( type , "title" ) )
1885 playlist_SortTitle( p_sys->p_playlist , i_order );
1886 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
1887 } else if( !strcmp( type , "group" ) )
1889 playlist_SortGroup( p_sys->p_playlist , i_order );
1890 msg_Dbg( p_intf, "requested playlist sort by group (%d)" , i_order );
1891 } else if( !strcmp( type , "author" ) )
1893 playlist_SortAuthor( p_sys->p_playlist , i_order );
1894 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
1900 /* admin function */
1904 uri_extract_value( p_request, "id", id, 512 );
1905 msg_Dbg( p_intf, "requested close id=%s", id );
1906 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
1908 msg_Warn( p_intf, "close failed for id=%s", id );
1914 msg_Dbg( p_intf, "requested shutdown" );
1915 p_intf->p_vlc->b_die = VLC_TRUE;
1919 PRINTS( "<!-- control param(%s) unsuported -->", control );
1930 if( i_request <= 0 ||
1931 *m->param1 == '\0' ||
1932 strstr( p_request, m->param1 ) == NULL )
1936 uri_extract_value( p_request, m->param1, value, 512 );
1937 uri_decode_url_encoded( value );
1939 switch( StrToMacroType( m->param2 ) )
1943 config_PutInt( p_intf, m->param1, i );
1947 config_PutFloat( p_intf, m->param1, f );
1950 config_PutPsz( p_intf, m->param1, value );
1953 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
1964 if( *m->param1 == '\0' )
1969 switch( StrToMacroType( m->param2 ) )
1972 i = config_GetInt( p_intf, m->param1 );
1973 sprintf( value, "%i", i );
1976 f = config_GetFloat( p_intf, m->param1 );
1977 sprintf( value, "%f", f );
1980 psz = config_GetPsz( p_intf, m->param1 );
1981 sprintf( value, "%s", psz ? psz : "" );
1982 if( psz ) free( psz );
1985 sprintf( value, "invalid type(%s) in set", m->param2 );
1988 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
1989 PRINTS( "%s", value );
1998 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1999 s = SSPop( &p_args->stack );
2000 v = mvar_GetValue( p_args->vars, s );
2004 v = s = SSPop( &p_args->stack );
2012 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2016 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2017 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2025 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
2032 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
2037 i_skip = MacroParse( &m, src );
2039 i_id = StrToMacroType( m.id );
2056 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2057 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2059 return src + ( b_after ? i_skip : 0 );
2061 else if( i_level < 0 )
2077 static void Execute( httpd_file_callback_args_t *p_args,
2078 uint8_t *p_request, int i_request,
2079 uint8_t **pp_data, int *pi_data,
2081 uint8_t *_src, uint8_t *_end )
2083 intf_thread_t *p_intf = p_args->p_intf;
2085 uint8_t *src, *dup, *end;
2086 uint8_t *dst = *pp_dst;
2088 src = dup = malloc( _end - _src + 1 );
2089 end = src +( _end - _src );
2091 memcpy( src, _src, _end - _src );
2094 /* we parse searching <vlc */
2100 p = strstr( src, "<vlc" );
2101 if( p < end && p == src )
2105 src += MacroParse( &m, src );
2107 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2109 switch( StrToMacroType( m.id ) )
2116 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2117 if( SSPopN( &p_args->stack, p_args->vars ) )
2125 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2129 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2133 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2136 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2140 else if( i_test == 1 )
2143 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2145 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2149 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2158 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2159 uint8_t *start = src;
2160 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2167 if( !strcmp( m.param2, "integer" ) )
2169 char *arg = SSPop( &p_args->stack );
2170 index = mvar_IntegerSetNew( m.param1, arg );
2173 else if( !strcmp( m.param2, "directory" ) )
2175 char *arg = SSPop( &p_args->stack );
2176 index = mvar_FileSetNew( m.param1, arg );
2179 else if( !strcmp( m.param2, "playlist" ) )
2181 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2183 else if( !strcmp( m.param2, "informations" ) )
2185 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2187 else if( !strcmp( m.param2, "hosts" ) )
2189 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2191 else if( !strcmp( m.param2, "urls" ) )
2193 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2195 else if( !strcmp( m.param2, "connections" ) )
2197 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2199 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2201 index = mvar_Duplicate( v );
2205 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2210 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2212 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2214 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2217 f->name = strdup( m.param1 );
2220 mvar_PushVar( p_args->vars, f );
2221 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2222 mvar_RemoveVar( p_args->vars, f );
2226 mvar_Delete( index );
2233 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2241 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2244 int i_index = dst - *pp_data;
2247 *pp_data = realloc( *pp_data, *pi_data );
2248 dst = (*pp_data) + i_index;
2250 memcpy( dst, src, i_copy );
2260 /****************************************************************************
2262 ****************************************************************************
2263 * a file with mime == text/html is parsed and all "macro" replaced
2264 * <vlc id="macro name" [param1="" [param2=""]] />
2267 ****************************************************************************/
2268 static int http_get( httpd_file_callback_args_t *p_args,
2269 uint8_t *p_request, int i_request,
2270 uint8_t **pp_data, int *pi_data )
2275 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2277 p = *pp_data = malloc( 10240 );
2280 return VLC_EGENERIC;
2282 p += sprintf( p, "<html>\n" );
2283 p += sprintf( p, "<head>\n" );
2284 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2285 p += sprintf( p, "</head>\n" );
2286 p += sprintf( p, "<body>\n" );
2287 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2288 p += sprintf( p, "<hr />\n" );
2289 p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
2290 p += sprintf( p, "</body>\n" );
2291 p += sprintf( p, "</html>\n" );
2293 *pi_data = strlen( *pp_data );
2298 if( strcmp( p_args->mime, "text/html" ) )
2300 FileLoad( f, pp_data, pi_data );
2308 char position[4]; /* percentage */
2309 char time[12]; /* in seconds */
2310 char length[12]; /* in seconds */
2311 audio_volume_t i_volume;
2315 #define p_sys p_args->p_intf->p_sys
2316 if( p_sys->p_input )
2318 var_Get( p_sys->p_input, "position", &val);
2319 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2320 var_Get( p_sys->p_input, "time", &val);
2321 sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2322 var_Get( p_sys->p_input, "length", &val);
2323 sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2325 var_Get( p_sys->p_input, "state", &val );
2326 if( val.i_int == PLAYING_S )
2328 sprintf( state, "playing" );
2329 } else if( val.i_int == PAUSE_S )
2331 sprintf( state, "paused" );
2334 sprintf( state, "stop" );
2338 sprintf( position, "%d", 0 );
2339 sprintf( time, "%d", 0 );
2340 sprintf( length, "%d", 0 );
2341 sprintf( state, "stop" );
2345 aout_VolumeGet( p_args->p_intf , &i_volume );
2346 sprintf( volume , "%d" , (int)i_volume );
2348 p_args->vars = mvar_New( "variables", "" );
2349 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2350 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2351 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
2352 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2353 mvar_AppendNewVar( p_args->vars, "stream_position", position );
2354 mvar_AppendNewVar( p_args->vars, "stream_time", time );
2355 mvar_AppendNewVar( p_args->vars, "stream_length", length );
2356 mvar_AppendNewVar( p_args->vars, "volume", volume );
2357 mvar_AppendNewVar( p_args->vars, "stream_state", state );
2359 SSInit( &p_args->stack );
2361 /* first we load in a temporary buffer */
2362 FileLoad( f, &p_buffer, &i_buffer );
2364 /* allocate output */
2365 *pi_data = i_buffer + 1000;
2366 dst = *pp_data = malloc( *pi_data );
2368 /* we parse executing all <vlc /> macros */
2369 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2372 *pi_data = dst - *pp_data;
2374 SSClean( &p_args->stack );
2375 mvar_Delete( p_args->vars );
2384 /****************************************************************************
2386 ****************************************************************************/
2387 static char *uri_extract_value( char *psz_uri, char *psz_name,
2388 char *psz_value, int i_value_max )
2392 while( (p = strstr( p, psz_name )) )
2394 /* Verify that we are dealing with a post/get argument */
2395 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2404 p += strlen( psz_name );
2405 if( *p == '=' ) p++;
2407 if( strchr( p, '&' ) )
2409 i_len = strchr( p, '&' ) - p;
2413 /* for POST method */
2414 if( strchr( p, '\n' ) )
2416 i_len = strchr( p, '\n' ) - p;
2417 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2421 i_len = strlen( p );
2424 i_len = __MIN( i_value_max - 1, i_len );
2427 strncpy( psz_value, p, i_len );
2428 psz_value[i_len] = '\0';
2432 strncpy( psz_value, "", i_value_max );
2438 strncpy( psz_value, "", i_value_max );
2444 static void uri_decode_url_encoded( char *psz )
2446 char *dup = strdup( psz );
2464 *psz++ = strtol( val, NULL, 16 );
2466 else if( *p == '+' )
2480 /****************************************************************************
2481 * Light RPN evaluator
2482 ****************************************************************************/
2483 static void SSInit( rpn_stack_t *st )
2488 static void SSClean( rpn_stack_t *st )
2490 while( st->i_stack > 0 )
2492 free( st->stack[--st->i_stack] );
2496 static void SSPush( rpn_stack_t *st, char *s )
2498 if( st->i_stack < STACK_MAX )
2500 st->stack[st->i_stack++] = strdup( s );
2504 static char * SSPop( rpn_stack_t *st )
2506 if( st->i_stack <= 0 )
2508 return strdup( "" );
2512 return st->stack[--st->i_stack];
2516 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2525 i = strtol( name, &end, 0 );
2528 value = mvar_GetValue( vars, name );
2536 static void SSPushN( rpn_stack_t *st, int i )
2540 sprintf( v, "%d", i );
2544 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2551 while( *exp == ' ' )
2558 /* extract string */
2561 while( *exp && *exp != '\'' )
2572 p = strchr( exp, ' ' );
2577 exp += strlen( exp );
2582 strncpy( s, exp, i );
2593 /* 1. Integer function */
2594 if( !strcmp( s, "!" ) )
2596 SSPushN( st, ~SSPopN( st, vars ) );
2598 else if( !strcmp( s, "^" ) )
2600 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2602 else if( !strcmp( s, "&" ) )
2604 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2606 else if( !strcmp( s, "|" ) )
2608 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2610 else if( !strcmp( s, "+" ) )
2612 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2614 else if( !strcmp( s, "-" ) )
2616 int j = SSPopN( st, vars );
2617 int i = SSPopN( st, vars );
2618 SSPushN( st, i - j );
2620 else if( !strcmp( s, "*" ) )
2622 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2624 else if( !strcmp( s, "/" ) )
2628 j = SSPopN( st, vars );
2629 i = SSPopN( st, vars );
2631 SSPushN( st, j != 0 ? i / j : 0 );
2633 else if( !strcmp( s, "%" ) )
2637 j = SSPopN( st, vars );
2638 i = SSPopN( st, vars );
2640 SSPushN( st, j != 0 ? i % j : 0 );
2642 /* 2. integer tests */
2643 else if( !strcmp( s, "=" ) )
2645 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2647 else if( !strcmp( s, "<" ) )
2649 int j = SSPopN( st, vars );
2650 int i = SSPopN( st, vars );
2652 SSPushN( st, i < j ? -1 : 0 );
2654 else if( !strcmp( s, ">" ) )
2656 int j = SSPopN( st, vars );
2657 int i = SSPopN( st, vars );
2659 SSPushN( st, i > j ? -1 : 0 );
2661 else if( !strcmp( s, "<=" ) )
2663 int j = SSPopN( st, vars );
2664 int i = SSPopN( st, vars );
2666 SSPushN( st, i <= j ? -1 : 0 );
2668 else if( !strcmp( s, ">=" ) )
2670 int j = SSPopN( st, vars );
2671 int i = SSPopN( st, vars );
2673 SSPushN( st, i >= j ? -1 : 0 );
2675 /* 3. string functions */
2676 else if( !strcmp( s, "strcat" ) )
2678 char *s2 = SSPop( st );
2679 char *s1 = SSPop( st );
2680 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2690 else if( !strcmp( s, "strcmp" ) )
2692 char *s2 = SSPop( st );
2693 char *s1 = SSPop( st );
2695 SSPushN( st, strcmp( s1, s2 ) );
2699 else if( !strcmp( s, "strncmp" ) )
2701 int n = SSPopN( st, vars );
2702 char *s2 = SSPop( st );
2703 char *s1 = SSPop( st );
2705 SSPushN( st, strncmp( s1, s2 , n ) );
2709 else if( !strcmp( s, "strlen" ) )
2711 char *str = SSPop( st );
2713 SSPushN( st, strlen( str ) );
2716 /* 4. stack functions */
2717 else if( !strcmp( s, "dup" ) )
2719 char *str = SSPop( st );
2724 else if( !strcmp( s, "drop" ) )
2726 char *str = SSPop( st );
2729 else if( !strcmp( s, "swap" ) )
2731 char *s1 = SSPop( st );
2732 char *s2 = SSPop( st );
2739 else if( !strcmp( s, "flush" ) )
2744 else if( !strcmp( s, "store" ) )
2746 char *value = SSPop( st );
2747 char *name = SSPop( st );
2749 mvar_PushNewVar( vars, name, value );
2753 else if( !strcmp( s, "value" ) )
2755 char *name = SSPop( st );
2756 char *value = mvar_GetValue( vars, name );
2758 SSPush( st, value );
2762 else if( !strcmp( s, "url_extract" ) )
2764 char *url = mvar_GetValue( vars, "url_value" );
2765 char *name = SSPop( st );
2768 uri_extract_value( url, name, value, 512 );
2769 uri_decode_url_encoded( value );
2770 SSPush( st, value );
2779 /**********************************************************************
2780 * Find_end_MRL: Find the end of the sentence :
2781 * this function parses the string psz and find the end of the item
2782 * and/or option with detecting the " and ' problems.
2783 * returns NULL if an error is detected, otherwise, returns a pointer
2784 * of the end of the sentence (after the last character)
2785 **********************************************************************/
2786 static char *Find_end_MRL( char *psz )
2796 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
2798 if( *s_sent == '\'' )
2800 s_sent = Find_end_MRL( s_sent );
2802 if( s_sent == NULL )
2812 if( *s_sent == '\"' )
2816 } else /* *s_sent == '\0' , which means the number of " is incorrect */
2826 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
2828 if( *s_sent == '\"' )
2830 s_sent = Find_end_MRL( s_sent );
2832 if( s_sent == NULL )
2842 if( *s_sent == '\'' )
2846 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
2852 default: /* now we can look for spaces */
2854 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
2856 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
2858 s_sent = Find_end_MRL( s_sent );
2869 /**********************************************************************
2870 * parse_MRL: parse the MRL, find the mrl string and the options,
2871 * create an item with all informations in it, and return the item.
2872 * return NULL if there is an error.
2873 **********************************************************************/
2874 playlist_item_t * parse_MRL( char *psz )
2876 char **ppsz_options = NULL;
2883 playlist_item_t * p_item = NULL;
2885 /* In case there is spaces before the mrl */
2886 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
2891 /* extract the mrl */
2892 s_temp = strstr( s_mrl , " :" );
2893 if( s_temp == NULL )
2895 s_temp = s_mrl + strlen( s_mrl );
2898 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
2905 /* if the mrl is between " or ', we must remove them */
2906 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
2908 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
2909 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
2910 mrl[ s_temp - s_mrl - 2 ] = '\0';
2913 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
2914 strncpy( mrl , s_mrl , s_temp - s_mrl );
2915 mrl[ s_temp - s_mrl ] = '\0';
2920 /* now we can take care of the options */
2921 while( (*s_mrl != '\0') && (i_error == 0) )
2930 case ':': /* an option */
2932 s_temp = Find_end_MRL( s_mrl );
2934 if( s_temp == NULL )
2941 ppsz_options = realloc( ppsz_options , i_options *
2943 ppsz_options[ i_options - 1 ] =
2944 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
2946 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
2949 /* don't forget to finish the string with a '\0' */
2950 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
2970 /* now create an item */
2971 p_item = malloc( sizeof( playlist_item_t ) );
2972 memset( p_item, 0, sizeof( playlist_item_t ) );
2973 p_item->psz_name = mrl;
2974 p_item->psz_uri = strdup( mrl );
2975 p_item->i_duration = -1;
2976 p_item->b_enabled = VLC_TRUE;
2977 p_item->i_group = PLAYLIST_TYPE_MANUAL;
2979 for( i = 0 ; i< i_options ; i++ )
2981 playlist_AddItemOption( p_item, ppsz_options[i] );
2985 for( i = 0 ; i < i_options ; i++ )
2987 free( ppsz_options[i] );
2989 free( ppsz_options );