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