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