1 /*****************************************************************************
2 * http.c : HTTP/HTTPS Remote control interface
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Laurent Aimar <fenrir@via.ecp.fr>
9 * Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
28 /*****************************************************************************
30 *****************************************************************************/
31 static int Open ( vlc_object_t * );
32 static void Close( vlc_object_t * );
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 " \
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." )
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 );
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 );
86 /*****************************************************************************
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 );
97 /*****************************************************************************
99 *****************************************************************************/
100 #if !defined(__APPLE__) && !defined(SYS_BEOS) && !defined(WIN32)
101 static int DirectoryCheck( const char *psz_dir )
105 #ifdef HAVE_SYS_STAT_H
106 struct stat stat_info;
108 if( ( utf8_stat( psz_dir, &stat_info ) == -1 )
109 || !S_ISDIR( stat_info.st_mode ) )
115 if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
126 /*****************************************************************************
127 * Activate: initialize and create stuff
128 *****************************************************************************/
129 static int Open( vlc_object_t *p_this )
131 intf_thread_t *p_intf = (intf_thread_t*)p_this;
134 const char *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
139 psz_address = var_GetNonEmptyString(p_intf->p_libvlc, "http-host");
140 if( psz_address != NULL )
142 char *psz_parser = strchr( psz_address, ':' );
145 *psz_parser++ = '\0';
146 i_port = atoi( psz_parser );
150 psz_address = strdup("");
152 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
155 return( VLC_ENOMEM );
158 p_sys->p_playlist = pl_Yield( p_this );
159 p_sys->p_input = NULL;
161 p_sys->psz_address = psz_address;
162 p_sys->i_port = i_port;
163 p_sys->p_art_handler = NULL;
165 /* determine Content-Type value for HTML pages */
166 psz_src = config_GetPsz( p_intf, "http-charset" );
167 if( psz_src == NULL || !*psz_src )
169 if( psz_src != NULL ) free( psz_src );
170 psz_src = strdup("UTF-8");
173 p_sys->psz_html_type = malloc( 20 + strlen( psz_src ) );
174 if( p_sys->psz_html_type == NULL )
176 pl_Release( p_this );
177 free( p_sys->psz_address );
182 sprintf( p_sys->psz_html_type, "text/html; charset=%s", psz_src );
183 msg_Dbg( p_intf, "using charset=%s", psz_src );
185 if( strcmp( psz_src, "UTF-8" ) )
187 char psz_encoding[strlen( psz_src ) + sizeof( "//translit")];
188 sprintf( psz_encoding, "%s//translit", psz_src);
190 p_sys->iconv_from_utf8 = vlc_iconv_open( psz_encoding, "UTF-8" );
191 if( p_sys->iconv_from_utf8 == (vlc_iconv_t)-1 )
192 msg_Warn( p_intf, "unable to perform charset conversion to %s",
196 p_sys->iconv_to_utf8 = vlc_iconv_open( "UTF-8", psz_src );
197 if( p_sys->iconv_to_utf8 == (vlc_iconv_t)-1 )
199 "unable to perform charset conversion from %s",
205 p_sys->iconv_from_utf8 = p_sys->iconv_to_utf8 = (vlc_iconv_t)-1;
208 p_sys->psz_charset = psz_src;
211 /* determine file handler associations */
212 p_sys->i_handlers = 0;
213 p_sys->pp_handlers = NULL;
214 #if defined( HAVE_FORK ) || defined( WIN32 )
215 psz_src = config_GetPsz( p_intf, "http-handlers" );
216 if( psz_src != NULL && *psz_src )
221 http_association_t *p_handler;
223 char *psz_program, *psz_options;
224 p = strchr( p, '=' );
225 if( p == NULL ) break;
228 p = strchr( p, ',' );
232 p_handler = malloc( sizeof( http_association_t ) );
233 p_handler->psz_ext = strdup( psz_ext );
234 psz_options = E_(FirstWord)( psz_program, psz_program );
235 p_handler->i_argc = 0;
236 p_handler->ppsz_argv = NULL;
237 TAB_APPEND( p_handler->i_argc, p_handler->ppsz_argv,
238 strdup( psz_program ) );
239 while( psz_options != NULL && *psz_options )
241 char *psz_next = E_(FirstWord)( psz_options, psz_options );
242 TAB_APPEND( p_handler->i_argc, p_handler->ppsz_argv,
243 strdup( psz_options ) );
244 psz_options = psz_next;
246 /* NULL will be appended later on */
248 TAB_APPEND( p_sys->i_handlers, p_sys->pp_handlers, p_handler );
251 if( psz_src != NULL )
255 /* determine SSL configuration */
256 psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
257 if ( psz_cert != NULL )
259 msg_Dbg( p_intf, "enabling TLS for HTTP interface (cert file: %s)",
261 psz_key = var_GetNonEmptyString( p_intf, "http-intf-key" );
262 psz_ca = var_GetNonEmptyString( p_intf, "http-intf-ca" );
263 psz_crl = var_GetNonEmptyString( p_intf, "http-intf-crl" );
274 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
276 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_intf), psz_address,
277 i_port, psz_cert, psz_key, psz_ca,
279 if( p_sys->p_httpd_host == NULL )
281 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
282 pl_Release( p_this );
283 free( p_sys->psz_html_type );
284 free( p_sys->psz_address );
290 char psz_tmp[NI_MAXHOST + 6];
292 /* Ugly hack to run several HTTP servers on different ports */
293 snprintf( psz_tmp, sizeof (psz_tmp), "%s:%d", psz_address, i_port + 1 );
294 var_Create(p_intf->p_libvlc, "http-host", VLC_VAR_STRING );
295 var_SetString( p_intf->p_libvlc, "http-host", psz_tmp );
299 p_sys->pp_files = NULL;
301 #if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
302 if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
304 const char * psz_vlcpath = config_GetDataDir();
305 psz_src = malloc( strlen(psz_vlcpath) + strlen("/http" ) + 1 );
306 if( !psz_src ) return VLC_ENOMEM;
307 sprintf( psz_src, "%s/http", psz_vlcpath );
310 psz_src = config_GetPsz( p_intf, "http-src" );
312 if( ( psz_src == NULL ) || ( *psz_src == '\0' ) )
314 static char const* ppsz_paths[] = {
322 if( psz_src != NULL )
328 for( i = 0; ppsz_paths[i] != NULL; i++ )
329 if( !DirectoryCheck( ppsz_paths[i] ) )
331 psz_src = strdup( ppsz_paths[i] );
337 if( !psz_src || *psz_src == '\0' )
339 msg_Err( p_intf, "invalid web interface source directory" );
343 /* remove trainling \ or / */
344 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
345 psz_src[strlen( psz_src ) - 1] == '/' )
347 psz_src[strlen( psz_src ) - 1] = '\0';
350 E_(ParseDirectory)( p_intf, psz_src, psz_src );
351 if( p_sys->i_files <= 0 )
353 msg_Err( p_intf, "cannot find any file in directory %s", psz_src );
357 p_intf->pf_run = Run;
360 if( config_GetInt( p_intf, "http-album-art" ) )
362 /* FIXME: we're leaking h */
363 httpd_handler_sys_t *h = malloc( sizeof( httpd_handler_sys_t ) );
366 msg_Err( p_intf, "not enough memory to allocate album art handler" );
369 h->file.p_intf = p_intf;
372 /* TODO: use ACL and login/password stuff here too */
373 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
374 "/art", NULL, NULL, NULL,
375 E_(ArtCallback), h );
376 p_sys->p_art_handler = h->p_handler;
383 free( p_sys->pp_files );
384 httpd_HostDelete( p_sys->p_httpd_host );
385 free( p_sys->psz_address );
386 free( p_sys->psz_html_type );
387 if( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
388 vlc_iconv_close( p_sys->iconv_from_utf8 );
389 if( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
390 vlc_iconv_close( p_sys->iconv_to_utf8 );
392 pl_Release( p_this );
396 /*****************************************************************************
397 * Close: destroy interface
398 *****************************************************************************/
399 static void Close ( vlc_object_t *p_this )
401 intf_thread_t *p_intf = (intf_thread_t *)p_this;
402 intf_sys_t *p_sys = p_intf->p_sys;
408 vlm_Delete( p_sys->p_vlm );
410 for( i = 0; i < p_sys->i_files; i++ )
412 if( p_sys->pp_files[i]->b_handler )
413 httpd_HandlerDelete( ((httpd_handler_sys_t *)p_sys->pp_files[i])->p_handler );
415 httpd_FileDelete( p_sys->pp_files[i]->p_file );
416 if( p_sys->pp_files[i]->p_redir )
417 httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
418 if( p_sys->pp_files[i]->p_redir2 )
419 httpd_RedirectDelete( p_sys->pp_files[i]->p_redir2 );
421 free( p_sys->pp_files[i]->file );
422 free( p_sys->pp_files[i]->name );
423 free( p_sys->pp_files[i] );
425 if( p_sys->pp_files )
427 free( p_sys->pp_files );
429 for( i = 0; i < p_sys->i_handlers; i++ )
431 http_association_t *p_handler = p_sys->pp_handlers[i];
433 free( p_handler->psz_ext );
434 for( j = 0; j < p_handler->i_argc; j++ )
435 free( p_handler->ppsz_argv[j] );
436 if( p_handler->i_argc )
437 free( p_handler->ppsz_argv );
440 if( p_sys->i_handlers )
441 free( p_sys->pp_handlers );
442 if( p_sys->p_art_handler )
443 httpd_HandlerDelete( p_sys->p_art_handler );
444 httpd_HostDelete( p_sys->p_httpd_host );
445 free( p_sys->psz_address );
446 free( p_sys->psz_html_type );
448 if( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
449 vlc_iconv_close( p_sys->iconv_from_utf8 );
450 if( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
451 vlc_iconv_close( p_sys->iconv_to_utf8 );
453 pl_Release( p_this );
456 /*****************************************************************************
457 * Run: http interface thread
458 *****************************************************************************/
459 static void Run( intf_thread_t *p_intf )
461 while( !intf_ShouldDie( p_intf ) )
464 msleep( INTF_IDLE_SLEEP );
468 /****************************************************************************
470 ****************************************************************************
471 * a file with b_html is parsed and all "macro" replaced
472 ****************************************************************************/
473 static void Callback404( httpd_file_sys_t *p_args, char **pp_data,
476 char *p = *pp_data = malloc( 10240 );
481 p += sprintf( p, "Content-Type: text/html\n" );
482 p += sprintf( p, "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" );
483 p += sprintf( p, "<head>\n" );
484 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
485 p += sprintf( p, "</head>\n" );
486 p += sprintf( p, "<body>\n" );
487 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
488 p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
489 p += sprintf( p, "</body>\n" );
490 p += sprintf( p, "</html>\n" );
492 *pi_data = strlen( *pp_data );
495 static void ParseExecute( httpd_file_sys_t *p_args, char *p_buffer,
496 int i_buffer, char *p_request,
497 char **pp_data, int *pi_data )
499 intf_sys_t *p_sys = p_args->p_intf->p_sys;
500 int i_request = p_request != NULL ? strlen( p_request ) : 0;
503 char position[4]; /* percentage */
504 char time[12]; /* in seconds */
505 char length[12]; /* in seconds */
506 audio_volume_t i_volume;
511 assert( p_sys->p_input == NULL );
512 /* FIXME: proper locking anyone? */
513 p_sys->p_input = p_sys->p_playlist->p_input;
516 vlc_object_yield( p_sys->p_input );
517 var_Get( p_sys->p_input, "position", &val);
518 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
519 var_Get( p_sys->p_input, "time", &val);
520 sprintf( time, I64Fi, (int64_t)val.i_time / I64C(1000000) );
521 var_Get( p_sys->p_input, "length", &val);
522 sprintf( length, I64Fi, (int64_t)val.i_time / I64C(1000000) );
524 var_Get( p_sys->p_input, "state", &val );
525 if( val.i_int == PLAYING_S )
529 else if( val.i_int == OPENING_S )
531 state = "opening/connecting";
533 else if( val.i_int == BUFFERING_S )
537 else if( val.i_int == PAUSE_S )
548 sprintf( position, "%d", 0 );
549 sprintf( time, "%d", 0 );
550 sprintf( length, "%d", 0 );
554 aout_VolumeGet( p_args->p_intf, &i_volume );
555 sprintf( volume, "%d", (int)i_volume );
557 p_args->vars = E_(mvar_New)( "variables", "" );
558 E_(mvar_AppendNewVar)( p_args->vars, "url_param",
559 i_request > 0 ? "1" : "0" );
560 E_(mvar_AppendNewVar)( p_args->vars, "url_value", p_request );
561 E_(mvar_AppendNewVar)( p_args->vars, "version", VLC_Version() );
562 E_(mvar_AppendNewVar)( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
563 E_(mvar_AppendNewVar)( p_args->vars, "vlc_compile_by", VLC_CompileBy() );
564 E_(mvar_AppendNewVar)( p_args->vars, "vlc_compile_host",
566 E_(mvar_AppendNewVar)( p_args->vars, "vlc_compile_domain",
567 VLC_CompileDomain() );
568 E_(mvar_AppendNewVar)( p_args->vars, "vlc_compiler", VLC_Compiler() );
569 E_(mvar_AppendNewVar)( p_args->vars, "vlc_changeset", VLC_Changeset() );
570 E_(mvar_AppendNewVar)( p_args->vars, "stream_position", position );
571 E_(mvar_AppendNewVar)( p_args->vars, "stream_time", time );
572 E_(mvar_AppendNewVar)( p_args->vars, "stream_length", length );
573 E_(mvar_AppendNewVar)( p_args->vars, "volume", volume );
574 E_(mvar_AppendNewVar)( p_args->vars, "stream_state", state );
575 E_(mvar_AppendNewVar)( p_args->vars, "charset", p_sys->psz_charset );
580 /* FIXME: Workarround a stupid assert in input_GetItem */
581 input_item_t *p_item = p_sys->p_input && p_sys->p_input->p
582 ? input_GetItem( p_sys->p_input )
587 vlc_mutex_lock( &p_item->p_stats->lock );
588 #define STATS_INT( n ) sprintf( stats, "%d", p_item->p_stats->i_ ## n ); \
589 E_(mvar_AppendNewVar)( p_args->vars, #n, stats );
590 #define STATS_FLOAT( n ) sprintf( stats, "%f", p_item->p_stats->f_ ## n ); \
591 E_(mvar_AppendNewVar)( p_args->vars, #n, stats );
592 STATS_INT( read_bytes )
593 STATS_FLOAT( input_bitrate )
594 STATS_INT( demux_read_bytes )
595 STATS_FLOAT( demux_bitrate )
596 STATS_INT( decoded_video )
597 STATS_INT( displayed_pictures )
598 STATS_INT( lost_pictures )
599 STATS_INT( decoded_audio )
600 STATS_INT( played_abuffers )
601 STATS_INT( lost_abuffers )
602 STATS_INT( sent_packets )
603 STATS_INT( sent_bytes )
604 STATS_FLOAT( send_bitrate )
607 vlc_mutex_unlock( &p_item->p_stats->lock );
611 E_(SSInit)( &p_args->stack );
613 /* allocate output */
614 *pi_data = i_buffer + 1000;
615 dst = *pp_data = malloc( *pi_data );
617 /* we parse executing all <vlc /> macros */
618 E_(Execute)( p_args, p_request, i_request, pp_data, pi_data, &dst,
619 &p_buffer[0], &p_buffer[i_buffer] );
622 *pi_data = dst - *pp_data;
624 if( p_sys->p_input != NULL )
626 vlc_object_release( p_sys->p_input );
627 p_sys->p_input = NULL;
629 E_(SSClean)( &p_args->stack );
630 E_(mvar_Delete)( p_args->vars );
633 int E_(HttpCallback)( httpd_file_sys_t *p_args,
634 httpd_file_t *p_file,
636 uint8_t **_pp_data, int *pi_data )
638 char *p_request = (char *)_p_request;
639 char **pp_data = (char **)_pp_data;
642 if( ( f = utf8_fopen( p_args->file, "r" ) ) == NULL )
644 Callback404( p_args, pp_data, pi_data );
648 if( !p_args->b_html )
650 E_(FileLoad)( f, pp_data, pi_data );
657 /* first we load in a temporary buffer */
658 E_(FileLoad)( f, &p_buffer, &i_buffer );
660 ParseExecute( p_args, p_buffer, i_buffer, p_request, pp_data, pi_data );
670 /****************************************************************************
672 ****************************************************************************
673 * call the external handler and parse vlc macros if Content-Type is HTML
674 ****************************************************************************/
675 int E_(HandlerCallback)( httpd_handler_sys_t *p_args,
676 httpd_handler_t *p_handler, char *_p_url,
677 uint8_t *_p_request, int i_type,
678 uint8_t *_p_in, int i_in,
679 char *psz_remote_addr, char *psz_remote_host,
680 uint8_t **_pp_data, int *pi_data )
682 char *p_url = (char *)_p_url;
683 char *p_request = (char *)_p_request;
684 char **pp_data = (char **)_pp_data;
685 char *p_in = (char *)p_in;
686 int i_request = p_request != NULL ? strlen( p_request ) : 0;
689 char **ppsz_env = NULL;
694 char *psz_cwd, *psz_file = NULL;
703 /* Create environment for the CGI */
704 TAB_APPEND( i_env, ppsz_env, strdup("GATEWAY_INTERFACE=CGI/1.1") );
705 TAB_APPEND( i_env, ppsz_env, strdup("SERVER_PROTOCOL=HTTP/1.1") );
706 TAB_APPEND( i_env, ppsz_env, strdup("SERVER_SOFTWARE=" COPYRIGHT_MESSAGE) );
711 TAB_APPEND( i_env, ppsz_env, strdup("REQUEST_METHOD=GET") );
714 TAB_APPEND( i_env, ppsz_env, strdup("REQUEST_METHOD=POST") );
717 TAB_APPEND( i_env, ppsz_env, strdup("REQUEST_METHOD=HEAD") );
725 psz_tmp = malloc( sizeof("QUERY_STRING=") + i_request );
726 sprintf( psz_tmp, "QUERY_STRING=%s", p_request );
727 TAB_APPEND( i_env, ppsz_env, psz_tmp );
729 psz_tmp = malloc( sizeof("REQUEST_URI=?") + strlen(p_url)
731 sprintf( psz_tmp, "REQUEST_URI=%s?%s", p_url, p_request );
732 TAB_APPEND( i_env, ppsz_env, psz_tmp );
736 psz_tmp = malloc( sizeof("REQUEST_URI=") + strlen(p_url) );
737 sprintf( psz_tmp, "REQUEST_URI=%s", p_url );
738 TAB_APPEND( i_env, ppsz_env, psz_tmp );
741 psz_tmp = malloc( sizeof("SCRIPT_NAME=") + strlen(p_url) );
742 sprintf( psz_tmp, "SCRIPT_NAME=%s", p_url );
743 TAB_APPEND( i_env, ppsz_env, psz_tmp );
745 #define p_sys p_args->file.p_intf->p_sys
746 psz_tmp = malloc( sizeof("SERVER_NAME=") + strlen(p_sys->psz_address) );
747 sprintf( psz_tmp, "SERVER_NAME=%s", p_sys->psz_address );
748 TAB_APPEND( i_env, ppsz_env, psz_tmp );
750 psz_tmp = malloc( sizeof("SERVER_PORT=") + 5 );
751 sprintf( psz_tmp, "SERVER_PORT=%u", p_sys->i_port );
752 TAB_APPEND( i_env, ppsz_env, psz_tmp );
755 p = getenv( "PATH" );
758 psz_tmp = malloc( sizeof("PATH=") + strlen(p) );
759 sprintf( psz_tmp, "PATH=%s", p );
760 TAB_APPEND( i_env, ppsz_env, psz_tmp );
764 p = getenv( "windir" );
767 psz_tmp = malloc( sizeof("SYSTEMROOT=") + strlen(p) );
768 sprintf( psz_tmp, "SYSTEMROOT=%s", p );
769 TAB_APPEND( i_env, ppsz_env, psz_tmp );
773 if( psz_remote_addr != NULL && *psz_remote_addr )
775 psz_tmp = malloc( sizeof("REMOTE_ADDR=") + strlen(psz_remote_addr) );
776 sprintf( psz_tmp, "REMOTE_ADDR=%s", psz_remote_addr );
777 TAB_APPEND( i_env, ppsz_env, psz_tmp );
780 if( psz_remote_host != NULL && *psz_remote_host )
782 psz_tmp = malloc( sizeof("REMOTE_HOST=") + strlen(psz_remote_host) );
783 sprintf( psz_tmp, "REMOTE_HOST=%s", psz_remote_host );
784 TAB_APPEND( i_env, ppsz_env, psz_tmp );
792 if( !strncasecmp( p, "Content-Type: ", strlen("Content-Type: ") ) )
794 char *end = strchr( p, '\r' );
798 psz_tmp = malloc( sizeof("CONTENT_TYPE=") + strlen(p) );
799 sprintf( psz_tmp, "CONTENT_TYPE=%s", p );
800 TAB_APPEND( i_env, ppsz_env, psz_tmp );
803 if( !strncasecmp( p, "Content-Length: ",
804 strlen("Content-Length: ") ) )
806 char *end = strchr( p, '\r' );
810 psz_tmp = malloc( sizeof("CONTENT_LENGTH=") + strlen(p) );
811 sprintf( psz_tmp, "CONTENT_LENGTH=%s", p );
812 TAB_APPEND( i_env, ppsz_env, psz_tmp );
816 p = strchr( p, '\n' );
817 if( p == NULL || p[1] == '\r' )
826 psz_file = strrchr( p_args->file.file, sep );
827 if( psz_file != NULL )
830 psz_tmp = malloc( sizeof("SCRIPT_FILENAME=") + strlen(psz_file) );
831 sprintf( psz_tmp, "SCRIPT_FILENAME=%s", psz_file );
832 TAB_APPEND( i_env, ppsz_env, psz_tmp );
834 TAB_APPEND( p_args->p_association->i_argc,
835 p_args->p_association->ppsz_argv, psz_file );
838 TAB_APPEND( i_env, ppsz_env, NULL );
840 TAB_APPEND( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
843 psz_tmp = strdup( p_args->file.file );
844 p = strrchr( psz_tmp, sep );
856 i_ret = vlc_execve( p_args->file.p_intf, p_args->p_association->i_argc,
857 p_args->p_association->ppsz_argv, ppsz_env, psz_cwd,
858 (char *)p_in, i_in, &p_buffer, &i_buffer );
859 TAB_REMOVE( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
861 TAB_REMOVE( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
863 if( psz_cwd != NULL )
866 TAB_REMOVE( i_env, ppsz_env, ppsz_env[0] );
870 Callback404( (httpd_file_sys_t *)p_args, pp_data, pi_data );
874 while( strncasecmp( p, "Content-Type: text/html",
875 strlen("Content-Type: text/html") ) )
877 p = strchr( p, '\n' );
878 if( p == NULL || p[1] == '\r' )
893 ParseExecute( (httpd_file_sys_t *)p_args, p_buffer, i_buffer,
894 p_request, pp_data, pi_data );
902 int E_(ArtCallback)( httpd_handler_sys_t *p_args,
903 httpd_handler_t *p_handler, char *_p_url,
904 uint8_t *p_request, int i_type,
905 uint8_t *p_in, int i_in,
906 char *psz_remote_addr, char *psz_remote_host,
907 uint8_t **pp_data, int *pi_data )
909 char *psz_art = NULL;
910 intf_thread_t *p_intf = p_args->file.p_intf;
911 intf_sys_t *p_sys = p_intf->p_sys;
913 input_item_t *p_item = NULL;
918 E_(ExtractURIValue)( (char *)p_request, "id", psz_id, 15 );
919 i_id = atoi( psz_id );
922 playlist_item_t *p_pl_item = playlist_ItemGetById( p_sys->p_playlist,
925 p_item = p_pl_item->p_input;
929 /* FIXME: Workarround a stupid assert in input_GetItem */
930 if( p_sys->p_input && p_sys->p_input->p )
931 p_item = input_GetItem( p_sys->p_input );
936 psz_art = input_item_GetArtURL( p_item );
939 if( psz_art && !strncmp( psz_art, "file://", strlen( "file://" ) ) )
945 int i_header_size, i_data;
947 if( ( f = utf8_fopen( psz_art + strlen( "file://" ), "r" ) ) == NULL )
949 msg_Dbg( p_intf, "Couldn't open album art file %s",
950 psz_art + strlen( "file://" ) );
951 Callback404( &p_args->file, (char**)pp_data, pi_data );
956 E_(FileLoad)( f, &p_data, &i_data );
960 psz_ext = strrchr( psz_art, '.' );
961 if( psz_ext ) psz_ext++;
963 #define HEADER "Content-Type: image/%s\n" \
964 "Content-Length: %d\n" \
966 i_header_size = asprintf( &psz_header, HEADER, psz_ext, i_data );
969 *pi_data = i_header_size + i_data;
970 *pp_data = (uint8_t*)malloc( *pi_data );
971 memcpy( *pp_data, psz_header, i_header_size );
972 memcpy( *pp_data+i_header_size, p_data, i_data );
978 msg_Dbg( p_intf, "No album art found" );
979 Callback404( &p_args->file, (char**)pp_data, pi_data );