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