]> git.sesse.net Git - vlc/blob - modules/control/http/http.c
Export stats in http interface
[vlc] / modules / control / http / http.c
1 /*****************************************************************************
2  * http.c : HTTP/HTTPS Remote control interface
3  *****************************************************************************
4  * Copyright (C) 2001-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #include "http.h"
27
28 /*****************************************************************************
29  * Module descriptor
30  *****************************************************************************/
31 static int  Open ( vlc_object_t * );
32 static void Close( vlc_object_t * );
33
34 #define HOST_TEXT N_( "Host address" )
35 #define HOST_LONGTEXT N_( \
36     "You can set the address and port the http interface will bind to." )
37 #define SRC_TEXT N_( "Source directory" )
38 #define SRC_LONGTEXT N_( "Source directory" )
39 #define CHARSET_TEXT N_( "Charset" )
40 #define CHARSET_LONGTEXT N_( \
41         "Charset declared in Content-Type header (default UTF-8)." )
42 #define HANDLERS_TEXT N_( "Handlers" )
43 #define HANDLERS_LONGTEXT N_( \
44         "List of extensions and executable paths (for instance: " \
45         "php=/usr/bin/php,pl=/usr/bin/perl)." )
46 #define CERT_TEXT N_( "Certificate file" )
47 #define CERT_LONGTEXT N_( "HTTP interface x509 PEM certificate file " \
48                           "(enables SSL)" )
49 #define KEY_TEXT N_( "Private key file" )
50 #define KEY_LONGTEXT N_( "HTTP interface x509 PEM private key file" )
51 #define CA_TEXT N_( "Root CA file" )
52 #define CA_LONGTEXT N_( "HTTP interface x509 PEM trusted root CA " \
53                         "certificates file" )
54 #define CRL_TEXT N_( "CRL file" )
55 #define CRL_LONGTEXT N_( "HTTP interace Certificates Revocation List file" )
56
57 vlc_module_begin();
58     set_shortname( _("HTTP"));
59     set_description( _("HTTP remote control interface") );
60     set_category( CAT_INTERFACE );
61     set_subcategory( SUBCAT_INTERFACE_MAIN );
62         add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
63         add_string ( "http-src",  NULL, NULL, SRC_TEXT,  SRC_LONGTEXT,  VLC_TRUE );
64         add_string ( "http-charset", "UTF-8", NULL, CHARSET_TEXT, CHARSET_LONGTEXT, VLC_TRUE );
65 #if defined( HAVE_FORK ) || defined( WIN32 )
66         add_string ( "http-handlers", NULL, NULL, HANDLERS_TEXT, HANDLERS_LONGTEXT, VLC_TRUE );
67 #endif
68         set_section( N_("HTTP SSL" ), 0 );
69         add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
70         add_string ( "http-intf-key",  NULL, NULL, KEY_TEXT,  KEY_LONGTEXT,  VLC_TRUE );
71         add_string ( "http-intf-ca",   NULL, NULL, CA_TEXT,   CA_LONGTEXT,   VLC_TRUE );
72         add_string ( "http-intf-crl",  NULL, NULL, CRL_TEXT,  CRL_LONGTEXT,  VLC_TRUE );
73     set_capability( "interface", 0 );
74     set_callbacks( Open, Close );
75 vlc_module_end();
76
77
78 /*****************************************************************************
79  * Local prototypes
80  *****************************************************************************/
81 static void Run          ( intf_thread_t *p_intf );
82
83 /*****************************************************************************
84  * Local functions
85  *****************************************************************************/
86 #if !defined(__APPLE__) && !defined(SYS_BEOS) && !defined(WIN32)
87 static int DirectoryCheck( char *psz_dir )
88 {
89     DIR           *p_dir;
90
91 #ifdef HAVE_SYS_STAT_H
92     struct stat   stat_info;
93
94     if( utf8_stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
95     {
96         return VLC_EGENERIC;
97     }
98 #endif
99
100     if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
101     {
102         return VLC_EGENERIC;
103     }
104     closedir( p_dir );
105
106     return VLC_SUCCESS;
107 }
108 #endif
109
110
111 /*****************************************************************************
112  * Activate: initialize and create stuff
113  *****************************************************************************/
114 static int Open( vlc_object_t *p_this )
115 {
116     intf_thread_t *p_intf = (intf_thread_t*)p_this;
117     intf_sys_t    *p_sys;
118     char          *psz_address;
119     const char    *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
120                   *psz_crl = NULL;
121     int           i_port       = 0;
122     char          *psz_src;
123     char          psz_tmp[10];
124
125     psz_address = config_GetPsz( p_intf, "http-host" );
126     if( psz_address != NULL )
127     {
128         char *psz_parser = strchr( psz_address, ':' );
129         if( psz_parser )
130         {
131             *psz_parser++ = '\0';
132             i_port = atoi( psz_parser );
133         }
134     }
135     else
136         psz_address = strdup("");
137
138     p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
139     if( !p_intf->p_sys )
140     {
141         return( VLC_ENOMEM );
142     }
143     p_sys->p_playlist = NULL;
144     p_sys->p_input    = NULL;
145     p_sys->p_vlm      = NULL;
146     p_sys->psz_address = psz_address;
147     p_sys->i_port     = i_port;
148
149     /* determine Content-Type value for HTML pages */
150     psz_src = config_GetPsz( p_intf, "http-charset" );
151     if( psz_src == NULL || !*psz_src )
152     {
153         if( psz_src != NULL ) free( psz_src );
154         psz_src = strdup("UTF-8");
155     }
156
157     p_sys->psz_html_type = malloc( 20 + strlen( psz_src ) );
158     if( p_sys->psz_html_type == NULL )
159     {
160         free( p_sys->psz_address );
161         free( p_sys );
162         free( psz_src );
163         return VLC_ENOMEM ;
164     }
165     sprintf( p_sys->psz_html_type, "text/html; charset=%s", psz_src );
166     msg_Dbg( p_intf, "using charset=%s", psz_src );
167
168     if( strcmp( psz_src, "UTF-8" ) )
169     {
170         char psz_encoding[strlen( psz_src ) + sizeof( "//translit")];
171         sprintf( psz_encoding, "%s//translit", psz_src);
172
173         p_sys->iconv_from_utf8 = vlc_iconv_open( psz_encoding, "UTF-8" );
174         if( p_sys->iconv_from_utf8 == (vlc_iconv_t)-1 )
175             msg_Warn( p_intf, "unable to perform charset conversion to %s",
176                       psz_encoding );
177         else
178         {
179             p_sys->iconv_to_utf8 = vlc_iconv_open( "UTF-8", psz_src );
180             if( p_sys->iconv_to_utf8 == (vlc_iconv_t)-1 )
181                 msg_Warn( p_intf,
182                           "unable to perform charset conversion from %s",
183                           psz_src );
184         }
185     }
186     else
187     {
188         p_sys->iconv_from_utf8 = p_sys->iconv_to_utf8 = (vlc_iconv_t)-1;
189     }
190
191     p_sys->psz_charset = psz_src;
192     psz_src = NULL;
193
194     /* determine file handler associations */
195     p_sys->i_handlers = 0;
196     p_sys->pp_handlers = NULL;
197 #if defined( HAVE_FORK ) || defined( WIN32 )
198     psz_src = config_GetPsz( p_intf, "http-handlers" );
199     if( psz_src != NULL && *psz_src )
200     {
201         char *p = psz_src;
202         while( p != NULL )
203         {
204             http_association_t *p_handler;
205             char *psz_ext = p;
206             char *psz_program, *psz_options;
207             p = strchr( p, '=' );
208             if( p == NULL ) break;
209             *p++ = '\0';
210             psz_program = p;
211             p = strchr( p, ',' );
212             if( p != NULL )
213                 *p++ = '\0';
214
215             p_handler = malloc( sizeof( http_association_t ) );
216             p_handler->psz_ext = strdup( psz_ext );
217             psz_options = E_(FirstWord)( psz_program, psz_program );
218             p_handler->i_argc = 0;
219             p_handler->ppsz_argv = NULL;
220             TAB_APPEND( p_handler->i_argc, p_handler->ppsz_argv,
221                         strdup( psz_program ) );
222             while( psz_options != NULL && *psz_options )
223             {
224                 char *psz_next = E_(FirstWord)( psz_options, psz_options );
225                 TAB_APPEND( p_handler->i_argc, p_handler->ppsz_argv,
226                             strdup( psz_options ) );
227                 psz_options = psz_next;
228             }
229             /* NULL will be appended later on */
230
231             TAB_APPEND( p_sys->i_handlers, p_sys->pp_handlers, p_handler );
232         }
233     }
234     if( psz_src != NULL )
235         free( psz_src );
236 #endif
237
238     /* determine SSL configuration */
239     psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
240     if ( psz_cert != NULL )
241     {
242         msg_Dbg( p_intf, "enabling TLS for HTTP interface (cert file: %s)",
243                  psz_cert );
244         psz_key = config_GetPsz( p_intf, "http-intf-key" );
245         psz_ca = config_GetPsz( p_intf, "http-intf-ca" );
246         psz_crl = config_GetPsz( p_intf, "http-intf-crl" );
247
248         if( i_port <= 0 )
249             i_port = 8443;
250     }
251     else
252     {
253         if( i_port <= 0 )
254             i_port= 8080;
255     }
256
257     /* Ugly hack to allow to run several HTTP servers on different ports. */
258     sprintf( psz_tmp, ":%d", i_port + 1 );
259     config_PutPsz( p_intf, "http-host", psz_tmp );
260
261     msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
262
263     p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_intf), psz_address,
264                                             i_port, psz_cert, psz_key, psz_ca,
265                                             psz_crl );
266     if( p_sys->p_httpd_host == NULL )
267     {
268         msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
269         free( p_sys->psz_html_type );
270         free( p_sys->psz_address );
271         free( p_sys );
272         return VLC_EGENERIC;
273     }
274
275     p_sys->i_files  = 0;
276     p_sys->pp_files = NULL;
277
278 #if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
279     if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
280     {
281         char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
282         psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
283         if( !psz_src ) return VLC_ENOMEM;
284 #if defined(WIN32)
285         sprintf( psz_src, "%s/http", psz_vlcpath );
286 #else
287         sprintf( psz_src, "%s/share/http", psz_vlcpath );
288 #endif
289     }
290 #else
291     psz_src = config_GetPsz( p_intf, "http-src" );
292
293     if( !psz_src || *psz_src == '\0' )
294     {
295         if( !DirectoryCheck( "share/http" ) )
296         {
297             psz_src = strdup( "share/http" );
298         }
299         else if( !DirectoryCheck( DATA_PATH "/http" ) )
300         {
301             psz_src = strdup( DATA_PATH "/http" );
302         }
303     }
304 #endif
305
306     if( !psz_src || *psz_src == '\0' )
307     {
308         msg_Err( p_intf, "invalid src dir" );
309         goto failed;
310     }
311
312     /* remove trainling \ or / */
313     if( psz_src[strlen( psz_src ) - 1] == '\\' ||
314         psz_src[strlen( psz_src ) - 1] == '/' )
315     {
316         psz_src[strlen( psz_src ) - 1] = '\0';
317     }
318
319     E_(ParseDirectory)( p_intf, psz_src, psz_src );
320
321
322     if( p_sys->i_files <= 0 )
323     {
324         msg_Err( p_intf, "cannot find any files (%s)", psz_src );
325         goto failed;
326     }
327     p_intf->pf_run = Run;
328     free( psz_src );
329
330     return VLC_SUCCESS;
331
332 failed:
333     if( psz_src ) free( psz_src );
334     if( p_sys->pp_files )
335     {
336         free( p_sys->pp_files );
337     }
338     httpd_HostDelete( p_sys->p_httpd_host );
339     free( p_sys->psz_address );
340     free( p_sys->psz_html_type );
341     if( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
342         vlc_iconv_close( p_sys->iconv_from_utf8 );
343     if( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
344         vlc_iconv_close( p_sys->iconv_to_utf8 );
345     free( p_sys );
346     return VLC_EGENERIC;
347 }
348
349 /*****************************************************************************
350  * Close: destroy interface
351  *****************************************************************************/
352 static void Close ( vlc_object_t *p_this )
353 {
354     intf_thread_t *p_intf = (intf_thread_t *)p_this;
355     intf_sys_t    *p_sys = p_intf->p_sys;
356
357     int i;
358
359     if( p_sys->p_vlm )
360     {
361         vlm_Delete( p_sys->p_vlm );
362     }
363     for( i = 0; i < p_sys->i_files; i++ )
364     {
365         if( p_sys->pp_files[i]->b_handler )
366             httpd_HandlerDelete( ((httpd_handler_sys_t *)p_sys->pp_files[i])->p_handler );
367         else
368             httpd_FileDelete( p_sys->pp_files[i]->p_file );
369         if( p_sys->pp_files[i]->p_redir )
370             httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
371         if( p_sys->pp_files[i]->p_redir2 )
372             httpd_RedirectDelete( p_sys->pp_files[i]->p_redir2 );
373
374         free( p_sys->pp_files[i]->file );
375         free( p_sys->pp_files[i]->name );
376         free( p_sys->pp_files[i] );
377     }
378     if( p_sys->pp_files )
379     {
380         free( p_sys->pp_files );
381     }
382     for( i = 0; i < p_sys->i_handlers; i++ )
383     {
384         http_association_t *p_handler = p_sys->pp_handlers[i];
385         int j;
386         free( p_handler->psz_ext );
387         for( j = 0; j < p_handler->i_argc; j++ )
388             free( p_handler->ppsz_argv[j] );
389         if( p_handler->i_argc )
390             free( p_handler->ppsz_argv );
391         free( p_handler );
392     }
393     if( p_sys->i_handlers )
394         free( p_sys->pp_handlers );
395     httpd_HostDelete( p_sys->p_httpd_host );
396     free( p_sys->psz_address );
397     free( p_sys->psz_html_type );
398
399     if( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
400         vlc_iconv_close( p_sys->iconv_from_utf8 );
401     if( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
402         vlc_iconv_close( p_sys->iconv_to_utf8 );
403     free( p_sys );
404 }
405
406 /*****************************************************************************
407  * Run: http interface thread
408  *****************************************************************************/
409 static void Run( intf_thread_t *p_intf )
410 {
411     intf_sys_t     *p_sys = p_intf->p_sys;
412
413     while( !p_intf->b_die )
414     {
415         /* get the playlist */
416         if( p_sys->p_playlist == NULL )
417         {
418             p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
419         }
420
421         /* Manage the input part */
422         if( p_sys->p_input == NULL )
423         {
424             if( p_sys->p_playlist )
425             {
426                 p_sys->p_input = p_sys->p_playlist->p_input;
427             }
428         }
429         else if( p_sys->p_input->b_dead || p_sys->p_input->b_die )
430         {
431             p_sys->p_input = NULL;
432         }
433
434
435         /* Wait a bit */
436         msleep( INTF_IDLE_SLEEP );
437     }
438
439     if( p_sys->p_input )
440     {
441         vlc_object_release( p_sys->p_input );
442         p_sys->p_input = NULL;
443     }
444
445     if( p_sys->p_playlist )
446     {
447         vlc_object_release( p_sys->p_playlist );
448         p_sys->p_playlist = NULL;
449     }
450 }
451
452 /****************************************************************************
453  * HttpCallback:
454  ****************************************************************************
455  * a file with b_html is parsed and all "macro" replaced
456  ****************************************************************************/
457 static void Callback404( httpd_file_sys_t *p_args, char **pp_data,
458                          int *pi_data )
459 {
460     char *p = *pp_data = malloc( 10240 );
461     if( !p )
462     {
463         return;
464     }
465     p += sprintf( p, "<html>\n" );
466     p += sprintf( p, "<head>\n" );
467     p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
468     p += sprintf( p, "</head>\n" );
469     p += sprintf( p, "<body>\n" );
470     p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
471     p += sprintf( p, "<hr />\n" );
472     p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
473     p += sprintf( p, "</body>\n" );
474     p += sprintf( p, "</html>\n" );
475
476     *pi_data = strlen( *pp_data );
477 }
478
479 static void ParseExecute( httpd_file_sys_t *p_args, char *p_buffer,
480                           int i_buffer, char *p_request,
481                           char **pp_data, int *pi_data )
482 {
483     int i_request = p_request != NULL ? strlen( p_request ) : 0;
484     char *dst;
485     vlc_value_t val;
486     char position[4]; /* percentage */
487     char time[12]; /* in seconds */
488     char length[12]; /* in seconds */
489     audio_volume_t i_volume;
490     char volume[5];
491     char state[8];
492     char stats[20];
493
494 #define p_sys p_args->p_intf->p_sys
495     if( p_sys->p_input )
496     {
497         var_Get( p_sys->p_input, "position", &val);
498         sprintf( position, "%d" , (int)((val.f_float) * 100.0));
499         var_Get( p_sys->p_input, "time", &val);
500         sprintf( time, "%d" , (int)(val.i_time / 1000000) );
501         var_Get( p_sys->p_input, "length", &val);
502         sprintf( length, "%d" , (int)(val.i_time / 1000000) );
503
504         var_Get( p_sys->p_input, "state", &val );
505         if( val.i_int == PLAYING_S )
506         {
507             sprintf( state, "playing" );
508         }
509         else if( val.i_int == PAUSE_S )
510         {
511             sprintf( state, "paused" );
512         }
513         else
514         {
515             sprintf( state, "stop" );
516         }
517     }
518     else
519     {
520         sprintf( position, "%d", 0 );
521         sprintf( time, "%d", 0 );
522         sprintf( length, "%d", 0 );
523         sprintf( state, "stop" );
524     }
525 #undef p_sys
526
527     aout_VolumeGet( p_args->p_intf, &i_volume );
528     sprintf( volume, "%d", (int)i_volume );
529
530     p_args->vars = E_(mvar_New)( "variables", "" );
531     E_(mvar_AppendNewVar)( p_args->vars, "url_param",
532                            i_request > 0 ? "1" : "0" );
533     E_(mvar_AppendNewVar)( p_args->vars, "url_value", p_request );
534     E_(mvar_AppendNewVar)( p_args->vars, "version", VLC_Version() );
535     E_(mvar_AppendNewVar)( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
536     E_(mvar_AppendNewVar)( p_args->vars, "vlc_compile_by", VLC_CompileBy() );
537     E_(mvar_AppendNewVar)( p_args->vars, "vlc_compile_host",
538                            VLC_CompileHost() );
539     E_(mvar_AppendNewVar)( p_args->vars, "vlc_compile_domain",
540                            VLC_CompileDomain() );
541     E_(mvar_AppendNewVar)( p_args->vars, "vlc_compiler", VLC_Compiler() );
542 #ifndef HAVE_SHARED_LIBVLC
543     E_(mvar_AppendNewVar)( p_args->vars, "vlc_changeset", VLC_Changeset() );
544 #endif
545     E_(mvar_AppendNewVar)( p_args->vars, "stream_position", position );
546     E_(mvar_AppendNewVar)( p_args->vars, "stream_time", time );
547     E_(mvar_AppendNewVar)( p_args->vars, "stream_length", length );
548     E_(mvar_AppendNewVar)( p_args->vars, "volume", volume );
549     E_(mvar_AppendNewVar)( p_args->vars, "stream_state", state );
550     E_(mvar_AppendNewVar)( p_args->vars, "charset", ((intf_sys_t *)p_args->p_intf->p_sys)->psz_charset );
551
552     /* Stats */
553 #define p_sys p_args->p_intf->p_sys
554     if( p_sys->p_input )
555     {
556         input_item_t *p_item = p_sys->p_input->input.p_item;
557         if( p_item )
558         {
559             vlc_mutex_lock( &p_item->p_stats->lock );
560 #define STATS_INT( n ) sprintf( stats, "%d", p_item->p_stats->i_ ## n ); \
561                        E_(mvar_AppendNewVar)( p_args->vars, #n, stats );
562 #define STATS_FLOAT( n ) sprintf( stats, "%f", p_item->p_stats->f_ ## n ); \
563                        E_(mvar_AppendNewVar)( p_args->vars, #n, stats );
564             STATS_INT( read_bytes )
565             STATS_FLOAT( input_bitrate )
566             STATS_INT( demux_read_bytes )
567             STATS_FLOAT( demux_bitrate )
568             STATS_INT( decoded_video )
569             STATS_INT( displayed_pictures )
570             STATS_INT( lost_pictures )
571             STATS_INT( decoded_audio )
572             STATS_INT( played_abuffers )
573             STATS_INT( lost_abuffers )
574             STATS_INT( sent_packets )
575             STATS_INT( sent_bytes )
576             STATS_FLOAT( send_bitrate )
577 #undef STATS_INT
578 #undef STATS_FLOAT
579             vlc_mutex_unlock( &p_item->p_stats->lock );
580         }
581     }
582 #undef p_sys
583
584     E_(SSInit)( &p_args->stack );
585
586     /* allocate output */
587     *pi_data = i_buffer + 1000;
588     dst = *pp_data = malloc( *pi_data );
589
590     /* we parse executing all  <vlc /> macros */
591     E_(Execute)( p_args, p_request, i_request, pp_data, pi_data, &dst,
592                  &p_buffer[0], &p_buffer[i_buffer] );
593
594     *dst     = '\0';
595     *pi_data = dst - *pp_data;
596
597     E_(SSClean)( &p_args->stack );
598     E_(mvar_Delete)( p_args->vars );
599 }
600
601 int  E_(HttpCallback)( httpd_file_sys_t *p_args,
602                        httpd_file_t *p_file,
603                        uint8_t *_p_request,
604                        uint8_t **_pp_data, int *pi_data )
605 {
606     char *p_request = (char *)_p_request;
607     char **pp_data = (char **)_pp_data;
608     FILE *f;
609
610     /* FIXME: do we need character encoding translation here? */
611     if( ( f = fopen( p_args->file, "r" ) ) == NULL )
612     {
613         Callback404( p_args, pp_data, pi_data );
614         return VLC_SUCCESS;
615     }
616
617     if( !p_args->b_html )
618     {
619         E_(FileLoad)( f, pp_data, pi_data );
620     }
621     else
622     {
623         int  i_buffer;
624         char *p_buffer;
625
626         /* first we load in a temporary buffer */
627         E_(FileLoad)( f, &p_buffer, &i_buffer );
628
629         ParseExecute( p_args, p_buffer, i_buffer, p_request, pp_data, pi_data );
630
631         free( p_buffer );
632     }
633
634     fclose( f );
635
636     return VLC_SUCCESS;
637 }
638
639 /****************************************************************************
640  * HandlerCallback:
641  ****************************************************************************
642  * call the external handler and parse vlc macros if Content-Type is HTML
643  ****************************************************************************/
644 int  E_(HandlerCallback)( httpd_handler_sys_t *p_args,
645                           httpd_handler_t *p_handler, char *_p_url,
646                           uint8_t *_p_request, int i_type,
647                           uint8_t *_p_in, int i_in,
648                           char *psz_remote_addr, char *psz_remote_host,
649                           uint8_t **_pp_data, int *pi_data )
650 {
651     char *p_url = (char *)_p_url;
652     char *p_request = (char *)_p_request;
653     char **pp_data = (char **)_pp_data;
654     char *p_in = (char *)p_in;
655     int i_request = p_request != NULL ? strlen( p_request ) : 0;
656     char *p;
657     int i_env = 0;
658     char **ppsz_env = NULL;
659     char *psz_tmp;
660     char sep;
661     int  i_buffer;
662     char *p_buffer;
663     char *psz_cwd, *psz_file = NULL;
664     int i_ret;
665
666 #ifdef WIN32
667     sep = '\\';
668 #else
669     sep = '/';
670 #endif
671
672     /* Create environment for the CGI */
673     TAB_APPEND( i_env, ppsz_env, strdup("GATEWAY_INTERFACE=CGI/1.1") );
674     TAB_APPEND( i_env, ppsz_env, strdup("SERVER_PROTOCOL=HTTP/1.1") );
675     TAB_APPEND( i_env, ppsz_env, strdup("SERVER_SOFTWARE=" COPYRIGHT_MESSAGE) );
676
677     switch( i_type )
678     {
679     case HTTPD_MSG_GET:
680         TAB_APPEND( i_env, ppsz_env, strdup("REQUEST_METHOD=GET") );
681         break;
682     case HTTPD_MSG_POST:
683         TAB_APPEND( i_env, ppsz_env, strdup("REQUEST_METHOD=POST") );
684         break;
685     case HTTPD_MSG_HEAD:
686         TAB_APPEND( i_env, ppsz_env, strdup("REQUEST_METHOD=HEAD") );
687         break;
688     default:
689         break;
690     }
691
692     if( i_request )
693     {
694         psz_tmp = malloc( sizeof("QUERY_STRING=") + i_request );
695         sprintf( psz_tmp, "QUERY_STRING=%s", p_request );
696         TAB_APPEND( i_env, ppsz_env, psz_tmp );
697
698         psz_tmp = malloc( sizeof("REQUEST_URI=?") + strlen(p_url)
699                            + i_request );
700         sprintf( psz_tmp, "REQUEST_URI=%s?%s", p_url, p_request );
701         TAB_APPEND( i_env, ppsz_env, psz_tmp );
702     }
703     else
704     {
705         psz_tmp = malloc( sizeof("REQUEST_URI=") + strlen(p_url) );
706         sprintf( psz_tmp, "REQUEST_URI=%s", p_url );
707         TAB_APPEND( i_env, ppsz_env, psz_tmp );
708     }
709
710     psz_tmp = malloc( sizeof("SCRIPT_NAME=") + strlen(p_url) );
711     sprintf( psz_tmp, "SCRIPT_NAME=%s", p_url );
712     TAB_APPEND( i_env, ppsz_env, psz_tmp );
713
714 #define p_sys p_args->file.p_intf->p_sys
715     psz_tmp = malloc( sizeof("SERVER_NAME=") + strlen(p_sys->psz_address) );
716     sprintf( psz_tmp, "SERVER_NAME=%s", p_sys->psz_address );
717     TAB_APPEND( i_env, ppsz_env, psz_tmp );
718
719     psz_tmp = malloc( sizeof("SERVER_PORT=") + 5 );
720     sprintf( psz_tmp, "SERVER_PORT=%u", p_sys->i_port );
721     TAB_APPEND( i_env, ppsz_env, psz_tmp );
722 #undef p_sys
723
724     p = getenv( "PATH" );
725     if( p != NULL )
726     {
727         psz_tmp = malloc( sizeof("PATH=") + strlen(p) );
728         sprintf( psz_tmp, "PATH=%s", p );
729         TAB_APPEND( i_env, ppsz_env, psz_tmp );
730     }
731
732 #ifdef WIN32
733     p = getenv( "windir" );
734     if( p != NULL )
735     {
736         psz_tmp = malloc( sizeof("SYSTEMROOT=") + strlen(p) );
737         sprintf( psz_tmp, "SYSTEMROOT=%s", p );
738         TAB_APPEND( i_env, ppsz_env, psz_tmp );
739     }
740 #endif
741
742     if( psz_remote_addr != NULL && *psz_remote_addr )
743     {
744         psz_tmp = malloc( sizeof("REMOTE_ADDR=") + strlen(psz_remote_addr) );
745         sprintf( psz_tmp, "REMOTE_ADDR=%s", psz_remote_addr );
746         TAB_APPEND( i_env, ppsz_env, psz_tmp );
747     }
748
749     if( psz_remote_host != NULL && *psz_remote_host )
750     {
751         psz_tmp = malloc( sizeof("REMOTE_HOST=") + strlen(psz_remote_host) );
752         sprintf( psz_tmp, "REMOTE_HOST=%s", psz_remote_host );
753         TAB_APPEND( i_env, ppsz_env, psz_tmp );
754     }
755
756     if( i_in )
757     {
758         p = p_in;
759         for ( ; ; )
760         {
761             if( !strncasecmp( p, "Content-Type: ", strlen("Content-Type: ") ) )
762             {
763                 char *end = strchr( p, '\r' );
764                 if( end == NULL )
765                     break;
766                 *end = '\0';
767                 psz_tmp = malloc( sizeof("CONTENT_TYPE=") + strlen(p) );
768                 sprintf( psz_tmp, "CONTENT_TYPE=%s", p );
769                 TAB_APPEND( i_env, ppsz_env, psz_tmp );
770                 *end = '\r';
771             }
772             if( !strncasecmp( p, "Content-Length: ",
773                               strlen("Content-Length: ") ) )
774             {
775                 char *end = strchr( p, '\r' );
776                 if( end == NULL )
777                     break;
778                 *end = '\0';
779                 psz_tmp = malloc( sizeof("CONTENT_LENGTH=") + strlen(p) );
780                 sprintf( psz_tmp, "CONTENT_LENGTH=%s", p );
781                 TAB_APPEND( i_env, ppsz_env, psz_tmp );
782                 *end = '\r';
783             }
784
785             p = strchr( p, '\n' );
786             if( p == NULL || p[1] == '\r' )
787             {
788                 p = NULL;
789                 break;
790             }
791             p++;
792         }
793     }
794
795     psz_file = strrchr( p_args->file.file, sep );
796     if( psz_file != NULL )
797     {
798         psz_file++;
799         psz_tmp = malloc( sizeof("SCRIPT_FILENAME=") + strlen(psz_file) );
800         sprintf( psz_tmp, "SCRIPT_FILENAME=%s", psz_file );
801         TAB_APPEND( i_env, ppsz_env, psz_tmp );
802
803         TAB_APPEND( p_args->p_association->i_argc,
804                     p_args->p_association->ppsz_argv, psz_file );
805     }
806
807     TAB_APPEND( i_env, ppsz_env, NULL );
808
809     TAB_APPEND( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
810                 NULL );
811
812     psz_tmp = strdup( p_args->file.file );
813     p = strrchr( psz_tmp, sep );
814     if( p != NULL )
815     {
816         *p = '\0';
817         psz_cwd = psz_tmp;
818     }
819     else
820     {
821         free( psz_tmp );
822         psz_cwd = NULL;
823     }
824
825     i_ret = vlc_execve( p_args->file.p_intf, p_args->p_association->i_argc,
826                         p_args->p_association->ppsz_argv, ppsz_env, psz_cwd,
827                         (char *)p_in, i_in, &p_buffer, &i_buffer );
828     TAB_REMOVE( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
829                 NULL );
830     TAB_REMOVE( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
831                 psz_file );
832     if( psz_cwd != NULL )
833         free( psz_cwd );
834     while( i_env )
835         TAB_REMOVE( i_env, ppsz_env, ppsz_env[0] );
836
837     if( i_ret == -1 )
838     {
839         Callback404( (httpd_file_sys_t *)p_args, pp_data, pi_data );
840         return VLC_SUCCESS;
841     }
842     p = p_buffer;
843     while( strncasecmp( p, "Content-Type: text/html",
844                         strlen("Content-Type: text/html") ) )
845     {
846         p = strchr( p, '\n' );
847         if( p == NULL || p[1] == '\r' )
848         {
849             p = NULL;
850             break;
851         }
852         p++;
853     }
854
855     if( p == NULL )
856     {
857         *pp_data = p_buffer;
858         *pi_data = i_buffer;
859     }
860     else
861     {
862         ParseExecute( (httpd_file_sys_t *)p_args, p_buffer, i_buffer,
863                       p_request, pp_data, pi_data );
864
865         free( p_buffer );
866     }
867
868     return VLC_SUCCESS;
869 }