http.c 92.7 KB
Newer Older
Gildas Bazin's avatar
 
Gildas Bazin committed
1
/*****************************************************************************
2
 * http.c :  http mini-server ;)
Gildas Bazin's avatar
 
Gildas Bazin committed
3
 *****************************************************************************
4
 * Copyright (C) 2001-2004 VideoLAN
Xavier Martin's avatar
Xavier Martin committed
5
 * $Id$
Gildas Bazin's avatar
 
Gildas Bazin committed
6 7
 *
 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8
 *          Laurent Aimar <fenrir@via.ecp.fr>
Gildas Bazin's avatar
 
Gildas Bazin committed
9 10 11 12 13
 *
 * 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.
14
 *
Gildas Bazin's avatar
 
Gildas Bazin committed
15 16 17 18 19 20 21 22 23 24 25 26 27
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28
/* TODO:
29 30 31 32
 * - clean up ?
 * - doc ! (mouarf ;)
 */

33
#include <stdlib.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
34 35 36
#include <vlc/vlc.h>
#include <vlc/intf.h>

37
#include <vlc/aout.h>
38 39
#include <vlc/vout.h> /* for fullscreen */

40
#include "vlc_httpd.h"
41
#include "vlc_vlm.h"
42 43 44 45 46 47 48 49 50 51 52

#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif
#ifdef HAVE_ERRNO_H
#   include <errno.h>
#endif
#ifdef HAVE_FCNTL_H
#   include <fcntl.h>
#endif

Gildas Bazin's avatar
 
Gildas Bazin committed
53
#ifdef HAVE_UNISTD_H
54 55 56
#   include <unistd.h>
#elif defined( WIN32 ) && !defined( UNDER_CE )
#   include <io.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
57 58
#endif

59 60 61
#if (!defined( WIN32 ) || defined(__MINGW32__))
/* Mingw has its own version of dirent */
#   include <dirent.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
62 63 64
#endif

/*****************************************************************************
65
 * Module descriptor
Gildas Bazin's avatar
 
Gildas Bazin committed
66
 *****************************************************************************/
67 68
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
69 70 71

#define HOST_TEXT N_( "Host address" )
#define HOST_LONGTEXT N_( \
Anil Daoud's avatar
Anil Daoud committed
72
    "You can set the address and port the http interface will bind to." )
73 74 75 76 77 78 79 80
#define SRC_TEXT N_( "Source directory" )
#define SRC_LONGTEXT N_( "Source directory" )

vlc_module_begin();
    set_description( _("HTTP remote control interface") );
        add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
        add_string ( "http-src",  NULL, NULL, SRC_TEXT,  SRC_LONGTEXT,  VLC_TRUE );
    set_capability( "interface", 0 );
81
    set_callbacks( Open, Close );
82 83 84 85 86 87
vlc_module_end();


/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
88 89
static void Run          ( intf_thread_t *p_intf );

90 91
static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
                           char *psz_dir );
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
static int DirectoryCheck( char *psz_dir )
{
    DIR           *p_dir;

#ifdef HAVE_SYS_STAT_H
    struct stat   stat_info;

    if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
    {
        return VLC_EGENERIC;
    }
#endif

    if( ( p_dir = opendir( psz_dir ) ) == NULL )
    {
        return VLC_EGENERIC;
    }
    closedir( p_dir );

    return VLC_SUCCESS;
}

115 116 117 118
static int  HttpCallback( httpd_file_sys_t *p_args,
                          httpd_file_t *,
                          uint8_t *p_request,
                          uint8_t **pp_data, int *pi_data );
119

Gildas Bazin's avatar
 
Gildas Bazin committed
120 121
static char *uri_extract_value( char *psz_uri, char *psz_name,
                                char *psz_value, int i_value_max );
122 123
static int uri_test_param( char *psz_uri, char *psz_name );

124
static void uri_decode_url_encoded( char *psz );
Gildas Bazin's avatar
 
Gildas Bazin committed
125

126 127
static char *Find_end_MRL( char *psz );

128
static playlist_item_t * parse_MRL( intf_thread_t * , char *psz );
129

Gildas Bazin's avatar
 
Gildas Bazin committed
130
/*****************************************************************************
131
 *
Gildas Bazin's avatar
 
Gildas Bazin committed
132
 *****************************************************************************/
133 134 135 136 137 138 139 140 141 142 143 144 145 146
typedef struct mvar_s
{
    char *name;
    char *value;

    int           i_field;
    struct mvar_s **field;
} mvar_t;

#define STACK_MAX 100
typedef struct
{
    char *stack[STACK_MAX];
    int  i_stack;
147
} rpn_stack_t;
148

149
struct httpd_file_sys_t
150
{
151 152 153
    intf_thread_t    *p_intf;
    httpd_file_t     *p_file;
    httpd_redirect_t *p_redir;
154 155 156

    char          *file;
    char          *name;
157 158

    vlc_bool_t    b_html;
159 160

    /* inited for each access */
161
    rpn_stack_t   stack;
162 163 164
    mvar_t        *vars;
};

Gildas Bazin's avatar
 
Gildas Bazin committed
165 166 167 168
struct intf_sys_t
{
    httpd_host_t        *p_httpd_host;

169 170
    int                 i_files;
    httpd_file_sys_t    **pp_files;
171 172 173

    playlist_t          *p_playlist;
    input_thread_t      *p_input;
174
    vlm_t               *p_vlm;
175 176 177 178 179 180 181
};



/*****************************************************************************
 * Activate: initialize and create stuff
 *****************************************************************************/
