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 dir (%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 dir (%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 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
267 f->b_html ? ( strstr( &dir[strlen( psz_root )], ".xml" ) ? "text/xml; charset=UTF-8" : p_sys->psz_html_type ) :
269 user, password, p_acl,
270 E_(HttpCallback), f );
271 if( f->p_file != NULL )
273 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
278 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
280 user, password, p_acl,
281 E_(HandlerCallback), h );
282 if( h->p_handler != NULL )
284 TAB_APPEND( p_sys->i_files, p_sys->pp_files,
285 (httpd_file_sys_t *)h );
289 /* for url that ends by / add
290 * - a redirect from rep to rep/
291 * - in case of index.* rep/index.html to rep/ */
292 if( f && f->name[strlen(f->name) - 1] == '/' )
294 char *psz_redir = strdup( f->name );
296 psz_redir[strlen( psz_redir ) - 1] = '\0';
298 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
299 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
302 if( b_index && ( p = strstr( f->file, "index." ) ) )
304 asprintf( &psz_redir, "%s%s", f->name, p );
306 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
307 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
308 f->name, psz_redir );
325 ACL_Destroy( p_acl );
332 /**************************************************************************
334 **************************************************************************/
335 char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 )
337 intf_sys_t *p_sys = p_intf->p_sys;
339 if ( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
341 size_t i_in = strlen(psz_utf8);
342 size_t i_out = i_in * 2;
343 char *psz_local = malloc(i_out + 1);
344 char *psz_out = psz_local;
346 char psz_tmp[i_in + 1];
347 char *psz_in = psz_tmp;
348 uint8_t *p = (uint8_t *)psz_tmp;
349 strcpy( psz_tmp, psz_utf8 );
351 /* Fix Unicode quotes. If we are here we are probably converting
352 * to an inferior charset not understanding Unicode quotes. */
355 if( p[0] == 0xe2 && p[1] == 0x80 && p[2] == 0x99 )
358 memmove( &p[1], &p[3], strlen((char *)&p[3]) + 1 );
360 if( p[0] == 0xe2 && p[1] == 0x80 && p[2] == 0x9a )
363 memmove( &p[1], &p[3], strlen((char *)&p[3]) + 1 );
367 i_in = strlen( psz_tmp );
369 i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
371 if( i_ret == (size_t)-1 || i_in )
374 "failed to convert \"%s\" to desired charset (%s)",
375 psz_utf8, strerror(errno) );
377 return strdup( psz_utf8 );
384 return strdup( psz_utf8 );
387 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
389 intf_sys_t *p_sys = p_intf->p_sys;
391 if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
393 char *psz_in = psz_local;
394 size_t i_in = strlen(psz_in);
395 size_t i_out = i_in * 6;
396 char *psz_utf8 = malloc(i_out + 1);
397 char *psz_out = psz_utf8;
399 size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
401 if( i_ret == (size_t)-1 || i_in )
404 "failed to convert \"%s\" to desired charset (%s)",
405 psz_local, strerror(errno) );
407 return strdup( psz_local );
414 return strdup( psz_local );
417 /*************************************************************************
419 *************************************************************************/
420 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
421 playlist_item_t *p_node, char *name, mvar_t *s,
426 if( p_node->i_children == -1 )
430 mvar_t *itm = E_(mvar_New)( name, "set" );
432 sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
433 E_(mvar_AppendNewVar)( itm, "current", value );
435 sprintf( value, "%d", p_node->input.i_id );
436 E_(mvar_AppendNewVar)( itm, "index", value );
438 psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
439 E_(mvar_AppendNewVar)( itm, "name", psz );
442 psz = E_(FromUTF8)( p_intf, p_node->input.psz_uri );
443 E_(mvar_AppendNewVar)( itm, "uri", psz );
446 sprintf( value, "Item");
447 E_(mvar_AppendNewVar)( itm, "type", value );
449 sprintf( value, "%d", i_depth );
450 E_(mvar_AppendNewVar)( itm, "depth", value );
452 if( p_node->i_flags & PLAYLIST_RO_FLAG )
454 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
458 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
461 sprintf( value, "%d", p_node->input.i_duration );
462 E_(mvar_AppendNewVar)( itm, "duration", value );
464 E_(mvar_AppendVar)( s, itm );
471 mvar_t *itm = E_(mvar_New)( name, "set" );
473 psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
474 E_(mvar_AppendNewVar)( itm, "name", psz );
475 E_(mvar_AppendNewVar)( itm, "uri", psz );
478 sprintf( value, "Node" );
479 E_(mvar_AppendNewVar)( itm, "type", value );
481 sprintf( value, "%d", p_node->input.i_id );
482 E_(mvar_AppendNewVar)( itm, "index", value );
484 sprintf( value, "%d", p_node->i_children);
485 E_(mvar_AppendNewVar)( itm, "i_children", value );
487 sprintf( value, "%d", i_depth );
488 E_(mvar_AppendNewVar)( itm, "depth", value );
490 if( p_node->i_flags & PLAYLIST_RO_FLAG )
492 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
496 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
499 E_(mvar_AppendVar)( s, itm );
501 for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
502 E_(PlaylistListNode)( p_intf, p_pl,
503 p_node->pp_children[i_child],
504 name, s, i_depth + 1);
510 /****************************************************************************
511 * Seek command parsing handling
512 ****************************************************************************/
513 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
515 intf_sys_t *p_sys = p_intf->p_sys;
521 #define POSITION_ABSOLUTE 12
522 #define POSITION_REL_FOR 13
523 #define POSITION_REL_BACK 11
524 #define VL_TIME_ABSOLUTE 0
525 #define VL_TIME_REL_FOR 1
526 #define VL_TIME_REL_BACK -1
529 var_Get( p_sys->p_input, "length", &val );
530 i_length = val.i_time;
532 while( p_value[0] != '\0' )
538 i_relative = VL_TIME_REL_FOR;
544 i_relative = VL_TIME_REL_BACK;
548 case '0': case '1': case '2': case '3': case '4':
549 case '5': case '6': case '7': case '8': case '9':
551 i_stock = strtol( p_value , &p_value , 10 );
554 case '%': /* for percentage ie position */
556 i_relative += POSITION_ABSOLUTE;
564 i_value = 60 * (i_value + i_stock) ;
569 case 'h': case 'H': /* hours */
571 i_value += 3600 * i_stock;
573 /* other characters which are not numbers are not important */
574 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
580 case 'm': case 'M': case '\'': /* minutes */
582 i_value += 60 * i_stock;
585 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
591 case 's': case 'S': case '"': /* seconds */
595 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
609 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
614 case VL_TIME_ABSOLUTE:
616 if( (uint64_t)( i_value ) * 1000000 <= i_length )
617 val.i_time = (uint64_t)( i_value ) * 1000000;
619 val.i_time = i_length;
621 var_Set( p_sys->p_input, "time", val );
622 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
625 case VL_TIME_REL_FOR:
627 var_Get( p_sys->p_input, "time", &val );
628 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
630 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
633 val.i_time = i_length;
635 var_Set( p_sys->p_input, "time", val );
636 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
639 case VL_TIME_REL_BACK:
641 var_Get( p_sys->p_input, "time", &val );
642 if( (int64_t)( i_value ) * 1000000 > val.i_time )
647 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
649 var_Set( p_sys->p_input, "time", val );
650 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
653 case POSITION_ABSOLUTE:
655 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
656 var_Set( p_sys->p_input, "position", val );
657 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
660 case POSITION_REL_FOR:
662 var_Get( p_sys->p_input, "position", &val );
663 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
664 var_Set( p_sys->p_input, "position", val );
665 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
668 case POSITION_REL_BACK:
670 var_Get( p_sys->p_input, "position", &val );
671 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
672 var_Set( p_sys->p_input, "position", val );
673 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
678 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
683 #undef POSITION_ABSOLUTE
684 #undef POSITION_REL_FOR
685 #undef POSITION_REL_BACK
686 #undef VL_TIME_ABSOLUTE
687 #undef VL_TIME_REL_FOR
688 #undef VL_TIME_REL_BACK
692 /****************************************************************************
693 * URI Parsing functions
694 ****************************************************************************/
695 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
699 while( (p = strstr( p, psz_name )) )
701 /* Verify that we are dealing with a post/get argument */
702 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
703 && p[strlen(psz_name)] == '=' )
712 char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
713 char *psz_value, int i_value_max )
717 while( (p = strstr( p, psz_name )) )
719 /* Verify that we are dealing with a post/get argument */
720 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
721 && p[strlen(psz_name)] == '=' )
730 p += strlen( psz_name );
733 if( strchr( p, '&' ) )
735 i_len = strchr( p, '&' ) - p;
739 /* for POST method */
740 if( strchr( p, '\n' ) )
742 i_len = strchr( p, '\n' ) - p;
743 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
750 i_len = __MIN( i_value_max - 1, i_len );
753 strncpy( psz_value, p, i_len );
754 psz_value[i_len] = '\0';
758 strncpy( psz_value, "", i_value_max );
764 strncpy( psz_value, "", i_value_max );
770 void E_(DecodeEncodedURI)( char *psz )
772 decode_encoded_URI( psz );
775 /* Since the resulting string is smaller we can work in place, so it is
776 * permitted to have psz == new. new points to the first word of the
777 * string, the function returns the remaining string. */
778 char *E_(FirstWord)( char *psz, char *new )
785 while( *psz != '\0' && *psz != ' ' )
790 while( *psz != '\0' && *psz != c )
792 if( *psz == '\\' && psz[1] != '\0' )
801 if( *psz == '\\' && psz[1] != '\0' )
814 /**********************************************************************
815 * Find_end_MRL: Find the end of the sentence :
816 * this function parses the string psz and find the end of the item
817 * and/or option with detecting the " and ' problems.
818 * returns NULL if an error is detected, otherwise, returns a pointer
819 * of the end of the sentence (after the last character)
820 **********************************************************************/
821 static char *Find_end_MRL( char *psz )
831 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
833 if( *s_sent == '\'' )
835 s_sent = Find_end_MRL( s_sent );
847 if( *s_sent == '\"' )
851 } else /* *s_sent == '\0' , which means the number of " is incorrect */
861 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
863 if( *s_sent == '\"' )
865 s_sent = Find_end_MRL( s_sent );
877 if( *s_sent == '\'' )
881 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
887 default: /* now we can look for spaces */
889 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
891 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
893 s_sent = Find_end_MRL( s_sent );
903 /**********************************************************************
904 * parse_MRL: parse the MRL, find the mrl string and the options,
905 * create an item with all information in it, and return the item.
906 * return NULL if there is an error.
907 **********************************************************************/
908 playlist_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *psz,
911 char **ppsz_options = NULL;
918 playlist_item_t * p_item = NULL;
920 /* In case there is spaces before the mrl */
921 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
926 /* extract the mrl */
927 s_temp = strstr( s_mrl , " :" );
930 s_temp = s_mrl + strlen( s_mrl );
933 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
940 /* if the mrl is between " or ', we must remove them */
941 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
943 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
944 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
945 mrl[ s_temp - s_mrl - 2 ] = '\0';
948 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
949 strncpy( mrl , s_mrl , s_temp - s_mrl );
950 mrl[ s_temp - s_mrl ] = '\0';
955 /* now we can take care of the options */
956 while( (*s_mrl != '\0') && (i_error == 0) )
965 case ':': /* an option */
967 s_temp = Find_end_MRL( s_mrl );
976 ppsz_options = realloc( ppsz_options , i_options *
978 ppsz_options[ i_options - 1 ] =
979 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
981 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
984 /* don't forget to finish the string with a '\0' */
985 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
1005 /* now create an item */
1006 p_item = playlist_ItemNew( p_intf, mrl, psz_name );
1007 for( i = 0 ; i< i_options ; i++ )
1009 playlist_ItemAddOption( p_item, ppsz_options[i] );
1013 for( i = 0; i < i_options; i++ ) free( ppsz_options[i] );
1014 if( i_options ) free( ppsz_options );
1018 /**********************************************************************
1019 * RealPath: parse ../, ~ and path stuff
1020 **********************************************************************/
1021 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
1025 int i_len = strlen(psz_src);
1028 #if defined( WIN32 )
1034 psz_dir = malloc( i_len + 2 );
1035 strcpy( psz_dir, psz_src );
1037 /* Add a trailing sep to ease the .. step */
1038 psz_dir[i_len] = sep;
1039 psz_dir[i_len + 1] = '\0';
1042 /* Convert all / to native separator */
1044 while( (p = strchr( p, '/' )) != NULL )
1050 /* Remove multiple separators and /./ */
1052 while( (p = strchr( p, sep )) != NULL )
1055 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
1056 else if( p[1] == '.' && p[2] == sep )
1057 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
1062 if( psz_dir[0] == '~' )
1064 char *dir = malloc( strlen(psz_dir)
1065 + strlen(p_intf->p_vlc->psz_userdir) );
1066 /* This is incomplete : we should also support the ~cmassiot/ syntax. */
1067 sprintf( dir, "%s%s", p_intf->p_vlc->psz_userdir, psz_dir + 1 );
1072 if( strlen(psz_dir) > 2 )
1074 /* Fix all .. dir */
1076 while( (p = strchr( p, sep )) != NULL )
1078 if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
1082 if( (q = strrchr( psz_dir, sep )) != NULL )
1084 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
1089 memmove( psz_dir, p, strlen(p) + 1 );
1098 /* Remove trailing sep if there are at least 2 sep in the string
1099 * (handles the C:\ stuff) */
1100 p = strrchr( psz_dir, sep );
1101 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )