http.c 49.4 KB
Newer Older
1
/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
2
 * http.c: HTTP input module
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2001-2008 VLC authors and VideoLAN
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
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
12 13 14
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
15
 * (at your option) any later version.
16
 *
17 18
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
21
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
22 23 24
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software 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


Clément Stenac's avatar
Clément Stenac committed
38
#include <vlc_access.h>
39

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
40
#include <vlc_dialog.h>
Clément Stenac's avatar
Clément Stenac committed
41 42 43 44
#include <vlc_meta.h>
#include <vlc_network.h>
#include <vlc_url.h>
#include <vlc_tls.h>
45
#include <vlc_strings.h>
46
#include <vlc_charset.h>
47
#include <vlc_input.h>
48
#include <vlc_md5.h>
49
#include <vlc_http.h>
50

51 52 53 54
#ifdef HAVE_ZLIB_H
#   include <zlib.h>
#endif

55
#include <assert.h>
56
#include <limits.h>
57

58 59 60
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Clément Stenac's avatar
Clément Stenac committed
61
static int  Open ( vlc_object_t * );
62 63
static void Close( vlc_object_t * );

Gildas Bazin's avatar
 
Gildas Bazin committed
64
#define PROXY_TEXT N_("HTTP proxy")
Gildas Bazin's avatar
 
Gildas Bazin 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." )

74
#define RECONNECT_TEXT N_("Auto re-connect")
Clément Stenac's avatar
Clément Stenac committed
75 76 77
#define RECONNECT_LONGTEXT N_( \
    "Automatically try to reconnect to the stream in case of a sudden " \
    "disconnect." )
78

79
#define CONTINUOUS_TEXT N_("Continuous stream")
Clément Stenac's avatar
Clément Stenac committed
80
#define CONTINUOUS_LONGTEXT N_("Read a file that is " \
Christophe Mutricy's avatar
Christophe Mutricy committed
81
    "being constantly updated (for example, a JPG file on a server). " \
82 83
    "You should not globally enable this option as it will break all other " \
    "types of HTTP streams." )
84

85
#define FORWARD_COOKIES_TEXT N_("Forward Cookies")
Rémi Duraffort's avatar
Rémi Duraffort committed
86
#define FORWARD_COOKIES_LONGTEXT N_("Forward Cookies across http redirections.")
87

88 89 90 91
#define REFERER_TEXT N_("HTTP referer value")
#define REFERER_LONGTEXT N_("Customize the HTTP referer, simulating a previous document")

#define UA_TEXT N_("User Agent")
92 93 94 95
#define UA_LONGTEXT N_("The name and version of the program will be " \
    "provided to the HTTP server. They must be separated by a forward " \
    "slash, e.g. FooBar/1.2.3. This option can only be specified per input " \
    "item, not globally.")
96

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

104
    add_string( "http-proxy", NULL, PROXY_TEXT, PROXY_LONGTEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
105
                false )
106
    add_password( "http-proxy-pwd", NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
107
                  PROXY_PASS_TEXT, PROXY_PASS_LONGTEXT, false )
108
    add_obsolete_bool( "http-use-IE-proxy" )
109
    add_string( "http-referrer", NULL, REFERER_TEXT, REFERER_LONGTEXT, false )
110
        change_safe()
111
    add_string( "http-user-agent", NULL, UA_TEXT, UA_LONGTEXT, false )
112
        change_safe()
113
        change_private()
114
    add_bool( "http-reconnect", false, RECONNECT_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
115
              RECONNECT_LONGTEXT, true )
116
    add_bool( "http-continuous", false, CONTINUOUS_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
117
              CONTINUOUS_LONGTEXT, true )
118
        change_safe()
119
    add_bool( "http-forward-cookies", true, FORWARD_COOKIES_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
120
              FORWARD_COOKIES_LONGTEXT, true )
121 122
    /* 'itpc' = iTunes Podcast */
    add_shortcut( "http", "https", "unsv", "itpc", "icyx" )
123 124
    set_callbacks( Open, Close )
vlc_module_end ()
125 126

/*****************************************************************************
127
 * Local prototypes
128
 *****************************************************************************/
129

130
struct access_sys_t
131
{
132
    int fd;
133
    bool b_error;
134 135
    vlc_tls_creds_t *p_creds;
    vlc_tls_t *p_tls;
136
    v_socket_t *p_vs;
137

138 139 140
    /* From uri */
    vlc_url_t url;
    char    *psz_user_agent;
141
    char    *psz_referrer;
142
    http_auth_t auth;
143

144
    /* Proxy */
145
    bool b_proxy;
146
    vlc_url_t  proxy;
147
    http_auth_t proxy_auth;
148
    char       *psz_proxy_passbuf;
149

150 151
    /* */
    int        i_code;
Laurent Aimar's avatar
Laurent Aimar committed
152
    const char *psz_protocol;
153
    int        i_version;
154

155
    char       *psz_mime;
156
    char       *psz_pragma;
157
    char       *psz_location;
158 159
    bool b_mms;
    bool b_icecast;
160
#ifdef HAVE_ZLIB_H
161
    bool b_compressed;
162 163 164 165 166 167
    struct
    {
        z_stream   stream;
        uint8_t   *p_buffer;
    } inflate;
#endif
168

169
    bool b_chunked;
170
    int64_t    i_chunk;
171

172
    int        i_icy_meta;
173
    uint64_t   i_icy_offset;
174 175 176 177
    char       *psz_icy_name;
    char       *psz_icy_genre;
    char       *psz_icy_title;

178
    uint64_t i_remaining;
179
    uint64_t size;
180

181 182 183 184
    bool b_seekable;
    bool b_reconnect;
    bool b_continuous;
    bool b_pace_control;
185
    bool b_persist;
186
    bool b_has_size;
187

188
    vlc_http_cookie_jar_t * cookies;
189
};
190

191
/* */
192
static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
193
                            unsigned i_redirect, vlc_http_cookie_jar_t *cookies );
194

195
/* */
196
static ssize_t Read( access_t *, uint8_t *, size_t );
197
static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
198
static int Seek( access_t *, uint64_t );
199
static int Control( access_t *, int, va_list );
200

201
/* */
202 203
static int Connect( access_t *, uint64_t );
static int Request( access_t *p_access, uint64_t i_tell );
204
static void Disconnect( access_t * );
205

206 207 208

static void AuthReply( access_t *p_acces, const char *psz_prefix,
                       vlc_url_t *p_url, http_auth_t *p_auth );
209 210
static int AuthCheckReply( access_t *p_access, const char *psz_header,
                           vlc_url_t *p_url, http_auth_t *p_auth );
211

212 213 214
/*****************************************************************************
 * Open:
 *****************************************************************************/
215
static int Open( vlc_object_t *p_this )
216
{
217
    access_t *p_access = (access_t*)p_this;
218
    return OpenWithCookies( p_this, p_access->psz_access, 5, NULL );
219 220
}

221 222 223 224 225
/**
 * Open the given url using the given cookies
 * @param p_this: the vlc object
 * @psz_access: the acces to use (http, https, ...) (this value must be used
 *              instead of p_access->psz_access)
226
 * @i_redirect: number of redirections remaining
227 228 229 230
 * @cookies: the available cookies
 * @return vlc error codes
 */
static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
231
                            unsigned i_redirect, vlc_http_cookie_jar_t *cookies )
232
{
233 234
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;
235
    char         *psz, *p;
236

237
    /* Set up p_access */
238
    STANDARD_READ_ACCESS_INIT;
239 240 241
#ifdef HAVE_ZLIB_H
    p_access->pf_read = ReadCompressed;
#endif
242
    p_sys->fd = -1;
243
    p_sys->b_proxy = false;
244
    p_sys->psz_proxy_passbuf = NULL;
245
    p_sys->i_version = 1;
246
    p_sys->b_seekable = true;
247
    p_sys->psz_mime = NULL;
248
    p_sys->psz_pragma = NULL;
249 250
    p_sys->b_mms = false;
    p_sys->b_icecast = false;
251 252
    p_sys->psz_location = NULL;
    p_sys->psz_user_agent = NULL;
253
    p_sys->psz_referrer = NULL;
254
    p_sys->b_pace_control = true;
255
#ifdef HAVE_ZLIB_H
256
    p_sys->b_compressed = false;
257 258 259 260 261 262 263 264
    /* 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
265 266
    p_sys->p_tls = NULL;
    p_sys->p_vs = NULL;
267
    p_sys->i_icy_meta = 0;
268
    p_sys->i_icy_offset = 0;
269 270 271
    p_sys->psz_icy_name = NULL;
    p_sys->psz_icy_genre = NULL;
    p_sys->psz_icy_title = NULL;
272
    p_sys->i_remaining = 0;
273
    p_sys->b_persist = false;
274
    p_sys->b_has_size = false;
275
    p_sys->size = 0;
276 277
    p_access->info.i_pos  = 0;
    p_access->info.b_eof  = false;
278

279 280
    /* Only forward an store cookies if the corresponding option is activated */
    if( var_CreateGetBool( p_access, "http-forward-cookies" ) )
281
        p_sys->cookies = (cookies != NULL) ? cookies : vlc_http_cookies_new();
282 283
    else
        p_sys->cookies = NULL;
284

285 286 287
    http_auth_Init( &p_sys->auth );
    http_auth_Init( &p_sys->proxy_auth );

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

295
    if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
296
    {
297
        msg_Warn( p_access, "invalid host" );
298
        goto error;
299
    }
300
    if( !strncmp( psz_access, "https", 5 ) )
301
    {
302
        /* HTTP over SSL */
303 304 305
        p_sys->p_creds = vlc_tls_ClientCreate( p_this );
        if( p_sys->p_creds == NULL )
            goto error;
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
    /* Determine the HTTP user agent */
316 317
    /* See RFC2616 §2.2 token and comment definition, and §3.8 and
     * §14.43 user-agent header */
318
    p_sys->psz_user_agent = var_InheritString( p_access, "http-user-agent" );
319
    if (p_sys->psz_user_agent)
320
    {
321
        unsigned comment_level = 0;
322 323 324
        for( char *p = p_sys->psz_user_agent; *p; p++ )
        {
            uint8_t c = *p;
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
            if (comment_level == 0)
            {
                if( c < 32 || strchr( ")<>@,;:\\\"[]?={}", c ) )
                    *p = '_'; /* remove potentially harmful characters */
            }
            else
            {
                if (c == ')')
                    comment_level--;
                else if( c < 32 && strchr( "\t\r\n", c ) == NULL)
                    *p = '_'; /* remove potentially harmful characters */
            }
            if (c == '(')
            {
                if (comment_level == UINT_MAX)
                    break;
                comment_level++;
            }
        }
        /* truncate evil unclosed comments */
        if (comment_level > 0)
        {
            char *p = strchr(p_sys->psz_user_agent, '(');
            *p = '\0';
349
        }
350
    }
351

352 353 354
    /* HTTP referrer */
    p_sys->psz_referrer = var_InheritString( p_access, "http-referrer" );

355
    /* Check proxy */
356
    psz = var_InheritString( p_access, "http-proxy" );
357
    if( psz == NULL )
358
    {
359 360 361 362
        char *url;

        if (likely(asprintf(&url, "%s://%s", psz_access,
                            p_access->psz_location) != -1))
363
        {
364 365 366
            msg_Dbg(p_access, "querying proxy for %s", url);
            psz = vlc_getProxyUrl(url);
            free(url);
367
        }
368

369 370 371 372
        if (psz != NULL)
            msg_Dbg(p_access, "proxy: %s", psz);
        else
            msg_Dbg(p_access, "no proxy");
373
    }
374
    if( psz != NULL )
375
    {
376 377 378
        p_sys->b_proxy = true;
        vlc_UrlParse( &p_sys->proxy, psz, 0 );
        free( psz );
379

380
        psz = var_InheritString( p_access, "http-proxy-pwd" );
381 382
        if( psz )
            p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
383

384 385
        if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
        {
386
            msg_Warn( p_access, "invalid proxy host" );
387 388 389
            goto error;
        }
        if( p_sys->proxy.i_port <= 0 )
390
        {
391
            p_sys->proxy.i_port = 80;
392 393
        }
    }
394

395
    msg_Dbg( p_access, "http: server='%s' port=%d file='%s'",
396 397
             p_sys->url.psz_host, p_sys->url.i_port,
             p_sys->url.psz_path != NULL ? p_sys->url.psz_path : "" );
398
    if( p_sys->b_proxy )
399
    {
400
        msg_Dbg( p_access, "      proxy %s:%d", p_sys->proxy.psz_host,
Gildas Bazin's avatar
 
Gildas Bazin committed
401
                 p_sys->proxy.i_port );
402
    }
403
    if( p_sys->url.psz_username && *p_sys->url.psz_username )
404
    {
405
        msg_Dbg( p_access, "      user='%s'", p_sys->url.psz_username );
406 407
    }

408 409
    p_sys->b_reconnect = var_InheritBool( p_access, "http-reconnect" );
    p_sys->b_continuous = var_InheritBool( p_access, "http-continuous" );
410

411
connect:
412
    /* Connect */
413
    switch( Connect( p_access, 0 ) )
414
    {
415
        case -1:
416
            goto error;
417 418 419 420 421

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

424
            if( !vlc_object_alive (p_access) || Connect( p_access, 0 ) )
425 426 427 428 429 430
                goto error;

        case 0:
            break;

        default:
431
            assert(0);
432
    }
433

434 435
    if( p_sys->i_code == 401 )
    {
436 437 438 439 440
        if( p_sys->auth.psz_realm == NULL )
        {
            msg_Err( p_access, "authentication failed without realm" );
            goto error;
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
441
        char *psz_login, *psz_password;
442 443 444 445
        /* FIXME ? */
        if( p_sys->url.psz_username && p_sys->url.psz_password &&
            p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
        {
446
            Disconnect( p_access );
447 448 449
            goto connect;
        }
        msg_Dbg( p_access, "authentication failed for realm %s",
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
450 451 452 453 454 455
                 p_sys->auth.psz_realm );
        dialog_Login( p_access, &psz_login, &psz_password,
                      _("HTTP authentication"),
             _("Please enter a valid login name and a password for realm %s."),
                      p_sys->auth.psz_realm );
        if( psz_login != NULL && psz_password != NULL )
456
        {
457
            msg_Dbg( p_access, "retrying with user=%s", psz_login );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
458 459
            p_sys->url.psz_username = psz_login;
            p_sys->url.psz_password = psz_password;
460
            Disconnect( p_access );
461 462 463 464
            goto connect;
        }
        else
        {
465 466
            free( psz_login );
            free( psz_password );
467 468 469 470
            goto error;
        }
    }

Gildas Bazin's avatar
Gildas Bazin committed
471 472
    if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
          p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
473
        p_sys->psz_location && *p_sys->psz_location )
474
    {
475
        msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
476

477
        /* Check the number of redirection already done */
478
        if( i_redirect == 0 )
479 480 481 482 483 484
        {
            msg_Err( p_access, "Too many redirection: break potential infinite"
                     "loop" );
            goto error;
        }

485
        const char *psz_protocol;
486
        if( !strncmp( p_sys->psz_location, "http://", 7 ) )
487
            psz_protocol = "http";
488
        else if( !strncmp( p_sys->psz_location, "https://", 8 ) )
489 490
            psz_protocol = "https";
        else
491 492
        {   /* Do not accept redirection outside of HTTP */
            msg_Err( p_access, "unsupported redirection ignored" );
493 494
            goto error;
        }
495
        free( p_access->psz_location );
496 497
        p_access->psz_location = strdup( p_sys->psz_location
                                       + strlen( psz_protocol ) + 3 );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
498 499
        /* Clean up current Open() run */
        vlc_UrlClean( &p_sys->url );
500
        http_auth_Reset( &p_sys->auth );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
501
        vlc_UrlClean( &p_sys->proxy );
502
        free( p_sys->psz_proxy_passbuf );
503
        http_auth_Reset( &p_sys->proxy_auth );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
504 505 506 507
        free( p_sys->psz_mime );
        free( p_sys->psz_pragma );
        free( p_sys->psz_location );
        free( p_sys->psz_user_agent );
508
        free( p_sys->psz_referrer );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
509

510
        Disconnect( p_access );
511
        vlc_tls_Delete( p_sys->p_creds );
512
        cookies = p_sys->cookies;
513 514 515
#ifdef HAVE_ZLIB_H
        inflateEnd( &p_sys->inflate.stream );
#endif
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
516 517 518
        free( p_sys );

        /* Do new Open() run with new data */
519 520
        return OpenWithCookies( p_this, psz_protocol, i_redirect - 1,
                                cookies );
521 522
    }

523
    if( p_sys->b_mms )
524
    {
525
        msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
526 527 528
        goto error;
    }

529
    if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
530
    {
531
        if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
532
        {
533
            if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
534
                !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
Rafaël Carré's avatar
Rafaël Carré committed
535 536
            {
                free( p_access->psz_demux );
537
                p_access->psz_demux = strdup( "nsv" );
Rafaël Carré's avatar
Rafaël Carré committed
538
            }
539 540
            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
541 542
            {
                free( p_access->psz_demux );
543
                p_access->psz_demux = strdup( "m4a" );
Rafaël Carré's avatar
Rafaël Carré committed
544
            }
545
            else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
Rafaël Carré's avatar
Rafaël Carré committed
546 547
            {
                free( p_access->psz_demux );
548
                p_access->psz_demux = strdup( "mp3" );
Rafaël Carré's avatar
Rafaël Carré committed
549
            }
550 551 552 553

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

554 555 556
#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). */
557
            p_sys->b_pace_control = false;
558
#endif
559 560 561
        }
        else if( !p_sys->psz_mime )
        {
Rafaël Carré's avatar
Rafaël Carré committed
562 563 564
            free( p_access->psz_demux );
            /* Shoutcast */
            p_access->psz_demux = strdup( "mp3" );
565 566
        }
        /* else probably Ogg Vorbis */
567
    }
568
    else if( !strcasecmp( psz_access, "unsv" ) &&
569 570 571
             p_sys->psz_mime &&
             !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
    {
Rafaël Carré's avatar
Rafaël Carré committed
572
        free( p_access->psz_demux );
573 574 575
        /* Grrrr! detect ultravox server and force NSV demuxer */
        p_access->psz_demux = strdup( "nsv" );
    }
576
    else if( !strcmp( psz_access, "itpc" ) )
577
    {
Rafaël Carré's avatar
Rafaël Carré committed
578
        free( p_access->psz_demux );
579 580
        p_access->psz_demux = strdup( "podcast" );
    }
581

582 583
    if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );

584
    return VLC_SUCCESS;
585

586 587 588
error:
    vlc_UrlClean( &p_sys->url );
    vlc_UrlClean( &p_sys->proxy );
589
    free( p_sys->psz_proxy_passbuf );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
590 591 592 593
    free( p_sys->psz_mime );
    free( p_sys->psz_pragma );
    free( p_sys->psz_location );
    free( p_sys->psz_user_agent );
594
    free( p_sys->psz_referrer );
595

596
    Disconnect( p_access );
597
    vlc_tls_Delete( p_sys->p_creds );
598

599
    vlc_http_cookies_destroy( p_sys->cookies );
600 601 602 603

#ifdef HAVE_ZLIB_H
    inflateEnd( &p_sys->inflate.stream );
#endif
604 605 606
    free( p_sys );
    return VLC_EGENERIC;
}
607

608 609 610 611 612
/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
613 614
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys = p_access->p_sys;
615

616
    vlc_UrlClean( &p_sys->url );
617
    http_auth_Reset( &p_sys->auth );
618
    vlc_UrlClean( &p_sys->proxy );
619
    http_auth_Reset( &p_sys->proxy_auth );
620

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
621 622 623
    free( p_sys->psz_mime );
    free( p_sys->psz_pragma );
    free( p_sys->psz_location );
624

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
625 626 627
    free( p_sys->psz_icy_name );
    free( p_sys->psz_icy_genre );
    free( p_sys->psz_icy_title );
628

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
629
    free( p_sys->psz_user_agent );
630
    free( p_sys->psz_referrer );
631

632
    Disconnect( p_access );
633
    vlc_tls_Delete( p_sys->p_creds );
634

635
    vlc_http_cookies_destroy( p_sys->cookies );
636

637 638 639 640 641
#ifdef HAVE_ZLIB_H
    inflateEnd( &p_sys->inflate.stream );
    free( p_sys->inflate.p_buffer );
#endif

642 643 644
    free( p_sys );
}

645 646 647
/* Read data from the socket taking care of chunked transfer if needed */
static int ReadData( access_t *p_access, int *pi_read,
                     uint8_t *p_buffer, size_t i_len )
648
{
649
    access_sys_t *p_sys = p_access->p_sys;
650 651 652
    if( p_sys->b_chunked )
    {
        if( p_sys->i_chunk < 0 )
653
            return VLC_EGENERIC;
654 655 656

        if( p_sys->i_chunk <= 0 )
        {
Rémi Duraffort's avatar
Rémi Duraffort committed
657
            char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
658 659 660
            /* read the chunk header */
            if( psz == NULL )
            {
661
                /* fatal error - end of file */
662
                msg_Dbg( p_access, "failed reading chunk-header line" );
663
                return VLC_EGENERIC;
664 665 666 667 668 669 670
            }
            p_sys->i_chunk = strtoll( psz, NULL, 16 );
            free( psz );

            if( p_sys->i_chunk <= 0 )   /* eof */
            {
                p_sys->i_chunk = -1;
671
                return VLC_EGENERIC;
672 673 674 675 676
            }
        }

        if( i_len > p_sys->i_chunk )
            i_len = p_sys->i_chunk;
677
    }
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
    *pi_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
    if( *pi_read <= 0 )
        return VLC_SUCCESS;

    if( p_sys->b_chunked )
    {
        p_sys->i_chunk -= *pi_read;
        if( p_sys->i_chunk <= 0 )
        {
            /* read the empty line */
            char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
            free( psz );
        }
    }
    return VLC_SUCCESS;
}
694

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
/*****************************************************************************
 * Read: Read up to i_len bytes from the http connection and place in
 * p_buffer. Return the actual number of bytes read
 *****************************************************************************/
static int ReadICYMeta( access_t *p_access );
static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
{
    access_sys_t *p_sys = p_access->p_sys;
    int i_read;

    if( p_sys->fd == -1 )
        goto fatal;

    if( p_sys->b_has_size )
    {
        /* Remaining bytes in the file */
711
        uint64_t remainder = p_sys->size - p_access->info.i_pos;
712 713 714 715 716 717 718
        if( remainder < i_len )
            i_len = remainder;

        /* Remaining bytes in the response */
        if( p_sys->i_remaining < i_len )
            i_len = p_sys->i_remaining;
    }
719
    if( i_len == 0 )
720
        goto fatal;
721

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
722
    if( p_sys->i_icy_meta > 0 && p_access->info.i_pos - p_sys->i_icy_offset > 0 )
723
    {
724
        int64_t i_next = p_sys->i_icy_meta -
725
                                    (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
726

727 728 729
        if( i_next == p_sys->i_icy_meta )
        {
            if( ReadICYMeta( p_access ) )
730
                goto fatal;
731 732 733 734
        }
        if( i_len > i_next )
            i_len = i_next;
    }
735

736 737
    if( ReadData( p_access, &i_read, p_buffer, i_len ) )
        goto fatal;
738

739
    if( i_read <= 0 )
740
    {
741 742 743 744 745 746
        /*
         * 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
         */
747 748 749
        if( p_sys->b_continuous )
        {
            Request( p_access, 0 );
750
            p_sys->b_continuous = false;
751
            i_read = Read( p_access, p_buffer, i_len );
752
            p_sys->b_continuous = true;
753
        }
754
        Disconnect( p_access );
755
        if( p_sys->b_reconnect && vlc_object_alive( p_access ) )
756 757 758 759 760 761 762 763
        {
            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
            {
764
                p_sys->b_reconnect = false;
765
                i_read = Read( p_access, p_buffer, i_len );
766
                p_sys->b_reconnect = true;
767 768

                return i_read;
769 770 771
            }
        }

772 773 774 775
        if( i_read <= 0 )
        {
            if( i_read < 0 )
                p_sys->b_error = true;
776
            goto fatal;
777
        }
778
    }
779

780
    assert( i_read >= 0 );
781
    p_access->info.i_pos += i_read;
782
    if( p_sys->b_has_size )
783
    {
784
        assert( p_access->info.i_pos <= p_sys->size );
785
        assert( (unsigned)i_read <= p_sys->i_remaining );
786 787 788
        p_sys->i_remaining -= i_read;
    }

789
    return i_read;