http.c 29.9 KB
Newer Older
1
2
3
/*****************************************************************************
 * http.c : HTTP/HTTPS Remote control interface
 *****************************************************************************
4
 * Copyright (C) 2001-2006 the VideoLAN team
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 * $Id$
 *
 * Authors: Gildas Bazin <gbazin@netcourrier.com>
 *          Laurent Aimar <fenrir@via.ecp.fr>
 *          Christophe Massiot <massiot@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24
 *****************************************************************************/
25
26
27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
28
29
30
31
32
33
34
35
36
37
38

#include "http.h"

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

#define HOST_TEXT N_( "Host address" )
#define HOST_LONGTEXT N_( \
zorglub's avatar
zorglub committed
39
40
    "Address and port the HTTP interface will listen on. It defaults to " \
    "all network interfaces (0.0.0.0)." \
Christophe Mutricy's avatar
Christophe Mutricy committed
41
    " If you want the HTTP interface to be available only on the local " \
zorglub's avatar
zorglub committed
42
    "machine, enter 127.0.0.1" )
43
44
#define SRC_TEXT N_( "Source directory" )
#define SRC_LONGTEXT N_( "Source directory" )
45
46
#define HANDLERS_TEXT N_( "Handlers" )
#define HANDLERS_LONGTEXT N_( \
47
        "List of handler extensions and executable paths (for instance: " \
zorglub's avatar
zorglub committed
48
        "php=/usr/bin/php,pl=/usr/bin/perl)." )
49
50
51
52
#define ART_TEXT N_( "Export album art as /art." )
#define ART_LONGTEXT N_( \
        "Allow exporting album art for current playlist items at the " \
        "/art and /art?id=<id> URLs." )
53
54
#define CERT_TEXT N_( "Certificate file" )
#define CERT_LONGTEXT N_( "HTTP interface x509 PEM certificate file " \
zorglub's avatar
zorglub committed
55
                          "(enables SSL)." )
56
#define KEY_TEXT N_( "Private key file" )
zorglub's avatar
zorglub committed
57
#define KEY_LONGTEXT N_( "HTTP interface x509 PEM private key file." )
58
59
#define CA_TEXT N_( "Root CA file" )
#define CA_LONGTEXT N_( "HTTP interface x509 PEM trusted root CA " \
zorglub's avatar
zorglub committed
60
                        "certificates file." )
61
#define CRL_TEXT N_( "CRL file" )
zorglub's avatar
zorglub committed
62
#define CRL_LONGTEXT N_( "HTTP interace Certificates Revocation List file." )
63
64
65
66
67

vlc_module_begin();
    set_shortname( _("HTTP"));
    set_description( _("HTTP remote control interface") );
    set_category( CAT_INTERFACE );
zorglub's avatar
zorglub committed
68
    set_subcategory( SUBCAT_INTERFACE_MAIN );
69
70
        add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
        add_string ( "http-src",  NULL, NULL, SRC_TEXT,  SRC_LONGTEXT,  VLC_TRUE );
71
        add_obsolete_string ( "http-charset" );
72
#if defined( HAVE_FORK ) || defined( WIN32 )
73
74
        add_string ( "http-handlers", NULL, NULL, HANDLERS_TEXT, HANDLERS_LONGTEXT, VLC_TRUE );
#endif
75
        add_bool   ( "http-album-art", VLC_FALSE, NULL, ART_TEXT, ART_LONGTEXT, VLC_TRUE );
76
77
78
79
80
81
82
83
84
85
86
87
88
        set_section( N_("HTTP SSL" ), 0 );
        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 );
    set_capability( "interface", 0 );
    set_callbacks( Open, Close );
vlc_module_end();


/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
89
90
91
92
93
94
int  E_(ArtCallback)( httpd_handler_sys_t *p_args,
                          httpd_handler_t *p_handler, char *_p_url,
                          uint8_t *_p_request, int i_type,
                          uint8_t *_p_in, int i_in,
                          char *psz_remote_addr, char *psz_remote_host,
                          uint8_t **pp_data, int *pi_data );
95
96
97
98

/*****************************************************************************
 * Local functions
 *****************************************************************************/
99
#if !defined(__APPLE__) && !defined(SYS_BEOS) && !defined(WIN32)
100
static int DirectoryCheck( const char *psz_dir )
101
102
103
104
105
106
{
    DIR           *p_dir;

#ifdef HAVE_SYS_STAT_H
    struct stat   stat_info;

107
108
    if( ( utf8_stat( psz_dir, &stat_info ) == -1 )
      || !S_ISDIR( stat_info.st_mode ) )
109
110
111
112
113
    {
        return VLC_EGENERIC;
    }
#endif

114
    if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    {
        return VLC_EGENERIC;
    }
    closedir( p_dir );

    return VLC_SUCCESS;
}
#endif


/*****************************************************************************
 * Activate: initialize and create stuff
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
    intf_sys_t    *p_sys;
132
    char          *psz_address;
133
134
135
    const char    *psz_cert = NULL, *psz_key = NULL, *psz_ca = NULL,
                  *psz_crl = NULL;
    int           i_port       = 0;
136
    char          *psz_src = NULL;
137

138
    psz_address = var_CreateGetNonEmptyString( p_intf, "http-host" );
139
    if( psz_address != NULL )
140
    {
141
        char *psz_parser = strchr( psz_address, ':' );
142
143
144
145
146
147
        if( psz_parser )
        {
            *psz_parser++ = '\0';
            i_port = atoi( psz_parser );
        }
    }
148
149
    else
        psz_address = strdup("");
150
151
152
153
154
155

    p_intf->p_sys = p_sys = malloc( sizeof( intf_sys_t ) );
    if( !p_intf->p_sys )
    {
        return( VLC_ENOMEM );
    }
156
157

    p_sys->p_playlist = pl_Yield( p_this );
158
159
    p_sys->p_input    = NULL;
    p_sys->p_vlm      = NULL;
160
161
    p_sys->psz_address = psz_address;
    p_sys->i_port     = i_port;
162
    p_sys->p_art_handler = NULL;
163

164
165
166
    /* determine file handler associations */
    p_sys->i_handlers = 0;
    p_sys->pp_handlers = NULL;
167
#if defined( HAVE_FORK ) || defined( WIN32 )
168
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
    psz_src = config_GetPsz( p_intf, "http-handlers" );
    if( psz_src != NULL && *psz_src )
    {
        char *p = psz_src;
        while( p != NULL )
        {
            http_association_t *p_handler;
            char *psz_ext = p;
            char *psz_program, *psz_options;
            p = strchr( p, '=' );
            if( p == NULL ) break;
            *p++ = '\0';
            psz_program = p;
            p = strchr( p, ',' );
            if( p != NULL )
                *p++ = '\0';

            p_handler = malloc( sizeof( http_association_t ) );
            p_handler->psz_ext = strdup( psz_ext );
            psz_options = E_(FirstWord)( psz_program, psz_program );
            p_handler->i_argc = 0;
            p_handler->ppsz_argv = NULL;
            TAB_APPEND( p_handler->i_argc, p_handler->ppsz_argv,
                        strdup( psz_program ) );
            while( psz_options != NULL && *psz_options )
            {
                char *psz_next = E_(FirstWord)( psz_options, psz_options );
                TAB_APPEND( p_handler->i_argc, p_handler->ppsz_argv,
                            strdup( psz_options ) );
                psz_options = psz_next;
            }
            /* NULL will be appended later on */

            TAB_APPEND( p_sys->i_handlers, p_sys->pp_handlers, p_handler );
        }
    }
    if( psz_src != NULL )
        free( psz_src );
#endif

208
209
210
211
212
213
    /* determine SSL configuration */
    psz_cert = config_GetPsz( p_intf, "http-intf-cert" );
    if ( psz_cert != NULL )
    {
        msg_Dbg( p_intf, "enabling TLS for HTTP interface (cert file: %s)",
                 psz_cert );
214
215
216
        psz_key = var_GetNonEmptyString( p_intf, "http-intf-key" );
        psz_ca = var_GetNonEmptyString( p_intf, "http-intf-ca" );
        psz_crl = var_GetNonEmptyString( p_intf, "http-intf-crl" );
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

        if( i_port <= 0 )
            i_port = 8443;
    }
    else
    {
        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, psz_cert, psz_key, psz_ca,
                                            psz_crl );
    if( p_sys->p_httpd_host == NULL )
    {
        msg_Err( p_intf, "cannot listen on %s:%d", psz_address, i_port );
235
        pl_Release( p_this );
236
        free( p_sys->psz_address );
237
238
239
        free( p_sys );
        return VLC_EGENERIC;
    }
240
241
242
243
244
245
    else
    {
        char psz_tmp[NI_MAXHOST + 6];

        /* Ugly hack to run several HTTP servers on different ports */
        snprintf( psz_tmp, sizeof (psz_tmp), "%s:%d", psz_address, i_port + 1 );
246
        var_Create(p_intf->p_libvlc, "http-host", VLC_VAR_STRING );
247
        var_SetString( p_intf->p_libvlc, "http-host", psz_tmp );
248
    }
249
250
251
252

    p_sys->i_files  = 0;
    p_sys->pp_files = NULL;

253
#if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
254
255
    if ( ( psz_src = config_GetPsz( p_intf, "http-src" )) == NULL )
    {
256
        const char * psz_vlcpath = config_GetDataDir();
257
        psz_src = malloc( strlen(psz_vlcpath) + strlen("/http" ) + 1 );
258
        if( !psz_src ) return VLC_ENOMEM;
259
        sprintf( psz_src, "%s/http", psz_vlcpath );
260
261
262
263
    }
#else
    psz_src = config_GetPsz( p_intf, "http-src" );

264
    if( ( psz_src == NULL ) || ( *psz_src == '\0' ) )
265
    {
266
267
268
269
270
        const char *data_path = config_GetDataDir ();
        char buf[strlen (data_path) + sizeof ("/http")];
        snprintf (buf, sizeof (buf), "%s/http", data_path);

        const char const* ppsz_paths[] = {
271
272
            "share/http",
            "../share/http",
273
            buf,
274
275
276
277
278
            NULL
        };
        unsigned i;

        if( psz_src != NULL )
279
        {
280
281
            free( psz_src );
            psz_src = NULL;
282
        }
283
284
285
286
287
288
289

        for( i = 0; ppsz_paths[i] != NULL; i++ )
            if( !DirectoryCheck( ppsz_paths[i] ) )
            {
                psz_src = strdup( ppsz_paths[i] );
                break;
            }
290
291
292
293
294
    }
#endif

    if( !psz_src || *psz_src == '\0' )
    {
295
        msg_Err( p_intf, "invalid web interface source directory" );
296
297
298
299
300
301
302
303
304
305
306
        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';
    }

    E_(ParseDirectory)( p_intf, psz_src, psz_src );
307
308
309
310
311
312
313
    if( p_sys->i_files <= 0 )
    {
        msg_Err( p_intf, "cannot find any file in directory %s", psz_src );
        goto failed;
    }

    free( psz_src );
314

315
    if( config_GetInt( p_intf, "http-album-art" ) )
316
    {
317
318
319
320
321
322
323
324
325
326
327
328
329
330
        /* FIXME: we're leaking h */
        httpd_handler_sys_t *h = malloc( sizeof( httpd_handler_sys_t ) );
        if( !h )
        {
            msg_Err( p_intf, "not enough memory to allocate album art handler" );
            goto failed;
        }
        h->file.p_intf = p_intf;
        h->file.file = NULL;
        h->file.name = NULL;
        /* TODO: use ACL and login/password stuff here too */
        h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
                                         "/art", NULL, NULL, NULL,
                                         E_(ArtCallback), h );
331
        p_sys->p_art_handler = h->p_handler;
332
    }
333
334
335
336

    return VLC_SUCCESS;

failed:
337
338
    free( psz_src );
    free( p_sys->pp_files );
339
    httpd_HostDelete( p_sys->p_httpd_host );
340
    free( p_sys->psz_address );
341
    free( p_sys );
342
    pl_Release( p_this );
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
    return VLC_EGENERIC;
}

/*****************************************************************************
 * Close: destroy interface
 *****************************************************************************/
static 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;

    if( p_sys->p_vlm )
    {
        vlm_Delete( p_sys->p_vlm );
    }
    for( i = 0; i < p_sys->i_files; i++ )
    {
362
363
364
365
366
367
368
369
370
371
372
373
        if( p_sys->pp_files[i]->b_handler )
            httpd_HandlerDelete( ((httpd_handler_sys_t *)p_sys->pp_files[i])->p_handler );
        else
            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 );
        if( p_sys->pp_files[i]->p_redir2 )
            httpd_RedirectDelete( p_sys->pp_files[i]->p_redir2 );

        free( p_sys->pp_files[i]->file );
        free( p_sys->pp_files[i]->name );
        free( p_sys->pp_files[i] );
374
375
376
377
378
    }
    if( p_sys->pp_files )
    {
        free( p_sys->pp_files );
    }
379
380
381
382
383
384
385
386
387
388
389
390
391
    for( i = 0; i < p_sys->i_handlers; i++ )
    {
        http_association_t *p_handler = p_sys->pp_handlers[i];
        int j;
        free( p_handler->psz_ext );
        for( j = 0; j < p_handler->i_argc; j++ )
            free( p_handler->ppsz_argv[j] );
        if( p_handler->i_argc )
            free( p_handler->ppsz_argv );
        free( p_handler );
    }
    if( p_sys->i_handlers )
        free( p_sys->pp_handlers );
392
393
    if( p_sys->p_art_handler )
        httpd_HandlerDelete( p_sys->p_art_handler );
394
    httpd_HostDelete( p_sys->p_httpd_host );
395
    free( p_sys->psz_address );
396
    free( p_sys );
397
    pl_Release( p_this );
398
399
400
401
402
403
404
}

/****************************************************************************
 * HttpCallback:
 ****************************************************************************
 * a file with b_html is parsed and all "macro" replaced
 ****************************************************************************/
405
406
407
408
409
410
411
412
static void Callback404( httpd_file_sys_t *p_args, char **pp_data,
                         int *pi_data )
{
    char *p = *pp_data = malloc( 10240 );
    if( !p )
    {
        return;
    }
Christophe Mutricy's avatar
Christophe Mutricy committed
413
414
    p += sprintf( p, "Content-Type: text/html\n" );
    p += sprintf( p, "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" );
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
    p += sprintf( p, "<head>\n" );
    p += sprintf( p, "<title>Error loading %s</title>\n", p_args->file );
    p += sprintf( p, "</head>\n" );
    p += sprintf( p, "<body>\n" );
    p += sprintf( p, "<h1><center>Error loading %s for %s</center></h1>\n", p_args->file, p_args->name );
    p += sprintf( p, "<a href=\"http://www.videolan.org/\">VideoLAN</a>\n" );
    p += sprintf( p, "</body>\n" );
    p += sprintf( p, "</html>\n" );

    *pi_data = strlen( *pp_data );
}

static void ParseExecute( httpd_file_sys_t *p_args, char *p_buffer,
                          int i_buffer, char *p_request,
                          char **pp_data, int *pi_data )
{
431
    intf_sys_t *p_sys = p_args->p_intf->p_sys;
432
433
434
435
436
437
438
439
    int i_request = p_request != NULL ? strlen( p_request ) : 0;
    char *dst;
    vlc_value_t val;
    char position[4]; /* percentage */
    char time[12]; /* in seconds */
    char length[12]; /* in seconds */
    audio_volume_t i_volume;
    char volume[5];
440
    const char *state;
dionoea's avatar
dionoea committed
441
    char stats[20];
442

443
444
445
    assert( p_sys->p_input == NULL );
    /* FIXME: proper locking anyone? */
    p_sys->p_input = p_sys->p_playlist->p_input;
446
447
    if( p_sys->p_input )
    {
448
        vlc_object_yield( p_sys->p_input );
449
450
451
        var_Get( p_sys->p_input, "position", &val);
        sprintf( position, "%d" , (int)((val.f_float) * 100.0));
        var_Get( p_sys->p_input, "time", &val);
Laurent Aimar's avatar
Laurent Aimar committed
452
        sprintf( time, I64Fi, (int64_t)val.i_time / I64C(1000000) );
453
        var_Get( p_sys->p_input, "length", &val);
Laurent Aimar's avatar
Laurent Aimar committed
454
        sprintf( length, I64Fi, (int64_t)val.i_time / I64C(1000000) );
455
456
457
458

        var_Get( p_sys->p_input, "state", &val );
        if( val.i_int == PLAYING_S )
        {
459
            state = "playing";
460
        }
461
462
        else if( val.i_int == OPENING_S )
        {
463
            state = "opening/connecting";
464
465
466
        }
        else if( val.i_int == BUFFERING_S )
        {
467
            state = "buffering";
468
        }
469
470
        else if( val.i_int == PAUSE_S )
        {
471
            state = "paused";
472
473
474
        }
        else
        {
475
            state = "stop";
476
477
478
479
480
481
482
        }
    }
    else
    {
        sprintf( position, "%d", 0 );
        sprintf( time, "%d", 0 );
        sprintf( length, "%d", 0 );
483
        state = "stop";
484
485
486
487
488
489
490
491
492
493
494
495
496
    }

    aout_VolumeGet( p_args->p_intf, &i_volume );
    sprintf( volume, "%d", (int)i_volume );

    p_args->vars = E_(mvar_New)( "variables", "" );
    E_(mvar_AppendNewVar)( p_args->vars, "url_param",
                           i_request > 0 ? "1" : "0" );
    E_(mvar_AppendNewVar)( p_args->vars, "url_value", p_request );
    E_(mvar_AppendNewVar)( p_args->vars, "version", VLC_Version() );
    E_(mvar_AppendNewVar)( p_args->vars, "copyright", COPYRIGHT_MESSAGE );
    E_(mvar_AppendNewVar)( p_args->vars, "vlc_compile_by", VLC_CompileBy() );
    E_(mvar_AppendNewVar)( p_args->vars, "vlc_compile_host",
497
                           VLC_CompileHost() );
498
    E_(mvar_AppendNewVar)( p_args->vars, "vlc_compile_domain",
499
                           VLC_CompileDomain() );
500
501
502
503
504
505
506
    E_(mvar_AppendNewVar)( p_args->vars, "vlc_compiler", VLC_Compiler() );
    E_(mvar_AppendNewVar)( p_args->vars, "vlc_changeset", VLC_Changeset() );
    E_(mvar_AppendNewVar)( p_args->vars, "stream_position", position );
    E_(mvar_AppendNewVar)( p_args->vars, "stream_time", time );
    E_(mvar_AppendNewVar)( p_args->vars, "stream_length", length );
    E_(mvar_AppendNewVar)( p_args->vars, "volume", volume );
    E_(mvar_AppendNewVar)( p_args->vars, "stream_state", state );
507
    E_(mvar_AppendNewVar)( p_args->vars, "charset", "UTF-8" );
508

dionoea's avatar
dionoea committed
509
510
511
    /* Stats */
    if( p_sys->p_input )
    {
512
513
514
515
516
        /* FIXME: Workarround a stupid assert in input_GetItem */
        input_item_t *p_item = p_sys->p_input && p_sys->p_input->p
                               ? input_GetItem( p_sys->p_input )
                               : NULL;

dionoea's avatar
dionoea committed
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
        if( p_item )
        {
            vlc_mutex_lock( &p_item->p_stats->lock );
#define STATS_INT( n ) sprintf( stats, "%d", p_item->p_stats->i_ ## n ); \
                       E_(mvar_AppendNewVar)( p_args->vars, #n, stats );
#define STATS_FLOAT( n ) sprintf( stats, "%f", p_item->p_stats->f_ ## n ); \
                       E_(mvar_AppendNewVar)( p_args->vars, #n, stats );
            STATS_INT( read_bytes )
            STATS_FLOAT( input_bitrate )
            STATS_INT( demux_read_bytes )
            STATS_FLOAT( demux_bitrate )
            STATS_INT( decoded_video )
            STATS_INT( displayed_pictures )
            STATS_INT( lost_pictures )
            STATS_INT( decoded_audio )
            STATS_INT( played_abuffers )
            STATS_INT( lost_abuffers )
            STATS_INT( sent_packets )
            STATS_INT( sent_bytes )
            STATS_FLOAT( send_bitrate )
#undef STATS_INT
#undef STATS_FLOAT
            vlc_mutex_unlock( &p_item->p_stats->lock );
        }
    }

543
544
545
546
547
548
549
550
551
552
553
554
555
    E_(SSInit)( &p_args->stack );

    /* allocate output */
    *pi_data = i_buffer + 1000;
    dst = *pp_data = malloc( *pi_data );

    /* we parse executing all  <vlc /> macros */
    E_(Execute)( p_args, p_request, i_request, pp_data, pi_data, &dst,
                 &p_buffer[0], &p_buffer[i_buffer] );

    *dst     = '\0';
    *pi_data = dst - *pp_data;

556
557
558
559
560
    if( p_sys->p_input != NULL )
    {
        vlc_object_release( p_sys->p_input );
        p_sys->p_input = NULL;
    }
561
562
563
564
    E_(SSClean)( &p_args->stack );
    E_(mvar_Delete)( p_args->vars );
}

565
566
567
568
569
int  E_(HttpCallback)( httpd_file_sys_t *p_args,
                       httpd_file_t *p_file,
                       uint8_t *_p_request,
                       uint8_t **_pp_data, int *pi_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
570
    VLC_UNUSED(p_file);
571
572
573
574
    char *p_request = (char *)_p_request;
    char **pp_data = (char **)_pp_data;
    FILE *f;

575
    if( ( f = utf8_fopen( p_args->file, "r" ) ) == NULL )
576
    {
577
        Callback404( p_args, pp_data, pi_data );
578
579
580
581
582
583
584
585
586
587
588
        return VLC_SUCCESS;
    }

    if( !p_args->b_html )
    {
        E_(FileLoad)( f, pp_data, pi_data );
    }
    else
    {
        int  i_buffer;
        char *p_buffer;
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608

        /* first we load in a temporary buffer */
        E_(FileLoad)( f, &p_buffer, &i_buffer );

        ParseExecute( p_args, p_buffer, i_buffer, p_request, pp_data, pi_data );

        free( p_buffer );
    }

    fclose( f );

    return VLC_SUCCESS;
}

/****************************************************************************
 * HandlerCallback:
 ****************************************************************************
 * call the external handler and parse vlc macros if Content-Type is HTML
 ****************************************************************************/
int  E_(HandlerCallback)( httpd_handler_sys_t *p_args,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
609
                          httpd_handler_t *p_handler, char *_p_url,
610
611
612
613
614
                          uint8_t *_p_request, int i_type,
                          uint8_t *_p_in, int i_in,
                          char *psz_remote_addr, char *psz_remote_host,
                          uint8_t **_pp_data, int *pi_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
615
    VLC_UNUSED(p_handler); VLC_UNUSED(_p_in);
616
617
618
619
620
621
622
    char *p_url = (char *)_p_url;
    char *p_request = (char *)_p_request;
    char **pp_data = (char **)_pp_data;
    char *p_in = (char *)p_in;
    int i_request = p_request != NULL ? strlen( p_request ) : 0;
    char *p;
    int i_env = 0;
623
    char **ppsz_env = NULL;
624
625
    char *psz_tmp;
    char sep;
626
    size_t i_buffer;
627
    char *p_buffer;
628
    char *psz_cwd, *psz_file = NULL;
629
    int i_ret;
630
631
632
633
634
635
636
637

#ifdef WIN32
    sep = '\\';
#else
    sep = '/';
#endif

    /* Create environment for the CGI */
638
639
640
    TAB_APPEND( i_env, ppsz_env, strdup("GATEWAY_INTERFACE=CGI/1.1") );
    TAB_APPEND( i_env, ppsz_env, strdup("SERVER_PROTOCOL=HTTP/1.1") );
    TAB_APPEND( i_env, ppsz_env, strdup("SERVER_SOFTWARE=" COPYRIGHT_MESSAGE) );
641
642
643
644

    switch( i_type )
    {
    case HTTPD_MSG_GET:
645
        TAB_APPEND( i_env, ppsz_env, strdup("REQUEST_METHOD=GET") );
646
647
        break;
    case HTTPD_MSG_POST:
648
        TAB_APPEND( i_env, ppsz_env, strdup("REQUEST_METHOD=POST") );
649
650
        break;
    case HTTPD_MSG_HEAD:
651
        TAB_APPEND( i_env, ppsz_env, strdup("REQUEST_METHOD=HEAD") );
652
653
654
655
656
657
658
659
660
        break;
    default:
        break;
    }

    if( i_request )
    {
        psz_tmp = malloc( sizeof("QUERY_STRING=") + i_request );
        sprintf( psz_tmp, "QUERY_STRING=%s", p_request );
661
        TAB_APPEND( i_env, ppsz_env, psz_tmp );
662
663
664
665

        psz_tmp = malloc( sizeof("REQUEST_URI=?") + strlen(p_url)
                           + i_request );
        sprintf( psz_tmp, "REQUEST_URI=%s?%s", p_url, p_request );
666
        TAB_APPEND( i_env, ppsz_env, psz_tmp );
667
668
669
670
671
    }
    else
    {
        psz_tmp = malloc( sizeof("REQUEST_URI=") + strlen(p_url) );
        sprintf( psz_tmp, "REQUEST_URI=%s", p_url );
672
        TAB_APPEND( i_env, ppsz_env, psz_tmp );
673
674
675
676
    }

    psz_tmp = malloc( sizeof("SCRIPT_NAME=") + strlen(p_url) );
    sprintf( psz_tmp, "SCRIPT_NAME=%s", p_url );
677
    TAB_APPEND( i_env, ppsz_env, psz_tmp );
678
679
680
681

#define p_sys p_args->file.p_intf->p_sys
    psz_tmp = malloc( sizeof("SERVER_NAME=") + strlen(p_sys->psz_address) );
    sprintf( psz_tmp, "SERVER_NAME=%s", p_sys->psz_address );
682
    TAB_APPEND( i_env, ppsz_env, psz_tmp );
683
684
685

    psz_tmp = malloc( sizeof("SERVER_PORT=") + 5 );
    sprintf( psz_tmp, "SERVER_PORT=%u", p_sys->i_port );
686
    TAB_APPEND( i_env, ppsz_env, psz_tmp );
687
688
#undef p_sys

689
690
691
    p = getenv( "PATH" );
    if( p != NULL )
    {
692
693
        psz_tmp = malloc( sizeof("PATH=") + strlen(p) );
        sprintf( psz_tmp, "PATH=%s", p );
694
695
696
        TAB_APPEND( i_env, ppsz_env, psz_tmp );
    }

697
698
699
700
701
702
703
704
705
706
#ifdef WIN32
    p = getenv( "windir" );
    if( p != NULL )
    {
        psz_tmp = malloc( sizeof("SYSTEMROOT=") + strlen(p) );
        sprintf( psz_tmp, "SYSTEMROOT=%s", p );
        TAB_APPEND( i_env, ppsz_env, psz_tmp );
    }
#endif

707
708
709
710
    if( psz_remote_addr != NULL && *psz_remote_addr )
    {
        psz_tmp = malloc( sizeof("REMOTE_ADDR=") + strlen(psz_remote_addr) );
        sprintf( psz_tmp, "REMOTE_ADDR=%s", psz_remote_addr );
711
        TAB_APPEND( i_env, ppsz_env, psz_tmp );
712
713
714
715
716
717
    }

    if( psz_remote_host != NULL && *psz_remote_host )
    {
        psz_tmp = malloc( sizeof("REMOTE_HOST=") + strlen(psz_remote_host) );
        sprintf( psz_tmp, "REMOTE_HOST=%s", psz_remote_host );
718
        TAB_APPEND( i_env, ppsz_env, psz_tmp );
719
720
721
722
723
724
    }

    if( i_in )
    {
        p = p_in;
        for ( ; ; )
725
        {
726
            if( !strncasecmp( p, "Content-Type: ", strlen("Content-Type: ") ) )
727
            {
728
729
730
731
732
733
                char *end = strchr( p, '\r' );
                if( end == NULL )
                    break;
                *end = '\0';
                psz_tmp = malloc( sizeof("CONTENT_TYPE=") + strlen(p) );
                sprintf( psz_tmp, "CONTENT_TYPE=%s", p );
734
                TAB_APPEND( i_env, ppsz_env, psz_tmp );
735
                *end = '\r';
736
            }
737
738
            if( !strncasecmp( p, "Content-Length: ",
                              strlen("Content-Length: ") ) )
739
            {
740
741
742
743
744
745
                char *end = strchr( p, '\r' );
                if( end == NULL )
                    break;
                *end = '\0';
                psz_tmp = malloc( sizeof("CONTENT_LENGTH=") + strlen(p) );
                sprintf( psz_tmp, "CONTENT_LENGTH=%s", p );
746
                TAB_APPEND( i_env, ppsz_env, psz_tmp );
747
                *end = '\r';
748
            }
749
750
751

            p = strchr( p, '\n' );
            if( p == NULL || p[1] == '\r' )
752
            {
753
754
                p = NULL;
                break;
755
            }
756
            p++;
757
        }
758
    }
759

760
761
    psz_file = strrchr( p_args->file.file, sep );
    if( psz_file != NULL )
762
    {
763
764
765
        psz_file++;
        psz_tmp = malloc( sizeof("SCRIPT_FILENAME=") + strlen(psz_file) );
        sprintf( psz_tmp, "SCRIPT_FILENAME=%s", psz_file );
766
767
768
        TAB_APPEND( i_env, ppsz_env, psz_tmp );

        TAB_APPEND( p_args->p_association->i_argc,
769
                    p_args->p_association->ppsz_argv, psz_file );
770
771
    }

772
    TAB_APPEND( i_env, ppsz_env, NULL );
773

774
775
    TAB_APPEND( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
                NULL );
776

777
778
779
780
781
782
783
784
785
786
787
788
    psz_tmp = strdup( p_args->file.file );
    p = strrchr( psz_tmp, sep );
    if( p != NULL )
    {
        *p = '\0';
        psz_cwd = psz_tmp;
    }
    else
    {
        free( psz_tmp );
        psz_cwd = NULL;
    }
789

790
791
792
    i_ret = vlc_execve( p_args->file.p_intf, p_args->p_association->i_argc,
                        p_args->p_association->ppsz_argv, ppsz_env, psz_cwd,
                        (char *)p_in, i_in, &p_buffer, &i_buffer );
793
794
795
    TAB_REMOVE( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
                NULL );
    TAB_REMOVE( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
796
                psz_file );
797
798
    if( psz_cwd != NULL )
        free( psz_cwd );
799
800
    while( i_env )
        TAB_REMOVE( i_env, ppsz_env, ppsz_env[0] );
801

802
803
804
805
806
    if( i_ret == -1 )
    {
        Callback404( (httpd_file_sys_t *)p_args, pp_data, pi_data );
        return VLC_SUCCESS;
    }
807
    p = p_buffer;
808
809
    while( strncasecmp( p, "Content-Type: text/html",
                        strlen("Content-Type: text/html") ) )
810
811
812
813
814
815
816
817
818
    {
        p = strchr( p, '\n' );
        if( p == NULL || p[1] == '\r' )
        {
            p = NULL;
            break;
        }
        p++;
    }
819

820
821
822
823
824
825
826
827
828
    if( p == NULL )
    {
        *pp_data = p_buffer;
        *pi_data = i_buffer;
    }
    else
    {
        ParseExecute( (httpd_file_sys_t *)p_args, p_buffer, i_buffer,
                      p_request, pp_data, pi_data );
829
830
831
832
833
834

        free( p_buffer );
    }

    return VLC_SUCCESS;
}
835
836
837

int  E_(ArtCallback)( httpd_handler_sys_t *p_args,
                          httpd_handler_t *p_handler, char *_p_url,
838
839
                          uint8_t *p_request, int i_type,
                          uint8_t *p_in, int i_in,
840
841
842
                          char *psz_remote_addr, char *psz_remote_host,
                          uint8_t **pp_data, int *pi_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
843
844
845
846
    VLC_UNUSED(p_handler); VLC_UNUSED(_p_url); VLC_UNUSED(i_type); 
    VLC_UNUSED(p_in); VLC_UNUSED(i_in); VLC_UNUSED(psz_remote_addr); 
    VLC_UNUSED(psz_remote_host); 

847
848
849
    char *psz_art = NULL;
    intf_thread_t *p_intf = p_args->file.p_intf;
    intf_sys_t *p_sys = p_intf->p_sys;
850
    char psz_id[16];
851
852
    input_item_t *p_item = NULL;
    int i_id;
853

854
855
    psz_id[0] = '\0';
    if( p_request )
856
        E_(ExtractURIValue)( (char *)p_request, "id", psz_id, 15 );
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
    i_id = atoi( psz_id );
    if( i_id )
    {
        playlist_item_t *p_pl_item = playlist_ItemGetById( p_sys->p_playlist,
                                                           i_id, VLC_FALSE );
        if( p_pl_item )
            p_item = p_pl_item->p_input;
    }
    else
    {
        /* FIXME: Workarround a stupid assert in input_GetItem */
        if( p_sys->p_input && p_sys->p_input->p )
            p_item = input_GetItem( p_sys->p_input );
    }

    if( p_item )
873
    {
874
        psz_art = input_item_GetArtURL( p_item );
875
876
877
878
879
880
881
    }

    if( psz_art && !strncmp( psz_art, "file://", strlen( "file://" ) ) )
    {
        FILE *f;
        char *psz_ext;
        char *psz_header;
882
883
        char *p_data = NULL;
        int i_header_size, i_data;
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

        if( ( f = utf8_fopen( psz_art + strlen( "file://" ), "r" ) ) == NULL )
        {
            msg_Dbg( p_intf, "Couldn't open album art file %s",
                     psz_art + strlen( "file://" ) );
            Callback404( &p_args->file, (char**)pp_data, pi_data );
            free( psz_art );
            return VLC_SUCCESS;
        }

        E_(FileLoad)( f, &p_data, &i_data );

        fclose( f );

        psz_ext = strrchr( psz_art, '.' );
        if( psz_ext ) psz_ext++;

#define HEADER  "Content-Type: image/%s\n" \
                "Content-Length: %d\n" \
                "\n"
        i_header_size = asprintf( &psz_header, HEADER, psz_ext, i_data );
#undef HEADER

        *pi_data = i_header_size + i_data;
        *pp_data = (uint8_t*)malloc( *pi_data );
        memcpy( *pp_data, psz_header, i_header_size );
        memcpy( *pp_data+i_header_size, p_data, i_data );
        free( psz_header );
        free( p_data );
    }
    else
    {
        msg_Dbg( p_intf, "No album art found" );
        Callback404( &p_args->file, (char**)pp_data, pi_data );
    }

    free( psz_art );

    return VLC_SUCCESS;
}