182
static int Open( vlc_object_t *p_this )
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
    intf_sys_t    *p_sys;
    char          *psz_host;
    char          *psz_address = "";
    int           i_port       = 0;
    char          *psz_src;

    psz_host = config_GetPsz( p_intf, "http-host" );
    if( psz_host )
    {
        char *psz_parser;
        psz_address = psz_host;

        psz_parser = strchr( psz_host, ':' );
        if( psz_parser )
        {
            *psz_parser++ = '\0';
            i_port = atoi( psz_parser );
        }
    }
    if( i_port <= 0 )
    {
        i_port= 8080;
    }

    msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );
    p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
Clément Stenac's avatar
Clément Stenac committed
211 212 213 214
    if( !p_intf->p_sys )
    {
        return( VLC_ENOMEM );
    }
215 216
    p_sys->p_playlist = NULL;
    p_sys->p_input    = NULL;
217
    p_sys->p_vlm      = NULL;
218

219 220
    p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_intf), psz_address, i_port );
    if( p_sys->p_httpd_host == NULL )
221 222 223 224 225 226 227 228 229 230 231
    {
        msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
        free( p_sys );
        return VLC_EGENERIC;
    }

    if( psz_host )
    {
        free( psz_host );
    }

232 233
    p_sys->i_files  = 0;
    p_sys->pp_files = NULL;
234

235 236
#if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
        ( defined(WIN32) && !defined(UNDER_CE ) )
237 238 239 240
    if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
    {
        char * psz_vlcpath = p_intf->p_libvlc->psz_vlcpath;
        psz_src = malloc( strlen(psz_vlcpath) + strlen("/share/http" ) + 1 );
Clément Stenac's avatar
Clément Stenac committed
241 242
        if( !psz_src )
        {
243
            return VLC_ENOMEM;
Clément Stenac's avatar
Clément Stenac committed
244
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
245 246 247
#if defined(WIN32)
        sprintf( psz_src, "%s/http", psz_vlcpath);
#else
248
        sprintf( psz_src, "%s/share/http", psz_vlcpath);
Gildas Bazin's avatar
 
Gildas Bazin committed
249
#endif
250 251 252
    }
#else
    psz_src = config_GetPsz( p_intf, "http-src" );
253 254 255 256 257 258 259 260 261 262 263 264

    if( !psz_src || *psz_src == '\0' )
    {
        if( !DirectoryCheck( "share/http" ) )
        {
            psz_src = strdup( "share/http" );
        }
        else if( !DirectoryCheck( DATA_PATH "/http" ) )
        {
            psz_src = strdup( DATA_PATH "/http" );
        }
    }
265
#endif
266

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    if( !psz_src || *psz_src == '\0' )
    {
        msg_Err( p_intf, "invalid src dir" );
        goto failed;
    }

    /* remove trainling \ or / */
    if( psz_src[strlen( psz_src ) - 1] == '\\' ||
        psz_src[strlen( psz_src ) - 1] == '/' )
    {
        psz_src[strlen( psz_src ) - 1] = '\0';
    }

    ParseDirectory( p_intf, psz_src, psz_src );


    if( p_sys->i_files <= 0 )
    {
285
        msg_Err( p_intf, "cannot find any files (%s)", psz_src );
286 287 288
        goto failed;
    }
    p_intf->pf_run = Run;
Gildas Bazin's avatar
 
Gildas Bazin committed
289
    free( psz_src );
290 291 292 293

    return VLC_SUCCESS;

failed:
Gildas Bazin's avatar
 
Gildas Bazin committed
294
    if( psz_src ) free( psz_src );
295 296 297 298 299
    if( p_sys->pp_files )
    {
        free( p_sys->pp_files );
    }
    httpd_HostDelete( p_sys->p_httpd_host );
300 301 302 303 304 305 306 307 308 309 310 311 312 313
    free( p_sys );
    return VLC_EGENERIC;
}

/*****************************************************************************
 * CloseIntf: destroy interface
 *****************************************************************************/
void Close ( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
    intf_sys_t    *p_sys = p_intf->p_sys;

    int i;

314 315 316 317 318
    if( p_sys->p_vlm )
    {
        vlm_Delete( p_sys->p_vlm );
    }

319 320
    for( i = 0; i < p_sys->i_files; i++ )
    {
321 322 323 324 325 326
       httpd_FileDelete( p_sys->pp_files[i]->p_file );
       if( p_sys->pp_files[i]->p_redir )
       {
           httpd_RedirectDelete( p_sys->pp_files[i]->p_redir );
       }

327 328 329 330
       free( p_sys->pp_files[i]->file );
       free( p_sys->pp_files[i]->name );
       free( p_sys->pp_files[i] );
    }
331 332 333 334 335 336
    if( p_sys->pp_files )
    {
        free( p_sys->pp_files );
    }
    httpd_HostDelete( p_sys->p_httpd_host );

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
    free( p_sys );
}

/*****************************************************************************
 * Run: http interface thread
 *****************************************************************************/
