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