http.c 96.5 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-2004 VideoLAN
Xavier Martin's avatar
Xavier Martin committed
5
 * $Id$
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
/* TODO:
29
30
31
32
 * - clean up ?
 * - doc ! (mouarf ;)
 */

33
#include <stdlib.h>
gbazin's avatar
 
gbazin 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
#include "vlc_tls.h"
43
44
45
46
47
48
49
50
51
52
53

#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
54
#ifdef HAVE_UNISTD_H
55
56
57
#   include <unistd.h>
#elif defined( WIN32 ) && !defined( UNDER_CE )
#   include <io.h>
gbazin's avatar
 
gbazin committed
58
59
#endif

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

65
66
67
68
69
/* stat() support for large files on win32 */
#if defined( WIN32 ) && !defined( UNDER_CE )
#   define stat _stati64
#endif

gbazin's avatar
 
gbazin committed
70
/*****************************************************************************
71
 * Module descriptor
gbazin's avatar
 
gbazin committed
72
 *****************************************************************************/
73
74
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
75
76
77

#define HOST_TEXT N_( "Host address" )
#define HOST_LONGTEXT N_( \
Anil Daoud's avatar
Anil Daoud committed
78
    "You can set the address and port the http interface will bind to." )
79
80
#define SRC_TEXT N_( "Source directory" )
#define SRC_LONGTEXT N_( "Source directory" )
81
82
83
84
85
86
87
88
#define CERT_TEXT N_( "Certificate file" )
#define CERT_LONGTEXT N_( "x509 PEM certificates path file" )
#define KEY_TEXT N_( "Private key file" )
#define KEY_LONGTEXT N_( "x509 PEM private key file" )
#define CA_TEXT N_( "Root CA file" )
#define CA_LONGTEXT N_( "x509 PEM trusted root CA certificates file" )
#define CRL_TEXT N_( "CRL file" )
#define CRL_LONGTEXT N_( "Certificates revocation list file" )
89
90
91
92
93

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 );
94
95
96
97
        add_string ( "http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT, VLC_TRUE );
        add_string ( "http-intf-key",  NULL, NULL, KEY_TEXT,  KEY_LONGTEXT,  VLC_TRUE );
        add_string ( "http-intf-ca",   NULL, NULL, CA_TEXT,   CA_LONGTEXT,   VLC_TRUE );
        add_string ( "http-intf-crl",  NULL, NULL, CRL_TEXT,  CRL_LONGTEXT,  VLC_TRUE );
98
    set_capability( "interface", 0 );
99
    set_callbacks( Open, Close );
100
101
102
103
104
105
vlc_module_end();


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

gbazin's avatar
gbazin committed
108
109
static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
                           char *psz_dir );
110

111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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;
}

133
134
135
136
static int  HttpCallback( httpd_file_sys_t *p_args,
                          httpd_file_t *,
                          uint8_t *p_request,
                          uint8_t **pp_data, int *pi_data );
137

138
static char *uri_extract_value( char *psz_uri, const char *psz_name,
gbazin's avatar
   
gbazin committed
139
                                char *psz_value, int i_value_max );
140
static int uri_test_param( char *psz_uri, const char *psz_name );
141

142
static void uri_decode_url_encoded( char *psz );
gbazin's avatar
 
gbazin committed
143

144
145
static char *Find_end_MRL( char *psz );

146
static playlist_item_t * parse_MRL( intf_thread_t * , char *psz );
147

gbazin's avatar
 
gbazin committed
148
/*****************************************************************************
149
 *
gbazin's avatar
 
gbazin committed
150
 *****************************************************************************/
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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;
165
} rpn_stack_t;
166

167
struct httpd_file_sys_t
168
{
169
170
171
    intf_thread_t    *p_intf;
    httpd_file_t     *p_file;
    httpd_redirect_t *p_redir;
172
173
174

    char          *file;
    char          *name;
175
176

    vlc_bool_t    b_html;
177
178

    /* inited for each access */
179
    rpn_stack_t   stack;
180
181
182
    mvar_t        *vars;
};

gbazin's avatar
 
gbazin committed
183
184
185
186
struct intf_sys_t
{
    httpd_host_t        *p_httpd_host;

187
188
    int                 i_files;
    httpd_file_sys_t    **pp_files;
189
190
191

    playlist_t          *p_playlist;
    input_thread_t      *p_input;
192
    vlm_t               *p_vlm;
193
    tls_t               *p_tls;
194
195
196
197
198
199
200
};



/*****************************************************************************
 * Activate: initialize and create stuff
 *****************************************************************************/
201
static int Open( vlc_object_t *p_this )
202
203
204
205
206
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
    intf_sys_t    *p_sys;
    char          *psz_host;
    char          *psz_address = "";
207
    const char    *psz_cert;
208
209
    int           i_port       = 0;
    char          *psz_src;
210
    tls_server_t  *p_tls;
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

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

    p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
zorglub's avatar
zorglub committed
227
228
229
230
    if( !p_intf->p_sys )
    {
        return( VLC_ENOMEM );
    }
231
232
    p_sys->p_playlist = NULL;
    p_sys->p_input    = NULL;
233
    p_sys->p_vlm      = NULL;
234

235
    /* TODO: avoid possible code duplication in other modules */
236
    psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
237
238
239
    if ( psz_cert != NULL )
    {
        const char *psz_pem;
240
241
242
243
244
245
246
247
248
249
250
251
252
253

        p_sys->p_tls = vlc_object_create( p_this, VLC_OBJECT_TLS );
        vlc_object_attach( p_sys->p_tls, p_this );

        p_sys->p_tls->p_module = module_Need( p_sys->p_tls, "tls", 0, 0 );
        if( p_sys->p_tls->p_module == NULL )
        {
            msg_Err( p_this, "cannot find TLS/SSL provider" );
            vlc_object_detach( p_sys->p_tls );
            vlc_object_destroy( p_sys->p_tls );
            p_sys->p_tls = NULL;
            return VLC_EGENERIC;
        }

254
255
        msg_Dbg( p_intf, "enablind TLS for HTTP interface (cert file: %s)",
                 psz_cert );
256
        psz_pem = config_GetPsz( p_intf, "http-intf-key" );
257
258
259
        if ( psz_pem == NULL )
            psz_pem = psz_cert;

260
        p_tls = tls_ServerCreate( p_sys->p_tls, psz_cert, psz_pem );
261
262
263
264
265
266
267
        if ( p_tls == NULL )
        {
            msg_Err( p_intf, "TLS initialization error" );
            free( p_sys );
            return VLC_EGENERIC;
        }

268
        psz_pem = config_GetPsz( p_intf, "http-intf-ca" );
269
270
271
272
273
274
275
276
        if ( ( psz_pem != NULL) && tls_ServerAddCA( p_tls, psz_pem ) )
        {
            msg_Err( p_intf, "TLS CA error" );
            tls_ServerDelete( p_tls );
            free( p_sys );
            return VLC_EGENERIC;
        }

277
        psz_pem = config_GetPsz( p_intf, "http-intf-crl" );
278
279
280
281
282
283
284
285
286
287
288
289
290
        if ( ( psz_pem != NULL) && tls_ServerAddCRL( p_tls, psz_pem ) )
        {
            msg_Err( p_intf, "TLS CRL error" );
            tls_ServerDelete( p_tls );
            free( p_sys );
            return VLC_EGENERIC;
        }

        if( i_port <= 0 )
            i_port = 8443;
    }
    else
    {
291
        p_sys->p_tls = NULL;
292
293
294
295
296
297
298
299
300
        p_tls = NULL;
        if( i_port <= 0 )
            i_port= 8080;
    }

    msg_Dbg( p_intf, "base %s:%d", psz_address, i_port );

    p_sys->p_httpd_host = httpd_TLSHostNew( VLC_OBJECT(p_intf), psz_address,
                                            i_port, p_tls );
301
    if( p_sys->p_httpd_host == NULL )
302
303
    {
        msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
304
305
        if ( p_tls != NULL )
            tls_ServerDelete( p_tls );
306
307
308
309
310
311
312
313
314
        free( p_sys );
        return VLC_EGENERIC;
    }

    if( psz_host )
    {
        free( psz_host );
    }

315
316
    p_sys->i_files  = 0;
    p_sys->pp_files = NULL;
317

gbazin's avatar
gbazin committed
318
319
#if defined(SYS_DARWIN) || defined(SYS_BEOS) || \
        ( defined(WIN32) && !defined(UNDER_CE ) )
320
321
322
323
    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
324
325
        if( !psz_src )
        {
326
            return VLC_ENOMEM;
zorglub's avatar
zorglub committed
327
        }
gbazin's avatar
   
gbazin committed
328
329
330
#if defined(WIN32)
        sprintf( psz_src, "%s/http", psz_vlcpath);
#else
331
        sprintf( psz_src, "%s/share/http", psz_vlcpath);
gbazin's avatar
   
gbazin committed
332
#endif
333
334
335
    }
#else
    psz_src = config_GetPsz( p_intf, "http-src" );
336
337
338
339
340
341
342
343
344
345
346
347

    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" );
        }
    }
348
#endif
349

350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
    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 )
    {
368
        msg_Err( p_intf, "cannot find any files (%s)", psz_src );
369
370
371
        goto failed;
    }
    p_intf->pf_run = Run;
gbazin's avatar
   
gbazin committed
372
    free( psz_src );
373
374
375
376

    return VLC_SUCCESS;

failed:
gbazin's avatar
   
gbazin committed
377
    if( psz_src ) free( psz_src );
378
379
380
381
382
    if( p_sys->pp_files )
    {
        free( p_sys->pp_files );
    }
    httpd_HostDelete( p_sys->p_httpd_host );
383
384
385
386
387
388
389
390
391
392
393
394
395
396
    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;

397
398
399
400
    if( p_sys->p_vlm )
    {
        vlm_Delete( p_sys->p_vlm );
    }
401
402
    for( i = 0; i < p_sys->i_files; i++ )
    {
403
404
405
406
407
408
       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 );
       }

409
410
411
412
       free( p_sys->pp_files[i]->file );
       free( p_sys->pp_files[i]->name );
       free( p_sys->pp_files[i] );
    }
413
414
415
416
417
    if( p_sys->pp_files )
    {
        free( p_sys->pp_files );
    }
    httpd_HostDelete( p_sys->p_httpd_host );
418
419
420
421
422
423
424
    /* TODO: do this in the httpd code to avoid code duplication */
    if( p_sys->p_tls != NULL )
    {
        module_Unneed( p_sys->p_tls, p_sys->p_tls->p_module );
        vlc_object_detach( p_sys->p_tls );
        vlc_object_destroy( p_sys->p_tls );
    }
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
456
457
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
    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
494
495
496
497
498
    if( !url || !p )
    {
        return NULL;
    }

499
#ifdef WIN32
gbazin's avatar
gbazin committed
500
501
    while( *name == '\\' || *name == '/' )
#else
502
    while( *name == '/' )
gbazin's avatar
gbazin committed
503
#endif
504
505
506
507
508
    {
        name++;
    }

    *p++ = '/';
gbazin's avatar
gbazin committed
509
510
511
512
513
    strcpy( p, name );

#ifdef WIN32
    /* convert '\\' into '/' */
    name = p;
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
    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
 ****************************************************************************/
gbazin's avatar
gbazin committed
538
539
static int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
                           char *psz_dir )
540
541
542
543
544
545
546
547
548
549
550
551
552
553
{
    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
554
    if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
    {
        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
580
581
            while( i_size > 0 && ( line[i_size-1] == '\n' ||
                   line[i_size-1] == '\r' ) )
582
583
584
585
586
587
588
589
590
591
592
593
594
595
            {
                i_size--;
            }

            line[i_size] = '\0';

            p = strchr( line, ':' );
            if( p )
            {
                *p++ = '\0';
                user = strdup( line );
                password = strdup( p );
            }
        }
gbazin's avatar
gbazin committed
596
597
        msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
                 user, password, i_size );
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616

        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 ) )
        {
617
618
            httpd_file_sys_t *f = malloc( sizeof( httpd_file_sys_t ) );

619
            f->p_intf  = p_intf;
620
621
            f->p_file = NULL;
            f->p_redir = NULL;
622
623
            f->file = strdup( dir );
            f->name = FileToUrl( &dir[strlen( psz_root )] );
624
            f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
625

626
            if( !f->name )
zorglub's avatar
zorglub committed
627
            {
Anil Daoud's avatar
Anil Daoud committed
628
                msg_Err( p_intf , "unable to parse directory" );
gbazin's avatar
   
gbazin committed
629
                closedir( p_dir );
gbazin's avatar
   
gbazin committed
630
                free( f );
zorglub's avatar
zorglub committed
631
632
                return( VLC_ENOMEM );
            }
633
634
635
636
637
638
639
640
            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 );

641
642
            if( f->p_file )
            {
643
                TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
644
            }
645
646
            /* For rep/ add a redir from rep to rep/ */
            if( f && f->name[strlen(f->name) - 1] == '/' )
647
            {
648
649
650
651
652
653
                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 );
654
655
656
657
658
659
660
661
662
663
664
665
            }
        }
    }

    if( user )
    {
        free( user );
    }
    if( password )
    {
        free( password );
    }
gbazin's avatar
   
gbazin committed
666
667
668

    closedir( p_dir );

669
670
671
672
673
674
675
676
677
678
679
    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
680
    if( !v ) return NULL;
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
830
831
832
833
834
835
836
    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
        {
837
            return field;
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
935
936
937
938
939
        }
    }
}

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

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

942
        mvar_AppendNewVar( itm, "uri", p_pl->pp_items[i]->input.psz_uri );
943

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

947
948
949
950
951
952
953
954
955
956
        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" );
957
    int i, j;
958
959
960
961
962
963
964

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

Laurent Aimar's avatar
Laurent Aimar committed
965
966
    vlc_mutex_lock( &p_input->input.p_item->lock );
    for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
967
    {
Laurent Aimar's avatar
Laurent Aimar committed
968
        info_category_t *p_category = p_input->input.p_item->pp_categories[i];
969
970
971
972
973
974
        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 );

975
        for ( j = 0; j < p_category->i_infos; j++ )
976
        {
977
            info_t *p_info = p_category->pp_infos[j];
978
979
            mvar_t *info = mvar_New( "info", "" );

980
981
            msg_Dbg( p_input, "adding info name=%s value=%s",
                     p_info->psz_name, p_info->psz_value );
982
983
984
985
986
987
            mvar_AppendNewVar( info, "name",  p_info->psz_name );
            mvar_AppendNewVar( info, "value", p_info->psz_value );
            mvar_AppendVar( iset, info );
        }
        mvar_AppendVar( s, cat );
    }
Laurent Aimar's avatar
Laurent Aimar committed
988
    vlc_mutex_unlock( &p_input->input.p_item->lock );
989
990
991

    return s;
}
992
#if 0
993
994
995
996
997
998
999
1000
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 ) )