add_integer: remove callback parameter
[vlc/asuraparaju-public.git] / modules / access / http.c
blob16b59589959459dd8f72de90409f84c62366da17
1 /*****************************************************************************
2 * http.c: HTTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2008 the VideoLAN team
5 * $Id$
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>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
38 #include <vlc_access.h>
40 #include <vlc_dialog.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 #include <vlc_http.h>
51 #ifdef HAVE_ZLIB_H
52 # include <zlib.h>
53 #endif
55 #include <assert.h>
57 #ifdef HAVE_LIBPROXY
58 # include <proxy.h>
59 #endif
61 #ifdef WIN32
62 # include <windows.h>
63 #endif
65 /*****************************************************************************
66 * Module descriptor
67 *****************************************************************************/
68 static int Open ( vlc_object_t * );
69 static void Close( vlc_object_t * );
71 #define PROXY_TEXT N_("HTTP proxy")
72 #define PROXY_LONGTEXT N_( \
73 "HTTP proxy to be used It must be of the form " \
74 "http://[user@]myproxy.mydomain:myport/ ; " \
75 "if empty, the http_proxy environment variable will be tried." )
77 #define PROXY_PASS_TEXT N_("HTTP proxy password")
78 #define PROXY_PASS_LONGTEXT N_( \
79 "If your HTTP proxy requires a password, set it here." )
81 #define CACHING_TEXT N_("Caching value in ms")
82 #define CACHING_LONGTEXT N_( \
83 "Caching value for HTTP streams. This " \
84 "value should be set in milliseconds." )
86 #define RECONNECT_TEXT N_("Auto re-connect")
87 #define RECONNECT_LONGTEXT N_( \
88 "Automatically try to reconnect to the stream in case of a sudden " \
89 "disconnect." )
91 #define CONTINUOUS_TEXT N_("Continuous stream")
92 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
93 "being constantly updated (for example, a JPG file on a server). " \
94 "You should not globally enable this option as it will break all other " \
95 "types of HTTP streams." )
97 #define FORWARD_COOKIES_TEXT N_("Forward Cookies")
98 #define FORWARD_COOKIES_LONGTEXT N_("Forward Cookies across http redirections.")
100 #define MAX_REDIRECT_TEXT N_("Max number of redirection")
101 #define MAX_REDIRECT_LONGTEXT N_("Limit the number of redirection to follow.")
103 #define USE_IE_PROXY_TEXT N_("Use Internet Explorer entered HTTP proxy server")
104 #define USE_IE_PROXY_LONGTEXT N_("Use Internet Explorer entered HTTP proxy " \
105 "server for all URL. Don't take into account bypasses settings and auto " \
106 "configuration scripts.")
108 vlc_module_begin ()
109 set_description( N_("HTTP input") )
110 set_capability( "access", 0 )
111 set_shortname( N_( "HTTP(S)" ) )
112 set_category( CAT_INPUT )
113 set_subcategory( SUBCAT_INPUT_ACCESS )
115 add_string( "http-proxy", NULL, PROXY_TEXT, PROXY_LONGTEXT,
116 false )
117 add_password( "http-proxy-pwd", NULL,
118 PROXY_PASS_TEXT, PROXY_PASS_LONGTEXT, false )
119 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000,
120 CACHING_TEXT, CACHING_LONGTEXT, true )
121 change_safe()
122 add_string( "http-user-agent", NULL, NULL, NULL, false )
123 change_safe()
124 change_private()
125 add_bool( "http-reconnect", false, NULL, RECONNECT_TEXT,
126 RECONNECT_LONGTEXT, true )
127 add_bool( "http-continuous", false, NULL, CONTINUOUS_TEXT,
128 CONTINUOUS_LONGTEXT, true )
129 change_safe()
130 add_bool( "http-forward-cookies", true, NULL, FORWARD_COOKIES_TEXT,
131 FORWARD_COOKIES_LONGTEXT, true )
132 add_integer( "http-max-redirect", 5, MAX_REDIRECT_TEXT,
133 MAX_REDIRECT_LONGTEXT, true )
134 #ifdef WIN32
135 add_bool( "http-use-IE-proxy", false, NULL, USE_IE_PROXY_TEXT,
136 USE_IE_PROXY_LONGTEXT, true )
137 #endif
138 add_obsolete_string("http-user")
139 add_obsolete_string("http-pwd")
140 /* 'itpc' = iTunes Podcast */
141 add_shortcut( "http", "https", "unsv", "itpc", "icyx" )
142 set_callbacks( Open, Close )
143 vlc_module_end ()
145 /*****************************************************************************
146 * Local prototypes
147 *****************************************************************************/
149 struct access_sys_t
151 int fd;
152 bool b_error;
153 tls_session_t *p_tls;
154 v_socket_t *p_vs;
156 /* From uri */
157 vlc_url_t url;
158 char *psz_user_agent;
159 http_auth_t auth;
161 /* Proxy */
162 bool b_proxy;
163 vlc_url_t proxy;
164 http_auth_t proxy_auth;
165 char *psz_proxy_passbuf;
167 /* */
168 int i_code;
169 const char *psz_protocol;
170 int i_version;
172 char *psz_mime;
173 char *psz_pragma;
174 char *psz_location;
175 bool b_mms;
176 bool b_icecast;
177 bool b_ssl;
178 #ifdef HAVE_ZLIB_H
179 bool b_compressed;
180 struct
182 z_stream stream;
183 uint8_t *p_buffer;
184 } inflate;
185 #endif
187 bool b_chunked;
188 int64_t i_chunk;
190 int i_icy_meta;
191 uint64_t i_icy_offset;
192 char *psz_icy_name;
193 char *psz_icy_genre;
194 char *psz_icy_title;
196 uint64_t i_remaining;
198 bool b_seekable;
199 bool b_reconnect;
200 bool b_continuous;
201 bool b_pace_control;
202 bool b_persist;
203 bool b_has_size;
205 vlc_array_t * cookies;
208 /* */
209 static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
210 int i_nb_redirect, int i_max_redirect,
211 vlc_array_t *cookies );
213 /* */
214 static ssize_t Read( access_t *, uint8_t *, size_t );
215 static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
216 static int Seek( access_t *, uint64_t );
217 static int Control( access_t *, int, va_list );
219 /* */
220 static int Connect( access_t *, uint64_t );
221 static int Request( access_t *p_access, uint64_t i_tell );
222 static void Disconnect( access_t * );
224 /* Small Cookie utilities. Cookies support is partial. */
225 static char * cookie_get_content( const char * cookie );
226 static char * cookie_get_domain( const char * cookie );
227 static char * cookie_get_name( const char * cookie );
228 static void cookie_append( vlc_array_t * cookies, char * cookie );
231 static void AuthReply( access_t *p_acces, const char *psz_prefix,
232 vlc_url_t *p_url, http_auth_t *p_auth );
233 static int AuthCheckReply( access_t *p_access, const char *psz_header,
234 vlc_url_t *p_url, http_auth_t *p_auth );
236 /*****************************************************************************
237 * Open:
238 *****************************************************************************/
239 static int Open( vlc_object_t *p_this )
241 access_t *p_access = (access_t*)p_this;
242 return OpenWithCookies( p_this, p_access->psz_access, 0,
243 var_InheritInteger( p_access, "http-max-redirect" ), NULL );
247 * Open the given url using the given cookies
248 * @param p_this: the vlc object
249 * @psz_access: the acces to use (http, https, ...) (this value must be used
250 * instead of p_access->psz_access)
251 * @i_nb_redirect: the number of redirection already done
252 * @i_max_redirect: limit to the number of redirection to follow
253 * @cookies: the available cookies
254 * @return vlc error codes
256 static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
257 int i_nb_redirect, int i_max_redirect,
258 vlc_array_t *cookies )
260 access_t *p_access = (access_t*)p_this;
261 access_sys_t *p_sys;
262 char *psz, *p;
264 /* Only forward an store cookies if the corresponding option is activated */
265 bool b_forward_cookies = var_InheritBool( p_access, "http-forward-cookies" );
266 vlc_array_t * saved_cookies = b_forward_cookies ? (cookies ? cookies : vlc_array_new()) : NULL;
268 /* Set up p_access */
269 STANDARD_READ_ACCESS_INIT;
270 #ifdef HAVE_ZLIB_H
271 p_access->pf_read = ReadCompressed;
272 #endif
273 p_sys->fd = -1;
274 p_sys->b_proxy = false;
275 p_sys->psz_proxy_passbuf = NULL;
276 p_sys->i_version = 1;
277 p_sys->b_seekable = true;
278 p_sys->psz_mime = NULL;
279 p_sys->psz_pragma = NULL;
280 p_sys->b_mms = false;
281 p_sys->b_icecast = false;
282 p_sys->psz_location = NULL;
283 p_sys->psz_user_agent = NULL;
284 p_sys->b_pace_control = true;
285 p_sys->b_ssl = false;
286 #ifdef HAVE_ZLIB_H
287 p_sys->b_compressed = false;
288 /* 15 is the max windowBits, +32 to enable optional gzip decoding */
289 if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
290 msg_Warn( p_access, "Error during zlib initialisation: %s",
291 p_sys->inflate.stream.msg );
292 if( zlibCompileFlags() & (1<<17) )
293 msg_Warn( p_access, "Your zlib was compiled without gzip support." );
294 p_sys->inflate.p_buffer = NULL;
295 #endif
296 p_sys->p_tls = NULL;
297 p_sys->p_vs = NULL;
298 p_sys->i_icy_meta = 0;
299 p_sys->i_icy_offset = 0;
300 p_sys->psz_icy_name = NULL;
301 p_sys->psz_icy_genre = NULL;
302 p_sys->psz_icy_title = NULL;
303 p_sys->i_remaining = 0;
304 p_sys->b_persist = false;
305 p_sys->b_has_size = false;
306 p_access->info.i_size = 0;
307 p_access->info.i_pos = 0;
308 p_access->info.b_eof = false;
310 p_sys->cookies = saved_cookies;
312 http_auth_Init( &p_sys->auth );
313 http_auth_Init( &p_sys->proxy_auth );
315 /* Parse URI - remove spaces */
316 p = psz = strdup( p_access->psz_location );
317 while( (p = strchr( p, ' ' )) != NULL )
318 *p = '+';
319 vlc_UrlParse( &p_sys->url, psz, 0 );
320 free( psz );
322 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
324 msg_Warn( p_access, "invalid host" );
325 goto error;
327 if( !strncmp( psz_access, "https", 5 ) )
329 /* HTTP over SSL */
330 p_sys->b_ssl = true;
331 if( p_sys->url.i_port <= 0 )
332 p_sys->url.i_port = 443;
334 else
336 if( p_sys->url.i_port <= 0 )
337 p_sys->url.i_port = 80;
340 /* Determine the HTTP user agent */
341 /* See RFC2616 §2.2 token definition and §3.8 user-agent header */
342 p_sys->psz_user_agent = var_InheritString( p_access, "http-user-agent" );
343 if (p_sys->psz_user_agent)
345 for( char *p = p_sys->psz_user_agent; *p; p++ )
347 uint8_t c = *p;
348 if( c < 32 || strchr( "()<>@,;:\\\"[]?={}", c ) )
349 *p = '_'; /* remove potentially harmful characters */
353 /* Check proxy */
354 psz = var_InheritString( p_access, "http-proxy" );
355 if( psz )
357 p_sys->b_proxy = true;
358 vlc_UrlParse( &p_sys->proxy, psz, 0 );
359 free( psz );
361 #ifdef HAVE_LIBPROXY
362 else
364 pxProxyFactory *pf = px_proxy_factory_new();
365 if (pf)
367 char *buf;
368 int i;
369 i=asprintf(&buf, "%s://%s", psz_access, p_access->psz_location);
370 if (i >= 0)
372 msg_Dbg(p_access, "asking libproxy about url '%s'", buf);
373 char **proxies = px_proxy_factory_get_proxies(pf, buf);
374 if (proxies[0])
376 msg_Dbg(p_access, "libproxy suggest to use '%s'", proxies[0]);
377 if(strcmp(proxies[0],"direct://") != 0)
379 p_sys->b_proxy = true;
380 vlc_UrlParse( &p_sys->proxy, proxies[0], 0);
383 for(i=0;proxies[i];i++) free(proxies[i]);
384 free(proxies);
385 free(buf);
387 px_proxy_factory_free(pf);
389 else
391 msg_Err(p_access, "Allocating memory for libproxy failed");
394 #elif defined( WIN32 )
395 else
397 if( var_InheritBool( p_access, "http-use-IE-proxy" ) )
399 /* Try to get the proxy server address from Windows internet settings using registry. */
400 HKEY h_key;
401 /* Open the key */
402 if( RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft" \
403 "\\Windows\\CurrentVersion\\Internet Settings",
404 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
406 DWORD i_dataReadSize = 4; /* sizeof( DWORD ); */
407 DWORD proxyEnable = 0;
408 /* Get the proxy enable value */
409 if( RegQueryValueEx( h_key, "ProxyEnable", NULL, NULL,
410 (char *)&proxyEnable, &i_dataReadSize )
411 == ERROR_SUCCESS )
413 if( proxyEnable )
415 /* Proxy is enable */
416 char psz_key[256];
417 i_dataReadSize = 256;
418 if( RegQueryValueEx( h_key, "ProxyServer",
419 NULL, NULL, psz_key,
420 &i_dataReadSize )
421 == ERROR_SUCCESS )
423 /* Get the proxy URL :
424 Proxy server value in the registry can be something like "address:port"
425 or "ftp=address1:port1;http=address2:port2 ..." depending of the
426 confirguration. */
427 char *psz_proxy;
428 psz_proxy = strstr( psz_key, "http=" );
429 if( psz_proxy != NULL )
431 psz_proxy += strlen( "http=" );
432 char *psz_endUrl = strchr( psz_proxy, ';' );
433 if( psz_endUrl != NULL )
434 *psz_endUrl = '\0';
436 else
437 psz_proxy = psz_key;
438 /* Set proxy enable for this connection. */
439 p_sys->b_proxy = true;
440 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
442 msg_Warn( p_access, "Couldn't read in registry " \
443 "the proxy server address." );
446 else
447 msg_Warn( p_access, "Couldn't read in registry if the " \
448 "proxy is enable or not." );
450 else
451 msg_Warn( p_access, "Couldn't open internet settings key " \
452 "in registry." );
455 #elif defined( HAVE_GETENV )
456 else
458 psz = getenv( "http_proxy" );
459 if( psz )
461 p_sys->b_proxy = true;
462 vlc_UrlParse( &p_sys->proxy, psz, 0 );
465 #endif
467 if( psz ) /* No, this is NOT a use-after-free error */
469 psz = var_InheritString( p_access, "http-proxy-pwd" );
470 if( psz )
471 p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
474 if( p_sys->b_proxy )
476 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
478 msg_Warn( p_access, "invalid proxy host" );
479 goto error;
481 if( p_sys->proxy.i_port <= 0 )
483 p_sys->proxy.i_port = 80;
487 msg_Dbg( p_access, "http: server='%s' port=%d file='%s'",
488 p_sys->url.psz_host, p_sys->url.i_port,
489 p_sys->url.psz_path != NULL ? p_sys->url.psz_path : "" );
490 if( p_sys->b_proxy )
492 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
493 p_sys->proxy.i_port );
495 if( p_sys->url.psz_username && *p_sys->url.psz_username )
497 msg_Dbg( p_access, " user='%s'", p_sys->url.psz_username );
500 p_sys->b_reconnect = var_InheritBool( p_access, "http-reconnect" );
501 p_sys->b_continuous = var_InheritBool( p_access, "http-continuous" );
503 connect:
504 /* Connect */
505 switch( Connect( p_access, 0 ) )
507 case -1:
508 goto error;
510 case -2:
511 /* Retry with http 1.0 */
512 msg_Dbg( p_access, "switching to HTTP version 1.0" );
513 p_sys->i_version = 0;
514 p_sys->b_seekable = false;
516 if( !vlc_object_alive (p_access) || Connect( p_access, 0 ) )
517 goto error;
519 #ifndef NDEBUG
520 case 0:
521 break;
523 default:
524 msg_Err( p_access, "You should not be here" );
525 abort();
526 #endif
529 if( p_sys->i_code == 401 )
531 char *psz_login, *psz_password;
532 /* FIXME ? */
533 if( p_sys->url.psz_username && p_sys->url.psz_password &&
534 p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
536 Disconnect( p_access );
537 goto connect;
539 msg_Dbg( p_access, "authentication failed for realm %s",
540 p_sys->auth.psz_realm );
541 dialog_Login( p_access, &psz_login, &psz_password,
542 _("HTTP authentication"),
543 _("Please enter a valid login name and a password for realm %s."),
544 p_sys->auth.psz_realm );
545 if( psz_login != NULL && psz_password != NULL )
547 msg_Dbg( p_access, "retrying with user=%s", psz_login );
548 p_sys->url.psz_username = psz_login;
549 p_sys->url.psz_password = psz_password;
550 Disconnect( p_access );
551 goto connect;
553 else
555 free( psz_login );
556 free( psz_password );
557 goto error;
561 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
562 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
563 p_sys->psz_location && *p_sys->psz_location )
565 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
567 /* Check the number of redirection already done */
568 if( i_nb_redirect >= i_max_redirect )
570 msg_Err( p_access, "Too many redirection: break potential infinite"
571 "loop" );
572 goto error;
576 /* Do not accept redirection outside of HTTP works */
577 const char *psz_protocol;
578 if( !strncmp( p_sys->psz_location, "http:", 5 ) )
579 psz_protocol = "http";
580 else if( !strncmp( p_sys->psz_location, "https:", 6 ) )
581 psz_protocol = "https";
582 else
584 msg_Err( p_access, "insecure redirection ignored" );
585 goto error;
587 free( p_access->psz_location );
588 p_access->psz_location = strdup( p_sys->psz_location );
589 /* Clean up current Open() run */
590 vlc_UrlClean( &p_sys->url );
591 http_auth_Reset( &p_sys->auth );
592 vlc_UrlClean( &p_sys->proxy );
593 free( p_sys->psz_proxy_passbuf );
594 http_auth_Reset( &p_sys->proxy_auth );
595 free( p_sys->psz_mime );
596 free( p_sys->psz_pragma );
597 free( p_sys->psz_location );
598 free( p_sys->psz_user_agent );
600 Disconnect( p_access );
601 cookies = p_sys->cookies;
602 #ifdef HAVE_ZLIB_H
603 inflateEnd( &p_sys->inflate.stream );
604 #endif
605 free( p_sys );
607 /* Do new Open() run with new data */
608 return OpenWithCookies( p_this, psz_protocol, i_nb_redirect + 1,
609 i_max_redirect, cookies );
612 if( p_sys->b_mms )
614 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
615 goto error;
618 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
620 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
622 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
623 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
625 free( p_access->psz_demux );
626 p_access->psz_demux = strdup( "nsv" );
628 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
629 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
631 free( p_access->psz_demux );
632 p_access->psz_demux = strdup( "m4a" );
634 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
636 free( p_access->psz_demux );
637 p_access->psz_demux = strdup( "mp3" );
640 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
641 p_access->psz_demux );
643 #if 0 /* Doesn't work really well because of the pre-buffering in
644 * shoutcast servers (the buffer content will be sent as fast as
645 * possible). */
646 p_sys->b_pace_control = false;
647 #endif
649 else if( !p_sys->psz_mime )
651 free( p_access->psz_demux );
652 /* Shoutcast */
653 p_access->psz_demux = strdup( "mp3" );
655 /* else probably Ogg Vorbis */
657 else if( !strcasecmp( psz_access, "unsv" ) &&
658 p_sys->psz_mime &&
659 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
661 free( p_access->psz_demux );
662 /* Grrrr! detect ultravox server and force NSV demuxer */
663 p_access->psz_demux = strdup( "nsv" );
665 else if( !strcmp( psz_access, "itpc" ) )
667 free( p_access->psz_demux );
668 p_access->psz_demux = strdup( "podcast" );
670 else if( p_sys->psz_mime &&
671 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
672 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
674 free( p_access->psz_demux );
675 p_access->psz_demux = strdup( "xspf-open" );
678 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
680 /* PTS delay */
681 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
683 return VLC_SUCCESS;
685 error:
686 vlc_UrlClean( &p_sys->url );
687 vlc_UrlClean( &p_sys->proxy );
688 free( p_sys->psz_proxy_passbuf );
689 free( p_sys->psz_mime );
690 free( p_sys->psz_pragma );
691 free( p_sys->psz_location );
692 free( p_sys->psz_user_agent );
694 Disconnect( p_access );
696 if( p_sys->cookies )
698 int i;
699 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
700 free(vlc_array_item_at_index( p_sys->cookies, i ));
701 vlc_array_destroy( p_sys->cookies );
704 #ifdef HAVE_ZLIB_H
705 inflateEnd( &p_sys->inflate.stream );
706 #endif
707 free( p_sys );
708 return VLC_EGENERIC;
711 /*****************************************************************************
712 * Close:
713 *****************************************************************************/
714 static void Close( vlc_object_t *p_this )
716 access_t *p_access = (access_t*)p_this;
717 access_sys_t *p_sys = p_access->p_sys;
719 vlc_UrlClean( &p_sys->url );
720 http_auth_Reset( &p_sys->auth );
721 vlc_UrlClean( &p_sys->proxy );
722 http_auth_Reset( &p_sys->proxy_auth );
724 free( p_sys->psz_mime );
725 free( p_sys->psz_pragma );
726 free( p_sys->psz_location );
728 free( p_sys->psz_icy_name );
729 free( p_sys->psz_icy_genre );
730 free( p_sys->psz_icy_title );
732 free( p_sys->psz_user_agent );
734 Disconnect( p_access );
736 if( p_sys->cookies )
738 int i;
739 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
740 free(vlc_array_item_at_index( p_sys->cookies, i ));
741 vlc_array_destroy( p_sys->cookies );
744 #ifdef HAVE_ZLIB_H
745 inflateEnd( &p_sys->inflate.stream );
746 free( p_sys->inflate.p_buffer );
747 #endif
749 free( p_sys );
752 /*****************************************************************************
753 * Read: Read up to i_len bytes from the http connection and place in
754 * p_buffer. Return the actual number of bytes read
755 *****************************************************************************/
756 static int ReadICYMeta( access_t *p_access );
757 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
759 access_sys_t *p_sys = p_access->p_sys;
760 int i_read;
762 if( p_sys->fd == -1 )
764 p_access->info.b_eof = true;
765 return 0;
768 if( p_sys->b_has_size )
770 /* Remaining bytes in the file */
771 uint64_t remainder = p_access->info.i_size - p_access->info.i_pos;
772 if( remainder < i_len )
773 i_len = remainder;
775 /* Remaining bytes in the response */
776 if( p_sys->i_remaining < i_len )
777 i_len = p_sys->i_remaining;
780 if( p_sys->b_chunked )
782 if( p_sys->i_chunk < 0 )
784 p_access->info.b_eof = true;
785 return 0;
788 if( p_sys->i_chunk <= 0 )
790 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
791 /* read the chunk header */
792 if( psz == NULL )
794 /* fatal error - end of file */
795 msg_Dbg( p_access, "failed reading chunk-header line" );
796 return 0;
798 p_sys->i_chunk = strtoll( psz, NULL, 16 );
799 free( psz );
801 if( p_sys->i_chunk <= 0 ) /* eof */
803 p_sys->i_chunk = -1;
804 p_access->info.b_eof = true;
805 return 0;
809 if( i_len > p_sys->i_chunk )
810 i_len = p_sys->i_chunk;
813 if( i_len == 0 )
815 p_access->info.b_eof = true;
816 return 0;
819 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos-p_sys->i_icy_offset > 0 )
821 int64_t i_next = p_sys->i_icy_meta -
822 (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
824 if( i_next == p_sys->i_icy_meta )
826 if( ReadICYMeta( p_access ) )
828 p_access->info.b_eof = true;
829 return -1;
832 if( i_len > i_next )
833 i_len = i_next;
836 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
838 if( i_read > 0 )
840 if( p_sys->b_chunked )
842 p_sys->i_chunk -= i_read;
843 if( p_sys->i_chunk <= 0 )
845 /* read the empty line */
846 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
847 free( psz );
851 else
854 * I very much doubt that this will work.
855 * If i_read == 0, the connection *IS* dead, so the only
856 * sensible thing to do is Disconnect() and then retry.
857 * Otherwise, I got recv() completely wrong. -- Courmisch
859 if( p_sys->b_continuous )
861 Request( p_access, 0 );
862 p_sys->b_continuous = false;
863 i_read = Read( p_access, p_buffer, i_len );
864 p_sys->b_continuous = true;
866 Disconnect( p_access );
867 if( p_sys->b_reconnect && vlc_object_alive( p_access ) )
869 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
870 if( Connect( p_access, p_access->info.i_pos ) )
872 msg_Dbg( p_access, "reconnection failed" );
874 else
876 p_sys->b_reconnect = false;
877 i_read = Read( p_access, p_buffer, i_len );
878 p_sys->b_reconnect = true;
880 return i_read;
884 if( i_read <= 0 )
886 p_access->info.b_eof = true;
887 if( i_read < 0 )
888 p_sys->b_error = true;
889 return 0;
893 assert( i_read >= 0 );
894 p_access->info.i_pos += i_read;
895 if( p_sys->b_has_size )
897 assert( p_access->info.i_pos <= p_access->info.i_size );
898 assert( (unsigned)i_read <= p_sys->i_remaining );
899 p_sys->i_remaining -= i_read;
902 return i_read;
905 static int ReadICYMeta( access_t *p_access )
907 access_sys_t *p_sys = p_access->p_sys;
909 uint8_t buffer;
910 char *p, *psz_meta;
911 int i_read;
913 /* Read meta data length */
914 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
915 true );
916 if( i_read <= 0 )
917 return VLC_EGENERIC;
918 if( buffer == 0 )
919 return VLC_SUCCESS;
921 i_read = buffer << 4;
922 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
924 psz_meta = malloc( i_read + 1 );
925 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
926 (uint8_t *)psz_meta, i_read, true ) != i_read )
928 free( psz_meta );
929 return VLC_EGENERIC;
932 psz_meta[i_read] = '\0'; /* Just in case */
934 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
936 /* Now parse the meta */
937 /* Look for StreamTitle= */
938 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
939 if( p )
941 p += strlen( "StreamTitle=" );
942 if( *p == '\'' || *p == '"' )
944 char closing[] = { p[0], ';', '\0' };
945 char *psz = strstr( &p[1], closing );
946 if( !psz )
947 psz = strchr( &p[1], ';' );
949 if( psz ) *psz = '\0';
951 else
953 char *psz = strchr( &p[1], ';' );
954 if( psz ) *psz = '\0';
957 if( !p_sys->psz_icy_title ||
958 strcmp( p_sys->psz_icy_title, &p[1] ) )
960 free( p_sys->psz_icy_title );
961 char *psz_tmp = strdup( &p[1] );
962 p_sys->psz_icy_title = EnsureUTF8( psz_tmp );
963 if( !p_sys->psz_icy_title )
964 free( psz_tmp );
965 p_access->info.i_update |= INPUT_UPDATE_META;
967 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
970 free( psz_meta );
972 return VLC_SUCCESS;
975 #ifdef HAVE_ZLIB_H
976 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
977 size_t i_len )
979 access_sys_t *p_sys = p_access->p_sys;
981 if( p_sys->b_compressed )
983 int i_ret;
985 if( !p_sys->inflate.p_buffer )
986 p_sys->inflate.p_buffer = malloc( 256 * 1024 );
988 if( p_sys->inflate.stream.avail_in == 0 )
990 ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer, 256 * 1024 );
991 if( i_read <= 0 ) return i_read;
992 p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
993 p_sys->inflate.stream.avail_in = i_read;
996 p_sys->inflate.stream.avail_out = i_len;
997 p_sys->inflate.stream.next_out = p_buffer;
999 i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
1000 msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
1002 return i_len - p_sys->inflate.stream.avail_out;
1004 else
1006 return Read( p_access, p_buffer, i_len );
1009 #endif
1011 /*****************************************************************************
1012 * Seek: close and re-open a connection at the right place
1013 *****************************************************************************/
1014 static int Seek( access_t *p_access, uint64_t i_pos )
1016 msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
1018 Disconnect( p_access );
1020 if( p_access->info.i_size
1021 && i_pos >= p_access->info.i_size ) {
1022 msg_Err( p_access, "seek to far" );
1023 int retval = Seek( p_access, p_access->info.i_size - 1 );
1024 if( retval == VLC_SUCCESS ) {
1025 uint8_t p_buffer[2];
1026 Read( p_access, p_buffer, 1);
1027 p_access->info.b_eof = false;
1029 return retval;
1031 if( Connect( p_access, i_pos ) )
1033 msg_Err( p_access, "seek failed" );
1034 p_access->info.b_eof = true;
1035 return VLC_EGENERIC;
1037 return VLC_SUCCESS;
1040 /*****************************************************************************
1041 * Control:
1042 *****************************************************************************/
1043 static int Control( access_t *p_access, int i_query, va_list args )
1045 access_sys_t *p_sys = p_access->p_sys;
1046 bool *pb_bool;
1047 int64_t *pi_64;
1048 vlc_meta_t *p_meta;
1050 switch( i_query )
1052 /* */
1053 case ACCESS_CAN_SEEK:
1054 pb_bool = (bool*)va_arg( args, bool* );
1055 *pb_bool = p_sys->b_seekable;
1056 break;
1057 case ACCESS_CAN_FASTSEEK:
1058 pb_bool = (bool*)va_arg( args, bool* );
1059 *pb_bool = false;
1060 break;
1061 case ACCESS_CAN_PAUSE:
1062 case ACCESS_CAN_CONTROL_PACE:
1063 pb_bool = (bool*)va_arg( args, bool* );
1065 #if 0 /* Disable for now until we have a clock synchro algo
1066 * which works with something else than MPEG over UDP */
1067 *pb_bool = p_sys->b_pace_control;
1068 #endif
1069 *pb_bool = true;
1070 break;
1072 /* */
1073 case ACCESS_GET_PTS_DELAY:
1074 pi_64 = (int64_t*)va_arg( args, int64_t * );
1075 *pi_64 = var_GetInteger( p_access, "http-caching" ) * 1000;
1076 break;
1078 /* */
1079 case ACCESS_SET_PAUSE_STATE:
1080 break;
1082 case ACCESS_GET_META:
1083 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1085 if( p_sys->psz_icy_name )
1086 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
1087 if( p_sys->psz_icy_genre )
1088 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
1089 if( p_sys->psz_icy_title )
1090 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
1091 break;
1093 case ACCESS_GET_CONTENT_TYPE:
1094 *va_arg( args, char ** ) =
1095 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
1096 break;
1098 case ACCESS_GET_TITLE_INFO:
1099 case ACCESS_SET_TITLE:
1100 case ACCESS_SET_SEEKPOINT:
1101 case ACCESS_SET_PRIVATE_ID_STATE:
1102 return VLC_EGENERIC;
1104 default:
1105 msg_Warn( p_access, "unimplemented query in control" );
1106 return VLC_EGENERIC;
1109 return VLC_SUCCESS;
1112 /*****************************************************************************
1113 * Connect:
1114 *****************************************************************************/
1115 static int Connect( access_t *p_access, uint64_t i_tell )
1117 access_sys_t *p_sys = p_access->p_sys;
1118 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
1120 /* Clean info */
1121 free( p_sys->psz_location );
1122 free( p_sys->psz_mime );
1123 free( p_sys->psz_pragma );
1125 free( p_sys->psz_icy_genre );
1126 free( p_sys->psz_icy_name );
1127 free( p_sys->psz_icy_title );
1130 p_sys->psz_location = NULL;
1131 p_sys->psz_mime = NULL;
1132 p_sys->psz_pragma = NULL;
1133 p_sys->b_mms = false;
1134 p_sys->b_chunked = false;
1135 p_sys->i_chunk = 0;
1136 p_sys->i_icy_meta = 0;
1137 p_sys->i_icy_offset = i_tell;
1138 p_sys->psz_icy_name = NULL;
1139 p_sys->psz_icy_genre = NULL;
1140 p_sys->psz_icy_title = NULL;
1141 p_sys->i_remaining = 0;
1142 p_sys->b_persist = false;
1143 p_sys->b_has_size = false;
1144 p_access->info.i_size = 0;
1145 p_access->info.i_pos = i_tell;
1146 p_access->info.b_eof = false;
1148 /* Open connection */
1149 assert( p_sys->fd == -1 ); /* No open sockets (leaking fds is BAD) */
1150 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
1151 if( p_sys->fd == -1 )
1153 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
1154 return -1;
1156 setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));
1158 /* Initialize TLS/SSL session */
1159 if( p_sys->b_ssl == true )
1161 /* CONNECT to establish TLS tunnel through HTTP proxy */
1162 if( p_sys->b_proxy )
1164 char *psz;
1165 unsigned i_status = 0;
1167 if( p_sys->i_version == 0 )
1169 /* CONNECT is not in HTTP/1.0 */
1170 Disconnect( p_access );
1171 return -1;
1174 net_Printf( p_access, p_sys->fd, NULL,
1175 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
1176 p_sys->url.psz_host, p_sys->url.i_port,
1177 p_sys->i_version,
1178 p_sys->url.psz_host, p_sys->url.i_port);
1180 psz = net_Gets( p_access, p_sys->fd, NULL );
1181 if( psz == NULL )
1183 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
1184 Disconnect( p_access );
1185 return -1;
1188 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
1189 free( psz );
1191 if( ( i_status / 100 ) != 2 )
1193 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
1194 Disconnect( p_access );
1195 return -1;
1200 psz = net_Gets( p_access, p_sys->fd, NULL );
1201 if( psz == NULL )
1203 msg_Err( p_access, "HTTP proxy connection failed" );
1204 Disconnect( p_access );
1205 return -1;
1208 if( *psz == '\0' )
1209 i_status = 0;
1211 free( psz );
1213 if( !vlc_object_alive (p_access) || p_sys->b_error )
1215 Disconnect( p_access );
1216 return -1;
1219 while( i_status );
1222 /* TLS/SSL handshake */
1223 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
1224 p_sys->url.psz_host );
1225 if( p_sys->p_tls == NULL )
1227 msg_Err( p_access, "cannot establish HTTP/TLS session" );
1228 Disconnect( p_access );
1229 return -1;
1231 p_sys->p_vs = &p_sys->p_tls->sock;
1234 return Request( p_access, i_tell ) ? -2 : 0;
1238 static int Request( access_t *p_access, uint64_t i_tell )
1240 access_sys_t *p_sys = p_access->p_sys;
1241 char *psz ;
1242 v_socket_t *pvs = p_sys->p_vs;
1243 p_sys->b_persist = false;
1245 p_sys->i_remaining = 0;
1246 if( p_sys->b_proxy )
1248 if( p_sys->url.psz_path )
1250 net_Printf( p_access, p_sys->fd, NULL,
1251 "GET http://%s:%d%s HTTP/1.%d\r\n",
1252 p_sys->url.psz_host, p_sys->url.i_port,
1253 p_sys->url.psz_path, p_sys->i_version );
1255 else
1257 net_Printf( p_access, p_sys->fd, NULL,
1258 "GET http://%s:%d/ HTTP/1.%d\r\n",
1259 p_sys->url.psz_host, p_sys->url.i_port,
1260 p_sys->i_version );
1263 else
1265 const char *psz_path = p_sys->url.psz_path;
1266 if( !psz_path || !*psz_path )
1268 psz_path = "/";
1270 if( p_sys->url.i_port != (pvs ? 443 : 80) )
1272 net_Printf( p_access, p_sys->fd, pvs,
1273 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
1274 psz_path, p_sys->i_version, p_sys->url.psz_host,
1275 p_sys->url.i_port );
1277 else
1279 net_Printf( p_access, p_sys->fd, pvs,
1280 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
1281 psz_path, p_sys->i_version, p_sys->url.psz_host );
1284 /* User Agent */
1285 net_Printf( p_access, p_sys->fd, pvs,
1286 "User-Agent: %s\r\n",
1287 p_sys->psz_user_agent );
1288 /* Offset */
1289 if( p_sys->i_version == 1 && ! p_sys->b_continuous )
1291 p_sys->b_persist = true;
1292 net_Printf( p_access, p_sys->fd, pvs,
1293 "Range: bytes=%"PRIu64"-\r\n", i_tell );
1294 net_Printf( p_access, p_sys->fd, pvs, "Connection: close\r\n" );
1297 /* Cookies */
1298 if( p_sys->cookies )
1300 int i;
1301 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1303 const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1304 char * psz_cookie_content = cookie_get_content( cookie );
1305 char * psz_cookie_domain = cookie_get_domain( cookie );
1307 assert( psz_cookie_content );
1309 /* FIXME: This is clearly not conforming to the rfc */
1310 bool is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1312 if( is_in_right_domain )
1314 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1315 if( net_Printf( p_access, p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1316 msg_Err( p_access, "failed to send Cookie" );
1318 free( psz_cookie_content );
1319 free( psz_cookie_domain );
1323 /* Authentication */
1324 if( p_sys->url.psz_username || p_sys->url.psz_password )
1325 AuthReply( p_access, "", &p_sys->url, &p_sys->auth );
1327 /* Proxy Authentication */
1328 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1329 AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
1331 /* ICY meta data request */
1332 net_Printf( p_access, p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1335 if( net_Printf( p_access, p_sys->fd, pvs, "\r\n" ) < 0 )
1337 msg_Err( p_access, "failed to send request" );
1338 Disconnect( p_access );
1339 return VLC_EGENERIC;
1342 /* Read Answer */
1343 if( ( psz = net_Gets( p_access, p_sys->fd, pvs ) ) == NULL )
1345 msg_Err( p_access, "failed to read answer" );
1346 goto error;
1348 if( !strncmp( psz, "HTTP/1.", 7 ) )
1350 p_sys->psz_protocol = "HTTP";
1351 p_sys->i_code = atoi( &psz[9] );
1353 else if( !strncmp( psz, "ICY", 3 ) )
1355 p_sys->psz_protocol = "ICY";
1356 p_sys->i_code = atoi( &psz[4] );
1357 p_sys->b_reconnect = true;
1359 else
1361 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1362 free( psz );
1363 goto error;
1365 msg_Dbg( p_access, "protocol '%s' answer code %d",
1366 p_sys->psz_protocol, p_sys->i_code );
1367 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1369 p_sys->b_seekable = false;
1371 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1373 p_sys->b_seekable = false;
1375 /* Authentication error - We'll have to display the dialog */
1376 if( p_sys->i_code == 401 )
1380 /* Other fatal error */
1381 else if( p_sys->i_code >= 400 )
1383 msg_Err( p_access, "error: %s", psz );
1384 free( psz );
1385 goto error;
1387 free( psz );
1389 for( ;; )
1391 char *psz = net_Gets( p_access, p_sys->fd, pvs );
1392 char *p;
1394 if( psz == NULL )
1396 msg_Err( p_access, "failed to read answer" );
1397 goto error;
1400 if( !vlc_object_alive (p_access) || p_sys->b_error )
1402 free( psz );
1403 goto error;
1406 /* msg_Dbg( p_input, "Line=%s", psz ); */
1407 if( *psz == '\0' )
1409 free( psz );
1410 break;
1413 if( ( p = strchr( psz, ':' ) ) == NULL )
1415 msg_Err( p_access, "malformed header line: %s", psz );
1416 free( psz );
1417 goto error;
1419 *p++ = '\0';
1420 while( *p == ' ' ) p++;
1422 if( !strcasecmp( psz, "Content-Length" ) )
1424 uint64_t i_size = i_tell + (p_sys->i_remaining = (uint64_t)atoll( p ));
1425 if(i_size > p_access->info.i_size) {
1426 p_sys->b_has_size = true;
1427 p_access->info.i_size = i_size;
1429 msg_Dbg( p_access, "this frame size=%"PRIu64, p_sys->i_remaining );
1431 else if( !strcasecmp( psz, "Content-Range" ) ) {
1432 uint64_t i_ntell = i_tell;
1433 uint64_t i_nend = (p_access->info.i_size > 0)?(p_access->info.i_size - 1):i_tell;
1434 uint64_t i_nsize = p_access->info.i_size;
1435 sscanf(p,"bytes %"SCNu64"-%"SCNu64"/%"SCNu64,&i_ntell,&i_nend,&i_nsize);
1436 if(i_nend > i_ntell ) {
1437 p_access->info.i_pos = i_ntell;
1438 p_sys->i_icy_offset = i_ntell;
1439 p_sys->i_remaining = i_nend+1-i_ntell;
1440 int64_t i_size = (i_nsize > i_nend) ? i_nsize : (i_nend + 1);
1441 if(i_size > p_access->info.i_size) {
1442 p_sys->b_has_size = true;
1443 p_access->info.i_size = i_size;
1445 msg_Dbg( p_access, "stream size=%"PRIu64",pos=%"PRIu64",remaining=%"PRIu64,
1446 i_nsize, i_ntell, p_sys->i_remaining);
1449 else if( !strcasecmp( psz, "Connection" ) ) {
1450 msg_Dbg( p_access, "Connection: %s",p );
1451 int i = -1;
1452 sscanf(p, "close%n",&i);
1453 if( i >= 0 ) {
1454 p_sys->b_persist = false;
1457 else if( !strcasecmp( psz, "Location" ) )
1459 char * psz_new_loc;
1461 /* This does not follow RFC 2068, but yet if the url is not absolute,
1462 * handle it as everyone does. */
1463 if( p[0] == '/' )
1465 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1467 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1469 if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1470 p_sys->url.psz_host, p) < 0 )
1471 goto error;
1473 else
1475 if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1476 p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1477 goto error;
1480 else
1482 psz_new_loc = strdup( p );
1485 free( p_sys->psz_location );
1486 p_sys->psz_location = psz_new_loc;
1488 else if( !strcasecmp( psz, "Content-Type" ) )
1490 free( p_sys->psz_mime );
1491 p_sys->psz_mime = strdup( p );
1492 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1494 else if( !strcasecmp( psz, "Content-Encoding" ) )
1496 msg_Dbg( p_access, "Content-Encoding: %s", p );
1497 if( !strcasecmp( p, "identity" ) )
1499 #ifdef HAVE_ZLIB_H
1500 else if( !strcasecmp( p, "gzip" ) || !strcasecmp( p, "deflate" ) )
1501 p_sys->b_compressed = true;
1502 #endif
1503 else
1504 msg_Warn( p_access, "Unknown content coding: %s", p );
1506 else if( !strcasecmp( psz, "Pragma" ) )
1508 if( !strcasecmp( psz, "Pragma: features" ) )
1509 p_sys->b_mms = true;
1510 free( p_sys->psz_pragma );
1511 p_sys->psz_pragma = strdup( p );
1512 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1514 else if( !strcasecmp( psz, "Server" ) )
1516 msg_Dbg( p_access, "Server: %s", p );
1517 if( !strncasecmp( p, "Icecast", 7 ) ||
1518 !strncasecmp( p, "Nanocaster", 10 ) )
1520 /* Remember if this is Icecast
1521 * we need to force demux in this case without breaking
1522 * autodetection */
1524 /* Let live 365 streams (nanocaster) piggyback on the icecast
1525 * routine. They look very similar */
1527 p_sys->b_reconnect = true;
1528 p_sys->b_pace_control = false;
1529 p_sys->b_icecast = true;
1532 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1534 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1535 if( !strncasecmp( p, "chunked", 7 ) )
1537 p_sys->b_chunked = true;
1540 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1542 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1543 p_sys->i_icy_meta = atoi( p );
1544 if( p_sys->i_icy_meta < 0 )
1545 p_sys->i_icy_meta = 0;
1546 if( p_sys->i_icy_meta > 0 )
1547 p_sys->b_icecast = true;
1549 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1551 else if( !strcasecmp( psz, "Icy-Name" ) )
1553 free( p_sys->psz_icy_name );
1554 char *psz_tmp = strdup( p );
1555 p_sys->psz_icy_name = EnsureUTF8( psz_tmp );
1556 if( !p_sys->psz_icy_name )
1557 free( psz_tmp );
1558 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1560 p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
1561 p_sys->b_reconnect = true;
1562 p_sys->b_pace_control = false;
1564 else if( !strcasecmp( psz, "Icy-Genre" ) )
1566 free( p_sys->psz_icy_genre );
1567 char *psz_tmp = strdup( p );
1568 p_sys->psz_icy_genre = EnsureUTF8( psz_tmp );
1569 if( !p_sys->psz_icy_genre )
1570 free( psz_tmp );
1571 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1573 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1575 msg_Dbg( p_access, "Icy-Notice: %s", p );
1577 else if( !strncasecmp( psz, "icy-", 4 ) ||
1578 !strncasecmp( psz, "ice-", 4 ) ||
1579 !strncasecmp( psz, "x-audiocast", 11 ) )
1581 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1583 else if( !strcasecmp( psz, "Set-Cookie" ) )
1585 if( p_sys->cookies )
1587 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1588 cookie_append( p_sys->cookies, strdup(p) );
1590 else
1591 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1593 else if( !strcasecmp( psz, "www-authenticate" ) )
1595 msg_Dbg( p_access, "Authentication header: %s", p );
1596 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1597 &p_sys->auth, p );
1599 else if( !strcasecmp( psz, "proxy-authenticate" ) )
1601 msg_Dbg( p_access, "Proxy authentication header: %s", p );
1602 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1603 &p_sys->proxy_auth, p );
1605 else if( !strcasecmp( psz, "authentication-info" ) )
1607 msg_Dbg( p_access, "Authentication Info header: %s", p );
1608 if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
1609 goto error;
1611 else if( !strcasecmp( psz, "proxy-authentication-info" ) )
1613 msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
1614 if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
1615 goto error;
1618 free( psz );
1620 /* We close the stream for zero length data, unless of course the
1621 * server has already promised to do this for us.
1623 if( p_sys->b_has_size && p_sys->i_remaining == 0 && p_sys->b_persist ) {
1624 Disconnect( p_access );
1626 return VLC_SUCCESS;
1628 error:
1629 Disconnect( p_access );
1630 return VLC_EGENERIC;
1633 /*****************************************************************************
1634 * Disconnect:
1635 *****************************************************************************/
1636 static void Disconnect( access_t *p_access )
1638 access_sys_t *p_sys = p_access->p_sys;
1640 if( p_sys->p_tls != NULL)
1642 tls_ClientDelete( p_sys->p_tls );
1643 p_sys->p_tls = NULL;
1644 p_sys->p_vs = NULL;
1646 if( p_sys->fd != -1)
1648 net_Close(p_sys->fd);
1649 p_sys->fd = -1;
1654 /*****************************************************************************
1655 * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1656 * them) (FIXME: only support the "domain=" param)
1657 *****************************************************************************/
1659 /* Get the NAME=VALUE part of the Cookie */
1660 static char * cookie_get_content( const char * cookie )
1662 char * ret = strdup( cookie );
1663 if( !ret ) return NULL;
1664 char * str = ret;
1665 /* Look for a ';' */
1666 while( *str && *str != ';' ) str++;
1667 /* Replace it by a end-char */
1668 if( *str == ';' ) *str = 0;
1669 return ret;
1672 /* Get the domain where the cookie is stored */
1673 static char * cookie_get_domain( const char * cookie )
1675 const char * str = cookie;
1676 static const char domain[] = "domain=";
1677 if( !str )
1678 return NULL;
1679 /* Look for a ';' */
1680 while( *str )
1682 if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1684 str += sizeof(domain) - 1 /* minus \0 */;
1685 char * ret = strdup( str );
1686 /* Now remove the next ';' if present */
1687 char * ret_iter = ret;
1688 while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1689 if( *ret_iter == ';' )
1690 *ret_iter = 0;
1691 return ret;
1693 /* Go to next ';' field */
1694 while( *str && *str != ';' ) str++;
1695 if( *str == ';' ) str++;
1696 /* skip blank */
1697 while( *str && *str == ' ' ) str++;
1699 return NULL;
1702 /* Get NAME in the NAME=VALUE field */
1703 static char * cookie_get_name( const char * cookie )
1705 char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1706 if( !ret ) return NULL;
1707 char * str = ret;
1708 while( *str && *str != '=' ) str++;
1709 *str = 0;
1710 return ret;
1713 /* Add a cookie in cookies, checking to see how it should be added */
1714 static void cookie_append( vlc_array_t * cookies, char * cookie )
1716 int i;
1718 if( !cookie )
1719 return;
1721 char * cookie_name = cookie_get_name( cookie );
1723 /* Don't send invalid cookies */
1724 if( !cookie_name )
1725 return;
1727 char * cookie_domain = cookie_get_domain( cookie );
1728 for( i = 0; i < vlc_array_count( cookies ); i++ )
1730 char * current_cookie = vlc_array_item_at_index( cookies, i );
1731 char * current_cookie_name = cookie_get_name( current_cookie );
1732 char * current_cookie_domain = cookie_get_domain( current_cookie );
1734 assert( current_cookie_name );
1736 bool is_domain_matching = ( cookie_domain && current_cookie_domain &&
1737 !strcmp( cookie_domain, current_cookie_domain ) );
1739 if( is_domain_matching && !strcmp( cookie_name, current_cookie_name ) )
1741 /* Remove previous value for this cookie */
1742 free( current_cookie );
1743 vlc_array_remove( cookies, i );
1745 /* Clean */
1746 free( current_cookie_name );
1747 free( current_cookie_domain );
1748 break;
1750 free( current_cookie_name );
1751 free( current_cookie_domain );
1753 free( cookie_name );
1754 free( cookie_domain );
1755 vlc_array_append( cookies, cookie );
1759 /*****************************************************************************
1760 * HTTP authentication
1761 *****************************************************************************/
1763 static void AuthReply( access_t *p_access, const char *psz_prefix,
1764 vlc_url_t *p_url, http_auth_t *p_auth )
1766 access_sys_t *p_sys = p_access->p_sys;
1767 char *psz_value;
1769 psz_value =
1770 http_auth_FormatAuthorizationHeader( VLC_OBJECT(p_access), p_auth,
1771 "GET", p_url->psz_path,
1772 p_url->psz_username,
1773 p_url->psz_password );
1774 if ( psz_value == NULL )
1775 return;
1777 net_Printf( p_access, p_sys->fd, p_sys->p_vs,
1778 "%sAuthorization: %s\r\n", psz_prefix, psz_value );
1779 free( psz_value );
1782 static int AuthCheckReply( access_t *p_access, const char *psz_header,
1783 vlc_url_t *p_url, http_auth_t *p_auth )
1785 return
1786 http_auth_ParseAuthenticationInfoHeader( VLC_OBJECT(p_access), p_auth,
1787 psz_header, "",
1788 p_url->psz_path,
1789 p_url->psz_username,
1790 p_url->psz_password );