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>
38 /****************************************************************************
39 * File and directory functions
40 ****************************************************************************/
42 /* ToUrl: create a good name for an url from filename */
43 char *FileToUrl( char *name, bool *pb_index )
47 url = p = malloc( strlen( name ) + 1 );
56 while( *name == '\\' || *name == '/' )
68 /* convert '\\' into '/' */
79 if( ( p = strrchr( url, '/' ) ) != NULL )
81 if( !strncmp( p, "/index.", 7 ) )
91 int FileLoad( FILE *f, char **pp_data, int *pi_data )
95 /* just load the file */
97 *pp_data = xmalloc( 1025 ); /* +1 for \0 */
99 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
102 *pp_data = xrealloc( *pp_data, *pi_data + 1025 );
108 (*pp_data)[*pi_data] = '\0';
113 /* Parse a directory and recursively add files */
114 int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
117 intf_sys_t *p_sys = p_intf->p_sys;
118 char dir[MAX_DIR_SIZE];
124 char *password = NULL;
128 if( ( p_dir = vlc_opendir( psz_dir ) ) == NULL )
130 if( errno != ENOENT && errno != ENOTDIR )
131 msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
135 i_dirlen = strlen( psz_dir );
136 if( i_dirlen + 10 > MAX_DIR_SIZE )
138 msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
143 msg_Dbg( p_intf, "dir=%s", psz_dir );
145 snprintf( dir, sizeof( dir ), "%s"DIR_SEP".access", psz_dir );
146 if( ( file = vlc_fopen( dir, "r" ) ) != NULL )
151 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
153 i_size = fread( line, 1, 1023, file );
157 while( i_size > 0 && ( line[i_size-1] == '\n' ||
158 line[i_size-1] == '\r' ) )
165 p = strchr( line, ':' );
169 user = strdup( line );
170 password = strdup( p );
173 msg_Dbg( p_intf, "using user=%s (read=%d)", user, i_size );
178 snprintf( dir, sizeof( dir ), "%s"DIR_SEP".hosts", psz_dir );
179 p_acl = ACL_Create( p_intf, false );
180 if( ACL_LoadFile( p_acl, dir ) )
182 ACL_Destroy( p_acl );
185 if( vlc_stat( dir, &st ) == 0 )
198 /* parse psz_src dir */
199 if( ( psz_filename = vlc_readdir( p_dir ) ) == NULL )
204 if( ( psz_filename[0] == '.' )
205 || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
207 free( psz_filename );
211 snprintf( dir, sizeof( dir ), "%s"DIR_SEP"%s", psz_dir, psz_filename );
212 free( psz_filename );
214 if( ParseDirectory( p_intf, psz_root, dir ) )
216 httpd_file_sys_t *f = NULL;
217 httpd_handler_sys_t *h = NULL;
219 char *psz_name, *psz_ext;
221 psz_name = FileToUrl( &dir[strlen( psz_root )], &b_index );
222 psz_ext = strrchr( dir, '.' );
223 if( psz_ext != NULL )
227 for( i = 0; i < p_sys->i_handlers; i++ )
228 if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
230 if( i < p_sys->i_handlers )
232 f = malloc( sizeof( httpd_handler_sys_t ) );
233 h = (httpd_handler_sys_t *)f;
235 h->p_association = p_sys->pp_handlers[i];
240 f = xmalloc( sizeof( httpd_file_sys_t ) );
241 f->b_handler = false;
248 f->file = strdup (dir);
250 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? true : false;
254 msg_Err( p_intf , "unable to parse directory" );
257 return( VLC_ENOMEM );
259 msg_Dbg( p_intf, "file=%s (url=%s)",
264 char *psz_type = strdup( "text/html; charset=UTF-8" );
265 if( strstr( &dir[strlen( psz_root )], ".xml" ) )
267 char *psz = strstr( psz_type, "html;" );
277 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
279 f->b_html ? psz_type : NULL,
280 user, password, p_acl,
283 if( f->p_file != NULL )
285 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
290 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
292 user, password, p_acl,
293 HandlerCallback, h );
294 if( h->p_handler != NULL )
296 TAB_APPEND( p_sys->i_files, p_sys->pp_files,
297 (httpd_file_sys_t *)h );
301 /* for url that ends by / add
302 * - a redirect from rep to rep/
303 * - in case of index.* rep/index.html to rep/ */
304 if( f && f->name[strlen(f->name) - 1] == '/' )
306 char *psz_redir = strdup( f->name );
308 psz_redir[strlen( psz_redir ) - 1] = '\0';
310 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
311 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
314 if( b_index && ( p = strstr( f->file, "index." ) ) )
316 if( asprintf( &psz_redir, "%s%s", f->name, p ) != -1 )
318 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
319 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
320 f->name, psz_redir );
332 ACL_Destroy( p_acl );
339 /*************************************************************************
341 *************************************************************************/
342 void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl,
343 playlist_item_t *p_node, char *name, mvar_t *s,
346 if( !p_node || !p_node->p_input )
349 if( p_node->i_children == -1 )
353 playlist_item_t * p_item = playlist_CurrentPlayingItem( p_pl );
354 if( !p_item || !p_item->p_input )
357 mvar_t *itm = mvar_New( name, "set" );
358 if( p_item->p_input == p_node->p_input )
359 mvar_AppendNewVar( itm, "current", "1" );
361 mvar_AppendNewVar( itm, "current", "0" );
363 sprintf( value, "%d", p_node->i_id );
364 mvar_AppendNewVar( itm, "index", value );
366 psz = input_item_GetName( p_node->p_input );
367 mvar_AppendNewVar( itm, "name", psz );
370 psz = input_item_GetURI( p_node->p_input );
371 mvar_AppendNewVar( itm, "uri", psz );
374 mvar_AppendNewVar( itm, "type", "Item" );
376 sprintf( value, "%d", i_depth );
377 mvar_AppendNewVar( itm, "depth", value );
379 if( p_node->i_flags & PLAYLIST_RO_FLAG )
380 mvar_AppendNewVar( itm, "ro", "ro" );
382 mvar_AppendNewVar( itm, "ro", "rw" );
384 sprintf( value, "%"PRId64, input_item_GetDuration( p_node->p_input ) );
385 mvar_AppendNewVar( itm, "duration", value );
387 //Adding extra meta-information to each playlist item
389 psz = input_item_GetTitle( p_node->p_input );
390 mvar_AppendNewVar( itm, "title", psz );
393 psz = input_item_GetArtist( p_node->p_input );
394 mvar_AppendNewVar( itm, "artist", psz );
397 psz = input_item_GetGenre( p_node->p_input );
398 mvar_AppendNewVar( itm, "genre", psz );
401 psz = input_item_GetCopyright( p_node->p_input );
402 mvar_AppendNewVar( itm, "copyright", psz );
405 psz = input_item_GetAlbum( p_node->p_input );
406 mvar_AppendNewVar( itm, "album", psz );
409 psz = input_item_GetTrackNum( p_node->p_input );
410 mvar_AppendNewVar( itm, "track", psz );
413 psz = input_item_GetDescription( p_node->p_input );
414 mvar_AppendNewVar( itm, "description", psz );
417 psz = input_item_GetRating( p_node->p_input );
418 mvar_AppendNewVar( itm, "rating", psz );
421 psz = input_item_GetDate( p_node->p_input );
422 mvar_AppendNewVar( itm, "date", psz );
425 psz = input_item_GetURL( p_node->p_input );
426 mvar_AppendNewVar( itm, "url", psz );
429 psz = input_item_GetLanguage( p_node->p_input );
430 mvar_AppendNewVar( itm, "language", psz );
433 psz = input_item_GetNowPlaying( p_node->p_input );
434 mvar_AppendNewVar( itm, "now_playing", psz );
437 psz = input_item_GetPublisher( p_node->p_input );
438 mvar_AppendNewVar( itm, "publisher", psz );
441 psz = input_item_GetEncodedBy( p_node->p_input );
442 mvar_AppendNewVar( itm, "encoded_by", psz );
445 psz = input_item_GetArtURL( p_node->p_input );
446 mvar_AppendNewVar( itm, "art_url", psz );
449 psz = input_item_GetTrackID( p_node->p_input );
450 mvar_AppendNewVar( itm, "track_id", psz );
453 mvar_AppendVar( s, itm );
459 mvar_t *itm = mvar_New( name, "set" );
461 mvar_AppendNewVar( itm, "name", p_node->p_input->psz_name );
462 mvar_AppendNewVar( itm, "uri", p_node->p_input->psz_name );
464 mvar_AppendNewVar( itm, "type", "Node" );
466 sprintf( value, "%d", p_node->i_id );
467 mvar_AppendNewVar( itm, "index", value );
469 sprintf( value, "%d", p_node->i_children);
470 mvar_AppendNewVar( itm, "i_children", value );
472 sprintf( value, "%d", i_depth );
473 mvar_AppendNewVar( itm, "depth", value );
475 if( p_node->i_flags & PLAYLIST_RO_FLAG )
476 mvar_AppendNewVar( itm, "ro", "ro" );
478 mvar_AppendNewVar( itm, "ro", "rw" );
480 mvar_AppendVar( s, itm );
482 for( i_child = 0 ; i_child < p_node->i_children ; i_child++ )
483 PlaylistListNode( p_intf, p_pl, p_node->pp_children[i_child],
484 name, s, i_depth + 1);
488 /****************************************************************************
489 * Seek command parsing handling
490 ****************************************************************************/
491 void HandleSeek( intf_thread_t *p_intf, char *p_value )
493 intf_sys_t *p_sys = p_intf->p_sys;
499 #define POSITION_ABSOLUTE 12
500 #define POSITION_REL_FOR 13
501 #define POSITION_REL_BACK 11
502 #define VL_TIME_ABSOLUTE 0
503 #define VL_TIME_REL_FOR 1
504 #define VL_TIME_REL_BACK -1
507 var_Get( p_sys->p_input, "length", &val );
508 i_length = val.i_time;
510 while( p_value[0] != '\0' )
516 i_relative = VL_TIME_REL_FOR;
522 i_relative = VL_TIME_REL_BACK;
526 case '0': case '1': case '2': case '3': case '4':
527 case '5': case '6': case '7': case '8': case '9':
529 i_stock = strtol( p_value , &p_value , 10 );
532 case '%': /* for percentage ie position */
534 i_relative += POSITION_ABSOLUTE;
542 i_value = 60 * (i_value + i_stock) ;
547 case 'h': case 'H': /* hours */
549 i_value += 3600 * i_stock;
551 /* other characters which are not numbers are not
553 while( ((p_value[0] < '0') || (p_value[0] > '9'))
554 && (p_value[0] != '\0') )
560 case 'm': case 'M': case '\'': /* minutes */
562 i_value += 60 * i_stock;
565 while( ((p_value[0] < '0') || (p_value[0] > '9'))
566 && (p_value[0] != '\0') )
572 case 's': case 'S': case '"': /* seconds */
576 while( ((p_value[0] < '0') || (p_value[0] > '9'))
577 && (p_value[0] != '\0') )
591 /* if there is no known symbol, I consider it as seconds.
592 * Otherwise, i_stock = 0 */
597 case VL_TIME_ABSOLUTE:
599 if( (uint64_t)( i_value ) * 1000000 <= i_length )
600 val.i_time = (uint64_t)( i_value ) * 1000000;
602 val.i_time = i_length;
604 var_Set( p_sys->p_input, "time", val );
605 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
608 case VL_TIME_REL_FOR:
610 var_Get( p_sys->p_input, "time", &val );
611 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
613 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
616 val.i_time = i_length;
618 var_Set( p_sys->p_input, "time", val );
619 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
622 case VL_TIME_REL_BACK:
624 var_Get( p_sys->p_input, "time", &val );
625 if( (int64_t)( i_value ) * 1000000 > val.i_time )
630 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
632 var_Set( p_sys->p_input, "time", val );
633 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
636 case POSITION_ABSOLUTE:
638 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
640 var_Set( p_sys->p_input, "position", val );
641 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
644 case POSITION_REL_FOR:
646 var_Get( p_sys->p_input, "position", &val );
647 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
649 var_Set( p_sys->p_input, "position", val );
650 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
654 case POSITION_REL_BACK:
656 var_Get( p_sys->p_input, "position", &val );
657 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
659 var_Set( p_sys->p_input, "position", val );
660 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
666 msg_Dbg( p_intf, "invalid seek request" );
671 #undef POSITION_ABSOLUTE
672 #undef POSITION_REL_FOR
673 #undef POSITION_REL_BACK
674 #undef VL_TIME_ABSOLUTE
675 #undef VL_TIME_REL_FOR
676 #undef VL_TIME_REL_BACK
680 /****************************************************************************
681 * URI Parsing functions
682 ****************************************************************************/
683 int TestURIParam( char *psz_uri, const char *psz_name )
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)] == '=' )
701 static const char *FindURIValue( const char *psz_uri, const char *restrict psz_name,
702 size_t *restrict p_len )
704 const char *p = psz_uri, *end;
707 while( (p = strstr( p, psz_name )) )
709 /* Verify that we are dealing with a post/get argument */
710 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
711 && p[strlen(psz_name)] == '=' )
722 p += strlen( psz_name );
725 if( ( end = strchr( p, '\n' ) ) != NULL )
728 if( ( end > p ) && ( end[-1] == '\r' ) )
736 if( ( end = strchr( p, '&' ) ) != NULL )
746 const char *ExtractURIValue( const char *restrict psz_uri,
747 const char *restrict psz_name,
748 char *restrict psz_buf, size_t bufsize )
751 const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
752 const char *psz_next;
754 if( psz_value == NULL )
761 psz_next = psz_value + len;
767 strncpy( psz_buf, psz_value, len );
774 char *ExtractURIString( const char *restrict psz_uri,
775 const char *restrict psz_name )
778 const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
780 if( psz_value == NULL )
783 char *res = malloc( len + 1 );
787 memcpy( res, psz_value, len );
793 /* Since the resulting string is smaller we can work in place, so it is
794 * permitted to have psz == new. new points to the first word of the
795 * string, the function returns the remaining string. */
796 char *FirstWord( char *psz, char *new )
803 while( *psz != '\0' && *psz != ' ' )
808 while( *psz != '\0' && *psz != c )
810 if( *psz == '\\' && psz[1] != '\0' )
819 if( *psz == '\\' && psz[1] != '\0' )
833 /**********************************************************************
834 * MRLParse: parse the MRL, find the MRL string and the options,
835 * create an item with all information in it, and return the item.
836 * return NULL if there is an error.
837 **********************************************************************/
839 /* Function analog to FirstWord except that it relies on colon instead
840 * of space to delimit option boundaries. */
841 static char *FirstOption( char *psz, char *new )
843 bool b_end, b_start = true;
848 while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
853 while( *psz != '\0' && *psz != c )
855 if( *psz == '\\' && psz[1] != '\0' )
865 if( *psz == '\\' && psz[1] != '\0' )
874 while (new[-1] == ' ')
884 input_item_t *MRLParse( intf_thread_t *p_intf, const char *mrl,
887 char *psz = strdup( mrl ), *s_mrl = psz, *s_temp;
890 /* extract the mrl */
891 s_temp = FirstOption( s_mrl, s_mrl );
894 s_temp = s_mrl + strlen( s_mrl );
897 input_item_t *p_input = input_item_New( p_intf, s_mrl, psz_name );
898 if( p_input == NULL )
902 /* now we can take care of the options */
903 while ( *s_mrl != '\0' )
905 s_temp = FirstOption( s_mrl, s_mrl );
910 s_temp = s_mrl + strlen( s_mrl );
912 input_item_AddOption( p_input, s_mrl, VLC_INPUT_OPTION_TRUSTED );
919 /**********************************************************************
920 * RealPath: parse ../, ~ and path stuff
921 **********************************************************************/
922 char *RealPath( const char *psz_src )
926 int i_len = strlen(psz_src);
928 psz_dir = xmalloc( i_len + 2 );
929 strcpy( psz_dir, psz_src );
931 /* Add a trailing sep to ease the .. step */
932 psz_dir[i_len] = DIR_SEP_CHAR;
933 psz_dir[i_len + 1] = '\0';
935 #if (DIR_SEP_CHAR != '/')
936 /* Convert all / to native separator */
938 while( (p = strchr( p, '/' )) != NULL )
944 /* FIXME: this could be O(N) rather than O(N²)... */
945 /* Remove multiple separators and /./ */
947 while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
949 if( p[1] == DIR_SEP_CHAR )
950 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
951 else if( p[1] == '.' && p[2] == DIR_SEP_CHAR )
952 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
957 if( psz_dir[0] == '~' )
959 char *home = config_GetUserDir( VLC_HOME_DIR ), *dir;
960 if( asprintf( &dir, "%s%s", home, psz_dir + 1 ) != -1 )
968 if( strlen(psz_dir) > 2 )
972 while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
974 if( p[-1] == '.' && p[-2] == '.' && p[-3] == DIR_SEP_CHAR )
978 if( (q = strrchr( psz_dir, DIR_SEP_CHAR )) != NULL )
980 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
985 memmove( psz_dir, p, strlen(p) + 1 );
994 /* Remove trailing sep if there are at least 2 sep in the string
995 * (handles the C:\ stuff) */
996 p = strrchr( psz_dir, DIR_SEP_CHAR );
997 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, DIR_SEP_CHAR ) )