]> git.sesse.net Git - vlc/blob - modules/control/http.c
* src/misc/win32_specific.c: change the mode of stdin to _O_BINARY so we can use...
[vlc] / modules / control / http.c
1 /*****************************************************************************
2  * http.c :  http mini-server ;)
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: http.c,v 1.20 2003/07/31 21:46:37 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 separator */
1001     while( strlen( psz_dir ) > 1 &&
1002 #if defined( WIN32 )
1003            !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
1004 #endif
1005            psz_dir[strlen( psz_dir ) -1 ] == sep )
1006     {
1007         psz_dir[strlen( psz_dir ) -1 ]  ='\0';
1008     }
1009     /* remove double separator */
1010     for( p = src = psz_dir; *src != '\0'; src++, p++ )
1011     {
1012         if( src[0] == sep && src[1] == sep )
1013         {
1014             src++;
1015         }
1016         *p = *src;
1017     }
1018     *p = '\0';
1019
1020     if( *psz_dir == '\0' )
1021     {
1022         return s;
1023     }
1024     /* first fix all .. dir */
1025     p = src = psz_dir;
1026     while( *src )
1027     {
1028         if( src[0] == '.' && src[1] == '.' )
1029         {
1030             src += 2;
1031             if( p <= &psz_dir[1] )
1032             {
1033                 continue;
1034             }
1035
1036             p -= 2;
1037
1038             while( p > &psz_dir[1] && *p != sep )
1039             {
1040                 p--;
1041             }
1042         }
1043         else if( *src == sep )
1044         {
1045             if( p > psz_dir && p[-1] == sep )
1046             {
1047                 src++;
1048             }
1049             else
1050             {
1051                 *p++ = *src++;
1052             }
1053         }
1054         else
1055         {
1056             do
1057             {
1058                 *p++ = *src++;
1059             } while( *src && *src != sep );
1060         }
1061     }
1062     *p = '\0';
1063
1064     fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );
1065
1066 #ifdef HAVE_SYS_STAT_H
1067     if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
1068     {
1069         return s;
1070     }
1071 #endif
1072
1073     if( ( p_dir = opendir( psz_dir ) ) == NULL )
1074     {
1075         fprintf( stderr, "cannot open dir (%s)", psz_dir );
1076         return s;
1077     }
1078
1079     /* remove traling / or \ */
1080     for( p = &psz_dir[strlen( psz_dir) - 1]; p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
1081     {
1082         *p = '\0';
1083     }
1084
1085     for( ;; )
1086     {
1087         mvar_t *f;
1088
1089         /* parse psz_src dir */
1090         if( ( p_dir_content = readdir( p_dir ) ) == NULL )
1091         {
1092             break;
1093         }
1094         if( !strcmp( p_dir_content->d_name, "." ) )
1095         {
1096             continue;
1097         }
1098
1099 #if defined( WIN32 )
1100         sprintf( tmp, "%s\\%s", psz_dir, p_dir_content->d_name );
1101 #else
1102         sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
1103 #endif
1104
1105 #ifdef HAVE_SYS_STAT_H
1106         if( stat( tmp, &stat_info ) == -1 )
1107         {
1108             continue;
1109         }
1110 #endif
1111         f = mvar_New( name, "set" );
1112         mvar_AppendNewVar( f, "name", tmp );
1113 #ifdef HAVE_SYS_STAT_H
1114         if( S_ISDIR( stat_info.st_mode ) )
1115         {
1116             mvar_AppendNewVar( f, "type", "directory" );
1117         }
1118         else if( S_ISREG( stat_info.st_mode ) )
1119         {
1120             mvar_AppendNewVar( f, "type", "file" );
1121         }
1122         else
1123         {
1124             mvar_AppendNewVar( f, "type", "unknown" );
1125         }
1126
1127         sprintf( tmp, "%lld", stat_info.st_size );
1128         mvar_AppendNewVar( f, "size", tmp );
1129
1130         /* FIXME memory leak FIXME */
1131 #ifdef HAVE_CTIME_R
1132         ctime_r( &stat_info.st_mtime, tmp );
1133         mvar_AppendNewVar( f, "date", tmp );
1134 #else
1135         mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
1136 #endif
1137
1138 #else
1139         mvar_AppendNewVar( f, "type", "unknown" );
1140         mvar_AppendNewVar( f, "size", "unknown" );
1141         mvar_AppendNewVar( f, "date", "unknown" );
1142 #endif
1143         mvar_AppendVar( s, f );
1144     }
1145
1146     return s;
1147 }
1148
1149 static void SSInit( rpn_stack_t * );
1150 static void SSClean( rpn_stack_t * );
1151 static void EvaluateRPN( mvar_t  *, rpn_stack_t *, char * );
1152
1153 static void SSPush  ( rpn_stack_t *, char * );
1154 static char *SSPop  ( rpn_stack_t * );
1155
1156 static void SSPushN ( rpn_stack_t *, int );
1157 static int  SSPopN  ( rpn_stack_t *, mvar_t  * );
1158
1159
1160 /****************************************************************************
1161  * Macro handling
1162  ****************************************************************************/
1163 typedef struct
1164 {
1165     char *id;
1166     char *param1;
1167     char *param2;
1168 } macro_t;
1169
1170 static int FileLoad( FILE *f, uint8_t **pp_data, int *pi_data )
1171 {
1172     int i_read;
1173
1174     /* just load the file */
1175     *pi_data = 0;
1176     *pp_data = malloc( 1025 );  /* +1 for \0 */
1177     while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
1178     {
1179         *pi_data += 1024;
1180         *pp_data = realloc( *pp_data, *pi_data  + 1025 );
1181     }
1182     if( i_read > 0 )
1183     {
1184         *pi_data += i_read;
1185     }
1186     (*pp_data)[*pi_data] = '\0';
1187
1188     return VLC_SUCCESS;
1189 }
1190
1191 static int MacroParse( macro_t *m, uint8_t *psz_src )
1192 {
1193     uint8_t *dup = strdup( psz_src );
1194     uint8_t *src = dup;
1195     uint8_t *p;
1196     int     i_skip;
1197
1198 #define EXTRACT( name, l ) \
1199         src += l;    \
1200         p = strchr( src, '"' );             \
1201         if( p )                             \
1202         {                                   \
1203             *p++ = '\0';                    \
1204         }                                   \
1205         m->name = strdup( src );            \
1206         if( !p )                            \
1207         {                                   \
1208             break;                          \
1209         }                                   \
1210         src = p;
1211
1212     /* init m */
1213     m->id = NULL;
1214     m->param1 = NULL;
1215     m->param2 = NULL;
1216
1217     /* parse */
1218     src += 4;
1219
1220     while( *src )
1221     {
1222         while( *src == ' ')
1223         {
1224             src++;
1225         }
1226         if( !strncmp( src, "id=\"", 4 ) )
1227         {
1228             EXTRACT( id, 4 );
1229         }
1230         else if( !strncmp( src, "param1=\"", 8 ) )
1231         {
1232             EXTRACT( param1, 8 );
1233         }
1234         else if( !strncmp( src, "param2=\"", 8 ) )
1235         {
1236             EXTRACT( param2, 8 );
1237         }
1238         else
1239         {
1240             break;
1241         }
1242     }
1243     if( strstr( src, "/>" ) )
1244     {
1245         src = strstr( src, "/>" ) + 2;
1246     }
1247     else
1248     {
1249         src += strlen( src );
1250     }
1251
1252     if( m->id == NULL )
1253     {
1254         m->id = strdup( "" );
1255     }
1256     if( m->param1 == NULL )
1257     {
1258         m->param1 = strdup( "" );
1259     }
1260     if( m->param2 == NULL )
1261     {
1262         m->param2 = strdup( "" );
1263     }
1264     i_skip = src - dup;
1265
1266     free( dup );
1267     return i_skip;
1268 #undef EXTRACT
1269 }
1270
1271 static void MacroClean( macro_t *m )
1272 {
1273     free( m->id );
1274     free( m->param1 );
1275     free( m->param2 );
1276 }
1277
1278 enum macroType
1279 {
1280     MVLC_UNKNOWN = 0,
1281     MVLC_CONTROL,
1282         MVLC_PLAY,
1283         MVLC_STOP,
1284         MVLC_PAUSE,
1285         MVLC_NEXT,
1286         MVLC_PREVIOUS,
1287         MVLC_ADD,
1288         MVLC_DEL,
1289         MVLC_EMPTY,
1290
1291         MVLC_CLOSE,
1292         MVLC_SHUTDOWN,
1293     MVLC_FOREACH,
1294     MVLC_IF,
1295     MVLC_RPN,
1296     MVLC_ELSE,
1297     MVLC_END,
1298     MVLC_GET,
1299     MVLC_SET,
1300         MVLC_INT,
1301         MVLC_FLOAT,
1302         MVLC_STRING,
1303
1304     MVLC_VALUE
1305 };
1306
1307 static struct
1308 {
1309     char *psz_name;
1310     int  i_type;
1311 }
1312 StrToMacroTypeTab [] =
1313 {
1314     { "control",    MVLC_CONTROL },
1315         /* player control */
1316         { "play",           MVLC_PLAY },
1317         { "stop",           MVLC_STOP },
1318         { "pause",          MVLC_PAUSE },
1319         { "next",           MVLC_NEXT },
1320         { "previous",       MVLC_PREVIOUS },
1321
1322         /* playlist management */
1323         { "add",            MVLC_ADD },
1324         { "del",            MVLC_DEL },
1325         { "empty",          MVLC_EMPTY },
1326
1327         /* admin control */
1328         { "close",          MVLC_CLOSE },
1329         { "shutdown",       MVLC_SHUTDOWN },
1330
1331     { "rpn",        MVLC_RPN },
1332
1333     { "foreach",    MVLC_FOREACH },
1334     { "value",      MVLC_VALUE },
1335
1336     { "if",         MVLC_IF },
1337     { "else",       MVLC_ELSE },
1338     { "end",        MVLC_END },
1339     { "get",         MVLC_GET },
1340     { "set",         MVLC_SET },
1341         { "int",            MVLC_INT },
1342         { "float",          MVLC_FLOAT },
1343         { "string",         MVLC_STRING },
1344
1345     /* end */
1346     { NULL,         MVLC_UNKNOWN }
1347 };
1348
1349 static int StrToMacroType( char *name )
1350 {
1351     int i;
1352
1353     if( !name || *name == '\0')
1354     {
1355         return MVLC_UNKNOWN;
1356     }
1357     for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
1358     {
1359         if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
1360         {
1361             return StrToMacroTypeTab[i].i_type;
1362         }
1363     }
1364     return MVLC_UNKNOWN;
1365 }
1366
1367 static void MacroDo( httpd_file_callback_args_t *p_args,
1368                      macro_t *m,
1369                      uint8_t *p_request, int i_request,
1370                      uint8_t **pp_data,  int *pi_data,
1371                      uint8_t **pp_dst )
1372 {
1373     intf_thread_t  *p_intf = p_args->p_intf;
1374     intf_sys_t     *p_sys = p_args->p_intf->p_sys;
1375     char control[512];
1376
1377 #define ALLOC( l ) \
1378     {               \
1379         int __i__ = *pp_dst - *pp_data; \
1380         *pi_data += (l);                  \
1381         *pp_data = realloc( *pp_data, *pi_data );   \
1382         *pp_dst = (*pp_data) + __i__;   \
1383     }
1384 #define PRINT( str ) \
1385     ALLOC( strlen( str ) + 1 ); \
1386     *pp_dst += sprintf( *pp_dst, str );
1387
1388 #define PRINTS( str, s ) \
1389     ALLOC( strlen( str ) + strlen( s ) + 1 ); \
1390     *pp_dst += sprintf( *pp_dst, str, s );
1391
1392     switch( StrToMacroType( m->id ) )
1393     {
1394         case MVLC_CONTROL:
1395             if( i_request <= 0 )
1396             {
1397                 break;
1398             }
1399             uri_extract_value( p_request, "control", control, 512 );
1400             if( *m->param1 && !strstr( m->param1, control ) )
1401             {
1402                 msg_Warn( p_intf, "unauthorized control=%s", control );
1403                 break;
1404             }
1405             switch( StrToMacroType( control ) )
1406             {
1407                 case MVLC_PLAY:
1408                 {
1409                     int i_item;
1410                     char item[512];
1411
1412                     uri_extract_value( p_request, "item", item, 512 );
1413                     i_item = atoi( item );
1414                     playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
1415                     msg_Dbg( p_intf, "requested playlist item: %i", i_item );
1416                     break;
1417                 }
1418                 case MVLC_STOP:
1419                     playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
1420                     msg_Dbg( p_intf, "requested playlist stop" );
1421                     break;
1422                 case MVLC_PAUSE:
1423                     playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
1424                     msg_Dbg( p_intf, "requested playlist pause" );
1425                     break;
1426                 case MVLC_NEXT:
1427                     playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1428                                       p_sys->p_playlist->i_index + 1 );
1429                     msg_Dbg( p_intf, "requested playlist next" );
1430                     break;
1431                 case MVLC_PREVIOUS:
1432                     playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
1433                                       p_sys->p_playlist->i_index - 1 );
1434                     msg_Dbg( p_intf, "requested playlist next" );
1435                     break;
1436
1437                 /* playlist management */
1438                 case MVLC_ADD:
1439                 {
1440                     char mrl[512];
1441                     uri_extract_value( p_request, "mrl", mrl, 512 );
1442                     uri_decode_url_encoded( mrl );
1443                     playlist_Add( p_sys->p_playlist, mrl, NULL, 0,
1444                                   PLAYLIST_APPEND, PLAYLIST_END );
1445                     msg_Dbg( p_intf, "requested playlist add: %s", mrl );
1446                     break;
1447                 }
1448                 case MVLC_DEL:
1449                 {
1450                     int i_item;
1451                     char item[512];
1452
1453                     uri_extract_value( p_request, "item", item, 512 );
1454                     i_item = atoi( item );
1455
1456                     playlist_Delete( p_sys->p_playlist, i_item );
1457                     msg_Dbg( p_intf, "requested playlist del: %d", i_item );
1458                     break;
1459                 }
1460                 case MVLC_EMPTY:
1461                 {
1462                     while( p_sys->p_playlist->i_size > 0 )
1463                     {
1464                         playlist_Delete( p_sys->p_playlist, 0 );
1465                     }
1466                     msg_Dbg( p_intf, "requested playlist empty" );
1467                     break;
1468                 }
1469
1470                 /* admin function */
1471                 case MVLC_CLOSE:
1472                 {
1473                     char id[512];
1474                     uri_extract_value( p_request, "id", id, 512 );
1475                     msg_Dbg( p_intf, "requested close id=%s", id );
1476                     if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
1477                     {
1478                         msg_Warn( p_intf, "close failed for id=%s", id );
1479                     }
1480                     break;
1481                 }
1482                 case MVLC_SHUTDOWN:
1483                 {
1484                     msg_Dbg( p_intf, "requested shutdown" );
1485                     p_intf->p_vlc->b_die = VLC_TRUE;
1486                     break;
1487                 }
1488                 default:
1489                     PRINTS( "<!-- control param(%s) unsuported -->", control );
1490                     break;
1491             }
1492             break;
1493
1494         case MVLC_SET:
1495         {
1496             char    value[512];
1497             int     i;
1498             float   f;
1499
1500             if( i_request <= 0 ||
1501                 *m->param1  == '\0' ||
1502                 strstr( p_request, m->param1 ) == NULL )
1503             {
1504                 break;
1505             }
1506             uri_extract_value( p_request, m->param1,  value, 512 );
1507             uri_decode_url_encoded( value );
1508
1509             switch( StrToMacroType( m->param2 ) )
1510             {
1511                 case MVLC_INT:
1512                     i = atoi( value );
1513                     config_PutInt( p_intf, m->param1, i );
1514                     break;
1515                 case MVLC_FLOAT:
1516                     f = atof( value );
1517                     config_PutFloat( p_intf, m->param1, f );
1518                     break;
1519                 case MVLC_STRING:
1520                     config_PutPsz( p_intf, m->param1, value );
1521                     break;
1522                 default:
1523                     PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
1524             }
1525             break;
1526         }
1527         case MVLC_GET:
1528         {
1529             char    value[512];
1530             int     i;
1531             float   f;
1532             char    *psz;
1533
1534             if( *m->param1  == '\0' )
1535             {
1536                 break;
1537             }
1538
1539             switch( StrToMacroType( m->param2 ) )
1540             {
1541                 case MVLC_INT:
1542                     i = config_GetInt( p_intf, m->param1 );
1543                     sprintf( value, "%i", i );
1544                     break;
1545                 case MVLC_FLOAT:
1546                     f = config_GetFloat( p_intf, m->param1 );
1547                     sprintf( value, "%f", f );
1548                     break;
1549                 case MVLC_STRING:
1550                     psz = config_GetPsz( p_intf, m->param1 );
1551                     sprintf( value, "%s", psz ? psz : "" );
1552                     break;
1553                 default:
1554                     sprintf( value, "invalid type(%s) in set", m->param2 );
1555                     break;
1556             }
1557             msg_Dbg( p_intf, "get name=%s value=%s type=%s", m->param1, value, m->param2 );
1558             PRINTS( "%s", value );
1559             break;
1560         }
1561         case MVLC_VALUE:
1562         {
1563             char *s, *v;
1564
1565             if( m->param1 )
1566             {
1567                 EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1568                 s = SSPop( &p_args->stack );
1569                 v = mvar_GetValue( p_args->vars, s );
1570             }
1571             else
1572             {
1573                 v = s = SSPop( &p_args->stack );
1574             }
1575
1576             PRINTS( "%s", v );
1577             free( s );
1578             break;
1579         }
1580         case MVLC_RPN:
1581             EvaluateRPN( p_args->vars, &p_args->stack, m->param1 );
1582             break;
1583         case MVLC_UNKNOWN:
1584         default:
1585             PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
1586             msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
1587             break;
1588     }
1589 #undef PRINTS
1590 #undef PRINT
1591 #undef ALLOC
1592 }
1593
1594 static uint8_t *MacroSearch( uint8_t *src, uint8_t *end, int i_mvlc, vlc_bool_t b_after )
1595 {
1596     int     i_id;
1597     int     i_level = 0;
1598
1599     while( src < end )
1600     {
1601         if( src + 4 < end  && !strncmp( src, "<vlc", 4 ) )
1602         {
1603             int i_skip;
1604             macro_t m;
1605
1606             i_skip = MacroParse( &m, src );
1607
1608             i_id = StrToMacroType( m.id );
1609
1610             switch( i_id )
1611             {
1612                 case MVLC_IF:
1613                 case MVLC_FOREACH:
1614                     i_level++;
1615                     break;
1616                 case MVLC_END:
1617                     i_level--;
1618                     break;
1619                 default:
1620                     break;
1621             }
1622
1623             if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
1624                 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
1625             {
1626                 return src + ( b_after ? i_skip : 0 );
1627             }
1628             else if( i_level < 0 )
1629             {
1630                 return NULL;
1631             }
1632
1633             src += i_skip;
1634         }
1635         else
1636         {
1637             src++;
1638         }
1639     }
1640
1641     return NULL;
1642 }
1643
1644 static void Execute( httpd_file_callback_args_t *p_args,
1645                      uint8_t *p_request, int i_request,
1646                      uint8_t **pp_data, int *pi_data,
1647                      uint8_t **pp_dst,
1648                      uint8_t *_src, uint8_t *_end )
1649 {
1650     intf_thread_t  *p_intf = p_args->p_intf;
1651
1652     uint8_t *src, *dup, *end;
1653     uint8_t *dst = *pp_dst;
1654
1655     src = dup = malloc( _end - _src + 1 );
1656     end = src +( _end - _src );
1657
1658     memcpy( src, _src, _end - _src );
1659     *end = '\0';
1660
1661     /* we parse searching <vlc */
1662     while( src < end )
1663     {
1664         uint8_t *p;
1665         int i_copy;
1666
1667         p = strstr( src, "<vlc" );
1668         if( p < end && p == src )
1669         {
1670             macro_t m;
1671
1672             src += MacroParse( &m, src );
1673
1674             //msg_Dbg( p_intf, "macro_id=%s", m.id );
1675
1676             switch( StrToMacroType( m.id ) )
1677             {
1678                 case MVLC_IF:
1679                 {
1680                     vlc_bool_t i_test;
1681                     uint8_t    *endif;
1682
1683                     EvaluateRPN( p_args->vars, &p_args->stack, m.param1 );
1684                     if( SSPopN( &p_args->stack, p_args->vars ) )
1685                     {
1686                         i_test = 1;
1687                     }
1688                     else
1689                     {
1690                         i_test = 0;
1691                     }
1692                     endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
1693
1694                     if( i_test == 0 )
1695                     {
1696                         uint8_t *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
1697
1698                         if( start )
1699                         {
1700                             uint8_t *stop  = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
1701                             if( stop )
1702                             {
1703                                 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
1704                             }
1705                         }
1706                     }
1707                     else if( i_test == 1 )
1708                     {
1709                         uint8_t *stop;
1710                         if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
1711                         {
1712                             stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
1713                         }
1714                         if( stop )
1715                         {
1716                             Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, src, stop );
1717                         }
1718                     }
1719
1720                     src = endif;
1721                     break;
1722                 }
1723                 case MVLC_FOREACH:
1724                 {
1725                     uint8_t *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
1726                     uint8_t *start = src;
1727                     uint8_t *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
1728
1729                     if( stop )
1730                     {
1731                         mvar_t *index;
1732                         int    i_idx;
1733                         mvar_t *v;
1734                         if( !strcmp( m.param2, "integer" ) )
1735                         {
1736                             char *arg = SSPop( &p_args->stack );
1737                             index = mvar_IntegerSetNew( m.param1, arg );
1738                             free( arg );
1739                         }
1740                         else if( !strcmp( m.param2, "directory" ) )
1741                         {
1742                             char *arg = SSPop( &p_args->stack );
1743                             index = mvar_FileSetNew( m.param1, arg );
1744                             free( arg );
1745                         }
1746                         else if( !strcmp( m.param2, "playlist" ) )
1747                         {
1748                             index = mvar_PlaylistSetNew( m.param1, p_intf->p_sys->p_playlist );
1749                         }
1750                         else if( !strcmp( m.param2, "informations" ) )
1751                         {
1752                             index = mvar_InfoSetNew( m.param1, p_intf->p_sys->p_input );
1753                         }
1754                         else if( !strcmp( m.param2, "hosts" ) )
1755                         {
1756                             index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
1757                         }
1758                         else if( !strcmp( m.param2, "urls" ) )
1759                         {
1760                             index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
1761                         }
1762                         else if( !strcmp( m.param2, "connections" ) )
1763                         {
1764                             index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
1765                         }
1766                         else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
1767                         {
1768                             index = mvar_Duplicate( v );
1769                         }
1770                         else
1771                         {
1772                             msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
1773                             src = endfor;
1774                             break;
1775                         }
1776
1777                         for( i_idx = 0; i_idx < index->i_field; i_idx++ )
1778                         {
1779                             mvar_t *f = mvar_Duplicate( index->field[i_idx] );
1780
1781                             //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
1782
1783                             free( f->name );
1784                             f->name = strdup( m.param1 );
1785
1786
1787                             mvar_PushVar( p_args->vars, f );
1788                             Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, start, stop );
1789                             mvar_RemoveVar( p_args->vars, f );
1790
1791                             mvar_Delete( f );
1792                         }
1793                         mvar_Delete( index );
1794
1795                         src = endfor;
1796                     }
1797                     break;
1798                 }
1799                 default:
1800                     MacroDo( p_args, &m, p_request, i_request, pp_data, pi_data, &dst );
1801                     break;
1802             }
1803
1804             MacroClean( &m );
1805             continue;
1806         }
1807
1808         i_copy =   ( (p == NULL || p > end ) ? end : p  ) - src;
1809         if( i_copy > 0 )
1810         {
1811             int i_index = dst - *pp_data;
1812
1813             *pi_data += i_copy;
1814             *pp_data = realloc( *pp_data, *pi_data );
1815             dst = (*pp_data) + i_index;
1816
1817             memcpy( dst, src, i_copy );
1818             dst += i_copy;
1819             src += i_copy;
1820         }
1821     }
1822
1823     *pp_dst = dst;
1824     free( dup );
1825 }
1826
1827 /****************************************************************************
1828  * http_get:
1829  ****************************************************************************
1830  * a file with mime == text/html is parsed and all "macro" replaced
1831  * <vlc id="macro name" [param1="" [param2=""]] />
1832  * valid id are
1833  *
1834  ****************************************************************************/
1835 static int  http_get( httpd_file_callback_args_t *p_args,
1836                       uint8_t *p_request, int i_request,
1837                       uint8_t **pp_data, int *pi_data )
1838 {
1839     char *p;
1840     FILE *f;
1841
1842     if( ( f = fopen( p_args->file, "r" ) ) == NULL )
1843     {
1844         p = *pp_data = malloc( 10240 );
1845
1846         p += sprintf( p, "<html>\n" );
1847         p += sprintf( p, "<head>\n" );
1848         p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
1849         p += sprintf( p, "</head>\n" );
1850         p += sprintf( p, "<body>\n" );
1851         p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
1852         p += sprintf( p, "<hr />\n" );
1853         p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
1854         p += sprintf( p, "</body>\n" );
1855         p += sprintf( p, "</html>\n" );
1856
1857         *pi_data = strlen( *pp_data ) + 1;
1858
1859         return VLC_SUCCESS;
1860     }
1861
1862     if( strcmp( p_args->mime, "text/html" ) )
1863     {
1864         FileLoad( f, pp_data, pi_data );
1865     }
1866     else
1867     {
1868         int  i_buffer;
1869         uint8_t *p_buffer;
1870         uint8_t *dst;
1871
1872         p_args->vars = mvar_New( "variables", "" );
1873         mvar_AppendNewVar( p_args->vars, "url_param", i_request > 0 ? "1" : "0" );
1874         mvar_AppendNewVar( p_args->vars, "url_value", p_request );
1875         mvar_AppendNewVar( p_args->vars, "version",   VERSION_MESSAGE );
1876         mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
1877
1878         SSInit( &p_args->stack );
1879
1880         /* first we load in a temporary buffer */
1881         FileLoad( f, &p_buffer, &i_buffer );
1882
1883         /* allocate output */
1884         *pi_data = i_buffer + 1000;
1885         dst = *pp_data = malloc( *pi_data );
1886
1887         /* we parse executing all  <vlc /> macros */
1888         Execute( p_args, p_request, i_request, pp_data, pi_data, &dst, &p_buffer[0], &p_buffer[i_buffer] );
1889
1890         *dst++ = '\0';
1891         *pi_data = dst - *pp_data;
1892
1893         SSClean( &p_args->stack );
1894         mvar_Delete( p_args->vars );
1895     }
1896
1897     fclose( f );
1898
1899     return VLC_SUCCESS;
1900 }
1901
1902 /****************************************************************************
1903  * uri parser
1904  ****************************************************************************/
1905 static void uri_extract_value( char *psz_uri, char *psz_name,
1906                                char *psz_value, int i_value_max )
1907 {
1908     char *p;
1909
1910     p = strstr( psz_uri, psz_name );
1911     if( p )
1912     {
1913         int i_len;
1914
1915         p += strlen( psz_name );
1916         if( *p == '=' ) p++;
1917
1918         if( strchr( p, '&' ) )
1919         {
1920             i_len = strchr( p, '&' ) - p;
1921         }
1922         else
1923         {
1924             /* for POST method */
1925             if( strchr( p, '\n' ) )
1926             {
1927                 i_len = strchr( p, '\n' ) - p;
1928                 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
1929             }
1930             else
1931             {
1932                 i_len = strlen( p );
1933             }
1934         }
1935         i_len = __MIN( i_value_max - 1, i_len );
1936         if( i_len > 0 )
1937         {
1938             strncpy( psz_value, p, i_len );
1939             psz_value[i_len] = '\0';
1940         }
1941         else
1942         {
1943             strncpy( psz_value, "", i_value_max );
1944         }
1945     }
1946     else
1947     {
1948         strncpy( psz_value, "", i_value_max );
1949     }
1950 }
1951
1952 static void uri_decode_url_encoded( char *psz )
1953 {
1954     char *dup = strdup( psz );
1955     char *p = dup;
1956
1957     while( *p )
1958     {
1959         if( *p == '%' )
1960         {
1961             char val[3];
1962             p++;
1963             if( !*p )
1964             {
1965                 break;
1966             }
1967
1968             val[0] = *p++;
1969             val[1] = *p++;
1970             val[2] = '\0';
1971
1972             *psz++ = strtol( val, NULL, 16 );
1973         }
1974         else if( *p == '+' )
1975         {
1976             *psz++ = ' ';
1977             p++;
1978         }
1979         else
1980         {
1981             *psz++ = *p++;
1982         }
1983     }
1984     *psz++  ='\0';
1985     free( dup );
1986 }
1987
1988 /****************************************************************************
1989  * Light RPN evaluator
1990  ****************************************************************************/
1991 static void SSInit( rpn_stack_t *st )
1992 {
1993     st->i_stack = 0;
1994 }
1995
1996 static void SSClean( rpn_stack_t *st )
1997 {
1998     while( st->i_stack > 0 )
1999     {
2000         free( st->stack[--st->i_stack] );
2001     }
2002 }
2003
2004 static void SSPush( rpn_stack_t *st, char *s )
2005 {
2006     if( st->i_stack < STACK_MAX )
2007     {
2008         st->stack[st->i_stack++] = strdup( s );
2009     }
2010 }
2011
2012 static char * SSPop( rpn_stack_t *st )
2013 {
2014     if( st->i_stack <= 0 )
2015     {
2016         return strdup( "" );
2017     }
2018     else
2019     {
2020         return st->stack[--st->i_stack];
2021     }
2022 }
2023
2024 static int SSPopN( rpn_stack_t *st, mvar_t  *vars )
2025 {
2026     char *name;
2027     char *value;
2028
2029     char *end;
2030     int  i;
2031
2032     name = SSPop( st );
2033     i = strtol( name, &end, 0 );
2034     if( end == name )
2035     {
2036         value = mvar_GetValue( vars, name );
2037         i = atoi( value );
2038     }
2039     free( name );
2040
2041     return( i );
2042 }
2043
2044 static void SSPushN( rpn_stack_t *st, int i )
2045 {
2046     char v[512];
2047
2048     sprintf( v, "%d", i );
2049     SSPush( st, v );
2050 }
2051
2052 static void  EvaluateRPN( mvar_t  *vars, rpn_stack_t *st, char *exp )
2053 {
2054     for( ;; )
2055     {
2056         char s[100], *p;
2057
2058         /* skip spcae */
2059         while( *exp == ' ' )
2060         {
2061             exp++;
2062         }
2063
2064         if( *exp == '\'' )
2065         {
2066             /* extract string */
2067             p = &s[0];
2068             exp++;
2069             while( *exp && *exp != '\'' )
2070             {
2071                 *p++ = *exp++;
2072             }
2073             *p = '\0';
2074             exp++;
2075             SSPush( st, s );
2076             continue;
2077         }
2078
2079         /* extract token */
2080         p = strchr( exp, ' ' );
2081         if( !p )
2082         {
2083             strcpy( s, exp );
2084
2085             exp += strlen( exp );
2086         }
2087         else
2088         {
2089             int i = p -exp;
2090             strncpy( s, exp, i );
2091             s[i] = '\0';
2092
2093             exp = p + 1;
2094         }
2095
2096         if( *s == '\0' )
2097         {
2098             break;
2099         }
2100
2101         /* 1. Integer function */
2102         if( !strcmp( s, "!" ) )
2103         {
2104             SSPushN( st, ~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             SSPushN( st, SSPopN( st, vars ) + SSPopN( st, vars ) );
2121         }
2122         else if( !strcmp( s, "-" ) )
2123         {
2124             int i = SSPopN( st, vars );
2125             int j = SSPopN( st, vars );
2126             SSPushN( st, i - j );
2127         }
2128         else if( !strcmp( s, "*" ) )
2129         {
2130             SSPushN( st, SSPopN( st, vars ) * SSPopN( st, vars ) );
2131         }
2132         else if( !strcmp( s, "/" ) )
2133         {
2134             int i, j;
2135
2136             i = SSPopN( st, vars );
2137             j = SSPopN( st, vars );
2138
2139             SSPushN( st, j != 0 ? i / j : 0 );
2140         }
2141         else if( !strcmp( s, "%" ) )
2142         {
2143             int i, j;
2144
2145             i = SSPopN( st, vars );
2146             j = SSPopN( st, vars );
2147
2148             SSPushN( st, j != 0 ? i % j : 0 );
2149         }
2150         /* 2. integer tests */
2151         else if( !strcmp( s, "=" ) )
2152         {
2153             SSPushN( st, SSPopN( st, vars ) == SSPopN( st, vars ) ? -1 : 0 );
2154         }
2155         else if( !strcmp( s, "<" ) )
2156         {
2157             int i = SSPopN( st, vars );
2158             int j = SSPopN( st, vars );
2159
2160             SSPushN( st, i < j ? -1 : 0 );
2161         }
2162         else if( !strcmp( s, ">" ) )
2163         {
2164             int i = SSPopN( st, vars );
2165             int j = SSPopN( st, vars );
2166
2167             SSPushN( st, i > j ? -1 : 0 );
2168         }
2169         else if( !strcmp( s, "<=" ) )
2170         {
2171             int i = SSPopN( st, vars );
2172             int j = SSPopN( st, vars );
2173
2174             SSPushN( st, i <= j ? -1 : 0 );
2175         }
2176         else if( !strcmp( s, ">=" ) )
2177         {
2178             int i = SSPopN( st, vars );
2179             int j = SSPopN( st, vars );
2180
2181             SSPushN( st, i >= j ? -1 : 0 );
2182         }
2183         /* 3. string functions */
2184         else if( !strcmp( s, "strcat" ) )
2185         {
2186             char *s1 = SSPop( st );
2187             char *s2 = SSPop( st );
2188             char *str = malloc( strlen( s1 ) + strlen( s2 ) + 1 );
2189
2190             strcpy( str, s1 );
2191             strcat( str, s2 );
2192
2193             SSPush( st, str );
2194             free( s1 );
2195             free( s2 );
2196             free( str );
2197         }
2198         else if( !strcmp( s, "strcmp" ) )
2199         {
2200             char *s1 = SSPop( st );
2201             char *s2 = SSPop( st );
2202
2203             SSPushN( st, strcmp( s1, s2 ) );
2204             free( s1 );
2205             free( s2 );
2206         }
2207         else if( !strcmp( s, "strlen" ) )
2208         {
2209             char *str = SSPop( st );
2210
2211             SSPushN( st, strlen( str ) );
2212             free( str );
2213         }
2214         /* 4. stack functions */
2215         else if( !strcmp( s, "dup" ) )
2216         {
2217             char *str = SSPop( st );
2218             SSPush( st, str );
2219             SSPush( st, str );
2220             free( str );
2221         }
2222         else if( !strcmp( s, "drop" ) )
2223         {
2224             char *str = SSPop( st );
2225             free( str );
2226         }
2227         else if( !strcmp( s, "swap" ) )
2228         {
2229             char *s1 = SSPop( st );
2230             char *s2 = SSPop( st );
2231
2232             SSPush( st, s1 );
2233             SSPush( st, s2 );
2234             free( s1 );
2235             free( s2 );
2236         }
2237         else if( !strcmp( s, "flush" ) )
2238         {
2239             SSClean( st );
2240             SSInit( st );
2241         }
2242         else if( !strcmp( s, "store" ) )
2243         {
2244             char *name  = SSPop( st );
2245             char *value = SSPop( st );
2246
2247             mvar_PushNewVar( vars, name, value );
2248             free( name );
2249             free( value );
2250         }
2251         else if( !strcmp( s, "value" ) )
2252         {
2253             char *name  = SSPop( st );
2254             char *value = mvar_GetValue( vars, name );
2255
2256             SSPush( st, value );
2257
2258             free( name );
2259         }
2260         else if( !strcmp( s, "url_extract" ) )
2261         {
2262             char *url = mvar_GetValue( vars, "url_value" );
2263             char *name = SSPop( st );
2264             char value[512];
2265
2266             uri_extract_value( url, name, value, 512 );
2267             uri_decode_url_encoded( value );
2268             SSPush( st, value );
2269         }
2270         else
2271         {
2272             SSPush( st, s );
2273         }
2274     }
2275 }
2276
2277
2278