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