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