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