static void Run( intf_thread_t *p_intf )
{
    intf_sys_t     *p_sys = p_intf->p_sys;

    while( !p_intf->b_die )
    {
        /* get the playlist */
        if( p_sys->p_playlist == NULL )
        {
            p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
        }

        /* Manage the input part */
        if( p_sys->p_input == NULL )
        {
            if( p_sys->p_playlist )
            {
                p_sys->p_input =
                    vlc_object_find( p_sys->p_playlist,
                                     VLC_OBJECT_INPUT,
                                     FIND_CHILD );
            }
        }
        else if( p_sys->p_input->b_dead )
        {
            vlc_object_release( p_sys->p_input );
            p_sys->p_input = NULL;
        }


        /* Wait a bit */
        msleep( INTF_IDLE_SLEEP );
    }

    if( p_sys->p_input )
    {
        vlc_object_release( p_sys->p_input );
        p_sys->p_input = NULL;
    }

    if( p_sys->p_playlist )
    {
        vlc_object_release( p_sys->p_playlist );
        p_sys->p_playlist = NULL;
    }
}


/*****************************************************************************
 * Local functions
 *****************************************************************************/
#define MAX_DIR_SIZE 10240

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

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

Clément Stenac's avatar
Clément Stenac committed
405 406 407 408 409
    if( !url || !p )
    {
        return NULL;
    }

410
#ifdef WIN32
411 412
    while( *name == '\\' || *name == '/' )
#else
413
    while( *name == '/' )
414
#endif
415 416 417 418 419
    {
        name++;
    }

    *p++ = '/';
420 421 422 423 424
    strcpy( p, name );

#ifdef WIN32
    /* convert '\\' into '/' */
    name = p;
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
    while( *name )
    {
        if( *name == '\\' )
        {
            *p++ = '/';
        }
        name++;
    }
#endif

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

/****************************************************************************
 * ParseDirectory: parse recursively a directory, adding each file
 ****************************************************************************/
449 450
static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
                           char *psz_dir )
451 452 453 454 455 456 457 458 459 460 461 462 463 464
{
    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;
    struct dirent *p_dir_content;
    FILE          *file;

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

#ifdef HAVE_SYS_STAT_H
465
    if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
    {
        return VLC_EGENERIC;
    }
#endif

    if( ( p_dir = opendir( psz_dir ) ) == NULL )
    {
        msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
        return VLC_EGENERIC;
    }

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

    sprintf( dir, "%s/.access", psz_dir );
    if( ( file = fopen( dir, "r" ) ) != NULL )
    {
        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;
491 492
            while( i_size > 0 && ( line[i_size-1] == '\n' ||
                   line[i_size-1] == '\r' ) )
493 494 495 496 497 498 499 500 501 502 503 504 505 506
            {
                i_size--;
            }

            line[i_size] = '\0';

            p = strchr( line, ':' );
            if( p )
            {
                *p++ = '\0';
                user = strdup( line );
                password = strdup( p );
            }
        }
507 508
        msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
                 user, password, i_size );
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527

        fclose( file );
    }

    for( ;; )
    {
        /* parse psz_src dir */
        if( ( p_dir_content = readdir( p_dir ) ) == NULL )
        {
            break;
        }

        if( p_dir_content->d_name[0] == '.' )
        {
            continue;
        }
        sprintf( dir, "%s/%s", psz_dir, p_dir_content->d_name );
        if( ParseDirectory( p_intf, psz_root, dir ) )
        {
528 529
            httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );

530
            f->p_intf  = p_intf;
531 532
            f->p_file = NULL;
            f->p_redir = NULL;
533 534
            f->file = strdup( dir );
            f->name = FileToUrl( &dir[strlen( psz_root )] );
535
            f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
536

537
            if( !f->name )
Clément Stenac's avatar
Clément Stenac committed
538
            {
Anil Daoud's avatar
Anil Daoud committed
539
                msg_Err( p_intf , "unable to parse directory" );
Gildas Bazin's avatar
 
Gildas Bazin committed
540
                closedir( p_dir );
Gildas Bazin's avatar
 
Gildas Bazin committed
541
                free( f );
Clément Stenac's avatar
Clément Stenac committed
542 543
                return( VLC_ENOMEM );
            }
544 545 546 547 548 549 550 551
            msg_Dbg( p_intf, "file=%s (url=%s)",
                     f->file, f->name );

            f->p_file = httpd_FileNew( p_sys->p_httpd_host,
                                       f->name, f->b_html ? "text/html" : NULL,
                                       user, password,
                                       HttpCallback, f );

552 553
            if( f->p_file )
            {
554
                TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
555
            }
556 557
            /* For rep/ add a redir from rep to rep/ */
            if( f && f->name[strlen(f->name) - 1] == '/' )
558
            {
559 560 561 562 563 564
                char *psz_redir = strdup( f->name );
                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 );
565 566 567 568 569 570 571 572 573 574 575 576
            }
        }
    }

    if( user )
    {
        free( user );
    }
    if( password )
    {
        free( password );
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
577 578 579

    closedir( p_dir );

580 581 582 583 584 585 586 587 588 589 590
    return VLC_SUCCESS;
}

/****************************************************************************
 * var and set handling
 ****************************************************************************/

static mvar_t *mvar_New( char *name, char *value )
{
    mvar_t *v = malloc( sizeof( mvar_t ) );

Clément Stenac's avatar
Clément Stenac committed
591
    if( !v ) return NULL;
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 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 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 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
    v->name = strdup( name );
    v->value = strdup( value ? value : "" );

    v->i_field = 0;
    v->field = malloc( sizeof( mvar_t * ) );
    v->field[0] = NULL;

    return v;
}

static void mvar_Delete( mvar_t *v )
{
    int i;

    free( v->name );
    free( v->value );

    for( i = 0; i < v->i_field; i++ )
    {
        mvar_Delete( v->field[i] );
    }
    free( v->field );
    free( v );
}

