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