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