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