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 size_t i_in = strlen(psz_utf8);
339 size_t i_out = i_in * 2;
340 char *psz_local = malloc(i_out + 1);
341 char *psz_out = psz_local;
343 char psz_tmp[i_in + 1];
344 char *psz_in = psz_tmp;
345 uint8_t *p = (uint8_t *)psz_tmp;
346 strcpy( psz_tmp, psz_utf8 );
348 /* Fix Unicode quotes. If we are here we are probably converting
349 * to an inferior charset not understanding Unicode quotes. */
352 if( p[0] == 0xe2 && p[1] == 0x80 && p[2] == 0x99 )
355 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
357 if( p[0] == 0xe2 && p[1] == 0x80 && p[2] == 0x9a )
360 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
364 i_in = strlen( psz_tmp );
366 i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
368 if( i_ret == (size_t)-1 || i_in )
371 "failed to convert \"%s\" to desired charset (%s)",
372 psz_utf8, strerror(errno) );
374 return strdup( psz_utf8 );
381 return strdup( psz_utf8 );
384 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
386 intf_sys_t *p_sys = p_intf->p_sys;
388 if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
390 char *psz_in = psz_local;
391 size_t i_in = strlen(psz_in);
392 size_t i_out = i_in * 6;
393 char *psz_utf8 = malloc(i_out + 1);
394 char *psz_out = psz_utf8;
396 size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
398 if( i_ret == (size_t)-1 || i_in )
401 "failed to convert \"%s\" to desired charset (%s)",
402 psz_local, strerror(errno) );
404 return strdup( psz_local );
411 return strdup( psz_local );
414 /*************************************************************************
416 *************************************************************************/
417 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
418 playlist_item_t *p_node, char *name, mvar_t *s,
423 if( p_node->i_children == -1 )
427 mvar_t *itm = E_(mvar_New)( name, "set" );
429 sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
430 E_(mvar_AppendNewVar)( itm, "current", value );
432 sprintf( value, "%d", p_node->input.i_id );
433 E_(mvar_AppendNewVar)( itm, "index", value );
435 psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
436 E_(mvar_AppendNewVar)( itm, "name", psz );
439 psz = E_(FromUTF8)( p_intf, p_node->input.psz_uri );
440 E_(mvar_AppendNewVar)( itm, "uri", psz );
443 sprintf( value, "Item");
444 E_(mvar_AppendNewVar)( itm, "type", value );
446 sprintf( value, "%d", i_depth );
447 E_(mvar_AppendNewVar)( itm, "depth", value );
449 E_(mvar_AppendVar)( s, itm );
456 mvar_t *itm = E_(mvar_New)( name, "set" );
458 psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
459 E_(mvar_AppendNewVar)( itm, "name", psz );
460 E_(mvar_AppendNewVar)( itm, "uri", psz );
463 sprintf( value, "Node" );
464 E_(mvar_AppendNewVar)( itm, "type", value );
466 sprintf( value, "%d", p_node->input.i_id );
467 E_(mvar_AppendNewVar)( itm, "index", value );
469 sprintf( value, "%d", p_node->i_children);
470 E_(mvar_AppendNewVar)( itm, "i_children", value );
472 sprintf( value, "%d", i_depth );
473 E_(mvar_AppendNewVar)( itm, "depth", value );
475 E_(mvar_AppendVar)( s, itm );
477 for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
478 E_(PlaylistListNode)( p_intf, p_pl,
479 p_node->pp_children[i_child],
480 name, s, i_depth + 1);
486 /****************************************************************************
487 * Seek command parsing handling
488 ****************************************************************************/
489 void E_(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 important */
550 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (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')) && (p_value[0] != '\0') )
567 case 's': case 'S': case '"': /* seconds */
571 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
585 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
590 case VL_TIME_ABSOLUTE:
592 if( (uint64_t)( i_value ) * 1000000 <= i_length )
593 val.i_time = (uint64_t)( i_value ) * 1000000;
595 val.i_time = i_length;
597 var_Set( p_sys->p_input, "time", val );
598 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
601 case VL_TIME_REL_FOR:
603 var_Get( p_sys->p_input, "time", &val );
604 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
606 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
609 val.i_time = i_length;
611 var_Set( p_sys->p_input, "time", val );
612 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
615 case VL_TIME_REL_BACK:
617 var_Get( p_sys->p_input, "time", &val );
618 if( (int64_t)( i_value ) * 1000000 > val.i_time )
623 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
625 var_Set( p_sys->p_input, "time", val );
626 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
629 case POSITION_ABSOLUTE:
631 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
632 var_Set( p_sys->p_input, "position", val );
633 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
636 case POSITION_REL_FOR:
638 var_Get( p_sys->p_input, "position", &val );
639 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
640 var_Set( p_sys->p_input, "position", val );
641 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
644 case POSITION_REL_BACK:
646 var_Get( p_sys->p_input, "position", &val );
647 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
648 var_Set( p_sys->p_input, "position", val );
649 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
654 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
659 #undef POSITION_ABSOLUTE
660 #undef POSITION_REL_FOR
661 #undef POSITION_REL_BACK
662 #undef VL_TIME_ABSOLUTE
663 #undef VL_TIME_REL_FOR
664 #undef VL_TIME_REL_BACK
668 /****************************************************************************
669 * URI Parsing functions
670 ****************************************************************************/
671 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
675 while( (p = strstr( p, psz_name )) )
677 /* Verify that we are dealing with a post/get argument */
678 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
679 && p[strlen(psz_name)] == '=' )
688 char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
689 char *psz_value, int i_value_max )
693 while( (p = strstr( p, psz_name )) )
695 /* Verify that we are dealing with a post/get argument */
696 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
697 && p[strlen(psz_name)] == '=' )
706 p += strlen( psz_name );
709 if( strchr( p, '&' ) )
711 i_len = strchr( p, '&' ) - p;
715 /* for POST method */
716 if( strchr( p, '\n' ) )
718 i_len = strchr( p, '\n' ) - p;
719 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
726 i_len = __MIN( i_value_max - 1, i_len );
729 strncpy( psz_value, p, i_len );
730 psz_value[i_len] = '\0';
734 strncpy( psz_value, "", i_value_max );
740 strncpy( psz_value, "", i_value_max );
746 void E_(DecodeEncodedURI)( char *psz )
748 char *dup = strdup( psz );
766 *psz++ = strtol( val, NULL, 16 );
782 /* Since the resulting string is smaller we can work in place, so it is
783 * permitted to have psz == new. new points to the first word of the
784 * string, the function returns the remaining string. */
785 char *E_(FirstWord)( char *psz, char *new )
792 while( *psz != '\0' && *psz != ' ' )
797 while( *psz != '\0' && *psz != c )
799 if( *psz == '\\' && psz[1] != '\0' )
808 if( *psz == '\\' && psz[1] != '\0' )
822 /**********************************************************************
823 * parse_MRL: parse the MRL, find the mrl string and the options,
824 * create an item with all information in it, and return the item.
825 * return NULL if there is an error.
826 **********************************************************************/
827 playlist_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *_psz,
830 char *psz = strdup( _psz );
833 playlist_item_t * p_item = NULL;
835 /* extract the mrl */
836 s_temp = E_(FirstWord)( s_mrl, s_mrl );
839 s_temp = s_mrl + strlen( s_mrl );
842 p_item = playlist_ItemNew( p_intf, s_mrl, psz_name );
845 /* now we can take care of the options */
846 while( *s_mrl != '\0' )
848 s_temp = E_(FirstWord)( s_mrl, s_mrl );
853 s_temp = s_mrl + strlen( s_mrl );
856 msg_Warn( p_intf, "invalid MRL option: %s", s_mrl );
858 playlist_ItemAddOption( p_item, s_mrl );
866 /**********************************************************************
867 * RealPath: parse ../, ~ and path stuff
868 **********************************************************************/
869 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
873 int i_len = strlen(psz_src);
882 psz_dir = malloc( i_len + 2 );
883 strcpy( psz_dir, psz_src );
885 /* Add a trailing sep to ease the .. step */
886 psz_dir[i_len] = sep;
887 psz_dir[i_len + 1] = '\0';
890 /* Convert all / to native separator */
892 while( (p = strchr( p, '/' )) != NULL )
898 /* Remove multiple separators and /./ */
900 while( (p = strchr( p, sep )) != NULL )
903 memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
904 else if( p[1] == '.' && p[2] == sep )
905 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
910 if( psz_dir[0] == '~' )
912 char *dir = malloc( strlen(psz_dir)
913 + strlen(p_intf->p_vlc->psz_userdir) );
914 /* This is incomplete : we should also support the ~cmassiot/ syntax. */
915 sprintf( dir, "%s%s", p_intf->p_vlc->psz_userdir, psz_dir + 1 );
920 if( strlen(psz_dir) > 2 )
924 while( (p = strchr( p, sep )) != NULL )
926 if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
930 if( (q = strrchr( psz_dir, sep )) != NULL )
932 memmove( q + 1, p + 1, strlen(p + 1) + 1 );
937 memmove( psz_dir, p, strlen(p) + 1 );
946 /* Remove trailing sep if there are at least 2 sep in the string
947 * (handles the C:\ stuff) */
948 p = strrchr( psz_dir, sep );
949 if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )