util.c 31.9 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * util.c : Utility functions for HTTP interface
 *****************************************************************************
 * Copyright (C) 2001-2005 the VideoLAN team
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Authors: Gildas Bazin <gbazin@netcourrier.com>
 *          Laurent Aimar <fenrir@via.ecp.fr>
 *          Christophe Massiot <massiot@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

/****************************************************************************
 * File and directory functions
 ****************************************************************************/

/* ToUrl: create a good name for an url from filename */
char *E_(FileToUrl)( char *name, vlc_bool_t *pb_index )
{
    char *url, *p;

    url = p = malloc( strlen( name ) + 1 );

    *pb_index = VLC_FALSE;
    if( !url || !p )
    {
        return NULL;
    }

#ifdef WIN32
    while( *name == '\\' || *name == '/' )
#else
    while( *name == '/' )
#endif
    {
        name++;
    }

    *p++ = '/';
    strcpy( p, name );

#ifdef WIN32
    /* convert '\\' into '/' */
    name = p;
    while( *name )
    {
        if( *name == '\\' )
64
            *name = '/';
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        name++;
    }
#endif

    /* index.* -> / */
    if( ( p = strrchr( url, '/' ) ) != NULL )
    {
        if( !strncmp( p, "/index.", 7 ) )
        {
            p[1] = '\0';
            *pb_index = VLC_TRUE;
        }
    }
    return url;
}

/* Load a file */
int E_(FileLoad)( FILE *f, char **pp_data, int *pi_data )
{
    int i_read;

    /* just load the file */
    *pi_data = 0;
    *pp_data = malloc( 1025 );  /* +1 for \0 */
    while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
    {
        *pi_data += 1024;
        *pp_data = realloc( *pp_data, *pi_data  + 1025 );
    }
    if( i_read > 0 )
    {
        *pi_data += i_read;
    }
    (*pp_data)[*pi_data] = '\0';

    return VLC_SUCCESS;
}

/* Parse a directory and recursively add files */
int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
105
                        char *psz_dir )
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
{
    intf_sys_t     *p_sys = p_intf->p_sys;
    char           dir[MAX_DIR_SIZE];
#ifdef HAVE_SYS_STAT_H
    struct stat   stat_info;
#endif
    DIR           *p_dir;
    vlc_acl_t     *p_acl;
    FILE          *file;

    char          *user = NULL;
    char          *password = NULL;

    int           i_dirlen;

121 122 123 124 125 126 127 128
    char sep;

#if defined( WIN32 )
    sep = '\\';
#else
    sep = '/';
#endif

129
#ifdef HAVE_SYS_STAT_H
130
    if( utf8_stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
131 132 133 134 135
    {
        return VLC_EGENERIC;
    }
#endif

136
    if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
137
    {
138
        msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
139 140 141 142 143 144
        return VLC_EGENERIC;
    }

    i_dirlen = strlen( psz_dir );
    if( i_dirlen + 10 > MAX_DIR_SIZE )
    {
145
        msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
146 147 148 149 150
        return 0;
    }

    msg_Dbg( p_intf, "dir=%s", psz_dir );

151
    sprintf( dir, "%s%c.access", psz_dir, sep );
152
    if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    {
        char line[1024];
        int  i_size;

        msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );

        i_size = fread( line, 1, 1023, file );
        if( i_size > 0 )
        {
            char *p;
            while( i_size > 0 && ( line[i_size-1] == '\n' ||
                   line[i_size-1] == '\r' ) )
            {
                i_size--;
            }

            line[i_size] = '\0';

            p = strchr( line, ':' );
            if( p )
            {
                *p++ = '\0';
                user = strdup( line );
                password = strdup( p );
            }
        }
        msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
                 user, password, i_size );

        fclose( file );
    }

185
    sprintf( dir, "%s%c.hosts", psz_dir, sep );
186 187 188 189 190 191 192 193 194
    p_acl = ACL_Create( p_intf, VLC_FALSE );
    if( ACL_LoadFile( p_acl, dir ) )
    {
        ACL_Destroy( p_acl );
        p_acl = NULL;
    }

    for( ;; )
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
195
        const char *psz_filename;
196
        /* parse psz_src dir */
197
        if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
198 199 200 201
        {
            break;
        }

202 203
        if( ( psz_filename[0] == '.' )
         || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
204 205
            continue;

206 207 208
        sprintf( dir, "%s%c%s", psz_dir, sep, psz_filename );
        LocaleFree( psz_filename );

209 210
        if( E_(ParseDirectory)( p_intf, psz_root, dir ) )
        {
211 212
            httpd_file_sys_t *f = NULL;
            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 219 220
            free( psz_tmp );
            psz_tmp = vlc_fix_readdir_charset( p_intf,
                                               &dir[strlen( psz_root )] );
221
            psz_name = E_(FileToUrl)( psz_tmp, &b_index );
222
            free( psz_tmp );
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
            psz_ext = strrchr( psz_file, '.' );
            if( psz_ext != NULL )
            {
                int i;
                psz_ext++;
                for( i = 0; i < p_sys->i_handlers; i++ )
                    if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
                        break;
                if( i < p_sys->i_handlers )
                {
                    f = malloc( sizeof( httpd_handler_sys_t ) );
                    h = (httpd_handler_sys_t *)f;
                    f->b_handler = VLC_TRUE;
                    h->p_association = p_sys->pp_handlers[i];
                }
            }
            if( f == NULL )
            {
                f = malloc( sizeof( httpd_file_sys_t ) );
                f->b_handler = VLC_FALSE;
            }

            f->p_intf  = p_intf;
            f->p_file = NULL;
            f->p_redir = NULL;
            f->p_redir2 = NULL;
            f->file = psz_file;
            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 254 255 256 257 258 259 260 261 262

            if( !f->name )
            {
                msg_Err( p_intf , "unable to parse directory" );
                closedir( p_dir );
                free( f );
                return( VLC_ENOMEM );
            }
            msg_Dbg( p_intf, "file=%s (url=%s)",
                     f->file, f->name );

263 264
            if( !f->b_handler )
            {
265 266 267 268 269 270 271 272 273 274 275 276 277
                char *psz_type = strdup( p_sys->psz_html_type );
                if( strstr( &dir[strlen( psz_root )], ".xml" ) )
                {
                    char *psz = strstr( psz_type, "html;" );
                    if( psz )
                    {
                        psz[0] = 'x';
                        psz[1] = 'm';
                        psz[2] = 'l';
                        psz[3] = ';';
                        psz[4] = ' ';
                    }
                }
278 279
                f->p_file = httpd_FileNew( p_sys->p_httpd_host,
                                           f->name,
280
                                           f->b_html ? psz_type : NULL,
281 282
                                           user, password, p_acl,
                                           E_(HttpCallback), f );
283
                free( psz_type );
284 285 286 287 288 289
                if( f->p_file != NULL )
                {
                    TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
                }
            }
            else
290
            {
291 292 293 294 295 296 297 298 299
                h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
                                                 f->name,
                                                 user, password, p_acl,
                                                 E_(HandlerCallback), h );
                if( h->p_handler != NULL )
                {
                    TAB_APPEND( p_sys->i_files, p_sys->pp_files,
                                (httpd_file_sys_t *)h );
                }
300
            }
301

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
            /* for url that ends by / add
             *  - a redirect from rep to rep/
             *  - in case of index.* rep/index.html to rep/ */
            if( f && f->name[strlen(f->name) - 1] == '/' )
            {
                char *psz_redir = strdup( f->name );
                char *p;
                psz_redir[strlen( psz_redir ) - 1] = '\0';

                msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
                f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
                free( psz_redir );

                if( b_index && ( p = strstr( f->file, "index." ) ) )
                {
                    asprintf( &psz_redir, "%s%s", f->name, p );

                    msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
                    f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
                                                     f->name, psz_redir );

                    free( psz_redir );
                }
            }
        }
    }

    if( user )
    {
        free( user );
    }
    if( password )
    {
        free( password );
    }

    ACL_Destroy( p_acl );
    closedir( p_dir );

    return VLC_SUCCESS;
}


/**************************************************************************
 * Locale functions
 **************************************************************************/
char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 )
{
    intf_sys_t    *p_sys = p_intf->p_sys;

    if ( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
    {
354
        size_t i_in = strlen(psz_utf8);
355 356 357
        size_t i_out = i_in * 2;
        char *psz_local = malloc(i_out + 1);
        char *psz_out = psz_local;
358 359
        size_t i_ret;
        char psz_tmp[i_in + 1];
360
        const char *psz_in = psz_tmp;
361 362 363
        strcpy( psz_tmp, psz_utf8 );

        i_in = strlen( psz_tmp );
364

365 366
        i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
                           &psz_out, &i_out );
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
        if( i_ret == (size_t)-1 || i_in )
        {
            msg_Warn( p_intf,
                      "failed to convert \"%s\" to desired charset (%s)",
                      psz_utf8, strerror(errno) );
            free( psz_local );
            return strdup( psz_utf8 );
        }

        *psz_out = '\0';
        return psz_local;
    }
    else
        return strdup( psz_utf8 );
}

char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
{
    intf_sys_t    *p_sys = p_intf->p_sys;

    if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
    {
389
        const char *psz_in = psz_local;
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
        size_t i_in = strlen(psz_in);
        size_t i_out = i_in * 6;
        char *psz_utf8 = malloc(i_out + 1);
        char *psz_out = psz_utf8;

        size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
                                  &psz_out, &i_out );
        if( i_ret == (size_t)-1 || i_in )
        {
            msg_Warn( p_intf,
                      "failed to convert \"%s\" to desired charset (%s)",
                      psz_local, strerror(errno) );
            free( psz_utf8 );
            return strdup( psz_local );
        }

        *psz_out = '\0';
        return psz_utf8;
    }
    else
        return strdup( psz_local );
}

/*************************************************************************
 * Playlist stuff
 *************************************************************************/
void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
                           playlist_item_t *p_node, char *name, mvar_t *s,
                           int i_depth )
{
    if( p_node != NULL )
    {
        if( p_node->i_children == -1 )
        {
            char value[512];
            char *psz;
426
            mvar_t *itm = E_(mvar_New)( name, "set" );
427 428

            sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
429
            E_(mvar_AppendNewVar)( itm, "current", value );
430 431

            sprintf( value, "%d", p_node->input.i_id );
432
            E_(mvar_AppendNewVar)( itm, "index", value );
433 434

            psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
435
            E_(mvar_AppendNewVar)( itm, "name", psz );
436 437 438
            free( psz );

            psz = E_(FromUTF8)( p_intf, p_node->input.psz_uri );
439
            E_(mvar_AppendNewVar)( itm, "uri", psz );
440 441 442
            free( psz );

            sprintf( value, "Item");
443
            E_(mvar_AppendNewVar)( itm, "type", value );
444 445

            sprintf( value, "%d", i_depth );
446
            E_(mvar_AppendNewVar)( itm, "depth", value );
447

448 449 450 451 452 453 454 455 456
            if( p_node->i_flags & PLAYLIST_RO_FLAG )
            {
                E_(mvar_AppendNewVar)( itm, "ro", "ro" );
            }
            else
            {
                E_(mvar_AppendNewVar)( itm, "ro", "rw" );
            }

457
            sprintf( value, "%ld", (long)p_node->input.i_duration );
458 459
            E_(mvar_AppendNewVar)( itm, "duration", value );

460
            E_(mvar_AppendVar)( s, itm );
461 462 463 464 465 466
        }
        else
        {
            char value[512];
            char *psz;
            int i_child;
467
            mvar_t *itm = E_(mvar_New)( name, "set" );
468 469

            psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
470 471
            E_(mvar_AppendNewVar)( itm, "name", psz );
            E_(mvar_AppendNewVar)( itm, "uri", psz );
472 473 474
            free( psz );

            sprintf( value, "Node" );
475
            E_(mvar_AppendNewVar)( itm, "type", value );
476 477

            sprintf( value, "%d", p_node->input.i_id );
478
            E_(mvar_AppendNewVar)( itm, "index", value );
479 480

            sprintf( value, "%d", p_node->i_children);
481
            E_(mvar_AppendNewVar)( itm, "i_children", value );
482 483

            sprintf( value, "%d", i_depth );
484
            E_(mvar_AppendNewVar)( itm, "depth", value );
485

486 487 488 489 490 491 492 493 494
            if( p_node->i_flags & PLAYLIST_RO_FLAG )
            {
                E_(mvar_AppendNewVar)( itm, "ro", "ro" );
            }
            else
            {
                E_(mvar_AppendNewVar)( itm, "ro", "rw" );
            }

495
            E_(mvar_AppendVar)( s, itm );
496 497 498 499 500 501 502 503 504 505 506 507 508

            for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
                E_(PlaylistListNode)( p_intf, p_pl,
                                      p_node->pp_children[i_child],
                                      name, s, i_depth + 1);

        }
    }
}

/****************************************************************************
 * Seek command parsing handling
 ****************************************************************************/
509
void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
{
    intf_sys_t     *p_sys = p_intf->p_sys;
    vlc_value_t val;
    int i_stock = 0;
    uint64_t i_length;
    int i_value = 0;
    int i_relative = 0;
#define POSITION_ABSOLUTE 12
#define POSITION_REL_FOR 13
#define POSITION_REL_BACK 11
#define VL_TIME_ABSOLUTE 0
#define VL_TIME_REL_FOR 1
#define VL_TIME_REL_BACK -1
    if( p_sys->p_input )
    {
        var_Get( p_sys->p_input, "length", &val );
        i_length = val.i_time;

        while( p_value[0] != '\0' )
        {
            switch(p_value[0])
            {
                case '+':
                {
                    i_relative = VL_TIME_REL_FOR;
                    p_value++;
                    break;
                }
                case '-':
                {
                    i_relative = VL_TIME_REL_BACK;
                    p_value++;
                    break;
                }
                case '0': case '1': case '2': case '3': case '4':
                case '5': case '6': case '7': case '8': case '9':
                {
                    i_stock = strtol( p_value , &p_value , 10 );
                    break;
                }
                case '%': /* for percentage ie position */
                {
                    i_relative += POSITION_ABSOLUTE;
                    i_value = i_stock;
                    i_stock = 0;
                    p_value[0] = '\0';
                    break;
                }
                case ':':
                {
                    i_value = 60 * (i_value + i_stock) ;
                    i_stock = 0;
                    p_value++;
                    break;
                }
                case 'h': case 'H': /* hours */
                {
                    i_value += 3600 * i_stock;
                    i_stock = 0;
569 570 571 572
                    /* other characters which are not numbers are not
                     * important */
                    while( ((p_value[0] < '0') || (p_value[0] > '9'))
                           && (p_value[0] != '\0') )
573 574 575 576 577 578 579 580 581 582
                    {
                        p_value++;
                    }
                    break;
                }
                case 'm': case 'M': case '\'': /* minutes */
                {
                    i_value += 60 * i_stock;
                    i_stock = 0;
                    p_value++;
583 584
                    while( ((p_value[0] < '0') || (p_value[0] > '9'))
                           && (p_value[0] != '\0') )
585 586 587 588 589 590 591 592 593
                    {
                        p_value++;
                    }
                    break;
                }
                case 's': case 'S': case '"':  /* seconds */
                {
                    i_value += i_stock;
                    i_stock = 0;
594 595
                    while( ((p_value[0] < '0') || (p_value[0] > '9'))
                           && (p_value[0] != '\0') )
596 597 598 599 600 601 602 603 604 605 606 607 608
                    {
                        p_value++;
                    }
                    break;
                }
                default:
                {
                    p_value++;
                    break;
                }
            }
        }

609 610
        /* if there is no known symbol, I consider it as seconds.
         * Otherwise, i_stock = 0 */
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
        i_value += i_stock;

        switch(i_relative)
        {
            case VL_TIME_ABSOLUTE:
            {
                if( (uint64_t)( i_value ) * 1000000 <= i_length )
                    val.i_time = (uint64_t)( i_value ) * 1000000;
                else
                    val.i_time = i_length;

                var_Set( p_sys->p_input, "time", val );
                msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
                break;
            }
            case VL_TIME_REL_FOR:
            {
                var_Get( p_sys->p_input, "time", &val );
                if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
                {
                    val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
                } else
                {
                    val.i_time = i_length;
                }
                var_Set( p_sys->p_input, "time", val );
                msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
                break;
            }
            case VL_TIME_REL_BACK:
            {
                var_Get( p_sys->p_input, "time", &val );
                if( (int64_t)( i_value ) * 1000000 > val.i_time )
                {
                    val.i_time = 0;
                } else
                {
                    val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
                }
                var_Set( p_sys->p_input, "time", val );
                msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
                break;
            }
            case POSITION_ABSOLUTE:
            {
656 657
                val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
                                            0.0 ), 100.0 );
658
                var_Set( p_sys->p_input, "position", val );
659
                msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
660 661 662 663 664
                break;
            }
            case POSITION_REL_FOR:
            {
                var_Get( p_sys->p_input, "position", &val );
665 666
                val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
                                             0.0 ) , 100.0 );
667
                var_Set( p_sys->p_input, "position", val );
668 669
                msg_Dbg( p_intf, "requested seek percent forward: %d%%",
                         i_value );
670 671 672 673 674
                break;
            }
            case POSITION_REL_BACK:
            {
                var_Get( p_sys->p_input, "position", &val );
675 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 679
                msg_Dbg( p_intf, "requested seek percent backward: %d%%",
                         i_value );
680 681 682 683
                break;
            }
            default:
            {
684
                msg_Dbg( p_intf, "invalid seek request" );
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
                break;
            }
        }
    }
#undef POSITION_ABSOLUTE
#undef POSITION_REL_FOR
#undef POSITION_REL_BACK
#undef VL_TIME_ABSOLUTE
#undef VL_TIME_REL_FOR
#undef VL_TIME_REL_BACK
}


/****************************************************************************
 * URI Parsing functions
 ****************************************************************************/
701
int E_(TestURIParam)( char *psz_uri, const char *psz_name )
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
{
    char *p = psz_uri;

    while( (p = strstr( p, psz_name )) )
    {
        /* Verify that we are dealing with a post/get argument */
        if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
              && p[strlen(psz_name)] == '=' )
        {
            return VLC_TRUE;
        }
        p++;
    }

    return VLC_FALSE;
}
718
char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
                             char *psz_value, int i_value_max )
{
    char *p = psz_uri;

    while( (p = strstr( p, psz_name )) )
    {
        /* Verify that we are dealing with a post/get argument */
        if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
              && p[strlen(psz_name)] == '=' )
            break;
        p++;
    }

    if( p )
    {
        int i_len;

        p += strlen( psz_name );
        if( *p == '=' ) p++;

        if( strchr( p, '&' ) )
        {
            i_len = strchr( p, '&' ) - p;
        }
        else
        {
            /* for POST method */
            if( strchr( p, '\n' ) )
            {
                i_len = strchr( p, '\n' ) - p;
                if( i_len && *(p+i_len-1) == '\r' ) i_len--;
            }
            else
            {
                i_len = strlen( p );
            }
        }
        i_len = __MIN( i_value_max - 1, i_len );
        if( i_len > 0 )
        {
            strncpy( psz_value, p, i_len );
            psz_value[i_len] = '\0';
        }
        else
        {
            strncpy( psz_value, "", i_value_max );
        }
        p += i_len;
    }
    else
    {
        strncpy( psz_value, "", i_value_max );
    }

    return p;
}

776
void E_(DecodeEncodedURI)( char *psz )
777
{
778
    decode_encoded_URI( psz );
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
}

/* Since the resulting string is smaller we can work in place, so it is
 * permitted to have psz == new. new points to the first word of the
 * string, the function returns the remaining string. */
char *E_(FirstWord)( char *psz, char *new )
{
    vlc_bool_t b_end;

    while( *psz == ' ' )
        psz++;

    while( *psz != '\0' && *psz != ' ' )
    {
        if( *psz == '\'' )
        {
            char c = *psz++;
            while( *psz != '\0' && *psz != c )
            {
                if( *psz == '\\' && psz[1] != '\0' )
                    psz++;
                *new++ = *psz++;
            }
            if( *psz == c )
                psz++;
        }
        else
        {
            if( *psz == '\\' && psz[1] != '\0' )
                psz++;
            *new++ = *psz++;
        }
    }
    b_end = !*psz;

    *new++ = '\0';
    if( !b_end )
        return psz + 1;
    else
        return NULL;
}
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
/**********************************************************************
 * Find_end_MRL: Find the end of the sentence :
 * this function parses the string psz and find the end of the item
 * and/or option with detecting the " and ' problems.
 * returns NULL if an error is detected, otherwise, returns a pointer
 * of the end of the sentence (after the last character)
**********************************************************************/
static char *Find_end_MRL( char *psz )
{
    char *s_sent = psz;

    switch( *s_sent )
    {
        case '\"':
        {
            s_sent++;

            while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
            {
                if( *s_sent == '\'' )
                {
                    s_sent = Find_end_MRL( s_sent );

                    if( s_sent == NULL )
                    {
                        return NULL;
                    }
                } else
                {
                    s_sent++;
                }
            }

            if( *s_sent == '\"' )
            {
                s_sent++;
                return s_sent;
            } else  /* *s_sent == '\0' , which means the number of " is incorrect */
            {
                return NULL;
            }
            break;
        }
        case '\'':
        {
            s_sent++;
866

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
            while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
            {
                if( *s_sent == '\"' )
                {
                    s_sent = Find_end_MRL( s_sent );

                    if( s_sent == NULL )
                    {
                        return NULL;
                    }
                } else
                {
                    s_sent++;
                }
            }

            if( *s_sent == '\'' )
            {
                s_sent++;
                return s_sent;
            } else  /* *s_sent == '\0' , which means the number of ' is incorrect */
            {
                return NULL;
            }
            break;
        }
        default: /* now we can look for spaces */
        {
            while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
            {
                if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
                {
                    s_sent = Find_end_MRL( s_sent );
                } else
                {
                    s_sent++;
                }
            }
            return s_sent;
        }
    }
}
909 910 911 912 913
/**********************************************************************
 * parse_MRL: parse the MRL, find the mrl string and the options,
 * create an item with all information in it, and return the item.
 * return NULL if there is an error.
 **********************************************************************/
914
playlist_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *psz,
915 916
                                   char *psz_name )
{
917 918
    char **ppsz_options = NULL;
    char *mrl;
919
    char *s_mrl = psz;
920
    int i_error = 0;
921
    char *s_temp;
922 923
    int i = 0;
    int i_options = 0;
924 925
    playlist_item_t * p_item = NULL;

926
    /* In case there is spaces before the mrl */
927
    while( *s_mrl == ' ' )
928 929
        s_mrl++;

930
    /* extract the mrl */
931
    s_temp = strstr( s_mrl , " :" );
932 933 934
    if( s_temp == NULL )
    {
        s_temp = s_mrl + strlen( s_mrl );
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
    } else
    {
        while( (*s_temp == ' ') && (s_temp != s_mrl ) )
        {
            s_temp--;
        }
        s_temp++;
    }

    /* if the mrl is between " or ', we must remove them */
    if( (*s_mrl == '\'') || (*s_mrl == '\"') )
    {
        mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
        strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
        mrl[ s_temp - s_mrl - 2 ] = '\0';
    } else
    {
        mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
        strncpy( mrl , s_mrl , s_temp - s_mrl );
        mrl[ s_temp - s_mrl ] = '\0';
955 956 957 958 959
    }

    s_mrl = s_temp;

    /* now we can take care of the options */
960
    while( (*s_mrl != '\0') && (i_error == 0) )
961
    {
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
        switch( *s_mrl )
        {
            case ' ':
            {
                s_mrl++;
                break;
            }
            case ':': /* an option */
            {
                s_temp = Find_end_MRL( s_mrl );

                if( s_temp == NULL )
                {
                    i_error = 1;
                }
                else
                {
                    i_options++;
                    ppsz_options = realloc( ppsz_options , i_options *
                                            sizeof(char *) );
                    ppsz_options[ i_options - 1 ] =
                        malloc( (s_temp - s_mrl + 1) * sizeof(char) );

                    strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
                             s_temp - s_mrl );

                    /* don't forget to finish the string with a '\0' */
                    (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';

                    s_mrl = s_temp;
                }
                break;
            }
            default:
            {
                i_error = 1;
                break;
            }
        }
    }

    if( i_error != 0 )
    {
        free( mrl );
    }
    else
    {
        /* now create an item */
        p_item = playlist_ItemNew( p_intf, mrl, psz_name );
        for( i = 0 ; i< i_options ; i++ )
1012
        {
1013
            playlist_ItemAddOption( p_item, ppsz_options[i] );
1014 1015 1016
        }
    }

1017 1018 1019
    for( i = 0; i < i_options; i++ ) free( ppsz_options[i] );
    if( i_options ) free( ppsz_options );

1020 1021
    return p_item;
}
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
/**********************************************************************
 * RealPath: parse ../, ~ and path stuff
 **********************************************************************/
char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
{
    char *psz_dir;
    char *p;
    int i_len = strlen(psz_src);
    char sep;

#if defined( WIN32 )
    sep = '\\';
#else
    sep = '/';
#endif

    psz_dir = malloc( i_len + 2 );
    strcpy( psz_dir, psz_src );

    /* Add a trailing sep to ease the .. step */
    psz_dir[i_len] = sep;
    psz_dir[i_len + 1] = '\0';

#ifdef WIN32
    /* Convert all / to native separator */
    p = psz_dir;
    while( (p = strchr( p, '/' )) != NULL )
    {
        *p = sep;
    }
#endif

    /* Remove multiple separators and /./ */
    p = psz_dir;
    while( (p = strchr( p, sep )) != NULL )
    {
        if( p[1] == sep )
            memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
        else if( p[1] == '.' && p[2] == sep )
            memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
        else
            p++;
    }

    if( psz_dir[0] == '~' )
    {
        char *dir = malloc( strlen(psz_dir)
1069
                             + strlen(p_intf->p_vlc->psz_userdir) );
1070
        /* This is incomplete : we should also support the ~cmassiot/ syntax. */
1071
        sprintf( dir, "%s%s", p_intf->p_vlc->psz_userdir, psz_dir + 1 );
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
        free( psz_dir );
        psz_dir = dir;
    }

    if( strlen(psz_dir) > 2 )
    {
        /* Fix all .. dir */
        p = psz_dir + 3;
        while( (p = strchr( p, sep )) != NULL )
        {
            if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
            {
                char *q;
                p[-3] = '\0';
                if( (q = strrchr( psz_dir, sep )) != NULL )
                {
                    memmove( q + 1, p + 1, strlen(p + 1) + 1 );
                    p = q + 1;
                }
                else
                {
1093
                    memmove( psz_dir, p, strlen(p) + 1 );
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
                    p = psz_dir + 3;
                }
            }
            else
                p++;
        }
    }

    /* Remove trailing sep if there are at least 2 sep in the string
     * (handles the C:\ stuff) */
    p = strrchr( psz_dir, sep );
    if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )
        *p = '\0';

    return psz_dir;
}