1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: http.c,v 1.32 2003/11/12 02:43:33 garf Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
41 #include <vlc/vout.h> /* for fullscreen */
45 #ifdef HAVE_SYS_STAT_H
46 # include <sys/stat.h>
57 #elif defined( WIN32 ) && !defined( UNDER_CE )
61 #if (!defined( WIN32 ) || defined(__MINGW32__))
62 /* Mingw has its own version of dirent */
66 /*****************************************************************************
68 *****************************************************************************/
69 static int Activate ( vlc_object_t * );
70 static void Close ( vlc_object_t * );
72 #define HOST_TEXT N_( "Host address" )
73 #define HOST_LONGTEXT N_( \
74 "You can set the address and port on which the http interface will bind" )
75 #define SRC_TEXT N_( "Source directory" )
76 #define SRC_LONGTEXT N_( "Source directory" )
79 set_description( _("HTTP remote control interface") );
80 add_category_hint( N_("HTTP remote control"), NULL, VLC_TRUE );
81 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
82 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
83 set_capability( "interface", 0 );
84 set_callbacks( Activate, Close );
88 /*****************************************************************************
90 *****************************************************************************/
91 static void Run ( intf_thread_t *p_intf );
93 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
96 static int DirectoryCheck( char *psz_dir )
100 #ifdef HAVE_SYS_STAT_H
101 struct stat stat_info;
103 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
109 if( ( p_dir = opendir( psz_dir ) ) == NULL )
119 static int http_get( httpd_file_callback_args_t *p_args,
120 uint8_t *p_request, int i_request,
121 uint8_t **pp_data, int *pi_data );
123 static char *uri_extract_value( char *psz_uri, char *psz_name,
124 char *psz_value, int i_value_max );
125 static void uri_decode_url_encoded( char *psz );
127 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;
304 free( p_sys->pp_files );
305 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
306 p_sys->p_httpd_host );
307 httpd_Release( p_sys->p_httpd );
312 /*****************************************************************************
313 * CloseIntf: destroy interface
314 *****************************************************************************/
315 void Close ( vlc_object_t *p_this )
317 intf_thread_t *p_intf = (intf_thread_t *)p_this;
318 intf_sys_t *p_sys = p_intf->p_sys;
322 for( i = 0; i < p_sys->i_files; i++ )
324 p_sys->p_httpd->pf_unregister_file( p_sys->p_httpd,
325 p_sys->pp_files[i]->p_file );
326 /* do not free mime */
327 free( p_sys->pp_files[i]->file );
328 free( p_sys->pp_files[i]->name );
329 free( p_sys->pp_files[i] );
331 free( p_sys->pp_files );
332 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
333 p_sys->p_httpd_host );
334 httpd_Release( p_sys->p_httpd );
338 /*****************************************************************************
339 * Run: http interface thread
340 *****************************************************************************/
341 static void Run( intf_thread_t *p_intf )
343 intf_sys_t *p_sys = p_intf->p_sys;
345 while( !p_intf->b_die )
347 /* get the playlist */
348 if( p_sys->p_playlist == NULL )
350 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
353 /* Manage the input part */
354 if( p_sys->p_input == NULL )
356 if( p_sys->p_playlist )
359 vlc_object_find( p_sys->p_playlist,
364 else if( p_sys->p_input->b_dead )
366 vlc_object_release( p_sys->p_input );
367 p_sys->p_input = NULL;
372 msleep( INTF_IDLE_SLEEP );
377 vlc_object_release( p_sys->p_input );
378 p_sys->p_input = NULL;
381 if( p_sys->p_playlist )
383 vlc_object_release( p_sys->p_playlist );
384 p_sys->p_playlist = NULL;
389 /*****************************************************************************
391 *****************************************************************************/
392 #define MAX_DIR_SIZE 10240
394 /****************************************************************************
395 * FileToUrl: create a good name for an url from filename
396 ****************************************************************************/
397 static char *FileToUrl( char *name )
401 url = p = malloc( strlen( name ) + 1 );
409 while( *name == '\\' || *name == '/' )
411 while( *name == '/' )
421 /* convert '\\' into '/' */
434 if( ( p = strrchr( url, '/' ) ) != NULL )
436 if( !strncmp( p, "/index.", 7 ) )
444 /****************************************************************************
445 * FileToMime: XXX duplicated with modules/access_out/http.c
446 ****************************************************************************/
453 { ".htm", "text/html" },
454 { ".html", "text/html" },
456 { ".css", "text/css" },
459 { ".avi", "video/avi" },
460 { ".asf", "video/x-ms-asf" },
461 { ".m1a", "audio/mpeg" },
462 { ".m2a", "audio/mpeg" },
463 { ".m1v", "video/mpeg" },
464 { ".m2v", "video/mpeg" },
465 { ".mp2", "audio/mpeg" },
466 { ".mp3", "audio/mpeg" },
467 { ".mpa", "audio/mpeg" },
468 { ".mpg", "video/mpeg" },
469 { ".mpeg", "video/mpeg" },
470 { ".mpe", "video/mpeg" },
471 { ".mov", "video/quicktime" },
472 { ".moov", "video/quicktime" },
473 { ".ogg", "application/ogg" },
474 { ".ogm", "application/ogg" },
475 { ".wav", "audio/wav" },
481 static char *FileToMime( char *psz_name )
485 psz_ext = strrchr( psz_name, '.' );
490 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
492 if( !strcmp( http_mime[i].psz_ext, psz_ext ) )
494 return( http_mime[i].psz_mime );
498 return( "application/octet-stream" );
501 /****************************************************************************
502 * ParseDirectory: parse recursively a directory, adding each file
503 ****************************************************************************/
504 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
507 intf_sys_t *p_sys = p_intf->p_sys;
508 char dir[MAX_DIR_SIZE];
509 #ifdef HAVE_SYS_STAT_H
510 struct stat stat_info;
513 struct dirent *p_dir_content;
517 char *password = NULL;
519 #ifdef HAVE_SYS_STAT_H
520 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
526 if( ( p_dir = opendir( psz_dir ) ) == NULL )
528 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
532 msg_Dbg( p_intf, "dir=%s", psz_dir );
534 sprintf( dir, "%s/.access", psz_dir );
535 if( ( file = fopen( dir, "r" ) ) != NULL )
540 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
542 i_size = fread( line, 1, 1023, file );
546 while( i_size > 0 && ( line[i_size-1] == '\n' ||
547 line[i_size-1] == '\r' ) )
554 p = strchr( line, ':' );
558 user = strdup( line );
559 password = strdup( p );
562 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
563 user, password, i_size );
570 /* parse psz_src dir */
571 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
576 if( p_dir_content->d_name[0] == '.' )
580 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
581 if( ParseDirectory( p_intf, psz_root, dir ) )
583 #define f p_sys->pp_files[p_sys->i_files]
584 f = malloc( sizeof( httpd_file_callback_args_t ) );
587 msg_Err( p_intf, "Out of memory" );
588 return( VLC_ENOMEM );
591 f->file = strdup( dir );
592 f->name = FileToUrl( &dir[strlen( psz_root )] );
593 f->mime = FileToMime( &dir[strlen( psz_root )] );
595 if( !f->name || !f->mime )
597 msg_Err( p_intf , "Unable to parse directory" );
598 return( VLC_ENOMEM );
600 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)",
601 f->file, f->name, f->mime );
604 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
612 p_sys->pp_files = realloc( p_sys->pp_files,
613 (p_sys->i_files+1) * sizeof( httpd_file_callback_args_t ) );
615 #define fold p_sys->pp_files[p_sys->i_files-1]
617 /* FIXME for rep/ add rep (it would be better to do a redirection) */
618 if( p_sys->i_files && strlen(fold->name) > 1 &&
619 fold->name[strlen(fold->name) - 1] == '/' )
621 f = malloc( sizeof( httpd_file_callback_args_t ) );
624 msg_Err( p_intf, "Out of memory" );
625 return( VLC_ENOMEM );
628 f->file = strdup( fold->file );
629 f->name = strdup( fold->name );
630 f->mime = fold->mime;
632 f->name[strlen(f->name) - 1] = '\0';
633 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)", f->file, f->name,
636 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
645 realloc( p_sys->pp_files, (p_sys->i_files+1) *
646 sizeof( httpd_file_callback_args_t ) );
665 /****************************************************************************
666 * var and set handling
667 ****************************************************************************/
669 static mvar_t *mvar_New( char *name, char *value )
671 mvar_t *v = malloc( sizeof( mvar_t ) );
673 if( !v ) return NULL;
674 v->name = strdup( name );
675 v->value = strdup( value ? value : "" );
678 v->field = malloc( sizeof( mvar_t * ) );
684 static void mvar_Delete( mvar_t *v )
691 for( i = 0; i < v->i_field; i++ )
693 mvar_Delete( v->field[i] );
699 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
701 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
702 v->field[v->i_field] = f;
706 static mvar_t *mvar_Duplicate( mvar_t *v )
711 n = mvar_New( v->name, v->value );
712 for( i = 0; i < v->i_field; i++ )
714 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
720 static void mvar_PushVar( mvar_t *v, mvar_t *f )
722 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
725 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
731 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
734 for( i = 0; i < v->i_field; i++ )
736 if( v->field[i] == f )
741 if( i >= v->i_field )
746 if( i + 1 < v->i_field )
748 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
751 /* FIXME should do a realloc */
754 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
757 char base[512], *field, *p;
760 /* format: name[index].field */
762 field = strchr( name, '.' );
765 int i = field - name;
766 strncpy( base, name, i );
772 strcpy( base, name );
775 if( ( p = strchr( base, '[' ) ) )
778 sscanf( p, "%d]", &i_index );
789 for( i = 0; i < s->i_field; i++ )
791 if( !strcmp( s->field[i]->name, base ) )
801 return mvar_GetVar( s->field[i], field );
815 static char *mvar_GetValue( mvar_t *v, char *field )
823 mvar_t *f = mvar_GetVar( v, field );
835 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
837 mvar_t *f = mvar_New( name, value );
838 mvar_PushVar( vars, f );
841 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
843 mvar_t *f = mvar_New( name, value );
844 mvar_AppendVar( vars, f );
848 /* arg= start[:stop[:step]],.. */
849 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
851 char *dup = strdup( arg );
853 mvar_t *s = mvar_New( name, "set" );
855 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
860 int i_start,i_stop,i_step;
863 p = strchr( str, ',' );
870 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
871 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
878 else if( i_match == 2 )
880 i_step = i_start < i_stop ? 1 : -1;
887 if( ( i_start < i_stop && i_step > 0 ) ||
888 ( i_start > i_stop && i_step < 0 ) )
890 for( i = i_start; ; i += i_step )
894 if( ( i_step > 0 && i > i_stop ) ||
895 ( i_step < 0 && i < i_stop ) )
900 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
901 sprintf( value, "%d", i );
903 mvar_PushNewVar( s, name, value );
914 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
916 mvar_t *s = mvar_New( name, "set" );
919 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
921 vlc_mutex_lock( &p_pl->object_lock );
922 for( i = 0; i < p_pl->i_size; i++ )
924 mvar_t *itm = mvar_New( name, "set" );
927 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
928 mvar_AppendNewVar( itm, "current", value );
930 sprintf( value, "%d", i );
931 mvar_AppendNewVar( itm, "index", value );
933 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->psz_name );
935 mvar_AppendVar( s, itm );
937 vlc_mutex_unlock( &p_pl->object_lock );
942 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
944 mvar_t *s = mvar_New( name, "set" );
946 input_info_category_t * p_category;
947 input_info_t * p_info;
949 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
950 if( p_input == NULL )
955 vlc_mutex_lock( &p_input->stream.stream_lock );
956 p_category = p_input->stream.p_info;
959 mvar_t *cat = mvar_New( name, "set" );
960 mvar_t *iset = mvar_New( "info", "set" );
962 mvar_AppendNewVar( cat, "name", p_category->psz_name );
963 mvar_AppendVar( cat, iset );
965 p_info = p_category->p_info;
968 mvar_t *info = mvar_New( "info", "" );
970 msg_Dbg( p_input, "adding info name=%s value=%s", p_info->psz_name, p_info->psz_value );
971 mvar_AppendNewVar( info, "name", p_info->psz_name );
972 mvar_AppendNewVar( info, "value", p_info->psz_value );
973 mvar_AppendVar( iset, info );
974 p_info = p_info->p_next;
976 mvar_AppendVar( s, cat );
977 p_category = p_category->p_next;
979 vlc_mutex_unlock( &p_input->stream.stream_lock );
984 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
986 mvar_t *s = mvar_New( name, "set" );
990 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
991 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
993 for( i= 0; i < info.i_count; )
997 inf = mvar_New( name, "set" );
1000 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
1001 info.info[i].psz_name, info.info[i].psz_value ); */
1002 mvar_AppendNewVar( inf,
1003 info.info[i].psz_name,
1004 info.info[i].psz_value );
1006 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
1007 mvar_AppendVar( s, inf );
1012 for( i = 0; i < info.i_count; i++ )
1014 free( info.info[i].psz_name );
1015 free( info.info[i].psz_value );
1020 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1022 mvar_t *s = mvar_New( name, "set" );
1023 char tmp[MAX_DIR_SIZE], *p, *src;
1024 #ifdef HAVE_SYS_STAT_H
1025 struct stat stat_info;
1028 struct dirent *p_dir_content;
1031 /* convert all / to native separator */
1032 #if defined( WIN32 )
1033 while( (p = strchr( psz_dir, '/' )) )
1042 /* remove trailling separator */
1043 while( strlen( psz_dir ) > 1 &&
1044 #if defined( WIN32 )
1045 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1047 psz_dir[strlen( psz_dir ) -1 ] == sep )
1049 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1051 /* remove double separator */
1052 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1054 if( src[0] == sep && src[1] == sep )
1062 if( *psz_dir == '\0' )
1066 /* first fix all .. dir */
1070 if( src[0] == '.' && src[1] == '.' )
1073 if( p <= &psz_dir[1] )
1080 while( p > &psz_dir[1] && *p != sep )
1085 else if( *src == sep )
1087 if( p > psz_dir && p[-1] == sep )
1101 } while( *src && *src != sep );
1106 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1108 #ifdef HAVE_SYS_STAT_H
1109 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1115 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1117 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1121 /* remove traling / or \ */
1122 for( p = &psz_dir[strlen( psz_dir) - 1]; p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1131 /* parse psz_src dir */
1132 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1136 if( !strcmp( p_dir_content->d_name, "." ) )
1141 #if defined( WIN32 )
1142 sprintf( tmp, "%s\\%s", psz_dir, p_dir_content->d_name );
1144 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1147 #ifdef HAVE_SYS_STAT_H
1148 if( stat( tmp, &stat_info ) == -1 )
1153 f = mvar_New( name, "set" );
1154 mvar_AppendNewVar( f, "name", tmp );
1155 #ifdef HAVE_SYS_STAT_H
1156 if( S_ISDIR( stat_info.st_mode ) )
1158 mvar_AppendNewVar( f, "type", "directory" );
1160 else if( S_ISREG( stat_info.st_mode ) )
1162 mvar_AppendNewVar( f, "type", "file" );
1166 mvar_AppendNewVar( f, "type", "unknown" );
1169 sprintf( tmp, "%lld", stat_info.st_size );
1170 mvar_AppendNewVar( f, "size", tmp );
1172 /* FIXME memory leak FIXME */
1174 ctime_r( &stat_info.st_mtime, tmp );
1175 mvar_AppendNewVar( f, "date", tmp );
1177 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1181 mvar_AppendNewVar( f, "type", "unknown" );
1182 mvar_AppendNewVar( f, "size", "unknown" );
1183 mvar_AppendNewVar( f, "date", "unknown" );
1185 mvar_AppendVar( s, f );
1191 static void SSInit( rpn_stack_t * );
1192 static void SSClean( rpn_stack_t * );
1193 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1195 static void SSPush ( rpn_stack_t *, char * );
1196 static char *SSPop ( rpn_stack_t * );
1198 static void SSPushN ( rpn_stack_t *, int );
1199 static int SSPopN ( rpn_stack_t *, mvar_t * );
1202 /****************************************************************************
1204 ****************************************************************************/
1212 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1216 /* just load the file */
1218 *pp_data = malloc( 1025 ); /* +1 for \0 */
1219 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1222 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1228 (*pp_data)[*pi_data] = '\0';
1233 static int MacroParse( macro_t *m, uint8_t *psz_src )
1235 uint8_t *dup = strdup( psz_src );
1240 #define EXTRACT( name, l ) \
1242 p = strchr( src, '"' ); \
1247 m->name = strdup( src ); \
1268 if( !strncmp( src, "id=\"", 4 ) )
1272 else if( !strncmp( src, "param1=\"", 8 ) )
1274 EXTRACT( param1, 8 );
1276 else if( !strncmp( src, "param2=\"", 8 ) )
1278 EXTRACT( param2, 8 );
1285 if( strstr( src, "/>" ) )
1287 src = strstr( src, "/>" ) + 2;
1291 src += strlen( src );
1296 m->id = strdup( "" );
1298 if( m->param1 == NULL )
1300 m->param1 = strdup( "" );
1302 if( m->param2 == NULL )
1304 m->param2 = strdup( "" );
1313 static void MacroClean( macro_t *m )
1359 StrToMacroTypeTab [] =
1361 { "control", MVLC_CONTROL },
1362 /* player control */
1363 { "play", MVLC_PLAY },
1364 { "stop", MVLC_STOP },
1365 { "pause", MVLC_PAUSE },
1366 { "next", MVLC_NEXT },
1367 { "previous", MVLC_PREVIOUS },
1368 { "seek", MVLC_SEEK },
1369 { "keep", MVLC_KEEP },
1370 { "fullscreen", MVLC_FULLSCREEN },
1371 { "volume", MVLC_VOLUME },
1373 /* playlist management */
1374 { "add", MVLC_ADD },
1375 { "delete", MVLC_DEL },
1376 { "empty", MVLC_EMPTY },
1377 { "sort", MVLC_SORT },
1380 { "close", MVLC_CLOSE },
1381 { "shutdown", MVLC_SHUTDOWN },
1383 { "rpn", MVLC_RPN },
1385 { "foreach", MVLC_FOREACH },
1386 { "value", MVLC_VALUE },
1389 { "else", MVLC_ELSE },
1390 { "end", MVLC_END },
1391 { "get", MVLC_GET },
1392 { "set", MVLC_SET },
1393 { "int", MVLC_INT },
1394 { "float", MVLC_FLOAT },
1395 { "string", MVLC_STRING },
1398 { NULL, MVLC_UNKNOWN }
1401 static int StrToMacroType( char *name )
1405 if( !name || *name == '\0')
1407 return MVLC_UNKNOWN;
1409 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1411 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1413 return StrToMacroTypeTab[i].i_type;
1416 return MVLC_UNKNOWN;
1419 static void MacroDo( httpd_file_callback_args_t *p_args,
1421 uint8_t *p_request, int i_request,
1422 uint8_t **pp_data, int *pi_data,
1425 intf_thread_t *p_intf = p_args->p_intf;
1426 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1429 #define ALLOC( l ) \
1431 int __i__ = *pp_dst - *pp_data; \
1433 *pp_data = realloc( *pp_data, *pi_data ); \
1434 *pp_dst = (*pp_data) + __i__; \
1436 #define PRINT( str ) \
1437 ALLOC( strlen( str ) + 1 ); \
1438 *pp_dst += sprintf( *pp_dst, str );
1440 #define PRINTS( str, s ) \
1441 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1443 char * psz_cur = *pp_dst; \
1444 *pp_dst += sprintf( *pp_dst, str, s ); \
1445 while( psz_cur && *psz_cur ) \
1447 /* Prevent script injection */ \
1448 if( *psz_cur == '<' ) *psz_cur = '*'; \
1449 if( *psz_cur == '>' ) *psz_cur = '*'; \
1454 switch( StrToMacroType( m->id ) )
1457 if( i_request <= 0 )
1461 uri_extract_value( p_request, "control", control, 512 );
1462 if( *m->param1 && !strstr( m->param1, control ) )
1464 msg_Warn( p_intf, "unauthorized control=%s", control );
1467 switch( StrToMacroType( control ) )
1474 uri_extract_value( p_request, "item", item, 512 );
1475 i_item = atoi( item );
1476 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
1477 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1481 playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1482 msg_Dbg( p_intf, "requested playlist stop" );
1485 playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1486 msg_Dbg( p_intf, "requested playlist pause" );
1489 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1490 p_sys->p_playlist->i_index + 1 );
1491 msg_Dbg( p_intf, "requested playlist next" );
1494 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1495 p_sys->p_playlist->i_index - 1 );
1496 msg_Dbg( p_intf, "requested playlist next" );
1498 case MVLC_FULLSCREEN:
1500 if( p_sys->p_input )
1502 vout_thread_t *p_vout;
1503 p_vout = vlc_object_find( p_sys->p_input,
1504 VLC_OBJECT_VOUT, FIND_CHILD );
1508 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1509 vlc_object_release( p_vout );
1510 msg_Dbg( p_intf, "requested fullscreen toggle" );
1524 #define POSITION_ABSOLUTE 12
1525 #define POSITION_REL_FOR 13
1526 #define POSITION_REL_BACK 11
1527 #define TIME_ABSOLUTE 0
1528 #define TIME_REL_FOR 1
1529 #define TIME_REL_BACK -1
1530 if( p_sys->p_input )
1532 uri_extract_value( p_request, "seek_value", value, 20 );
1533 uri_decode_url_encoded( value );
1535 var_Get( p_sys->p_input, "length", &val);
1536 i_length = val.i_time;
1538 while( p_value[0] != '\0' )
1544 i_relative = TIME_REL_FOR;
1550 i_relative = TIME_REL_BACK;
1554 case '0': case '1': case '2': case '3': case '4':
1555 case '5': case '6': case '7': case '8': case '9':
1557 i_stock = strtol( p_value , &p_value , 10 );
1560 case '%': /* for percentage ie position */
1562 i_relative += POSITION_ABSOLUTE;
1570 i_value = 60 * (i_value + i_stock) ;
1575 case 'h': case 'H': /* hours */
1577 i_value += 3600 * i_stock;
1579 /* other characters which are not numbers are not important */
1580 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1586 case 'm': case 'M': case '\'': /* minutes */
1588 i_value += 60 * i_stock;
1591 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1597 case 's': case 'S': case '"': /* seconds */
1601 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1615 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1622 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1623 val.i_time = (uint64_t)( i_value ) * 1000000;
1625 val.i_time = i_length;
1627 var_Set( p_sys->p_input, "time", val );
1628 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1633 var_Get( p_sys->p_input, "time", &val );
1634 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1636 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1639 val.i_time = i_length;
1641 var_Set( p_sys->p_input, "time", val );
1642 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1647 var_Get( p_sys->p_input, "time", &val );
1648 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1653 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1655 var_Set( p_sys->p_input, "time", val );
1656 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1659 case POSITION_ABSOLUTE:
1661 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1662 var_Set( p_sys->p_input, "position", val );
1663 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1666 case POSITION_REL_FOR:
1668 var_Get( p_sys->p_input, "position", &val );
1669 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1670 var_Set( p_sys->p_input, "position", val );
1671 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1674 case POSITION_REL_BACK:
1676 var_Get( p_sys->p_input, "position", &val );
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 backward: %d", i_value );
1684 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1689 #undef POSITION_ABSOLUTE
1690 #undef POSITION_REL_FOR
1691 #undef POSITION_REL_BACK
1692 #undef TIME_ABSOLUTE
1694 #undef TIME_REL_BACK
1700 audio_volume_t i_volume;
1703 uri_extract_value( p_request, "value", vol, 8 );
1704 aout_VolumeGet( p_intf, &i_volume );
1705 uri_decode_url_encoded( vol );
1709 i_value = atoi( vol + 1 );
1710 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1712 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1713 msg_Dbg( p_intf, "requested volume set: max" );
1716 aout_VolumeSet( p_intf , (i_volume + i_value) );
1717 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1722 i_value = atoi( vol + 1 );
1723 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1725 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1726 msg_Dbg( p_intf, "requested volume set: min" );
1729 aout_VolumeSet( p_intf , (i_volume - i_value) );
1730 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1734 i_value = atoi( vol );
1735 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1737 aout_VolumeSet( p_intf , atoi( vol ) );
1738 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1744 /* playlist management */
1748 playlist_item_t * p_item;
1750 uri_extract_value( p_request, "mrl", mrl, 512 );
1751 uri_decode_url_encoded( mrl );
1752 p_item = parse_MRL( mrl );
1754 if( p_item == NULL )
1756 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1759 playlist_AddItem( p_sys->p_playlist , p_item ,
1760 PLAYLIST_APPEND, PLAYLIST_END );
1761 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1768 int i_item, *p_items = NULL, i_nb_items = 0;
1769 char item[512], *p_parser = p_request;
1771 /* Get the list of items to delete */
1773 uri_extract_value( p_parser, "item", item, 512 )) )
1775 if( !*item ) continue;
1777 i_item = atoi( item );
1778 p_items = realloc( p_items, (i_nb_items + 1) *
1780 p_items[i_nb_items] = i_item;
1784 /* The items need to be deleted from in reversed order */
1788 for( i = 0; i < i_nb_items; i++ )
1791 for( j = 0; j < i_nb_items; j++ )
1793 if( p_items[j] > p_items[i_index] )
1797 playlist_Delete( p_sys->p_playlist,
1799 msg_Dbg( p_intf, "requested playlist delete: %d",
1801 p_items[i_index] = -1;
1805 if( p_items ) free( p_items );
1810 int i_item, *p_items = NULL, i_nb_items = 0;
1811 char item[512], *p_parser = p_request;
1814 /* Get the list of items to keep */
1816 uri_extract_value( p_parser, "item", item, 512 )) )
1818 if( !*item ) continue;
1820 i_item = atoi( item );
1821 p_items = realloc( p_items, (i_nb_items + 3) *
1823 p_items[i_nb_items + 1] = i_item;
1827 /* sort item list */
1828 for( i=1 ; i < (i_nb_items + 1) ; i++)
1830 for( j=(i+1) ; j < (i_nb_items + 1) ; j++)
1832 if( p_items[j] > p_items[i] )
1835 p_items[j] = p_items[i];
1841 p_items[0] = p_sys->p_playlist->i_size;
1842 p_items[ i_nb_items + 1 ] = -1;
1844 /* The items need to be deleted from in reversed order */
1845 for( i=0 ; i <= i_nb_items ; i++ )
1847 for( j = (p_items[i] - 1) ; j > p_items[i + 1] ; j-- )
1849 playlist_Delete( p_sys->p_playlist,
1851 msg_Dbg( p_intf, "requested playlist delete: %d",
1856 if( p_items ) free( p_items );
1861 while( p_sys->p_playlist->i_size > 0 )
1863 playlist_Delete( p_sys->p_playlist, 0 );
1865 msg_Dbg( p_intf, "requested playlist empty" );
1874 uri_extract_value( p_request, "type", type, 12 );
1875 uri_extract_value( p_request, "order", order, 2 );
1877 if( order[0] == '0' ) i_order = SORT_NORMAL;
1878 else i_order = SORT_REVERSE;
1880 if( !strcmp( type , "title" ) )
1882 playlist_SortTitle( p_sys->p_playlist , i_order );
1883 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
1884 } else if( !strcmp( type , "group" ) )
1886 playlist_SortGroup( p_sys->p_playlist , i_order );
1887 msg_Dbg( p_intf, "requested playlist sort by group (%d)" , i_order );
1888 } else if( !strcmp( type , "author" ) )
1890 playlist_SortAuthor( p_sys->p_playlist , i_order );
1891 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
1897 /* admin function */
1901 uri_extract_value( p_request, "id", id, 512 );
1902 msg_Dbg( p_intf, "requested close id=%s", id );
1903 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
1905 msg_Warn( p_intf, "close failed for id=%s", id );
1911 msg_Dbg( p_intf, "requested shutdown" );
1912 p_intf->p_vlc->b_die = VLC_TRUE;
1916 PRINTS( "<!-- control param(%s) unsuported -->", control );
1927 if( i_request <= 0 ||
1928 *m->param1 == '\0' ||
1929 strstr( p_request, m->param1 ) == NULL )
1933 uri_extract_value( p_request, m->param1, value, 512 );
1934 uri_decode_url_encoded( value );
1936 switch( StrToMacroType( m->param2 ) )
1940 config_PutInt( p_intf, m->param1, i );
1944 config_PutFloat( p_intf, m->param1, f );
1947 config_PutPsz( p_intf, m->param1, value );
1950 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
1961 if( *m->param1 == '\0' )
1966 switch( StrToMacroType( m->param2 ) )
1969 i = config_GetInt( p_intf, m->param1 );
1970 sprintf( value, "%i", i );
1973 f = config_GetFloat( p_intf, m->param1 );
1974 sprintf( value, "%f", f );
1977 psz = config_GetPsz( p_intf, m->param1 );
1978 sprintf( value, "%s", psz ? psz : "" );
1981 sprintf( value, "invalid type(%s) in set", m->param2 );
1984 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
1985 PRINTS( "%s", value );
1994 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1995 s = SSPop( &p_args->stack );
1996 v = mvar_GetValue( p_args->vars, s );
2000 v = s = SSPop( &p_args->stack );
2008 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2012 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2013 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2021 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
2028 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
2033 i_skip = MacroParse( &m, src );
2035 i_id = StrToMacroType( m.id );
2050 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2051 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2053 return src + ( b_after ? i_skip : 0 );
2055 else if( i_level < 0 )
2071 static void Execute( httpd_file_callback_args_t *p_args,
2072 uint8_t *p_request, int i_request,
2073 uint8_t **pp_data, int *pi_data,
2075 uint8_t *_src, uint8_t *_end )
2077 intf_thread_t *p_intf = p_args->p_intf;
2079 uint8_t *src, *dup, *end;
2080 uint8_t *dst = *pp_dst;
2082 src = dup = malloc( _end - _src + 1 );
2083 end = src +( _end - _src );
2085 memcpy( src, _src, _end - _src );
2088 /* we parse searching <vlc */
2094 p = strstr( src, "<vlc" );
2095 if( p < end && p == src )
2099 src += MacroParse( &m, src );
2101 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2103 switch( StrToMacroType( m.id ) )
2110 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2111 if( SSPopN( &p_args->stack, p_args->vars ) )
2119 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2123 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2127 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2130 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2134 else if( i_test == 1 )
2137 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2139 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2143 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2152 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2153 uint8_t *start = src;
2154 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2161 if( !strcmp( m.param2, "integer" ) )
2163 char *arg = SSPop( &p_args->stack );
2164 index = mvar_IntegerSetNew( m.param1, arg );
2167 else if( !strcmp( m.param2, "directory" ) )
2169 char *arg = SSPop( &p_args->stack );
2170 index = mvar_FileSetNew( m.param1, arg );
2173 else if( !strcmp( m.param2, "playlist" ) )
2175 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2177 else if( !strcmp( m.param2, "informations" ) )
2179 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2181 else if( !strcmp( m.param2, "hosts" ) )
2183 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2185 else if( !strcmp( m.param2, "urls" ) )
2187 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2189 else if( !strcmp( m.param2, "connections" ) )
2191 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2193 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2195 index = mvar_Duplicate( v );
2199 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2204 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2206 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2208 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2211 f->name = strdup( m.param1 );
2214 mvar_PushVar( p_args->vars, f );
2215 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2216 mvar_RemoveVar( p_args->vars, f );
2220 mvar_Delete( index );
2227 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2235 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2238 int i_index = dst - *pp_data;
2241 *pp_data = realloc( *pp_data, *pi_data );
2242 dst = (*pp_data) + i_index;
2244 memcpy( dst, src, i_copy );
2254 /****************************************************************************
2256 ****************************************************************************
2257 * a file with mime == text/html is parsed and all "macro" replaced
2258 * <vlc id="macro name" [param1="" [param2=""]] />
2261 ****************************************************************************/
2262 static int http_get( httpd_file_callback_args_t *p_args,
2263 uint8_t *p_request, int i_request,
2264 uint8_t **pp_data, int *pi_data )
2269 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2271 p = *pp_data = malloc( 10240 );
2274 return VLC_EGENERIC;
2276 p += sprintf( p, "<html>\n" );
2277 p += sprintf( p, "<head>\n" );
2278 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2279 p += sprintf( p, "</head>\n" );
2280 p += sprintf( p, "<body>\n" );
2281 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2282 p += sprintf( p, "<hr />\n" );
2283 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2284 p += sprintf( p, "</body>\n" );
2285 p += sprintf( p, "</html>\n" );
2287 *pi_data = strlen( *pp_data ) + 1;
2292 if( strcmp( p_args->mime, "text/html" ) )
2294 FileLoad( f, pp_data, pi_data );
2302 char position[4]; /* percentage */
2303 char time[12]; /* in seconds */
2304 char length[12]; /* in seconds */
2305 audio_volume_t i_volume;
2309 #define p_sys p_args->p_intf->p_sys
2310 if( p_sys->p_input )
2312 var_Get( p_sys->p_input, "position", &val);
2313 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2314 var_Get( p_sys->p_input, "time", &val);
2315 sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2316 var_Get( p_sys->p_input, "length", &val);
2317 sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2319 var_Get( p_sys->p_input, "state", &val );
2320 if( val.i_int == PLAYING_S )
2322 sprintf( state, "playing" );
2323 } else if( val.i_int == PAUSE_S )
2325 sprintf( state, "paused" );
2328 sprintf( state, "stop" );
2332 sprintf( position, "%d", 0 );
2333 sprintf( time, "%d", 0 );
2334 sprintf( length, "%d", 0 );
2335 sprintf( state, "stop" );
2339 aout_VolumeGet( p_args->p_intf , &i_volume );
2340 sprintf( volume , "%d" , (int)i_volume );
2342 p_args->vars = mvar_New( "variables", "" );
2343 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2344 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2345 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
2346 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2347 mvar_AppendNewVar( p_args->vars, "stream_position", position );
2348 mvar_AppendNewVar( p_args->vars, "stream_time", time );
2349 mvar_AppendNewVar( p_args->vars, "stream_length", length );
2350 mvar_AppendNewVar( p_args->vars, "volume", volume );
2351 mvar_AppendNewVar( p_args->vars, "stream_state", state );
2353 SSInit( &p_args->stack );
2355 /* first we load in a temporary buffer */
2356 FileLoad( f, &p_buffer, &i_buffer );
2358 /* allocate output */
2359 *pi_data = i_buffer + 1000;
2360 dst = *pp_data = malloc( *pi_data );
2362 /* we parse executing all <vlc /> macros */
2363 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2366 *pi_data = dst - *pp_data;
2368 SSClean( &p_args->stack );
2369 mvar_Delete( p_args->vars );
2377 /****************************************************************************
2379 ****************************************************************************/
2380 static char *uri_extract_value( char *psz_uri, char *psz_name,
2381 char *psz_value, int i_value_max )
2385 p = strstr( psz_uri, psz_name );
2390 p += strlen( psz_name );
2391 if( *p == '=' ) p++;
2393 if( strchr( p, '&' ) )
2395 i_len = strchr( p, '&' ) - p;
2399 /* for POST method */
2400 if( strchr( p, '\n' ) )
2402 i_len = strchr( p, '\n' ) - p;
2403 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2407 i_len = strlen( p );
2410 i_len = __MIN( i_value_max - 1, i_len );
2413 strncpy( psz_value, p, i_len );
2414 psz_value[i_len] = '\0';
2418 strncpy( psz_value, "", i_value_max );
2424 strncpy( psz_value, "", i_value_max );
2430 static void uri_decode_url_encoded( char *psz )
2432 char *dup = strdup( psz );
2450 *psz++ = strtol( val, NULL, 16 );
2452 else if( *p == '+' )
2466 /****************************************************************************
2467 * Light RPN evaluator
2468 ****************************************************************************/
2469 static void SSInit( rpn_stack_t *st )
2474 static void SSClean( rpn_stack_t *st )
2476 while( st->i_stack > 0 )
2478 free( st->stack[--st->i_stack] );
2482 static void SSPush( rpn_stack_t *st, char *s )
2484 if( st->i_stack < STACK_MAX )
2486 st->stack[st->i_stack++] = strdup( s );
2490 static char * SSPop( rpn_stack_t *st )
2492 if( st->i_stack <= 0 )
2494 return strdup( "" );
2498 return st->stack[--st->i_stack];
2502 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2511 i = strtol( name, &end, 0 );
2514 value = mvar_GetValue( vars, name );
2522 static void SSPushN( rpn_stack_t *st, int i )
2526 sprintf( v, "%d", i );
2530 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2537 while( *exp == ' ' )
2544 /* extract string */
2547 while( *exp && *exp != '\'' )
2558 p = strchr( exp, ' ' );
2563 exp += strlen( exp );
2568 strncpy( s, exp, i );
2579 /* 1. Integer function */
2580 if( !strcmp( s, "!" ) )
2582 SSPushN( st, ~SSPopN( st, vars ) );
2584 else if( !strcmp( s, "^" ) )
2586 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2588 else if( !strcmp( s, "&" ) )
2590 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2592 else if( !strcmp( s, "|" ) )
2594 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2596 else if( !strcmp( s, "+" ) )
2598 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2600 else if( !strcmp( s, "-" ) )
2602 int i = SSPopN( st, vars );
2603 int j = SSPopN( st, vars );
2604 SSPushN( st, i - j );
2606 else if( !strcmp( s, "*" ) )
2608 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2610 else if( !strcmp( s, "/" ) )
2614 i = SSPopN( st, vars );
2615 j = SSPopN( st, vars );
2617 SSPushN( st, j != 0 ? i / j : 0 );
2619 else if( !strcmp( s, "%" ) )
2623 i = SSPopN( st, vars );
2624 j = SSPopN( st, vars );
2626 SSPushN( st, j != 0 ? i % j : 0 );
2628 /* 2. integer tests */
2629 else if( !strcmp( s, "=" ) )
2631 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2633 else if( !strcmp( s, "<" ) )
2635 int i = SSPopN( st, vars );
2636 int j = SSPopN( st, vars );
2638 SSPushN( st, i < j ? -1 : 0 );
2640 else if( !strcmp( s, ">" ) )
2642 int i = SSPopN( st, vars );
2643 int j = SSPopN( st, vars );
2645 SSPushN( st, i > j ? -1 : 0 );
2647 else if( !strcmp( s, "<=" ) )
2649 int i = SSPopN( st, vars );
2650 int j = SSPopN( st, vars );
2652 SSPushN( st, i <= j ? -1 : 0 );
2654 else if( !strcmp( s, ">=" ) )
2656 int i = SSPopN( st, vars );
2657 int j = SSPopN( st, vars );
2659 SSPushN( st, i >= j ? -1 : 0 );
2661 /* 3. string functions */
2662 else if( !strcmp( s, "strcat" ) )
2664 char *s1 = SSPop( st );
2665 char *s2 = SSPop( st );
2666 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2676 else if( !strcmp( s, "strcmp" ) )
2678 char *s1 = SSPop( st );
2679 char *s2 = SSPop( st );
2681 SSPushN( st, strcmp( s1, s2 ) );
2685 else if( !strcmp( s, "strncmp" ) )
2687 char *s1 = SSPop( st );
2688 char *s2 = SSPop( st );
2689 int n = SSPopN( st, vars );
2691 SSPushN( st, strncmp( s1, s2 , n ) );
2695 else if( !strcmp( s, "strlen" ) )
2697 char *str = SSPop( st );
2699 SSPushN( st, strlen( str ) );
2702 /* 4. stack functions */
2703 else if( !strcmp( s, "dup" ) )
2705 char *str = SSPop( st );
2710 else if( !strcmp( s, "drop" ) )
2712 char *str = SSPop( st );
2715 else if( !strcmp( s, "swap" ) )
2717 char *s1 = SSPop( st );
2718 char *s2 = SSPop( st );
2725 else if( !strcmp( s, "flush" ) )
2730 else if( !strcmp( s, "store" ) )
2732 char *name = SSPop( st );
2733 char *value = SSPop( st );
2735 mvar_PushNewVar( vars, name, value );
2739 else if( !strcmp( s, "value" ) )
2741 char *name = SSPop( st );
2742 char *value = mvar_GetValue( vars, name );
2744 SSPush( st, value );
2748 else if( !strcmp( s, "url_extract" ) )
2750 char *url = mvar_GetValue( vars, "url_value" );
2751 char *name = SSPop( st );
2754 uri_extract_value( url, name, value, 512 );
2755 uri_decode_url_encoded( value );
2756 SSPush( st, value );
2765 /**********************************************************************
2766 * Find_end_MRL: Find the end of the sentence :
2767 * this function parses the string psz and find the end of the item
2768 * and/or option with detecting the " and ' problems.
2769 * returns NULL if an error is detected, otherwise, returns a pointer
2770 * of the end of the sentence (after the last character)
2771 **********************************************************************/
2772 static char *Find_end_MRL( char *psz )
2782 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
2784 if( *s_sent == '\'' )
2786 s_sent = Find_end_MRL( s_sent );
2788 if( s_sent == NULL )
2798 if( *s_sent == '\"' )
2802 } else /* *s_sent == '\0' , which means the number of " is incorrect */
2812 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
2814 if( *s_sent == '\"' )
2816 s_sent = Find_end_MRL( s_sent );
2818 if( s_sent == NULL )
2828 if( *s_sent == '\'' )
2832 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
2838 default: /* now we can look for spaces */
2840 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
2842 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
2844 s_sent = Find_end_MRL( s_sent );
2855 /**********************************************************************
2856 * parse_MRL: parse the MRL, find the mrl string and the options,
2857 * create an item with all informations in it, and return the item.
2858 * return NULL if there is an error.
2859 **********************************************************************/
2860 playlist_item_t * parse_MRL( char *psz )
2862 char **ppsz_options = NULL;
2869 playlist_item_t * p_item;
2871 /* In case there is spaces before the mrl */
2872 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
2877 /* extract the mrl */
2878 s_temp = Find_end_MRL( s_mrl );
2880 if( s_temp == NULL )
2885 /* if the mrl is between " or ', we must remove them */
2886 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
2888 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
2889 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
2890 mrl[ s_temp - s_mrl - 2 ] = '\0';
2893 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
2894 strncpy( mrl , s_mrl , s_temp - s_mrl );
2895 mrl[ s_temp - s_mrl ] = '\0';
2900 /* now we can take care of the options */
2901 while( (*s_mrl != '\0') && (i_error == 0) )
2910 case ':': /* an option */
2912 s_temp = Find_end_MRL( s_mrl );
2914 if( s_temp == NULL )
2920 ppsz_options = (char **)realloc( ppsz_options , i_options * sizeof(char *) );
2921 ppsz_options[ i_options - 1 ] = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
2923 strncpy( ppsz_options[ i_options - 1 ] , s_mrl , s_temp - s_mrl );
2924 /* don't forget to finish the string with a '\0' */
2925 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
2942 for( i = 0 ; i < i_options ; i++ )
2944 free( ppsz_options[i] );
2946 free( ppsz_options );
2950 /* now create an item */
2951 p_item = malloc( sizeof( playlist_item_t ) );
2953 p_item->psz_name = mrl;
2954 p_item->psz_uri = mrl;
2955 p_item->psz_author = strdup( "" );
2956 p_item->i_duration = -1;
2958 p_item->i_status = 0;
2959 p_item->b_autodeletion = VLC_FALSE;
2960 p_item->b_enabled = VLC_TRUE;
2961 p_item->i_group = PLAYLIST_TYPE_MANUAL;
2963 p_item->ppsz_options = NULL;
2964 p_item->i_options = i_options;
2968 p_item->ppsz_options = ppsz_options;