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_utf8 = input_item_GetName( p_node->p_input );
441 psz = E_(FromUTF8)( p_intf, psz_utf8 );
442 E_(mvar_AppendNewVar)( itm, "name", psz );
446 psz_utf8 = input_item_GetURI( p_node->p_input );
447 psz = E_(FromUTF8)( p_intf, psz_utf8 );
448 E_(mvar_AppendNewVar)( itm, "uri", psz );
452 sprintf( value, "Item");
453 E_(mvar_AppendNewVar)( itm, "type", value );
455 sprintf( value, "%d", i_depth );
456 E_(mvar_AppendNewVar)( itm, "depth", value );
458 if( p_node->i_flags & PLAYLIST_RO_FLAG )
460 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
464 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
467 sprintf( value, "%ld",
468 (long) input_item_GetDuration( p_node->p_input ) );
469 E_(mvar_AppendNewVar)( itm, "duration", value );
471 E_(mvar_AppendVar)( s, itm );
478 mvar_t *itm = E_(mvar_New)( name, "set" );
480 psz = E_(FromUTF8)( p_intf, p_node->p_input->psz_name );
481 E_(mvar_AppendNewVar)( itm, "name", psz );
482 E_(mvar_AppendNewVar)( itm, "uri", psz );
485 sprintf( value, "Node" );
486 E_(mvar_AppendNewVar)( itm, "type", value );
488 sprintf( value, "%d", p_node->i_id );
489 E_(mvar_AppendNewVar)( itm, "index", value );
491 sprintf( value, "%d", p_node->i_children);
492 E_(mvar_AppendNewVar)( itm, "i_children", value );
494 sprintf( value, "%d", i_depth );
495 E_(mvar_AppendNewVar)( itm, "depth", value );
497 if( p_node->i_flags & PLAYLIST_RO_FLAG )
499 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
503 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
506 E_(mvar_AppendVar)( s, itm );
508 for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
509 E_(PlaylistListNode)( p_intf, p_pl,
510 p_node->pp_children[i_child],
511 name, s, i_depth + 1);
517 /****************************************************************************
518 * Seek command parsing handling
519 ****************************************************************************/
520 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
522 intf_sys_t *p_sys = p_intf->p_sys;
528 #define POSITION_ABSOLUTE 12
529 #define POSITION_REL_FOR 13
530 #define POSITION_REL_BACK 11
531 #define VL_TIME_ABSOLUTE 0
532 #define VL_TIME_REL_FOR 1
533 #define VL_TIME_REL_BACK -1
536 var_Get( p_sys->p_input, "length", &val );
537 i_length = val.i_time;
539 while( p_value[0] != '\0' )
545 i_relative = VL_TIME_REL_FOR;
551 i_relative = VL_TIME_REL_BACK;
555 case '0': case '1': case '2': case '3': case '4':
556 case '5': case '6': case '7': case '8': case '9':
558 i_stock = strtol( p_value , &p_value , 10 );
561 case '%': /* for percentage ie position */
563 i_relative += POSITION_ABSOLUTE;
571 i_value = 60 * (i_value + i_stock) ;
576 case 'h': case 'H': /* hours */
578 i_value += 3600 * i_stock;
580 /* other characters which are not numbers are not
582 while( ((p_value[0] < '0') || (p_value[0] > '9'))
583 && (p_value[0] != '\0') )
589 case 'm': case 'M': case '\'': /* minutes */
591 i_value += 60 * i_stock;
594 while( ((p_value[0] < '0') || (p_value[0] > '9'))
595 && (p_value[0] != '\0') )
601 case 's': case 'S': case '"': /* seconds */
605 while( ((p_value[0] < '0') || (p_value[0] > '9'))
606 && (p_value[0] != '\0') )
620 /* if there is no known symbol, I consider it as seconds.
621 * Otherwise, i_stock = 0 */
626 case VL_TIME_ABSOLUTE:
628 if( (uint64_t)( i_value ) * 1000000 <= i_length )
629 val.i_time = (uint64_t)( i_value ) * 1000000;
631 val.i_time = i_length;
633 var_Set( p_sys->p_input, "time", val );
634 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
637 case VL_TIME_REL_FOR:
639 var_Get( p_sys->p_input, "time", &val );
640 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
642 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
645 val.i_time = i_length;
647 var_Set( p_sys->p_input, "time", val );
648 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
651 case VL_TIME_REL_BACK:
653 var_Get( p_sys->p_input, "time", &val );
654 if( (int64_t)( i_value ) * 1000000 > val.i_time )
659 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
661 var_Set( p_sys->p_input, "time", val );
662 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
665 case POSITION_ABSOLUTE:
667 val.f_float = __MIN( __MAX( ((float) i_value ) / 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,
678 var_Set( p_sys->p_input, "position", val );
679 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
683 case POSITION_REL_BACK:
685 var_Get( p_sys->p_input, "position", &val );
686 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
688 var_Set( p_sys->p_input, "position", val );
689 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
695 msg_Dbg( p_intf, "invalid seek request" );
700 #undef POSITION_ABSOLUTE
701 #undef POSITION_REL_FOR
702 #undef POSITION_REL_BACK
703 #undef VL_TIME_ABSOLUTE
704 #undef VL_TIME_REL_FOR
705 #undef VL_TIME_REL_BACK
709 /****************************************************************************
710 * URI Parsing functions
711 ****************************************************************************/
712 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
716 while( (p = strstr( p, psz_name )) )
718 /* Verify that we are dealing with a post/get argument */
719 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
720 && p[strlen(psz_name)] == '=' )
730 static char *FindURIValue( char *psz_uri, const char *restrict psz_name,
731 size_t *restrict p_len )
733 char *p = psz_uri, *end;
736 while( (p = strstr( p, psz_name )) )
738 /* Verify that we are dealing with a post/get argument */
739 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
740 && p[strlen(psz_name)] == '=' )
751 p += strlen( psz_name );
754 if( ( end = strchr( p, '\n' ) ) != NULL )
757 if( ( end > p ) && ( end[-1] == '\r' ) )
765 if( ( end = strchr( p, '&' ) ) != NULL )
775 char *E_(ExtractURIValue)( char *restrict psz_uri,
776 const char *restrict psz_name,
777 char *restrict psz_buf, size_t bufsize )
780 char *psz_value = FindURIValue( psz_uri, psz_name, &len );
783 if( psz_value == NULL )
790 psz_next = psz_value + len;
796 strncpy( psz_buf, psz_value, len );
803 char *E_(ExtractURIString)( char *restrict psz_uri,
804 const char *restrict psz_name )
807 char *psz_value = FindURIValue( psz_uri, psz_name, &len );
809 if( psz_value == NULL )
812 char *res = malloc( len + 1 );
816 memcpy( res, psz_value, len );
822 /* Since the resulting string is smaller we can work in place, so it is
823 * permitted to have psz == new. new points to the first word of the
824 * string, the function returns the remaining string. */
825 char *E_(FirstWord)( char *psz, char *new )
832 while( *psz != '\0' && *psz != ' ' )
837 while( *psz != '\0' && *psz != c )
839 if( *psz == '\\' && psz[1] != '\0' )
848 if( *psz == '\\' && psz[1] != '\0' )
862 /**********************************************************************
863 * MRLParse: parse the MRL, find the MRL string and the options,
864 * create an item with all information in it, and return the item.
865 * return NULL if there is an error.
866 **********************************************************************/
868 /* Function analog to FirstWord except that it relies on colon instead
869 * of space to delimit option boundaries. */
870 static char *FirstOption( char *psz, char *new )
872 vlc_bool_t b_end, b_start = VLC_TRUE;
877 while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
882 while( *psz != '\0' && *psz != c )
884 if( *psz == '\\' && psz[1] != '\0' )
894 if( *psz == '\\' && psz[1] != '\0' )
903 while (new[-1] == ' ')
913 input_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *_psz,
916 char *psz = strdup( _psz );
919 input_item_t * p_input = NULL;
921 /* extract the mrl */
922 s_temp = FirstOption( s_mrl, s_mrl );
925 s_temp = s_mrl + strlen( s_mrl );
928 p_input = input_ItemNew( p_intf, s_mrl, psz_name );
931 /* now we can take care of the options */
932 while ( *s_mrl != '\0' )
934 s_temp = FirstOption( s_mrl, s_mrl );
939 s_temp = s_mrl + strlen( s_mrl );
941 input_ItemAddOption( p_input, s_mrl );
949 /**********************************************************************
950 * RealPath: parse ../, ~ and path stuff
951 **********************************************************************/
952 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
956 int i_len = strlen(psz_src);
957 const char sep = DIR_SEP_CHAR;
959 psz_dir = malloc( i_len + 2 );
960 strcpy( psz_dir, psz_src );
962 /* Add a trailing sep to ease the .. step */
963 psz_dir[i_len] = sep;
964 psz_dir[i_len + 1] = '\0';
966 #if (DIR_SEP_CHAR != '/')
967 /* Convert all / to native separator */
969 while( (p = strchr( p, '/' )) != NULL )
975 /* FIXME: this could be O(N) rather than O(N²)... */
976 /* Remove multiple separators and /./ */
978 while( (p = strchr( p, sep )) != NULL )
981 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
982 else if( p[1] == '.' && p[2] == sep )
983 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
988 if( psz_dir[0] == '~' )
991 /* This is incomplete : we should also support the ~cmassiot/ syntax. */
992 asprintf( &dir, "%s%s", p_intf->p_libvlc->psz_userdir, psz_dir + 1 );
997 if( strlen(psz_dir) > 2 )
1001 while( (p = strchr( p, sep )) != NULL )
1003 if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
1007 if( (q = strrchr( psz_dir, sep )) != NULL )
1009 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
1014 memmove( psz_dir, p, strlen(p) + 1 );
1023 /* Remove trailing sep if there are at least 2 sep in the string
1024 * (handles the C:\ stuff) */
1025 p = strrchr( psz_dir, sep );
1026 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )