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>
36 /****************************************************************************
37 * File and directory functions
38 ****************************************************************************/
40 /* ToUrl: create a good name for an url from filename */
41 char *FileToUrl( char *name, bool *pb_index )
45 url = p = malloc( strlen( name ) + 1 );
54 while( *name == '\\' || *name == '/' )
66 /* convert '\\' into '/' */
77 if( ( p = strrchr( url, '/' ) ) != NULL )
79 if( !strncmp( p, "/index.", 7 ) )
89 int FileLoad( FILE *f, char **pp_data, int *pi_data )
93 /* just load the file */
95 *pp_data = xmalloc( 1025 ); /* +1 for \0 */
97 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
100 *pp_data = xrealloc( *pp_data, *pi_data + 1025 );
106 (*pp_data)[*pi_data] = '\0';
111 /* Parse a directory and recursively add files */
112 int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
115 intf_sys_t *p_sys = p_intf->p_sys;
116 char dir[MAX_DIR_SIZE];
122 char *password = NULL;
126 if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
128 if( errno != ENOENT && errno != ENOTDIR )
129 msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
133 i_dirlen = strlen( psz_dir );
134 if( i_dirlen + 10 > MAX_DIR_SIZE )
136 msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
141 msg_Dbg( p_intf, "dir=%s", psz_dir );
143 snprintf( dir, sizeof( dir ), "%s"DIR_SEP".access", psz_dir );
144 if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
149 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
151 i_size = fread( line, 1, 1023, file );
155 while( i_size > 0 && ( line[i_size-1] == '\n' ||
156 line[i_size-1] == '\r' ) )
163 p = strchr( line, ':' );
167 user = strdup( line );
168 password = strdup( p );
171 msg_Dbg( p_intf, "using user=%s (read=%d)", user, i_size );
176 snprintf( dir, sizeof( dir ), "%s"DIR_SEP".hosts", psz_dir );
177 p_acl = ACL_Create( p_intf, false );
178 if( ACL_LoadFile( p_acl, dir ) )
180 ACL_Destroy( p_acl );
183 if( utf8_stat( dir, &st ) == 0 )
196 /* parse psz_src dir */
197 if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
202 if( ( psz_filename[0] == '.' )
203 || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
205 free( psz_filename );
209 snprintf( dir, sizeof( dir ), "%s"DIR_SEP"%s", psz_dir, psz_filename );
210 free( psz_filename );
212 if( ParseDirectory( p_intf, psz_root, dir ) )
214 httpd_file_sys_t *f = NULL;
215 httpd_handler_sys_t *h = NULL;
217 char *psz_name, *psz_ext;
219 psz_name = FileToUrl( &dir[strlen( psz_root )], &b_index );
220 psz_ext = strrchr( dir, '.' );
221 if( psz_ext != NULL )
225 for( i = 0; i < p_sys->i_handlers; i++ )
226 if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
228 if( i < p_sys->i_handlers )
230 f = malloc( sizeof( httpd_handler_sys_t ) );
231 h = (httpd_handler_sys_t *)f;
233 h->p_association = p_sys->pp_handlers[i];
238 f = xmalloc( sizeof( httpd_file_sys_t ) );
239 f->b_handler = false;
246 f->file = strdup (dir);
248 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? true : false;
252 msg_Err( p_intf , "unable to parse directory" );
255 return( VLC_ENOMEM );
257 msg_Dbg( p_intf, "file=%s (url=%s)",
262 char *psz_type = strdup( "text/html; charset=UTF-8" );
263 if( strstr( &dir[strlen( psz_root )], ".xml" ) )
265 char *psz = strstr( psz_type, "html;" );
275 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
277 f->b_html ? psz_type : NULL,
278 user, password, p_acl,
281 if( f->p_file != NULL )
283 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
288 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
290 user, password, p_acl,
291 HandlerCallback, h );
292 if( h->p_handler != NULL )
294 TAB_APPEND( p_sys->i_files, p_sys->pp_files,
295 (httpd_file_sys_t *)h );
299 /* for url that ends by / add
300 * - a redirect from rep to rep/
301 * - in case of index.* rep/index.html to rep/ */
302 if( f && f->name[strlen(f->name) - 1] == '/' )
304 char *psz_redir = strdup( f->name );
306 psz_redir[strlen( psz_redir ) - 1] = '\0';
308 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
309 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
312 if( b_index && ( p = strstr( f->file, "index." ) ) )
314 if( asprintf( &psz_redir, "%s%s", f->name, p ) != -1 )
316 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
317 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
318 f->name, psz_redir );
330 ACL_Destroy( p_acl );
337 /*************************************************************************
339 *************************************************************************/
340 void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl,
341 playlist_item_t *p_node, char *name, mvar_t *s,
344 if( !p_node || !p_node->p_input )
347 if( p_node->i_children == -1 )
351 playlist_item_t * p_item = playlist_CurrentPlayingItem( p_pl );
352 if( !p_item || !p_item->p_input )
355 mvar_t *itm = mvar_New( name, "set" );
356 if( p_item->p_input == p_node->p_input )
357 mvar_AppendNewVar( itm, "current", "1" );
359 mvar_AppendNewVar( itm, "current", "0" );
361 sprintf( value, "%d", p_node->i_id );
362 mvar_AppendNewVar( itm, "index", value );
364 psz = input_item_GetName( p_node->p_input );
365 mvar_AppendNewVar( itm, "name", psz );
368 psz = input_item_GetURI( p_node->p_input );
369 mvar_AppendNewVar( itm, "uri", psz );
372 mvar_AppendNewVar( itm, "type", "Item" );
374 sprintf( value, "%d", i_depth );
375 mvar_AppendNewVar( itm, "depth", value );
377 if( p_node->i_flags & PLAYLIST_RO_FLAG )
378 mvar_AppendNewVar( itm, "ro", "ro" );
380 mvar_AppendNewVar( itm, "ro", "rw" );
382 sprintf( value, "%"PRId64, input_item_GetDuration( p_node->p_input ) );
383 mvar_AppendNewVar( itm, "duration", value );
385 //Adding extra meta-information to each playlist item
387 psz = input_item_GetTitle( p_node->p_input );
388 mvar_AppendNewVar( itm, "title", psz );
391 psz = input_item_GetArtist( p_node->p_input );
392 mvar_AppendNewVar( itm, "artist", psz );
395 psz = input_item_GetGenre( p_node->p_input );
396 mvar_AppendNewVar( itm, "genre", psz );
399 psz = input_item_GetCopyright( p_node->p_input );
400 mvar_AppendNewVar( itm, "copyright", psz );
403 psz = input_item_GetAlbum( p_node->p_input );
404 mvar_AppendNewVar( itm, "album", psz );
407 psz = input_item_GetTrackNum( p_node->p_input );
408 mvar_AppendNewVar( itm, "track", psz );
411 psz = input_item_GetDescription( p_node->p_input );
412 mvar_AppendNewVar( itm, "description", psz );
415 psz = input_item_GetRating( p_node->p_input );
416 mvar_AppendNewVar( itm, "rating", psz );
419 psz = input_item_GetDate( p_node->p_input );
420 mvar_AppendNewVar( itm, "date", psz );
423 psz = input_item_GetURL( p_node->p_input );
424 mvar_AppendNewVar( itm, "url", psz );
427 psz = input_item_GetLanguage( p_node->p_input );
428 mvar_AppendNewVar( itm, "language", psz );
431 psz = input_item_GetNowPlaying( p_node->p_input );
432 mvar_AppendNewVar( itm, "now_playing", psz );
435 psz = input_item_GetPublisher( p_node->p_input );
436 mvar_AppendNewVar( itm, "publisher", psz );
439 psz = input_item_GetEncodedBy( p_node->p_input );
440 mvar_AppendNewVar( itm, "encoded_by", psz );
443 psz = input_item_GetArtURL( p_node->p_input );
444 mvar_AppendNewVar( itm, "art_url", psz );
447 psz = input_item_GetTrackID( p_node->p_input );
448 mvar_AppendNewVar( itm, "track_id", psz );
451 mvar_AppendVar( s, itm );
457 mvar_t *itm = mvar_New( name, "set" );
459 mvar_AppendNewVar( itm, "name", p_node->p_input->psz_name );
460 mvar_AppendNewVar( itm, "uri", p_node->p_input->psz_name );
462 mvar_AppendNewVar( itm, "type", "Node" );
464 sprintf( value, "%d", p_node->i_id );
465 mvar_AppendNewVar( itm, "index", value );
467 sprintf( value, "%d", p_node->i_children);
468 mvar_AppendNewVar( itm, "i_children", value );
470 sprintf( value, "%d", i_depth );
471 mvar_AppendNewVar( itm, "depth", value );
473 if( p_node->i_flags & PLAYLIST_RO_FLAG )
474 mvar_AppendNewVar( itm, "ro", "ro" );
476 mvar_AppendNewVar( itm, "ro", "rw" );
478 mvar_AppendVar( s, itm );
480 for( i_child = 0 ; i_child < p_node->i_children ; i_child++ )
481 PlaylistListNode( p_intf, p_pl, p_node->pp_children[i_child],
482 name, s, i_depth + 1);
486 /****************************************************************************
487 * Seek command parsing handling
488 ****************************************************************************/
489 void HandleSeek( intf_thread_t *p_intf, char *p_value )
491 intf_sys_t *p_sys = p_intf->p_sys;
497 #define POSITION_ABSOLUTE 12
498 #define POSITION_REL_FOR 13
499 #define POSITION_REL_BACK 11
500 #define VL_TIME_ABSOLUTE 0
501 #define VL_TIME_REL_FOR 1
502 #define VL_TIME_REL_BACK -1
505 var_Get( p_sys->p_input, "length", &val );
506 i_length = val.i_time;
508 while( p_value[0] != '\0' )
514 i_relative = VL_TIME_REL_FOR;
520 i_relative = VL_TIME_REL_BACK;
524 case '0': case '1': case '2': case '3': case '4':
525 case '5': case '6': case '7': case '8': case '9':
527 i_stock = strtol( p_value , &p_value , 10 );
530 case '%': /* for percentage ie position */
532 i_relative += POSITION_ABSOLUTE;
540 i_value = 60 * (i_value + i_stock) ;
545 case 'h': case 'H': /* hours */
547 i_value += 3600 * i_stock;
549 /* other characters which are not numbers are not
551 while( ((p_value[0] < '0') || (p_value[0] > '9'))
552 && (p_value[0] != '\0') )
558 case 'm': case 'M': case '\'': /* minutes */
560 i_value += 60 * i_stock;
563 while( ((p_value[0] < '0') || (p_value[0] > '9'))
564 && (p_value[0] != '\0') )
570 case 's': case 'S': case '"': /* seconds */
574 while( ((p_value[0] < '0') || (p_value[0] > '9'))
575 && (p_value[0] != '\0') )
589 /* if there is no known symbol, I consider it as seconds.
590 * Otherwise, i_stock = 0 */
595 case VL_TIME_ABSOLUTE:
597 if( (uint64_t)( i_value ) * 1000000 <= i_length )
598 val.i_time = (uint64_t)( i_value ) * 1000000;
600 val.i_time = i_length;
602 var_Set( p_sys->p_input, "time", val );
603 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
606 case VL_TIME_REL_FOR:
608 var_Get( p_sys->p_input, "time", &val );
609 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
611 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
614 val.i_time = i_length;
616 var_Set( p_sys->p_input, "time", val );
617 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
620 case VL_TIME_REL_BACK:
622 var_Get( p_sys->p_input, "time", &val );
623 if( (int64_t)( i_value ) * 1000000 > val.i_time )
628 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
630 var_Set( p_sys->p_input, "time", val );
631 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
634 case POSITION_ABSOLUTE:
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: %d%%", i_value );
642 case POSITION_REL_FOR:
644 var_Get( p_sys->p_input, "position", &val );
645 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
647 var_Set( p_sys->p_input, "position", val );
648 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
652 case POSITION_REL_BACK:
654 var_Get( p_sys->p_input, "position", &val );
655 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
657 var_Set( p_sys->p_input, "position", val );
658 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
664 msg_Dbg( p_intf, "invalid seek request" );
669 #undef POSITION_ABSOLUTE
670 #undef POSITION_REL_FOR
671 #undef POSITION_REL_BACK
672 #undef VL_TIME_ABSOLUTE
673 #undef VL_TIME_REL_FOR
674 #undef VL_TIME_REL_BACK
678 /****************************************************************************
679 * URI Parsing functions
680 ****************************************************************************/
681 int TestURIParam( char *psz_uri, const char *psz_name )
685 while( (p = strstr( p, psz_name )) )
687 /* Verify that we are dealing with a post/get argument */
688 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
689 && p[strlen(psz_name)] == '=' )
699 static const char *FindURIValue( const char *psz_uri, const char *restrict psz_name,
700 size_t *restrict p_len )
702 const char *p = psz_uri, *end;
705 while( (p = strstr( p, psz_name )) )
707 /* Verify that we are dealing with a post/get argument */
708 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
709 && p[strlen(psz_name)] == '=' )
720 p += strlen( psz_name );
723 if( ( end = strchr( p, '\n' ) ) != NULL )
726 if( ( end > p ) && ( end[-1] == '\r' ) )
734 if( ( end = strchr( p, '&' ) ) != NULL )
744 const char *ExtractURIValue( const char *restrict psz_uri,
745 const char *restrict psz_name,
746 char *restrict psz_buf, size_t bufsize )
749 const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
750 const char *psz_next;
752 if( psz_value == NULL )
759 psz_next = psz_value + len;
765 strncpy( psz_buf, psz_value, len );
772 char *ExtractURIString( const char *restrict psz_uri,
773 const char *restrict psz_name )
776 const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
778 if( psz_value == NULL )
781 char *res = malloc( len + 1 );
785 memcpy( res, psz_value, len );
791 /* Since the resulting string is smaller we can work in place, so it is
792 * permitted to have psz == new. new points to the first word of the
793 * string, the function returns the remaining string. */
794 char *FirstWord( char *psz, char *new )
801 while( *psz != '\0' && *psz != ' ' )
806 while( *psz != '\0' && *psz != c )
808 if( *psz == '\\' && psz[1] != '\0' )
817 if( *psz == '\\' && psz[1] != '\0' )
831 /**********************************************************************
832 * MRLParse: parse the MRL, find the MRL string and the options,
833 * create an item with all information in it, and return the item.
834 * return NULL if there is an error.
835 **********************************************************************/
837 /* Function analog to FirstWord except that it relies on colon instead
838 * of space to delimit option boundaries. */
839 static char *FirstOption( char *psz, char *new )
841 bool b_end, b_start = true;
846 while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
851 while( *psz != '\0' && *psz != c )
853 if( *psz == '\\' && psz[1] != '\0' )
863 if( *psz == '\\' && psz[1] != '\0' )
872 while (new[-1] == ' ')
882 input_item_t *MRLParse( intf_thread_t *p_intf, const char *mrl,
885 char *psz = strdup( mrl ), *s_mrl = psz, *s_temp;
888 /* extract the mrl */
889 s_temp = FirstOption( s_mrl, s_mrl );
892 s_temp = s_mrl + strlen( s_mrl );
895 input_item_t *p_input = input_item_New( p_intf, s_mrl, psz_name );
896 if( p_input == NULL )
900 /* now we can take care of the options */
901 while ( *s_mrl != '\0' )
903 s_temp = FirstOption( s_mrl, s_mrl );
908 s_temp = s_mrl + strlen( s_mrl );
910 input_item_AddOption( p_input, s_mrl, VLC_INPUT_OPTION_TRUSTED );
917 /**********************************************************************
918 * RealPath: parse ../, ~ and path stuff
919 **********************************************************************/
920 char *RealPath( const char *psz_src )
924 int i_len = strlen(psz_src);
926 psz_dir = xmalloc( i_len + 2 );
927 strcpy( psz_dir, psz_src );
929 /* Add a trailing sep to ease the .. step */
930 psz_dir[i_len] = DIR_SEP_CHAR;
931 psz_dir[i_len + 1] = '\0';
933 #if (DIR_SEP_CHAR != '/')
934 /* Convert all / to native separator */
936 while( (p = strchr( p, '/' )) != NULL )
942 /* FIXME: this could be O(N) rather than O(N²)... */
943 /* Remove multiple separators and /./ */
945 while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
947 if( p[1] == DIR_SEP_CHAR )
948 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
949 else if( p[1] == '.' && p[2] == DIR_SEP_CHAR )
950 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
955 if( psz_dir[0] == '~' )
957 char *home = config_GetUserDir( VLC_HOME_DIR ), *dir;
958 if( asprintf( &dir, "%s%s", home, psz_dir + 1 ) != -1 )
966 if( strlen(psz_dir) > 2 )
970 while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
972 if( p[-1] == '.' && p[-2] == '.' && p[-3] == DIR_SEP_CHAR )
976 if( (q = strrchr( psz_dir, DIR_SEP_CHAR )) != NULL )
978 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
983 memmove( psz_dir, p, strlen(p) + 1 );
992 /* Remove trailing sep if there are at least 2 sep in the string
993 * (handles the C:\ stuff) */
994 p = strrchr( psz_dir, DIR_SEP_CHAR );
995 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, DIR_SEP_CHAR ) )