]> git.sesse.net Git - vlc/blob - modules/access/http.c
Do not print the password in the log. That's dangerous!
[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,
455 #if 1
456                      "yeah right, like we're going to print a password."
457 #else
458                      psz_password
459 #endif
460                 );
461             p_sys->url.psz_username = psz_login;
462             p_sys->url.psz_password = psz_password;
463             Disconnect( p_access );
464             goto connect;
465         }
466         else
467         {
468             free( psz_login );
469             free( psz_password );
470             goto error;
471         }
472     }
473
474     if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
475           p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
476         p_sys->psz_location && *p_sys->psz_location )
477     {
478         msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
479
480         /* Do not accept redirection outside of HTTP works */
481         if( strncmp( p_sys->psz_location, "http", 4 )
482          || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
483            && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
484         {
485             msg_Err( p_access, "insecure redirection ignored" );
486             goto error;
487         }
488         free( p_access->psz_path );
489         p_access->psz_path = strdup( p_sys->psz_location );
490         /* Clean up current Open() run */
491         vlc_UrlClean( &p_sys->url );
492         AuthReset( &p_sys->auth );
493         vlc_UrlClean( &p_sys->proxy );
494         free( p_sys->psz_proxy_passbuf );
495         AuthReset( &p_sys->proxy_auth );
496         free( p_sys->psz_mime );
497         free( p_sys->psz_pragma );
498         free( p_sys->psz_location );
499         free( p_sys->psz_user_agent );
500
501         Disconnect( p_access );
502         cookies = p_sys->cookies;
503         free( p_sys );
504
505         /* Do new Open() run with new data */
506         return OpenWithCookies( p_this, cookies );
507     }
508
509     if( p_sys->b_mms )
510     {
511         msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
512         goto error;
513     }
514
515     if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
516     {
517         if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
518         {
519             if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
520                 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
521             {
522                 free( p_access->psz_demux );
523                 p_access->psz_demux = strdup( "nsv" );
524             }
525             else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
526                      !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
527             {
528                 free( p_access->psz_demux );
529                 p_access->psz_demux = strdup( "m4a" );
530             }
531             else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
532             {
533                 free( p_access->psz_demux );
534                 p_access->psz_demux = strdup( "mp3" );
535             }
536
537             msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
538                       p_access->psz_demux );
539
540 #if 0       /* Doesn't work really well because of the pre-buffering in
541              * shoutcast servers (the buffer content will be sent as fast as
542              * possible). */
543             p_sys->b_pace_control = false;
544 #endif
545         }
546         else if( !p_sys->psz_mime )
547         {
548             free( p_access->psz_demux );
549             /* Shoutcast */
550             p_access->psz_demux = strdup( "mp3" );
551         }
552         /* else probably Ogg Vorbis */
553     }
554     else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
555              p_sys->psz_mime &&
556              !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
557     {
558         free( p_access->psz_demux );
559         /* Grrrr! detect ultravox server and force NSV demuxer */
560         p_access->psz_demux = strdup( "nsv" );
561     }
562     else if( !strcmp( p_access->psz_access, "itpc" ) )
563     {
564         free( p_access->psz_demux );
565         p_access->psz_demux = strdup( "podcast" );
566     }
567     else if( p_sys->psz_mime &&
568              !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
569              ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
570     {
571         free( p_access->psz_demux );
572         p_access->psz_demux = strdup( "xspf-open" );
573     }
574
575     if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
576
577     /* PTS delay */
578     var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
579
580     return VLC_SUCCESS;
581
582 error:
583     vlc_UrlClean( &p_sys->url );
584     vlc_UrlClean( &p_sys->proxy );
585     free( p_sys->psz_proxy_passbuf );
586     free( p_sys->psz_mime );
587     free( p_sys->psz_pragma );
588     free( p_sys->psz_location );
589     free( p_sys->psz_user_agent );
590
591     Disconnect( p_access );
592
593     if( p_sys->cookies )
594     {
595         int i;
596         for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
597             free(vlc_array_item_at_index( p_sys->cookies, i ));
598         vlc_array_destroy( p_sys->cookies );
599     }
600
601 #ifdef HAVE_ZLIB_H
602     inflateEnd( &p_sys->inflate.stream );
603 #endif
604     free( p_sys );
605     return VLC_EGENERIC;
606 }
607
608 /*****************************************************************************
609  * Close:
610  *****************************************************************************/
611 static void Close( vlc_object_t *p_this )
612 {
613     access_t     *p_access = (access_t*)p_this;
614     access_sys_t *p_sys = p_access->p_sys;
615
616     vlc_UrlClean( &p_sys->url );
617     AuthReset( &p_sys->auth );
618     vlc_UrlClean( &p_sys->proxy );
619     AuthReset( &p_sys->proxy_auth );
620
621     free( p_sys->psz_mime );
622     free( p_sys->psz_pragma );
623     free( p_sys->psz_location );
624
625     free( p_sys->psz_icy_name );
626     free( p_sys->psz_icy_genre );
627     free( p_sys->psz_icy_title );
628
629     free( p_sys->psz_user_agent );
630
631     Disconnect( p_access );
632
633     if( p_sys->cookies )
634     {
635         int i;
636         for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
637             free(vlc_array_item_at_index( p_sys->cookies, i ));
638         vlc_array_destroy( p_sys->cookies );
639     }
640
641 #ifdef HAVE_ZLIB_H
642     inflateEnd( &p_sys->inflate.stream );
643     free( p_sys->inflate.p_buffer );
644 #endif
645
646     free( p_sys );
647 }
648
649 /*****************************************************************************
650  * Read: Read up to i_len bytes from the http connection and place in
651  * p_buffer. Return the actual number of bytes read
652  *****************************************************************************/
653 static int ReadICYMeta( access_t *p_access );
654 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
655 {
656     access_sys_t *p_sys = p_access->p_sys;
657     int i_read;
658
659     if( p_sys->fd == -1 )
660     {
661         p_access->info.b_eof = true;
662         return 0;
663     }
664
665     if( p_access->info.i_size >= 0 &&
666         i_len + p_access->info.i_pos > p_access->info.i_size )
667     {
668         if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
669         {
670             p_access->info.b_eof = true;
671             return 0;
672         }
673     }
674
675     if( p_sys->b_chunked )
676     {
677         if( p_sys->i_chunk < 0 )
678         {
679             p_access->info.b_eof = true;
680             return 0;
681         }
682
683         if( p_sys->i_chunk <= 0 )
684         {
685             char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
686             /* read the chunk header */
687             if( psz == NULL )
688             {
689                 /* fatal error - end of file */
690                 msg_Dbg( p_access, "failed reading chunk-header line" );
691                 return 0;
692             }
693             p_sys->i_chunk = strtoll( psz, NULL, 16 );
694             free( psz );
695
696             if( p_sys->i_chunk <= 0 )   /* eof */
697             {
698                 p_sys->i_chunk = -1;
699                 p_access->info.b_eof = true;
700                 return 0;
701             }
702         }
703
704         if( i_len > p_sys->i_chunk )
705         {
706             i_len = p_sys->i_chunk;
707         }
708     }
709     else if( p_access->info.i_size != -1 && (int64_t)i_len > p_sys->i_remaining) {
710         /* Only ask for the remaining length */
711         i_len = (size_t)p_sys->i_remaining;
712         if(i_len == 0) {
713             p_access->info.b_eof = true;
714             return 0;
715         }
716     }
717
718
719     if( p_sys->i_icy_meta > 0 && p_access->info.i_pos-p_sys->i_icy_offset > 0 )
720     {
721         int64_t i_next = p_sys->i_icy_meta -
722                                     (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
723
724         if( i_next == p_sys->i_icy_meta )
725         {
726             if( ReadICYMeta( p_access ) )
727             {
728                 p_access->info.b_eof = true;
729                 return -1;
730             }
731         }
732         if( i_len > i_next )
733             i_len = i_next;
734     }
735
736     i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
737
738     if( i_read > 0 )
739     {
740         p_access->info.i_pos += i_read;
741
742         if( p_sys->b_chunked )
743         {
744             p_sys->i_chunk -= i_read;
745             if( p_sys->i_chunk <= 0 )
746             {
747                 /* read the empty line */
748                 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
749                 free( psz );
750             }
751         }
752     }
753     else if( i_read == 0 )
754     {
755         /*
756          * I very much doubt that this will work.
757          * If i_read == 0, the connection *IS* dead, so the only
758          * sensible thing to do is Disconnect() and then retry.
759          * Otherwise, I got recv() completely wrong. -- Courmisch
760          */
761         if( p_sys->b_continuous )
762         {
763             Request( p_access, 0 );
764             p_sys->b_continuous = false;
765             i_read = Read( p_access, p_buffer, i_len );
766             p_sys->b_continuous = true;
767         }
768         Disconnect( p_access );
769         if( p_sys->b_reconnect )
770         {
771             msg_Dbg( p_access, "got disconnected, trying to reconnect" );
772             if( Connect( p_access, p_access->info.i_pos ) )
773             {
774                 msg_Dbg( p_access, "reconnection failed" );
775             }
776             else
777             {
778                 p_sys->b_reconnect = false;
779                 i_read = Read( p_access, p_buffer, i_len );
780                 p_sys->b_reconnect = true;
781             }
782         }
783
784         if( i_read == 0 ) p_access->info.b_eof = true;
785     }
786
787     if( p_access->info.i_size != -1 )
788     {
789         p_sys->i_remaining -= i_read;
790     }
791
792     return i_read;
793 }
794
795 static int ReadICYMeta( access_t *p_access )
796 {
797     access_sys_t *p_sys = p_access->p_sys;
798
799     uint8_t buffer;
800     char *p, *psz_meta;
801     int i_read;
802
803     /* Read meta data length */
804     i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
805                        true );
806     if( i_read <= 0 )
807         return VLC_EGENERIC;
808     if( buffer == 0 )
809         return VLC_SUCCESS;
810
811     i_read = buffer << 4;
812     /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
813
814     psz_meta = malloc( i_read + 1 );
815     if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
816                   (uint8_t *)psz_meta, i_read, true ) != i_read )
817         return VLC_EGENERIC;
818
819     psz_meta[i_read] = '\0'; /* Just in case */
820
821     /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
822
823     /* Now parse the meta */
824     /* Look for StreamTitle= */
825     p = strcasestr( (char *)psz_meta, "StreamTitle=" );
826     if( p )
827     {
828         p += strlen( "StreamTitle=" );
829         if( *p == '\'' || *p == '"' )
830         {
831             char closing[] = { p[0], ';', '\0' };
832             char *psz = strstr( &p[1], closing );
833             if( !psz )
834                 psz = strchr( &p[1], ';' );
835
836             if( psz ) *psz = '\0';
837         }
838         else
839         {
840             char *psz = strchr( &p[1], ';' );
841             if( psz ) *psz = '\0';
842         }
843
844         if( !p_sys->psz_icy_title ||
845             strcmp( p_sys->psz_icy_title, &p[1] ) )
846         {
847             free( p_sys->psz_icy_title );
848             p_sys->psz_icy_title = EnsureUTF8( strdup( &p[1] ));
849             p_access->info.i_update |= INPUT_UPDATE_META;
850
851             msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
852         }
853     }
854     free( psz_meta );
855
856     return VLC_SUCCESS;
857 }
858
859 #ifdef HAVE_ZLIB_H
860 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
861                                size_t i_len )
862 {
863     access_sys_t *p_sys = p_access->p_sys;
864
865     if( p_sys->b_compressed )
866     {
867         int i_ret;
868
869         if( !p_sys->inflate.p_buffer )
870             p_sys->inflate.p_buffer = malloc( 256 * 1024 );
871
872         if( p_sys->inflate.stream.avail_in == 0 )
873         {
874             ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer + p_sys->inflate.stream.avail_in, 256 * 1024 );
875             if( i_read <= 0 ) return i_read;
876             p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
877             p_sys->inflate.stream.avail_in = i_read;
878         }
879
880         p_sys->inflate.stream.avail_out = i_len;
881         p_sys->inflate.stream.next_out = p_buffer;
882
883         i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
884         msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
885
886         return i_len - p_sys->inflate.stream.avail_out;
887     }
888     else
889     {
890         return Read( p_access, p_buffer, i_len );
891     }
892 }
893 #endif
894
895 /*****************************************************************************
896  * Seek: close and re-open a connection at the right place
897  *****************************************************************************/
898 static int Seek( access_t *p_access, int64_t i_pos )
899 {
900     msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
901
902     Disconnect( p_access );
903
904     if( p_access->info.i_size
905      && (uint64_t)i_pos >= (uint64_t)p_access->info.i_size ) {
906         msg_Err( p_access, "seek to far" );
907         int retval = Seek( p_access, p_access->info.i_size - 1 );
908         if( retval == VLC_SUCCESS ) {
909             uint8_t p_buffer[2];
910             Read( p_access, p_buffer, 1);
911             p_access->info.b_eof  = false;
912         }
913         return retval;
914     }
915     if( Connect( p_access, i_pos ) )
916     {
917         msg_Err( p_access, "seek failed" );
918         p_access->info.b_eof = true;
919         return VLC_EGENERIC;
920     }
921     return VLC_SUCCESS;
922 }
923
924 /*****************************************************************************
925  * Control:
926  *****************************************************************************/
927 static int Control( access_t *p_access, int i_query, va_list args )
928 {
929     access_sys_t *p_sys = p_access->p_sys;
930     bool       *pb_bool;
931     int64_t    *pi_64;
932     vlc_meta_t *p_meta;
933
934     switch( i_query )
935     {
936         /* */
937         case ACCESS_CAN_SEEK:
938             pb_bool = (bool*)va_arg( args, bool* );
939             *pb_bool = p_sys->b_seekable;
940             break;
941         case ACCESS_CAN_FASTSEEK:
942             pb_bool = (bool*)va_arg( args, bool* );
943             *pb_bool = false;
944             break;
945         case ACCESS_CAN_PAUSE:
946         case ACCESS_CAN_CONTROL_PACE:
947             pb_bool = (bool*)va_arg( args, bool* );
948
949 #if 0       /* Disable for now until we have a clock synchro algo
950              * which works with something else than MPEG over UDP */
951             *pb_bool = p_sys->b_pace_control;
952 #endif
953             *pb_bool = true;
954             break;
955
956         /* */
957         case ACCESS_GET_PTS_DELAY:
958             pi_64 = (int64_t*)va_arg( args, int64_t * );
959             *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
960             break;
961
962         /* */
963         case ACCESS_SET_PAUSE_STATE:
964             break;
965
966         case ACCESS_GET_META:
967             p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
968
969             if( p_sys->psz_icy_name )
970                 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
971             if( p_sys->psz_icy_genre )
972                 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
973             if( p_sys->psz_icy_title )
974                 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
975             break;
976
977         case ACCESS_GET_CONTENT_TYPE:
978             *va_arg( args, char ** ) =
979                 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
980             break;
981
982         case ACCESS_GET_TITLE_INFO:
983         case ACCESS_SET_TITLE:
984         case ACCESS_SET_SEEKPOINT:
985         case ACCESS_SET_PRIVATE_ID_STATE:
986             return VLC_EGENERIC;
987
988         default:
989             msg_Warn( p_access, "unimplemented query in control" );
990             return VLC_EGENERIC;
991
992     }
993     return VLC_SUCCESS;
994 }
995
996 /*****************************************************************************
997  * Connect:
998  *****************************************************************************/
999 static int Connect( access_t *p_access, int64_t i_tell )
1000 {
1001     access_sys_t   *p_sys = p_access->p_sys;
1002     vlc_url_t      srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
1003
1004     /* Clean info */
1005     free( p_sys->psz_location );
1006     free( p_sys->psz_mime );
1007     free( p_sys->psz_pragma );
1008
1009     free( p_sys->psz_icy_genre );
1010     free( p_sys->psz_icy_name );
1011     free( p_sys->psz_icy_title );
1012
1013
1014     p_sys->psz_location = NULL;
1015     p_sys->psz_mime = NULL;
1016     p_sys->psz_pragma = NULL;
1017     p_sys->b_mms = false;
1018     p_sys->b_chunked = false;
1019     p_sys->i_chunk = 0;
1020     p_sys->i_icy_meta = 0;
1021     p_sys->i_icy_offset = i_tell;
1022     p_sys->psz_icy_name = NULL;
1023     p_sys->psz_icy_genre = NULL;
1024     p_sys->psz_icy_title = NULL;
1025     p_sys->i_remaining = 0;
1026     p_sys->b_persist = false;
1027
1028     p_access->info.i_size = -1;
1029     p_access->info.i_pos  = i_tell;
1030     p_access->info.b_eof  = false;
1031
1032     /* Open connection */
1033     assert( p_sys->fd == -1 ); /* No open sockets (leaking fds is BAD) */
1034     p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
1035     if( p_sys->fd == -1 )
1036     {
1037         msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
1038         return -1;
1039     }
1040     setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));
1041
1042     /* Initialize TLS/SSL session */
1043     if( p_sys->b_ssl == true )
1044     {
1045         /* CONNECT to establish TLS tunnel through HTTP proxy */
1046         if( p_sys->b_proxy )
1047         {
1048             char *psz;
1049             unsigned i_status = 0;
1050
1051             if( p_sys->i_version == 0 )
1052             {
1053                 /* CONNECT is not in HTTP/1.0 */
1054                 Disconnect( p_access );
1055                 return -1;
1056             }
1057
1058             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1059                         "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
1060                         p_sys->url.psz_host, p_sys->url.i_port,
1061                         p_sys->i_version,
1062                         p_sys->url.psz_host, p_sys->url.i_port);
1063
1064             psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
1065             if( psz == NULL )
1066             {
1067                 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
1068                 Disconnect( p_access );
1069                 return -1;
1070             }
1071
1072             sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
1073             free( psz );
1074
1075             if( ( i_status / 100 ) != 2 )
1076             {
1077                 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
1078                 Disconnect( p_access );
1079                 return -1;
1080             }
1081
1082             do
1083             {
1084                 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
1085                 if( psz == NULL )
1086                 {
1087                     msg_Err( p_access, "HTTP proxy connection failed" );
1088                     Disconnect( p_access );
1089                     return -1;
1090                 }
1091
1092                 if( *psz == '\0' )
1093                     i_status = 0;
1094
1095                 free( psz );
1096
1097                 if( !vlc_object_alive (p_access) || p_access->b_error )
1098                 {
1099                     Disconnect( p_access );
1100                     return -1;
1101                 }
1102             }
1103             while( i_status );
1104         }
1105
1106         /* TLS/SSL handshake */
1107         p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
1108                                          srv.psz_host );
1109         if( p_sys->p_tls == NULL )
1110         {
1111             msg_Err( p_access, "cannot establish HTTP/TLS session" );
1112             Disconnect( p_access );
1113             return -1;
1114         }
1115         p_sys->p_vs = &p_sys->p_tls->sock;
1116     }
1117
1118     return Request( p_access, i_tell ) ? -2 : 0;
1119 }
1120
1121
1122 static int Request( access_t *p_access, int64_t i_tell )
1123 {
1124     access_sys_t   *p_sys = p_access->p_sys;
1125     char           *psz ;
1126     v_socket_t     *pvs = p_sys->p_vs;
1127     p_sys->b_persist = false;
1128
1129     p_sys->i_remaining = 0;
1130     if( p_sys->b_proxy )
1131     {
1132         if( p_sys->url.psz_path )
1133         {
1134             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1135                         "GET http://%s:%d%s HTTP/1.%d\r\n",
1136                         p_sys->url.psz_host, p_sys->url.i_port,
1137                         p_sys->url.psz_path, p_sys->i_version );
1138         }
1139         else
1140         {
1141             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1142                         "GET http://%s:%d/ HTTP/1.%d\r\n",
1143                         p_sys->url.psz_host, p_sys->url.i_port,
1144                         p_sys->i_version );
1145         }
1146     }
1147     else
1148     {
1149         const char *psz_path = p_sys->url.psz_path;
1150         if( !psz_path || !*psz_path )
1151         {
1152             psz_path = "/";
1153         }
1154         if( p_sys->url.i_port != (pvs ? 443 : 80) )
1155         {
1156             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1157                         "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
1158                         psz_path, p_sys->i_version, p_sys->url.psz_host,
1159                         p_sys->url.i_port );
1160         }
1161         else
1162         {
1163             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1164                         "GET %s HTTP/1.%d\r\nHost: %s\r\n",
1165                         psz_path, p_sys->i_version, p_sys->url.psz_host );
1166         }
1167     }
1168     /* User Agent */
1169     net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
1170                 p_sys->psz_user_agent );
1171     /* Offset */
1172     if( p_sys->i_version == 1 && ! p_sys->b_continuous )
1173     {
1174         p_sys->b_persist = true;
1175         net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1176                     "Range: bytes=%"PRIu64"-\r\n", i_tell );
1177     }
1178
1179     /* Cookies */
1180     if( p_sys->cookies )
1181     {
1182         int i;
1183         for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1184         {
1185             const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1186             char * psz_cookie_content = cookie_get_content( cookie );
1187             char * psz_cookie_domain = cookie_get_domain( cookie );
1188
1189             assert( psz_cookie_content );
1190
1191             /* FIXME: This is clearly not conforming to the rfc */
1192             bool is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1193
1194             if( is_in_right_domain )
1195             {
1196                 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1197                 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1198                     msg_Err( p_access, "failed to send Cookie" );
1199             }
1200             free( psz_cookie_content );
1201             free( psz_cookie_domain );
1202         }
1203     }
1204
1205     /* Authentication */
1206     if( p_sys->url.psz_username || p_sys->url.psz_password )
1207         AuthReply( p_access, "", &p_sys->url, &p_sys->auth );
1208
1209     /* Proxy Authentication */
1210     if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1211         AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
1212
1213     /* ICY meta data request */
1214     net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1215
1216
1217     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
1218     {
1219         msg_Err( p_access, "failed to send request" );
1220         Disconnect( p_access );
1221         return VLC_EGENERIC;
1222     }
1223
1224     /* Read Answer */
1225     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
1226     {
1227         msg_Err( p_access, "failed to read answer" );
1228         goto error;
1229     }
1230     if( !strncmp( psz, "HTTP/1.", 7 ) )
1231     {
1232         p_sys->psz_protocol = "HTTP";
1233         p_sys->i_code = atoi( &psz[9] );
1234     }
1235     else if( !strncmp( psz, "ICY", 3 ) )
1236     {
1237         p_sys->psz_protocol = "ICY";
1238         p_sys->i_code = atoi( &psz[4] );
1239         p_sys->b_reconnect = true;
1240     }
1241     else
1242     {
1243         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1244         free( psz );
1245         goto error;
1246     }
1247     msg_Dbg( p_access, "protocol '%s' answer code %d",
1248              p_sys->psz_protocol, p_sys->i_code );
1249     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1250     {
1251         p_sys->b_seekable = false;
1252     }
1253     if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1254     {
1255         p_sys->b_seekable = false;
1256     }
1257     /* Authentication error - We'll have to display the dialog */
1258     if( p_sys->i_code == 401 )
1259     {
1260
1261     }
1262     /* Other fatal error */
1263     else if( p_sys->i_code >= 400 )
1264     {
1265         msg_Err( p_access, "error: %s", psz );
1266         free( psz );
1267         goto error;
1268     }
1269     free( psz );
1270
1271     for( ;; )
1272     {
1273         char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1274         char *p;
1275
1276         if( psz == NULL )
1277         {
1278             msg_Err( p_access, "failed to read answer" );
1279             goto error;
1280         }
1281
1282         if( !vlc_object_alive (p_access) || p_access->b_error )
1283         {
1284             free( psz );
1285             goto error;
1286         }
1287
1288         /* msg_Dbg( p_input, "Line=%s", psz ); */
1289         if( *psz == '\0' )
1290         {
1291             free( psz );
1292             break;
1293         }
1294
1295         if( ( p = strchr( psz, ':' ) ) == NULL )
1296         {
1297             msg_Err( p_access, "malformed header line: %s", psz );
1298             free( psz );
1299             goto error;
1300         }
1301         *p++ = '\0';
1302         while( *p == ' ' ) p++;
1303
1304         if( !strcasecmp( psz, "Content-Length" ) )
1305         {
1306             int64_t i_size = i_tell + (p_sys->i_remaining = atoll( p ));
1307             if(i_size > p_access->info.i_size) {
1308                 p_access->info.i_size = i_size;
1309             }
1310             msg_Dbg( p_access, "this frame size=%"PRId64, p_sys->i_remaining );
1311         }
1312         else if( !strcasecmp( psz, "Content-Range" ) ) {
1313             int64_t i_ntell = i_tell;
1314             int64_t i_nend = (p_access->info.i_size > 0)?(p_access->info.i_size - 1):i_tell;
1315             int64_t i_nsize = p_access->info.i_size;
1316             sscanf(p,"bytes %"PRId64"-%"PRId64"/%"PRId64,&i_ntell,&i_nend,&i_nsize);
1317             if(i_nend > i_ntell ) {
1318                 p_access->info.i_pos = i_ntell;
1319                 p_sys->i_remaining = i_nend+1-i_ntell;
1320                 int64_t i_size = (i_nsize > i_nend) ? i_nsize : (i_nend + 1);
1321                 if(i_size > p_access->info.i_size) {
1322                     p_access->info.i_size = i_size;
1323                 }
1324                 msg_Dbg( p_access, "stream size=%"PRId64",pos=%"PRId64",remaining=%"PRId64,i_nsize,i_ntell,p_sys->i_remaining);
1325             }
1326         }
1327         else if( !strcasecmp( psz, "Connection" ) ) {
1328             msg_Dbg( p_access, "Connection: %s",p );
1329             int i = -1;
1330             sscanf(p, "close%n",&i);
1331             if( i >= 0 ) {
1332                 p_sys->b_persist = false;
1333             }
1334         }
1335         else if( !strcasecmp( psz, "Location" ) )
1336         {
1337             char * psz_new_loc;
1338
1339             /* This does not follow RFC 2068, but yet if the url is not absolute,
1340              * handle it as everyone does. */
1341             if( p[0] == '/' )
1342             {
1343                 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1344
1345                 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1346                 {
1347                     if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1348                                  p_sys->url.psz_host, p) < 0 )
1349                         goto error;
1350                 }
1351                 else
1352                 {
1353                     if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1354                                  p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1355                         goto error;
1356                 }
1357             }
1358             else
1359             {
1360                 psz_new_loc = strdup( p );
1361             }
1362
1363             free( p_sys->psz_location );
1364             p_sys->psz_location = psz_new_loc;
1365         }
1366         else if( !strcasecmp( psz, "Content-Type" ) )
1367         {
1368             free( p_sys->psz_mime );
1369             p_sys->psz_mime = strdup( p );
1370             msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1371         }
1372         else if( !strcasecmp( psz, "Content-Encoding" ) )
1373         {
1374             msg_Dbg( p_access, "Content-Encoding: %s", p );
1375             if( strcasecmp( p, "identity" ) )
1376 #ifdef HAVE_ZLIB_H
1377                 p_sys->b_compressed = true;
1378 #else
1379                 msg_Warn( p_access, "Compressed content not supported. Rebuild with zlib support." );
1380 #endif
1381         }
1382         else if( !strcasecmp( psz, "Pragma" ) )
1383         {
1384             if( !strcasecmp( psz, "Pragma: features" ) )
1385                 p_sys->b_mms = true;
1386             free( p_sys->psz_pragma );
1387             p_sys->psz_pragma = strdup( p );
1388             msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1389         }
1390         else if( !strcasecmp( psz, "Server" ) )
1391         {
1392             msg_Dbg( p_access, "Server: %s", p );
1393             if( !strncasecmp( p, "Icecast", 7 ) ||
1394                 !strncasecmp( p, "Nanocaster", 10 ) )
1395             {
1396                 /* Remember if this is Icecast
1397                  * we need to force demux in this case without breaking
1398                  *  autodetection */
1399
1400                 /* Let live 365 streams (nanocaster) piggyback on the icecast
1401                  * routine. They look very similar */
1402
1403                 p_sys->b_reconnect = true;
1404                 p_sys->b_pace_control = false;
1405                 p_sys->b_icecast = true;
1406             }
1407         }
1408         else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1409         {
1410             msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1411             if( !strncasecmp( p, "chunked", 7 ) )
1412             {
1413                 p_sys->b_chunked = true;
1414             }
1415         }
1416         else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1417         {
1418             msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1419             p_sys->i_icy_meta = atoi( p );
1420             if( p_sys->i_icy_meta < 0 )
1421                 p_sys->i_icy_meta = 0;
1422             if( p_sys->i_icy_meta > 0 )
1423                 p_sys->b_icecast = true;
1424
1425             msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1426         }
1427         else if( !strcasecmp( psz, "Icy-Name" ) )
1428         {
1429             free( p_sys->psz_icy_name );
1430             p_sys->psz_icy_name = EnsureUTF8( strdup( p ));
1431             msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1432
1433             p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
1434             p_sys->b_reconnect = true;
1435             p_sys->b_pace_control = false;
1436         }
1437         else if( !strcasecmp( psz, "Icy-Genre" ) )
1438         {
1439             free( p_sys->psz_icy_genre );
1440             p_sys->psz_icy_genre = EnsureUTF8( strdup( p ));
1441             msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1442         }
1443         else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1444         {
1445             msg_Dbg( p_access, "Icy-Notice: %s", p );
1446         }
1447         else if( !strncasecmp( psz, "icy-", 4 ) ||
1448                  !strncasecmp( psz, "ice-", 4 ) ||
1449                  !strncasecmp( psz, "x-audiocast", 11 ) )
1450         {
1451             msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1452         }
1453         else if( !strcasecmp( psz, "Set-Cookie" ) )
1454         {
1455             if( p_sys->cookies )
1456             {
1457                 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1458                 cookie_append( p_sys->cookies, strdup(p) );
1459             }
1460             else
1461                 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1462         }
1463         else if( !strcasecmp( psz, "www-authenticate" ) )
1464         {
1465             msg_Dbg( p_access, "Authentication header: %s", p );
1466             AuthParseHeader( p_access, p, &p_sys->auth );
1467         }
1468         else if( !strcasecmp( psz, "proxy-authenticate" ) )
1469         {
1470             msg_Dbg( p_access, "Proxy authentication header: %s", p );
1471             AuthParseHeader( p_access, p, &p_sys->proxy_auth );
1472         }
1473         else if( !strcasecmp( psz, "authentication-info" ) )
1474         {
1475             msg_Dbg( p_access, "Authentication Info header: %s", p );
1476             if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
1477                 goto error;
1478         }
1479         else if( !strcasecmp( psz, "proxy-authentication-info" ) )
1480         {
1481             msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
1482             if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
1483                 goto error;
1484         }
1485
1486         free( psz );
1487     }
1488     /* We close the stream for zero length data, unless of course the
1489      * server has already promised to do this for us.
1490      */
1491     if( p_access->info.i_size != -1 && p_sys->i_remaining == 0 && p_sys->b_persist ) {
1492         Disconnect( p_access );
1493     }
1494     return VLC_SUCCESS;
1495
1496 error:
1497     Disconnect( p_access );
1498     return VLC_EGENERIC;
1499 }
1500
1501 /*****************************************************************************
1502  * Disconnect:
1503  *****************************************************************************/
1504 static void Disconnect( access_t *p_access )
1505 {
1506     access_sys_t *p_sys = p_access->p_sys;
1507
1508     if( p_sys->p_tls != NULL)
1509     {
1510         tls_ClientDelete( p_sys->p_tls );
1511         p_sys->p_tls = NULL;
1512         p_sys->p_vs = NULL;
1513     }
1514     if( p_sys->fd != -1)
1515     {
1516         net_Close(p_sys->fd);
1517         p_sys->fd = -1;
1518     }
1519
1520 }
1521
1522 /*****************************************************************************
1523  * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1524  * them) (FIXME: only support the "domain=" param)
1525  *****************************************************************************/
1526
1527 /* Get the NAME=VALUE part of the Cookie */
1528 static char * cookie_get_content( const char * cookie )
1529 {
1530     char * ret = strdup( cookie );
1531     if( !ret ) return NULL;
1532     char * str = ret;
1533     /* Look for a ';' */
1534     while( *str && *str != ';' ) str++;
1535     /* Replace it by a end-char */
1536     if( *str == ';' ) *str = 0;
1537     return ret;
1538 }
1539
1540 /* Get the domain where the cookie is stored */
1541 static char * cookie_get_domain( const char * cookie )
1542 {
1543     const char * str = cookie;
1544     static const char domain[] = "domain=";
1545     if( !str )
1546         return NULL;
1547     /* Look for a ';' */
1548     while( *str )
1549     {
1550         if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1551         {
1552             str += sizeof(domain) - 1 /* minus \0 */;
1553             char * ret = strdup( str );
1554             /* Now remove the next ';' if present */
1555             char * ret_iter = ret;
1556             while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1557             if( *ret_iter == ';' )
1558                 *ret_iter = 0;
1559             return ret;
1560         }
1561         /* Go to next ';' field */
1562         while( *str && *str != ';' ) str++;
1563         if( *str == ';' ) str++;
1564         /* skip blank */
1565         while( *str && *str == ' ' ) str++;
1566     }
1567     return NULL;
1568 }
1569
1570 /* Get NAME in the NAME=VALUE field */
1571 static char * cookie_get_name( const char * cookie )
1572 {
1573     char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1574     if( !ret ) return NULL;
1575     char * str = ret;
1576     while( *str && *str != '=' ) str++;
1577     *str = 0;
1578     return ret;
1579 }
1580
1581 /* Add a cookie in cookies, checking to see how it should be added */
1582 static void cookie_append( vlc_array_t * cookies, char * cookie )
1583 {
1584     int i;
1585
1586     if( !cookie )
1587         return;
1588
1589     char * cookie_name = cookie_get_name( cookie );
1590
1591     /* Don't send invalid cookies */
1592     if( !cookie_name )
1593         return;
1594
1595     char * cookie_domain = cookie_get_domain( cookie );
1596     for( i = 0; i < vlc_array_count( cookies ); i++ )
1597     {
1598         char * current_cookie = vlc_array_item_at_index( cookies, i );
1599         char * current_cookie_name = cookie_get_name( current_cookie );
1600         char * current_cookie_domain = cookie_get_domain( current_cookie );
1601
1602         assert( current_cookie_name );
1603
1604         bool is_domain_matching = ( cookie_domain && current_cookie_domain &&
1605                                          !strcmp( cookie_domain, current_cookie_domain ) );
1606
1607         if( is_domain_matching && !strcmp( cookie_name, current_cookie_name )  )
1608         {
1609             /* Remove previous value for this cookie */
1610             free( current_cookie );
1611             vlc_array_remove( cookies, i );
1612
1613             /* Clean */
1614             free( current_cookie_name );
1615             free( current_cookie_domain );
1616             break;
1617         }
1618         free( current_cookie_name );
1619         free( current_cookie_domain );
1620     }
1621     free( cookie_name );
1622     free( cookie_domain );
1623     vlc_array_append( cookies, cookie );
1624 }
1625
1626 /*****************************************************************************
1627  * "RFC 2617: Basic and Digest Access Authentication" header parsing
1628  *****************************************************************************/
1629 static char *AuthGetParam( const char *psz_header, const char *psz_param )
1630 {
1631     char psz_what[strlen(psz_param)+3];
1632     sprintf( psz_what, "%s=\"", psz_param );
1633     psz_header = strstr( psz_header, psz_what );
1634     if( psz_header )
1635     {
1636         const char *psz_end;
1637         psz_header += strlen( psz_what );
1638         psz_end = strchr( psz_header, '"' );
1639         if( !psz_end ) /* Invalid since we should have a closing quote */
1640             return strdup( psz_header );
1641         return strndup( psz_header, psz_end - psz_header );
1642     }
1643     else
1644     {
1645         return NULL;
1646     }
1647 }
1648
1649 static char *AuthGetParamNoQuotes( const char *psz_header, const char *psz_param )
1650 {
1651     char psz_what[strlen(psz_param)+2];
1652     sprintf( psz_what, "%s=", psz_param );
1653     psz_header = strstr( psz_header, psz_what );
1654     if( psz_header )
1655     {
1656         const char *psz_end;
1657         psz_header += strlen( psz_what );
1658         psz_end = strchr( psz_header, ',' );
1659         /* XXX: Do we need to filter out trailing space between the value and
1660          * the comma/end of line? */
1661         if( !psz_end ) /* Can be valid if this is the last parameter */
1662             return strdup( psz_header );
1663         return strndup( psz_header, psz_end - psz_header );
1664     }
1665     else
1666     {
1667         return NULL;
1668     }
1669 }
1670
1671 static void AuthParseHeader( access_t *p_access, const char *psz_header,
1672                              http_auth_t *p_auth )
1673 {
1674     /* FIXME: multiple auth methods can be listed (comma seperated) */
1675
1676     /* 2 Basic Authentication Scheme */
1677     if( !strncasecmp( psz_header, "Basic ", strlen( "Basic " ) ) )
1678     {
1679         msg_Dbg( p_access, "Using Basic Authentication" );
1680         psz_header += strlen( "Basic " );
1681         p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
1682         if( !p_auth->psz_realm )
1683             msg_Warn( p_access, "Basic Authentication: "
1684                       "Mandatory 'realm' parameter is missing" );
1685     }
1686     /* 3 Digest Access Authentication Scheme */
1687     else if( !strncasecmp( psz_header, "Digest ", strlen( "Digest " ) ) )
1688     {
1689         msg_Dbg( p_access, "Using Digest Access Authentication" );
1690         if( p_auth->psz_nonce ) return; /* FIXME */
1691         psz_header += strlen( "Digest " );
1692         p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
1693         p_auth->psz_domain = AuthGetParam( psz_header, "domain" );
1694         p_auth->psz_nonce = AuthGetParam( psz_header, "nonce" );
1695         p_auth->psz_opaque = AuthGetParam( psz_header, "opaque" );
1696         p_auth->psz_stale = AuthGetParamNoQuotes( psz_header, "stale" );
1697         p_auth->psz_algorithm = AuthGetParamNoQuotes( psz_header, "algorithm" );
1698         p_auth->psz_qop = AuthGetParam( psz_header, "qop" );
1699         p_auth->i_nonce = 0;
1700         /* printf("realm: |%s|\ndomain: |%s|\nnonce: |%s|\nopaque: |%s|\n"
1701                   "stale: |%s|\nalgorithm: |%s|\nqop: |%s|\n",
1702                   p_auth->psz_realm,p_auth->psz_domain,p_auth->psz_nonce,
1703                   p_auth->psz_opaque,p_auth->psz_stale,p_auth->psz_algorithm,
1704                   p_auth->psz_qop); */
1705         if( !p_auth->psz_realm )
1706             msg_Warn( p_access, "Digest Access Authentication: "
1707                       "Mandatory 'realm' parameter is missing" );
1708         if( !p_auth->psz_nonce )
1709             msg_Warn( p_access, "Digest Access Authentication: "
1710                       "Mandatory 'nonce' parameter is missing" );
1711         if( p_auth->psz_qop ) /* FIXME: parse the qop list */
1712         {
1713             char *psz_tmp = strchr( p_auth->psz_qop, ',' );
1714             if( psz_tmp ) *psz_tmp = '\0';
1715         }
1716     }
1717     else
1718     {
1719         const char *psz_end = strchr( psz_header, ' ' );
1720         if( psz_end )
1721             msg_Warn( p_access, "Unknown authentication scheme: '%*s'",
1722                       (int)(psz_end - psz_header), psz_header );
1723         else
1724             msg_Warn( p_access, "Unknown authentication scheme: '%s'",
1725                       psz_header );
1726     }
1727 }
1728
1729 static char *AuthDigest( access_t *p_access, vlc_url_t *p_url,
1730                          http_auth_t *p_auth, const char *psz_method )
1731 {
1732     (void)p_access;
1733     const char *psz_username = p_url->psz_username ?: "";
1734     const char *psz_password = p_url->psz_password ?: "";
1735
1736     char *psz_HA1 = NULL;
1737     char *psz_HA2 = NULL;
1738     char *psz_response = NULL;
1739     struct md5_s md5;
1740
1741     /* H(A1) */
1742     if( p_auth->psz_HA1 )
1743     {
1744         psz_HA1 = strdup( p_auth->psz_HA1 );
1745         if( !psz_HA1 ) goto error;
1746     }
1747     else
1748     {
1749         InitMD5( &md5 );
1750         AddMD5( &md5, psz_username, strlen( psz_username ) );
1751         AddMD5( &md5, ":", 1 );
1752         AddMD5( &md5, p_auth->psz_realm, strlen( p_auth->psz_realm ) );
1753         AddMD5( &md5, ":", 1 );
1754         AddMD5( &md5, psz_password, strlen( psz_password ) );
1755         EndMD5( &md5 );
1756
1757         psz_HA1 = psz_md5_hash( &md5 );
1758         if( !psz_HA1 ) goto error;
1759
1760         if( p_auth->psz_algorithm
1761             && !strcmp( p_auth->psz_algorithm, "MD5-sess" ) )
1762         {
1763             InitMD5( &md5 );
1764             AddMD5( &md5, psz_HA1, 32 );
1765             free( psz_HA1 );
1766             AddMD5( &md5, ":", 1 );
1767             AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
1768             AddMD5( &md5, ":", 1 );
1769             AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
1770             EndMD5( &md5 );
1771
1772             psz_HA1 = psz_md5_hash( &md5 );
1773             if( !psz_HA1 ) goto error;
1774             p_auth->psz_HA1 = strdup( psz_HA1 );
1775             if( !p_auth->psz_HA1 ) goto error;
1776         }
1777     }
1778
1779     /* H(A2) */
1780     InitMD5( &md5 );
1781     if( *psz_method )
1782         AddMD5( &md5, psz_method, strlen( psz_method ) );
1783     AddMD5( &md5, ":", 1 );
1784     if( p_url->psz_path )
1785         AddMD5( &md5, p_url->psz_path, strlen( p_url->psz_path ) );
1786     else
1787         AddMD5( &md5, "/", 1 );
1788     if( p_auth->psz_qop && !strcmp( p_auth->psz_qop, "auth-int" ) )
1789     {
1790         char *psz_ent;
1791         struct md5_s ent;
1792         InitMD5( &ent );
1793         AddMD5( &ent, "", 0 ); /* XXX: entity-body. should be ok for GET */
1794         EndMD5( &ent );
1795         psz_ent = psz_md5_hash( &ent );
1796         if( !psz_ent ) goto error;
1797         AddMD5( &md5, ":", 1 );
1798         AddMD5( &md5, psz_ent, 32 );
1799         free( psz_ent );
1800     }
1801     EndMD5( &md5 );
1802     psz_HA2 = psz_md5_hash( &md5 );
1803     if( !psz_HA2 ) goto error;
1804
1805     /* Request digest */
1806     InitMD5( &md5 );
1807     AddMD5( &md5, psz_HA1, 32 );
1808     AddMD5( &md5, ":", 1 );
1809     AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
1810     AddMD5( &md5, ":", 1 );
1811     if( p_auth->psz_qop
1812         && ( !strcmp( p_auth->psz_qop, "auth" )
1813              || !strcmp( p_auth->psz_qop, "auth-int" ) ) )
1814     {
1815         char psz_inonce[9];
1816         snprintf( psz_inonce, 9, "%08x", p_auth->i_nonce );
1817         AddMD5( &md5, psz_inonce, 8 );
1818         AddMD5( &md5, ":", 1 );
1819         AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
1820         AddMD5( &md5, ":", 1 );
1821         AddMD5( &md5, p_auth->psz_qop, strlen( p_auth->psz_qop ) );
1822         AddMD5( &md5, ":", 1 );
1823     }
1824     AddMD5( &md5, psz_HA2, 32 );
1825     EndMD5( &md5 );
1826     psz_response = psz_md5_hash( &md5 );
1827
1828     error:
1829         free( psz_HA1 );
1830         free( psz_HA2 );
1831         return psz_response;
1832 }
1833
1834
1835 static void AuthReply( access_t *p_access, const char *psz_prefix,
1836                        vlc_url_t *p_url, http_auth_t *p_auth )
1837 {
1838     access_sys_t *p_sys = p_access->p_sys;
1839     v_socket_t     *pvs = p_sys->p_vs;
1840
1841     const char *psz_username = p_url->psz_username ?: "";
1842     const char *psz_password = p_url->psz_password ?: "";
1843
1844     if( p_auth->psz_nonce )
1845     {
1846         /* Digest Access Authentication */
1847         char *psz_response;
1848
1849         if(    p_auth->psz_algorithm
1850             && strcmp( p_auth->psz_algorithm, "MD5" )
1851             && strcmp( p_auth->psz_algorithm, "MD5-sess" ) )
1852         {
1853             msg_Err( p_access, "Digest Access Authentication: "
1854                      "Unknown algorithm '%s'", p_auth->psz_algorithm );
1855             return;
1856         }
1857
1858         if( p_auth->psz_qop || !p_auth->psz_cnonce )
1859         {
1860             /* FIXME: needs to be really random to prevent man in the middle
1861              * attacks */
1862             free( p_auth->psz_cnonce );
1863             p_auth->psz_cnonce = strdup( "Some random string FIXME" );
1864         }
1865         p_auth->i_nonce ++;
1866
1867         psz_response = AuthDigest( p_access, p_url, p_auth, "GET" );
1868         if( !psz_response ) return;
1869
1870         net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1871                     "%sAuthorization: Digest "
1872                     /* Mandatory parameters */
1873                     "username=\"%s\", "
1874                     "realm=\"%s\", "
1875                     "nonce=\"%s\", "
1876                     "uri=\"%s\", "
1877                     "response=\"%s\", "
1878                     /* Optional parameters */
1879                     "%s%s%s" /* algorithm */
1880                     "%s%s%s" /* cnonce */
1881                     "%s%s%s" /* opaque */
1882                     "%s%s%s" /* message qop */
1883                     "%s%08x%s" /* nonce count */
1884                     "\r\n",
1885                     /* Mandatory parameters */
1886                     psz_prefix,
1887                     psz_username,
1888                     p_auth->psz_realm,
1889                     p_auth->psz_nonce,
1890                     p_url->psz_path ?: "/",
1891                     psz_response,
1892                     /* Optional parameters */
1893                     p_auth->psz_algorithm ? "algorithm=\"" : "",
1894                     p_auth->psz_algorithm ?: "",
1895                     p_auth->psz_algorithm ? "\", " : "",
1896                     p_auth->psz_cnonce ? "cnonce=\"" : "",
1897                     p_auth->psz_cnonce ?: "",
1898                     p_auth->psz_cnonce ? "\", " : "",
1899                     p_auth->psz_opaque ? "opaque=\"" : "",
1900                     p_auth->psz_opaque ?: "",
1901                     p_auth->psz_opaque ? "\", " : "",
1902                     p_auth->psz_qop ? "qop=\"" : "",
1903                     p_auth->psz_qop ?: "",
1904                     p_auth->psz_qop ? "\", " : "",
1905                     p_auth->i_nonce ? "nc=\"" : "uglyhack=\"", /* Will be parsed as an unhandled extension */
1906                     p_auth->i_nonce,
1907                     p_auth->i_nonce ? "\"" : "\""
1908                   );
1909
1910         free( psz_response );
1911     }
1912     else
1913     {
1914         /* Basic Access Authentication */
1915         char buf[strlen( psz_username ) + strlen( psz_password ) + 2];
1916         char *b64;
1917
1918         snprintf( buf, sizeof( buf ), "%s:%s", psz_username, psz_password );
1919         b64 = vlc_b64_encode( buf );
1920
1921         if( b64 != NULL )
1922         {
1923              net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1924                          "%sAuthorization: Basic %s\r\n", psz_prefix, b64 );
1925              free( b64 );
1926         }
1927     }
1928 }
1929
1930 static int AuthCheckReply( access_t *p_access, const char *psz_header,
1931                            vlc_url_t *p_url, http_auth_t *p_auth )
1932 {
1933     int i_ret = VLC_EGENERIC;
1934     char *psz_nextnonce = AuthGetParam( psz_header, "nextnonce" );
1935     char *psz_qop = AuthGetParamNoQuotes( psz_header, "qop" );
1936     char *psz_rspauth = AuthGetParam( psz_header, "rspauth" );
1937     char *psz_cnonce = AuthGetParam( psz_header, "cnonce" );
1938     char *psz_nc = AuthGetParamNoQuotes( psz_header, "nc" );
1939
1940     if( psz_cnonce )
1941     {
1942         char *psz_digest;
1943
1944         if( strcmp( psz_cnonce, p_auth->psz_cnonce ) )
1945         {
1946             msg_Err( p_access, "HTTP Digest Access Authentication: server replied with a different client nonce value." );
1947             goto error;
1948         }
1949
1950         if( psz_nc )
1951         {
1952             int i_nonce;
1953             i_nonce = strtol( psz_nc, NULL, 16 );
1954             if( i_nonce != p_auth->i_nonce )
1955             {
1956                 msg_Err( p_access, "HTTP Digest Access Authentication: server replied with a different nonce count value." );
1957                 goto error;
1958             }
1959         }
1960
1961         if( psz_qop && p_auth->psz_qop && strcmp( psz_qop, p_auth->psz_qop ) )
1962             msg_Warn( p_access, "HTTP Digest Access Authentication: server replied using a different 'quality of protection' option" );
1963
1964         /* All the clear text values match, let's now check the response
1965          * digest */
1966         psz_digest = AuthDigest( p_access, p_url, p_auth, "" );
1967         if( strcmp( psz_digest, psz_rspauth ) )
1968         {
1969             msg_Err( p_access, "HTTP Digest Access Authentication: server replied with an invalid response digest (expected value: %s).", psz_digest );
1970             free( psz_digest );
1971             goto error;
1972         }
1973         free( psz_digest );
1974     }
1975
1976     if( psz_nextnonce )
1977     {
1978         free( p_auth->psz_nonce );
1979         p_auth->psz_nonce = psz_nextnonce;
1980         psz_nextnonce = NULL;
1981     }
1982
1983     i_ret = VLC_SUCCESS;
1984     error:
1985         free( psz_nextnonce );
1986         free( psz_qop );
1987         free( psz_rspauth );
1988         free( psz_cnonce );
1989         free( psz_nc );
1990
1991     return i_ret;
1992 }
1993
1994 static void AuthReset( http_auth_t *p_auth )
1995 {
1996     FREENULL( p_auth->psz_realm );
1997     FREENULL( p_auth->psz_domain );
1998     FREENULL( p_auth->psz_nonce );
1999     FREENULL( p_auth->psz_opaque );
2000     FREENULL( p_auth->psz_stale );
2001     FREENULL( p_auth->psz_algorithm );
2002     FREENULL( p_auth->psz_qop );
2003     p_auth->i_nonce = 0;
2004     FREENULL( p_auth->psz_cnonce );
2005     FREENULL( p_auth->psz_HA1 );
2006 }