]> git.sesse.net Git - vlc/blob - modules/control/http/util.c
Fix oldhttp build : needs struct stat / S_ISDIR
[vlc] / modules / control / http / util.c
1 /*****************************************************************************
2  * util.c : Utility functions for HTTP interface
3  *****************************************************************************
4  * Copyright (C) 2001-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include "http.h"
32 #include <vlc_strings.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <sys/stat.h>
36
37 /****************************************************************************
38  * File and directory functions
39  ****************************************************************************/
40
41 /* ToUrl: create a good name for an url from filename */
42 char *FileToUrl( char *name, bool *pb_index )
43 {
44     char *url, *p;
45
46     url = p = malloc( strlen( name ) + 1 );
47
48     *pb_index = false;
49     if( !url || !p )
50     {
51         return NULL;
52     }
53
54 #ifdef WIN32
55     while( *name == '\\' || *name == '/' )
56 #else
57     while( *name == '/' )
58 #endif
59     {
60         name++;
61     }
62
63     *p++ = '/';
64     strcpy( p, name );
65
66 #ifdef WIN32
67     /* convert '\\' into '/' */
68     name = p;
69     while( *name )
70     {
71         if( *name == '\\' )
72             *name = '/';
73         name++;
74     }
75 #endif
76
77     /* index.* -> / */
78     if( ( p = strrchr( url, '/' ) ) != NULL )
79     {
80         if( !strncmp( p, "/index.", 7 ) )
81         {
82             p[1] = '\0';
83             *pb_index = true;
84         }
85     }
86     return url;
87 }
88
89 /* Load a file */
90 int FileLoad( FILE *f, char **pp_data, int *pi_data )
91 {
92     int i_read;
93
94     /* just load the file */
95     *pi_data = 0;
96     *pp_data = xmalloc( 1025 );  /* +1 for \0 */
97
98     while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
99     {
100         *pi_data += 1024;
101         *pp_data = xrealloc( *pp_data, *pi_data  + 1025 );
102     }
103     if( i_read > 0 )
104     {
105         *pi_data += i_read;
106     }
107     (*pp_data)[*pi_data] = '\0';
108
109     return VLC_SUCCESS;
110 }
111
112 /* Parse a directory and recursively add files */
113 int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
114                         char *psz_dir )
115 {
116     intf_sys_t     *p_sys = p_intf->p_sys;
117     char           dir[MAX_DIR_SIZE];
118     DIR           *p_dir;
119     vlc_acl_t     *p_acl;
120     FILE          *file;
121
122     char          *user = NULL;
123     char          *password = NULL;
124
125     int           i_dirlen;
126
127     if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
128     {
129         if( errno != ENOENT && errno != ENOTDIR )
130             msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
131         return VLC_EGENERIC;
132     }
133
134     i_dirlen = strlen( psz_dir );
135     if( i_dirlen + 10 > MAX_DIR_SIZE )
136     {
137         msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
138         closedir( p_dir );
139         return 0;
140     }
141
142     msg_Dbg( p_intf, "dir=%s", psz_dir );
143
144     snprintf( dir, sizeof( dir ), "%s"DIR_SEP".access", psz_dir );
145     if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
146     {
147         char line[1024];
148         int  i_size;
149
150         msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
151
152         i_size = fread( line, 1, 1023, file );
153         if( i_size > 0 )
154         {
155             char *p;
156             while( i_size > 0 && ( line[i_size-1] == '\n' ||
157                    line[i_size-1] == '\r' ) )
158             {
159                 i_size--;
160             }
161
162             line[i_size] = '\0';
163
164             p = strchr( line, ':' );
165             if( p )
166             {
167                 *p++ = '\0';
168                 user = strdup( line );
169                 password = strdup( p );
170             }
171         }
172         msg_Dbg( p_intf, "using user=%s (read=%d)", user, i_size );
173
174         fclose( file );
175     }
176
177     snprintf( dir, sizeof( dir ), "%s"DIR_SEP".hosts", psz_dir );
178     p_acl = ACL_Create( p_intf, false );
179     if( ACL_LoadFile( p_acl, dir ) )
180     {
181         ACL_Destroy( p_acl );
182
183         struct stat st;
184         if( utf8_stat( dir, &st ) == 0 )
185         {
186             free( user );
187             free( password );
188             closedir( p_dir );
189             return VLC_EGENERIC;
190         }
191         p_acl = NULL;
192     }
193
194     for( ;; )
195     {
196         char *psz_filename;
197         /* parse psz_src dir */
198         if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
199         {
200             break;
201         }
202
203         if( ( psz_filename[0] == '.' )
204          || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
205         {
206             free( psz_filename );
207             continue;
208         }
209
210         snprintf( dir, sizeof( dir ), "%s"DIR_SEP"%s", psz_dir, psz_filename );
211         free( psz_filename );
212
213         if( ParseDirectory( p_intf, psz_root, dir ) )
214         {
215             httpd_file_sys_t *f = NULL;
216             httpd_handler_sys_t *h = NULL;
217             bool b_index;
218             char *psz_name, *psz_ext;
219
220             psz_name = FileToUrl( &dir[strlen( psz_root )], &b_index );
221             psz_ext = strrchr( dir, '.' );
222             if( psz_ext != NULL )
223             {
224                 int i;
225                 psz_ext++;
226                 for( i = 0; i < p_sys->i_handlers; i++ )
227                     if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
228                         break;
229                 if( i < p_sys->i_handlers )
230                 {
231                     f = malloc( sizeof( httpd_handler_sys_t ) );
232                     h = (httpd_handler_sys_t *)f;
233                     f->b_handler = true;
234                     h->p_association = p_sys->pp_handlers[i];
235                 }
236             }
237             if( f == NULL )
238             {
239                 f = xmalloc( sizeof( httpd_file_sys_t ) );
240                 f->b_handler = false;
241             }
242
243             f->p_intf  = p_intf;
244             f->p_file = NULL;
245             f->p_redir = NULL;
246             f->p_redir2 = NULL;
247             f->file = strdup (dir);
248             f->name = psz_name;
249             f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? true : false;
250
251             if( !f->name )
252             {
253                 msg_Err( p_intf , "unable to parse directory" );
254                 closedir( p_dir );
255                 free( f );
256                 return( VLC_ENOMEM );
257             }
258             msg_Dbg( p_intf, "file=%s (url=%s)",
259                      f->file, f->name );
260
261             if( !f->b_handler )
262             {
263                 char *psz_type = strdup( "text/html; charset=UTF-8" );
264                 if( strstr( &dir[strlen( psz_root )], ".xml" ) )
265                 {
266                     char *psz = strstr( psz_type, "html;" );
267                     if( psz )
268                     {
269                         psz[0] = 'x';
270                         psz[1] = 'm';
271                         psz[2] = 'l';
272                         psz[3] = ';';
273                         psz[4] = ' ';
274                     }
275                 }
276                 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
277                                            f->name,
278                                            f->b_html ? psz_type : NULL,
279                                            user, password, p_acl,
280                                            HttpCallback, f );
281                 free( psz_type );
282                 if( f->p_file != NULL )
283                 {
284                     TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
285                 }
286             }
287             else
288             {
289                 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
290                                                  f->name,
291                                                  user, password, p_acl,
292                                                  HandlerCallback, h );
293                 if( h->p_handler != NULL )
294                 {
295                     TAB_APPEND( p_sys->i_files, p_sys->pp_files,
296                                 (httpd_file_sys_t *)h );
297                 }
298             }
299
300             /* for url that ends by / add
301              *  - a redirect from rep to rep/
302              *  - in case of index.* rep/index.html to rep/ */
303             if( f && f->name[strlen(f->name) - 1] == '/' )
304             {
305                 char *psz_redir = strdup( f->name );
306                 char *p;
307                 psz_redir[strlen( psz_redir ) - 1] = '\0';
308
309                 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
310                 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
311                 free( psz_redir );
312
313                 if( b_index && ( p = strstr( f->file, "index." ) ) )
314                 {
315                     if( asprintf( &psz_redir, "%s%s", f->name, p ) != -1 )
316                     {
317                         msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
318                         f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
319                                                          f->name, psz_redir );
320
321                         free( psz_redir );
322                     }
323                 }
324             }
325         }
326     }
327
328     free( user );
329     free( password );
330
331     ACL_Destroy( p_acl );
332     closedir( p_dir );
333
334     return VLC_SUCCESS;
335 }
336
337
338 /*************************************************************************
339  * Playlist stuff
340  *************************************************************************/
341 void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl,
342                            playlist_item_t *p_node, char *name, mvar_t *s,
343                            int i_depth )
344 {
345     if( !p_node || !p_node->p_input )
346         return;
347
348     if( p_node->i_children == -1 )
349     {
350         char value[512];
351         char *psz;
352         playlist_item_t * p_item = playlist_CurrentPlayingItem( p_pl );
353         if( !p_item || !p_item->p_input )
354             return;
355
356         mvar_t *itm = mvar_New( name, "set" );
357         if( p_item->p_input == p_node->p_input )
358             mvar_AppendNewVar( itm, "current", "1" );
359         else
360             mvar_AppendNewVar( itm, "current", "0" );
361
362         sprintf( value, "%d", p_node->i_id );
363         mvar_AppendNewVar( itm, "index", value );
364
365         psz = input_item_GetName( p_node->p_input );
366         mvar_AppendNewVar( itm, "name", psz );
367         free( psz );
368
369         psz = input_item_GetURI( p_node->p_input );
370         mvar_AppendNewVar( itm, "uri", psz );
371         free( psz );
372
373         mvar_AppendNewVar( itm, "type", "Item" );
374
375         sprintf( value, "%d", i_depth );
376         mvar_AppendNewVar( itm, "depth", value );
377
378         if( p_node->i_flags & PLAYLIST_RO_FLAG )
379             mvar_AppendNewVar( itm, "ro", "ro" );
380         else
381             mvar_AppendNewVar( itm, "ro", "rw" );
382
383         sprintf( value, "%"PRId64, input_item_GetDuration( p_node->p_input ) );
384         mvar_AppendNewVar( itm, "duration", value );
385
386         //Adding extra meta-information to each playlist item
387
388         psz = input_item_GetTitle( p_node->p_input );
389         mvar_AppendNewVar( itm, "title", psz );
390         free( psz );
391
392         psz = input_item_GetArtist( p_node->p_input );
393         mvar_AppendNewVar( itm, "artist", psz );
394         free( psz );
395
396         psz = input_item_GetGenre( p_node->p_input );
397         mvar_AppendNewVar( itm, "genre", psz );
398         free( psz );
399
400         psz = input_item_GetCopyright( p_node->p_input );
401         mvar_AppendNewVar( itm, "copyright", psz );
402         free( psz );
403
404         psz = input_item_GetAlbum( p_node->p_input );
405         mvar_AppendNewVar( itm, "album", psz );
406         free( psz );
407
408         psz = input_item_GetTrackNum( p_node->p_input );
409         mvar_AppendNewVar( itm, "track", psz );
410         free( psz );
411
412         psz = input_item_GetDescription( p_node->p_input );
413         mvar_AppendNewVar( itm, "description", psz );
414         free( psz );
415
416         psz = input_item_GetRating( p_node->p_input );
417         mvar_AppendNewVar( itm, "rating", psz );
418         free( psz );
419
420         psz = input_item_GetDate( p_node->p_input );
421         mvar_AppendNewVar( itm, "date", psz );
422         free( psz );
423
424         psz = input_item_GetURL( p_node->p_input );
425         mvar_AppendNewVar( itm, "url", psz );
426         free( psz );
427
428         psz = input_item_GetLanguage( p_node->p_input );
429         mvar_AppendNewVar( itm, "language", psz );
430         free( psz );
431
432         psz = input_item_GetNowPlaying( p_node->p_input );
433         mvar_AppendNewVar( itm, "now_playing", psz );
434         free( psz );
435
436         psz = input_item_GetPublisher( p_node->p_input );
437         mvar_AppendNewVar( itm, "publisher", psz );
438         free( psz );
439
440         psz = input_item_GetEncodedBy( p_node->p_input );
441         mvar_AppendNewVar( itm, "encoded_by", psz );
442         free( psz );
443
444         psz = input_item_GetArtURL( p_node->p_input );
445         mvar_AppendNewVar( itm, "art_url", psz );
446         free( psz );
447
448         psz = input_item_GetTrackID( p_node->p_input );
449         mvar_AppendNewVar( itm, "track_id", psz );
450         free( psz );
451
452         mvar_AppendVar( s, itm );
453     }
454     else
455     {
456         char value[512];
457         int i_child;
458         mvar_t *itm = mvar_New( name, "set" );
459
460         mvar_AppendNewVar( itm, "name", p_node->p_input->psz_name );
461         mvar_AppendNewVar( itm, "uri", p_node->p_input->psz_name );
462
463         mvar_AppendNewVar( itm, "type", "Node" );
464
465         sprintf( value, "%d", p_node->i_id );
466         mvar_AppendNewVar( itm, "index", value );
467
468         sprintf( value, "%d", p_node->i_children);
469         mvar_AppendNewVar( itm, "i_children", value );
470
471         sprintf( value, "%d", i_depth );
472         mvar_AppendNewVar( itm, "depth", value );
473
474         if( p_node->i_flags & PLAYLIST_RO_FLAG )
475             mvar_AppendNewVar( itm, "ro", "ro" );
476         else
477             mvar_AppendNewVar( itm, "ro", "rw" );
478
479         mvar_AppendVar( s, itm );
480
481         for( i_child = 0 ; i_child < p_node->i_children ; i_child++ )
482              PlaylistListNode( p_intf, p_pl, p_node->pp_children[i_child],
483                                name, s, i_depth + 1);
484     }
485 }
486
487 /****************************************************************************
488  * Seek command parsing handling
489  ****************************************************************************/
490 void HandleSeek( intf_thread_t *p_intf, char *p_value )
491 {
492     intf_sys_t     *p_sys = p_intf->p_sys;
493     vlc_value_t val;
494     int i_stock = 0;
495     uint64_t i_length;
496     int i_value = 0;
497     int i_relative = 0;
498 #define POSITION_ABSOLUTE 12
499 #define POSITION_REL_FOR 13
500 #define POSITION_REL_BACK 11
501 #define VL_TIME_ABSOLUTE 0
502 #define VL_TIME_REL_FOR 1
503 #define VL_TIME_REL_BACK -1
504     if( p_sys->p_input )
505     {
506         var_Get( p_sys->p_input, "length", &val );
507         i_length = val.i_time;
508
509         while( p_value[0] != '\0' )
510         {
511             switch(p_value[0])
512             {
513                 case '+':
514                 {
515                     i_relative = VL_TIME_REL_FOR;
516                     p_value++;
517                     break;
518                 }
519                 case '-':
520                 {
521                     i_relative = VL_TIME_REL_BACK;
522                     p_value++;
523                     break;
524                 }
525                 case '0': case '1': case '2': case '3': case '4':
526                 case '5': case '6': case '7': case '8': case '9':
527                 {
528                     i_stock = strtol( p_value , &p_value , 10 );
529                     break;
530                 }
531                 case '%': /* for percentage ie position */
532                 {
533                     i_relative += POSITION_ABSOLUTE;
534                     i_value = i_stock;
535                     i_stock = 0;
536                     p_value[0] = '\0';
537                     break;
538                 }
539                 case ':':
540                 {
541                     i_value = 60 * (i_value + i_stock) ;
542                     i_stock = 0;
543                     p_value++;
544                     break;
545                 }
546                 case 'h': case 'H': /* hours */
547                 {
548                     i_value += 3600 * i_stock;
549                     i_stock = 0;
550                     /* other characters which are not numbers are not
551                      * important */
552                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
553                            && (p_value[0] != '\0') )
554                     {
555                         p_value++;
556                     }
557                     break;
558                 }
559                 case 'm': case 'M': case '\'': /* minutes */
560                 {
561                     i_value += 60 * i_stock;
562                     i_stock = 0;
563                     p_value++;
564                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
565                            && (p_value[0] != '\0') )
566                     {
567                         p_value++;
568                     }
569                     break;
570                 }
571                 case 's': case 'S': case '"':  /* seconds */
572                 {
573                     i_value += i_stock;
574                     i_stock = 0;
575                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
576                            && (p_value[0] != '\0') )
577                     {
578                         p_value++;
579                     }
580                     break;
581                 }
582                 default:
583                 {
584                     p_value++;
585                     break;
586                 }
587             }
588         }
589
590         /* if there is no known symbol, I consider it as seconds.
591          * Otherwise, i_stock = 0 */
592         i_value += i_stock;
593
594         switch(i_relative)
595         {
596             case VL_TIME_ABSOLUTE:
597             {
598                 if( (uint64_t)( i_value ) * 1000000 <= i_length )
599                     val.i_time = (uint64_t)( i_value ) * 1000000;
600                 else
601                     val.i_time = i_length;
602
603                 var_Set( p_sys->p_input, "time", val );
604                 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
605                 break;
606             }
607             case VL_TIME_REL_FOR:
608             {
609                 var_Get( p_sys->p_input, "time", &val );
610                 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
611                 {
612                     val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
613                 } else
614                 {
615                     val.i_time = i_length;
616                 }
617                 var_Set( p_sys->p_input, "time", val );
618                 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
619                 break;
620             }
621             case VL_TIME_REL_BACK:
622             {
623                 var_Get( p_sys->p_input, "time", &val );
624                 if( (int64_t)( i_value ) * 1000000 > val.i_time )
625                 {
626                     val.i_time = 0;
627                 } else
628                 {
629                     val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
630                 }
631                 var_Set( p_sys->p_input, "time", val );
632                 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
633                 break;
634             }
635             case POSITION_ABSOLUTE:
636             {
637                 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
638                                             0.0 ), 100.0 );
639                 var_Set( p_sys->p_input, "position", val );
640                 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
641                 break;
642             }
643             case POSITION_REL_FOR:
644             {
645                 var_Get( p_sys->p_input, "position", &val );
646                 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
647                                              0.0 ) , 100.0 );
648                 var_Set( p_sys->p_input, "position", val );
649                 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
650                          i_value );
651                 break;
652             }
653             case POSITION_REL_BACK:
654             {
655                 var_Get( p_sys->p_input, "position", &val );
656                 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
657                                              0.0 ) , 100.0 );
658                 var_Set( p_sys->p_input, "position", val );
659                 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
660                          i_value );
661                 break;
662             }
663             default:
664             {
665                 msg_Dbg( p_intf, "invalid seek request" );
666                 break;
667             }
668         }
669     }
670 #undef POSITION_ABSOLUTE
671 #undef POSITION_REL_FOR
672 #undef POSITION_REL_BACK
673 #undef VL_TIME_ABSOLUTE
674 #undef VL_TIME_REL_FOR
675 #undef VL_TIME_REL_BACK
676 }
677
678
679 /****************************************************************************
680  * URI Parsing functions
681  ****************************************************************************/
682 int TestURIParam( char *psz_uri, const char *psz_name )
683 {
684     char *p = psz_uri;
685
686     while( (p = strstr( p, psz_name )) )
687     {
688         /* Verify that we are dealing with a post/get argument */
689         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
690               && p[strlen(psz_name)] == '=' )
691         {
692             return true;
693         }
694         p++;
695     }
696
697     return false;
698 }
699
700 static const char *FindURIValue( const char *psz_uri, const char *restrict psz_name,
701                            size_t *restrict p_len )
702 {
703     const char *p = psz_uri, *end;
704     size_t len;
705
706     while( (p = strstr( p, psz_name )) )
707     {
708         /* Verify that we are dealing with a post/get argument */
709         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
710               && p[strlen(psz_name)] == '=' )
711             break;
712         p++;
713     }
714
715     if( p == NULL )
716     {
717         *p_len = 0;
718         return NULL;
719     }
720
721     p += strlen( psz_name );
722     if( *p == '=' ) p++;
723
724     if( ( end = strchr( p, '\n' ) ) != NULL )
725     {
726         /* POST method */
727         if( ( end > p ) && ( end[-1] == '\r' ) )
728             end--;
729
730         len = end - p;
731     }
732     else
733     {
734         /* GET method */
735         if( ( end = strchr( p, '&' ) ) != NULL )
736             len = end - p;
737         else
738             len = strlen( p );
739     }
740
741     *p_len = len;
742     return p;
743 }
744
745 const char *ExtractURIValue( const char *restrict psz_uri,
746                            const char *restrict psz_name,
747                            char *restrict psz_buf, size_t bufsize )
748 {
749     size_t len;
750     const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
751     const char *psz_next;
752
753     if( psz_value == NULL )
754     {
755         if( bufsize > 0 )
756             *psz_buf = '\0';
757         return NULL;
758     }
759
760     psz_next = psz_value + len;
761
762     if( len >= bufsize )
763         len = bufsize - 1;
764
765     if( len > 0 )
766         strncpy( psz_buf, psz_value, len );
767     if( bufsize > 0 )
768         psz_buf[len] = '\0';
769
770     return psz_next;
771 }
772
773 char *ExtractURIString( const char *restrict psz_uri,
774                             const char *restrict psz_name )
775 {
776     size_t len;
777     const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
778
779     if( psz_value == NULL )
780         return NULL;
781
782     char *res = malloc( len + 1 );
783     if( res == NULL )
784         return NULL;
785
786     memcpy( res, psz_value, len );
787     res[len] = '\0';
788
789     return res;
790 }
791
792 /* Since the resulting string is smaller we can work in place, so it is
793  * permitted to have psz == new. new points to the first word of the
794  * string, the function returns the remaining string. */
795 char *FirstWord( char *psz, char *new )
796 {
797     bool b_end;
798
799     while( *psz == ' ' )
800         psz++;
801
802     while( *psz != '\0' && *psz != ' ' )
803     {
804         if( *psz == '\'' )
805         {
806             char c = *psz++;
807             while( *psz != '\0' && *psz != c )
808             {
809                 if( *psz == '\\' && psz[1] != '\0' )
810                     psz++;
811                 *new++ = *psz++;
812             }
813             if( *psz == c )
814                 psz++;
815         }
816         else
817         {
818             if( *psz == '\\' && psz[1] != '\0' )
819                 psz++;
820             *new++ = *psz++;
821         }
822     }
823     b_end = !*psz;
824
825     *new++ = '\0';
826     if( !b_end )
827         return psz + 1;
828     else
829         return NULL;
830 }
831
832 /**********************************************************************
833  * MRLParse: parse the MRL, find the MRL string and the options,
834  * create an item with all information in it, and return the item.
835  * return NULL if there is an error.
836  **********************************************************************/
837
838 /* Function analog to FirstWord except that it relies on colon instead
839  * of space to delimit option boundaries. */
840 static char *FirstOption( char *psz, char *new )
841 {
842     bool b_end, b_start = true;
843
844     while( *psz == ' ' )
845         psz++;
846
847     while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
848     {
849         if( *psz == '\'' )
850         {
851             char c = *psz++;
852             while( *psz != '\0' && *psz != c )
853             {
854                 if( *psz == '\\' && psz[1] != '\0' )
855                     psz++;
856                 *new++ = *psz++;
857                 b_start = false;
858             }
859             if( *psz == c )
860                 psz++;
861         }
862         else
863         {
864             if( *psz == '\\' && psz[1] != '\0' )
865                 psz++;
866             *new++ = *psz++;
867             b_start = false;
868         }
869     }
870     b_end = !*psz;
871
872     if ( !b_start )
873         while (new[-1] == ' ')
874             new--;
875
876     *new++ = '\0';
877     if( !b_end )
878         return psz + 1;
879     else
880         return NULL;
881 }
882
883 input_item_t *MRLParse( intf_thread_t *p_intf, const char *mrl,
884                                    char *psz_name )
885 {
886     char *psz = strdup( mrl ), *s_mrl = psz, *s_temp;
887     if( psz == NULL )
888         return NULL;
889     /* extract the mrl */
890     s_temp = FirstOption( s_mrl, s_mrl );
891     if( s_temp == NULL )
892     {
893         s_temp = s_mrl + strlen( s_mrl );
894     }
895
896     input_item_t *p_input = input_item_New( p_intf, s_mrl, psz_name );
897     if( p_input == NULL )
898         return NULL;
899     s_mrl = s_temp;
900
901     /* now we can take care of the options */
902     while ( *s_mrl != '\0' )
903     {
904         s_temp = FirstOption( s_mrl, s_mrl );
905         if( s_mrl == '\0' )
906             break;
907         if( s_temp == NULL )
908         {
909             s_temp = s_mrl + strlen( s_mrl );
910         }
911         input_item_AddOption( p_input, s_mrl, VLC_INPUT_OPTION_TRUSTED );
912         s_mrl = s_temp;
913     }
914
915     return p_input;
916 }
917
918 /**********************************************************************
919  * RealPath: parse ../, ~ and path stuff
920  **********************************************************************/
921 char *RealPath( const char *psz_src )
922 {
923     char *psz_dir;
924     char *p;
925     int i_len = strlen(psz_src);
926
927     psz_dir = xmalloc( i_len + 2 );
928     strcpy( psz_dir, psz_src );
929
930     /* Add a trailing sep to ease the .. step */
931     psz_dir[i_len] = DIR_SEP_CHAR;
932     psz_dir[i_len + 1] = '\0';
933
934 #if (DIR_SEP_CHAR != '/')
935     /* Convert all / to native separator */
936     p = psz_dir;
937     while( (p = strchr( p, '/' )) != NULL )
938     {
939         *p = DIR_SEP_CHAR;
940     }
941 #endif
942
943     /* FIXME: this could be O(N) rather than O(N²)... */
944     /* Remove multiple separators and /./ */
945     p = psz_dir;
946     while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
947     {
948         if( p[1] == DIR_SEP_CHAR )
949             memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
950         else if( p[1] == '.' && p[2] == DIR_SEP_CHAR )
951             memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
952         else
953             p++;
954     }
955
956     if( psz_dir[0] == '~' )
957     {
958         char *home = config_GetUserDir( VLC_HOME_DIR ), *dir;
959         if( asprintf( &dir, "%s%s", home, psz_dir + 1 ) != -1 )
960         {
961             free( psz_dir );
962             psz_dir = dir;
963         }
964         free( home );
965     }
966
967     if( strlen(psz_dir) > 2 )
968     {
969         /* Fix all .. dir */
970         p = psz_dir + 3;
971         while( (p = strchr( p, DIR_SEP_CHAR )) != NULL )
972         {
973             if( p[-1] == '.' && p[-2] == '.' && p[-3] == DIR_SEP_CHAR )
974             {
975                 char *q;
976                 p[-3] = '\0';
977                 if( (q = strrchr( psz_dir, DIR_SEP_CHAR )) != NULL )
978                 {
979                     memmove( q + 1, p + 1, strlen(p + 1) + 1 );
980                     p = q + 1;
981                 }
982                 else
983                 {
984                     memmove( psz_dir, p, strlen(p) + 1 );
985                     p = psz_dir + 3;
986                 }
987             }
988             else
989                 p++;
990         }
991     }
992
993     /* Remove trailing sep if there are at least 2 sep in the string
994      * (handles the C:\ stuff) */
995     p = strrchr( psz_dir, DIR_SEP_CHAR );
996     if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, DIR_SEP_CHAR ) )
997         *p = '\0';
998
999     return psz_dir;
1000 }