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