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