http.c 62.3 KB
Newer Older
1
/*****************************************************************************
gbazin's avatar
 
gbazin committed
2
 * http.c: HTTP input module
3
 *****************************************************************************
4
 * Copyright (C) 2001-2008 the VideoLAN team
5
 * $Id$
6
 *
7 8
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Christophe Massiot <massiot@via.ecp.fr>
9
 *          Rémi Denis-Courmont <rem # videolan.org>
10
 *          Antoine Cellerier <dionoea at videolan dot org>
11 12 13 14 15
 *
 * 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.
16
 *
17 18 19 20 21 22 23
 * 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
24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 26 27 28 29
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
30 31 32 33
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

34
#include <vlc_common.h>
35
#include <vlc_plugin.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
36 37


zorglub's avatar
zorglub committed
38
#include <vlc_access.h>
39

zorglub's avatar
zorglub committed
40 41 42 43 44
#include <vlc_interface.h>
#include <vlc_meta.h>
#include <vlc_network.h>
#include <vlc_url.h>
#include <vlc_tls.h>
45
#include <vlc_strings.h>
46
#include <vlc_input.h>
47
#include <vlc_md5.h>
48

49 50 51 52
#ifdef HAVE_ZLIB_H
#   include <zlib.h>
#endif

53 54
#include <assert.h>

55 56 57
#ifdef HAVE_PROXY_H
#    include "proxy.h"
#endif
58 59 60
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
zorglub's avatar
zorglub committed
61
static int  Open ( vlc_object_t * );
62 63
static void Close( vlc_object_t * );

gbazin's avatar
 
gbazin committed
64
#define PROXY_TEXT N_("HTTP proxy")
gbazin's avatar
 
gbazin committed
65
#define PROXY_LONGTEXT N_( \
66
    "HTTP proxy to be used It must be of the form " \
67
    "http://[user@]myproxy.mydomain:myport/ ; " \
68
    "if empty, the http_proxy environment variable will be tried." )
69

70 71 72 73
#define PROXY_PASS_TEXT N_("HTTP proxy password")
#define PROXY_PASS_LONGTEXT N_( \
    "If your HTTP proxy requires a password, set it here." )

Christophe Massiot's avatar
Christophe Massiot committed
74
#define CACHING_TEXT N_("Caching value in ms")
gbazin's avatar
 
gbazin committed
75
#define CACHING_LONGTEXT N_( \
76
    "Caching value for HTTP streams. This " \
zorglub's avatar
zorglub committed
77
    "value should be set in milliseconds." )
gbazin's avatar
 
gbazin committed
78

gbazin's avatar
 
gbazin committed
79
#define AGENT_TEXT N_("HTTP user agent")
zorglub's avatar
zorglub committed
80
#define AGENT_LONGTEXT N_("User agent that will be " \
gbazin's avatar
 
gbazin committed
81 82
    "used for the connection.")

83
#define RECONNECT_TEXT N_("Auto re-connect")
zorglub's avatar
zorglub committed
84 85 86
#define RECONNECT_LONGTEXT N_( \
    "Automatically try to reconnect to the stream in case of a sudden " \
    "disconnect." )
87

zorglub's avatar
zorglub committed
88
#define CONTINUOUS_TEXT N_("Continuous stream")
zorglub's avatar
zorglub committed
89
#define CONTINUOUS_LONGTEXT N_("Read a file that is " \
Christophe Mutricy's avatar
Christophe Mutricy committed
90
    "being constantly updated (for example, a JPG file on a server). " \
91 92
    "You should not globally enable this option as it will break all other " \
    "types of HTTP streams." )
zorglub's avatar
zorglub committed
93

94 95 96
#define FORWARD_COOKIES_TEXT N_("Forward Cookies")
#define FORWARD_COOKIES_LONGTEXT N_("Forward Cookies Across http redirections ")

97
vlc_module_begin();
98
    set_description( N_("HTTP input") );
99
    set_capability( "access", 0 );
100
    set_shortname( N_( "HTTP(S)" ) );
zorglub's avatar
zorglub committed
101 102
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_ACCESS );
gbazin's avatar
 
gbazin committed
103 104

    add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
105
                false );
106 107
    add_password( "http-proxy-pwd", NULL, NULL,
                  PROXY_PASS_TEXT, PROXY_PASS_LONGTEXT, false );
gbazin's avatar
 
gbazin committed
108
    add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
109
                 CACHING_TEXT, CACHING_LONGTEXT, true );
gbazin's avatar
 
gbazin committed
110
    add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
111
                AGENT_LONGTEXT, true );
112
    add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
113
              RECONNECT_LONGTEXT, true );
zorglub's avatar
zorglub committed
114
    add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
115
              CONTINUOUS_LONGTEXT, true );
116
    add_bool( "http-forward-cookies", 0, NULL, FORWARD_COOKIES_TEXT,
117
              FORWARD_COOKIES_LONGTEXT, true );
118 119
    add_obsolete_string("http-user");
    add_obsolete_string("http-pwd");
120
    add_shortcut( "http" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
121
    add_shortcut( "https" );
zorglub's avatar
zorglub committed
122
    add_shortcut( "unsv" );
123
    add_shortcut( "itpc" ); /* iTunes Podcast */
124 125 126 127
    set_callbacks( Open, Close );
vlc_module_end();

/*****************************************************************************
128
 * Local prototypes
129
 *****************************************************************************/
130

131
/* RFC 2617: Basic and Digest Access Authentication */
132 133 134 135 136 137 138 139 140 141 142
typedef struct http_auth_t
{
    char *psz_realm;
    char *psz_domain;
    char *psz_nonce;
    char *psz_opaque;
    char *psz_stale;
    char *psz_algorithm;
    char *psz_qop;
    int i_nonce;
    char *psz_cnonce;
143
    char *psz_HA1; /* stored H(A1) value if algorithm = "MD5-sess" */
144 145
} http_auth_t;

146
struct access_sys_t
147
{
148
    int fd;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
149 150
    tls_session_t *p_tls;
    v_socket_t    *p_vs;
151

152 153 154
    /* From uri */
    vlc_url_t url;
    char    *psz_user_agent;
155
    http_auth_t auth;
156

157
    /* Proxy */
158
    bool b_proxy;
159
    vlc_url_t  proxy;
160
    http_auth_t proxy_auth;
161
    char       *psz_proxy_passbuf;
162

163 164
    /* */
    int        i_code;
Laurent Aimar's avatar
Laurent Aimar committed
165
    const char *psz_protocol;
166
    int        i_version;
167

168
    char       *psz_mime;
169
    char       *psz_pragma;
170
    char       *psz_location;
171 172 173
    bool b_mms;
    bool b_icecast;
    bool b_ssl;
174
#ifdef HAVE_ZLIB_H
175
    bool b_compressed;
176 177 178 179 180 181
    struct
    {
        z_stream   stream;
        uint8_t   *p_buffer;
    } inflate;
#endif
182

183
    bool b_chunked;
184
    int64_t    i_chunk;
185

186 187 188 189 190
    int        i_icy_meta;
    char       *psz_icy_name;
    char       *psz_icy_genre;
    char       *psz_icy_title;

191
    int64_t i_remaining;
zorglub's avatar
zorglub committed
192

193 194 195 196
    bool b_seekable;
    bool b_reconnect;
    bool b_continuous;
    bool b_pace_control;
197
    bool b_persist;
198

199
    vlc_array_t * cookies;
200
};
201

202
/* */
203
static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies );
204

205
/* */
206
static ssize_t Read( access_t *, uint8_t *, size_t );
207
static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
208 209
static int Seek( access_t *, int64_t );
static int Control( access_t *, int, va_list );
210

211
/* */
212
static int Connect( access_t *, int64_t );
zorglub's avatar
zorglub committed
213
static int Request( access_t *p_access, int64_t i_tell );
214
static void Disconnect( access_t * );
215

216 217 218 219 220 221
/* Small Cookie utilities. Cookies support is partial. */
static char * cookie_get_content( const char * cookie );
static char * cookie_get_domain( const char * cookie );
static char * cookie_get_name( const char * cookie );
static void cookie_append( vlc_array_t * cookies, char * cookie );

222 223 224 225 226

static void AuthParseHeader( access_t *p_access, const char *psz_header,
                             http_auth_t *p_auth );
static void AuthReply( access_t *p_acces, const char *psz_prefix,
                       vlc_url_t *p_url, http_auth_t *p_auth );
227 228
static int AuthCheckReply( access_t *p_access, const char *psz_header,
                           vlc_url_t *p_url, http_auth_t *p_auth );
229 230
static void AuthReset( http_auth_t *p_auth );

231 232 233
/*****************************************************************************
 * Open:
 *****************************************************************************/
234
static int Open( vlc_object_t *p_this )
235
{
236
    return OpenWithCookies( p_this, NULL );
237 238
}

239
static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies )
240
{
241 242
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;
243
    char         *psz, *p;
244
    /* Only forward an store cookies if the corresponding option is activated */
245
    bool   b_forward_cookies = var_CreateGetBool( p_access, "http-forward-cookies" );
246
    vlc_array_t * saved_cookies = b_forward_cookies ? (cookies ?: vlc_array_new()) : NULL;
247

248
    /* Set up p_access */
249
    STANDARD_READ_ACCESS_INIT;
250 251 252
#ifdef HAVE_ZLIB_H
    p_access->pf_read = ReadCompressed;
#endif
253
    p_sys->fd = -1;
254
    p_sys->b_proxy = false;
255
    p_sys->psz_proxy_passbuf = NULL;
256
    p_sys->i_version = 1;
257
    p_sys->b_seekable = true;
258
    p_sys->psz_mime = NULL;
259
    p_sys->psz_pragma = NULL;
260 261
    p_sys->b_mms = false;
    p_sys->b_icecast = false;
262 263
    p_sys->psz_location = NULL;
    p_sys->psz_user_agent = NULL;
264 265
    p_sys->b_pace_control = true;
    p_sys->b_ssl = false;
266
#ifdef HAVE_ZLIB_H
267
    p_sys->b_compressed = false;
268 269 270 271 272 273 274 275
    /* 15 is the max windowBits, +32 to enable optional gzip decoding */
    if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
        msg_Warn( p_access, "Error during zlib initialisation: %s",
                  p_sys->inflate.stream.msg );
    if( zlibCompileFlags() & (1<<17) )
        msg_Warn( p_access, "Your zlib was compiled without gzip support." );
    p_sys->inflate.p_buffer = NULL;
#endif
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
276 277
    p_sys->p_tls = NULL;
    p_sys->p_vs = NULL;
278 279 280 281
    p_sys->i_icy_meta = 0;
    p_sys->psz_icy_name = NULL;
    p_sys->psz_icy_genre = NULL;
    p_sys->psz_icy_title = NULL;
zorglub's avatar
zorglub committed
282
    p_sys->i_remaining = 0;
283 284 285 286
    p_sys->b_persist = false;
    p_access->info.i_size = -1;
    p_access->info.i_pos  = 0;
    p_access->info.b_eof  = false;
287

288
    p_sys->cookies = saved_cookies;
289

290 291 292 293 294 295
    /* Parse URI - remove spaces */
    p = psz = strdup( p_access->psz_path );
    while( (p = strchr( p, ' ' )) != NULL )
        *p = '+';
    vlc_UrlParse( &p_sys->url, psz, 0 );
    free( psz );
296

297
    if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
298
    {
299
        msg_Warn( p_access, "invalid host" );
300
        goto error;
301
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
302
    if( !strncmp( p_access->psz_access, "https", 5 ) )
303
    {
304
        /* HTTP over SSL */
305
        p_sys->b_ssl = true;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
306 307 308 309 310 311 312
        if( p_sys->url.i_port <= 0 )
            p_sys->url.i_port = 443;
    }
    else
    {
        if( p_sys->url.i_port <= 0 )
            p_sys->url.i_port = 80;
313 314 315
    }

    /* Do user agent */
316
    p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
317 318

    /* Check proxy */
319 320
    psz = var_CreateGetNonEmptyString( p_access, "http-proxy" );
    if( psz )
321
    {
322
        p_sys->b_proxy = true;
323
        vlc_UrlParse( &p_sys->proxy, psz, 0 );
324
        free( psz );
325
    }
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
#ifdef HAVE_PROXY_H
    else
    {
        pxProxyFactory *pf = px_proxy_factory_new();
        if (pf)
        {
            char *buf;
            int i;
            i=asprintf(&buf, "%s://%s", p_access->psz_access, p_access->psz_path);
            if (i >= 0)
            {
                msg_Dbg(p_access, "asking libproxy about url '%s'", buf);
                char **proxies = px_proxy_factory_get_proxies(pf, buf);
                if (proxies[0])
                {
                    msg_Dbg(p_access, "libproxy suggest to use '%s'", proxies[0]);
342
                    if(strcmp(proxies[0],"direct://") != 0)
343 344 345 346 347 348 349 350 351
                    {
                        p_sys->b_proxy = true;
                        vlc_UrlParse( &p_sys->proxy, proxies[0], 0);
                    }
                }
                for(i=0;proxies[i];i++) free(proxies[i]);
                free(proxies);
                free(buf);
            }
352
            px_proxy_factory_free(pf);
353 354 355 356 357 358 359
        }
        else
        {
            msg_Err(p_access, "Allocating memory for libproxy failed");
        }
    }
#elif HAVE_GETENV
360
    else
361
    {
362 363
        psz = getenv( "http_proxy" );
        if( psz )
gbazin's avatar
 
gbazin committed
364
        {
365
            p_sys->b_proxy = true;
366
            vlc_UrlParse( &p_sys->proxy, psz, 0 );
367
        }
368
    }
369
#endif
370 371 372 373 374 375
    if( psz ) /* No, this is NOT a use-after-free error */
    {
        psz = var_CreateGetNonEmptyString( p_access, "http-proxy-pwd" );
        if( psz )
            p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
    }
376

377
    if( p_sys->b_proxy )
378
    {
379 380
        if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
        {
381
            msg_Warn( p_access, "invalid proxy host" );
382 383 384
            goto error;
        }
        if( p_sys->proxy.i_port <= 0 )
385
        {
386
            p_sys->proxy.i_port = 80;
387 388
        }
    }
389

390
    msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
gbazin's avatar
 
gbazin committed
391
             p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
392
    if( p_sys->b_proxy )
393
    {
394
        msg_Dbg( p_access, "      proxy %s:%d", p_sys->proxy.psz_host,
gbazin's avatar
 
gbazin committed
395
                 p_sys->proxy.i_port );
396
    }
397
    if( p_sys->url.psz_username && *p_sys->url.psz_username )
398
    {
399
        msg_Dbg( p_access, "      user='%s'", p_sys->url.psz_username );
400 401
    }

402
    p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
zorglub's avatar
zorglub committed
403
    p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
404

405
connect:
406
    /* Connect */
407
    switch( Connect( p_access, 0 ) )
408
    {
409
        case -1:
410
            goto error;
411 412 413 414 415

        case -2:
            /* Retry with http 1.0 */
            msg_Dbg( p_access, "switching to HTTP version 1.0" );
            p_sys->i_version = 0;
416
            p_sys->b_seekable = false;
417

418
            if( !vlc_object_alive (p_access) || Connect( p_access, 0 ) )
419 420
                goto error;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
421
#ifndef NDEBUG
422 423 424 425 426 427 428
        case 0:
            break;

        default:
            msg_Err( p_access, "You should not be here" );
            abort();
#endif
429
    }
430

431 432
    if( p_sys->i_code == 401 )
    {
433
        char *psz_login = NULL, *psz_password = NULL;
434
        char psz_msg[250];
435
        int i_ret;
436 437 438 439 440 441 442 443 444 445 446
        /* FIXME ? */
        if( p_sys->url.psz_username && p_sys->url.psz_password &&
            p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
        {
            goto connect;
        }
        snprintf( psz_msg, 250,
            _("Please enter a valid login name and a password for realm %s."),
            p_sys->auth.psz_realm );
        msg_Dbg( p_access, "authentication failed for realm %s",
            p_sys->auth.psz_realm );
447
        i_ret = intf_UserLoginPassword( p_access, _("HTTP authentication"),
448
                                        psz_msg, &psz_login, &psz_password );
449 450 451 452 453 454
        if( i_ret == DIALOG_OK_YES )
        {
            msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
                        psz_login, psz_password );
            if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
            if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
455 456
            free( psz_login );
            free( psz_password );
457 458 459 460
            goto connect;
        }
        else
        {
461 462
            free( psz_login );
            free( psz_password );
463 464 465 466
            goto error;
        }
    }

gbazin's avatar
gbazin committed
467 468
    if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
          p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
469
        p_sys->psz_location && *p_sys->psz_location )
470
    {
471
        msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
472

473 474 475 476 477 478 479 480
        /* Do not accept redirection outside of HTTP works */
        if( strncmp( p_sys->psz_location, "http", 4 )
         || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
           && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
        {
            msg_Err( p_access, "insecure redirection ignored" );
            goto error;
        }
hartman's avatar
hartman committed
481 482 483 484
        free( p_access->psz_path );
        p_access->psz_path = strdup( p_sys->psz_location );
        /* Clean up current Open() run */
        vlc_UrlClean( &p_sys->url );
485
        AuthReset( &p_sys->auth );
hartman's avatar
hartman committed
486
        vlc_UrlClean( &p_sys->proxy );
487
        free( p_sys->psz_proxy_passbuf );
488
        AuthReset( &p_sys->proxy_auth );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
489 490 491 492
        free( p_sys->psz_mime );
        free( p_sys->psz_pragma );
        free( p_sys->psz_location );
        free( p_sys->psz_user_agent );
hartman's avatar
hartman committed
493

494
        Disconnect( p_access );
495
        cookies = p_sys->cookies;
hartman's avatar
hartman committed
496 497 498
        free( p_sys );

        /* Do new Open() run with new data */
499
        return OpenWithCookies( p_this, cookies );
500 501
    }

502
    if( p_sys->b_mms )
503
    {
504
        msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
505 506 507
        goto error;
    }

508
    if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
509
    {
510
        if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
511
        {
512
            if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
513
                !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
Rafaël Carré's avatar
Rafaël Carré committed
514 515
            {
                free( p_access->psz_demux );
516
                p_access->psz_demux = strdup( "nsv" );
Rafaël Carré's avatar
Rafaël Carré committed
517
            }
518 519
            else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
                     !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
Rafaël Carré's avatar
Rafaël Carré committed
520 521
            {
                free( p_access->psz_demux );
522
                p_access->psz_demux = strdup( "m4a" );
Rafaël Carré's avatar
Rafaël Carré committed
523
            }
524
            else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
Rafaël Carré's avatar
Rafaël Carré committed
525 526
            {
                free( p_access->psz_demux );
527
                p_access->psz_demux = strdup( "mp3" );
Rafaël Carré's avatar
Rafaël Carré committed
528
            }
529 530 531 532

            msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
                      p_access->psz_demux );

533 534 535
#if 0       /* Doesn't work really well because of the pre-buffering in
             * shoutcast servers (the buffer content will be sent as fast as
             * possible). */
536
            p_sys->b_pace_control = false;
537
#endif
538 539 540
        }
        else if( !p_sys->psz_mime )
        {
Rafaël Carré's avatar
Rafaël Carré committed
541 542 543
            free( p_access->psz_demux );
            /* Shoutcast */
            p_access->psz_demux = strdup( "mp3" );
544 545
        }
        /* else probably Ogg Vorbis */
546
    }
547 548 549 550
    else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
             p_sys->psz_mime &&
             !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
    {
Rafaël Carré's avatar
Rafaël Carré committed
551
        free( p_access->psz_demux );
552 553 554
        /* Grrrr! detect ultravox server and force NSV demuxer */
        p_access->psz_demux = strdup( "nsv" );
    }
555 556
    else if( !strcmp( p_access->psz_access, "itpc" ) )
    {
Rafaël Carré's avatar
Rafaël Carré committed
557
        free( p_access->psz_demux );
558 559
        p_access->psz_demux = strdup( "podcast" );
    }
560
    else if( p_sys->psz_mime &&
561 562
             !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
             ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
Rafaël Carré's avatar
Rafaël Carré committed
563 564
    {
        free( p_access->psz_demux );
565
        p_access->psz_demux = strdup( "xspf-open" );
Rafaël Carré's avatar
Rafaël Carré committed
566
    }
567

568 569 570 571
    if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );

    /* PTS delay */
    var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
572

573
    return VLC_SUCCESS;
574

575 576 577
error:
    vlc_UrlClean( &p_sys->url );
    vlc_UrlClean( &p_sys->proxy );
578
    free( p_sys->psz_proxy_passbuf );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
579 580 581 582
    free( p_sys->psz_mime );
    free( p_sys->psz_pragma );
    free( p_sys->psz_location );
    free( p_sys->psz_user_agent );
583

584
    Disconnect( p_access );
585 586 587
    free( p_sys );
    return VLC_EGENERIC;
}
588

589 590 591 592 593
/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
594 595
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys = p_access->p_sys;
596

597
    vlc_UrlClean( &p_sys->url );
598
    AuthReset( &p_sys->auth );
599
    vlc_UrlClean( &p_sys->proxy );
600
    AuthReset( &p_sys->proxy_auth );
601

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
602 603 604
    free( p_sys->psz_mime );
    free( p_sys->psz_pragma );
    free( p_sys->psz_location );
605

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
606 607 608
    free( p_sys->psz_icy_name );
    free( p_sys->psz_icy_genre );
    free( p_sys->psz_icy_title );
609

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
610
    free( p_sys->psz_user_agent );
611

612
    Disconnect( p_access );
613

614 615 616 617 618 619 620
    if( p_sys->cookies )
    {
        int i;
        for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
            free(vlc_array_item_at_index( p_sys->cookies, i ));
        vlc_array_destroy( p_sys->cookies );
    }
621

622 623 624 625 626
#ifdef HAVE_ZLIB_H
    inflateEnd( &p_sys->inflate.stream );
    free( p_sys->inflate.p_buffer );
#endif

627 628 629 630 631 632 633
    free( p_sys );
}

/*****************************************************************************
 * Read: Read up to i_len bytes from the http connection and place in
 * p_buffer. Return the actual number of bytes read
 *****************************************************************************/
634
static int ReadICYMeta( access_t *p_access );
635
static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
636
{
637 638
    access_sys_t *p_sys = p_access->p_sys;
    int i_read;
639 640

    if( p_sys->fd < 0 )
641
    {
642
        p_access->info.b_eof = true;
643
        return 0;
644
    }
645

646
    if( p_access->info.i_size >= 0 &&
647
        i_len + p_access->info.i_pos > p_access->info.i_size )
648
    {
649
        if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
650
        {
651
            p_access->info.b_eof = true;
652 653
            return 0;
        }
654
    }
zorglub's avatar
zorglub committed
655

656 657 658 659
    if( p_sys->b_chunked )
    {
        if( p_sys->i_chunk < 0 )
        {
660
            p_access->info.b_eof = true;
661 662 663 664 665
            return 0;
        }

        if( p_sys->i_chunk <= 0 )
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
666
            char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
667 668 669
            /* read the chunk header */
            if( psz == NULL )
            {
670
                /* fatal error - end of file */
671
                msg_Dbg( p_access, "failed reading chunk-header line" );
672
                return 0;
673 674 675 676 677 678 679
            }
            p_sys->i_chunk = strtoll( psz, NULL, 16 );
            free( psz );

            if( p_sys->i_chunk <= 0 )   /* eof */
            {
                p_sys->i_chunk = -1;
680
                p_access->info.b_eof = true;
681 682 683 684 685 686 687 688 689
                return 0;
            }
        }

        if( i_len > p_sys->i_chunk )
        {
            i_len = p_sys->i_chunk;
        }
    }
