util.c 28.3 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 27 28 29
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

30
#include <vlc_common.h>
31
#include "http.h"
32
#include "vlc_strings.h"
33 34 35 36 37 38

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

/* ToUrl: create a good name for an url from filename */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
39
char *FileToUrl( char *name, bool *pb_index )
40 41 42 43 44
{
    char *url, *p;

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

45
    *pb_index = false;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    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 == '\\' )
69
            *name = '/';
70 71 72 73 74 75 76 77 78 79
        name++;
    }
#endif

    /* index.* -> / */
    if( ( p = strrchr( url, '/' ) ) != NULL )
    {
        if( !strncmp( p, "/index.", 7 ) )
        {
            p[1] = '\0';
80
            *pb_index = true;
81 82 83 84 85 86
        }
    }
    return url;
}

/* Load a file */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
87
int FileLoad( FILE *f, char **pp_data, int *pi_data )
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
{
    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 */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
109
int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
110
                        char *psz_dir )
111 112 113 114 115 116 117 118 119 120 121 122
{
    intf_sys_t     *p_sys = p_intf->p_sys;
    char           dir[MAX_DIR_SIZE];
    DIR           *p_dir;
    vlc_acl_t     *p_acl;
    FILE          *file;

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

    int           i_dirlen;

123 124 125 126 127 128 129 130
    char sep;

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

131
    if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
132
    {
133 134
        if( errno != ENOENT && errno != ENOTDIR )
            msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
135 136 137 138 139 140
        return VLC_EGENERIC;
    }

    i_dirlen = strlen( psz_dir );
    if( i_dirlen + 10 > MAX_DIR_SIZE )
    {
141
        msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
142
        closedir( p_dir );
143 144 145 146 147
        return 0;
    }

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

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
148
    snprintf( dir, sizeof( dir ), "%s%c.access", psz_dir, sep );
149
    if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
150 151 152 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
    {
        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 );
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
182
    snprintf( dir, sizeof( dir ), "%s%c.hosts", psz_dir, sep );
183
    p_acl = ACL_Create( p_intf, false );
184 185 186
    if( ACL_LoadFile( p_acl, dir ) )
    {
        ACL_Destroy( p_acl );
187 188 189 190

        struct stat st;
        if( stat( dir, &st ) == 0 )
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
191
            closedir( p_dir );
192 193
            return VLC_EGENERIC;
        }
194 195 196 197 198
        p_acl = NULL;
    }

    for( ;; )
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
199
        char *psz_filename;
200
        /* parse psz_src dir */
201
        if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
202 203 204 205
        {
            break;
        }

206 207
        if( ( psz_filename[0] == '.' )
         || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
208 209
        {
            free( psz_filename );
210
            continue;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
211
        }
212

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
213 214
        snprintf( dir, sizeof( dir ), "%s%c%s", psz_dir, sep, psz_filename );
        free( psz_filename );
215

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
216
        if( ParseDirectory( p_intf, psz_root, dir ) )
217
        {
218 219
            httpd_file_sys_t *f = NULL;
            httpd_handler_sys_t *h = NULL;
220
            bool b_index;
221
            char *psz_name, *psz_ext;
222

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
223
            psz_name = FileToUrl( &dir[strlen( psz_root )], &b_index );
224
            psz_ext = strrchr( dir, '.' );
225 226 227 228 229 230 231 232 233 234 235
            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;
236
                    f->b_handler = true;
237 238 239 240 241 242
                    h->p_association = p_sys->pp_handlers[i];
                }
            }
            if( f == NULL )
            {
                f = malloc( sizeof( httpd_file_sys_t ) );
243
                f->b_handler = false;
244 245 246 247 248 249
            }

            f->p_intf  = p_intf;
            f->p_file = NULL;
            f->p_redir = NULL;
            f->p_redir2 = NULL;
250
            f->file = strdup (dir);
251
            f->name = psz_name;
252
            f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? true : false;
