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