690
    else if( p_access->info.i_size != -1 && (int64_t)i_len > p_sys->i_remaining) {
zorglub's avatar
zorglub committed
691
        /* Only ask for the remaining length */
692 693 694 695
        i_len = (size_t)p_sys->i_remaining;
        if(i_len == 0) {
            p_access->info.b_eof = true;
            return 0;
zorglub's avatar
zorglub committed
696 697 698
        }
    }

699

700 701
    if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
    {
zorglub's avatar
zorglub committed
702
        int64_t i_next = p_sys->i_icy_meta -
703
                                    p_access->info.i_pos % p_sys->i_icy_meta;
zorglub's avatar
zorglub committed
704

705 706 707 708
        if( i_next == p_sys->i_icy_meta )
        {
            if( ReadICYMeta( p_access ) )
            {
709
                p_access->info.b_eof = true;
710 711 712 713 714 715
                return -1;
            }
        }
        if( i_len > i_next )
            i_len = i_next;
    }
716

717
    i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
zorglub's avatar
zorglub committed
718

719
    if( i_read > 0 )
720
    {
721
        p_access->info.i_pos += i_read;
722 723 724 725 726 727 728

        if( p_sys->b_chunked )
        {
            p_sys->i_chunk -= i_read;
            if( p_sys->i_chunk <= 0 )
            {
                /* read the empty line */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
729
                char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
730
                free( psz );
731 732
            }
        }
733
    }
734 735
    else if( i_read == 0 )
    {
736 737 738 739 740 741
        /*
         * I very much doubt that this will work.
         * If i_read == 0, the connection *IS* dead, so the only
         * sensible thing to do is Disconnect() and then retry.
         * Otherwise, I got recv() completely wrong. -- Courmisch
         */
zorglub's avatar
zorglub committed
742 743 744
        if( p_sys->b_continuous )
        {
            Request( p_access, 0 );
745
            p_sys->b_continuous = false;
zorglub's avatar
zorglub committed
746
            i_read = Read( p_access, p_buffer, i_len );
747
            p_sys->b_continuous = true;
zorglub's avatar
zorglub committed
748
        }
749
        Disconnect( p_access );
750 751 752 753 754 755 756 757 758
        if( p_sys->b_reconnect )
        {
            msg_Dbg( p_access, "got disconnected, trying to reconnect" );
            if( Connect( p_access, p_access->info.i_pos ) )
            {
                msg_Dbg( p_access, "reconnection failed" );
            }
            else
            {
759
                p_sys->b_reconnect = false;
760
                i_read = Read( p_access, p_buffer, i_len );
761
                p_sys->b_reconnect = true;
762