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 (read=%d)", user, i_size );
173 snprintf( dir, sizeof( dir ), "%s"DIR_SEP".hosts", psz_dir );
174 p_acl = ACL_Create( p_intf, false );
175 if( ACL_LoadFile( p_acl, dir ) )
177 ACL_Destroy( p_acl );
180 if( stat( dir, &st ) == 0 )
191 /* parse psz_src dir */
192 if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
197 if( ( psz_filename[0] == '.' )
198 || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
200 free( psz_filename );
204 snprintf( dir, sizeof( dir ), "%s"DIR_SEP"%s", psz_dir, psz_filename );
205 free( psz_filename );
207 if( ParseDirectory( p_intf, psz_root, dir ) )
209 httpd_file_sys_t *f = NULL;
210 httpd_handler_sys_t *h = NULL;
212 char *psz_name, *psz_ext;
214 psz_name = FileToUrl( &dir[strlen( psz_root )], &b_index );
215 psz_ext = strrchr( dir, '.' );
216 if( psz_ext != NULL )
220 for( i = 0; i < p_sys->i_handlers; i++ )
221 if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
223 if( i < p_sys->i_handlers )
225 f = malloc( sizeof( httpd_handler_sys_t ) );
226 h = (httpd_handler_sys_t *)f;
228 h->p_association = p_sys->pp_handlers[i];
233 f = malloc( sizeof( httpd_file_sys_t ) );
234 f->b_handler = false;
241 f->file = strdup (dir);
243 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? true : false;
247 msg_Err( p_intf , "unable to parse directory" );
250 return( VLC_ENOMEM );
252 msg_Dbg( p_intf, "file=%s (url=%s)",
257 char *psz_type = strdup( "text/html; charset=UTF-8" );
258 if( strstr( &dir[strlen( psz_root )], ".xml" ) )
260 char *psz = strstr( psz_type, "html;" );
270 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
272 f->b_html ? psz_type : NULL,
273 user, password, p_acl,
276 if( f->p_file != NULL )
278 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
283 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
285 user, password, p_acl,
286 HandlerCallback, h );
287 if( h->p_handler != NULL )
289 TAB_APPEND( p_sys->i_files, p_sys->pp_files,
290 (httpd_file_sys_t *)h );
294 /* for url that ends by / add
295 * - a redirect from rep to rep/
296 * - in case of index.* rep/index.html to rep/ */
297 if( f && f->name[strlen(f->name) - 1] == '/' )
299 char *psz_redir = strdup( f->name );
301 psz_redir[strlen( psz_redir ) - 1] = '\0';
303 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
304 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
307 if( b_index && ( p = strstr( f->file, "index." ) ) )
309 if( asprintf( &psz_redir, "%s%s", f->name, p ) != -1 )
311 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
312 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
313 f->name, psz_redir );
325 ACL_Destroy( p_acl );
332 /*************************************************************************
334 *************************************************************************/
335 void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl,
336 playlist_item_t *p_node, char *name, mvar_t *s,
339 if( !p_node || !p_node->p_input )
342 if( p_node->i_children == -1 )
346 playlist_item_t * p_item = playlist_CurrentPlayingItem( p_pl );
347 if( !p_item || !p_item->p_input )
350 mvar_t *itm = mvar_New( name, "set" );
351 if( p_item->p_input == p_node->p_input )
352 mvar_AppendNewVar( itm, "current", "1" );
354 mvar_AppendNewVar( itm, "current", "0" );
356 sprintf( value, "%d", p_node->i_id );
357 mvar_AppendNewVar( itm, "index", value );
359 psz = input_item_GetName( p_node->p_input );
360 mvar_AppendNewVar( itm, "name", psz );
363 psz = input_item_GetURI( p_node->p_input );
364 mvar_AppendNewVar( itm, "uri", psz );
367 sprintf( value, "Item");
368 mvar_AppendNewVar( itm, "type", value );
370 sprintf( value, "%d", i_depth );
371 mvar_AppendNewVar( itm, "depth", value );
373 if( p_node->i_flags & PLAYLIST_RO_FLAG )
374 mvar_AppendNewVar( itm, "ro", "ro" );
376 mvar_AppendNewVar( itm, "ro", "rw" );
378 sprintf( value, "%"PRId64, input_item_GetDuration( p_node->p_input ) );
379 mvar_AppendNewVar( itm, "duration", value );
381 //Adding extra meta-information to each playlist item
383 psz = input_item_GetTitle( p_node->p_input );
384 mvar_AppendNewVar( itm, "title", psz );
386 psz = input_item_GetArtist( p_node->p_input );
387 mvar_AppendNewVar( itm, "artist", psz );
389 psz = input_item_GetGenre( p_node->p_input );
390 mvar_AppendNewVar( itm, "genre", psz );
392 psz = input_item_GetCopyright( p_node->p_input );
393 mvar_AppendNewVar( itm, "copyright", psz );
395 psz = input_item_GetAlbum( p_node->p_input );
396 mvar_AppendNewVar( itm, "album", psz );
398 psz = input_item_GetTrackNum( p_node->p_input );
399 mvar_AppendNewVar( itm, "track", psz );
401 psz = input_item_GetDescription( p_node->p_input );
402 mvar_AppendNewVar( itm, "description", psz );
404 psz = input_item_GetRating( p_node->p_input );
405 mvar_AppendNewVar( itm, "rating", psz );
407 psz = input_item_GetDate( p_node->p_input );
408 mvar_AppendNewVar( itm, "date", psz );
410 psz = input_item_GetURL( p_node->p_input );
411 mvar_AppendNewVar( itm, "url", psz );
413 psz = input_item_GetLanguage( p_node->p_input );
414 mvar_AppendNewVar( itm, "language", psz );
416 psz = input_item_GetNowPlaying( p_node->p_input );
417 mvar_AppendNewVar( itm, "now_playing", psz );
419 psz = input_item_GetPublisher( p_node->p_input );
420 mvar_AppendNewVar( itm, "publisher", psz );
422 psz = input_item_GetEncodedBy( p_node->p_input );
423 mvar_AppendNewVar( itm, "encoded_by", psz );
425 psz = input_item_GetArtURL( p_node->p_input );
426 mvar_AppendNewVar( itm, "art_url", psz );
428 psz = input_item_GetTrackID( p_node->p_input );
429 mvar_AppendNewVar( itm, "track_id", psz );
431 mvar_AppendVar( s, itm );
437 mvar_t *itm = mvar_New( name, "set" );
439 mvar_AppendNewVar( itm, "name", p_node->p_input->psz_name );
440 mvar_AppendNewVar( itm, "uri", p_node->p_input->psz_name );
442 sprintf( value, "Node" );
443 mvar_AppendNewVar( itm, "type", value );
445 sprintf( value, "%d", p_node->i_id );
446 mvar_AppendNewVar( itm, "index", value );
448 sprintf( value, "%d", p_node->i_children);
449 mvar_AppendNewVar( itm, "i_children", value );
451 sprintf( value, "%d", i_depth );
452 mvar_AppendNewVar( itm, "depth", value );
454 if( p_node->i_flags & PLAYLIST_RO_FLAG )
455 mvar_AppendNewVar( itm, "ro", "ro" );
457 mvar_AppendNewVar( itm, "ro", "rw" );
459 mvar_AppendVar( s, itm );
461 for( i_child = 0 ; i_child < p_node->i_children ; i_child++ )
462 PlaylistListNode( p_intf, p_pl, p_node->pp_children[i_child],
463 name, s, i_depth + 1);
467 /****************************************************************************
468 * Seek command parsing handling
469 ****************************************************************************/
470 void HandleSeek( intf_thread_t *p_intf, char *p_value )
472 intf_sys_t *p_sys = p_intf->p_sys;
478 #define POSITION_ABSOLUTE 12
479 #define POSITION_REL_FOR 13
480 #define POSITION_REL_BACK 11
481 #define VL_TIME_ABSOLUTE 0
482 #define VL_TIME_REL_FOR 1
483 #define VL_TIME_REL_BACK -1
486 var_Get( p_sys->p_input, "length", &val );
487 i_length = val.i_time;
489 while( p_value[0] != '\0' )
495 i_relative = VL_TIME_REL_FOR;
501 i_relative = VL_TIME_REL_BACK;
505 case '0': case '1': case '2': case '3': case '4':
506 case '5': case '6': case '7': case '8': case '9':
508 i_stock = strtol( p_value , &p_value , 10 );
511 case '%': /* for percentage ie position */
513 i_relative += POSITION_ABSOLUTE;
521 i_value = 60 * (i_value + i_stock) ;
526 case 'h': case 'H': /* hours */
528 i_value += 3600 * i_stock;
530 /* other characters which are not numbers are not
532 while( ((p_value[0] < '0') || (p_value[0] > '9'))
533 && (p_value[0] != '\0') )
539 case 'm': case 'M': case '\'': /* minutes */
541 i_value += 60 * i_stock;
544 while( ((p_value[0] < '0') || (p_value[0] > '9'))
545 && (p_value[0] != '\0') )
551 case 's': case 'S': case '"': /* seconds */
555 while( ((p_value[0] < '0') || (p_value[0] > '9'))
556 && (p_value[0] != '\0') )
570 /* if there is no known symbol, I consider it as seconds.
571 * Otherwise, i_stock = 0 */
576 case VL_TIME_ABSOLUTE:
578 if( (uint64_t)( i_value ) * 1000000 <= i_length )
579 val.i_time = (uint64_t)( i_value ) * 1000000;
581 val.i_time = i_length;
583 var_Set( p_sys->p_input, "time", val );
584 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
587 case VL_TIME_REL_FOR:
589 var_Get( p_sys->p_input, "time", &val );
590 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
592 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
595 val.i_time = i_length;
597 var_Set( p_sys->p_input, "time", val );
598 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
601 case VL_TIME_REL_BACK:
603 var_Get( p_sys->p_input, "time", &val );
604 if( (int64_t)( i_value ) * 1000000 > val.i_time )
609 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
611 var_Set( p_sys->p_input, "time", val );
612 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
615 case POSITION_ABSOLUTE:
617 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
619 var_Set( p_sys->p_input, "position", val );
620 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
623 case POSITION_REL_FOR:
625 var_Get( p_sys->p_input, "position", &val );
626 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
628 var_Set( p_sys->p_input, "position", val );
629 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
633 case POSITION_REL_BACK:
635 var_Get( p_sys->p_input, "position", &val );
636 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
638 var_Set( p_sys->p_input, "position", val );
639 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
645 msg_Dbg( p_intf, "invalid seek request" );
650 #undef POSITION_ABSOLUTE
651 #undef POSITION_REL_FOR
652 #undef POSITION_REL_BACK
653 #undef VL_TIME_ABSOLUTE
654 #undef VL_TIME_REL_FOR
655 #undef VL_TIME_REL_BACK
659 /****************************************************************************
660 * URI Parsing functions
661 ****************************************************************************/
662 int TestURIParam( char *psz_uri, const char *psz_name )
666 while( (p = strstr( p, psz_name )) )
668 /* Verify that we are dealing with a post/get argument */
669 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
670 && p[strlen(psz_name)] == '=' )
680 static const char *FindURIValue( const char *psz_uri, const char *restrict psz_name,
681 size_t *restrict p_len )
683 const char *p = psz_uri, *end;
686 while( (p = strstr( p, psz_name )) )
688 /* Verify that we are dealing with a post/get argument */
689 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
690 && p[strlen(psz_name)] == '=' )
701 p += strlen( psz_name );
704 if( ( end = strchr( p, '\n' ) ) != NULL )
707 if( ( end > p ) && ( end[-1] == '\r' ) )
715 if( ( end = strchr( p, '&' ) ) != NULL )
725 const char *ExtractURIValue( const char *restrict psz_uri,
726 const char *restrict psz_name,
727 char *restrict psz_buf, size_t bufsize )
730 const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
731 const char *psz_next;
733 if( psz_value == NULL )
740 psz_next = psz_value + len;
746 strncpy( psz_buf, psz_value, len );
753 char *ExtractURIString( const char *restrict psz_uri,
754 const char *restrict psz_name )
757 const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
759 if( psz_value == NULL )
762 char *res = malloc( len + 1 );
766 memcpy( res, psz_value, len );
772 /* Since the resulting string is smaller we can work in place, so it is
773 * permitted to have psz == new. new points to the first word of the
774 * string, the function returns the remaining string. */
775 char *FirstWord( char *psz, char *new )
782 while( *psz != '\0' && *psz != ' ' )
787 while( *psz != '\0' && *psz != c )
789 if( *psz == '\\' && psz[1] != '\0' )
798 if( *psz == '\\' && psz[1] != '\0' )
812 /**********************************************************************
813 * MRLParse: parse the MRL, find the MRL string and the options,
814 * create an item with all information in it, and return the item.
815 * return NULL if there is an error.
816 **********************************************************************/
818 /* Function analog to FirstWord except that it relies on colon instead
819 * of space to delimit option boundaries. */
820 static char *FirstOption( char *psz, char *new )
822 bool b_end, b_start = true;
827 while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
832 while( *psz != '\0' && *psz != c )
834 if( *psz == '\\' && psz[1] != '\0' )
844 if( *psz == '\\' && psz[1] != '\0' )
853 while (new[-1] == ' ')
863 input_item_t *MRLParse( intf_thread_t *p_intf, const char *mrl,
866 char *psz = strdup( mrl ), *s_mrl = psz, *s_temp;
869 /* extract the mrl */
870 s_temp = FirstOption( s_mrl, s_mrl );
873 s_temp = s_mrl + strlen( s_mrl );
876 input_item_t *p_input = input_item_New( p_intf, s_mrl, psz_name );
877 if( p_input == NULL )
881 /* now we can take care of the options */
882 while ( *s_mrl != '\0' )
884 s_temp = FirstOption( s_mrl, s_mrl );
889 s_temp = s_mrl + strlen( s_mrl );
891 input_item_AddOption( p_input, s_mrl, VLC_INPUT_OPTION_TRUSTED );
898 /**********************************************************************
899 * RealPath: parse ../, ~ and path stuff
900 **********************************************************************/
901 char *RealPath( const char *psz_src )
905 int i_len = strlen(psz_src);
907 psz_dir = malloc( i_len + 2 );
908 strcpy( psz_dir, psz_src );
910 /* Add a trailing sep to ease the .. step */
911 psz_dir[i_len] = DIR_SEP_CHAR;
912 psz_dir[i_len + 1] = '\0';
914 #if (DIR_SEP_CHAR != '/')
915 /* Convert all / to native separator */
917 while( (p = strchr( p, '/' )) != NULL )
923 /* FIXME: this could be O(N) rather than O(N²)... */
924 /* Remove multiple separators and /./ */
926 while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
928 if( p[1] == DIR_SEP_CHAR )
929 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
930 else if( p[1] == '.' && p[2] == DIR_SEP_CHAR )
931 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
936 if( psz_dir[0] == '~' )
939 asprintf( &dir, "%s%s", config_GetHomeDir(), psz_dir + 1 );
944 if( strlen(psz_dir) > 2 )
948 while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
950 if( p[-1] == '.' && p[-2] == '.' && p[-3] == DIR_SEP_CHAR )
954 if( (q = strrchr( psz_dir, DIR_SEP_CHAR )) != NULL )
956 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
961 memmove( psz_dir, p, strlen(p) + 1 );
970 /* Remove trailing sep if there are at least 2 sep in the string
971 * (handles the C:\ stuff) */
972 p = strrchr( psz_dir, DIR_SEP_CHAR );
973 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, DIR_SEP_CHAR ) )