http.c 49.8 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
    /* cookie jar borrowed from playlist, do not free */
    vlc_http_cookie_jar_t * cookies;

184 185 186 187
    bool b_seekable;
    bool b_reconnect;
    bool b_continuous;
    bool b_pace_control;
188
    bool b_persist;
189
    bool b_has_size;
190
};
191

192
/* */
193 194
static int OpenRedirected( vlc_object_t *p_this, const char *psz_access,
                           unsigned i_redirect );
195

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

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

207 208 209

static void AuthReply( access_t *p_acces, const char *psz_prefix,
                       vlc_url_t *p_url, http_auth_t *p_auth );
210 211
static int AuthCheckReply( access_t *p_access, const char *psz_header,
                           vlc_url_t *p_url, http_auth_t *p_auth );
212
static vlc_http_cookie_jar_t *GetCookieJar( vlc_object_t *p_this );
213

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

223
/**
224
 * Open the given url with limited redirects
225 226 227
 * @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)
228
 * @i_redirect: number of redirections remaining
229 230
 * @return vlc error codes
 */
231 232
static int OpenRedirected( vlc_object_t *p_this, const char *psz_access,
                           unsigned i_redirect )
233
{
234 235
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;
236
    char         *psz, *p;
237

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

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

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

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

296
    if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
297
    {
298
        msg_Warn( p_access, "invalid host" );
299
        goto error;
300
    }
301
    if( !strncmp( psz_access, "https", 5 ) )
302
    {
303
        /* HTTP over SSL */
304 305 306
        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
307 308 309 310 311 312 313
        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;
314 315
    }

316
    /* Determine the HTTP user agent */
317 318
    /* See RFC2616 §2.2 token and comment definition, and §3.8 and
     * §14.43 user-agent header */
319
    p_sys->psz_user_agent = var_InheritString( p_access, "http-user-agent" );
320
    if (p_sys->psz_user_agent)
321
    {
322
        unsigned comment_level = 0;
323 324 325
        for( char *p = p_sys->psz_user_agent; *p; p++ )
        {
            uint8_t c = *p;
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
            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';
350
        }
351
    }
352

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

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

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

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

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

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

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

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

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

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

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

        case 0:
            break;

        default:
432
            assert(0);
433
    }
434

435 436
    if( p_sys->i_code == 401 )
    {
437 438 439 440 441
        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
442
        char *psz_login, *psz_password;
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 )
        {
447
            Disconnect( p_access );
448 449 450
            goto connect;
        }
        msg_Dbg( p_access, "authentication failed for realm %s",
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
451 452 453 454 455 456
                 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 )
457
        {
458
            msg_Dbg( p_access, "retrying with user=%s", psz_login );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
459 460
            p_sys->url.psz_username = psz_login;
            p_sys->url.psz_password = psz_password;
461
            Disconnect( p_access );
462 463 464 465
            goto connect;
        }
        else
        {
466 467
            free( psz_login );
            free( psz_password );
468 469 470 471
            goto error;
        }
    }

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

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

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

511
        Disconnect( p_access );
512
        vlc_tls_Delete( p_sys->p_creds );
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
        return OpenRedirected( p_this, psz_protocol, i_redirect - 1 );
520 521
    }

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

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

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

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

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

583
    return VLC_SUCCESS;
584

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

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

#ifdef HAVE_ZLIB_H
    inflateEnd( &p_sys->inflate.stream );
#endif
601 602 603
    free( p_sys );
    return VLC_EGENERIC;
}
604

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

613
    vlc_UrlClean( &p_sys->url );
614
    http_auth_Reset( &p_sys->auth );
615
    vlc_UrlClean( &p_sys->proxy );
616
    http_auth_Reset( &p_sys->proxy_auth );
617

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
618 619 620
    free( p_sys->psz_mime );
    free( p_sys->psz_pragma );
    free( p_sys->psz_location );
621

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
622 623 624
    free( p_sys->psz_icy_name );
    free( p_sys->psz_icy_genre );
    free( p_sys->psz_icy_title );
625

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
626
    free( p_sys->psz_user_agent );
627
    free( p_sys->psz_referrer );
628

629
    Disconnect( p_access );
630
    vlc_tls_Delete( p_sys->p_creds );
631

632 633 634 635 636
#ifdef HAVE_ZLIB_H
    inflateEnd( &p_sys->inflate.stream );
    free( p_sys->inflate.p_buffer );
#endif

637 638 639
    free( p_sys );
}

640 641 642
/* 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 )
643
{
644
    access_sys_t *p_sys = p_access->p_sys;
645 646 647
    if( p_sys->b_chunked )
    {
        if( p_sys->i_chunk < 0 )
648
            return VLC_EGENERIC;
649 650 651

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

            if( p_sys->i_chunk <= 0 )   /* eof */
            {
                p_sys->i_chunk = -1;
666
                return VLC_EGENERIC;
667 668 669 670 671
            }
        }

        if( i_len > p_sys->i_chunk )
            i_len = p_sys->i_chunk;
672
    }
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
    *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;
}
689

690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
/*****************************************************************************
 * 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 */
706
        uint64_t remainder = p_sys->size - p_access->info.i_pos;
707 708 709 710 711 712 713
        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;
    }
714
    if( i_len == 0 )
715
        goto fatal;
716

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
717
    if( p_sys->i_icy_meta > 0 && p_access->info.i_pos - p_sys->i_icy_offset > 0 )
718
    {
719
        int64_t i_next = p_sys->i_icy_meta -
720
                                    (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
721

722 723 724
        if( i_next == p_sys->i_icy_meta )
        {
            if( ReadICYMeta( p_access ) )
725
                goto fatal;
726 727 728 729
        }
        if( i_len > i_next )
            i_len = i_next;
    }
730

731 732
    if( ReadData( p_access, &i_read, p_buffer, i_len ) )
        goto fatal;
733

734
    if( i_read <= 0 )
735
    {
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
         */
742 743 744
        if( p_sys->b_continuous )
        {
            Request( p_access, 0 );
745
            p_sys->b_continuous = false;
746
            i_read = Read( p_access, p_buffer, i_len );
747
            p_sys->b_continuous = true;
748
        }
749
        Disconnect( p_access );
750
        if( p_sys->b_reconnect && vlc_object_alive( p_access ) )
751 752 753 754 755 756 757 758
        {
            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 763

                return i_read;
764 765 766
            }
        }

767 768 769 770
        if( i_read <= 0 )
        {
            if( i_read < 0 )
                p_sys->b_error = true;
771
            goto fatal;
772
        }
773
    }
774

775
    assert( i_read >= 0 );
776
    p_access->info.i_pos += i_read;
777
    if( p_sys->b_has_size )
778
    {
779
        assert( p_access->info.i_pos <= p_sys->size );
780
        assert( (unsigned)i_read <= p_sys->i_remaining );
781 782 783
        p_sys->i_remaining -= i_read;
    }

784
    return i_read;
785 786 787 788

fatal:
    p_access->info.b_eof = true;
    return 0;
789 790
}

791 792 793 794
static int ReadICYMeta( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;