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