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