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