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 *****************************************************************************/
38 #include <vlc/vout.h> /* for fullscreen */
40 #include "vlc_httpd.h"
44 #ifdef HAVE_SYS_STAT_H
45 # include <sys/stat.h>
56 #elif defined( WIN32 ) && !defined( UNDER_CE )
60 #if (!defined( WIN32 ) || defined(__MINGW32__))
61 /* Mingw has its own version of dirent */
65 /* stat() support for large files on win32 */
66 #if defined( WIN32 ) && !defined( UNDER_CE )
67 # define stat _stati64
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Open ( vlc_object_t * );
74 static void Close( vlc_object_t * );
76 #define HOST_TEXT N_( "Host address" )
77 #define HOST_LONGTEXT N_( \
78 "You can set the address and port the http interface will bind to." )
79 #define SRC_TEXT N_( "Source directory" )
80 #define SRC_LONGTEXT N_( "Source directory" )
81 #define CERT_TEXT N_( "Certificate file" )
82 #define CERT_LONGTEXT N_( "x509 PEM certificates path file" )
83 #define KEY_TEXT N_( "Private key file" )
84 #define KEY_LONGTEXT N_( "x509 PEM private key file" )
85 #define CA_TEXT N_( "Root CA file" )
86 #define CA_LONGTEXT N_( "x509 PEM trusted root CA certificates file" )
87 #define CRL_TEXT N_( "CRL file" )
88 #define CRL_LONGTEXT N_( "Certificates revocation list file" )
91 set_description( _("HTTP remote control interface") );
92 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
93 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
94 add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
95 add_string ( "http-intf-key", NULL, NULL, KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
96 add_string ( "http-intf-ca", NULL, NULL, CA_TEXT, CA_LONGTEXT, VLC_TRUE );
97 add_string ( "http-intf-crl", NULL, NULL, CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
98 set_capability( "interface", 0 );
99 set_callbacks( Open, Close );
103 /*****************************************************************************
105 *****************************************************************************/
106 static void Run ( intf_thread_t *p_intf );
108 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
111 static int DirectoryCheck( char *psz_dir )
115 #ifdef HAVE_SYS_STAT_H
116 struct stat stat_info;
118 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
124 if( ( p_dir = opendir( psz_dir ) ) == NULL )
133 static int HttpCallback( httpd_file_sys_t *p_args,
136 uint8_t **pp_data, int *pi_data );
138 static char *uri_extract_value( char *psz_uri, const char *psz_name,
139 char *psz_value, int i_value_max );
140 static int uri_test_param( char *psz_uri, const char *psz_name );
142 static void uri_decode_url_encoded( char *psz );
144 static char *Find_end_MRL( char *psz );
146 static playlist_item_t * parse_MRL( intf_thread_t * , char *psz );
148 /*****************************************************************************
150 *****************************************************************************/
151 typedef struct mvar_s
157 struct mvar_s **field;
160 #define STACK_MAX 100
163 char *stack[STACK_MAX];
167 struct httpd_file_sys_t
169 intf_thread_t *p_intf;
170 httpd_file_t *p_file;
171 httpd_redirect_t *p_redir;
178 /* inited for each access */
185 httpd_host_t *p_httpd_host;
188 httpd_file_sys_t **pp_files;
190 playlist_t *p_playlist;
191 input_thread_t *p_input;
197 /*****************************************************************************
198 * Activate: initialize and create stuff
199 *****************************************************************************/
200 static int Open( vlc_object_t *p_this )
202 intf_thread_t *p_intf = (intf_thread_t*)p_this;
205 char *psz_address = "";
206 const char *psz_cert;
211 psz_host = config_GetPsz( p_intf, "http-host" );
215 psz_address = psz_host;
217 psz_parser = strchr( psz_host, ':' );
220 *psz_parser++ = '\0';
221 i_port = atoi( psz_parser );
225 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
228 return( VLC_ENOMEM );
230 p_sys->p_playlist = NULL;
231 p_sys->p_input = NULL;
234 /* TODO: avoid possible code duplication in other modules */
235 psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
236 if ( psz_cert != NULL )
239 msg_Dbg( p_intf, "enablind TLS for HTTP interface (cert file: %s)",
241 psz_pem = config_GetPsz( p_intf, "http-intf-key" );
242 if ( psz_pem == NULL )
245 p_tls = tls_ServerCreate( VLC_OBJECT(p_intf), psz_cert, psz_pem );
248 msg_Err( p_intf, "TLS initialization error" );
253 psz_pem = config_GetPsz( p_intf, "http-intf-ca" );
254 if ( ( psz_pem != NULL) && tls_ServerAddCA( p_tls, psz_pem ) )
256 msg_Err( p_intf, "TLS CA error" );
257 tls_ServerDelete( p_tls );
262 psz_pem = config_GetPsz( p_intf, "http-intf-crl" );
263 if ( ( psz_pem != NULL) && tls_ServerAddCRL( p_tls, psz_pem ) )
265 msg_Err( p_intf, "TLS CRL error" );
266 tls_ServerDelete( p_tls );
281 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
283 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_intf), psz_address,
285 if( p_sys->p_httpd_host == NULL )
287 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
289 tls_ServerDelete( p_tls );
300 p_sys->pp_files = NULL;
302 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
303 ( defined(WIN32) && !defined(UNDER_CE ) )
304 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
306 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
307 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
313 sprintf( psz_src, "%s/http", psz_vlcpath);
315 sprintf( psz_src, "%s/share/http", psz_vlcpath);
319 psz_src = config_GetPsz( p_intf, "http-src" );
321 if( !psz_src || *psz_src == '\0' )
323 if( !DirectoryCheck( "share/http" ) )
325 psz_src = strdup( "share/http" );
327 else if( !DirectoryCheck( DATA_PATH "/http" ) )
329 psz_src = strdup( DATA_PATH "/http" );
334 if( !psz_src || *psz_src == '\0' )
336 msg_Err( p_intf, "invalid src dir" );
340 /* remove trainling \ or / */
341 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
342 psz_src[strlen( psz_src ) - 1] == '/' )
344 psz_src[strlen( psz_src ) - 1] = '\0';
347 ParseDirectory( p_intf, psz_src, psz_src );
350 if( p_sys->i_files <= 0 )
352 msg_Err( p_intf, "cannot find any files (%s)", psz_src );
355 p_intf->pf_run = Run;
361 if( psz_src ) free( psz_src );
362 if( p_sys->pp_files )
364 free( p_sys->pp_files );
366 httpd_HostDelete( p_sys->p_httpd_host );
371 /*****************************************************************************
372 * CloseIntf: destroy interface
373 *****************************************************************************/
374 void Close ( vlc_object_t *p_this )
376 intf_thread_t *p_intf = (intf_thread_t *)p_this;
377 intf_sys_t *p_sys = p_intf->p_sys;
383 vlm_Delete( p_sys->p_vlm );
386 for( i = 0; i < p_sys->i_files; i++ )
388 httpd_FileDelete( p_sys->pp_files[i]->p_file );
389 if( p_sys->pp_files[i]->p_redir )
391 httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
394 free( p_sys->pp_files[i]->file );
395 free( p_sys->pp_files[i]->name );
396 free( p_sys->pp_files[i] );
398 if( p_sys->pp_files )
400 free( p_sys->pp_files );
402 httpd_HostDelete( p_sys->p_httpd_host );
407 /*****************************************************************************
408 * Run: http interface thread
409 *****************************************************************************/
410 static void Run( intf_thread_t *p_intf )
412 intf_sys_t *p_sys = p_intf->p_sys;
414 while( !p_intf->b_die )
416 /* get the playlist */
417 if( p_sys->p_playlist == NULL )
419 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
422 /* Manage the input part */
423 if( p_sys->p_input == NULL )
425 if( p_sys->p_playlist )
428 vlc_object_find( p_sys->p_playlist,
433 else if( p_sys->p_input->b_dead )
435 vlc_object_release( p_sys->p_input );
436 p_sys->p_input = NULL;
441 msleep( INTF_IDLE_SLEEP );
446 vlc_object_release( p_sys->p_input );
447 p_sys->p_input = NULL;
450 if( p_sys->p_playlist )
452 vlc_object_release( p_sys->p_playlist );
453 p_sys->p_playlist = NULL;
458 /*****************************************************************************
460 *****************************************************************************/
461 #define MAX_DIR_SIZE 10240
463 /****************************************************************************
464 * FileToUrl: create a good name for an url from filename
465 ****************************************************************************/
466 static char *FileToUrl( char *name )
470 url = p = malloc( strlen( name ) + 1 );
478 while( *name == '\\' || *name == '/' )
480 while( *name == '/' )
490 /* convert '\\' into '/' */
503 if( ( p = strrchr( url, '/' ) ) != NULL )
505 if( !strncmp( p, "/index.", 7 ) )
513 /****************************************************************************
514 * ParseDirectory: parse recursively a directory, adding each file
515 ****************************************************************************/
516 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
519 intf_sys_t *p_sys = p_intf->p_sys;
520 char dir[MAX_DIR_SIZE];
521 #ifdef HAVE_SYS_STAT_H
522 struct stat stat_info;
525 struct dirent *p_dir_content;
529 char *password = NULL;
531 #ifdef HAVE_SYS_STAT_H
532 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
538 if( ( p_dir = opendir( psz_dir ) ) == NULL )
540 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
544 msg_Dbg( p_intf, "dir=%s", psz_dir );
546 sprintf( dir, "%s/.access", psz_dir );
547 if( ( file = fopen( dir, "r" ) ) != NULL )
552 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
554 i_size = fread( line, 1, 1023, file );
558 while( i_size > 0 && ( line[i_size-1] == '\n' ||
559 line[i_size-1] == '\r' ) )
566 p = strchr( line, ':' );
570 user = strdup( line );
571 password = strdup( p );
574 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
575 user, password, i_size );
582 /* parse psz_src dir */
583 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
588 if( p_dir_content->d_name[0] == '.' )
592 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
593 if( ParseDirectory( p_intf, psz_root, dir ) )
595 httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
600 f->file = strdup( dir );
601 f->name = FileToUrl( &dir[strlen( psz_root )] );
602 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
606 msg_Err( p_intf , "unable to parse directory" );
609 return( VLC_ENOMEM );
611 msg_Dbg( p_intf, "file=%s (url=%s)",
614 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
615 f->name, f->b_html ? "text/html" : NULL,
621 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
623 /* For rep/ add a redir from rep to rep/ */
624 if( f && f->name[strlen(f->name) - 1] == '/' )
626 char *psz_redir = strdup( f->name );
627 psz_redir[strlen( psz_redir ) - 1] = '\0';
629 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
630 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
650 /****************************************************************************
651 * var and set handling
652 ****************************************************************************/
654 static mvar_t *mvar_New( char *name, char *value )
656 mvar_t *v = malloc( sizeof( mvar_t ) );
658 if( !v ) return NULL;
659 v->name = strdup( name );
660 v->value = strdup( value ? value : "" );
663 v->field = malloc( sizeof( mvar_t * ) );
669 static void mvar_Delete( mvar_t *v )
676 for( i = 0; i < v->i_field; i++ )
678 mvar_Delete( v->field[i] );
684 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
686 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
687 v->field[v->i_field] = f;
691 static mvar_t *mvar_Duplicate( mvar_t *v )
696 n = mvar_New( v->name, v->value );
697 for( i = 0; i < v->i_field; i++ )
699 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
705 static void mvar_PushVar( mvar_t *v, mvar_t *f )
707 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
710 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
716 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
719 for( i = 0; i < v->i_field; i++ )
721 if( v->field[i] == f )
726 if( i >= v->i_field )
731 if( i + 1 < v->i_field )
733 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
736 /* FIXME should do a realloc */
739 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
742 char base[512], *field, *p;
745 /* format: name[index].field */
747 field = strchr( name, '.' );
750 int i = field - name;
751 strncpy( base, name, i );
757 strcpy( base, name );
760 if( ( p = strchr( base, '[' ) ) )
763 sscanf( p, "%d]", &i_index );
774 for( i = 0; i < s->i_field; i++ )
776 if( !strcmp( s->field[i]->name, base ) )
786 return mvar_GetVar( s->field[i], field );
800 static char *mvar_GetValue( mvar_t *v, char *field )
808 mvar_t *f = mvar_GetVar( v, field );
820 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
822 mvar_t *f = mvar_New( name, value );
823 mvar_PushVar( vars, f );
826 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
828 mvar_t *f = mvar_New( name, value );
829 mvar_AppendVar( vars, f );
833 /* arg= start[:stop[:step]],.. */
834 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
836 char *dup = strdup( arg );
838 mvar_t *s = mvar_New( name, "set" );
840 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
845 int i_start,i_stop,i_step;
848 p = strchr( str, ',' );
855 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
856 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
863 else if( i_match == 2 )
865 i_step = i_start < i_stop ? 1 : -1;
872 if( ( i_start < i_stop && i_step > 0 ) ||
873 ( i_start > i_stop && i_step < 0 ) )
875 for( i = i_start; ; i += i_step )
879 if( ( i_step > 0 && i > i_stop ) ||
880 ( i_step < 0 && i < i_stop ) )
885 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
886 sprintf( value, "%d", i );
888 mvar_PushNewVar( s, name, value );
899 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
901 mvar_t *s = mvar_New( name, "set" );
904 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
906 vlc_mutex_lock( &p_pl->object_lock );
907 for( i = 0; i < p_pl->i_size; i++ )
909 mvar_t *itm = mvar_New( name, "set" );
912 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
913 mvar_AppendNewVar( itm, "current", value );
915 sprintf( value, "%d", i );
916 mvar_AppendNewVar( itm, "index", value );
918 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->input.psz_name );
920 mvar_AppendNewVar( itm, "uri", p_pl->pp_items[i]->input.psz_uri );
922 sprintf( value, "%d", p_pl->pp_items[i]->i_group );
923 mvar_AppendNewVar( itm, "group", value );
925 mvar_AppendVar( s, itm );
927 vlc_mutex_unlock( &p_pl->object_lock );
932 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
934 mvar_t *s = mvar_New( name, "set" );
937 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
938 if( p_input == NULL )
943 vlc_mutex_lock( &p_input->input.p_item->lock );
944 for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
946 info_category_t *p_category = p_input->input.p_item->pp_categories[i];
947 mvar_t *cat = mvar_New( name, "set" );
948 mvar_t *iset = mvar_New( "info", "set" );
950 mvar_AppendNewVar( cat, "name", p_category->psz_name );
951 mvar_AppendVar( cat, iset );
953 for ( j = 0; j < p_category->i_infos; j++ )
955 info_t *p_info = p_category->pp_infos[j];
956 mvar_t *info = mvar_New( "info", "" );
958 msg_Dbg( p_input, "adding info name=%s value=%s",
959 p_info->psz_name, p_info->psz_value );
960 mvar_AppendNewVar( info, "name", p_info->psz_name );
961 mvar_AppendNewVar( info, "value", p_info->psz_value );
962 mvar_AppendVar( iset, info );
964 mvar_AppendVar( s, cat );
966 vlc_mutex_unlock( &p_input->input.p_item->lock );
971 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
973 mvar_t *s = mvar_New( name, "set" );
977 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
978 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
980 for( i= 0; i < info.i_count; )
984 inf = mvar_New( name, "set" );
987 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
988 info.info[i].psz_name, info.info[i].psz_value ); */
989 mvar_AppendNewVar( inf,
990 info.info[i].psz_name,
991 info.info[i].psz_value );
993 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
994 mvar_AppendVar( s, inf );
999 for( i = 0; i < info.i_count; i++ )
1001 free( info.info[i].psz_name );
1002 free( info.info[i].psz_value );
1004 if( info.i_count > 0 )
1013 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1015 mvar_t *s = mvar_New( name, "set" );
1016 char tmp[MAX_DIR_SIZE], *p, *src;
1017 #ifdef HAVE_SYS_STAT_H
1018 struct stat stat_info;
1021 struct dirent *p_dir_content;
1024 /* convert all / to native separator */
1025 #if defined( WIN32 )
1026 while( (p = strchr( psz_dir, '/' )) )
1035 /* remove trailling separator */
1036 while( strlen( psz_dir ) > 1 &&
1037 #if defined( WIN32 )
1038 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1040 psz_dir[strlen( psz_dir ) -1 ] == sep )
1042 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1044 /* remove double separator */
1045 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1047 if( src[0] == sep && src[1] == sep )
1055 if( *psz_dir == '\0' )
1059 /* first fix all .. dir */
1063 if( src[0] == '.' && src[1] == '.' )
1066 if( p <= &psz_dir[1] )
1073 while( p > &psz_dir[1] && *p != sep )
1078 else if( *src == sep )
1080 if( p > psz_dir && p[-1] == sep )
1094 } while( *src && *src != sep );
1099 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1101 #ifdef HAVE_SYS_STAT_H
1102 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1108 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1110 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1114 /* remove traling / or \ */
1115 for( p = &psz_dir[strlen( psz_dir) - 1];
1116 p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1125 /* parse psz_src dir */
1126 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1130 if( !strcmp( p_dir_content->d_name, "." ) )
1135 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1137 #ifdef HAVE_SYS_STAT_H
1138 if( stat( tmp, &stat_info ) == -1 )
1143 f = mvar_New( name, "set" );
1144 mvar_AppendNewVar( f, "name", tmp );
1146 #ifdef HAVE_SYS_STAT_H
1147 if( S_ISDIR( stat_info.st_mode ) )
1149 mvar_AppendNewVar( f, "type", "directory" );
1151 else if( S_ISREG( stat_info.st_mode ) )
1153 mvar_AppendNewVar( f, "type", "file" );
1157 mvar_AppendNewVar( f, "type", "unknown" );
1160 sprintf( tmp, I64Fd, (int64_t)stat_info.st_size );
1161 mvar_AppendNewVar( f, "size", tmp );
1163 /* FIXME memory leak FIXME */
1165 ctime_r( &stat_info.st_mtime, tmp );
1166 mvar_AppendNewVar( f, "date", tmp );
1168 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1172 mvar_AppendNewVar( f, "type", "unknown" );
1173 mvar_AppendNewVar( f, "size", "unknown" );
1174 mvar_AppendNewVar( f, "date", "unknown" );
1176 mvar_AppendVar( s, f );
1182 static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
1184 mvar_t *s = mvar_New( name, "set" );
1188 /* fprintf( stderr," mvar_VlmSetNew: name=`%s'\n", name ); */
1189 if( vlm == NULL ) return s;
1191 if( vlm_ExecuteCommand( vlm, "show", &msg ) )
1196 for( i = 0; i < msg->i_child; i++ )
1198 /* Over media, schedule */
1199 vlm_message_t *ch = msg->child[i];
1202 for( j = 0; j < ch->i_child; j++ )
1205 vlm_message_t *el = ch->child[j];
1206 vlm_message_t *inf, *desc;
1211 sprintf( psz, "show %s", el->psz_name );
1212 if( vlm_ExecuteCommand( vlm, psz, &inf ) )
1214 desc = inf->child[0];
1216 /* Add a node with name and info */
1217 set = mvar_New( name, "set" );
1218 mvar_AppendNewVar( set, "name", el->psz_name );
1220 /* fprintf( stderr, "#### name=%s\n", el->psz_name ); */
1222 for( k = 0; k < desc->i_child; k++ )
1224 vlm_message_t *ch = desc->child[k];
1225 if( ch->i_child > 0 )
1228 mvar_t *n = mvar_New( ch->psz_name, "set" );
1230 /* fprintf( stderr, " child=%s [%d]\n", ch->psz_name, ch->i_child ); */
1231 for( c = 0; c < ch->i_child; c++ )
1233 mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
1234 mvar_AppendVar( n, in );
1236 /* fprintf( stderr, " sub=%s\n", ch->child[c]->psz_name );*/
1238 mvar_AppendVar( set, n );
1242 /* fprintf( stderr, " child=%s->%s\n", ch->psz_name, ch->psz_value ); */
1243 mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
1246 vlm_MessageDelete( inf );
1248 mvar_AppendVar( s, set );
1251 vlm_MessageDelete( msg );
1257 static void SSInit( rpn_stack_t * );
1258 static void SSClean( rpn_stack_t * );
1259 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1261 static void SSPush ( rpn_stack_t *, char * );
1262 static char *SSPop ( rpn_stack_t * );
1264 static void SSPushN ( rpn_stack_t *, int );
1265 static int SSPopN ( rpn_stack_t *, mvar_t * );
1268 /****************************************************************************
1270 ****************************************************************************/
1278 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1282 /* just load the file */
1284 *pp_data = malloc( 1025 ); /* +1 for \0 */
1285 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1288 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1294 (*pp_data)[*pi_data] = '\0';
1299 static int MacroParse( macro_t *m, uint8_t *psz_src )
1301 uint8_t *dup = strdup( psz_src );
1306 #define EXTRACT( name, l ) \
1308 p = strchr( src, '"' ); \
1313 m->name = strdup( src ); \
1334 if( !strncmp( src, "id=\"", 4 ) )
1338 else if( !strncmp( src, "param1=\"", 8 ) )
1340 EXTRACT( param1, 8 );
1342 else if( !strncmp( src, "param2=\"", 8 ) )
1344 EXTRACT( param2, 8 );
1351 if( strstr( src, "/>" ) )
1353 src = strstr( src, "/>" ) + 2;
1357 src += strlen( src );
1362 m->id = strdup( "" );
1364 if( m->param1 == NULL )
1366 m->param1 = strdup( "" );
1368 if( m->param2 == NULL )
1370 m->param2 = strdup( "" );
1379 static void MacroClean( macro_t *m )
1437 StrToMacroTypeTab [] =
1439 { "control", MVLC_CONTROL },
1440 /* player control */
1441 { "play", MVLC_PLAY },
1442 { "stop", MVLC_STOP },
1443 { "pause", MVLC_PAUSE },
1444 { "next", MVLC_NEXT },
1445 { "previous", MVLC_PREVIOUS },
1446 { "seek", MVLC_SEEK },
1447 { "keep", MVLC_KEEP },
1448 { "fullscreen", MVLC_FULLSCREEN },
1449 { "volume", MVLC_VOLUME },
1451 /* playlist management */
1452 { "add", MVLC_ADD },
1453 { "delete", MVLC_DEL },
1454 { "empty", MVLC_EMPTY },
1455 { "sort", MVLC_SORT },
1456 { "move", MVLC_MOVE },
1459 { "close", MVLC_CLOSE },
1460 { "shutdown", MVLC_SHUTDOWN },
1463 { "vlm_new", MVLC_VLM_NEW },
1464 { "vlm_setup", MVLC_VLM_SETUP },
1465 { "vlm_del", MVLC_VLM_DEL },
1466 { "vlm_play", MVLC_VLM_PLAY },
1467 { "vlm_pause", MVLC_VLM_PAUSE },
1468 { "vlm_stop", MVLC_VLM_STOP },
1469 { "vlm_seek", MVLC_VLM_SEEK },
1470 { "vlm_load", MVLC_VLM_LOAD },
1471 { "vlm_save", MVLC_VLM_SAVE },
1473 { "rpn", MVLC_RPN },
1475 { "foreach", MVLC_FOREACH },
1476 { "value", MVLC_VALUE },
1479 { "else", MVLC_ELSE },
1480 { "end", MVLC_END },
1481 { "get", MVLC_GET },
1482 { "set", MVLC_SET },
1483 { "int", MVLC_INT },
1484 { "float", MVLC_FLOAT },
1485 { "string", MVLC_STRING },
1488 { NULL, MVLC_UNKNOWN }
1491 static int StrToMacroType( char *name )
1495 if( !name || *name == '\0')
1497 return MVLC_UNKNOWN;
1499 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1501 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1503 return StrToMacroTypeTab[i].i_type;
1506 return MVLC_UNKNOWN;
1509 static void MacroDo( httpd_file_sys_t *p_args,
1511 uint8_t *p_request, int i_request,
1512 uint8_t **pp_data, int *pi_data,
1515 intf_thread_t *p_intf = p_args->p_intf;
1516 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1519 #define ALLOC( l ) \
1521 int __i__ = *pp_dst - *pp_data; \
1523 *pp_data = realloc( *pp_data, *pi_data ); \
1524 *pp_dst = (*pp_data) + __i__; \
1526 #define PRINT( str ) \
1527 ALLOC( strlen( str ) + 1 ); \
1528 *pp_dst += sprintf( *pp_dst, str );
1530 #define PRINTS( str, s ) \
1531 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1533 char * psz_cur = *pp_dst; \
1534 *pp_dst += sprintf( *pp_dst, str, s ); \
1535 while( psz_cur && *psz_cur ) \
1537 /* Prevent script injection */ \
1538 if( *psz_cur == '<' ) *psz_cur = '*'; \
1539 if( *psz_cur == '>' ) *psz_cur = '*'; \
1544 switch( StrToMacroType( m->id ) )
1547 if( i_request <= 0 )
1551 uri_extract_value( p_request, "control", control, 512 );
1552 if( *m->param1 && !strstr( m->param1, control ) )
1554 msg_Warn( p_intf, "unauthorized control=%s", control );
1557 switch( StrToMacroType( control ) )
1564 uri_extract_value( p_request, "item", item, 512 );
1565 i_item = atoi( item );
1566 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
1567 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1571 playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1572 msg_Dbg( p_intf, "requested playlist stop" );
1575 playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1576 msg_Dbg( p_intf, "requested playlist pause" );
1579 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1580 p_sys->p_playlist->i_index + 1 );
1581 msg_Dbg( p_intf, "requested playlist next" );
1584 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1585 p_sys->p_playlist->i_index - 1 );
1586 msg_Dbg( p_intf, "requested playlist next" );
1588 case MVLC_FULLSCREEN:
1590 if( p_sys->p_input )
1592 vout_thread_t *p_vout;
1593 p_vout = vlc_object_find( p_sys->p_input,
1594 VLC_OBJECT_VOUT, FIND_CHILD );
1598 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1599 vlc_object_release( p_vout );
1600 msg_Dbg( p_intf, "requested fullscreen toggle" );
1614 #define POSITION_ABSOLUTE 12
1615 #define POSITION_REL_FOR 13
1616 #define POSITION_REL_BACK 11
1617 #define TIME_ABSOLUTE 0
1618 #define TIME_REL_FOR 1
1619 #define TIME_REL_BACK -1
1620 if( p_sys->p_input )
1622 uri_extract_value( p_request, "seek_value", value, 20 );
1623 uri_decode_url_encoded( value );
1625 var_Get( p_sys->p_input, "length", &val);
1626 i_length = val.i_time;
1628 while( p_value[0] != '\0' )
1634 i_relative = TIME_REL_FOR;
1640 i_relative = TIME_REL_BACK;
1644 case '0': case '1': case '2': case '3': case '4':
1645 case '5': case '6': case '7': case '8': case '9':
1647 i_stock = strtol( p_value , &p_value , 10 );
1650 case '%': /* for percentage ie position */
1652 i_relative += POSITION_ABSOLUTE;
1660 i_value = 60 * (i_value + i_stock) ;
1665 case 'h': case 'H': /* hours */
1667 i_value += 3600 * i_stock;
1669 /* other characters which are not numbers are not important */
1670 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1676 case 'm': case 'M': case '\'': /* minutes */
1678 i_value += 60 * i_stock;
1681 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1687 case 's': case 'S': case '"': /* seconds */
1691 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1705 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1712 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1713 val.i_time = (uint64_t)( i_value ) * 1000000;
1715 val.i_time = i_length;
1717 var_Set( p_sys->p_input, "time", val );
1718 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1723 var_Get( p_sys->p_input, "time", &val );
1724 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1726 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1729 val.i_time = i_length;
1731 var_Set( p_sys->p_input, "time", val );
1732 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1737 var_Get( p_sys->p_input, "time", &val );
1738 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1743 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1745 var_Set( p_sys->p_input, "time", val );
1746 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1749 case POSITION_ABSOLUTE:
1751 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1752 var_Set( p_sys->p_input, "position", val );
1753 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1756 case POSITION_REL_FOR:
1758 var_Get( p_sys->p_input, "position", &val );
1759 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1760 var_Set( p_sys->p_input, "position", val );
1761 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1764 case POSITION_REL_BACK:
1766 var_Get( p_sys->p_input, "position", &val );
1767 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1768 var_Set( p_sys->p_input, "position", val );
1769 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
1774 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1779 #undef POSITION_ABSOLUTE
1780 #undef POSITION_REL_FOR
1781 #undef POSITION_REL_BACK
1782 #undef TIME_ABSOLUTE
1784 #undef TIME_REL_BACK
1790 audio_volume_t i_volume;
1793 uri_extract_value( p_request, "value", vol, 8 );
1794 aout_VolumeGet( p_intf, &i_volume );
1795 uri_decode_url_encoded( vol );
1799 i_value = atoi( vol + 1 );
1800 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1802 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1803 msg_Dbg( p_intf, "requested volume set: max" );
1806 aout_VolumeSet( p_intf , (i_volume + i_value) );
1807 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1812 i_value = atoi( vol + 1 );
1813 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1815 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1816 msg_Dbg( p_intf, "requested volume set: min" );
1819 aout_VolumeSet( p_intf , (i_volume - i_value) );
1820 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1823 if( strstr(vol, "%") != NULL )
1825 i_value = atoi( vol );
1826 if( (i_value <= 100) && (i_value>=0) ){
1827 aout_VolumeSet( p_intf, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/100+AOUT_VOLUME_MIN);
1828 msg_Dbg( p_intf, "requested volume set: %i%%", atoi( vol ));
1832 i_value = atoi( vol );
1833 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1835 aout_VolumeSet( p_intf , atoi( vol ) );
1836 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1842 /* playlist management */
1846 playlist_item_t * p_item;
1848 uri_extract_value( p_request, "mrl", mrl, 512 );
1849 uri_decode_url_encoded( mrl );
1850 p_item = parse_MRL( p_intf, mrl );
1852 if( !p_item || !p_item->input.psz_uri ||
1853 !*p_item->input.psz_uri )
1855 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1858 playlist_AddItem( p_sys->p_playlist , p_item ,
1859 PLAYLIST_APPEND, PLAYLIST_END );
1860 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1867 int i_item, *p_items = NULL, i_nb_items = 0;
1868 char item[512], *p_parser = p_request;
1870 /* Get the list of items to delete */
1872 uri_extract_value( p_parser, "item", item, 512 )) )
1874 if( !*item ) continue;
1876 i_item = atoi( item );
1877 p_items = realloc( p_items, (i_nb_items + 1) *
1879 p_items[i_nb_items] = i_item;
1883 /* The items need to be deleted from in reversed order */
1887 for( i = 0; i < i_nb_items; i++ )
1890 for( j = 0; j < i_nb_items; j++ )
1892 if( p_items[j] > p_items[i_index] )
1896 playlist_Delete( p_sys->p_playlist,
1898 msg_Dbg( p_intf, "requested playlist delete: %d",
1900 p_items[i_index] = -1;
1904 if( p_items ) free( p_items );
1909 int i_item, *p_items = NULL, i_nb_items = 0;
1910 char item[512], *p_parser = p_request;
1913 /* Get the list of items to keep */
1915 uri_extract_value( p_parser, "item", item, 512 )) )
1917 if( !*item ) continue;
1919 i_item = atoi( item );
1920 p_items = realloc( p_items, (i_nb_items + 1) *
1922 p_items[i_nb_items] = i_item;
1926 /* The items need to be deleted from in reversed order */
1927 for( i = p_sys->p_playlist->i_size - 1; i >= 0 ; i-- )
1929 /* Check if the item is in the keep list */
1930 for( j = 0 ; j < i_nb_items ; j++ )
1932 if( p_items[j] == i ) break;
1934 if( j == i_nb_items )
1936 playlist_Delete( p_sys->p_playlist, i );
1937 msg_Dbg( p_intf, "requested playlist delete: %d",
1942 if( p_items ) free( p_items );
1947 while( p_sys->p_playlist->i_size > 0 )
1949 playlist_Delete( p_sys->p_playlist, 0 );
1951 msg_Dbg( p_intf, "requested playlist empty" );
1960 uri_extract_value( p_request, "type", type, 12 );
1961 uri_extract_value( p_request, "order", order, 2 );
1963 if( order[0] == '0' ) i_order = ORDER_NORMAL;
1964 else i_order = ORDER_REVERSE;
1966 if( !strcmp( type , "title" ) )
1968 playlist_SortTitle( p_sys->p_playlist , i_order );
1969 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
1970 } else if( !strcmp( type , "group" ) )
1972 playlist_SortGroup( p_sys->p_playlist , i_order );
1973 msg_Dbg( p_intf, "requested playlist sort by group (%d)" , i_order );
1974 } else if( !strcmp( type , "author" ) )
1976 playlist_SortAuthor( p_sys->p_playlist , i_order );
1977 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
1978 } else if( !strcmp( type , "shuffle" ) )
1980 playlist_Sort( p_sys->p_playlist , SORT_RANDOM, ORDER_NORMAL );
1981 msg_Dbg( p_intf, "requested playlist shuffle");
1992 uri_extract_value( p_request, "psz_pos", psz_pos, 6 );
1993 uri_extract_value( p_request, "psz_newpos", psz_newpos, 6 );
1994 i_pos = atoi( psz_pos );
1995 i_newpos = atoi( psz_newpos );
1996 if ( i_pos < i_newpos )
1998 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
2000 playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
2002 msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
2006 /* admin function */
2010 uri_extract_value( p_request, "id", id, 512 );
2011 msg_Dbg( p_intf, "requested close id=%s", id );
2013 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
2015 msg_Warn( p_intf, "close failed for id=%s", id );
2022 msg_Dbg( p_intf, "requested shutdown" );
2023 p_intf->p_vlc->b_die = VLC_TRUE;
2028 case MVLC_VLM_SETUP:
2030 static const char *vlm_properties[11] =
2033 "enabled", "disabled", "loop", "unloop",
2035 "input", "output", "option", "date", "period", "repeat", "append",
2037 vlm_message_t *vlm_answer;
2039 char *psz = malloc( strlen( p_request ) + 1000 );
2044 if( p_intf->p_sys->p_vlm == NULL )
2045 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2047 if( p_intf->p_sys->p_vlm == NULL ) break;
2049 uri_extract_value( p_request, "name", name, 512 );
2050 if( StrToMacroType( control ) == MVLC_VLM_NEW )
2053 uri_extract_value( p_request, "type", type, 20 );
2054 p += sprintf( psz, "new %s %s", name, type );
2058 p += sprintf( psz, "setup %s", name );
2060 /* Parse the request */
2061 for( i = 0; i < 11; i++ )
2064 uri_extract_value( p_request, vlm_properties[i], val, 512 );
2065 uri_decode_url_encoded( val );
2066 if( strlen( val ) > 0 && i >= 4 )
2068 p += sprintf( p, " %s %s", vlm_properties[i], val );
2070 else if( uri_test_param( p_request, vlm_properties[i] ) && i < 4 )
2072 p += sprintf( p, " %s", vlm_properties[i] );
2075 fprintf( stderr, "vlm_ExecuteCommand: %s\n", psz );
2076 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2077 if( vlm_answer->psz_value == NULL ) /* there is no error */
2079 vlm_error = strdup( "" );
2083 vlm_error = malloc( strlen(vlm_answer->psz_name) +
2084 strlen(vlm_answer->psz_value) +
2085 strlen( " : ") + 1 );
2086 sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
2087 vlm_answer->psz_value );
2090 mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
2092 vlm_MessageDelete( vlm_answer );
2100 vlm_message_t *vlm_answer;
2103 if( p_intf->p_sys->p_vlm == NULL )
2104 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2106 if( p_intf->p_sys->p_vlm == NULL ) break;
2108 uri_extract_value( p_request, "name", name, 512 );
2109 sprintf( psz, "del %s", name );
2111 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2112 /* FIXME do a vlm_answer -> var stack conversion */
2113 vlm_MessageDelete( vlm_answer );
2118 case MVLC_VLM_PAUSE:
2122 vlm_message_t *vlm_answer;
2125 if( p_intf->p_sys->p_vlm == NULL )
2126 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2128 if( p_intf->p_sys->p_vlm == NULL ) break;
2130 uri_extract_value( p_request, "name", name, 512 );
2131 if( StrToMacroType( control ) == MVLC_VLM_PLAY )
2132 sprintf( psz, "control %s play", name );
2133 else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
2134 sprintf( psz, "control %s pause", name );
2135 else if( StrToMacroType( control ) == MVLC_VLM_STOP )
2136 sprintf( psz, "control %s stop", name );
2137 else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
2140 uri_extract_value( p_request, "percent", percent, 512 );
2141 sprintf( psz, "control %s seek %s", name, percent );
2144 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2145 /* FIXME do a vlm_answer -> var stack conversion */
2146 vlm_MessageDelete( vlm_answer );
2152 vlm_message_t *vlm_answer;
2156 if( p_intf->p_sys->p_vlm == NULL )
2157 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2159 if( p_intf->p_sys->p_vlm == NULL ) break;
2161 uri_extract_value( p_request, "file", file, 512 );
2162 uri_decode_url_encoded( file );
2164 if( StrToMacroType( control ) == MVLC_VLM_LOAD )
2165 sprintf( psz, "load %s", file );
2167 sprintf( psz, "save %s", file );
2169 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2170 /* FIXME do a vlm_answer -> var stack conversion */
2171 vlm_MessageDelete( vlm_answer );
2178 PRINTS( "<!-- control param(%s) unsuported -->", control );
2190 if( i_request <= 0 ||
2191 *m->param1 == '\0' ||
2192 strstr( p_request, m->param1 ) == NULL )
2196 uri_extract_value( p_request, m->param1, value, 512 );
2197 uri_decode_url_encoded( value );
2199 switch( StrToMacroType( m->param2 ) )
2203 config_PutInt( p_intf, m->param1, i );
2207 config_PutFloat( p_intf, m->param1, f );
2210 config_PutPsz( p_intf, m->param1, value );
2213 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
2224 if( *m->param1 == '\0' )
2229 switch( StrToMacroType( m->param2 ) )
2232 i = config_GetInt( p_intf, m->param1 );
2233 sprintf( value, "%i", i );
2236 f = config_GetFloat( p_intf, m->param1 );
2237 sprintf( value, "%f", f );
2240 psz = config_GetPsz( p_intf, m->param1 );
2241 sprintf( value, "%s", psz ? psz : "" );
2242 if( psz ) free( psz );
2245 sprintf( value, "invalid type(%s) in set", m->param2 );
2248 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
2249 PRINTS( "%s", value );
2258 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2259 s = SSPop( &p_args->stack );
2260 v = mvar_GetValue( p_args->vars, s );
2264 v = s = SSPop( &p_args->stack );
2272 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2277 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2278 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2286 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
2293 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
2298 i_skip = MacroParse( &m, src );
2300 i_id = StrToMacroType( m.id );
2317 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2318 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2320 return src + ( b_after ? i_skip : 0 );
2322 else if( i_level < 0 )
2338 static void Execute( httpd_file_sys_t *p_args,
2339 uint8_t *p_request, int i_request,
2340 uint8_t **pp_data, int *pi_data,
2342 uint8_t *_src, uint8_t *_end )
2344 intf_thread_t *p_intf = p_args->p_intf;
2346 uint8_t *src, *dup, *end;
2347 uint8_t *dst = *pp_dst;
2349 src = dup = malloc( _end - _src + 1 );
2350 end = src +( _end - _src );
2352 memcpy( src, _src, _end - _src );
2355 /* we parse searching <vlc */
2361 p = strstr( src, "<vlc" );
2362 if( p < end && p == src )
2366 src += MacroParse( &m, src );
2368 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2370 switch( StrToMacroType( m.id ) )
2377 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2378 if( SSPopN( &p_args->stack, p_args->vars ) )
2386 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2390 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2394 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2397 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2401 else if( i_test == 1 )
2404 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2406 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2410 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2419 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2420 uint8_t *start = src;
2421 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2428 if( !strcmp( m.param2, "integer" ) )
2430 char *arg = SSPop( &p_args->stack );
2431 index = mvar_IntegerSetNew( m.param1, arg );
2434 else if( !strcmp( m.param2, "directory" ) )
2436 char *arg = SSPop( &p_args->stack );
2437 index = mvar_FileSetNew( m.param1, arg );
2440 else if( !strcmp( m.param2, "playlist" ) )
2442 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2444 else if( !strcmp( m.param2, "information" ) )
2446 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2448 else if( !strcmp( m.param2, "vlm" ) )
2450 if( p_intf->p_sys->p_vlm == NULL )
2451 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2452 index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
2455 else if( !strcmp( m.param2, "hosts" ) )
2457 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2459 else if( !strcmp( m.param2, "urls" ) )
2461 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2463 else if( !strcmp( m.param2, "connections" ) )
2465 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2468 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2470 index = mvar_Duplicate( v );
2474 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2479 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2481 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2483 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2486 f->name = strdup( m.param1 );
2489 mvar_PushVar( p_args->vars, f );
2490 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2491 mvar_RemoveVar( p_args->vars, f );
2495 mvar_Delete( index );
2502 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2510 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2513 int i_index = dst - *pp_data;
2516 *pp_data = realloc( *pp_data, *pi_data );
2517 dst = (*pp_data) + i_index;
2519 memcpy( dst, src, i_copy );
2529 /****************************************************************************
2531 ****************************************************************************
2532 * a file with b_html is parsed and all "macro" replaced
2533 * <vlc id="macro name" [param1="" [param2=""]] />
2536 ****************************************************************************/
2537 static int HttpCallback( httpd_file_sys_t *p_args,
2538 httpd_file_t *p_file,
2540 uint8_t **pp_data, int *pi_data )
2542 int i_request = p_request ? strlen( p_request ) : 0;
2546 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2548 p = *pp_data = malloc( 10240 );
2551 return VLC_EGENERIC;
2553 p += sprintf( p, "<html>\n" );
2554 p += sprintf( p, "<head>\n" );
2555 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2556 p += sprintf( p, "</head>\n" );
2557 p += sprintf( p, "<body>\n" );
2558 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2559 p += sprintf( p, "<hr />\n" );
2560 p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
2561 p += sprintf( p, "</body>\n" );
2562 p += sprintf( p, "</html>\n" );
2564 *pi_data = strlen( *pp_data );
2569 if( !p_args->b_html )
2571 FileLoad( f, pp_data, pi_data );
2579 char position[4]; /* percentage */
2580 char time[12]; /* in seconds */
2581 char length[12]; /* in seconds */
2582 audio_volume_t i_volume;
2586 #define p_sys p_args->p_intf->p_sys
2587 if( p_sys->p_input )
2589 var_Get( p_sys->p_input, "position", &val);
2590 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2591 var_Get( p_sys->p_input, "time", &val);
2592 sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2593 var_Get( p_sys->p_input, "length", &val);
2594 sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2596 var_Get( p_sys->p_input, "state", &val );
2597 if( val.i_int == PLAYING_S )
2599 sprintf( state, "playing" );
2600 } else if( val.i_int == PAUSE_S )
2602 sprintf( state, "paused" );
2605 sprintf( state, "stop" );
2609 sprintf( position, "%d", 0 );
2610 sprintf( time, "%d", 0 );
2611 sprintf( length, "%d", 0 );
2612 sprintf( state, "stop" );
2616 aout_VolumeGet( p_args->p_intf , &i_volume );
2617 sprintf( volume , "%d" , (int)i_volume );
2619 p_args->vars = mvar_New( "variables", "" );
2620 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2621 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2622 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
2623 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2624 mvar_AppendNewVar( p_args->vars, "stream_position", position );
2625 mvar_AppendNewVar( p_args->vars, "stream_time", time );
2626 mvar_AppendNewVar( p_args->vars, "stream_length", length );
2627 mvar_AppendNewVar( p_args->vars, "volume", volume );
2628 mvar_AppendNewVar( p_args->vars, "stream_state", state );
2630 SSInit( &p_args->stack );
2632 /* first we load in a temporary buffer */
2633 FileLoad( f, &p_buffer, &i_buffer );
2635 /* allocate output */
2636 *pi_data = i_buffer + 1000;
2637 dst = *pp_data = malloc( *pi_data );
2639 /* we parse executing all <vlc /> macros */
2640 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2643 *pi_data = dst - *pp_data;
2645 SSClean( &p_args->stack );
2646 mvar_Delete( p_args->vars );
2655 /****************************************************************************
2657 ****************************************************************************/
2658 static int uri_test_param( char *psz_uri, const char *psz_name )
2662 while( (p = strstr( p, psz_name )) )
2664 /* Verify that we are dealing with a post/get argument */
2665 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2674 static char *uri_extract_value( char *psz_uri, const char *psz_name,
2675 char *psz_value, int i_value_max )
2679 while( (p = strstr( p, psz_name )) )
2681 /* Verify that we are dealing with a post/get argument */
2682 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2691 p += strlen( psz_name );
2692 if( *p == '=' ) p++;
2694 if( strchr( p, '&' ) )
2696 i_len = strchr( p, '&' ) - p;
2700 /* for POST method */
2701 if( strchr( p, '\n' ) )
2703 i_len = strchr( p, '\n' ) - p;
2704 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2708 i_len = strlen( p );
2711 i_len = __MIN( i_value_max - 1, i_len );
2714 strncpy( psz_value, p, i_len );
2715 psz_value[i_len] = '\0';
2719 strncpy( psz_value, "", i_value_max );
2725 strncpy( psz_value, "", i_value_max );
2731 static void uri_decode_url_encoded( char *psz )
2733 char *dup = strdup( psz );
2751 *psz++ = strtol( val, NULL, 16 );
2753 else if( *p == '+' )
2767 /****************************************************************************
2768 * Light RPN evaluator
2769 ****************************************************************************/
2770 static void SSInit( rpn_stack_t *st )
2775 static void SSClean( rpn_stack_t *st )
2777 while( st->i_stack > 0 )
2779 free( st->stack[--st->i_stack] );
2783 static void SSPush( rpn_stack_t *st, char *s )
2785 if( st->i_stack < STACK_MAX )
2787 st->stack[st->i_stack++] = strdup( s );
2791 static char * SSPop( rpn_stack_t *st )
2793 if( st->i_stack <= 0 )
2795 return strdup( "" );
2799 return st->stack[--st->i_stack];
2803 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2812 i = strtol( name, &end, 0 );
2815 value = mvar_GetValue( vars, name );
2823 static void SSPushN( rpn_stack_t *st, int i )
2827 sprintf( v, "%d", i );
2831 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2838 while( *exp == ' ' )
2845 /* extract string */
2848 while( *exp && *exp != '\'' )
2859 p = strchr( exp, ' ' );
2864 exp += strlen( exp );
2869 strncpy( s, exp, i );
2880 /* 1. Integer function */
2881 if( !strcmp( s, "!" ) )
2883 SSPushN( st, ~SSPopN( st, vars ) );
2885 else if( !strcmp( s, "^" ) )
2887 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2889 else if( !strcmp( s, "&" ) )
2891 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2893 else if( !strcmp( s, "|" ) )
2895 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2897 else if( !strcmp( s, "+" ) )
2899 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2901 else if( !strcmp( s, "-" ) )
2903 int j = SSPopN( st, vars );
2904 int i = SSPopN( st, vars );
2905 SSPushN( st, i - j );
2907 else if( !strcmp( s, "*" ) )
2909 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2911 else if( !strcmp( s, "/" ) )
2915 j = SSPopN( st, vars );
2916 i = SSPopN( st, vars );
2918 SSPushN( st, j != 0 ? i / j : 0 );
2920 else if( !strcmp( s, "%" ) )
2924 j = SSPopN( st, vars );
2925 i = SSPopN( st, vars );
2927 SSPushN( st, j != 0 ? i % j : 0 );
2929 /* 2. integer tests */
2930 else if( !strcmp( s, "=" ) )
2932 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2934 else if( !strcmp( s, "<" ) )
2936 int j = SSPopN( st, vars );
2937 int i = SSPopN( st, vars );
2939 SSPushN( st, i < j ? -1 : 0 );
2941 else if( !strcmp( s, ">" ) )
2943 int j = SSPopN( st, vars );
2944 int i = SSPopN( st, vars );
2946 SSPushN( st, i > j ? -1 : 0 );
2948 else if( !strcmp( s, "<=" ) )
2950 int j = SSPopN( st, vars );
2951 int i = SSPopN( st, vars );
2953 SSPushN( st, i <= j ? -1 : 0 );
2955 else if( !strcmp( s, ">=" ) )
2957 int j = SSPopN( st, vars );
2958 int i = SSPopN( st, vars );
2960 SSPushN( st, i >= j ? -1 : 0 );
2962 /* 3. string functions */
2963 else if( !strcmp( s, "strcat" ) )
2965 char *s2 = SSPop( st );
2966 char *s1 = SSPop( st );
2967 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2977 else if( !strcmp( s, "strcmp" ) )
2979 char *s2 = SSPop( st );
2980 char *s1 = SSPop( st );
2982 SSPushN( st, strcmp( s1, s2 ) );
2986 else if( !strcmp( s, "strncmp" ) )
2988 int n = SSPopN( st, vars );
2989 char *s2 = SSPop( st );
2990 char *s1 = SSPop( st );
2992 SSPushN( st, strncmp( s1, s2 , n ) );
2996 else if( !strcmp( s, "strsub" ) )
2998 int n = SSPopN( st, vars );
2999 int m = SSPopN( st, vars );
3001 char *s = SSPop( st );
3013 str = malloc( i_len + 1 );
3015 memcpy( str, s + m - 1, i_len );
3016 str[ i_len ] = '\0';
3022 else if( !strcmp( s, "strlen" ) )
3024 char *str = SSPop( st );
3026 SSPushN( st, strlen( str ) );
3029 /* 4. stack functions */
3030 else if( !strcmp( s, "dup" ) )
3032 char *str = SSPop( st );
3037 else if( !strcmp( s, "drop" ) )
3039 char *str = SSPop( st );
3042 else if( !strcmp( s, "swap" ) )
3044 char *s1 = SSPop( st );
3045 char *s2 = SSPop( st );
3052 else if( !strcmp( s, "flush" ) )
3057 else if( !strcmp( s, "store" ) )
3059 char *value = SSPop( st );
3060 char *name = SSPop( st );
3062 mvar_PushNewVar( vars, name, value );
3066 else if( !strcmp( s, "value" ) )
3068 char *name = SSPop( st );
3069 char *value = mvar_GetValue( vars, name );
3071 SSPush( st, value );
3075 else if( !strcmp( s, "url_extract" ) )
3077 char *url = mvar_GetValue( vars, "url_value" );
3078 char *name = SSPop( st );
3081 uri_extract_value( url, name, value, 512 );
3082 uri_decode_url_encoded( value );
3083 SSPush( st, value );
3092 /**********************************************************************
3093 * Find_end_MRL: Find the end of the sentence :
3094 * this function parses the string psz and find the end of the item
3095 * and/or option with detecting the " and ' problems.
3096 * returns NULL if an error is detected, otherwise, returns a pointer
3097 * of the end of the sentence (after the last character)
3098 **********************************************************************/
3099 static char *Find_end_MRL( char *psz )
3109 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
3111 if( *s_sent == '\'' )
3113 s_sent = Find_end_MRL( s_sent );
3115 if( s_sent == NULL )
3125 if( *s_sent == '\"' )
3129 } else /* *s_sent == '\0' , which means the number of " is incorrect */
3139 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
3141 if( *s_sent == '\"' )
3143 s_sent = Find_end_MRL( s_sent );
3145 if( s_sent == NULL )
3155 if( *s_sent == '\'' )
3159 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
3165 default: /* now we can look for spaces */
3167 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
3169 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
3171 s_sent = Find_end_MRL( s_sent );
3182 /**********************************************************************
3183 * parse_MRL: parse the MRL, find the mrl string and the options,
3184 * create an item with all information in it, and return the item.
3185 * return NULL if there is an error.
3186 **********************************************************************/
3187 playlist_item_t * parse_MRL( intf_thread_t *p_intf, char *psz )
3189 char **ppsz_options = NULL;
3196 playlist_item_t * p_item = NULL;
3198 /* In case there is spaces before the mrl */
3199 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
3204 /* extract the mrl */
3205 s_temp = strstr( s_mrl , " :" );
3206 if( s_temp == NULL )
3208 s_temp = s_mrl + strlen( s_mrl );
3211 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
3218 /* if the mrl is between " or ', we must remove them */
3219 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
3221 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
3222 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
3223 mrl[ s_temp - s_mrl - 2 ] = '\0';
3226 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
3227 strncpy( mrl , s_mrl , s_temp - s_mrl );
3228 mrl[ s_temp - s_mrl ] = '\0';
3233 /* now we can take care of the options */
3234 while( (*s_mrl != '\0') && (i_error == 0) )
3243 case ':': /* an option */
3245 s_temp = Find_end_MRL( s_mrl );
3247 if( s_temp == NULL )
3254 ppsz_options = realloc( ppsz_options , i_options *
3256 ppsz_options[ i_options - 1 ] =
3257 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
3259 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
3262 /* don't forget to finish the string with a '\0' */
3263 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
3283 /* now create an item */
3284 p_item = playlist_ItemNew( p_intf, mrl, mrl);
3285 for( i = 0 ; i< i_options ; i++ )
3287 playlist_ItemAddOption( p_item, ppsz_options[i] );
3291 for( i = 0 ; i < i_options ; i++ )
3293 free( ppsz_options[i] );
3295 free( ppsz_options );