static void mvar_AppendVar( mvar_t *v, mvar_t *f )
{
    v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
    v->field[v->i_field] = f;
    v->i_field++;
}

static mvar_t *mvar_Duplicate( mvar_t *v )
{
    int i;
    mvar_t *n;

    n = mvar_New( v->name, v->value );
    for( i = 0; i < v->i_field; i++ )
    {
        mvar_AppendVar( n, mvar_Duplicate( v->field[i] ) );
    }

    return n;
}

static void mvar_PushVar( mvar_t *v, mvar_t *f )
{
    v->field = realloc( v->field, sizeof( mvar_t * ) * ( v->i_field + 2 ) );
    if( v->i_field > 0 )
    {
        memmove( &v->field[1], &v->field[0], sizeof( mvar_t * ) * v->i_field );
    }
    v->field[0] = f;
    v->i_field++;
}

static void mvar_RemoveVar( mvar_t *v, mvar_t *f )
{
    int i;
    for( i = 0; i < v->i_field; i++ )
    {
        if( v->field[i] == f )
        {
            break;
        }
    }
    if( i >= v->i_field )
    {
        return;
    }

    if( i + 1 < v->i_field )
    {
        memmove( &v->field[i], &v->field[i+1], sizeof( mvar_t * ) * ( v->i_field - i - 1 ) );
    }
    v->i_field--;
    /* FIXME should do a realloc */
}

static mvar_t *mvar_GetVar( mvar_t *s, char *name )
{
    int i;
    char base[512], *field, *p;
    int  i_index;

    /* format: name[index].field */

    field = strchr( name, '.' );
    if( field )
    {
        int i = field - name;
        strncpy( base, name, i );
        base[i] = '\0';
        field++;
    }
    else
    {
        strcpy( base, name );
    }

    if( ( p = strchr( base, '[' ) ) )
    {
        *p++ = '\0';
        sscanf( p, "%d]", &i_index );
        if( i_index < 0 )
        {
            return NULL;
        }
    }
    else
    {
        i_index = 0;
    }

    for( i = 0; i < s->i_field; i++ )
    {
        if( !strcmp( s->field[i]->name, base ) )
        {
            if( i_index > 0 )
            {
                i_index--;
            }
            else
            {
                if( field )
                {
                    return mvar_GetVar( s->field[i], field );
                }
                else
                {
                    return s->field[i];
                }
            }
        }
    }
    return NULL;
}



static char *mvar_GetValue( mvar_t *v, char *field )
{
    if( *field == '\0' )
    {
        return v->value;
    }
    else
    {
        mvar_t *f = mvar_GetVar( v, field );
        if( f )
        {
            return f->value;
        }
        else
        {
748
            return field;
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 776 777 778 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 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
        }
    }
}

static void mvar_PushNewVar( mvar_t *vars, char *name, char *value )
{
    mvar_t *f = mvar_New( name, value );
    mvar_PushVar( vars, f );
}

static void mvar_AppendNewVar( mvar_t *vars, char *name, char *value )
{
    mvar_t *f = mvar_New( name, value );
    mvar_AppendVar( vars, f );
}


/* arg= start[:stop[:step]],.. */
static mvar_t *mvar_IntegerSetNew( char *name, char *arg )
{
    char *dup = strdup( arg );
    char *str = dup;
    mvar_t *s = mvar_New( name, "set" );

    fprintf( stderr," mvar_IntegerSetNew: name=`%s' arg=`%s'\n", name, str );

    while( str )
    {
        char *p;
        int  i_start,i_stop,i_step;
        int  i_match;

        p = strchr( str, ',' );
        if( p )
        {
            *p++ = '\0';
        }

        i_step = 0;
        i_match = sscanf( str, "%d:%d:%d", &i_start, &i_stop, &i_step );
        fprintf( stderr," mvar_IntegerSetNew: m=%d start=%d stop=%d step=%d\n", i_match, i_start, i_stop, i_step );

        if( i_match == 1 )
        {
            i_stop = i_start;
            i_step = 1;
        }
        else if( i_match == 2 )
        {
            i_step = i_start < i_stop ? 1 : -1;
        }

        if( i_match >= 1 )
        {
            int i;

            if( ( i_start < i_stop && i_step > 0 ) ||
                ( i_start > i_stop && i_step < 0 ) )
            {
                for( i = i_start; ; i += i_step )
                {
                    char   value[512];

                    if( ( i_step > 0 && i > i_stop ) ||
                        ( i_step < 0 && i < i_stop ) )
                    {
                        break;
                    }

                    fprintf( stderr," mvar_IntegerSetNew: adding %d\n", i );
                    sprintf( value, "%d", i );

                    mvar_PushNewVar( s, name, value );
                }
            }
        }
        str = p;
    }

    free( dup );
    return s;
}

static mvar_t *mvar_PlaylistSetNew( char *name, playlist_t *p_pl )
{
    mvar_t *s = mvar_New( name, "set" );
    int    i;

    fprintf( stderr," mvar_PlaylistSetNew: name=`%s'\n", name );

    vlc_mutex_lock( &p_pl->object_lock );
    for( i = 0; i < p_pl->i_size; i++ )
    {
        mvar_t *itm = mvar_New( name, "set" );
        char   value[512];

        sprintf( value, "%d", i == p_pl->i_index ? 1 : 0 );
        mvar_AppendNewVar( itm, "current", value );

        sprintf( value, "%d", i );
        mvar_AppendNewVar( itm, "index", value );

851
        mvar_AppendNewVar( itm, "name", p_pl->pp_items[i]->input.psz_name );
852

853
        mvar_AppendNewVar( itm, "uri", p_pl->pp_items[i]->input.psz_uri );
Laurent Aimar's avatar
Laurent Aimar committed
854

Simon Latapie's avatar
Simon Latapie committed
855 856 857
        sprintf( value, "%d", p_pl->pp_items[i]->i_group );
        mvar_AppendNewVar( itm, "group", value );

858 859 860 861 862 863 864 865 866 867
        mvar_AppendVar( s, itm );
    }
    vlc_mutex_unlock( &p_pl->object_lock );

    return s;
}

static mvar_t *mvar_InfoSetNew( char *name, input_thread_t *p_input )
{
    mvar_t *s = mvar_New( name, "set" );
868
    int i, j;
869 870 871 872 873 874 875

    fprintf( stderr," mvar_InfoSetNew: name=`%s'\n", name );
    if( p_input == NULL )
    {
        return s;
    }

876 877
    vlc_mutex_lock( &p_input->p_item->lock );
    for ( i = 0; i < p_input->p_item->i_categories; i++ )
878
    {
879
        info_category_t *p_category = p_input->p_item->pp_categories[i];
880 881 882 883 884 885
        mvar_t *cat  = mvar_New( name, "set" );
        mvar_t *iset = mvar_New( "info", "set" );

        mvar_AppendNewVar( cat, "name", p_category->psz_name );
        mvar_AppendVar( cat, iset );

886
        for ( j = 0; j < p_category->i_infos; j++ )
887
        {
888
            info_t *p_info = p_category->pp_infos[j];
889 890
            mvar_t *info = mvar_New( "info", "" );

891 892
            msg_Dbg( p_input, "adding info name=%s value=%s",
                     p_info->psz_name, p_info->psz_value );
893 894 895 896 897 898
            mvar_AppendNewVar( info, "name",  p_info->psz_name );
            mvar_AppendNewVar( info, "value", p_info->psz_value );
            mvar_AppendVar( iset, info );
        }
        mvar_AppendVar( s, cat );
    }
899
    vlc_mutex_unlock( &p_input->p_item->lock );
900 901 902

    return s;
}
903
#if 0
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
static mvar_t *mvar_HttpdInfoSetNew( char *name, httpd_t *p_httpd, int i_type )
{
    mvar_t       *s = mvar_New( name, "set" );
    httpd_info_t info;
    int          i;

    fprintf( stderr," mvar_HttpdInfoSetNew: name=`%s'\n", name );
    if( !p_httpd->pf_control( p_httpd, i_type, &info, NULL ) )
    {
        for( i= 0; i < info.i_count; )
        {
            mvar_t *inf;

            inf = mvar_New( name, "set" );
            do
            {
                /* fprintf( stderr," mvar_HttpdInfoSetNew: append name=`%s' value=`%s'\n",
                            info.info[i].psz_name, info.info[i].psz_value ); */
                mvar_AppendNewVar( inf,
                                   info.info[i].psz_name,
                                   info.info[i].psz_value );
                i++;
            } while( i < info.i_count && strcmp( info.info[i].psz_name, "id" ) );
            mvar_AppendVar( s, inf );
        }
    }

    /* free mem */
    for( i = 0; i < info.i_count; i++ )
    {
        free( info.info[i].psz_name );
        free( info.info[i].psz_value );
    }
937 938 939 940
    if( info.i_count > 0 )
    {
        free( info.info );
    }
941 942 943

    return s;
}
944 945
#endif

946 947 948 949 950 951 952 953 954 955 956 957 958
static mvar_t *mvar_FileSetNew( char *name, char *psz_dir )
{
    mvar_t *s = mvar_New( name, "set" );
    char           tmp[MAX_DIR_SIZE], *p, *src;
#ifdef HAVE_SYS_STAT_H
    struct stat   stat_info;
#endif
    DIR           *p_dir;
    struct dirent *p_dir_content;
    char          sep;

    /* convert all / to native separator */
#if defined( WIN32 )
959
    while( (p = strchr( psz_dir, '/' )) )
960 961 962 963 964 965 966 967
    {
        *p = '\\';
    }
    sep = '\\';
#else
    sep = '/';
#endif

Gildas Bazin's avatar
 
Gildas Bazin committed
968 969 970 971 972 973
    /* remove trailling separator */
    while( strlen( psz_dir ) > 1 &&
#if defined( WIN32 )
           !( strlen(psz_dir)==3 && psz_dir[1]==':' && psz_dir[2]==sep ) &&
#endif
           psz_dir[strlen( psz_dir ) -1 ] == sep )
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 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 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 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
    {
        psz_dir[strlen( psz_dir ) -1 ]  ='\0';
    }
    /* remove double separator */
    for( p = src = psz_dir; *src != '\0'; src++, p++ )
    {
        if( src[0] == sep && src[1] == sep )
        {
            src++;
        }
        *p = *src;
    }
    *p = '\0';

    if( *psz_dir == '\0' )
    {
        return s;
    }
    /* first fix all .. dir */
    p = src = psz_dir;
    while( *src )
    {
        if( src[0] == '.' && src[1] == '.' )
        {
            src += 2;
            if( p <= &psz_dir[1] )
            {
                continue;
            }

            p -= 2;

            while( p > &psz_dir[1] && *p != sep )
            {
                p--;
            }
        }
        else if( *src == sep )
        {
            if( p > psz_dir && p[-1] == sep )
            {
                src++;
            }
            else
            {
                *p++ = *src++;
            }
        }
        else
        {
            do
            {
                *p++ = *src++;
            } while( *src && *src != sep );
        }
    }
    *p = '\0';

    fprintf( stderr," mvar_FileSetNew: name=`%s' dir=`%s'\n", name, psz_dir );

#ifdef HAVE_SYS_STAT_H
    if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
    {
        return s;
    }
#endif

    if( ( p_dir = opendir( psz_dir ) ) == NULL )
    {
        fprintf( stderr, "cannot open dir (%s)", psz_dir );
        return s;
    }

    /* remove traling / or \ */
    for( p = &psz_dir[strlen( psz_dir) - 1]; p >= psz_dir && ( *p =='/' || *p =='\\' ); p-- )
    {
        *p = '\0';
    }

    for( ;; )
    {
        mvar_t *f;

        /* parse psz_src dir */
        if( ( p_dir_content = readdir( p_dir ) ) == NULL )
        {
            break;
        }
        if( !strcmp( p_dir_content->d_name, "." ) )
        {
            continue;
        }

#if defined( WIN32 )
        sprintf( tmp, "%s\\%s", psz_dir, p_dir_content->d_name );
#else
        sprintf( tmp, "%s/%s", psz_dir, p_dir_content->d_name );
#endif

#ifdef HAVE_SYS_STAT_H
        if( stat( tmp, &stat_info ) == -1 )
        {
            continue;
        }
#endif
        f = mvar_New( name, "set" );
        mvar_AppendNewVar( f, "name", tmp );
#ifdef HAVE_SYS_STAT_H
        if( S_ISDIR( stat_info.st_mode ) )
        {
            mvar_AppendNewVar( f, "type", "directory" );
        }
        else if( S_ISREG( stat_info.st_mode ) )
        {
            mvar_AppendNewVar( f, "type", "file" );
        }
        else
        {
            mvar_AppendNewVar( f, "type", "unknown" );
        }

        sprintf( tmp, "%lld", stat_info.st_size );
        mvar_AppendNewVar( f, "size", tmp );

        /* FIXME memory leak FIXME */
1099
#ifdef HAVE_CTIME_R
1100 1101
        ctime_r( &stat_info.st_mtime, tmp );
        mvar_AppendNewVar( f, "date", tmp );
1102 1103 1104 1105
#else
        mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
#endif

1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
#else
        mvar_AppendNewVar( f, "type", "unknown" );
        mvar_AppendNewVar( f, "size", "unknown" );
        mvar_AppendNewVar( f, "date", "unknown" );
#endif
        mvar_AppendVar( s, f );
    }

    return s;
}

1117 1118 1119 1120 1121 1122
static mvar_t *mvar_VlmSetNew( char *name, vlm_t *vlm )
{
    mvar_t        *s = mvar_New( name, "set" );
    vlm_message_t *msg;
    int    i;

1123
    /* fprintf( stderr," mvar_VlmSetNew: name=`%s'\n", name ); */
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
    if( vlm == NULL )
        return s;
    if( vlm_ExecuteCommand( vlm, "show", &msg ) )
    {
        return s;
    }

    for( i = 0; i < msg->i_child; i++ )
    {
        /* Over media, schedule */
        vlm_message_t *ch = msg->child[i];
        int j;

        for( j = 0; j < ch->i_child; j++ )
        {
            /* Over name */
            vlm_message_t *el = ch->child[j];
            vlm_message_t *inf, *desc;
            mvar_t        *set;
            char          psz[500];
            int k;

            sprintf( psz, "show %s", el->psz_name );
            if( vlm_ExecuteCommand( vlm, psz, &inf ) )
                continue;
            desc = inf->child[0];

            /* Add a node with name and info */
            set = mvar_New( name, "set" );
            mvar_AppendNewVar( set, "name", el->psz_name );

1155
            /* fprintf( stderr, "#### name=%s\n", el->psz_name ); */
Laurent Aimar's avatar
Laurent Aimar committed
1156

1157 1158
            for( k = 0; k < desc->i_child; k++ )
            {
Laurent Aimar's avatar
Laurent Aimar committed
1159 1160 1161 1162 1163 1164
                vlm_message_t *ch = desc->child[k];
                if( ch->i_child > 0 )
                {
                    int c;
                    mvar_t *n = mvar_New( ch->psz_name, "set" );

1165
                    /* fprintf( stderr, "        child=%s [%d]\n", ch->psz_name, ch->i_child ); */
Laurent Aimar's avatar
Laurent Aimar committed
1166 1167 1168 1169 1170
                    for( c = 0; c < ch->i_child; c++ )
                    {
                        mvar_t *in = mvar_New( ch->psz_name, ch->child[c]->psz_name );
                        mvar_AppendVar( n, in );

1171
                        /* fprintf( stderr, "            sub=%s\n", ch->child[c]->psz_name );*/
Laurent Aimar's avatar
Laurent Aimar committed
1172 1173 1174 1175 1176
                    }
                    mvar_AppendVar( set, n );
                }
                else
                {
1177
                    /* fprintf( stderr, "        child=%s->%s\n", ch->psz_name, ch->psz_value ); */
Laurent Aimar's avatar
Laurent Aimar committed
1178 1179
                    mvar_AppendNewVar( set, ch->psz_name, ch->psz_value );
                }
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
            }
            vlm_MessageDelete( inf );

            mvar_AppendVar( s, set );
        }
    }
    vlm_MessageDelete( msg );

    return s;
}


1192 1193 1194
static void SSInit( rpn_stack_t * );
static void SSClean( rpn_stack_t * );
static void EvaluateRPN( mvar_t  *, rpn_stack_t *, char * );
1195

