http.c 84.1 KB
Newer Older
gbazin's avatar
   
gbazin committed
1
/*****************************************************************************
2
 * http.c :  http mini-server ;)
gbazin's avatar
   
gbazin committed
3
4
 *****************************************************************************
 * Copyright (C) 2001 VideoLAN
5
 * $Id: http.c,v 1.36 2003/11/18 12:32:04 gbazin Exp $
gbazin's avatar
   
gbazin committed
6
7
 *
 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8
 *          Laurent Aimar <fenrir@via.ecp.fr>
gbazin's avatar
   
gbazin 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
 *
gbazin's avatar
   
gbazin 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
29
30
31
32
33
34
35
/*
 * TODO:
 *
 * - clean up ?
 * - doc ! (mouarf ;)
 *
 */

36
#include <stdlib.h>
gbazin's avatar
   
gbazin committed
37
38
39
#include <vlc/vlc.h>
#include <vlc/intf.h>

40
#include <vlc/aout.h>
41
42
#include <vlc/vout.h> /* for fullscreen */

43
44
45
46
47
48
49
50
51
52
53
54
#include "httpd.h"

#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

gbazin's avatar
   
gbazin committed
55
#ifdef HAVE_UNISTD_H
56
57
58
#   include <unistd.h>
#elif defined( WIN32 ) && !defined( UNDER_CE )
#   include <io.h>
gbazin's avatar
   
gbazin committed
59
60
#endif

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

/*****************************************************************************
67
 * Module descriptor
gbazin's avatar
   
gbazin committed
68
69
70
 *****************************************************************************/
static int  Activate     ( vlc_object_t * );
static void Close        ( vlc_object_t * );
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

#define HOST_TEXT N_( "Host address" )
#define HOST_LONGTEXT N_( \
    "You can set the address and port on which the http interface will bind" )
#define SRC_TEXT N_( "Source directory" )
#define SRC_LONGTEXT N_( "Source directory" )

vlc_module_begin();
    set_description( _("HTTP remote control interface") );
    add_category_hint( N_("HTTP remote control"), NULL, VLC_TRUE );
        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 );
    set_callbacks( Activate, Close );
vlc_module_end();


/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
91
92
static void Run          ( intf_thread_t *p_intf );

gbazin's avatar
gbazin committed
93
94
static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
                           char *psz_dir );
95

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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;
}


119
120
121
122
static int  http_get( httpd_file_callback_args_t *p_args,
                      uint8_t *p_request, int i_request,
                      uint8_t **pp_data, int *pi_data );

gbazin's avatar
   
gbazin committed
123
124
static char *uri_extract_value( char *psz_uri, char *psz_name,
                                char *psz_value, int i_value_max );
125
static void uri_decode_url_encoded( char *psz );
gbazin's avatar
   
gbazin committed
126

127
128
129
130
static char *Find_end_MRL( char *psz );

static playlist_item_t * parse_MRL( char *psz );

gbazin's avatar
   
gbazin committed
131
/*****************************************************************************
132
 *
gbazin's avatar
   
gbazin committed
133
 *****************************************************************************/
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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;
148
} rpn_stack_t;
149
150
151
152
153
154
155
156
157
158
159

struct httpd_file_callback_args_t
{
    intf_thread_t *p_intf;
    httpd_file_t  *p_file;

    char          *file;
    char          *name;
    char          *mime;

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

gbazin's avatar
   
gbazin committed
164
165
166
167
168
struct intf_sys_t
{
    httpd_t             *p_httpd;
    httpd_host_t        *p_httpd_host;

169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
    int                         i_files;
    httpd_file_callback_args_t  **pp_files;

    playlist_t          *p_playlist;
    input_thread_t      *p_input;
};



/*****************************************************************************
 * Activate: initialize and create stuff
 *****************************************************************************/
static int Activate( vlc_object_t *p_this )
{
    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 ) );
