]> git.sesse.net Git - vlc/blob - modules/control/http/util.c
Remove unneed setting of val.b_bool before calling var_Set( p_input, "rate-{faster...
[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( psz_utf8 == NULL )
351         return NULL;
352
353     if ( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
354     {
355         size_t i_in = strlen(psz_utf8);
356         size_t i_out = i_in * 2;
357         char *psz_local = malloc(i_out + 1);
358         char *psz_out = psz_local;
359         size_t i_ret;
360         char psz_tmp[i_in + 1];
361         const char *psz_in = psz_tmp;
362         strcpy( psz_tmp, psz_utf8 );
363
364         i_in = strlen( psz_tmp );
365
366         i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
367                            &psz_out, &i_out );
368         if( i_ret == (size_t)-1 || i_in )
369         {
370             msg_Warn( p_intf,
371                       "failed to convert \"%s\" to desired charset (%m)",
372                       psz_utf8 );
373             free( psz_local );
374             return strdup( psz_utf8 );
375         }
376
377         *psz_out = '\0';
378         return psz_local;
379     }
380     else
381         return strdup( psz_utf8 );
382 }
383
384 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
385 {
386     intf_sys_t    *p_sys = p_intf->p_sys;
387
388     if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
389     {
390         const char *psz_in = psz_local;
391         size_t i_in = strlen(psz_in);
392         size_t i_out = i_in * 6;
393         char *psz_utf8 = malloc(i_out + 1);
394         char *psz_out = psz_utf8;
395
396         size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
397                                   &psz_out, &i_out );
398         if( i_ret == (size_t)-1 || i_in )
399         {
400             msg_Warn( p_intf,
401                       "failed to convert \"%s\" to desired charset (%m)",
402                       psz_local );
403             free( psz_utf8 );
404             return strdup( psz_local );
405         }
406
407         *psz_out = '\0';
408         return psz_utf8;
409     }
410     else
411         return strdup( psz_local );
412 }
413
414 /*************************************************************************
415  * Playlist stuff
416  *************************************************************************/
417 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
418                            playlist_item_t *p_node, char *name, mvar_t *s,
419                            int i_depth )
420 {
421     if( p_node != NULL )
422     {
423         if( p_node->i_children == -1 )
424         {
425             char value[512];
426             char *psz, *psz_utf8;
427             mvar_t *itm = E_(mvar_New)( name, "set" );
428
429             if( p_pl->status.p_item && p_node &&
430                 p_pl->status.p_item->p_input && p_node->p_input &&
431                 p_pl->status.p_item->p_input->i_id == p_node->p_input->i_id )
432             {
433                 E_(mvar_AppendNewVar)( itm, "current", "1" );
434             }
435             else
436             {
437                 E_(mvar_AppendNewVar)( itm, "current", "0" );
438             }
439
440             sprintf( value, "%d", p_node->i_id );
441             E_(mvar_AppendNewVar)( itm, "index", value );
442
443             psz_utf8 = input_item_GetName( p_node->p_input );
444             psz = E_(FromUTF8)( p_intf, psz_utf8 );
445             E_(mvar_AppendNewVar)( itm, "name", psz );
446             free( psz_utf8 );
447             free( psz );
448
449             psz_utf8 = input_item_GetURI( p_node->p_input );
450             psz = E_(FromUTF8)( p_intf, psz_utf8 );
451             E_(mvar_AppendNewVar)( itm, "uri", psz );
452             free( psz_utf8 );
453             free( psz );
454
455             sprintf( value, "Item");
456             E_(mvar_AppendNewVar)( itm, "type", value );
457
458             sprintf( value, "%d", i_depth );
459             E_(mvar_AppendNewVar)( itm, "depth", value );
460
461             if( p_node->i_flags & PLAYLIST_RO_FLAG )
462             {
463                 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
464             }
465             else
466             {
467                 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
468             }
469
470             sprintf( value, "%ld",
471                     (long) input_item_GetDuration( p_node->p_input ) );
472             E_(mvar_AppendNewVar)( itm, "duration", value );
473
474             E_(mvar_AppendVar)( s, itm );
475         }
476         else
477         {
478             char value[512];
479             char *psz;
480             int i_child;
481             mvar_t *itm = E_(mvar_New)( name, "set" );
482
483             psz = E_(FromUTF8)( p_intf, p_node->p_input->psz_name );
484             E_(mvar_AppendNewVar)( itm, "name", psz );
485             E_(mvar_AppendNewVar)( itm, "uri", psz );
486             free( psz );
487
488             sprintf( value, "Node" );
489             E_(mvar_AppendNewVar)( itm, "type", value );
490
491             sprintf( value, "%d", p_node->i_id );
492             E_(mvar_AppendNewVar)( itm, "index", value );
493
494             sprintf( value, "%d", p_node->i_children);
495             E_(mvar_AppendNewVar)( itm, "i_children", value );
496
497             sprintf( value, "%d", i_depth );
498             E_(mvar_AppendNewVar)( itm, "depth", value );
499
500             if( p_node->i_flags & PLAYLIST_RO_FLAG )
501             {
502                 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
503             }
504             else
505             {
506                 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
507             }
508
509             E_(mvar_AppendVar)( s, itm );
510
511             for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
512                 E_(PlaylistListNode)( p_intf, p_pl,
513                                       p_node->pp_children[i_child],
514                                       name, s, i_depth + 1);
515
516         }
517     }
518 }
519
520 /****************************************************************************
521  * Seek command parsing handling
522  ****************************************************************************/
523 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
524 {
525     intf_sys_t     *p_sys = p_intf->p_sys;
526     vlc_value_t val;
527     int i_stock = 0;
528     uint64_t i_length;
529     int i_value = 0;
530     int i_relative = 0;
531 #define POSITION_ABSOLUTE 12
532 #define POSITION_REL_FOR 13
533 #define POSITION_REL_BACK 11
534 #define VL_TIME_ABSOLUTE 0
535 #define VL_TIME_REL_FOR 1
536 #define VL_TIME_REL_BACK -1
537     if( p_sys->p_input )
538     {
539         var_Get( p_sys->p_input, "length", &val );
540         i_length = val.i_time;
541
542         while( p_value[0] != '\0' )
543         {
544             switch(p_value[0])
545             {
546                 case '+':
547                 {
548                     i_relative = VL_TIME_REL_FOR;
549                     p_value++;
550                     break;
551                 }
552                 case '-':
553                 {
554                     i_relative = VL_TIME_REL_BACK;
555                     p_value++;
556                     break;
557                 }
558                 case '0': case '1': case '2': case '3': case '4':
559                 case '5': case '6': case '7': case '8': case '9':
560                 {
561                     i_stock = strtol( p_value , &p_value , 10 );
562                     break;
563                 }
564                 case '%': /* for percentage ie position */
565                 {
566                     i_relative += POSITION_ABSOLUTE;
567                     i_value = i_stock;
568                     i_stock = 0;
569                     p_value[0] = '\0';
570                     break;
571                 }
572                 case ':':
573                 {
574                     i_value = 60 * (i_value + i_stock) ;
575                     i_stock = 0;
576                     p_value++;
577                     break;
578                 }
579                 case 'h': case 'H': /* hours */
580                 {
581                     i_value += 3600 * i_stock;
582                     i_stock = 0;
583                     /* other characters which are not numbers are not
584                      * important */
585                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
586                            && (p_value[0] != '\0') )
587                     {
588                         p_value++;
589                     }
590                     break;
591                 }
592                 case 'm': case 'M': case '\'': /* minutes */
593                 {
594                     i_value += 60 * i_stock;
595                     i_stock = 0;
596                     p_value++;
597                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
598                            && (p_value[0] != '\0') )
599                     {
600                         p_value++;
601                     }
602                     break;
603                 }
604                 case 's': case 'S': case '"':  /* seconds */
605                 {
606                     i_value += i_stock;
607                     i_stock = 0;
608                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
609                            && (p_value[0] != '\0') )
610                     {
611                         p_value++;
612                     }
613                     break;
614                 }
615                 default:
616                 {
617                     p_value++;
618                     break;
619                 }
620             }
621         }
622
623         /* if there is no known symbol, I consider it as seconds.
624          * Otherwise, i_stock = 0 */
625         i_value += i_stock;
626
627         switch(i_relative)
628         {
629             case VL_TIME_ABSOLUTE:
630             {
631                 if( (uint64_t)( i_value ) * 1000000 <= i_length )
632                     val.i_time = (uint64_t)( i_value ) * 1000000;
633                 else
634                     val.i_time = i_length;
635
636                 var_Set( p_sys->p_input, "time", val );
637                 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
638                 break;
639             }
640             case VL_TIME_REL_FOR:
641             {
642                 var_Get( p_sys->p_input, "time", &val );
643                 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
644                 {
645                     val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
646                 } else
647                 {
648                     val.i_time = i_length;
649                 }
650                 var_Set( p_sys->p_input, "time", val );
651                 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
652                 break;
653             }
654             case VL_TIME_REL_BACK:
655             {
656                 var_Get( p_sys->p_input, "time", &val );
657                 if( (int64_t)( i_value ) * 1000000 > val.i_time )
658                 {
659                     val.i_time = 0;
660                 } else
661                 {
662                     val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
663                 }
664                 var_Set( p_sys->p_input, "time", val );
665                 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
666                 break;
667             }
668             case POSITION_ABSOLUTE:
669             {
670                 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
671                                             0.0 ), 100.0 );
672                 var_Set( p_sys->p_input, "position", val );
673                 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
674                 break;
675             }
676             case POSITION_REL_FOR:
677             {
678                 var_Get( p_sys->p_input, "position", &val );
679                 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
680                                              0.0 ) , 100.0 );
681                 var_Set( p_sys->p_input, "position", val );
682                 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
683                          i_value );
684                 break;
685             }
686             case POSITION_REL_BACK:
687             {
688                 var_Get( p_sys->p_input, "position", &val );
689                 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
690                                              0.0 ) , 100.0 );
691                 var_Set( p_sys->p_input, "position", val );
692                 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
693                          i_value );
694                 break;
695             }
696             default:
697             {
698                 msg_Dbg( p_intf, "invalid seek request" );
699                 break;
700             }
701         }
702     }
703 #undef POSITION_ABSOLUTE
704 #undef POSITION_REL_FOR
705 #undef POSITION_REL_BACK
706 #undef VL_TIME_ABSOLUTE
707 #undef VL_TIME_REL_FOR
708 #undef VL_TIME_REL_BACK
709 }
710
711
712 /****************************************************************************
713  * URI Parsing functions
714  ****************************************************************************/
715 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
716 {
717     char *p = psz_uri;
718
719     while( (p = strstr( p, psz_name )) )
720     {
721         /* Verify that we are dealing with a post/get argument */
722         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
723               && p[strlen(psz_name)] == '=' )
724         {
725             return VLC_TRUE;
726         }
727         p++;
728     }
729
730     return VLC_FALSE;
731 }
732
733 static char *FindURIValue( char *psz_uri, const char *restrict psz_name,
734                            size_t *restrict p_len )
735 {
736     char *p = psz_uri, *end;
737     size_t len;
738
739     while( (p = strstr( p, psz_name )) )
740     {
741         /* Verify that we are dealing with a post/get argument */
742         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
743               && p[strlen(psz_name)] == '=' )
744             break;
745         p++;
746     }
747
748     if( p == NULL )
749     {
750         *p_len = 0;
751         return NULL;
752     }
753
754     p += strlen( psz_name );
755     if( *p == '=' ) p++;
756
757     if( ( end = strchr( p, '\n' ) ) != NULL )
758     {
759         /* POST method */
760         if( ( end > p ) && ( end[-1] == '\r' ) )
761             end--;
762
763         len = end - p;
764     }
765     else
766     {
767         /* GET method */
768         if( ( end = strchr( p, '&' ) ) != NULL )
769             len = end - p;
770         else
771             len = strlen( p );
772     }
773
774     *p_len = len;
775     return p;
776 }
777
778 char *E_(ExtractURIValue)( char *restrict psz_uri,
779                            const char *restrict psz_name,
780                            char *restrict psz_buf, size_t bufsize )
781 {
782     size_t len;
783     char *psz_value = FindURIValue( psz_uri, psz_name, &len );
784     char *psz_next;
785
786     if( psz_value == NULL )
787     {
788         if( bufsize > 0 )
789             *psz_buf = '\0';
790         return NULL;
791     }
792
793     psz_next = psz_value + len;
794
795     if( len >= bufsize )
796         len = bufsize - 1;
797
798     if( len > 0 )
799         strncpy( psz_buf, psz_value, len );
800     if( bufsize > 0 )
801         psz_buf[len] = '\0';
802
803     return psz_next;
804 }
805
806 char *E_(ExtractURIString)( char *restrict psz_uri,
807                             const char *restrict psz_name )
808 {
809     size_t len;
810     char *psz_value = FindURIValue( psz_uri, psz_name, &len );
811
812     if( psz_value == NULL )
813         return NULL;
814
815     char *res = malloc( len + 1 );
816     if( res == NULL )
817         return NULL;
818
819     memcpy( res, psz_value, len );
820     res[len] = '\0';
821
822     return res;
823 }
824
825 /* Since the resulting string is smaller we can work in place, so it is
826  * permitted to have psz == new. new points to the first word of the
827  * string, the function returns the remaining string. */
828 char *E_(FirstWord)( char *psz, char *new )
829 {
830     vlc_bool_t b_end;
831
832     while( *psz == ' ' )
833         psz++;
834
835     while( *psz != '\0' && *psz != ' ' )
836     {
837         if( *psz == '\'' )
838         {
839             char c = *psz++;
840             while( *psz != '\0' && *psz != c )
841             {
842                 if( *psz == '\\' && psz[1] != '\0' )
843                     psz++;
844                 *new++ = *psz++;
845             }
846             if( *psz == c )
847                 psz++;
848         }
849         else
850         {
851             if( *psz == '\\' && psz[1] != '\0' )
852                 psz++;
853             *new++ = *psz++;
854         }
855     }
856     b_end = !*psz;
857
858     *new++ = '\0';
859     if( !b_end )
860         return psz + 1;
861     else
862         return NULL;
863 }
864
865 /**********************************************************************
866  * MRLParse: parse the MRL, find the MRL string and the options,
867  * create an item with all information in it, and return the item.
868  * return NULL if there is an error.
869  **********************************************************************/
870
871 /* Function analog to FirstWord except that it relies on colon instead
872  * of space to delimit option boundaries. */
873 static char *FirstOption( char *psz, char *new )
874 {
875     vlc_bool_t b_end, b_start = VLC_TRUE;
876
877     while( *psz == ' ' )
878         psz++;
879
880     while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
881     {
882         if( *psz == '\'' )
883         {
884             char c = *psz++;
885             while( *psz != '\0' && *psz != c )
886             {
887                 if( *psz == '\\' && psz[1] != '\0' )
888                     psz++;
889                 *new++ = *psz++;
890                 b_start = VLC_FALSE;
891             }
892             if( *psz == c )
893                 psz++;
894         }
895         else
896         {
897             if( *psz == '\\' && psz[1] != '\0' )
898                 psz++;
899             *new++ = *psz++;
900             b_start = VLC_FALSE;
901         }
902     }
903     b_end = !*psz;
904
905     if ( !b_start )
906         while (new[-1] == ' ')
907             new--;
908
909     *new++ = '\0';
910     if( !b_end )
911         return psz + 1;
912     else
913         return NULL;
914 }
915
916 input_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *_psz,
917                                    char *psz_name )
918 {
919     char *psz = strdup( _psz );
920     char *s_mrl = psz;
921     char *s_temp;
922     input_item_t * p_input = NULL;
923
924     /* extract the mrl */
925     s_temp = FirstOption( s_mrl, s_mrl );
926     if( s_temp == NULL )
927     {
928         s_temp = s_mrl + strlen( s_mrl );
929     }
930
931     p_input = input_ItemNew( p_intf, s_mrl, psz_name );
932     s_mrl = s_temp;
933
934     /* now we can take care of the options */
935     while ( *s_mrl != '\0' )
936     {
937         s_temp = FirstOption( s_mrl, s_mrl );
938         if( s_mrl == '\0' )
939             break;
940         if( s_temp == NULL )
941         {
942             s_temp = s_mrl + strlen( s_mrl );
943         }
944         input_ItemAddOption( p_input, s_mrl );
945         s_mrl = s_temp;
946     }
947
948     free( psz );
949     return p_input;
950 }
951
952 /**********************************************************************
953  * RealPath: parse ../, ~ and path stuff
954  **********************************************************************/
955 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
956 {
957     char *psz_dir;
958     char *p;
959     int i_len = strlen(psz_src);
960     const char sep = DIR_SEP_CHAR;
961
962     psz_dir = malloc( i_len + 2 );
963     strcpy( psz_dir, psz_src );
964
965     /* Add a trailing sep to ease the .. step */
966     psz_dir[i_len] = sep;
967     psz_dir[i_len + 1] = '\0';
968
969 #if (DIR_SEP_CHAR != '/')
970     /* Convert all / to native separator */
971     p = psz_dir;
972     while( (p = strchr( p, '/' )) != NULL )
973     {
974         *p = sep;
975     }
976 #endif
977
978     /* FIXME: this could be O(N) rather than O(N²)... */
979     /* Remove multiple separators and /./ */
980     p = psz_dir;
981     while( (p = strchr( p, sep )) != NULL )
982     {
983         if( p[1] == sep )
984             memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
985         else if( p[1] == '.' && p[2] == sep )
986             memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
987         else
988             p++;
989     }
990
991     if( psz_dir[0] == '~' )
992     {
993         char *dir;
994         /* This is incomplete : we should also support the ~cmassiot/ syntax. */
995         asprintf( &dir, "%s%s", p_intf->p_libvlc->psz_homedir, psz_dir + 1 );
996         free( psz_dir );
997         psz_dir = dir;
998     }
999
1000     if( strlen(psz_dir) > 2 )
1001     {
1002         /* Fix all .. dir */
1003         p = psz_dir + 3;
1004         while( (p = strchr( p, sep )) != NULL )
1005         {
1006             if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
1007             {
1008                 char *q;
1009                 p[-3] = '\0';
1010                 if( (q = strrchr( psz_dir, sep )) != NULL )
1011                 {
1012                     memmove( q + 1, p + 1, strlen(p + 1) + 1 );
1013                     p = q + 1;
1014                 }
1015                 else
1016                 {
1017                     memmove( psz_dir, p, strlen(p) + 1 );
1018                     p = psz_dir + 3;
1019                 }
1020             }
1021             else
1022                 p++;
1023         }
1024     }
1025
1026     /* Remove trailing sep if there are at least 2 sep in the string
1027      * (handles the C:\ stuff) */
1028     p = strrchr( psz_dir, sep );
1029     if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )
1030         *p = '\0';
1031
1032     return psz_dir;
1033 }