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 *****************************************************************************/
27 #include "vlc_strings.h"
29 /****************************************************************************
30 * File and directory functions
31 ****************************************************************************/
33 /* ToUrl: create a good name for an url from filename */
34 char *E_(FileToUrl)( char *name, vlc_bool_t *pb_index )
38 url = p = malloc( strlen( name ) + 1 );
40 *pb_index = VLC_FALSE;
47 while( *name == '\\' || *name == '/' )
59 /* convert '\\' into '/' */
70 if( ( p = strrchr( url, '/' ) ) != NULL )
72 if( !strncmp( p, "/index.", 7 ) )
82 int E_(FileLoad)( FILE *f, char **pp_data, int *pi_data )
86 /* just load the file */
88 *pp_data = malloc( 1025 ); /* +1 for \0 */
89 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
92 *pp_data = realloc( *pp_data, *pi_data + 1025 );
98 (*pp_data)[*pi_data] = '\0';
103 /* Parse a directory and recursively add files */
104 int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
107 intf_sys_t *p_sys = p_intf->p_sys;
108 char dir[MAX_DIR_SIZE];
109 #ifdef HAVE_SYS_STAT_H
110 struct stat stat_info;
117 char *password = NULL;
129 #ifdef HAVE_SYS_STAT_H
130 if( utf8_stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
136 if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
138 msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
142 i_dirlen = strlen( psz_dir );
143 if( i_dirlen + 10 > MAX_DIR_SIZE )
145 msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
149 msg_Dbg( p_intf, "dir=%s", psz_dir );
151 sprintf( dir, "%s%c.access", psz_dir, sep );
152 if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
157 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
159 i_size = fread( line, 1, 1023, file );
163 while( i_size > 0 && ( line[i_size-1] == '\n' ||
164 line[i_size-1] == '\r' ) )
171 p = strchr( line, ':' );
175 user = strdup( line );
176 password = strdup( p );
179 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
180 user, password, i_size );
185 sprintf( dir, "%s%c.hosts", psz_dir, sep );
186 p_acl = ACL_Create( p_intf, VLC_FALSE );
187 if( ACL_LoadFile( p_acl, dir ) )
189 ACL_Destroy( p_acl );
195 const char *psz_filename;
196 /* parse psz_src dir */
197 if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
202 if( ( psz_filename[0] == '.' )
203 || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
206 sprintf( dir, "%s%c%s", psz_dir, sep, psz_filename );
207 LocaleFree( psz_filename );
209 if( E_(ParseDirectory)( p_intf, psz_root, dir ) )
211 httpd_file_sys_t *f = NULL;
212 httpd_handler_sys_t *h = NULL;
214 char *psz_tmp, *psz_file, *psz_name, *psz_ext;
216 psz_tmp = vlc_fix_readdir_charset( p_intf, dir );
217 psz_file = E_(FromUTF8)( p_intf, psz_tmp );
219 psz_tmp = vlc_fix_readdir_charset( p_intf,
220 &dir[strlen( psz_root )] );
221 psz_name = E_(FileToUrl)( psz_tmp, &b_index );
223 psz_ext = strrchr( psz_file, '.' );
224 if( psz_ext != NULL )
228 for( i = 0; i < p_sys->i_handlers; i++ )
229 if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
231 if( i < p_sys->i_handlers )
233 f = malloc( sizeof( httpd_handler_sys_t ) );
234 h = (httpd_handler_sys_t *)f;
235 f->b_handler = VLC_TRUE;
236 h->p_association = p_sys->pp_handlers[i];
241 f = malloc( sizeof( httpd_file_sys_t ) );
242 f->b_handler = VLC_FALSE;
251 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? VLC_TRUE : VLC_FALSE;
255 msg_Err( p_intf , "unable to parse directory" );
258 return( VLC_ENOMEM );
260 msg_Dbg( p_intf, "file=%s (url=%s)",
265 char *psz_type = strdup( p_sys->psz_html_type );
266 if( strstr( &dir[strlen( psz_root )], ".xml" ) )
268 char *psz = strstr( psz_type, "html;" );
278 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
280 f->b_html ? psz_type : NULL,
281 user, password, p_acl,
282 E_(HttpCallback), f );
284 if( f->p_file != NULL )
286 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
291 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
293 user, password, p_acl,
294 E_(HandlerCallback), h );
295 if( h->p_handler != NULL )
297 TAB_APPEND( p_sys->i_files, p_sys->pp_files,
298 (httpd_file_sys_t *)h );
302 /* for url that ends by / add
303 * - a redirect from rep to rep/
304 * - in case of index.* rep/index.html to rep/ */
305 if( f && f->name[strlen(f->name) - 1] == '/' )
307 char *psz_redir = strdup( f->name );
309 psz_redir[strlen( psz_redir ) - 1] = '\0';
311 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
312 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
315 if( b_index && ( p = strstr( f->file, "index." ) ) )
317 asprintf( &psz_redir, "%s%s", f->name, p );
319 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
320 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
321 f->name, psz_redir );
338 ACL_Destroy( p_acl );
345 /**************************************************************************
347 **************************************************************************/
348 char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 )
350 intf_sys_t *p_sys = p_intf->p_sys;
352 if ( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
354 size_t i_in = strlen(psz_utf8);
355 size_t i_out = i_in * 2;
356 char *psz_local = malloc(i_out + 1);
357 char *psz_out = psz_local;
359 char psz_tmp[i_in + 1];
360 const char *psz_in = psz_tmp;
361 strcpy( psz_tmp, psz_utf8 );
363 i_in = strlen( psz_tmp );
365 i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
367 if( i_ret == (size_t)-1 || i_in )
370 "failed to convert \"%s\" to desired charset (%s)",
371 psz_utf8, strerror(errno) );
373 return strdup( psz_utf8 );
380 return strdup( psz_utf8 );
383 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
385 intf_sys_t *p_sys = p_intf->p_sys;
387 if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
389 const char *psz_in = psz_local;
390 size_t i_in = strlen(psz_in);
391 size_t i_out = i_in * 6;
392 char *psz_utf8 = malloc(i_out + 1);
393 char *psz_out = psz_utf8;
395 size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
397 if( i_ret == (size_t)-1 || i_in )
400 "failed to convert \"%s\" to desired charset (%s)",
401 psz_local, strerror(errno) );
403 return strdup( psz_local );
410 return strdup( psz_local );
413 /*************************************************************************
415 *************************************************************************/
416 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
417 playlist_item_t *p_node, char *name, mvar_t *s,
422 if( p_node->i_children == -1 )
426 mvar_t *itm = E_(mvar_New)( name, "set" );
428 sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
429 E_(mvar_AppendNewVar)( itm, "current", value );
431 sprintf( value, "%d", p_node->input.i_id );
432 E_(mvar_AppendNewVar)( itm, "index", value );
434 psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
435 E_(mvar_AppendNewVar)( itm, "name", psz );
438 psz = E_(FromUTF8)( p_intf, p_node->input.psz_uri );
439 E_(mvar_AppendNewVar)( itm, "uri", psz );
442 sprintf( value, "Item");
443 E_(mvar_AppendNewVar)( itm, "type", value );
445 sprintf( value, "%d", i_depth );
446 E_(mvar_AppendNewVar)( itm, "depth", value );
448 if( p_node->i_flags & PLAYLIST_RO_FLAG )
450 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
454 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
457 sprintf( value, "%ld", (long)p_node->input.i_duration );
458 E_(mvar_AppendNewVar)( itm, "duration", value );
460 E_(mvar_AppendVar)( s, itm );
467 mvar_t *itm = E_(mvar_New)( name, "set" );
469 psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
470 E_(mvar_AppendNewVar)( itm, "name", psz );
471 E_(mvar_AppendNewVar)( itm, "uri", psz );
474 sprintf( value, "Node" );
475 E_(mvar_AppendNewVar)( itm, "type", value );
477 sprintf( value, "%d", p_node->input.i_id );
478 E_(mvar_AppendNewVar)( itm, "index", value );
480 sprintf( value, "%d", p_node->i_children);
481 E_(mvar_AppendNewVar)( itm, "i_children", value );
483 sprintf( value, "%d", i_depth );
484 E_(mvar_AppendNewVar)( itm, "depth", value );
486 if( p_node->i_flags & PLAYLIST_RO_FLAG )
488 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
492 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
495 E_(mvar_AppendVar)( s, itm );
497 for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
498 E_(PlaylistListNode)( p_intf, p_pl,
499 p_node->pp_children[i_child],
500 name, s, i_depth + 1);
506 /****************************************************************************
507 * Seek command parsing handling
508 ****************************************************************************/
509 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
511 intf_sys_t *p_sys = p_intf->p_sys;
517 #define POSITION_ABSOLUTE 12
518 #define POSITION_REL_FOR 13
519 #define POSITION_REL_BACK 11
520 #define VL_TIME_ABSOLUTE 0
521 #define VL_TIME_REL_FOR 1
522 #define VL_TIME_REL_BACK -1
525 var_Get( p_sys->p_input, "length", &val );
526 i_length = val.i_time;
528 while( p_value[0] != '\0' )
534 i_relative = VL_TIME_REL_FOR;
540 i_relative = VL_TIME_REL_BACK;
544 case '0': case '1': case '2': case '3': case '4':
545 case '5': case '6': case '7': case '8': case '9':
547 i_stock = strtol( p_value , &p_value , 10 );
550 case '%': /* for percentage ie position */
552 i_relative += POSITION_ABSOLUTE;
560 i_value = 60 * (i_value + i_stock) ;
565 case 'h': case 'H': /* hours */
567 i_value += 3600 * i_stock;
569 /* other characters which are not numbers are not
571 while( ((p_value[0] < '0') || (p_value[0] > '9'))
572 && (p_value[0] != '\0') )
578 case 'm': case 'M': case '\'': /* minutes */
580 i_value += 60 * i_stock;
583 while( ((p_value[0] < '0') || (p_value[0] > '9'))
584 && (p_value[0] != '\0') )
590 case 's': case 'S': case '"': /* seconds */
594 while( ((p_value[0] < '0') || (p_value[0] > '9'))
595 && (p_value[0] != '\0') )
609 /* if there is no known symbol, I consider it as seconds.
610 * Otherwise, i_stock = 0 */
615 case VL_TIME_ABSOLUTE:
617 if( (uint64_t)( i_value ) * 1000000 <= i_length )
618 val.i_time = (uint64_t)( i_value ) * 1000000;
620 val.i_time = i_length;
622 var_Set( p_sys->p_input, "time", val );
623 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
626 case VL_TIME_REL_FOR:
628 var_Get( p_sys->p_input, "time", &val );
629 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
631 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
634 val.i_time = i_length;
636 var_Set( p_sys->p_input, "time", val );
637 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
640 case VL_TIME_REL_BACK:
642 var_Get( p_sys->p_input, "time", &val );
643 if( (int64_t)( i_value ) * 1000000 > val.i_time )
648 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
650 var_Set( p_sys->p_input, "time", val );
651 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
654 case POSITION_ABSOLUTE:
656 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
658 var_Set( p_sys->p_input, "position", val );
659 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
662 case POSITION_REL_FOR:
664 var_Get( p_sys->p_input, "position", &val );
665 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
667 var_Set( p_sys->p_input, "position", val );
668 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
672 case POSITION_REL_BACK:
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 backward: %d%%",
684 msg_Dbg( p_intf, "invalid seek request" );
689 #undef POSITION_ABSOLUTE
690 #undef POSITION_REL_FOR
691 #undef POSITION_REL_BACK
692 #undef VL_TIME_ABSOLUTE
693 #undef VL_TIME_REL_FOR
694 #undef VL_TIME_REL_BACK
698 /****************************************************************************
699 * URI Parsing functions
700 ****************************************************************************/
701 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
705 while( (p = strstr( p, psz_name )) )
707 /* Verify that we are dealing with a post/get argument */
708 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
709 && p[strlen(psz_name)] == '=' )
718 char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
719 char *psz_value, int i_value_max )
723 while( (p = strstr( p, psz_name )) )
725 /* Verify that we are dealing with a post/get argument */
726 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
727 && p[strlen(psz_name)] == '=' )
736 p += strlen( psz_name );
739 if( strchr( p, '&' ) )
741 i_len = strchr( p, '&' ) - p;
745 /* for POST method */
746 if( strchr( p, '\n' ) )
748 i_len = strchr( p, '\n' ) - p;
749 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
756 i_len = __MIN( i_value_max - 1, i_len );
759 strncpy( psz_value, p, i_len );
760 psz_value[i_len] = '\0';
764 strncpy( psz_value, "", i_value_max );
770 strncpy( psz_value, "", i_value_max );
776 void E_(DecodeEncodedURI)( char *psz )
778 decode_encoded_URI( psz );
781 /* Since the resulting string is smaller we can work in place, so it is
782 * permitted to have psz == new. new points to the first word of the
783 * string, the function returns the remaining string. */
784 char *E_(FirstWord)( char *psz, char *new )
791 while( *psz != '\0' && *psz != ' ' )
796 while( *psz != '\0' && *psz != c )
798 if( *psz == '\\' && psz[1] != '\0' )
807 if( *psz == '\\' && psz[1] != '\0' )
820 /**********************************************************************
821 * Find_end_MRL: Find the end of the sentence :
822 * this function parses the string psz and find the end of the item
823 * and/or option with detecting the " and ' problems.
824 * returns NULL if an error is detected, otherwise, returns a pointer
825 * of the end of the sentence (after the last character)
826 **********************************************************************/
827 static char *Find_end_MRL( char *psz )
837 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
839 if( *s_sent == '\'' )
841 s_sent = Find_end_MRL( s_sent );
853 if( *s_sent == '\"' )
857 } else /* *s_sent == '\0' , which means the number of " is incorrect */
867 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
869 if( *s_sent == '\"' )
871 s_sent = Find_end_MRL( s_sent );
883 if( *s_sent == '\'' )
887 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
893 default: /* now we can look for spaces */
895 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
897 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
899 s_sent = Find_end_MRL( s_sent );
909 /**********************************************************************
910 * parse_MRL: parse the MRL, find the mrl string and the options,
911 * create an item with all information in it, and return the item.
912 * return NULL if there is an error.
913 **********************************************************************/
914 playlist_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *psz,
917 char **ppsz_options = NULL;
924 playlist_item_t * p_item = NULL;
926 /* In case there is spaces before the mrl */
927 while( *s_mrl == ' ' )
930 /* extract the mrl */
931 s_temp = strstr( s_mrl , " :" );
934 s_temp = s_mrl + strlen( s_mrl );
937 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
944 /* if the mrl is between " or ', we must remove them */
945 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
947 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
948 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
949 mrl[ s_temp - s_mrl - 2 ] = '\0';
952 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
953 strncpy( mrl , s_mrl , s_temp - s_mrl );
954 mrl[ s_temp - s_mrl ] = '\0';
959 /* now we can take care of the options */
960 while( (*s_mrl != '\0') && (i_error == 0) )
969 case ':': /* an option */
971 s_temp = Find_end_MRL( s_mrl );
980 ppsz_options = realloc( ppsz_options , i_options *
982 ppsz_options[ i_options - 1 ] =
983 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
985 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
988 /* don't forget to finish the string with a '\0' */
989 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
1009 /* now create an item */
1010 p_item = playlist_ItemNew( p_intf, mrl, psz_name );
1011 for( i = 0 ; i< i_options ; i++ )
1013 playlist_ItemAddOption( p_item, ppsz_options[i] );
1017 for( i = 0; i < i_options; i++ ) free( ppsz_options[i] );
1018 if( i_options ) free( ppsz_options );
1022 /**********************************************************************
1023 * RealPath: parse ../, ~ and path stuff
1024 **********************************************************************/
1025 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
1029 int i_len = strlen(psz_src);
1032 #if defined( WIN32 )
1038 psz_dir = malloc( i_len + 2 );
1039 strcpy( psz_dir, psz_src );
1041 /* Add a trailing sep to ease the .. step */
1042 psz_dir[i_len] = sep;
1043 psz_dir[i_len + 1] = '\0';
1046 /* Convert all / to native separator */
1048 while( (p = strchr( p, '/' )) != NULL )
1054 /* Remove multiple separators and /./ */
1056 while( (p = strchr( p, sep )) != NULL )
1059 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
1060 else if( p[1] == '.' && p[2] == sep )
1061 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
1066 if( psz_dir[0] == '~' )
1068 char *dir = malloc( strlen(psz_dir)
1069 + strlen(p_intf->p_vlc->psz_userdir) );
1070 /* This is incomplete : we should also support the ~cmassiot/ syntax. */
1071 sprintf( dir, "%s%s", p_intf->p_vlc->psz_userdir, psz_dir + 1 );
1076 if( strlen(psz_dir) > 2 )
1078 /* Fix all .. dir */
1080 while( (p = strchr( p, sep )) != NULL )
1082 if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
1086 if( (q = strrchr( psz_dir, sep )) != NULL )
1088 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
1093 memmove( psz_dir, p, strlen(p) + 1 );
1102 /* Remove trailing sep if there are at least 2 sep in the string
1103 * (handles the C:\ stuff) */
1104 p = strrchr( psz_dir, sep );
1105 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )