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