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 = xmalloc( 1025 ); /* +1 for \0 */
95 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
98 *pp_data = xrealloc( *pp_data, *pi_data + 1025 );
104 (*pp_data)[*pi_data] = '\0';
109 /* Parse a directory and recursively add files */
110 int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
113 intf_sys_t *p_sys = p_intf->p_sys;
114 char dir[MAX_DIR_SIZE];
120 char *password = NULL;
124 if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
126 if( errno != ENOENT && errno != ENOTDIR )
127 msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
131 i_dirlen = strlen( psz_dir );
132 if( i_dirlen + 10 > MAX_DIR_SIZE )
134 msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
139 msg_Dbg( p_intf, "dir=%s", psz_dir );
141 snprintf( dir, sizeof( dir ), "%s"DIR_SEP".access", psz_dir );
142 if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
147 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
149 i_size = fread( line, 1, 1023, file );
153 while( i_size > 0 && ( line[i_size-1] == '\n' ||
154 line[i_size-1] == '\r' ) )
161 p = strchr( line, ':' );
165 user = strdup( line );
166 password = strdup( p );
169 msg_Dbg( p_intf, "using user=%s (read=%d)", user, 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( utf8_stat( dir, &st ) == 0 )
194 /* parse psz_src dir */
195 if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
200 if( ( psz_filename[0] == '.' )
201 || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
203 free( psz_filename );
207 snprintf( dir, sizeof( dir ), "%s"DIR_SEP"%s", psz_dir, psz_filename );
208 free( psz_filename );
210 if( ParseDirectory( p_intf, psz_root, dir ) )
212 httpd_file_sys_t *f = NULL;
213 httpd_handler_sys_t *h = NULL;
215 char *psz_name, *psz_ext;
217 psz_name = FileToUrl( &dir[strlen( psz_root )], &b_index );
218 psz_ext = strrchr( dir, '.' );
219 if( psz_ext != NULL )
223 for( i = 0; i < p_sys->i_handlers; i++ )
224 if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
226 if( i < p_sys->i_handlers )
228 f = malloc( sizeof( httpd_handler_sys_t ) );
229 h = (httpd_handler_sys_t *)f;
231 h->p_association = p_sys->pp_handlers[i];
236 f = xmalloc( sizeof( httpd_file_sys_t ) );
237 f->b_handler = false;
244 f->file = strdup (dir);
246 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? true : false;
250 msg_Err( p_intf , "unable to parse directory" );
253 return( VLC_ENOMEM );
255 msg_Dbg( p_intf, "file=%s (url=%s)",
260 char *psz_type = strdup( "text/html; charset=UTF-8" );
261 if( strstr( &dir[strlen( psz_root )], ".xml" ) )
263 char *psz = strstr( psz_type, "html;" );
273 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
275 f->b_html ? psz_type : NULL,
276 user, password, p_acl,
279 if( f->p_file != NULL )
281 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
286 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
288 user, password, p_acl,
289 HandlerCallback, h );
290 if( h->p_handler != NULL )
292 TAB_APPEND( p_sys->i_files, p_sys->pp_files,
293 (httpd_file_sys_t *)h );
297 /* for url that ends by / add
298 * - a redirect from rep to rep/
299 * - in case of index.* rep/index.html to rep/ */
300 if( f && f->name[strlen(f->name) - 1] == '/' )
302 char *psz_redir = strdup( f->name );
304 psz_redir[strlen( psz_redir ) - 1] = '\0';
306 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
307 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
310 if( b_index && ( p = strstr( f->file, "index." ) ) )
312 if( asprintf( &psz_redir, "%s%s", f->name, p ) != -1 )
314 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
315 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
316 f->name, psz_redir );
328 ACL_Destroy( p_acl );
335 /*************************************************************************
337 *************************************************************************/
338 void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl,
339 playlist_item_t *p_node, char *name, mvar_t *s,
342 if( !p_node || !p_node->p_input )
345 if( p_node->i_children == -1 )
349 playlist_item_t * p_item = playlist_CurrentPlayingItem( p_pl );
350 if( !p_item || !p_item->p_input )
353 mvar_t *itm = mvar_New( name, "set" );
354 if( p_item->p_input == p_node->p_input )
355 mvar_AppendNewVar( itm, "current", "1" );
357 mvar_AppendNewVar( itm, "current", "0" );
359 sprintf( value, "%d", p_node->i_id );
360 mvar_AppendNewVar( itm, "index", value );
362 psz = input_item_GetName( p_node->p_input );
363 mvar_AppendNewVar( itm, "name", psz );
366 psz = input_item_GetURI( p_node->p_input );
367 mvar_AppendNewVar( itm, "uri", psz );
370 mvar_AppendNewVar( itm, "type", "Item" );
372 sprintf( value, "%d", i_depth );
373 mvar_AppendNewVar( itm, "depth", value );
375 if( p_node->i_flags & PLAYLIST_RO_FLAG )
376 mvar_AppendNewVar( itm, "ro", "ro" );
378 mvar_AppendNewVar( itm, "ro", "rw" );
380 sprintf( value, "%"PRId64, input_item_GetDuration( p_node->p_input ) );
381 mvar_AppendNewVar( itm, "duration", value );
383 //Adding extra meta-information to each playlist item
385 psz = input_item_GetTitle( p_node->p_input );
386 mvar_AppendNewVar( itm, "title", psz );
389 psz = input_item_GetArtist( p_node->p_input );
390 mvar_AppendNewVar( itm, "artist", psz );
393 psz = input_item_GetGenre( p_node->p_input );
394 mvar_AppendNewVar( itm, "genre", psz );
397 psz = input_item_GetCopyright( p_node->p_input );
398 mvar_AppendNewVar( itm, "copyright", psz );
401 psz = input_item_GetAlbum( p_node->p_input );
402 mvar_AppendNewVar( itm, "album", psz );
405 psz = input_item_GetTrackNum( p_node->p_input );
406 mvar_AppendNewVar( itm, "track", psz );
409 psz = input_item_GetDescription( p_node->p_input );
410 mvar_AppendNewVar( itm, "description", psz );
413 psz = input_item_GetRating( p_node->p_input );
414 mvar_AppendNewVar( itm, "rating", psz );
417 psz = input_item_GetDate( p_node->p_input );
418 mvar_AppendNewVar( itm, "date", psz );
421 psz = input_item_GetURL( p_node->p_input );
422 mvar_AppendNewVar( itm, "url", psz );
425 psz = input_item_GetLanguage( p_node->p_input );
426 mvar_AppendNewVar( itm, "language", psz );
429 psz = input_item_GetNowPlaying( p_node->p_input );
430 mvar_AppendNewVar( itm, "now_playing", psz );
433 psz = input_item_GetPublisher( p_node->p_input );
434 mvar_AppendNewVar( itm, "publisher", psz );
437 psz = input_item_GetEncodedBy( p_node->p_input );
438 mvar_AppendNewVar( itm, "encoded_by", psz );
441 psz = input_item_GetArtURL( p_node->p_input );
442 mvar_AppendNewVar( itm, "art_url", psz );
445 psz = input_item_GetTrackID( p_node->p_input );
446 mvar_AppendNewVar( itm, "track_id", psz );
449 mvar_AppendVar( s, itm );
455 mvar_t *itm = mvar_New( name, "set" );
457 mvar_AppendNewVar( itm, "name", p_node->p_input->psz_name );
458 mvar_AppendNewVar( itm, "uri", p_node->p_input->psz_name );
460 mvar_AppendNewVar( itm, "type", "Node" );
462 sprintf( value, "%d", p_node->i_id );
463 mvar_AppendNewVar( itm, "index", value );
465 sprintf( value, "%d", p_node->i_children);
466 mvar_AppendNewVar( itm, "i_children", value );
468 sprintf( value, "%d", i_depth );
469 mvar_AppendNewVar( itm, "depth", value );
471 if( p_node->i_flags & PLAYLIST_RO_FLAG )
472 mvar_AppendNewVar( itm, "ro", "ro" );
474 mvar_AppendNewVar( itm, "ro", "rw" );
476 mvar_AppendVar( s, itm );
478 for( i_child = 0 ; i_child < p_node->i_children ; i_child++ )
479 PlaylistListNode( p_intf, p_pl, p_node->pp_children[i_child],
480 name, s, i_depth + 1);
484 /****************************************************************************
485 * Seek command parsing handling
486 ****************************************************************************/
487 void HandleSeek( intf_thread_t *p_intf, char *p_value )
489 intf_sys_t *p_sys = p_intf->p_sys;
495 #define POSITION_ABSOLUTE 12
496 #define POSITION_REL_FOR 13
497 #define POSITION_REL_BACK 11
498 #define VL_TIME_ABSOLUTE 0
499 #define VL_TIME_REL_FOR 1
500 #define VL_TIME_REL_BACK -1
503 var_Get( p_sys->p_input, "length", &val );
504 i_length = val.i_time;
506 while( p_value[0] != '\0' )
512 i_relative = VL_TIME_REL_FOR;
518 i_relative = VL_TIME_REL_BACK;
522 case '0': case '1': case '2': case '3': case '4':
523 case '5': case '6': case '7': case '8': case '9':
525 i_stock = strtol( p_value , &p_value , 10 );
528 case '%': /* for percentage ie position */
530 i_relative += POSITION_ABSOLUTE;
538 i_value = 60 * (i_value + i_stock) ;
543 case 'h': case 'H': /* hours */
545 i_value += 3600 * i_stock;
547 /* other characters which are not numbers are not
549 while( ((p_value[0] < '0') || (p_value[0] > '9'))
550 && (p_value[0] != '\0') )
556 case 'm': case 'M': case '\'': /* minutes */
558 i_value += 60 * i_stock;
561 while( ((p_value[0] < '0') || (p_value[0] > '9'))
562 && (p_value[0] != '\0') )
568 case 's': case 'S': case '"': /* seconds */
572 while( ((p_value[0] < '0') || (p_value[0] > '9'))
573 && (p_value[0] != '\0') )
587 /* if there is no known symbol, I consider it as seconds.
588 * Otherwise, i_stock = 0 */
593 case VL_TIME_ABSOLUTE:
595 if( (uint64_t)( i_value ) * 1000000 <= i_length )
596 val.i_time = (uint64_t)( i_value ) * 1000000;
598 val.i_time = i_length;
600 var_Set( p_sys->p_input, "time", val );
601 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
604 case VL_TIME_REL_FOR:
606 var_Get( p_sys->p_input, "time", &val );
607 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
609 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
612 val.i_time = i_length;
614 var_Set( p_sys->p_input, "time", val );
615 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
618 case VL_TIME_REL_BACK:
620 var_Get( p_sys->p_input, "time", &val );
621 if( (int64_t)( i_value ) * 1000000 > val.i_time )
626 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
628 var_Set( p_sys->p_input, "time", val );
629 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
632 case POSITION_ABSOLUTE:
634 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
636 var_Set( p_sys->p_input, "position", val );
637 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
640 case POSITION_REL_FOR:
642 var_Get( p_sys->p_input, "position", &val );
643 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
645 var_Set( p_sys->p_input, "position", val );
646 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
650 case POSITION_REL_BACK:
652 var_Get( p_sys->p_input, "position", &val );
653 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
655 var_Set( p_sys->p_input, "position", val );
656 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
662 msg_Dbg( p_intf, "invalid seek request" );
667 #undef POSITION_ABSOLUTE
668 #undef POSITION_REL_FOR
669 #undef POSITION_REL_BACK
670 #undef VL_TIME_ABSOLUTE
671 #undef VL_TIME_REL_FOR
672 #undef VL_TIME_REL_BACK
676 /****************************************************************************
677 * URI Parsing functions
678 ****************************************************************************/
679 int TestURIParam( char *psz_uri, const char *psz_name )
683 while( (p = strstr( p, psz_name )) )
685 /* Verify that we are dealing with a post/get argument */
686 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
687 && p[strlen(psz_name)] == '=' )
697 static const char *FindURIValue( const char *psz_uri, const char *restrict psz_name,
698 size_t *restrict p_len )
700 const char *p = psz_uri, *end;
703 while( (p = strstr( p, psz_name )) )
705 /* Verify that we are dealing with a post/get argument */
706 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
707 && p[strlen(psz_name)] == '=' )
718 p += strlen( psz_name );
721 if( ( end = strchr( p, '\n' ) ) != NULL )
724 if( ( end > p ) && ( end[-1] == '\r' ) )
732 if( ( end = strchr( p, '&' ) ) != NULL )
742 const char *ExtractURIValue( const char *restrict psz_uri,
743 const char *restrict psz_name,
744 char *restrict psz_buf, size_t bufsize )
747 const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
748 const char *psz_next;
750 if( psz_value == NULL )
757 psz_next = psz_value + len;
763 strncpy( psz_buf, psz_value, len );
770 char *ExtractURIString( const char *restrict psz_uri,
771 const char *restrict psz_name )
774 const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
776 if( psz_value == NULL )
779 char *res = malloc( len + 1 );
783 memcpy( res, psz_value, len );
789 /* Since the resulting string is smaller we can work in place, so it is
790 * permitted to have psz == new. new points to the first word of the
791 * string, the function returns the remaining string. */
792 char *FirstWord( char *psz, char *new )
799 while( *psz != '\0' && *psz != ' ' )
804 while( *psz != '\0' && *psz != c )
806 if( *psz == '\\' && psz[1] != '\0' )
815 if( *psz == '\\' && psz[1] != '\0' )
829 /**********************************************************************
830 * MRLParse: parse the MRL, find the MRL string and the options,
831 * create an item with all information in it, and return the item.
832 * return NULL if there is an error.
833 **********************************************************************/
835 /* Function analog to FirstWord except that it relies on colon instead
836 * of space to delimit option boundaries. */
837 static char *FirstOption( char *psz, char *new )
839 bool b_end, b_start = true;
844 while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
849 while( *psz != '\0' && *psz != c )
851 if( *psz == '\\' && psz[1] != '\0' )
861 if( *psz == '\\' && psz[1] != '\0' )
870 while (new[-1] == ' ')
880 input_item_t *MRLParse( intf_thread_t *p_intf, const char *mrl,
883 char *psz = strdup( mrl ), *s_mrl = psz, *s_temp;
886 /* extract the mrl */
887 s_temp = FirstOption( s_mrl, s_mrl );
890 s_temp = s_mrl + strlen( s_mrl );
893 input_item_t *p_input = input_item_New( p_intf, s_mrl, psz_name );
894 if( p_input == NULL )
898 /* now we can take care of the options */
899 while ( *s_mrl != '\0' )
901 s_temp = FirstOption( s_mrl, s_mrl );
906 s_temp = s_mrl + strlen( s_mrl );
908 input_item_AddOption( p_input, s_mrl, VLC_INPUT_OPTION_TRUSTED );
915 /**********************************************************************
916 * RealPath: parse ../, ~ and path stuff
917 **********************************************************************/
918 char *RealPath( const char *psz_src )
922 int i_len = strlen(psz_src);
924 psz_dir = xmalloc( i_len + 2 );
925 strcpy( psz_dir, psz_src );
927 /* Add a trailing sep to ease the .. step */
928 psz_dir[i_len] = DIR_SEP_CHAR;
929 psz_dir[i_len + 1] = '\0';
931 #if (DIR_SEP_CHAR != '/')
932 /* Convert all / to native separator */
934 while( (p = strchr( p, '/' )) != NULL )
940 /* FIXME: this could be O(N) rather than O(N²)... */
941 /* Remove multiple separators and /./ */
943 while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
945 if( p[1] == DIR_SEP_CHAR )
946 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
947 else if( p[1] == '.' && p[2] == DIR_SEP_CHAR )
948 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
953 if( psz_dir[0] == '~' )
955 char *home = config_GetUserDir( VLC_HOME_DIR ), *dir;
956 asprintf( &dir, "%s%s", home, psz_dir + 1 );
962 if( strlen(psz_dir) > 2 )
966 while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
968 if( p[-1] == '.' && p[-2] == '.' && p[-3] == DIR_SEP_CHAR )
972 if( (q = strrchr( psz_dir, DIR_SEP_CHAR )) != NULL )
974 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
979 memmove( psz_dir, p, strlen(p) + 1 );
988 /* Remove trailing sep if there are at least 2 sep in the string
989 * (handles the C:\ stuff) */
990 p = strrchr( psz_dir, DIR_SEP_CHAR );
991 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, DIR_SEP_CHAR ) )