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 = NULL;
209 httpd_handler_sys_t *h = NULL;
211 char *psz_tmp, *psz_file, *psz_name, *psz_ext;
213 psz_tmp = vlc_fix_readdir_charset( p_intf, dir );
214 psz_file = E_(FromUTF8)( p_intf, psz_tmp );
216 psz_tmp = vlc_fix_readdir_charset( p_intf,
217 &dir[strlen( psz_root )] );
218 psz_name = E_(FileToUrl)( psz_tmp, &b_index );
220 psz_ext = strrchr( psz_file, '.' );
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;
232 f->b_handler = VLC_TRUE;
233 h->p_association = p_sys->pp_handlers[i];
238 f = malloc( sizeof( httpd_file_sys_t ) );
239 f->b_handler = VLC_FALSE;
248 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
252 msg_Err( p_intf , "unable to parse directory" );
255 return( VLC_ENOMEM );
257 msg_Dbg( p_intf, "file=%s (url=%s)",
262 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
264 f->b_html ? p_sys->psz_html_type :
266 user, password, p_acl,
267 E_(HttpCallback), f );
268 if( f->p_file != NULL )
270 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
275 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
277 user, password, p_acl,
278 E_(HandlerCallback), h );
279 if( h->p_handler != NULL )
281 TAB_APPEND( p_sys->i_files, p_sys->pp_files,
282 (httpd_file_sys_t *)h );
286 /* for url that ends by / add
287 * - a redirect from rep to rep/
288 * - in case of index.* rep/index.html to rep/ */
289 if( f && f->name[strlen(f->name) - 1] == '/' )
291 char *psz_redir = strdup( f->name );
293 psz_redir[strlen( psz_redir ) - 1] = '\0';
295 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
296 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
299 if( b_index && ( p = strstr( f->file, "index." ) ) )
301 asprintf( &psz_redir, "%s%s", f->name, p );
303 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
304 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
305 f->name, psz_redir );
322 ACL_Destroy( p_acl );
329 /**************************************************************************
331 **************************************************************************/
332 char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 )
334 intf_sys_t *p_sys = p_intf->p_sys;
336 if ( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
338 char *psz_in = psz_utf8;
339 size_t i_in = strlen(psz_in);
340 size_t i_out = i_in * 2;
341 char *psz_local = malloc(i_out + 1);
342 char *psz_out = psz_local;
344 size_t i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
346 if( i_ret == (size_t)-1 || i_in )
349 "failed to convert \"%s\" to desired charset (%s)",
350 psz_utf8, strerror(errno) );
352 return strdup( psz_utf8 );
359 return strdup( psz_utf8 );
362 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
364 intf_sys_t *p_sys = p_intf->p_sys;
366 if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
368 char *psz_in = psz_local;
369 size_t i_in = strlen(psz_in);
370 size_t i_out = i_in * 6;
371 char *psz_utf8 = malloc(i_out + 1);
372 char *psz_out = psz_utf8;
374 size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
376 if( i_ret == (size_t)-1 || i_in )
379 "failed to convert \"%s\" to desired charset (%s)",
380 psz_local, strerror(errno) );
382 return strdup( psz_local );
389 return strdup( psz_local );
392 /*************************************************************************
394 *************************************************************************/
395 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
396 playlist_item_t *p_node, char *name, mvar_t *s,
401 if( p_node->i_children == -1 )
405 mvar_t *itm = E_(mvar_New)( name, "set" );
407 sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
408 E_(mvar_AppendNewVar)( itm, "current", value );
410 sprintf( value, "%d", p_node->input.i_id );
411 E_(mvar_AppendNewVar)( itm, "index", value );
413 psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
414 E_(mvar_AppendNewVar)( itm, "name", psz );
417 psz = E_(FromUTF8)( p_intf, p_node->input.psz_uri );
418 E_(mvar_AppendNewVar)( itm, "uri", psz );
421 sprintf( value, "Item");
422 E_(mvar_AppendNewVar)( itm, "type", value );
424 sprintf( value, "%d", i_depth );
425 E_(mvar_AppendNewVar)( itm, "depth", value );
427 E_(mvar_AppendVar)( s, itm );
434 mvar_t *itm = E_(mvar_New)( name, "set" );
436 psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
437 E_(mvar_AppendNewVar)( itm, "name", psz );
438 E_(mvar_AppendNewVar)( itm, "uri", psz );
441 sprintf( value, "Node" );
442 E_(mvar_AppendNewVar)( itm, "type", value );
444 sprintf( value, "%d", p_node->input.i_id );
445 E_(mvar_AppendNewVar)( itm, "index", value );
447 sprintf( value, "%d", p_node->i_children);
448 E_(mvar_AppendNewVar)( itm, "i_children", value );
450 sprintf( value, "%d", i_depth );
451 E_(mvar_AppendNewVar)( itm, "depth", value );
453 E_(mvar_AppendVar)( s, itm );
455 for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
456 E_(PlaylistListNode)( p_intf, p_pl,
457 p_node->pp_children[i_child],
458 name, s, i_depth + 1);
464 /****************************************************************************
465 * Seek command parsing handling
466 ****************************************************************************/
467 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
469 intf_sys_t *p_sys = p_intf->p_sys;
475 #define POSITION_ABSOLUTE 12
476 #define POSITION_REL_FOR 13
477 #define POSITION_REL_BACK 11
478 #define VL_TIME_ABSOLUTE 0
479 #define VL_TIME_REL_FOR 1
480 #define VL_TIME_REL_BACK -1
483 var_Get( p_sys->p_input, "length", &val );
484 i_length = val.i_time;
486 while( p_value[0] != '\0' )
492 i_relative = VL_TIME_REL_FOR;
498 i_relative = VL_TIME_REL_BACK;
502 case '0': case '1': case '2': case '3': case '4':
503 case '5': case '6': case '7': case '8': case '9':
505 i_stock = strtol( p_value , &p_value , 10 );
508 case '%': /* for percentage ie position */
510 i_relative += POSITION_ABSOLUTE;
518 i_value = 60 * (i_value + i_stock) ;
523 case 'h': case 'H': /* hours */
525 i_value += 3600 * i_stock;
527 /* other characters which are not numbers are not important */
528 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
534 case 'm': case 'M': case '\'': /* minutes */
536 i_value += 60 * i_stock;
539 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
545 case 's': case 'S': case '"': /* seconds */
549 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
563 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
568 case VL_TIME_ABSOLUTE:
570 if( (uint64_t)( i_value ) * 1000000 <= i_length )
571 val.i_time = (uint64_t)( i_value ) * 1000000;
573 val.i_time = i_length;
575 var_Set( p_sys->p_input, "time", val );
576 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
579 case VL_TIME_REL_FOR:
581 var_Get( p_sys->p_input, "time", &val );
582 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
584 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
587 val.i_time = i_length;
589 var_Set( p_sys->p_input, "time", val );
590 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
593 case VL_TIME_REL_BACK:
595 var_Get( p_sys->p_input, "time", &val );
596 if( (int64_t)( i_value ) * 1000000 > val.i_time )
601 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
603 var_Set( p_sys->p_input, "time", val );
604 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
607 case POSITION_ABSOLUTE:
609 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
610 var_Set( p_sys->p_input, "position", val );
611 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
614 case POSITION_REL_FOR:
616 var_Get( p_sys->p_input, "position", &val );
617 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
618 var_Set( p_sys->p_input, "position", val );
619 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
622 case POSITION_REL_BACK:
624 var_Get( p_sys->p_input, "position", &val );
625 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
626 var_Set( p_sys->p_input, "position", val );
627 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
632 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
637 #undef POSITION_ABSOLUTE
638 #undef POSITION_REL_FOR
639 #undef POSITION_REL_BACK
640 #undef VL_TIME_ABSOLUTE
641 #undef VL_TIME_REL_FOR
642 #undef VL_TIME_REL_BACK
646 /****************************************************************************
647 * URI Parsing functions
648 ****************************************************************************/
649 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
653 while( (p = strstr( p, psz_name )) )
655 /* Verify that we are dealing with a post/get argument */
656 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
657 && p[strlen(psz_name)] == '=' )
666 char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
667 char *psz_value, int i_value_max )
671 while( (p = strstr( p, psz_name )) )
673 /* Verify that we are dealing with a post/get argument */
674 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
675 && p[strlen(psz_name)] == '=' )
684 p += strlen( psz_name );
687 if( strchr( p, '&' ) )
689 i_len = strchr( p, '&' ) - p;
693 /* for POST method */
694 if( strchr( p, '\n' ) )
696 i_len = strchr( p, '\n' ) - p;
697 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
704 i_len = __MIN( i_value_max - 1, i_len );
707 strncpy( psz_value, p, i_len );
708 psz_value[i_len] = '\0';
712 strncpy( psz_value, "", i_value_max );
718 strncpy( psz_value, "", i_value_max );
724 void E_(DecodeEncodedURI)( char *psz )
726 char *dup = strdup( psz );
744 *psz++ = strtol( val, NULL, 16 );
760 /* Since the resulting string is smaller we can work in place, so it is
761 * permitted to have psz == new. new points to the first word of the
762 * string, the function returns the remaining string. */
763 char *E_(FirstWord)( char *psz, char *new )
770 while( *psz != '\0' && *psz != ' ' )
775 while( *psz != '\0' && *psz != c )
777 if( *psz == '\\' && psz[1] != '\0' )
786 if( *psz == '\\' && psz[1] != '\0' )
800 /**********************************************************************
801 * parse_MRL: parse the MRL, find the mrl string and the options,
802 * create an item with all information in it, and return the item.
803 * return NULL if there is an error.
804 **********************************************************************/
805 playlist_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *_psz,
808 char *psz = strdup( _psz );
811 playlist_item_t * p_item = NULL;
813 /* extract the mrl */
814 s_temp = E_(FirstWord)( s_mrl, s_mrl );
817 s_temp = s_mrl + strlen( s_mrl );
820 p_item = playlist_ItemNew( p_intf, s_mrl, psz_name );
823 /* now we can take care of the options */
824 while( *s_mrl != '\0' )
826 s_temp = E_(FirstWord)( s_mrl, s_mrl );
831 s_temp = s_mrl + strlen( s_mrl );
834 msg_Warn( p_intf, "invalid MRL option: %s", s_mrl );
836 playlist_ItemAddOption( p_item, s_mrl );
844 /**********************************************************************
845 * RealPath: parse ../, ~ and path stuff
846 **********************************************************************/
847 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
851 int i_len = strlen(psz_src);
860 psz_dir = malloc( i_len + 2 );
861 strcpy( psz_dir, psz_src );
863 /* Add a trailing sep to ease the .. step */
864 psz_dir[i_len] = sep;
865 psz_dir[i_len + 1] = '\0';
868 /* Convert all / to native separator */
870 while( (p = strchr( p, '/' )) != NULL )
876 /* Remove multiple separators and /./ */
878 while( (p = strchr( p, sep )) != NULL )
881 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
882 else if( p[1] == '.' && p[2] == sep )
883 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
888 if( psz_dir[0] == '~' )
890 char *dir = malloc( strlen(psz_dir)
891 + strlen(p_intf->p_vlc->psz_homedir) );
892 /* This is incomplete : we should also support the ~cmassiot/ syntax. */
893 sprintf( dir, "%s%s", p_intf->p_vlc->psz_homedir, psz_dir + 1 );
898 if( strlen(psz_dir) > 2 )
902 while( (p = strchr( p, sep )) != NULL )
904 if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
908 if( (q = strrchr( psz_dir, sep )) != NULL )
910 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
915 memmove( psz_dir, p, strlen(p) + 1 );
924 /* Remove trailing sep if there are at least 2 sep in the string
925 * (handles the C:\ stuff) */
926 p = strrchr( psz_dir, sep );
927 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )