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