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