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