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