zorglub's avatar
zorglub committed
210
211
212
213
    if( !p_intf->p_sys )
    {
        return( VLC_ENOMEM );
    }
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
    p_sys->p_playlist = NULL;
    p_sys->p_input    = NULL;

    if( ( p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_intf), VLC_TRUE ) ) == NULL )
    {
        msg_Err( p_intf, "cannot create/find httpd" );
        free( p_sys );
        return VLC_EGENERIC;
    }

    if( ( p_sys->p_httpd_host =
                p_sys->p_httpd->pf_register_host( p_sys->p_httpd,
                                                  psz_address, i_port ) ) == NULL )
    {
        msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
        httpd_Release( p_sys->p_httpd );
        free( p_sys );
        return VLC_EGENERIC;
    }

    if( psz_host )
    {
        free( psz_host );
    }

    p_sys->i_files = 0;
    p_sys->pp_files = malloc( sizeof( httpd_file_callback_args_t *) );
zorglub's avatar
zorglub committed
241
242
243
244
    if( !p_sys->pp_files )
    {
        return( VLC_ENOMEM );
    }
245

gbazin's avatar
gbazin committed
246
247
#if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
        ( defined(WIN32) && !defined(UNDER_CE ) )
248
249
250
251
    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 );
zorglub's avatar
zorglub committed
252
253
254
255
        if( !psz_src )
        {
            return( VLC_ENOMEM );
        }
gbazin's avatar
   
gbazin committed
256
257
258
#if defined(WIN32)
        sprintf( psz_src, "%s/http", psz_vlcpath);
#else
259
        sprintf( psz_src, "%s/share/http", psz_vlcpath);
gbazin's avatar
   
gbazin committed
260
#endif
261
262
263
    }
#else
    psz_src = config_GetPsz( p_intf, "http-src" );
264
265
266
267
268
269
270
271
272
273
274
275

    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" );
        }
    }
276
#endif
277

278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
    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 )
    {
296
        msg_Err( p_intf, "cannot find any files (%s)", psz_src );
297
298
299
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
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
        goto failed;
    }
    p_intf->pf_run = Run;

    return VLC_SUCCESS;

failed:
    free( p_sys->pp_files );
    p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
                                        p_sys->p_httpd_host );
    httpd_Release( p_sys->p_httpd );
    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;

    for( i = 0; i < p_sys->i_files; i++ )
    {
       p_sys->p_httpd->pf_unregister_file( p_sys->p_httpd,
                                           p_sys->pp_files[i]->p_file );
       /* do not free mime */
       free( p_sys->pp_files[i]->file );
       free( p_sys->pp_files[i]->name );
       free( p_sys->pp_files[i] );
    }
    free( p_sys->pp_files );
    p_sys->p_httpd->pf_unregister_host( p_sys->p_httpd,
                                        p_sys->p_httpd_host );
    httpd_Release( p_sys->p_httpd );
    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 );

zorglub's avatar
zorglub committed
403
404
405
406
407
    if( !url || !p )
    {
        return NULL;
    }

408
#ifdef WIN32
gbazin's avatar
gbazin committed
409
410
    while( *name == '\\' || *name == '/' )
#else
411
    while( *name == '/' )
gbazin's avatar
gbazin committed
412
#endif
413
414
415
416
417
    {
        name++;
    }

    *p++ = '/';
gbazin's avatar
gbazin committed
418
419
420
421
422
    strcpy( p, name );

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

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

/****************************************************************************
 * FileToMime: XXX duplicated with modules/access_out/http.c
 ****************************************************************************/
static struct
{
    char *psz_ext;
    char *psz_mime;
} http_mime[] =
{
    { ".htm",   "text/html" },
    { ".html",  "text/html" },

456
457
    { ".css",   "text/css" },

458
459
460
461
462
463
464
465
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
491
492
493
494
495
496
497
498
499
500
501
502
503
    /* media mime */
    { ".avi",   "video/avi" },
    { ".asf",   "video/x-ms-asf" },
    { ".m1a",   "audio/mpeg" },
    { ".m2a",   "audio/mpeg" },
    { ".m1v",   "video/mpeg" },
    { ".m2v",   "video/mpeg" },
    { ".mp2",   "audio/mpeg" },
    { ".mp3",   "audio/mpeg" },
    { ".mpa",   "audio/mpeg" },
    { ".mpg",   "video/mpeg" },
    { ".mpeg",  "video/mpeg" },
    { ".mpe",   "video/mpeg" },
    { ".mov",   "video/quicktime" },
    { ".moov",  "video/quicktime" },
    { ".ogg",   "application/ogg" },
    { ".ogm",   "application/ogg" },
    { ".wav",   "audio/wav" },

    /* end */
    { NULL,     NULL }
};

static char *FileToMime( char *psz_name )
{
    char *psz_ext;

    psz_ext = strrchr( psz_name, '.' );
    if( psz_ext )
    {
        int i;

        for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
        {
            if( !strcmp( http_mime[i].psz_ext, psz_ext ) )
            {
                return( http_mime[i].psz_mime );
            }
        }
    }
    return( "application/octet-stream" );
}

/****************************************************************************
 * ParseDirectory: parse recursively a directory, adding each file
 ****************************************************************************/
gbazin's avatar
gbazin committed
504
505
static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
                           char *psz_dir )
506
507
508
509
510
511
512
513
514
515
516
517
518
519
{
    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
gbazin's avatar
gbazin committed
520
    if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
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
    {
        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;
gbazin's avatar
gbazin committed
546
547
            while( i_size > 0 && ( line[i_size-1] == '\n' ||
                   line[i_size-1] == '\r' ) )
548
549
550
551
552
553
554
555
556
557
558
559
560
561
            {
                i_size--;
            }

            line[i_size] = '\0';

            p = strchr( line, ':' );
            if( p )
            {
                *p++ = '\0';
                user = strdup( line );
                password = strdup( p );
            }
        }
gbazin's avatar
gbazin committed
562
563
        msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
                 user, password, i_size );
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584

        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 ) )
        {
#define f p_sys->pp_files[p_sys->i_files]
            f = malloc( sizeof( httpd_file_callback_args_t ) );
zorglub's avatar
zorglub committed
585
586
587
588
589
            if( !f )
            {
                msg_Err( p_intf, "Out of memory" );
                return( VLC_ENOMEM );
            }
590
591
592
593
594
            f->p_intf  = p_intf;
            f->file = strdup( dir );
            f->name = FileToUrl( &dir[strlen( psz_root )] );
            f->mime = FileToMime( &dir[strlen( psz_root )] );

zorglub's avatar
zorglub committed
595
596
597
598
599
            if( !f->name || !f->mime )
            {
                msg_Err( p_intf , "Unable to parse directory" );
                return( VLC_ENOMEM );
            }
gbazin's avatar
gbazin committed
600
601
            msg_Dbg( p_intf, "file=%s (url=%s mime=%s)",
                     f->file, f->name, f->mime );
602
603
604
605
606
607
608
609
610
611

            f->p_file =
                p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
                                                  f->name, f->mime,
                                                  user, password,
                                                  http_get, http_get,
                                                  f );
            if( f->p_file )
            {
                p_sys->i_files++;
gbazin's avatar
gbazin committed
612
613
                p_sys->pp_files = realloc( p_sys->pp_files,
                  (p_sys->i_files+1) * sizeof( httpd_file_callback_args_t ) );
614
615
616
617
            }
#define fold p_sys->pp_files[p_sys->i_files-1]

            /* FIXME for rep/ add rep (it would be better to do a redirection) */
gbazin's avatar
   
gbazin committed
618
            if( p_sys->i_files && strlen(fold->name) > 1 &&
gbazin's avatar
gbazin committed
619
                fold->name[strlen(fold->name) - 1] == '/' )
620
621
            {
                f = malloc( sizeof( httpd_file_callback_args_t ) );
zorglub's avatar
zorglub committed
622
623
624
625
626
                if( !f )
                {
                    msg_Err( p_intf, "Out of memory" );
                    return( VLC_ENOMEM );
                }
627
628
629
630
631
632
                f->p_intf  = p_intf;
                f->file = strdup( fold->file );
                f->name = strdup( fold->name );
                f->mime = fold->mime;

                f->name[strlen(f->name) - 1] = '\0';
gbazin's avatar
gbazin committed
633
634
                msg_Dbg( p_intf, "file=%s (url=%s mime=%s)", f->file, f->name,
                         f->mime );
635
636
637
638
639
640
641
642
643
                f->p_file =
                    p_sys->p_httpd->pf_register_file( p_sys->p_httpd,
                                                      f->name, f->mime,
                                                      user, password,
                                                      http_get, http_get,
                                                      f );
                if( f->p_file )
                {
                    p_sys->i_files++;
gbazin's avatar
gbazin committed
644
645
646
                    p_sys->pp_files =
                        realloc( p_sys->pp_files, (p_sys->i_files+1) *
                                 sizeof( httpd_file_callback_args_t ) );
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
                }
            }
