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