1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: http.c,v 1.12 2003/07/11 09:50:10 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
46 #elif defined( WIN32 ) && !defined( UNDER_CE )
50 #if (!defined( WIN32 ) || defined(__MINGW32__))
51 /* Mingw has its own version of dirent */
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Activate ( vlc_object_t * );
60 static void Close ( vlc_object_t * );
61 static void Run ( intf_thread_t *p_intf );
63 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
66 static int http_get( httpd_file_callback_args_t *p_args,
67 uint8_t *p_request, int i_request,
68 uint8_t **pp_data, int *pi_data );
70 static void uri_extract_value( char *psz_uri, char *psz_name,
71 char *psz_value, int i_value_max );
72 static void uri_decode_url_encoded( char *psz );
74 /*****************************************************************************
76 *****************************************************************************/
83 struct mvar_s **field;
89 char *stack[STACK_MAX];
93 struct httpd_file_callback_args_t
95 intf_thread_t *p_intf;
102 /* inited for each access */
110 httpd_host_t *p_httpd_host;
113 httpd_file_callback_args_t **pp_files;
115 playlist_t *p_playlist;
116 input_thread_t *p_input;
120 /*****************************************************************************
122 *****************************************************************************/
123 #define HOST_TEXT N_( "Host address" )
124 #define HOST_LONGTEXT N_( \
125 "You can set the address and port on which the http interface will bind" )
126 #define SRC_TEXT N_( "Source directory" )
127 #define SRC_LONGTEXT N_( "Source directory" )
130 set_description( _("HTTP remote control interface") );
131 add_category_hint( N_("HTTP remote control"), NULL, VLC_TRUE );
132 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
133 #if defined(SYS_DARWIN) || defined(SYS_BEOS) \
134 || (defined(WIN32) && !defined(UNDER_CE))
135 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
137 add_string ( "http-src", "share/http", NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
139 set_capability( "interface", 0 );
140 set_callbacks( Activate, Close );
143 /*****************************************************************************
144 * Activate: initialize and create stuff
145 *****************************************************************************/
146 static int Activate( vlc_object_t *p_this )
148 intf_thread_t *p_intf = (intf_thread_t*)p_this;
151 char *psz_address = "";
155 psz_host = config_GetPsz( p_intf, "http-host" );
159 psz_address = psz_host;
161 psz_parser = strchr( psz_host, ':' );
164 *psz_parser++ = '\0';
165 i_port = atoi( psz_parser );
173 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
174 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
175 p_sys->p_playlist = NULL;
176 p_sys->p_input = NULL;
178 if( ( p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_intf), VLC_TRUE ) ) == NULL )
180 msg_Err( p_intf, "cannot create/find httpd" );
185 if( ( p_sys->p_httpd_host =
186 p_sys->p_httpd->pf_register_host( p_sys->p_httpd,
187 psz_address, i_port ) ) == NULL )
189 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
190 httpd_Release( p_sys->p_httpd );
201 p_sys->pp_files = malloc( sizeof( httpd_file_callback_args_t *) );
203 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
204 ( defined(WIN32) && !defined(UNDER_CE ) )
205 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
207 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
208 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
209 sprintf( psz_src, "%s/share/http", psz_vlcpath);
212 psz_src = config_GetPsz( p_intf, "http-src" );
214 if( !psz_src || *psz_src == '\0' )
216 msg_Err( p_intf, "invalid src dir" );
220 /* remove trainling \ or / */
221 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
222 psz_src[strlen( psz_src ) - 1] == '/' )
224 psz_src[strlen( psz_src ) - 1] = '\0';
227 ParseDirectory( p_intf, psz_src, psz_src );
230 if( p_sys->i_files <= 0 )
232 msg_Err( p_intf, "cannot find any files" );
235 p_intf->pf_run = Run;
240 free( p_sys->pp_files );
241 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
242 p_sys->p_httpd_host );
243 httpd_Release( p_sys->p_httpd );
248 /*****************************************************************************
249 * CloseIntf: destroy interface
250 *****************************************************************************/
251 void Close ( vlc_object_t *p_this )
253 intf_thread_t *p_intf = (intf_thread_t *)p_this;
254 intf_sys_t *p_sys = p_intf->p_sys;
258 for( i = 0; i < p_sys->i_files; i++ )
260 p_sys->p_httpd->pf_unregister_file( p_sys->p_httpd,
261 p_sys->pp_files[i]->p_file );
262 /* do not free mime */
263 free( p_sys->pp_files[i]->file );
264 free( p_sys->pp_files[i]->name );
265 free( p_sys->pp_files[i] );
267 free( p_sys->pp_files );
268 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
269 p_sys->p_httpd_host );
270 httpd_Release( p_sys->p_httpd );
274 /*****************************************************************************
275 * Run: http interface thread
276 *****************************************************************************/
277 static void Run( intf_thread_t *p_intf )
279 intf_sys_t *p_sys = p_intf->p_sys;
281 while( !p_intf->b_die )
283 /* get the playlist */
284 if( p_sys->p_playlist == NULL )
286 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
289 /* Manage the input part */
290 if( p_sys->p_input == NULL )
292 if( p_sys->p_playlist )
295 vlc_object_find( p_sys->p_playlist,
300 else if( p_sys->p_input->b_dead )
302 vlc_object_release( p_sys->p_input );
303 p_sys->p_input = NULL;
308 msleep( INTF_IDLE_SLEEP );
313 vlc_object_release( p_sys->p_input );
314 p_sys->p_input = NULL;
317 if( p_sys->p_playlist )
319 vlc_object_release( p_sys->p_playlist );
320 p_sys->p_playlist = NULL;
325 /*****************************************************************************
327 *****************************************************************************/
328 #define MAX_DIR_SIZE 10240
330 /****************************************************************************
331 * FileToUrl: create a good name for an url from filename
332 ****************************************************************************/
333 static char *FileToUrl( char *name )
337 url = p = malloc( strlen( name ) + 1 );
340 while( *name == '\\' || *name == '/' )
342 while( *name == '\\' )
352 /* convert '\\' into '/' */
365 if( ( p = strrchr( url, '/' ) ) != NULL )
367 if( !strncmp( p, "/index.", 7 ) )
375 /****************************************************************************
376 * FileToMime: XXX duplicated with modules/access_out/http.c
377 ****************************************************************************/
384 { ".htm", "text/html" },
385 { ".html", "text/html" },
388 { ".avi", "video/avi" },
389 { ".asf", "video/x-ms-asf" },
390 { ".m1a", "audio/mpeg" },
391 { ".m2a", "audio/mpeg" },
392 { ".m1v", "video/mpeg" },
393 { ".m2v", "video/mpeg" },
394 { ".mp2", "audio/mpeg" },
395 { ".mp3", "audio/mpeg" },
396 { ".mpa", "audio/mpeg" },
397 { ".mpg", "video/mpeg" },
398 { ".mpeg", "video/mpeg" },
399 { ".mpe", "video/mpeg" },
400 { ".mov", "video/quicktime" },
401 { ".moov", "video/quicktime" },
402 { ".ogg", "application/ogg" },
403 { ".ogm", "application/ogg" },
404 { ".wav", "audio/wav" },
410 static char *FileToMime( char *psz_name )
414 psz_ext = strrchr( psz_name, '.' );
419 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
421 if( !strcmp( http_mime[i].psz_ext, psz_ext ) )
423 return( http_mime[i].psz_mime );
427 return( "application/octet-stream" );
430 /****************************************************************************
431 * ParseDirectory: parse recursively a directory, adding each file
432 ****************************************************************************/
433 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
436 intf_sys_t *p_sys = p_intf->p_sys;
437 char dir[MAX_DIR_SIZE];
438 #ifdef HAVE_SYS_STAT_H
439 struct stat stat_info;
442 struct dirent *p_dir_content;
446 char *password = NULL;
448 #ifdef HAVE_SYS_STAT_H
449 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
455 if( ( p_dir = opendir( psz_dir ) ) == NULL )
457 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
461 msg_Dbg( p_intf, "dir=%s", psz_dir );
463 sprintf( dir, "%s/.access", psz_dir );
464 if( ( file = fopen( dir, "r" ) ) != NULL )
469 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
471 i_size = fread( line, 1, 1023, file );
475 while( i_size > 0 && ( line[i_size-1] == '\n' ||
476 line[i_size-1] == '\r' ) )
483 p = strchr( line, ':' );
487 user = strdup( line );
488 password = strdup( p );
491 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
492 user, password, i_size );
499 /* parse psz_src dir */
500 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
505 if( p_dir_content->d_name[0] == '.' )
509 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
510 if( ParseDirectory( p_intf, psz_root, dir ) )
512 #define f p_sys->pp_files[p_sys->i_files]
513 f = malloc( sizeof( httpd_file_callback_args_t ) );
515 f->file = strdup( dir );
516 f->name = FileToUrl( &dir[strlen( psz_root )] );
517 f->mime = FileToMime( &dir[strlen( psz_root )] );
519 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)",
520 f->file, f->name, f->mime );
523 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
531 p_sys->pp_files = realloc( p_sys->pp_files,
532 (p_sys->i_files+1) * sizeof( httpd_file_callback_args_t ) );
534 #define fold p_sys->pp_files[p_sys->i_files-1]
536 /* FIXME for rep/ add rep (it would be better to do a redirection) */
537 if( strlen(fold->name) > 1 &&
538 fold->name[strlen(fold->name) - 1] == '/' )
540 f = malloc( sizeof( httpd_file_callback_args_t ) );
542 f->file = strdup( fold->file );
543 f->name = strdup( fold->name );
544 f->mime = fold->mime;
546 f->name[strlen(f->name) - 1] = '\0';
547 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)", f->file, f->name,
550 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
559 realloc( p_sys->pp_files, (p_sys->i_files+1) *
560 sizeof( httpd_file_callback_args_t ) );
579 /****************************************************************************
580 * var and set handling
581 ****************************************************************************/
583 static mvar_t *mvar_New( char *name, char *value )
585 mvar_t *v = malloc( sizeof( mvar_t ) );
587 v->name = strdup( name );
588 v->value = strdup( value ? value : "" );
591 v->field = malloc( sizeof( mvar_t * ) );
597 static void mvar_Delete( mvar_t *v )
604 for( i = 0; i < v->i_field; i++ )
606 mvar_Delete( v->field[i] );
612 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
614 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
615 v->field[v->i_field] = f;
619 static mvar_t *mvar_Duplicate( mvar_t *v )
624 n = mvar_New( v->name, v->value );
625 for( i = 0; i < v->i_field; i++ )
627 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
633 static void mvar_PushVar( mvar_t *v, mvar_t *f )
635 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
638 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
644 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
647 for( i = 0; i < v->i_field; i++ )
649 if( v->field[i] == f )
654 if( i >= v->i_field )
659 if( i + 1 < v->i_field )
661 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
664 /* FIXME should do a realloc */
667 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
670 char base[512], *field, *p;
673 /* format: name[index].field */
675 field = strchr( name, '.' );
678 int i = field - name;
679 strncpy( base, name, i );
685 strcpy( base, name );
688 if( ( p = strchr( base, '[' ) ) )
691 sscanf( p, "%d]", &i_index );
702 for( i = 0; i < s->i_field; i++ )
704 if( !strcmp( s->field[i]->name, base ) )
714 return mvar_GetVar( s->field[i], field );
728 static char *mvar_GetValue( mvar_t *v, char *field )
736 mvar_t *f = mvar_GetVar( v, field );
748 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
750 mvar_t *f = mvar_New( name, value );
751 mvar_PushVar( vars, f );
754 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
756 mvar_t *f = mvar_New( name, value );
757 mvar_AppendVar( vars, f );
761 /* arg= start[:stop[:step]],.. */
762 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
764 char *dup = strdup( arg );
766 mvar_t *s = mvar_New( name, "set" );
768 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
773 int i_start,i_stop,i_step;
776 p = strchr( str, ',' );
783 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
784 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
791 else if( i_match == 2 )
793 i_step = i_start < i_stop ? 1 : -1;
800 if( ( i_start < i_stop && i_step > 0 ) ||
801 ( i_start > i_stop && i_step < 0 ) )
803 for( i = i_start; ; i += i_step )
807 if( ( i_step > 0 && i > i_stop ) ||
808 ( i_step < 0 && i < i_stop ) )
813 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
814 sprintf( value, "%d", i );
816 mvar_PushNewVar( s, name, value );
827 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
829 mvar_t *s = mvar_New( name, "set" );
832 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
834 vlc_mutex_lock( &p_pl->object_lock );
835 for( i = 0; i < p_pl->i_size; i++ )
837 mvar_t *itm = mvar_New( name, "set" );
840 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
841 mvar_AppendNewVar( itm, "current", value );
843 sprintf( value, "%d", i );
844 mvar_AppendNewVar( itm, "index", value );
846 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->psz_name );
848 mvar_AppendVar( s, itm );
850 vlc_mutex_unlock( &p_pl->object_lock );
855 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
857 mvar_t *s = mvar_New( name, "set" );
859 input_info_category_t * p_category;
860 input_info_t * p_info;
862 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
863 if( p_input == NULL )
868 vlc_mutex_lock( &p_input->stream.stream_lock );
869 p_category = p_input->stream.p_info;
872 mvar_t *cat = mvar_New( name, "set" );
873 mvar_t *iset = mvar_New( "info", "set" );
875 mvar_AppendNewVar( cat, "name", p_category->psz_name );
876 mvar_AppendVar( cat, iset );
878 p_info = p_category->p_info;
881 mvar_t *info = mvar_New( "info", "" );
883 msg_Dbg( p_input, "adding info name=%s value=%s", p_info->psz_name, p_info->psz_value );
884 mvar_AppendNewVar( info, "name", p_info->psz_name );
885 mvar_AppendNewVar( info, "value", p_info->psz_value );
886 mvar_AppendVar( iset, info );
887 p_info = p_info->p_next;
889 mvar_AppendVar( s, cat );
890 p_category = p_category->p_next;
892 vlc_mutex_unlock( &p_input->stream.stream_lock );
897 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
899 mvar_t *s = mvar_New( name, "set" );
903 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
904 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
906 for( i= 0; i < info.i_count; )
910 inf = mvar_New( name, "set" );
913 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
914 info.info[i].psz_name, info.info[i].psz_value ); */
915 mvar_AppendNewVar( inf,
916 info.info[i].psz_name,
917 info.info[i].psz_value );
919 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
920 mvar_AppendVar( s, inf );
925 for( i = 0; i < info.i_count; i++ )
927 free( info.info[i].psz_name );
928 free( info.info[i].psz_value );
933 static void SSInit( stack_t * );
934 static void SSClean( stack_t * );
935 static void EvaluateRPN( mvar_t *, stack_t *, char * );
937 static void SSPush ( stack_t *, char * );
938 static char *SSPop ( stack_t * );
940 static void SSPushN ( stack_t *, int );
941 static int SSPopN ( stack_t *, mvar_t * );
944 /****************************************************************************
946 ****************************************************************************/
954 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
958 /* just load the file */
960 *pp_data = malloc( 1025 ); /* +1 for \0 */
961 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
964 *pp_data = realloc( *pp_data, *pi_data + 1025 );
970 (*pp_data)[*pi_data] = '\0';
975 static int MacroParse( macro_t *m, uint8_t *psz_src )
977 uint8_t *dup = strdup( psz_src );
982 #define EXTRACT( name, l ) \
984 p = strchr( src, '"' ); \
989 m->name = strdup( src ); \
1010 if( !strncmp( src, "id=\"", 4 ) )
1014 else if( !strncmp( src, "param1=\"", 8 ) )
1016 EXTRACT( param1, 8 );
1018 else if( !strncmp( src, "param2=\"", 8 ) )
1020 EXTRACT( param2, 8 );
1027 if( strstr( src, "/>" ) )
1029 src = strstr( src, "/>" ) + 2;
1033 src += strlen( src );
1038 m->id = strdup( "" );
1040 if( m->param1 == NULL )
1042 m->param1 = strdup( "" );
1044 if( m->param2 == NULL )
1046 m->param2 = strdup( "" );
1055 static void MacroClean( macro_t *m )
1094 StrToMacroTypeTab [] =
1096 { "control", MVLC_CONTROL },
1097 /* player control */
1098 { "play", MVLC_PLAY },
1099 { "stop", MVLC_STOP },
1100 { "pause", MVLC_PAUSE },
1101 { "next", MVLC_NEXT },
1102 { "prevous", MVLC_PREVIOUS },
1103 { "add", MVLC_ADD },
1106 { "close", MVLC_CLOSE },
1107 { "shutdown", MVLC_SHUTDOWN },
1109 { "rpn", MVLC_RPN },
1111 { "foreach", MVLC_FOREACH },
1112 { "value", MVLC_VALUE },
1115 { "else", MVLC_ELSE },
1116 { "end", MVLC_END },
1117 { "get", MVLC_GET },
1118 { "set", MVLC_SET },
1119 { "int", MVLC_INT },
1120 { "float", MVLC_FLOAT },
1121 { "string", MVLC_STRING },
1124 { NULL, MVLC_UNKNOWN }
1127 static int StrToMacroType( char *name )
1131 if( !name || *name == '\0')
1133 return MVLC_UNKNOWN;
1135 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1137 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1139 return StrToMacroTypeTab[i].i_type;
1142 return MVLC_UNKNOWN;
1145 static void MacroDo( httpd_file_callback_args_t *p_args,
1147 uint8_t *p_request, int i_request,
1148 uint8_t **pp_data, int *pi_data,
1151 intf_thread_t *p_intf = p_args->p_intf;
1152 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1155 #define ALLOC( l ) \
1157 int __i__ = *pp_dst - *pp_data; \
1159 *pp_data = realloc( *pp_data, *pi_data ); \
1160 *pp_dst = (*pp_data) + __i__; \
1162 #define PRINT( str ) \
1163 ALLOC( strlen( str ) + 1 ); \
1164 *pp_dst += sprintf( *pp_dst, str );
1166 #define PRINTS( str, s ) \
1167 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1168 *pp_dst += sprintf( *pp_dst, str, s );
1170 switch( StrToMacroType( m->id ) )
1173 if( i_request <= 0 )
1177 uri_extract_value( p_request, "control", control, 512 );
1178 if( *m->param1 && !strstr( m->param1, control ) )
1180 msg_Warn( p_intf, "unauthorized control=%s", control );
1183 switch( StrToMacroType( control ) )
1190 uri_extract_value( p_request, "item", item, 512 );
1191 i_item = atoi( item );
1192 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
1193 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1197 playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1198 msg_Dbg( p_intf, "requested playlist stop" );
1201 playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1202 msg_Dbg( p_intf, "requested playlist pause" );
1205 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1206 p_sys->p_playlist->i_index + 1 );
1207 msg_Dbg( p_intf, "requested playlist next" );
1210 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1211 p_sys->p_playlist->i_index - 1 );
1212 msg_Dbg( p_intf, "requested playlist next" );
1217 uri_extract_value( p_request, "mrl", mrl, 512 );
1218 uri_decode_url_encoded( mrl );
1219 playlist_Add( p_sys->p_playlist, mrl,
1220 PLAYLIST_APPEND, PLAYLIST_END );
1221 msg_Dbg( p_intf, "requested playlist add: %s", mrl );
1224 /* admin function */
1228 uri_extract_value( p_request, "id", id, 512 );
1229 msg_Dbg( p_intf, "requested close id=%s", id );
1230 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
1232 msg_Warn( p_intf, "close failed for id=%s", id );
1238 msg_Dbg( p_intf, "requested shutdown" );
1239 p_intf->p_vlc->b_die = VLC_TRUE;
1243 PRINTS( "<!-- control param(%s) unsuported -->", control );
1254 if( i_request <= 0 ||
1255 *m->param1 == '\0' ||
1256 strstr( p_request, m->param1 ) == NULL )
1260 uri_extract_value( p_request, m->param1, value, 512 );
1261 uri_decode_url_encoded( value );
1263 switch( StrToMacroType( m->param2 ) )
1267 config_PutInt( p_intf, m->param1, i );
1271 config_PutFloat( p_intf, m->param1, f );
1274 config_PutPsz( p_intf, m->param1, value );
1277 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
1288 if( *m->param1 == '\0' )
1293 switch( StrToMacroType( m->param2 ) )
1296 i = config_GetInt( p_intf, m->param1 );
1297 sprintf( value, "%i", i );
1300 f = config_GetFloat( p_intf, m->param1 );
1301 sprintf( value, "%f", f );
1304 psz = config_GetPsz( p_intf, m->param1 );
1305 sprintf( value, "%s", psz ? psz : "" );
1308 sprintf( value, "invalid type(%s) in set", m->param2 );
1311 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
1312 PRINTS( "%s", value );
1319 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1320 s = SSPop( &p_args->stack );
1321 v = mvar_GetValue( p_args->vars, s );
1328 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1332 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
1333 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
1341 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
1348 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
1353 i_skip = MacroParse( &m, src );
1355 i_id = StrToMacroType( m.id );
1370 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
1371 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
1373 return src + ( b_after ? i_skip : 0 );
1375 else if( i_level < 0 )
1391 static void Execute( httpd_file_callback_args_t *p_args,
1392 uint8_t *p_request, int i_request,
1393 uint8_t **pp_data, int *pi_data,
1395 uint8_t *_src, uint8_t *_end )
1397 intf_thread_t *p_intf = p_args->p_intf;
1399 uint8_t *src, *dup, *end;
1400 uint8_t *dst = *pp_dst;
1402 src = dup = malloc( _end - _src + 1 );
1403 end = src +( _end - _src );
1405 memcpy( src, _src, _end - _src );
1408 /* we parse searching <vlc */
1414 p = strstr( src, "<vlc" );
1415 if( p < end && p == src )
1419 src += MacroParse( &m, src );
1421 //msg_Dbg( p_intf, "macro_id=%s", m.id );
1423 switch( StrToMacroType( m.id ) )
1430 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
1431 if( SSPopN( &p_args->stack, p_args->vars ) )
1439 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
1443 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
1447 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
1450 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
1454 else if( i_test == 1 )
1457 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
1459 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
1463 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
1472 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
1473 uint8_t *start = src;
1474 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
1481 if( !strncmp( m.param2, "integer=", 8 ) )
1483 index = mvar_IntegerSetNew( m.param1, &m.param2[8] );
1485 else if( !strcmp( m.param2, "playlist" ) )
1487 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
1489 else if( !strcmp( m.param2, "informations" ) )
1491 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
1493 else if( !strcmp( m.param2, "hosts" ) )
1495 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
1497 else if( !strcmp( m.param2, "urls" ) )
1499 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
1501 else if( !strcmp( m.param2, "connections" ) )
1503 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
1505 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
1507 index = mvar_Duplicate( v );
1511 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
1516 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
1518 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
1520 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
1523 f->name = strdup( m.param1 );
1526 mvar_PushVar( p_args->vars, f );
1527 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
1528 mvar_RemoveVar( p_args->vars, f );
1532 mvar_Delete( index );
1539 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
1547 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
1550 int i_index = dst - *pp_data;
1553 *pp_data = realloc( *pp_data, *pi_data );
1554 dst = (*pp_data) + i_index;
1556 memcpy( dst, src, i_copy );
1566 /****************************************************************************
1568 ****************************************************************************
1569 * a file with mime == text/html is parsed and all "macro" replaced
1570 * <vlc id="macro name" [param1="" [param2=""]] />
1573 ****************************************************************************/
1574 static int http_get( httpd_file_callback_args_t *p_args,
1575 uint8_t *p_request, int i_request,
1576 uint8_t **pp_data, int *pi_data )
1581 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
1583 p = *pp_data = malloc( 10240 );
1585 p += sprintf( p, "<html>\n" );
1586 p += sprintf( p, "<head>\n" );
1587 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
1588 p += sprintf( p, "</head>\n" );
1589 p += sprintf( p, "<body>\n" );
1590 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
1591 p += sprintf( p, "<hr />\n" );
1592 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1593 p += sprintf( p, "</body>\n" );
1594 p += sprintf( p, "</html>\n" );
1596 *pi_data = strlen( *pp_data ) + 1;
1601 if( strcmp( p_args->mime, "text/html" ) )
1603 FileLoad( f, pp_data, pi_data );
1611 p_args->vars = mvar_New( "variables", "" );
1612 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
1613 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
1614 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
1616 SSInit( &p_args->stack );
1618 /* first we load in a temporary buffer */
1619 FileLoad( f, &p_buffer, &i_buffer );
1621 /* allocate output */
1622 *pi_data = i_buffer + 1000;
1623 dst = *pp_data = malloc( *pi_data );
1625 /* we parse executing all <vlc /> macros */
1626 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
1629 *pi_data = dst - *pp_data;
1631 SSClean( &p_args->stack );
1632 mvar_Delete( p_args->vars );
1640 /****************************************************************************
1642 ****************************************************************************/
1643 static void uri_extract_value( char *psz_uri, char *psz_name,
1644 char *psz_value, int i_value_max )
1648 p = strstr( psz_uri, psz_name );
1653 p += strlen( psz_name );
1654 if( *p == '=' ) p++;
1656 if( strchr( p, '&' ) )
1658 i_len = strchr( p, '&' ) - p;
1662 /* for POST method */
1663 if( strchr( p, '\n' ) )
1665 i_len = strchr( p, '\n' ) - p;
1666 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
1670 i_len = strlen( p );
1673 i_len = __MIN( i_value_max - 1, i_len );
1676 strncpy( psz_value, p, i_len );
1677 psz_value[i_len] = '\0';
1681 strncpy( psz_value, "", i_value_max );
1686 strncpy( psz_value, "", i_value_max );
1690 static void uri_decode_url_encoded( char *psz )
1692 char *dup = strdup( psz );
1710 *psz++ = strtol( val, NULL, 16 );
1712 else if( *p == '+' )
1726 /****************************************************************************
1727 * Light RPN evaluator
1728 ****************************************************************************/
1729 static void SSInit( stack_t *st )
1734 static void SSClean( stack_t *st )
1736 while( st->i_stack > 0 )
1738 free( st->stack[--st->i_stack] );
1742 static void SSPush( stack_t *st, char *s )
1744 if( st->i_stack < STACK_MAX )
1746 st->stack[st->i_stack++] = strdup( s );
1750 static char * SSPop( stack_t *st )
1752 if( st->i_stack <= 0 )
1754 return strdup( "" );
1758 return st->stack[--st->i_stack];
1762 static int SSPopN( stack_t *st, mvar_t *vars )
1771 i = strtol( name, &end, 0 );
1774 value = mvar_GetValue( vars, name );
1782 static void SSPushN( stack_t *st, int i )
1786 sprintf( v, "%d", i );
1790 static void EvaluateRPN( mvar_t *vars, stack_t *st, char *exp )
1797 while( *exp == ' ' )
1804 /* extract string */
1806 while( *exp && *exp != '\'' )
1816 p = strchr( exp, ' ' );
1821 exp += strlen( exp );
1826 strncpy( s, exp, i );
1837 /* 1. Integer function */
1838 if( !strcmp( s, "!" ) )
1840 SSPushN( st, ~SSPopN( st, vars ) );
1842 else if( !strcmp( s, "^" ) )
1844 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
1846 else if( !strcmp( s, "&" ) )
1848 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
1850 else if( !strcmp( s, "|" ) )
1852 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
1854 else if( !strcmp( s, "+" ) )
1856 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
1858 else if( !strcmp( s, "-" ) )
1860 int i = SSPopN( st, vars );
1861 int j = SSPopN( st, vars );
1862 SSPushN( st, i - j );
1864 else if( !strcmp( s, "*" ) )
1866 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
1868 else if( !strcmp( s, "/" ) )
1872 i = SSPopN( st, vars );
1873 j = SSPopN( st, vars );
1875 SSPushN( st, j != 0 ? i / j : 0 );
1877 else if( !strcmp( s, "%" ) )
1881 i = SSPopN( st, vars );
1882 j = SSPopN( st, vars );
1884 SSPushN( st, j != 0 ? i % j : 0 );
1886 /* 2. integer tests */
1887 else if( !strcmp( s, "=" ) )
1889 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
1891 else if( !strcmp( s, "<" ) )
1893 int i = SSPopN( st, vars );
1894 int j = SSPopN( st, vars );
1896 SSPushN( st, i < j ? -1 : 0 );
1898 else if( !strcmp( s, ">" ) )
1900 int i = SSPopN( st, vars );
1901 int j = SSPopN( st, vars );
1903 SSPushN( st, i > j ? -1 : 0 );
1905 else if( !strcmp( s, "<=" ) )
1907 int i = SSPopN( st, vars );
1908 int j = SSPopN( st, vars );
1910 SSPushN( st, i <= j ? -1 : 0 );
1912 else if( !strcmp( s, ">=" ) )
1914 int i = SSPopN( st, vars );
1915 int j = SSPopN( st, vars );
1917 SSPushN( st, i >= j ? -1 : 0 );
1919 /* 3. string functions */
1920 else if( !strcmp( s, "strcat" ) )
1922 char *s1 = SSPop( st );
1923 char *s2 = SSPop( st );
1924 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
1934 else if( !strcmp( s, "strcmp" ) )
1936 char *s1 = SSPop( st );
1937 char *s2 = SSPop( st );
1939 SSPushN( st, strcmp( s1, s2 ) );
1943 else if( !strcmp( s, "strlen" ) )
1945 char *str = SSPop( st );
1947 SSPushN( st, strlen( str ) );
1950 /* 4. stack functions */
1951 else if( !strcmp( s, "dup" ) )
1953 char *str = SSPop( st );
1958 else if( !strcmp( s, "drop" ) )
1960 char *str = SSPop( st );
1963 else if( !strcmp( s, "swap" ) )
1965 char *s1 = SSPop( st );
1966 char *s2 = SSPop( st );
1973 else if( !strcmp( s, "flush" ) )
1978 else if( !strcmp( s, "store" ) )
1980 char *name = SSPop( st );
1981 char *value = SSPop( st );
1983 mvar_PushNewVar( vars, name, value );
1987 else if( !strcmp( s, "value" ) )
1989 char *name = SSPop( st );
1990 char *value = mvar_GetValue( vars, name );
1992 SSPush( st, value );