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 free( (char*) 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_name, *psz_ext;
218 psz_name = E_(FileToUrl)( &dir[strlen( psz_root )], &b_index );
219 psz_ext = strrchr( dir, '.' );
220 if( psz_ext != NULL )
224 for( i = 0; i < p_sys->i_handlers; i++ )
225 if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
227 if( i < p_sys->i_handlers )
229 f = malloc( sizeof( httpd_handler_sys_t ) );
230 h = (httpd_handler_sys_t *)f;
231 f->b_handler = VLC_TRUE;
232 h->p_association = p_sys->pp_handlers[i];
237 f = malloc( sizeof( httpd_file_sys_t ) );
238 f->b_handler = VLC_FALSE;
245 f->file = strdup (dir);
247 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? VLC_TRUE : VLC_FALSE;
251 msg_Err( p_intf , "unable to parse directory" );
254 return( VLC_ENOMEM );
256 msg_Dbg( p_intf, "file=%s (url=%s)",
261 char *psz_type = strdup( p_sys->psz_html_type );
262 if( strstr( &dir[strlen( psz_root )], ".xml" ) )
264 char *psz = strstr( psz_type, "html;" );
274 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
276 f->b_html ? psz_type : NULL,
277 user, password, p_acl,
278 E_(HttpCallback), f );
280 if( f->p_file != NULL )
282 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
287 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
289 user, password, p_acl,
290 E_(HandlerCallback), h );
291 if( h->p_handler != NULL )
293 TAB_APPEND( p_sys->i_files, p_sys->pp_files,
294 (httpd_file_sys_t *)h );
298 /* for url that ends by / add
299 * - a redirect from rep to rep/
300 * - in case of index.* rep/index.html to rep/ */
301 if( f && f->name[strlen(f->name) - 1] == '/' )
303 char *psz_redir = strdup( f->name );
305 psz_redir[strlen( psz_redir ) - 1] = '\0';
307 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
308 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
311 if( b_index && ( p = strstr( f->file, "index." ) ) )
313 asprintf( &psz_redir, "%s%s", f->name, p );
315 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
316 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
317 f->name, psz_redir );
334 ACL_Destroy( p_acl );
341 /**************************************************************************
343 **************************************************************************/
344 char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 )
346 intf_sys_t *p_sys = p_intf->p_sys;
348 if ( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
350 size_t i_in = strlen(psz_utf8);
351 size_t i_out = i_in * 2;
352 char *psz_local = malloc(i_out + 1);
353 char *psz_out = psz_local;
355 char psz_tmp[i_in + 1];
356 const char *psz_in = psz_tmp;
357 strcpy( psz_tmp, psz_utf8 );
359 i_in = strlen( psz_tmp );
361 i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
363 if( i_ret == (size_t)-1 || i_in )
366 "failed to convert \"%s\" to desired charset (%s)",
367 psz_utf8, strerror(errno) );
369 return strdup( psz_utf8 );
376 return strdup( psz_utf8 );
379 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
381 intf_sys_t *p_sys = p_intf->p_sys;
383 if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
385 const char *psz_in = psz_local;
386 size_t i_in = strlen(psz_in);
387 size_t i_out = i_in * 6;
388 char *psz_utf8 = malloc(i_out + 1);
389 char *psz_out = psz_utf8;
391 size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
393 if( i_ret == (size_t)-1 || i_in )
396 "failed to convert \"%s\" to desired charset (%s)",
397 psz_local, strerror(errno) );
399 return strdup( psz_local );
406 return strdup( psz_local );
409 /*************************************************************************
411 *************************************************************************/
412 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
413 playlist_item_t *p_node, char *name, mvar_t *s,
418 if( p_node->i_children == -1 )
422 mvar_t *itm = E_(mvar_New)( name, "set" );
424 if( p_pl->status.p_item && p_node &&
425 p_pl->status.p_item->p_input && p_node->p_input &&
426 p_pl->status.p_item->p_input->i_id == p_node->p_input->i_id )
428 E_(mvar_AppendNewVar)( itm, "current", "1" );
432 E_(mvar_AppendNewVar)( itm, "current", "0" );
435 sprintf( value, "%d", p_node->p_input->i_id );
436 E_(mvar_AppendNewVar)( itm, "index", value );
438 psz = E_(FromUTF8)( p_intf, p_node->p_input->psz_name );
439 E_(mvar_AppendNewVar)( itm, "name", psz );
442 psz = E_(FromUTF8)( p_intf, p_node->p_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, "%ld", (long)p_node->p_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->p_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->p_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
575 while( ((p_value[0] < '0') || (p_value[0] > '9'))
576 && (p_value[0] != '\0') )
582 case 'm': case 'M': case '\'': /* minutes */
584 i_value += 60 * i_stock;
587 while( ((p_value[0] < '0') || (p_value[0] > '9'))
588 && (p_value[0] != '\0') )
594 case 's': case 'S': case '"': /* seconds */
598 while( ((p_value[0] < '0') || (p_value[0] > '9'))
599 && (p_value[0] != '\0') )
613 /* if there is no known symbol, I consider it as seconds.
614 * Otherwise, i_stock = 0 */
619 case VL_TIME_ABSOLUTE:
621 if( (uint64_t)( i_value ) * 1000000 <= i_length )
622 val.i_time = (uint64_t)( i_value ) * 1000000;
624 val.i_time = i_length;
626 var_Set( p_sys->p_input, "time", val );
627 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
630 case VL_TIME_REL_FOR:
632 var_Get( p_sys->p_input, "time", &val );
633 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
635 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
638 val.i_time = i_length;
640 var_Set( p_sys->p_input, "time", val );
641 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
644 case VL_TIME_REL_BACK:
646 var_Get( p_sys->p_input, "time", &val );
647 if( (int64_t)( i_value ) * 1000000 > val.i_time )
652 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
654 var_Set( p_sys->p_input, "time", val );
655 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
658 case POSITION_ABSOLUTE:
660 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
662 var_Set( p_sys->p_input, "position", val );
663 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
666 case POSITION_REL_FOR:
668 var_Get( p_sys->p_input, "position", &val );
669 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
671 var_Set( p_sys->p_input, "position", val );
672 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
676 case POSITION_REL_BACK:
678 var_Get( p_sys->p_input, "position", &val );
679 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
681 var_Set( p_sys->p_input, "position", val );
682 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
688 msg_Dbg( p_intf, "invalid seek request" );
693 #undef POSITION_ABSOLUTE
694 #undef POSITION_REL_FOR
695 #undef POSITION_REL_BACK
696 #undef VL_TIME_ABSOLUTE
697 #undef VL_TIME_REL_FOR
698 #undef VL_TIME_REL_BACK
702 /****************************************************************************
703 * URI Parsing functions
704 ****************************************************************************/
705 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
709 while( (p = strstr( p, psz_name )) )
711 /* Verify that we are dealing with a post/get argument */
712 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
713 && p[strlen(psz_name)] == '=' )
722 char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
723 char *psz_value, int i_value_max )
727 while( (p = strstr( p, psz_name )) )
729 /* Verify that we are dealing with a post/get argument */
730 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
731 && p[strlen(psz_name)] == '=' )
740 p += strlen( psz_name );
743 if( strchr( p, '&' ) )
745 i_len = strchr( p, '&' ) - p;
749 /* for POST method */
750 if( strchr( p, '\n' ) )
752 i_len = strchr( p, '\n' ) - p;
753 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
760 i_len = __MIN( i_value_max - 1, i_len );
763 strncpy( psz_value, p, i_len );
764 psz_value[i_len] = '\0';
768 strncpy( psz_value, "", i_value_max );
774 strncpy( psz_value, "", i_value_max );
780 /* Since the resulting string is smaller we can work in place, so it is
781 * permitted to have psz == new. new points to the first word of the
782 * string, the function returns the remaining string. */
783 char *E_(FirstWord)( char *psz, char *new )
790 while( *psz != '\0' && *psz != ' ' )
795 while( *psz != '\0' && *psz != c )
797 if( *psz == '\\' && psz[1] != '\0' )
806 if( *psz == '\\' && psz[1] != '\0' )
820 /**********************************************************************
821 * MRLParse: parse the MRL, find the MRL string and the options,
822 * create an item with all information in it, and return the item.
823 * return NULL if there is an error.
824 **********************************************************************/
826 /* Function analog to FirstWord except that it relies on colon instead
827 * of space to delimit option boundaries. */
828 static char *FirstOption( char *psz, char *new )
830 vlc_bool_t b_end, b_start = VLC_TRUE;
835 while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
840 while( *psz != '\0' && *psz != c )
842 if( *psz == '\\' && psz[1] != '\0' )
852 if( *psz == '\\' && psz[1] != '\0' )
861 while (new[-1] == ' ')
871 input_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *_psz,
874 char *psz = strdup( _psz );
877 input_item_t * p_input = NULL;
879 /* extract the mrl */
880 s_temp = FirstOption( s_mrl, s_mrl );
883 s_temp = s_mrl + strlen( s_mrl );
886 p_input = input_ItemNew( p_intf, s_mrl, psz_name );
889 /* now we can take care of the options */
890 while ( *s_mrl != '\0' )
892 s_temp = FirstOption( s_mrl, s_mrl );
897 s_temp = s_mrl + strlen( s_mrl );
899 input_ItemAddOption( p_input, s_mrl );
907 /**********************************************************************
908 * RealPath: parse ../, ~ and path stuff
909 **********************************************************************/
910 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
914 int i_len = strlen(psz_src);
923 psz_dir = malloc( i_len + 2 );
924 strcpy( psz_dir, psz_src );
926 /* Add a trailing sep to ease the .. step */
927 psz_dir[i_len] = sep;
928 psz_dir[i_len + 1] = '\0';
931 /* Convert all / to native separator */
933 while( (p = strchr( p, '/' )) != NULL )
939 /* Remove multiple separators and /./ */
941 while( (p = strchr( p, sep )) != NULL )
944 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
945 else if( p[1] == '.' && p[2] == sep )
946 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
951 if( psz_dir[0] == '~' )
953 char *dir = malloc( strlen(psz_dir)
954 + strlen(p_intf->p_libvlc->psz_userdir) );
955 /* This is incomplete : we should also support the ~cmassiot/ syntax. */
956 sprintf( dir, "%s%s", p_intf->p_libvlc->psz_userdir, psz_dir + 1 );
961 if( strlen(psz_dir) > 2 )
965 while( (p = strchr( p, sep )) != NULL )
967 if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
971 if( (q = strrchr( psz_dir, sep )) != NULL )
973 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
978 memmove( psz_dir, p, strlen(p) + 1 );
987 /* Remove trailing sep if there are at least 2 sep in the string
988 * (handles the C:\ stuff) */
989 p = strrchr( psz_dir, sep );
990 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )