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