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