1196 1197
static void SSPush  ( rpn_stack_t *, char * );
static char *SSPop  ( rpn_stack_t * );
1198

1199 1200
static void SSPushN ( rpn_stack_t *, int );
static int  SSPopN  ( rpn_stack_t *, mvar_t  * );
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312


/****************************************************************************
 * Macro handling
 ****************************************************************************/
typedef struct
{
    char *id;
    char *param1;
    char *param2;
} macro_t;

static int FileLoad( FILE *f, uint8_t **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;
}

static int MacroParse( macro_t *m, uint8_t *psz_src )
{
    uint8_t *dup = strdup( psz_src );
    uint8_t *src = dup;
    uint8_t *p;
    int     i_skip;

#define EXTRACT( name, l ) \
        src += l;    \
        p = strchr( src, '"' );             \
        if( p )                             \
        {                                   \
            *p++ = '\0';                    \
        }                                   \
        m->name = strdup( src );            \
        if( !p )                            \
        {                                   \
            break;                          \
        }                                   \
        src = p;

    /* init m */
    m->id = NULL;
    m->param1 = NULL;
    m->param2 = NULL;

    /* parse */
    src += 4;

    while( *src )
    {
        while( *src == ' ')
        {
            src++;
        }
        if( !strncmp( src, "id=\"", 4 ) )
        {
            EXTRACT( id, 4 );
        }
        else if( !strncmp( src, "param1=\"", 8 ) )
        {
            EXTRACT( param1, 8 );
        }
        else if( !strncmp( src, "param2=\"", 8 ) )
        {
            EXTRACT( param2, 8 );
        }
        else
        {
            break;
        }
    }
    if( strstr( src, "/>" ) )
    {
        src = strstr( src, "/>" ) + 2;
    }
    else
    {
        src += strlen( src );
    }

    if( m->id == NULL )
    {
        m->id = strdup( "" );
    }
    if( m->param1 == NULL )
    {
        m->param1 = strdup( "" );
    }
    if( m->param2 == NULL )
    {
        m->param2 = strdup( "" );
    }
    i_skip = src - dup;

    free( dup );
    return i_skip;
#undef EXTRACT
}
Gildas Bazin's avatar
 
Gildas Bazin committed
1313

1314 1315 1316 1317 1318 1319
static void MacroClean( macro_t *m )
{
    free( m->id );
    free( m->param1 );
    free( m->param2 );
}
1320

1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
enum macroType
{
    MVLC_UNKNOWN = 0,
    MVLC_CONTROL,
        MVLC_PLAY,
        MVLC_STOP,
        MVLC_PAUSE,
        MVLC_NEXT,
        MVLC_PREVIOUS,
        MVLC_ADD,
1331 1332
        MVLC_DEL,
        MVLC_EMPTY,
1333
        MVLC_SEEK,
1334
        MVLC_KEEP,
1335
        MVLC_SORT,
1336
        MVLC_MOVE,
1337
        MVLC_VOLUME,
1338
        MVLC_FULLSCREEN,
1339 1340 1341

        MVLC_CLOSE,
        MVLC_SHUTDOWN,
1342 1343 1344 1345 1346 1347 1348 1349

        MVLC_VLM_NEW,
        MVLC_VLM_SETUP,
        MVLC_VLM_DEL,
        MVLC_VLM_PLAY,
        MVLC_VLM_PAUSE,
        MVLC_VLM_STOP,
        MVLC_VLM_SEEK,
1350 1351
        MVLC_VLM_LOAD,
        MVLC_VLM_SAVE,
1352

1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
    MVLC_FOREACH,
    MVLC_IF,
    MVLC_RPN,
    MVLC_ELSE,
    MVLC_END,
    MVLC_GET,
    MVLC_SET,
        MVLC_INT,
        MVLC_FLOAT,
        MVLC_STRING,

    MVLC_VALUE
};
Gildas Bazin's avatar
 
Gildas Bazin committed
1366

1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
static struct
{
    char *psz_name;
    int  i_type;
}
StrToMacroTypeTab [] =
{
    { "control",    MVLC_CONTROL },
        /* player control */
        { "play",           MVLC_PLAY },
        { "stop",           MVLC_STOP },
        { "pause",          MVLC_PAUSE },
        { "next",           MVLC_NEXT },
1380
        { "previous",       MVLC_PREVIOUS },
1381
        { "seek",           MVLC_SEEK },
1382 1383
        { "keep",           MVLC_KEEP },
        { "fullscreen",     MVLC_FULLSCREEN },
1384
        { "volume",         MVLC_VOLUME },
1385 1386

        /* playlist management */
1387
        { "add",            MVLC_ADD },
Gildas Bazin's avatar
 
Gildas Bazin committed
1388
        { "delete",         MVLC_DEL },
1389
        { "empty",          MVLC_EMPTY },
1390
        { "sort",           MVLC_SORT },
1391
        { "move",           MVLC_MOVE },
1392 1393 1394 1395 1396

        /* admin control */
        { "close",          MVLC_CLOSE },
        { "shutdown",       MVLC_SHUTDOWN },

1397 1398 1399 1400 1401 1402 1403 1404
        /* vlm control */
        { "vlm_new",        MVLC_VLM_NEW },
        { "vlm_setup",      MVLC_VLM_SETUP },
        { "vlm_del",        MVLC_VLM_DEL },
        { "vlm_play",       MVLC_VLM_PLAY },
        { "vlm_pause",      MVLC_VLM_PAUSE },
        { "vlm_stop",       MVLC_VLM_STOP },
        { "vlm_seek",       MVLC_VLM_SEEK },
1405 1406
        { "vlm_load",       MVLC_VLM_LOAD },
        { "vlm_save",       MVLC_VLM_SAVE },
1407

1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
    { "rpn",        MVLC_RPN },

    { "foreach",    MVLC_FOREACH },
    { "value",      MVLC_VALUE },

    { "if",         MVLC_IF },
    { "else",       MVLC_ELSE },
    { "end",        MVLC_END },
    { "get",         MVLC_GET },
    { "set",         MVLC_SET },
        { "int",            MVLC_INT },
        { "float",          MVLC_FLOAT },
        { "string",         MVLC_STRING },

    /* end */
    { NULL,         MVLC_UNKNOWN }
};
Gildas Bazin's avatar
 
