1 /*****************************************************************************
2 * http.c : http mini-server ;)
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: http.c,v 1.23 2003/10/17 18:59:00 zorglub 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 ) );
205 return( VLC_ENOMEM );
207 p_sys->p_playlist = NULL;
208 p_sys->p_input = NULL;
210 if( ( p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_intf), VLC_TRUE ) ) == NULL )
212 msg_Err( p_intf, "cannot create/find httpd" );
217 if( ( p_sys->p_httpd_host =
218 p_sys->p_httpd->pf_register_host( p_sys->p_httpd,
219 psz_address, i_port ) ) == NULL )
221 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
222 httpd_Release( p_sys->p_httpd );
233 p_sys->pp_files = malloc( sizeof( httpd_file_callback_args_t *) );
234 if( !p_sys->pp_files )
236 return( VLC_ENOMEM );
239 #if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
240 ( defined(WIN32) && !defined(UNDER_CE ) )
241 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
243 char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
244 psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
247 return( VLC_ENOMEM );
250 sprintf( psz_src, "%s/http", psz_vlcpath);
252 sprintf( psz_src, "%s/share/http", psz_vlcpath);
256 psz_src = config_GetPsz( p_intf, "http-src" );
258 if( !psz_src || *psz_src == '\0' )
260 if( !DirectoryCheck( "share/http" ) )
262 psz_src = strdup( "share/http" );
264 else if( !DirectoryCheck( DATA_PATH "/http" ) )
266 psz_src = strdup( DATA_PATH "/http" );
271 if( !psz_src || *psz_src == '\0' )
273 msg_Err( p_intf, "invalid src dir" );
277 /* remove trainling \ or / */
278 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
279 psz_src[strlen( psz_src ) - 1] == '/' )
281 psz_src[strlen( psz_src ) - 1] = '\0';
284 ParseDirectory( p_intf, psz_src, psz_src );
287 if( p_sys->i_files <= 0 )
289 msg_Err( p_intf, "cannot find any files (%s)", psz_src );
292 p_intf->pf_run = Run;
297 free( p_sys->pp_files );
298 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
299 p_sys->p_httpd_host );
300 httpd_Release( p_sys->p_httpd );
305 /*****************************************************************************
306 * CloseIntf: destroy interface
307 *****************************************************************************/
308 void Close ( vlc_object_t *p_this )
310 intf_thread_t *p_intf = (intf_thread_t *)p_this;
311 intf_sys_t *p_sys = p_intf->p_sys;
315 for( i = 0; i < p_sys->i_files; i++ )
317 p_sys->p_httpd->pf_unregister_file( p_sys->p_httpd,
318 p_sys->pp_files[i]->p_file );
319 /* do not free mime */
320 free( p_sys->pp_files[i]->file );
321 free( p_sys->pp_files[i]->name );
322 free( p_sys->pp_files[i] );
324 free( p_sys->pp_files );
325 p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
326 p_sys->p_httpd_host );
327 httpd_Release( p_sys->p_httpd );
331 /*****************************************************************************
332 * Run: http interface thread
333 *****************************************************************************/
334 static void Run( intf_thread_t *p_intf )
336 intf_sys_t *p_sys = p_intf->p_sys;
338 while( !p_intf->b_die )
340 /* get the playlist */
341 if( p_sys->p_playlist == NULL )
343 p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
346 /* Manage the input part */
347 if( p_sys->p_input == NULL )
349 if( p_sys->p_playlist )
352 vlc_object_find( p_sys->p_playlist,
357 else if( p_sys->p_input->b_dead )
359 vlc_object_release( p_sys->p_input );
360 p_sys->p_input = NULL;
365 msleep( INTF_IDLE_SLEEP );
370 vlc_object_release( p_sys->p_input );
371 p_sys->p_input = NULL;
374 if( p_sys->p_playlist )
376 vlc_object_release( p_sys->p_playlist );
377 p_sys->p_playlist = NULL;
382 /*****************************************************************************
384 *****************************************************************************/
385 #define MAX_DIR_SIZE 10240
387 /****************************************************************************
388 * FileToUrl: create a good name for an url from filename
389 ****************************************************************************/
390 static char *FileToUrl( char *name )
394 url = p = malloc( strlen( name ) + 1 );
402 while( *name == '\\' || *name == '/' )
404 while( *name == '/' )
414 /* convert '\\' into '/' */
427 if( ( p = strrchr( url, '/' ) ) != NULL )
429 if( !strncmp( p, "/index.", 7 ) )
437 /****************************************************************************
438 * FileToMime: XXX duplicated with modules/access_out/http.c
439 ****************************************************************************/
446 { ".htm", "text/html" },
447 { ".html", "text/html" },
449 { ".css", "text/css" },
452 { ".avi", "video/avi" },
453 { ".asf", "video/x-ms-asf" },
454 { ".m1a", "audio/mpeg" },
455 { ".m2a", "audio/mpeg" },
456 { ".m1v", "video/mpeg" },
457 { ".m2v", "video/mpeg" },
458 { ".mp2", "audio/mpeg" },
459 { ".mp3", "audio/mpeg" },
460 { ".mpa", "audio/mpeg" },
461 { ".mpg", "video/mpeg" },
462 { ".mpeg", "video/mpeg" },
463 { ".mpe", "video/mpeg" },
464 { ".mov", "video/quicktime" },
465 { ".moov", "video/quicktime" },
466 { ".ogg", "application/ogg" },
467 { ".ogm", "application/ogg" },
468 { ".wav", "audio/wav" },
474 static char *FileToMime( char *psz_name )
478 psz_ext = strrchr( psz_name, '.' );
483 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
485 if( !strcmp( http_mime[i].psz_ext, psz_ext ) )
487 return( http_mime[i].psz_mime );
491 return( "application/octet-stream" );
494 /****************************************************************************
495 * ParseDirectory: parse recursively a directory, adding each file
496 ****************************************************************************/
497 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
500 intf_sys_t *p_sys = p_intf->p_sys;
501 char dir[MAX_DIR_SIZE];
502 #ifdef HAVE_SYS_STAT_H
503 struct stat stat_info;
506 struct dirent *p_dir_content;
510 char *password = NULL;
512 #ifdef HAVE_SYS_STAT_H
513 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
519 if( ( p_dir = opendir( psz_dir ) ) == NULL )
521 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
525 msg_Dbg( p_intf, "dir=%s", psz_dir );
527 sprintf( dir, "%s/.access", psz_dir );
528 if( ( file = fopen( dir, "r" ) ) != NULL )
533 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
535 i_size = fread( line, 1, 1023, file );
539 while( i_size > 0 && ( line[i_size-1] == '\n' ||
540 line[i_size-1] == '\r' ) )
547 p = strchr( line, ':' );
551 user = strdup( line );
552 password = strdup( p );
555 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
556 user, password, i_size );
563 /* parse psz_src dir */
564 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
569 if( p_dir_content->d_name[0] == '.' )
573 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
574 if( ParseDirectory( p_intf, psz_root, dir ) )
576 #define f p_sys->pp_files[p_sys->i_files]
577 f = malloc( sizeof( httpd_file_callback_args_t ) );
580 msg_Err( p_intf, "Out of memory" );
581 return( VLC_ENOMEM );
584 f->file = strdup( dir );
585 f->name = FileToUrl( &dir[strlen( psz_root )] );
586 f->mime = FileToMime( &dir[strlen( psz_root )] );
588 if( !f->name || !f->mime )
590 msg_Err( p_intf , "Unable to parse directory" );
591 return( VLC_ENOMEM );
593 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)",
594 f->file, f->name, f->mime );
597 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
605 p_sys->pp_files = realloc( p_sys->pp_files,
606 (p_sys->i_files+1) * sizeof( httpd_file_callback_args_t ) );
608 #define fold p_sys->pp_files[p_sys->i_files-1]
610 /* FIXME for rep/ add rep (it would be better to do a redirection) */
611 if( p_sys->i_files && strlen(fold->name) > 1 &&
612 fold->name[strlen(fold->name) - 1] == '/' )
614 f = malloc( sizeof( httpd_file_callback_args_t ) );
617 msg_Err( p_intf, "Out of memory" );
618 return( VLC_ENOMEM );
621 f->file = strdup( fold->file );
622 f->name = strdup( fold->name );
623 f->mime = fold->mime;
625 f->name[strlen(f->name) - 1] = '\0';
626 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)", f->file, f->name,
629 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
638 realloc( p_sys->pp_files, (p_sys->i_files+1) *
639 sizeof( httpd_file_callback_args_t ) );
658 /****************************************************************************
659 * var and set handling
660 ****************************************************************************/
662 static mvar_t *mvar_New( char *name, char *value )
664 mvar_t *v = malloc( sizeof( mvar_t ) );
666 if( !v ) return NULL;
667 v->name = strdup( name );
668 v->value = strdup( value ? value : "" );
671 v->field = malloc( sizeof( mvar_t * ) );
677 static void mvar_Delete( mvar_t *v )
684 for( i = 0; i < v->i_field; i++ )
686 mvar_Delete( v->field[i] );
692 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
694 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
695 v->field[v->i_field] = f;
699 static mvar_t *mvar_Duplicate( mvar_t *v )
704 n = mvar_New( v->name, v->value );
705 for( i = 0; i < v->i_field; i++ )
707 mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
713 static void mvar_PushVar( mvar_t *v, mvar_t *f )
715 v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
718 memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
724 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
727 for( i = 0; i < v->i_field; i++ )
729 if( v->field[i] == f )
734 if( i >= v->i_field )
739 if( i + 1 < v->i_field )
741 memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
744 /* FIXME should do a realloc */
747 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
750 char base[512], *field, *p;
753 /* format: name[index].field */
755 field = strchr( name, '.' );
758 int i = field - name;
759 strncpy( base, name, i );
765 strcpy( base, name );
768 if( ( p = strchr( base, '[' ) ) )
771 sscanf( p, "%d]", &i_index );
782 for( i = 0; i < s->i_field; i++ )
784 if( !strcmp( s->field[i]->name, base ) )
794 return mvar_GetVar( s->field[i], field );
808 static char *mvar_GetValue( mvar_t *v, char *field )
816 mvar_t *f = mvar_GetVar( v, field );
828 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
830 mvar_t *f = mvar_New( name, value );
831 mvar_PushVar( vars, f );
834 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
836 mvar_t *f = mvar_New( name, value );
837 mvar_AppendVar( vars, f );
841 /* arg= start[:stop[:step]],.. */
842 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
844 char *dup = strdup( arg );
846 mvar_t *s = mvar_New( name, "set" );
848 fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
853 int i_start,i_stop,i_step;
856 p = strchr( str, ',' );
863 i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
864 fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );
871 else if( i_match == 2 )
873 i_step = i_start < i_stop ? 1 : -1;
880 if( ( i_start < i_stop && i_step > 0 ) ||
881 ( i_start > i_stop && i_step < 0 ) )
883 for( i = i_start; ; i += i_step )
887 if( ( i_step > 0 && i > i_stop ) ||
888 ( i_step < 0 && i < i_stop ) )
893 fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
894 sprintf( value, "%d", i );
896 mvar_PushNewVar( s, name, value );
907 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
909 mvar_t *s = mvar_New( name, "set" );
912 fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
914 vlc_mutex_lock( &p_pl->object_lock );
915 for( i = 0; i < p_pl->i_size; i++ )
917 mvar_t *itm = mvar_New( name, "set" );
920 sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
921 mvar_AppendNewVar( itm, "current", value );
923 sprintf( value, "%d", i );
924 mvar_AppendNewVar( itm, "index", value );
926 mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->psz_name );
928 mvar_AppendVar( s, itm );
930 vlc_mutex_unlock( &p_pl->object_lock );
935 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
937 mvar_t *s = mvar_New( name, "set" );
939 input_info_category_t * p_category;
940 input_info_t * p_info;
942 fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
943 if( p_input == NULL )
948 vlc_mutex_lock( &p_input->stream.stream_lock );
949 p_category = p_input->stream.p_info;
952 mvar_t *cat = mvar_New( name, "set" );
953 mvar_t *iset = mvar_New( "info", "set" );
955 mvar_AppendNewVar( cat, "name", p_category->psz_name );
956 mvar_AppendVar( cat, iset );
958 p_info = p_category->p_info;
961 mvar_t *info = mvar_New( "info", "" );
963 msg_Dbg( p_input, "adding info name=%s value=%s", p_info->psz_name, p_info->psz_value );
964 mvar_AppendNewVar( info, "name", p_info->psz_name );
965 mvar_AppendNewVar( info, "value", p_info->psz_value );
966 mvar_AppendVar( iset, info );
967 p_info = p_info->p_next;
969 mvar_AppendVar( s, cat );
970 p_category = p_category->p_next;
972 vlc_mutex_unlock( &p_input->stream.stream_lock );
977 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
979 mvar_t *s = mvar_New( name, "set" );
983 fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
984 if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
986 for( i= 0; i < info.i_count; )
990 inf = mvar_New( name, "set" );
993 /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
994 info.info[i].psz_name, info.info[i].psz_value ); */
995 mvar_AppendNewVar( inf,
996 info.info[i].psz_name,
997 info.info[i].psz_value );
999 } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
1000 mvar_AppendVar( s, inf );
1005 for( i = 0; i < info.i_count; i++ )
1007 free( info.info[i].psz_name );
1008 free( info.info[i].psz_value );
1013 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1015 mvar_t *s = mvar_New( name, "set" );
1016 char tmp[MAX_DIR_SIZE], *p, *src;
1017 #ifdef HAVE_SYS_STAT_H
1018 struct stat stat_info;
1021 struct dirent *p_dir_content;
1024 /* convert all / to native separator */
1025 #if defined( WIN32 )
1026 while( (p = strchr( psz_dir, '/' )) )
1035 /* remove trailling separator */
1036 while( strlen( psz_dir ) > 1 &&
1037 #if defined( WIN32 )
1038 !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1040 psz_dir[strlen( psz_dir ) -1 ] == sep )
1042 psz_dir[strlen( psz_dir ) -1 ] ='\0';
1044 /* remove double separator */
1045 for( p = src = psz_dir; *src != '\0'; src++, p++ )
1047 if( src[0] == sep && src[1] == sep )
1055 if( *psz_dir == '\0' )
1059 /* first fix all .. dir */
1063 if( src[0] == '.' && src[1] == '.' )
1066 if( p <= &psz_dir[1] )
1073 while( p > &psz_dir[1] && *p != sep )
1078 else if( *src == sep )
1080 if( p > psz_dir && p[-1] == sep )
1094 } while( *src && *src != sep );
1099 fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1101 #ifdef HAVE_SYS_STAT_H
1102 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1108 if( ( p_dir = opendir( psz_dir ) ) == NULL )
1110 fprintf( stderr, "cannot open dir (%s)", psz_dir );
1114 /* remove traling / or \ */
1115 for( p = &psz_dir[strlen( psz_dir) - 1]; p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1124 /* parse psz_src dir */
1125 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1129 if( !strcmp( p_dir_content->d_name, "." ) )
1134 #if defined( WIN32 )
1135 sprintf( tmp, "%s\\%s", psz_dir, p_dir_content->d_name );
1137 sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1140 #ifdef HAVE_SYS_STAT_H
1141 if( stat( tmp, &stat_info ) == -1 )
1146 f = mvar_New( name, "set" );
1147 mvar_AppendNewVar( f, "name", tmp );
1148 #ifdef HAVE_SYS_STAT_H
1149 if( S_ISDIR( stat_info.st_mode ) )
1151 mvar_AppendNewVar( f, "type", "directory" );
1153 else if( S_ISREG( stat_info.st_mode ) )
1155 mvar_AppendNewVar( f, "type", "file" );
1159 mvar_AppendNewVar( f, "type", "unknown" );
1162 sprintf( tmp, "%lld", stat_info.st_size );
1163 mvar_AppendNewVar( f, "size", tmp );
1165 /* FIXME memory leak FIXME */
1167 ctime_r( &stat_info.st_mtime, tmp );
1168 mvar_AppendNewVar( f, "date", tmp );
1170 mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1174 mvar_AppendNewVar( f, "type", "unknown" );
1175 mvar_AppendNewVar( f, "size", "unknown" );
1176 mvar_AppendNewVar( f, "date", "unknown" );
1178 mvar_AppendVar( s, f );
1184 static void SSInit( rpn_stack_t * );
1185 static void SSClean( rpn_stack_t * );
1186 static void EvaluateRPN( mvar_t *, rpn_stack_t *, char * );
1188 static void SSPush ( rpn_stack_t *, char * );
1189 static char *SSPop ( rpn_stack_t * );
1191 static void SSPushN ( rpn_stack_t *, int );
1192 static int SSPopN ( rpn_stack_t *, mvar_t * );
1195 /****************************************************************************
1197 ****************************************************************************/
1205 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1209 /* just load the file */
1211 *pp_data = malloc( 1025 ); /* +1 for \0 */
1212 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1215 *pp_data = realloc( *pp_data, *pi_data + 1025 );
1221 (*pp_data)[*pi_data] = '\0';
1226 static int MacroParse( macro_t *m, uint8_t *psz_src )
1228 uint8_t *dup = strdup( psz_src );
1233 #define EXTRACT( name, l ) \
1235 p = strchr( src, '"' ); \
1240 m->name = strdup( src ); \
1261 if( !strncmp( src, "id=\"", 4 ) )
1265 else if( !strncmp( src, "param1=\"", 8 ) )
1267 EXTRACT( param1, 8 );
1269 else if( !strncmp( src, "param2=\"", 8 ) )
1271 EXTRACT( param2, 8 );
1278 if( strstr( src, "/>" ) )
1280 src = strstr( src, "/>" ) + 2;
1284 src += strlen( src );
1289 m->id = strdup( "" );
1291 if( m->param1 == NULL )
1293 m->param1 = strdup( "" );
1295 if( m->param2 == NULL )
1297 m->param2 = strdup( "" );
1306 static void MacroClean( macro_t *m )
1347 StrToMacroTypeTab [] =
1349 { "control", MVLC_CONTROL },
1350 /* player control */
1351 { "play", MVLC_PLAY },
1352 { "stop", MVLC_STOP },
1353 { "pause", MVLC_PAUSE },
1354 { "next", MVLC_NEXT },
1355 { "previous", MVLC_PREVIOUS },
1357 /* playlist management */
1358 { "add", MVLC_ADD },
1359 { "del", MVLC_DEL },
1360 { "empty", MVLC_EMPTY },
1363 { "close", MVLC_CLOSE },
1364 { "shutdown", MVLC_SHUTDOWN },
1366 { "rpn", MVLC_RPN },
1368 { "foreach", MVLC_FOREACH },
1369 { "value", MVLC_VALUE },
1372 { "else", MVLC_ELSE },
1373 { "end", MVLC_END },
1374 { "get", MVLC_GET },
1375 { "set", MVLC_SET },
1376 { "int", MVLC_INT },
1377 { "float", MVLC_FLOAT },
1378 { "string", MVLC_STRING },
1381 { NULL, MVLC_UNKNOWN }
1384 static int StrToMacroType( char *name )
1388 if( !name || *name == '\0')
1390 return MVLC_UNKNOWN;
1392 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1394 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1396 return StrToMacroTypeTab[i].i_type;
1399 return MVLC_UNKNOWN;
1402 static void MacroDo( httpd_file_callback_args_t *p_args,
1404 uint8_t *p_request, int i_request,
1405 uint8_t **pp_data, int *pi_data,
1408 intf_thread_t *p_intf = p_args->p_intf;
1409 intf_sys_t *p_sys = p_args->p_intf->p_sys;
1412 #define ALLOC( l ) \
1414 int __i__ = *pp_dst - *pp_data; \
1416 *pp_data = realloc( *pp_data, *pi_data ); \
1417 *pp_dst = (*pp_data) + __i__; \
1419 #define PRINT( str ) \
1420 ALLOC( strlen( str ) + 1 ); \
1421 *pp_dst += sprintf( *pp_dst, str );
1423 #define PRINTS( str, s ) \
1424 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1425 char * psz_cur = *pp_dst; \
1426 *pp_dst += sprintf( *pp_dst, str, s ); \
1427 while( psz_cur && *psz_cur ) \
1429 /* Prevent script injection */ \
1430 if( *psz_cur == '<' ) *psz_cur = '*'; \
1431 if( *psz_cur == '>' ) *psz_cur = '*'; \
1435 switch( StrToMacroType( m->id ) )
1438 if( i_request <= 0 )
1442 uri_extract_value( p_request, "control", control, 512 );
1443 if( *m->param1 && !strstr( m->param1, control ) )
1445 msg_Warn( p_intf, "unauthorized control=%s", control );
1448 switch( StrToMacroType( control ) )
1455 uri_extract_value( p_request, "item", item, 512 );
1456 i_item = atoi( item );
1457 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
1458 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1462 playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1463 msg_Dbg( p_intf, "requested playlist stop" );
1466 playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1467 msg_Dbg( p_intf, "requested playlist pause" );
1470 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1471 p_sys->p_playlist->i_index + 1 );
1472 msg_Dbg( p_intf, "requested playlist next" );
1475 playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1476 p_sys->p_playlist->i_index - 1 );
1477 msg_Dbg( p_intf, "requested playlist next" );
1480 /* playlist management */
1484 uri_extract_value( p_request, "mrl", mrl, 512 );
1485 uri_decode_url_encoded( mrl );
1486 playlist_Add( p_sys->p_playlist, mrl, NULL, 0,
1487 PLAYLIST_APPEND, PLAYLIST_END );
1488 msg_Dbg( p_intf, "requested playlist add: %s", mrl );
1496 uri_extract_value( p_request, "item", item, 512 );
1497 i_item = atoi( item );
1499 playlist_Delete( p_sys->p_playlist, i_item );
1500 msg_Dbg( p_intf, "requested playlist del: %d", i_item );
1505 while( p_sys->p_playlist->i_size > 0 )
1507 playlist_Delete( p_sys->p_playlist, 0 );
1509 msg_Dbg( p_intf, "requested playlist empty" );
1513 /* admin function */
1517 uri_extract_value( p_request, "id", id, 512 );
1518 msg_Dbg( p_intf, "requested close id=%s", id );
1519 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
1521 msg_Warn( p_intf, "close failed for id=%s", id );
1527 msg_Dbg( p_intf, "requested shutdown" );
1528 p_intf->p_vlc->b_die = VLC_TRUE;
1532 PRINTS( "<!-- control param(%s) unsuported -->", control );
1543 if( i_request <= 0 ||
1544 *m->param1 == '\0' ||
1545 strstr( p_request, m->param1 ) == NULL )
1549 uri_extract_value( p_request, m->param1, value, 512 );
1550 uri_decode_url_encoded( value );
1552 switch( StrToMacroType( m->param2 ) )
1556 config_PutInt( p_intf, m->param1, i );
1560 config_PutFloat( p_intf, m->param1, f );
1563 config_PutPsz( p_intf, m->param1, value );
1566 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
1577 if( *m->param1 == '\0' )
1582 switch( StrToMacroType( m->param2 ) )
1585 i = config_GetInt( p_intf, m->param1 );
1586 sprintf( value, "%i", i );
1589 f = config_GetFloat( p_intf, m->param1 );
1590 sprintf( value, "%f", f );
1593 psz = config_GetPsz( p_intf, m->param1 );
1594 sprintf( value, "%s", psz ? psz : "" );
1597 sprintf( value, "invalid type(%s) in set", m->param2 );
1600 msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
1601 PRINTS( "%s", value );
1610 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1611 s = SSPop( &p_args->stack );
1612 v = mvar_GetValue( p_args->vars, s );
1616 v = s = SSPop( &p_args->stack );
1624 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1628 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
1629 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
1637 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
1644 if( src + 4 < end && !strncmp( src, "<vlc", 4 ) )
1649 i_skip = MacroParse( &m, src );
1651 i_id = StrToMacroType( m.id );
1666 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
1667 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
1669 return src + ( b_after ? i_skip : 0 );
1671 else if( i_level < 0 )
1687 static void Execute( httpd_file_callback_args_t *p_args,
1688 uint8_t *p_request, int i_request,
1689 uint8_t **pp_data, int *pi_data,
1691 uint8_t *_src, uint8_t *_end )
1693 intf_thread_t *p_intf = p_args->p_intf;
1695 uint8_t *src, *dup, *end;
1696 uint8_t *dst = *pp_dst;
1698 src = dup = malloc( _end - _src + 1 );
1699 end = src +( _end - _src );
1701 memcpy( src, _src, _end - _src );
1704 /* we parse searching <vlc */
1710 p = strstr( src, "<vlc" );
1711 if( p < end && p == src )
1715 src += MacroParse( &m, src );
1717 //msg_Dbg( p_intf, "macro_id=%s", m.id );
1719 switch( StrToMacroType( m.id ) )
1726 EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
1727 if( SSPopN( &p_args->stack, p_args->vars ) )
1735 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
1739 uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
1743 uint8_t *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
1746 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
1750 else if( i_test == 1 )
1753 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
1755 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
1759 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
1768 uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
1769 uint8_t *start = src;
1770 uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
1777 if( !strcmp( m.param2, "integer" ) )
1779 char *arg = SSPop( &p_args->stack );
1780 index = mvar_IntegerSetNew( m.param1, arg );
1783 else if( !strcmp( m.param2, "directory" ) )
1785 char *arg = SSPop( &p_args->stack );
1786 index = mvar_FileSetNew( m.param1, arg );
1789 else if( !strcmp( m.param2, "playlist" ) )
1791 index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
1793 else if( !strcmp( m.param2, "informations" ) )
1795 index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
1797 else if( !strcmp( m.param2, "hosts" ) )
1799 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
1801 else if( !strcmp( m.param2, "urls" ) )
1803 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
1805 else if( !strcmp( m.param2, "connections" ) )
1807 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
1809 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
1811 index = mvar_Duplicate( v );
1815 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
1820 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
1822 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
1824 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
1827 f->name = strdup( m.param1 );
1830 mvar_PushVar( p_args->vars, f );
1831 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
1832 mvar_RemoveVar( p_args->vars, f );
1836 mvar_Delete( index );
1843 MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
1851 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
1854 int i_index = dst - *pp_data;
1857 *pp_data = realloc( *pp_data, *pi_data );
1858 dst = (*pp_data) + i_index;
1860 memcpy( dst, src, i_copy );
1870 /****************************************************************************
1872 ****************************************************************************
1873 * a file with mime == text/html is parsed and all "macro" replaced
1874 * <vlc id="macro name" [param1="" [param2=""]] />
1877 ****************************************************************************/
1878 static int http_get( httpd_file_callback_args_t *p_args,
1879 uint8_t *p_request, int i_request,
1880 uint8_t **pp_data, int *pi_data )
1885 if( ( f = fopen( p_args->file, "r" ) ) == NULL )
1887 p = *pp_data = malloc( 10240 );
1890 return VLC_EGENERIC;
1892 p += sprintf( p, "<html>\n" );
1893 p += sprintf( p, "<head>\n" );
1894 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
1895 p += sprintf( p, "</head>\n" );
1896 p += sprintf( p, "<body>\n" );
1897 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
1898 p += sprintf( p, "<hr />\n" );
1899 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1900 p += sprintf( p, "</body>\n" );
1901 p += sprintf( p, "</html>\n" );
1903 *pi_data = strlen( *pp_data ) + 1;
1908 if( strcmp( p_args->mime, "text/html" ) )
1910 FileLoad( f, pp_data, pi_data );
1918 p_args->vars = mvar_New( "variables", "" );
1919 mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
1920 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
1921 mvar_AppendNewVar( p_args->vars, "version", VERSION_MESSAGE );
1922 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
1924 SSInit( &p_args->stack );
1926 /* first we load in a temporary buffer */
1927 FileLoad( f, &p_buffer, &i_buffer );
1929 /* allocate output */
1930 *pi_data = i_buffer + 1000;
1931 dst = *pp_data = malloc( *pi_data );
1933 /* we parse executing all <vlc /> macros */
1934 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
1937 *pi_data = dst - *pp_data;
1939 SSClean( &p_args->stack );
1940 mvar_Delete( p_args->vars );
1948 /****************************************************************************
1950 ****************************************************************************/
1951 static void uri_extract_value( char *psz_uri, char *psz_name,
1952 char *psz_value, int i_value_max )
1956 p = strstr( psz_uri, psz_name );
1961 p += strlen( psz_name );
1962 if( *p == '=' ) p++;
1964 if( strchr( p, '&' ) )
1966 i_len = strchr( p, '&' ) - p;
1970 /* for POST method */
1971 if( strchr( p, '\n' ) )
1973 i_len = strchr( p, '\n' ) - p;
1974 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
1978 i_len = strlen( p );
1981 i_len = __MIN( i_value_max - 1, i_len );
1984 strncpy( psz_value, p, i_len );
1985 psz_value[i_len] = '\0';
1989 strncpy( psz_value, "", i_value_max );
1994 strncpy( psz_value, "", i_value_max );
1998 static void uri_decode_url_encoded( char *psz )
2000 char *dup = strdup( psz );
2018 *psz++ = strtol( val, NULL, 16 );
2020 else if( *p == '+' )
2034 /****************************************************************************
2035 * Light RPN evaluator
2036 ****************************************************************************/
2037 static void SSInit( rpn_stack_t *st )
2042 static void SSClean( rpn_stack_t *st )
2044 while( st->i_stack > 0 )
2046 free( st->stack[--st->i_stack] );
2050 static void SSPush( rpn_stack_t *st, char *s )
2052 if( st->i_stack < STACK_MAX )
2054 st->stack[st->i_stack++] = strdup( s );
2058 static char * SSPop( rpn_stack_t *st )
2060 if( st->i_stack <= 0 )
2062 return strdup( "" );
2066 return st->stack[--st->i_stack];
2070 static int SSPopN( rpn_stack_t *st, mvar_t *vars )
2079 i = strtol( name, &end, 0 );
2082 value = mvar_GetValue( vars, name );
2090 static void SSPushN( rpn_stack_t *st, int i )
2094 sprintf( v, "%d", i );
2098 static void EvaluateRPN( mvar_t *vars, rpn_stack_t *st, char *exp )
2105 while( *exp == ' ' )
2112 /* extract string */
2115 while( *exp && *exp != '\'' )
2126 p = strchr( exp, ' ' );
2131 exp += strlen( exp );
2136 strncpy( s, exp, i );
2147 /* 1. Integer function */
2148 if( !strcmp( s, "!" ) )
2150 SSPushN( st, ~SSPopN( st, vars ) );
2152 else if( !strcmp( s, "^" ) )
2154 SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2156 else if( !strcmp( s, "&" ) )
2158 SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2160 else if( !strcmp( s, "|" ) )
2162 SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2164 else if( !strcmp( s, "+" ) )
2166 SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2168 else if( !strcmp( s, "-" ) )
2170 int i = SSPopN( st, vars );
2171 int j = SSPopN( st, vars );
2172 SSPushN( st, i - j );
2174 else if( !strcmp( s, "*" ) )
2176 SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2178 else if( !strcmp( s, "/" ) )
2182 i = SSPopN( st, vars );
2183 j = SSPopN( st, vars );
2185 SSPushN( st, j != 0 ? i / j : 0 );
2187 else if( !strcmp( s, "%" ) )
2191 i = SSPopN( st, vars );
2192 j = SSPopN( st, vars );
2194 SSPushN( st, j != 0 ? i % j : 0 );
2196 /* 2. integer tests */
2197 else if( !strcmp( s, "=" ) )
2199 SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2201 else if( !strcmp( s, "<" ) )
2203 int i = SSPopN( st, vars );
2204 int j = SSPopN( st, vars );
2206 SSPushN( st, i < j ? -1 : 0 );
2208 else if( !strcmp( s, ">" ) )
2210 int i = SSPopN( st, vars );
2211 int j = SSPopN( st, vars );
2213 SSPushN( st, i > j ? -1 : 0 );
2215 else if( !strcmp( s, "<=" ) )
2217 int i = SSPopN( st, vars );
2218 int j = SSPopN( st, vars );
2220 SSPushN( st, i <= j ? -1 : 0 );
2222 else if( !strcmp( s, ">=" ) )
2224 int i = SSPopN( st, vars );
2225 int j = SSPopN( st, vars );
2227 SSPushN( st, i >= j ? -1 : 0 );
2229 /* 3. string functions */
2230 else if( !strcmp( s, "strcat" ) )
2232 char *s1 = SSPop( st );
2233 char *s2 = SSPop( st );
2234 char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2244 else if( !strcmp( s, "strcmp" ) )
2246 char *s1 = SSPop( st );
2247 char *s2 = SSPop( st );
2249 SSPushN( st, strcmp( s1, s2 ) );
2253 else if( !strcmp( s, "strlen" ) )
2255 char *str = SSPop( st );
2257 SSPushN( st, strlen( str ) );
2260 /* 4. stack functions */
2261 else if( !strcmp( s, "dup" ) )
2263 char *str = SSPop( st );
2268 else if( !strcmp( s, "drop" ) )
2270 char *str = SSPop( st );
2273 else if( !strcmp( s, "swap" ) )
2275 char *s1 = SSPop( st );
2276 char *s2 = SSPop( st );
2283 else if( !strcmp( s, "flush" ) )
2288 else if( !strcmp( s, "store" ) )
2290 char *name = SSPop( st );
2291 char *value = SSPop( st );
2293 mvar_PushNewVar( vars, name, value );
2297 else if( !strcmp( s, "value" ) )
2299 char *name = SSPop( st );
2300 char *value = mvar_GetValue( vars, name );
2302 SSPush( st, value );
2306 else if( !strcmp( s, "url_extract" ) )
2308 char *url = mvar_GetValue( vars, "url_value" );
2309 char *name = SSPop( st );
2312 uri_extract_value( url, name, value, 512 );
2313 uri_decode_url_encoded( value );
2314 SSPush( st, value );