1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001-2005 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"
45 #ifdef HAVE_SYS_STAT_H
46 # include <sys/stat.h>
57 #elif defined( WIN32 ) && !defined( UNDER_CE )
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_( "HTTP interface x509 PEM certificate file " \
84 #define KEY_TEXT N_( "Private key file" )
85 #define KEY_LONGTEXT N_( "HTTP interface x509 PEM private key file" )
86 #define CA_TEXT N_( "Root CA file" )
87 #define CA_LONGTEXT N_( "HTTP interface x509 PEM trusted root CA " \
89 #define CRL_TEXT N_( "CRL file" )
90 #define CRL_LONGTEXT N_( "HTTP interace Certificates Revocation List file" )
93 set_shortname( _("HTTP"));
94 set_description( _("HTTP remote control interface") );
95 set_category( CAT_INTERFACE );
96 set_subcategory( SUBCAT_INTERFACE_GENERAL );
97 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
98 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
99 set_section( N_("HTTP SSL" ), 0 );
100 add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
101 add_string ( "http-intf-key", NULL, NULL, KEY_TEXT, KEY_LONGTEXT, VLC_TRUE );
102 add_string ( "http-intf-ca", NULL, NULL, CA_TEXT, CA_LONGTEXT, VLC_TRUE );
103 add_string ( "http-intf-crl", NULL, NULL, CRL_TEXT, CRL_LONGTEXT, VLC_TRUE );
104 set_capability( "interface", 0 );
105 set_callbacks( Open, Close );
109 /*****************************************************************************
111 *****************************************************************************/
112 static void Run ( intf_thread_t *p_intf );
114 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
117 static int DirectoryCheck( char *psz_dir )
121 #ifdef HAVE_SYS_STAT_H
122 struct stat stat_info;
124 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
130 if( ( p_dir = opendir( psz_dir ) ) == NULL )
139 static int HttpCallback( httpd_file_sys_t *p_args,
142 uint8_t **pp_data, int *pi_data );
144 static char *uri_extract_value( char *psz_uri, const char *psz_name,
145 char *psz_value, int i_value_max );
146 static int uri_test_param( char *psz_uri, const char *psz_name );
148 static void uri_decode_url_encoded( char *psz );
150 static char *Find_end_MRL( char *psz );
151 static playlist_item_t *parse_MRL( intf_thread_t * , char *psz );
153 /*****************************************************************************
155 *****************************************************************************/
156 typedef struct mvar_s
162 struct mvar_s **field;
165 #define STACK_MAX 100
168 char *stack[STACK_MAX];
172 struct httpd_file_sys_t
174 intf_thread_t *p_intf;
175 httpd_file_t *p_file;
176 httpd_redirect_t *p_redir;
177 httpd_redirect_t *p_redir2;
184 /* inited for each access */
191 httpd_host_t *p_httpd_host;
194 httpd_file_sys_t **pp_files;
196 playlist_t *p_playlist;
197 input_thread_t *p_input;
204 /*****************************************************************************
205 * Activate: initialize and create stuff
206 *****************************************************************************/
207 static int Open( vlc_object_t *p_this )
209 intf_thread_t *p_intf = (intf_thread_t*)p_this;
212 char *psz_address = "";
213 const char *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
218 psz_host = config_GetPsz( p_intf, "http-host" );
222 psz_address = psz_host;
224 psz_parser = strchr( psz_host, ':' );
227 *psz_parser++ = '\0';
228 i_port = atoi( psz_parser );
232 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
235 return( VLC_ENOMEM );
237 p_sys->p_playlist = NULL;
238 p_sys->p_input = NULL;
241 /* determine Content-Type value for HTML pages */
242 vlc_current_charset(&psz_src);
243 if( psz_src == NULL )
248 p_sys->psz_html_type = malloc( 20 + strlen( psz_src ) );
249 if( p_sys->psz_html_type == NULL )
255 sprintf( p_sys->psz_html_type, "text/html; charset=%s", psz_src );
258 /* determine SSL configuration */
259 psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
260 if ( psz_cert != NULL )
262 msg_Dbg( p_intf, "enablind TLS for HTTP interface (cert file: %s)",
264 psz_key = config_GetPsz( p_intf, "http-intf-key" );
265 psz_ca = config_GetPsz( p_intf, "http-intf-ca" );
266 psz_crl = config_GetPsz( p_intf, "http-intf-crl" );
277 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
279 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_intf), psz_address,
280 i_port, psz_cert, psz_key, psz_ca,
282 if( p_sys->p_httpd_host == NULL )
284 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
285 free( p_sys->psz_html_type );
296 p_sys->pp_files = NULL;
298 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || defined(WIN32)
299 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
301 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
302 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
303 if( !psz_src ) return VLC_ENOMEM;
305 sprintf( psz_src, "%s/http", psz_vlcpath);
307 sprintf( psz_src, "%s/share/http", psz_vlcpath);
311 psz_src = config_GetPsz( p_intf, "http-src" );
313 if( !psz_src || *psz_src == '\0' )
315 if( !DirectoryCheck( "share/http" ) )
317 psz_src = strdup( "share/http" );
319 else if( !DirectoryCheck( DATA_PATH "/http" ) )
321 psz_src = strdup( DATA_PATH "/http" );
326 if( !psz_src || *psz_src == '\0' )
328 msg_Err( p_intf, "invalid src dir" );
332 /* remove trainling \ or / */
333 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
334 psz_src[strlen( psz_src ) - 1] == '/' )
336 psz_src[strlen( psz_src ) - 1] = '\0';
339 ParseDirectory( p_intf, psz_src, psz_src );
342 if( p_sys->i_files <= 0 )
344 msg_Err( p_intf, "cannot find any files (%s)", psz_src );
347 p_intf->pf_run = Run;
353 if( psz_src ) free( psz_src );
354 if( p_sys->pp_files )
356 free( p_sys->pp_files );
358 httpd_HostDelete( p_sys->p_httpd_host );
359 free( p_sys->psz_html_type );
364 /*****************************************************************************
365 * CloseIntf: destroy interface
366 *****************************************************************************/
367 void Close ( vlc_object_t *p_this )
369 intf_thread_t *p_intf = (intf_thread_t *)p_this;
370 intf_sys_t *p_sys = p_intf->p_sys;
376 vlm_Delete( p_sys->p_vlm );
378 for( i = 0; i < p_sys->i_files; i++ )
380 httpd_FileDelete( p_sys->pp_files[i]->p_file );
381 if( p_sys->pp_files[i]->p_redir )
382 httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
383 if( p_sys->pp_files[i]->p_redir2 )
384 httpd_RedirectDelete( p_sys->pp_files[i]->p_redir2 );
386 free( p_sys->pp_files[i]->file );
387 free( p_sys->pp_files[i]->name );
388 free( p_sys->pp_files[i] );
390 if( p_sys->pp_files )
392 free( p_sys->pp_files );
394 httpd_HostDelete( p_sys->p_httpd_host );
396 free( p_sys->psz_html_type );
400 /*****************************************************************************
401 * Run: http interface thread
402 *****************************************************************************/
403 static void Run( intf_thread_t *p_intf )
405 intf_sys_t *p_sys = p_intf->p_sys;
407 while( !p_intf->b_die )
409 /* get the playlist */
410 if( p_sys->p_playlist == NULL )
412 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
415 /* Manage the input part */
416 if( p_sys->p_input == NULL )
418 if( p_sys->p_playlist )
421 vlc_object_find( p_sys->p_playlist,
426 else if( p_sys->p_input->b_dead )
428 vlc_object_release( p_sys->p_input );
429 p_sys->p_input = NULL;
434 msleep( INTF_IDLE_SLEEP );
439 vlc_object_release( p_sys->p_input );
440 p_sys->p_input = NULL;
443 if( p_sys->p_playlist )
445 vlc_object_release( p_sys->p_playlist );
446 p_sys->p_playlist = NULL;
451 /*****************************************************************************
453 *****************************************************************************/
454 #define MAX_DIR_SIZE 10240
456 /****************************************************************************
457 * FileToUrl: create a good name for an url from filename
458 ****************************************************************************/
459 static char *FileToUrl( char *name, vlc_bool_t *pb_index )
463 url = p = malloc( strlen( name ) + 1 );
471 while( *name == '\\' || *name == '/' )
473 while( *name == '/' )
483 /* convert '\\' into '/' */
495 *pb_index = VLC_FALSE;
497 if( ( p = strrchr( url, '/' ) ) != NULL )
499 if( !strncmp( p, "/index.", 7 ) )
502 *pb_index = VLC_TRUE;
508 /****************************************************************************
509 * ParseDirectory: parse recursively a directory, adding each file
510 ****************************************************************************/
511 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
514 intf_sys_t *p_sys = p_intf->p_sys;
515 char dir[MAX_DIR_SIZE];
516 #ifdef HAVE_SYS_STAT_H
517 struct stat stat_info;
520 struct dirent *p_dir_content;
524 char *password = NULL;
526 #ifdef HAVE_SYS_STAT_H
527 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
533 if( ( p_dir = opendir( psz_dir ) ) == NULL )
535 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
539 msg_Dbg( p_intf, "dir=%s", psz_dir );
541 sprintf( dir, "%s/.access", psz_dir );
542 if( ( file = fopen( dir, "r" ) ) != NULL )
547 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
549 i_size = fread( line, 1, 1023, file );
553 while( i_size > 0 && ( line[i_size-1] == '\n' ||
554 line[i_size-1] == '\r' ) )
561 p = strchr( line, ':' );
565 user = strdup( line );
566 password = strdup( p );
569 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
570 user, password, i_size );
577 /* parse psz_src dir */
578 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
583 if( p_dir_content->d_name[0] == '.' )
587 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
588 if( ParseDirectory( p_intf, psz_root, dir ) )
590 httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
597 f->file = strdup( dir );
598 f->name = FileToUrl( &dir[strlen( psz_root )], &b_index );
599 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
603 msg_Err( p_intf , "unable to parse directory" );
606 return( VLC_ENOMEM );
608 msg_Dbg( p_intf, "file=%s (url=%s)",
611 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
613 f->b_html ? p_sys->psz_html_type : NULL,
619 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
621 /* for url that ends by / add
622 * - a redirect from rep to rep/
623 * - in case of index.* rep/index.html to rep/ */
624 if( f && f->name[strlen(f->name) - 1] == '/' )
626 char *psz_redir = strdup( f->name );
628 psz_redir[strlen( psz_redir ) - 1] = '\0';
630 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
631 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
634 if( b_index && ( p = strstr( f->file, "index." ) ) )
636 asprintf( &psz_redir, "%s%s", f->name, p );
638 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
639 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
640 f->name, psz_redir );
662 /****************************************************************************
663 * var and set handling
664 ****************************************************************************/
666 static mvar_t *mvar_New( char *name, char *value )
668 mvar_t *v = malloc( sizeof( mvar_t ) );
670 if( !v ) return NULL;
671 v->name = strdup( name );
672 v->value = strdup( value ? value : "" );
675 v->field = malloc( sizeof( mvar_t * ) );
681 static void mvar_Delete( mvar_t *v )
688 for( i = 0; i < v->i_field; i++ )
690 mvar_Delete( v->field[i] );
696 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
698 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
699 v->field[v->i_field] = f;
703 static mvar_t *mvar_Duplicate( mvar_t *v )
708 n = mvar_New( v->name, v->value );
709 for( i = 0; i < v->i_field; i++ )
711 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
717 static void mvar_PushVar( mvar_t *v, mvar_t *f )
719 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
722 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
728 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
731 for( i = 0; i < v->i_field; i++ )
733 if( v->field[i] == f )
738 if( i >= v->i_field )
743 if( i + 1 < v->i_field )
745 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
748 /* FIXME should do a realloc */
751 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
754 char base[512], *field, *p;
757 /* format: name[index].field */
759 field = strchr( name, '.' );
762 int i = field - name;
763 strncpy( base, name, i );
769 strcpy( base, name );
772 if( ( p = strchr( base, '[' ) ) )
775 sscanf( p, "%d]", &i_index );
786 for( i = 0; i < s->i_field; i++ )
788 if( !strcmp( s->field[i]->name, base ) )
798 return mvar_GetVar( s->field[i], field );
812 static char *mvar_GetValue( mvar_t *v, char *field )
820 mvar_t *f = mvar_GetVar( v, field );
832 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
834 mvar_t *f = mvar_New( name, value );
835 mvar_PushVar( vars, f );
838 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
840 mvar_t *f = mvar_New( name, value );
841 mvar_AppendVar( vars, f );
845 /* arg= start[:stop[:step]],.. */
846 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
848 char *dup = strdup( arg );
850 mvar_t *s = mvar_New( name, "set" );
856 int i_start,i_stop,i_step;
859 p = strchr( str, ',' );
866 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
873 else if( i_match == 2 )
875 i_step = i_start < i_stop ? 1 : -1;
882 if( ( i_start <= i_stop && i_step > 0 ) ||
883 ( i_start >= i_stop && i_step < 0 ) )
885 for( i = i_start; ; i += i_step )
889 if( ( i_step > 0 && i > i_stop ) ||
890 ( i_step < 0 && i < i_stop ) )
895 sprintf( value, "%d", i );
897 mvar_PushNewVar( s, name, value );
908 void PlaylistListNode( playlist_t *p_pl, playlist_item_t *p_node,
909 char *name, mvar_t *s, int i_depth )
913 if (p_node->i_children == -1)
916 mvar_t *itm = mvar_New( name, "set" );
918 sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
919 mvar_AppendNewVar( itm, "current", value );
921 sprintf( value, "%d", p_node->input.i_id );
922 mvar_AppendNewVar( itm, "index", value );
924 mvar_AppendNewVar( itm, "name", p_node->input.psz_name );
926 mvar_AppendNewVar( itm, "uri", p_node->input.psz_uri );
928 sprintf( value, "Item");
929 mvar_AppendNewVar( itm, "type", value );
931 sprintf( value, "%d", i_depth );
932 mvar_AppendNewVar( itm, "depth", value );
934 mvar_AppendVar( s, itm );
940 mvar_t *itm = mvar_New( name, "set" );
942 mvar_AppendNewVar( itm, "name", p_node->input.psz_name );
943 mvar_AppendNewVar( itm, "uri", p_node->input.psz_name );
945 sprintf( value, "Node" );
946 mvar_AppendNewVar( itm, "type", value );
948 sprintf( value, "%d", p_node->input.i_id );
949 mvar_AppendNewVar( itm, "index", value );
951 sprintf( value, "%d", p_node->i_children);
952 mvar_AppendNewVar( itm, "i_children", value );
954 sprintf( value, "%d", i_depth );
955 mvar_AppendNewVar( itm, "depth", value );
957 mvar_AppendVar( s, itm );
959 for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
960 PlaylistListNode( p_pl, p_node->pp_children[i_child], name, s, i_depth + 1);
966 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
968 playlist_view_t *p_view;
969 mvar_t *s = mvar_New( name, "set" );
972 vlc_mutex_lock( &p_pl->object_lock );
974 p_view = playlist_ViewFind( p_pl, VIEW_CATEGORY ); /* FIXME */
977 PlaylistListNode( p_pl, p_view->p_root, name, s, 0 );
979 vlc_mutex_unlock( &p_pl->object_lock );
984 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
986 mvar_t *s = mvar_New( name, "set" );
989 if( p_input == NULL )
994 vlc_mutex_lock( &p_input->input.p_item->lock );
995 for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
997 info_category_t *p_category = p_input->input.p_item->pp_categories[i];
998 mvar_t *cat = mvar_New( name, "set" );
999 mvar_t *iset = mvar_New( "info", "set" );
1001 mvar_AppendNewVar( cat, "name", p_category->psz_name );
1002 mvar_AppendVar( cat, iset );
1004 for ( j = 0; j < p_category->i_infos; j++ )
1006 info_t *p_info = p_category->pp_infos[j];
1007 mvar_t *info = mvar_New( "info", "" );
1009 msg_Dbg( p_input, "adding info name=%s value=%s",
1010 p_info->psz_name, p_info->psz_value );
1011 mvar_AppendNewVar( info, "name", p_info->psz_name );
1012 mvar_AppendNewVar( info, "value", p_info->psz_value );
1013 mvar_AppendVar( iset, info );
1015 mvar_AppendVar( s, cat );
1017 vlc_mutex_unlock( &p_input->input.p_item->lock );
1022 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
1024 mvar_t *s = mvar_New( name, "set" );
1028 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
1030 for( i= 0; i < info.i_count; )
1034 inf = mvar_New( name, "set" );
1037 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
1038 info.info[i].psz_name, info.info[i].psz_value ); */
1039 mvar_AppendNewVar( inf,
1040 info.info[i].psz_name,
1041 info.info[i].psz_value );
1043 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
1044 mvar_AppendVar( s, inf );
1049 for( i = 0; i < info.i_count; i++ )
1051 free( info.info[i].psz_name );
1052 free( info.info[i].psz_value );
1054 if( info.i_count > 0 )
1063 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1065 mvar_t *s = mvar_New( name, "set" );
1066 char tmp[MAX_DIR_SIZE], *p, *src;
1067 #ifdef HAVE_SYS_STAT_H
1068 struct stat stat_info;
1071 struct dirent *p_dir_content;
1074 /* convert all / to native separator */
1075 #if defined( WIN32 )
1076 while( (p = strchr( psz_dir, '/' )) )
1085 /* remove trailling separator */
1086 while( strlen( psz_dir ) > 1 &&
1087 #if defined( WIN32 )
1088 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1090 psz_dir[strlen( psz_dir ) -1 ] == sep )
1092 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1094 /* remove double separator */
1095 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1097 if( src[0] == sep && src[1] == sep )
1105 if( *psz_dir == '\0' )
1109 /* first fix all .. dir */
1113 if( src[0] == '.' && src[1] == '.' )
1116 if( p <= &psz_dir[1] )
1123 while( p > &psz_dir[1] && *p != sep )
1128 else if( *src == sep )
1130 if( p > psz_dir && p[-1] == sep )
1144 } while( *src && *src != sep );
1150 #ifdef HAVE_SYS_STAT_H
1151 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1157 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1159 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1163 /* remove traling / or \ */
1164 for( p = &psz_dir[strlen( psz_dir) - 1];
1165 p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1174 /* parse psz_src dir */
1175 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1179 if( !strcmp( p_dir_content->d_name, "." ) )
1184 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1186 #ifdef HAVE_SYS_STAT_H
1187 if( stat( tmp, &stat_info ) == -1 )
1192 f = mvar_New( name, "set" );
1193 mvar_AppendNewVar( f, "name", tmp );
1195 #ifdef HAVE_SYS_STAT_H
1196 if( S_ISDIR( stat_info.st_mode ) )
1198 mvar_AppendNewVar( f, "type", "directory" );
1200 else if( S_ISREG( stat_info.st_mode ) )
1202 mvar_AppendNewVar( f, "type", "file" );
1206 mvar_AppendNewVar( f, "type", "unknown" );
1209 sprintf( tmp, I64Fd, (int64_t)stat_info.st_size );
1210 mvar_AppendNewVar( f, "size", tmp );
1212 /* FIXME memory leak FIXME */
1214 ctime_r( &stat_info.st_mtime, tmp );
1215 mvar_AppendNewVar( f, "date", tmp );
1217 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1221 mvar_AppendNewVar( f, "type", "unknown" );
1222 mvar_AppendNewVar( f, "size", "unknown" );
1223 mvar_AppendNewVar( f, "date", "unknown" );
1225 mvar_AppendVar( s, f );
1231 static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
1233 mvar_t *s = mvar_New( name, "set" );
1237 if( vlm == NULL ) return s;
1239 if( vlm_ExecuteCommand( vlm, "show", &msg ) )
1244 for( i = 0; i < msg->i_child; i++ )
1246 /* Over media, schedule */
1247 vlm_message_t *ch = msg->child[i];
1250 for( j = 0; j < ch->i_child; j++ )
1253 vlm_message_t *el = ch->child[j];
1254 vlm_message_t *inf, *desc;
1259 sprintf( psz, "show %s", el->psz_name );
1260 if( vlm_ExecuteCommand( vlm, psz, &inf ) )
1262 desc = inf->child[0];
1264 /* Add a node with name and info */
1265 set = mvar_New( name, "set" );
1266 mvar_AppendNewVar( set, "name", el->psz_name );
1268 for( k = 0; k < desc->i_child; k++ )
1270 vlm_message_t *ch = desc->child[k];
1271 if( ch->i_child > 0 )
1274 mvar_t *n = mvar_New( ch->psz_name, "set" );
1276 for( c = 0; c < ch->i_child; c++ )
1278 if( ch->child[c]->psz_value )
1280 mvar_AppendNewVar( n, ch->child[c]->psz_name, ch->child[c]->psz_value );
1284 mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
1285 mvar_AppendVar( n, in );
1288 mvar_AppendVar( set, n );
1292 mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
1295 vlm_MessageDelete( inf );
1297 mvar_AppendVar( s, set );
1300 vlm_MessageDelete( msg );
1306 static void SSInit( rpn_stack_t * );
1307 static void SSClean( rpn_stack_t * );
1308 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1310 static void SSPush ( rpn_stack_t *, char * );
1311 static char *SSPop ( rpn_stack_t * );
1313 static void SSPushN ( rpn_stack_t *, int );
1314 static int SSPopN ( rpn_stack_t *, mvar_t * );
1317 /****************************************************************************
1319 ****************************************************************************/
1327 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1331 /* just load the file */
1333 *pp_data = malloc( 1025 ); /* +1 for \0 */
1334 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1337 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1343 (*pp_data)[*pi_data] = '\0';
1348 static int MacroParse( macro_t *m, uint8_t *psz_src )
1350 uint8_t *dup = strdup( psz_src );
1355 #define EXTRACT( name, l ) \
1357 p = strchr( src, '"' ); \
1362 m->name = strdup( src ); \
1383 if( !strncmp( src, "id=\"", 4 ) )
1387 else if( !strncmp( src, "param1=\"", 8 ) )
1389 EXTRACT( param1, 8 );
1391 else if( !strncmp( src, "param2=\"", 8 ) )
1393 EXTRACT( param2, 8 );
1400 if( strstr( src, "/>" ) )
1402 src = strstr( src, "/>" ) + 2;
1406 src += strlen( src );
1411 m->id = strdup( "" );
1413 if( m->param1 == NULL )
1415 m->param1 = strdup( "" );
1417 if( m->param2 == NULL )
1419 m->param2 = strdup( "" );
1428 static void MacroClean( macro_t *m )
1487 StrToMacroTypeTab [] =
1489 { "control", MVLC_CONTROL },
1490 /* player control */
1491 { "play", MVLC_PLAY },
1492 { "stop", MVLC_STOP },
1493 { "pause", MVLC_PAUSE },
1494 { "next", MVLC_NEXT },
1495 { "previous", MVLC_PREVIOUS },
1496 { "seek", MVLC_SEEK },
1497 { "keep", MVLC_KEEP },
1498 { "fullscreen", MVLC_FULLSCREEN },
1499 { "volume", MVLC_VOLUME },
1501 /* playlist management */
1502 { "add", MVLC_ADD },
1503 { "delete", MVLC_DEL },
1504 { "empty", MVLC_EMPTY },
1505 { "sort", MVLC_SORT },
1506 { "move", MVLC_MOVE },
1509 { "close", MVLC_CLOSE },
1510 { "shutdown", MVLC_SHUTDOWN },
1513 { "vlm_new", MVLC_VLM_NEW },
1514 { "vlm_setup", MVLC_VLM_SETUP },
1515 { "vlm_del", MVLC_VLM_DEL },
1516 { "vlm_play", MVLC_VLM_PLAY },
1517 { "vlm_pause", MVLC_VLM_PAUSE },
1518 { "vlm_stop", MVLC_VLM_STOP },
1519 { "vlm_seek", MVLC_VLM_SEEK },
1520 { "vlm_load", MVLC_VLM_LOAD },
1521 { "vlm_save", MVLC_VLM_SAVE },
1523 { "rpn", MVLC_RPN },
1524 { "stack", MVLC_STACK },
1526 { "foreach", MVLC_FOREACH },
1527 { "value", MVLC_VALUE },
1530 { "else", MVLC_ELSE },
1531 { "end", MVLC_END },
1532 { "get", MVLC_GET },
1533 { "set", MVLC_SET },
1534 { "int", MVLC_INT },
1535 { "float", MVLC_FLOAT },
1536 { "string", MVLC_STRING },
1539 { NULL, MVLC_UNKNOWN }
1542 static int StrToMacroType( char *name )
1546 if( !name || *name == '\0')
1548 return MVLC_UNKNOWN;
1550 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1552 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1554 return StrToMacroTypeTab[i].i_type;
1557 return MVLC_UNKNOWN;
1560 static void MacroDo( httpd_file_sys_t *p_args,
1562 uint8_t *p_request, int i_request,
1563 uint8_t **pp_data, int *pi_data,
1566 intf_thread_t *p_intf = p_args->p_intf;
1567 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1570 #define ALLOC( l ) \
1572 int __i__ = *pp_dst - *pp_data; \
1574 *pp_data = realloc( *pp_data, *pi_data ); \
1575 *pp_dst = (*pp_data) + __i__; \
1577 #define PRINT( str ) \
1578 ALLOC( strlen( str ) + 1 ); \
1579 *pp_dst += sprintf( *pp_dst, str );
1581 #define PRINTS( str, s ) \
1582 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1584 char * psz_cur = *pp_dst; \
1585 *pp_dst += sprintf( *pp_dst, str, s ); \
1586 while( psz_cur && *psz_cur ) \
1588 /* Prevent script injection */ \
1589 if( *psz_cur == '<' ) *psz_cur = '*'; \
1590 if( *psz_cur == '>' ) *psz_cur = '*'; \
1595 switch( StrToMacroType( m->id ) )
1598 if( i_request <= 0 )
1602 uri_extract_value( p_request, "control", control, 512 );
1603 if( *m->param1 && !strstr( m->param1, control ) )
1605 msg_Warn( p_intf, "unauthorized control=%s", control );
1608 switch( StrToMacroType( control ) )
1615 uri_extract_value( p_request, "item", item, 512 );
1616 i_item = atoi( item );
1617 playlist_Control( p_sys->p_playlist, PLAYLIST_ITEMPLAY,
1618 playlist_ItemGetById( p_sys->p_playlist,
1620 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1624 playlist_Control( p_sys->p_playlist, PLAYLIST_STOP);
1625 msg_Dbg( p_intf, "requested playlist stop" );
1628 playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE );
1629 msg_Dbg( p_intf, "requested playlist pause" );
1632 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, 1 );
1633 msg_Dbg( p_intf, "requested playlist next" );
1636 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, -1);
1637 msg_Dbg( p_intf, "requested playlist next" );
1639 case MVLC_FULLSCREEN:
1641 if( p_sys->p_input )
1643 vout_thread_t *p_vout;
1644 p_vout = vlc_object_find( p_sys->p_input,
1645 VLC_OBJECT_VOUT, FIND_CHILD );
1649 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1650 vlc_object_release( p_vout );
1651 msg_Dbg( p_intf, "requested fullscreen toggle" );
1665 #define POSITION_ABSOLUTE 12
1666 #define POSITION_REL_FOR 13
1667 #define POSITION_REL_BACK 11
1668 #define VL_TIME_ABSOLUTE 0
1669 #define VL_TIME_REL_FOR 1
1670 #define VL_TIME_REL_BACK -1
1671 if( p_sys->p_input )
1673 uri_extract_value( p_request, "seek_value", value, 20 );
1674 uri_decode_url_encoded( value );
1676 var_Get( p_sys->p_input, "length", &val);
1677 i_length = val.i_time;
1679 while( p_value[0] != '\0' )
1685 i_relative = VL_TIME_REL_FOR;
1691 i_relative = VL_TIME_REL_BACK;
1695 case '0': case '1': case '2': case '3': case '4':
1696 case '5': case '6': case '7': case '8': case '9':
1698 i_stock = strtol( p_value , &p_value , 10 );
1701 case '%': /* for percentage ie position */
1703 i_relative += POSITION_ABSOLUTE;
1711 i_value = 60 * (i_value + i_stock) ;
1716 case 'h': case 'H': /* hours */
1718 i_value += 3600 * i_stock;
1720 /* other characters which are not numbers are not important */
1721 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1727 case 'm': case 'M': case '\'': /* minutes */
1729 i_value += 60 * i_stock;
1732 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1738 case 's': case 'S': case '"': /* seconds */
1742 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
1756 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
1761 case VL_TIME_ABSOLUTE:
1763 if( (uint64_t)( i_value ) * 1000000 <= i_length )
1764 val.i_time = (uint64_t)( i_value ) * 1000000;
1766 val.i_time = i_length;
1768 var_Set( p_sys->p_input, "time", val );
1769 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
1772 case VL_TIME_REL_FOR:
1774 var_Get( p_sys->p_input, "time", &val );
1775 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1777 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
1780 val.i_time = i_length;
1782 var_Set( p_sys->p_input, "time", val );
1783 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
1786 case VL_TIME_REL_BACK:
1788 var_Get( p_sys->p_input, "time", &val );
1789 if( (int64_t)( i_value ) * 1000000 > val.i_time )
1794 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
1796 var_Set( p_sys->p_input, "time", val );
1797 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
1800 case POSITION_ABSOLUTE:
1802 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1803 var_Set( p_sys->p_input, "position", val );
1804 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
1807 case POSITION_REL_FOR:
1809 var_Get( p_sys->p_input, "position", &val );
1810 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1811 var_Set( p_sys->p_input, "position", val );
1812 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
1815 case POSITION_REL_BACK:
1817 var_Get( p_sys->p_input, "position", &val );
1818 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
1819 var_Set( p_sys->p_input, "position", val );
1820 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
1825 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
1830 #undef POSITION_ABSOLUTE
1831 #undef POSITION_REL_FOR
1832 #undef POSITION_REL_BACK
1833 #undef VL_TIME_ABSOLUTE
1834 #undef VL_TIME_REL_FOR
1835 #undef VL_TIME_REL_BACK
1841 audio_volume_t i_volume;
1844 uri_extract_value( p_request, "value", vol, 8 );
1845 aout_VolumeGet( p_intf, &i_volume );
1846 uri_decode_url_encoded( vol );
1850 i_value = atoi( vol + 1 );
1851 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1853 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
1854 msg_Dbg( p_intf, "requested volume set: max" );
1857 aout_VolumeSet( p_intf , (i_volume + i_value) );
1858 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
1863 i_value = atoi( vol + 1 );
1864 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
1866 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
1867 msg_Dbg( p_intf, "requested volume set: min" );
1870 aout_VolumeSet( p_intf , (i_volume - i_value) );
1871 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
1874 if( strstr(vol, "%") != NULL )
1876 i_value = atoi( vol );
1877 if( (i_value <= 100) && (i_value>=0) ){
1878 aout_VolumeSet( p_intf, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/100+AOUT_VOLUME_MIN);
1879 msg_Dbg( p_intf, "requested volume set: %i%%", atoi( vol ));
1883 i_value = atoi( vol );
1884 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
1886 aout_VolumeSet( p_intf , atoi( vol ) );
1887 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1893 /* playlist management */
1897 playlist_item_t * p_item;
1899 uri_extract_value( p_request, "mrl", mrl, 512 );
1900 uri_decode_url_encoded( mrl );
1901 p_item = parse_MRL( p_intf, mrl );
1903 if( !p_item || !p_item->input.psz_uri ||
1904 !*p_item->input.psz_uri )
1906 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
1909 playlist_AddItem( p_sys->p_playlist , p_item ,
1910 PLAYLIST_APPEND, PLAYLIST_END );
1911 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
1918 int i_item, *p_items = NULL, i_nb_items = 0;
1919 char item[512], *p_parser = p_request;
1921 /* Get the list of items to delete */
1923 uri_extract_value( p_parser, "item", item, 512 )) )
1925 if( !*item ) continue;
1927 i_item = atoi( item );
1928 p_items = realloc( p_items, (i_nb_items + 1) *
1930 p_items[i_nb_items] = i_item;
1937 for( i = 0; i < i_nb_items; i++ )
1939 playlist_LockDelete( p_sys->p_playlist, p_items[i] );
1940 msg_Dbg( p_intf, "requested playlist delete: %d",
1946 if( p_items ) free( p_items );
1951 int i_item, *p_items = NULL, i_nb_items = 0;
1952 char item[512], *p_parser = p_request;
1955 /* Get the list of items to keep */
1957 uri_extract_value( p_parser, "item", item, 512 )) )
1959 if( !*item ) continue;
1961 i_item = atoi( item );
1962 p_items = realloc( p_items, (i_nb_items + 1) *
1964 p_items[i_nb_items] = i_item;
1968 for( i = p_sys->p_playlist->i_size - 1 ; i >= 0; i-- )
1970 /* Check if the item is in the keep list */
1971 for( j = 0 ; j < i_nb_items ; j++ )
1974 p_sys->p_playlist->pp_items[i]->input.i_id ) break;
1976 if( j == i_nb_items )
1978 playlist_LockDelete( p_sys->p_playlist, p_sys->p_playlist->pp_items[i]->input.i_id );
1979 msg_Dbg( p_intf, "requested playlist delete: %d",
1984 if( p_items ) free( p_items );
1989 playlist_LockClear( p_sys->p_playlist );
1990 msg_Dbg( p_intf, "requested playlist empty" );
2001 uri_extract_value( p_request, "type", type, 12 );
2002 uri_extract_value( p_request, "order", order, 2 );
2003 uri_extract_value( p_request, "item", item, 512 );
2004 i_item = atoi( item );
2006 if( order[0] == '0' ) i_order = ORDER_NORMAL;
2007 else i_order = ORDER_REVERSE;
2009 if( !strcmp( type , "title" ) )
2011 playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
2012 p_sys->p_playlist->pp_views[0]->p_root,
2013 SORT_TITLE_NODES_FIRST,
2014 ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
2015 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
2016 } else if( !strcmp( type , "author" ) )
2018 playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
2019 p_sys->p_playlist->pp_views[0]->p_root,
2021 ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
2022 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
2023 } else if( !strcmp( type , "shuffle" ) )
2025 playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
2026 p_sys->p_playlist->pp_views[0]->p_root,
2028 ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
2029 msg_Dbg( p_intf, "requested playlist shuffle");
2040 uri_extract_value( p_request, "psz_pos", psz_pos, 6 );
2041 uri_extract_value( p_request, "psz_newpos", psz_newpos, 6 );
2042 i_pos = atoi( psz_pos );
2043 i_newpos = atoi( psz_newpos );
2044 if ( i_pos < i_newpos )
2046 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
2048 playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
2050 msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
2054 /* admin function */
2058 uri_extract_value( p_request, "id", id, 512 );
2059 msg_Dbg( p_intf, "requested close id=%s", id );
2061 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
2063 msg_Warn( p_intf, "close failed for id=%s", id );
2070 msg_Dbg( p_intf, "requested shutdown" );
2071 p_intf->p_vlc->b_die = VLC_TRUE;
2076 case MVLC_VLM_SETUP:
2078 static const char *vlm_properties[11] =
2081 "enabled", "disabled", "loop", "unloop",
2083 "input", "output", "option", "date", "period", "repeat", "append",
2085 vlm_message_t *vlm_answer;
2087 char *psz = malloc( strlen( p_request ) + 1000 );
2092 if( p_intf->p_sys->p_vlm == NULL )
2093 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2095 if( p_intf->p_sys->p_vlm == NULL ) break;
2097 uri_extract_value( p_request, "name", name, 512 );
2098 if( StrToMacroType( control ) == MVLC_VLM_NEW )
2101 uri_extract_value( p_request, "type", type, 20 );
2102 p += sprintf( psz, "new %s %s", name, type );
2106 p += sprintf( psz, "setup %s", name );
2108 /* Parse the request */
2109 for( i = 0; i < 11; i++ )
2112 uri_extract_value( p_request, vlm_properties[i], val, 512 );
2113 uri_decode_url_encoded( val );
2114 if( strlen( val ) > 0 && i >= 4 )
2116 p += sprintf( p, " %s %s", vlm_properties[i], val );
2118 else if( uri_test_param( p_request, vlm_properties[i] ) && i < 4 )
2120 p += sprintf( p, " %s", vlm_properties[i] );
2123 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2124 if( vlm_answer->psz_value == NULL ) /* there is no error */
2126 vlm_error = strdup( "" );
2130 vlm_error = malloc( strlen(vlm_answer->psz_name) +
2131 strlen(vlm_answer->psz_value) +
2132 strlen( " : ") + 1 );
2133 sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
2134 vlm_answer->psz_value );
2137 mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
2139 vlm_MessageDelete( vlm_answer );
2147 vlm_message_t *vlm_answer;
2150 if( p_intf->p_sys->p_vlm == NULL )
2151 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2153 if( p_intf->p_sys->p_vlm == NULL ) break;
2155 uri_extract_value( p_request, "name", name, 512 );
2156 sprintf( psz, "del %s", name );
2158 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2159 /* FIXME do a vlm_answer -> var stack conversion */
2160 vlm_MessageDelete( vlm_answer );
2165 case MVLC_VLM_PAUSE:
2169 vlm_message_t *vlm_answer;
2172 if( p_intf->p_sys->p_vlm == NULL )
2173 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2175 if( p_intf->p_sys->p_vlm == NULL ) break;
2177 uri_extract_value( p_request, "name", name, 512 );
2178 if( StrToMacroType( control ) == MVLC_VLM_PLAY )
2179 sprintf( psz, "control %s play", name );
2180 else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
2181 sprintf( psz, "control %s pause", name );
2182 else if( StrToMacroType( control ) == MVLC_VLM_STOP )
2183 sprintf( psz, "control %s stop", name );
2184 else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
2187 uri_extract_value( p_request, "percent", percent, 512 );
2188 sprintf( psz, "control %s seek %s", name, percent );
2191 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2192 /* FIXME do a vlm_answer -> var stack conversion */
2193 vlm_MessageDelete( vlm_answer );
2199 vlm_message_t *vlm_answer;
2203 if( p_intf->p_sys->p_vlm == NULL )
2204 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2206 if( p_intf->p_sys->p_vlm == NULL ) break;
2208 uri_extract_value( p_request, "file", file, 512 );
2209 uri_decode_url_encoded( file );
2211 if( StrToMacroType( control ) == MVLC_VLM_LOAD )
2212 sprintf( psz, "load %s", file );
2214 sprintf( psz, "save %s", file );
2216 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
2217 /* FIXME do a vlm_answer -> var stack conversion */
2218 vlm_MessageDelete( vlm_answer );
2225 PRINTS( "<!-- control param(%s) unsuported -->", control );
2237 if( i_request <= 0 ||
2238 *m->param1 == '\0' ||
2239 strstr( p_request, m->param1 ) == NULL )
2243 uri_extract_value( p_request, m->param1, value, 512 );
2244 uri_decode_url_encoded( value );
2246 switch( StrToMacroType( m->param2 ) )
2250 config_PutInt( p_intf, m->param1, i );
2254 config_PutFloat( p_intf, m->param1, f );
2257 config_PutPsz( p_intf, m->param1, value );
2260 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
2271 if( *m->param1 == '\0' )
2276 switch( StrToMacroType( m->param2 ) )
2279 i = config_GetInt( p_intf, m->param1 );
2280 sprintf( value, "%i", i );
2283 f = config_GetFloat( p_intf, m->param1 );
2284 sprintf( value, "%f", f );
2287 psz = config_GetPsz( p_intf, m->param1 );
2288 sprintf( value, "%s", psz ? psz : "" );
2289 if( psz ) free( psz );
2292 sprintf( value, "invalid type(%s) in set", m->param2 );
2295 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
2296 PRINTS( "%s", value );
2305 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2306 s = SSPop( &p_args->stack );
2307 v = mvar_GetValue( p_args->vars, s );
2311 v = s = SSPop( &p_args->stack );
2319 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
2322 /* Usefull for learning stack management */
2326 msg_Dbg( p_intf, "stack" );
2327 for (i=0;i<(&p_args->stack)->i_stack;i++)
2328 msg_Dbg( p_intf, "%d -> %s", i, (&p_args->stack)->stack[i] );
2334 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
2335 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
2343 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
2350 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
2355 i_skip = MacroParse( &m, src );
2357 i_id = StrToMacroType( m.id );
2374 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
2375 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
2377 return src + ( b_after ? i_skip : 0 );
2379 else if( i_level < 0 )
2395 static void Execute( httpd_file_sys_t *p_args,
2396 uint8_t *p_request, int i_request,
2397 uint8_t **pp_data, int *pi_data,
2399 uint8_t *_src, uint8_t *_end )
2401 intf_thread_t *p_intf = p_args->p_intf;
2403 uint8_t *src, *dup, *end;
2404 uint8_t *dst = *pp_dst;
2406 src = dup = malloc( _end - _src + 1 );
2407 end = src +( _end - _src );
2409 memcpy( src, _src, _end - _src );
2412 /* we parse searching <vlc */
2418 p = strstr( src, "<vlc" );
2419 if( p < end && p == src )
2423 src += MacroParse( &m, src );
2425 //msg_Dbg( p_intf, "macro_id=%s", m.id );
2427 switch( StrToMacroType( m.id ) )
2434 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
2435 if( SSPopN( &p_args->stack, p_args->vars ) )
2443 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2447 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
2451 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
2454 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2458 else if( i_test == 1 )
2461 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
2463 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
2467 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
2476 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
2477 uint8_t *start = src;
2478 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
2485 if( !strcmp( m.param2, "integer" ) )
2487 char *arg = SSPop( &p_args->stack );
2488 index = mvar_IntegerSetNew( m.param1, arg );
2491 else if( !strcmp( m.param2, "directory" ) )
2493 char *arg = SSPop( &p_args->stack );
2494 index = mvar_FileSetNew( m.param1, arg );
2497 else if( !strcmp( m.param2, "playlist" ) )
2499 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
2501 else if( !strcmp( m.param2, "information" ) )
2503 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
2505 else if( !strcmp( m.param2, "vlm" ) )
2507 if( p_intf->p_sys->p_vlm == NULL )
2508 p_intf->p_sys->p_vlm = vlm_New( p_intf );
2509 index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
2512 else if( !strcmp( m.param2, "hosts" ) )
2514 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
2516 else if( !strcmp( m.param2, "urls" ) )
2518 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
2520 else if( !strcmp( m.param2, "connections" ) )
2522 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
2525 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
2527 index = mvar_Duplicate( v );
2531 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
2536 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
2538 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
2540 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
2543 f->name = strdup( m.param1 );
2546 mvar_PushVar( p_args->vars, f );
2547 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
2548 mvar_RemoveVar( p_args->vars, f );
2552 mvar_Delete( index );
2559 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
2567 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
2570 int i_index = dst - *pp_data;
2573 *pp_data = realloc( *pp_data, *pi_data );
2574 dst = (*pp_data) + i_index;
2576 memcpy( dst, src, i_copy );
2586 /****************************************************************************
2588 ****************************************************************************
2589 * a file with b_html is parsed and all "macro" replaced
2590 * <vlc id="macro name" [param1="" [param2=""]] />
2593 ****************************************************************************/
2594 static int HttpCallback( httpd_file_sys_t *p_args,
2595 httpd_file_t *p_file,
2597 uint8_t **pp_data, int *pi_data )
2599 int i_request = p_request ? strlen( p_request ) : 0;
2603 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
2605 p = *pp_data = malloc( 10240 );
2608 return VLC_EGENERIC;
2610 p += sprintf( p, "<html>\n" );
2611 p += sprintf( p, "<head>\n" );
2612 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
2613 p += sprintf( p, "</head>\n" );
2614 p += sprintf( p, "<body>\n" );
2615 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
2616 p += sprintf( p, "<hr />\n" );
2617 p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
2618 p += sprintf( p, "</body>\n" );
2619 p += sprintf( p, "</html>\n" );
2621 *pi_data = strlen( *pp_data );
2626 if( !p_args->b_html )
2628 FileLoad( f, pp_data, pi_data );
2636 char position[4]; /* percentage */
2637 char time[12]; /* in seconds */
2638 char length[12]; /* in seconds */
2639 audio_volume_t i_volume;
2643 #define p_sys p_args->p_intf->p_sys
2644 if( p_sys->p_input )
2646 var_Get( p_sys->p_input, "position", &val);
2647 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
2648 var_Get( p_sys->p_input, "time", &val);
2649 sprintf( time, "%d" , (int)(val.i_time / 1000000) );
2650 var_Get( p_sys->p_input, "length", &val);
2651 sprintf( length, "%d" , (int)(val.i_time / 1000000) );
2653 var_Get( p_sys->p_input, "state", &val );
2654 if( val.i_int == PLAYING_S )
2656 sprintf( state, "playing" );
2657 } else if( val.i_int == PAUSE_S )
2659 sprintf( state, "paused" );
2662 sprintf( state, "stop" );
2666 sprintf( position, "%d", 0 );
2667 sprintf( time, "%d", 0 );
2668 sprintf( length, "%d", 0 );
2669 sprintf( state, "stop" );
2673 aout_VolumeGet( p_args->p_intf , &i_volume );
2674 sprintf( volume , "%d" , (int)i_volume );
2676 p_args->vars = mvar_New( "variables", "" );
2677 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
2678 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
2679 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
2680 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
2681 mvar_AppendNewVar( p_args->vars, "stream_position", position );
2682 mvar_AppendNewVar( p_args->vars, "stream_time", time );
2683 mvar_AppendNewVar( p_args->vars, "stream_length", length );
2684 mvar_AppendNewVar( p_args->vars, "volume", volume );
2685 mvar_AppendNewVar( p_args->vars, "stream_state", state );
2687 SSInit( &p_args->stack );
2689 /* first we load in a temporary buffer */
2690 FileLoad( f, &p_buffer, &i_buffer );
2692 /* allocate output */
2693 *pi_data = i_buffer + 1000;
2694 dst = *pp_data = malloc( *pi_data );
2696 /* we parse executing all <vlc /> macros */
2697 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
2700 *pi_data = dst - *pp_data;
2702 SSClean( &p_args->stack );
2703 mvar_Delete( p_args->vars );
2712 /****************************************************************************
2714 ****************************************************************************/
2715 static int uri_test_param( char *psz_uri, const char *psz_name )
2719 while( (p = strstr( p, psz_name )) )
2721 /* Verify that we are dealing with a post/get argument */
2722 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2731 static char *uri_extract_value( char *psz_uri, const char *psz_name,
2732 char *psz_value, int i_value_max )
2736 while( (p = strstr( p, psz_name )) )
2738 /* Verify that we are dealing with a post/get argument */
2739 if( p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n' )
2748 p += strlen( psz_name );
2749 if( *p == '=' ) p++;
2751 if( strchr( p, '&' ) )
2753 i_len = strchr( p, '&' ) - p;
2757 /* for POST method */
2758 if( strchr( p, '\n' ) )
2760 i_len = strchr( p, '\n' ) - p;
2761 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
2765 i_len = strlen( p );
2768 i_len = __MIN( i_value_max - 1, i_len );
2771 strncpy( psz_value, p, i_len );
2772 psz_value[i_len] = '\0';
2776 strncpy( psz_value, "", i_value_max );
2782 strncpy( psz_value, "", i_value_max );
2788 static void uri_decode_url_encoded( char *psz )
2790 char *dup = strdup( psz );
2808 *psz++ = strtol( val, NULL, 16 );
2810 else if( *p == '+' )
2824 /****************************************************************************
2825 * Light RPN evaluator
2826 ****************************************************************************/
2827 static void SSInit( rpn_stack_t *st )
2832 static void SSClean( rpn_stack_t *st )
2834 while( st->i_stack > 0 )
2836 free( st->stack[--st->i_stack] );
2840 static void SSPush( rpn_stack_t *st, char *s )
2842 if( st->i_stack < STACK_MAX )
2844 st->stack[st->i_stack++] = strdup( s );
2848 static char * SSPop( rpn_stack_t *st )
2850 if( st->i_stack <= 0 )
2852 return strdup( "" );
2856 return st->stack[--st->i_stack];
2860 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2869 i = strtol( name, &end, 0 );
2872 value = mvar_GetValue( vars, name );
2880 static void SSPushN( rpn_stack_t *st, int i )
2884 sprintf( v, "%d", i );
2888 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2895 while( *exp == ' ' )
2902 /* extract string */
2905 while( *exp && *exp != '\'' )
2916 p = strchr( exp, ' ' );
2921 exp += strlen( exp );
2926 strncpy( s, exp, i );
2937 /* 1. Integer function */
2938 if( !strcmp( s, "!" ) )
2940 SSPushN( st, ~SSPopN( st, vars ) );
2942 else if( !strcmp( s, "^" ) )
2944 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2946 else if( !strcmp( s, "&" ) )
2948 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2950 else if( !strcmp( s, "|" ) )
2952 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2954 else if( !strcmp( s, "+" ) )
2956 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2958 else if( !strcmp( s, "-" ) )
2960 int j = SSPopN( st, vars );
2961 int i = SSPopN( st, vars );
2962 SSPushN( st, i - j );
2964 else if( !strcmp( s, "*" ) )
2966 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2968 else if( !strcmp( s, "/" ) )
2972 j = SSPopN( st, vars );
2973 i = SSPopN( st, vars );
2975 SSPushN( st, j != 0 ? i / j : 0 );
2977 else if( !strcmp( s, "%" ) )
2981 j = SSPopN( st, vars );
2982 i = SSPopN( st, vars );
2984 SSPushN( st, j != 0 ? i % j : 0 );
2986 /* 2. integer tests */
2987 else if( !strcmp( s, "=" ) )
2989 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2991 else if( !strcmp( s, "!=" ) )
2993 SSPushN( st, SSPopN( st, vars ) != SSPopN( st, vars ) ? -1 : 0 );
2995 else if( !strcmp( s, "<" ) )
2997 int j = SSPopN( st, vars );
2998 int i = SSPopN( st, vars );
3000 SSPushN( st, i < j ? -1 : 0 );
3002 else if( !strcmp( s, ">" ) )
3004 int j = SSPopN( st, vars );
3005 int i = SSPopN( st, vars );
3007 SSPushN( st, i > j ? -1 : 0 );
3009 else if( !strcmp( s, "<=" ) )
3011 int j = SSPopN( st, vars );
3012 int i = SSPopN( st, vars );
3014 SSPushN( st, i <= j ? -1 : 0 );
3016 else if( !strcmp( s, ">=" ) )
3018 int j = SSPopN( st, vars );
3019 int i = SSPopN( st, vars );
3021 SSPushN( st, i >= j ? -1 : 0 );
3023 /* 3. string functions */
3024 else if( !strcmp( s, "strcat" ) )
3026 char *s2 = SSPop( st );
3027 char *s1 = SSPop( st );
3028 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
3038 else if( !strcmp( s, "strcmp" ) )
3040 char *s2 = SSPop( st );
3041 char *s1 = SSPop( st );
3043 SSPushN( st, strcmp( s1, s2 ) );
3047 else if( !strcmp( s, "strncmp" ) )
3049 int n = SSPopN( st, vars );
3050 char *s2 = SSPop( st );
3051 char *s1 = SSPop( st );
3053 SSPushN( st, strncmp( s1, s2 , n ) );
3057 else if( !strcmp( s, "strsub" ) )
3059 int n = SSPopN( st, vars );
3060 int m = SSPopN( st, vars );
3062 char *s = SSPop( st );
3074 str = malloc( i_len + 1 );
3076 memcpy( str, s + m - 1, i_len );
3077 str[ i_len ] = '\0';
3083 else if( !strcmp( s, "strlen" ) )
3085 char *str = SSPop( st );
3087 SSPushN( st, strlen( str ) );
3090 /* 4. stack functions */
3091 else if( !strcmp( s, "dup" ) )
3093 char *str = SSPop( st );
3098 else if( !strcmp( s, "drop" ) )
3100 char *str = SSPop( st );
3103 else if( !strcmp( s, "swap" ) )
3105 char *s1 = SSPop( st );
3106 char *s2 = SSPop( st );
3113 else if( !strcmp( s, "flush" ) )
3118 else if( !strcmp( s, "store" ) )
3120 char *value = SSPop( st );
3121 char *name = SSPop( st );
3123 mvar_PushNewVar( vars, name, value );
3127 else if( !strcmp( s, "value" ) )
3129 char *name = SSPop( st );
3130 char *value = mvar_GetValue( vars, name );
3132 SSPush( st, value );
3136 else if( !strcmp( s, "url_extract" ) )
3138 char *url = mvar_GetValue( vars, "url_value" );
3139 char *name = SSPop( st );
3142 uri_extract_value( url, name, value, 512 );
3143 uri_decode_url_encoded( value );
3144 SSPush( st, value );
3153 /**********************************************************************
3154 * Find_end_MRL: Find the end of the sentence :
3155 * this function parses the string psz and find the end of the item
3156 * and/or option with detecting the " and ' problems.
3157 * returns NULL if an error is detected, otherwise, returns a pointer
3158 * of the end of the sentence (after the last character)
3159 **********************************************************************/
3160 static char *Find_end_MRL( char *psz )
3170 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
3172 if( *s_sent == '\'' )
3174 s_sent = Find_end_MRL( s_sent );
3176 if( s_sent == NULL )
3186 if( *s_sent == '\"' )
3190 } else /* *s_sent == '\0' , which means the number of " is incorrect */
3200 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
3202 if( *s_sent == '\"' )
3204 s_sent = Find_end_MRL( s_sent );
3206 if( s_sent == NULL )
3216 if( *s_sent == '\'' )
3220 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
3226 default: /* now we can look for spaces */
3228 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
3230 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
3232 s_sent = Find_end_MRL( s_sent );
3243 /**********************************************************************
3244 * parse_MRL: parse the MRL, find the mrl string and the options,
3245 * create an item with all information in it, and return the item.
3246 * return NULL if there is an error.
3247 **********************************************************************/
3248 static playlist_item_t *parse_MRL( intf_thread_t *p_intf, char *psz )
3250 char **ppsz_options = NULL;
3257 playlist_item_t * p_item = NULL;
3259 /* In case there is spaces before the mrl */
3260 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
3265 /* extract the mrl */
3266 s_temp = strstr( s_mrl , " :" );
3267 if( s_temp == NULL )
3269 s_temp = s_mrl + strlen( s_mrl );
3272 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
3279 /* if the mrl is between " or ', we must remove them */
3280 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
3282 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
3283 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
3284 mrl[ s_temp - s_mrl - 2 ] = '\0';
3287 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
3288 strncpy( mrl , s_mrl , s_temp - s_mrl );
3289 mrl[ s_temp - s_mrl ] = '\0';
3294 /* now we can take care of the options */
3295 while( (*s_mrl != '\0') && (i_error == 0) )
3304 case ':': /* an option */
3306 s_temp = Find_end_MRL( s_mrl );
3308 if( s_temp == NULL )
3315 ppsz_options = realloc( ppsz_options , i_options *
3317 ppsz_options[ i_options - 1 ] =
3318 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
3320 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
3323 /* don't forget to finish the string with a '\0' */
3324 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
3344 /* now create an item */
3345 p_item = playlist_ItemNew( p_intf, mrl, mrl);
3346 for( i = 0 ; i< i_options ; i++ )
3348 playlist_ItemAddOption( p_item, ppsz_options[i] );
3352 for( i = 0; i < i_options; i++ ) free( ppsz_options[i] );
3353 if( i_options ) free( ppsz_options );