1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: http.c,v 1.21 2003/08/01 17:30:33 fenrir 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" },
432 { ".css", "text/css" },
435 { ".avi", "video/avi" },
436 { ".asf", "video/x-ms-asf" },
437 { ".m1a", "audio/mpeg" },
438 { ".m2a", "audio/mpeg" },
439 { ".m1v", "video/mpeg" },
440 { ".m2v", "video/mpeg" },
441 { ".mp2", "audio/mpeg" },
442 { ".mp3", "audio/mpeg" },
443 { ".mpa", "audio/mpeg" },
444 { ".mpg", "video/mpeg" },
445 { ".mpeg", "video/mpeg" },
446 { ".mpe", "video/mpeg" },
447 { ".mov", "video/quicktime" },
448 { ".moov", "video/quicktime" },
449 { ".ogg", "application/ogg" },
450 { ".ogm", "application/ogg" },
451 { ".wav", "audio/wav" },
457 static char *FileToMime( char *psz_name )
461 psz_ext = strrchr( psz_name, '.' );
466 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
468 if( !strcmp( http_mime[i].psz_ext, psz_ext ) )
470 return( http_mime[i].psz_mime );
474 return( "application/octet-stream" );
477 /****************************************************************************
478 * ParseDirectory: parse recursively a directory, adding each file
479 ****************************************************************************/
480 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
483 intf_sys_t *p_sys = p_intf->p_sys;
484 char dir[MAX_DIR_SIZE];
485 #ifdef HAVE_SYS_STAT_H
486 struct stat stat_info;
489 struct dirent *p_dir_content;
493 char *password = NULL;
495 #ifdef HAVE_SYS_STAT_H
496 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
502 if( ( p_dir = opendir( psz_dir ) ) == NULL )
504 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
508 msg_Dbg( p_intf, "dir=%s", psz_dir );
510 sprintf( dir, "%s/.access", psz_dir );
511 if( ( file = fopen( dir, "r" ) ) != NULL )
516 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
518 i_size = fread( line, 1, 1023, file );
522 while( i_size > 0 && ( line[i_size-1] == '\n' ||
523 line[i_size-1] == '\r' ) )
530 p = strchr( line, ':' );
534 user = strdup( line );
535 password = strdup( p );
538 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
539 user, password, i_size );
546 /* parse psz_src dir */
547 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
552 if( p_dir_content->d_name[0] == '.' )
556 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
557 if( ParseDirectory( p_intf, psz_root, dir ) )
559 #define f p_sys->pp_files[p_sys->i_files]
560 f = malloc( sizeof( httpd_file_callback_args_t ) );
562 f->file = strdup( dir );
563 f->name = FileToUrl( &dir[strlen( psz_root )] );
564 f->mime = FileToMime( &dir[strlen( psz_root )] );
566 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)",
567 f->file, f->name, f->mime );
570 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
578 p_sys->pp_files = realloc( p_sys->pp_files,
579 (p_sys->i_files+1) * sizeof( httpd_file_callback_args_t ) );
581 #define fold p_sys->pp_files[p_sys->i_files-1]
583 /* FIXME for rep/ add rep (it would be better to do a redirection) */
584 if( strlen(fold->name) > 1 &&
585 fold->name[strlen(fold->name) - 1] == '/' )
587 f = malloc( sizeof( httpd_file_callback_args_t ) );
589 f->file = strdup( fold->file );
590 f->name = strdup( fold->name );
591 f->mime = fold->mime;
593 f->name[strlen(f->name) - 1] = '\0';
594 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)", f->file, f->name,
597 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
606 realloc( p_sys->pp_files, (p_sys->i_files+1) *
607 sizeof( httpd_file_callback_args_t ) );
626 /****************************************************************************
627 * var and set handling
628 ****************************************************************************/
630 static mvar_t *mvar_New( char *name, char *value )
632 mvar_t *v = malloc( sizeof( mvar_t ) );
634 v->name = strdup( name );
635 v->value = strdup( value ? value : "" );
638 v->field = malloc( sizeof( mvar_t * ) );
644 static void mvar_Delete( mvar_t *v )
651 for( i = 0; i < v->i_field; i++ )
653 mvar_Delete( v->field[i] );
659 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
661 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
662 v->field[v->i_field] = f;
666 static mvar_t *mvar_Duplicate( mvar_t *v )
671 n = mvar_New( v->name, v->value );
672 for( i = 0; i < v->i_field; i++ )
674 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
680 static void mvar_PushVar( mvar_t *v, mvar_t *f )
682 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
685 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
691 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
694 for( i = 0; i < v->i_field; i++ )
696 if( v->field[i] == f )
701 if( i >= v->i_field )
706 if( i + 1 < v->i_field )
708 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
711 /* FIXME should do a realloc */
714 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
717 char base[512], *field, *p;
720 /* format: name[index].field */
722 field = strchr( name, '.' );
725 int i = field - name;
726 strncpy( base, name, i );
732 strcpy( base, name );
735 if( ( p = strchr( base, '[' ) ) )
738 sscanf( p, "%d]", &i_index );
749 for( i = 0; i < s->i_field; i++ )
751 if( !strcmp( s->field[i]->name, base ) )
761 return mvar_GetVar( s->field[i], field );
775 static char *mvar_GetValue( mvar_t *v, char *field )
783 mvar_t *f = mvar_GetVar( v, field );
795 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
797 mvar_t *f = mvar_New( name, value );
798 mvar_PushVar( vars, f );
801 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
803 mvar_t *f = mvar_New( name, value );
804 mvar_AppendVar( vars, f );
808 /* arg= start[:stop[:step]],.. */
809 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
811 char *dup = strdup( arg );
813 mvar_t *s = mvar_New( name, "set" );
815 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
820 int i_start,i_stop,i_step;
823 p = strchr( str, ',' );
830 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
831 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
838 else if( i_match == 2 )
840 i_step = i_start < i_stop ? 1 : -1;
847 if( ( i_start < i_stop && i_step > 0 ) ||
848 ( i_start > i_stop && i_step < 0 ) )
850 for( i = i_start; ; i += i_step )
854 if( ( i_step > 0 && i > i_stop ) ||
855 ( i_step < 0 && i < i_stop ) )
860 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
861 sprintf( value, "%d", i );
863 mvar_PushNewVar( s, name, value );
874 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
876 mvar_t *s = mvar_New( name, "set" );
879 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
881 vlc_mutex_lock( &p_pl->object_lock );
882 for( i = 0; i < p_pl->i_size; i++ )
884 mvar_t *itm = mvar_New( name, "set" );
887 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
888 mvar_AppendNewVar( itm, "current", value );
890 sprintf( value, "%d", i );
891 mvar_AppendNewVar( itm, "index", value );
893 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->psz_name );
895 mvar_AppendVar( s, itm );
897 vlc_mutex_unlock( &p_pl->object_lock );
902 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
904 mvar_t *s = mvar_New( name, "set" );
906 input_info_category_t * p_category;
907 input_info_t * p_info;
909 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
910 if( p_input == NULL )
915 vlc_mutex_lock( &p_input->stream.stream_lock );
916 p_category = p_input->stream.p_info;
919 mvar_t *cat = mvar_New( name, "set" );
920 mvar_t *iset = mvar_New( "info", "set" );
922 mvar_AppendNewVar( cat, "name", p_category->psz_name );
923 mvar_AppendVar( cat, iset );
925 p_info = p_category->p_info;
928 mvar_t *info = mvar_New( "info", "" );
930 msg_Dbg( p_input, "adding info name=%s value=%s", p_info->psz_name, p_info->psz_value );
931 mvar_AppendNewVar( info, "name", p_info->psz_name );
932 mvar_AppendNewVar( info, "value", p_info->psz_value );
933 mvar_AppendVar( iset, info );
934 p_info = p_info->p_next;
936 mvar_AppendVar( s, cat );
937 p_category = p_category->p_next;
939 vlc_mutex_unlock( &p_input->stream.stream_lock );
944 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
946 mvar_t *s = mvar_New( name, "set" );
950 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
951 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
953 for( i= 0; i < info.i_count; )
957 inf = mvar_New( name, "set" );
960 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
961 info.info[i].psz_name, info.info[i].psz_value ); */
962 mvar_AppendNewVar( inf,
963 info.info[i].psz_name,
964 info.info[i].psz_value );
966 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
967 mvar_AppendVar( s, inf );
972 for( i = 0; i < info.i_count; i++ )
974 free( info.info[i].psz_name );
975 free( info.info[i].psz_value );
980 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
982 mvar_t *s = mvar_New( name, "set" );
983 char tmp[MAX_DIR_SIZE], *p, *src;
984 #ifdef HAVE_SYS_STAT_H
985 struct stat stat_info;
988 struct dirent *p_dir_content;
991 /* convert all / to native separator */
993 while( (p = strchr( psz_dir, '/' )) )
1002 /* remove trailling separator */
1003 while( strlen( psz_dir ) > 1 &&
1004 #if defined( WIN32 )
1005 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1007 psz_dir[strlen( psz_dir ) -1 ] == sep )
1009 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1011 /* remove double separator */
1012 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1014 if( src[0] == sep && src[1] == sep )
1022 if( *psz_dir == '\0' )
1026 /* first fix all .. dir */
1030 if( src[0] == '.' && src[1] == '.' )
1033 if( p <= &psz_dir[1] )
1040 while( p > &psz_dir[1] && *p != sep )
1045 else if( *src == sep )
1047 if( p > psz_dir && p[-1] == sep )
1061 } while( *src && *src != sep );
1066 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1068 #ifdef HAVE_SYS_STAT_H
1069 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1075 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1077 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1081 /* remove traling / or \ */
1082 for( p = &psz_dir[strlen( psz_dir) - 1]; p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1091 /* parse psz_src dir */
1092 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1096 if( !strcmp( p_dir_content->d_name, "." ) )
1101 #if defined( WIN32 )
1102 sprintf( tmp, "%s\\%s", psz_dir, p_dir_content->d_name );
1104 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1107 #ifdef HAVE_SYS_STAT_H
1108 if( stat( tmp, &stat_info ) == -1 )
1113 f = mvar_New( name, "set" );
1114 mvar_AppendNewVar( f, "name", tmp );
1115 #ifdef HAVE_SYS_STAT_H
1116 if( S_ISDIR( stat_info.st_mode ) )
1118 mvar_AppendNewVar( f, "type", "directory" );
1120 else if( S_ISREG( stat_info.st_mode ) )
1122 mvar_AppendNewVar( f, "type", "file" );
1126 mvar_AppendNewVar( f, "type", "unknown" );
1129 sprintf( tmp, "%lld", stat_info.st_size );
1130 mvar_AppendNewVar( f, "size", tmp );
1132 /* FIXME memory leak FIXME */
1134 ctime_r( &stat_info.st_mtime, tmp );
1135 mvar_AppendNewVar( f, "date", tmp );
1137 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1141 mvar_AppendNewVar( f, "type", "unknown" );
1142 mvar_AppendNewVar( f, "size", "unknown" );
1143 mvar_AppendNewVar( f, "date", "unknown" );
1145 mvar_AppendVar( s, f );
1151 static void SSInit( rpn_stack_t * );
1152 static void SSClean( rpn_stack_t * );
1153 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1155 static void SSPush ( rpn_stack_t *, char * );
1156 static char *SSPop ( rpn_stack_t * );
1158 static void SSPushN ( rpn_stack_t *, int );
1159 static int SSPopN ( rpn_stack_t *, mvar_t * );
1162 /****************************************************************************
1164 ****************************************************************************/
1172 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1176 /* just load the file */
1178 *pp_data = malloc( 1025 ); /* +1 for \0 */
1179 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1182 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1188 (*pp_data)[*pi_data] = '\0';
1193 static int MacroParse( macro_t *m, uint8_t *psz_src )
1195 uint8_t *dup = strdup( psz_src );
1200 #define EXTRACT( name, l ) \
1202 p = strchr( src, '"' ); \
1207 m->name = strdup( src ); \
1228 if( !strncmp( src, "id=\"", 4 ) )
1232 else if( !strncmp( src, "param1=\"", 8 ) )
1234 EXTRACT( param1, 8 );
1236 else if( !strncmp( src, "param2=\"", 8 ) )
1238 EXTRACT( param2, 8 );
1245 if( strstr( src, "/>" ) )
1247 src = strstr( src, "/>" ) + 2;
1251 src += strlen( src );
1256 m->id = strdup( "" );
1258 if( m->param1 == NULL )
1260 m->param1 = strdup( "" );
1262 if( m->param2 == NULL )
1264 m->param2 = strdup( "" );
1273 static void MacroClean( macro_t *m )
1314 StrToMacroTypeTab [] =
1316 { "control", MVLC_CONTROL },
1317 /* player control */
1318 { "play", MVLC_PLAY },
1319 { "stop", MVLC_STOP },
1320 { "pause", MVLC_PAUSE },
1321 { "next", MVLC_NEXT },
1322 { "previous", MVLC_PREVIOUS },
1324 /* playlist management */
1325 { "add", MVLC_ADD },
1326 { "del", MVLC_DEL },
1327 { "empty", MVLC_EMPTY },
1330 { "close", MVLC_CLOSE },
1331 { "shutdown", MVLC_SHUTDOWN },
1333 { "rpn", MVLC_RPN },
1335 { "foreach", MVLC_FOREACH },
1336 { "value", MVLC_VALUE },
1339 { "else", MVLC_ELSE },
1340 { "end", MVLC_END },
1341 { "get", MVLC_GET },
1342 { "set", MVLC_SET },
1343 { "int", MVLC_INT },
1344 { "float", MVLC_FLOAT },
1345 { "string", MVLC_STRING },
1348 { NULL, MVLC_UNKNOWN }
1351 static int StrToMacroType( char *name )
1355 if( !name || *name == '\0')
1357 return MVLC_UNKNOWN;
1359 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1361 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1363 return StrToMacroTypeTab[i].i_type;
1366 return MVLC_UNKNOWN;
1369 static void MacroDo( httpd_file_callback_args_t *p_args,
1371 uint8_t *p_request, int i_request,
1372 uint8_t **pp_data, int *pi_data,
1375 intf_thread_t *p_intf = p_args->p_intf;
1376 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1379 #define ALLOC( l ) \
1381 int __i__ = *pp_dst - *pp_data; \
1383 *pp_data = realloc( *pp_data, *pi_data ); \
1384 *pp_dst = (*pp_data) + __i__; \
1386 #define PRINT( str ) \
1387 ALLOC( strlen( str ) + 1 ); \
1388 *pp_dst += sprintf( *pp_dst, str );
1390 #define PRINTS( str, s ) \
1391 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1392 *pp_dst += sprintf( *pp_dst, str, s );
1394 switch( StrToMacroType( m->id ) )
1397 if( i_request <= 0 )
1401 uri_extract_value( p_request, "control", control, 512 );
1402 if( *m->param1 && !strstr( m->param1, control ) )
1404 msg_Warn( p_intf, "unauthorized control=%s", control );
1407 switch( StrToMacroType( control ) )
1414 uri_extract_value( p_request, "item", item, 512 );
1415 i_item = atoi( item );
1416 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
1417 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1421 playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1422 msg_Dbg( p_intf, "requested playlist stop" );
1425 playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1426 msg_Dbg( p_intf, "requested playlist pause" );
1429 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1430 p_sys->p_playlist->i_index + 1 );
1431 msg_Dbg( p_intf, "requested playlist next" );
1434 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1435 p_sys->p_playlist->i_index - 1 );
1436 msg_Dbg( p_intf, "requested playlist next" );
1439 /* playlist management */
1443 uri_extract_value( p_request, "mrl", mrl, 512 );
1444 uri_decode_url_encoded( mrl );
1445 playlist_Add( p_sys->p_playlist, mrl, NULL, 0,
1446 PLAYLIST_APPEND, PLAYLIST_END );
1447 msg_Dbg( p_intf, "requested playlist add: %s", mrl );
1455 uri_extract_value( p_request, "item", item, 512 );
1456 i_item = atoi( item );
1458 playlist_Delete( p_sys->p_playlist, i_item );
1459 msg_Dbg( p_intf, "requested playlist del: %d", i_item );
1464 while( p_sys->p_playlist->i_size > 0 )
1466 playlist_Delete( p_sys->p_playlist, 0 );
1468 msg_Dbg( p_intf, "requested playlist empty" );
1472 /* admin function */
1476 uri_extract_value( p_request, "id", id, 512 );
1477 msg_Dbg( p_intf, "requested close id=%s", id );
1478 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
1480 msg_Warn( p_intf, "close failed for id=%s", id );
1486 msg_Dbg( p_intf, "requested shutdown" );
1487 p_intf->p_vlc->b_die = VLC_TRUE;
1491 PRINTS( "<!-- control param(%s) unsuported -->", control );
1502 if( i_request <= 0 ||
1503 *m->param1 == '\0' ||
1504 strstr( p_request, m->param1 ) == NULL )
1508 uri_extract_value( p_request, m->param1, value, 512 );
1509 uri_decode_url_encoded( value );
1511 switch( StrToMacroType( m->param2 ) )
1515 config_PutInt( p_intf, m->param1, i );
1519 config_PutFloat( p_intf, m->param1, f );
1522 config_PutPsz( p_intf, m->param1, value );
1525 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
1536 if( *m->param1 == '\0' )
1541 switch( StrToMacroType( m->param2 ) )
1544 i = config_GetInt( p_intf, m->param1 );
1545 sprintf( value, "%i", i );
1548 f = config_GetFloat( p_intf, m->param1 );
1549 sprintf( value, "%f", f );
1552 psz = config_GetPsz( p_intf, m->param1 );
1553 sprintf( value, "%s", psz ? psz : "" );
1556 sprintf( value, "invalid type(%s) in set", m->param2 );
1559 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
1560 PRINTS( "%s", value );
1569 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1570 s = SSPop( &p_args->stack );
1571 v = mvar_GetValue( p_args->vars, s );
1575 v = s = SSPop( &p_args->stack );
1583 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1587 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
1588 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
1596 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
1603 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
1608 i_skip = MacroParse( &m, src );
1610 i_id = StrToMacroType( m.id );
1625 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
1626 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
1628 return src + ( b_after ? i_skip : 0 );
1630 else if( i_level < 0 )
1646 static void Execute( httpd_file_callback_args_t *p_args,
1647 uint8_t *p_request, int i_request,
1648 uint8_t **pp_data, int *pi_data,
1650 uint8_t *_src, uint8_t *_end )
1652 intf_thread_t *p_intf = p_args->p_intf;
1654 uint8_t *src, *dup, *end;
1655 uint8_t *dst = *pp_dst;
1657 src = dup = malloc( _end - _src + 1 );
1658 end = src +( _end - _src );
1660 memcpy( src, _src, _end - _src );
1663 /* we parse searching <vlc */
1669 p = strstr( src, "<vlc" );
1670 if( p < end && p == src )
1674 src += MacroParse( &m, src );
1676 //msg_Dbg( p_intf, "macro_id=%s", m.id );
1678 switch( StrToMacroType( m.id ) )
1685 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
1686 if( SSPopN( &p_args->stack, p_args->vars ) )
1694 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
1698 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
1702 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
1705 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
1709 else if( i_test == 1 )
1712 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
1714 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
1718 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
1727 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
1728 uint8_t *start = src;
1729 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
1736 if( !strcmp( m.param2, "integer" ) )
1738 char *arg = SSPop( &p_args->stack );
1739 index = mvar_IntegerSetNew( m.param1, arg );
1742 else if( !strcmp( m.param2, "directory" ) )
1744 char *arg = SSPop( &p_args->stack );
1745 index = mvar_FileSetNew( m.param1, arg );
1748 else if( !strcmp( m.param2, "playlist" ) )
1750 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
1752 else if( !strcmp( m.param2, "informations" ) )
1754 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
1756 else if( !strcmp( m.param2, "hosts" ) )
1758 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
1760 else if( !strcmp( m.param2, "urls" ) )
1762 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
1764 else if( !strcmp( m.param2, "connections" ) )
1766 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
1768 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
1770 index = mvar_Duplicate( v );
1774 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
1779 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
1781 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
1783 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
1786 f->name = strdup( m.param1 );
1789 mvar_PushVar( p_args->vars, f );
1790 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
1791 mvar_RemoveVar( p_args->vars, f );
1795 mvar_Delete( index );
1802 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
1810 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
1813 int i_index = dst - *pp_data;
1816 *pp_data = realloc( *pp_data, *pi_data );
1817 dst = (*pp_data) + i_index;
1819 memcpy( dst, src, i_copy );
1829 /****************************************************************************
1831 ****************************************************************************
1832 * a file with mime == text/html is parsed and all "macro" replaced
1833 * <vlc id="macro name" [param1="" [param2=""]] />
1836 ****************************************************************************/
1837 static int http_get( httpd_file_callback_args_t *p_args,
1838 uint8_t *p_request, int i_request,
1839 uint8_t **pp_data, int *pi_data )
1844 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
1846 p = *pp_data = malloc( 10240 );
1848 p += sprintf( p, "<html>\n" );
1849 p += sprintf( p, "<head>\n" );
1850 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
1851 p += sprintf( p, "</head>\n" );
1852 p += sprintf( p, "<body>\n" );
1853 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
1854 p += sprintf( p, "<hr />\n" );
1855 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1856 p += sprintf( p, "</body>\n" );
1857 p += sprintf( p, "</html>\n" );
1859 *pi_data = strlen( *pp_data ) + 1;
1864 if( strcmp( p_args->mime, "text/html" ) )
1866 FileLoad( f, pp_data, pi_data );
1874 p_args->vars = mvar_New( "variables", "" );
1875 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
1876 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
1877 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
1878 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
1880 SSInit( &p_args->stack );
1882 /* first we load in a temporary buffer */
1883 FileLoad( f, &p_buffer, &i_buffer );
1885 /* allocate output */
1886 *pi_data = i_buffer + 1000;
1887 dst = *pp_data = malloc( *pi_data );
1889 /* we parse executing all <vlc /> macros */
1890 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
1893 *pi_data = dst - *pp_data;
1895 SSClean( &p_args->stack );
1896 mvar_Delete( p_args->vars );
1904 /****************************************************************************
1906 ****************************************************************************/
1907 static void uri_extract_value( char *psz_uri, char *psz_name,
1908 char *psz_value, int i_value_max )
1912 p = strstr( psz_uri, psz_name );
1917 p += strlen( psz_name );
1918 if( *p == '=' ) p++;
1920 if( strchr( p, '&' ) )
1922 i_len = strchr( p, '&' ) - p;
1926 /* for POST method */
1927 if( strchr( p, '\n' ) )
1929 i_len = strchr( p, '\n' ) - p;
1930 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
1934 i_len = strlen( p );
1937 i_len = __MIN( i_value_max - 1, i_len );
1940 strncpy( psz_value, p, i_len );
1941 psz_value[i_len] = '\0';
1945 strncpy( psz_value, "", i_value_max );
1950 strncpy( psz_value, "", i_value_max );
1954 static void uri_decode_url_encoded( char *psz )
1956 char *dup = strdup( psz );
1974 *psz++ = strtol( val, NULL, 16 );
1976 else if( *p == '+' )
1990 /****************************************************************************
1991 * Light RPN evaluator
1992 ****************************************************************************/
1993 static void SSInit( rpn_stack_t *st )
1998 static void SSClean( rpn_stack_t *st )
2000 while( st->i_stack > 0 )
2002 free( st->stack[--st->i_stack] );
2006 static void SSPush( rpn_stack_t *st, char *s )
2008 if( st->i_stack < STACK_MAX )
2010 st->stack[st->i_stack++] = strdup( s );
2014 static char * SSPop( rpn_stack_t *st )
2016 if( st->i_stack <= 0 )
2018 return strdup( "" );
2022 return st->stack[--st->i_stack];
2026 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2035 i = strtol( name, &end, 0 );
2038 value = mvar_GetValue( vars, name );
2046 static void SSPushN( rpn_stack_t *st, int i )
2050 sprintf( v, "%d", i );
2054 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2061 while( *exp == ' ' )
2068 /* extract string */
2071 while( *exp && *exp != '\'' )
2082 p = strchr( exp, ' ' );
2087 exp += strlen( exp );
2092 strncpy( s, exp, i );
2103 /* 1. Integer function */
2104 if( !strcmp( s, "!" ) )
2106 SSPushN( st, ~SSPopN( st, vars ) );
2108 else if( !strcmp( s, "^" ) )
2110 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2112 else if( !strcmp( s, "&" ) )
2114 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2116 else if( !strcmp( s, "|" ) )
2118 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2120 else if( !strcmp( s, "+" ) )
2122 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2124 else if( !strcmp( s, "-" ) )
2126 int i = SSPopN( st, vars );
2127 int j = SSPopN( st, vars );
2128 SSPushN( st, i - j );
2130 else if( !strcmp( s, "*" ) )
2132 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2134 else if( !strcmp( s, "/" ) )
2138 i = SSPopN( st, vars );
2139 j = SSPopN( st, vars );
2141 SSPushN( st, j != 0 ? i / j : 0 );
2143 else if( !strcmp( s, "%" ) )
2147 i = SSPopN( st, vars );
2148 j = SSPopN( st, vars );
2150 SSPushN( st, j != 0 ? i % j : 0 );
2152 /* 2. integer tests */
2153 else if( !strcmp( s, "=" ) )
2155 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2157 else if( !strcmp( s, "<" ) )
2159 int i = SSPopN( st, vars );
2160 int j = SSPopN( st, vars );
2162 SSPushN( st, i < j ? -1 : 0 );
2164 else if( !strcmp( s, ">" ) )
2166 int i = SSPopN( st, vars );
2167 int j = SSPopN( st, vars );
2169 SSPushN( st, i > j ? -1 : 0 );
2171 else if( !strcmp( s, "<=" ) )
2173 int i = SSPopN( st, vars );
2174 int j = SSPopN( st, vars );
2176 SSPushN( st, i <= j ? -1 : 0 );
2178 else if( !strcmp( s, ">=" ) )
2180 int i = SSPopN( st, vars );
2181 int j = SSPopN( st, vars );
2183 SSPushN( st, i >= j ? -1 : 0 );
2185 /* 3. string functions */
2186 else if( !strcmp( s, "strcat" ) )
2188 char *s1 = SSPop( st );
2189 char *s2 = SSPop( st );
2190 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2200 else if( !strcmp( s, "strcmp" ) )
2202 char *s1 = SSPop( st );
2203 char *s2 = SSPop( st );
2205 SSPushN( st, strcmp( s1, s2 ) );
2209 else if( !strcmp( s, "strlen" ) )
2211 char *str = SSPop( st );
2213 SSPushN( st, strlen( str ) );
2216 /* 4. stack functions */
2217 else if( !strcmp( s, "dup" ) )
2219 char *str = SSPop( st );
2224 else if( !strcmp( s, "drop" ) )
2226 char *str = SSPop( st );
2229 else if( !strcmp( s, "swap" ) )
2231 char *s1 = SSPop( st );
2232 char *s2 = SSPop( st );
2239 else if( !strcmp( s, "flush" ) )
2244 else if( !strcmp( s, "store" ) )
2246 char *name = SSPop( st );
2247 char *value = SSPop( st );
2249 mvar_PushNewVar( vars, name, value );
2253 else if( !strcmp( s, "value" ) )
2255 char *name = SSPop( st );
2256 char *value = mvar_GetValue( vars, name );
2258 SSPush( st, value );
2262 else if( !strcmp( s, "url_extract" ) )
2264 char *url = mvar_GetValue( vars, "url_value" );
2265 char *name = SSPop( st );
2268 uri_extract_value( url, name, value, 512 );
2269 uri_decode_url_encoded( value );
2270 SSPush( st, value );