1 /*****************************************************************************
2 * http.c : HTTP/HTTPS Remote control interface
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
6 * Authors: Gildas Bazin <gbazin@netcourrier.com>
7 * Laurent Aimar <fenrir@via.ecp.fr>
8 * Christophe Massiot <massiot@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
29 #include <vlc_plugin.h>
33 /*****************************************************************************
35 *****************************************************************************/
36 static int Open ( vlc_object_t * );
37 static void Close( vlc_object_t * );
39 #define HOST_TEXT N_( "Host address" )
40 #define HOST_LONGTEXT N_( \
41 "Address and port the HTTP interface will listen on. It defaults to " \
42 "all network interfaces (0.0.0.0)." \
43 " If you want the HTTP interface to be available only on the local " \
44 "machine, enter 127.0.0.1" )
45 #define SRC_TEXT N_( "Source directory" )
46 #define SRC_LONGTEXT N_( "Source directory" )
47 #define HANDLERS_TEXT N_( "Handlers" )
48 #define HANDLERS_LONGTEXT N_( \
49 "List of handler extensions and executable paths (for instance: " \
50 "php=/usr/bin/php,pl=/usr/bin/perl)." )
51 #define ART_TEXT N_( "Export album art as /art." )
52 #define ART_LONGTEXT N_( \
53 "Allow exporting album art for current playlist items at the " \
54 "/art and /art?id=<id> URLs." )
55 #define CERT_TEXT N_( "Certificate file" )
56 #define CERT_LONGTEXT N_( "HTTP interface x509 PEM certificate file " \
58 #define KEY_TEXT N_( "Private key file" )
59 #define KEY_LONGTEXT N_( "HTTP interface x509 PEM private key file." )
60 #define CA_TEXT N_( "Root CA file" )
61 #define CA_LONGTEXT N_( "HTTP interface x509 PEM trusted root CA " \
62 "certificates file." )
63 #define CRL_TEXT N_( "CRL file" )
64 #define CRL_LONGTEXT N_( "HTTP interace Certificates Revocation List file." )
67 set_shortname( N_("HTTP"));
68 set_description( N_("HTTP remote control interface") );
69 set_category( CAT_INTERFACE );
70 set_subcategory( SUBCAT_INTERFACE_MAIN );
71 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, true );
72 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, true );
73 add_obsolete_string ( "http-charset" );
74 #if defined( HAVE_FORK ) || defined( WIN32 )
75 add_string ( "http-handlers", NULL, NULL, HANDLERS_TEXT, HANDLERS_LONGTEXT, true );
77 add_bool ( "http-album-art", false, NULL, ART_TEXT, ART_LONGTEXT, true );
78 set_section( N_("HTTP SSL" ), 0 );
79 add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, true );
80 add_string ( "http-intf-key", NULL, NULL, KEY_TEXT, KEY_LONGTEXT, true );
81 add_string ( "http-intf-ca", NULL, NULL, CA_TEXT, CA_LONGTEXT, true );
82 add_string ( "http-intf-crl", NULL, NULL, CRL_TEXT, CRL_LONGTEXT, true );
83 set_capability( "interface", 0 );
84 set_callbacks( Open, Close );
88 /*****************************************************************************
90 *****************************************************************************/
91 int ArtCallback( httpd_handler_sys_t *p_args,
92 httpd_handler_t *p_handler, char *_p_url,
93 uint8_t *_p_request, int i_type,
94 uint8_t *_p_in, int i_in,
95 char *psz_remote_addr, char *psz_remote_host,
96 uint8_t **pp_data, int *pi_data );
98 /*****************************************************************************
99 * Activate: initialize and create stuff
100 *****************************************************************************/
101 static int Open( vlc_object_t *p_this )
103 intf_thread_t *p_intf = (intf_thread_t*)p_this;
106 char *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
109 char *psz_src = NULL;
111 psz_address = var_CreateGetNonEmptyString( p_intf, "http-host" );
112 if( psz_address != NULL )
114 char *psz_parser = strrchr( psz_address, ':' );
117 *psz_parser++ = '\0';
118 i_port = atoi( psz_parser );
122 psz_address = strdup("");
124 p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
128 return( VLC_ENOMEM );
131 p_sys->p_playlist = pl_Hold( p_this );
132 p_sys->p_input = NULL;
134 p_sys->psz_address = psz_address;
135 p_sys->i_port = i_port;
136 p_sys->p_art_handler = NULL;
138 /* determine file handler associations */
139 p_sys->i_handlers = 0;
140 p_sys->pp_handlers = NULL;
141 #if defined( HAVE_FORK ) || defined( WIN32 )
142 psz_src = config_GetPsz( p_intf, "http-handlers" );
143 if( psz_src != NULL && *psz_src )
148 http_association_t *p_handler;
150 char *psz_program, *psz_options;
151 p = strchr( p, '=' );
152 if( p == NULL ) break;
155 p = strchr( p, ',' );
159 p_handler = malloc( sizeof( http_association_t ) );
160 p_handler->psz_ext = strdup( psz_ext );
161 psz_options = FirstWord( psz_program, psz_program );
162 p_handler->i_argc = 0;
163 p_handler->ppsz_argv = NULL;
164 TAB_APPEND( p_handler->i_argc, p_handler->ppsz_argv,
165 strdup( psz_program ) );
166 while( psz_options != NULL && *psz_options )
168 char *psz_next = FirstWord( psz_options, psz_options );
169 TAB_APPEND( p_handler->i_argc, p_handler->ppsz_argv,
170 strdup( psz_options ) );
171 psz_options = psz_next;
173 /* NULL will be appended later on */
175 TAB_APPEND( p_sys->i_handlers, p_sys->pp_handlers, p_handler );
181 /* determine SSL configuration */
182 psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
183 if ( psz_cert != NULL )
185 msg_Dbg( p_intf, "enabling TLS for HTTP interface (cert file: %s)",
187 psz_key = var_GetNonEmptyString( p_intf, "http-intf-key" );
188 psz_ca = var_GetNonEmptyString( p_intf, "http-intf-ca" );
189 psz_crl = var_GetNonEmptyString( p_intf, "http-intf-crl" );
200 msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
202 p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_intf), psz_address,
203 i_port, psz_cert, psz_key, psz_ca,
210 if( p_sys->p_httpd_host == NULL )
212 msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
213 pl_Release( p_this );
214 free( p_sys->psz_address );
220 char psz_tmp[NI_MAXHOST + 6];
222 /* Ugly hack to run several HTTP servers on different ports */
223 snprintf( psz_tmp, sizeof (psz_tmp), "%s:%d", psz_address, i_port + 1 );
224 var_Create(p_intf->p_libvlc, "http-host", VLC_VAR_STRING );
225 var_SetString( p_intf->p_libvlc, "http-host", psz_tmp );
229 p_sys->pp_files = NULL;
231 psz_src = config_GetPsz( p_intf, "http-src" );
232 if( ( psz_src == NULL ) || ( *psz_src == '\0' ) )
234 const char *data_path = config_GetDataDir ();
235 if( asprintf( &psz_src, "%s" DIR_SEP "http", data_path ) == -1 )
239 if( !psz_src || *psz_src == '\0' )
241 msg_Err( p_intf, "invalid web interface source directory" );
245 /* remove trainling \ or / */
246 if( psz_src[strlen( psz_src ) - 1] == '\\' ||
247 psz_src[strlen( psz_src ) - 1] == '/' )
249 psz_src[strlen( psz_src ) - 1] = '\0';
252 ParseDirectory( p_intf, psz_src, psz_src );
253 if( p_sys->i_files <= 0 )
255 msg_Err( p_intf, "cannot find any file in directory %s", psz_src );
261 if( config_GetInt( p_intf, "http-album-art" ) )
263 /* FIXME: we're leaking h */
264 httpd_handler_sys_t *h = malloc( sizeof( httpd_handler_sys_t ) );
267 msg_Err( p_intf, "not enough memory to allocate album art handler" );
270 h->file.p_intf = p_intf;
273 /* TODO: use ACL and login/password stuff here too */
274 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
275 "/art", NULL, NULL, NULL,
277 p_sys->p_art_handler = h->p_handler;
284 free( p_sys->pp_files );
285 httpd_HostDelete( p_sys->p_httpd_host );
286 free( p_sys->psz_address );
288 pl_Release( p_this );
292 /*****************************************************************************
293 * Close: destroy interface
294 *****************************************************************************/
295 static void Close ( vlc_object_t *p_this )
297 intf_thread_t *p_intf = (intf_thread_t *)p_this;
298 intf_sys_t *p_sys = p_intf->p_sys;
303 vlm_Delete( p_sys->p_vlm );
305 for( i = 0; i < p_sys->i_files; i++ )
307 if( p_sys->pp_files[i]->b_handler )
308 httpd_HandlerDelete( ((httpd_handler_sys_t *)p_sys->pp_files[i])->p_handler );
310 httpd_FileDelete( p_sys->pp_files[i]->p_file );
311 if( p_sys->pp_files[i]->p_redir )
312 httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
313 if( p_sys->pp_files[i]->p_redir2 )
314 httpd_RedirectDelete( p_sys->pp_files[i]->p_redir2 );
316 free( p_sys->pp_files[i]->file );
317 free( p_sys->pp_files[i]->name );
318 free( p_sys->pp_files[i] );
320 free( p_sys->pp_files );
321 for( i = 0; i < p_sys->i_handlers; i++ )
323 http_association_t *p_handler = p_sys->pp_handlers[i];
325 free( p_handler->psz_ext );
326 for( j = 0; j < p_handler->i_argc; j++ )
327 free( p_handler->ppsz_argv[j] );
328 if( p_handler->i_argc )
329 free( p_handler->ppsz_argv );
332 if( p_sys->i_handlers )
333 free( p_sys->pp_handlers );
334 if( p_sys->p_art_handler )
335 httpd_HandlerDelete( p_sys->p_art_handler );
336 httpd_HostDelete( p_sys->p_httpd_host );
337 free( p_sys->psz_address );
339 pl_Release( p_this );
342 /****************************************************************************
344 ****************************************************************************
345 * a file with b_html is parsed and all "macro" replaced
346 ****************************************************************************/
347 static void Callback404( httpd_file_sys_t *p_args, char **pp_data,
350 char *p = *pp_data = malloc( 10240 );
355 p += sprintf( p, "Content-Type: text/html\n" );
356 p += sprintf( p, "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" );
357 p += sprintf( p, "<head>\n" );
358 p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
359 p += sprintf( p, "</head>\n" );
360 p += sprintf( p, "<body>\n" );
361 p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
362 p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
363 p += sprintf( p, "</body>\n" );
364 p += sprintf( p, "</html>\n" );
366 *pi_data = strlen( *pp_data );
369 static void ParseExecute( httpd_file_sys_t *p_args, char *p_buffer,
370 int i_buffer, char *p_request,
371 char **pp_data, int *pi_data )
373 intf_sys_t *p_sys = p_args->p_intf->p_sys;
374 int i_request = p_request != NULL ? strlen( p_request ) : 0;
377 char position[4]; /* percentage */
378 char time[12]; /* in seconds */
379 char length[12]; /* in seconds */
380 audio_volume_t i_volume;
385 assert( p_sys->p_input == NULL );
386 /* FIXME: proper locking anyone? */
387 p_sys->p_input = p_sys->p_playlist->p_input;
390 vlc_object_hold( p_sys->p_input );
391 var_Get( p_sys->p_input, "position", &val);
392 sprintf( position, "%d" , (int)((val.f_float) * 100.0));
393 var_Get( p_sys->p_input, "time", &val);
394 sprintf( time, "%"PRIi64, (int64_t)val.i_time / INT64_C(1000000) );
395 var_Get( p_sys->p_input, "length", &val);
396 sprintf( length, "%"PRIi64, (int64_t)val.i_time / INT64_C(1000000) );
398 var_Get( p_sys->p_input, "state", &val );
399 if( val.i_int == PLAYING_S )
403 else if( val.i_int == OPENING_S )
405 state = "opening/connecting";
407 else if( val.i_int == BUFFERING_S )
411 else if( val.i_int == PAUSE_S )
422 sprintf( position, "%d", 0 );
423 sprintf( time, "%d", 0 );
424 sprintf( length, "%d", 0 );
428 aout_VolumeGet( p_args->p_intf, &i_volume );
429 sprintf( volume, "%d", (int)i_volume );
431 p_args->vars = mvar_New( "variables", "" );
432 mvar_AppendNewVar( p_args->vars, "url_param",
433 i_request > 0 ? "1" : "0" );
434 mvar_AppendNewVar( p_args->vars, "url_value", p_request );
435 mvar_AppendNewVar( p_args->vars, "version", VLC_Version() );
436 mvar_AppendNewVar( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
437 mvar_AppendNewVar( p_args->vars, "vlc_compile_by", VLC_CompileBy() );
438 mvar_AppendNewVar( p_args->vars, "vlc_compile_host",
440 mvar_AppendNewVar( p_args->vars, "vlc_compile_domain",
441 VLC_CompileDomain() );
442 mvar_AppendNewVar( p_args->vars, "vlc_compiler", VLC_Compiler() );
443 mvar_AppendNewVar( p_args->vars, "vlc_changeset", VLC_Changeset() );
444 mvar_AppendNewVar( p_args->vars, "stream_position", position );
445 mvar_AppendNewVar( p_args->vars, "stream_time", time );
446 mvar_AppendNewVar( p_args->vars, "stream_length", length );
447 mvar_AppendNewVar( p_args->vars, "volume", volume );
448 mvar_AppendNewVar( p_args->vars, "stream_state", state );
449 mvar_AppendNewVar( p_args->vars, "charset", "UTF-8" );
454 /* FIXME: Workarround a stupid assert in input_GetItem */
455 input_item_t *p_item = p_sys->p_input && p_sys->p_input->p
456 ? input_GetItem( p_sys->p_input )
461 vlc_mutex_lock( &p_item->p_stats->lock );
462 #define STATS_INT( n ) sprintf( stats, "%d", p_item->p_stats->i_ ## n ); \
463 mvar_AppendNewVar( p_args->vars, #n, stats );
464 #define STATS_FLOAT( n ) sprintf( stats, "%f", p_item->p_stats->f_ ## n ); \
465 mvar_AppendNewVar( p_args->vars, #n, stats );
466 STATS_INT( read_bytes )
467 STATS_FLOAT( input_bitrate )
468 STATS_INT( demux_read_bytes )
469 STATS_FLOAT( demux_bitrate )
470 STATS_INT( decoded_video )
471 STATS_INT( displayed_pictures )
472 STATS_INT( lost_pictures )
473 STATS_INT( decoded_audio )
474 STATS_INT( played_abuffers )
475 STATS_INT( lost_abuffers )
476 STATS_INT( sent_packets )
477 STATS_INT( sent_bytes )
478 STATS_FLOAT( send_bitrate )
481 vlc_mutex_unlock( &p_item->p_stats->lock );
485 SSInit( &p_args->stack );
487 /* allocate output */
488 *pi_data = i_buffer + 1000;
489 dst = *pp_data = malloc( *pi_data );
491 /* we parse executing all <vlc /> macros */
492 Execute( p_args, p_request, i_request, pp_data, pi_data, &dst,
493 &p_buffer[0], &p_buffer[i_buffer] );
496 *pi_data = dst - *pp_data;
498 if( p_sys->p_input != NULL )
500 vlc_object_release( p_sys->p_input );
501 p_sys->p_input = NULL;
503 SSClean( &p_args->stack );
504 mvar_Delete( p_args->vars );
507 int HttpCallback( httpd_file_sys_t *p_args,
508 httpd_file_t *p_file,
510 uint8_t **_pp_data, int *pi_data )
513 char *p_request = (char *)_p_request;
514 char **pp_data = (char **)_pp_data;
517 if( ( f = utf8_fopen( p_args->file, "r" ) ) == NULL )
519 Callback404( p_args, pp_data, pi_data );
523 if( !p_args->b_html )
525 FileLoad( f, pp_data, pi_data );
532 /* first we load in a temporary buffer */
533 FileLoad( f, &p_buffer, &i_buffer );
535 ParseExecute( p_args, p_buffer, i_buffer, p_request, pp_data, pi_data );
545 /****************************************************************************
547 ****************************************************************************
548 * call the external handler and parse vlc macros if Content-Type is HTML
549 ****************************************************************************/
550 int HandlerCallback( httpd_handler_sys_t *p_args,
551 httpd_handler_t *p_handler, char *_p_url,
552 uint8_t *_p_request, int i_type,
553 uint8_t *_p_in, int i_in,
554 char *psz_remote_addr, char *psz_remote_host,
555 uint8_t **_pp_data, int *pi_data )
557 VLC_UNUSED(p_handler); VLC_UNUSED(_p_in);
558 char *p_url = (char *)_p_url;
559 char *p_request = (char *)_p_request;
560 char **pp_data = (char **)_pp_data;
561 char *p_in = (char *)_p_in;
562 int i_request = p_request != NULL ? strlen( p_request ) : 0;
565 char **ppsz_env = NULL;
570 char *psz_cwd, *psz_file = NULL;
579 /* Create environment for the CGI */
580 TAB_APPEND( i_env, ppsz_env, strdup("GATEWAY_INTERFACE=CGI/1.1") );
581 TAB_APPEND( i_env, ppsz_env, strdup("SERVER_PROTOCOL=HTTP/1.1") );
582 TAB_APPEND( i_env, ppsz_env, strdup("SERVER_SOFTWARE=" COPYRIGHT_MESSAGE) );
587 TAB_APPEND( i_env, ppsz_env, strdup("REQUEST_METHOD=GET") );
590 TAB_APPEND( i_env, ppsz_env, strdup("REQUEST_METHOD=POST") );
593 TAB_APPEND( i_env, ppsz_env, strdup("REQUEST_METHOD=HEAD") );
601 psz_tmp = malloc( sizeof("QUERY_STRING=") + i_request );
602 sprintf( psz_tmp, "QUERY_STRING=%s", p_request );
603 TAB_APPEND( i_env, ppsz_env, psz_tmp );
605 psz_tmp = malloc( sizeof("REQUEST_URI=?") + strlen(p_url)
607 sprintf( psz_tmp, "REQUEST_URI=%s?%s", p_url, p_request );
608 TAB_APPEND( i_env, ppsz_env, psz_tmp );
612 psz_tmp = malloc( sizeof("REQUEST_URI=") + strlen(p_url) );
613 sprintf( psz_tmp, "REQUEST_URI=%s", p_url );
614 TAB_APPEND( i_env, ppsz_env, psz_tmp );
617 psz_tmp = malloc( sizeof("SCRIPT_NAME=") + strlen(p_url) );
618 sprintf( psz_tmp, "SCRIPT_NAME=%s", p_url );
619 TAB_APPEND( i_env, ppsz_env, psz_tmp );
621 #define p_sys p_args->file.p_intf->p_sys
622 psz_tmp = malloc( sizeof("SERVER_NAME=") + strlen(p_sys->psz_address) );
623 sprintf( psz_tmp, "SERVER_NAME=%s", p_sys->psz_address );
624 TAB_APPEND( i_env, ppsz_env, psz_tmp );
626 psz_tmp = malloc( sizeof("SERVER_PORT=") + 5 );
627 sprintf( psz_tmp, "SERVER_PORT=%u", p_sys->i_port );
628 TAB_APPEND( i_env, ppsz_env, psz_tmp );
631 p = getenv( "PATH" );
634 psz_tmp = malloc( sizeof("PATH=") + strlen(p) );
635 sprintf( psz_tmp, "PATH=%s", p );
636 TAB_APPEND( i_env, ppsz_env, psz_tmp );
640 p = getenv( "windir" );
643 psz_tmp = malloc( sizeof("SYSTEMROOT=") + strlen(p) );
644 sprintf( psz_tmp, "SYSTEMROOT=%s", p );
645 TAB_APPEND( i_env, ppsz_env, psz_tmp );
649 if( psz_remote_addr != NULL && *psz_remote_addr )
651 psz_tmp = malloc( sizeof("REMOTE_ADDR=") + strlen(psz_remote_addr) );
652 sprintf( psz_tmp, "REMOTE_ADDR=%s", psz_remote_addr );
653 TAB_APPEND( i_env, ppsz_env, psz_tmp );
656 if( psz_remote_host != NULL && *psz_remote_host )
658 psz_tmp = malloc( sizeof("REMOTE_HOST=") + strlen(psz_remote_host) );
659 sprintf( psz_tmp, "REMOTE_HOST=%s", psz_remote_host );
660 TAB_APPEND( i_env, ppsz_env, psz_tmp );
668 if( !strncasecmp( p, "Content-Type: ", strlen("Content-Type: ") ) )
670 char *end = strchr( p, '\r' );
674 psz_tmp = malloc( sizeof("CONTENT_TYPE=") + strlen(p) );
675 sprintf( psz_tmp, "CONTENT_TYPE=%s", p );
676 TAB_APPEND( i_env, ppsz_env, psz_tmp );
679 if( !strncasecmp( p, "Content-Length: ",
680 strlen("Content-Length: ") ) )
682 char *end = strchr( p, '\r' );
686 psz_tmp = malloc( sizeof("CONTENT_LENGTH=") + strlen(p) );
687 sprintf( psz_tmp, "CONTENT_LENGTH=%s", p );
688 TAB_APPEND( i_env, ppsz_env, psz_tmp );
692 p = strchr( p, '\n' );
693 if( p == NULL || p[1] == '\r' )
702 psz_file = strrchr( p_args->file.file, sep );
703 if( psz_file != NULL )
706 psz_tmp = malloc( sizeof("SCRIPT_FILENAME=") + strlen(psz_file) );
707 sprintf( psz_tmp, "SCRIPT_FILENAME=%s", psz_file );
708 TAB_APPEND( i_env, ppsz_env, psz_tmp );
710 TAB_APPEND( p_args->p_association->i_argc,
711 p_args->p_association->ppsz_argv, psz_file );
714 TAB_APPEND( i_env, ppsz_env, NULL );
716 TAB_APPEND( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
719 psz_tmp = strdup( p_args->file.file );
720 p = strrchr( psz_tmp, sep );
732 i_ret = vlc_execve( p_args->file.p_intf, p_args->p_association->i_argc,
733 p_args->p_association->ppsz_argv, ppsz_env, psz_cwd,
734 (char *)p_in, i_in, &p_buffer, &i_buffer );
735 TAB_REMOVE( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
737 TAB_REMOVE( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
741 TAB_REMOVE( i_env, ppsz_env, ppsz_env[0] );
745 Callback404( (httpd_file_sys_t *)p_args, pp_data, pi_data );
749 while( strncasecmp( p, "Content-Type: text/html",
750 strlen("Content-Type: text/html") ) )
752 p = strchr( p, '\n' );
753 if( p == NULL || p[1] == '\r' )
768 ParseExecute( (httpd_file_sys_t *)p_args, p_buffer, i_buffer,
769 p_request, pp_data, pi_data );
777 int ArtCallback( httpd_handler_sys_t *p_args,
778 httpd_handler_t *p_handler, char *_p_url,
779 uint8_t *p_request, int i_type,
780 uint8_t *p_in, int i_in,
781 char *psz_remote_addr, char *psz_remote_host,
782 uint8_t **pp_data, int *pi_data )
784 VLC_UNUSED(p_handler); VLC_UNUSED(_p_url); VLC_UNUSED(i_type);
785 VLC_UNUSED(p_in); VLC_UNUSED(i_in); VLC_UNUSED(psz_remote_addr);
786 VLC_UNUSED(psz_remote_host);
788 char *psz_art = NULL;
789 intf_thread_t *p_intf = p_args->file.p_intf;
790 intf_sys_t *p_sys = p_intf->p_sys;
792 input_item_t *p_item = NULL;
797 ExtractURIValue( (char *)p_request, "id", psz_id, 15 );
798 i_id = atoi( psz_id );
801 playlist_item_t *p_pl_item = playlist_ItemGetById( p_sys->p_playlist,
804 p_item = p_pl_item->p_input;
808 /* FIXME: Workarround a stupid assert in input_GetItem */
809 if( p_sys->p_input && p_sys->p_input->p )
810 p_item = input_GetItem( p_sys->p_input );
815 psz_art = input_item_GetArtURL( p_item );
818 if( psz_art && !strncmp( psz_art, "file://", strlen( "file://" ) ) )
824 int i_header_size, i_data;
826 if( ( f = utf8_fopen( psz_art + strlen( "file://" ), "r" ) ) == NULL )
828 msg_Dbg( p_intf, "Couldn't open album art file %s",
829 psz_art + strlen( "file://" ) );
830 Callback404( &p_args->file, (char**)pp_data, pi_data );
835 FileLoad( f, &p_data, &i_data );
839 psz_ext = strrchr( psz_art, '.' );
840 if( psz_ext ) psz_ext++;
842 #define HEADER "Content-Type: image/%s\n" \
843 "Content-Length: %d\n" \
845 i_header_size = asprintf( &psz_header, HEADER, psz_ext, i_data );
848 *pi_data = i_header_size + i_data;
849 *pp_data = (uint8_t*)malloc( *pi_data );
850 memcpy( *pp_data, psz_header, i_header_size );
851 memcpy( *pp_data+i_header_size, p_data, i_data );
857 msg_Dbg( p_intf, "No album art found" );
858 Callback404( &p_args->file, (char**)pp_data, pi_data );