1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: http.c,v 1.17 2003/07/23 01:13:47 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 *****************************************************************************/
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
54 #elif defined( WIN32 ) && !defined( UNDER_CE )
58 #if (!defined( WIN32 ) || defined(__MINGW32__))
59 /* Mingw has its own version of dirent */
64 /*****************************************************************************
66 *****************************************************************************/
67 static int Activate ( vlc_object_t * );
68 static void Close ( vlc_object_t * );
69 static void Run ( intf_thread_t *p_intf );
71 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
74 static int http_get( httpd_file_callback_args_t *p_args,
75 uint8_t *p_request, int i_request,
76 uint8_t **pp_data, int *pi_data );
78 static void uri_extract_value( char *psz_uri, char *psz_name,
79 char *psz_value, int i_value_max );
80 static void uri_decode_url_encoded( char *psz );
82 /*****************************************************************************
84 *****************************************************************************/
91 struct mvar_s **field;
97 char *stack[STACK_MAX];
101 struct httpd_file_callback_args_t
103 intf_thread_t *p_intf;
104 httpd_file_t *p_file;
110 /* inited for each access */
118 httpd_host_t *p_httpd_host;
121 httpd_file_callback_args_t **pp_files;
123 playlist_t *p_playlist;
124 input_thread_t *p_input;
128 /*****************************************************************************
130 *****************************************************************************/
131 #define HOST_TEXT N_( "Host address" )
132 #define HOST_LONGTEXT N_( \
133 "You can set the address and port on which the http interface will bind" )
134 #define SRC_TEXT N_( "Source directory" )
135 #define SRC_LONGTEXT N_( "Source directory" )
138 set_description( _("HTTP remote control interface") );
139 add_category_hint( N_("HTTP remote control"), NULL, VLC_TRUE );
140 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
141 #if defined(SYS_DARWIN) || defined(SYS_BEOS) \
142 || (defined(WIN32) && !defined(UNDER_CE))
143 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
145 add_string ( "http-src", "share/http", NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
147 set_capability( "interface", 0 );
148 set_callbacks( Activate, Close );
151 /*****************************************************************************
152 * Activate: initialize and create stuff
153 *****************************************************************************/
154 static int Activate( vlc_object_t *p_this )
156 intf_thread_t *p_intf = (intf_thread_t*)p_this;
159 char *psz_address = "";
163 psz_host = config_GetPsz( p_intf, "http-host" );
167 psz_address = psz_host;
169 psz_parser = strchr( psz_host, ':' );
172 *psz_parser++ = '\0';
173 i_port = atoi( psz_parser );
181 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
182 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
183 p_sys->p_playlist = NULL;
184 p_sys->p_input = NULL;
186 if( ( p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_intf), VLC_TRUE ) ) == NULL )
188 msg_Err( p_intf, "cannot create/find httpd" );
193 if( ( p_sys->p_httpd_host =
194 p_sys->p_httpd->pf_register_host( p_sys->p_httpd,
195 psz_address, i_port ) ) == NULL )
197 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
198 httpd_Release( p_sys->p_httpd );
209 p_sys->pp_files = malloc( sizeof( httpd_file_callback_args_t *) );
211 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
212 ( defined(WIN32) && !defined(UNDER_CE ) )
213 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
215 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
216 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
217 sprintf( psz_src, "%s/share/http", psz_vlcpath);
220 psz_src = config_GetPsz( p_intf, "http-src" );
222 if( !psz_src || *psz_src == '\0' )
224 msg_Err( p_intf, "invalid src dir" );
228 /* remove trainling \ or / */
229 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
230 psz_src[strlen( psz_src ) - 1] == '/' )
232 psz_src[strlen( psz_src ) - 1] = '\0';
235 ParseDirectory( p_intf, psz_src, psz_src );
238 if( p_sys->i_files <= 0 )
240 msg_Err( p_intf, "cannot find any files" );
243 p_intf->pf_run = Run;
248 free( p_sys->pp_files );
249 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
250 p_sys->p_httpd_host );
251 httpd_Release( p_sys->p_httpd );
256 /*****************************************************************************
257 * CloseIntf: destroy interface
258 *****************************************************************************/
259 void Close ( vlc_object_t *p_this )
261 intf_thread_t *p_intf = (intf_thread_t *)p_this;
262 intf_sys_t *p_sys = p_intf->p_sys;
266 for( i = 0; i < p_sys->i_files; i++ )
268 p_sys->p_httpd->pf_unregister_file( p_sys->p_httpd,
269 p_sys->pp_files[i]->p_file );
270 /* do not free mime */
271 free( p_sys->pp_files[i]->file );
272 free( p_sys->pp_files[i]->name );
273 free( p_sys->pp_files[i] );
275 free( p_sys->pp_files );
276 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
277 p_sys->p_httpd_host );
278 httpd_Release( p_sys->p_httpd );
282 /*****************************************************************************
283 * Run: http interface thread
284 *****************************************************************************/
285 static void Run( intf_thread_t *p_intf )
287 intf_sys_t *p_sys = p_intf->p_sys;
289 while( !p_intf->b_die )
291 /* get the playlist */
292 if( p_sys->p_playlist == NULL )
294 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
297 /* Manage the input part */
298 if( p_sys->p_input == NULL )
300 if( p_sys->p_playlist )
303 vlc_object_find( p_sys->p_playlist,
308 else if( p_sys->p_input->b_dead )
310 vlc_object_release( p_sys->p_input );
311 p_sys->p_input = NULL;
316 msleep( INTF_IDLE_SLEEP );
321 vlc_object_release( p_sys->p_input );
322 p_sys->p_input = NULL;
325 if( p_sys->p_playlist )
327 vlc_object_release( p_sys->p_playlist );
328 p_sys->p_playlist = NULL;
333 /*****************************************************************************
335 *****************************************************************************/
336 #define MAX_DIR_SIZE 10240
338 /****************************************************************************
339 * FileToUrl: create a good name for an url from filename
340 ****************************************************************************/
341 static char *FileToUrl( char *name )
345 url = p = malloc( strlen( name ) + 1 );
348 while( *name == '\\' || *name == '/' )
350 while( *name == '/' )
360 /* convert '\\' into '/' */
373 if( ( p = strrchr( url, '/' ) ) != NULL )
375 if( !strncmp( p, "/index.", 7 ) )
383 /****************************************************************************
384 * FileToMime: XXX duplicated with modules/access_out/http.c
385 ****************************************************************************/
392 { ".htm", "text/html" },
393 { ".html", "text/html" },
396 { ".avi", "video/avi" },
397 { ".asf", "video/x-ms-asf" },
398 { ".m1a", "audio/mpeg" },
399 { ".m2a", "audio/mpeg" },
400 { ".m1v", "video/mpeg" },
401 { ".m2v", "video/mpeg" },
402 { ".mp2", "audio/mpeg" },
403 { ".mp3", "audio/mpeg" },
404 { ".mpa", "audio/mpeg" },
405 { ".mpg", "video/mpeg" },
406 { ".mpeg", "video/mpeg" },
407 { ".mpe", "video/mpeg" },
408 { ".mov", "video/quicktime" },
409 { ".moov", "video/quicktime" },
410 { ".ogg", "application/ogg" },
411 { ".ogm", "application/ogg" },
412 { ".wav", "audio/wav" },
418 static char *FileToMime( char *psz_name )
422 psz_ext = strrchr( psz_name, '.' );
427 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
429 if( !strcmp( http_mime[i].psz_ext, psz_ext ) )
431 return( http_mime[i].psz_mime );
435 return( "application/octet-stream" );
438 /****************************************************************************
439 * ParseDirectory: parse recursively a directory, adding each file
440 ****************************************************************************/
441 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
444 intf_sys_t *p_sys = p_intf->p_sys;
445 char dir[MAX_DIR_SIZE];
446 #ifdef HAVE_SYS_STAT_H
447 struct stat stat_info;
450 struct dirent *p_dir_content;
454 char *password = NULL;
456 #ifdef HAVE_SYS_STAT_H
457 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
463 if( ( p_dir = opendir( psz_dir ) ) == NULL )
465 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
469 msg_Dbg( p_intf, "dir=%s", psz_dir );
471 sprintf( dir, "%s/.access", psz_dir );
472 if( ( file = fopen( dir, "r" ) ) != NULL )
477 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
479 i_size = fread( line, 1, 1023, file );
483 while( i_size > 0 && ( line[i_size-1] == '\n' ||
484 line[i_size-1] == '\r' ) )
491 p = strchr( line, ':' );
495 user = strdup( line );
496 password = strdup( p );
499 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
500 user, password, i_size );
507 /* parse psz_src dir */
508 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
513 if( p_dir_content->d_name[0] == '.' )
517 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
518 if( ParseDirectory( p_intf, psz_root, dir ) )
520 #define f p_sys->pp_files[p_sys->i_files]
521 f = malloc( sizeof( httpd_file_callback_args_t ) );
523 f->file = strdup( dir );
524 f->name = FileToUrl( &dir[strlen( psz_root )] );
525 f->mime = FileToMime( &dir[strlen( psz_root )] );
527 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)",
528 f->file, f->name, f->mime );
531 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
539 p_sys->pp_files = realloc( p_sys->pp_files,
540 (p_sys->i_files+1) * sizeof( httpd_file_callback_args_t ) );
542 #define fold p_sys->pp_files[p_sys->i_files-1]
544 /* FIXME for rep/ add rep (it would be better to do a redirection) */
545 if( strlen(fold->name) > 1 &&
546 fold->name[strlen(fold->name) - 1] == '/' )
548 f = malloc( sizeof( httpd_file_callback_args_t ) );
550 f->file = strdup( fold->file );
551 f->name = strdup( fold->name );
552 f->mime = fold->mime;
554 f->name[strlen(f->name) - 1] = '\0';
555 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)", f->file, f->name,
558 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
567 realloc( p_sys->pp_files, (p_sys->i_files+1) *
568 sizeof( httpd_file_callback_args_t ) );
587 /****************************************************************************
588 * var and set handling
589 ****************************************************************************/
591 static mvar_t *mvar_New( char *name, char *value )
593 mvar_t *v = malloc( sizeof( mvar_t ) );
595 v->name = strdup( name );
596 v->value = strdup( value ? value : "" );
599 v->field = malloc( sizeof( mvar_t * ) );
605 static void mvar_Delete( mvar_t *v )
612 for( i = 0; i < v->i_field; i++ )
614 mvar_Delete( v->field[i] );
620 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
622 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
623 v->field[v->i_field] = f;
627 static mvar_t *mvar_Duplicate( mvar_t *v )
632 n = mvar_New( v->name, v->value );
633 for( i = 0; i < v->i_field; i++ )
635 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
641 static void mvar_PushVar( mvar_t *v, mvar_t *f )
643 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
646 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
652 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
655 for( i = 0; i < v->i_field; i++ )
657 if( v->field[i] == f )
662 if( i >= v->i_field )
667 if( i + 1 < v->i_field )
669 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
672 /* FIXME should do a realloc */
675 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
678 char base[512], *field, *p;
681 /* format: name[index].field */
683 field = strchr( name, '.' );
686 int i = field - name;
687 strncpy( base, name, i );
693 strcpy( base, name );
696 if( ( p = strchr( base, '[' ) ) )
699 sscanf( p, "%d]", &i_index );
710 for( i = 0; i < s->i_field; i++ )
712 if( !strcmp( s->field[i]->name, base ) )
722 return mvar_GetVar( s->field[i], field );
736 static char *mvar_GetValue( mvar_t *v, char *field )
744 mvar_t *f = mvar_GetVar( v, field );
756 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
758 mvar_t *f = mvar_New( name, value );
759 mvar_PushVar( vars, f );
762 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
764 mvar_t *f = mvar_New( name, value );
765 mvar_AppendVar( vars, f );
769 /* arg= start[:stop[:step]],.. */
770 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
772 char *dup = strdup( arg );
774 mvar_t *s = mvar_New( name, "set" );
776 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
781 int i_start,i_stop,i_step;
784 p = strchr( str, ',' );
791 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
792 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
799 else if( i_match == 2 )
801 i_step = i_start < i_stop ? 1 : -1;
808 if( ( i_start < i_stop && i_step > 0 ) ||
809 ( i_start > i_stop && i_step < 0 ) )
811 for( i = i_start; ; i += i_step )
815 if( ( i_step > 0 && i > i_stop ) ||
816 ( i_step < 0 && i < i_stop ) )
821 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
822 sprintf( value, "%d", i );
824 mvar_PushNewVar( s, name, value );
835 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
837 mvar_t *s = mvar_New( name, "set" );
840 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
842 vlc_mutex_lock( &p_pl->object_lock );
843 for( i = 0; i < p_pl->i_size; i++ )
845 mvar_t *itm = mvar_New( name, "set" );
848 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
849 mvar_AppendNewVar( itm, "current", value );
851 sprintf( value, "%d", i );
852 mvar_AppendNewVar( itm, "index", value );
854 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->psz_name );
856 mvar_AppendVar( s, itm );
858 vlc_mutex_unlock( &p_pl->object_lock );
863 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
865 mvar_t *s = mvar_New( name, "set" );
867 input_info_category_t * p_category;
868 input_info_t * p_info;
870 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
871 if( p_input == NULL )
876 vlc_mutex_lock( &p_input->stream.stream_lock );
877 p_category = p_input->stream.p_info;
880 mvar_t *cat = mvar_New( name, "set" );
881 mvar_t *iset = mvar_New( "info", "set" );
883 mvar_AppendNewVar( cat, "name", p_category->psz_name );
884 mvar_AppendVar( cat, iset );
886 p_info = p_category->p_info;
889 mvar_t *info = mvar_New( "info", "" );
891 msg_Dbg( p_input, "adding info name=%s value=%s", p_info->psz_name, p_info->psz_value );
892 mvar_AppendNewVar( info, "name", p_info->psz_name );
893 mvar_AppendNewVar( info, "value", p_info->psz_value );
894 mvar_AppendVar( iset, info );
895 p_info = p_info->p_next;
897 mvar_AppendVar( s, cat );
898 p_category = p_category->p_next;
900 vlc_mutex_unlock( &p_input->stream.stream_lock );
905 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
907 mvar_t *s = mvar_New( name, "set" );
911 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
912 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
914 for( i= 0; i < info.i_count; )
918 inf = mvar_New( name, "set" );
921 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
922 info.info[i].psz_name, info.info[i].psz_value ); */
923 mvar_AppendNewVar( inf,
924 info.info[i].psz_name,
925 info.info[i].psz_value );
927 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
928 mvar_AppendVar( s, inf );
933 for( i = 0; i < info.i_count; i++ )
935 free( info.info[i].psz_name );
936 free( info.info[i].psz_value );
941 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
943 mvar_t *s = mvar_New( name, "set" );
944 char tmp[MAX_DIR_SIZE], *p, *src;
945 #ifdef HAVE_SYS_STAT_H
946 struct stat stat_info;
949 struct dirent *p_dir_content;
952 /* convert all / to native separator */
954 while( (p = strchr( psz_dir, '/' )) )
963 /* remove trailling separoator */
964 while( strlen( psz_dir ) > 1 && psz_dir[strlen( psz_dir ) -1 ] == sep )
966 psz_dir[strlen( psz_dir ) -1 ] ='\0';
968 /* remove double separator */
969 for( p = src = psz_dir; *src != '\0'; src++, p++ )
971 if( src[0] == sep && src[1] == sep )
979 if( *psz_dir == '\0' )
983 /* first fix all .. dir */
987 if( src[0] == '.' && src[1] == '.' )
990 if( p <= &psz_dir[1] )
997 while( p > &psz_dir[1] && *p != sep )
1002 else if( *src == sep )
1004 if( p > psz_dir && p[-1] == sep )
1018 } while( *src && *src != sep );
1023 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1025 #ifdef HAVE_SYS_STAT_H
1026 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1032 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1034 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1038 /* remove traling / or \ */
1039 for( p = &psz_dir[strlen( psz_dir) - 1]; p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1048 /* parse psz_src dir */
1049 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1053 if( !strcmp( p_dir_content->d_name, "." ) )
1058 #if defined( WIN32 )
1059 sprintf( tmp, "%s\\%s", psz_dir, p_dir_content->d_name );
1061 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1064 #ifdef HAVE_SYS_STAT_H
1065 if( stat( tmp, &stat_info ) == -1 )
1070 f = mvar_New( name, "set" );
1071 mvar_AppendNewVar( f, "name", tmp );
1072 #ifdef HAVE_SYS_STAT_H
1073 if( S_ISDIR( stat_info.st_mode ) )
1075 mvar_AppendNewVar( f, "type", "directory" );
1077 else if( S_ISREG( stat_info.st_mode ) )
1079 mvar_AppendNewVar( f, "type", "file" );
1083 mvar_AppendNewVar( f, "type", "unknown" );
1086 sprintf( tmp, "%lld", stat_info.st_size );
1087 mvar_AppendNewVar( f, "size", tmp );
1089 /* FIXME memory leak FIXME */
1091 ctime_r( &stat_info.st_mtime, tmp );
1092 mvar_AppendNewVar( f, "date", tmp );
1094 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1098 mvar_AppendNewVar( f, "type", "unknown" );
1099 mvar_AppendNewVar( f, "size", "unknown" );
1100 mvar_AppendNewVar( f, "date", "unknown" );
1102 mvar_AppendVar( s, f );
1108 static void SSInit( rpn_stack_t * );
1109 static void SSClean( rpn_stack_t * );
1110 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1112 static void SSPush ( rpn_stack_t *, char * );
1113 static char *SSPop ( rpn_stack_t * );
1115 static void SSPushN ( rpn_stack_t *, int );
1116 static int SSPopN ( rpn_stack_t *, mvar_t * );
1119 /****************************************************************************
1121 ****************************************************************************/
1129 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1133 /* just load the file */
1135 *pp_data = malloc( 1025 ); /* +1 for \0 */
1136 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1139 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1145 (*pp_data)[*pi_data] = '\0';
1150 static int MacroParse( macro_t *m, uint8_t *psz_src )
1152 uint8_t *dup = strdup( psz_src );
1157 #define EXTRACT( name, l ) \
1159 p = strchr( src, '"' ); \
1164 m->name = strdup( src ); \
1185 if( !strncmp( src, "id=\"", 4 ) )
1189 else if( !strncmp( src, "param1=\"", 8 ) )
1191 EXTRACT( param1, 8 );
1193 else if( !strncmp( src, "param2=\"", 8 ) )
1195 EXTRACT( param2, 8 );
1202 if( strstr( src, "/>" ) )
1204 src = strstr( src, "/>" ) + 2;
1208 src += strlen( src );
1213 m->id = strdup( "" );
1215 if( m->param1 == NULL )
1217 m->param1 = strdup( "" );
1219 if( m->param2 == NULL )
1221 m->param2 = strdup( "" );
1230 static void MacroClean( macro_t *m )
1271 StrToMacroTypeTab [] =
1273 { "control", MVLC_CONTROL },
1274 /* player control */
1275 { "play", MVLC_PLAY },
1276 { "stop", MVLC_STOP },
1277 { "pause", MVLC_PAUSE },
1278 { "next", MVLC_NEXT },
1279 { "previous", MVLC_PREVIOUS },
1281 /* playlist management */
1282 { "add", MVLC_ADD },
1283 { "del", MVLC_DEL },
1284 { "empty", MVLC_EMPTY },
1287 { "close", MVLC_CLOSE },
1288 { "shutdown", MVLC_SHUTDOWN },
1290 { "rpn", MVLC_RPN },
1292 { "foreach", MVLC_FOREACH },
1293 { "value", MVLC_VALUE },
1296 { "else", MVLC_ELSE },
1297 { "end", MVLC_END },
1298 { "get", MVLC_GET },
1299 { "set", MVLC_SET },
1300 { "int", MVLC_INT },
1301 { "float", MVLC_FLOAT },
1302 { "string", MVLC_STRING },
1305 { NULL, MVLC_UNKNOWN }
1308 static int StrToMacroType( char *name )
1312 if( !name || *name == '\0')
1314 return MVLC_UNKNOWN;
1316 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1318 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1320 return StrToMacroTypeTab[i].i_type;
1323 return MVLC_UNKNOWN;
1326 static void MacroDo( httpd_file_callback_args_t *p_args,
1328 uint8_t *p_request, int i_request,
1329 uint8_t **pp_data, int *pi_data,
1332 intf_thread_t *p_intf = p_args->p_intf;
1333 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1336 #define ALLOC( l ) \
1338 int __i__ = *pp_dst - *pp_data; \
1340 *pp_data = realloc( *pp_data, *pi_data ); \
1341 *pp_dst = (*pp_data) + __i__; \
1343 #define PRINT( str ) \
1344 ALLOC( strlen( str ) + 1 ); \
1345 *pp_dst += sprintf( *pp_dst, str );
1347 #define PRINTS( str, s ) \
1348 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1349 *pp_dst += sprintf( *pp_dst, str, s );
1351 switch( StrToMacroType( m->id ) )
1354 if( i_request <= 0 )
1358 uri_extract_value( p_request, "control", control, 512 );
1359 if( *m->param1 && !strstr( m->param1, control ) )
1361 msg_Warn( p_intf, "unauthorized control=%s", control );
1364 switch( StrToMacroType( control ) )
1371 uri_extract_value( p_request, "item", item, 512 );
1372 i_item = atoi( item );
1373 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
1374 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1378 playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1379 msg_Dbg( p_intf, "requested playlist stop" );
1382 playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1383 msg_Dbg( p_intf, "requested playlist pause" );
1386 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1387 p_sys->p_playlist->i_index + 1 );
1388 msg_Dbg( p_intf, "requested playlist next" );
1391 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1392 p_sys->p_playlist->i_index - 1 );
1393 msg_Dbg( p_intf, "requested playlist next" );
1396 /* playlist management */
1400 uri_extract_value( p_request, "mrl", mrl, 512 );
1401 uri_decode_url_encoded( mrl );
1402 playlist_Add( p_sys->p_playlist, mrl, NULL, 0,
1403 PLAYLIST_APPEND, PLAYLIST_END );
1404 msg_Dbg( p_intf, "requested playlist add: %s", mrl );
1412 uri_extract_value( p_request, "item", item, 512 );
1413 i_item = atoi( item );
1415 playlist_Delete( p_sys->p_playlist, i_item );
1416 msg_Dbg( p_intf, "requested playlist del: %d", i_item );
1421 while( p_sys->p_playlist->i_size > 0 )
1423 playlist_Delete( p_sys->p_playlist, 0 );
1425 msg_Dbg( p_intf, "requested playlist empty" );
1429 /* admin function */
1433 uri_extract_value( p_request, "id", id, 512 );
1434 msg_Dbg( p_intf, "requested close id=%s", id );
1435 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
1437 msg_Warn( p_intf, "close failed for id=%s", id );
1443 msg_Dbg( p_intf, "requested shutdown" );
1444 p_intf->p_vlc->b_die = VLC_TRUE;
1448 PRINTS( "<!-- control param(%s) unsuported -->", control );
1459 if( i_request <= 0 ||
1460 *m->param1 == '\0' ||
1461 strstr( p_request, m->param1 ) == NULL )
1465 uri_extract_value( p_request, m->param1, value, 512 );
1466 uri_decode_url_encoded( value );
1468 switch( StrToMacroType( m->param2 ) )
1472 config_PutInt( p_intf, m->param1, i );
1476 config_PutFloat( p_intf, m->param1, f );
1479 config_PutPsz( p_intf, m->param1, value );
1482 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
1493 if( *m->param1 == '\0' )
1498 switch( StrToMacroType( m->param2 ) )
1501 i = config_GetInt( p_intf, m->param1 );
1502 sprintf( value, "%i", i );
1505 f = config_GetFloat( p_intf, m->param1 );
1506 sprintf( value, "%f", f );
1509 psz = config_GetPsz( p_intf, m->param1 );
1510 sprintf( value, "%s", psz ? psz : "" );
1513 sprintf( value, "invalid type(%s) in set", m->param2 );
1516 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
1517 PRINTS( "%s", value );
1526 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1527 s = SSPop( &p_args->stack );
1528 v = mvar_GetValue( p_args->vars, s );
1532 v = s = SSPop( &p_args->stack );
1540 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1544 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
1545 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
1553 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
1560 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
1565 i_skip = MacroParse( &m, src );
1567 i_id = StrToMacroType( m.id );
1582 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
1583 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
1585 return src + ( b_after ? i_skip : 0 );
1587 else if( i_level < 0 )
1603 static void Execute( httpd_file_callback_args_t *p_args,
1604 uint8_t *p_request, int i_request,
1605 uint8_t **pp_data, int *pi_data,
1607 uint8_t *_src, uint8_t *_end )
1609 intf_thread_t *p_intf = p_args->p_intf;
1611 uint8_t *src, *dup, *end;
1612 uint8_t *dst = *pp_dst;
1614 src = dup = malloc( _end - _src + 1 );
1615 end = src +( _end - _src );
1617 memcpy( src, _src, _end - _src );
1620 /* we parse searching <vlc */
1626 p = strstr( src, "<vlc" );
1627 if( p < end && p == src )
1631 src += MacroParse( &m, src );
1633 //msg_Dbg( p_intf, "macro_id=%s", m.id );
1635 switch( StrToMacroType( m.id ) )
1642 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
1643 if( SSPopN( &p_args->stack, p_args->vars ) )
1651 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
1655 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
1659 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
1662 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
1666 else if( i_test == 1 )
1669 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
1671 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
1675 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
1684 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
1685 uint8_t *start = src;
1686 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
1693 if( !strcmp( m.param2, "integer" ) )
1695 char *arg = SSPop( &p_args->stack );
1696 index = mvar_IntegerSetNew( m.param1, arg );
1699 else if( !strcmp( m.param2, "directory" ) )
1701 char *arg = SSPop( &p_args->stack );
1702 index = mvar_FileSetNew( m.param1, arg );
1705 else if( !strcmp( m.param2, "playlist" ) )
1707 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
1709 else if( !strcmp( m.param2, "informations" ) )
1711 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
1713 else if( !strcmp( m.param2, "hosts" ) )
1715 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
1717 else if( !strcmp( m.param2, "urls" ) )
1719 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
1721 else if( !strcmp( m.param2, "connections" ) )
1723 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
1725 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
1727 index = mvar_Duplicate( v );
1731 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
1736 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
1738 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
1740 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
1743 f->name = strdup( m.param1 );
1746 mvar_PushVar( p_args->vars, f );
1747 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
1748 mvar_RemoveVar( p_args->vars, f );
1752 mvar_Delete( index );
1759 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
1767 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
1770 int i_index = dst - *pp_data;
1773 *pp_data = realloc( *pp_data, *pi_data );
1774 dst = (*pp_data) + i_index;
1776 memcpy( dst, src, i_copy );
1786 /****************************************************************************
1788 ****************************************************************************
1789 * a file with mime == text/html is parsed and all "macro" replaced
1790 * <vlc id="macro name" [param1="" [param2=""]] />
1793 ****************************************************************************/
1794 static int http_get( httpd_file_callback_args_t *p_args,
1795 uint8_t *p_request, int i_request,
1796 uint8_t **pp_data, int *pi_data )
1801 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
1803 p = *pp_data = malloc( 10240 );
1805 p += sprintf( p, "<html>\n" );
1806 p += sprintf( p, "<head>\n" );
1807 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
1808 p += sprintf( p, "</head>\n" );
1809 p += sprintf( p, "<body>\n" );
1810 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
1811 p += sprintf( p, "<hr />\n" );
1812 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1813 p += sprintf( p, "</body>\n" );
1814 p += sprintf( p, "</html>\n" );
1816 *pi_data = strlen( *pp_data ) + 1;
1821 if( strcmp( p_args->mime, "text/html" ) )
1823 FileLoad( f, pp_data, pi_data );
1831 p_args->vars = mvar_New( "variables", "" );
1832 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
1833 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
1834 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
1835 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
1837 SSInit( &p_args->stack );
1839 /* first we load in a temporary buffer */
1840 FileLoad( f, &p_buffer, &i_buffer );
1842 /* allocate output */
1843 *pi_data = i_buffer + 1000;
1844 dst = *pp_data = malloc( *pi_data );
1846 /* we parse executing all <vlc /> macros */
1847 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
1850 *pi_data = dst - *pp_data;
1852 SSClean( &p_args->stack );
1853 mvar_Delete( p_args->vars );
1861 /****************************************************************************
1863 ****************************************************************************/
1864 static void uri_extract_value( char *psz_uri, char *psz_name,
1865 char *psz_value, int i_value_max )
1869 p = strstr( psz_uri, psz_name );
1874 p += strlen( psz_name );
1875 if( *p == '=' ) p++;
1877 if( strchr( p, '&' ) )
1879 i_len = strchr( p, '&' ) - p;
1883 /* for POST method */
1884 if( strchr( p, '\n' ) )
1886 i_len = strchr( p, '\n' ) - p;
1887 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
1891 i_len = strlen( p );
1894 i_len = __MIN( i_value_max - 1, i_len );
1897 strncpy( psz_value, p, i_len );
1898 psz_value[i_len] = '\0';
1902 strncpy( psz_value, "", i_value_max );
1907 strncpy( psz_value, "", i_value_max );
1911 static void uri_decode_url_encoded( char *psz )
1913 char *dup = strdup( psz );
1931 *psz++ = strtol( val, NULL, 16 );
1933 else if( *p == '+' )
1947 /****************************************************************************
1948 * Light RPN evaluator
1949 ****************************************************************************/
1950 static void SSInit( rpn_stack_t *st )
1955 static void SSClean( rpn_stack_t *st )
1957 while( st->i_stack > 0 )
1959 free( st->stack[--st->i_stack] );
1963 static void SSPush( rpn_stack_t *st, char *s )
1965 if( st->i_stack < STACK_MAX )
1967 st->stack[st->i_stack++] = strdup( s );
1971 static char * SSPop( rpn_stack_t *st )
1973 if( st->i_stack <= 0 )
1975 return strdup( "" );
1979 return st->stack[--st->i_stack];
1983 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
1992 i = strtol( name, &end, 0 );
1995 value = mvar_GetValue( vars, name );
2003 static void SSPushN( rpn_stack_t *st, int i )
2007 sprintf( v, "%d", i );
2011 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2018 while( *exp == ' ' )
2025 /* extract string */
2028 while( *exp && *exp != '\'' )
2039 p = strchr( exp, ' ' );
2044 exp += strlen( exp );
2049 strncpy( s, exp, i );
2060 /* 1. Integer function */
2061 if( !strcmp( s, "!" ) )
2063 SSPushN( st, ~SSPopN( st, vars ) );
2065 else if( !strcmp( s, "^" ) )
2067 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2069 else if( !strcmp( s, "&" ) )
2071 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2073 else if( !strcmp( s, "|" ) )
2075 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2077 else if( !strcmp( s, "+" ) )
2079 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2081 else if( !strcmp( s, "-" ) )
2083 int i = SSPopN( st, vars );
2084 int j = SSPopN( st, vars );
2085 SSPushN( st, i - j );
2087 else if( !strcmp( s, "*" ) )
2089 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2091 else if( !strcmp( s, "/" ) )
2095 i = SSPopN( st, vars );
2096 j = SSPopN( st, vars );
2098 SSPushN( st, j != 0 ? i / j : 0 );
2100 else if( !strcmp( s, "%" ) )
2104 i = SSPopN( st, vars );
2105 j = SSPopN( st, vars );
2107 SSPushN( st, j != 0 ? i % j : 0 );
2109 /* 2. integer tests */
2110 else if( !strcmp( s, "=" ) )
2112 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2114 else if( !strcmp( s, "<" ) )
2116 int i = SSPopN( st, vars );
2117 int j = SSPopN( st, vars );
2119 SSPushN( st, i < j ? -1 : 0 );
2121 else if( !strcmp( s, ">" ) )
2123 int i = SSPopN( st, vars );
2124 int j = SSPopN( st, vars );
2126 SSPushN( st, i > j ? -1 : 0 );
2128 else if( !strcmp( s, "<=" ) )
2130 int i = SSPopN( st, vars );
2131 int j = SSPopN( st, vars );
2133 SSPushN( st, i <= j ? -1 : 0 );
2135 else if( !strcmp( s, ">=" ) )
2137 int i = SSPopN( st, vars );
2138 int j = SSPopN( st, vars );
2140 SSPushN( st, i >= j ? -1 : 0 );
2142 /* 3. string functions */
2143 else if( !strcmp( s, "strcat" ) )
2145 char *s1 = SSPop( st );
2146 char *s2 = SSPop( st );
2147 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2157 else if( !strcmp( s, "strcmp" ) )
2159 char *s1 = SSPop( st );
2160 char *s2 = SSPop( st );
2162 SSPushN( st, strcmp( s1, s2 ) );
2166 else if( !strcmp( s, "strlen" ) )
2168 char *str = SSPop( st );
2170 SSPushN( st, strlen( str ) );
2173 /* 4. stack functions */
2174 else if( !strcmp( s, "dup" ) )
2176 char *str = SSPop( st );
2181 else if( !strcmp( s, "drop" ) )
2183 char *str = SSPop( st );
2186 else if( !strcmp( s, "swap" ) )
2188 char *s1 = SSPop( st );
2189 char *s2 = SSPop( st );
2196 else if( !strcmp( s, "flush" ) )
2201 else if( !strcmp( s, "store" ) )
2203 char *name = SSPop( st );
2204 char *value = SSPop( st );
2206 mvar_PushNewVar( vars, name, value );
2210 else if( !strcmp( s, "value" ) )
2212 char *name = SSPop( st );
2213 char *value = mvar_GetValue( vars, name );
2215 SSPush( st, value );
2219 else if( !strcmp( s, "url_extract" ) )
2221 char *url = mvar_GetValue( vars, "url_value" );
2222 char *name = SSPop( st );
2225 uri_extract_value( url, name, value, 512 );
2226 uri_decode_url_encoded( value );
2227 SSPush( st, value );