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