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 *****************************************************************************/
32 #include "vlc_strings.h"
34 /****************************************************************************
35 * File and directory functions
36 ****************************************************************************/
38 /* ToUrl: create a good name for an url from filename */
39 char *E_(FileToUrl)( char *name, vlc_bool_t *pb_index )
43 url = p = malloc( strlen( name ) + 1 );
45 *pb_index = VLC_FALSE;
52 while( *name == '\\' || *name == '/' )
64 /* convert '\\' into '/' */
75 if( ( p = strrchr( url, '/' ) ) != NULL )
77 if( !strncmp( p, "/index.", 7 ) )
87 int E_(FileLoad)( FILE *f, char **pp_data, int *pi_data )
91 /* just load the file */
93 *pp_data = malloc( 1025 ); /* +1 for \0 */
94 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
97 *pp_data = realloc( *pp_data, *pi_data + 1025 );
103 (*pp_data)[*pi_data] = '\0';
108 /* Parse a directory and recursively add files */
109 int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
112 intf_sys_t *p_sys = p_intf->p_sys;
113 char dir[MAX_DIR_SIZE];
114 #ifdef HAVE_SYS_STAT_H
115 struct stat stat_info;
122 char *password = NULL;
134 #ifdef HAVE_SYS_STAT_H
135 if( utf8_stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
141 if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
143 msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
147 i_dirlen = strlen( psz_dir );
148 if( i_dirlen + 10 > MAX_DIR_SIZE )
150 msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
154 msg_Dbg( p_intf, "dir=%s", psz_dir );
156 snprintf( dir, sizeof( dir ), "%s%c.access", psz_dir, sep );
157 if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
162 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
164 i_size = fread( line, 1, 1023, file );
168 while( i_size > 0 && ( line[i_size-1] == '\n' ||
169 line[i_size-1] == '\r' ) )
176 p = strchr( line, ':' );
180 user = strdup( line );
181 password = strdup( p );
184 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
185 user, password, i_size );
190 snprintf( dir, sizeof( dir ), "%s%c.hosts", psz_dir, sep );
191 p_acl = ACL_Create( p_intf, VLC_FALSE );
192 if( ACL_LoadFile( p_acl, dir ) )
194 ACL_Destroy( p_acl );
201 /* parse psz_src dir */
202 if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
207 if( ( psz_filename[0] == '.' )
208 || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
210 free( psz_filename );
214 snprintf( dir, sizeof( dir ), "%s%c%s", psz_dir, sep, psz_filename );
215 free( psz_filename );
217 if( E_(ParseDirectory)( p_intf, psz_root, dir ) )
219 httpd_file_sys_t *f = NULL;
220 httpd_handler_sys_t *h = NULL;
222 char *psz_name, *psz_ext;
224 psz_name = E_(FileToUrl)( &dir[strlen( psz_root )], &b_index );
225 psz_ext = strrchr( dir, '.' );
226 if( psz_ext != NULL )
230 for( i = 0; i < p_sys->i_handlers; i++ )
231 if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
233 if( i < p_sys->i_handlers )
235 f = malloc( sizeof( httpd_handler_sys_t ) );
236 h = (httpd_handler_sys_t *)f;
237 f->b_handler = VLC_TRUE;
238 h->p_association = p_sys->pp_handlers[i];
243 f = malloc( sizeof( httpd_file_sys_t ) );
244 f->b_handler = VLC_FALSE;
251 f->file = strdup (dir);
253 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? VLC_TRUE : VLC_FALSE;
257 msg_Err( p_intf , "unable to parse directory" );
260 return( VLC_ENOMEM );
262 msg_Dbg( p_intf, "file=%s (url=%s)",
267 char *psz_type = strdup( "text/html; charset=UTF-8" );
268 if( strstr( &dir[strlen( psz_root )], ".xml" ) )
270 char *psz = strstr( psz_type, "html;" );
280 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
282 f->b_html ? psz_type : NULL,
283 user, password, p_acl,
284 E_(HttpCallback), f );
286 if( f->p_file != NULL )
288 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
293 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
295 user, password, p_acl,
296 E_(HandlerCallback), h );
297 if( h->p_handler != NULL )
299 TAB_APPEND( p_sys->i_files, p_sys->pp_files,
300 (httpd_file_sys_t *)h );
304 /* for url that ends by / add
305 * - a redirect from rep to rep/
306 * - in case of index.* rep/index.html to rep/ */
307 if( f && f->name[strlen(f->name) - 1] == '/' )
309 char *psz_redir = strdup( f->name );
311 psz_redir[strlen( psz_redir ) - 1] = '\0';
313 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
314 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
317 if( b_index && ( p = strstr( f->file, "index." ) ) )
319 asprintf( &psz_redir, "%s%s", f->name, p );
321 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
322 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
323 f->name, psz_redir );
334 ACL_Destroy( p_acl );
341 /*************************************************************************
343 *************************************************************************/
344 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
345 playlist_item_t *p_node, char *name, mvar_t *s,
350 if( p_node->i_children == -1 )
354 mvar_t *itm = E_(mvar_New)( name, "set" );
356 if( p_pl->status.p_item && p_node &&
357 p_pl->status.p_item->p_input && p_node->p_input &&
358 p_pl->status.p_item->p_input->i_id == p_node->p_input->i_id )
360 E_(mvar_AppendNewVar)( itm, "current", "1" );
364 E_(mvar_AppendNewVar)( itm, "current", "0" );
367 sprintf( value, "%d", p_node->i_id );
368 E_(mvar_AppendNewVar)( itm, "index", value );
370 psz = input_item_GetName( p_node->p_input );
371 E_(mvar_AppendNewVar)( itm, "name", psz );
374 psz = input_item_GetURI( p_node->p_input );
375 E_(mvar_AppendNewVar)( itm, "uri", psz );
378 sprintf( value, "Item");
379 E_(mvar_AppendNewVar)( itm, "type", value );
381 sprintf( value, "%d", i_depth );
382 E_(mvar_AppendNewVar)( itm, "depth", value );
384 if( p_node->i_flags & PLAYLIST_RO_FLAG )
386 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
390 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
393 sprintf( value, "%ld",
394 (long) input_item_GetDuration( p_node->p_input ) );
395 E_(mvar_AppendNewVar)( itm, "duration", value );
397 E_(mvar_AppendVar)( s, itm );
403 mvar_t *itm = E_(mvar_New)( name, "set" );
405 E_(mvar_AppendNewVar)( itm, "name", p_node->p_input->psz_name );
406 E_(mvar_AppendNewVar)( itm, "uri", p_node->p_input->psz_name );
408 sprintf( value, "Node" );
409 E_(mvar_AppendNewVar)( itm, "type", value );
411 sprintf( value, "%d", p_node->i_id );
412 E_(mvar_AppendNewVar)( itm, "index", value );
414 sprintf( value, "%d", p_node->i_children);
415 E_(mvar_AppendNewVar)( itm, "i_children", value );
417 sprintf( value, "%d", i_depth );
418 E_(mvar_AppendNewVar)( itm, "depth", value );
420 if( p_node->i_flags & PLAYLIST_RO_FLAG )
422 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
426 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
429 E_(mvar_AppendVar)( s, itm );
431 for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
432 E_(PlaylistListNode)( p_intf, p_pl,
433 p_node->pp_children[i_child],
434 name, s, i_depth + 1);
440 /****************************************************************************
441 * Seek command parsing handling
442 ****************************************************************************/
443 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
445 intf_sys_t *p_sys = p_intf->p_sys;
451 #define POSITION_ABSOLUTE 12
452 #define POSITION_REL_FOR 13
453 #define POSITION_REL_BACK 11
454 #define VL_TIME_ABSOLUTE 0
455 #define VL_TIME_REL_FOR 1
456 #define VL_TIME_REL_BACK -1
459 var_Get( p_sys->p_input, "length", &val );
460 i_length = val.i_time;
462 while( p_value[0] != '\0' )
468 i_relative = VL_TIME_REL_FOR;
474 i_relative = VL_TIME_REL_BACK;
478 case '0': case '1': case '2': case '3': case '4':
479 case '5': case '6': case '7': case '8': case '9':
481 i_stock = strtol( p_value , &p_value , 10 );
484 case '%': /* for percentage ie position */
486 i_relative += POSITION_ABSOLUTE;
494 i_value = 60 * (i_value + i_stock) ;
499 case 'h': case 'H': /* hours */
501 i_value += 3600 * i_stock;
503 /* other characters which are not numbers are not
505 while( ((p_value[0] < '0') || (p_value[0] > '9'))
506 && (p_value[0] != '\0') )
512 case 'm': case 'M': case '\'': /* minutes */
514 i_value += 60 * i_stock;
517 while( ((p_value[0] < '0') || (p_value[0] > '9'))
518 && (p_value[0] != '\0') )
524 case 's': case 'S': case '"': /* seconds */
528 while( ((p_value[0] < '0') || (p_value[0] > '9'))
529 && (p_value[0] != '\0') )
543 /* if there is no known symbol, I consider it as seconds.
544 * Otherwise, i_stock = 0 */
549 case VL_TIME_ABSOLUTE:
551 if( (uint64_t)( i_value ) * 1000000 <= i_length )
552 val.i_time = (uint64_t)( i_value ) * 1000000;
554 val.i_time = i_length;
556 var_Set( p_sys->p_input, "time", val );
557 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
560 case VL_TIME_REL_FOR:
562 var_Get( p_sys->p_input, "time", &val );
563 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
565 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
568 val.i_time = i_length;
570 var_Set( p_sys->p_input, "time", val );
571 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
574 case VL_TIME_REL_BACK:
576 var_Get( p_sys->p_input, "time", &val );
577 if( (int64_t)( i_value ) * 1000000 > val.i_time )
582 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
584 var_Set( p_sys->p_input, "time", val );
585 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
588 case POSITION_ABSOLUTE:
590 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
592 var_Set( p_sys->p_input, "position", val );
593 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
596 case POSITION_REL_FOR:
598 var_Get( p_sys->p_input, "position", &val );
599 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
601 var_Set( p_sys->p_input, "position", val );
602 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
606 case POSITION_REL_BACK:
608 var_Get( p_sys->p_input, "position", &val );
609 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
611 var_Set( p_sys->p_input, "position", val );
612 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
618 msg_Dbg( p_intf, "invalid seek request" );
623 #undef POSITION_ABSOLUTE
624 #undef POSITION_REL_FOR
625 #undef POSITION_REL_BACK
626 #undef VL_TIME_ABSOLUTE
627 #undef VL_TIME_REL_FOR
628 #undef VL_TIME_REL_BACK
632 /****************************************************************************
633 * URI Parsing functions
634 ****************************************************************************/
635 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
639 while( (p = strstr( p, psz_name )) )
641 /* Verify that we are dealing with a post/get argument */
642 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
643 && p[strlen(psz_name)] == '=' )
653 static char *FindURIValue( char *psz_uri, const char *restrict psz_name,
654 size_t *restrict p_len )
656 char *p = psz_uri, *end;
659 while( (p = strstr( p, psz_name )) )
661 /* Verify that we are dealing with a post/get argument */
662 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
663 && p[strlen(psz_name)] == '=' )
674 p += strlen( psz_name );
677 if( ( end = strchr( p, '\n' ) ) != NULL )
680 if( ( end > p ) && ( end[-1] == '\r' ) )
688 if( ( end = strchr( p, '&' ) ) != NULL )
698 char *E_(ExtractURIValue)( char *restrict psz_uri,
699 const char *restrict psz_name,
700 char *restrict psz_buf, size_t bufsize )
703 char *psz_value = FindURIValue( psz_uri, psz_name, &len );
706 if( psz_value == NULL )
713 psz_next = psz_value + len;
719 strncpy( psz_buf, psz_value, len );
726 char *E_(ExtractURIString)( char *restrict psz_uri,
727 const char *restrict psz_name )
730 char *psz_value = FindURIValue( psz_uri, psz_name, &len );
732 if( psz_value == NULL )
735 char *res = malloc( len + 1 );
739 memcpy( res, psz_value, len );
745 /* Since the resulting string is smaller we can work in place, so it is
746 * permitted to have psz == new. new points to the first word of the
747 * string, the function returns the remaining string. */
748 char *E_(FirstWord)( char *psz, char *new )
755 while( *psz != '\0' && *psz != ' ' )
760 while( *psz != '\0' && *psz != c )
762 if( *psz == '\\' && psz[1] != '\0' )
771 if( *psz == '\\' && psz[1] != '\0' )
785 /**********************************************************************
786 * MRLParse: parse the MRL, find the MRL string and the options,
787 * create an item with all information in it, and return the item.
788 * return NULL if there is an error.
789 **********************************************************************/
791 /* Function analog to FirstWord except that it relies on colon instead
792 * of space to delimit option boundaries. */
793 static char *FirstOption( char *psz, char *new )
795 vlc_bool_t b_end, b_start = VLC_TRUE;
800 while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
805 while( *psz != '\0' && *psz != c )
807 if( *psz == '\\' && psz[1] != '\0' )
817 if( *psz == '\\' && psz[1] != '\0' )
826 while (new[-1] == ' ')
836 input_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *_psz,
839 char *psz = strdup( _psz );
842 input_item_t * p_input = NULL;
844 /* extract the mrl */
845 s_temp = FirstOption( s_mrl, s_mrl );
848 s_temp = s_mrl + strlen( s_mrl );
851 p_input = input_ItemNew( p_intf, s_mrl, psz_name );
854 /* now we can take care of the options */
855 while ( *s_mrl != '\0' )
857 s_temp = FirstOption( s_mrl, s_mrl );
862 s_temp = s_mrl + strlen( s_mrl );
864 input_ItemAddOption( p_input, s_mrl );
872 /**********************************************************************
873 * RealPath: parse ../, ~ and path stuff
874 **********************************************************************/
875 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
879 int i_len = strlen(psz_src);
880 const char sep = DIR_SEP_CHAR;
882 psz_dir = malloc( i_len + 2 );
883 strcpy( psz_dir, psz_src );
885 /* Add a trailing sep to ease the .. step */
886 psz_dir[i_len] = sep;
887 psz_dir[i_len + 1] = '\0';
889 #if (DIR_SEP_CHAR != '/')
890 /* Convert all / to native separator */
892 while( (p = strchr( p, '/' )) != NULL )
898 /* FIXME: this could be O(N) rather than O(N²)... */
899 /* Remove multiple separators and /./ */
901 while( (p = strchr( p, sep )) != NULL )
904 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
905 else if( p[1] == '.' && p[2] == sep )
906 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
911 if( psz_dir[0] == '~' )
914 /* This is incomplete : we should also support the ~cmassiot/ syntax. */
915 asprintf( &dir, "%s%s", p_intf->p_libvlc->psz_homedir, psz_dir + 1 );
920 if( strlen(psz_dir) > 2 )
924 while( (p = strchr( p, sep )) != NULL )
926 if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
930 if( (q = strrchr( psz_dir, sep )) != NULL )
932 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
937 memmove( psz_dir, p, strlen(p) + 1 );
946 /* Remove trailing sep if there are at least 2 sep in the string
947 * (handles the C:\ stuff) */
948 p = strrchr( psz_dir, sep );
949 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )