1 /*****************************************************************************
2 * util.c : Utility functions for HTTP interface
3 *****************************************************************************
4 * Copyright (C) 2001-2005 the VideoLAN team
5 * $Id: http.c 12225 2005-08-18 10:01:30Z massiot $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
28 /****************************************************************************
29 * File and directory functions
30 ****************************************************************************/
32 /* ToUrl: create a good name for an url from filename */
33 char *E_(FileToUrl)( char *name, vlc_bool_t *pb_index )
37 url = p = malloc( strlen( name ) + 1 );
39 *pb_index = VLC_FALSE;
46 while( *name == '\\' || *name == '/' )
58 /* convert '\\' into '/' */
69 if( ( p = strrchr( url, '/' ) ) != NULL )
71 if( !strncmp( p, "/index.", 7 ) )
81 int E_(FileLoad)( FILE *f, char **pp_data, int *pi_data )
85 /* just load the file */
87 *pp_data = malloc( 1025 ); /* +1 for \0 */
88 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
91 *pp_data = realloc( *pp_data, *pi_data + 1025 );
97 (*pp_data)[*pi_data] = '\0';
102 /* Parse a directory and recursively add files */
103 int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
106 intf_sys_t *p_sys = p_intf->p_sys;
107 char dir[MAX_DIR_SIZE];
108 #ifdef HAVE_SYS_STAT_H
109 struct stat stat_info;
112 struct dirent *p_dir_content;
117 char *password = NULL;
129 #ifdef HAVE_SYS_STAT_H
130 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
136 if( ( p_dir = opendir( psz_dir ) ) == NULL )
138 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
142 i_dirlen = strlen( psz_dir );
143 if( i_dirlen + 10 > MAX_DIR_SIZE )
145 msg_Warn( p_intf, "skipping too deep dir (%s)", psz_dir );
149 msg_Dbg( p_intf, "dir=%s", psz_dir );
151 sprintf( dir, "%s%c.access", psz_dir, sep );
152 if( ( file = fopen( dir, "r" ) ) != NULL )
157 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
159 i_size = fread( line, 1, 1023, file );
163 while( i_size > 0 && ( line[i_size-1] == '\n' ||
164 line[i_size-1] == '\r' ) )
171 p = strchr( line, ':' );
175 user = strdup( line );
176 password = strdup( p );
179 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
180 user, password, i_size );
185 sprintf( dir, "%s%c.hosts", psz_dir, sep );
186 p_acl = ACL_Create( p_intf, VLC_FALSE );
187 if( ACL_LoadFile( p_acl, dir ) )
189 ACL_Destroy( p_acl );
195 /* parse psz_src dir */
196 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
201 if( ( p_dir_content->d_name[0] == '.' )
202 || ( i_dirlen + strlen( p_dir_content->d_name ) > MAX_DIR_SIZE ) )
205 sprintf( dir, "%s%c%s", psz_dir, sep, p_dir_content->d_name );
206 if( E_(ParseDirectory)( p_intf, psz_root, dir ) )
208 httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
216 psz_tmp = vlc_fix_readdir_charset( p_intf, dir );
217 f->file = E_(FromUTF8)( p_intf, psz_tmp );
219 psz_tmp = vlc_fix_readdir_charset( p_intf,
220 &dir[strlen( psz_root )] );
221 f->name = E_(FileToUrl)( psz_tmp, &b_index );
223 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
227 msg_Err( p_intf , "unable to parse directory" );
230 return( VLC_ENOMEM );
232 msg_Dbg( p_intf, "file=%s (url=%s)",
235 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
237 f->b_html ? p_sys->psz_html_type : NULL,
238 user, password, p_acl,
239 E_(HttpCallback), f );
243 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
245 /* for url that ends by / add
246 * - a redirect from rep to rep/
247 * - in case of index.* rep/index.html to rep/ */
248 if( f && f->name[strlen(f->name) - 1] == '/' )
250 char *psz_redir = strdup( f->name );
252 psz_redir[strlen( psz_redir ) - 1] = '\0';
254 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
255 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
258 if( b_index && ( p = strstr( f->file, "index." ) ) )
260 asprintf( &psz_redir, "%s%s", f->name, p );
262 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
263 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
264 f->name, psz_redir );
281 ACL_Destroy( p_acl );
288 /**************************************************************************
290 **************************************************************************/
291 char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 )
293 intf_sys_t *p_sys = p_intf->p_sys;
295 if ( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
297 char *psz_in = psz_utf8;
298 size_t i_in = strlen(psz_in);
299 size_t i_out = i_in * 2;
300 char *psz_local = malloc(i_out + 1);
301 char *psz_out = psz_local;
303 size_t i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
305 if( i_ret == (size_t)-1 || i_in )
308 "failed to convert \"%s\" to desired charset (%s)",
309 psz_utf8, strerror(errno) );
311 return strdup( psz_utf8 );
318 return strdup( psz_utf8 );
321 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
323 intf_sys_t *p_sys = p_intf->p_sys;
325 if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
327 char *psz_in = psz_local;
328 size_t i_in = strlen(psz_in);
329 size_t i_out = i_in * 6;
330 char *psz_utf8 = malloc(i_out + 1);
331 char *psz_out = psz_utf8;
333 size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
335 if( i_ret == (size_t)-1 || i_in )
338 "failed to convert \"%s\" to desired charset (%s)",
339 psz_local, strerror(errno) );
341 return strdup( psz_local );
348 return strdup( psz_local );
351 /*************************************************************************
353 *************************************************************************/
354 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
355 playlist_item_t *p_node, char *name, mvar_t *s,
360 if( p_node->i_children == -1 )
364 mvar_t *itm = mvar_New( name, "set" );
366 sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
367 mvar_AppendNewVar( itm, "current", value );
369 sprintf( value, "%d", p_node->input.i_id );
370 mvar_AppendNewVar( itm, "index", value );
372 psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
373 mvar_AppendNewVar( itm, "name", psz );
376 psz = E_(FromUTF8)( p_intf, p_node->input.psz_uri );
377 mvar_AppendNewVar( itm, "uri", psz );
380 sprintf( value, "Item");
381 mvar_AppendNewVar( itm, "type", value );
383 sprintf( value, "%d", i_depth );
384 mvar_AppendNewVar( itm, "depth", value );
386 mvar_AppendVar( s, itm );
393 mvar_t *itm = mvar_New( name, "set" );
395 psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
396 mvar_AppendNewVar( itm, "name", psz );
397 mvar_AppendNewVar( itm, "uri", psz );
400 sprintf( value, "Node" );
401 mvar_AppendNewVar( itm, "type", value );
403 sprintf( value, "%d", p_node->input.i_id );
404 mvar_AppendNewVar( itm, "index", value );
406 sprintf( value, "%d", p_node->i_children);
407 mvar_AppendNewVar( itm, "i_children", value );
409 sprintf( value, "%d", i_depth );
410 mvar_AppendNewVar( itm, "depth", value );
412 mvar_AppendVar( s, itm );
414 for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
415 E_(PlaylistListNode)( p_intf, p_pl,
416 p_node->pp_children[i_child],
417 name, s, i_depth + 1);
423 /****************************************************************************
424 * Seek command parsing handling
425 ****************************************************************************/
426 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
428 intf_sys_t *p_sys = p_intf->p_sys;
434 #define POSITION_ABSOLUTE 12
435 #define POSITION_REL_FOR 13
436 #define POSITION_REL_BACK 11
437 #define VL_TIME_ABSOLUTE 0
438 #define VL_TIME_REL_FOR 1
439 #define VL_TIME_REL_BACK -1
442 var_Get( p_sys->p_input, "length", &val );
443 i_length = val.i_time;
445 while( p_value[0] != '\0' )
451 i_relative = VL_TIME_REL_FOR;
457 i_relative = VL_TIME_REL_BACK;
461 case '0': case '1': case '2': case '3': case '4':
462 case '5': case '6': case '7': case '8': case '9':
464 i_stock = strtol( p_value , &p_value , 10 );
467 case '%': /* for percentage ie position */
469 i_relative += POSITION_ABSOLUTE;
477 i_value = 60 * (i_value + i_stock) ;
482 case 'h': case 'H': /* hours */
484 i_value += 3600 * i_stock;
486 /* other characters which are not numbers are not important */
487 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
493 case 'm': case 'M': case '\'': /* minutes */
495 i_value += 60 * i_stock;
498 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
504 case 's': case 'S': case '"': /* seconds */
508 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
522 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
527 case VL_TIME_ABSOLUTE:
529 if( (uint64_t)( i_value ) * 1000000 <= i_length )
530 val.i_time = (uint64_t)( i_value ) * 1000000;
532 val.i_time = i_length;
534 var_Set( p_sys->p_input, "time", val );
535 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
538 case VL_TIME_REL_FOR:
540 var_Get( p_sys->p_input, "time", &val );
541 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
543 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
546 val.i_time = i_length;
548 var_Set( p_sys->p_input, "time", val );
549 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
552 case VL_TIME_REL_BACK:
554 var_Get( p_sys->p_input, "time", &val );
555 if( (int64_t)( i_value ) * 1000000 > val.i_time )
560 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
562 var_Set( p_sys->p_input, "time", val );
563 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
566 case POSITION_ABSOLUTE:
568 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
569 var_Set( p_sys->p_input, "position", val );
570 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
573 case POSITION_REL_FOR:
575 var_Get( p_sys->p_input, "position", &val );
576 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
577 var_Set( p_sys->p_input, "position", val );
578 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
581 case POSITION_REL_BACK:
583 var_Get( p_sys->p_input, "position", &val );
584 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
585 var_Set( p_sys->p_input, "position", val );
586 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
591 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
596 #undef POSITION_ABSOLUTE
597 #undef POSITION_REL_FOR
598 #undef POSITION_REL_BACK
599 #undef VL_TIME_ABSOLUTE
600 #undef VL_TIME_REL_FOR
601 #undef VL_TIME_REL_BACK
605 /****************************************************************************
606 * URI Parsing functions
607 ****************************************************************************/
608 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
612 while( (p = strstr( p, psz_name )) )
614 /* Verify that we are dealing with a post/get argument */
615 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
616 && p[strlen(psz_name)] == '=' )
625 char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
626 char *psz_value, int i_value_max )
630 while( (p = strstr( p, psz_name )) )
632 /* Verify that we are dealing with a post/get argument */
633 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
634 && p[strlen(psz_name)] == '=' )
643 p += strlen( psz_name );
646 if( strchr( p, '&' ) )
648 i_len = strchr( p, '&' ) - p;
652 /* for POST method */
653 if( strchr( p, '\n' ) )
655 i_len = strchr( p, '\n' ) - p;
656 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
663 i_len = __MIN( i_value_max - 1, i_len );
666 strncpy( psz_value, p, i_len );
667 psz_value[i_len] = '\0';
671 strncpy( psz_value, "", i_value_max );
677 strncpy( psz_value, "", i_value_max );
683 void E_(DecodeEncodedURI)( char *psz )
685 char *dup = strdup( psz );
703 *psz++ = strtol( val, NULL, 16 );
719 /* Since the resulting string is smaller we can work in place, so it is
720 * permitted to have psz == new. new points to the first word of the
721 * string, the function returns the remaining string. */
722 char *E_(FirstWord)( char *psz, char *new )
729 while( *psz != '\0' && *psz != ' ' )
734 while( *psz != '\0' && *psz != c )
736 if( *psz == '\\' && psz[1] != '\0' )
745 if( *psz == '\\' && psz[1] != '\0' )
759 /**********************************************************************
760 * parse_MRL: parse the MRL, find the mrl string and the options,
761 * create an item with all information in it, and return the item.
762 * return NULL if there is an error.
763 **********************************************************************/
764 playlist_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *_psz,
767 char *psz = strdup( _psz );
770 playlist_item_t * p_item = NULL;
772 /* extract the mrl */
773 s_temp = E_(FirstWord)( s_mrl, s_mrl );
776 s_temp = s_mrl + strlen( s_mrl );
779 p_item = playlist_ItemNew( p_intf, s_mrl, psz_name );
782 /* now we can take care of the options */
783 while( *s_mrl != '\0' )
785 s_temp = E_(FirstWord)( s_mrl, s_mrl );
790 s_temp = s_mrl + strlen( s_mrl );
793 msg_Warn( p_intf, "invalid MRL option: %s", s_mrl );
795 playlist_ItemAddOption( p_item, s_mrl );
803 /**********************************************************************
804 * RealPath: parse ../, ~ and path stuff
805 **********************************************************************/
806 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
810 int i_len = strlen(psz_src);
819 psz_dir = malloc( i_len + 2 );
820 strcpy( psz_dir, psz_src );
822 /* Add a trailing sep to ease the .. step */
823 psz_dir[i_len] = sep;
824 psz_dir[i_len + 1] = '\0';
827 /* Convert all / to native separator */
829 while( (p = strchr( p, '/' )) != NULL )
835 /* Remove multiple separators and /./ */
837 while( (p = strchr( p, sep )) != NULL )
840 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
841 else if( p[1] == '.' && p[2] == sep )
842 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
847 if( psz_dir[0] == '~' )
849 char *dir = malloc( strlen(psz_dir)
850 + strlen(p_intf->p_vlc->psz_homedir) );
851 /* This is incomplete : we should also support the ~cmassiot/ syntax. */
852 sprintf( dir, "%s%s", p_intf->p_vlc->psz_homedir, psz_dir + 1 );
857 if( strlen(psz_dir) > 2 )
861 while( (p = strchr( p, sep )) != NULL )
863 if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
867 if( (q = strrchr( psz_dir, sep )) != NULL )
869 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
874 memmove( psz_dir, p + 1, strlen(p + 1) + 1 );
883 /* Remove trailing sep if there are at least 2 sep in the string
884 * (handles the C:\ stuff) */
885 p = strrchr( psz_dir, sep );
886 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )