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 *****************************************************************************/
30 #include <vlc_common.h>
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 *FileToUrl( char *name, bool *pb_index )
43 url = p = malloc( strlen( name ) + 1 );
52 while( *name == '\\' || *name == '/' )
64 /* convert '\\' into '/' */
75 if( ( p = strrchr( url, '/' ) ) != NULL )
77 if( !strncmp( p, "/index.", 7 ) )
87 int 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 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];
119 char *password = NULL;
131 if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
133 if( errno != ENOENT && errno != ENOTDIR )
134 msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
138 i_dirlen = strlen( psz_dir );
139 if( i_dirlen + 10 > MAX_DIR_SIZE )
141 msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
146 msg_Dbg( p_intf, "dir=%s", psz_dir );
148 snprintf( dir, sizeof( dir ), "%s%c.access", psz_dir, sep );
149 if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
154 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
156 i_size = fread( line, 1, 1023, file );
160 while( i_size > 0 && ( line[i_size-1] == '\n' ||
161 line[i_size-1] == '\r' ) )
168 p = strchr( line, ':' );
172 user = strdup( line );
173 password = strdup( p );
176 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
177 user, password, i_size );
182 snprintf( dir, sizeof( dir ), "%s%c.hosts", psz_dir, sep );
183 p_acl = ACL_Create( p_intf, false );
184 if( ACL_LoadFile( p_acl, dir ) )
186 ACL_Destroy( p_acl );
189 if( stat( dir, &st ) == 0 )
200 /* parse psz_src dir */
201 if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
206 if( ( psz_filename[0] == '.' )
207 || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
209 free( psz_filename );
213 snprintf( dir, sizeof( dir ), "%s%c%s", psz_dir, sep, psz_filename );
214 free( psz_filename );
216 if( ParseDirectory( p_intf, psz_root, dir ) )
218 httpd_file_sys_t *f = NULL;
219 httpd_handler_sys_t *h = NULL;
221 char *psz_name, *psz_ext;
223 psz_name = FileToUrl( &dir[strlen( psz_root )], &b_index );
224 psz_ext = strrchr( dir, '.' );
225 if( psz_ext != NULL )
229 for( i = 0; i < p_sys->i_handlers; i++ )
230 if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
232 if( i < p_sys->i_handlers )
234 f = malloc( sizeof( httpd_handler_sys_t ) );
235 h = (httpd_handler_sys_t *)f;
237 h->p_association = p_sys->pp_handlers[i];
242 f = malloc( sizeof( httpd_file_sys_t ) );
243 f->b_handler = false;
250 f->file = strdup (dir);
252 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? true : false;
256 msg_Err( p_intf , "unable to parse directory" );
259 return( VLC_ENOMEM );
261 msg_Dbg( p_intf, "file=%s (url=%s)",
266 char *psz_type = strdup( "text/html; charset=UTF-8" );
267 if( strstr( &dir[strlen( psz_root )], ".xml" ) )
269 char *psz = strstr( psz_type, "html;" );
279 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
281 f->b_html ? psz_type : NULL,
282 user, password, p_acl,
285 if( f->p_file != NULL )
287 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
292 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
294 user, password, p_acl,
295 HandlerCallback, h );
296 if( h->p_handler != NULL )
298 TAB_APPEND( p_sys->i_files, p_sys->pp_files,
299 (httpd_file_sys_t *)h );
303 /* for url that ends by / add
304 * - a redirect from rep to rep/
305 * - in case of index.* rep/index.html to rep/ */
306 if( f && f->name[strlen(f->name) - 1] == '/' )
308 char *psz_redir = strdup( f->name );
310 psz_redir[strlen( psz_redir ) - 1] = '\0';
312 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
313 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
316 if( b_index && ( p = strstr( f->file, "index." ) ) )
318 asprintf( &psz_redir, "%s%s", f->name, p );
320 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
321 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
322 f->name, psz_redir );
333 ACL_Destroy( p_acl );
340 /*************************************************************************
342 *************************************************************************/
343 void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl,
344 playlist_item_t *p_node, char *name, mvar_t *s,
349 if( p_node->i_children == -1 )
353 mvar_t *itm = mvar_New( name, "set" );
355 if( p_pl->status.p_item && p_node &&
356 p_pl->status.p_item->p_input && p_node->p_input &&
357 p_pl->status.p_item->p_input->i_id == p_node->p_input->i_id )
359 mvar_AppendNewVar( itm, "current", "1" );
363 mvar_AppendNewVar( itm, "current", "0" );
366 sprintf( value, "%d", p_node->i_id );
367 mvar_AppendNewVar( itm, "index", value );
369 psz = input_item_GetName( p_node->p_input );
370 mvar_AppendNewVar( itm, "name", psz );
373 psz = input_item_GetURI( p_node->p_input );
374 mvar_AppendNewVar( itm, "uri", psz );
377 sprintf( value, "Item");
378 mvar_AppendNewVar( itm, "type", value );
380 sprintf( value, "%d", i_depth );
381 mvar_AppendNewVar( itm, "depth", value );
383 if( p_node->i_flags & PLAYLIST_RO_FLAG )
385 mvar_AppendNewVar( itm, "ro", "ro" );
389 mvar_AppendNewVar( itm, "ro", "rw" );
392 sprintf( value, "%ld",
393 (long) input_item_GetDuration( p_node->p_input ) );
394 mvar_AppendNewVar( itm, "duration", value );
396 mvar_AppendVar( s, itm );
402 mvar_t *itm = mvar_New( name, "set" );
404 mvar_AppendNewVar( itm, "name", p_node->p_input->psz_name );
405 mvar_AppendNewVar( itm, "uri", p_node->p_input->psz_name );
407 sprintf( value, "Node" );
408 mvar_AppendNewVar( itm, "type", value );
410 sprintf( value, "%d", p_node->i_id );
411 mvar_AppendNewVar( itm, "index", value );
413 sprintf( value, "%d", p_node->i_children);
414 mvar_AppendNewVar( itm, "i_children", value );
416 sprintf( value, "%d", i_depth );
417 mvar_AppendNewVar( itm, "depth", value );
419 if( p_node->i_flags & PLAYLIST_RO_FLAG )
421 mvar_AppendNewVar( itm, "ro", "ro" );
425 mvar_AppendNewVar( itm, "ro", "rw" );
428 mvar_AppendVar( s, itm );
430 for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
431 PlaylistListNode( p_intf, p_pl,
432 p_node->pp_children[i_child],
433 name, s, i_depth + 1);
439 /****************************************************************************
440 * Seek command parsing handling
441 ****************************************************************************/
442 void HandleSeek( intf_thread_t *p_intf, char *p_value )
444 intf_sys_t *p_sys = p_intf->p_sys;
450 #define POSITION_ABSOLUTE 12
451 #define POSITION_REL_FOR 13
452 #define POSITION_REL_BACK 11
453 #define VL_TIME_ABSOLUTE 0
454 #define VL_TIME_REL_FOR 1
455 #define VL_TIME_REL_BACK -1
458 var_Get( p_sys->p_input, "length", &val );
459 i_length = val.i_time;
461 while( p_value[0] != '\0' )
467 i_relative = VL_TIME_REL_FOR;
473 i_relative = VL_TIME_REL_BACK;
477 case '0': case '1': case '2': case '3': case '4':
478 case '5': case '6': case '7': case '8': case '9':
480 i_stock = strtol( p_value , &p_value , 10 );
483 case '%': /* for percentage ie position */
485 i_relative += POSITION_ABSOLUTE;
493 i_value = 60 * (i_value + i_stock) ;
498 case 'h': case 'H': /* hours */
500 i_value += 3600 * i_stock;
502 /* other characters which are not numbers are not
504 while( ((p_value[0] < '0') || (p_value[0] > '9'))
505 && (p_value[0] != '\0') )
511 case 'm': case 'M': case '\'': /* minutes */
513 i_value += 60 * i_stock;
516 while( ((p_value[0] < '0') || (p_value[0] > '9'))
517 && (p_value[0] != '\0') )
523 case 's': case 'S': case '"': /* seconds */
527 while( ((p_value[0] < '0') || (p_value[0] > '9'))
528 && (p_value[0] != '\0') )
542 /* if there is no known symbol, I consider it as seconds.
543 * Otherwise, i_stock = 0 */
548 case VL_TIME_ABSOLUTE:
550 if( (uint64_t)( i_value ) * 1000000 <= i_length )
551 val.i_time = (uint64_t)( i_value ) * 1000000;
553 val.i_time = i_length;
555 var_Set( p_sys->p_input, "time", val );
556 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
559 case VL_TIME_REL_FOR:
561 var_Get( p_sys->p_input, "time", &val );
562 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
564 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
567 val.i_time = i_length;
569 var_Set( p_sys->p_input, "time", val );
570 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
573 case VL_TIME_REL_BACK:
575 var_Get( p_sys->p_input, "time", &val );
576 if( (int64_t)( i_value ) * 1000000 > val.i_time )
581 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
583 var_Set( p_sys->p_input, "time", val );
584 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
587 case POSITION_ABSOLUTE:
589 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
591 var_Set( p_sys->p_input, "position", val );
592 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
595 case POSITION_REL_FOR:
597 var_Get( p_sys->p_input, "position", &val );
598 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
600 var_Set( p_sys->p_input, "position", val );
601 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
605 case POSITION_REL_BACK:
607 var_Get( p_sys->p_input, "position", &val );
608 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
610 var_Set( p_sys->p_input, "position", val );
611 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
617 msg_Dbg( p_intf, "invalid seek request" );
622 #undef POSITION_ABSOLUTE
623 #undef POSITION_REL_FOR
624 #undef POSITION_REL_BACK
625 #undef VL_TIME_ABSOLUTE
626 #undef VL_TIME_REL_FOR
627 #undef VL_TIME_REL_BACK
631 /****************************************************************************
632 * URI Parsing functions
633 ****************************************************************************/
634 int TestURIParam( char *psz_uri, const char *psz_name )
638 while( (p = strstr( p, psz_name )) )
640 /* Verify that we are dealing with a post/get argument */
641 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
642 && p[strlen(psz_name)] == '=' )
652 static char *FindURIValue( char *psz_uri, const char *restrict psz_name,
653 size_t *restrict p_len )
655 char *p = psz_uri, *end;
658 while( (p = strstr( p, psz_name )) )
660 /* Verify that we are dealing with a post/get argument */
661 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
662 && p[strlen(psz_name)] == '=' )
673 p += strlen( psz_name );
676 if( ( end = strchr( p, '\n' ) ) != NULL )
679 if( ( end > p ) && ( end[-1] == '\r' ) )
687 if( ( end = strchr( p, '&' ) ) != NULL )
697 char *ExtractURIValue( char *restrict psz_uri,
698 const char *restrict psz_name,
699 char *restrict psz_buf, size_t bufsize )
702 char *psz_value = FindURIValue( psz_uri, psz_name, &len );
705 if( psz_value == NULL )
712 psz_next = psz_value + len;
718 strncpy( psz_buf, psz_value, len );
725 char *ExtractURIString( char *restrict psz_uri,
726 const char *restrict psz_name )
729 char *psz_value = FindURIValue( psz_uri, psz_name, &len );
731 if( psz_value == NULL )
734 char *res = malloc( len + 1 );
738 memcpy( res, psz_value, len );
744 /* Since the resulting string is smaller we can work in place, so it is
745 * permitted to have psz == new. new points to the first word of the
746 * string, the function returns the remaining string. */
747 char *FirstWord( char *psz, char *new )
754 while( *psz != '\0' && *psz != ' ' )
759 while( *psz != '\0' && *psz != c )
761 if( *psz == '\\' && psz[1] != '\0' )
770 if( *psz == '\\' && psz[1] != '\0' )
784 /**********************************************************************
785 * MRLParse: parse the MRL, find the MRL string and the options,
786 * create an item with all information in it, and return the item.
787 * return NULL if there is an error.
788 **********************************************************************/
790 /* Function analog to FirstWord except that it relies on colon instead
791 * of space to delimit option boundaries. */
792 static char *FirstOption( char *psz, char *new )
794 bool b_end, b_start = true;
799 while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
804 while( *psz != '\0' && *psz != c )
806 if( *psz == '\\' && psz[1] != '\0' )
816 if( *psz == '\\' && psz[1] != '\0' )
825 while (new[-1] == ' ')
835 input_item_t *MRLParse( intf_thread_t *p_intf, char *_psz,
838 char *psz = strdup( _psz );
841 input_item_t * p_input = NULL;
843 /* extract the mrl */
844 s_temp = FirstOption( s_mrl, s_mrl );
847 s_temp = s_mrl + strlen( s_mrl );
850 p_input = input_ItemNew( p_intf, s_mrl, psz_name );
853 /* now we can take care of the options */
854 while ( *s_mrl != '\0' )
856 s_temp = FirstOption( s_mrl, s_mrl );
861 s_temp = s_mrl + strlen( s_mrl );
863 input_ItemAddOption( p_input, s_mrl );
871 /**********************************************************************
872 * RealPath: parse ../, ~ and path stuff
873 **********************************************************************/
874 char *RealPath( const char *psz_src )
878 int i_len = strlen(psz_src);
879 const char sep = DIR_SEP_CHAR;
881 psz_dir = malloc( i_len + 2 );
882 strcpy( psz_dir, psz_src );
884 /* Add a trailing sep to ease the .. step */
885 psz_dir[i_len] = sep;
886 psz_dir[i_len + 1] = '\0';
888 #if (DIR_SEP_CHAR != '/')
889 /* Convert all / to native separator */
891 while( (p = strchr( p, '/' )) != NULL )
897 /* FIXME: this could be O(N) rather than O(N²)... */
898 /* Remove multiple separators and /./ */
900 while( (p = strchr( p, sep )) != NULL )
903 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
904 else if( p[1] == '.' && p[2] == sep )
905 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
910 if( psz_dir[0] == '~' )
913 asprintf( &dir, "%s%s", config_GetHomeDir(), psz_dir + 1 );
918 if( strlen(psz_dir) > 2 )
922 while( (p = strchr( p, sep )) != NULL )
924 if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
928 if( (q = strrchr( psz_dir, sep )) != NULL )
930 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
935 memmove( psz_dir, p, strlen(p) + 1 );
944 /* Remove trailing sep if there are at least 2 sep in the string
945 * (handles the C:\ stuff) */
946 p = strrchr( psz_dir, sep );
947 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )