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 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 char *psz_in = psz_tmp;
361 uint8_t *p = (uint8_t *)psz_tmp;
362 strcpy( psz_tmp, psz_utf8 );
364 /* Fix Unicode quotes. If we are here we are probably converting
365 * to an inferior charset not understanding Unicode quotes. */
368 if( p[0] == 0xe2 && p[1] == 0x80 && p[2] == 0x99 )
371 memmove( &p[1], &p[3], strlen((char *)&p[3]) + 1 );
373 if( p[0] == 0xe2 && p[1] == 0x80 && p[2] == 0x9a )
376 memmove( &p[1], &p[3], strlen((char *)&p[3]) + 1 );
380 i_in = strlen( psz_tmp );
382 i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
384 if( i_ret == (size_t)-1 || i_in )
387 "failed to convert \"%s\" to desired charset (%s)",
388 psz_utf8, strerror(errno) );
390 return strdup( psz_utf8 );
397 return strdup( psz_utf8 );
400 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
402 intf_sys_t *p_sys = p_intf->p_sys;
404 if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
406 char *psz_in = psz_local;
407 size_t i_in = strlen(psz_in);
408 size_t i_out = i_in * 6;
409 char *psz_utf8 = malloc(i_out + 1);
410 char *psz_out = psz_utf8;
412 size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
414 if( i_ret == (size_t)-1 || i_in )
417 "failed to convert \"%s\" to desired charset (%s)",
418 psz_local, strerror(errno) );
420 return strdup( psz_local );
427 return strdup( psz_local );
430 /*************************************************************************
432 *************************************************************************/
433 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
434 playlist_item_t *p_node, char *name, mvar_t *s,
439 if( p_node->i_children == -1 )
443 mvar_t *itm = E_(mvar_New)( name, "set" );
445 sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
446 E_(mvar_AppendNewVar)( itm, "current", value );
448 sprintf( value, "%d", p_node->input.i_id );
449 E_(mvar_AppendNewVar)( itm, "index", value );
451 psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
452 E_(mvar_AppendNewVar)( itm, "name", psz );
455 psz = E_(FromUTF8)( p_intf, p_node->input.psz_uri );
456 E_(mvar_AppendNewVar)( itm, "uri", psz );
459 sprintf( value, "Item");
460 E_(mvar_AppendNewVar)( itm, "type", value );
462 sprintf( value, "%d", i_depth );
463 E_(mvar_AppendNewVar)( itm, "depth", value );
465 if( p_node->i_flags & PLAYLIST_RO_FLAG )
467 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
471 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
474 sprintf( value, "%d", p_node->input.i_duration );
475 E_(mvar_AppendNewVar)( itm, "duration", value );
477 E_(mvar_AppendVar)( s, itm );
484 mvar_t *itm = E_(mvar_New)( name, "set" );
486 psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
487 E_(mvar_AppendNewVar)( itm, "name", psz );
488 E_(mvar_AppendNewVar)( itm, "uri", psz );
491 sprintf( value, "Node" );
492 E_(mvar_AppendNewVar)( itm, "type", value );
494 sprintf( value, "%d", p_node->input.i_id );
495 E_(mvar_AppendNewVar)( itm, "index", value );
497 sprintf( value, "%d", p_node->i_children);
498 E_(mvar_AppendNewVar)( itm, "i_children", value );
500 sprintf( value, "%d", i_depth );
501 E_(mvar_AppendNewVar)( itm, "depth", value );
503 if( p_node->i_flags & PLAYLIST_RO_FLAG )
505 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
509 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
512 E_(mvar_AppendVar)( s, itm );
514 for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
515 E_(PlaylistListNode)( p_intf, p_pl,
516 p_node->pp_children[i_child],
517 name, s, i_depth + 1);
523 /****************************************************************************
524 * Seek command parsing handling
525 ****************************************************************************/
526 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
528 intf_sys_t *p_sys = p_intf->p_sys;
534 #define POSITION_ABSOLUTE 12
535 #define POSITION_REL_FOR 13
536 #define POSITION_REL_BACK 11
537 #define VL_TIME_ABSOLUTE 0
538 #define VL_TIME_REL_FOR 1
539 #define VL_TIME_REL_BACK -1
542 var_Get( p_sys->p_input, "length", &val );
543 i_length = val.i_time;
545 while( p_value[0] != '\0' )
551 i_relative = VL_TIME_REL_FOR;
557 i_relative = VL_TIME_REL_BACK;
561 case '0': case '1': case '2': case '3': case '4':
562 case '5': case '6': case '7': case '8': case '9':
564 i_stock = strtol( p_value , &p_value , 10 );
567 case '%': /* for percentage ie position */
569 i_relative += POSITION_ABSOLUTE;
577 i_value = 60 * (i_value + i_stock) ;
582 case 'h': case 'H': /* hours */
584 i_value += 3600 * i_stock;
586 /* other characters which are not numbers are not important */
587 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
593 case 'm': case 'M': case '\'': /* minutes */
595 i_value += 60 * i_stock;
598 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
604 case 's': case 'S': case '"': /* seconds */
608 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
622 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
627 case VL_TIME_ABSOLUTE:
629 if( (uint64_t)( i_value ) * 1000000 <= i_length )
630 val.i_time = (uint64_t)( i_value ) * 1000000;
632 val.i_time = i_length;
634 var_Set( p_sys->p_input, "time", val );
635 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
638 case VL_TIME_REL_FOR:
640 var_Get( p_sys->p_input, "time", &val );
641 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
643 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
646 val.i_time = i_length;
648 var_Set( p_sys->p_input, "time", val );
649 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
652 case VL_TIME_REL_BACK:
654 var_Get( p_sys->p_input, "time", &val );
655 if( (int64_t)( i_value ) * 1000000 > val.i_time )
660 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
662 var_Set( p_sys->p_input, "time", val );
663 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
666 case POSITION_ABSOLUTE:
668 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
669 var_Set( p_sys->p_input, "position", val );
670 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
673 case POSITION_REL_FOR:
675 var_Get( p_sys->p_input, "position", &val );
676 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
677 var_Set( p_sys->p_input, "position", val );
678 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
681 case POSITION_REL_BACK:
683 var_Get( p_sys->p_input, "position", &val );
684 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
685 var_Set( p_sys->p_input, "position", val );
686 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
691 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
696 #undef POSITION_ABSOLUTE
697 #undef POSITION_REL_FOR
698 #undef POSITION_REL_BACK
699 #undef VL_TIME_ABSOLUTE
700 #undef VL_TIME_REL_FOR
701 #undef VL_TIME_REL_BACK
705 /****************************************************************************
706 * URI Parsing functions
707 ****************************************************************************/
708 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
712 while( (p = strstr( p, psz_name )) )
714 /* Verify that we are dealing with a post/get argument */
715 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
716 && p[strlen(psz_name)] == '=' )
725 char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
726 char *psz_value, int i_value_max )
730 while( (p = strstr( p, psz_name )) )
732 /* Verify that we are dealing with a post/get argument */
733 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
734 && p[strlen(psz_name)] == '=' )
743 p += strlen( psz_name );
746 if( strchr( p, '&' ) )
748 i_len = strchr( p, '&' ) - p;
752 /* for POST method */
753 if( strchr( p, '\n' ) )
755 i_len = strchr( p, '\n' ) - p;
756 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
763 i_len = __MIN( i_value_max - 1, i_len );
766 strncpy( psz_value, p, i_len );
767 psz_value[i_len] = '\0';
771 strncpy( psz_value, "", i_value_max );
777 strncpy( psz_value, "", i_value_max );
783 void E_(DecodeEncodedURI)( char *psz )
785 decode_encoded_URI( psz );
788 /* Since the resulting string is smaller we can work in place, so it is
789 * permitted to have psz == new. new points to the first word of the
790 * string, the function returns the remaining string. */
791 char *E_(FirstWord)( char *psz, char *new )
798 while( *psz != '\0' && *psz != ' ' )
803 while( *psz != '\0' && *psz != c )
805 if( *psz == '\\' && psz[1] != '\0' )
814 if( *psz == '\\' && psz[1] != '\0' )
827 /**********************************************************************
828 * Find_end_MRL: Find the end of the sentence :
829 * this function parses the string psz and find the end of the item
830 * and/or option with detecting the " and ' problems.
831 * returns NULL if an error is detected, otherwise, returns a pointer
832 * of the end of the sentence (after the last character)
833 **********************************************************************/
834 static char *Find_end_MRL( char *psz )
844 while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
846 if( *s_sent == '\'' )
848 s_sent = Find_end_MRL( s_sent );
860 if( *s_sent == '\"' )
864 } else /* *s_sent == '\0' , which means the number of " is incorrect */
874 while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
876 if( *s_sent == '\"' )
878 s_sent = Find_end_MRL( s_sent );
890 if( *s_sent == '\'' )
894 } else /* *s_sent == '\0' , which means the number of ' is incorrect */
900 default: /* now we can look for spaces */
902 while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
904 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
906 s_sent = Find_end_MRL( s_sent );
916 /**********************************************************************
917 * parse_MRL: parse the MRL, find the mrl string and the options,
918 * create an item with all information in it, and return the item.
919 * return NULL if there is an error.
920 **********************************************************************/
921 playlist_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *psz,
924 char **ppsz_options = NULL;
931 playlist_item_t * p_item = NULL;
933 /* In case there is spaces before the mrl */
934 while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
939 /* extract the mrl */
940 s_temp = strstr( s_mrl , " :" );
943 s_temp = s_mrl + strlen( s_mrl );
946 while( (*s_temp == ' ') && (s_temp != s_mrl ) )
953 /* if the mrl is between " or ', we must remove them */
954 if( (*s_mrl == '\'') || (*s_mrl == '\"') )
956 mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
957 strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
958 mrl[ s_temp - s_mrl - 2 ] = '\0';
961 mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
962 strncpy( mrl , s_mrl , s_temp - s_mrl );
963 mrl[ s_temp - s_mrl ] = '\0';
968 /* now we can take care of the options */
969 while( (*s_mrl != '\0') && (i_error == 0) )
978 case ':': /* an option */
980 s_temp = Find_end_MRL( s_mrl );
989 ppsz_options = realloc( ppsz_options , i_options *
991 ppsz_options[ i_options - 1 ] =
992 malloc( (s_temp - s_mrl + 1) * sizeof(char) );
994 strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
997 /* don't forget to finish the string with a '\0' */
998 (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
1018 /* now create an item */
1019 p_item = playlist_ItemNew( p_intf, mrl, psz_name );
1020 for( i = 0 ; i< i_options ; i++ )
1022 playlist_ItemAddOption( p_item, ppsz_options[i] );
1026 for( i = 0; i < i_options; i++ ) free( ppsz_options[i] );
1027 if( i_options ) free( ppsz_options );
1031 /**********************************************************************
1032 * RealPath: parse ../, ~ and path stuff
1033 **********************************************************************/
1034 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
1038 int i_len = strlen(psz_src);
1041 #if defined( WIN32 )
1047 psz_dir = malloc( i_len + 2 );
1048 strcpy( psz_dir, psz_src );
1050 /* Add a trailing sep to ease the .. step */
1051 psz_dir[i_len] = sep;
1052 psz_dir[i_len + 1] = '\0';
1055 /* Convert all / to native separator */
1057 while( (p = strchr( p, '/' )) != NULL )
1063 /* Remove multiple separators and /./ */
1065 while( (p = strchr( p, sep )) != NULL )
1068 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
1069 else if( p[1] == '.' && p[2] == sep )
1070 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
1075 if( psz_dir[0] == '~' )
1077 char *dir = malloc( strlen(psz_dir)
1078 + strlen(p_intf->p_vlc->psz_userdir) );
1079 /* This is incomplete : we should also support the ~cmassiot/ syntax. */
1080 sprintf( dir, "%s%s", p_intf->p_vlc->psz_userdir, psz_dir + 1 );
1085 if( strlen(psz_dir) > 2 )
1087 /* Fix all .. dir */
1089 while( (p = strchr( p, sep )) != NULL )
1091 if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
1095 if( (q = strrchr( psz_dir, sep )) != NULL )
1097 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
1102 memmove( psz_dir, p, strlen(p) + 1 );
1111 /* Remove trailing sep if there are at least 2 sep in the string
1112 * (handles the C:\ stuff) */
1113 p = strrchr( psz_dir, sep );
1114 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )