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