]> git.sesse.net Git - vlc/blob - modules/control/http/util.c
97767bdc0001f40f3dd807ad9e767fefe5108f90
[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/vlc.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     char sep;
124
125 #if defined( WIN32 )
126     sep = '\\';
127 #else
128     sep = '/';
129 #endif
130
131     if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
132     {
133         if( errno != ENOENT && errno != ENOTDIR )
134             msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
135         return VLC_EGENERIC;
136     }
137
138     i_dirlen = strlen( psz_dir );
139     if( i_dirlen + 10 > MAX_DIR_SIZE )
140     {
141         msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
142         closedir( p_dir );
143         return 0;
144     }
145
146     msg_Dbg( p_intf, "dir=%s", psz_dir );
147
148     snprintf( dir, sizeof( dir ), "%s%c.access", psz_dir, sep );
149     if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
150     {
151         char line[1024];
152         int  i_size;
153
154         msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
155
156         i_size = fread( line, 1, 1023, file );
157         if( i_size > 0 )
158         {
159             char *p;
160             while( i_size > 0 && ( line[i_size-1] == '\n' ||
161                    line[i_size-1] == '\r' ) )
162             {
163                 i_size--;
164             }
165
166             line[i_size] = '\0';
167
168             p = strchr( line, ':' );
169             if( p )
170             {
171                 *p++ = '\0';
172                 user = strdup( line );
173                 password = strdup( p );
174             }
175         }
176         msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
177                  user, password, i_size );
178
179         fclose( file );
180     }
181
182     snprintf( dir, sizeof( dir ), "%s%c.hosts", psz_dir, sep );
183     p_acl = ACL_Create( p_intf, false );
184     if( ACL_LoadFile( p_acl, dir ) )
185     {
186         ACL_Destroy( p_acl );
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%c%s", psz_dir, sep, 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                     asprintf( &psz_redir, "%s%s", f->name, p );
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     free( user );
324     free( password );
325
326     ACL_Destroy( p_acl );
327     closedir( p_dir );
328
329     return VLC_SUCCESS;
330 }
331
332
333 /*************************************************************************
334  * Playlist stuff
335  *************************************************************************/
336 void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl,
337                            playlist_item_t *p_node, char *name, mvar_t *s,
338                            int i_depth )
339 {
340     if( p_node != NULL )
341     {
342         if( p_node->i_children == -1 )
343         {
344             char value[512];
345             char *psz;
346             mvar_t *itm = mvar_New( name, "set" );
347
348             if( p_pl->status.p_item && p_node &&
349                 p_pl->status.p_item->p_input && p_node->p_input &&
350                 p_pl->status.p_item->p_input->i_id == p_node->p_input->i_id )
351             {
352                 mvar_AppendNewVar( itm, "current", "1" );
353             }
354             else
355             {
356                 mvar_AppendNewVar( itm, "current", "0" );
357             }
358
359             sprintf( value, "%d", p_node->i_id );
360             mvar_AppendNewVar( itm, "index", value );
361
362             psz = input_item_GetName( p_node->p_input );
363             mvar_AppendNewVar( itm, "name", psz );
364             free( psz );
365
366             psz = input_item_GetURI( p_node->p_input );
367             mvar_AppendNewVar( itm, "uri", psz );
368             free( psz );
369
370             sprintf( value, "Item");
371             mvar_AppendNewVar( itm, "type", value );
372
373             sprintf( value, "%d", i_depth );
374             mvar_AppendNewVar( itm, "depth", value );
375
376             if( p_node->i_flags & PLAYLIST_RO_FLAG )
377             {
378                 mvar_AppendNewVar( itm, "ro", "ro" );
379             }
380             else
381             {
382                 mvar_AppendNewVar( itm, "ro", "rw" );
383             }
384
385             sprintf( value, "%ld",
386                     (long) input_item_GetDuration( p_node->p_input ) );
387             mvar_AppendNewVar( itm, "duration", value );
388
389             mvar_AppendVar( s, itm );
390         }
391         else
392         {
393             char value[512];
394             int i_child;
395             mvar_t *itm = mvar_New( name, "set" );
396
397             mvar_AppendNewVar( itm, "name", p_node->p_input->psz_name );
398             mvar_AppendNewVar( itm, "uri", p_node->p_input->psz_name );
399
400             sprintf( value, "Node" );
401             mvar_AppendNewVar( itm, "type", value );
402
403             sprintf( value, "%d", p_node->i_id );
404             mvar_AppendNewVar( itm, "index", value );
405
406             sprintf( value, "%d", p_node->i_children);
407             mvar_AppendNewVar( itm, "i_children", value );
408
409             sprintf( value, "%d", i_depth );
410             mvar_AppendNewVar( itm, "depth", value );
411
412             if( p_node->i_flags & PLAYLIST_RO_FLAG )
413             {
414                 mvar_AppendNewVar( itm, "ro", "ro" );
415             }
416             else
417             {
418                 mvar_AppendNewVar( itm, "ro", "rw" );
419             }
420
421             mvar_AppendVar( s, itm );
422
423             for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
424                 PlaylistListNode( p_intf, p_pl,
425                                       p_node->pp_children[i_child],
426                                       name, s, i_depth + 1);
427
428         }
429     }
430 }
431
432 /****************************************************************************
433  * Seek command parsing handling
434  ****************************************************************************/
435 void HandleSeek( intf_thread_t *p_intf, char *p_value )
436 {
437     intf_sys_t     *p_sys = p_intf->p_sys;
438     vlc_value_t val;
439     int i_stock = 0;
440     uint64_t i_length;
441     int i_value = 0;
442     int i_relative = 0;
443 #define POSITION_ABSOLUTE 12
444 #define POSITION_REL_FOR 13
445 #define POSITION_REL_BACK 11
446 #define VL_TIME_ABSOLUTE 0
447 #define VL_TIME_REL_FOR 1
448 #define VL_TIME_REL_BACK -1
449     if( p_sys->p_input )
450     {
451         var_Get( p_sys->p_input, "length", &val );
452         i_length = val.i_time;
453
454         while( p_value[0] != '\0' )
455         {
456             switch(p_value[0])
457             {
458                 case '+':
459                 {
460                     i_relative = VL_TIME_REL_FOR;
461                     p_value++;
462                     break;
463                 }
464                 case '-':
465                 {
466                     i_relative = VL_TIME_REL_BACK;
467                     p_value++;
468                     break;
469                 }
470                 case '0': case '1': case '2': case '3': case '4':
471                 case '5': case '6': case '7': case '8': case '9':
472                 {
473                     i_stock = strtol( p_value , &p_value , 10 );
474                     break;
475                 }
476                 case '%': /* for percentage ie position */
477                 {
478                     i_relative += POSITION_ABSOLUTE;
479                     i_value = i_stock;
480                     i_stock = 0;
481                     p_value[0] = '\0';
482                     break;
483                 }
484                 case ':':
485                 {
486                     i_value = 60 * (i_value + i_stock) ;
487                     i_stock = 0;
488                     p_value++;
489                     break;
490                 }
491                 case 'h': case 'H': /* hours */
492                 {
493                     i_value += 3600 * i_stock;
494                     i_stock = 0;
495                     /* other characters which are not numbers are not
496                      * important */
497                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
498                            && (p_value[0] != '\0') )
499                     {
500                         p_value++;
501                     }
502                     break;
503                 }
504                 case 'm': case 'M': case '\'': /* minutes */
505                 {
506                     i_value += 60 * i_stock;
507                     i_stock = 0;
508                     p_value++;
509                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
510                            && (p_value[0] != '\0') )
511                     {
512                         p_value++;
513                     }
514                     break;
515                 }
516                 case 's': case 'S': case '"':  /* seconds */
517                 {
518                     i_value += i_stock;
519                     i_stock = 0;
520                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
521                            && (p_value[0] != '\0') )
522                     {
523                         p_value++;
524                     }
525                     break;
526                 }
527                 default:
528                 {
529                     p_value++;
530                     break;
531                 }
532             }
533         }
534
535         /* if there is no known symbol, I consider it as seconds.
536          * Otherwise, i_stock = 0 */
537         i_value += i_stock;
538
539         switch(i_relative)
540         {
541             case VL_TIME_ABSOLUTE:
542             {
543                 if( (uint64_t)( i_value ) * 1000000 <= i_length )
544                     val.i_time = (uint64_t)( i_value ) * 1000000;
545                 else
546                     val.i_time = i_length;
547
548                 var_Set( p_sys->p_input, "time", val );
549                 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
550                 break;
551             }
552             case VL_TIME_REL_FOR:
553             {
554                 var_Get( p_sys->p_input, "time", &val );
555                 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
556                 {
557                     val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
558                 } else
559                 {
560                     val.i_time = i_length;
561                 }
562                 var_Set( p_sys->p_input, "time", val );
563                 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
564                 break;
565             }
566             case VL_TIME_REL_BACK:
567             {
568                 var_Get( p_sys->p_input, "time", &val );
569                 if( (int64_t)( i_value ) * 1000000 > val.i_time )
570                 {
571                     val.i_time = 0;
572                 } else
573                 {
574                     val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
575                 }
576                 var_Set( p_sys->p_input, "time", val );
577                 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
578                 break;
579             }
580             case POSITION_ABSOLUTE:
581             {
582                 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
583                                             0.0 ), 100.0 );
584                 var_Set( p_sys->p_input, "position", val );
585                 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
586                 break;
587             }
588             case POSITION_REL_FOR:
589             {
590                 var_Get( p_sys->p_input, "position", &val );
591                 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
592                                              0.0 ) , 100.0 );
593                 var_Set( p_sys->p_input, "position", val );
594                 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
595                          i_value );
596                 break;
597             }
598             case POSITION_REL_BACK:
599             {
600                 var_Get( p_sys->p_input, "position", &val );
601                 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
602                                              0.0 ) , 100.0 );
603                 var_Set( p_sys->p_input, "position", val );
604                 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
605                          i_value );
606                 break;
607             }
608             default:
609             {
610                 msg_Dbg( p_intf, "invalid seek request" );
611                 break;
612             }
613         }
614     }
615 #undef POSITION_ABSOLUTE
616 #undef POSITION_REL_FOR
617 #undef POSITION_REL_BACK
618 #undef VL_TIME_ABSOLUTE
619 #undef VL_TIME_REL_FOR
620 #undef VL_TIME_REL_BACK
621 }
622
623
624 /****************************************************************************
625  * URI Parsing functions
626  ****************************************************************************/
627 int TestURIParam( char *psz_uri, const char *psz_name )
628 {
629     char *p = psz_uri;
630
631     while( (p = strstr( p, psz_name )) )
632     {
633         /* Verify that we are dealing with a post/get argument */
634         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
635               && p[strlen(psz_name)] == '=' )
636         {
637             return true;
638         }
639         p++;
640     }
641
642     return false;
643 }
644
645 static char *FindURIValue( char *psz_uri, const char *restrict psz_name,
646                            size_t *restrict p_len )
647 {
648     char *p = psz_uri, *end;
649     size_t len;
650
651     while( (p = strstr( p, psz_name )) )
652     {
653         /* Verify that we are dealing with a post/get argument */
654         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
655               && p[strlen(psz_name)] == '=' )
656             break;
657         p++;
658     }
659
660     if( p == NULL )
661     {
662         *p_len = 0;
663         return NULL;
664     }
665
666     p += strlen( psz_name );
667     if( *p == '=' ) p++;
668
669     if( ( end = strchr( p, '\n' ) ) != NULL )
670     {
671         /* POST method */
672         if( ( end > p ) && ( end[-1] == '\r' ) )
673             end--;
674
675         len = end - p;
676     }
677     else
678     {
679         /* GET method */
680         if( ( end = strchr( p, '&' ) ) != NULL )
681             len = end - p;
682         else
683             len = strlen( p );
684     }
685
686     *p_len = len;
687     return p;
688 }
689
690 char *ExtractURIValue( char *restrict psz_uri,
691                            const char *restrict psz_name,
692                            char *restrict psz_buf, size_t bufsize )
693 {
694     size_t len;
695     char *psz_value = FindURIValue( psz_uri, psz_name, &len );
696     char *psz_next;
697
698     if( psz_value == NULL )
699     {
700         if( bufsize > 0 )
701             *psz_buf = '\0';
702         return NULL;
703     }
704
705     psz_next = psz_value + len;
706
707     if( len >= bufsize )
708         len = bufsize - 1;
709
710     if( len > 0 )
711         strncpy( psz_buf, psz_value, len );
712     if( bufsize > 0 )
713         psz_buf[len] = '\0';
714
715     return psz_next;
716 }
717
718 char *ExtractURIString( char *restrict psz_uri,
719                             const char *restrict psz_name )
720 {
721     size_t len;
722     char *psz_value = FindURIValue( psz_uri, psz_name, &len );
723
724     if( psz_value == NULL )
725         return NULL;
726
727     char *res = malloc( len + 1 );
728     if( res == NULL )
729         return NULL;
730
731     memcpy( res, psz_value, len );
732     res[len] = '\0';
733
734     return res;
735 }
736
737 /* Since the resulting string is smaller we can work in place, so it is
738  * permitted to have psz == new. new points to the first word of the
739  * string, the function returns the remaining string. */
740 char *FirstWord( char *psz, char *new )
741 {
742     bool b_end;
743
744     while( *psz == ' ' )
745         psz++;
746
747     while( *psz != '\0' && *psz != ' ' )
748     {
749         if( *psz == '\'' )
750         {
751             char c = *psz++;
752             while( *psz != '\0' && *psz != c )
753             {
754                 if( *psz == '\\' && psz[1] != '\0' )
755                     psz++;
756                 *new++ = *psz++;
757             }
758             if( *psz == c )
759                 psz++;
760         }
761         else
762         {
763             if( *psz == '\\' && psz[1] != '\0' )
764                 psz++;
765             *new++ = *psz++;
766         }
767     }
768     b_end = !*psz;
769
770     *new++ = '\0';
771     if( !b_end )
772         return psz + 1;
773     else
774         return NULL;
775 }
776
777 /**********************************************************************
778  * MRLParse: parse the MRL, find the MRL string and the options,
779  * create an item with all information in it, and return the item.
780  * return NULL if there is an error.
781  **********************************************************************/
782
783 /* Function analog to FirstWord except that it relies on colon instead
784  * of space to delimit option boundaries. */
785 static char *FirstOption( char *psz, char *new )
786 {
787     bool b_end, b_start = true;
788
789     while( *psz == ' ' )
790         psz++;
791
792     while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
793     {
794         if( *psz == '\'' )
795         {
796             char c = *psz++;
797             while( *psz != '\0' && *psz != c )
798             {
799                 if( *psz == '\\' && psz[1] != '\0' )
800                     psz++;
801                 *new++ = *psz++;
802                 b_start = false;
803             }
804             if( *psz == c )
805                 psz++;
806         }
807         else
808         {
809             if( *psz == '\\' && psz[1] != '\0' )
810                 psz++;
811             *new++ = *psz++;
812             b_start = false;
813         }
814     }
815     b_end = !*psz;
816
817     if ( !b_start )
818         while (new[-1] == ' ')
819             new--;
820
821     *new++ = '\0';
822     if( !b_end )
823         return psz + 1;
824     else
825         return NULL;
826 }
827
828 input_item_t *MRLParse( intf_thread_t *p_intf, char *_psz,
829                                    char *psz_name )
830 {
831     char *psz = strdup( _psz );
832     char *s_mrl = psz;
833     char *s_temp;
834     input_item_t * p_input = NULL;
835
836     /* extract the mrl */
837     s_temp = FirstOption( s_mrl, s_mrl );
838     if( s_temp == NULL )
839     {
840         s_temp = s_mrl + strlen( s_mrl );
841     }
842
843     p_input = input_ItemNew( p_intf, s_mrl, psz_name );
844     s_mrl = s_temp;
845
846     /* now we can take care of the options */
847     while ( *s_mrl != '\0' )
848     {
849         s_temp = FirstOption( s_mrl, s_mrl );
850         if( s_mrl == '\0' )
851             break;
852         if( s_temp == NULL )
853         {
854             s_temp = s_mrl + strlen( s_mrl );
855         }
856         input_ItemAddOption( p_input, s_mrl );
857         s_mrl = s_temp;
858     }
859
860     free( psz );
861     return p_input;
862 }
863
864 /**********************************************************************
865  * RealPath: parse ../, ~ and path stuff
866  **********************************************************************/
867 char *RealPath( intf_thread_t *p_intf, const char *psz_src )
868 {
869     char *psz_dir;
870     char *p;
871     int i_len = strlen(psz_src);
872     const char sep = DIR_SEP_CHAR;
873
874     psz_dir = malloc( i_len + 2 );
875     strcpy( psz_dir, psz_src );
876
877     /* Add a trailing sep to ease the .. step */
878     psz_dir[i_len] = sep;
879     psz_dir[i_len + 1] = '\0';
880
881 #if (DIR_SEP_CHAR != '/')
882     /* Convert all / to native separator */
883     p = psz_dir;
884     while( (p = strchr( p, '/' )) != NULL )
885     {
886         *p = sep;
887     }
888 #endif
889
890     /* FIXME: this could be O(N) rather than O(N²)... */
891     /* Remove multiple separators and /./ */
892     p = psz_dir;
893     while( (p = strchr( p, sep )) != NULL )
894     {
895         if( p[1] == sep )
896             memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
897         else if( p[1] == '.' && p[2] == sep )
898             memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
899         else
900             p++;
901     }
902
903     if( psz_dir[0] == '~' )
904     {
905         char *dir;
906         /* This is incomplete : we should also support the ~cmassiot/ syntax. */
907         asprintf( &dir, "%s%s", p_intf->p_libvlc->psz_homedir, psz_dir + 1 );
908         free( psz_dir );
909         psz_dir = dir;
910     }
911
912     if( strlen(psz_dir) > 2 )
913     {
914         /* Fix all .. dir */
915         p = psz_dir + 3;
916         while( (p = strchr( p, sep )) != NULL )
917         {
918             if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
919             {
920                 char *q;
921                 p[-3] = '\0';
922                 if( (q = strrchr( psz_dir, sep )) != NULL )
923                 {
924                     memmove( q + 1, p + 1, strlen(p + 1) + 1 );
925                     p = q + 1;
926                 }
927                 else
928                 {
929                     memmove( psz_dir, p, strlen(p) + 1 );
930                     p = psz_dir + 3;
931                 }
932             }
933             else
934                 p++;
935         }
936     }
937
938     /* Remove trailing sep if there are at least 2 sep in the string
939      * (handles the C:\ stuff) */
940     p = strrchr( psz_dir, sep );
941     if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )
942         *p = '\0';
943
944     return psz_dir;
945 }