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 )
193 /* parse psz_src dir */
194 if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
199 if( ( psz_filename[0] == '.' )
200 || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
202 free( psz_filename );
206 snprintf( dir, sizeof( dir ), "%s"DIR_SEP"%s", psz_dir, psz_filename );
207 free( psz_filename );
209 if( ParseDirectory( p_intf, psz_root, dir ) )
211 httpd_file_sys_t *f = NULL;
212 httpd_handler_sys_t *h = NULL;
214 char *psz_name, *psz_ext;
216 psz_name = FileToUrl( &dir[strlen( psz_root )], &b_index );
217 psz_ext = strrchr( dir, '.' );
218 if( psz_ext != NULL )
222 for( i = 0; i < p_sys->i_handlers; i++ )
223 if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
225 if( i < p_sys->i_handlers )
227 f = malloc( sizeof( httpd_handler_sys_t ) );
228 h = (httpd_handler_sys_t *)f;
230 h->p_association = p_sys->pp_handlers[i];
235 f = malloc( sizeof( httpd_file_sys_t ) );
236 f->b_handler = false;
243 f->file = strdup (dir);
245 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? true : false;
249 msg_Err( p_intf , "unable to parse directory" );
252 return( VLC_ENOMEM );
254 msg_Dbg( p_intf, "file=%s (url=%s)",
259 char *psz_type = strdup( "text/html; charset=UTF-8" );
260 if( strstr( &dir[strlen( psz_root )], ".xml" ) )
262 char *psz = strstr( psz_type, "html;" );
272 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
274 f->b_html ? psz_type : NULL,
275 user, password, p_acl,
278 if( f->p_file != NULL )
280 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
285 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
287 user, password, p_acl,
288 HandlerCallback, h );
289 if( h->p_handler != NULL )
291 TAB_APPEND( p_sys->i_files, p_sys->pp_files,
292 (httpd_file_sys_t *)h );
296 /* for url that ends by / add
297 * - a redirect from rep to rep/
298 * - in case of index.* rep/index.html to rep/ */
299 if( f && f->name[strlen(f->name) - 1] == '/' )
301 char *psz_redir = strdup( f->name );
303 psz_redir[strlen( psz_redir ) - 1] = '\0';
305 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
306 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
309 if( b_index && ( p = strstr( f->file, "index." ) ) )
311 if( asprintf( &psz_redir, "%s%s", f->name, p ) != -1 )
313 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
314 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
315 f->name, psz_redir );
327 ACL_Destroy( p_acl );
334 /*************************************************************************
336 *************************************************************************/
337 void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl,
338 playlist_item_t *p_node, char *name, mvar_t *s,
341 if( !p_node || !p_node->p_input )
344 if( p_node->i_children == -1 )
348 playlist_item_t * p_item = playlist_CurrentPlayingItem( p_pl );
349 if( !p_item || !p_item->p_input )
352 mvar_t *itm = mvar_New( name, "set" );
353 if( p_item->p_input == p_node->p_input )
354 mvar_AppendNewVar( itm, "current", "1" );
356 mvar_AppendNewVar( itm, "current", "0" );
358 sprintf( value, "%d", p_node->i_id );
359 mvar_AppendNewVar( itm, "index", value );
361 psz = input_item_GetName( p_node->p_input );
362 mvar_AppendNewVar( itm, "name", psz );
365 psz = input_item_GetURI( p_node->p_input );
366 mvar_AppendNewVar( itm, "uri", psz );
369 sprintf( value, "Item");
370 mvar_AppendNewVar( itm, "type", value );
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 );
388 psz = input_item_GetArtist( p_node->p_input );
389 mvar_AppendNewVar( itm, "artist", psz );
391 psz = input_item_GetGenre( p_node->p_input );
392 mvar_AppendNewVar( itm, "genre", psz );
394 psz = input_item_GetCopyright( p_node->p_input );
395 mvar_AppendNewVar( itm, "copyright", psz );
397 psz = input_item_GetAlbum( p_node->p_input );
398 mvar_AppendNewVar( itm, "album", psz );
400 psz = input_item_GetTrackNum( p_node->p_input );
401 mvar_AppendNewVar( itm, "track", psz );
403 psz = input_item_GetDescription( p_node->p_input );
404 mvar_AppendNewVar( itm, "description", psz );
406 psz = input_item_GetRating( p_node->p_input );
407 mvar_AppendNewVar( itm, "rating", psz );
409 psz = input_item_GetDate( p_node->p_input );
410 mvar_AppendNewVar( itm, "date", psz );
412 psz = input_item_GetURL( p_node->p_input );
413 mvar_AppendNewVar( itm, "url", psz );
415 psz = input_item_GetLanguage( p_node->p_input );
416 mvar_AppendNewVar( itm, "language", psz );
418 psz = input_item_GetNowPlaying( p_node->p_input );
419 mvar_AppendNewVar( itm, "now_playing", psz );
421 psz = input_item_GetPublisher( p_node->p_input );
422 mvar_AppendNewVar( itm, "publisher", psz );
424 psz = input_item_GetEncodedBy( p_node->p_input );
425 mvar_AppendNewVar( itm, "encoded_by", psz );
427 psz = input_item_GetArtURL( p_node->p_input );
428 mvar_AppendNewVar( itm, "art_url", psz );
430 psz = input_item_GetTrackID( p_node->p_input );
431 mvar_AppendNewVar( itm, "track_id", psz );
433 mvar_AppendVar( s, itm );
439 mvar_t *itm = mvar_New( name, "set" );
441 mvar_AppendNewVar( itm, "name", p_node->p_input->psz_name );
442 mvar_AppendNewVar( itm, "uri", p_node->p_input->psz_name );
444 sprintf( value, "Node" );
445 mvar_AppendNewVar( itm, "type", value );
447 sprintf( value, "%d", p_node->i_id );
448 mvar_AppendNewVar( itm, "index", value );
450 sprintf( value, "%d", p_node->i_children);
451 mvar_AppendNewVar( itm, "i_children", value );
453 sprintf( value, "%d", i_depth );
454 mvar_AppendNewVar( itm, "depth", value );
456 if( p_node->i_flags & PLAYLIST_RO_FLAG )
457 mvar_AppendNewVar( itm, "ro", "ro" );
459 mvar_AppendNewVar( itm, "ro", "rw" );
461 mvar_AppendVar( s, itm );
463 for( i_child = 0 ; i_child < p_node->i_children ; i_child++ )
464 PlaylistListNode( p_intf, p_pl, p_node->pp_children[i_child],
465 name, s, i_depth + 1);
469 /****************************************************************************
470 * Seek command parsing handling
471 ****************************************************************************/
472 void HandleSeek( intf_thread_t *p_intf, char *p_value )
474 intf_sys_t *p_sys = p_intf->p_sys;
480 #define POSITION_ABSOLUTE 12
481 #define POSITION_REL_FOR 13
482 #define POSITION_REL_BACK 11
483 #define VL_TIME_ABSOLUTE 0
484 #define VL_TIME_REL_FOR 1
485 #define VL_TIME_REL_BACK -1
488 var_Get( p_sys->p_input, "length", &val );
489 i_length = val.i_time;
491 while( p_value[0] != '\0' )
497 i_relative = VL_TIME_REL_FOR;
503 i_relative = VL_TIME_REL_BACK;
507 case '0': case '1': case '2': case '3': case '4':
508 case '5': case '6': case '7': case '8': case '9':
510 i_stock = strtol( p_value , &p_value , 10 );
513 case '%': /* for percentage ie position */
515 i_relative += POSITION_ABSOLUTE;
523 i_value = 60 * (i_value + i_stock) ;
528 case 'h': case 'H': /* hours */
530 i_value += 3600 * i_stock;
532 /* other characters which are not numbers are not
534 while( ((p_value[0] < '0') || (p_value[0] > '9'))
535 && (p_value[0] != '\0') )
541 case 'm': case 'M': case '\'': /* minutes */
543 i_value += 60 * i_stock;
546 while( ((p_value[0] < '0') || (p_value[0] > '9'))
547 && (p_value[0] != '\0') )
553 case 's': case 'S': case '"': /* seconds */
557 while( ((p_value[0] < '0') || (p_value[0] > '9'))
558 && (p_value[0] != '\0') )
572 /* if there is no known symbol, I consider it as seconds.
573 * Otherwise, i_stock = 0 */
578 case VL_TIME_ABSOLUTE:
580 if( (uint64_t)( i_value ) * 1000000 <= i_length )
581 val.i_time = (uint64_t)( i_value ) * 1000000;
583 val.i_time = i_length;
585 var_Set( p_sys->p_input, "time", val );
586 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
589 case VL_TIME_REL_FOR:
591 var_Get( p_sys->p_input, "time", &val );
592 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
594 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
597 val.i_time = i_length;
599 var_Set( p_sys->p_input, "time", val );
600 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
603 case VL_TIME_REL_BACK:
605 var_Get( p_sys->p_input, "time", &val );
606 if( (int64_t)( i_value ) * 1000000 > val.i_time )
611 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
613 var_Set( p_sys->p_input, "time", val );
614 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
617 case POSITION_ABSOLUTE:
619 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
621 var_Set( p_sys->p_input, "position", val );
622 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
625 case POSITION_REL_FOR:
627 var_Get( p_sys->p_input, "position", &val );
628 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
630 var_Set( p_sys->p_input, "position", val );
631 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
635 case POSITION_REL_BACK:
637 var_Get( p_sys->p_input, "position", &val );
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 backward: %d%%",
647 msg_Dbg( p_intf, "invalid seek request" );
652 #undef POSITION_ABSOLUTE
653 #undef POSITION_REL_FOR
654 #undef POSITION_REL_BACK
655 #undef VL_TIME_ABSOLUTE
656 #undef VL_TIME_REL_FOR
657 #undef VL_TIME_REL_BACK
661 /****************************************************************************
662 * URI Parsing functions
663 ****************************************************************************/
664 int TestURIParam( char *psz_uri, const char *psz_name )
668 while( (p = strstr( p, psz_name )) )
670 /* Verify that we are dealing with a post/get argument */
671 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
672 && p[strlen(psz_name)] == '=' )
682 static const char *FindURIValue( const char *psz_uri, const char *restrict psz_name,
683 size_t *restrict p_len )
685 const char *p = psz_uri, *end;
688 while( (p = strstr( p, psz_name )) )
690 /* Verify that we are dealing with a post/get argument */
691 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
692 && p[strlen(psz_name)] == '=' )
703 p += strlen( psz_name );
706 if( ( end = strchr( p, '\n' ) ) != NULL )
709 if( ( end > p ) && ( end[-1] == '\r' ) )
717 if( ( end = strchr( p, '&' ) ) != NULL )
727 const char *ExtractURIValue( const char *restrict psz_uri,
728 const char *restrict psz_name,
729 char *restrict psz_buf, size_t bufsize )
732 const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
733 const char *psz_next;
735 if( psz_value == NULL )
742 psz_next = psz_value + len;
748 strncpy( psz_buf, psz_value, len );
755 char *ExtractURIString( const char *restrict psz_uri,
756 const char *restrict psz_name )
759 const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
761 if( psz_value == NULL )
764 char *res = malloc( len + 1 );
768 memcpy( res, psz_value, len );
774 /* Since the resulting string is smaller we can work in place, so it is
775 * permitted to have psz == new. new points to the first word of the
776 * string, the function returns the remaining string. */
777 char *FirstWord( char *psz, char *new )
784 while( *psz != '\0' && *psz != ' ' )
789 while( *psz != '\0' && *psz != c )
791 if( *psz == '\\' && psz[1] != '\0' )
800 if( *psz == '\\' && psz[1] != '\0' )
814 /**********************************************************************
815 * MRLParse: parse the MRL, find the MRL string and the options,
816 * create an item with all information in it, and return the item.
817 * return NULL if there is an error.
818 **********************************************************************/
820 /* Function analog to FirstWord except that it relies on colon instead
821 * of space to delimit option boundaries. */
822 static char *FirstOption( char *psz, char *new )
824 bool b_end, b_start = true;
829 while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
834 while( *psz != '\0' && *psz != c )
836 if( *psz == '\\' && psz[1] != '\0' )
846 if( *psz == '\\' && psz[1] != '\0' )
855 while (new[-1] == ' ')
865 input_item_t *MRLParse( intf_thread_t *p_intf, const char *mrl,
868 char *psz = strdup( mrl ), *s_mrl = psz, *s_temp;
871 /* extract the mrl */
872 s_temp = FirstOption( s_mrl, s_mrl );
875 s_temp = s_mrl + strlen( s_mrl );
878 input_item_t *p_input = input_item_New( p_intf, s_mrl, psz_name );
879 if( p_input == NULL )
883 /* now we can take care of the options */
884 while ( *s_mrl != '\0' )
886 s_temp = FirstOption( s_mrl, s_mrl );
891 s_temp = s_mrl + strlen( s_mrl );
893 input_item_AddOption( p_input, s_mrl, VLC_INPUT_OPTION_TRUSTED );
900 /**********************************************************************
901 * RealPath: parse ../, ~ and path stuff
902 **********************************************************************/
903 char *RealPath( const char *psz_src )
907 int i_len = strlen(psz_src);
909 psz_dir = malloc( i_len + 2 );
910 strcpy( psz_dir, psz_src );
912 /* Add a trailing sep to ease the .. step */
913 psz_dir[i_len] = DIR_SEP_CHAR;
914 psz_dir[i_len + 1] = '\0';
916 #if (DIR_SEP_CHAR != '/')
917 /* Convert all / to native separator */
919 while( (p = strchr( p, '/' )) != NULL )
925 /* FIXME: this could be O(N) rather than O(N²)... */
926 /* Remove multiple separators and /./ */
928 while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
930 if( p[1] == DIR_SEP_CHAR )
931 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
932 else if( p[1] == '.' && p[2] == DIR_SEP_CHAR )
933 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
938 if( psz_dir[0] == '~' )
941 asprintf( &dir, "%s%s", config_GetHomeDir(), psz_dir + 1 );
946 if( strlen(psz_dir) > 2 )
950 while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
952 if( p[-1] == '.' && p[-2] == '.' && p[-3] == DIR_SEP_CHAR )
956 if( (q = strrchr( psz_dir, DIR_SEP_CHAR )) != NULL )
958 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
963 memmove( psz_dir, p, strlen(p) + 1 );
972 /* Remove trailing sep if there are at least 2 sep in the string
973 * (handles the C:\ stuff) */
974 p = strrchr( psz_dir, DIR_SEP_CHAR );
975 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, DIR_SEP_CHAR ) )