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 '/' */
71 if( ( p = strrchr( url, '/' ) ) != NULL )
73 if( !strncmp( p, "/index.", 7 ) )
83 int E_(FileLoad)( FILE *f, char **pp_data, int *pi_data )
87 /* just load the file */
89 *pp_data = malloc( 1025 ); /* +1 for \0 */
90 while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
93 *pp_data = realloc( *pp_data, *pi_data + 1025 );
99 (*pp_data)[*pi_data] = '\0';
104 /* Parse a directory and recursively add files */
105 int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
108 intf_sys_t *p_sys = p_intf->p_sys;
109 char dir[MAX_DIR_SIZE];
110 #ifdef HAVE_SYS_STAT_H
111 struct stat stat_info;
114 struct dirent *p_dir_content;
119 char *password = NULL;
123 #ifdef HAVE_SYS_STAT_H
124 if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
130 if( ( p_dir = opendir( psz_dir ) ) == NULL )
132 msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
136 i_dirlen = strlen( psz_dir );
137 if( i_dirlen + 10 > MAX_DIR_SIZE )
139 msg_Warn( p_intf, "skipping too deep dir (%s)", psz_dir );
143 msg_Dbg( p_intf, "dir=%s", psz_dir );
145 sprintf( dir, "%s/.access", psz_dir );
146 if( ( file = fopen( dir, "r" ) ) != NULL )
151 msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
153 i_size = fread( line, 1, 1023, file );
157 while( i_size > 0 && ( line[i_size-1] == '\n' ||
158 line[i_size-1] == '\r' ) )
165 p = strchr( line, ':' );
169 user = strdup( line );
170 password = strdup( p );
173 msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
174 user, password, i_size );
179 sprintf( dir, "%s/.hosts", psz_dir );
180 p_acl = ACL_Create( p_intf, VLC_FALSE );
181 if( ACL_LoadFile( p_acl, dir ) )
183 ACL_Destroy( p_acl );
189 /* parse psz_src dir */
190 if( ( p_dir_content = readdir( p_dir ) ) == NULL )
195 if( ( p_dir_content->d_name[0] == '.' )
196 || ( i_dirlen + strlen( p_dir_content->d_name ) > MAX_DIR_SIZE ) )
199 sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
200 if( E_(ParseDirectory)( p_intf, psz_root, dir ) )
202 httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );
210 psz_tmp = vlc_fix_readdir_charset( p_intf, dir );
211 f->file = E_(FromUTF8)( p_intf, psz_tmp );
213 psz_tmp = vlc_fix_readdir_charset( p_intf,
214 &dir[strlen( psz_root )] );
215 f->name = E_(FileToUrl)( psz_tmp, &b_index );
217 f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
221 msg_Err( p_intf , "unable to parse directory" );
224 return( VLC_ENOMEM );
226 msg_Dbg( p_intf, "file=%s (url=%s)",
229 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
231 f->b_html ? p_sys->psz_html_type : NULL,
232 user, password, p_acl,
233 E_(HttpCallback), f );
237 TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
239 /* for url that ends by / add
240 * - a redirect from rep to rep/
241 * - in case of index.* rep/index.html to rep/ */
242 if( f && f->name[strlen(f->name) - 1] == '/' )
244 char *psz_redir = strdup( f->name );
246 psz_redir[strlen( psz_redir ) - 1] = '\0';
248 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
249 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
252 if( b_index && ( p = strstr( f->file, "index." ) ) )
254 asprintf( &psz_redir, "%s%s", f->name, p );
256 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
257 f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
258 f->name, psz_redir );
275 ACL_Destroy( p_acl );
282 /**************************************************************************
284 **************************************************************************/
285 char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 )
287 intf_sys_t *p_sys = p_intf->p_sys;
289 if ( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
291 char *psz_in = psz_utf8;
292 size_t i_in = strlen(psz_in);
293 size_t i_out = i_in * 2;
294 char *psz_local = malloc(i_out + 1);
295 char *psz_out = psz_local;
297 size_t i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
299 if( i_ret == (size_t)-1 || i_in )
302 "failed to convert \"%s\" to desired charset (%s)",
303 psz_utf8, strerror(errno) );
305 return strdup( psz_utf8 );
312 return strdup( psz_utf8 );
315 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
317 intf_sys_t *p_sys = p_intf->p_sys;
319 if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
321 char *psz_in = psz_local;
322 size_t i_in = strlen(psz_in);
323 size_t i_out = i_in * 6;
324 char *psz_utf8 = malloc(i_out + 1);
325 char *psz_out = psz_utf8;
327 size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
329 if( i_ret == (size_t)-1 || i_in )
332 "failed to convert \"%s\" to desired charset (%s)",
333 psz_local, strerror(errno) );
335 return strdup( psz_local );
342 return strdup( psz_local );
345 /*************************************************************************
347 *************************************************************************/
348 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
349 playlist_item_t *p_node, char *name, mvar_t *s,
354 if( p_node->i_children == -1 )
358 mvar_t *itm = mvar_New( name, "set" );
360 sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
361 mvar_AppendNewVar( itm, "current", value );
363 sprintf( value, "%d", p_node->input.i_id );
364 mvar_AppendNewVar( itm, "index", value );
366 psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
367 mvar_AppendNewVar( itm, "name", psz );
370 psz = E_(FromUTF8)( p_intf, p_node->input.psz_uri );
371 mvar_AppendNewVar( itm, "uri", psz );
374 sprintf( value, "Item");
375 mvar_AppendNewVar( itm, "type", value );
377 sprintf( value, "%d", i_depth );
378 mvar_AppendNewVar( itm, "depth", value );
380 mvar_AppendVar( s, itm );
387 mvar_t *itm = mvar_New( name, "set" );
389 psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
390 mvar_AppendNewVar( itm, "name", psz );
391 mvar_AppendNewVar( itm, "uri", psz );
394 sprintf( value, "Node" );
395 mvar_AppendNewVar( itm, "type", value );
397 sprintf( value, "%d", p_node->input.i_id );
398 mvar_AppendNewVar( itm, "index", value );
400 sprintf( value, "%d", p_node->i_children);
401 mvar_AppendNewVar( itm, "i_children", value );
403 sprintf( value, "%d", i_depth );
404 mvar_AppendNewVar( itm, "depth", value );
406 mvar_AppendVar( s, itm );
408 for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
409 E_(PlaylistListNode)( p_intf, p_pl,
410 p_node->pp_children[i_child],
411 name, s, i_depth + 1);
417 /****************************************************************************
418 * Seek command parsing handling
419 ****************************************************************************/
421 void E_(Seek)( intf_thread_t *p_intf, char *p_value )
423 intf_sys_t *p_sys = p_intf->p_sys;
429 #define POSITION_ABSOLUTE 12
430 #define POSITION_REL_FOR 13
431 #define POSITION_REL_BACK 11
432 #define VL_TIME_ABSOLUTE 0
433 #define VL_TIME_REL_FOR 1
434 #define VL_TIME_REL_BACK -1
437 var_Get( p_sys->p_input, "length", &val );
438 i_length = val.i_time;
440 while( p_value[0] != '\0' )
446 i_relative = VL_TIME_REL_FOR;
452 i_relative = VL_TIME_REL_BACK;
456 case '0': case '1': case '2': case '3': case '4':
457 case '5': case '6': case '7': case '8': case '9':
459 i_stock = strtol( p_value , &p_value , 10 );
462 case '%': /* for percentage ie position */
464 i_relative += POSITION_ABSOLUTE;
472 i_value = 60 * (i_value + i_stock) ;
477 case 'h': case 'H': /* hours */
479 i_value += 3600 * i_stock;
481 /* other characters which are not numbers are not important */
482 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
488 case 'm': case 'M': case '\'': /* minutes */
490 i_value += 60 * i_stock;
493 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
499 case 's': case 'S': case '"': /* seconds */
503 while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
517 /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
522 case VL_TIME_ABSOLUTE:
524 if( (uint64_t)( i_value ) * 1000000 <= i_length )
525 val.i_time = (uint64_t)( i_value ) * 1000000;
527 val.i_time = i_length;
529 var_Set( p_sys->p_input, "time", val );
530 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
533 case VL_TIME_REL_FOR:
535 var_Get( p_sys->p_input, "time", &val );
536 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
538 val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
541 val.i_time = i_length;
543 var_Set( p_sys->p_input, "time", val );
544 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
547 case VL_TIME_REL_BACK:
549 var_Get( p_sys->p_input, "time", &val );
550 if( (int64_t)( i_value ) * 1000000 > val.i_time )
555 val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
557 var_Set( p_sys->p_input, "time", val );
558 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
561 case POSITION_ABSOLUTE:
563 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
564 var_Set( p_sys->p_input, "position", val );
565 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
568 case POSITION_REL_FOR:
570 var_Get( p_sys->p_input, "position", &val );
571 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
572 var_Set( p_sys->p_input, "position", val );
573 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
576 case POSITION_REL_BACK:
578 var_Get( p_sys->p_input, "position", &val );
579 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
580 var_Set( p_sys->p_input, "position", val );
581 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
586 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
591 #undef POSITION_ABSOLUTE
592 #undef POSITION_REL_FOR
593 #undef POSITION_REL_BACK
594 #undef VL_TIME_ABSOLUTE
595 #undef VL_TIME_REL_FOR
596 #undef VL_TIME_REL_BACK
600 /****************************************************************************
601 * URI Parsing functions
602 ****************************************************************************/
603 int E_(uri_test_param)( char *psz_uri, const char *psz_name )
607 while( (p = strstr( p, psz_name )) )
609 /* Verify that we are dealing with a post/get argument */
610 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
611 && p[strlen(psz_name)] == '=' )
620 char *E_(uri_extract_value)( char *psz_uri, const char *psz_name,
621 char *psz_value, int i_value_max )
625 while( (p = strstr( p, psz_name )) )
627 /* Verify that we are dealing with a post/get argument */
628 if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
629 && p[strlen(psz_name)] == '=' )
638 p += strlen( psz_name );
641 if( strchr( p, '&' ) )
643 i_len = strchr( p, '&' ) - p;
647 /* for POST method */
648 if( strchr( p, '\n' ) )
650 i_len = strchr( p, '\n' ) - p;
651 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
658 i_len = __MIN( i_value_max - 1, i_len );
661 strncpy( psz_value, p, i_len );
662 psz_value[i_len] = '\0';
666 strncpy( psz_value, "", i_value_max );
672 strncpy( psz_value, "", i_value_max );
678 void E_(uri_decode_url_encoded)( char *psz )
680 char *dup = strdup( psz );
698 *psz++ = strtol( val, NULL, 16 );
714 /* Since the resulting string is smaller we can work in place, so it is
715 * permitted to have psz == new. new points to the first word of the
716 * string, the function returns the remaining string. */
717 char *E_(FirstWord)( char *psz, char *new )
724 while( *psz != '\0' && *psz != ' ' )
729 while( *psz != '\0' && *psz != c )
731 if( *psz == '\\' && psz[1] != '\0' )
740 if( *psz == '\\' && psz[1] != '\0' )
754 /**********************************************************************
755 * parse_MRL: parse the MRL, find the mrl string and the options,
756 * create an item with all information in it, and return the item.
757 * return NULL if there is an error.
758 **********************************************************************/
759 playlist_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *_psz,
762 char *psz = strdup( _psz );
765 playlist_item_t * p_item = NULL;
767 /* extract the mrl */
768 s_temp = E_(FirstWord)( s_mrl, s_mrl );
771 s_temp = s_mrl + strlen( s_mrl );
774 p_item = playlist_ItemNew( p_intf, s_mrl, psz_name );
777 /* now we can take care of the options */
778 while( *s_mrl != '\0' )
780 s_temp = E_(FirstWord)( s_mrl, s_mrl );
785 s_temp = s_mrl + strlen( s_mrl );
788 msg_Warn( p_intf, "invalid MRL option: %s", s_mrl );
790 playlist_ItemAddOption( p_item, s_mrl );