1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
5 * $Id: http.c,v 1.45 2004/01/17 13:25:21 gbazin 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" );
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 );
1031 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1033 mvar_t *s = mvar_New( name, "set" );
1034 char tmp[MAX_DIR_SIZE], *p, *src;
1035 #ifdef HAVE_SYS_STAT_H
1036 struct stat stat_info;
1039 struct dirent *p_dir_content;
1042 /* convert all / to native separator */
1043 #if defined( WIN32 )
1044 while( (p = strchr( psz_dir, '/' )) )
1053 /* remove trailling separator */
1054 while( strlen( psz_dir ) > 1 &&
1055 #if defined( WIN32 )
1056 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1058 psz_dir[strlen( psz_dir ) -1 ] == sep )
1060 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1062 /* remove double separator */
1063 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1065 if( src[0] == sep && src[1] == sep )
1073 if( *psz_dir == '\0' )
1077 /* first fix all .. dir */
1081 if( src[0] == '.' && src[1] == '.' )
1084 if( p <= &psz_dir[1] )
1091 while( p > &psz_dir[1] && *p != sep )
1096 else if( *src == sep )
1098 if( p > psz_dir && p[-1] == sep )
1112 } while( *src && *src != sep );
1117 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1119 #ifdef HAVE_SYS_STAT_H
1120 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1126 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1128 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1132 /* remove traling / or \ */
1133 for( p = &psz_dir[strlen( psz_dir) - 1]; p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1142 /* parse psz_src dir */
1143 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1147 if( !strcmp( p_dir_content->d_name, "." ) )
1152 #if defined( WIN32 )
1153 sprintf( tmp, "%s\\%s", psz_dir, p_dir_content->d_name );
1155 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1158 #ifdef HAVE_SYS_STAT_H
1159 if( stat( tmp, &stat_info ) == -1 )
1164 f = mvar_New( name, "set" );
1165 mvar_AppendNewVar( f, "name", tmp );
1166 #ifdef HAVE_SYS_STAT_H
1167 if( S_ISDIR( stat_info.st_mode ) )
1169 mvar_AppendNewVar( f, "type", "directory" );
1171 else if( S_ISREG( stat_info.st_mode ) )
1173 mvar_AppendNewVar( f, "type", "file" );
1177 mvar_AppendNewVar( f, "type", "unknown" );
1180 sprintf( tmp, "%lld", stat_info.st_size );
1181 mvar_AppendNewVar( f, "size", tmp );
1183 /* FIXME memory leak FIXME */
1185 ctime_r( &stat_info.st_mtime, tmp );
1186 mvar_AppendNewVar( f, "date", tmp );
1188 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1192 mvar_AppendNewVar( f, "type", "unknown" );
1193 mvar_AppendNewVar( f, "size", "unknown" );
1194 mvar_AppendNewVar( f, "date", "unknown" );
1196 mvar_AppendVar( s, f );
1202 static void SSInit( rpn_stack_t * );
1203 static void SSClean( rpn_stack_t * );
1204 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1206 static void SSPush ( rpn_stack_t *, char * );
1207 static char *SSPop ( rpn_stack_t * );
1209 static void SSPushN ( rpn_stack_t *, int );
1210 static int SSPopN ( rpn_stack_t *, mvar_t * );
1213 /****************************************************************************
1215 ****************************************************************************/
1223 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1227 /* just load the file */
1229 *pp_data = malloc( 1025 ); /* +1 for \0 */
1230 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1233 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1239 (*pp_data)[*pi_data] = '\0';
1244 static int MacroParse( macro_t *m, uint8_t *psz_src )
1246 uint8_t *dup = strdup( psz_src );
1251 #define EXTRACT( name, l ) \
1253 p = strchr( src, '"' ); \
1258 m->name = strdup( src ); \
1279 if( !strncmp( src, "id=\"", 4 ) )
1283 else if( !strncmp( src, "param1=\"", 8 ) )
1285 EXTRACT( param1, 8 );
1287 else if( !strncmp( src, "param2=\"", 8 ) )
1289 EXTRACT( param2, 8 );
1296 if( strstr( src, "/>" ) )
1298 src = strstr( src, "/>" ) + 2;
1302 src += strlen( src );
1307 m->id = strdup( "" );
1309 if( m->param1 == NULL )
1311 m->param1 = strdup( "" );
1313 if( m->param2 == NULL )
1315 m->param2 = strdup( "" );
1324 static void MacroClean( macro_t *m )
1370 StrToMacroTypeTab [] =
1372 { "control", MVLC_CONTROL },
1373 /* player control */
1374 { "play", MVLC_PLAY },
1375 { "stop", MVLC_STOP },
1376 { "pause", MVLC_PAUSE },
1377 { "next", MVLC_NEXT },
1378 { "previous", MVLC_PREVIOUS },
1379 { "seek", MVLC_SEEK },
1380 { "keep", MVLC_KEEP },
1381 { "fullscreen", MVLC_FULLSCREEN },
1382 { "volume", MVLC_VOLUME },
1384 /* playlist management */
1385 { "add", MVLC_ADD },
1386 { "delete", MVLC_DEL },
1387 { "empty", MVLC_EMPTY },
1388 { "sort", MVLC_SORT },
1391 { "close", MVLC_CLOSE },
1392 { "shutdown", MVLC_SHUTDOWN },
1394 { "rpn", MVLC_RPN },
1396 { "foreach", MVLC_FOREACH },
1397 { "value", MVLC_VALUE },
1400 { "else", MVLC_ELSE },
1401 { "end", MVLC_END },
1402 { "get", MVLC_GET },
1403 { "set", MVLC_SET },
1404 { "int", MVLC_INT },
1405 { "float", MVLC_FLOAT },
1406 { "string", MVLC_STRING },
1409 { NULL, MVLC_UNKNOWN }
1412 static int StrToMacroType( char *name )
1416 if( !name || *name == '\0')
1418 return MVLC_UNKNOWN;
1420 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1422 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1424 return StrToMacroTypeTab[i].i_type;
1427 return MVLC_UNKNOWN;
1430 static void MacroDo( httpd_file_callback_args_t *p_args,
1432 uint8_t *p_request, int i_request,
1433 uint8_t **pp_data, int *pi_data,
1436 intf_thread_t *p_intf = p_args->p_intf;
1437 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1440 #define ALLOC( l ) \
1442 int __i__ = *pp_dst - *pp_data; \
1444 *pp_data = realloc( *pp_data, *pi_data ); \
1445 *pp_dst = (*pp_data) + __i__; \
1447 #define PRINT( str ) \
1448 ALLOC( strlen( str ) + 1 ); \
1449 *pp_dst += sprintf( *pp_dst, str );
1451 #define PRINTS( str, s ) \
1452 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1454 char * psz_cur = *pp_dst; \
1455 *pp_dst += sprintf( *pp_dst, str, s ); \
1456 while( psz_cur && *psz_cur ) \
1458 /* Prevent script injection */ \
1459 if( *psz_cur == '<' ) *psz_cur = '*'; \
1460 if( *psz_cur == '>' ) *psz_cur = '*'; \
1465 switch( StrToMacroType( m->id ) )
1468 if( i_request <= 0 )
1472 uri_extract_value( p_request, "control", control, 512 );
1473 if( *m->param1 && !strstr( m->param1, control ) )
1475 msg_Warn( p_intf, "unauthorized control=%s", control );
1478 switch( StrToMacroType( control ) )
1485 uri_extract_value( p_request, "item", item, 512 );
1486 i_item = atoi( item );
1487 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
1488 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1492 playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1493 msg_Dbg( p_intf, "requested playlist stop" );
1496 playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1497 msg_Dbg( p_intf, "requested playlist pause" );
1500 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1501 p_sys->p_playlist->i_index + 1 );
1502 msg_Dbg( p_intf, "requested playlist next" );
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" );
1509 case MVLC_FULLSCREEN:
1511 if( p_sys->p_input )
1513 vout_thread_t *p_vout;
1514 p_vout = vlc_object_find( p_sys->p_input,
1515 VLC_OBJECT_VOUT, FIND_CHILD );
1519 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1520 vlc_object_release( p_vout );
1521 msg_Dbg( p_intf, "requested fullscreen toggle" );
1535 #define POSITION_ABSOLUTE 12
1536 #define POSITION_REL_FOR 13
1537 #define POSITION_REL_BACK 11
1538 #define TIME_ABSOLUTE 0
1539 #define TIME_REL_FOR 1
1540 #define TIME_REL_BACK -1
1541 if( p_sys->p_input )
1543 uri_extract_value( p_request, "seek_value", value, 20 );
1544 uri_decode_url_encoded( value );
1546 var_Get( p_sys->p_input, "length", &val);
1547 i_length = val.i_time;
1549 while( p_value[0] != '\0' )
1555 i_relative = TIME_REL_FOR;
1561 i_relative = TIME_REL_BACK;
1565 case '0': case '1': case '2': case '3': case '4':
1566 case '5': case '6': case '7': case '8': case '9':
1568 i_stock = strtol( p_value , &p_value , 10 );
1571 case '%': /* for percentage ie position */
1573 i_relative += POSITION_ABSOLUTE;
1581 i_value = 60 * (i_value + i_stock) ;
1586 case 'h': case 'H': /* hours */
1588 i_value += 3600 * i_stock;
1590 /* other characters which are not numbers are not important */
1591 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1597 case 'm': case 'M': case '\'': /* minutes */
1599 i_value += 60 * i_stock;
1602 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1608 case 's': case 'S': case '"': /* seconds */
1612 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1626 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1633 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1634 val.i_time = (uint64_t)( i_value ) * 1000000;
1636 val.i_time = i_length;
1638 var_Set( p_sys->p_input, "time", val );
1639 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1644 var_Get( p_sys->p_input, "time", &val );
1645 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1647 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1650 val.i_time = i_length;
1652 var_Set( p_sys->p_input, "time", val );
1653 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1658 var_Get( p_sys->p_input, "time", &val );
1659 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1664 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1666 var_Set( p_sys->p_input, "time", val );
1667 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1670 case POSITION_ABSOLUTE:
1672 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1673 var_Set( p_sys->p_input, "position", val );
1674 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1677 case POSITION_REL_FOR:
1679 var_Get( p_sys->p_input, "position", &val );
1680 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1681 var_Set( p_sys->p_input, "position", val );
1682 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1685 case POSITION_REL_BACK:
1687 var_Get( p_sys->p_input, "position", &val );
1688 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1689 var_Set( p_sys->p_input, "position", val );
1690 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
1695 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1700 #undef POSITION_ABSOLUTE
1701 #undef POSITION_REL_FOR
1702 #undef POSITION_REL_BACK
1703 #undef TIME_ABSOLUTE
1705 #undef TIME_REL_BACK
1711 audio_volume_t i_volume;
1714 uri_extract_value( p_request, "value", vol, 8 );
1715 aout_VolumeGet( p_intf, &i_volume );
1716 uri_decode_url_encoded( vol );
1720 i_value = atoi( vol + 1 );
1721 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1723 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1724 msg_Dbg( p_intf, "requested volume set: max" );
1727 aout_VolumeSet( p_intf , (i_volume + i_value) );
1728 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1733 i_value = atoi( vol + 1 );
1734 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1736 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1737 msg_Dbg( p_intf, "requested volume set: min" );
1740 aout_VolumeSet( p_intf , (i_volume - i_value) );
1741 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1745 i_value = atoi( vol );
1746 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1748 aout_VolumeSet( p_intf , atoi( vol ) );
1749 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1755 /* playlist management */
1759 playlist_item_t * p_item;
1761 uri_extract_value( p_request, "mrl", mrl, 512 );
1762 uri_decode_url_encoded( mrl );
1763 p_item = parse_MRL( mrl );
1765 if( !p_item || !p_item->psz_uri || !*p_item->psz_uri )
1767 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1770 playlist_AddItem( p_sys->p_playlist , p_item ,
1771 PLAYLIST_APPEND, PLAYLIST_END );
1772 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1779 int i_item, *p_items = NULL, i_nb_items = 0;
1780 char item[512], *p_parser = p_request;
1782 /* Get the list of items to delete */
1784 uri_extract_value( p_parser, "item", item, 512 )) )
1786 if( !*item ) continue;
1788 i_item = atoi( item );
1789 p_items = realloc( p_items, (i_nb_items + 1) *
1791 p_items[i_nb_items] = i_item;
1795 /* The items need to be deleted from in reversed order */
1799 for( i = 0; i < i_nb_items; i++ )
1802 for( j = 0; j < i_nb_items; j++ )
1804 if( p_items[j] > p_items[i_index] )
1808 playlist_Delete( p_sys->p_playlist,
1810 msg_Dbg( p_intf, "requested playlist delete: %d",
1812 p_items[i_index] = -1;
1816 if( p_items ) free( p_items );
1821 int i_item, *p_items = NULL, i_nb_items = 0;
1822 char item[512], *p_parser = p_request;
1825 /* Get the list of items to keep */
1827 uri_extract_value( p_parser, "item", item, 512 )) )
1829 if( !*item ) continue;
1831 i_item = atoi( item );
1832 p_items = realloc( p_items, (i_nb_items + 1) *
1834 p_items[i_nb_items] = i_item;
1838 /* The items need to be deleted from in reversed order */
1839 for( i = p_sys->p_playlist->i_size - 1; i >= 0 ; i-- )
1841 /* Check if the item is in the keep list */
1842 for( j = 0 ; j < i_nb_items ; j++ )
1844 if( p_items[j] == i ) break;
1846 if( j == i_nb_items )
1848 playlist_Delete( p_sys->p_playlist, i );
1849 msg_Dbg( p_intf, "requested playlist delete: %d",
1854 if( p_items ) free( p_items );
1859 while( p_sys->p_playlist->i_size > 0 )
1861 playlist_Delete( p_sys->p_playlist, 0 );
1863 msg_Dbg( p_intf, "requested playlist empty" );
1872 uri_extract_value( p_request, "type", type, 12 );
1873 uri_extract_value( p_request, "order", order, 2 );
1875 if( order[0] == '0' ) i_order = ORDER_NORMAL;
1876 else i_order = ORDER_REVERSE;
1878 if( !strcmp( type , "title" ) )
1880 playlist_SortTitle( p_sys->p_playlist , i_order );
1881 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
1882 } else if( !strcmp( type , "group" ) )
1884 playlist_SortGroup( p_sys->p_playlist , i_order );
1885 msg_Dbg( p_intf, "requested playlist sort by group (%d)" , i_order );
1886 } else if( !strcmp( type , "author" ) )
1888 playlist_SortAuthor( p_sys->p_playlist , i_order );
1889 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
1895 /* admin function */
1899 uri_extract_value( p_request, "id", id, 512 );
1900 msg_Dbg( p_intf, "requested close id=%s", id );
1901 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
1903 msg_Warn( p_intf, "close failed for id=%s", id );
1909 msg_Dbg( p_intf, "requested shutdown" );
1910 p_intf->p_vlc->b_die = VLC_TRUE;
1914 PRINTS( "<!-- control param(%s) unsuported -->", control );
1925 if( i_request <= 0 ||
1926 *m->param1 == '\0' ||
1927 strstr( p_request, m->param1 ) == NULL )
1931 uri_extract_value( p_request, m->param1, value, 512 );
1932 uri_decode_url_encoded( value );
1934 switch( StrToMacroType( m->param2 ) )
1938 config_PutInt( p_intf, m->param1, i );
1942 config_PutFloat( p_intf, m->param1, f );
1945 config_PutPsz( p_intf, m->param1, value );
1948 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
1959 if( *m->param1 == '\0' )
1964 switch( StrToMacroType( m->param2 ) )
1967 i = config_GetInt( p_intf, m->param1 );
1968 sprintf( value, "%i", i );
1971 f = config_GetFloat( p_intf, m->param1 );
1972 sprintf( value, "%f", f );
1975 psz = config_GetPsz( p_intf, m->param1 );
1976 sprintf( value, "%s", psz ? psz : "" );
1977 if( psz ) free( psz );
1980 sprintf( value, "invalid type(%s) in set", m->param2 );
1983 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
1984 PRINTS( "%s", value );
1993 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1994 s = SSPop( &p_args->stack );
1995 v = mvar_GetValue( p_args->vars, s );
1999 v = s = SSPop( &p_args->stack );
2007 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2011 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2012 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2020 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
2027 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
2032 i_skip = MacroParse( &m, src );
2034 i_id = StrToMacroType( m.id );
2051 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2052 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2054 return src + ( b_after ? i_skip : 0 );
2056 else if( i_level < 0 )
2072 static void Execute( httpd_file_callback_args_t *p_args,
2073 uint8_t *p_request, int i_request,
2074 uint8_t **pp_data, int *pi_data,
2076 uint8_t *_src, uint8_t *_end )
2078 intf_thread_t *p_intf = p_args->p_intf;
2080 uint8_t *src, *dup, *end;
2081 uint8_t *dst = *pp_dst;
2083 src = dup = malloc( _end - _src + 1 );
2084 end = src +( _end - _src );
2086 memcpy( src, _src, _end - _src );
2089 /* we parse searching <vlc */
2095 p = strstr( src, "<vlc" );
2096 if( p < end && p == src )
2100 src += MacroParse( &m, src );
2102 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2104 switch( StrToMacroType( m.id ) )
2111 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2112 if( SSPopN( &p_args->stack, p_args->vars ) )
2120 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2124 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2128 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2131 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2135 else if( i_test == 1 )
2138 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2140 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2144 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2153 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2154 uint8_t *start = src;
2155 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2162 if( !strcmp( m.param2, "integer" ) )
2164 char *arg = SSPop( &p_args->stack );
2165 index = mvar_IntegerSetNew( m.param1, arg );
2168 else if( !strcmp( m.param2, "directory" ) )
2170 char *arg = SSPop( &p_args->stack );
2171 index = mvar_FileSetNew( m.param1, arg );
2174 else if( !strcmp( m.param2, "playlist" ) )
2176 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2178 else if( !strcmp( m.param2, "informations" ) )
2180 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2182 else if( !strcmp( m.param2, "hosts" ) )
2184 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2186 else if( !strcmp( m.param2, "urls" ) )
2188 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2190 else if( !strcmp( m.param2, "connections" ) )
2192 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2194 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2196 index = mvar_Duplicate( v );
2200 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2205 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2207 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2209 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2212 f->name = strdup( m.param1 );
2215 mvar_PushVar( p_args->vars, f );
2216 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2217 mvar_RemoveVar( p_args->vars, f );
2221 mvar_Delete( index );
2228 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2236 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2239 int i_index = dst - *pp_data;
2242 *pp_data = realloc( *pp_data, *pi_data );
2243 dst = (*pp_data) + i_index;
2245 memcpy( dst, src, i_copy );
2255 /****************************************************************************
2257 ****************************************************************************
2258 * a file with mime == text/html is parsed and all "macro" replaced
2259 * <vlc id="macro name" [param1="" [param2=""]] />
2262 ****************************************************************************/
2263 static int http_get( httpd_file_callback_args_t *p_args,
2264 uint8_t *p_request, int i_request,
2265 uint8_t **pp_data, int *pi_data )
2270 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2272 p = *pp_data = malloc( 10240 );
2275 return VLC_EGENERIC;
2277 p += sprintf( p, "<html>\n" );
2278 p += sprintf( p, "<head>\n" );
2279 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2280 p += sprintf( p, "</head>\n" );
2281 p += sprintf( p, "<body>\n" );
2282 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2283 p += sprintf( p, "<hr />\n" );
2284 p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
2285 p += sprintf( p, "</body>\n" );
2286 p += sprintf( p, "</html>\n" );
2288 *pi_data = strlen( *pp_data ) + 1;
2293 if( strcmp( p_args->mime, "text/html" ) )
2295 FileLoad( f, pp_data, pi_data );
2303 char position[4]; /* percentage */
2304 char time[12]; /* in seconds */
2305 char length[12]; /* in seconds */
2306 audio_volume_t i_volume;
2310 #define p_sys p_args->p_intf->p_sys
2311 if( p_sys->p_input )
2313 var_Get( p_sys->p_input, "position", &val);
2314 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2315 var_Get( p_sys->p_input, "time", &val);
2316 sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2317 var_Get( p_sys->p_input, "length", &val);
2318 sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2320 var_Get( p_sys->p_input, "state", &val );
2321 if( val.i_int == PLAYING_S )
2323 sprintf( state, "playing" );
2324 } else if( val.i_int == PAUSE_S )
2326 sprintf( state, "paused" );
2329 sprintf( state, "stop" );
2333 sprintf( position, "%d", 0 );
2334 sprintf( time, "%d", 0 );
2335 sprintf( length, "%d", 0 );
2336 sprintf( state, "stop" );
2340 aout_VolumeGet( p_args->p_intf , &i_volume );
2341 sprintf( volume , "%d" , (int)i_volume );
2343 p_args->vars = mvar_New( "variables", "" );
2344 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2345 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2346 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
2347 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2348 mvar_AppendNewVar( p_args->vars, "stream_position", position );
2349 mvar_AppendNewVar( p_args->vars, "stream_time", time );
2350 mvar_AppendNewVar( p_args->vars, "stream_length", length );
2351 mvar_AppendNewVar( p_args->vars, "volume", volume );
2352 mvar_AppendNewVar( p_args->vars, "stream_state", state );
2354 SSInit( &p_args->stack );
2356 /* first we load in a temporary buffer */
2357 FileLoad( f, &p_buffer, &i_buffer );
2359 /* allocate output */
2360 *pi_data = i_buffer + 1000;
2361 dst = *pp_data = malloc( *pi_data );
2363 /* we parse executing all <vlc /> macros */
2364 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2367 *pi_data = dst - *pp_data;
2369 SSClean( &p_args->stack );
2370 mvar_Delete( p_args->vars );
2378 /****************************************************************************
2380 ****************************************************************************/
2381 static char *uri_extract_value( char *psz_uri, char *psz_name,
2382 char *psz_value, int i_value_max )
2386 p = strstr( psz_uri, psz_name );
2391 p += strlen( psz_name );
2392 if( *p == '=' ) p++;
2394 if( strchr( p, '&' ) )
2396 i_len = strchr( p, '&' ) - p;
2400 /* for POST method */
2401 if( strchr( p, '\n' ) )
2403 i_len = strchr( p, '\n' ) - p;
2404 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2408 i_len = strlen( p );
2411 i_len = __MIN( i_value_max - 1, i_len );
2414 strncpy( psz_value, p, i_len );
2415 psz_value[i_len] = '\0';
2419 strncpy( psz_value, "", i_value_max );
2425 strncpy( psz_value, "", i_value_max );
2431 static void uri_decode_url_encoded( char *psz )
2433 char *dup = strdup( psz );
2451 *psz++ = strtol( val, NULL, 16 );
2453 else if( *p == '+' )
2467 /****************************************************************************
2468 * Light RPN evaluator
2469 ****************************************************************************/
2470 static void SSInit( rpn_stack_t *st )
2475 static void SSClean( rpn_stack_t *st )
2477 while( st->i_stack > 0 )
2479 free( st->stack[--st->i_stack] );
2483 static void SSPush( rpn_stack_t *st, char *s )
2485 if( st->i_stack < STACK_MAX )
2487 st->stack[st->i_stack++] = strdup( s );
2491 static char * SSPop( rpn_stack_t *st )
2493 if( st->i_stack <= 0 )
2495 return strdup( "" );
2499 return st->stack[--st->i_stack];
2503 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2512 i = strtol( name, &end, 0 );
2515 value = mvar_GetValue( vars, name );
2523 static void SSPushN( rpn_stack_t *st, int i )
2527 sprintf( v, "%d", i );
2531 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2538 while( *exp == ' ' )
2545 /* extract string */
2548 while( *exp && *exp != '\'' )
2559 p = strchr( exp, ' ' );
2564 exp += strlen( exp );
2569 strncpy( s, exp, i );
2580 /* 1. Integer function */
2581 if( !strcmp( s, "!" ) )
2583 SSPushN( st, ~SSPopN( st, vars ) );
2585 else if( !strcmp( s, "^" ) )
2587 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2589 else if( !strcmp( s, "&" ) )
2591 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2593 else if( !strcmp( s, "|" ) )
2595 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2597 else if( !strcmp( s, "+" ) )
2599 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2601 else if( !strcmp( s, "-" ) )
2603 int j = SSPopN( st, vars );
2604 int i = SSPopN( st, vars );
2605 SSPushN( st, i - j );
2607 else if( !strcmp( s, "*" ) )
2609 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2611 else if( !strcmp( s, "/" ) )
2615 j = SSPopN( st, vars );
2616 i = SSPopN( st, vars );
2618 SSPushN( st, j != 0 ? i / j : 0 );
2620 else if( !strcmp( s, "%" ) )
2624 j = SSPopN( st, vars );
2625 i = SSPopN( st, vars );
2627 SSPushN( st, j != 0 ? i % j : 0 );
2629 /* 2. integer tests */
2630 else if( !strcmp( s, "=" ) )
2632 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2634 else if( !strcmp( s, "<" ) )
2636 int j = SSPopN( st, vars );
2637 int i = SSPopN( st, vars );
2639 SSPushN( st, i < j ? -1 : 0 );
2641 else if( !strcmp( s, ">" ) )
2643 int j = SSPopN( st, vars );
2644 int i = SSPopN( st, vars );
2646 SSPushN( st, i > j ? -1 : 0 );
2648 else if( !strcmp( s, "<=" ) )
2650 int j = SSPopN( st, vars );
2651 int i = SSPopN( st, vars );
2653 SSPushN( st, i <= j ? -1 : 0 );
2655 else if( !strcmp( s, ">=" ) )
2657 int j = SSPopN( st, vars );
2658 int i = SSPopN( st, vars );
2660 SSPushN( st, i >= j ? -1 : 0 );
2662 /* 3. string functions */
2663 else if( !strcmp( s, "strcat" ) )
2665 char *s2 = SSPop( st );
2666 char *s1 = SSPop( st );
2667 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2677 else if( !strcmp( s, "strcmp" ) )
2679 char *s2 = SSPop( st );
2680 char *s1 = SSPop( st );
2682 SSPushN( st, strcmp( s1, s2 ) );
2686 else if( !strcmp( s, "strncmp" ) )
2688 int n = SSPopN( st, vars );
2689 char *s2 = SSPop( st );
2690 char *s1 = SSPop( st );
2692 SSPushN( st, strncmp( s1, s2 , n ) );
2696 else if( !strcmp( s, "strlen" ) )
2698 char *str = SSPop( st );
2700 SSPushN( st, strlen( str ) );
2703 /* 4. stack functions */
2704 else if( !strcmp( s, "dup" ) )
2706 char *str = SSPop( st );
2711 else if( !strcmp( s, "drop" ) )
2713 char *str = SSPop( st );
2716 else if( !strcmp( s, "swap" ) )
2718 char *s1 = SSPop( st );
2719 char *s2 = SSPop( st );
2726 else if( !strcmp( s, "flush" ) )
2731 else if( !strcmp( s, "store" ) )
2733 char *value = SSPop( st );
2734 char *name = SSPop( st );
2736 mvar_PushNewVar( vars, name, value );
2740 else if( !strcmp( s, "value" ) )
2742 char *name = SSPop( st );
2743 char *value = mvar_GetValue( vars, name );
2745 SSPush( st, value );
2749 else if( !strcmp( s, "url_extract" ) )
2751 char *url = mvar_GetValue( vars, "url_value" );
2752 char *name = SSPop( st );
2755 uri_extract_value( url, name, value, 512 );
2756 uri_decode_url_encoded( value );
2757 SSPush( st, value );
2766 /**********************************************************************
2767 * Find_end_MRL: Find the end of the sentence :
2768 * this function parses the string psz and find the end of the item
2769 * and/or option with detecting the " and ' problems.
2770 * returns NULL if an error is detected, otherwise, returns a pointer
2771 * of the end of the sentence (after the last character)
2772 **********************************************************************/
2773 static char *Find_end_MRL( char *psz )
2783 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
2785 if( *s_sent == '\'' )
2787 s_sent = Find_end_MRL( s_sent );
2789 if( s_sent == NULL )
2799 if( *s_sent == '\"' )
2803 } else /* *s_sent == '\0' , which means the number of " is incorrect */
2813 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
2815 if( *s_sent == '\"' )
2817 s_sent = Find_end_MRL( s_sent );
2819 if( s_sent == NULL )
2829 if( *s_sent == '\'' )
2833 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
2839 default: /* now we can look for spaces */
2841 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
2843 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
2845 s_sent = Find_end_MRL( s_sent );
2856 /**********************************************************************
2857 * parse_MRL: parse the MRL, find the mrl string and the options,
2858 * create an item with all informations in it, and return the item.
2859 * return NULL if there is an error.
2860 **********************************************************************/
2861 playlist_item_t * parse_MRL( char *psz )
2863 char **ppsz_options = NULL;
2870 playlist_item_t * p_item;
2872 /* In case there is spaces before the mrl */
2873 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
2878 /* extract the mrl */
2879 s_temp = strstr( s_mrl , " :" );
2880 if( s_temp == NULL )
2882 s_temp = s_mrl + strlen( s_mrl );
2885 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
2892 /* if the mrl is between " or ', we must remove them */
2893 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
2895 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
2896 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
2897 mrl[ s_temp - s_mrl - 2 ] = '\0';
2900 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
2901 strncpy( mrl , s_mrl , s_temp - s_mrl );
2902 mrl[ s_temp - s_mrl ] = '\0';
2907 /* now we can take care of the options */
2908 while( (*s_mrl != '\0') && (i_error == 0) )
2917 case ':': /* an option */
2919 s_temp = Find_end_MRL( s_mrl );
2921 if( s_temp == NULL )
2928 ppsz_options = realloc( ppsz_options , i_options *
2930 ppsz_options[ i_options - 1 ] =
2931 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
2933 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
2936 /* don't forget to finish the string with a '\0' */
2937 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
2957 /* now create an item */
2958 p_item = malloc( sizeof( playlist_item_t ) );
2959 memset( p_item, 0, sizeof( playlist_item_t ) );
2960 p_item->psz_name = mrl;
2961 p_item->psz_uri = strdup( mrl );
2962 p_item->i_duration = -1;
2963 p_item->b_enabled = VLC_TRUE;
2964 p_item->i_group = PLAYLIST_TYPE_MANUAL;
2966 for( i = 0 ; i< i_options ; i++ )
2968 playlist_AddItemOption( p_item, ppsz_options[i] );
2972 for( i = 0 ; i < i_options ; i++ )
2974 free( ppsz_options[i] );
2976 free( ppsz_options );
2978 if( i_error != 0 ) return NULL;