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