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