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 *****************************************************************************/
28 #include "vlc_strings.h"
30 /****************************************************************************
31 * File and directory functions
32 ****************************************************************************/
34 /* ToUrl: create a good name for an url from filename */
35 char *E_(FileToUrl)( char *name, vlc_bool_t *pb_index )
39 url = p = malloc( strlen( name ) + 1 );
41 *pb_index = VLC_FALSE;
48 while( *name == '\\' || *name == '/' )
60 /* convert '\\' into '/' */
71 if( ( p = strrchr( url, '/' ) ) != NULL )
73 if( !strncmp( p, "/index.", 7 ) )
83 int E_(FileLoad)( FILE *f, char **pp_data, int *pi_data )
87 /* just load the file */
89 *pp_data = malloc( 1025 ); /* +1 for \0 */
90 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
93 *pp_data = realloc( *pp_data, *pi_data + 1025 );
99 (*pp_data)[*pi_data] = '\0';
104 /* Parse a directory and recursively add files */
105 int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
108 intf_sys_t *p_sys = p_intf->p_sys;
109 char dir[MAX_DIR_SIZE];
110 #ifdef HAVE_SYS_STAT_H
111 struct stat stat_info;
118 char *password = NULL;
130 #ifdef HAVE_SYS_STAT_H
131 if( utf8_stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
137 if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
139 msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
143 i_dirlen = strlen( psz_dir );
144 if( i_dirlen + 10 > MAX_DIR_SIZE )
146 msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
150 msg_Dbg( p_intf, "dir=%s", psz_dir );
152 snprintf( dir, sizeof( dir ), "%s%c.access", psz_dir, sep );
153 if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
158 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
160 i_size = fread( line, 1, 1023, file );
164 while( i_size > 0 && ( line[i_size-1] == '\n' ||
165 line[i_size-1] == '\r' ) )
172 p = strchr( line, ':' );
176 user = strdup( line );
177 password = strdup( p );
180 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
181 user, password, i_size );
186 snprintf( dir, sizeof( dir ), "%s%c.hosts", psz_dir, sep );
187 p_acl = ACL_Create( p_intf, VLC_FALSE );
188 if( ACL_LoadFile( p_acl, dir ) )
190 ACL_Destroy( p_acl );
197 /* parse psz_src dir */
198 if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
203 if( ( psz_filename[0] == '.' )
204 || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
206 free( psz_filename );
210 snprintf( dir, sizeof( dir ), "%s%c%s", psz_dir, sep, psz_filename );
211 free( psz_filename );
213 if( E_(ParseDirectory)( p_intf, psz_root, dir ) )
215 httpd_file_sys_t *f = NULL;
216 httpd_handler_sys_t *h = NULL;
218 char *psz_name, *psz_ext;
220 psz_name = E_(FileToUrl)( &dir[strlen( psz_root )], &b_index );
221 psz_ext = strrchr( dir, '.' );
222 if( psz_ext != NULL )
226 for( i = 0; i < p_sys->i_handlers; i++ )
227 if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
229 if( i < p_sys->i_handlers )
231 f = malloc( sizeof( httpd_handler_sys_t ) );
232 h = (httpd_handler_sys_t *)f;
233 f->b_handler = VLC_TRUE;
234 h->p_association = p_sys->pp_handlers[i];
239 f = malloc( sizeof( httpd_file_sys_t ) );
240 f->b_handler = VLC_FALSE;
247 f->file = strdup (dir);
249 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? VLC_TRUE : VLC_FALSE;
253 msg_Err( p_intf , "unable to parse directory" );
256 return( VLC_ENOMEM );
258 msg_Dbg( p_intf, "file=%s (url=%s)",
263 char *psz_type = strdup( p_sys->psz_html_type );
264 if( strstr( &dir[strlen( psz_root )], ".xml" ) )
266 char *psz = strstr( psz_type, "html;" );
276 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
278 f->b_html ? psz_type : NULL,
279 user, password, p_acl,
280 E_(HttpCallback), f );
282 if( f->p_file != NULL )
284 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
289 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
291 user, password, p_acl,
292 E_(HandlerCallback), h );
293 if( h->p_handler != NULL )
295 TAB_APPEND( p_sys->i_files, p_sys->pp_files,
296 (httpd_file_sys_t *)h );
300 /* for url that ends by / add
301 * - a redirect from rep to rep/
302 * - in case of index.* rep/index.html to rep/ */
303 if( f && f->name[strlen(f->name) - 1] == '/' )
305 char *psz_redir = strdup( f->name );
307 psz_redir[strlen( psz_redir ) - 1] = '\0';
309 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
310 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
313 if( b_index && ( p = strstr( f->file, "index." ) ) )
315 asprintf( &psz_redir, "%s%s", f->name, p );
317 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
318 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
319 f->name, psz_redir );
336 ACL_Destroy( p_acl );
343 /**************************************************************************
345 **************************************************************************/
346 char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 )
348 intf_sys_t *p_sys = p_intf->p_sys;
350 if ( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
352 size_t i_in = strlen(psz_utf8);
353 size_t i_out = i_in * 2;
354 char *psz_local = malloc(i_out + 1);
355 char *psz_out = psz_local;
357 char psz_tmp[i_in + 1];
358 const char *psz_in = psz_tmp;
359 strcpy( psz_tmp, psz_utf8 );
361 i_in = strlen( psz_tmp );
363 i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
365 if( i_ret == (size_t)-1 || i_in )
368 "failed to convert \"%s\" to desired charset (%s)",
369 psz_utf8, strerror(errno) );
371 return strdup( psz_utf8 );
378 return strdup( psz_utf8 );
381 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
383 intf_sys_t *p_sys = p_intf->p_sys;
385 if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
387 const char *psz_in = psz_local;
388 size_t i_in = strlen(psz_in);
389 size_t i_out = i_in * 6;
390 char *psz_utf8 = malloc(i_out + 1);
391 char *psz_out = psz_utf8;
393 size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
395 if( i_ret == (size_t)-1 || i_in )
398 "failed to convert \"%s\" to desired charset (%s)",
399 psz_local, strerror(errno) );
401 return strdup( psz_local );
408 return strdup( psz_local );
411 /*************************************************************************
413 *************************************************************************/
414 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
415 playlist_item_t *p_node, char *name, mvar_t *s,
420 if( p_node->i_children == -1 )
424 mvar_t *itm = E_(mvar_New)( name, "set" );
426 if( p_pl->status.p_item && p_node &&
427 p_pl->status.p_item->p_input && p_node->p_input &&
428 p_pl->status.p_item->p_input->i_id == p_node->p_input->i_id )
430 E_(mvar_AppendNewVar)( itm, "current", "1" );
434 E_(mvar_AppendNewVar)( itm, "current", "0" );
437 sprintf( value, "%d", p_node->i_id );
438 E_(mvar_AppendNewVar)( itm, "index", value );
440 psz = E_(FromUTF8)( p_intf, p_node->p_input->psz_name );
441 E_(mvar_AppendNewVar)( itm, "name", psz );
444 psz = E_(FromUTF8)( p_intf, p_node->p_input->psz_uri );
445 E_(mvar_AppendNewVar)( itm, "uri", psz );
448 sprintf( value, "Item");
449 E_(mvar_AppendNewVar)( itm, "type", value );
451 sprintf( value, "%d", i_depth );
452 E_(mvar_AppendNewVar)( itm, "depth", value );
454 if( p_node->i_flags & PLAYLIST_RO_FLAG )
456 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
460 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
463 sprintf( value, "%ld", (long)p_node->p_input->i_duration );
464 E_(mvar_AppendNewVar)( itm, "duration", value );
466 E_(mvar_AppendVar)( s, itm );
473 mvar_t *itm = E_(mvar_New)( name, "set" );
475 psz = E_(FromUTF8)( p_intf, p_node->p_input->psz_name );
476 E_(mvar_AppendNewVar)( itm, "name", psz );
477 E_(mvar_AppendNewVar)( itm, "uri", psz );
480 sprintf( value, "Node" );
481 E_(mvar_AppendNewVar)( itm, "type", value );
483 sprintf( value, "%d", p_node->i_id );
484 E_(mvar_AppendNewVar)( itm, "index", value );
486 sprintf( value, "%d", p_node->i_children);
487 E_(mvar_AppendNewVar)( itm, "i_children", value );
489 sprintf( value, "%d", i_depth );
490 E_(mvar_AppendNewVar)( itm, "depth", value );
492 if( p_node->i_flags & PLAYLIST_RO_FLAG )
494 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
498 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
501 E_(mvar_AppendVar)( s, itm );
503 for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
504 E_(PlaylistListNode)( p_intf, p_pl,
505 p_node->pp_children[i_child],
506 name, s, i_depth + 1);
512 /****************************************************************************
513 * Seek command parsing handling
514 ****************************************************************************/
515 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
517 intf_sys_t *p_sys = p_intf->p_sys;
523 #define POSITION_ABSOLUTE 12
524 #define POSITION_REL_FOR 13
525 #define POSITION_REL_BACK 11
526 #define VL_TIME_ABSOLUTE 0
527 #define VL_TIME_REL_FOR 1
528 #define VL_TIME_REL_BACK -1
531 var_Get( p_sys->p_input, "length", &val );
532 i_length = val.i_time;
534 while( p_value[0] != '\0' )
540 i_relative = VL_TIME_REL_FOR;
546 i_relative = VL_TIME_REL_BACK;
550 case '0': case '1': case '2': case '3': case '4':
551 case '5': case '6': case '7': case '8': case '9':
553 i_stock = strtol( p_value , &p_value , 10 );
556 case '%': /* for percentage ie position */
558 i_relative += POSITION_ABSOLUTE;
566 i_value = 60 * (i_value + i_stock) ;
571 case 'h': case 'H': /* hours */
573 i_value += 3600 * i_stock;
575 /* other characters which are not numbers are not
577 while( ((p_value[0] < '0') || (p_value[0] > '9'))
578 && (p_value[0] != '\0') )
584 case 'm': case 'M': case '\'': /* minutes */
586 i_value += 60 * i_stock;
589 while( ((p_value[0] < '0') || (p_value[0] > '9'))
590 && (p_value[0] != '\0') )
596 case 's': case 'S': case '"': /* seconds */
600 while( ((p_value[0] < '0') || (p_value[0] > '9'))
601 && (p_value[0] != '\0') )
615 /* if there is no known symbol, I consider it as seconds.
616 * Otherwise, i_stock = 0 */
621 case VL_TIME_ABSOLUTE:
623 if( (uint64_t)( i_value ) * 1000000 <= i_length )
624 val.i_time = (uint64_t)( i_value ) * 1000000;
626 val.i_time = i_length;
628 var_Set( p_sys->p_input, "time", val );
629 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
632 case VL_TIME_REL_FOR:
634 var_Get( p_sys->p_input, "time", &val );
635 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
637 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
640 val.i_time = i_length;
642 var_Set( p_sys->p_input, "time", val );
643 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
646 case VL_TIME_REL_BACK:
648 var_Get( p_sys->p_input, "time", &val );
649 if( (int64_t)( i_value ) * 1000000 > val.i_time )
654 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
656 var_Set( p_sys->p_input, "time", val );
657 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
660 case POSITION_ABSOLUTE:
662 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
664 var_Set( p_sys->p_input, "position", val );
665 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
668 case POSITION_REL_FOR:
670 var_Get( p_sys->p_input, "position", &val );
671 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
673 var_Set( p_sys->p_input, "position", val );
674 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
678 case POSITION_REL_BACK:
680 var_Get( p_sys->p_input, "position", &val );
681 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
683 var_Set( p_sys->p_input, "position", val );
684 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
690 msg_Dbg( p_intf, "invalid seek request" );
695 #undef POSITION_ABSOLUTE
696 #undef POSITION_REL_FOR
697 #undef POSITION_REL_BACK
698 #undef VL_TIME_ABSOLUTE
699 #undef VL_TIME_REL_FOR
700 #undef VL_TIME_REL_BACK
704 /****************************************************************************
705 * URI Parsing functions
706 ****************************************************************************/
707 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
711 while( (p = strstr( p, psz_name )) )
713 /* Verify that we are dealing with a post/get argument */
714 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
715 && p[strlen(psz_name)] == '=' )
725 static char *FindURIValue( char *psz_uri, const char *restrict psz_name,
726 size_t *restrict p_len )
728 char *p = psz_uri, *end;
731 while( (p = strstr( p, psz_name )) )
733 /* Verify that we are dealing with a post/get argument */
734 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
735 && p[strlen(psz_name)] == '=' )
746 p += strlen( psz_name );
749 if( ( end = strchr( p, '\n' ) ) != NULL )
752 if( ( end > p ) && ( end[-1] == '\r' ) )
760 if( ( end = strchr( p, '&' ) ) != NULL )
770 char *E_(ExtractURIValue)( char *restrict psz_uri,
771 const char *restrict psz_name,
772 char *restrict psz_buf, size_t bufsize )
775 char *psz_value = FindURIValue( psz_uri, psz_name, &len );
778 if( psz_value == NULL )
785 psz_next = psz_value + len;
791 strncpy( psz_buf, psz_value, len );
798 char *E_(ExtractURIString)( char *restrict psz_uri,
799 const char *restrict psz_name )
802 char *psz_value = FindURIValue( psz_uri, psz_name, &len );
804 if( psz_value == NULL )
807 char *res = malloc( len + 1 );
811 memcpy( res, psz_value, len );
817 /* Since the resulting string is smaller we can work in place, so it is
818 * permitted to have psz == new. new points to the first word of the
819 * string, the function returns the remaining string. */
820 char *E_(FirstWord)( char *psz, char *new )
827 while( *psz != '\0' && *psz != ' ' )
832 while( *psz != '\0' && *psz != c )
834 if( *psz == '\\' && psz[1] != '\0' )
843 if( *psz == '\\' && psz[1] != '\0' )
857 /**********************************************************************
858 * MRLParse: parse the MRL, find the MRL string and the options,
859 * create an item with all information in it, and return the item.
860 * return NULL if there is an error.
861 **********************************************************************/
863 /* Function analog to FirstWord except that it relies on colon instead
864 * of space to delimit option boundaries. */
865 static char *FirstOption( char *psz, char *new )
867 vlc_bool_t b_end, b_start = VLC_TRUE;
872 while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
877 while( *psz != '\0' && *psz != c )
879 if( *psz == '\\' && psz[1] != '\0' )
889 if( *psz == '\\' && psz[1] != '\0' )
898 while (new[-1] == ' ')
908 input_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *_psz,
911 char *psz = strdup( _psz );
914 input_item_t * p_input = NULL;
916 /* extract the mrl */
917 s_temp = FirstOption( s_mrl, s_mrl );
920 s_temp = s_mrl + strlen( s_mrl );
923 p_input = input_ItemNew( p_intf, s_mrl, psz_name );
926 /* now we can take care of the options */
927 while ( *s_mrl != '\0' )
929 s_temp = FirstOption( s_mrl, s_mrl );
934 s_temp = s_mrl + strlen( s_mrl );
936 input_ItemAddOption( p_input, s_mrl );
944 /**********************************************************************
945 * RealPath: parse ../, ~ and path stuff
946 **********************************************************************/
947 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
951 int i_len = strlen(psz_src);
952 const char sep = DIR_SEP_CHAR;
954 psz_dir = malloc( i_len + 2 );
955 strcpy( psz_dir, psz_src );
957 /* Add a trailing sep to ease the .. step */
958 psz_dir[i_len] = sep;
959 psz_dir[i_len + 1] = '\0';
961 #if (DIR_SEP_CHAR != '/')
962 /* Convert all / to native separator */
964 while( (p = strchr( p, '/' )) != NULL )
970 /* FIXME: this could be O(N) rather than O(N²)... */
971 /* Remove multiple separators and /./ */
973 while( (p = strchr( p, sep )) != NULL )
976 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
977 else if( p[1] == '.' && p[2] == sep )
978 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
983 if( psz_dir[0] == '~' )
986 /* This is incomplete : we should also support the ~cmassiot/ syntax. */
987 asprintf( &dir, "%s%s", p_intf->p_libvlc->psz_userdir, psz_dir + 1 );
992 if( strlen(psz_dir) > 2 )
996 while( (p = strchr( p, sep )) != NULL )
998 if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
1002 if( (q = strrchr( psz_dir, sep )) != NULL )
1004 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
1009 memmove( psz_dir, p, strlen(p) + 1 );
1018 /* Remove trailing sep if there are at least 2 sep in the string
1019 * (handles the C:\ stuff) */
1020 p = strrchr( psz_dir, sep );
1021 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )