1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: http.c,v 1.19 2003/07/30 21:09:06 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 */
63 /*****************************************************************************
65 *****************************************************************************/
66 static int Activate ( vlc_object_t * );
67 static void Close ( vlc_object_t * );
69 #define HOST_TEXT N_( "Host address" )
70 #define HOST_LONGTEXT N_( \
71 "You can set the address and port on which the http interface will bind" )
72 #define SRC_TEXT N_( "Source directory" )
73 #define SRC_LONGTEXT N_( "Source directory" )
76 set_description( _("HTTP remote control interface") );
77 add_category_hint( N_("HTTP remote control"), NULL, VLC_TRUE );
78 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
79 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, VLC_TRUE );
80 set_capability( "interface", 0 );
81 set_callbacks( Activate, Close );
85 /*****************************************************************************
87 *****************************************************************************/
88 static void Run ( intf_thread_t *p_intf );
90 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
93 static int DirectoryCheck( char *psz_dir )
97 #ifdef HAVE_SYS_STAT_H
98 struct stat stat_info;
100 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
106 if( ( p_dir = opendir( psz_dir ) ) == NULL )
116 static int http_get( httpd_file_callback_args_t *p_args,
117 uint8_t *p_request, int i_request,
118 uint8_t **pp_data, int *pi_data );
120 static void uri_extract_value( char *psz_uri, char *psz_name,
121 char *psz_value, int i_value_max );
122 static void uri_decode_url_encoded( char *psz );
124 /*****************************************************************************
126 *****************************************************************************/
127 typedef struct mvar_s
133 struct mvar_s **field;
136 #define STACK_MAX 100
139 char *stack[STACK_MAX];
143 struct httpd_file_callback_args_t
145 intf_thread_t *p_intf;
146 httpd_file_t *p_file;
152 /* inited for each access */
160 httpd_host_t *p_httpd_host;
163 httpd_file_callback_args_t **pp_files;
165 playlist_t *p_playlist;
166 input_thread_t *p_input;
171 /*****************************************************************************
172 * Activate: initialize and create stuff
173 *****************************************************************************/
174 static int Activate( vlc_object_t *p_this )
176 intf_thread_t *p_intf = (intf_thread_t*)p_this;
179 char *psz_address = "";
183 psz_host = config_GetPsz( p_intf, "http-host" );
187 psz_address = psz_host;
189 psz_parser = strchr( psz_host, ':' );
192 *psz_parser++ = '\0';
193 i_port = atoi( psz_parser );
201 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
202 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
203 p_sys->p_playlist = NULL;
204 p_sys->p_input = NULL;
206 if( ( p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_intf), VLC_TRUE ) ) == NULL )
208 msg_Err( p_intf, "cannot create/find httpd" );
213 if( ( p_sys->p_httpd_host =
214 p_sys->p_httpd->pf_register_host( p_sys->p_httpd,
215 psz_address, i_port ) ) == NULL )
217 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
218 httpd_Release( p_sys->p_httpd );
229 p_sys->pp_files = malloc( sizeof( httpd_file_callback_args_t *) );
231 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
232 ( defined(WIN32) && !defined(UNDER_CE ) )
233 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
235 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
236 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
238 sprintf( psz_src, "%s/http", psz_vlcpath);
240 sprintf( psz_src, "%s/share/http", psz_vlcpath);
244 psz_src = config_GetPsz( p_intf, "http-src" );
246 if( !psz_src || *psz_src == '\0' )
248 if( !DirectoryCheck( "share/http" ) )
250 psz_src = strdup( "share/http" );
252 else if( !DirectoryCheck( DATA_PATH "/http" ) )
254 psz_src = strdup( DATA_PATH "/http" );
259 if( !psz_src || *psz_src == '\0' )
261 msg_Err( p_intf, "invalid src dir" );
265 /* remove trainling \ or / */
266 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
267 psz_src[strlen( psz_src ) - 1] == '/' )
269 psz_src[strlen( psz_src ) - 1] = '\0';
272 ParseDirectory( p_intf, psz_src, psz_src );
275 if( p_sys->i_files <= 0 )
277 msg_Err( p_intf, "cannot find any files (%s)", psz_src );
280 p_intf->pf_run = Run;
285 free( p_sys->pp_files );
286 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
287 p_sys->p_httpd_host );
288 httpd_Release( p_sys->p_httpd );
293 /*****************************************************************************
294 * CloseIntf: destroy interface
295 *****************************************************************************/
296 void Close ( vlc_object_t *p_this )
298 intf_thread_t *p_intf = (intf_thread_t *)p_this;
299 intf_sys_t *p_sys = p_intf->p_sys;
303 for( i = 0; i < p_sys->i_files; i++ )
305 p_sys->p_httpd->pf_unregister_file( p_sys->p_httpd,
306 p_sys->pp_files[i]->p_file );
307 /* do not free mime */
308 free( p_sys->pp_files[i]->file );
309 free( p_sys->pp_files[i]->name );
310 free( p_sys->pp_files[i] );
312 free( p_sys->pp_files );
313 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
314 p_sys->p_httpd_host );
315 httpd_Release( p_sys->p_httpd );
319 /*****************************************************************************
320 * Run: http interface thread
321 *****************************************************************************/
322 static void Run( intf_thread_t *p_intf )
324 intf_sys_t *p_sys = p_intf->p_sys;
326 while( !p_intf->b_die )
328 /* get the playlist */
329 if( p_sys->p_playlist == NULL )
331 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
334 /* Manage the input part */
335 if( p_sys->p_input == NULL )
337 if( p_sys->p_playlist )
340 vlc_object_find( p_sys->p_playlist,
345 else if( p_sys->p_input->b_dead )
347 vlc_object_release( p_sys->p_input );
348 p_sys->p_input = NULL;
353 msleep( INTF_IDLE_SLEEP );
358 vlc_object_release( p_sys->p_input );
359 p_sys->p_input = NULL;
362 if( p_sys->p_playlist )
364 vlc_object_release( p_sys->p_playlist );
365 p_sys->p_playlist = NULL;
370 /*****************************************************************************
372 *****************************************************************************/
373 #define MAX_DIR_SIZE 10240
375 /****************************************************************************
376 * FileToUrl: create a good name for an url from filename
377 ****************************************************************************/
378 static char *FileToUrl( char *name )
382 url = p = malloc( strlen( name ) + 1 );
385 while( *name == '\\' || *name == '/' )
387 while( *name == '/' )
397 /* convert '\\' into '/' */
410 if( ( p = strrchr( url, '/' ) ) != NULL )
412 if( !strncmp( p, "/index.", 7 ) )
420 /****************************************************************************
421 * FileToMime: XXX duplicated with modules/access_out/http.c
422 ****************************************************************************/
429 { ".htm", "text/html" },
430 { ".html", "text/html" },
433 { ".avi", "video/avi" },
434 { ".asf", "video/x-ms-asf" },
435 { ".m1a", "audio/mpeg" },
436 { ".m2a", "audio/mpeg" },
437 { ".m1v", "video/mpeg" },
438 { ".m2v", "video/mpeg" },
439 { ".mp2", "audio/mpeg" },
440 { ".mp3", "audio/mpeg" },
441 { ".mpa", "audio/mpeg" },
442 { ".mpg", "video/mpeg" },
443 { ".mpeg", "video/mpeg" },
444 { ".mpe", "video/mpeg" },
445 { ".mov", "video/quicktime" },
446 { ".moov", "video/quicktime" },
447 { ".ogg", "application/ogg" },
448 { ".ogm", "application/ogg" },
449 { ".wav", "audio/wav" },
455 static char *FileToMime( char *psz_name )
459 psz_ext = strrchr( psz_name, '.' );
464 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
466 if( !strcmp( http_mime[i].psz_ext, psz_ext ) )
468 return( http_mime[i].psz_mime );
472 return( "application/octet-stream" );
475 /****************************************************************************
476 * ParseDirectory: parse recursively a directory, adding each file
477 ****************************************************************************/
478 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
481 intf_sys_t *p_sys = p_intf->p_sys;
482 char dir[MAX_DIR_SIZE];
483 #ifdef HAVE_SYS_STAT_H
484 struct stat stat_info;
487 struct dirent *p_dir_content;
491 char *password = NULL;
493 #ifdef HAVE_SYS_STAT_H
494 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
500 if( ( p_dir = opendir( psz_dir ) ) == NULL )
502 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
506 msg_Dbg( p_intf, "dir=%s", psz_dir );
508 sprintf( dir, "%s/.access", psz_dir );
509 if( ( file = fopen( dir, "r" ) ) != NULL )
514 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
516 i_size = fread( line, 1, 1023, file );
520 while( i_size > 0 && ( line[i_size-1] == '\n' ||
521 line[i_size-1] == '\r' ) )
528 p = strchr( line, ':' );
532 user = strdup( line );
533 password = strdup( p );
536 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
537 user, password, i_size );
544 /* parse psz_src dir */
545 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
550 if( p_dir_content->d_name[0] == '.' )
554 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
555 if( ParseDirectory( p_intf, psz_root, dir ) )
557 #define f p_sys->pp_files[p_sys->i_files]
558 f = malloc( sizeof( httpd_file_callback_args_t ) );
560 f->file = strdup( dir );
561 f->name = FileToUrl( &dir[strlen( psz_root )] );
562 f->mime = FileToMime( &dir[strlen( psz_root )] );
564 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)",
565 f->file, f->name, f->mime );
568 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
576 p_sys->pp_files = realloc( p_sys->pp_files,
577 (p_sys->i_files+1) * sizeof( httpd_file_callback_args_t ) );
579 #define fold p_sys->pp_files[p_sys->i_files-1]
581 /* FIXME for rep/ add rep (it would be better to do a redirection) */
582 if( strlen(fold->name) > 1 &&
583 fold->name[strlen(fold->name) - 1] == '/' )
585 f = malloc( sizeof( httpd_file_callback_args_t ) );
587 f->file = strdup( fold->file );
588 f->name = strdup( fold->name );
589 f->mime = fold->mime;
591 f->name[strlen(f->name) - 1] = '\0';
592 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)", f->file, f->name,
595 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
604 realloc( p_sys->pp_files, (p_sys->i_files+1) *
605 sizeof( httpd_file_callback_args_t ) );
624 /****************************************************************************
625 * var and set handling
626 ****************************************************************************/
628 static mvar_t *mvar_New( char *name, char *value )
630 mvar_t *v = malloc( sizeof( mvar_t ) );
632 v->name = strdup( name );
633 v->value = strdup( value ? value : "" );
636 v->field = malloc( sizeof( mvar_t * ) );
642 static void mvar_Delete( mvar_t *v )
649 for( i = 0; i < v->i_field; i++ )
651 mvar_Delete( v->field[i] );
657 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
659 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
660 v->field[v->i_field] = f;
664 static mvar_t *mvar_Duplicate( mvar_t *v )
669 n = mvar_New( v->name, v->value );
670 for( i = 0; i < v->i_field; i++ )
672 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
678 static void mvar_PushVar( mvar_t *v, mvar_t *f )
680 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
683 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
689 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
692 for( i = 0; i < v->i_field; i++ )
694 if( v->field[i] == f )
699 if( i >= v->i_field )
704 if( i + 1 < v->i_field )
706 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
709 /* FIXME should do a realloc */
712 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
715 char base[512], *field, *p;
718 /* format: name[index].field */
720 field = strchr( name, '.' );
723 int i = field - name;
724 strncpy( base, name, i );
730 strcpy( base, name );
733 if( ( p = strchr( base, '[' ) ) )
736 sscanf( p, "%d]", &i_index );
747 for( i = 0; i < s->i_field; i++ )
749 if( !strcmp( s->field[i]->name, base ) )
759 return mvar_GetVar( s->field[i], field );
773 static char *mvar_GetValue( mvar_t *v, char *field )
781 mvar_t *f = mvar_GetVar( v, field );
793 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
795 mvar_t *f = mvar_New( name, value );
796 mvar_PushVar( vars, f );
799 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
801 mvar_t *f = mvar_New( name, value );
802 mvar_AppendVar( vars, f );
806 /* arg= start[:stop[:step]],.. */
807 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
809 char *dup = strdup( arg );
811 mvar_t *s = mvar_New( name, "set" );
813 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
818 int i_start,i_stop,i_step;
821 p = strchr( str, ',' );
828 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
829 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
836 else if( i_match == 2 )
838 i_step = i_start < i_stop ? 1 : -1;
845 if( ( i_start < i_stop && i_step > 0 ) ||
846 ( i_start > i_stop && i_step < 0 ) )
848 for( i = i_start; ; i += i_step )
852 if( ( i_step > 0 && i > i_stop ) ||
853 ( i_step < 0 && i < i_stop ) )
858 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
859 sprintf( value, "%d", i );
861 mvar_PushNewVar( s, name, value );
872 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
874 mvar_t *s = mvar_New( name, "set" );
877 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
879 vlc_mutex_lock( &p_pl->object_lock );
880 for( i = 0; i < p_pl->i_size; i++ )
882 mvar_t *itm = mvar_New( name, "set" );
885 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
886 mvar_AppendNewVar( itm, "current", value );
888 sprintf( value, "%d", i );
889 mvar_AppendNewVar( itm, "index", value );
891 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->psz_name );
893 mvar_AppendVar( s, itm );
895 vlc_mutex_unlock( &p_pl->object_lock );
900 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
902 mvar_t *s = mvar_New( name, "set" );
904 input_info_category_t * p_category;
905 input_info_t * p_info;
907 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
908 if( p_input == NULL )
913 vlc_mutex_lock( &p_input->stream.stream_lock );
914 p_category = p_input->stream.p_info;
917 mvar_t *cat = mvar_New( name, "set" );
918 mvar_t *iset = mvar_New( "info", "set" );
920 mvar_AppendNewVar( cat, "name", p_category->psz_name );
921 mvar_AppendVar( cat, iset );
923 p_info = p_category->p_info;
926 mvar_t *info = mvar_New( "info", "" );
928 msg_Dbg( p_input, "adding info name=%s value=%s", p_info->psz_name, p_info->psz_value );
929 mvar_AppendNewVar( info, "name", p_info->psz_name );
930 mvar_AppendNewVar( info, "value", p_info->psz_value );
931 mvar_AppendVar( iset, info );
932 p_info = p_info->p_next;
934 mvar_AppendVar( s, cat );
935 p_category = p_category->p_next;
937 vlc_mutex_unlock( &p_input->stream.stream_lock );
942 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
944 mvar_t *s = mvar_New( name, "set" );
948 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
949 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
951 for( i= 0; i < info.i_count; )
955 inf = mvar_New( name, "set" );
958 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
959 info.info[i].psz_name, info.info[i].psz_value ); */
960 mvar_AppendNewVar( inf,
961 info.info[i].psz_name,
962 info.info[i].psz_value );
964 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
965 mvar_AppendVar( s, inf );
970 for( i = 0; i < info.i_count; i++ )
972 free( info.info[i].psz_name );
973 free( info.info[i].psz_value );
978 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
980 mvar_t *s = mvar_New( name, "set" );
981 char tmp[MAX_DIR_SIZE], *p, *src;
982 #ifdef HAVE_SYS_STAT_H
983 struct stat stat_info;
986 struct dirent *p_dir_content;
989 /* convert all / to native separator */
991 while( (p = strchr( psz_dir, '/' )) )
1000 /* remove trailling separoator */
1001 while( strlen( psz_dir ) > 1 && psz_dir[strlen( psz_dir ) -1 ] == sep )
1003 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1005 /* remove double separator */
1006 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1008 if( src[0] == sep && src[1] == sep )
1016 if( *psz_dir == '\0' )
1020 /* first fix all .. dir */
1024 if( src[0] == '.' && src[1] == '.' )
1027 if( p <= &psz_dir[1] )
1034 while( p > &psz_dir[1] && *p != sep )
1039 else if( *src == sep )
1041 if( p > psz_dir && p[-1] == sep )
1055 } while( *src && *src != sep );
1060 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1062 #ifdef HAVE_SYS_STAT_H
1063 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1069 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1071 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1075 /* remove traling / or \ */
1076 for( p = &psz_dir[strlen( psz_dir) - 1]; p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1085 /* parse psz_src dir */
1086 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1090 if( !strcmp( p_dir_content->d_name, "." ) )
1095 #if defined( WIN32 )
1096 sprintf( tmp, "%s\\%s", psz_dir, p_dir_content->d_name );
1098 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1101 #ifdef HAVE_SYS_STAT_H
1102 if( stat( tmp, &stat_info ) == -1 )
1107 f = mvar_New( name, "set" );
1108 mvar_AppendNewVar( f, "name", tmp );
1109 #ifdef HAVE_SYS_STAT_H
1110 if( S_ISDIR( stat_info.st_mode ) )
1112 mvar_AppendNewVar( f, "type", "directory" );
1114 else if( S_ISREG( stat_info.st_mode ) )
1116 mvar_AppendNewVar( f, "type", "file" );
1120 mvar_AppendNewVar( f, "type", "unknown" );
1123 sprintf( tmp, "%lld", stat_info.st_size );
1124 mvar_AppendNewVar( f, "size", tmp );
1126 /* FIXME memory leak FIXME */
1128 ctime_r( &stat_info.st_mtime, tmp );
1129 mvar_AppendNewVar( f, "date", tmp );
1131 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1135 mvar_AppendNewVar( f, "type", "unknown" );
1136 mvar_AppendNewVar( f, "size", "unknown" );
1137 mvar_AppendNewVar( f, "date", "unknown" );
1139 mvar_AppendVar( s, f );
1145 static void SSInit( rpn_stack_t * );
1146 static void SSClean( rpn_stack_t * );
1147 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1149 static void SSPush ( rpn_stack_t *, char * );
1150 static char *SSPop ( rpn_stack_t * );
1152 static void SSPushN ( rpn_stack_t *, int );
1153 static int SSPopN ( rpn_stack_t *, mvar_t * );
1156 /****************************************************************************
1158 ****************************************************************************/
1166 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1170 /* just load the file */
1172 *pp_data = malloc( 1025 ); /* +1 for \0 */
1173 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1176 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1182 (*pp_data)[*pi_data] = '\0';
1187 static int MacroParse( macro_t *m, uint8_t *psz_src )
1189 uint8_t *dup = strdup( psz_src );
1194 #define EXTRACT( name, l ) \
1196 p = strchr( src, '"' ); \
1201 m->name = strdup( src ); \
1222 if( !strncmp( src, "id=\"", 4 ) )
1226 else if( !strncmp( src, "param1=\"", 8 ) )
1228 EXTRACT( param1, 8 );
1230 else if( !strncmp( src, "param2=\"", 8 ) )
1232 EXTRACT( param2, 8 );
1239 if( strstr( src, "/>" ) )
1241 src = strstr( src, "/>" ) + 2;
1245 src += strlen( src );
1250 m->id = strdup( "" );
1252 if( m->param1 == NULL )
1254 m->param1 = strdup( "" );
1256 if( m->param2 == NULL )
1258 m->param2 = strdup( "" );
1267 static void MacroClean( macro_t *m )
1308 StrToMacroTypeTab [] =
1310 { "control", MVLC_CONTROL },
1311 /* player control */
1312 { "play", MVLC_PLAY },
1313 { "stop", MVLC_STOP },
1314 { "pause", MVLC_PAUSE },
1315 { "next", MVLC_NEXT },
1316 { "previous", MVLC_PREVIOUS },
1318 /* playlist management */
1319 { "add", MVLC_ADD },
1320 { "del", MVLC_DEL },
1321 { "empty", MVLC_EMPTY },
1324 { "close", MVLC_CLOSE },
1325 { "shutdown", MVLC_SHUTDOWN },
1327 { "rpn", MVLC_RPN },
1329 { "foreach", MVLC_FOREACH },
1330 { "value", MVLC_VALUE },
1333 { "else", MVLC_ELSE },
1334 { "end", MVLC_END },
1335 { "get", MVLC_GET },
1336 { "set", MVLC_SET },
1337 { "int", MVLC_INT },
1338 { "float", MVLC_FLOAT },
1339 { "string", MVLC_STRING },
1342 { NULL, MVLC_UNKNOWN }
1345 static int StrToMacroType( char *name )
1349 if( !name || *name == '\0')
1351 return MVLC_UNKNOWN;
1353 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1355 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1357 return StrToMacroTypeTab[i].i_type;
1360 return MVLC_UNKNOWN;
1363 static void MacroDo( httpd_file_callback_args_t *p_args,
1365 uint8_t *p_request, int i_request,
1366 uint8_t **pp_data, int *pi_data,
1369 intf_thread_t *p_intf = p_args->p_intf;
1370 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1373 #define ALLOC( l ) \
1375 int __i__ = *pp_dst - *pp_data; \
1377 *pp_data = realloc( *pp_data, *pi_data ); \
1378 *pp_dst = (*pp_data) + __i__; \
1380 #define PRINT( str ) \
1381 ALLOC( strlen( str ) + 1 ); \
1382 *pp_dst += sprintf( *pp_dst, str );
1384 #define PRINTS( str, s ) \
1385 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1386 *pp_dst += sprintf( *pp_dst, str, s );
1388 switch( StrToMacroType( m->id ) )
1391 if( i_request <= 0 )
1395 uri_extract_value( p_request, "control", control, 512 );
1396 if( *m->param1 && !strstr( m->param1, control ) )
1398 msg_Warn( p_intf, "unauthorized control=%s", control );
1401 switch( StrToMacroType( control ) )
1408 uri_extract_value( p_request, "item", item, 512 );
1409 i_item = atoi( item );
1410 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
1411 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1415 playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1416 msg_Dbg( p_intf, "requested playlist stop" );
1419 playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1420 msg_Dbg( p_intf, "requested playlist pause" );
1423 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1424 p_sys->p_playlist->i_index + 1 );
1425 msg_Dbg( p_intf, "requested playlist next" );
1428 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1429 p_sys->p_playlist->i_index - 1 );
1430 msg_Dbg( p_intf, "requested playlist next" );
1433 /* playlist management */
1437 uri_extract_value( p_request, "mrl", mrl, 512 );
1438 uri_decode_url_encoded( mrl );
1439 playlist_Add( p_sys->p_playlist, mrl, NULL, 0,
1440 PLAYLIST_APPEND, PLAYLIST_END );
1441 msg_Dbg( p_intf, "requested playlist add: %s", mrl );
1449 uri_extract_value( p_request, "item", item, 512 );
1450 i_item = atoi( item );
1452 playlist_Delete( p_sys->p_playlist, i_item );
1453 msg_Dbg( p_intf, "requested playlist del: %d", i_item );
1458 while( p_sys->p_playlist->i_size > 0 )
1460 playlist_Delete( p_sys->p_playlist, 0 );
1462 msg_Dbg( p_intf, "requested playlist empty" );
1466 /* admin function */
1470 uri_extract_value( p_request, "id", id, 512 );
1471 msg_Dbg( p_intf, "requested close id=%s", id );
1472 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
1474 msg_Warn( p_intf, "close failed for id=%s", id );
1480 msg_Dbg( p_intf, "requested shutdown" );
1481 p_intf->p_vlc->b_die = VLC_TRUE;
1485 PRINTS( "<!-- control param(%s) unsuported -->", control );
1496 if( i_request <= 0 ||
1497 *m->param1 == '\0' ||
1498 strstr( p_request, m->param1 ) == NULL )
1502 uri_extract_value( p_request, m->param1, value, 512 );
1503 uri_decode_url_encoded( value );
1505 switch( StrToMacroType( m->param2 ) )
1509 config_PutInt( p_intf, m->param1, i );
1513 config_PutFloat( p_intf, m->param1, f );
1516 config_PutPsz( p_intf, m->param1, value );
1519 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
1530 if( *m->param1 == '\0' )
1535 switch( StrToMacroType( m->param2 ) )
1538 i = config_GetInt( p_intf, m->param1 );
1539 sprintf( value, "%i", i );
1542 f = config_GetFloat( p_intf, m->param1 );
1543 sprintf( value, "%f", f );
1546 psz = config_GetPsz( p_intf, m->param1 );
1547 sprintf( value, "%s", psz ? psz : "" );
1550 sprintf( value, "invalid type(%s) in set", m->param2 );
1553 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
1554 PRINTS( "%s", value );
1563 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1564 s = SSPop( &p_args->stack );
1565 v = mvar_GetValue( p_args->vars, s );
1569 v = s = SSPop( &p_args->stack );
1577 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1581 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
1582 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
1590 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
1597 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
1602 i_skip = MacroParse( &m, src );
1604 i_id = StrToMacroType( m.id );
1619 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
1620 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
1622 return src + ( b_after ? i_skip : 0 );
1624 else if( i_level < 0 )
1640 static void Execute( httpd_file_callback_args_t *p_args,
1641 uint8_t *p_request, int i_request,
1642 uint8_t **pp_data, int *pi_data,
1644 uint8_t *_src, uint8_t *_end )
1646 intf_thread_t *p_intf = p_args->p_intf;
1648 uint8_t *src, *dup, *end;
1649 uint8_t *dst = *pp_dst;
1651 src = dup = malloc( _end - _src + 1 );
1652 end = src +( _end - _src );
1654 memcpy( src, _src, _end - _src );
1657 /* we parse searching <vlc */
1663 p = strstr( src, "<vlc" );
1664 if( p < end && p == src )
1668 src += MacroParse( &m, src );
1670 //msg_Dbg( p_intf, "macro_id=%s", m.id );
1672 switch( StrToMacroType( m.id ) )
1679 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
1680 if( SSPopN( &p_args->stack, p_args->vars ) )
1688 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
1692 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
1696 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
1699 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
1703 else if( i_test == 1 )
1706 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
1708 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
1712 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
1721 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
1722 uint8_t *start = src;
1723 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
1730 if( !strcmp( m.param2, "integer" ) )
1732 char *arg = SSPop( &p_args->stack );
1733 index = mvar_IntegerSetNew( m.param1, arg );
1736 else if( !strcmp( m.param2, "directory" ) )
1738 char *arg = SSPop( &p_args->stack );
1739 index = mvar_FileSetNew( m.param1, arg );
1742 else if( !strcmp( m.param2, "playlist" ) )
1744 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
1746 else if( !strcmp( m.param2, "informations" ) )
1748 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
1750 else if( !strcmp( m.param2, "hosts" ) )
1752 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
1754 else if( !strcmp( m.param2, "urls" ) )
1756 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
1758 else if( !strcmp( m.param2, "connections" ) )
1760 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
1762 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
1764 index = mvar_Duplicate( v );
1768 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
1773 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
1775 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
1777 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
1780 f->name = strdup( m.param1 );
1783 mvar_PushVar( p_args->vars, f );
1784 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
1785 mvar_RemoveVar( p_args->vars, f );
1789 mvar_Delete( index );
1796 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
1804 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
1807 int i_index = dst - *pp_data;
1810 *pp_data = realloc( *pp_data, *pi_data );
1811 dst = (*pp_data) + i_index;
1813 memcpy( dst, src, i_copy );
1823 /****************************************************************************
1825 ****************************************************************************
1826 * a file with mime == text/html is parsed and all "macro" replaced
1827 * <vlc id="macro name" [param1="" [param2=""]] />
1830 ****************************************************************************/
1831 static int http_get( httpd_file_callback_args_t *p_args,
1832 uint8_t *p_request, int i_request,
1833 uint8_t **pp_data, int *pi_data )
1838 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
1840 p = *pp_data = malloc( 10240 );
1842 p += sprintf( p, "<html>\n" );
1843 p += sprintf( p, "<head>\n" );
1844 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
1845 p += sprintf( p, "</head>\n" );
1846 p += sprintf( p, "<body>\n" );
1847 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
1848 p += sprintf( p, "<hr />\n" );
1849 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1850 p += sprintf( p, "</body>\n" );
1851 p += sprintf( p, "</html>\n" );
1853 *pi_data = strlen( *pp_data ) + 1;
1858 if( strcmp( p_args->mime, "text/html" ) )
1860 FileLoad( f, pp_data, pi_data );
1868 p_args->vars = mvar_New( "variables", "" );
1869 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
1870 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
1871 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
1872 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
1874 SSInit( &p_args->stack );
1876 /* first we load in a temporary buffer */
1877 FileLoad( f, &p_buffer, &i_buffer );
1879 /* allocate output */
1880 *pi_data = i_buffer + 1000;
1881 dst = *pp_data = malloc( *pi_data );
1883 /* we parse executing all <vlc /> macros */
1884 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
1887 *pi_data = dst - *pp_data;
1889 SSClean( &p_args->stack );
1890 mvar_Delete( p_args->vars );
1898 /****************************************************************************
1900 ****************************************************************************/
1901 static void uri_extract_value( char *psz_uri, char *psz_name,
1902 char *psz_value, int i_value_max )
1906 p = strstr( psz_uri, psz_name );
1911 p += strlen( psz_name );
1912 if( *p == '=' ) p++;
1914 if( strchr( p, '&' ) )
1916 i_len = strchr( p, '&' ) - p;
1920 /* for POST method */
1921 if( strchr( p, '\n' ) )
1923 i_len = strchr( p, '\n' ) - p;
1924 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
1928 i_len = strlen( p );
1931 i_len = __MIN( i_value_max - 1, i_len );
1934 strncpy( psz_value, p, i_len );
1935 psz_value[i_len] = '\0';
1939 strncpy( psz_value, "", i_value_max );
1944 strncpy( psz_value, "", i_value_max );
1948 static void uri_decode_url_encoded( char *psz )
1950 char *dup = strdup( psz );
1968 *psz++ = strtol( val, NULL, 16 );
1970 else if( *p == '+' )
1984 /****************************************************************************
1985 * Light RPN evaluator
1986 ****************************************************************************/
1987 static void SSInit( rpn_stack_t *st )
1992 static void SSClean( rpn_stack_t *st )
1994 while( st->i_stack > 0 )
1996 free( st->stack[--st->i_stack] );
2000 static void SSPush( rpn_stack_t *st, char *s )
2002 if( st->i_stack < STACK_MAX )
2004 st->stack[st->i_stack++] = strdup( s );
2008 static char * SSPop( rpn_stack_t *st )
2010 if( st->i_stack <= 0 )
2012 return strdup( "" );
2016 return st->stack[--st->i_stack];
2020 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2029 i = strtol( name, &end, 0 );
2032 value = mvar_GetValue( vars, name );
2040 static void SSPushN( rpn_stack_t *st, int i )
2044 sprintf( v, "%d", i );
2048 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2055 while( *exp == ' ' )
2062 /* extract string */
2065 while( *exp && *exp != '\'' )
2076 p = strchr( exp, ' ' );
2081 exp += strlen( exp );
2086 strncpy( s, exp, i );
2097 /* 1. Integer function */
2098 if( !strcmp( s, "!" ) )
2100 SSPushN( st, ~SSPopN( st, vars ) );
2102 else if( !strcmp( s, "^" ) )
2104 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2106 else if( !strcmp( s, "&" ) )
2108 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2110 else if( !strcmp( s, "|" ) )
2112 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2114 else if( !strcmp( s, "+" ) )
2116 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2118 else if( !strcmp( s, "-" ) )
2120 int i = SSPopN( st, vars );
2121 int j = SSPopN( st, vars );
2122 SSPushN( st, i - j );
2124 else if( !strcmp( s, "*" ) )
2126 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2128 else if( !strcmp( s, "/" ) )
2132 i = SSPopN( st, vars );
2133 j = SSPopN( st, vars );
2135 SSPushN( st, j != 0 ? i / j : 0 );
2137 else if( !strcmp( s, "%" ) )
2141 i = SSPopN( st, vars );
2142 j = SSPopN( st, vars );
2144 SSPushN( st, j != 0 ? i % j : 0 );
2146 /* 2. integer tests */
2147 else if( !strcmp( s, "=" ) )
2149 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2151 else if( !strcmp( s, "<" ) )
2153 int i = SSPopN( st, vars );
2154 int j = SSPopN( st, vars );
2156 SSPushN( st, i < j ? -1 : 0 );
2158 else if( !strcmp( s, ">" ) )
2160 int i = SSPopN( st, vars );
2161 int j = SSPopN( st, vars );
2163 SSPushN( st, i > j ? -1 : 0 );
2165 else if( !strcmp( s, "<=" ) )
2167 int i = SSPopN( st, vars );
2168 int j = SSPopN( st, vars );
2170 SSPushN( st, i <= j ? -1 : 0 );
2172 else if( !strcmp( s, ">=" ) )
2174 int i = SSPopN( st, vars );
2175 int j = SSPopN( st, vars );
2177 SSPushN( st, i >= j ? -1 : 0 );
2179 /* 3. string functions */
2180 else if( !strcmp( s, "strcat" ) )
2182 char *s1 = SSPop( st );
2183 char *s2 = SSPop( st );
2184 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2194 else if( !strcmp( s, "strcmp" ) )
2196 char *s1 = SSPop( st );
2197 char *s2 = SSPop( st );
2199 SSPushN( st, strcmp( s1, s2 ) );
2203 else if( !strcmp( s, "strlen" ) )
2205 char *str = SSPop( st );
2207 SSPushN( st, strlen( str ) );
2210 /* 4. stack functions */
2211 else if( !strcmp( s, "dup" ) )
2213 char *str = SSPop( st );
2218 else if( !strcmp( s, "drop" ) )
2220 char *str = SSPop( st );
2223 else if( !strcmp( s, "swap" ) )
2225 char *s1 = SSPop( st );
2226 char *s2 = SSPop( st );
2233 else if( !strcmp( s, "flush" ) )
2238 else if( !strcmp( s, "store" ) )
2240 char *name = SSPop( st );
2241 char *value = SSPop( st );
2243 mvar_PushNewVar( vars, name, value );
2247 else if( !strcmp( s, "value" ) )
2249 char *name = SSPop( st );
2250 char *value = mvar_GetValue( vars, name );
2252 SSPush( st, value );
2256 else if( !strcmp( s, "url_extract" ) )
2258 char *url = mvar_GetValue( vars, "url_value" );
2259 char *name = SSPop( st );
2262 uri_extract_value( url, name, value, 512 );
2263 uri_decode_url_encoded( value );
2264 SSPush( st, value );