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