1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
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 */
43 #include "vlc_httpd.h"
45 #ifdef HAVE_SYS_STAT_H
46 # include <sys/stat.h>
57 #elif defined( WIN32 ) && !defined( UNDER_CE )
61 #if (!defined( WIN32 ) || defined(__MINGW32__))
62 /* Mingw has its own version of dirent */
66 /*****************************************************************************
68 *****************************************************************************/
69 static int Activate ( vlc_object_t * );
70 static void Close ( vlc_object_t * );
72 #define HOST_TEXT N_( "Host address" )
73 #define HOST_LONGTEXT N_( \
74 "You can set the address and port the http interface will bind to." )
75 #define SRC_TEXT N_( "Source directory" )
76 #define SRC_LONGTEXT N_( "Source directory" )
79 set_description( _("HTTP remote control interface") );
80 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
81 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
82 set_capability( "interface", 0 );
83 set_callbacks( Activate, Close );
87 /*****************************************************************************
89 *****************************************************************************/
90 static void Run ( intf_thread_t *p_intf );
92 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
95 static int DirectoryCheck( char *psz_dir )
99 #ifdef HAVE_SYS_STAT_H
100 struct stat stat_info;
102 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
108 if( ( p_dir = opendir( psz_dir ) ) == NULL )
117 static int HttpCallback( httpd_file_sys_t *p_args,
120 uint8_t **pp_data, int *pi_data );
122 static char *uri_extract_value( char *psz_uri, char *psz_name,
123 char *psz_value, int i_value_max );
124 static void uri_decode_url_encoded( char *psz );
126 static char *Find_end_MRL( char *psz );
128 static playlist_item_t * parse_MRL( intf_thread_t * , char *psz );
130 /*****************************************************************************
132 *****************************************************************************/
133 typedef struct mvar_s
139 struct mvar_s **field;
142 #define STACK_MAX 100
145 char *stack[STACK_MAX];
149 struct httpd_file_sys_t
151 intf_thread_t *p_intf;
152 httpd_file_t *p_file;
153 httpd_redirect_t *p_redir;
160 /* inited for each access */
167 httpd_host_t *p_httpd_host;
170 httpd_file_sys_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 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_intf), psz_address, i_port );
218 if( p_sys->p_httpd_host == NULL )
220 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
231 p_sys->pp_files = NULL;
233 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
234 ( defined(WIN32) && !defined(UNDER_CE ) )
235 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
237 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
238 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
244 sprintf( psz_src, "%s/http", psz_vlcpath);
246 sprintf( psz_src, "%s/share/http", psz_vlcpath);
250 psz_src = config_GetPsz( p_intf, "http-src" );
252 if( !psz_src || *psz_src == '\0' )
254 if( !DirectoryCheck( "share/http" ) )
256 psz_src = strdup( "share/http" );
258 else if( !DirectoryCheck( DATA_PATH "/http" ) )
260 psz_src = strdup( DATA_PATH "/http" );
265 if( !psz_src || *psz_src == '\0' )
267 msg_Err( p_intf, "invalid src dir" );
271 /* remove trainling \ or / */
272 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
273 psz_src[strlen( psz_src ) - 1] == '/' )
275 psz_src[strlen( psz_src ) - 1] = '\0';
278 ParseDirectory( p_intf, psz_src, psz_src );
281 if( p_sys->i_files <= 0 )
283 msg_Err( p_intf, "cannot find any files (%s)", psz_src );
286 p_intf->pf_run = Run;
292 if( psz_src ) free( psz_src );
293 if( p_sys->pp_files )
295 free( p_sys->pp_files );
297 httpd_HostDelete( p_sys->p_httpd_host );
302 /*****************************************************************************
303 * CloseIntf: destroy interface
304 *****************************************************************************/
305 void Close ( vlc_object_t *p_this )
307 intf_thread_t *p_intf = (intf_thread_t *)p_this;
308 intf_sys_t *p_sys = p_intf->p_sys;
312 for( i = 0; i < p_sys->i_files; i++ )
314 httpd_FileDelete( p_sys->pp_files[i]->p_file );
315 if( p_sys->pp_files[i]->p_redir )
317 httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
320 free( p_sys->pp_files[i]->file );
321 free( p_sys->pp_files[i]->name );
322 free( p_sys->pp_files[i] );
324 if( p_sys->pp_files )
326 free( p_sys->pp_files );
328 httpd_HostDelete( p_sys->p_httpd_host );
333 /*****************************************************************************
334 * Run: http interface thread
335 *****************************************************************************/
336 static void Run( intf_thread_t *p_intf )
338 intf_sys_t *p_sys = p_intf->p_sys;
340 while( !p_intf->b_die )
342 /* get the playlist */
343 if( p_sys->p_playlist == NULL )
345 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
348 /* Manage the input part */
349 if( p_sys->p_input == NULL )
351 if( p_sys->p_playlist )
354 vlc_object_find( p_sys->p_playlist,
359 else if( p_sys->p_input->b_dead )
361 vlc_object_release( p_sys->p_input );
362 p_sys->p_input = NULL;
367 msleep( INTF_IDLE_SLEEP );
372 vlc_object_release( p_sys->p_input );
373 p_sys->p_input = NULL;
376 if( p_sys->p_playlist )
378 vlc_object_release( p_sys->p_playlist );
379 p_sys->p_playlist = NULL;
384 /*****************************************************************************
386 *****************************************************************************/
387 #define MAX_DIR_SIZE 10240
389 /****************************************************************************
390 * FileToUrl: create a good name for an url from filename
391 ****************************************************************************/
392 static char *FileToUrl( char *name )
396 url = p = malloc( strlen( name ) + 1 );
404 while( *name == '\\' || *name == '/' )
406 while( *name == '/' )
416 /* convert '\\' into '/' */
429 if( ( p = strrchr( url, '/' ) ) != NULL )
431 if( !strncmp( p, "/index.", 7 ) )
439 /****************************************************************************
440 * ParseDirectory: parse recursively a directory, adding each file
441 ****************************************************************************/
442 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
445 intf_sys_t *p_sys = p_intf->p_sys;
446 char dir[MAX_DIR_SIZE];
447 #ifdef HAVE_SYS_STAT_H
448 struct stat stat_info;
451 struct dirent *p_dir_content;
455 char *password = NULL;
457 #ifdef HAVE_SYS_STAT_H
458 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
464 if( ( p_dir = opendir( psz_dir ) ) == NULL )
466 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
470 msg_Dbg( p_intf, "dir=%s", psz_dir );
472 sprintf( dir, "%s/.access", psz_dir );
473 if( ( file = fopen( dir, "r" ) ) != NULL )
478 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
480 i_size = fread( line, 1, 1023, file );
484 while( i_size > 0 && ( line[i_size-1] == '\n' ||
485 line[i_size-1] == '\r' ) )
492 p = strchr( line, ':' );
496 user = strdup( line );
497 password = strdup( p );
500 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
501 user, password, i_size );
508 /* parse psz_src dir */
509 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
514 if( p_dir_content->d_name[0] == '.' )
518 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
519 if( ParseDirectory( p_intf, psz_root, dir ) )
521 httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
526 f->file = strdup( dir );
527 f->name = FileToUrl( &dir[strlen( psz_root )] );
528 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
532 msg_Err( p_intf , "unable to parse directory" );
535 return( VLC_ENOMEM );
537 msg_Dbg( p_intf, "file=%s (url=%s)",
540 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
541 f->name, f->b_html ? "text/html" : NULL,
547 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
549 /* For rep/ add a redir from rep to rep/ */
550 if( f && f->name[strlen(f->name) - 1] == '/' )
552 char *psz_redir = strdup( f->name );
553 psz_redir[strlen( psz_redir ) - 1] = '\0';
555 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
556 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
576 /****************************************************************************
577 * var and set handling
578 ****************************************************************************/
580 static mvar_t *mvar_New( char *name, char *value )
582 mvar_t *v = malloc( sizeof( mvar_t ) );
584 if( !v ) return NULL;
585 v->name = strdup( name );
586 v->value = strdup( value ? value : "" );
589 v->field = malloc( sizeof( mvar_t * ) );
595 static void mvar_Delete( mvar_t *v )
602 for( i = 0; i < v->i_field; i++ )
604 mvar_Delete( v->field[i] );
610 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
612 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
613 v->field[v->i_field] = f;
617 static mvar_t *mvar_Duplicate( mvar_t *v )
622 n = mvar_New( v->name, v->value );
623 for( i = 0; i < v->i_field; i++ )
625 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
631 static void mvar_PushVar( mvar_t *v, mvar_t *f )
633 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
636 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
642 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
645 for( i = 0; i < v->i_field; i++ )
647 if( v->field[i] == f )
652 if( i >= v->i_field )
657 if( i + 1 < v->i_field )
659 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
662 /* FIXME should do a realloc */
665 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
668 char base[512], *field, *p;
671 /* format: name[index].field */
673 field = strchr( name, '.' );
676 int i = field - name;
677 strncpy( base, name, i );
683 strcpy( base, name );
686 if( ( p = strchr( base, '[' ) ) )
689 sscanf( p, "%d]", &i_index );
700 for( i = 0; i < s->i_field; i++ )
702 if( !strcmp( s->field[i]->name, base ) )
712 return mvar_GetVar( s->field[i], field );
726 static char *mvar_GetValue( mvar_t *v, char *field )
734 mvar_t *f = mvar_GetVar( v, field );
746 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
748 mvar_t *f = mvar_New( name, value );
749 mvar_PushVar( vars, f );
752 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
754 mvar_t *f = mvar_New( name, value );
755 mvar_AppendVar( vars, f );
759 /* arg= start[:stop[:step]],.. */
760 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
762 char *dup = strdup( arg );
764 mvar_t *s = mvar_New( name, "set" );
766 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
771 int i_start,i_stop,i_step;
774 p = strchr( str, ',' );
781 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
782 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
789 else if( i_match == 2 )
791 i_step = i_start < i_stop ? 1 : -1;
798 if( ( i_start < i_stop && i_step > 0 ) ||
799 ( i_start > i_stop && i_step < 0 ) )
801 for( i = i_start; ; i += i_step )
805 if( ( i_step > 0 && i > i_stop ) ||
806 ( i_step < 0 && i < i_stop ) )
811 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
812 sprintf( value, "%d", i );
814 mvar_PushNewVar( s, name, value );
825 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
827 mvar_t *s = mvar_New( name, "set" );
830 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
832 vlc_mutex_lock( &p_pl->object_lock );
833 for( i = 0; i < p_pl->i_size; i++ )
835 mvar_t *itm = mvar_New( name, "set" );
838 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
839 mvar_AppendNewVar( itm, "current", value );
841 sprintf( value, "%d", i );
842 mvar_AppendNewVar( itm, "index", value );
844 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->psz_name );
846 mvar_AppendNewVar( itm, "uri", p_pl->pp_items[i]->psz_uri );
848 sprintf( value, "%d", p_pl->pp_items[i]->i_group );
849 mvar_AppendNewVar( itm, "group", value );
851 mvar_AppendVar( s, itm );
853 vlc_mutex_unlock( &p_pl->object_lock );
858 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
860 mvar_t *s = mvar_New( name, "set" );
862 input_info_category_t * p_category;
863 input_info_t * p_info;
865 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
866 if( p_input == NULL )
871 vlc_mutex_lock( &p_input->stream.stream_lock );
872 p_category = p_input->stream.p_info;
875 mvar_t *cat = mvar_New( name, "set" );
876 mvar_t *iset = mvar_New( "info", "set" );
878 mvar_AppendNewVar( cat, "name", p_category->psz_name );
879 mvar_AppendVar( cat, iset );
881 p_info = p_category->p_info;
884 mvar_t *info = mvar_New( "info", "" );
886 msg_Dbg( p_input, "adding info name=%s value=%s", p_info->psz_name, p_info->psz_value );
887 mvar_AppendNewVar( info, "name", p_info->psz_name );
888 mvar_AppendNewVar( info, "value", p_info->psz_value );
889 mvar_AppendVar( iset, info );
890 p_info = p_info->p_next;
892 mvar_AppendVar( s, cat );
893 p_category = p_category->p_next;
895 vlc_mutex_unlock( &p_input->stream.stream_lock );
900 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
902 mvar_t *s = mvar_New( name, "set" );
906 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
907 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
909 for( i= 0; i < info.i_count; )
913 inf = mvar_New( name, "set" );
916 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
917 info.info[i].psz_name, info.info[i].psz_value ); */
918 mvar_AppendNewVar( inf,
919 info.info[i].psz_name,
920 info.info[i].psz_value );
922 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
923 mvar_AppendVar( s, inf );
928 for( i = 0; i < info.i_count; i++ )
930 free( info.info[i].psz_name );
931 free( info.info[i].psz_value );
933 if( info.i_count > 0 )
942 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
944 mvar_t *s = mvar_New( name, "set" );
945 char tmp[MAX_DIR_SIZE], *p, *src;
946 #ifdef HAVE_SYS_STAT_H
947 struct stat stat_info;
950 struct dirent *p_dir_content;
953 /* convert all / to native separator */
955 while( (p = strchr( psz_dir, '/' )) )
964 /* remove trailling separator */
965 while( strlen( psz_dir ) > 1 &&
967 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
969 psz_dir[strlen( psz_dir ) -1 ] == sep )
971 psz_dir[strlen( psz_dir ) -1 ] ='\0';
973 /* remove double separator */
974 for( p = src = psz_dir; *src != '\0'; src++, p++ )
976 if( src[0] == sep && src[1] == sep )
984 if( *psz_dir == '\0' )
988 /* first fix all .. dir */
992 if( src[0] == '.' && src[1] == '.' )
995 if( p <= &psz_dir[1] )
1002 while( p > &psz_dir[1] && *p != sep )
1007 else if( *src == sep )
1009 if( p > psz_dir && p[-1] == sep )
1023 } while( *src && *src != sep );
1028 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1030 #ifdef HAVE_SYS_STAT_H
1031 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1037 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1039 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1043 /* remove traling / or \ */
1044 for( p = &psz_dir[strlen( psz_dir) - 1]; p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1053 /* parse psz_src dir */
1054 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1058 if( !strcmp( p_dir_content->d_name, "." ) )
1063 #if defined( WIN32 )
1064 sprintf( tmp, "%s\\%s", psz_dir, p_dir_content->d_name );
1066 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1069 #ifdef HAVE_SYS_STAT_H
1070 if( stat( tmp, &stat_info ) == -1 )
1075 f = mvar_New( name, "set" );
1076 mvar_AppendNewVar( f, "name", tmp );
1077 #ifdef HAVE_SYS_STAT_H
1078 if( S_ISDIR( stat_info.st_mode ) )
1080 mvar_AppendNewVar( f, "type", "directory" );
1082 else if( S_ISREG( stat_info.st_mode ) )
1084 mvar_AppendNewVar( f, "type", "file" );
1088 mvar_AppendNewVar( f, "type", "unknown" );
1091 sprintf( tmp, "%lld", stat_info.st_size );
1092 mvar_AppendNewVar( f, "size", tmp );
1094 /* FIXME memory leak FIXME */
1096 ctime_r( &stat_info.st_mtime, tmp );
1097 mvar_AppendNewVar( f, "date", tmp );
1099 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1103 mvar_AppendNewVar( f, "type", "unknown" );
1104 mvar_AppendNewVar( f, "size", "unknown" );
1105 mvar_AppendNewVar( f, "date", "unknown" );
1107 mvar_AppendVar( s, f );
1113 static void SSInit( rpn_stack_t * );
1114 static void SSClean( rpn_stack_t * );
1115 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1117 static void SSPush ( rpn_stack_t *, char * );
1118 static char *SSPop ( rpn_stack_t * );
1120 static void SSPushN ( rpn_stack_t *, int );
1121 static int SSPopN ( rpn_stack_t *, mvar_t * );
1124 /****************************************************************************
1126 ****************************************************************************/
1134 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1138 /* just load the file */
1140 *pp_data = malloc( 1025 ); /* +1 for \0 */
1141 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1144 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1150 (*pp_data)[*pi_data] = '\0';
1155 static int MacroParse( macro_t *m, uint8_t *psz_src )
1157 uint8_t *dup = strdup( psz_src );
1162 #define EXTRACT( name, l ) \
1164 p = strchr( src, '"' ); \
1169 m->name = strdup( src ); \
1190 if( !strncmp( src, "id=\"", 4 ) )
1194 else if( !strncmp( src, "param1=\"", 8 ) )
1196 EXTRACT( param1, 8 );
1198 else if( !strncmp( src, "param2=\"", 8 ) )
1200 EXTRACT( param2, 8 );
1207 if( strstr( src, "/>" ) )
1209 src = strstr( src, "/>" ) + 2;
1213 src += strlen( src );
1218 m->id = strdup( "" );
1220 if( m->param1 == NULL )
1222 m->param1 = strdup( "" );
1224 if( m->param2 == NULL )
1226 m->param2 = strdup( "" );
1235 static void MacroClean( macro_t *m )
1282 StrToMacroTypeTab [] =
1284 { "control", MVLC_CONTROL },
1285 /* player control */
1286 { "play", MVLC_PLAY },
1287 { "stop", MVLC_STOP },
1288 { "pause", MVLC_PAUSE },
1289 { "next", MVLC_NEXT },
1290 { "previous", MVLC_PREVIOUS },
1291 { "seek", MVLC_SEEK },
1292 { "keep", MVLC_KEEP },
1293 { "fullscreen", MVLC_FULLSCREEN },
1294 { "volume", MVLC_VOLUME },
1296 /* playlist management */
1297 { "add", MVLC_ADD },
1298 { "delete", MVLC_DEL },
1299 { "empty", MVLC_EMPTY },
1300 { "sort", MVLC_SORT },
1301 { "move", MVLC_MOVE },
1304 { "close", MVLC_CLOSE },
1305 { "shutdown", MVLC_SHUTDOWN },
1307 { "rpn", MVLC_RPN },
1309 { "foreach", MVLC_FOREACH },
1310 { "value", MVLC_VALUE },
1313 { "else", MVLC_ELSE },
1314 { "end", MVLC_END },
1315 { "get", MVLC_GET },
1316 { "set", MVLC_SET },
1317 { "int", MVLC_INT },
1318 { "float", MVLC_FLOAT },
1319 { "string", MVLC_STRING },
1322 { NULL, MVLC_UNKNOWN }
1325 static int StrToMacroType( char *name )
1329 if( !name || *name == '\0')
1331 return MVLC_UNKNOWN;
1333 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1335 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1337 return StrToMacroTypeTab[i].i_type;
1340 return MVLC_UNKNOWN;
1343 static void MacroDo( httpd_file_sys_t *p_args,
1345 uint8_t *p_request, int i_request,
1346 uint8_t **pp_data, int *pi_data,
1349 intf_thread_t *p_intf = p_args->p_intf;
1350 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1353 #define ALLOC( l ) \
1355 int __i__ = *pp_dst - *pp_data; \
1357 *pp_data = realloc( *pp_data, *pi_data ); \
1358 *pp_dst = (*pp_data) + __i__; \
1360 #define PRINT( str ) \
1361 ALLOC( strlen( str ) + 1 ); \
1362 *pp_dst += sprintf( *pp_dst, str );
1364 #define PRINTS( str, s ) \
1365 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1367 char * psz_cur = *pp_dst; \
1368 *pp_dst += sprintf( *pp_dst, str, s ); \
1369 while( psz_cur && *psz_cur ) \
1371 /* Prevent script injection */ \
1372 if( *psz_cur == '<' ) *psz_cur = '*'; \
1373 if( *psz_cur == '>' ) *psz_cur = '*'; \
1378 switch( StrToMacroType( m->id ) )
1381 if( i_request <= 0 )
1385 uri_extract_value( p_request, "control", control, 512 );
1386 if( *m->param1 && !strstr( m->param1, control ) )
1388 msg_Warn( p_intf, "unauthorized control=%s", control );
1391 switch( StrToMacroType( control ) )
1398 uri_extract_value( p_request, "item", item, 512 );
1399 i_item = atoi( item );
1400 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
1401 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1405 playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1406 msg_Dbg( p_intf, "requested playlist stop" );
1409 playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1410 msg_Dbg( p_intf, "requested playlist pause" );
1413 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1414 p_sys->p_playlist->i_index + 1 );
1415 msg_Dbg( p_intf, "requested playlist next" );
1418 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1419 p_sys->p_playlist->i_index - 1 );
1420 msg_Dbg( p_intf, "requested playlist next" );
1422 case MVLC_FULLSCREEN:
1424 if( p_sys->p_input )
1426 vout_thread_t *p_vout;
1427 p_vout = vlc_object_find( p_sys->p_input,
1428 VLC_OBJECT_VOUT, FIND_CHILD );
1432 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1433 vlc_object_release( p_vout );
1434 msg_Dbg( p_intf, "requested fullscreen toggle" );
1448 #define POSITION_ABSOLUTE 12
1449 #define POSITION_REL_FOR 13
1450 #define POSITION_REL_BACK 11
1451 #define TIME_ABSOLUTE 0
1452 #define TIME_REL_FOR 1
1453 #define TIME_REL_BACK -1
1454 if( p_sys->p_input )
1456 uri_extract_value( p_request, "seek_value", value, 20 );
1457 uri_decode_url_encoded( value );
1459 var_Get( p_sys->p_input, "length", &val);
1460 i_length = val.i_time;
1462 while( p_value[0] != '\0' )
1468 i_relative = TIME_REL_FOR;
1474 i_relative = TIME_REL_BACK;
1478 case '0': case '1': case '2': case '3': case '4':
1479 case '5': case '6': case '7': case '8': case '9':
1481 i_stock = strtol( p_value , &p_value , 10 );
1484 case '%': /* for percentage ie position */
1486 i_relative += POSITION_ABSOLUTE;
1494 i_value = 60 * (i_value + i_stock) ;
1499 case 'h': case 'H': /* hours */
1501 i_value += 3600 * i_stock;
1503 /* other characters which are not numbers are not important */
1504 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1510 case 'm': case 'M': case '\'': /* minutes */
1512 i_value += 60 * i_stock;
1515 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1521 case 's': case 'S': case '"': /* seconds */
1525 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1539 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1546 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1547 val.i_time = (uint64_t)( i_value ) * 1000000;
1549 val.i_time = i_length;
1551 var_Set( p_sys->p_input, "time", val );
1552 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1557 var_Get( p_sys->p_input, "time", &val );
1558 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1560 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1563 val.i_time = i_length;
1565 var_Set( p_sys->p_input, "time", val );
1566 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1571 var_Get( p_sys->p_input, "time", &val );
1572 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1577 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1579 var_Set( p_sys->p_input, "time", val );
1580 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1583 case POSITION_ABSOLUTE:
1585 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1586 var_Set( p_sys->p_input, "position", val );
1587 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1590 case POSITION_REL_FOR:
1592 var_Get( p_sys->p_input, "position", &val );
1593 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1594 var_Set( p_sys->p_input, "position", val );
1595 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1598 case POSITION_REL_BACK:
1600 var_Get( p_sys->p_input, "position", &val );
1601 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1602 var_Set( p_sys->p_input, "position", val );
1603 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
1608 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1613 #undef POSITION_ABSOLUTE
1614 #undef POSITION_REL_FOR
1615 #undef POSITION_REL_BACK
1616 #undef TIME_ABSOLUTE
1618 #undef TIME_REL_BACK
1624 audio_volume_t i_volume;
1627 uri_extract_value( p_request, "value", vol, 8 );
1628 aout_VolumeGet( p_intf, &i_volume );
1629 uri_decode_url_encoded( vol );
1633 i_value = atoi( vol + 1 );
1634 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1636 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1637 msg_Dbg( p_intf, "requested volume set: max" );
1640 aout_VolumeSet( p_intf , (i_volume + i_value) );
1641 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1646 i_value = atoi( vol + 1 );
1647 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1649 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1650 msg_Dbg( p_intf, "requested volume set: min" );
1653 aout_VolumeSet( p_intf , (i_volume - i_value) );
1654 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1658 i_value = atoi( vol );
1659 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1661 aout_VolumeSet( p_intf , atoi( vol ) );
1662 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1668 /* playlist management */
1672 playlist_item_t * p_item;
1674 uri_extract_value( p_request, "mrl", mrl, 512 );
1675 uri_decode_url_encoded( mrl );
1676 p_item = parse_MRL( p_intf, mrl );
1678 if( !p_item || !p_item->psz_uri || !*p_item->psz_uri )
1680 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1683 playlist_AddItem( p_sys->p_playlist , p_item ,
1684 PLAYLIST_APPEND, PLAYLIST_END );
1685 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1692 int i_item, *p_items = NULL, i_nb_items = 0;
1693 char item[512], *p_parser = p_request;
1695 /* Get the list of items to delete */
1697 uri_extract_value( p_parser, "item", item, 512 )) )
1699 if( !*item ) continue;
1701 i_item = atoi( item );
1702 p_items = realloc( p_items, (i_nb_items + 1) *
1704 p_items[i_nb_items] = i_item;
1708 /* The items need to be deleted from in reversed order */
1712 for( i = 0; i < i_nb_items; i++ )
1715 for( j = 0; j < i_nb_items; j++ )
1717 if( p_items[j] > p_items[i_index] )
1721 playlist_Delete( p_sys->p_playlist,
1723 msg_Dbg( p_intf, "requested playlist delete: %d",
1725 p_items[i_index] = -1;
1729 if( p_items ) free( p_items );
1734 int i_item, *p_items = NULL, i_nb_items = 0;
1735 char item[512], *p_parser = p_request;
1738 /* Get the list of items to keep */
1740 uri_extract_value( p_parser, "item", item, 512 )) )
1742 if( !*item ) continue;
1744 i_item = atoi( item );
1745 p_items = realloc( p_items, (i_nb_items + 1) *
1747 p_items[i_nb_items] = i_item;
1751 /* The items need to be deleted from in reversed order */
1752 for( i = p_sys->p_playlist->i_size - 1; i >= 0 ; i-- )
1754 /* Check if the item is in the keep list */
1755 for( j = 0 ; j < i_nb_items ; j++ )
1757 if( p_items[j] == i ) break;
1759 if( j == i_nb_items )
1761 playlist_Delete( p_sys->p_playlist, i );
1762 msg_Dbg( p_intf, "requested playlist delete: %d",
1767 if( p_items ) free( p_items );
1772 while( p_sys->p_playlist->i_size > 0 )
1774 playlist_Delete( p_sys->p_playlist, 0 );
1776 msg_Dbg( p_intf, "requested playlist empty" );
1785 uri_extract_value( p_request, "type", type, 12 );
1786 uri_extract_value( p_request, "order", order, 2 );
1788 if( order[0] == '0' ) i_order = ORDER_NORMAL;
1789 else i_order = ORDER_REVERSE;
1791 if( !strcmp( type , "title" ) )
1793 playlist_SortTitle( p_sys->p_playlist , i_order );
1794 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
1795 } else if( !strcmp( type , "group" ) )
1797 playlist_SortGroup( p_sys->p_playlist , i_order );
1798 msg_Dbg( p_intf, "requested playlist sort by group (%d)" , i_order );
1799 } else if( !strcmp( type , "author" ) )
1801 playlist_SortAuthor( p_sys->p_playlist , i_order );
1802 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
1803 } else if( !strcmp( type , "shuffle" ) )
1805 playlist_Sort( p_sys->p_playlist , SORT_RANDOM, ORDER_NORMAL );
1806 msg_Dbg( p_intf, "requested playlist shuffle");
1817 uri_extract_value( p_request, "psz_pos", psz_pos, 6 );
1818 uri_extract_value( p_request, "psz_newpos", psz_newpos, 6 );
1819 i_pos = atoi( psz_pos );
1820 i_newpos = atoi( psz_newpos );
1821 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
1822 msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
1826 /* admin function */
1830 uri_extract_value( p_request, "id", id, 512 );
1831 msg_Dbg( p_intf, "requested close id=%s", id );
1833 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
1835 msg_Warn( p_intf, "close failed for id=%s", id );
1842 msg_Dbg( p_intf, "requested shutdown" );
1843 p_intf->p_vlc->b_die = VLC_TRUE;
1847 PRINTS( "<!-- control param(%s) unsuported -->", control );
1858 if( i_request <= 0 ||
1859 *m->param1 == '\0' ||
1860 strstr( p_request, m->param1 ) == NULL )
1864 uri_extract_value( p_request, m->param1, value, 512 );
1865 uri_decode_url_encoded( value );
1867 switch( StrToMacroType( m->param2 ) )
1871 config_PutInt( p_intf, m->param1, i );
1875 config_PutFloat( p_intf, m->param1, f );
1878 config_PutPsz( p_intf, m->param1, value );
1881 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
1892 if( *m->param1 == '\0' )
1897 switch( StrToMacroType( m->param2 ) )
1900 i = config_GetInt( p_intf, m->param1 );
1901 sprintf( value, "%i", i );
1904 f = config_GetFloat( p_intf, m->param1 );
1905 sprintf( value, "%f", f );
1908 psz = config_GetPsz( p_intf, m->param1 );
1909 sprintf( value, "%s", psz ? psz : "" );
1910 if( psz ) free( psz );
1913 sprintf( value, "invalid type(%s) in set", m->param2 );
1916 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
1917 PRINTS( "%s", value );
1926 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1927 s = SSPop( &p_args->stack );
1928 v = mvar_GetValue( p_args->vars, s );
1932 v = s = SSPop( &p_args->stack );
1940 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1944 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
1945 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
1953 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
1960 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
1965 i_skip = MacroParse( &m, src );
1967 i_id = StrToMacroType( m.id );
1984 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
1985 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
1987 return src + ( b_after ? i_skip : 0 );
1989 else if( i_level < 0 )
2005 static void Execute( httpd_file_sys_t *p_args,
2006 uint8_t *p_request, int i_request,
2007 uint8_t **pp_data, int *pi_data,
2009 uint8_t *_src, uint8_t *_end )
2011 intf_thread_t *p_intf = p_args->p_intf;
2013 uint8_t *src, *dup, *end;
2014 uint8_t *dst = *pp_dst;
2016 src = dup = malloc( _end - _src + 1 );
2017 end = src +( _end - _src );
2019 memcpy( src, _src, _end - _src );
2022 /* we parse searching <vlc */
2028 p = strstr( src, "<vlc" );
2029 if( p < end && p == src )
2033 src += MacroParse( &m, src );
2035 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2037 switch( StrToMacroType( m.id ) )
2044 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2045 if( SSPopN( &p_args->stack, p_args->vars ) )
2053 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2057 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2061 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2064 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2068 else if( i_test == 1 )
2071 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2073 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2077 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2086 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2087 uint8_t *start = src;
2088 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2095 if( !strcmp( m.param2, "integer" ) )
2097 char *arg = SSPop( &p_args->stack );
2098 index = mvar_IntegerSetNew( m.param1, arg );
2101 else if( !strcmp( m.param2, "directory" ) )
2103 char *arg = SSPop( &p_args->stack );
2104 index = mvar_FileSetNew( m.param1, arg );
2107 else if( !strcmp( m.param2, "playlist" ) )
2109 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2111 else if( !strcmp( m.param2, "informations" ) )
2113 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2116 else if( !strcmp( m.param2, "hosts" ) )
2118 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2120 else if( !strcmp( m.param2, "urls" ) )
2122 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2124 else if( !strcmp( m.param2, "connections" ) )
2126 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2129 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2131 index = mvar_Duplicate( v );
2135 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2140 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2142 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2144 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2147 f->name = strdup( m.param1 );
2150 mvar_PushVar( p_args->vars, f );
2151 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2152 mvar_RemoveVar( p_args->vars, f );
2156 mvar_Delete( index );
2163 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2171 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2174 int i_index = dst - *pp_data;
2177 *pp_data = realloc( *pp_data, *pi_data );
2178 dst = (*pp_data) + i_index;
2180 memcpy( dst, src, i_copy );
2190 /****************************************************************************
2192 ****************************************************************************
2193 * a file with b_html is parsed and all "macro" replaced
2194 * <vlc id="macro name" [param1="" [param2=""]] />
2197 ****************************************************************************/
2198 static int HttpCallback( httpd_file_sys_t *p_args,
2199 httpd_file_t *p_file,
2201 uint8_t **pp_data, int *pi_data )
2203 int i_request = p_request ? strlen( p_request ) : 0;
2207 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2209 p = *pp_data = malloc( 10240 );
2212 return VLC_EGENERIC;
2214 p += sprintf( p, "<html>\n" );
2215 p += sprintf( p, "<head>\n" );
2216 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2217 p += sprintf( p, "</head>\n" );
2218 p += sprintf( p, "<body>\n" );
2219 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2220 p += sprintf( p, "<hr />\n" );
2221 p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
2222 p += sprintf( p, "</body>\n" );
2223 p += sprintf( p, "</html>\n" );
2225 *pi_data = strlen( *pp_data );
2230 if( !p_args->b_html )
2232 FileLoad( f, pp_data, pi_data );
2240 char position[4]; /* percentage */
2241 char time[12]; /* in seconds */
2242 char length[12]; /* in seconds */
2243 audio_volume_t i_volume;
2247 #define p_sys p_args->p_intf->p_sys
2248 if( p_sys->p_input )
2250 var_Get( p_sys->p_input, "position", &val);
2251 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2252 var_Get( p_sys->p_input, "time", &val);
2253 sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2254 var_Get( p_sys->p_input, "length", &val);
2255 sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2257 var_Get( p_sys->p_input, "state", &val );
2258 if( val.i_int == PLAYING_S )
2260 sprintf( state, "playing" );
2261 } else if( val.i_int == PAUSE_S )
2263 sprintf( state, "paused" );
2266 sprintf( state, "stop" );
2270 sprintf( position, "%d", 0 );
2271 sprintf( time, "%d", 0 );
2272 sprintf( length, "%d", 0 );
2273 sprintf( state, "stop" );
2277 aout_VolumeGet( p_args->p_intf , &i_volume );
2278 sprintf( volume , "%d" , (int)i_volume );
2280 p_args->vars = mvar_New( "variables", "" );
2281 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2282 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2283 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
2284 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2285 mvar_AppendNewVar( p_args->vars, "stream_position", position );
2286 mvar_AppendNewVar( p_args->vars, "stream_time", time );
2287 mvar_AppendNewVar( p_args->vars, "stream_length", length );
2288 mvar_AppendNewVar( p_args->vars, "volume", volume );
2289 mvar_AppendNewVar( p_args->vars, "stream_state", state );
2291 SSInit( &p_args->stack );
2293 /* first we load in a temporary buffer */
2294 FileLoad( f, &p_buffer, &i_buffer );
2296 /* allocate output */
2297 *pi_data = i_buffer + 1000;
2298 dst = *pp_data = malloc( *pi_data );
2300 /* we parse executing all <vlc /> macros */
2301 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2304 *pi_data = dst - *pp_data;
2306 SSClean( &p_args->stack );
2307 mvar_Delete( p_args->vars );
2316 /****************************************************************************
2318 ****************************************************************************/
2319 static char *uri_extract_value( char *psz_uri, char *psz_name,
2320 char *psz_value, int i_value_max )
2324 while( (p = strstr( p, psz_name )) )
2326 /* Verify that we are dealing with a post/get argument */
2327 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2336 p += strlen( psz_name );
2337 if( *p == '=' ) p++;
2339 if( strchr( p, '&' ) )
2341 i_len = strchr( p, '&' ) - p;
2345 /* for POST method */
2346 if( strchr( p, '\n' ) )
2348 i_len = strchr( p, '\n' ) - p;
2349 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2353 i_len = strlen( p );
2356 i_len = __MIN( i_value_max - 1, i_len );
2359 strncpy( psz_value, p, i_len );
2360 psz_value[i_len] = '\0';
2364 strncpy( psz_value, "", i_value_max );
2370 strncpy( psz_value, "", i_value_max );
2376 static void uri_decode_url_encoded( char *psz )
2378 char *dup = strdup( psz );
2396 *psz++ = strtol( val, NULL, 16 );
2398 else if( *p == '+' )
2412 /****************************************************************************
2413 * Light RPN evaluator
2414 ****************************************************************************/
2415 static void SSInit( rpn_stack_t *st )
2420 static void SSClean( rpn_stack_t *st )
2422 while( st->i_stack > 0 )
2424 free( st->stack[--st->i_stack] );
2428 static void SSPush( rpn_stack_t *st, char *s )
2430 if( st->i_stack < STACK_MAX )
2432 st->stack[st->i_stack++] = strdup( s );
2436 static char * SSPop( rpn_stack_t *st )
2438 if( st->i_stack <= 0 )
2440 return strdup( "" );
2444 return st->stack[--st->i_stack];
2448 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2457 i = strtol( name, &end, 0 );
2460 value = mvar_GetValue( vars, name );
2468 static void SSPushN( rpn_stack_t *st, int i )
2472 sprintf( v, "%d", i );
2476 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2483 while( *exp == ' ' )
2490 /* extract string */
2493 while( *exp && *exp != '\'' )
2504 p = strchr( exp, ' ' );
2509 exp += strlen( exp );
2514 strncpy( s, exp, i );
2525 /* 1. Integer function */
2526 if( !strcmp( s, "!" ) )
2528 SSPushN( st, ~SSPopN( st, vars ) );
2530 else if( !strcmp( s, "^" ) )
2532 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2534 else if( !strcmp( s, "&" ) )
2536 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2538 else if( !strcmp( s, "|" ) )
2540 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2542 else if( !strcmp( s, "+" ) )
2544 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2546 else if( !strcmp( s, "-" ) )
2548 int j = SSPopN( st, vars );
2549 int i = SSPopN( st, vars );
2550 SSPushN( st, i - j );
2552 else if( !strcmp( s, "*" ) )
2554 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2556 else if( !strcmp( s, "/" ) )
2560 j = SSPopN( st, vars );
2561 i = SSPopN( st, vars );
2563 SSPushN( st, j != 0 ? i / j : 0 );
2565 else if( !strcmp( s, "%" ) )
2569 j = SSPopN( st, vars );
2570 i = SSPopN( st, vars );
2572 SSPushN( st, j != 0 ? i % j : 0 );
2574 /* 2. integer tests */
2575 else if( !strcmp( s, "=" ) )
2577 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2579 else if( !strcmp( s, "<" ) )
2581 int j = SSPopN( st, vars );
2582 int i = SSPopN( st, vars );
2584 SSPushN( st, i < j ? -1 : 0 );
2586 else if( !strcmp( s, ">" ) )
2588 int j = SSPopN( st, vars );
2589 int i = SSPopN( st, vars );
2591 SSPushN( st, i > j ? -1 : 0 );
2593 else if( !strcmp( s, "<=" ) )
2595 int j = SSPopN( st, vars );
2596 int i = SSPopN( st, vars );
2598 SSPushN( st, i <= j ? -1 : 0 );
2600 else if( !strcmp( s, ">=" ) )
2602 int j = SSPopN( st, vars );
2603 int i = SSPopN( st, vars );
2605 SSPushN( st, i >= j ? -1 : 0 );
2607 /* 3. string functions */
2608 else if( !strcmp( s, "strcat" ) )
2610 char *s2 = SSPop( st );
2611 char *s1 = SSPop( st );
2612 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2622 else if( !strcmp( s, "strcmp" ) )
2624 char *s2 = SSPop( st );
2625 char *s1 = SSPop( st );
2627 SSPushN( st, strcmp( s1, s2 ) );
2631 else if( !strcmp( s, "strncmp" ) )
2633 int n = SSPopN( st, vars );
2634 char *s2 = SSPop( st );
2635 char *s1 = SSPop( st );
2637 SSPushN( st, strncmp( s1, s2 , n ) );
2641 else if( !strcmp( s, "strsub" ) )
2643 int n = SSPopN( st, vars );
2644 int m = SSPopN( st, vars );
2646 char *s = SSPop( st );
2658 str = malloc( i_len + 1 );
2660 memcpy( str, s + m - 1, i_len );
2661 str[ i_len ] = '\0';
2667 else if( !strcmp( s, "strlen" ) )
2669 char *str = SSPop( st );
2671 SSPushN( st, strlen( str ) );
2674 /* 4. stack functions */
2675 else if( !strcmp( s, "dup" ) )
2677 char *str = SSPop( st );
2682 else if( !strcmp( s, "drop" ) )
2684 char *str = SSPop( st );
2687 else if( !strcmp( s, "swap" ) )
2689 char *s1 = SSPop( st );
2690 char *s2 = SSPop( st );
2697 else if( !strcmp( s, "flush" ) )
2702 else if( !strcmp( s, "store" ) )
2704 char *value = SSPop( st );
2705 char *name = SSPop( st );
2707 mvar_PushNewVar( vars, name, value );
2711 else if( !strcmp( s, "value" ) )
2713 char *name = SSPop( st );
2714 char *value = mvar_GetValue( vars, name );
2716 SSPush( st, value );
2720 else if( !strcmp( s, "url_extract" ) )
2722 char *url = mvar_GetValue( vars, "url_value" );
2723 char *name = SSPop( st );
2726 uri_extract_value( url, name, value, 512 );
2727 uri_decode_url_encoded( value );
2728 SSPush( st, value );
2737 /**********************************************************************
2738 * Find_end_MRL: Find the end of the sentence :
2739 * this function parses the string psz and find the end of the item
2740 * and/or option with detecting the " and ' problems.
2741 * returns NULL if an error is detected, otherwise, returns a pointer
2742 * of the end of the sentence (after the last character)
2743 **********************************************************************/
2744 static char *Find_end_MRL( char *psz )
2754 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
2756 if( *s_sent == '\'' )
2758 s_sent = Find_end_MRL( s_sent );
2760 if( s_sent == NULL )
2770 if( *s_sent == '\"' )
2774 } else /* *s_sent == '\0' , which means the number of " is incorrect */
2784 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
2786 if( *s_sent == '\"' )
2788 s_sent = Find_end_MRL( s_sent );
2790 if( s_sent == NULL )
2800 if( *s_sent == '\'' )
2804 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
2810 default: /* now we can look for spaces */
2812 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
2814 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
2816 s_sent = Find_end_MRL( s_sent );
2827 /**********************************************************************
2828 * parse_MRL: parse the MRL, find the mrl string and the options,
2829 * create an item with all informations in it, and return the item.
2830 * return NULL if there is an error.
2831 **********************************************************************/
2832 playlist_item_t * parse_MRL( intf_thread_t *p_intf, char *psz )
2834 char **ppsz_options = NULL;
2841 playlist_item_t * p_item = NULL;
2843 /* In case there is spaces before the mrl */
2844 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
2849 /* extract the mrl */
2850 s_temp = strstr( s_mrl , " :" );
2851 if( s_temp == NULL )
2853 s_temp = s_mrl + strlen( s_mrl );
2856 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
2863 /* if the mrl is between " or ', we must remove them */
2864 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
2866 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
2867 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
2868 mrl[ s_temp - s_mrl - 2 ] = '\0';
2871 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
2872 strncpy( mrl , s_mrl , s_temp - s_mrl );
2873 mrl[ s_temp - s_mrl ] = '\0';
2878 /* now we can take care of the options */
2879 while( (*s_mrl != '\0') && (i_error == 0) )
2888 case ':': /* an option */
2890 s_temp = Find_end_MRL( s_mrl );
2892 if( s_temp == NULL )
2899 ppsz_options = realloc( ppsz_options , i_options *
2901 ppsz_options[ i_options - 1 ] =
2902 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
2904 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
2907 /* don't forget to finish the string with a '\0' */
2908 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
2928 /* now create an item */
2929 p_item = playlist_ItemNew( p_intf, mrl, mrl);
2930 for( i = 0 ; i< i_options ; i++ )
2932 playlist_ItemAddOption( p_item, ppsz_options[i] );
2936 for( i = 0 ; i < i_options ; i++ )
2938 free( ppsz_options[i] );
2940 free( ppsz_options );