http.c 31.1 KB
Newer Older
1
/*****************************************************************************
2
 * http.c: HTTP input module
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
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
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
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
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 35
#include <errno.h>

36
#include <vlc_common.h>
37
#include <vlc_plugin.h>
38 39 40 41
#include <vlc_access.h>
#include <vlc_meta.h>
#include <vlc_network.h>
#include <vlc_url.h>
42
#include <vlc_strings.h>
43
#include <vlc_charset.h>
44
#include <vlc_input.h>
45
#include <vlc_http.h>
46
#include <vlc_interrupt.h>
47
#include <vlc_keystore.h>
48
#include <vlc_memstream.h>
49

50
#include <assert.h>
51
#include <limits.h>
52

53 54 55
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Clément Stenac's avatar
Clément Stenac committed
56
static int  Open ( vlc_object_t * );
57 58
static void Close( vlc_object_t * );

59
#define RECONNECT_TEXT N_("Auto re-connect")
60 61 62
#define RECONNECT_LONGTEXT N_( \
    "Automatically try to reconnect to the stream in case of a sudden " \
    "disconnect." )
63

64 65 66 67 68 69
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 )
70

71
    add_bool( "http-reconnect", false, RECONNECT_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
72
              RECONNECT_LONGTEXT, true )
73
    /* 'itpc' = iTunes Podcast */
74
    add_shortcut( "http", "unsv", "itpc", "icyx" )
75 76
    set_callbacks( Open, Close )
vlc_module_end ()
77 78

/*****************************************************************************
79
 * Local prototypes
80
 *****************************************************************************/
81

82
struct access_sys_t
83
{
84
    int fd;
85

86 87 88
    /* From uri */
    vlc_url_t url;
    char    *psz_user_agent;
89
    char    *psz_referrer;
90 91
    char    *psz_username;
    char    *psz_password;
92
    vlc_http_auth_t auth;
93

94
    /* Proxy */
95
    bool b_proxy;
96
    vlc_url_t  proxy;
97
    vlc_http_auth_t proxy_auth;
98
    char       *psz_proxy_passbuf;
99

100 101
    /* */
    int        i_code;
102

103 104
    char       *psz_mime;
    char       *psz_location;
105
    bool b_icecast;
106

107
    int        i_icy_meta;
108
    uint64_t   i_icy_offset;
109 110 111 112
    char       *psz_icy_name;
    char       *psz_icy_genre;
    char       *psz_icy_title;

113
    uint64_t offset;
114
    uint64_t size;
115

116
    bool b_reconnect;
117
    bool b_has_size;
118
};
119

120
/* */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
121 122 123
static ssize_t Read( stream_t *, void *, size_t );
static int Seek( stream_t *, uint64_t );
static int Control( stream_t *, int, va_list );
124

125
/* */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
126 127
static int Connect( stream_t * );
static void Disconnect( stream_t * );
128

129

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
130
static int AuthCheckReply( stream_t *p_access, const char *psz_header,
131
                           vlc_url_t *p_url, vlc_http_auth_t *p_auth );
132

133 134 135
/*****************************************************************************
 * Open:
 *****************************************************************************/
136
static int Open( vlc_object_t *p_this )
137
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
138
    stream_t *p_access = (stream_t*)p_this;
139 140
    const char *psz_url = p_access->psz_url;
    char *psz;
141
    int ret = VLC_EGENERIC;
142
    vlc_credential credential;
143

144
    access_sys_t *p_sys = vlc_obj_malloc( p_this, sizeof(*p_sys) );
145 146 147
    if( unlikely(p_sys == NULL) )
        return VLC_ENOMEM;

148
    p_sys->fd = -1;
149
    p_sys->b_proxy = false;
150
    p_sys->psz_proxy_passbuf = NULL;
151
    p_sys->psz_mime = NULL;
152
    p_sys->b_icecast = false;
153 154
    p_sys->psz_location = NULL;
    p_sys->psz_user_agent = NULL;
155
    p_sys->psz_referrer = NULL;
156 157
    p_sys->psz_username = NULL;
    p_sys->psz_password = NULL;
158
    p_sys->i_icy_meta = 0;
159
    p_sys->i_icy_offset = 0;
160 161 162
    p_sys->psz_icy_name = NULL;
    p_sys->psz_icy_genre = NULL;
    p_sys->psz_icy_title = NULL;
163
    p_sys->b_has_size = false;
164
    p_sys->offset = 0;
165
    p_sys->size = 0;
166
    p_access->p_sys = p_sys;
167

168
    if( vlc_UrlParse( &p_sys->url, psz_url ) || p_sys->url.psz_host == NULL )
169
    {
170 171 172
        msg_Err( p_access, "invalid URL" );
        vlc_UrlClean( &p_sys->url );
        return VLC_EGENERIC;
173
    }
174 175
    if( p_sys->url.i_port <= 0 )
        p_sys->url.i_port = 80;
176

177 178
    vlc_credential_init( &credential, &p_sys->url );

179
    /* Determine the HTTP user agent */
180 181
    /* See RFC2616 §2.2 token and comment definition, and §3.8 and
     * §14.43 user-agent header */
182
    p_sys->psz_user_agent = var_InheritString( p_access, "http-user-agent" );
183
    if (p_sys->psz_user_agent)
184
    {
185
        unsigned comment_level = 0;
186 187 188
        for( char *p = p_sys->psz_user_agent; *p; p++ )
        {
            uint8_t c = *p;
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
            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';
213
        }
214
    }
215

216 217 218
    /* HTTP referrer */
    p_sys->psz_referrer = var_InheritString( p_access, "http-referrer" );

219
    /* Check proxy */
220
    psz = var_InheritString( p_access, "http-proxy" );
221
    if( psz == NULL )
222
    {
223 224
        msg_Dbg(p_access, "querying proxy for %s", psz_url);
        psz = vlc_getProxyUrl(psz_url);
225

226 227 228 229
        if (psz != NULL)
            msg_Dbg(p_access, "proxy: %s", psz);
        else
            msg_Dbg(p_access, "no proxy");
230
    }
231
    if( psz != NULL )
232
    {
233
        p_sys->b_proxy = true;
234
        vlc_UrlParse( &p_sys->proxy, psz );
235
        free( psz );
236

237
        psz = var_InheritString( p_access, "http-proxy-pwd" );
238 239
        if( psz )
            p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
240

241 242
        if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
        {
243
            msg_Warn( p_access, "invalid proxy host" );
244 245 246
            goto error;
        }
        if( p_sys->proxy.i_port <= 0 )
247
        {
248
            p_sys->proxy.i_port = 80;
249 250
        }
    }
251

252
    msg_Dbg( p_access, "http: server='%s' port=%d file='%s'",
253 254
             p_sys->url.psz_host, p_sys->url.i_port,
             p_sys->url.psz_path != NULL ? p_sys->url.psz_path : "" );
255
    if( p_sys->b_proxy )
256
    {
257
        msg_Dbg( p_access, "      proxy %s:%d", p_sys->proxy.psz_host,
258
                 p_sys->proxy.i_port );
259
    }
260
    if( p_sys->url.psz_username && *p_sys->url.psz_username )
261
    {
262
        msg_Dbg( p_access, "      user='%s'", p_sys->url.psz_username );
263 264
    }

265
    p_sys->b_reconnect = var_InheritBool( p_access, "http-reconnect" );
266

267 268 269 270 271 272
    if( vlc_credential_get( &credential, p_access, NULL, NULL, NULL, NULL ) )
    {
        p_sys->url.psz_username = (char *) credential.psz_username;
        p_sys->url.psz_password = (char *) credential.psz_password;
    }

273
connect:
274
    /* Connect */
275
    if( Connect( p_access ) )
276
        goto disconnect;
277

278 279
    if( p_sys->i_code == 401 )
    {
280 281 282
        if( p_sys->auth.psz_realm == NULL )
        {
            msg_Err( p_access, "authentication failed without realm" );
283
            goto disconnect;
284
        }
285 286 287 288
        /* FIXME ? */
        if( p_sys->url.psz_username && p_sys->url.psz_password &&
            p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
        {
289
            Disconnect( p_access );
290 291
            goto connect;
        }
292 293 294 295
        free( p_sys->psz_username );
        free( p_sys->psz_password );
        p_sys->psz_username = p_sys->psz_password = NULL;

296
        msg_Dbg( p_access, "authentication failed for realm %s",
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
297
                 p_sys->auth.psz_realm );
298 299 300 301 302 303 304 305

        credential.psz_realm = p_sys->auth.psz_realm;
        credential.psz_authtype = p_sys->auth.psz_nonce  ? "Digest" : "Basic";

        if( vlc_credential_get( &credential, p_access, NULL, NULL,
                               _("HTTP authentication"),
                               _("Please enter a valid login name and a "
                               "password for realm %s."), p_sys->auth.psz_realm ) )
306
        {
307 308 309
            p_sys->psz_username = strdup(credential.psz_username);
            p_sys->psz_password = strdup(credential.psz_password);
            if (!p_sys->psz_username || !p_sys->psz_password)
310
                goto disconnect;
311
            msg_Err( p_access, "retrying with user=%s", p_sys->psz_username );
312 313
            p_sys->url.psz_username = p_sys->psz_username;
            p_sys->url.psz_password = p_sys->psz_password;
314
            Disconnect( p_access );
315 316 317
            goto connect;
        }
        else
318
            goto disconnect;
319
    }
320
    else
321
        vlc_credential_store( &credential, p_access );
322

323 324
    if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
          p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
325
        p_sys->psz_location != NULL )
326
    {
327
        p_access->psz_url = p_sys->psz_location;
328 329
        p_sys->psz_location = NULL;
        ret = VLC_ACCESS_REDIRECT;
330
        goto disconnect;
331 332
    }

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

335 336 337 338 339
    /* Set up p_access */
    p_access->pf_read = Read;
    p_access->pf_control = Control;
    p_access->pf_seek = Seek;

340 341
    vlc_credential_clean( &credential );

342
    return VLC_SUCCESS;
343

344 345 346
disconnect:
    Disconnect( p_access );

347
error:
348
    vlc_credential_clean( &credential );
349
    vlc_UrlClean( &p_sys->url );
350 351
    if( p_sys->b_proxy )
        vlc_UrlClean( &p_sys->proxy );
352
    free( p_sys->psz_proxy_passbuf );
353 354 355
    free( p_sys->psz_mime );
    free( p_sys->psz_location );
    free( p_sys->psz_user_agent );
356
    free( p_sys->psz_referrer );
357 358
    free( p_sys->psz_username );
    free( p_sys->psz_password );
359

360
    return ret;
361
}
362

363 364 365 366 367
/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
368
    stream_t     *p_access = (stream_t*)p_this;
369
    access_sys_t *p_sys = p_access->p_sys;
370

371
    vlc_UrlClean( &p_sys->url );
372 373
    if( p_sys->b_proxy )
        vlc_UrlClean( &p_sys->proxy );
374

375 376
    free( p_sys->psz_mime );
    free( p_sys->psz_location );
377

378 379 380
    free( p_sys->psz_icy_name );
    free( p_sys->psz_icy_genre );
    free( p_sys->psz_icy_title );
381

382
    free( p_sys->psz_user_agent );
383
    free( p_sys->psz_referrer );
384 385
    free( p_sys->psz_username );
    free( p_sys->psz_password );
386

387
    Disconnect( p_access );
388 389
}

390
/* Read data from the socket */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
391
static int ReadData( stream_t *p_access, int *pi_read,
392
                     void *p_buffer, size_t i_len )
393
{
394
    access_sys_t *p_sys = p_access->p_sys;
395

396
    *pi_read = vlc_recv_i11e( p_sys->fd, p_buffer, i_len, 0 );
397 398
    if( *pi_read < 0 && errno != EINTR && errno != EAGAIN )
        return VLC_EGENERIC;
399 400
    return VLC_SUCCESS;
}
401

402 403 404 405
/*****************************************************************************
 * Read: Read up to i_len bytes from the http connection and place in
 * p_buffer. Return the actual number of bytes read
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
406
static int ReadICYMeta( stream_t *p_access );
407

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
408
static ssize_t Read( stream_t *p_access, void *p_buffer, size_t i_len )
409 410
{
    access_sys_t *p_sys = p_access->p_sys;
411 412
    int i_total_read = 0;
    int i_remain_toread = i_len;
413 414

    if( p_sys->fd == -1 )
415
        return 0;
416

417
    while( i_remain_toread > 0 )
418
    {
419
        int i_chunk = i_remain_toread;
420

421
        if( p_sys->i_icy_meta > 0 )
422
        {
423 424 425 426 427
            if( UINT64_MAX - i_chunk < p_sys->offset )
                i_chunk = i_remain_toread = UINT64_MAX - p_sys->offset;

            if( p_sys->offset + i_chunk > p_sys->i_icy_offset )
                i_chunk = p_sys->i_icy_offset - p_sys->offset;
428
        }
429

430 431 432
        int i_read = 0;
        if( ReadData( p_access, &i_read, &((uint8_t*)p_buffer)[i_total_read], i_chunk ) )
            return 0;
433

434 435
        if( i_read < 0 )
            return -1; /* EINTR / EAGAIN */
436

437
        if( i_read == 0 )
438
        {
439 440 441 442 443 444 445 446 447 448
            Disconnect( p_access );
            if( p_sys->b_reconnect )
            {
                msg_Dbg( p_access, "got disconnected, trying to reconnect" );
                if( Connect( p_access ) )
                    msg_Dbg( p_access, "reconnection failed" );
                else
                    return -1;
            }
            return 0;
449 450
        }

451 452 453 454
        assert( i_read >= 0 );
        p_sys->offset += i_read;
        i_total_read += i_read;
        i_remain_toread -= i_read;
455

456 457 458 459 460 461 462 463 464 465
        if( p_sys->i_icy_meta > 0 &&
            p_sys->offset == p_sys->i_icy_offset )
        {
            if( ReadICYMeta( p_access ) )
                return 0;
            p_sys->i_icy_offset = p_sys->offset + p_sys->i_icy_meta;
        }
    }

    return i_total_read;
466 467
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
468
static int ReadICYMeta( stream_t *p_access )
469 470 471
{
    access_sys_t *p_sys = p_access->p_sys;

472 473
    uint8_t buffer;
    char *p, *psz_meta;
474 475 476
    int i_read;

    /* Read meta data length */
477
    if( ReadData( p_access, &i_read, &buffer, 1 ) )
478
        return VLC_EGENERIC;
479 480 481 482
    if( i_read != 1 )
        return VLC_EGENERIC;
    const int i_size = buffer << 4;
    /* msg_Dbg( p_access, "ICY meta size=%u", i_size); */
483

484 485
    psz_meta = malloc( i_size + 1 );
    for( i_read = 0; i_read < i_size; )
486
    {
487
        int i_tmp;
488
        if( ReadData( p_access, &i_tmp, (uint8_t *)&psz_meta[i_read], i_size - i_read ) || i_tmp <= 0 )
489 490 491 492 493
        {
            free( psz_meta );
            return VLC_EGENERIC;
        }
        i_read += i_tmp;
494
    }
495
    psz_meta[i_read] = '\0'; /* Just in case */
496

497
    /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
498 499 500

    /* Now parse the meta */
    /* Look for StreamTitle= */
501
    p = strcasestr( (char *)psz_meta, "StreamTitle=" );
502 503 504 505 506
    if( p )
    {
        p += strlen( "StreamTitle=" );
        if( *p == '\'' || *p == '"' )
        {
507 508
            char closing[] = { p[0], ';', '\0' };
            char *psz = strstr( &p[1], closing );
509 510 511 512
            if( !psz )
                psz = strchr( &p[1], ';' );

            if( psz ) *psz = '\0';
513
            p++;
514 515 516
        }
        else
        {
517
            char *psz = strchr( p, ';' );
518 519 520
            if( psz ) *psz = '\0';
        }

521
        if( !p_sys->psz_icy_title ||
522
            strcmp( p_sys->psz_icy_title, p ) )
523
        {
524
            free( p_sys->psz_icy_title );
525
            char *psz_tmp = strdup( p );
526 527 528
            p_sys->psz_icy_title = EnsureUTF8( psz_tmp );
            if( !p_sys->psz_icy_title )
                free( psz_tmp );
529

530
            msg_Dbg( p_access, "New Icy-Title=%s", p_sys->psz_icy_title );
531
            input_thread_t *p_input = p_access->p_input;
532 533 534 535 536 537
            if( p_input )
            {
                input_item_t *p_input_item = input_GetItem( p_access->p_input );
                if( p_input_item )
                    input_item_SetMeta( p_input_item, vlc_meta_NowPlaying, p_sys->psz_icy_title );
            }
538
        }
539 540 541 542 543 544
    }
    free( psz_meta );

    return VLC_SUCCESS;
}

545 546 547
/*****************************************************************************
 * Seek: close and re-open a connection at the right place
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
548
static int Seek( stream_t *p_access, uint64_t i_pos )
549
{
550 551
    (void) p_access; (void) i_pos;
    return VLC_EGENERIC;
552 553 554 555 556
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
557
static int Control( stream_t *p_access, int i_query, va_list args )
558 559
{
    access_sys_t *p_sys = p_access->p_sys;
560 561
    bool       *pb_bool;
    int64_t    *pi_64;
562 563 564 565

    switch( i_query )
    {
        /* */
566 567
        case STREAM_CAN_SEEK:
        case STREAM_CAN_FASTSEEK:
568
            pb_bool = va_arg( args, bool* );
569
            *pb_bool = false;
570
            break;
571 572
        case STREAM_CAN_PAUSE:
        case STREAM_CAN_CONTROL_PACE:
573
            pb_bool = va_arg( args, bool* );
574
            *pb_bool = true;
575 576 577
            break;

        /* */
578
        case STREAM_GET_PTS_DELAY:
579
            pi_64 = va_arg( args, int64_t * );
580 581
            *pi_64 = INT64_C(1000)
                * var_InheritInteger( p_access, "network-caching" );
582 583
            break;

584
        case STREAM_GET_SIZE:
585 586
            if( !p_sys->b_has_size )
                return VLC_EGENERIC;
587
            *va_arg( args, uint64_t*) = p_sys->size;
588 589
           break;

590
        /* */
591
        case STREAM_SET_PAUSE_STATE:
592 593
            break;

594
        case STREAM_GET_CONTENT_TYPE:
595 596 597 598 599
        {
            char **type = va_arg( args, char ** );

            if( p_sys->b_icecast && p_sys->psz_mime == NULL )
                *type = strdup( "audio/mpeg" );
600
            else if( !strcasecmp( p_access->psz_name, "itpc" ) )
601
                *type = strdup( "application/rss+xml" );
602
            else if( !strcasecmp( p_access->psz_name, "unsv" ) &&
603 604 605 606
                p_sys->psz_mime != NULL &&
                !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
                /* Grrrr! detect ultravox server and force NSV demuxer */
                *type = strdup( "video/nsa" );
607
            else if( p_sys->psz_mime )
608
                *type = strdup( p_sys->psz_mime );
609 610
            else
                return VLC_EGENERIC;
611
            break;
612
        }
613

614 615 616 617 618 619 620
        default:
            return VLC_EGENERIC;

    }
    return VLC_SUCCESS;
}

621
/*****************************************************************************
622
 * Connect:
623
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
624
static int Connect( stream_t *p_access )
625
{
626
    access_sys_t   *p_sys = p_access->p_sys;
627
    vlc_url_t      srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
628
    ssize_t val;
629

630
    /* Clean info */
631 632
    free( p_sys->psz_location );
    free( p_sys->psz_mime );
633

634 635 636
    free( p_sys->psz_icy_genre );
    free( p_sys->psz_icy_name );
    free( p_sys->psz_icy_title );
637

638 639
    vlc_http_auth_Init( &p_sys->auth );
    vlc_http_auth_Init( &p_sys->proxy_auth );
640 641
    p_sys->psz_location = NULL;
    p_sys->psz_mime = NULL;
642
    p_sys->i_icy_meta = 0;
643
    p_sys->i_icy_offset = 0;
644 645 646
    p_sys->psz_icy_name = NULL;
    p_sys->psz_icy_genre = NULL;
    p_sys->psz_icy_title = NULL;
647
    p_sys->b_has_size = false;
648
    p_sys->offset = 0;
649
    p_sys->size = 0;
650

651 652 653
    struct vlc_memstream stream;

    vlc_memstream_open(&stream);
654

655
    vlc_memstream_puts(&stream, "GET ");
656
    if( p_sys->b_proxy )
657 658 659 660
        vlc_memstream_printf( &stream, "http://%s:%d",
                              p_sys->url.psz_host, p_sys->url.i_port );
    if( p_sys->url.psz_path == NULL || p_sys->url.psz_path[0] == '\0' )
        vlc_memstream_putc( &stream, '/' );
661
    else
662 663 664 665 666 667
        vlc_memstream_puts( &stream, p_sys->url.psz_path );
    if( p_sys->url.psz_option != NULL )
        vlc_memstream_printf( &stream, "?%s", p_sys->url.psz_option );
    vlc_memstream_puts( &stream, " HTTP/1.0\r\n" );

    vlc_memstream_printf( &stream, "Host: %s", p_sys->url.psz_host );
668
    if( p_sys->url.i_port != 80 )
669 670 671
        vlc_memstream_printf( &stream, ":%d", p_sys->url.i_port );
    vlc_memstream_puts( &stream, "\r\n" );

672
    /* User Agent */
673 674
    vlc_memstream_printf( &stream, "User-Agent: %s\r\n",
                          p_sys->psz_user_agent );
675 676
    /* Referrer */
    if (p_sys->psz_referrer)
677 678
        vlc_memstream_printf( &stream, "Referer: %s\r\n",
                              p_sys->psz_referrer );
679

680
    /* Authentication */
681 682 683 684 685 686 687 688 689 690 691
    if( p_sys->url.psz_username != NULL && p_sys->url.psz_password != NULL )
    {
        char *auth;

        auth = vlc_http_auth_FormatAuthorizationHeader( VLC_OBJECT(p_access),
                            &p_sys->auth, "GET", p_sys->url.psz_path,
                            p_sys->url.psz_username, p_sys->url.psz_password );
        if( auth != NULL )
             vlc_memstream_printf( &stream, "Authorization: %s\r\n", auth );
        free( auth );
    }
692

693
    /* Proxy Authentication */
694 695
    if( p_sys->b_proxy && p_sys->proxy.psz_username != NULL
     && p_sys->proxy.psz_password != NULL )
696 697 698 699
    {
        char *auth;

        auth = vlc_http_auth_FormatAuthorizationHeader( VLC_OBJECT(p_access),
700 701
                        &p_sys->proxy_auth, "GET", p_sys->url.psz_path,
                        p_sys->proxy.psz_username, p_sys->proxy.psz_password );
702 703 704 705 706
        if( auth != NULL )
             vlc_memstream_printf( &stream, "Proxy-Authorization: %s\r\n",
                                   auth );
        free( auth );
    }
707

708
    /* ICY meta data request */
709 710 711 712 713 714
    vlc_memstream_puts( &stream, "Icy-MetaData: 1\r\n" );

    vlc_memstream_puts( &stream, "\r\n" );

    if( vlc_memstream_close( &stream ) )
        return -1;
715

716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
    /* Open connection */
    assert( p_sys->fd == -1 ); /* No open sockets (leaking fds is BAD) */
    p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
    if( p_sys->fd == -1 )
    {
        msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
        free( stream.ptr );
        return -1;
    }
    setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));

    msg_Dbg( p_access, "sending request:\n%s", stream.ptr );
    val = net_Write( p_access, p_sys->fd, stream.ptr, stream.length );
    free( stream.ptr );

    if( val < (ssize_t)stream.length )
732
    {
733
        msg_Err( p_access, "failed to send request" );
734
        Disconnect( p_access );
735
        return -2;
736 737
    }

738
    /* Read Answer */
739
    char *psz = net_Gets( p_access, p_sys->fd );
740
    if( psz == NULL )
741
    {
742
        msg_Err( p_access, "failed to read answer" );
743
        goto error;
744
    }
745
    if( !strncmp( psz, "HTTP/1.", 7 ) )
746
    {
747
        p_sys->i_code = atoi( &psz[9] );
748
        msg_Dbg( p_access, "HTTP answer code %d", p_sys->i_code );
749
    }
750
    else if( !strncmp( psz, "ICY", 3 ) )
751
    {
752
        p_sys->i_code = atoi( &psz[4] );
753
        msg_Dbg( p_access, "ICY answer code %d", p_sys->i_code );
754
        p_sys->b_icecast = true;
755
        p_sys->b_reconnect = true;
756
    }
757
    else
758
    {
759
        msg_Err( p_access, "invalid HTTP reply '%s'", psz );
760 761
        free( psz );
        goto error;
762
    }
763 764 765 766 767 768 769
    /* Authentication error - We'll have to display the dialog */
    if( p_sys->i_code == 401 )
    {

    }
    /* Other fatal error */
    else if( p_sys->i_code >= 400 )
770
    {
771
        msg_Err( p_access, "error: %s", psz );
772 773
        free( psz );
        goto error;
774
    }
775
    free( psz );
776

777
    for( ;; )
778
    {
779
        char *p, *p_trailing;
780

781
        psz = net_Gets( p_access, p_sys->fd );
782
        if( psz == NULL )
783
        {
784
            msg_Err( p_access, "failed to read answer" );
785
            goto error;
786
        }
787

788
        /* msg_Dbg( p_input, "Line=%s", psz ); */
789
        if( *psz == '\0' )
790
        {
791 792
            free( psz );
            break;
793
        }
794

795
        if( ( p = strchr( psz, ':' ) ) == NULL )
796
        {
797
            msg_Err( p_access, "malformed header line: %s", psz );
798 799
            free( psz );
            goto error;
800
        }
801
        *p++ = '\0';
802 803 804 805 806 807 808 809 810 811 812 813 814
        p += strspn( p, " \t" );

        /* trim trailing white space */
        p_trailing = p + strlen( p );
        if( p_trailing > p )
        {
            p_trailing--;
            while( ( *p_trailing == ' ' || *p_trailing == '\t' ) && p_trailing > p )
            {
                *p_trailing = '\0';
                p_trailing--;
            }
        }
815

816
        if( !strcasecmp( psz, "Content-Length" ) )
817
        {
818
            uint64_t i_size = (uint64_t)atoll( p );
819
            if(i_size > p_sys->size) {
820
                p_sys->b_has_size = true;
821
                p_sys->size = i_size;
822
            }
823
        }
824
        else if( !strcasecmp( psz, "Location" ) )
825
        {
826 827
            char * psz_new_loc;

828 829
            /* This does not follow RFC 2068, but yet if the url is not absolute,
             * handle it as everyone does. */
830
            if( p[0] == '/' )
831
            {
832
                if( p_sys->url.i_port == 80 )
833
                {
834
                    if( asprintf(&psz_new_loc, "http://%s%s",
835 836
                                 p_sys->url.psz_host, p) < 0 )
                        goto error;
837 838 839
                }
                else
                {
840
                    if( asprintf(&psz_new_loc, "http://%s:%d%s",
841 842
                                 p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
                        goto error;
843
                }
844 845
            }
            else
846
            {
847
                psz_new_loc = strdup( p );
848
            }
849

850
            free( p_sys->psz_location );
851
            p_sys->psz_location = psz_new_loc;
852
        }
853
        else if( !strcasecmp( psz, "Content-Type" ) )
854
        {
855
            free( p_sys->psz_mime );
856
            p_sys->psz_mime = strdup( p );
857
            msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
858
        }
859 860 861 862
        else if( !strcasecmp( psz, "Content-Encoding" ) )
        {
            msg_Dbg( p_access, "Content-Encoding: %s", p );
        }
863
        else if( !strcasecmp( psz, "Server" ) )
864 865
        {
            msg_Dbg( p_access, "Server: %s", p );
866 867
            if( !strncasecmp( p, "Icecast", 7 ) ||
                !strncasecmp( p, "Nanocaster", 10 ) )
868
            {
869
                /* Remember if this is Icecast
870
                 * we need to force demux in this case without breaking
871
                 *  autodetection */
872

873
                /* Let live 365 streams (nanocaster) piggyback on the icecast
874
                 * routine. They look very similar */
875

876 877
                p_sys->b_reconnect = true;
                p_sys->b_icecast = true;
878
            }
879
        }
880 881 882 883 884 885
        else if( !strcasecmp( psz, "Icy-MetaInt" ) )
        {
            msg_Dbg( p_access, "Icy-MetaInt: %s", p );
            p_sys->i_icy_meta = atoi( p );
            if( p_sys->i_icy_meta < 0 )
                p_sys->i_icy_meta = 0;
886
            if( p_sys->i_icy_meta > 1 )
887 888
            {
                p_sys->i_icy_offset = p_sys->i_icy_meta;
889
                p_sys->b_icecast = true;
890
            }
891 892 893 894 895

            msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
        }
        else if( !strcasecmp( psz, "Icy-Name" ) )
        {
896
            free( p_sys->psz_icy_name );
897 898 899 900
            char *psz_tmp = strdup( p );
            p_sys->psz_icy_name = EnsureUTF8( psz_tmp );
            if( !p_sys->psz_icy_name )
                free( psz_tmp );
901
            else
902
                vlc_xml_decode( p_sys->psz_icy_name );
903
            msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
904
            input_thread_t *p_input = p_access->p_input;
905 906 907 908 909 910
            if ( p_input )
            {
                input_item_t *p_input_item = input_GetItem( p_access->p_input );
                if ( p_input_item )
                    input_item_SetMeta( p_input_item, vlc_meta_Title, p_sys->psz_icy_name );
            }
911

912 913
            p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
            p_sys->b_reconnect = true;
914 915 916
        }
        else if( !strcasecmp( psz, "Icy-Genre" ) )
        {
917
            free( p_sys->psz_icy_genre );
918 919 920 921
            char *psz_tmp = strdup( p );
            p_sys->psz_icy_genre = EnsureUTF8( psz_tmp );
            if( !p_sys->psz_icy_genre )
                free( psz_tmp );
922
            else
923
                vlc_xml_decode( p_sys->psz_icy_genre );
924
            msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
925
            input_thread_t *p_input = p_access->p_input;
926 927 928 929 930 931
            if( p_input )
            {
                input_item_t *p_input_item = input_GetItem( p_access->p_input );
                if( p_input_item )
                    input_item_SetMeta( p_input_item, vlc_meta_Genre, p_sys->psz_icy_genre );
            }
932 933 934 935 936
        }
        else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
        {
            msg_Dbg( p_access, "Icy-Notice: %s", p );
        }
937 938 939 940 941
        else if( !strncasecmp( psz, "icy-", 4 ) ||
                 !strncasecmp( psz, "ice-", 4 ) ||
                 !strncasecmp( psz, "x-audiocast", 11 ) )
        {
            msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
942 943 944 945
        }
        else if( !strcasecmp( psz, "www-authenticate" ) )
        {
            msg_Dbg( p_access, "Authentication header: %s", p );
946 947
            vlc_http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
                                                      &p_sys->auth, p );
948 949 950 951
        }
        else if( !strcasecmp( psz, "proxy-authenticate" ) )
        {
            msg_Dbg( p_access, "Proxy authentication header: %s", p );
952 953
            vlc_http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
                                                      &p_sys->proxy_auth, p );
954 955 956
        }
        else if( !strcasecmp( psz, "authentication-info" ) )
        {
957 958 959 960 961 962 963 964 965
            msg_Dbg( p_access, "Authentication Info header: %s", p );
            if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
                goto error;
        }
        else if( !strcasecmp( psz, "proxy-authentication-info" ) )
        {
            msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
            if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
                goto error;
966
        }
967

968
        free( psz );
969
    }
970
    return 0;
971

972
error:
973
    Disconnect( p_access );
974
    return -2;
975 976 977 978 979
}

/*****************************************************************************
 * Disconnect:
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
980
static void Disconnect( stream_t *p_access )
981 982 983
{
    access_sys_t *p_sys = p_access->p_sys;

984
    if( p_sys->fd != -1)
985
        net_Close(p_sys->fd);
986
    p_sys->fd = -1;
987 988 989

    vlc_http_auth_Deinit( &p_sys->auth );
    vlc_http_auth_Deinit( &p_sys->proxy_auth );
990
}
991

992
/*****************************************************************************
993
 * HTTP authentication
994
 *****************************************************************************/
995

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
996
static int AuthCheckReply( stream_t *p_access, const char *psz_header,
997
                           vlc_url_t *p_url, vlc_http_auth_t *p_auth )
998
{
999
    return
1000 1001 1002 1003 1004 1005
        vlc_http_auth_ParseAuthenticationInfoHeader( VLC_OBJECT(p_access),
                                                     p_auth,
                                                     psz_header, "",
                                                     p_url->psz_path,
                                                     p_url->psz_username,
                                                     p_url->psz_password );
1006
}