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