]> git.sesse.net Git - vlc/blob - modules/access/http.c
4b24ad3dcc9759c5e6f949443e1fbc5ca1d2050e
[vlc] / modules / access / http.c
1 /*****************************************************************************
2  * http.c: HTTP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          RĂ©mi Denis-Courmont <rem # videolan.org>
10  *          Antoine Cellerier <dionoea at videolan dot org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36
37
38 #include <vlc_access.h>
39
40 #include <vlc_dialog.h>
41 #include <vlc_meta.h>
42 #include <vlc_network.h>
43 #include <vlc_url.h>
44 #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
50 #ifdef HAVE_ZLIB_H
51 #   include <zlib.h>
52 #endif
53
54 #include <assert.h>
55
56 #ifdef HAVE_LIBPROXY
57 #    include <proxy.h>
58 #endif
59 /*****************************************************************************
60  * Module descriptor
61  *****************************************************************************/
62 static int  Open ( vlc_object_t * );
63 static void Close( vlc_object_t * );
64
65 #define PROXY_TEXT N_("HTTP proxy")
66 #define PROXY_LONGTEXT N_( \
67     "HTTP proxy to be used It must be of the form " \
68     "http://[user@]myproxy.mydomain:myport/ ; " \
69     "if empty, the http_proxy environment variable will be tried." )
70
71 #define PROXY_PASS_TEXT N_("HTTP proxy password")
72 #define PROXY_PASS_LONGTEXT N_( \
73     "If your HTTP proxy requires a password, set it here." )
74
75 #define CACHING_TEXT N_("Caching value in ms")
76 #define CACHING_LONGTEXT N_( \
77     "Caching value for HTTP streams. This " \
78     "value should be set in milliseconds." )
79
80 #define AGENT_TEXT N_("HTTP user agent")
81 #define AGENT_LONGTEXT N_("User agent that will be " \
82     "used for the connection.")
83
84 #define RECONNECT_TEXT N_("Auto re-connect")
85 #define RECONNECT_LONGTEXT N_( \
86     "Automatically try to reconnect to the stream in case of a sudden " \
87     "disconnect." )
88
89 #define CONTINUOUS_TEXT N_("Continuous stream")
90 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
91     "being constantly updated (for example, a JPG file on a server). " \
92     "You should not globally enable this option as it will break all other " \
93     "types of HTTP streams." )
94
95 #define FORWARD_COOKIES_TEXT N_("Forward Cookies")
96 #define FORWARD_COOKIES_LONGTEXT N_("Forward Cookies across http redirections ")
97
98 vlc_module_begin ()
99     set_description( N_("HTTP input") )
100     set_capability( "access", 0 )
101     set_shortname( N_( "HTTP(S)" ) )
102     set_category( CAT_INPUT )
103     set_subcategory( SUBCAT_INPUT_ACCESS )
104
105     add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
106                 false )
107     add_password( "http-proxy-pwd", NULL, NULL,
108                   PROXY_PASS_TEXT, PROXY_PASS_LONGTEXT, false )
109     add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
110                  CACHING_TEXT, CACHING_LONGTEXT, true )
111     add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
112                 AGENT_LONGTEXT, true )
113     add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
114               RECONNECT_LONGTEXT, true )
115     add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
116               CONTINUOUS_LONGTEXT, true )
117         change_safe()
118     add_bool( "http-forward-cookies", true, NULL, FORWARD_COOKIES_TEXT,
119               FORWARD_COOKIES_LONGTEXT, true )
120     add_obsolete_string("http-user")
121     add_obsolete_string("http-pwd")
122     add_shortcut( "http" )
123     add_shortcut( "https" )
124     add_shortcut( "unsv" )
125     add_shortcut( "itpc" ) /* iTunes Podcast */
126     set_callbacks( Open, Close )
127 vlc_module_end ()
128
129 /*****************************************************************************
130  * Local prototypes
131  *****************************************************************************/
132
133 /* RFC 2617: Basic and Digest Access Authentication */
134 typedef struct http_auth_t
135 {
136     char *psz_realm;
137     char *psz_domain;
138     char *psz_nonce;
139     char *psz_opaque;
140     char *psz_stale;
141     char *psz_algorithm;
142     char *psz_qop;
143     int i_nonce;
144     char *psz_cnonce;
145     char *psz_HA1; /* stored H(A1) value if algorithm = "MD5-sess" */
146 } http_auth_t;
147
148 struct access_sys_t
149 {
150     int fd;
151     tls_session_t *p_tls;
152     v_socket_t    *p_vs;
153
154     /* From uri */
155     vlc_url_t url;
156     char    *psz_user_agent;
157     http_auth_t auth;
158
159     /* Proxy */
160     bool b_proxy;
161     vlc_url_t  proxy;
162     http_auth_t proxy_auth;
163     char       *psz_proxy_passbuf;
164
165     /* */
166     int        i_code;
167     const char *psz_protocol;
168     int        i_version;
169
170     char       *psz_mime;
171     char       *psz_pragma;
172     char       *psz_location;
173     bool b_mms;
174     bool b_icecast;
175     bool b_ssl;
176 #ifdef HAVE_ZLIB_H
177     bool b_compressed;
178     struct
179     {
180         z_stream   stream;
181         uint8_t   *p_buffer;
182     } inflate;
183 #endif
184
185     bool b_chunked;
186     int64_t    i_chunk;
187
188     int        i_icy_meta;
189     int64_t    i_icy_offset;
190     char       *psz_icy_name;
191     char       *psz_icy_genre;
192     char       *psz_icy_title;
193
194     int64_t i_remaining;
195
196     bool b_seekable;
197     bool b_reconnect;
198     bool b_continuous;
199     bool b_pace_control;
200     bool b_persist;
201
202     vlc_array_t * cookies;
203 };
204
205 /* */
206 static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies );
207
208 /* */
209 static ssize_t Read( access_t *, uint8_t *, size_t );
210 static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
211 static int Seek( access_t *, int64_t );
212 static int Control( access_t *, int, va_list );
213
214 /* */
215 static int Connect( access_t *, int64_t );
216 static int Request( access_t *p_access, int64_t i_tell );
217 static void Disconnect( access_t * );
218
219 /* Small Cookie utilities. Cookies support is partial. */
220 static char * cookie_get_content( const char * cookie );
221 static char * cookie_get_domain( const char * cookie );
222 static char * cookie_get_name( const char * cookie );
223 static void cookie_append( vlc_array_t * cookies, char * cookie );
224
225
226 static void AuthParseHeader( access_t *p_access, const char *psz_header,
227                              http_auth_t *p_auth );
228 static void AuthReply( access_t *p_acces, const char *psz_prefix,
229                        vlc_url_t *p_url, http_auth_t *p_auth );
230 static int AuthCheckReply( access_t *p_access, const char *psz_header,
231                            vlc_url_t *p_url, http_auth_t *p_auth );
232 static void AuthReset( http_auth_t *p_auth );
233
234 /*****************************************************************************
235  * Open:
236  *****************************************************************************/
237 static int Open( vlc_object_t *p_this )
238 {
239     return OpenWithCookies( p_this, NULL );
240 }
241
242 static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies )
243 {
244     access_t     *p_access = (access_t*)p_this;
245     access_sys_t *p_sys;
246     char         *psz, *p;
247     /* Only forward an store cookies if the corresponding option is activated */
248     bool   b_forward_cookies = var_CreateGetBool( p_access, "http-forward-cookies" );
249     vlc_array_t * saved_cookies = b_forward_cookies ? (cookies ?: vlc_array_new()) : NULL;
250
251     /* Set up p_access */
252     STANDARD_READ_ACCESS_INIT;
253 #ifdef HAVE_ZLIB_H
254     p_access->pf_read = ReadCompressed;
255 #endif
256     p_sys->fd = -1;
257     p_sys->b_proxy = false;
258     p_sys->psz_proxy_passbuf = NULL;
259     p_sys->i_version = 1;
260     p_sys->b_seekable = true;
261     p_sys->psz_mime = NULL;
262     p_sys->psz_pragma = NULL;
263     p_sys->b_mms = false;
264     p_sys->b_icecast = false;
265     p_sys->psz_location = NULL;
266     p_sys->psz_user_agent = NULL;
267     p_sys->b_pace_control = true;
268     p_sys->b_ssl = false;
269 #ifdef HAVE_ZLIB_H
270     p_sys->b_compressed = false;
271     /* 15 is the max windowBits, +32 to enable optional gzip decoding */
272     if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
273         msg_Warn( p_access, "Error during zlib initialisation: %s",
274                   p_sys->inflate.stream.msg );
275     if( zlibCompileFlags() & (1<<17) )
276         msg_Warn( p_access, "Your zlib was compiled without gzip support." );
277     p_sys->inflate.p_buffer = NULL;
278 #endif
279     p_sys->p_tls = NULL;
280     p_sys->p_vs = NULL;
281     p_sys->i_icy_meta = 0;
282     p_sys->i_icy_offset = 0;
283     p_sys->psz_icy_name = NULL;
284     p_sys->psz_icy_genre = NULL;
285     p_sys->psz_icy_title = NULL;
286     p_sys->i_remaining = 0;
287     p_sys->b_persist = false;
288     p_access->info.i_size = -1;
289     p_access->info.i_pos  = 0;
290     p_access->info.b_eof  = false;
291
292     p_sys->cookies = saved_cookies;
293
294     /* Parse URI - remove spaces */
295     p = psz = strdup( p_access->psz_path );
296     while( (p = strchr( p, ' ' )) != NULL )
297         *p = '+';
298     vlc_UrlParse( &p_sys->url, psz, 0 );
299     free( psz );
300
301     if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
302     {
303         msg_Warn( p_access, "invalid host" );
304         goto error;
305     }
306     if( !strncmp( p_access->psz_access, "https", 5 ) )
307     {
308         /* HTTP over SSL */
309         p_sys->b_ssl = true;
310         if( p_sys->url.i_port <= 0 )
311             p_sys->url.i_port = 443;
312     }
313     else
314     {
315         if( p_sys->url.i_port <= 0 )
316             p_sys->url.i_port = 80;
317     }
318
319     /* Do user agent */
320     p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
321
322     /* Check proxy */
323     psz = var_CreateGetNonEmptyString( p_access, "http-proxy" );
324     if( psz )
325     {
326         p_sys->b_proxy = true;
327         vlc_UrlParse( &p_sys->proxy, psz, 0 );
328         free( psz );
329     }
330 #ifdef HAVE_LIBPROXY
331     else
332     {
333         pxProxyFactory *pf = px_proxy_factory_new();
334         if (pf)
335         {
336             char *buf;
337             int i;
338             i=asprintf(&buf, "%s://%s", p_access->psz_access, p_access->psz_path);
339             if (i >= 0)
340             {
341                 msg_Dbg(p_access, "asking libproxy about url '%s'", buf);
342                 char **proxies = px_proxy_factory_get_proxies(pf, buf);
343                 if (proxies[0])
344                 {
345                     msg_Dbg(p_access, "libproxy suggest to use '%s'", proxies[0]);
346                     if(strcmp(proxies[0],"direct://") != 0)
347                     {
348                         p_sys->b_proxy = true;
349                         vlc_UrlParse( &p_sys->proxy, proxies[0], 0);
350                     }
351                 }
352                 for(i=0;proxies[i];i++) free(proxies[i]);
353                 free(proxies);
354                 free(buf);
355             }
356             px_proxy_factory_free(pf);
357         }
358         else
359         {
360             msg_Err(p_access, "Allocating memory for libproxy failed");
361         }
362     }
363 #elif HAVE_GETENV
364     else
365     {
366         psz = getenv( "http_proxy" );
367         if( psz )
368         {
369             p_sys->b_proxy = true;
370             vlc_UrlParse( &p_sys->proxy, psz, 0 );
371         }
372     }
373 #endif
374     if( psz ) /* No, this is NOT a use-after-free error */
375     {
376         psz = var_CreateGetNonEmptyString( p_access, "http-proxy-pwd" );
377         if( psz )
378             p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
379     }
380
381     if( p_sys->b_proxy )
382     {
383         if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
384         {
385             msg_Warn( p_access, "invalid proxy host" );
386             goto error;
387         }
388         if( p_sys->proxy.i_port <= 0 )
389         {
390             p_sys->proxy.i_port = 80;
391         }
392     }
393
394     msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
395              p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
396     if( p_sys->b_proxy )
397     {
398         msg_Dbg( p_access, "      proxy %s:%d", p_sys->proxy.psz_host,
399                  p_sys->proxy.i_port );
400     }
401     if( p_sys->url.psz_username && *p_sys->url.psz_username )
402     {
403         msg_Dbg( p_access, "      user='%s'", p_sys->url.psz_username );
404     }
405
406     p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
407     p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
408
409 connect:
410     /* Connect */
411     switch( Connect( p_access, 0 ) )
412     {
413         case -1:
414             goto error;
415
416         case -2:
417             /* Retry with http 1.0 */
418             msg_Dbg( p_access, "switching to HTTP version 1.0" );
419             p_sys->i_version = 0;
420             p_sys->b_seekable = false;
421
422             if( !vlc_object_alive (p_access) || Connect( p_access, 0 ) )
423                 goto error;
424
425 #ifndef NDEBUG
426         case 0:
427             break;
428
429         default:
430             msg_Err( p_access, "You should not be here" );
431             abort();
432 #endif
433     }
434
435     if( p_sys->i_code == 401 )
436     {
437         char *psz_login, *psz_password;
438         /* FIXME ? */
439         if( p_sys->url.psz_username && p_sys->url.psz_password &&
440             p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
441         {
442             Disconnect( p_access );
443             goto connect;
444         }
445         msg_Dbg( p_access, "authentication failed for realm %s",
446                  p_sys->auth.psz_realm );
447         dialog_Login( p_access, &psz_login, &psz_password,
448                       _("HTTP authentication"),
449              _("Please enter a valid login name and a password for realm %s."),
450                       p_sys->auth.psz_realm );
451         if( psz_login != NULL && psz_password != NULL )
452         {
453             msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
454                         psz_login, psz_password );
455             p_sys->url.psz_username = psz_login;
456             p_sys->url.psz_password = psz_password;
457             Disconnect( p_access );
458             goto connect;
459         }
460         else
461         {
462             free( psz_login );
463             free( psz_password );
464             goto error;
465         }
466     }
467
468     if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
469           p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
470         p_sys->psz_location && *p_sys->psz_location )
471     {
472         msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
473
474         /* Do not accept redirection outside of HTTP works */
475         if( strncmp( p_sys->psz_location, "http", 4 )
476          || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
477            && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
478         {
479             msg_Err( p_access, "insecure redirection ignored" );
480             goto error;
481         }
482         free( p_access->psz_path );
483         p_access->psz_path = strdup( p_sys->psz_location );
484         /* Clean up current Open() run */
485         vlc_UrlClean( &p_sys->url );
486         AuthReset( &p_sys->auth );
487         vlc_UrlClean( &p_sys->proxy );
488         free( p_sys->psz_proxy_passbuf );
489         AuthReset( &p_sys->proxy_auth );
490         free( p_sys->psz_mime );
491         free( p_sys->psz_pragma );
492         free( p_sys->psz_location );
493         free( p_sys->psz_user_agent );
494
495         Disconnect( p_access );
496         cookies = p_sys->cookies;
497         free( p_sys );
498
499         /* Do new Open() run with new data */
500         return OpenWithCookies( p_this, cookies );
501     }
502
503     if( p_sys->b_mms )
504     {
505         msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
506         goto error;
507     }
508
509     if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
510     {
511         if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
512         {
513             if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
514                 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
515             {
516                 free( p_access->psz_demux );
517                 p_access->psz_demux = strdup( "nsv" );
518             }
519             else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
520                      !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
521             {
522                 free( p_access->psz_demux );
523                 p_access->psz_demux = strdup( "m4a" );
524             }
525             else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
526             {
527                 free( p_access->psz_demux );
528                 p_access->psz_demux = strdup( "mp3" );
529             }
530
531             msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
532                       p_access->psz_demux );
533
534 #if 0       /* Doesn't work really well because of the pre-buffering in
535              * shoutcast servers (the buffer content will be sent as fast as
536              * possible). */
537             p_sys->b_pace_control = false;
538 #endif
539         }
540         else if( !p_sys->psz_mime )
541         {
542             free( p_access->psz_demux );
543             /* Shoutcast */
544             p_access->psz_demux = strdup( "mp3" );
545         }
546         /* else probably Ogg Vorbis */
547     }
548     else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
549              p_sys->psz_mime &&
550              !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
551     {
552         free( p_access->psz_demux );
553         /* Grrrr! detect ultravox server and force NSV demuxer */
554         p_access->psz_demux = strdup( "nsv" );
555     }
556     else if( !strcmp( p_access->psz_access, "itpc" ) )
557     {
558         free( p_access->psz_demux );
559         p_access->psz_demux = strdup( "podcast" );
560     }
561     else if( p_sys->psz_mime &&
562              !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
563              ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
564     {
565         free( p_access->psz_demux );
566         p_access->psz_demux = strdup( "xspf-open" );
567     }
568
569     if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
570
571     /* PTS delay */
572     var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
573
574     return VLC_SUCCESS;
575
576 error:
577     vlc_UrlClean( &p_sys->url );
578     vlc_UrlClean( &p_sys->proxy );
579     free( p_sys->psz_proxy_passbuf );
580     free( p_sys->psz_mime );
581     free( p_sys->psz_pragma );
582     free( p_sys->psz_location );
583     free( p_sys->psz_user_agent );
584
585     Disconnect( p_access );
586
587     if( p_sys->cookies )
588     {
589         int i;
590         for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
591             free(vlc_array_item_at_index( p_sys->cookies, i ));
592         vlc_array_destroy( p_sys->cookies );
593     }
594
595 #ifdef HAVE_ZLIB_H
596     inflateEnd( &p_sys->inflate.stream );
597 #endif
598     free( p_sys );
599     return VLC_EGENERIC;
600 }
601
602 /*****************************************************************************
603  * Close:
604  *****************************************************************************/
605 static void Close( vlc_object_t *p_this )
606 {
607     access_t     *p_access = (access_t*)p_this;
608     access_sys_t *p_sys = p_access->p_sys;
609
610     vlc_UrlClean( &p_sys->url );
611     AuthReset( &p_sys->auth );
612     vlc_UrlClean( &p_sys->proxy );
613     AuthReset( &p_sys->proxy_auth );
614
615     free( p_sys->psz_mime );
616     free( p_sys->psz_pragma );
617     free( p_sys->psz_location );
618
619     free( p_sys->psz_icy_name );
620     free( p_sys->psz_icy_genre );
621     free( p_sys->psz_icy_title );
622
623     free( p_sys->psz_user_agent );
624
625     Disconnect( p_access );
626
627     if( p_sys->cookies )
628     {
629         int i;
630         for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
631             free(vlc_array_item_at_index( p_sys->cookies, i ));
632         vlc_array_destroy( p_sys->cookies );
633     }
634
635 #ifdef HAVE_ZLIB_H
636     inflateEnd( &p_sys->inflate.stream );
637     free( p_sys->inflate.p_buffer );
638 #endif
639
640     free( p_sys );
641 }
642
643 /*****************************************************************************
644  * Read: Read up to i_len bytes from the http connection and place in
645  * p_buffer. Return the actual number of bytes read
646  *****************************************************************************/
647 static int ReadICYMeta( access_t *p_access );
648 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
649 {
650     access_sys_t *p_sys = p_access->p_sys;
651     int i_read;
652
653     if( p_sys->fd == -1 )
654     {
655         p_access->info.b_eof = true;
656         return 0;
657     }
658
659     if( p_access->info.i_size >= 0 &&
660         i_len + p_access->info.i_pos > p_access->info.i_size )
661     {
662         if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
663         {
664             p_access->info.b_eof = true;
665             return 0;
666         }
667     }
668
669     if( p_sys->b_chunked )
670     {
671         if( p_sys->i_chunk < 0 )
672         {
673             p_access->info.b_eof = true;
674             return 0;
675         }
676
677         if( p_sys->i_chunk <= 0 )
678         {
679             char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
680             /* read the chunk header */
681             if( psz == NULL )
682             {
683                 /* fatal error - end of file */
684                 msg_Dbg( p_access, "failed reading chunk-header line" );
685                 return 0;
686             }
687             p_sys->i_chunk = strtoll( psz, NULL, 16 );
688             free( psz );
689
690             if( p_sys->i_chunk <= 0 )   /* eof */
691             {
692                 p_sys->i_chunk = -1;
693                 p_access->info.b_eof = true;
694                 return 0;
695             }
696         }
697
698         if( i_len > p_sys->i_chunk )
699         {
700             i_len = p_sys->i_chunk;
701         }
702     }
703     else if( p_access->info.i_size != -1 && (int64_t)i_len > p_sys->i_remaining) {
704         /* Only ask for the remaining length */
705         i_len = (size_t)p_sys->i_remaining;
706         if(i_len == 0) {
707             p_access->info.b_eof = true;
708             return 0;
709         }
710     }
711
712
713     if( p_sys->i_icy_meta > 0 && p_access->info.i_pos-p_sys->i_icy_offset > 0 )
714     {
715         int64_t i_next = p_sys->i_icy_meta -
716                                     (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
717
718         if( i_next == p_sys->i_icy_meta )
719         {
720             if( ReadICYMeta( p_access ) )
721             {
722                 p_access->info.b_eof = true;
723                 return -1;
724             }
725         }
726         if( i_len > i_next )
727             i_len = i_next;
728     }
729
730     i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
731
732     if( i_read > 0 )
733     {
734         p_access->info.i_pos += i_read;
735
736         if( p_sys->b_chunked )
737         {
738             p_sys->i_chunk -= i_read;
739             if( p_sys->i_chunk <= 0 )
740             {
741                 /* read the empty line */
742                 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
743                 free( psz );
744             }
745         }
746     }
747     else if( i_read == 0 )
748     {
749         /*
750          * I very much doubt that this will work.
751          * If i_read == 0, the connection *IS* dead, so the only
752          * sensible thing to do is Disconnect() and then retry.
753          * Otherwise, I got recv() completely wrong. -- Courmisch
754          */
755         if( p_sys->b_continuous )
756         {
757             Request( p_access, 0 );
758             p_sys->b_continuous = false;
759             i_read = Read( p_access, p_buffer, i_len );
760             p_sys->b_continuous = true;
761         }
762         Disconnect( p_access );
763         if( p_sys->b_reconnect )
764         {
765             msg_Dbg( p_access, "got disconnected, trying to reconnect" );
766             if( Connect( p_access, p_access->info.i_pos ) )
767             {
768                 msg_Dbg( p_access, "reconnection failed" );
769             }
770             else
771             {
772                 p_sys->b_reconnect = false;
773                 i_read = Read( p_access, p_buffer, i_len );
774                 p_sys->b_reconnect = true;
775             }
776         }
777
778         if( i_read == 0 ) p_access->info.b_eof = true;
779     }
780
781     if( p_access->info.i_size != -1 )
782     {
783         p_sys->i_remaining -= i_read;
784     }
785
786     return i_read;
787 }
788
789 static int ReadICYMeta( access_t *p_access )
790 {
791     access_sys_t *p_sys = p_access->p_sys;
792
793     uint8_t buffer;
794     char *p, *psz_meta;
795     int i_read;
796
797     /* Read meta data length */
798     i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
799                        true );
800     if( i_read <= 0 )
801         return VLC_EGENERIC;
802     if( buffer == 0 )
803         return VLC_SUCCESS;
804
805     i_read = buffer << 4;
806     /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
807
808     psz_meta = malloc( i_read + 1 );
809     if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
810                   (uint8_t *)psz_meta, i_read, true ) != i_read )
811         return VLC_EGENERIC;
812
813     psz_meta[i_read] = '\0'; /* Just in case */
814
815     /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
816
817     /* Now parse the meta */
818     /* Look for StreamTitle= */
819     p = strcasestr( (char *)psz_meta, "StreamTitle=" );
820     if( p )
821     {
822         p += strlen( "StreamTitle=" );
823         if( *p == '\'' || *p == '"' )
824         {
825             char closing[] = { p[0], ';', '\0' };
826             char *psz = strstr( &p[1], closing );
827             if( !psz )
828                 psz = strchr( &p[1], ';' );
829
830             if( psz ) *psz = '\0';
831         }
832         else
833         {
834             char *psz = strchr( &p[1], ';' );
835             if( psz ) *psz = '\0';
836         }
837
838         if( !p_sys->psz_icy_title ||
839             strcmp( p_sys->psz_icy_title, &p[1] ) )
840         {
841             free( p_sys->psz_icy_title );
842             p_sys->psz_icy_title = EnsureUTF8( strdup( &p[1] ));
843             p_access->info.i_update |= INPUT_UPDATE_META;
844
845             msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
846         }
847     }
848     free( psz_meta );
849
850     return VLC_SUCCESS;
851 }
852
853 #ifdef HAVE_ZLIB_H
854 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
855                                size_t i_len )
856 {
857     access_sys_t *p_sys = p_access->p_sys;
858
859     if( p_sys->b_compressed )
860     {
861         int i_ret;
862
863         if( !p_sys->inflate.p_buffer )
864             p_sys->inflate.p_buffer = malloc( 256 * 1024 );
865
866         if( p_sys->inflate.stream.avail_in == 0 )
867         {
868             ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer + p_sys->inflate.stream.avail_in, 256 * 1024 );
869             if( i_read <= 0 ) return i_read;
870             p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
871             p_sys->inflate.stream.avail_in = i_read;
872         }
873
874         p_sys->inflate.stream.avail_out = i_len;
875         p_sys->inflate.stream.next_out = p_buffer;
876
877         i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
878         msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
879
880         return i_len - p_sys->inflate.stream.avail_out;
881     }
882     else
883     {
884         return Read( p_access, p_buffer, i_len );
885     }
886 }
887 #endif
888
889 /*****************************************************************************
890  * Seek: close and re-open a connection at the right place
891  *****************************************************************************/
892 static int Seek( access_t *p_access, int64_t i_pos )
893 {
894     msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
895
896     Disconnect( p_access );
897
898     if( p_access->info.i_size
899      && (uint64_t)i_pos >= (uint64_t)p_access->info.i_size ) {
900         msg_Err( p_access, "seek to far" );
901         int retval = Seek( p_access, p_access->info.i_size - 1 );
902         if( retval == VLC_SUCCESS ) {
903             uint8_t p_buffer[2];
904             Read( p_access, p_buffer, 1);
905             p_access->info.b_eof  = false;
906         }
907         return retval;
908     }
909     if( Connect( p_access, i_pos ) )
910     {
911         msg_Err( p_access, "seek failed" );
912         p_access->info.b_eof = true;
913         return VLC_EGENERIC;
914     }
915     return VLC_SUCCESS;
916 }
917
918 /*****************************************************************************
919  * Control:
920  *****************************************************************************/
921 static int Control( access_t *p_access, int i_query, va_list args )
922 {
923     access_sys_t *p_sys = p_access->p_sys;
924     bool       *pb_bool;
925     int64_t    *pi_64;
926     vlc_meta_t *p_meta;
927
928     switch( i_query )
929     {
930         /* */
931         case ACCESS_CAN_SEEK:
932             pb_bool = (bool*)va_arg( args, bool* );
933             *pb_bool = p_sys->b_seekable;
934             break;
935         case ACCESS_CAN_FASTSEEK:
936             pb_bool = (bool*)va_arg( args, bool* );
937             *pb_bool = false;
938             break;
939         case ACCESS_CAN_PAUSE:
940         case ACCESS_CAN_CONTROL_PACE:
941             pb_bool = (bool*)va_arg( args, bool* );
942
943 #if 0       /* Disable for now until we have a clock synchro algo
944              * which works with something else than MPEG over UDP */
945             *pb_bool = p_sys->b_pace_control;
946 #endif
947             *pb_bool = true;
948             break;
949
950         /* */
951         case ACCESS_GET_PTS_DELAY:
952             pi_64 = (int64_t*)va_arg( args, int64_t * );
953             *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
954             break;
955
956         /* */
957         case ACCESS_SET_PAUSE_STATE:
958             break;
959
960         case ACCESS_GET_META:
961             p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
962
963             if( p_sys->psz_icy_name )
964                 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
965             if( p_sys->psz_icy_genre )
966                 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
967             if( p_sys->psz_icy_title )
968                 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
969             break;
970
971         case ACCESS_GET_CONTENT_TYPE:
972             *va_arg( args, char ** ) =
973                 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
974             break;
975
976         case ACCESS_GET_TITLE_INFO:
977         case ACCESS_SET_TITLE:
978         case ACCESS_SET_SEEKPOINT:
979         case ACCESS_SET_PRIVATE_ID_STATE:
980             return VLC_EGENERIC;
981
982         default:
983             msg_Warn( p_access, "unimplemented query in control" );
984             return VLC_EGENERIC;
985
986     }
987     return VLC_SUCCESS;
988 }
989
990 /*****************************************************************************
991  * Connect:
992  *****************************************************************************/
993 static int Connect( access_t *p_access, int64_t i_tell )
994 {
995     access_sys_t   *p_sys = p_access->p_sys;
996     vlc_url_t      srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
997
998     /* Clean info */
999     free( p_sys->psz_location );
1000     free( p_sys->psz_mime );
1001     free( p_sys->psz_pragma );
1002
1003     free( p_sys->psz_icy_genre );
1004     free( p_sys->psz_icy_name );
1005     free( p_sys->psz_icy_title );
1006
1007
1008     p_sys->psz_location = NULL;
1009     p_sys->psz_mime = NULL;
1010     p_sys->psz_pragma = NULL;
1011     p_sys->b_mms = false;
1012     p_sys->b_chunked = false;
1013     p_sys->i_chunk = 0;
1014     p_sys->i_icy_meta = 0;
1015     p_sys->i_icy_offset = i_tell;
1016     p_sys->psz_icy_name = NULL;
1017     p_sys->psz_icy_genre = NULL;
1018     p_sys->psz_icy_title = NULL;
1019     p_sys->i_remaining = 0;
1020     p_sys->b_persist = false;
1021
1022     p_access->info.i_size = -1;
1023     p_access->info.i_pos  = i_tell;
1024     p_access->info.b_eof  = false;
1025
1026     /* Open connection */
1027     assert( p_sys->fd == -1 ); /* No open sockets (leaking fds is BAD) */
1028     p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
1029     if( p_sys->fd == -1 )
1030     {
1031         msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
1032         return -1;
1033     }
1034     setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));
1035
1036     /* Initialize TLS/SSL session */
1037     if( p_sys->b_ssl == true )
1038     {
1039         /* CONNECT to establish TLS tunnel through HTTP proxy */
1040         if( p_sys->b_proxy )
1041         {
1042             char *psz;
1043             unsigned i_status = 0;
1044
1045             if( p_sys->i_version == 0 )
1046             {
1047                 /* CONNECT is not in HTTP/1.0 */
1048                 Disconnect( p_access );
1049                 return -1;
1050             }
1051
1052             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1053                         "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
1054                         p_sys->url.psz_host, p_sys->url.i_port,
1055                         p_sys->i_version,
1056                         p_sys->url.psz_host, p_sys->url.i_port);
1057
1058             psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
1059             if( psz == NULL )
1060             {
1061                 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
1062                 Disconnect( p_access );
1063                 return -1;
1064             }
1065
1066             sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
1067             free( psz );
1068
1069             if( ( i_status / 100 ) != 2 )
1070             {
1071                 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
1072                 Disconnect( p_access );
1073                 return -1;
1074             }
1075
1076             do
1077             {
1078                 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
1079                 if( psz == NULL )
1080                 {
1081                     msg_Err( p_access, "HTTP proxy connection failed" );
1082                     Disconnect( p_access );
1083                     return -1;
1084                 }
1085
1086                 if( *psz == '\0' )
1087                     i_status = 0;
1088
1089                 free( psz );
1090
1091                 if( !vlc_object_alive (p_access) || p_access->b_error )
1092                 {
1093                     Disconnect( p_access );
1094                     return -1;
1095                 }
1096             }
1097             while( i_status );
1098         }
1099
1100         /* TLS/SSL handshake */
1101         p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
1102                                          srv.psz_host );
1103         if( p_sys->p_tls == NULL )
1104         {
1105             msg_Err( p_access, "cannot establish HTTP/TLS session" );
1106             Disconnect( p_access );
1107             return -1;
1108         }
1109         p_sys->p_vs = &p_sys->p_tls->sock;
1110     }
1111
1112     return Request( p_access, i_tell ) ? -2 : 0;
1113 }
1114
1115
1116 static int Request( access_t *p_access, int64_t i_tell )
1117 {
1118     access_sys_t   *p_sys = p_access->p_sys;
1119     char           *psz ;
1120     v_socket_t     *pvs = p_sys->p_vs;
1121     p_sys->b_persist = false;
1122
1123     p_sys->i_remaining = 0;
1124     if( p_sys->b_proxy )
1125     {
1126         if( p_sys->url.psz_path )
1127         {
1128             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1129                         "GET http://%s:%d%s HTTP/1.%d\r\n",
1130                         p_sys->url.psz_host, p_sys->url.i_port,
1131                         p_sys->url.psz_path, p_sys->i_version );
1132         }
1133         else
1134         {
1135             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1136                         "GET http://%s:%d/ HTTP/1.%d\r\n",
1137                         p_sys->url.psz_host, p_sys->url.i_port,
1138                         p_sys->i_version );
1139         }
1140     }
1141     else
1142     {
1143         const char *psz_path = p_sys->url.psz_path;
1144         if( !psz_path || !*psz_path )
1145         {
1146             psz_path = "/";
1147         }
1148         if( p_sys->url.i_port != (pvs ? 443 : 80) )
1149         {
1150             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1151                         "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
1152                         psz_path, p_sys->i_version, p_sys->url.psz_host,
1153                         p_sys->url.i_port );
1154         }
1155         else
1156         {
1157             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1158                         "GET %s HTTP/1.%d\r\nHost: %s\r\n",
1159                         psz_path, p_sys->i_version, p_sys->url.psz_host );
1160         }
1161     }
1162     /* User Agent */
1163     net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
1164                 p_sys->psz_user_agent );
1165     /* Offset */
1166     if( p_sys->i_version == 1 && ! p_sys->b_continuous )
1167     {
1168         p_sys->b_persist = true;
1169         net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1170                     "Range: bytes=%"PRIu64"-\r\n", i_tell );
1171     }
1172
1173     /* Cookies */
1174     if( p_sys->cookies )
1175     {
1176         int i;
1177         for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1178         {
1179             const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1180             char * psz_cookie_content = cookie_get_content( cookie );
1181             char * psz_cookie_domain = cookie_get_domain( cookie );
1182
1183             assert( psz_cookie_content );
1184
1185             /* FIXME: This is clearly not conforming to the rfc */
1186             bool is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1187
1188             if( is_in_right_domain )
1189             {
1190                 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1191                 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1192                     msg_Err( p_access, "failed to send Cookie" );
1193             }
1194             free( psz_cookie_content );
1195             free( psz_cookie_domain );
1196         }
1197     }
1198
1199     /* Authentication */
1200     if( p_sys->url.psz_username || p_sys->url.psz_password )
1201         AuthReply( p_access, "", &p_sys->url, &p_sys->auth );
1202
1203     /* Proxy Authentication */
1204     if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1205         AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
1206
1207     /* ICY meta data request */
1208     net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1209
1210
1211     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
1212     {
1213         msg_Err( p_access, "failed to send request" );
1214         Disconnect( p_access );
1215         return VLC_EGENERIC;
1216     }
1217
1218     /* Read Answer */
1219     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
1220     {
1221         msg_Err( p_access, "failed to read answer" );
1222         goto error;
1223     }
1224     if( !strncmp( psz, "HTTP/1.", 7 ) )
1225     {
1226         p_sys->psz_protocol = "HTTP";
1227         p_sys->i_code = atoi( &psz[9] );
1228     }
1229     else if( !strncmp( psz, "ICY", 3 ) )
1230     {
1231         p_sys->psz_protocol = "ICY";
1232         p_sys->i_code = atoi( &psz[4] );
1233         p_sys->b_reconnect = true;
1234     }
1235     else
1236     {
1237         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1238         free( psz );
1239         goto error;
1240     }
1241     msg_Dbg( p_access, "protocol '%s' answer code %d",
1242              p_sys->psz_protocol, p_sys->i_code );
1243     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1244     {
1245         p_sys->b_seekable = false;
1246     }
1247     if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1248     {
1249         p_sys->b_seekable = false;
1250     }
1251     /* Authentication error - We'll have to display the dialog */
1252     if( p_sys->i_code == 401 )
1253     {
1254
1255     }
1256     /* Other fatal error */
1257     else if( p_sys->i_code >= 400 )
1258     {
1259         msg_Err( p_access, "error: %s", psz );
1260         free( psz );
1261         goto error;
1262     }
1263     free( psz );
1264
1265     for( ;; )
1266     {
1267         char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1268         char *p;
1269
1270         if( psz == NULL )
1271         {
1272             msg_Err( p_access, "failed to read answer" );
1273             goto error;
1274         }
1275
1276         if( !vlc_object_alive (p_access) || p_access->b_error )
1277         {
1278             free( psz );
1279             goto error;
1280         }
1281
1282         /* msg_Dbg( p_input, "Line=%s", psz ); */
1283         if( *psz == '\0' )
1284         {
1285             free( psz );
1286             break;
1287         }
1288
1289         if( ( p = strchr( psz, ':' ) ) == NULL )
1290         {
1291             msg_Err( p_access, "malformed header line: %s", psz );
1292             free( psz );
1293             goto error;
1294         }
1295         *p++ = '\0';
1296         while( *p == ' ' ) p++;
1297
1298         if( !strcasecmp( psz, "Content-Length" ) )
1299         {
1300             int64_t i_size = i_tell + (p_sys->i_remaining = atoll( p ));
1301             if(i_size > p_access->info.i_size) {
1302                 p_access->info.i_size = i_size;
1303             }
1304             msg_Dbg( p_access, "this frame size=%"PRId64, p_sys->i_remaining );
1305         }
1306         else if( !strcasecmp( psz, "Content-Range" ) ) {
1307             int64_t i_ntell = i_tell;
1308             int64_t i_nend = (p_access->info.i_size > 0)?(p_access->info.i_size - 1):i_tell;
1309             int64_t i_nsize = p_access->info.i_size;
1310             sscanf(p,"bytes %"PRId64"-%"PRId64"/%"PRId64,&i_ntell,&i_nend,&i_nsize);
1311             if(i_nend > i_ntell ) {
1312                 p_access->info.i_pos = i_ntell;
1313                 p_sys->i_remaining = i_nend+1-i_ntell;
1314                 int64_t i_size = (i_nsize > i_nend) ? i_nsize : (i_nend + 1);
1315                 if(i_size > p_access->info.i_size) {
1316                     p_access->info.i_size = i_size;
1317                 }
1318                 msg_Dbg( p_access, "stream size=%"PRId64",pos=%"PRId64",remaining=%"PRId64,i_nsize,i_ntell,p_sys->i_remaining);
1319             }
1320         }
1321         else if( !strcasecmp( psz, "Connection" ) ) {
1322             msg_Dbg( p_access, "Connection: %s",p );
1323             int i = -1;
1324             sscanf(p, "close%n",&i);
1325             if( i >= 0 ) {
1326                 p_sys->b_persist = false;
1327             }
1328         }
1329         else if( !strcasecmp( psz, "Location" ) )
1330         {
1331             char * psz_new_loc;
1332
1333             /* This does not follow RFC 2068, but yet if the url is not absolute,
1334              * handle it as everyone does. */
1335             if( p[0] == '/' )
1336             {
1337                 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1338
1339                 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1340                 {
1341                     if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1342                                  p_sys->url.psz_host, p) < 0 )
1343                         goto error;
1344                 }
1345                 else
1346                 {
1347                     if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1348                                  p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1349                         goto error;
1350                 }
1351             }
1352             else
1353             {
1354                 psz_new_loc = strdup( p );
1355             }
1356
1357             free( p_sys->psz_location );
1358             p_sys->psz_location = psz_new_loc;
1359         }
1360         else if( !strcasecmp( psz, "Content-Type" ) )
1361         {
1362             free( p_sys->psz_mime );
1363             p_sys->psz_mime = strdup( p );
1364             msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1365         }
1366         else if( !strcasecmp( psz, "Content-Encoding" ) )
1367         {
1368             msg_Dbg( p_access, "Content-Encoding: %s", p );
1369             if( strcasecmp( p, "identity" ) )
1370 #ifdef HAVE_ZLIB_H
1371                 p_sys->b_compressed = true;
1372 #else
1373                 msg_Warn( p_access, "Compressed content not supported. Rebuild with zlib support." );
1374 #endif
1375         }
1376         else if( !strcasecmp( psz, "Pragma" ) )
1377         {
1378             if( !strcasecmp( psz, "Pragma: features" ) )
1379                 p_sys->b_mms = true;
1380             free( p_sys->psz_pragma );
1381             p_sys->psz_pragma = strdup( p );
1382             msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1383         }
1384         else if( !strcasecmp( psz, "Server" ) )
1385         {
1386             msg_Dbg( p_access, "Server: %s", p );
1387             if( !strncasecmp( p, "Icecast", 7 ) ||
1388                 !strncasecmp( p, "Nanocaster", 10 ) )
1389             {
1390                 /* Remember if this is Icecast
1391                  * we need to force demux in this case without breaking
1392                  *  autodetection */
1393
1394                 /* Let live 365 streams (nanocaster) piggyback on the icecast
1395                  * routine. They look very similar */
1396
1397                 p_sys->b_reconnect = true;
1398                 p_sys->b_pace_control = false;
1399                 p_sys->b_icecast = true;
1400             }
1401         }
1402         else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1403         {
1404             msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1405             if( !strncasecmp( p, "chunked", 7 ) )
1406             {
1407                 p_sys->b_chunked = true;
1408             }
1409         }
1410         else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1411         {
1412             msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1413             p_sys->i_icy_meta = atoi( p );
1414             if( p_sys->i_icy_meta < 0 )
1415                 p_sys->i_icy_meta = 0;
1416             if( p_sys->i_icy_meta > 0 )
1417                 p_sys->b_icecast = true;
1418
1419             msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1420         }
1421         else if( !strcasecmp( psz, "Icy-Name" ) )
1422         {
1423             free( p_sys->psz_icy_name );
1424             p_sys->psz_icy_name = EnsureUTF8( strdup( p ));
1425             msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1426
1427             p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
1428             p_sys->b_reconnect = true;
1429             p_sys->b_pace_control = false;
1430         }
1431         else if( !strcasecmp( psz, "Icy-Genre" ) )
1432         {
1433             free( p_sys->psz_icy_genre );
1434             p_sys->psz_icy_genre = EnsureUTF8( strdup( p ));
1435             msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1436         }
1437         else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1438         {
1439             msg_Dbg( p_access, "Icy-Notice: %s", p );
1440         }
1441         else if( !strncasecmp( psz, "icy-", 4 ) ||
1442                  !strncasecmp( psz, "ice-", 4 ) ||
1443                  !strncasecmp( psz, "x-audiocast", 11 ) )
1444         {
1445             msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1446         }
1447         else if( !strcasecmp( psz, "Set-Cookie" ) )
1448         {
1449             if( p_sys->cookies )
1450             {
1451                 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1452                 cookie_append( p_sys->cookies, strdup(p) );
1453             }
1454             else
1455                 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1456         }
1457         else if( !strcasecmp( psz, "www-authenticate" ) )
1458         {
1459             msg_Dbg( p_access, "Authentication header: %s", p );
1460             AuthParseHeader( p_access, p, &p_sys->auth );
1461         }
1462         else if( !strcasecmp( psz, "proxy-authenticate" ) )
1463         {
1464             msg_Dbg( p_access, "Proxy authentication header: %s", p );
1465             AuthParseHeader( p_access, p, &p_sys->proxy_auth );
1466         }
1467         else if( !strcasecmp( psz, "authentication-info" ) )
1468         {
1469             msg_Dbg( p_access, "Authentication Info header: %s", p );
1470             if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
1471                 goto error;
1472         }
1473         else if( !strcasecmp( psz, "proxy-authentication-info" ) )
1474         {
1475             msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
1476             if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
1477                 goto error;
1478         }
1479
1480         free( psz );
1481     }
1482     /* We close the stream for zero length data, unless of course the
1483      * server has already promised to do this for us.
1484      */
1485     if( p_access->info.i_size != -1 && p_sys->i_remaining == 0 && p_sys->b_persist ) {
1486         Disconnect( p_access );
1487     }
1488     return VLC_SUCCESS;
1489
1490 error:
1491     Disconnect( p_access );
1492     return VLC_EGENERIC;
1493 }
1494
1495 /*****************************************************************************
1496  * Disconnect:
1497  *****************************************************************************/
1498 static void Disconnect( access_t *p_access )
1499 {
1500     access_sys_t *p_sys = p_access->p_sys;
1501
1502     if( p_sys->p_tls != NULL)
1503     {
1504         tls_ClientDelete( p_sys->p_tls );
1505         p_sys->p_tls = NULL;
1506         p_sys->p_vs = NULL;
1507     }
1508     if( p_sys->fd != -1)
1509     {
1510         net_Close(p_sys->fd);
1511         p_sys->fd = -1;
1512     }
1513
1514 }
1515
1516 /*****************************************************************************
1517  * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1518  * them) (FIXME: only support the "domain=" param)
1519  *****************************************************************************/
1520
1521 /* Get the NAME=VALUE part of the Cookie */
1522 static char * cookie_get_content( const char * cookie )
1523 {
1524     char * ret = strdup( cookie );
1525     if( !ret ) return NULL;
1526     char * str = ret;
1527     /* Look for a ';' */
1528     while( *str && *str != ';' ) str++;
1529     /* Replace it by a end-char */
1530     if( *str == ';' ) *str = 0;
1531     return ret;
1532 }
1533
1534 /* Get the domain where the cookie is stored */
1535 static char * cookie_get_domain( const char * cookie )
1536 {
1537     const char * str = cookie;
1538     static const char domain[] = "domain=";
1539     if( !str )
1540         return NULL;
1541     /* Look for a ';' */
1542     while( *str )
1543     {
1544         if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1545         {
1546             str += sizeof(domain) - 1 /* minus \0 */;
1547             char * ret = strdup( str );
1548             /* Now remove the next ';' if present */
1549             char * ret_iter = ret;
1550             while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1551             if( *ret_iter == ';' )
1552                 *ret_iter = 0;
1553             return ret;
1554         }
1555         /* Go to next ';' field */
1556         while( *str && *str != ';' ) str++;
1557         if( *str == ';' ) str++;
1558         /* skip blank */
1559         while( *str && *str == ' ' ) str++;
1560     }
1561     return NULL;
1562 }
1563
1564 /* Get NAME in the NAME=VALUE field */
1565 static char * cookie_get_name( const char * cookie )
1566 {
1567     char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1568     if( !ret ) return NULL;
1569     char * str = ret;
1570     while( *str && *str != '=' ) str++;
1571     *str = 0;
1572     return ret;
1573 }
1574
1575 /* Add a cookie in cookies, checking to see how it should be added */
1576 static void cookie_append( vlc_array_t * cookies, char * cookie )
1577 {
1578     int i;
1579
1580     if( !cookie )
1581         return;
1582
1583     char * cookie_name = cookie_get_name( cookie );
1584
1585     /* Don't send invalid cookies */
1586     if( !cookie_name )
1587         return;
1588
1589     char * cookie_domain = cookie_get_domain( cookie );
1590     for( i = 0; i < vlc_array_count( cookies ); i++ )
1591     {
1592         char * current_cookie = vlc_array_item_at_index( cookies, i );
1593         char * current_cookie_name = cookie_get_name( current_cookie );
1594         char * current_cookie_domain = cookie_get_domain( current_cookie );
1595
1596         assert( current_cookie_name );
1597
1598         bool is_domain_matching = ( cookie_domain && current_cookie_domain &&
1599                                          !strcmp( cookie_domain, current_cookie_domain ) );
1600
1601         if( is_domain_matching && !strcmp( cookie_name, current_cookie_name )  )
1602         {
1603             /* Remove previous value for this cookie */
1604             free( current_cookie );
1605             vlc_array_remove( cookies, i );
1606
1607             /* Clean */
1608             free( current_cookie_name );
1609             free( current_cookie_domain );
1610             break;
1611         }
1612         free( current_cookie_name );
1613         free( current_cookie_domain );
1614     }
1615     free( cookie_name );
1616     free( cookie_domain );
1617     vlc_array_append( cookies, cookie );
1618 }
1619
1620 /*****************************************************************************
1621  * "RFC 2617: Basic and Digest Access Authentication" header parsing
1622  *****************************************************************************/
1623 static char *AuthGetParam( const char *psz_header, const char *psz_param )
1624 {
1625     char psz_what[strlen(psz_param)+3];
1626     sprintf( psz_what, "%s=\"", psz_param );
1627     psz_header = strstr( psz_header, psz_what );
1628     if( psz_header )
1629     {
1630         const char *psz_end;
1631         psz_header += strlen( psz_what );
1632         psz_end = strchr( psz_header, '"' );
1633         if( !psz_end ) /* Invalid since we should have a closing quote */
1634             return strdup( psz_header );
1635         return strndup( psz_header, psz_end - psz_header );
1636     }
1637     else
1638     {
1639         return NULL;
1640     }
1641 }
1642
1643 static char *AuthGetParamNoQuotes( const char *psz_header, const char *psz_param )
1644 {
1645     char psz_what[strlen(psz_param)+2];
1646     sprintf( psz_what, "%s=", psz_param );
1647     psz_header = strstr( psz_header, psz_what );
1648     if( psz_header )
1649     {
1650         const char *psz_end;
1651         psz_header += strlen( psz_what );
1652         psz_end = strchr( psz_header, ',' );
1653         /* XXX: Do we need to filter out trailing space between the value and
1654          * the comma/end of line? */
1655         if( !psz_end ) /* Can be valid if this is the last parameter */
1656             return strdup( psz_header );
1657         return strndup( psz_header, psz_end - psz_header );
1658     }
1659     else
1660     {
1661         return NULL;
1662     }
1663 }
1664
1665 static void AuthParseHeader( access_t *p_access, const char *psz_header,
1666                              http_auth_t *p_auth )
1667 {
1668     /* FIXME: multiple auth methods can be listed (comma seperated) */
1669
1670     /* 2 Basic Authentication Scheme */
1671     if( !strncasecmp( psz_header, "Basic ", strlen( "Basic " ) ) )
1672     {
1673         msg_Dbg( p_access, "Using Basic Authentication" );
1674         psz_header += strlen( "Basic " );
1675         p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
1676         if( !p_auth->psz_realm )
1677             msg_Warn( p_access, "Basic Authentication: "
1678                       "Mandatory 'realm' parameter is missing" );
1679     }
1680     /* 3 Digest Access Authentication Scheme */
1681     else if( !strncasecmp( psz_header, "Digest ", strlen( "Digest " ) ) )
1682     {
1683         msg_Dbg( p_access, "Using Digest Access Authentication" );
1684         if( p_auth->psz_nonce ) return; /* FIXME */
1685         psz_header += strlen( "Digest " );
1686         p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
1687         p_auth->psz_domain = AuthGetParam( psz_header, "domain" );
1688         p_auth->psz_nonce = AuthGetParam( psz_header, "nonce" );
1689         p_auth->psz_opaque = AuthGetParam( psz_header, "opaque" );
1690         p_auth->psz_stale = AuthGetParamNoQuotes( psz_header, "stale" );
1691         p_auth->psz_algorithm = AuthGetParamNoQuotes( psz_header, "algorithm" );
1692         p_auth->psz_qop = AuthGetParam( psz_header, "qop" );
1693         p_auth->i_nonce = 0;
1694         /* printf("realm: |%s|\ndomain: |%s|\nnonce: |%s|\nopaque: |%s|\n"
1695                   "stale: |%s|\nalgorithm: |%s|\nqop: |%s|\n",
1696                   p_auth->psz_realm,p_auth->psz_domain,p_auth->psz_nonce,
1697                   p_auth->psz_opaque,p_auth->psz_stale,p_auth->psz_algorithm,
1698                   p_auth->psz_qop); */
1699         if( !p_auth->psz_realm )
1700             msg_Warn( p_access, "Digest Access Authentication: "
1701                       "Mandatory 'realm' parameter is missing" );
1702         if( !p_auth->psz_nonce )
1703             msg_Warn( p_access, "Digest Access Authentication: "
1704                       "Mandatory 'nonce' parameter is missing" );
1705         if( p_auth->psz_qop ) /* FIXME: parse the qop list */
1706         {
1707             char *psz_tmp = strchr( p_auth->psz_qop, ',' );
1708             if( psz_tmp ) *psz_tmp = '\0';
1709         }
1710     }
1711     else
1712     {
1713         const char *psz_end = strchr( psz_header, ' ' );
1714         if( psz_end )
1715             msg_Warn( p_access, "Unknown authentication scheme: '%*s'",
1716                       (int)(psz_end - psz_header), psz_header );
1717         else
1718             msg_Warn( p_access, "Unknown authentication scheme: '%s'",
1719                       psz_header );
1720     }
1721 }
1722
1723 static char *AuthDigest( access_t *p_access, vlc_url_t *p_url,
1724                          http_auth_t *p_auth, const char *psz_method )
1725 {
1726     (void)p_access;
1727     const char *psz_username = p_url->psz_username ?: "";
1728     const char *psz_password = p_url->psz_password ?: "";
1729
1730     char *psz_HA1 = NULL;
1731     char *psz_HA2 = NULL;
1732     char *psz_response = NULL;
1733     struct md5_s md5;
1734
1735     /* H(A1) */
1736     if( p_auth->psz_HA1 )
1737     {
1738         psz_HA1 = strdup( p_auth->psz_HA1 );
1739         if( !psz_HA1 ) goto error;
1740     }
1741     else
1742     {
1743         InitMD5( &md5 );
1744         AddMD5( &md5, psz_username, strlen( psz_username ) );
1745         AddMD5( &md5, ":", 1 );
1746         AddMD5( &md5, p_auth->psz_realm, strlen( p_auth->psz_realm ) );
1747         AddMD5( &md5, ":", 1 );
1748         AddMD5( &md5, psz_password, strlen( psz_password ) );
1749         EndMD5( &md5 );
1750
1751         psz_HA1 = psz_md5_hash( &md5 );
1752         if( !psz_HA1 ) goto error;
1753
1754         if( p_auth->psz_algorithm
1755             && !strcmp( p_auth->psz_algorithm, "MD5-sess" ) )
1756         {
1757             InitMD5( &md5 );
1758             AddMD5( &md5, psz_HA1, 32 );
1759             free( psz_HA1 );
1760             AddMD5( &md5, ":", 1 );
1761             AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
1762             AddMD5( &md5, ":", 1 );
1763             AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
1764             EndMD5( &md5 );
1765
1766             psz_HA1 = psz_md5_hash( &md5 );
1767             if( !psz_HA1 ) goto error;
1768             p_auth->psz_HA1 = strdup( psz_HA1 );
1769             if( !p_auth->psz_HA1 ) goto error;
1770         }
1771     }
1772
1773     /* H(A2) */
1774     InitMD5( &md5 );
1775     if( *psz_method )
1776         AddMD5( &md5, psz_method, strlen( psz_method ) );
1777     AddMD5( &md5, ":", 1 );
1778     if( p_url->psz_path )
1779         AddMD5( &md5, p_url->psz_path, strlen( p_url->psz_path ) );
1780     else
1781         AddMD5( &md5, "/", 1 );
1782     if( p_auth->psz_qop && !strcmp( p_auth->psz_qop, "auth-int" ) )
1783     {
1784         char *psz_ent;
1785         struct md5_s ent;
1786         InitMD5( &ent );
1787         AddMD5( &ent, "", 0 ); /* XXX: entity-body. should be ok for GET */
1788         EndMD5( &ent );
1789         psz_ent = psz_md5_hash( &ent );
1790         if( !psz_ent ) goto error;
1791         AddMD5( &md5, ":", 1 );
1792         AddMD5( &md5, psz_ent, 32 );
1793         free( psz_ent );
1794     }
1795     EndMD5( &md5 );
1796     psz_HA2 = psz_md5_hash( &md5 );
1797     if( !psz_HA2 ) goto error;
1798
1799     /* Request digest */
1800     InitMD5( &md5 );
1801     AddMD5( &md5, psz_HA1, 32 );
1802     AddMD5( &md5, ":", 1 );
1803     AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
1804     AddMD5( &md5, ":", 1 );
1805     if( p_auth->psz_qop
1806         && ( !strcmp( p_auth->psz_qop, "auth" )
1807              || !strcmp( p_auth->psz_qop, "auth-int" ) ) )
1808     {
1809         char psz_inonce[9];
1810         snprintf( psz_inonce, 9, "%08x", p_auth->i_nonce );
1811         AddMD5( &md5, psz_inonce, 8 );
1812         AddMD5( &md5, ":", 1 );
1813         AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
1814         AddMD5( &md5, ":", 1 );
1815         AddMD5( &md5, p_auth->psz_qop, strlen( p_auth->psz_qop ) );
1816         AddMD5( &md5, ":", 1 );
1817     }
1818     AddMD5( &md5, psz_HA2, 32 );
1819     EndMD5( &md5 );
1820     psz_response = psz_md5_hash( &md5 );
1821
1822     error:
1823         free( psz_HA1 );
1824         free( psz_HA2 );
1825         return psz_response;
1826 }
1827
1828
1829 static void AuthReply( access_t *p_access, const char *psz_prefix,
1830                        vlc_url_t *p_url, http_auth_t *p_auth )
1831 {
1832     access_sys_t *p_sys = p_access->p_sys;
1833     v_socket_t     *pvs = p_sys->p_vs;
1834
1835     const char *psz_username = p_url->psz_username ?: "";
1836     const char *psz_password = p_url->psz_password ?: "";
1837
1838     if( p_auth->psz_nonce )
1839     {
1840         /* Digest Access Authentication */
1841         char *psz_response;
1842
1843         if(    p_auth->psz_algorithm
1844             && strcmp( p_auth->psz_algorithm, "MD5" )
1845             && strcmp( p_auth->psz_algorithm, "MD5-sess" ) )
1846         {
1847             msg_Err( p_access, "Digest Access Authentication: "
1848                      "Unknown algorithm '%s'", p_auth->psz_algorithm );
1849             return;
1850         }
1851
1852         if( p_auth->psz_qop || !p_auth->psz_cnonce )
1853         {
1854             /* FIXME: needs to be really random to prevent man in the middle
1855              * attacks */
1856             free( p_auth->psz_cnonce );
1857             p_auth->psz_cnonce = strdup( "Some random string FIXME" );
1858         }
1859         p_auth->i_nonce ++;
1860
1861         psz_response = AuthDigest( p_access, p_url, p_auth, "GET" );
1862         if( !psz_response ) return;
1863
1864         net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1865                     "%sAuthorization: Digest "
1866                     /* Mandatory parameters */
1867                     "username=\"%s\", "
1868                     "realm=\"%s\", "
1869                     "nonce=\"%s\", "
1870                     "uri=\"%s\", "
1871                     "response=\"%s\", "
1872                     /* Optional parameters */
1873                     "%s%s%s" /* algorithm */
1874                     "%s%s%s" /* cnonce */
1875                     "%s%s%s" /* opaque */
1876                     "%s%s%s" /* message qop */
1877                     "%s%08x%s" /* nonce count */
1878                     "\r\n",
1879                     /* Mandatory parameters */
1880                     psz_prefix,
1881                     psz_username,
1882                     p_auth->psz_realm,
1883                     p_auth->psz_nonce,
1884                     p_url->psz_path ?: "/",
1885                     psz_response,
1886                     /* Optional parameters */
1887                     p_auth->psz_algorithm ? "algorithm=\"" : "",
1888                     p_auth->psz_algorithm ?: "",
1889                     p_auth->psz_algorithm ? "\", " : "",
1890                     p_auth->psz_cnonce ? "cnonce=\"" : "",
1891                     p_auth->psz_cnonce ?: "",
1892                     p_auth->psz_cnonce ? "\", " : "",
1893                     p_auth->psz_opaque ? "opaque=\"" : "",
1894                     p_auth->psz_opaque ?: "",
1895                     p_auth->psz_opaque ? "\", " : "",
1896                     p_auth->psz_qop ? "qop=\"" : "",
1897                     p_auth->psz_qop ?: "",
1898                     p_auth->psz_qop ? "\", " : "",
1899                     p_auth->i_nonce ? "nc=\"" : "uglyhack=\"", /* Will be parsed as an unhandled extension */
1900                     p_auth->i_nonce,
1901                     p_auth->i_nonce ? "\"" : "\""
1902                   );
1903
1904         free( psz_response );
1905     }
1906     else
1907     {
1908         /* Basic Access Authentication */
1909         char buf[strlen( psz_username ) + strlen( psz_password ) + 2];
1910         char *b64;
1911
1912         snprintf( buf, sizeof( buf ), "%s:%s", psz_username, psz_password );
1913         b64 = vlc_b64_encode( buf );
1914
1915         if( b64 != NULL )
1916         {
1917              net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1918                          "%sAuthorization: Basic %s\r\n", psz_prefix, b64 );
1919              free( b64 );
1920         }
1921     }
1922 }
1923
1924 static int AuthCheckReply( access_t *p_access, const char *psz_header,
1925                            vlc_url_t *p_url, http_auth_t *p_auth )
1926 {
1927     int i_ret = VLC_EGENERIC;
1928     char *psz_nextnonce = AuthGetParam( psz_header, "nextnonce" );
1929     char *psz_qop = AuthGetParamNoQuotes( psz_header, "qop" );
1930     char *psz_rspauth = AuthGetParam( psz_header, "rspauth" );
1931     char *psz_cnonce = AuthGetParam( psz_header, "cnonce" );
1932     char *psz_nc = AuthGetParamNoQuotes( psz_header, "nc" );
1933
1934     if( psz_cnonce )
1935     {
1936         char *psz_digest;
1937
1938         if( strcmp( psz_cnonce, p_auth->psz_cnonce ) )
1939         {
1940             msg_Err( p_access, "HTTP Digest Access Authentication: server replied with a different client nonce value." );
1941             goto error;
1942         }
1943
1944         if( psz_nc )
1945         {
1946             int i_nonce;
1947             i_nonce = strtol( psz_nc, NULL, 16 );
1948             if( i_nonce != p_auth->i_nonce )
1949             {
1950                 msg_Err( p_access, "HTTP Digest Access Authentication: server replied with a different nonce count value." );
1951                 goto error;
1952             }
1953         }
1954
1955         if( psz_qop && p_auth->psz_qop && strcmp( psz_qop, p_auth->psz_qop ) )
1956             msg_Warn( p_access, "HTTP Digest Access Authentication: server replied using a different 'quality of protection' option" );
1957
1958         /* All the clear text values match, let's now check the response
1959          * digest */
1960         psz_digest = AuthDigest( p_access, p_url, p_auth, "" );
1961         if( strcmp( psz_digest, psz_rspauth ) )
1962         {
1963             msg_Err( p_access, "HTTP Digest Access Authentication: server replied with an invalid response digest (expected value: %s).", psz_digest );
1964             free( psz_digest );
1965             goto error;
1966         }
1967         free( psz_digest );
1968     }
1969
1970     if( psz_nextnonce )
1971     {
1972         free( p_auth->psz_nonce );
1973         p_auth->psz_nonce = psz_nextnonce;
1974         psz_nextnonce = NULL;
1975     }
1976
1977     i_ret = VLC_SUCCESS;
1978     error:
1979         free( psz_nextnonce );
1980         free( psz_qop );
1981         free( psz_rspauth );
1982         free( psz_cnonce );
1983         free( psz_nc );
1984
1985     return i_ret;
1986 }
1987
1988 static void AuthReset( http_auth_t *p_auth )
1989 {
1990     FREENULL( p_auth->psz_realm );
1991     FREENULL( p_auth->psz_domain );
1992     FREENULL( p_auth->psz_nonce );
1993     FREENULL( p_auth->psz_opaque );
1994     FREENULL( p_auth->psz_stale );
1995     FREENULL( p_auth->psz_algorithm );
1996     FREENULL( p_auth->psz_qop );
1997     p_auth->i_nonce = 0;
1998     FREENULL( p_auth->psz_cnonce );
1999     FREENULL( p_auth->psz_HA1 );
2000 }