1 /*****************************************************************************
2 * util.c : Utility functions for HTTP interface
3 *****************************************************************************
4 * Copyright (C) 2001-2005 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 *****************************************************************************/
29 #include "vlc_strings.h"
31 /****************************************************************************
32 * File and directory functions
33 ****************************************************************************/
35 /* ToUrl: create a good name for an url from filename */
36 char *E_(FileToUrl)( char *name, vlc_bool_t *pb_index )
40 url = p = malloc( strlen( name ) + 1 );
42 *pb_index = VLC_FALSE;
49 while( *name == '\\' || *name == '/' )
61 /* convert '\\' into '/' */
72 if( ( p = strrchr( url, '/' ) ) != NULL )
74 if( !strncmp( p, "/index.", 7 ) )
84 int E_(FileLoad)( FILE *f, char **pp_data, int *pi_data )
88 /* just load the file */
90 *pp_data = malloc( 1025 ); /* +1 for \0 */
91 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
94 *pp_data = realloc( *pp_data, *pi_data + 1025 );
100 (*pp_data)[*pi_data] = '\0';
105 /* Parse a directory and recursively add files */
106 int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
109 intf_sys_t *p_sys = p_intf->p_sys;
110 char dir[MAX_DIR_SIZE];
111 #ifdef HAVE_SYS_STAT_H
112 struct stat stat_info;
119 char *password = NULL;
131 #ifdef HAVE_SYS_STAT_H
132 if( utf8_stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
138 if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
140 msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
144 i_dirlen = strlen( psz_dir );
145 if( i_dirlen + 10 > MAX_DIR_SIZE )
147 msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
151 msg_Dbg( p_intf, "dir=%s", psz_dir );
153 sprintf( dir, "%s%c.access", psz_dir, sep );
154 if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
159 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
161 i_size = fread( line, 1, 1023, file );
165 while( i_size > 0 && ( line[i_size-1] == '\n' ||
166 line[i_size-1] == '\r' ) )
173 p = strchr( line, ':' );
177 user = strdup( line );
178 password = strdup( p );
181 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
182 user, password, i_size );
187 sprintf( dir, "%s%c.hosts", psz_dir, sep );
188 p_acl = ACL_Create( p_intf, VLC_FALSE );
189 if( ACL_LoadFile( p_acl, dir ) )
191 ACL_Destroy( p_acl );
197 const char *psz_filename;
198 /* parse psz_src dir */
199 if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
204 if( ( psz_filename[0] == '.' )
205 || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
208 sprintf( dir, "%s%c%s", psz_dir, sep, psz_filename );
209 LocaleFree( psz_filename );
211 if( E_(ParseDirectory)( p_intf, psz_root, dir ) )
213 httpd_file_sys_t *f = NULL;
214 httpd_handler_sys_t *h = NULL;
216 char *psz_tmp, *psz_file, *psz_name, *psz_ext;
218 psz_tmp = vlc_fix_readdir_charset( p_intf, dir );
219 psz_file = E_(FromUTF8)( p_intf, psz_tmp );
221 psz_tmp = vlc_fix_readdir_charset( p_intf,
222 &dir[strlen( psz_root )] );
223 psz_name = E_(FileToUrl)( psz_tmp, &b_index );
225 psz_ext = strrchr( psz_file, '.' );
226 if( psz_ext != NULL )
230 for( i = 0; i < p_sys->i_handlers; i++ )
231 if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
233 if( i < p_sys->i_handlers )
235 f = malloc( sizeof( httpd_handler_sys_t ) );
236 h = (httpd_handler_sys_t *)f;
237 f->b_handler = VLC_TRUE;
238 h->p_association = p_sys->pp_handlers[i];
243 f = malloc( sizeof( httpd_file_sys_t ) );
244 f->b_handler = VLC_FALSE;
253 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? VLC_TRUE : VLC_FALSE;
257 msg_Err( p_intf , "unable to parse directory" );
260 return( VLC_ENOMEM );
262 msg_Dbg( p_intf, "file=%s (url=%s)",
267 char *psz_type = strdup( p_sys->psz_html_type );
268 if( strstr( &dir[strlen( psz_root )], ".xml" ) )
270 char *psz = strstr( psz_type, "html;" );
280 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
282 f->b_html ? psz_type : NULL,
283 user, password, p_acl,
284 E_(HttpCallback), f );
286 if( f->p_file != NULL )
288 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
293 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
295 user, password, p_acl,
296 E_(HandlerCallback), h );
297 if( h->p_handler != NULL )
299 TAB_APPEND( p_sys->i_files, p_sys->pp_files,
300 (httpd_file_sys_t *)h );
304 /* for url that ends by / add
305 * - a redirect from rep to rep/
306 * - in case of index.* rep/index.html to rep/ */
307 if( f && f->name[strlen(f->name) - 1] == '/' )
309 char *psz_redir = strdup( f->name );
311 psz_redir[strlen( psz_redir ) - 1] = '\0';
313 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
314 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
317 if( b_index && ( p = strstr( f->file, "index." ) ) )
319 asprintf( &psz_redir, "%s%s", f->name, p );
321 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
322 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
323 f->name, psz_redir );
340 ACL_Destroy( p_acl );
347 /**************************************************************************
349 **************************************************************************/
350 char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 )
352 intf_sys_t *p_sys = p_intf->p_sys;
354 if ( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
356 size_t i_in = strlen(psz_utf8);
357 size_t i_out = i_in * 2;
358 char *psz_local = malloc(i_out + 1);
359 char *psz_out = psz_local;
361 char psz_tmp[i_in + 1];
362 const char *psz_in = psz_tmp;
363 strcpy( psz_tmp, psz_utf8 );
365 i_in = strlen( psz_tmp );
367 i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
369 if( i_ret == (size_t)-1 || i_in )
372 "failed to convert \"%s\" to desired charset (%s)",
373 psz_utf8, strerror(errno) );
375 return strdup( psz_utf8 );
382 return strdup( psz_utf8 );
385 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
387 intf_sys_t *p_sys = p_intf->p_sys;
389 if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
391 const char *psz_in = psz_local;
392 size_t i_in = strlen(psz_in);
393 size_t i_out = i_in * 6;
394 char *psz_utf8 = malloc(i_out + 1);
395 char *psz_out = psz_utf8;
397 size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
399 if( i_ret == (size_t)-1 || i_in )
402 "failed to convert \"%s\" to desired charset (%s)",
403 psz_local, strerror(errno) );
405 return strdup( psz_local );
412 return strdup( psz_local );
415 /*************************************************************************
417 *************************************************************************/
418 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
419 playlist_item_t *p_node, char *name, mvar_t *s,
424 if( p_node->i_children == -1 )
428 mvar_t *itm = E_(mvar_New)( name, "set" );
430 if( p_pl->status.p_item && p_node &&
431 p_pl->status.p_item->p_input && p_node->p_input &&
432 p_pl->status.p_item->p_input->i_id == p_node->p_input->i_id )
434 E_(mvar_AppendNewVar)( itm, "current", "1" );
438 E_(mvar_AppendNewVar)( itm, "current", "0" );
441 sprintf( value, "%d", p_node->p_input->i_id );
442 E_(mvar_AppendNewVar)( itm, "index", value );
444 psz = E_(FromUTF8)( p_intf, p_node->p_input->psz_name );
445 E_(mvar_AppendNewVar)( itm, "name", psz );
448 psz = E_(FromUTF8)( p_intf, p_node->p_input->psz_uri );
449 E_(mvar_AppendNewVar)( itm, "uri", psz );
452 sprintf( value, "Item");
453 E_(mvar_AppendNewVar)( itm, "type", value );
455 sprintf( value, "%d", i_depth );
456 E_(mvar_AppendNewVar)( itm, "depth", value );
458 if( p_node->i_flags & PLAYLIST_RO_FLAG )
460 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
464 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
467 sprintf( value, "%ld", (long)p_node->p_input->i_duration );
468 E_(mvar_AppendNewVar)( itm, "duration", value );
470 E_(mvar_AppendVar)( s, itm );
477 mvar_t *itm = E_(mvar_New)( name, "set" );
479 psz = E_(FromUTF8)( p_intf, p_node->p_input->psz_name );
480 E_(mvar_AppendNewVar)( itm, "name", psz );
481 E_(mvar_AppendNewVar)( itm, "uri", psz );
484 sprintf( value, "Node" );
485 E_(mvar_AppendNewVar)( itm, "type", value );
487 sprintf( value, "%d", p_node->p_input->i_id );
488 E_(mvar_AppendNewVar)( itm, "index", value );
490 sprintf( value, "%d", p_node->i_children);
491 E_(mvar_AppendNewVar)( itm, "i_children", value );
493 sprintf( value, "%d", i_depth );
494 E_(mvar_AppendNewVar)( itm, "depth", value );
496 if( p_node->i_flags & PLAYLIST_RO_FLAG )
498 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
502 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
505 E_(mvar_AppendVar)( s, itm );
507 for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
508 E_(PlaylistListNode)( p_intf, p_pl,
509 p_node->pp_children[i_child],
510 name, s, i_depth + 1);
516 /****************************************************************************
517 * Seek command parsing handling
518 ****************************************************************************/
519 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
521 intf_sys_t *p_sys = p_intf->p_sys;
527 #define POSITION_ABSOLUTE 12
528 #define POSITION_REL_FOR 13
529 #define POSITION_REL_BACK 11
530 #define VL_TIME_ABSOLUTE 0
531 #define VL_TIME_REL_FOR 1
532 #define VL_TIME_REL_BACK -1
535 var_Get( p_sys->p_input, "length", &val );
536 i_length = val.i_time;
538 while( p_value[0] != '\0' )
544 i_relative = VL_TIME_REL_FOR;
550 i_relative = VL_TIME_REL_BACK;
554 case '0': case '1': case '2': case '3': case '4':
555 case '5': case '6': case '7': case '8': case '9':
557 i_stock = strtol( p_value , &p_value , 10 );
560 case '%': /* for percentage ie position */
562 i_relative += POSITION_ABSOLUTE;
570 i_value = 60 * (i_value + i_stock) ;
575 case 'h': case 'H': /* hours */
577 i_value += 3600 * i_stock;
579 /* other characters which are not numbers are not
581 while( ((p_value[0] < '0') || (p_value[0] > '9'))
582 && (p_value[0] != '\0') )
588 case 'm': case 'M': case '\'': /* minutes */
590 i_value += 60 * i_stock;
593 while( ((p_value[0] < '0') || (p_value[0] > '9'))
594 && (p_value[0] != '\0') )
600 case 's': case 'S': case '"': /* seconds */
604 while( ((p_value[0] < '0') || (p_value[0] > '9'))
605 && (p_value[0] != '\0') )
619 /* if there is no known symbol, I consider it as seconds.
620 * Otherwise, i_stock = 0 */
625 case VL_TIME_ABSOLUTE:
627 if( (uint64_t)( i_value ) * 1000000 <= i_length )
628 val.i_time = (uint64_t)( i_value ) * 1000000;
630 val.i_time = i_length;
632 var_Set( p_sys->p_input, "time", val );
633 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
636 case VL_TIME_REL_FOR:
638 var_Get( p_sys->p_input, "time", &val );
639 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
641 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
644 val.i_time = i_length;
646 var_Set( p_sys->p_input, "time", val );
647 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
650 case VL_TIME_REL_BACK:
652 var_Get( p_sys->p_input, "time", &val );
653 if( (int64_t)( i_value ) * 1000000 > val.i_time )
658 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
660 var_Set( p_sys->p_input, "time", val );
661 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
664 case POSITION_ABSOLUTE:
666 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
668 var_Set( p_sys->p_input, "position", val );
669 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
672 case POSITION_REL_FOR:
674 var_Get( p_sys->p_input, "position", &val );
675 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
677 var_Set( p_sys->p_input, "position", val );
678 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
682 case POSITION_REL_BACK:
684 var_Get( p_sys->p_input, "position", &val );
685 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
687 var_Set( p_sys->p_input, "position", val );
688 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
694 msg_Dbg( p_intf, "invalid seek request" );
699 #undef POSITION_ABSOLUTE
700 #undef POSITION_REL_FOR
701 #undef POSITION_REL_BACK
702 #undef VL_TIME_ABSOLUTE
703 #undef VL_TIME_REL_FOR
704 #undef VL_TIME_REL_BACK
708 /****************************************************************************
709 * URI Parsing functions
710 ****************************************************************************/
711 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
715 while( (p = strstr( p, psz_name )) )
717 /* Verify that we are dealing with a post/get argument */
718 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
719 && p[strlen(psz_name)] == '=' )
728 char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
729 char *psz_value, int i_value_max )
733 while( (p = strstr( p, psz_name )) )
735 /* Verify that we are dealing with a post/get argument */
736 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
737 && p[strlen(psz_name)] == '=' )
746 p += strlen( psz_name );
749 if( strchr( p, '&' ) )
751 i_len = strchr( p, '&' ) - p;
755 /* for POST method */
756 if( strchr( p, '\n' ) )
758 i_len = strchr( p, '\n' ) - p;
759 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
766 i_len = __MIN( i_value_max - 1, i_len );
769 strncpy( psz_value, p, i_len );
770 psz_value[i_len] = '\0';
774 strncpy( psz_value, "", i_value_max );
780 strncpy( psz_value, "", i_value_max );
786 /* Since the resulting string is smaller we can work in place, so it is
787 * permitted to have psz == new. new points to the first word of the
788 * string, the function returns the remaining string. */
789 char *E_(FirstWord)( char *psz, char *new )
796 while( *psz != '\0' && *psz != ' ' )
801 while( *psz != '\0' && *psz != c )
803 if( *psz == '\\' && psz[1] != '\0' )
812 if( *psz == '\\' && psz[1] != '\0' )
826 /**********************************************************************
827 * MRLParse: parse the MRL, find the MRL string and the options,
828 * create an item with all information in it, and return the item.
829 * return NULL if there is an error.
830 **********************************************************************/
832 /* Function analog to FirstWord except that it relies on colon instead
833 * of space to delimit option boundaries. */
834 static char *FirstOption( char *psz, char *new )
836 vlc_bool_t b_end, b_start = VLC_TRUE;
841 while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
846 while( *psz != '\0' && *psz != c )
848 if( *psz == '\\' && psz[1] != '\0' )
858 if( *psz == '\\' && psz[1] != '\0' )
867 while (new[-1] == ' ')
877 input_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *_psz,
880 char *psz = strdup( _psz );
883 input_item_t * p_input = NULL;
885 /* extract the mrl */
886 s_temp = FirstOption( s_mrl, s_mrl );
889 s_temp = s_mrl + strlen( s_mrl );
892 p_input = input_ItemNew( p_intf, s_mrl, psz_name );
895 /* now we can take care of the options */
896 while ( *s_mrl != '\0' )
898 s_temp = FirstOption( s_mrl, s_mrl );
903 s_temp = s_mrl + strlen( s_mrl );
905 input_ItemAddOption( p_input, s_mrl );
913 /**********************************************************************
914 * RealPath: parse ../, ~ and path stuff
915 **********************************************************************/
916 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
920 int i_len = strlen(psz_src);
929 psz_dir = malloc( i_len + 2 );
930 strcpy( psz_dir, psz_src );
932 /* Add a trailing sep to ease the .. step */
933 psz_dir[i_len] = sep;
934 psz_dir[i_len + 1] = '\0';
937 /* Convert all / to native separator */
939 while( (p = strchr( p, '/' )) != NULL )
945 /* Remove multiple separators and /./ */
947 while( (p = strchr( p, sep )) != NULL )
950 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
951 else if( p[1] == '.' && p[2] == sep )
952 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
957 if( psz_dir[0] == '~' )
959 char *dir = malloc( strlen(psz_dir)
960 + strlen(p_intf->p_libvlc->psz_userdir) );
961 /* This is incomplete : we should also support the ~cmassiot/ syntax. */
962 sprintf( dir, "%s%s", p_intf->p_libvlc->psz_userdir, psz_dir + 1 );
967 if( strlen(psz_dir) > 2 )
971 while( (p = strchr( p, sep )) != NULL )
973 if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
977 if( (q = strrchr( psz_dir, sep )) != NULL )
979 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
984 memmove( psz_dir, p, strlen(p) + 1 );
993 /* Remove trailing sep if there are at least 2 sep in the string
994 * (handles the C:\ stuff) */
995 p = strrchr( psz_dir, sep );
996 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )