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