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