1
/*****************************************************************************
2
* util.c : Utility functions for HTTP interface
3
*****************************************************************************
4
* Copyright (C) 2001-2005 the VideoLAN team
5
* $Id: 565c100b1120c3d630a89c8a889dc4e65477b3b7 $
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>
38
/****************************************************************************
39
* File and directory functions
40
****************************************************************************/
42
/* ToUrl: create a good name for an url from filename */
43
static char *FileToUrl( const char *name, bool *pb_index )
47
char *url = malloc( strlen( name ) + 2 );
48
if( unlikely(url == NULL) )
51
#if (DIR_SEP_CHAR == '/')
52
name += strspn( name, "/" );
54
name += strspn( name, "/"DIR_SEP );
57
strcpy( url + 1, name );
59
#if (DIR_SEP_CHAR != '/')
60
/* convert '\\' into '/' */
61
for( char *ptr = url; *ptr; ptr++ )
62
if( *ptr == DIR_SEP_CHAR )
67
char *p = strrchr( url, '/' );
68
if( p != NULL && !strncmp( p, "/index.", 7 ) )
77
int FileLoad( FILE *f, char **pp_data, int *pi_data )
81
/* just load the file */
83
*pp_data = xmalloc( 1025 ); /* +1 for \0 */
85
while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
88
*pp_data = xrealloc( *pp_data, *pi_data + 1025 );
94
(*pp_data)[*pi_data] = '\0';
99
/* Parse a directory and recursively add files */
100
int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
103
intf_sys_t *p_sys = p_intf->p_sys;
104
char dir[MAX_DIR_SIZE];
110
char *password = NULL;
114
if( ( p_dir = vlc_opendir( psz_dir ) ) == NULL )
116
if( errno != ENOENT && errno != ENOTDIR )
117
msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
121
i_dirlen = strlen( psz_dir );
122
if( i_dirlen + 10 > MAX_DIR_SIZE )
124
msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
129
msg_Dbg( p_intf, "dir=%s", psz_dir );
131
snprintf( dir, sizeof( dir ), "%s"DIR_SEP".access", psz_dir );
132
if( ( file = vlc_fopen( dir, "r" ) ) != NULL )
137
msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
139
i_size = fread( line, 1, 1023, file );
143
while( i_size > 0 && ( line[i_size-1] == '\n' ||
144
line[i_size-1] == '\r' ) )
151
p = strchr( line, ':' );
155
user = strdup( line );
156
password = strdup( p );
159
msg_Dbg( p_intf, "using user=%s (read=%d)", user, i_size );
164
snprintf( dir, sizeof( dir ), "%s"DIR_SEP".hosts", psz_dir );
165
p_acl = ACL_Create( p_intf, false );
166
if( ACL_LoadFile( p_acl, dir ) )
168
ACL_Destroy( p_acl );
171
if( vlc_stat( dir, &st ) == 0 )
184
/* parse psz_src dir */
185
if( ( psz_filename = vlc_readdir( p_dir ) ) == NULL )
190
if( ( psz_filename[0] == '.' )
191
|| ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
193
free( psz_filename );
197
snprintf( dir, sizeof( dir ), "%s"DIR_SEP"%s", psz_dir, psz_filename );
198
free( psz_filename );
200
if( ParseDirectory( p_intf, psz_root, dir ) )
202
httpd_file_sys_t *f = NULL;
203
httpd_handler_sys_t *h = NULL;
205
char *psz_name, *psz_ext;
207
psz_name = FileToUrl( &dir[strlen( psz_root )], &b_index );
208
psz_ext = strrchr( dir, '.' );
209
if( psz_ext != NULL )
213
for( i = 0; i < p_sys->i_handlers; i++ )
214
if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
216
if( i < p_sys->i_handlers )
218
f = malloc( sizeof( httpd_handler_sys_t ) );
219
h = (httpd_handler_sys_t *)f;
221
h->p_association = p_sys->pp_handlers[i];
226
f = xmalloc( sizeof( httpd_file_sys_t ) );
227
f->b_handler = false;
234
f->file = strdup (dir);
236
f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? true : false;
240
msg_Err( p_intf , "unable to parse directory" );
243
return( VLC_ENOMEM );
245
msg_Dbg( p_intf, "file=%s (url=%s)",
250
char *psz_type = strdup( "text/html; charset=UTF-8" );
251
if( strstr( &dir[strlen( psz_root )], ".xml" ) )
253
char *psz = strstr( psz_type, "html;" );
263
f->p_file = httpd_FileNew( p_sys->p_httpd_host,
265
f->b_html ? psz_type : NULL,
266
user, password, p_acl,
269
if( f->p_file != NULL )
271
TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
276
h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
278
user, password, p_acl,
279
HandlerCallback, h );
280
if( h->p_handler != NULL )
282
TAB_APPEND( p_sys->i_files, p_sys->pp_files,
283
(httpd_file_sys_t *)h );
287
/* for url that ends by / add
288
* - a redirect from rep to rep/
289
* - in case of index.* rep/index.html to rep/ */
290
if( f && f->name[strlen(f->name) - 1] == '/' )
292
char *psz_redir = strdup( f->name );
294
psz_redir[strlen( psz_redir ) - 1] = '\0';
296
msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
297
f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
300
if( b_index && ( p = strstr( f->file, "index." ) ) )
302
if( asprintf( &psz_redir, "%s%s", f->name, p ) != -1 )
304
msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
305
f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
306
f->name, psz_redir );
318
ACL_Destroy( p_acl );
325
/*************************************************************************
327
*************************************************************************/
328
void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl,
329
playlist_item_t *p_node, char *name, mvar_t *s,
332
if( !p_node || !p_node->p_input )
335
if( p_node->i_children == -1 )
339
playlist_item_t * p_item = playlist_CurrentPlayingItem( p_pl );
340
if( !p_item || !p_item->p_input )
343
mvar_t *itm = mvar_New( name, "set" );
344
if( p_item->p_input == p_node->p_input )
345
mvar_AppendNewVar( itm, "current", "1" );
347
mvar_AppendNewVar( itm, "current", "0" );
349
sprintf( value, "%d", p_node->i_id );
350
mvar_AppendNewVar( itm, "index", value );
352
psz = input_item_GetName( p_node->p_input );
353
mvar_AppendNewVar( itm, "name", psz );
356
psz = input_item_GetURI( p_node->p_input );
357
mvar_AppendNewVar( itm, "uri", psz );
360
mvar_AppendNewVar( itm, "type", "Item" );
362
sprintf( value, "%d", i_depth );
363
mvar_AppendNewVar( itm, "depth", value );
365
if( p_node->i_flags & PLAYLIST_RO_FLAG )
366
mvar_AppendNewVar( itm, "ro", "ro" );
368
mvar_AppendNewVar( itm, "ro", "rw" );
370
sprintf( value, "%"PRId64, input_item_GetDuration( p_node->p_input ) );
371
mvar_AppendNewVar( itm, "duration", value );
373
//Adding extra meta-information to each playlist item
375
psz = input_item_GetTitle( p_node->p_input );
376
mvar_AppendNewVar( itm, "title", psz );
379
psz = input_item_GetArtist( p_node->p_input );
380
mvar_AppendNewVar( itm, "artist", psz );
383
psz = input_item_GetGenre( p_node->p_input );
384
mvar_AppendNewVar( itm, "genre", psz );
387
psz = input_item_GetCopyright( p_node->p_input );
388
mvar_AppendNewVar( itm, "copyright", psz );
391
psz = input_item_GetAlbum( p_node->p_input );
392
mvar_AppendNewVar( itm, "album", psz );
395
psz = input_item_GetTrackNum( p_node->p_input );
396
mvar_AppendNewVar( itm, "track", psz );
399
psz = input_item_GetDescription( p_node->p_input );
400
mvar_AppendNewVar( itm, "description", psz );
403
psz = input_item_GetRating( p_node->p_input );
404
mvar_AppendNewVar( itm, "rating", psz );
407
psz = input_item_GetDate( p_node->p_input );
408
mvar_AppendNewVar( itm, "date", psz );
411
psz = input_item_GetURL( p_node->p_input );
412
mvar_AppendNewVar( itm, "url", psz );
415
psz = input_item_GetLanguage( p_node->p_input );
416
mvar_AppendNewVar( itm, "language", psz );
419
psz = input_item_GetNowPlaying( p_node->p_input );
420
mvar_AppendNewVar( itm, "now_playing", psz );
423
psz = input_item_GetPublisher( p_node->p_input );
424
mvar_AppendNewVar( itm, "publisher", psz );
427
psz = input_item_GetEncodedBy( p_node->p_input );
428
mvar_AppendNewVar( itm, "encoded_by", psz );
431
psz = input_item_GetArtURL( p_node->p_input );
432
mvar_AppendNewVar( itm, "art_url", psz );
435
psz = input_item_GetTrackID( p_node->p_input );
436
mvar_AppendNewVar( itm, "track_id", psz );
439
mvar_AppendVar( s, itm );
445
mvar_t *itm = mvar_New( name, "set" );
447
mvar_AppendNewVar( itm, "name", p_node->p_input->psz_name );
448
mvar_AppendNewVar( itm, "uri", p_node->p_input->psz_name );
450
mvar_AppendNewVar( itm, "type", "Node" );
452
sprintf( value, "%d", p_node->i_id );
453
mvar_AppendNewVar( itm, "index", value );
455
sprintf( value, "%d", p_node->i_children);
456
mvar_AppendNewVar( itm, "i_children", value );
458
sprintf( value, "%d", i_depth );
459
mvar_AppendNewVar( itm, "depth", value );
461
if( p_node->i_flags & PLAYLIST_RO_FLAG )
462
mvar_AppendNewVar( itm, "ro", "ro" );
464
mvar_AppendNewVar( itm, "ro", "rw" );
466
mvar_AppendVar( s, itm );
468
for( i_child = 0 ; i_child < p_node->i_children ; i_child++ )
469
PlaylistListNode( p_intf, p_pl, p_node->pp_children[i_child],
470
name, s, i_depth + 1);
474
/****************************************************************************
475
* Seek command parsing handling
476
****************************************************************************/
477
void HandleSeek( intf_thread_t *p_intf, char *p_value )
479
intf_sys_t *p_sys = p_intf->p_sys;
485
#define POSITION_ABSOLUTE 12
486
#define POSITION_REL_FOR 13
487
#define POSITION_REL_BACK 11
488
#define VL_TIME_ABSOLUTE 0
489
#define VL_TIME_REL_FOR 1
490
#define VL_TIME_REL_BACK -1
493
var_Get( p_sys->p_input, "length", &val );
494
i_length = val.i_time;
496
while( p_value[0] != '\0' )
502
i_relative = VL_TIME_REL_FOR;
508
i_relative = VL_TIME_REL_BACK;
512
case '0': case '1': case '2': case '3': case '4':
513
case '5': case '6': case '7': case '8': case '9':
515
i_stock = strtol( p_value , &p_value , 10 );
518
case '%': /* for percentage ie position */
520
i_relative += POSITION_ABSOLUTE;
528
i_value = 60 * (i_value + i_stock) ;
533
case 'h': case 'H': /* hours */
535
i_value += 3600 * i_stock;
537
/* other characters which are not numbers are not
539
while( ((p_value[0] < '0') || (p_value[0] > '9'))
540
&& (p_value[0] != '\0') )
546
case 'm': case 'M': case '\'': /* minutes */
548
i_value += 60 * i_stock;
551
while( ((p_value[0] < '0') || (p_value[0] > '9'))
552
&& (p_value[0] != '\0') )
558
case 's': case 'S': case '"': /* seconds */
562
while( ((p_value[0] < '0') || (p_value[0] > '9'))
563
&& (p_value[0] != '\0') )
577
/* if there is no known symbol, I consider it as seconds.
578
* Otherwise, i_stock = 0 */
583
case VL_TIME_ABSOLUTE:
585
if( (uint64_t)( i_value ) * 1000000 <= i_length )
586
val.i_time = (uint64_t)( i_value ) * 1000000;
588
val.i_time = i_length;
590
var_Set( p_sys->p_input, "time", val );
591
msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
594
case VL_TIME_REL_FOR:
596
var_Get( p_sys->p_input, "time", &val );
597
if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
599
val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
602
val.i_time = i_length;
604
var_Set( p_sys->p_input, "time", val );
605
msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
608
case VL_TIME_REL_BACK:
610
var_Get( p_sys->p_input, "time", &val );
611
if( (int64_t)( i_value ) * 1000000 > val.i_time )
616
val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
618
var_Set( p_sys->p_input, "time", val );
619
msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
622
case POSITION_ABSOLUTE:
624
val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
626
var_Set( p_sys->p_input, "position", val );
627
msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
630
case POSITION_REL_FOR:
632
var_Get( p_sys->p_input, "position", &val );
633
val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
635
var_Set( p_sys->p_input, "position", val );
636
msg_Dbg( p_intf, "requested seek percent forward: %d%%",
640
case POSITION_REL_BACK:
642
var_Get( p_sys->p_input, "position", &val );
643
val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
645
var_Set( p_sys->p_input, "position", val );
646
msg_Dbg( p_intf, "requested seek percent backward: %d%%",
652
msg_Dbg( p_intf, "invalid seek request" );
657
#undef POSITION_ABSOLUTE
658
#undef POSITION_REL_FOR
659
#undef POSITION_REL_BACK
660
#undef VL_TIME_ABSOLUTE
661
#undef VL_TIME_REL_FOR
662
#undef VL_TIME_REL_BACK
666
/****************************************************************************
667
* URI Parsing functions
668
****************************************************************************/
669
int TestURIParam( char *psz_uri, const char *psz_name )
673
while( (p = strstr( p, psz_name )) )
675
/* Verify that we are dealing with a post/get argument */
676
if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
677
&& p[strlen(psz_name)] == '=' )
687
static const char *FindURIValue( const char *psz_uri, const char *restrict psz_name,
688
size_t *restrict p_len )
690
const char *p = psz_uri, *end;
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)] == '=' )
708
p += strlen( psz_name );
711
if( ( end = strchr( p, '\n' ) ) != NULL )
714
if( ( end > p ) && ( end[-1] == '\r' ) )
722
if( ( end = strchr( p, '&' ) ) != NULL )
732
const char *ExtractURIValue( const char *restrict psz_uri,
733
const char *restrict psz_name,
734
char *restrict psz_buf, size_t bufsize )
737
const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
738
const char *psz_next;
740
if( psz_value == NULL )
747
psz_next = psz_value + len;
753
strncpy( psz_buf, psz_value, len );
760
char *ExtractURIString( const char *restrict psz_uri,
761
const char *restrict psz_name )
764
const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
766
if( psz_value == NULL )
769
char *res = malloc( len + 1 );
773
memcpy( res, psz_value, len );
779
/* Since the resulting string is smaller we can work in place, so it is
780
* permitted to have psz == new. new points to the first word of the
781
* string, the function returns the remaining string. */
782
char *FirstWord( char *psz, char *new )
789
while( *psz != '\0' && *psz != ' ' )
794
while( *psz != '\0' && *psz != c )
796
if( *psz == '\\' && psz[1] != '\0' )
805
if( *psz == '\\' && psz[1] != '\0' )
819
/**********************************************************************
820
* MRLParse: parse the MRL, find the MRL string and the options,
821
* create an item with all information in it, and return the item.
822
* return NULL if there is an error.
823
**********************************************************************/
825
/* Function analog to FirstWord except that it relies on colon instead
826
* of space to delimit option boundaries. */
827
static char *FirstOption( char *psz, char *new )
829
bool b_end, b_start = true;
834
while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
839
while( *psz != '\0' && *psz != c )
841
if( *psz == '\\' && psz[1] != '\0' )
851
if( *psz == '\\' && psz[1] != '\0' )
860
while (new[-1] == ' ')
870
input_item_t *MRLParse( intf_thread_t *p_intf, const char *mrl,
873
char *psz = strdup( mrl ), *s_mrl = psz, *s_temp;
876
/* extract the mrl */
877
s_temp = FirstOption( s_mrl, s_mrl );
880
s_temp = s_mrl + strlen( s_mrl );
883
input_item_t *p_input = input_item_New( p_intf, s_mrl, psz_name );
884
if( p_input == NULL )
888
/* now we can take care of the options */
889
while ( *s_mrl != '\0' )
891
s_temp = FirstOption( s_mrl, s_mrl );
896
s_temp = s_mrl + strlen( s_mrl );
898
input_item_AddOption( p_input, s_mrl, VLC_INPUT_OPTION_TRUSTED );
905
/**********************************************************************
906
* RealPath: parse ../, ~ and path stuff
907
**********************************************************************/
908
char *RealPath( const char *psz_src )
912
int i_len = strlen(psz_src);
914
psz_dir = xmalloc( i_len + 2 );
915
strcpy( psz_dir, psz_src );
917
/* Add a trailing sep to ease the .. step */
918
psz_dir[i_len] = DIR_SEP_CHAR;
919
psz_dir[i_len + 1] = '\0';
921
#if (DIR_SEP_CHAR != '/')
922
/* Convert all / to native separator */
924
while( (p = strchr( p, '/' )) != NULL )
930
/* FIXME: this could be O(N) rather than O(N²)... */
931
/* Remove multiple separators and /./ */
933
while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
935
if( p[1] == DIR_SEP_CHAR )
936
memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
937
else if( p[1] == '.' && p[2] == DIR_SEP_CHAR )
938
memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
943
if( psz_dir[0] == '~' )
945
char *home = config_GetUserDir( VLC_HOME_DIR ), *dir;
946
if( asprintf( &dir, "%s%s", home, psz_dir + 1 ) != -1 )
954
if( strlen(psz_dir) > 2 )
958
while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
960
if( p[-1] == '.' && p[-2] == '.' && p[-3] == DIR_SEP_CHAR )
964
if( (q = strrchr( psz_dir, DIR_SEP_CHAR )) != NULL )
966
memmove( q + 1, p + 1, strlen(p + 1) + 1 );
971
memmove( psz_dir, p, strlen(p) + 1 );
980
/* Remove trailing sep if there are at least 2 sep in the string
981
* (handles the C:\ stuff) */
982
p = strrchr( psz_dir, DIR_SEP_CHAR );
983
if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, DIR_SEP_CHAR ) )