Gildas Bazin committed
1425

1426
static int StrToMacroType( char *name )
Gildas Bazin's avatar
 
Gildas Bazin committed
1427
{
1428
    int i;
Gildas Bazin's avatar
 
Gildas Bazin committed
1429

1430
    if( !name || *name == '\0')
Gildas Bazin's avatar
 
Gildas Bazin committed
1431
    {
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
        return MVLC_UNKNOWN;
    }
    for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
    {
        if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
        {
            return StrToMacroTypeTab[i].i_type;
        }
    }
    return MVLC_UNKNOWN;
Gildas Bazin's avatar
 
Gildas Bazin committed
1442 1443
}

1444
static void MacroDo( httpd_file_sys_t *p_args,
1445 1446 1447 1448
                     macro_t *m,
                     uint8_t *p_request, int i_request,
                     uint8_t **pp_data,  int *pi_data,
                     uint8_t **pp_dst )
Gildas Bazin's avatar
 
Gildas Bazin committed
1449
{
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
    intf_thread_t  *p_intf = p_args->p_intf;
    intf_sys_t     *p_sys = p_args->p_intf->p_sys;
    char control[512];

#define ALLOC( l ) \
    {               \
        int __i__ = *pp_dst - *pp_data; \
        *pi_data += (l);                  \
        *pp_data = realloc( *pp_data, *pi_data );   \
        *pp_dst = (*pp_data) + __i__;   \
    }
#define PRINT( str ) \
    ALLOC( strlen( str ) + 1 ); \
    *pp_dst += sprintf( *pp_dst, str );
Gildas Bazin's avatar
 
Gildas Bazin committed
1464

1465 1466
#define PRINTS( str, s ) \
    ALLOC( strlen( str ) + strlen( s ) + 1 ); \
Eric Petit's avatar
Eric Petit committed
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
    { \
        char * psz_cur = *pp_dst; \
        *pp_dst += sprintf( *pp_dst, str, s ); \
        while( psz_cur && *psz_cur ) \
        {  \
            /* Prevent script injection */ \
            if( *psz_cur == '<' ) *psz_cur = '*'; \
            if( *psz_cur == '>' ) *psz_cur = '*'; \
            psz_cur++ ; \
        } \
Clément Stenac's avatar
Clément Stenac committed
1477
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1478

1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
    switch( StrToMacroType( m->id ) )
    {
        case MVLC_CONTROL:
            if( i_request <= 0 )
            {
                break;
            }
            uri_extract_value( p_request, "control", control, 512 );
            if( *m->param1 && !strstr( m->param1, control ) )
            {
                msg_Warn( p_intf, "unauthorized control=%s", control );
                break;
            }
            switch( StrToMacroType( control ) )
            {
                case MVLC_PLAY:
                {
                    int i_item;
                    char item[512];

                    uri_extract_value( p_request, "item", item, 512 );
                    i_item = atoi( item );
                    playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO, i_item );
                    msg_Dbg( p_intf, "requested playlist item: %i", i_item );
                    break;
                }
                case MVLC_STOP:
                    playlist_Command( p_sys->p_playlist, PLAYLIST_STOP, 0 );
                    msg_Dbg( p_intf, "requested playlist stop" );
                    break;
                case MVLC_PAUSE:
                    playlist_Command( p_sys->p_playlist, PLAYLIST_PAUSE, 0 );
                    msg_Dbg( p_intf, "requested playlist pause" );
                    break;
                case MVLC_NEXT:
                    playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
                                      p_sys->p_playlist->i_index + 1 );
                    msg_Dbg( p_intf, "requested playlist next" );
                    break;
                case MVLC_PREVIOUS:
                    playlist_Command( p_sys->p_playlist, PLAYLIST_GOTO,
                                      p_sys->p_playlist->i_index - 1 );
                    msg_Dbg( p_intf, "requested playlist next" );
                    break;
1523
                case MVLC_FULLSCREEN:
1524
                {
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
                    if( p_sys->p_input )
                    {
                        vout_thread_t *p_vout;
                        p_vout = vlc_object_find( p_sys->p_input,
                                                  VLC_OBJECT_VOUT, FIND_CHILD );

                        if( p_vout )
                        {
                            p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
                            vlc_object_release( p_vout );
1535
                            msg_Dbg( p_intf, "requested fullscreen toggle" );
1536 1537
                        }
                    }
1538
                }
1539
                break;
1540 1541 1542
                case MVLC_SEEK:
                {
                    vlc_value_t val;
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
                    char value[30];
                    char * p_value;
                    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 TIME_ABSOLUTE 0
#define TIME_REL_FOR 1
#define TIME_REL_BACK -1