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;
123 if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
125 if( errno != ENOENT && errno != ENOTDIR )
126 msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
130 i_dirlen = strlen( psz_dir );
131 if( i_dirlen + 10 > MAX_DIR_SIZE )
133 msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
138 msg_Dbg( p_intf, "dir=%s", psz_dir );
140 snprintf( dir, sizeof( dir ), "%s"DIR_SEP".access", psz_dir );
141 if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
146 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
148 i_size = fread( line, 1, 1023, file );
152 while( i_size > 0 && ( line[i_size-1] == '\n' ||
153 line[i_size-1] == '\r' ) )
160 p = strchr( line, ':' );
164 user = strdup( line );
165 password = strdup( p );
168 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
169 user, password, i_size );
174 snprintf( dir, sizeof( dir ), "%s"DIR_SEP".hosts", psz_dir );
175 p_acl = ACL_Create( p_intf, false );
176 if( ACL_LoadFile( p_acl, dir ) )
178 ACL_Destroy( p_acl );
181 if( stat( dir, &st ) == 0 )
192 /* parse psz_src dir */
193 if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
198 if( ( psz_filename[0] == '.' )
199 || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
201 free( psz_filename );
205 snprintf( dir, sizeof( dir ), "%s"DIR_SEP"%s", psz_dir, psz_filename );
206 free( psz_filename );
208 if( ParseDirectory( p_intf, psz_root, dir ) )
210 httpd_file_sys_t *f = NULL;
211 httpd_handler_sys_t *h = NULL;
213 char *psz_name, *psz_ext;
215 psz_name = FileToUrl( &dir[strlen( psz_root )], &b_index );
216 psz_ext = strrchr( dir, '.' );
217 if( psz_ext != NULL )
221 for( i = 0; i < p_sys->i_handlers; i++ )
222 if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
224 if( i < p_sys->i_handlers )
226 f = malloc( sizeof( httpd_handler_sys_t ) );
227 h = (httpd_handler_sys_t *)f;
229 h->p_association = p_sys->pp_handlers[i];
234 f = malloc( sizeof( httpd_file_sys_t ) );
235 f->b_handler = false;
242 f->file = strdup (dir);
244 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? true : false;
248 msg_Err( p_intf , "unable to parse directory" );
251 return( VLC_ENOMEM );
253 msg_Dbg( p_intf, "file=%s (url=%s)",
258 char *psz_type = strdup( "text/html; charset=UTF-8" );
259 if( strstr( &dir[strlen( psz_root )], ".xml" ) )
261 char *psz = strstr( psz_type, "html;" );
271 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
273 f->b_html ? psz_type : NULL,
274 user, password, p_acl,
277 if( f->p_file != NULL )
279 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
284 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
286 user, password, p_acl,
287 HandlerCallback, h );
288 if( h->p_handler != NULL )
290 TAB_APPEND( p_sys->i_files, p_sys->pp_files,
291 (httpd_file_sys_t *)h );
295 /* for url that ends by / add
296 * - a redirect from rep to rep/
297 * - in case of index.* rep/index.html to rep/ */
298 if( f && f->name[strlen(f->name) - 1] == '/' )
300 char *psz_redir = strdup( f->name );
302 psz_redir[strlen( psz_redir ) - 1] = '\0';
304 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
305 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
308 if( b_index && ( p = strstr( f->file, "index." ) ) )
310 if( asprintf( &psz_redir, "%s%s", f->name, p ) != -1 )
312 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
313 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
314 f->name, psz_redir );
326 ACL_Destroy( p_acl );
333 /*************************************************************************
335 *************************************************************************/
336 void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl,
337 playlist_item_t *p_node, char *name, mvar_t *s,
340 if( !p_node || !p_node->p_input )
343 if( p_node->i_children == -1 )
347 playlist_item_t * p_item = playlist_CurrentPlayingItem( p_pl );
348 if( !p_item || !p_item->p_input )
351 mvar_t *itm = mvar_New( name, "set" );
352 if( p_item->p_input->i_id == p_node->p_input->i_id )
353 mvar_AppendNewVar( itm, "current", "1" );
355 mvar_AppendNewVar( itm, "current", "0" );
357 sprintf( value, "%d", p_node->i_id );
358 mvar_AppendNewVar( itm, "index", value );
360 psz = input_item_GetName( p_node->p_input );
361 mvar_AppendNewVar( itm, "name", psz );
364 psz = input_item_GetURI( p_node->p_input );
365 mvar_AppendNewVar( itm, "uri", psz );
368 sprintf( value, "Item");
369 mvar_AppendNewVar( itm, "type", value );
371 sprintf( value, "%d", i_depth );
372 mvar_AppendNewVar( itm, "depth", value );
374 if( p_node->i_flags & PLAYLIST_RO_FLAG )
375 mvar_AppendNewVar( itm, "ro", "ro" );
377 mvar_AppendNewVar( itm, "ro", "rw" );
379 sprintf( value, "%"PRId64, input_item_GetDuration( p_node->p_input ) );
380 mvar_AppendNewVar( itm, "duration", value );
382 //Adding extra meta-information to each playlist item
384 psz = input_item_GetTitle( p_node->p_input );
385 mvar_AppendNewVar( itm, "title", psz );
387 psz = input_item_GetArtist( p_node->p_input );
388 mvar_AppendNewVar( itm, "artist", psz );
390 psz = input_item_GetGenre( p_node->p_input );
391 mvar_AppendNewVar( itm, "genre", psz );
393 psz = input_item_GetCopyright( p_node->p_input );
394 mvar_AppendNewVar( itm, "copyright", psz );
396 psz = input_item_GetAlbum( p_node->p_input );
397 mvar_AppendNewVar( itm, "album", psz );
399 psz = input_item_GetTrackNum( p_node->p_input );
400 mvar_AppendNewVar( itm, "track", psz );
402 psz = input_item_GetDescription( p_node->p_input );
403 mvar_AppendNewVar( itm, "description", psz );
405 psz = input_item_GetRating( p_node->p_input );
406 mvar_AppendNewVar( itm, "rating", psz );
408 psz = input_item_GetDate( p_node->p_input );
409 mvar_AppendNewVar( itm, "date", psz );
411 psz = input_item_GetURL( p_node->p_input );
412 mvar_AppendNewVar( itm, "url", psz );
414 psz = input_item_GetLanguage( p_node->p_input );
415 mvar_AppendNewVar( itm, "language", psz );
417 psz = input_item_GetNowPlaying( p_node->p_input );
418 mvar_AppendNewVar( itm, "now_playing", psz );
420 psz = input_item_GetPublisher( p_node->p_input );
421 mvar_AppendNewVar( itm, "publisher", psz );
423 psz = input_item_GetEncodedBy( p_node->p_input );
424 mvar_AppendNewVar( itm, "encoded_by", psz );
426 psz = input_item_GetArtURL( p_node->p_input );
427 mvar_AppendNewVar( itm, "art_url", psz );
429 psz = input_item_GetTrackID( p_node->p_input );
430 mvar_AppendNewVar( itm, "track_id", psz );
432 mvar_AppendVar( s, itm );
438 mvar_t *itm = mvar_New( name, "set" );
440 mvar_AppendNewVar( itm, "name", p_node->p_input->psz_name );
441 mvar_AppendNewVar( itm, "uri", p_node->p_input->psz_name );
443 sprintf( value, "Node" );
444 mvar_AppendNewVar( itm, "type", value );
446 sprintf( value, "%d", p_node->i_id );
447 mvar_AppendNewVar( itm, "index", value );
449 sprintf( value, "%d", p_node->i_children);
450 mvar_AppendNewVar( itm, "i_children", value );
452 sprintf( value, "%d", i_depth );
453 mvar_AppendNewVar( itm, "depth", value );
455 if( p_node->i_flags & PLAYLIST_RO_FLAG )
456 mvar_AppendNewVar( itm, "ro", "ro" );
458 mvar_AppendNewVar( itm, "ro", "rw" );
460 mvar_AppendVar( s, itm );
462 for( i_child = 0 ; i_child < p_node->i_children ; i_child++ )
463 PlaylistListNode( p_intf, p_pl, p_node->pp_children[i_child],
464 name, s, i_depth + 1);
468 /****************************************************************************
469 * Seek command parsing handling
470 ****************************************************************************/
471 void HandleSeek( intf_thread_t *p_intf, char *p_value )
473 intf_sys_t *p_sys = p_intf->p_sys;
479 #define POSITION_ABSOLUTE 12
480 #define POSITION_REL_FOR 13
481 #define POSITION_REL_BACK 11
482 #define VL_TIME_ABSOLUTE 0
483 #define VL_TIME_REL_FOR 1
484 #define VL_TIME_REL_BACK -1
487 var_Get( p_sys->p_input, "length", &val );
488 i_length = val.i_time;
490 while( p_value[0] != '\0' )
496 i_relative = VL_TIME_REL_FOR;
502 i_relative = VL_TIME_REL_BACK;
506 case '0': case '1': case '2': case '3': case '4':
507 case '5': case '6': case '7': case '8': case '9':
509 i_stock = strtol( p_value , &p_value , 10 );
512 case '%': /* for percentage ie position */
514 i_relative += POSITION_ABSOLUTE;
522 i_value = 60 * (i_value + i_stock) ;
527 case 'h': case 'H': /* hours */
529 i_value += 3600 * i_stock;
531 /* other characters which are not numbers are not
533 while( ((p_value[0] < '0') || (p_value[0] > '9'))
534 && (p_value[0] != '\0') )
540 case 'm': case 'M': case '\'': /* minutes */
542 i_value += 60 * i_stock;
545 while( ((p_value[0] < '0') || (p_value[0] > '9'))
546 && (p_value[0] != '\0') )
552 case 's': case 'S': case '"': /* seconds */
556 while( ((p_value[0] < '0') || (p_value[0] > '9'))
557 && (p_value[0] != '\0') )
571 /* if there is no known symbol, I consider it as seconds.
572 * Otherwise, i_stock = 0 */
577 case VL_TIME_ABSOLUTE:
579 if( (uint64_t)( i_value ) * 1000000 <= i_length )
580 val.i_time = (uint64_t)( i_value ) * 1000000;
582 val.i_time = i_length;
584 var_Set( p_sys->p_input, "time", val );
585 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
588 case VL_TIME_REL_FOR:
590 var_Get( p_sys->p_input, "time", &val );
591 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
593 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
596 val.i_time = i_length;
598 var_Set( p_sys->p_input, "time", val );
599 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
602 case VL_TIME_REL_BACK:
604 var_Get( p_sys->p_input, "time", &val );
605 if( (int64_t)( i_value ) * 1000000 > val.i_time )
610 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
612 var_Set( p_sys->p_input, "time", val );
613 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
616 case POSITION_ABSOLUTE:
618 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
620 var_Set( p_sys->p_input, "position", val );
621 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
624 case POSITION_REL_FOR:
626 var_Get( p_sys->p_input, "position", &val );
627 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
629 var_Set( p_sys->p_input, "position", val );
630 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
634 case POSITION_REL_BACK:
636 var_Get( p_sys->p_input, "position", &val );
637 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
639 var_Set( p_sys->p_input, "position", val );
640 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
646 msg_Dbg( p_intf, "invalid seek request" );
651 #undef POSITION_ABSOLUTE
652 #undef POSITION_REL_FOR
653 #undef POSITION_REL_BACK
654 #undef VL_TIME_ABSOLUTE
655 #undef VL_TIME_REL_FOR
656 #undef VL_TIME_REL_BACK
660 /****************************************************************************
661 * URI Parsing functions
662 ****************************************************************************/
663 int TestURIParam( char *psz_uri, const char *psz_name )
667 while( (p = strstr( p, psz_name )) )
669 /* Verify that we are dealing with a post/get argument */
670 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
671 && p[strlen(psz_name)] == '=' )
681 static const char *FindURIValue( const char *psz_uri, const char *restrict psz_name,
682 size_t *restrict p_len )
684 const char *p = psz_uri, *end;
687 while( (p = strstr( p, psz_name )) )
689 /* Verify that we are dealing with a post/get argument */
690 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
691 && p[strlen(psz_name)] == '=' )
702 p += strlen( psz_name );
705 if( ( end = strchr( p, '\n' ) ) != NULL )
708 if( ( end > p ) && ( end[-1] == '\r' ) )
716 if( ( end = strchr( p, '&' ) ) != NULL )
726 const char *ExtractURIValue( const char *restrict psz_uri,
727 const char *restrict psz_name,
728 char *restrict psz_buf, size_t bufsize )
731 const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
732 const char *psz_next;
734 if( psz_value == NULL )
741 psz_next = psz_value + len;
747 strncpy( psz_buf, psz_value, len );
754 char *ExtractURIString( const char *restrict psz_uri,
755 const char *restrict psz_name )
758 const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
760 if( psz_value == NULL )
763 char *res = malloc( len + 1 );
767 memcpy( res, psz_value, len );
773 /* Since the resulting string is smaller we can work in place, so it is
774 * permitted to have psz == new. new points to the first word of the
775 * string, the function returns the remaining string. */
776 char *FirstWord( char *psz, char *new )
783 while( *psz != '\0' && *psz != ' ' )
788 while( *psz != '\0' && *psz != c )
790 if( *psz == '\\' && psz[1] != '\0' )
799 if( *psz == '\\' && psz[1] != '\0' )
813 /**********************************************************************
814 * MRLParse: parse the MRL, find the MRL string and the options,
815 * create an item with all information in it, and return the item.
816 * return NULL if there is an error.
817 **********************************************************************/
819 /* Function analog to FirstWord except that it relies on colon instead
820 * of space to delimit option boundaries. */
821 static char *FirstOption( char *psz, char *new )
823 bool b_end, b_start = true;
828 while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
833 while( *psz != '\0' && *psz != c )
835 if( *psz == '\\' && psz[1] != '\0' )
845 if( *psz == '\\' && psz[1] != '\0' )
854 while (new[-1] == ' ')
864 input_item_t *MRLParse( intf_thread_t *p_intf, const char *mrl,
867 char *psz = strdup( mrl ), *s_mrl = psz, *s_temp;
870 /* extract the mrl */
871 s_temp = FirstOption( s_mrl, s_mrl );
874 s_temp = s_mrl + strlen( s_mrl );
877 input_item_t *p_input = input_item_New( p_intf, s_mrl, psz_name );
878 if( p_input == NULL )
882 /* now we can take care of the options */
883 while ( *s_mrl != '\0' )
885 s_temp = FirstOption( s_mrl, s_mrl );
890 s_temp = s_mrl + strlen( s_mrl );
892 input_item_AddOption( p_input, s_mrl, VLC_INPUT_OPTION_TRUSTED );
899 /**********************************************************************
900 * RealPath: parse ../, ~ and path stuff
901 **********************************************************************/
902 char *RealPath( const char *psz_src )
906 int i_len = strlen(psz_src);
908 psz_dir = malloc( i_len + 2 );
909 strcpy( psz_dir, psz_src );
911 /* Add a trailing sep to ease the .. step */
912 psz_dir[i_len] = DIR_SEP_CHAR;
913 psz_dir[i_len + 1] = '\0';
915 #if (DIR_SEP_CHAR != '/')
916 /* Convert all / to native separator */
918 while( (p = strchr( p, '/' )) != NULL )
924 /* FIXME: this could be O(N) rather than O(N²)... */
925 /* Remove multiple separators and /./ */
927 while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
929 if( p[1] == DIR_SEP_CHAR )
930 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
931 else if( p[1] == '.' && p[2] == DIR_SEP_CHAR )
932 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
937 if( psz_dir[0] == '~' )
940 asprintf( &dir, "%s%s", config_GetHomeDir(), psz_dir + 1 );
945 if( strlen(psz_dir) > 2 )
949 while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
951 if( p[-1] == '.' && p[-2] == '.' && p[-3] == DIR_SEP_CHAR )
955 if( (q = strrchr( psz_dir, DIR_SEP_CHAR )) != NULL )
957 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
962 memmove( psz_dir, p, strlen(p) + 1 );
971 /* Remove trailing sep if there are at least 2 sep in the string
972 * (handles the C:\ stuff) */
973 p = strrchr( psz_dir, DIR_SEP_CHAR );
974 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, DIR_SEP_CHAR ) )