]> git.sesse.net Git - vlc/blob - modules/control/http.c
* A few sanity checks
[vlc] / modules / control / http.c
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 $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 /*
29  * TODO:
30  *
31  * - clean up ?
32  * - doc ! (mouarf ;)
33  *
34  */
35
36 #include <stdlib.h>
37 #include <vlc/vlc.h>
38 #include <vlc/intf.h>
39
40 #include "httpd.h"
41
42 #ifdef HAVE_SYS_STAT_H
43 #   include <sys/stat.h>
44 #endif
45 #ifdef HAVE_ERRNO_H
46 #   include <errno.h>
47 #endif
48 #ifdef HAVE_FCNTL_H
49 #   include <fcntl.h>
50 #endif
51
52 #ifdef HAVE_UNISTD_H
53 #   include <unistd.h>
54 #elif defined( WIN32 ) && !defined( UNDER_CE )
55 #   include <io.h>
56 #endif
57
58 #if (!defined( WIN32 ) || defined(__MINGW32__))
59 /* Mingw has its own version of dirent */
60 #   include <dirent.h>
61 #endif
62
63 /*****************************************************************************
64  * Module descriptor
65  *****************************************************************************/
66 static int  Activate     ( vlc_object_t * );
67 static void Close        ( vlc_object_t * );
68
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" )
74
75 vlc_module_begin();
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 );
82 vlc_module_end();
83
84
85 /*****************************************************************************
86  * Local prototypes
87  *****************************************************************************/
88 static void Run          ( intf_thread_t *p_intf );
89
90 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
91                            char *psz_dir );
92
93 static int DirectoryCheck( char *psz_dir )
94 {
95     DIR           *p_dir;
96
97 #ifdef HAVE_SYS_STAT_H
98     struct stat   stat_info;
99
100     if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
101     {
102         return VLC_EGENERIC;
103     }
104 #endif
105
106     if( ( p_dir = opendir( psz_dir ) ) == NULL )
107     {
108         return VLC_EGENERIC;
109     }
110     closedir( p_dir );
111
112     return VLC_SUCCESS;
113 }
114
115
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 );
119
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 );
123
124 /*****************************************************************************
125  *
126  *****************************************************************************/
127 typedef struct mvar_s
128 {
129     char *name;
130     char *value;
131
132     int           i_field;
133     struct mvar_s **field;
134 } mvar_t;
135
136 #define STACK_MAX 100
137 typedef struct
138 {
139     char *stack[STACK_MAX];
140     int  i_stack;
141 } rpn_stack_t;
142
143 struct httpd_file_callback_args_t
144 {
145     intf_thread_t *p_intf;
146     httpd_file_t  *p_file;
147
148     char          *file;
149     char          *name;
150     char          *mime;
151
152     /* inited for each access */
153     rpn_stack_t       stack;
154     mvar_t        *vars;
155 };
156
157 struct intf_sys_t
158 {
159     httpd_t             *p_httpd;
160     httpd_host_t        *p_httpd_host;
161
162     int                         i_files;
163     httpd_file_callback_args_t  **pp_files;
164
165     playlist_t          *p_playlist;
166     input_thread_t      *p_input;
167 };
168
169
170
171 /*****************************************************************************
172  * Activate: initialize and create stuff
173  *****************************************************************************/
174 static int Activate( vlc_object_t *p_this )
175 {
176     intf_thread_t *p_intf = (intf_thread_t*)p_this;
177     intf_sys_t    *p_sys;
178     char          *psz_host;
179     char          *psz_address = "";
180     int           i_port       = 0;
181     char          *psz_src;
182
183     psz_host = config_GetPsz( p_intf, "http-host" );
184     if( psz_host )
185     {
186         char *psz_parser;
187         psz_address = psz_host;
188
189         psz_parser = strchr( psz_host, ':' );
190         if( psz_parser )
191         {
192             *psz_parser++ = '\0';
193             i_port = atoi( psz_parser );
194         }
195     }
196     if( i_port <= 0 )
197     {
198         i_port= 8080;
199     }
200
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     if( !p_intf->p_sys )
204     {
205         return( VLC_ENOMEM );
206     }
207     p_sys->p_playlist = NULL;
208     p_sys->p_input    = NULL;
209
210     if( ( p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_intf), VLC_TRUE ) ) == NULL )
211     {
212         msg_Err( p_intf, "cannot create/find httpd" );
213         free( p_sys );
214         return VLC_EGENERIC;
215     }
216
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 )
220     {
221         msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
222         httpd_Release( p_sys->p_httpd );
223         free( p_sys );
224         return VLC_EGENERIC;
225     }
226
227     if( psz_host )
228     {
229         free( psz_host );
230     }
231
232     p_sys->i_files = 0;
233     p_sys->pp_files = malloc( sizeof( httpd_file_callback_args_t *) );
234     if( !p_sys->pp_files )
235     {
236         return( VLC_ENOMEM );
237     }
238
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 )
242     {
243         char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
244         psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
245         if( !psz_src )
246         {
247             return( VLC_ENOMEM );
248         }
249 #if defined(WIN32)
250         sprintf( psz_src, "%s/http", psz_vlcpath);
251 #else
252         sprintf( psz_src, "%s/share/http", psz_vlcpath);
253 #endif
254     }
255 #else
256     psz_src = config_GetPsz( p_intf, "http-src" );
257
258     if( !psz_src || *psz_src == '\0' )
259     {
260         if( !DirectoryCheck( "share/http" ) )
261         {
262             psz_src = strdup( "share/http" );
263         }
264         else if( !DirectoryCheck( DATA_PATH "/http" ) )
265         {
266             psz_src = strdup( DATA_PATH "/http" );
267         }
268     }
269 #endif
270
271     if( !psz_src || *psz_src == '\0' )
272     {
273         msg_Err( p_intf, "invalid src dir" );
274         goto failed;
275     }
276
277     /* remove trainling \ or / */
278     if( psz_src[strlen( psz_src ) - 1] == '\\' ||
279         psz_src[strlen( psz_src ) - 1] == '/' )
280     {
281         psz_src[strlen( psz_src ) - 1] = '\0';
282     }
283
284     ParseDirectory( p_intf, psz_src, psz_src );
285
286
287     if( p_sys->i_files <= 0 )
288     {
289         msg_Err( p_intf, "cannot find any files (%s)", psz_src );
290         goto failed;
291     }
292     p_intf->pf_run = Run;
293
294     return VLC_SUCCESS;
295
296 failed:
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 );
301     free( p_sys );
302     return VLC_EGENERIC;
303 }
304
305 /*****************************************************************************
306  * CloseIntf: destroy interface
307  *****************************************************************************/
308 void Close ( vlc_object_t *p_this )
309 {
310     intf_thread_t *p_intf = (intf_thread_t *)p_this;
311     intf_sys_t    *p_sys = p_intf->p_sys;
312
313     int i;
314
315     for( i = 0; i < p_sys->i_files; i++ )
316     {
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] );
323     }
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 );
328     free( p_sys );
329 }
330
331 /*****************************************************************************
332  * Run: http interface thread
333  *****************************************************************************/
334 static void Run( intf_thread_t *p_intf )
335 {
336     intf_sys_t     *p_sys = p_intf->p_sys;
337
338     while( !p_intf->b_die )
339     {
340         /* get the playlist */
341         if( p_sys->p_playlist == NULL )
342         {
343             p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
344         }
345
346         /* Manage the input part */
347         if( p_sys->p_input == NULL )
348         {
349             if( p_sys->p_playlist )
350             {
351                 p_sys->p_input =
352                     vlc_object_find( p_sys->p_playlist,
353                                      VLC_OBJECT_INPUT,
354                                      FIND_CHILD );
355             }
356         }
357         else if( p_sys->p_input->b_dead )
358         {
359             vlc_object_release( p_sys->p_input );
360             p_sys->p_input = NULL;
361         }
362
363
364         /* Wait a bit */
365         msleep( INTF_IDLE_SLEEP );
366     }
367
368     if( p_sys->p_input )
369     {
370         vlc_object_release( p_sys->p_input );
371         p_sys->p_input = NULL;
372     }
373
374     if( p_sys->p_playlist )
375     {
376         vlc_object_release( p_sys->p_playlist );
377         p_sys->p_playlist = NULL;
378     }
379 }
380
381
382 /*****************************************************************************
383  * Local functions
384  *****************************************************************************/
385 #define MAX_DIR_SIZE 10240
386
387 /****************************************************************************
388  * FileToUrl: create a good name for an url from filename
389  ****************************************************************************/
390 static char *FileToUrl( char *name )
391 {
392     char *url, *p;
393
394     url = p = malloc( strlen( name ) + 1 );
395
396     if( !url || !p )
397     {
398         return NULL;
399     }
400
401 #ifdef WIN32
402     while( *name == '\\' || *name == '/' )
403 #else
404     while( *name == '/' )
405 #endif
406     {
407         name++;
408     }
409
410     *p++ = '/';
411     strcpy( p, name );
412
413 #ifdef WIN32
414     /* convert '\\' into '/' */
415     name = p;
416     while( *name )
417     {
418         if( *name == '\\' )
419         {
420             *p++ = '/';
421         }
422         name++;
423     }
424 #endif
425
426     /* index.* -> / */
427     if( ( p = strrchr( url, '/' ) ) != NULL )
428     {
429         if( !strncmp( p, "/index.", 7 ) )
430         {
431             p[1] = '\0';
432         }
433     }
434     return url;
435 }
436
437 /****************************************************************************
438  * FileToMime: XXX duplicated with modules/access_out/http.c
439  ****************************************************************************/
440 static struct
441 {
442     char *psz_ext;
443     char *psz_mime;
444 } http_mime[] =
445 {
446     { ".htm",   "text/html" },
447     { ".html",  "text/html" },
448
449     { ".css",   "text/css" },
450
451     /* media mime */
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" },
469
470     /* end */
471     { NULL,     NULL }
472 };
473
474 static char *FileToMime( char *psz_name )
475 {
476     char *psz_ext;
477
478     psz_ext = strrchr( psz_name, '.' );
479     if( psz_ext )
480     {
481         int i;
482
483         for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
484         {
485             if( !strcmp( http_mime[i].psz_ext, psz_ext ) )
486             {
487                 return( http_mime[i].psz_mime );
488             }
489         }
490     }
491     return( "application/octet-stream" );
492 }
493
494 /****************************************************************************
495  * ParseDirectory: parse recursively a directory, adding each file
496  ****************************************************************************/
497 static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
498                            char *psz_dir )
499 {
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;
504 #endif
505     DIR           *p_dir;
506     struct dirent *p_dir_content;
507     FILE          *file;
508
509     char          *user = NULL;
510     char          *password = NULL;
511
512 #ifdef HAVE_SYS_STAT_H
513     if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
514     {
515         return VLC_EGENERIC;
516     }
517 #endif
518
519     if( ( p_dir = opendir( psz_dir ) ) == NULL )
520     {
521         msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
522         return VLC_EGENERIC;
523     }
524
525     msg_Dbg( p_intf, "dir=%s", psz_dir );
526
527     sprintf( dir, "%s/.access", psz_dir );
528     if( ( file = fopen( dir, "r" ) ) != NULL )
529     {
530         char line[1024];
531         int  i_size;
532
533         msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
534
535         i_size = fread( line, 1, 1023, file );
536         if( i_size > 0 )
537         {
538             char *p;
539             while( i_size > 0 && ( line[i_size-1] == '\n' ||
540                    line[i_size-1] == '\r' ) )
541             {
542                 i_size--;
543             }
544
545             line[i_size] = '\0';
546
547             p = strchr( line, ':' );
548             if( p )
549             {
550                 *p++ = '\0';
551                 user = strdup( line );
552                 password = strdup( p );
553             }
554         }
555         msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
556                  user, password, i_size );
557
558         fclose( file );
559     }
560
561     for( ;; )
562     {
563         /* parse psz_src dir */
564         if( ( p_dir_content = readdir( p_dir ) ) == NULL )
565         {
566             break;
567         }
568
569         if( p_dir_content->d_name[0] == '.' )
570         {
571             continue;
572         }
573         sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
574         if( ParseDirectory( p_intf, psz_root, dir ) )
575         {
576 #define f p_sys->pp_files[p_sys->i_files]
577             f = malloc( sizeof( httpd_file_callback_args_t ) );
578             if( !f )
579             {
580                 msg_Err( p_intf, "Out of memory" );
581                 return( VLC_ENOMEM );
582             }
583             f->p_intf  = p_intf;
584             f->file = strdup( dir );
585             f->name = FileToUrl( &dir[strlen( psz_root )] );
586             f->mime = FileToMime( &dir[strlen( psz_root )] );
587
588             if( !f->name || !f->mime )
589             {
590                 msg_Err( p_intf , "Unable to parse directory" );
591                 return( VLC_ENOMEM );
592             }
593             msg_Dbg( p_intf, "file=%s (url=%s mime=%s)",
594                      f->file, f->name, f->mime );
595
596             f->p_file =
597                 p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
598                                                   f->name, f->mime,
599                                                   user, password,
600                                                   http_get, http_get,
601                                                   f );
602             if( f->p_file )
603             {
604                 p_sys->i_files++;
605                 p_sys->pp_files = realloc( p_sys->pp_files,
606                   (p_sys->i_files+1) * sizeof( httpd_file_callback_args_t ) );
607             }
608 #define fold p_sys->pp_files[p_sys->i_files-1]
609
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] == '/' )
613             {
614                 f = malloc( sizeof( httpd_file_callback_args_t ) );
615                 if( !f )
616                 {
617                     msg_Err( p_intf, "Out of memory" );
618                     return( VLC_ENOMEM );
619                 }
620                 f->p_intf  = p_intf;
621                 f->file = strdup( fold->file );
622                 f->name = strdup( fold->name );
623                 f->mime = fold->mime;
624
625                 f->name[strlen(f->name) - 1] = '\0';
626                 msg_Dbg( p_intf, "file=%s (url=%s mime=%s)", f->file, f->name,
627                          f->mime );
628                 f->p_file =
629                     p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
630                                                       f->name, f->mime,
631                                                       user, password,
632                                                       http_get, http_get,
633                                                       f );
634                 if( f->p_file )
635                 {
636                     p_sys->i_files++;
637                     p_sys->pp_files =
638                         realloc( p_sys->pp_files, (p_sys->i_files+1) *
639                                  sizeof( httpd_file_callback_args_t ) );
640                 }
641             }
642 #undef fold
643 #undef f
644         }
645     }
646
647     if( user )
648     {
649         free( user );
650     }
651     if( password )
652     {
653         free( password );
654     }
655     return VLC_SUCCESS;
656 }
657
658 /****************************************************************************
659  * var and set handling
660  ****************************************************************************/
661
662 static mvar_t *mvar_New( char *name, char *value )
663 {
664     mvar_t *v = malloc( sizeof( mvar_t ) );
665
666     if( !v ) return NULL;
667     v->name = strdup( name );
668     v->value = strdup( value ? value : "" );
669
670     v->i_field = 0;
671     v->field = malloc( sizeof( mvar_t * ) );
672     v->field[0] = NULL;
673
674     return v;
675 }
676
677 static void mvar_Delete( mvar_t *v )
678 {
679     int i;
680
681     free( v->name );
682     free( v->value );
683
684     for( i = 0; i < v->i_field; i++ )
685     {
686         mvar_Delete( v->field[i] );
687     }
688     free( v->field );
689     free( v );
690 }
691
692 static void mvar_AppendVar( mvar_t *v, mvar_t *f )
693 {
694     v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
695     v->field[v->i_field] = f;
696     v->i_field++;
697 }
698
699 static mvar_t *mvar_Duplicate( mvar_t *v )
700 {
701     int i;
702     mvar_t *n;
703
704     n = mvar_New( v->name, v->value );
705     for( i = 0; i < v->i_field; i++ )
706     {
707         mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
708     }
709
710     return n;
711 }
712
713 static void mvar_PushVar( mvar_t *v, mvar_t *f )
714 {
715     v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
716     if( v->i_field > 0 )
717     {
718         memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
719     }
720     v->field[0] = f;
721     v->i_field++;
722 }
723
724 static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
725 {
726     int i;
727     for( i = 0; i < v->i_field; i++ )
728     {
729         if( v->field[i] == f )
730         {
731             break;
732         }
733     }
734     if( i >= v->i_field )
735     {
736         return;
737     }
738
739     if( i + 1 < v->i_field )
740     {
741         memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
742     }
743     v->i_field--;
744     /* FIXME should do a realloc */
745 }
746
747 static mvar_t *mvar_GetVar( mvar_t *s, char *name )
748 {
749     int i;
750     char base[512], *field, *p;
751     int  i_index;
752
753     /* format: name[index].field */
754
755     field = strchr( name, '.' );
756     if( field )
757     {
758         int i = field - name;
759         strncpy( base, name, i );
760         base[i] = '\0';
761         field++;
762     }
763     else
764     {
765         strcpy( base, name );
766     }
767
768     if( ( p = strchr( base, '[' ) ) )
769     {
770         *p++ = '\0';
771         sscanf( p, "%d]", &i_index );
772         if( i_index < 0 )
773         {
774             return NULL;
775         }
776     }
777     else
778     {
779         i_index = 0;
780     }
781
782     for( i = 0; i < s->i_field; i++ )
783     {
784         if( !strcmp( s->field[i]->name, base ) )
785         {
786             if( i_index > 0 )
787             {
788                 i_index--;
789             }
790             else
791             {
792                 if( field )
793                 {
794                     return mvar_GetVar( s->field[i], field );
795                 }
796                 else
797                 {
798                     return s->field[i];
799                 }
800             }
801         }
802     }
803     return NULL;
804 }
805
806
807
808 static char *mvar_GetValue( mvar_t *v, char *field )
809 {
810     if( *field == '\0' )
811     {
812         return v->value;
813     }
814     else
815     {
816         mvar_t *f = mvar_GetVar( v, field );
817         if( f )
818         {
819             return f->value;
820         }
821         else
822         {
823             return field;
824         }
825     }
826 }
827
828 static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
829 {
830     mvar_t *f = mvar_New( name, value );
831     mvar_PushVar( vars, f );
832 }
833
834 static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
835 {
836     mvar_t *f = mvar_New( name, value );
837     mvar_AppendVar( vars, f );
838 }
839
840
841 /* arg= start[:stop[:step]],.. */
842 static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
843 {
844     char *dup = strdup( arg );
845     char *str = dup;
846     mvar_t *s = mvar_New( name, "set" );
847
848     fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );
849
850     while( str )
851     {
852         char *p;
853         int  i_start,i_stop,i_step;
854         int  i_match;
855
856         p = strchr( str, ',' );
857         if( p )
858         {
859             *p++ = '\0';
860         }
861
862         i_step = 0;
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 );
865
866         if( i_match == 1 )
867         {
868             i_stop = i_start;
869             i_step = 1;
870         }
871         else if( i_match == 2 )
872         {
873             i_step = i_start < i_stop ? 1 : -1;
874         }
875
876         if( i_match >= 1 )
877         {
878             int i;
879
880             if( ( i_start < i_stop && i_step > 0 ) ||
881                 ( i_start > i_stop && i_step < 0 ) )
882             {
883                 for( i = i_start; ; i += i_step )
884                 {
885                     char   value[512];
886
887                     if( ( i_step > 0 && i > i_stop ) ||
888                         ( i_step < 0 && i < i_stop ) )
889                     {
890                         break;
891                     }
892
893                     fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
894                     sprintf( value, "%d", i );
895
896                     mvar_PushNewVar( s, name, value );
897                 }
898             }
899         }
900         str = p;
901     }
902
903     free( dup );
904     return s;
905 }
906
907 static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
908 {
909     mvar_t *s = mvar_New( name, "set" );
910     int    i;
911
912     fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );
913
914     vlc_mutex_lock( &p_pl->object_lock );
915     for( i = 0; i < p_pl->i_size; i++ )
916     {
917         mvar_t *itm = mvar_New( name, "set" );
918         char   value[512];
919
920         sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
921         mvar_AppendNewVar( itm, "current", value );
922
923         sprintf( value, "%d", i );
924         mvar_AppendNewVar( itm, "index", value );
925
926         mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->psz_name );
927
928         mvar_AppendVar( s, itm );
929     }
930     vlc_mutex_unlock( &p_pl->object_lock );
931
932     return s;
933 }
934
935 static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
936 {
937     mvar_t *s = mvar_New( name, "set" );
938
939     input_info_category_t * p_category;
940     input_info_t * p_info;
941
942     fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
943     if( p_input == NULL )
944     {
945         return s;
946     }
947
948     vlc_mutex_lock( &p_input->stream.stream_lock );
949     p_category = p_input->stream.p_info;
950     while ( p_category )
951     {
952         mvar_t *cat  = mvar_New( name, "set" );
953         mvar_t *iset = mvar_New( "info", "set" );
954
955         mvar_AppendNewVar( cat, "name", p_category->psz_name );
956         mvar_AppendVar( cat, iset );
957
958         p_info = p_category->p_info;
959         while ( p_info )
960         {
961             mvar_t *info = mvar_New( "info", "" );
962
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;
968         }
969         mvar_AppendVar( s, cat );
970         p_category = p_category->p_next;
971     }
972     vlc_mutex_unlock( &p_input->stream.stream_lock );
973
974     return s;
975 }
976
977 static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
978 {
979     mvar_t       *s = mvar_New( name, "set" );
980     httpd_info_t info;
981     int          i;
982
983     fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
984     if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
985     {
986         for( i= 0; i < info.i_count; )
987         {
988             mvar_t *inf;
989
990             inf = mvar_New( name, "set" );
991             do
992             {
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 );
998                 i++;
999             } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
1000             mvar_AppendVar( s, inf );
1001         }
1002     }
1003
1004     /* free mem */
1005     for( i = 0; i < info.i_count; i++ )
1006     {
1007         free( info.info[i].psz_name );
1008         free( info.info[i].psz_value );
1009     }
1010
1011     return s;
1012 }
1013 static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
1014 {
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;
1019 #endif
1020     DIR           *p_dir;
1021     struct dirent *p_dir_content;
1022     char          sep;
1023
1024     /* convert all / to native separator */
1025 #if defined( WIN32 )
1026     while( (p = strchr( psz_dir, '/' )) )
1027     {
1028         *p = '\\';
1029     }
1030     sep = '\\';
1031 #else
1032     sep = '/';
1033 #endif
1034
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 ) &&
1039 #endif
1040            psz_dir[strlen( psz_dir ) -1 ] == sep )
1041     {
1042         psz_dir[strlen( psz_dir ) -1 ]  ='\0';
1043     }
1044     /* remove double separator */
1045     for( p = src = psz_dir; *src != '\0'; src++, p++ )
1046     {
1047         if( src[0] == sep && src[1] == sep )
1048         {
1049             src++;
1050         }
1051         *p = *src;
1052     }
1053     *p = '\0';
1054
1055     if( *psz_dir == '\0' )
1056     {
1057         return s;
1058     }
1059     /* first fix all .. dir */
1060     p = src = psz_dir;
1061     while( *src )
1062     {
1063         if( src[0] == '.' && src[1] == '.' )
1064         {
1065             src += 2;
1066             if( p <= &psz_dir[1] )
1067             {
1068                 continue;
1069             }
1070
1071             p -= 2;
1072
1073             while( p > &psz_dir[1] && *p != sep )
1074             {
1075                 p--;
1076             }
1077         }
1078         else if( *src == sep )
1079         {
1080             if( p > psz_dir && p[-1] == sep )
1081             {
1082                 src++;
1083             }
1084             else
1085             {
1086                 *p++ = *src++;
1087             }
1088         }
1089         else
1090         {
1091             do
1092             {
1093                 *p++ = *src++;
1094             } while( *src && *src != sep );
1095         }
1096     }
1097     *p = '\0';
1098
1099     fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1100
1101 #ifdef HAVE_SYS_STAT_H
1102     if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1103     {
1104         return s;
1105     }
1106 #endif
1107
1108     if( ( p_dir = opendir( psz_dir ) ) == NULL )
1109     {
1110         fprintf( stderr, "cannot open dir (%s)", psz_dir );
1111         return s;
1112     }
1113
1114     /* remove traling / or \ */
1115     for( p = &psz_dir[strlen( psz_dir) - 1]; p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1116     {
1117         *p = '\0';
1118     }
1119
1120     for( ;; )
1121     {
1122         mvar_t *f;
1123
1124         /* parse psz_src dir */
1125         if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1126         {
1127             break;
1128         }
1129         if( !strcmp( p_dir_content->d_name, "." ) )
1130         {
1131             continue;
1132         }
1133
1134 #if defined( WIN32 )
1135         sprintf( tmp, "%s\\%s", psz_dir, p_dir_content->d_name );
1136 #else
1137         sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1138 #endif
1139
1140 #ifdef HAVE_SYS_STAT_H
1141         if( stat( tmp, &stat_info ) == -1 )
1142         {
1143             continue;
1144         }
1145 #endif
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 ) )
1150         {
1151             mvar_AppendNewVar( f, "type", "directory" );
1152         }
1153         else if( S_ISREG( stat_info.st_mode ) )
1154         {
1155             mvar_AppendNewVar( f, "type", "file" );
1156         }
1157         else
1158         {
1159             mvar_AppendNewVar( f, "type", "unknown" );
1160         }
1161
1162         sprintf( tmp, "%lld", stat_info.st_size );
1163         mvar_AppendNewVar( f, "size", tmp );
1164
1165         /* FIXME memory leak FIXME */
1166 #ifdef HAVE_CTIME_R
1167         ctime_r( &stat_info.st_mtime, tmp );
1168         mvar_AppendNewVar( f, "date", tmp );
1169 #else
1170         mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1171 #endif
1172
1173 #else
1174         mvar_AppendNewVar( f, "type", "unknown" );
1175         mvar_AppendNewVar( f, "size", "unknown" );
1176         mvar_AppendNewVar( f, "date", "unknown" );
1177 #endif
1178         mvar_AppendVar( s, f );
1179     }
1180
1181     return s;
1182 }
1183
1184 static void SSInit( rpn_stack_t * );
1185 static void SSClean( rpn_stack_t * );
1186 static void EvaluateRPN( mvar_t  *, rpn_stack_t *, char * );
1187
1188 static void SSPush  ( rpn_stack_t *, char * );
1189 static char *SSPop  ( rpn_stack_t * );
1190
1191 static void SSPushN ( rpn_stack_t *, int );
1192 static int  SSPopN  ( rpn_stack_t *, mvar_t  * );
1193
1194
1195 /****************************************************************************
1196  * Macro handling
1197  ****************************************************************************/
1198 typedef struct
1199 {
1200     char *id;
1201     char *param1;
1202     char *param2;
1203 } macro_t;
1204
1205 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1206 {
1207     int i_read;
1208
1209     /* just load the file */
1210     *pi_data = 0;
1211     *pp_data = malloc( 1025 );  /* +1 for \0 */
1212     while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1213     {
1214         *pi_data += 1024;
1215         *pp_data = realloc( *pp_data, *pi_data  + 1025 );
1216     }
1217     if( i_read > 0 )
1218     {
1219         *pi_data += i_read;
1220     }
1221     (*pp_data)[*pi_data] = '\0';
1222
1223     return VLC_SUCCESS;
1224 }
1225
1226 static int MacroParse( macro_t *m, uint8_t *psz_src )
1227 {
1228     uint8_t *dup = strdup( psz_src );
1229     uint8_t *src = dup;
1230     uint8_t *p;
1231     int     i_skip;
1232
1233 #define EXTRACT( name, l ) \
1234         src += l;    \
1235         p = strchr( src, '"' );             \
1236         if( p )                             \
1237         {                                   \
1238             *p++ = '\0';                    \
1239         }                                   \
1240         m->name = strdup( src );            \
1241         if( !p )                            \
1242         {                                   \
1243             break;                          \
1244         }                                   \
1245         src = p;
1246
1247     /* init m */
1248     m->id = NULL;
1249     m->param1 = NULL;
1250     m->param2 = NULL;
1251
1252     /* parse */
1253     src += 4;
1254
1255     while( *src )
1256     {
1257         while( *src == ' ')
1258         {
1259             src++;
1260         }
1261         if( !strncmp( src, "id=\"", 4 ) )
1262         {
1263             EXTRACT( id, 4 );
1264         }
1265         else if( !strncmp( src, "param1=\"", 8 ) )
1266         {
1267             EXTRACT( param1, 8 );
1268         }
1269         else if( !strncmp( src, "param2=\"", 8 ) )
1270         {
1271             EXTRACT( param2, 8 );
1272         }
1273         else
1274         {
1275             break;
1276         }
1277     }
1278     if( strstr( src, "/>" ) )
1279     {
1280         src = strstr( src, "/>" ) + 2;
1281     }
1282     else
1283     {
1284         src += strlen( src );
1285     }
1286
1287     if( m->id == NULL )
1288     {
1289         m->id = strdup( "" );
1290     }
1291     if( m->param1 == NULL )
1292     {
1293         m->param1 = strdup( "" );
1294     }
1295     if( m->param2 == NULL )
1296     {
1297         m->param2 = strdup( "" );
1298     }
1299     i_skip = src - dup;
1300
1301     free( dup );
1302     return i_skip;
1303 #undef EXTRACT
1304 }
1305
1306 static void MacroClean( macro_t *m )
1307 {
1308     free( m->id );
1309     free( m->param1 );
1310     free( m->param2 );
1311 }
1312
1313 enum macroType
1314 {
1315     MVLC_UNKNOWN = 0,
1316     MVLC_CONTROL,
1317         MVLC_PLAY,
1318         MVLC_STOP,
1319         MVLC_PAUSE,
1320         MVLC_NEXT,
1321         MVLC_PREVIOUS,
1322         MVLC_ADD,
1323         MVLC_DEL,
1324         MVLC_EMPTY,
1325
1326         MVLC_CLOSE,
1327         MVLC_SHUTDOWN,
1328     MVLC_FOREACH,
1329     MVLC_IF,
1330     MVLC_RPN,
1331     MVLC_ELSE,
1332     MVLC_END,
1333     MVLC_GET,
1334     MVLC_SET,
1335         MVLC_INT,
1336         MVLC_FLOAT,
1337         MVLC_STRING,
1338
1339     MVLC_VALUE
1340 };
1341
1342 static struct
1343 {
1344     char *psz_name;
1345     int  i_type;
1346 }
1347 StrToMacroTypeTab [] =
1348 {
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 },
1356
1357         /* playlist management */
1358         { "add",            MVLC_ADD },
1359         { "del",            MVLC_DEL },
1360         { "empty",          MVLC_EMPTY },
1361
1362         /* admin control */
1363         { "close",          MVLC_CLOSE },
1364         { "shutdown",       MVLC_SHUTDOWN },
1365
1366     { "rpn",        MVLC_RPN },
1367
1368     { "foreach",    MVLC_FOREACH },
1369     { "value",      MVLC_VALUE },
1370
1371     { "if",         MVLC_IF },
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 },
1379
1380     /* end */
1381     { NULL,         MVLC_UNKNOWN }
1382 };
1383
1384 static int StrToMacroType( char *name )
1385 {
1386     int i;
1387
1388     if( !name || *name == '\0')
1389     {
1390         return MVLC_UNKNOWN;
1391     }
1392     for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1393     {
1394         if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1395         {
1396             return StrToMacroTypeTab[i].i_type;
1397         }
1398     }
1399     return MVLC_UNKNOWN;
1400 }
1401
1402 static void MacroDo( httpd_file_callback_args_t *p_args,
1403                      macro_t *m,
1404                      uint8_t *p_request, int i_request,
1405                      uint8_t **pp_data,  int *pi_data,
1406                      uint8_t **pp_dst )
1407 {
1408     intf_thread_t  *p_intf = p_args->p_intf;
1409     intf_sys_t     *p_sys = p_args->p_intf->p_sys;
1410     char control[512];
1411
1412 #define ALLOC( l ) \
1413     {               \
1414         int __i__ = *pp_dst - *pp_data; \
1415         *pi_data += (l);                  \
1416         *pp_data = realloc( *pp_data, *pi_data );   \
1417         *pp_dst = (*pp_data) + __i__;   \
1418     }
1419 #define PRINT( str ) \
1420     ALLOC( strlen( str ) + 1 ); \
1421     *pp_dst += sprintf( *pp_dst, str );
1422
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 ) \
1428     {  \
1429         /* Prevent script injection */ \
1430         if( *psz_cur == '<' ) *psz_cur = '*'; \
1431         if( *psz_cur == '>' ) *psz_cur = '*'; \
1432         psz_cur++ ; \
1433     }
1434
1435     switch( StrToMacroType( m->id ) )
1436     {
1437         case MVLC_CONTROL:
1438             if( i_request <= 0 )
1439             {
1440                 break;
1441             }
1442             uri_extract_value( p_request, "control", control, 512 );
1443             if( *m->param1 && !strstr( m->param1, control ) )
1444             {
1445                 msg_Warn( p_intf, "unauthorized control=%s", control );
1446                 break;
1447             }
1448             switch( StrToMacroType( control ) )
1449             {
1450                 case MVLC_PLAY:
1451                 {
1452                     int i_item;
1453                     char item[512];
1454
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 );
1459                     break;
1460                 }
1461                 case MVLC_STOP:
1462                     playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1463                     msg_Dbg( p_intf, "requested playlist stop" );
1464                     break;
1465                 case MVLC_PAUSE:
1466                     playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1467                     msg_Dbg( p_intf, "requested playlist pause" );
1468                     break;
1469                 case MVLC_NEXT:
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" );
1473                     break;
1474                 case MVLC_PREVIOUS:
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" );
1478                     break;
1479
1480                 /* playlist management */
1481                 case MVLC_ADD:
1482                 {
1483                     char mrl[512];
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 );
1489                     break;
1490                 }
1491                 case MVLC_DEL:
1492                 {
1493                     int i_item;
1494                     char item[512];
1495
1496                     uri_extract_value( p_request, "item", item, 512 );
1497                     i_item = atoi( item );
1498
1499                     playlist_Delete( p_sys->p_playlist, i_item );
1500                     msg_Dbg( p_intf, "requested playlist del: %d", i_item );
1501                     break;
1502                 }
1503                 case MVLC_EMPTY:
1504                 {
1505                     while( p_sys->p_playlist->i_size > 0 )
1506                     {
1507                         playlist_Delete( p_sys->p_playlist, 0 );
1508                     }
1509                     msg_Dbg( p_intf, "requested playlist empty" );
1510                     break;
1511                 }
1512
1513                 /* admin function */
1514                 case MVLC_CLOSE:
1515                 {
1516                     char id[512];
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 ) )
1520                     {
1521                         msg_Warn( p_intf, "close failed for id=%s", id );
1522                     }
1523                     break;
1524                 }
1525                 case MVLC_SHUTDOWN:
1526                 {
1527                     msg_Dbg( p_intf, "requested shutdown" );
1528                     p_intf->p_vlc->b_die = VLC_TRUE;
1529                     break;
1530                 }
1531                 default:
1532                     PRINTS( "<!-- control param(%s) unsuported -->", control );
1533                     break;
1534             }
1535             break;
1536
1537         case MVLC_SET:
1538         {
1539             char    value[512];
1540             int     i;
1541             float   f;
1542
1543             if( i_request <= 0 ||
1544                 *m->param1  == '\0' ||
1545                 strstr( p_request, m->param1 ) == NULL )
1546             {
1547                 break;
1548             }
1549             uri_extract_value( p_request, m->param1,  value, 512 );
1550             uri_decode_url_encoded( value );
1551
1552             switch( StrToMacroType( m->param2 ) )
1553             {
1554                 case MVLC_INT:
1555                     i = atoi( value );
1556                     config_PutInt( p_intf, m->param1, i );
1557                     break;
1558                 case MVLC_FLOAT:
1559                     f = atof( value );
1560                     config_PutFloat( p_intf, m->param1, f );
1561                     break;
1562                 case MVLC_STRING:
1563                     config_PutPsz( p_intf, m->param1, value );
1564                     break;
1565                 default:
1566                     PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
1567             }
1568             break;
1569         }
1570         case MVLC_GET:
1571         {
1572             char    value[512];
1573             int     i;
1574             float   f;
1575             char    *psz;
1576
1577             if( *m->param1  == '\0' )
1578             {
1579                 break;
1580             }
1581
1582             switch( StrToMacroType( m->param2 ) )
1583             {
1584                 case MVLC_INT:
1585                     i = config_GetInt( p_intf, m->param1 );
1586                     sprintf( value, "%i", i );
1587                     break;
1588                 case MVLC_FLOAT:
1589                     f = config_GetFloat( p_intf, m->param1 );
1590                     sprintf( value, "%f", f );
1591                     break;
1592                 case MVLC_STRING:
1593                     psz = config_GetPsz( p_intf, m->param1 );
1594                     sprintf( value, "%s", psz ? psz : "" );
1595                     break;
1596                 default:
1597                     sprintf( value, "invalid type(%s) in set", m->param2 );
1598                     break;
1599             }
1600             msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
1601             PRINTS( "%s", value );
1602             break;
1603         }
1604         case MVLC_VALUE:
1605         {
1606             char *s, *v;
1607
1608             if( m->param1 )
1609             {
1610                 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1611                 s = SSPop( &p_args->stack );
1612                 v = mvar_GetValue( p_args->vars, s );
1613             }
1614             else
1615             {
1616                 v = s = SSPop( &p_args->stack );
1617             }
1618
1619             PRINTS( "%s", v );
1620             free( s );
1621             break;
1622         }
1623         case MVLC_RPN:
1624             EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1625             break;
1626         case MVLC_UNKNOWN:
1627         default:
1628             PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
1629             msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
1630             break;
1631     }
1632 #undef PRINTS
1633 #undef PRINT
1634 #undef ALLOC
1635 }
1636
1637 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
1638 {
1639     int     i_id;
1640     int     i_level = 0;
1641
1642     while( src < end )
1643     {
1644         if( src + 4 < end  && !strncmp( src, "<vlc", 4 ) )
1645         {
1646             int i_skip;
1647             macro_t m;
1648
1649             i_skip = MacroParse( &m, src );
1650
1651             i_id = StrToMacroType( m.id );
1652
1653             switch( i_id )
1654             {
1655                 case MVLC_IF:
1656                 case MVLC_FOREACH:
1657                     i_level++;
1658                     break;
1659                 case MVLC_END:
1660                     i_level--;
1661                     break;
1662                 default:
1663                     break;
1664             }
1665
1666             if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
1667                 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
1668             {
1669                 return src + ( b_after ? i_skip : 0 );
1670             }
1671             else if( i_level < 0 )
1672             {
1673                 return NULL;
1674             }
1675
1676             src += i_skip;
1677         }
1678         else
1679         {
1680             src++;
1681         }
1682     }
1683
1684     return NULL;
1685 }
1686
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,
1690                      uint8_t **pp_dst,
1691                      uint8_t *_src, uint8_t *_end )
1692 {
1693     intf_thread_t  *p_intf = p_args->p_intf;
1694
1695     uint8_t *src, *dup, *end;
1696     uint8_t *dst = *pp_dst;
1697
1698     src = dup = malloc( _end - _src + 1 );
1699     end = src +( _end - _src );
1700
1701     memcpy( src, _src, _end - _src );
1702     *end = '\0';
1703
1704     /* we parse searching <vlc */
1705     while( src < end )
1706     {
1707         uint8_t *p;
1708         int i_copy;
1709
1710         p = strstr( src, "<vlc" );
1711         if( p < end && p == src )
1712         {
1713             macro_t m;
1714
1715             src += MacroParse( &m, src );
1716
1717             //msg_Dbg( p_intf, "macro_id=%s", m.id );
1718
1719             switch( StrToMacroType( m.id ) )
1720             {
1721                 case MVLC_IF:
1722                 {
1723                     vlc_bool_t i_test;
1724                     uint8_t    *endif;
1725
1726                     EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
1727                     if( SSPopN( &p_args->stack, p_args->vars ) )
1728                     {
1729                         i_test = 1;
1730                     }
1731                     else
1732                     {
1733                         i_test = 0;
1734                     }
1735                     endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
1736
1737                     if( i_test == 0 )
1738                     {
1739                         uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
1740
1741                         if( start )
1742                         {
1743                             uint8_t *stop  = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
1744                             if( stop )
1745                             {
1746                                 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
1747                             }
1748                         }
1749                     }
1750                     else if( i_test == 1 )
1751                     {
1752                         uint8_t *stop;
1753                         if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
1754                         {
1755                             stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
1756                         }
1757                         if( stop )
1758                         {
1759                             Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
1760                         }
1761                     }
1762
1763                     src = endif;
1764                     break;
1765                 }
1766                 case MVLC_FOREACH:
1767                 {
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 );
1771
1772                     if( stop )
1773                     {
1774                         mvar_t *index;
1775                         int    i_idx;
1776                         mvar_t *v;
1777                         if( !strcmp( m.param2, "integer" ) )
1778                         {
1779                             char *arg = SSPop( &p_args->stack );
1780                             index = mvar_IntegerSetNew( m.param1, arg );
1781                             free( arg );
1782                         }
1783                         else if( !strcmp( m.param2, "directory" ) )
1784                         {
1785                             char *arg = SSPop( &p_args->stack );
1786                             index = mvar_FileSetNew( m.param1, arg );
1787                             free( arg );
1788                         }
1789                         else if( !strcmp( m.param2, "playlist" ) )
1790                         {
1791                             index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
1792                         }
1793                         else if( !strcmp( m.param2, "informations" ) )
1794                         {
1795                             index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
1796                         }
1797                         else if( !strcmp( m.param2, "hosts" ) )
1798                         {
1799                             index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
1800                         }
1801                         else if( !strcmp( m.param2, "urls" ) )
1802                         {
1803                             index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
1804                         }
1805                         else if( !strcmp( m.param2, "connections" ) )
1806                         {
1807                             index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
1808                         }
1809                         else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
1810                         {
1811                             index = mvar_Duplicate( v );
1812                         }
1813                         else
1814                         {
1815                             msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
1816                             src = endfor;
1817                             break;
1818                         }
1819
1820                         for( i_idx = 0; i_idx < index->i_field; i_idx++ )
1821                         {
1822                             mvar_t *f = mvar_Duplicate( index->field[i_idx] );
1823
1824                             //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
1825
1826                             free( f->name );
1827                             f->name = strdup( m.param1 );
1828
1829
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 );
1833
1834                             mvar_Delete( f );
1835                         }
1836                         mvar_Delete( index );
1837
1838                         src = endfor;
1839                     }
1840                     break;
1841                 }
1842                 default:
1843                     MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
1844                     break;
1845             }
1846
1847             MacroClean( &m );
1848             continue;
1849         }
1850
1851         i_copy =   ( (p == NULL || p > end ) ? end : p  ) - src;
1852         if( i_copy > 0 )
1853         {
1854             int i_index = dst - *pp_data;
1855
1856             *pi_data += i_copy;
1857             *pp_data = realloc( *pp_data, *pi_data );
1858             dst = (*pp_data) + i_index;
1859
1860             memcpy( dst, src, i_copy );
1861             dst += i_copy;
1862             src += i_copy;
1863         }
1864     }
1865
1866     *pp_dst = dst;
1867     free( dup );
1868 }
1869
1870 /****************************************************************************
1871  * http_get:
1872  ****************************************************************************
1873  * a file with mime == text/html is parsed and all "macro" replaced
1874  * <vlc id="macro name" [param1="" [param2=""]] />
1875  * valid id are
1876  *
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 )
1881 {
1882     char *p;
1883     FILE *f;
1884
1885     if( ( f = fopen( p_args->file, "r" ) ) == NULL )
1886     {
1887         p = *pp_data = malloc( 10240 );
1888         if( !p )
1889         {
1890                 return VLC_EGENERIC;
1891         }
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" );
1902
1903         *pi_data = strlen( *pp_data ) + 1;
1904
1905         return VLC_SUCCESS;
1906     }
1907
1908     if( strcmp( p_args->mime, "text/html" ) )
1909     {
1910         FileLoad( f, pp_data, pi_data );
1911     }
1912     else
1913     {
1914         int  i_buffer;
1915         uint8_t *p_buffer;
1916         uint8_t *dst;
1917
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 );
1923
1924         SSInit( &p_args->stack );
1925
1926         /* first we load in a temporary buffer */
1927         FileLoad( f, &p_buffer, &i_buffer );
1928
1929         /* allocate output */
1930         *pi_data = i_buffer + 1000;
1931         dst = *pp_data = malloc( *pi_data );
1932
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] );
1935
1936         *dst++ = '\0';
1937         *pi_data = dst - *pp_data;
1938
1939         SSClean( &p_args->stack );
1940         mvar_Delete( p_args->vars );
1941     }
1942
1943     fclose( f );
1944
1945     return VLC_SUCCESS;
1946 }
1947
1948 /****************************************************************************
1949  * uri parser
1950  ****************************************************************************/
1951 static void uri_extract_value( char *psz_uri, char *psz_name,
1952                                char *psz_value, int i_value_max )
1953 {
1954     char *p;
1955
1956     p = strstr( psz_uri, psz_name );
1957     if( p )
1958     {
1959         int i_len;
1960
1961         p += strlen( psz_name );
1962         if( *p == '=' ) p++;
1963
1964         if( strchr( p, '&' ) )
1965         {
1966             i_len = strchr( p, '&' ) - p;
1967         }
1968         else
1969         {
1970             /* for POST method */
1971             if( strchr( p, '\n' ) )
1972             {
1973                 i_len = strchr( p, '\n' ) - p;
1974                 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
1975             }
1976             else
1977             {
1978                 i_len = strlen( p );
1979             }
1980         }
1981         i_len = __MIN( i_value_max - 1, i_len );
1982         if( i_len > 0 )
1983         {
1984             strncpy( psz_value, p, i_len );
1985             psz_value[i_len] = '\0';
1986         }
1987         else
1988         {
1989             strncpy( psz_value, "", i_value_max );
1990         }
1991     }
1992     else
1993     {
1994         strncpy( psz_value, "", i_value_max );
1995     }
1996 }
1997
1998 static void uri_decode_url_encoded( char *psz )
1999 {
2000     char *dup = strdup( psz );
2001     char *p = dup;
2002
2003     while( *p )
2004     {
2005         if( *p == '%' )
2006         {
2007             char val[3];
2008             p++;
2009             if( !*p )
2010             {
2011                 break;
2012             }
2013
2014             val[0] = *p++;
2015             val[1] = *p++;
2016             val[2] = '\0';
2017
2018             *psz++ = strtol( val, NULL, 16 );
2019         }
2020         else if( *p == '+' )
2021         {
2022             *psz++ = ' ';
2023             p++;
2024         }
2025         else
2026         {
2027             *psz++ = *p++;
2028         }
2029     }
2030     *psz++  ='\0';
2031     free( dup );
2032 }
2033
2034 /****************************************************************************
2035  * Light RPN evaluator
2036  ****************************************************************************/
2037 static void SSInit( rpn_stack_t *st )
2038 {
2039     st->i_stack = 0;
2040 }
2041
2042 static void SSClean( rpn_stack_t *st )
2043 {
2044     while( st->i_stack > 0 )
2045     {
2046         free( st->stack[--st->i_stack] );
2047     }
2048 }
2049
2050 static void SSPush( rpn_stack_t *st, char *s )
2051 {
2052     if( st->i_stack < STACK_MAX )
2053     {
2054         st->stack[st->i_stack++] = strdup( s );
2055     }
2056 }
2057
2058 static char * SSPop( rpn_stack_t *st )
2059 {
2060     if( st->i_stack <= 0 )
2061     {
2062         return strdup( "" );
2063     }
2064     else
2065     {
2066         return st->stack[--st->i_stack];
2067     }
2068 }
2069
2070 static int SSPopN( rpn_stack_t *st, mvar_t  *vars )
2071 {
2072     char *name;
2073     char *value;
2074
2075     char *end;
2076     int  i;
2077
2078     name = SSPop( st );
2079     i = strtol( name, &end, 0 );
2080     if( end == name )
2081     {
2082         value = mvar_GetValue( vars, name );
2083         i = atoi( value );
2084     }
2085     free( name );
2086
2087     return( i );
2088 }
2089
2090 static void SSPushN( rpn_stack_t *st, int i )
2091 {
2092     char v[512];
2093
2094     sprintf( v, "%d", i );
2095     SSPush( st, v );
2096 }
2097
2098 static void  EvaluateRPN( mvar_t  *vars, rpn_stack_t *st, char *exp )
2099 {
2100     for( ;; )
2101     {
2102         char s[100], *p;
2103
2104         /* skip spcae */
2105         while( *exp == ' ' )
2106         {
2107             exp++;
2108         }
2109
2110         if( *exp == '\'' )
2111         {
2112             /* extract string */
2113             p = &s[0];
2114             exp++;
2115             while( *exp && *exp != '\'' )
2116             {
2117                 *p++ = *exp++;
2118             }
2119             *p = '\0';
2120             exp++;
2121             SSPush( st, s );
2122             continue;
2123         }
2124
2125         /* extract token */
2126         p = strchr( exp, ' ' );
2127         if( !p )
2128         {
2129             strcpy( s, exp );
2130
2131             exp += strlen( exp );
2132         }
2133         else
2134         {
2135             int i = p -exp;
2136             strncpy( s, exp, i );
2137             s[i] = '\0';
2138
2139             exp = p + 1;
2140         }
2141
2142         if( *s == '\0' )
2143         {
2144             break;
2145         }
2146
2147         /* 1. Integer function */
2148         if( !strcmp( s, "!" ) )
2149         {
2150             SSPushN( st, ~SSPopN( st, vars ) );
2151         }
2152         else if( !strcmp( s, "^" ) )
2153         {
2154             SSPushN( st, SSPopN( st, vars ) ^ SSPopN( st, vars ) );
2155         }
2156         else if( !strcmp( s, "&" ) )
2157         {
2158             SSPushN( st, SSPopN( st, vars ) & SSPopN( st, vars ) );
2159         }
2160         else if( !strcmp( s, "|" ) )
2161         {
2162             SSPushN( st, SSPopN( st, vars ) | SSPopN( st, vars ) );
2163         }
2164         else if( !strcmp( s, "+" ) )
2165         {
2166             SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2167         }
2168         else if( !strcmp( s, "-" ) )
2169         {
2170             int i = SSPopN( st, vars );
2171             int j = SSPopN( st, vars );
2172             SSPushN( st, i - j );
2173         }
2174         else if( !strcmp( s, "*" ) )
2175         {
2176             SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2177         }
2178         else if( !strcmp( s, "/" ) )
2179         {
2180             int i, j;
2181
2182             i = SSPopN( st, vars );
2183             j = SSPopN( st, vars );
2184
2185             SSPushN( st, j != 0 ? i / j : 0 );
2186         }
2187         else if( !strcmp( s, "%" ) )
2188         {
2189             int i, j;
2190
2191             i = SSPopN( st, vars );
2192             j = SSPopN( st, vars );
2193
2194             SSPushN( st, j != 0 ? i % j : 0 );
2195         }
2196         /* 2. integer tests */
2197         else if( !strcmp( s, "=" ) )
2198         {
2199             SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2200         }
2201         else if( !strcmp( s, "<" ) )
2202         {
2203             int i = SSPopN( st, vars );
2204             int j = SSPopN( st, vars );
2205
2206             SSPushN( st, i < j ? -1 : 0 );
2207         }
2208         else if( !strcmp( s, ">" ) )
2209         {
2210             int i = SSPopN( st, vars );
2211             int j = SSPopN( st, vars );
2212
2213             SSPushN( st, i > j ? -1 : 0 );
2214         }
2215         else if( !strcmp( s, "<=" ) )
2216         {
2217             int i = SSPopN( st, vars );
2218             int j = SSPopN( st, vars );
2219
2220             SSPushN( st, i <= j ? -1 : 0 );
2221         }
2222         else if( !strcmp( s, ">=" ) )
2223         {
2224             int i = SSPopN( st, vars );
2225             int j = SSPopN( st, vars );
2226
2227             SSPushN( st, i >= j ? -1 : 0 );
2228         }
2229         /* 3. string functions */
2230         else if( !strcmp( s, "strcat" ) )
2231         {
2232             char *s1 = SSPop( st );
2233             char *s2 = SSPop( st );
2234             char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2235
2236             strcpy( str, s1 );
2237             strcat( str, s2 );
2238
2239             SSPush( st, str );
2240             free( s1 );
2241             free( s2 );
2242             free( str );
2243         }
2244         else if( !strcmp( s, "strcmp" ) )
2245         {
2246             char *s1 = SSPop( st );
2247             char *s2 = SSPop( st );
2248
2249             SSPushN( st, strcmp( s1, s2 ) );
2250             free( s1 );
2251             free( s2 );
2252         }
2253         else if( !strcmp( s, "strlen" ) )
2254         {
2255             char *str = SSPop( st );
2256
2257             SSPushN( st, strlen( str ) );
2258             free( str );
2259         }
2260         /* 4. stack functions */
2261         else if( !strcmp( s, "dup" ) )
2262         {
2263             char *str = SSPop( st );
2264             SSPush( st, str );
2265             SSPush( st, str );
2266             free( str );
2267         }
2268         else if( !strcmp( s, "drop" ) )
2269         {
2270             char *str = SSPop( st );
2271             free( str );
2272         }
2273         else if( !strcmp( s, "swap" ) )
2274         {
2275             char *s1 = SSPop( st );
2276             char *s2 = SSPop( st );
2277
2278             SSPush( st, s1 );
2279             SSPush( st, s2 );
2280             free( s1 );
2281             free( s2 );
2282         }
2283         else if( !strcmp( s, "flush" ) )
2284         {
2285             SSClean( st );
2286             SSInit( st );
2287         }
2288         else if( !strcmp( s, "store" ) )
2289         {
2290             char *name  = SSPop( st );
2291             char *value = SSPop( st );
2292
2293             mvar_PushNewVar( vars, name, value );
2294             free( name );
2295             free( value );
2296         }
2297         else if( !strcmp( s, "value" ) )
2298         {
2299             char *name  = SSPop( st );
2300             char *value = mvar_GetValue( vars, name );
2301
2302             SSPush( st, value );
2303
2304             free( name );
2305         }
2306         else if( !strcmp( s, "url_extract" ) )
2307         {
2308             char *url = mvar_GetValue( vars, "url_value" );
2309             char *name = SSPop( st );
2310             char value[512];
2311
2312             uri_extract_value( url, name, value, 512 );
2313             uri_decode_url_encoded( value );
2314             SSPush( st, value );
2315         }
2316         else
2317         {
2318             SSPush( st, s );
2319         }
2320     }
2321 }
2322
2323
2324