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