#undef fold
#undef f
        }
    }

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

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

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

zorglub's avatar
zorglub committed
673
    if( !v ) return NULL;
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
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
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
    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
        {
830
            return field;
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
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
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
        }
    }
}

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

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

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

938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
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
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
        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" );

    input_info_category_t * p_category;
    input_info_t * p_info;

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

    vlc_mutex_lock( &p_input->stream.stream_lock );
    p_category = p_input->stream.p_info;
    while ( p_category )
    {
        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 );

        p_info = p_category->p_info;
        while ( p_info )
        {
            mvar_t *info = mvar_New( "info", "" );

            msg_Dbg( p_input, "adding info name=%s value=%s", p_info->psz_name, p_info->psz_value );
            mvar_AppendNewVar( info, "name",  p_info->psz_name );
            mvar_AppendNewVar( info, "value", p_info->psz_value );
            mvar_AppendVar( iset, info );
            p_info = p_info->p_next;
        }
        mvar_AppendVar( s, cat );
        p_category = p_category->p_next;
    }
    vlc_mutex_unlock( &p_input->stream.stream_lock );

    return s;
}

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

    return s;
}
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
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 )
1036
    while( (p = strchr( psz_dir, '/' )) )
1037
1038
1039
1040
1041
1042
1043
1044
    {
        *p = '\\';
    }
    sep = '\\';
#else
    sep = '/';
#endif

gbazin's avatar
   
gbazin committed
1045
1046
1047
1048
1049
1050
    /* 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 )
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
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
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
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
    {
        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 */
1176
#ifdef HAVE_CTIME_R
1177
1178
        ctime_r( &stat_info.st_mtime, tmp );
        mvar_AppendNewVar( f, "date", tmp );
1179
1180
1181
1182
#else
        mvar_AppendNewVar( f, "date", ctime( &stat_info.st_mtime ) );
#endif

1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
#else
        mvar_AppendNewVar( f, "type", "unknown" );
        mvar_AppendNewVar( f, "size", "unknown" );
        mvar_AppendNewVar( f, "date", "unknown" );
#endif
        mvar_AppendVar( s, f );
    }

    return s;
}

1194
1195
1196
static void SSInit( rpn_stack_t * );
static void SSClean( rpn_stack_t * );
static void EvaluateRPN( mvar_t  *, rpn_stack_t *, char * );
1197

1198
1199
static void SSPush  ( rpn_stack_t *, char * );
static char *SSPop  ( rpn_stack_t * );
1200

1201
1202
static void SSPushN ( rpn_stack_t *, int );
static int  SSPopN  ( rpn_stack_t *, mvar_t  * );
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
1313
1314


/****************************************************************************
 * 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
}
gbazin's avatar
   
gbazin committed
1315

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

1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
enum macroType
{
    MVLC_UNKNOWN = 0,
    MVLC_CONTROL,
        MVLC_PLAY,
        MVLC_STOP,
        MVLC_PAUSE,
        MVLC_NEXT,
        MVLC_PREVIOUS,
        MVLC_ADD,
1333
1334
        MVLC_DEL,
        MVLC_EMPTY,
1335
        MVLC_SEEK,
1336
        MVLC_KEEP,
1337
1338
        MVLC_SORT,
        MVLC_VOLUME,
1339
        MVLC_FULLSCREEN,
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355

        MVLC_CLOSE,
        MVLC_SHUTDOWN,
    MVLC_FOREACH,
    MVLC_IF,
    MVLC_RPN,
    MVLC_ELSE,
    MVLC_END,
    MVLC_GET,
    MVLC_SET,
        MVLC_INT,
        MVLC_FLOAT,
        MVLC_STRING,

    MVLC_VALUE
};
gbazin's avatar
   
gbazin committed
1356

1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
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 },
1370
        { "previous",       MVLC_PREVIOUS },
1371
        { "seek",           MVLC_SEEK },
1372
1373
        { "keep",           MVLC_KEEP },
        { "fullscreen",     MVLC_FULLSCREEN },
1374
        { "volume",         MVLC_VOLUME },
1375
1376

        /* playlist management */
1377
        { "add",            MVLC_ADD },
gbazin's avatar
   
gbazin committed
1378
        { "delete",         MVLC_DEL },
1379
        { "empty",          MVLC_EMPTY },
1380
        { "sort",           MVLC_SORT },
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402

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

    { "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 }
};
gbazin's avatar
   
gbazin committed
1403

1404
static int StrToMacroType( char *name )
gbazin's avatar
   
gbazin committed
1405
{
1406
    int i;
gbazin's avatar
   
gbazin committed
1407

1408
    if( !name || *name == '\0')
gbazin's avatar
   
gbazin committed
1409
    {
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
        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;
gbazin's avatar
   
gbazin committed
1420
1421
}

1422
1423
1424
1425
1426
static void MacroDo( httpd_file_callback_args_t *p_args,
                     macro_t *m,
                     uint8_t *p_request, int i_request,
                     uint8_t **pp_data,  int *pi_data,
                     uint8_t **pp_dst )
gbazin's avatar
   
gbazin committed
1427
{
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
    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 );
gbazin's avatar
   
gbazin committed
1442

1443
1444
#define PRINTS( str, s ) \
    ALLOC( strlen( str ) + strlen( s ) + 1 ); \
Eric Petit's avatar
Eric Petit committed
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
    { \
        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++ ; \
        } \
zorglub's avatar
zorglub committed
1455
    }
gbazin's avatar
   
gbazin committed
1456

1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
    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;
1501
                case MVLC_FULLSCREEN:
1502
                {
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
                    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 );
1513
                            msg_Dbg( p_intf, "requested fullscreen toggle" );
1514
1515
                        }
                    }
1516
                }
1517
                break;
1518
1519
1520
                case MVLC_SEEK:
                {
                    vlc_value_t val;
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
                    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
1533
1534
                    if( p_sys->p_input )
                    {
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
                        uri_extract_value( p_request, "seek_value", value, 20 );
                        uri_decode_url_encoded( value );
                        p_value = value;
                        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 = TIME_REL_FOR;
                                    p_value++;
                                    break;
                                }
                                case '-':
                                {
                                    i_relative = 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;
                                    /* other characters which are not numbers are not important */
                                    while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
                                    {
                                        p_value++;
                                    }
                                    break;
                                }
                                case 'm': case 'M': case '\'': /* minutes */
                                {
                                    i_value += 60 * i_stock;
                                    i_stock = 0;
                                    p_value++;
                                    while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
                                    {
                                        p_value++;
                                    }
                                    break;
                                }
                                case 's': case 'S': case '"':  /* seconds */
                                {
                                    i_value += i_stock;
                                    i_stock = 0;
                                    while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
                                    {
                                        p_value++;
                                    }
                                    break;
                                }
                                default:
                                {
                                    p_value++;
                                    break;
                                }
                            }
                        }

                        /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
                        i_value += i_stock;

                        switch(i_relative)
                        {
                            case 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 TIME_REL_FOR:
                            {
                                var_Get( p_sys->p_input, "time", &val );
1637
                                if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
                                {
                                    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 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:
                            {
                                val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
                                var_Set( p_sys->p_input, "position", val );
                                msg_Dbg( p_intf, "requested seek percent: %d", i_value );
                                break;
                            }
                            case POSITION_REL_FOR:
                            {
                                var_Get( p_sys->p_input, "position", &val );
                                val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
                                var_Set( p_sys->p_input, "position", val );
                                msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
                                break;
                            }
                            case POSITION_REL_BACK:
                            {
                                var_Get( p_sys->p_input, "position", &val );
                                val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
                                var_Set( p_sys->p_input, "position", val );
                                msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
                                break;
                            }
                            default:
                            {
                                msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
                                break;
                            }
                        }
1691
                    }
1692
1693
1694
1695
1696
1697
#undef POSITION_ABSOLUTE
#undef POSITION_REL_FOR
#undef POSITION_REL_BACK
#undef TIME_ABSOLUTE
#undef TIME_REL_FOR
#undef TIME_REL_BACK
1698
1699
                    break;
                }
1700
1701
1702
1703
1704
1705
                case MVLC_VOLUME:
                {
                    char vol[8];
                    audio_volume_t i_volume;
                    int i_value;

1706
1707
1708
                    uri_extract_value( p_request, "value", vol, 8 );
                    aout_VolumeGet( p_intf, &i_volume );
                    uri_decode_url_encoded( vol );
1709

1710
1711
1712
1713
                    if( vol[0] == '+' )
                    {
                        i_value = atoi( vol + 1 );
                        if( (i_volume + i_value) > AOUT_VOLUME_MAX )
1714
                        {
1715
1716
                            aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
                            msg_Dbg( p_intf, "requested volume set: max" );
1717
1718
                        } else
                        {
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
                            aout_VolumeSet( p_intf , (i_volume + i_value) );
                            msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
                        }
                    } else
                    if( vol[0] == '-' )
                    {
                        i_value = atoi( vol + 1 );
                        if( (i_volume - i_value) < AOUT_VOLUME_MIN )
                        {
                            aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
                            msg_Dbg( p_intf, "requested volume set: min" );
1730
1731
                        } else
                        {
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
                            aout_VolumeSet( p_intf , (i_volume - i_value) );
                            msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
                        }
                    } else
                    {
                        i_value = atoi( vol );
                        if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
                        {
                            aout_VolumeSet( p_intf , atoi( vol ) );
                            msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
1742
1743
1744
1745
                        }
                    }
                    break;
                }
1746

1747
                /* playlist management */
1748
1749
1750
                case MVLC_ADD:
                {
                    char mrl[512];
1751
1752
                    playlist_item_t * p_item;

1753
1754
                    uri_extract_value( p_request, "mrl", mrl, 512 );
                    uri_decode_url_encoded( mrl );
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
                    p_item = parse_MRL( mrl );

                    if( p_item == NULL )
                    {
                        msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
                    } else
                    {
                        playlist_AddItem( p_sys->p_playlist , p_item ,
                                          PLAYLIST_APPEND, PLAYLIST_END );
                        msg_Dbg( p_intf, "requested mrl add: %s", mrl );
                    }

1767
1768
                    break;
                }
1769
1770
                case MVLC_DEL:
                {
gbazin's avatar
   
gbazin committed
1771
1772
                    int i_item, *p_items = NULL, i_nb_items = 0;
                    char item[512], *p_parser = p_request;
1773

gbazin's avatar
   
gbazin committed
1774
1775
1776
1777
1778
                    /* Get the list of items to delete */
                    while( (p_parser =
                            uri_extract_value( p_parser, "item", item, 512 )) )
                    {
                        if( !*item ) continue;
1779

gbazin's avatar
   
gbazin committed
1780
                        i_item = atoi( item );
gbazin's avatar
   
gbazin committed
1781
1782
                        p_items = realloc( p_items, (i_nb_items + 1) *
                                           sizeof(int) );
gbazin's avatar
   
gbazin committed
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
                        p_items[i_nb_items] = i_item;
                        i_nb_items++;
                    }

                    /* The items need to be deleted from in reversed order */
                    if( i_nb_items )
                    {
                        int i;
                        for( i = 0; i < i_nb_items; i++ )
                        {
                            int j, i_index = 0;
                            for( j = 0; j < i_nb_items; j++ )
                            {
                                if( p_items[j] > p_items[i_index] )
                                    i_index = j;
                            }

                            playlist_Delete( p_sys->p_playlist,
                                             p_items[i_index] );
                            msg_Dbg( p_intf, "requested playlist delete: %d",
                                     p_items[i_index] );
                            p_items[i_index] = -1;
                        }
                    }

                    if( p_items ) free( p_items );
1809
1810
                    break;
                }
