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