root/modules/access/http.c

Revision 90aa6fc5e9e425c6eb6f519224f2b7e868911d36, 62.5 kB (checked in by Derk-Jan Hartman <hartman@videolan.org>, 1 month ago)

http access: Use EnsureUTF8() on the ICY strings. Avoids "illegal byte sequence" warnings and the like such as in #1772

  • Property mode set to 100644
Line 
1 /*****************************************************************************
2  * http.c: HTTP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          RĂ©mi Denis-Courmont <rem # videolan.org>
10  *          Antoine Cellerier <dionoea at videolan dot org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36
37
38 #include <vlc_access.h>
39
40 #include <vlc_interface.h>
41 #include <vlc_meta.h>
42 #include <vlc_network.h>
43 #include <vlc_url.h>
44 #include <vlc_tls.h>
45 #include <vlc_strings.h>
46 #include <vlc_charset.h>
47 #include <vlc_input.h>
48 #include <vlc_md5.h>
49
50 #ifdef HAVE_ZLIB_H
51 #   include <zlib.h>
52 #endif
53
54 #include <assert.h>
55
56 #ifdef HAVE_PROXY_H
57 #    include "proxy.h"
58 #endif
59 /*****************************************************************************
60  * Module descriptor
61  *****************************************************************************/
62 static int  Open ( vlc_object_t * );
63 static void Close( vlc_object_t * );
64
65 #define PROXY_TEXT N_("HTTP proxy")
66 #define PROXY_LONGTEXT N_( \
67     "HTTP proxy to be used It must be of the form " \
68     "http://[user@]myproxy.mydomain:myport/ ; " \
69     "if empty, the http_proxy environment variable will be tried." )
70
71 #define PROXY_PASS_TEXT N_("HTTP proxy password")
72 #define PROXY_PASS_LONGTEXT N_( \
73     "If your HTTP proxy requires a password, set it here." )
74
75 #define CACHING_TEXT N_("Caching value in ms")
76 #define CACHING_LONGTEXT N_( \
77     "Caching value for HTTP streams. This " \
78     "value should be set in milliseconds." )
79
80 #define AGENT_TEXT N_("HTTP user agent")
81 #define AGENT_LONGTEXT N_("User agent that will be " \
82     "used for the connection.")
83
84 #define RECONNECT_TEXT N_("Auto re-connect")
85 #define RECONNECT_LONGTEXT N_( \
86     "Automatically try to reconnect to the stream in case of a sudden " \
87     "disconnect." )
88
89 #define CONTINUOUS_TEXT N_("Continuous stream")
90 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
91     "being constantly updated (for example, a JPG file on a server). " \
92     "You should not globally enable this option as it will break all other " \
93     "types of HTTP streams." )
94
95 #define FORWARD_COOKIES_TEXT N_("Forward Cookies")
96 #define FORWARD_COOKIES_LONGTEXT N_("Forward Cookies Across http redirections ")
97
98 vlc_module_begin();
99     set_description( N_("HTTP input") );
100     set_capability( "access", 0 );
101     set_shortname( N_( "HTTP(S)" ) );
102     set_category( CAT_INPUT );
103     set_subcategory( SUBCAT_INPUT_ACCESS );
104
105     add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
106                 false );
107     add_password( "http-proxy-pwd", NULL, NULL,
108                   PROXY_PASS_TEXT, PROXY_PASS_LONGTEXT, false );
109     add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
110                  CACHING_TEXT, CACHING_LONGTEXT, true );
111     add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
112                 AGENT_LONGTEXT, true );
113     add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
114               RECONNECT_LONGTEXT, true );
115     add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
116               CONTINUOUS_LONGTEXT, true );
117     add_bool( "http-forward-cookies", 0, NULL, FORWARD_COOKIES_TEXT,
118               FORWARD_COOKIES_LONGTEXT, true );
119     add_obsolete_string("http-user");
120     add_obsolete_string("http-pwd");
121     add_shortcut( "http" );
122     add_shortcut( "https" );
123     add_shortcut( "unsv" );
124     add_shortcut( "itpc" ); /* iTunes Podcast */
125     set_callbacks( Open, Close );
126 vlc_module_end();
127
128 /*****************************************************************************
129  * Local prototypes
130  *****************************************************************************/
131
132 /* RFC 2617: Basic and Digest Access Authentication */
133 typedef struct http_auth_t
134 {
135     char *psz_realm;
136     char *psz_domain;
137     char *psz_nonce;
138     char *psz_opaque;
139     char *psz_stale;
140     char *psz_algorithm;
141     char *psz_qop;
142     int i_nonce;
143     char *psz_cnonce;
144     char *psz_HA1; /* stored H(A1) value if algorithm = "MD5-sess" */
145 } http_auth_t;
146
147 struct access_sys_t
148 {
149     int fd;
150     tls_session_t *p_tls;
151     v_socket_t    *p_vs;
152
153     /* From uri */
154     vlc_url_t url;
155     char    *psz_user_agent;
156     http_auth_t auth;
157
158     /* Proxy */
159     bool b_proxy;
160     vlc_url_t  proxy;
161     http_auth_t proxy_auth;
162     char       *psz_proxy_passbuf;
163
164     /* */
165     int        i_code;
166     const char *psz_protocol;
167     int        i_version;
168
169     char       *psz_mime;
170     char       *psz_pragma;
171     char       *psz_location;
172     bool b_mms;
173     bool b_icecast;
174     bool b_ssl;
175 #ifdef HAVE_ZLIB_H
176     bool b_compressed;
177     struct
178     {
179         z_stream   stream;
180         uint8_t   *p_buffer;
181     } inflate;
182 #endif
183
184     bool b_chunked;
185     int64_t    i_chunk;
186
187     int        i_icy_meta;
188     int64_t    i_icy_offset;
189     char       *psz_icy_name;
190     char       *psz_icy_genre;
191     char       *psz_icy_title;
192
193     int64_t i_remaining;
194
195     bool b_seekable;
196     bool b_reconnect;
197     bool b_continuous;
198     bool b_pace_control;
199     bool b_persist;
200
201     vlc_array_t * cookies;
202 };
203
204 /* */
205 static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies );
206
207 /* */
208 static ssize_t Read( access_t *, uint8_t *, size_t );
209 static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
210 static int Seek( access_t *, int64_t );
211 static int Control( access_t *, int, va_list );
212
213 /* */
214 static int Connect( access_t *, int64_t );
215 static int Request( access_t *p_access, int64_t i_tell );
216 static void Disconnect( access_t * );
217
218 /* Small Cookie utilities. Cookies support is partial. */
219 static char * cookie_get_content( const char * cookie );
220 static char * cookie_get_domain( const char * cookie );
221 static char * cookie_get_name( const char * cookie );
222 static void cookie_append( vlc_array_t * cookies, char * cookie );
223
224
225 static void AuthParseHeader( access_t *p_access, const char *psz_header,
226                              http_auth_t *p_auth );
227 static void AuthReply( access_t *p_acces, const char *psz_prefix,
228                        vlc_url_t *p_url, http_auth_t *p_auth );
229 static int AuthCheckReply( access_t *p_access, const char *psz_header,
230                            vlc_url_t *p_url, http_auth_t *p_auth );
231 static void AuthReset( http_auth_t *p_auth );
232
233 /*****************************************************************************
234  * Open:
235  *****************************************************************************/
236 static int Open( vlc_object_t *p_this )
237 {
238     return OpenWithCookies( p_this, NULL );
239 }
240
241 static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies )
242 {
243     access_t     *p_access = (access_t*)p_this;
244     access_sys_t *p_sys;
245     char         *psz, *p;
246     /* Only forward an store cookies if the corresponding option is activated */
247     bool   b_forward_cookies = var_CreateGetBool( p_access, "http-forward-cookies" );
248     vlc_array_t * saved_cookies = b_forward_cookies ? (cookies ?: vlc_array_new()) : NULL;
249
250     /* Set up p_access */
251     STANDARD_READ_ACCESS_INIT;
252 #ifdef HAVE_ZLIB_H
253     p_access->pf_read = ReadCompressed;
254 #endif
255     p_sys->fd = -1;
256     p_sys->b_proxy = false;
257     p_sys->psz_proxy_passbuf = NULL;
258     p_sys->i_version = 1;
259     p_sys->b_seekable = true;
260     p_sys->psz_mime = NULL;
261     p_sys->psz_pragma = NULL;
262     p_sys->b_mms = false;
263     p_sys->b_icecast = false;
264     p_sys->psz_location = NULL;
265     p_sys->psz_user_agent = NULL;
266     p_sys->b_pace_control = true;
267     p_sys->b_ssl = false;
268 #ifdef HAVE_ZLIB_H
269     p_sys->b_compressed = false;
270     /* 15 is the max windowBits, +32 to enable optional gzip decoding */
271     if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
272         msg_Warn( p_access, "Error during zlib initialisation: %s",
273                   p_sys->inflate.stream.msg );
274     if( zlibCompileFlags() & (1<<17) )
275         msg_Warn( p_access, "Your zlib was compiled without gzip support." );
276     p_sys->inflate.p_buffer = NULL;
277 #endif
278     p_sys->p_tls = NULL;
279     p_sys->p_vs = NULL;
280     p_sys->i_icy_meta = 0;
281     p_sys->i_icy_offset = 0;
282     p_sys->psz_icy_name = NULL;
283     p_sys->psz_icy_genre = NULL;
284     p_sys->psz_icy_title = NULL;
285     p_sys->i_remaining = 0;
286     p_sys->b_persist = false;
287     p_access->info.i_size = -1;
288     p_access->info.i_pos  = 0;
289     p_access->info.b_eof  = false;
290
291     p_sys->cookies = saved_cookies;
292
293     /* Parse URI - remove spaces */
294     p = psz = strdup( p_access->psz_path );
295     while( (p = strchr( p, ' ' )) != NULL )
296         *p = '+';
297     vlc_UrlParse( &p_sys->url, psz, 0 );
298     free( psz );
299
300     if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
301     {
302         msg_Warn( p_access, "invalid host" );
303         goto error;
304     }
305     if( !strncmp( p_access->psz_access, "https", 5 ) )
306     {
307         /* HTTP over SSL */
308         p_sys->b_ssl = true;
309         if( p_sys->url.i_port <= 0 )
310             p_sys->url.i_port = 443;
311     }
312     else
313     {
314         if( p_sys->url.i_port <= 0 )
315             p_sys->url.i_port = 80;
316     }
317
318     /* Do user agent */
319     p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
320
321     /* Check proxy */
322     psz = var_CreateGetNonEmptyString( p_access, "http-proxy" );
323     if( psz )
324     {
325         p_sys->b_proxy = true;
326         vlc_UrlParse( &p_sys->proxy, psz, 0 );
327         free( psz );
328     }
329 #ifdef HAVE_PROXY_H
330     else
331     {
332         pxProxyFactory *pf = px_proxy_factory_new();
333         if (pf)
334         {
335             char *buf;
336             int i;
337             i=asprintf(&buf, "%s://%s", p_access->psz_access, p_access->psz_path);
338             if (i >= 0)
339             {
340                 msg_Dbg(p_access, "asking libproxy about url '%s'", buf);
341                 char **proxies = px_proxy_factory_get_proxies(pf, buf);
342                 if (proxies[0])
343                 {
344                     msg_Dbg(p_access, "libproxy suggest to use '%s'", proxies[0]);
345                     if(strcmp(proxies[0],"direct://") != 0)
346                     {
347                         p_sys->b_proxy = true;
348                         vlc_UrlParse( &p_sys->proxy, proxies[0], 0);
349                     }
350                 }
351                 for(i=0;proxies[i];i++) free(proxies[i]);
352                 free(proxies);
353                 free(buf);
354             }
355             px_proxy_factory_free(pf);
356         }
357         else
358         {
359             msg_Err(p_access, "Allocating memory for libproxy failed");
360         }
361     }
362 #elif HAVE_GETENV
363     else
364     {
365         psz = getenv( "http_proxy" );
366         if( psz )
367         {
368             p_sys->b_proxy = true;
369             vlc_UrlParse( &p_sys->proxy, psz, 0 );
370         }
371     }
372 #endif
373     if( psz ) /* No, this is NOT a use-after-free error */
374     {
375         psz = var_CreateGetNonEmptyString( p_access, "http-proxy-pwd" );
376         if( psz )
377             p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
378     }
379
380     if( p_sys->b_proxy )
381     {
382         if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
383         {
384             msg_Warn( p_access, "invalid proxy host" );
385             goto error;
386         }
387         if( p_sys->proxy.i_port <= 0 )
388         {
389             p_sys->proxy.i_port = 80;
390         }
391     }
392
393     msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
394              p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
395     if( p_sys->b_proxy )
396     {
397         msg_Dbg( p_access, "      proxy %s:%d", p_sys->proxy.psz_host,
398                  p_sys->proxy.i_port );
399     }
400     if( p_sys->url.psz_username && *p_sys->url.psz_username )
401     {
402         msg_Dbg( p_access, "      user='%s'", p_sys->url.psz_username );
403     }
404
405     p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
406     p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
407
408 connect:
409     /* Connect */
410     switch( Connect( p_access, 0 ) )
411     {
412         case -1:
413             goto error;
414
415         case -2:
416             /* Retry with http 1.0 */
417             msg_Dbg( p_access, "switching to HTTP version 1.0" );
418             p_sys->i_version = 0;
419             p_sys->b_seekable = false;
420
421             if( !vlc_object_alive (p_access) || Connect( p_access, 0 ) )
422                 goto error;
423
424 #ifndef NDEBUG
425         case 0:
426             break;
427
428         default:
429             msg_Err( p_access, "You should not be here" );
430             abort();
431 #endif
432     }
433
434     if( p_sys->i_code == 401 )
435     {
436         char *psz_login = NULL, *psz_password = NULL;
437         char psz_msg[250];
438         int i_ret;
439         /* FIXME ? */
440         if( p_sys->url.psz_username && p_sys->url.psz_password &&
441             p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
442         {
443             goto connect;
444         }
445         snprintf( psz_msg, 250,
446             _("Please enter a valid login name and a password for realm %s."),
447             p_sys->auth.psz_realm );
448         msg_Dbg( p_access, "authentication failed for realm %s",
449             p_sys->auth.psz_realm );
450         i_ret = intf_UserLoginPassword( p_access, _("HTTP authentication"),
451                                         psz_msg, &psz_login, &psz_password );
452         if( i_ret == DIALOG_OK_YES )
453         {
454             msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
455                         psz_login, psz_password );
456             if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
457             if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
458             free( psz_login );
459             free( psz_password );
460             goto connect;
461         }
462         else
463         {
464             free( psz_login );
465             free( psz_password );
466             goto error;
467         }
468     }
469
470     if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
471           p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
472         p_sys->psz_location && *p_sys->psz_location )
473     {
474         msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
475
476         /* Do not accept redirection outside of HTTP works */
477         if( strncmp( p_sys->psz_location, "http", 4 )
478          || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
479            && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
480         {
481             msg_Err( p_access, "insecure redirection ignored" );
482             goto error;
483         }
484         free( p_access->psz_path );
485         p_access->psz_path = strdup( p_sys->psz_location );
486         /* Clean up current Open() run */
487         vlc_UrlClean( &p_sys->url );
488         AuthReset( &p_sys->auth );
489         vlc_UrlClean( &p_sys->proxy );
490         free( p_sys->psz_proxy_passbuf );
491         AuthReset( &p_sys->proxy_auth );
492         free( p_sys->psz_mime );
493         free( p_sys->psz_pragma );
494         free( p_sys->psz_location );
495         free( p_sys->psz_user_agent );
496
497         Disconnect( p_access );
498         cookies = p_sys->cookies;
499         free( p_sys );
500
501         /* Do new Open() run with new data */
502         return OpenWithCookies( p_this, cookies );
503     }
504
505     if( p_sys->b_mms )
506     {
507         msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
508         goto error;
509     }
510
511     if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
512     {
513         if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
514         {
515             if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
516                 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
517             {
518                 free( p_access->psz_demux );
519                 p_access->psz_demux = strdup( "nsv" );
520             }
521             else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
522                      !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
523             {
524                 free( p_access->psz_demux );
525                 p_access->psz_demux = strdup( "m4a" );
526             }
527             else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
528             {
529                 free( p_access->psz_demux );
530                 p_access->psz_demux = strdup( "mp3" );
531             }
532
533             msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
534                       p_access->psz_demux );
535
536 #if 0       /* Doesn't work really well because of the pre-buffering in
537              * shoutcast servers (the buffer content will be sent as fast as
538              * possible). */
539             p_sys->b_pace_control = false;
540 #endif
541         }
542         else if( !p_sys->psz_mime )
543         {
544             free( p_access->psz_demux );
545             /* Shoutcast */
546             p_access->psz_demux = strdup( "mp3" );
547         }
548         /* else probably Ogg Vorbis */
549     }
550     else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
551              p_sys->psz_mime &&
552              !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
553     {
554         free( p_access->psz_demux );
555         /* Grrrr! detect ultravox server and force NSV demuxer */
556         p_access->psz_demux = strdup( "nsv" );
557     }
558     else if( !strcmp( p_access->psz_access, "itpc" ) )
559     {
560         free( p_access->psz_demux );
561         p_access->psz_demux = strdup( "podcast" );
562     }
563     else if( p_sys->psz_mime &&
564              !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
565              ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
566     {
567         free( p_access->psz_demux );
568         p_access->psz_demux = strdup( "xspf-open" );
569     }
570
571     if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
572
573     /* PTS delay */
574     var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
575
576     return VLC_SUCCESS;
577
578 error:
579     vlc_UrlClean( &p_sys->url );
580     vlc_UrlClean( &p_sys->proxy );
581     free( p_sys->psz_proxy_passbuf );
582     free( p_sys->psz_mime );
583     free( p_sys->psz_pragma );
584     free( p_sys->psz_location );
585     free( p_sys->psz_user_agent );
586
587     Disconnect( p_access );
588     free( p_sys );
589     return VLC_EGENERIC;
590 }
591
592 /*****************************************************************************
593  * Close:
594  *****************************************************************************/
595 static void Close( vlc_object_t *p_this )
596 {
597     access_t     *p_access = (access_t*)p_this;
598     access_sys_t *p_sys = p_access->p_sys;
599
600     vlc_UrlClean( &p_sys->url );
601     AuthReset( &p_sys->auth );
602     vlc_UrlClean( &p_sys->proxy );
603     AuthReset( &p_sys->proxy_auth );
604
605     free( p_sys->psz_mime );
606     free( p_sys->psz_pragma );
607     free( p_sys->psz_location );
608
609     free( p_sys->psz_icy_name );
610     free( p_sys->psz_icy_genre );
611     free( p_sys->psz_icy_title );
612
613     free( p_sys->psz_user_agent );
614
615     Disconnect( p_access );
616
617     if( p_sys->cookies )
618     {
619         int i;
620         for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
621             free(vlc_array_item_at_index( p_sys->cookies, i ));
622         vlc_array_destroy( p_sys->cookies );
623     }
624
625 #ifdef HAVE_ZLIB_H
626     inflateEnd( &p_sys->inflate.stream );
627     free( p_sys->inflate.p_buffer );
628 #endif
629
630     free( p_sys );
631 }
632
633 /*****************************************************************************
634  * Read: Read up to i_len bytes from the http connection and place in
635  * p_buffer. Return the actual number of bytes read
636  *****************************************************************************/
637 static int ReadICYMeta( access_t *p_access );
638 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
639 {
640     access_sys_t *p_sys = p_access->p_sys;
641     int i_read;
642
643     if( p_sys->fd < 0 )
644     {
645         p_access->info.b_eof = true;
646         return 0;
647     }
648
649     if( p_access->info.i_size >= 0 &&
650         i_len + p_access->info.i_pos > p_access->info.i_size )
651     {
652         if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
653         {
654             p_access->info.b_eof = true;
655             return 0;
656         }
657     }
658
659     if( p_sys->b_chunked )
660     {
661         if( p_sys->i_chunk < 0 )
662         {
663             p_access->info.b_eof = true;
664             return 0;
665         }
666
667         if( p_sys->i_chunk <= 0 )
668         {
669             char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
670             /* read the chunk header */
671             if( psz == NULL )
672             {
673                 /* fatal error - end of file */
674                 msg_Dbg( p_access, "failed reading chunk-header line" );
675                 return 0;
676             }
677             p_sys->i_chunk = strtoll( psz, NULL, 16 );
678             free( psz );
679
680             if( p_sys->i_chunk <= 0 )   /* eof */
681             {
682                 p_sys->i_chunk = -1;
683                 p_access->info.b_eof = true;
684                 return 0;
685             }
686         }
687
688         if( i_len > p_sys->i_chunk )
689         {
690             i_len = p_sys->i_chunk;
691         }
692     }
693     else if( p_access->info.i_size != -1 && (int64_t)i_len > p_sys->i_remaining) {
694         /* Only ask for the remaining length */
695         i_len = (size_t)p_sys->i_remaining;
696         if(i_len == 0) {
697             p_access->info.b_eof = true;
698             return 0;
699         }
700     }
701
702
703     if( p_sys->i_icy_meta > 0 && p_access->info.i_pos-p_sys->i_icy_offset > 0 )
704     {
705         int64_t i_next = p_sys->i_icy_meta -
706                                     (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
707
708         if( i_next == p_sys->i_icy_meta )
709         {
710             if( ReadICYMeta( p_access ) )
711             {
712                 p_access->info.b_eof = true;
713                 return -1;
714             }
715         }
716         if( i_len > i_next )
717             i_len = i_next;
718     }
719
720     i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
721
722     if( i_read > 0 )
723     {
724         p_access->info.i_pos += i_read;
725
726         if( p_sys->b_chunked )
727         {
728             p_sys->i_chunk -= i_read;
729             if( p_sys->i_chunk <= 0 )
730             {
731                 /* read the empty line */
732                 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
733                 free( psz );
734             }
735         }
736     }
737     else if( i_read == 0 )
738     {
739         /*
740          * I very much doubt that this will work.
741          * If i_read == 0, the connection *IS* dead, so the only
742          * sensible thing to do is Disconnect() and then retry.
743          * Otherwise, I got recv() completely wrong. -- Courmisch
744          */
745         if( p_sys->b_continuous )
746         {
747             Request( p_access, 0 );
748             p_sys->b_continuous = false;
749             i_read = Read( p_access, p_buffer, i_len );
750             p_sys->b_continuous = true;
751         }
752         Disconnect( p_access );
753         if( p_sys->b_reconnect )
754         {
755             msg_Dbg( p_access, "got disconnected, trying to reconnect" );
756             if( Connect( p_access, p_access->info.i_pos ) )
757             {
758                 msg_Dbg( p_access, "reconnection failed" );
759             }
760             else
761             {
762                 p_sys->b_reconnect = false;
763                 i_read = Read( p_access, p_buffer, i_len );
764                 p_sys->b_reconnect = true;
765             }
766         }
767
768         if( i_read == 0 ) p_access->info.b_eof = true;
769     }
770
771     if( p_access->info.i_size != -1 )
772     {
773         p_sys->i_remaining -= i_read;
774     }
775
776     return i_read;
777 }
778
779 static int ReadICYMeta( access_t *p_access )
780 {
781     access_sys_t *p_sys = p_access->p_sys;
782
783     uint8_t buffer;
784     char *p, *psz_meta;
785     int i_read;
786
787     /* Read meta data length */
788     i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
789                        true );
790     if( i_read <= 0 )
791         return VLC_EGENERIC;
792     if( buffer == 0 )
793         return VLC_SUCCESS;
794
795     i_read = buffer << 4;
796     /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
797
798     psz_meta = malloc( i_read + 1 );
799     if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
800                   (uint8_t *)psz_meta, i_read, true ) != i_read )
801         return VLC_EGENERIC;
802
803     psz_meta[i_read] = '\0'; /* Just in case */
804
805     /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
806
807     /* Now parse the meta */
808     /* Look for StreamTitle= */
809     p = strcasestr( (char *)psz_meta, "StreamTitle=" );
810     if( p )
811     {
812         p += strlen( "StreamTitle=" );
813         if( *p == '\'' || *p == '"' )
814         {
815             char closing[] = { p[0], ';', '\0' };
816             char *psz = strstr( &p[1], closing );
817             if( !psz )
818                 psz = strchr( &p[1], ';' );
819
820             if( psz ) *psz = '\0';
821         }
822         else
823         {
824             char *psz = strchr( &p[1], ';' );
825             if( psz ) *psz = '\0';
826         }
827
828         if( !p_sys->psz_icy_title ||
829             strcmp( p_sys->psz_icy_title, &p[1] ) )
830         {
831             free( p_sys->psz_icy_title );
832             p_sys->psz_icy_title = EnsureUTF8( strdup( &p[1] ));
833             p_access->info.i_update |= INPUT_UPDATE_META;
834
835             msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
836         }
837     }
838     free( psz_meta );
839
840     return VLC_SUCCESS;
841 }
842
843 #ifdef HAVE_ZLIB_H
844 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
845                                size_t i_len )
846 {
847     access_sys_t *p_sys = p_access->p_sys;
848
849     if( p_sys->b_compressed )
850     {
851         int i_ret;
852
853         if( !p_sys->inflate.p_buffer )
854             p_sys->inflate.p_buffer = malloc( 256 * 1024 );
855
856         if( p_sys->inflate.stream.avail_in == 0 )
857         {
858             ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer + p_sys->inflate.stream.avail_in, 256 * 1024 );
859             if( i_read <= 0 ) return i_read;
860             p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
861             p_sys->inflate.stream.avail_in = i_read;
862         }
863
864         p_sys->inflate.stream.avail_out = i_len;
865         p_sys->inflate.stream.next_out = p_buffer;
866
867         i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
868         msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
869
870         return i_len - p_sys->inflate.stream.avail_out;
871     }
872     else
873     {
874         return Read( p_access, p_buffer, i_len );
875     }
876 }
877 #endif
878
879 /*****************************************************************************
880  * Seek: close and re-open a connection at the right place
881  *****************************************************************************/
882 static int Seek( access_t *p_access, int64_t i_pos )
883 {
884     msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
885
886     Disconnect( p_access );
887
888     if( p_access->info.i_size
889      && (uint64_t)i_pos >= (uint64_t)p_access->info.i_size ) {
890         msg_Err( p_access, "seek to far" );
891         int retval = Seek( p_access, p_access->info.i_size - 1 );
892         if( retval == VLC_SUCCESS ) {
893             uint8_t p_buffer[2];
894             Read( p_access, p_buffer, 1);
895             p_access->info.b_eof  = false;
896         }
897         return retval;
898     }
899     if( Connect( p_access, i_pos ) )
900     {
901         msg_Err( p_access, "seek failed" );
902         p_access->info.b_eof = true;
903         return VLC_EGENERIC;
904     }
905     return VLC_SUCCESS;
906 }
907
908 /*****************************************************************************
909  * Control:
910  *****************************************************************************/
911 static int Control( access_t *p_access, int i_query, va_list args )
912 {
913     access_sys_t *p_sys = p_access->p_sys;
914     bool   *pb_bool;
915     int          *pi_int;
916     int64_t      *pi_64;
917     vlc_meta_t   *p_meta;
918
919     switch( i_query )
920     {
921         /* */
922         case ACCESS_CAN_SEEK:
923             pb_bool = (bool*)va_arg( args, bool* );
924             *pb_bool = p_sys->b_seekable;
925