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