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