1811
1812
1813
1814
                case MVLC_KEEP:
                {
                    int i_item, *p_items = NULL, i_nb_items = 0;
                    char item[512], *p_parser = p_request;
1815
                    int i,j;
1816
1817
1818
1819
1820
1821
1822
1823

                    /* Get the list of items to keep */
                    while( (p_parser =
                            uri_extract_value( p_parser, "item", item, 512 )) )
                    {
                        if( !*item ) continue;

                        i_item = atoi( item );
1824
                        p_items = realloc( p_items, (i_nb_items + 1) *
1825
                                           sizeof(int) );
1826
                        p_items[i_nb_items] = i_item;
1827
1828
1829
                        i_nb_items++;
                    }

1830
1831
                    /* The items need to be deleted from in reversed order */
                    for( i = p_sys->p_playlist->i_size - 1; i >= 0 ; i-- )
1832
                    {
1833
1834
                        /* Check if the item is in the keep list */
                        for( j = 0 ; j < i_nb_items ; j++ )
1835
                        {
1836
                            if( p_items[j] == i ) break;
1837
                        }
1838
                        if( j == i_nb_items )
1839
                        {
1840
1841
1842
                            playlist_Delete( p_sys->p_playlist, i );
                            msg_Dbg( p_intf, "requested playlist delete: %d",
                                     i );
1843
                        }
1844
                    }
1845
1846
1847
1848

                    if( p_items ) free( p_items );
                    break;
                }
1849
1850
1851
1852
1853
1854
1855
1856
1857
                case MVLC_EMPTY:
                {
                    while( p_sys->p_playlist->i_size > 0 )
                    {
                        playlist_Delete( p_sys->p_playlist, 0 );
                    }
                    msg_Dbg( p_intf, "requested playlist empty" );
                    break;
                }
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
                case MVLC_SORT:
                {
                    char type[12];
                    char order[2];
                    int i_order;

                    uri_extract_value( p_request, "type", type, 12 );
                    uri_extract_value( p_request, "order", order, 2 );

                    if( order[0] == '0' ) i_order = SORT_NORMAL;
                    else i_order = SORT_REVERSE;

                    if( !strcmp( type , "title" ) )
                    {
                        playlist_SortTitle( p_sys->p_playlist , i_order );
                        msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
                    } else if( !strcmp( type , "group" ) )
                    {
                        playlist_SortGroup( p_sys->p_playlist , i_order );
                        msg_Dbg( p_intf, "requested playlist sort by group (%d)" , i_order );
                    } else if( !strcmp( type , "author" ) )
                    {
                        playlist_SortAuthor( p_sys->p_playlist , i_order );
                        msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
                    }

                    break;
                }
1886

1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
                /* admin function */
                case MVLC_CLOSE:
                {
                    char id[512];
                    uri_extract_value( p_request, "id", id, 512 );
                    msg_Dbg( p_intf, "requested close id=%s", id );
                    if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
                    {
                        msg_Warn( p_intf, "close failed for id=%s", id );
                    }
                    break;
                }
                case MVLC_SHUTDOWN:
                {
                    msg_Dbg( p_intf, "requested shutdown" );
                    p_intf->p_vlc->b_die = VLC_TRUE;
                    break;
                }
                default:
                    PRINTS( "<!-- control param(%s) unsuported -->", control );
                    break;
            }
            break;
gbazin's avatar
   
gbazin committed
1910

1911
1912
1913
1914
1915
        case MVLC_SET:
        {
            char    value[512];
            int     i;
            float   f;
gbazin's avatar
   
gbazin committed
1916

1917
1918
1919
1920
1921
1922
1923
1924
            if( i_request <= 0 ||
                *m->param1  == '\0' ||
                strstr( p_request, m->param1 ) == NULL )
            {
                break;
            }
            uri_extract_value( p_request, m->param1,  value, 512 );
            uri_decode_url_encoded( value );
gbazin's avatar
   
gbazin committed
1925

1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
<