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>
35 /****************************************************************************
36 * File and directory functions
37 ****************************************************************************/
39 /* ToUrl: create a good name for an url from filename */
40 char *FileToUrl( char *name, bool *pb_index )
44 url = p = malloc( strlen( name ) + 1 );
53 while( *name == '\\' || *name == '/' )
65 /* convert '\\' into '/' */
76 if( ( p = strrchr( url, '/' ) ) != NULL )
78 if( !strncmp( p, "/index.", 7 ) )
88 int FileLoad( FILE *f, char **pp_data, int *pi_data )
92 /* just load the file */
94 *pp_data = xmalloc( 1025 ); /* +1 for \0 */
96 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
99 *pp_data = xrealloc( *pp_data, *pi_data + 1025 );
105 (*pp_data)[*pi_data] = '\0';
110 /* Parse a directory and recursively add files */
111 int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
114 intf_sys_t *p_sys = p_intf->p_sys;
115 char dir[MAX_DIR_SIZE];
121 char *password = NULL;
125 if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
127 if( errno != ENOENT && errno != ENOTDIR )
128 msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
132 i_dirlen = strlen( psz_dir );
133 if( i_dirlen + 10 > MAX_DIR_SIZE )
135 msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
140 msg_Dbg( p_intf, "dir=%s", psz_dir );
142 snprintf( dir, sizeof( dir ), "%s"DIR_SEP".access", psz_dir );
143 if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
148 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
150 i_size = fread( line, 1, 1023, file );
154 while( i_size > 0 && ( line[i_size-1] == '\n' ||
155 line[i_size-1] == '\r' ) )
162 p = strchr( line, ':' );
166 user = strdup( line );
167 password = strdup( p );
170 msg_Dbg( p_intf, "using user=%s (read=%d)", user, i_size );
175 snprintf( dir, sizeof( dir ), "%s"DIR_SEP".hosts", psz_dir );
176 p_acl = ACL_Create( p_intf, false );
177 if( ACL_LoadFile( p_acl, dir ) )
179 ACL_Destroy( p_acl );
182 if( utf8_stat( dir, &st ) == 0 )
195 /* parse psz_src dir */
196 if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
201 if( ( psz_filename[0] == '.' )
202 || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
204 free( psz_filename );
208 snprintf( dir, sizeof( dir ), "%s"DIR_SEP"%s", psz_dir, psz_filename );
209 free( psz_filename );
211 if( ParseDirectory( p_intf, psz_root, dir ) )
213 httpd_file_sys_t *f = NULL;
214 httpd_handler_sys_t *h = NULL;
216 char *psz_name, *psz_ext;
218 psz_name = FileToUrl( &dir[strlen( psz_root )], &b_index );
219 psz_ext = strrchr( dir, '.' );
220 if( psz_ext != NULL )
224 for( i = 0; i < p_sys->i_handlers; i++ )
225 if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
227 if( i < p_sys->i_handlers )
229 f = malloc( sizeof( httpd_handler_sys_t ) );
230 h = (httpd_handler_sys_t *)f;
232 h->p_association = p_sys->pp_handlers[i];
237 f = xmalloc( sizeof( httpd_file_sys_t ) );
238 f->b_handler = false;
245 f->file = strdup (dir);
247 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? true : false;
251 msg_Err( p_intf , "unable to parse directory" );
254 return( VLC_ENOMEM );
256 msg_Dbg( p_intf, "file=%s (url=%s)",
261 char *psz_type = strdup( "text/html; charset=UTF-8" );
262 if( strstr( &dir[strlen( psz_root )], ".xml" ) )
264 char *psz = strstr( psz_type, "html;" );
274 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
276 f->b_html ? psz_type : NULL,
277 user, password, p_acl,
280 if( f->p_file != NULL )
282 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
287 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
289 user, password, p_acl,
290 HandlerCallback, h );
291 if( h->p_handler != NULL )
293 TAB_APPEND( p_sys->i_files, p_sys->pp_files,
294 (httpd_file_sys_t *)h );
298 /* for url that ends by / add
299 * - a redirect from rep to rep/
300 * - in case of index.* rep/index.html to rep/ */
301 if( f && f->name[strlen(f->name) - 1] == '/' )
303 char *psz_redir = strdup( f->name );
305 psz_redir[strlen( psz_redir ) - 1] = '\0';
307 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
308 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
311 if( b_index && ( p = strstr( f->file, "index." ) ) )
313 if( asprintf( &psz_redir, "%s%s", f->name, p ) != -1 )
315 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
316 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
317 f->name, psz_redir );
329 ACL_Destroy( p_acl );
336 /*************************************************************************
338 *************************************************************************/
339 void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl,
340 playlist_item_t *p_node, char *name, mvar_t *s,
343 if( !p_node || !p_node->p_input )
346 if( p_node->i_children == -1 )
350 playlist_item_t * p_item = playlist_CurrentPlayingItem( p_pl );
351 if( !p_item || !p_item->p_input )
354 mvar_t *itm = mvar_New( name, "set" );
355 if( p_item->p_input == p_node->p_input )
356 mvar_AppendNewVar( itm, "current", "1" );
358 mvar_AppendNewVar( itm, "current", "0" );
360 sprintf( value, "%d", p_node->i_id );
361 mvar_AppendNewVar( itm, "index", value );
363 psz = input_item_GetName( p_node->p_input );
364 mvar_AppendNewVar( itm, "name", psz );
367 psz = input_item_GetURI( p_node->p_input );
368 mvar_AppendNewVar( itm, "uri", psz );
371 mvar_AppendNewVar( itm, "type", "Item" );
373 sprintf( value, "%d", i_depth );
374 mvar_AppendNewVar( itm, "depth", value );
376 if( p_node->i_flags & PLAYLIST_RO_FLAG )
377 mvar_AppendNewVar( itm, "ro", "ro" );
379 mvar_AppendNewVar( itm, "ro", "rw" );
381 sprintf( value, "%"PRId64, input_item_GetDuration( p_node->p_input ) );
382 mvar_AppendNewVar( itm, "duration", value );
384 //Adding extra meta-information to each playlist item
386 psz = input_item_GetTitle( p_node->p_input );
387 mvar_AppendNewVar( itm, "title", psz );
390 psz = input_item_GetArtist( p_node->p_input );
391 mvar_AppendNewVar( itm, "artist", psz );
394 psz = input_item_GetGenre( p_node->p_input );
395 mvar_AppendNewVar( itm, "genre", psz );
398 psz = input_item_GetCopyright( p_node->p_input );
399 mvar_AppendNewVar( itm, "copyright", psz );
402 psz = input_item_GetAlbum( p_node->p_input );
403 mvar_AppendNewVar( itm, "album", psz );
406 psz = input_item_GetTrackNum( p_node->p_input );
407 mvar_AppendNewVar( itm, "track", psz );
410 psz = input_item_GetDescription( p_node->p_input );
411 mvar_AppendNewVar( itm, "description", psz );
414 psz = input_item_GetRating( p_node->p_input );
415 mvar_AppendNewVar( itm, "rating", psz );
418 psz = input_item_GetDate( p_node->p_input );
419 mvar_AppendNewVar( itm, "date", psz );
422 psz = input_item_GetURL( p_node->p_input );
423 mvar_AppendNewVar( itm, "url", psz );
426 psz = input_item_GetLanguage( p_node->p_input );
427 mvar_AppendNewVar( itm, "language", psz );
430 psz = input_item_GetNowPlaying( p_node->p_input );
431 mvar_AppendNewVar( itm, "now_playing", psz );
434 psz = input_item_GetPublisher( p_node->p_input );
435 mvar_AppendNewVar( itm, "publisher", psz );
438 psz = input_item_GetEncodedBy( p_node->p_input );
439 mvar_AppendNewVar( itm, "encoded_by", psz );
442 psz = input_item_GetArtURL( p_node->p_input );
443 mvar_AppendNewVar( itm, "art_url", psz );
446 psz = input_item_GetTrackID( p_node->p_input );
447 mvar_AppendNewVar( itm, "track_id", psz );
450 mvar_AppendVar( s, itm );
456 mvar_t *itm = mvar_New( name, "set" );
458 mvar_AppendNewVar( itm, "name", p_node->p_input->psz_name );
459 mvar_AppendNewVar( itm, "uri", p_node->p_input->psz_name );
461 mvar_AppendNewVar( itm, "type", "Node" );
463 sprintf( value, "%d", p_node->i_id );
464 mvar_AppendNewVar( itm, "index", value );
466 sprintf( value, "%d", p_node->i_children);
467 mvar_AppendNewVar( itm, "i_children", value );
469 sprintf( value, "%d", i_depth );
470 mvar_AppendNewVar( itm, "depth", value );
472 if( p_node->i_flags & PLAYLIST_RO_FLAG )
473 mvar_AppendNewVar( itm, "ro", "ro" );
475 mvar_AppendNewVar( itm, "ro", "rw" );
477 mvar_AppendVar( s, itm );
479 for( i_child = 0 ; i_child < p_node->i_children ; i_child++ )
480 PlaylistListNode( p_intf, p_pl, p_node->pp_children[i_child],
481 name, s, i_depth + 1);
485 /****************************************************************************
486 * Seek command parsing handling
487 ****************************************************************************/
488 void HandleSeek( intf_thread_t *p_intf, char *p_value )
490 intf_sys_t *p_sys = p_intf->p_sys;
496 #define POSITION_ABSOLUTE 12
497 #define POSITION_REL_FOR 13
498 #define POSITION_REL_BACK 11
499 #define VL_TIME_ABSOLUTE 0
500 #define VL_TIME_REL_FOR 1
501 #define VL_TIME_REL_BACK -1
504 var_Get( p_sys->p_input, "length", &val );
505 i_length = val.i_time;
507 while( p_value[0] != '\0' )
513 i_relative = VL_TIME_REL_FOR;
519 i_relative = VL_TIME_REL_BACK;
523 case '0': case '1': case '2': case '3': case '4':
524 case '5': case '6': case '7': case '8': case '9':
526 i_stock = strtol( p_value , &p_value , 10 );
529 case '%': /* for percentage ie position */
531 i_relative += POSITION_ABSOLUTE;
539 i_value = 60 * (i_value + i_stock) ;
544 case 'h': case 'H': /* hours */
546 i_value += 3600 * i_stock;
548 /* other characters which are not numbers are not
550 while( ((p_value[0] < '0') || (p_value[0] > '9'))
551 && (p_value[0] != '\0') )
557 case 'm': case 'M': case '\'': /* minutes */
559 i_value += 60 * i_stock;
562 while( ((p_value[0] < '0') || (p_value[0] > '9'))
563 && (p_value[0] != '\0') )
569 case 's': case 'S': case '"': /* seconds */
573 while( ((p_value[0] < '0') || (p_value[0] > '9'))
574 && (p_value[0] != '\0') )
588 /* if there is no known symbol, I consider it as seconds.
589 * Otherwise, i_stock = 0 */
594 case VL_TIME_ABSOLUTE:
596 if( (uint64_t)( i_value ) * 1000000 <= i_length )
597 val.i_time = (uint64_t)( i_value ) * 1000000;
599 val.i_time = i_length;
601 var_Set( p_sys->p_input, "time", val );
602 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
605 case VL_TIME_REL_FOR:
607 var_Get( p_sys->p_input, "time", &val );
608 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
610 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
613 val.i_time = i_length;
615 var_Set( p_sys->p_input, "time", val );
616 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
619 case VL_TIME_REL_BACK:
621 var_Get( p_sys->p_input, "time", &val );
622 if( (int64_t)( i_value ) * 1000000 > val.i_time )
627 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
629 var_Set( p_sys->p_input, "time", val );
630 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
633 case POSITION_ABSOLUTE:
635 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
637 var_Set( p_sys->p_input, "position", val );
638 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
641 case POSITION_REL_FOR:
643 var_Get( p_sys->p_input, "position", &val );
644 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
646 var_Set( p_sys->p_input, "position", val );
647 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
651 case POSITION_REL_BACK:
653 var_Get( p_sys->p_input, "position", &val );
654 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
656 var_Set( p_sys->p_input, "position", val );
657 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
663 msg_Dbg( p_intf, "invalid seek request" );
668 #undef POSITION_ABSOLUTE
669 #undef POSITION_REL_FOR
670 #undef POSITION_REL_BACK
671 #undef VL_TIME_ABSOLUTE
672 #undef VL_TIME_REL_FOR
673 #undef VL_TIME_REL_BACK
677 /****************************************************************************
678 * URI Parsing functions
679 ****************************************************************************/
680 int TestURIParam( char *psz_uri, const char *psz_name )
684 while( (p = strstr( p, psz_name )) )
686 /* Verify that we are dealing with a post/get argument */
687 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
688 && p[strlen(psz_name)] == '=' )
698 static const char *FindURIValue( const char *psz_uri, const char *restrict psz_name,
699 size_t *restrict p_len )
701 const char *p = psz_uri, *end;
704 while( (p = strstr( p, psz_name )) )
706 /* Verify that we are dealing with a post/get argument */
707 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
708 && p[strlen(psz_name)] == '=' )
719 p += strlen( psz_name );
722 if( ( end = strchr( p, '\n' ) ) != NULL )
725 if( ( end > p ) && ( end[-1] == '\r' ) )
733 if( ( end = strchr( p, '&' ) ) != NULL )
743 const char *ExtractURIValue( const char *restrict psz_uri,
744 const char *restrict psz_name,
745 char *restrict psz_buf, size_t bufsize )
748 const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
749 const char *psz_next;
751 if( psz_value == NULL )
758 psz_next = psz_value + len;
764 strncpy( psz_buf, psz_value, len );
771 char *ExtractURIString( const char *restrict psz_uri,
772 const char *restrict psz_name )
775 const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
777 if( psz_value == NULL )
780 char *res = malloc( len + 1 );
784 memcpy( res, psz_value, len );
790 /* Since the resulting string is smaller we can work in place, so it is
791 * permitted to have psz == new. new points to the first word of the
792 * string, the function returns the remaining string. */
793 char *FirstWord( char *psz, char *new )
800 while( *psz != '\0' && *psz != ' ' )
805 while( *psz != '\0' && *psz != c )
807 if( *psz == '\\' && psz[1] != '\0' )
816 if( *psz == '\\' && psz[1] != '\0' )
830 /**********************************************************************
831 * MRLParse: parse the MRL, find the MRL string and the options,
832 * create an item with all information in it, and return the item.
833 * return NULL if there is an error.
834 **********************************************************************/
836 /* Function analog to FirstWord except that it relies on colon instead
837 * of space to delimit option boundaries. */
838 static char *FirstOption( char *psz, char *new )
840 bool b_end, b_start = true;
845 while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
850 while( *psz != '\0' && *psz != c )
852 if( *psz == '\\' && psz[1] != '\0' )
862 if( *psz == '\\' && psz[1] != '\0' )
871 while (new[-1] == ' ')
881 input_item_t *MRLParse( intf_thread_t *p_intf, const char *mrl,
884 char *psz = strdup( mrl ), *s_mrl = psz, *s_temp;
887 /* extract the mrl */
888 s_temp = FirstOption( s_mrl, s_mrl );
891 s_temp = s_mrl + strlen( s_mrl );
894 input_item_t *p_input = input_item_New( p_intf, s_mrl, psz_name );
895 if( p_input == NULL )
899 /* now we can take care of the options */
900 while ( *s_mrl != '\0' )
902 s_temp = FirstOption( s_mrl, s_mrl );
907 s_temp = s_mrl + strlen( s_mrl );
909 input_item_AddOption( p_input, s_mrl, VLC_INPUT_OPTION_TRUSTED );
916 /**********************************************************************
917 * RealPath: parse ../, ~ and path stuff
918 **********************************************************************/
919 char *RealPath( const char *psz_src )
923 int i_len = strlen(psz_src);
925 psz_dir = xmalloc( i_len + 2 );
926 strcpy( psz_dir, psz_src );
928 /* Add a trailing sep to ease the .. step */
929 psz_dir[i_len] = DIR_SEP_CHAR;
930 psz_dir[i_len + 1] = '\0';
932 #if (DIR_SEP_CHAR != '/')
933 /* Convert all / to native separator */
935 while( (p = strchr( p, '/' )) != NULL )
941 /* FIXME: this could be O(N) rather than O(N²)... */
942 /* Remove multiple separators and /./ */
944 while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
946 if( p[1] == DIR_SEP_CHAR )
947 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
948 else if( p[1] == '.' && p[2] == DIR_SEP_CHAR )
949 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
954 if( psz_dir[0] == '~' )
956 char *home = config_GetUserDir( VLC_HOME_DIR ), *dir;
957 if( asprintf( &dir, "%s%s", home, psz_dir + 1 ) != -1 )
965 if( strlen(psz_dir) > 2 )
969 while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
971 if( p[-1] == '.' && p[-2] == '.' && p[-3] == DIR_SEP_CHAR )
975 if( (q = strrchr( psz_dir, DIR_SEP_CHAR )) != NULL )
977 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
982 memmove( psz_dir, p, strlen(p) + 1 );
991 /* Remove trailing sep if there are at least 2 sep in the string
992 * (handles the C:\ stuff) */
993 p = strrchr( psz_dir, DIR_SEP_CHAR );
994 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, DIR_SEP_CHAR ) )