253 254 255 256

            if( !f->name )
            {
                msg_Err( p_intf , "unable to parse directory" );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
257
                closedir( p_dir );
258 259 260 261 262 263
                free( f );
                return( VLC_ENOMEM );
            }
            msg_Dbg( p_intf, "file=%s (url=%s)",
                     f->file, f->name );

264 265
            if( !f->b_handler )
            {
266
                char *psz_type = strdup( "text/html; charset=UTF-8" );
267 268 269 270 271 272 273 274 275 276 277 278
                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] = ' ';
                    }
                }
279 280
                f->p_file = httpd_FileNew( p_sys->p_httpd_host,
                                           f->name,
281
                                           f->b_html ? psz_type : NULL,
282
                                           user, password, p_acl,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
283
                                           HttpCallback, f );
284
                free( psz_type );
285 286 287 288 289 290
                if( f->p_file != NULL )
                {
                    TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
                }
            }
            else
291
            {
292 293 294
                h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
                                                 f->name,
                                                 user, password, p_acl,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
295
                                                 HandlerCallback, h );
296 297 298 299 300
                if( h->p_handler != NULL )
                {
                    TAB_APPEND( p_sys->i_files, p_sys->pp_files,
                                (httpd_file_sys_t *)h );
                }
301
            }
302

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
            /* 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." ) ) )
                {
Rémi Duraffort's avatar
Rémi Duraffort committed
318 319 320 321 322
                    if( asprintf( &psz_redir, "%s%s", f->name, p ) != -1 )
                    {
                        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 );
323

Rémi Duraffort's avatar
Rémi Duraffort committed
324 325
                        free( psz_redir );
                    }
326 327 328 329 330
                }
            }
        }
    }

331 332
    free( user );
    free( password );
333 334

    ACL_Destroy( p_acl );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
335
    closedir( p_dir );
336 337 338 339 340 341 342 343

    return VLC_SUCCESS;
}


/*************************************************************************
 * Playlist stuff
 *************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
344
void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl,
345 346 347
                           playlist_item_t *p_node, char *name, mvar_t *s,
                           int i_depth )
{
348 349
    if( !p_node || !p_node->p_input )
        return;
350

351 352 353 354 355 356 357
    if( p_node->i_children == -1 )
    {
        char value[512];
        char *psz;
        playlist_item_t * p_item = playlist_CurrentPlayingItem( p_pl );
        if( !p_item || !p_item->p_input )
            return;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
358 359

        mvar_t *itm = mvar_New( name, "set" );
360 361 362 363
        if( p_item->p_input->i_id == p_node->p_input->i_id )
            mvar_AppendNewVar( itm, "current", "1" );
        else
            mvar_AppendNewVar( itm, "current", "0" );
364

365 366
        sprintf( value, "%d", p_node->i_id );
        mvar_AppendNewVar( itm, "index", value );
367

368 369 370
        psz = input_item_GetName( p_node->p_input );
        mvar_AppendNewVar( itm, "name", psz );
        free( psz );
371

372 373 374
        psz = input_item_GetURI( p_node->p_input );
        mvar_AppendNewVar( itm, "uri", psz );
        free( psz );
375

376 377
        sprintf( value, "Item");
        mvar_AppendNewVar( itm, "type", value );
378

379 380
        sprintf( value, "%d", i_depth );
        mvar_AppendNewVar( itm, "depth", value );
381

382 383
        if( p_node->i_flags & PLAYLIST_RO_FLAG )
            mvar_AppendNewVar( itm, "ro", "ro" );
384
        else
385
            mvar_AppendNewVar( itm, "ro", "rw" );
386

387
        sprintf( value, "%"PRId64, input_item_GetDuration( p_node->p_input ) );
388
        mvar_AppendNewVar( itm, "duration", value );
389

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 426 427 428 429 430 431 432 433 434 435 436 437 438 439
        //Adding extra meta-information to each playlist item

        psz = input_item_GetTitle( p_node->p_input );
        mvar_AppendNewVar( itm, "title", psz );

        psz = input_item_GetArtist( p_node->p_input );
        mvar_AppendNewVar( itm, "artist", psz );

        psz = input_item_GetGenre( p_node->p_input );
        mvar_AppendNewVar( itm, "genre", psz );

        psz = input_item_GetCopyright( p_node->p_input );
        mvar_AppendNewVar( itm, "copyright", psz );

        psz = input_item_GetAlbum( p_node->p_input );
        mvar_AppendNewVar( itm, "album", psz );

        psz = input_item_GetTrackNum( p_node->p_input );
        mvar_AppendNewVar( itm, "track", psz );

        psz = input_item_GetDescription( p_node->p_input );
        mvar_AppendNewVar( itm, "description", psz );

        psz = input_item_GetRating( p_node->p_input );
        mvar_AppendNewVar( itm, "rating", psz );

        psz = input_item_GetDate( p_node->p_input );
        mvar_AppendNewVar( itm, "date", psz );

        psz = input_item_GetURL( p_node->p_input );
        mvar_AppendNewVar( itm, "url", psz );

        psz = input_item_GetLanguage( p_node->p_input );
        mvar_AppendNewVar( itm, "language", psz );

        psz = input_item_GetNowPlaying( p_node->p_input );
        mvar_AppendNewVar( itm, "now_playing", psz );

        psz = input_item_GetPublisher( p_node->p_input );
        mvar_AppendNewVar( itm, "publisher", psz );

        psz = input_item_GetEncodedBy( p_node->p_input );
        mvar_AppendNewVar( itm, "encoded_by", psz );

        psz = input_item_GetArtURL( p_node->p_input );
        mvar_AppendNewVar( itm, "art_url", psz );

        psz = input_item_GetTrackID( p_node->p_input );
        mvar_AppendNewVar( itm, "track_id", psz );

440 441 442 443 444 445 446
        mvar_AppendVar( s, itm );
    }
    else
    {
        char value[512];
        int i_child;
        mvar_t *itm = mvar_New( name, "set" );
447

448 449
        mvar_AppendNewVar( itm, "name", p_node->p_input->psz_name );
        mvar_AppendNewVar( itm, "uri", p_node->p_input->psz_name );
450

451 452
        sprintf( value, "Node" );
        mvar_AppendNewVar( itm, "type", value );
453

454 455
        sprintf( value, "%d", p_node->i_id );
        mvar_AppendNewVar( itm, "index", value );
456

457 458
        sprintf( value, "%d", p_node->i_children);
        mvar_AppendNewVar( itm, "i_children", value );
459

460 461
        sprintf( value, "%d", i_depth );
        mvar_AppendNewVar( itm, "depth", value );
462

463 464 465 466
        if( p_node->i_flags & PLAYLIST_RO_FLAG )
            mvar_AppendNewVar( itm, "ro", "ro" );
        else
            mvar_AppendNewVar( itm, "ro", "rw" );
467

468 469 470 471 472
        mvar_AppendVar( s, itm );

        for( i_child = 0 ; i_child < p_node->i_children ; i_child++ )
             PlaylistListNode( p_intf, p_pl, p_node->pp_children[i_child],
                               name, s, i_depth + 1);
473 474 475 476 477 478
    }
}

/****************************************************************************
 * Seek command parsing handling
 ****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
479
void HandleSeek( intf_thread_t *p_intf, char *p_value )
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 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
{
    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;
539 540 541 542
                    /* other characters which are not numbers are not
                     * important */
                    while( ((p_value[0] < '0') || (p_value[0] > '9'))
                           && (p_value[0] != '\0') )
543 544 545 546 547 548 549 550 551 552
                    {
                        p_value++;
                    }
                    break;
                }
                case 'm': case 'M': case '\'': /* minutes */
                {
                    i_value += 60 * i_stock;
                    i_stock = 0;
                    p_value++;
553 554
                    while( ((p_value[0] < '0') || (p_value[0] > '9'))
                           && (p_value[0] != '\0') )
555 556 557 558 559 560 561 562 563
                    {
                        p_value++;
                    }
                    break;
                }
                case 's': case 'S': case '"':  /* seconds */
                {
                    i_value += i_stock;
                    i_stock = 0;
564 565
                    while( ((p_value[0] < '0') || (p_value[0] > '9'))
                           && (p_value[0] != '\0') )
566 567 568 569 570 571 572 573 574 575 576 577 578
                    {
                        p_value++;
                    }
                    break;
                }
                default:
                {
                    p_value++;
                    break;
                }
            }
        }

579 580
        /* if there is no known symbol, I consider it as seconds.
         * Otherwise, i_stock = 0 */
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
        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:
            {
626 627
                val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
                                            0.0 ), 100.0 );
628
                var_Set( p_sys->p_input, "position", val );
629
                msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
630 631 632 633 634
                break;
            }
            case POSITION_REL_FOR:
            {
                var_Get( p_sys->p_input, "position", &val );
635 636
                val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
                                             0.0 ) , 100.0 );
637
                var_Set( p_sys->p_input, "position", val );
638 639
                msg_Dbg( p_intf, "requested seek percent forward: %d%%",
                         i_value );
640 641 642 643 644
                break;
            }
            case POSITION_REL_BACK:
            {
                var_Get( p_sys->p_input, "position", &val );
645 646
                val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
                                             0.0 ) , 100.0 );
647
                var_Set( p_sys->p_input, "position", val );
648 649
                msg_Dbg( p_intf, "requested seek percent backward: %d%%",
                         i_value );
650 651 652 653
                break;
            }
            default:
            {
654
                msg_Dbg( p_intf, "invalid seek request" );
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
                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
 ****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
671
int TestURIParam( char *psz_uri, const char *psz_name )
672 673 674 675 676 677 678 679 680
{
    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)] == '=' )
        {
681
            return true;
682 683 684 685
        }
        p++;
    }

686
    return false;
687
}
688

689
static const char *FindURIValue( const char *psz_uri, const char *restrict psz_name,
690
                           size_t *restrict p_len )
691
{
692
    const char *p = psz_uri, *end;
693
    size_t len;
694 695 696 697 698 699 700 701 702 703

    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++;
    }

704
    if( p == NULL )
705
    {
706 707 708
        *p_len = 0;
        return NULL;
    }
709

710 711
    p += strlen( psz_name );
    if( *p == '=' ) p++;
712

713 714 715 716 717 718 719
    if( ( end = strchr( p, '\n' ) ) != NULL )
    {
        /* POST method */
        if( ( end > p ) && ( end[-1] == '\r' ) )
            end--;

        len = end - p;
720 721 722
    }
    else
    {
723 724 725 726 727
        /* GET method */
        if( ( end = strchr( p, '&' ) ) != NULL )
            len = end - p;
        else
            len = strlen( p );
728 729
    }

730
    *p_len = len;
731 732 733
    return p;
}

734
const char *ExtractURIValue( const char *restrict psz_uri,
735 736 737 738
                           const char *restrict psz_name,
                           char *restrict psz_buf, size_t bufsize )
{
    size_t len;
739 740
    const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
    const char *psz_next;
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761

    if( psz_value == NULL )
    {
        if( bufsize > 0 )
            *psz_buf = '\0';
        return NULL;
    }

    psz_next = psz_value + len;

    if( len >= bufsize )
        len = bufsize - 1;

    if( len > 0 )
        strncpy( psz_buf, psz_value, len );
    if( bufsize > 0 )
        psz_buf[len] = '\0';

    return psz_next;
}

762
char *ExtractURIString( const char *restrict psz_uri,
763 764 765
                            const char *restrict psz_name )
{
    size_t len;
766
    const char *psz_value = FindURIValue( psz_uri, psz_name, &len );
767 768 769 770 771 772 773 774 775 776 777 778 779 780

    if( psz_value == NULL )
        return NULL;

    char *res = malloc( len + 1 );
    if( res == NULL )
        return NULL;

    memcpy( res, psz_value, len );
    res[len] = '\0';

    return res;
}

781 782 783
/* 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. */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
784
char *FirstWord( char *psz, char *new )
785
{
786
    bool b_end;
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

    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
/**********************************************************************
 * MRLParse: 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.
 **********************************************************************/
826

827 828 829 830
/* Function analog to FirstWord except that it relies on colon instead
 * of space to delimit option boundaries. */
static char *FirstOption( char *psz, char *new )
{
831
    bool b_end, b_start = true;
832

833 834
    while( *psz == ' ' )
        psz++;
835

836 837 838
    while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
    {
        if( *psz == '\'' )
839
        {
840 841
            char c = *psz++;
            while( *psz != '\0' && *psz != c )
842
            {
843 844 845
                if( *psz == '\\' && psz[1] != '\0' )
                    psz++;
                *new++ = *psz++;
846
                b_start = false;
847
            }
848 849
            if( *psz == c )
                psz++;
850
        }
851
        else
852
        {
853 854 855
            if( *psz == '\\' && psz[1] != '\0' )
                psz++;
            *new++ = *psz++;
856
            b_start = false;
857 858
        }
    }
859 860 861 862 863 864 865 866 867 868 869
    b_end = !*psz;

    if ( !b_start )
        while (new[-1] == ' ')
            new--;

    *new++ = '\0';
    if( !b_end )
        return psz + 1;
    else
        return NULL;
870
}
871

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
872
input_item_t *MRLParse( intf_thread_t *p_intf, const char *mrl,
873
                                   char *psz_name )
874
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
875 876 877
    char *psz = strdup( mrl ), *s_mrl = psz, *s_temp;
    if( psz == NULL )
        return NULL;
878
    /* extract the mrl */
879
    s_temp = FirstOption( s_mrl, s_mrl );
880 881 882 883 884
    if( s_temp == NULL )
    {
        s_temp = s_mrl + strlen( s_mrl );
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
885 886 887
    input_item_t *p_input = input_item_New( p_intf, s_mrl, psz_name );
    if( p_input == NULL )
        return NULL;
888 889 890
    s_mrl = s_temp;

    /* now we can take care of the options */
891
    while ( *s_mrl != '\0' )
892
    {
893 894 895 896
        s_temp = FirstOption( s_mrl, s_mrl );
        if( s_mrl == '\0' )
            break;
        if( s_temp == NULL )
897
        {
898
            s_temp = s_mrl + strlen( s_mrl );
899
        }
900
        input_item_AddOption( p_input, s_mrl, VLC_INPUT_OPTION_TRUSTED );
901
        s_mrl = s_temp;
902 903
    }

904
    return p_input;
905
}
906

907 908 909
/**********************************************************************
 * RealPath: parse ../, ~ and path stuff
 **********************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
910
char *RealPath( const char *psz_src )
911 912 913 914
{
    char *psz_dir;
    char *p;
    int i_len = strlen(psz_src);
Rémi Denis-Courmont's avatar
Cleanup  
Rémi Denis-Courmont committed
915
    const char sep = DIR_SEP_CHAR;
916 917 918 919 920 921 922 923

    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';

Rémi Denis-Courmont's avatar
Cleanup  
Rémi Denis-Courmont committed
924
#if (DIR_SEP_CHAR != '/')
925 926 927 928 929 930 931 932
    /* Convert all / to native separator */
    p = psz_dir;
    while( (p = strchr( p, '/' )) != NULL )
    {
        *p = sep;
    }
#endif

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
933
    /* FIXME: this could be O(N) rather than O(N²)... */
934 935 936 937 938 939 940 941 942 943 944 945 946 947
    /* 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] == '~' )
    {
Rémi Denis-Courmont's avatar
Oups  
Rémi Denis-Courmont committed
948
        char *dir;
949
        asprintf( &dir, "%s%s", config_GetHomeDir(), psz_dir + 1 );
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
        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
                {
971
                    memmove( psz_dir, p, strlen(p) + 1 );
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
                    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;
}