access_http: use the user agent provided by the user: fix #4162
[vlc/asuraparaju-public.git] / modules / access / http.c
blob398c38b0d7ef16075571a3af06564913251cb49a
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 AGENT_TEXT N_("HTTP user agent")
87 #define AGENT_LONGTEXT N_("User agent that will be " \
88 "used for the connection.")
90 #define RECONNECT_TEXT N_("Auto re-connect")
91 #define RECONNECT_LONGTEXT N_( \
92 "Automatically try to reconnect to the stream in case of a sudden " \
93 "disconnect." )
95 #define CONTINUOUS_TEXT N_("Continuous stream")
96 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
97 "being constantly updated (for example, a JPG file on a server). " \
98 "You should not globally enable this option as it will break all other " \
99 "types of HTTP streams." )
101 #define FORWARD_COOKIES_TEXT N_("Forward Cookies")
102 #define FORWARD_COOKIES_LONGTEXT N_("Forward Cookies across http redirections.")
104 #define MAX_REDIRECT_TEXT N_("Max number of redirection")
105 #define MAX_REDIRECT_LONGTEXT N_("Limit the number of redirection to follow.")
107 #define USE_IE_PROXY_TEXT N_("Use Internet Explorer entered HTTP proxy server")
108 #define USE_IE_PROXY_LONGTEXT N_("Use Internet Explorer entered HTTP proxy " \
109 "server for all URL. Don't take into account bypasses settings and auto " \
110 "configuration scripts.")
112 vlc_module_begin ()
113 set_description( N_("HTTP input") )
114 set_capability( "access", 0 )
115 set_shortname( N_( "HTTP(S)" ) )
116 set_category( CAT_INPUT )
117 set_subcategory( SUBCAT_INPUT_ACCESS )
119 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
120 false )
121 add_password( "http-proxy-pwd", NULL, NULL,
122 PROXY_PASS_TEXT, PROXY_PASS_LONGTEXT, false )
123 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
124 CACHING_TEXT, CACHING_LONGTEXT, true )
125 change_safe()
126 add_string( "http-user-agent", PACKAGE_NAME"/"PACKAGE_VERSION, NULL,
127 AGENT_TEXT, AGENT_LONGTEXT, true )
128 change_safe()
129 change_need_restart()
130 add_bool( "http-reconnect", false, NULL, RECONNECT_TEXT,
131 RECONNECT_LONGTEXT, true )
132 add_bool( "http-continuous", false, NULL, CONTINUOUS_TEXT,
133 CONTINUOUS_LONGTEXT, true )
134 change_safe()
135 add_bool( "http-forward-cookies", true, NULL, FORWARD_COOKIES_TEXT,
136 FORWARD_COOKIES_LONGTEXT, true )
137 add_integer( "http-max-redirect", 5, NULL, MAX_REDIRECT_TEXT,
138 MAX_REDIRECT_LONGTEXT, true )
139 #ifdef WIN32
140 add_bool( "http-use-IE-proxy", false, NULL, USE_IE_PROXY_TEXT,
141 USE_IE_PROXY_LONGTEXT, true )
142 #endif
143 add_obsolete_string("http-user")
144 add_obsolete_string("http-pwd")
145 /* 'itpc' = iTunes Podcast */
146 add_shortcut( "http", "https", "unsv", "itpc", "icyx" )
147 set_callbacks( Open, Close )
148 vlc_module_end ()
150 /*****************************************************************************
151 * Local prototypes
152 *****************************************************************************/
154 struct access_sys_t
156 int fd;
157 bool b_error;
158 tls_session_t *p_tls;
159 v_socket_t *p_vs;
161 /* From uri */
162 vlc_url_t url;
163 char *psz_user_agent;
164 http_auth_t auth;
166 /* Proxy */
167 bool b_proxy;
168 vlc_url_t proxy;
169 http_auth_t proxy_auth;
170 char *psz_proxy_passbuf;
172 /* */
173 int i_code;
174 const char *psz_protocol;
175 int i_version;
177 char *psz_mime;
178 char *psz_pragma;
179 char *psz_location;
180 bool b_mms;
181 bool b_icecast;
182 bool b_ssl;
183 #ifdef HAVE_ZLIB_H
184 bool b_compressed;
185 struct
187 z_stream stream;
188 uint8_t *p_buffer;
189 } inflate;
190 #endif
192 bool b_chunked;
193 int64_t i_chunk;
195 int i_icy_meta;
196 uint64_t i_icy_offset;
197 char *psz_icy_name;
198 char *psz_icy_genre;
199 char *psz_icy_title;
201 uint64_t i_remaining;
203 bool b_seekable;
204 bool b_reconnect;
205 bool b_continuous;
206 bool b_pace_control;
207 bool b_persist;
208 bool b_has_size;
210 vlc_array_t * cookies;
213 /* */
214 static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
215 int i_nb_redirect, int i_max_redirect,
216 vlc_array_t *cookies );
218 /* */
219 static ssize_t Read( access_t *, uint8_t *, size_t );
220 static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
221 static int Seek( access_t *, uint64_t );
222 static int Control( access_t *, int, va_list );
224 /* */
225 static int Connect( access_t *, uint64_t );
226 static int Request( access_t *p_access, uint64_t i_tell );
227 static void Disconnect( access_t * );
229 /* Small Cookie utilities. Cookies support is partial. */
230 static char * cookie_get_content( const char * cookie );
231 static char * cookie_get_domain( const char * cookie );
232 static char * cookie_get_name( const char * cookie );
233 static void cookie_append( vlc_array_t * cookies, char * cookie );
236 static void AuthReply( access_t *p_acces, const char *psz_prefix,
237 vlc_url_t *p_url, http_auth_t *p_auth );
238 static int AuthCheckReply( access_t *p_access, const char *psz_header,
239 vlc_url_t *p_url, http_auth_t *p_auth );
241 /*****************************************************************************
242 * Open:
243 *****************************************************************************/
244 static int Open( vlc_object_t *p_this )
246 access_t *p_access = (access_t*)p_this;
247 return OpenWithCookies( p_this, p_access->psz_access, 0,
248 var_InheritInteger( p_access, "http-max-redirect" ), NULL );
252 * Open the given url using the given cookies
253 * @param p_this: the vlc object
254 * @psz_access: the acces to use (http, https, ...) (this value must be used
255 * instead of p_access->psz_access)
256 * @i_nb_redirect: the number of redirection already done
257 * @i_max_redirect: limit to the number of redirection to follow
258 * @cookies: the available cookies
259 * @return vlc error codes
261 static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
262 int i_nb_redirect, int i_max_redirect,
263 vlc_array_t *cookies )
265 access_t *p_access = (access_t*)p_this;
266 access_sys_t *p_sys;
267 char *psz, *p;
269 /* Only forward an store cookies if the corresponding option is activated */
270 bool b_forward_cookies = var_InheritBool( p_access, "http-forward-cookies" );
271 vlc_array_t * saved_cookies = b_forward_cookies ? (cookies ? cookies : vlc_array_new()) : NULL;
273 /* Set up p_access */
274 STANDARD_READ_ACCESS_INIT;
275 #ifdef HAVE_ZLIB_H
276 p_access->pf_read = ReadCompressed;
277 #endif
278 p_sys->fd = -1;
279 p_sys->b_proxy = false;
280 p_sys->psz_proxy_passbuf = NULL;
281 p_sys->i_version = 1;
282 p_sys->b_seekable = true;
283 p_sys->psz_mime = NULL;
284 p_sys->psz_pragma = NULL;
285 p_sys->b_mms = false;
286 p_sys->b_icecast = false;
287 p_sys->psz_location = NULL;
288 p_sys->psz_user_agent = NULL;
289 p_sys->b_pace_control = true;
290 p_sys->b_ssl = false;
291 #ifdef HAVE_ZLIB_H
292 p_sys->b_compressed = false;
293 /* 15 is the max windowBits, +32 to enable optional gzip decoding */
294 if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
295 msg_Warn( p_access, "Error during zlib initialisation: %s",
296 p_sys->inflate.stream.msg );
297 if( zlibCompileFlags() & (1<<17) )
298 msg_Warn( p_access, "Your zlib was compiled without gzip support." );
299 p_sys->inflate.p_buffer = NULL;
300 #endif
301 p_sys->p_tls = NULL;
302 p_sys->p_vs = NULL;
303 p_sys->i_icy_meta = 0;
304 p_sys->i_icy_offset = 0;
305 p_sys->psz_icy_name = NULL;
306 p_sys->psz_icy_genre = NULL;
307 p_sys->psz_icy_title = NULL;
308 p_sys->i_remaining = 0;
309 p_sys->b_persist = false;
310 p_sys->b_has_size = false;
311 p_access->info.i_size = 0;
312 p_access->info.i_pos = 0;
313 p_access->info.b_eof = false;
315 p_sys->cookies = saved_cookies;
317 http_auth_Init( &p_sys->auth );
318 http_auth_Init( &p_sys->proxy_auth );
320 /* Parse URI - remove spaces */
321 p = psz = strdup( p_access->psz_location );
322 while( (p = strchr( p, ' ' )) != NULL )
323 *p = '+';
324 vlc_UrlParse( &p_sys->url, psz, 0 );
325 free( psz );
327 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
329 msg_Warn( p_access, "invalid host" );
330 goto error;
332 if( !strncmp( psz_access, "https", 5 ) )
334 /* HTTP over SSL */
335 p_sys->b_ssl = true;
336 if( p_sys->url.i_port <= 0 )
337 p_sys->url.i_port = 443;
339 else
341 if( p_sys->url.i_port <= 0 )
342 p_sys->url.i_port = 80;
345 /* Determine the HTTP user agent */
346 /* See RFC2616 §2.2 token definition and §3.8 user-agent header */
347 p_sys->psz_user_agent = var_InheritString( p_access, "http-user-agent" );
348 for( char *p = p_sys->psz_user_agent; *p; p++ )
350 uint8_t c = *p;
351 if( c < 32 || strchr( "()<>@,;:\\\"[]?={}", c ) )
352 *p = '_'; /* remove potentially harmful characters */
355 /* Check proxy */
356 psz = var_InheritString( p_access, "http-proxy" );
357 if( psz )
359 p_sys->b_proxy = true;
360 vlc_UrlParse( &p_sys->proxy, psz, 0 );
361 free( psz );
363 #ifdef HAVE_LIBPROXY
364 else
366 pxProxyFactory *pf = px_proxy_factory_new();
367 if (pf)
369 char *buf;
370 int i;
371 i=asprintf(&buf, "%s://%s", psz_access, p_access->psz_location);
372 if (i >= 0)
374 msg_Dbg(p_access, "asking libproxy about url '%s'", buf);
375 char **proxies = px_proxy_factory_get_proxies(pf, buf);
376 if (proxies[0])
378 msg_Dbg(p_access, "libproxy suggest to use '%s'", proxies[0]);
379 if(strcmp(proxies[0],"direct://") != 0)
381 p_sys->b_proxy = true;
382 vlc_UrlParse( &p_sys->proxy, proxies[0], 0);
385 for(i=0;proxies[i];i++) free(proxies[i]);
386 free(proxies);
387 free(buf);
389 px_proxy_factory_free(pf);
391 else
393 msg_Err(p_access, "Allocating memory for libproxy failed");
396 #elif defined( WIN32 )
397 else
399 if( var_InheritBool( p_access, "http-use-IE-proxy" ) )
401 /* Try to get the proxy server address from Windows internet settings using registry. */
402 HKEY h_key;
403 /* Open the key */
404 if( RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft" \
405 "\\Windows\\CurrentVersion\\Internet Settings",
406 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
408 DWORD i_dataReadSize = 4; /* sizeof( DWORD ); */
409 DWORD proxyEnable = 0;
410 /* Get the proxy enable value */
411 if( RegQueryValueEx( h_key, "ProxyEnable", NULL, NULL,
412 (char *)&proxyEnable, &i_dataReadSize )
413 == ERROR_SUCCESS )
415 if( proxyEnable )
417 /* Proxy is enable */
418 char psz_key[256];
419 i_dataReadSize = 256;
420 if( RegQueryValueEx( h_key, "ProxyServer",
421 NULL, NULL, psz_key,
422 &i_dataReadSize )
423 == ERROR_SUCCESS )
425 /* Get the proxy URL :
426 Proxy server value in the registry can be something like "address:port"
427 or "ftp=address1:port1;http=address2:port2 ..." depending of the
428 confirguration. */
429 char *psz_proxy;
430 psz_proxy = strstr( psz_key, "http=" );
431 if( psz_proxy != NULL )
433 psz_proxy += strlen( "http=" );
434 char *psz_endUrl = strchr( psz_proxy, ';' );
435 if( psz_endUrl != NULL )
436 *psz_endUrl = '\0';
438 else
439 psz_proxy = psz_key;
440 /* Set proxy enable for this connection. */
441 p_sys->b_proxy = true;
442 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
444 msg_Warn( p_access, "Couldn't read in registry " \
445 "the proxy server address." );
448 else
449 msg_Warn( p_access, "Couldn't read in registry if the " \
450 "proxy is enable or not." );
452 else
453 msg_Warn( p_access, "Couldn't open internet settings key " \
454 "in registry." );
457 #elif defined( HAVE_GETENV )
458 else
460 psz = getenv( "http_proxy" );
461 if( psz )
463 p_sys->b_proxy = true;
464 vlc_UrlParse( &p_sys->proxy, psz, 0 );
467 #endif
469 if( psz ) /* No, this is NOT a use-after-free error */
471 psz = var_InheritString( p_access, "http-proxy-pwd" );
472 if( psz )
473 p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
476 if( p_sys->b_proxy )
478 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
480 msg_Warn( p_access, "invalid proxy host" );
481 goto error;
483 if( p_sys->proxy.i_port <= 0 )
485 p_sys->proxy.i_port = 80;
489 msg_Dbg( p_access, "http: server='%s' port=%d file='%s'",
490 p_sys->url.psz_host, p_sys->url.i_port,
491 p_sys->url.psz_path != NULL ? p_sys->url.psz_path : "" );
492 if( p_sys->b_proxy )
494 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
495 p_sys->proxy.i_port );
497 if( p_sys->url.psz_username && *p_sys->url.psz_username )
499 msg_Dbg( p_access, " user='%s'", p_sys->url.psz_username );
502 p_sys->b_reconnect = var_InheritBool( p_access, "http-reconnect" );
503 p_sys->b_continuous = var_InheritBool( p_access, "http-continuous" );
505 connect:
506 /* Connect */
507 switch( Connect( p_access, 0 ) )
509 case -1:
510 goto error;
512 case -2:
513 /* Retry with http 1.0 */
514 msg_Dbg( p_access, "switching to HTTP version 1.0" );
515 p_sys->i_version = 0;
516 p_sys->b_seekable = false;
518 if( !vlc_object_alive (p_access) || Connect( p_access, 0 ) )
519 goto error;
521 #ifndef NDEBUG
522 case 0:
523 break;
525 default:
526 msg_Err( p_access, "You should not be here" );
527 abort();
528 #endif
531 if( p_sys->i_code == 401 )
533 char *psz_login, *psz_password;
534 /* FIXME ? */
535 if( p_sys->url.psz_username && p_sys->url.psz_password &&
536 p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
538 Disconnect( p_access );
539 goto connect;
541 msg_Dbg( p_access, "authentication failed for realm %s",
542 p_sys->auth.psz_realm );
543 dialog_Login( p_access, &psz_login, &psz_password,
544 _("HTTP authentication"),
545 _("Please enter a valid login name and a password for realm %s."),
546 p_sys->auth.psz_realm );
547 if( psz_login != NULL && psz_password != NULL )
549 msg_Dbg( p_access, "retrying with user=%s", psz_login );
550 p_sys->url.psz_username = psz_login;
551 p_sys->url.psz_password = psz_password;
552 Disconnect( p_access );
553 goto connect;
555 else
557 free( psz_login );
558 free( psz_password );
559 goto error;
563 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
564 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
565 p_sys->psz_location && *p_sys->psz_location )
567 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
569 /* Check the number of redirection already done */
570 if( i_nb_redirect >= i_max_redirect )
572 msg_Err( p_access, "Too many redirection: break potential infinite"
573 "loop" );
574 goto error;
578 /* Do not accept redirection outside of HTTP works */
579 const char *psz_protocol;
580 if( !strncmp( p_sys->psz_location, "http:", 5 ) )
581 psz_protocol = "http";
582 else if( !strncmp( p_sys->psz_location, "https:", 6 ) )
583 psz_protocol = "https";
584 else
586 msg_Err( p_access, "insecure redirection ignored" );
587 goto error;
589 free( p_access->psz_location );
590 p_access->psz_location = strdup( p_sys->psz_location );
591 /* Clean up current Open() run */
592 vlc_UrlClean( &p_sys->url );
593 http_auth_Reset( &p_sys->auth );
594 vlc_UrlClean( &p_sys->proxy );
595 free( p_sys->psz_proxy_passbuf );
596 http_auth_Reset( &p_sys->proxy_auth );
597 free( p_sys->psz_mime );
598 free( p_sys->psz_pragma );
599 free( p_sys->psz_location );
600 free( p_sys->psz_user_agent );
602 Disconnect( p_access );
603 cookies = p_sys->cookies;
604 #ifdef HAVE_ZLIB_H
605 inflateEnd( &p_sys->inflate.stream );
606 #endif
607 free( p_sys );
609 /* Do new Open() run with new data */
610 return OpenWithCookies( p_this, psz_protocol, i_nb_redirect + 1,
611 i_max_redirect, cookies );
614 if( p_sys->b_mms )
616 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
617 goto error;
620 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
622 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
624 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
625 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
627 free( p_access->psz_demux );
628 p_access->psz_demux = strdup( "nsv" );
630 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
631 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
633 free( p_access->psz_demux );
634 p_access->psz_demux = strdup( "m4a" );
636 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
638 free( p_access->psz_demux );
639 p_access->psz_demux = strdup( "mp3" );
642 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
643 p_access->psz_demux );
645 #if 0 /* Doesn't work really well because of the pre-buffering in
646 * shoutcast servers (the buffer content will be sent as fast as
647 * possible). */
648 p_sys->b_pace_control = false;
649 #endif
651 else if( !p_sys->psz_mime )
653 free( p_access->psz_demux );
654 /* Shoutcast */
655 p_access->psz_demux = strdup( "mp3" );
657 /* else probably Ogg Vorbis */
659 else if( !strcasecmp( psz_access, "unsv" ) &&
660 p_sys->psz_mime &&
661 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
663 free( p_access->psz_demux );
664 /* Grrrr! detect ultravox server and force NSV demuxer */
665 p_access->psz_demux = strdup( "nsv" );
667 else if( !strcmp( psz_access, "itpc" ) )
669 free( p_access->psz_demux );
670 p_access->psz_demux = strdup( "podcast" );
672 else if( p_sys->psz_mime &&
673 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
674 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
676 free( p_access->psz_demux );
677 p_access->psz_demux = strdup( "xspf-open" );
680 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
682 /* PTS delay */
683 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
685 return VLC_SUCCESS;
687 error:
688 vlc_UrlClean( &p_sys->url );
689 vlc_UrlClean( &p_sys->proxy );
690 free( p_sys->psz_proxy_passbuf );
691 free( p_sys->psz_mime );
692 free( p_sys->psz_pragma );
693 free( p_sys->psz_location );
694 free( p_sys->psz_user_agent );
696 Disconnect( p_access );
698 if( p_sys->cookies )
700 int i;
701 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
702 free(vlc_array_item_at_index( p_sys->cookies, i ));
703 vlc_array_destroy( p_sys->cookies );
706 #ifdef HAVE_ZLIB_H
707 inflateEnd( &p_sys->inflate.stream );
708 #endif
709 free( p_sys );
710 return VLC_EGENERIC;
713 /*****************************************************************************
714 * Close:
715 *****************************************************************************/
716 static void Close( vlc_object_t *p_this )
718 access_t *p_access = (access_t*)p_this;
719 access_sys_t *p_sys = p_access->p_sys;
721 vlc_UrlClean( &p_sys->url );
722 http_auth_Reset( &p_sys->auth );
723 vlc_UrlClean( &p_sys->proxy );
724 http_auth_Reset( &p_sys->proxy_auth );
726 free( p_sys->psz_mime );
727 free( p_sys->psz_pragma );
728 free( p_sys->psz_location );
730 free( p_sys->psz_icy_name );
731 free( p_sys->psz_icy_genre );
732 free( p_sys->psz_icy_title );
734 free( p_sys->psz_user_agent );
736 Disconnect( p_access );
738 if( p_sys->cookies )
740 int i;
741 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
742 free(vlc_array_item_at_index( p_sys->cookies, i ));
743 vlc_array_destroy( p_sys->cookies );
746 #ifdef HAVE_ZLIB_H
747 inflateEnd( &p_sys->inflate.stream );
748 free( p_sys->inflate.p_buffer );
749 #endif
751 free( p_sys );
754 /*****************************************************************************
755 * Read: Read up to i_len bytes from the http connection and place in
756 * p_buffer. Return the actual number of bytes read
757 *****************************************************************************/
758 static int ReadICYMeta( access_t *p_access );
759 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
761 access_sys_t *p_sys = p_access->p_sys;
762 int i_read;
764 if( p_sys->fd == -1 )
766 p_access->info.b_eof = true;
767 return 0;
770 if( p_sys->b_has_size )
772 /* Remaining bytes in the file */
773 uint64_t remainder = p_access->info.i_size - p_access->info.i_pos;
774 if( remainder < i_len )
775 i_len = remainder;
777 /* Remaining bytes in the response */
778 if( p_sys->i_remaining < i_len )
779 i_len = p_sys->i_remaining;
782 if( p_sys->b_chunked )
784 if( p_sys->i_chunk < 0 )
786 p_access->info.b_eof = true;
787 return 0;
790 if( p_sys->i_chunk <= 0 )
792 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
793 /* read the chunk header */
794 if( psz == NULL )
796 /* fatal error - end of file */
797 msg_Dbg( p_access, "failed reading chunk-header line" );
798 return 0;
800 p_sys->i_chunk = strtoll( psz, NULL, 16 );
801 free( psz );
803 if( p_sys->i_chunk <= 0 ) /* eof */
805 p_sys->i_chunk = -1;
806 p_access->info.b_eof = true;
807 return 0;
811 if( i_len > p_sys->i_chunk )
812 i_len = p_sys->i_chunk;
815 if( i_len == 0 )
817 p_access->info.b_eof = true;
818 return 0;
821 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos-p_sys->i_icy_offset > 0 )
823 int64_t i_next = p_sys->i_icy_meta -
824 (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
826 if( i_next == p_sys->i_icy_meta )
828 if( ReadICYMeta( p_access ) )
830 p_access->info.b_eof = true;
831 return -1;
834 if( i_len > i_next )
835 i_len = i_next;
838 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
840 if( i_read > 0 )
842 if( p_sys->b_chunked )
844 p_sys->i_chunk -= i_read;
845 if( p_sys->i_chunk <= 0 )
847 /* read the empty line */
848 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
849 free( psz );
853 else
856 * I very much doubt that this will work.
857 * If i_read == 0, the connection *IS* dead, so the only
858 * sensible thing to do is Disconnect() and then retry.
859 * Otherwise, I got recv() completely wrong. -- Courmisch
861 if( p_sys->b_continuous )
863 Request( p_access, 0 );
864 p_sys->b_continuous = false;
865 i_read = Read( p_access, p_buffer, i_len );
866 p_sys->b_continuous = true;
868 Disconnect( p_access );
869 if( p_sys->b_reconnect && vlc_object_alive( p_access ) )
871 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
872 if( Connect( p_access, p_access->info.i_pos ) )
874 msg_Dbg( p_access, "reconnection failed" );
876 else
878 p_sys->b_reconnect = false;
879 i_read = Read( p_access, p_buffer, i_len );
880 p_sys->b_reconnect = true;
882 return i_read;
886 if( i_read <= 0 )
888 p_access->info.b_eof = true;
889 if( i_read < 0 )
890 p_sys->b_error = true;
891 return 0;
895 assert( i_read >= 0 );
896 p_access->info.i_pos += i_read;
897 if( p_sys->b_has_size )
899 assert( p_access->info.i_pos <= p_access->info.i_size );
900 assert( (unsigned)i_read <= p_sys->i_remaining );
901 p_sys->i_remaining -= i_read;
904 return i_read;
907 static int ReadICYMeta( access_t *p_access )
909 access_sys_t *p_sys = p_access->p_sys;
911 uint8_t buffer;
912 char *p, *psz_meta;
913 int i_read;
915 /* Read meta data length */
916 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
917 true );
918 if( i_read <= 0 )
919 return VLC_EGENERIC;
920 if( buffer == 0 )
921 return VLC_SUCCESS;
923 i_read = buffer << 4;
924 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
926 psz_meta = malloc( i_read + 1 );
927 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
928 (uint8_t *)psz_meta, i_read, true ) != i_read )
930 free( psz_meta );
931 return VLC_EGENERIC;
934 psz_meta[i_read] = '\0'; /* Just in case */
936 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
938 /* Now parse the meta */
939 /* Look for StreamTitle= */
940 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
941 if( p )
943 p += strlen( "StreamTitle=" );
944 if( *p == '\'' || *p == '"' )
946 char closing[] = { p[0], ';', '\0' };
947 char *psz = strstr( &p[1], closing );
948 if( !psz )
949 psz = strchr( &p[1], ';' );
951 if( psz ) *psz = '\0';
953 else
955 char *psz = strchr( &p[1], ';' );
956 if( psz ) *psz = '\0';
959 if( !p_sys->psz_icy_title ||
960 strcmp( p_sys->psz_icy_title, &p[1] ) )
962 free( p_sys->psz_icy_title );
963 char *psz_tmp = strdup( &p[1] );
964 p_sys->psz_icy_title = EnsureUTF8( psz_tmp );
965 if( !p_sys->psz_icy_title )
966 free( psz_tmp );
967 p_access->info.i_update |= INPUT_UPDATE_META;
969 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
972 free( psz_meta );
974 return VLC_SUCCESS;
977 #ifdef HAVE_ZLIB_H
978 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
979 size_t i_len )
981 access_sys_t *p_sys = p_access->p_sys;
983 if( p_sys->b_compressed )
985 int i_ret;
987 if( !p_sys->inflate.p_buffer )
988 p_sys->inflate.p_buffer = malloc( 256 * 1024 );
990 if( p_sys->inflate.stream.avail_in == 0 )
992 ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer + p_sys->inflate.stream.avail_in, 256 * 1024 );
993 if( i_read <= 0 ) return i_read;
994 p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
995 p_sys->inflate.stream.avail_in = i_read;
998 p_sys->inflate.stream.avail_out = i_len;
999 p_sys->inflate.stream.next_out = p_buffer;
1001 i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
1002 msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
1004 return i_len - p_sys->inflate.stream.avail_out;
1006 else
1008 return Read( p_access, p_buffer, i_len );
1011 #endif
1013 /*****************************************************************************
1014 * Seek: close and re-open a connection at the right place
1015 *****************************************************************************/
1016 static int Seek( access_t *p_access, uint64_t i_pos )
1018 msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
1020 Disconnect( p_access );
1022 if( p_access->info.i_size
1023 && i_pos >= p_access->info.i_size ) {
1024 msg_Err( p_access, "seek to far" );
1025 int retval = Seek( p_access, p_access->info.i_size - 1 );
1026 if( retval == VLC_SUCCESS ) {
1027 uint8_t p_buffer[2];
1028 Read( p_access, p_buffer, 1);
1029 p_access->info.b_eof = false;
1031 return retval;
1033 if( Connect( p_access, i_pos ) )
1035 msg_Err( p_access, "seek failed" );
1036 p_access->info.b_eof = true;
1037 return VLC_EGENERIC;
1039 return VLC_SUCCESS;
1042 /*****************************************************************************
1043 * Control:
1044 *****************************************************************************/
1045 static int Control( access_t *p_access, int i_query, va_list args )
1047 access_sys_t *p_sys = p_access->p_sys;
1048 bool *pb_bool;
1049 int64_t *pi_64;
1050 vlc_meta_t *p_meta;
1052 switch( i_query )
1054 /* */
1055 case ACCESS_CAN_SEEK:
1056 pb_bool = (bool*)va_arg( args, bool* );
1057 *pb_bool = p_sys->b_seekable;
1058 break;
1059 case ACCESS_CAN_FASTSEEK:
1060 pb_bool = (bool*)va_arg( args, bool* );
1061 *pb_bool = false;
1062 break;
1063 case ACCESS_CAN_PAUSE:
1064 case ACCESS_CAN_CONTROL_PACE:
1065 pb_bool = (bool*)va_arg( args, bool* );
1067 #if 0 /* Disable for now until we have a clock synchro algo
1068 * which works with something else than MPEG over UDP */
1069 *pb_bool = p_sys->b_pace_control;
1070 #endif
1071 *pb_bool = true;
1072 break;
1074 /* */
1075 case ACCESS_GET_PTS_DELAY:
1076 pi_64 = (int64_t*)va_arg( args, int64_t * );
1077 *pi_64 = var_GetInteger( p_access, "http-caching" ) * 1000;
1078 break;
1080 /* */
1081 case ACCESS_SET_PAUSE_STATE:
1082 break;
1084 case ACCESS_GET_META:
1085 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1087 if( p_sys->psz_icy_name )
1088 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
1089 if( p_sys->psz_icy_genre )
1090 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
1091 if( p_sys->psz_icy_title )
1092 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
1093 break;
1095 case ACCESS_GET_CONTENT_TYPE:
1096 *va_arg( args, char ** ) =
1097 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
1098 break;
1100 case ACCESS_GET_TITLE_INFO:
1101 case ACCESS_SET_TITLE:
1102 case ACCESS_SET_SEEKPOINT:
1103 case ACCESS_SET_PRIVATE_ID_STATE:
1104 return VLC_EGENERIC;
1106 default:
1107 msg_Warn( p_access, "unimplemented query in control" );
1108 return VLC_EGENERIC;
1111 return VLC_SUCCESS;
1114 /*****************************************************************************
1115 * Connect:
1116 *****************************************************************************/
1117 static int Connect( access_t *p_access, uint64_t i_tell )
1119 access_sys_t *p_sys = p_access->p_sys;
1120 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
1122 /* Clean info */
1123 free( p_sys->psz_location );
1124 free( p_sys->psz_mime );
1125 free( p_sys->psz_pragma );
1127 free( p_sys->psz_icy_genre );
1128 free( p_sys->psz_icy_name );
1129 free( p_sys->psz_icy_title );
1132 p_sys->psz_location = NULL;
1133 p_sys->psz_mime = NULL;
1134 p_sys->psz_pragma = NULL;
1135 p_sys->b_mms = false;
1136 p_sys->b_chunked = false;
1137 p_sys->i_chunk = 0;
1138 p_sys->i_icy_meta = 0;
1139 p_sys->i_icy_offset = i_tell;
1140 p_sys->psz_icy_name = NULL;
1141 p_sys->psz_icy_genre = NULL;
1142 p_sys->psz_icy_title = NULL;
1143 p_sys->i_remaining = 0;
1144 p_sys->b_persist = false;
1145 p_sys->b_has_size = false;
1146 p_access->info.i_size = 0;
1147 p_access->info.i_pos = i_tell;
1148 p_access->info.b_eof = false;
1150 /* Open connection */
1151 assert( p_sys->fd == -1 ); /* No open sockets (leaking fds is BAD) */
1152 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
1153 if( p_sys->fd == -1 )
1155 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
1156 return -1;
1158 setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));
1160 /* Initialize TLS/SSL session */
1161 if( p_sys->b_ssl == true )
1163 /* CONNECT to establish TLS tunnel through HTTP proxy */
1164 if( p_sys->b_proxy )
1166 char *psz;
1167 unsigned i_status = 0;
1169 if( p_sys->i_version == 0 )
1171 /* CONNECT is not in HTTP/1.0 */
1172 Disconnect( p_access );
1173 return -1;
1176 net_Printf( p_access, p_sys->fd, NULL,
1177 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
1178 p_sys->url.psz_host, p_sys->url.i_port,
1179 p_sys->i_version,
1180 p_sys->url.psz_host, p_sys->url.i_port);
1182 psz = net_Gets( p_access, p_sys->fd, NULL );
1183 if( psz == NULL )
1185 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
1186 Disconnect( p_access );
1187 return -1;
1190 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
1191 free( psz );
1193 if( ( i_status / 100 ) != 2 )
1195 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
1196 Disconnect( p_access );
1197 return -1;
1202 psz = net_Gets( p_access, p_sys->fd, NULL );
1203 if( psz == NULL )
1205 msg_Err( p_access, "HTTP proxy connection failed" );
1206 Disconnect( p_access );
1207 return -1;
1210 if( *psz == '\0' )
1211 i_status = 0;
1213 free( psz );
1215 if( !vlc_object_alive (p_access) || p_sys->b_error )
1217 Disconnect( p_access );
1218 return -1;
1221 while( i_status );
1224 /* TLS/SSL handshake */
1225 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
1226 p_sys->url.psz_host );
1227 if( p_sys->p_tls == NULL )
1229 msg_Err( p_access, "cannot establish HTTP/TLS session" );
1230 Disconnect( p_access );
1231 return -1;
1233 p_sys->p_vs = &p_sys->p_tls->sock;
1236 return Request( p_access, i_tell ) ? -2 : 0;
1240 static int Request( access_t *p_access, uint64_t i_tell )
1242 access_sys_t *p_sys = p_access->p_sys;
1243 char *psz ;
1244 v_socket_t *pvs = p_sys->p_vs;
1245 p_sys->b_persist = false;
1247 p_sys->i_remaining = 0;
1248 if( p_sys->b_proxy )
1250 if( p_sys->url.psz_path )
1252 net_Printf( p_access, p_sys->fd, NULL,
1253 "GET http://%s:%d%s HTTP/1.%d\r\n",
1254 p_sys->url.psz_host, p_sys->url.i_port,
1255 p_sys->url.psz_path, p_sys->i_version );
1257 else
1259 net_Printf( p_access, p_sys->fd, NULL,
1260 "GET http://%s:%d/ HTTP/1.%d\r\n",
1261 p_sys->url.psz_host, p_sys->url.i_port,
1262 p_sys->i_version );
1265 else
1267 const char *psz_path = p_sys->url.psz_path;
1268 if( !psz_path || !*psz_path )
1270 psz_path = "/";
1272 if( p_sys->url.i_port != (pvs ? 443 : 80) )
1274 net_Printf( p_access, p_sys->fd, pvs,
1275 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
1276 psz_path, p_sys->i_version, p_sys->url.psz_host,
1277 p_sys->url.i_port );
1279 else
1281 net_Printf( p_access, p_sys->fd, pvs,
1282 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
1283 psz_path, p_sys->i_version, p_sys->url.psz_host );
1286 /* User Agent */
1287 net_Printf( p_access, p_sys->fd, pvs,
1288 "User-Agent: %s\r\n",
1289 p_sys->psz_user_agent );
1290 /* Offset */
1291 if( p_sys->i_version == 1 && ! p_sys->b_continuous )
1293 p_sys->b_persist = true;
1294 net_Printf( p_access, p_sys->fd, pvs,
1295 "Range: bytes=%"PRIu64"-\r\n", i_tell );
1296 net_Printf( p_access, p_sys->fd, pvs, "Connection: close\r\n" );
1299 /* Cookies */
1300 if( p_sys->cookies )
1302 int i;
1303 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1305 const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1306 char * psz_cookie_content = cookie_get_content( cookie );
1307 char * psz_cookie_domain = cookie_get_domain( cookie );
1309 assert( psz_cookie_content );
1311 /* FIXME: This is clearly not conforming to the rfc */
1312 bool is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1314 if( is_in_right_domain )
1316 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1317 if( net_Printf( p_access, p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1318 msg_Err( p_access, "failed to send Cookie" );
1320 free( psz_cookie_content );
1321 free( psz_cookie_domain );
1325 /* Authentication */
1326 if( p_sys->url.psz_username || p_sys->url.psz_password )
1327 AuthReply( p_access, "", &p_sys->url, &p_sys->auth );
1329 /* Proxy Authentication */
1330 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1331 AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
1333 /* ICY meta data request */
1334 net_Printf( p_access, p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1337 if( net_Printf( p_access, p_sys->fd, pvs, "\r\n" ) < 0 )
1339 msg_Err( p_access, "failed to send request" );
1340 Disconnect( p_access );
1341 return VLC_EGENERIC;
1344 /* Read Answer */
1345 if( ( psz = net_Gets( p_access, p_sys->fd, pvs ) ) == NULL )
1347 msg_Err( p_access, "failed to read answer" );
1348 goto error;
1350 if( !strncmp( psz, "HTTP/1.", 7 ) )
1352 p_sys->psz_protocol = "HTTP";
1353 p_sys->i_code = atoi( &psz[9] );
1355 else if( !strncmp( psz, "ICY", 3 ) )
1357 p_sys->psz_protocol = "ICY";
1358 p_sys->i_code = atoi( &psz[4] );
1359 p_sys->b_reconnect = true;
1361 else
1363 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1364 free( psz );
1365 goto error;
1367 msg_Dbg( p_access, "protocol '%s' answer code %d",
1368 p_sys->psz_protocol, p_sys->i_code );
1369 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1371 p_sys->b_seekable = false;
1373 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1375 p_sys->b_seekable = false;
1377 /* Authentication error - We'll have to display the dialog */
1378 if( p_sys->i_code == 401 )
1382 /* Other fatal error */
1383 else if( p_sys->i_code >= 400 )
1385 msg_Err( p_access, "error: %s", psz );
1386 free( psz );
1387 goto error;
1389 free( psz );
1391 for( ;; )
1393 char *psz = net_Gets( p_access, p_sys->fd, pvs );
1394 char *p;
1396 if( psz == NULL )
1398 msg_Err( p_access, "failed to read answer" );
1399 goto error;
1402 if( !vlc_object_alive (p_access) || p_sys->b_error )
1404 free( psz );
1405 goto error;
1408 /* msg_Dbg( p_input, "Line=%s", psz ); */
1409 if( *psz == '\0' )
1411 free( psz );
1412 break;
1415 if( ( p = strchr( psz, ':' ) ) == NULL )
1417 msg_Err( p_access, "malformed header line: %s", psz );
1418 free( psz );
1419 goto error;
1421 *p++ = '\0';
1422 while( *p == ' ' ) p++;
1424 if( !strcasecmp( psz, "Content-Length" ) )
1426 uint64_t i_size = i_tell + (p_sys->i_remaining = (uint64_t)atoll( p ));
1427 if(i_size > p_access->info.i_size) {
1428 p_sys->b_has_size = true;
1429 p_access->info.i_size = i_size;
1431 msg_Dbg( p_access, "this frame size=%"PRIu64, p_sys->i_remaining );
1433 else if( !strcasecmp( psz, "Content-Range" ) ) {
1434 uint64_t i_ntell = i_tell;
1435 uint64_t i_nend = (p_access->info.i_size > 0)?(p_access->info.i_size - 1):i_tell;
1436 uint64_t i_nsize = p_access->info.i_size;
1437 sscanf(p,"bytes %"SCNu64"-%"SCNu64"/%"SCNu64,&i_ntell,&i_nend,&i_nsize);
1438 if(i_nend > i_ntell ) {
1439 p_access->info.i_pos = i_ntell;
1440 p_sys->i_icy_offset = i_ntell;
1441 p_sys->i_remaining = i_nend+1-i_ntell;
1442 int64_t i_size = (i_nsize > i_nend) ? i_nsize : (i_nend + 1);
1443 if(i_size > p_access->info.i_size) {
1444 p_sys->b_has_size = true;
1445 p_access->info.i_size = i_size;
1447 msg_Dbg( p_access, "stream size=%"PRIu64",pos=%"PRIu64",remaining=%"PRIu64,
1448 i_nsize, i_ntell, p_sys->i_remaining);
1451 else if( !strcasecmp( psz, "Connection" ) ) {
1452 msg_Dbg( p_access, "Connection: %s",p );
1453 int i = -1;
1454 sscanf(p, "close%n",&i);
1455 if( i >= 0 ) {
1456 p_sys->b_persist = false;
1459 else if( !strcasecmp( psz, "Location" ) )
1461 char * psz_new_loc;
1463 /* This does not follow RFC 2068, but yet if the url is not absolute,
1464 * handle it as everyone does. */
1465 if( p[0] == '/' )
1467 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1469 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1471 if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1472 p_sys->url.psz_host, p) < 0 )
1473 goto error;
1475 else
1477 if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1478 p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1479 goto error;
1482 else
1484 psz_new_loc = strdup( p );
1487 free( p_sys->psz_location );
1488 p_sys->psz_location = psz_new_loc;
1490 else if( !strcasecmp( psz, "Content-Type" ) )
1492 free( p_sys->psz_mime );
1493 p_sys->psz_mime = strdup( p );
1494 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1496 else if( !strcasecmp( psz, "Content-Encoding" ) )
1498 msg_Dbg( p_access, "Content-Encoding: %s", p );
1499 if( !strcasecmp( p, "identity" ) )
1501 #ifdef HAVE_ZLIB_H
1502 else if( !strcasecmp( p, "gzip" ) || !strcasecmp( p, "deflate" ) )
1503 p_sys->b_compressed = true;
1504 #endif
1505 else
1506 msg_Warn( p_access, "Unknown content coding: %s", p );
1508 else if( !strcasecmp( psz, "Pragma" ) )
1510 if( !strcasecmp( psz, "Pragma: features" ) )
1511 p_sys->b_mms = true;
1512 free( p_sys->psz_pragma );
1513 p_sys->psz_pragma = strdup( p );
1514 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1516 else if( !strcasecmp( psz, "Server" ) )
1518 msg_Dbg( p_access, "Server: %s", p );
1519 if( !strncasecmp( p, "Icecast", 7 ) ||
1520 !strncasecmp( p, "Nanocaster", 10 ) )
1522 /* Remember if this is Icecast
1523 * we need to force demux in this case without breaking
1524 * autodetection */
1526 /* Let live 365 streams (nanocaster) piggyback on the icecast
1527 * routine. They look very similar */
1529 p_sys->b_reconnect = true;
1530 p_sys->b_pace_control = false;
1531 p_sys->b_icecast = true;
1534 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1536 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1537 if( !strncasecmp( p, "chunked", 7 ) )
1539 p_sys->b_chunked = true;
1542 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1544 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1545 p_sys->i_icy_meta = atoi( p );
1546 if( p_sys->i_icy_meta < 0 )
1547 p_sys->i_icy_meta = 0;
1548 if( p_sys->i_icy_meta > 0 )
1549 p_sys->b_icecast = true;
1551 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1553 else if( !strcasecmp( psz, "Icy-Name" ) )
1555 free( p_sys->psz_icy_name );
1556 char *psz_tmp = strdup( p );
1557 p_sys->psz_icy_name = EnsureUTF8( psz_tmp );
1558 if( !p_sys->psz_icy_name )
1559 free( psz_tmp );
1560 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1562 p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
1563 p_sys->b_reconnect = true;
1564 p_sys->b_pace_control = false;
1566 else if( !strcasecmp( psz, "Icy-Genre" ) )
1568 free( p_sys->psz_icy_genre );
1569 char *psz_tmp = strdup( p );
1570 p_sys->psz_icy_genre = EnsureUTF8( psz_tmp );
1571 if( !p_sys->psz_icy_genre )
1572 free( psz_tmp );
1573 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1575 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1577 msg_Dbg( p_access, "Icy-Notice: %s", p );
1579 else if( !strncasecmp( psz, "icy-", 4 ) ||
1580 !strncasecmp( psz, "ice-", 4 ) ||
1581 !strncasecmp( psz, "x-audiocast", 11 ) )
1583 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1585 else if( !strcasecmp( psz, "Set-Cookie" ) )
1587 if( p_sys->cookies )
1589 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1590 cookie_append( p_sys->cookies, strdup(p) );
1592 else
1593 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1595 else if( !strcasecmp( psz, "www-authenticate" ) )
1597 msg_Dbg( p_access, "Authentication header: %s", p );
1598 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1599 &p_sys->auth, p );
1601 else if( !strcasecmp( psz, "proxy-authenticate" ) )
1603 msg_Dbg( p_access, "Proxy authentication header: %s", p );
1604 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1605 &p_sys->proxy_auth, p );
1607 else if( !strcasecmp( psz, "authentication-info" ) )
1609 msg_Dbg( p_access, "Authentication Info header: %s", p );
1610 if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
1611 goto error;
1613 else if( !strcasecmp( psz, "proxy-authentication-info" ) )
1615 msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
1616 if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
1617 goto error;
1620 free( psz );
1622 /* We close the stream for zero length data, unless of course the
1623 * server has already promised to do this for us.
1625 if( p_sys->b_has_size && p_sys->i_remaining == 0 && p_sys->b_persist ) {
1626 Disconnect( p_access );
1628 return VLC_SUCCESS;
1630 error:
1631 Disconnect( p_access );
1632 return VLC_EGENERIC;
1635 /*****************************************************************************
1636 * Disconnect:
1637 *****************************************************************************/
1638 static void Disconnect( access_t *p_access )
1640 access_sys_t *p_sys = p_access->p_sys;
1642 if( p_sys->p_tls != NULL)
1644 tls_ClientDelete( p_sys->p_tls );
1645 p_sys->p_tls = NULL;
1646 p_sys->p_vs = NULL;
1648 if( p_sys->fd != -1)
1650 net_Close(p_sys->fd);
1651 p_sys->fd = -1;
1656 /*****************************************************************************
1657 * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1658 * them) (FIXME: only support the "domain=" param)
1659 *****************************************************************************/
1661 /* Get the NAME=VALUE part of the Cookie */
1662 static char * cookie_get_content( const char * cookie )
1664 char * ret = strdup( cookie );
1665 if( !ret ) return NULL;
1666 char * str = ret;
1667 /* Look for a ';' */
1668 while( *str && *str != ';' ) str++;
1669 /* Replace it by a end-char */
1670 if( *str == ';' ) *str = 0;
1671 return ret;
1674 /* Get the domain where the cookie is stored */
1675 static char * cookie_get_domain( const char * cookie )
1677 const char * str = cookie;
1678 static const char domain[] = "domain=";
1679 if( !str )
1680 return NULL;
1681 /* Look for a ';' */
1682 while( *str )
1684 if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1686 str += sizeof(domain) - 1 /* minus \0 */;
1687 char * ret = strdup( str );
1688 /* Now remove the next ';' if present */
1689 char * ret_iter = ret;
1690 while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1691 if( *ret_iter == ';' )
1692 *ret_iter = 0;
1693 return ret;
1695 /* Go to next ';' field */
1696 while( *str && *str != ';' ) str++;
1697 if( *str == ';' ) str++;
1698 /* skip blank */
1699 while( *str && *str == ' ' ) str++;
1701 return NULL;
1704 /* Get NAME in the NAME=VALUE field */
1705 static char * cookie_get_name( const char * cookie )
1707 char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1708 if( !ret ) return NULL;
1709 char * str = ret;
1710 while( *str && *str != '=' ) str++;
1711 *str = 0;
1712 return ret;
1715 /* Add a cookie in cookies, checking to see how it should be added */
1716 static void cookie_append( vlc_array_t * cookies, char * cookie )
1718 int i;
1720 if( !cookie )
1721 return;
1723 char * cookie_name = cookie_get_name( cookie );
1725 /* Don't send invalid cookies */
1726 if( !cookie_name )
1727 return;
1729 char * cookie_domain = cookie_get_domain( cookie );
1730 for( i = 0; i < vlc_array_count( cookies ); i++ )
1732 char * current_cookie = vlc_array_item_at_index( cookies, i );
1733 char * current_cookie_name = cookie_get_name( current_cookie );
1734 char * current_cookie_domain = cookie_get_domain( current_cookie );
1736 assert( current_cookie_name );
1738 bool is_domain_matching = ( cookie_domain && current_cookie_domain &&
1739 !strcmp( cookie_domain, current_cookie_domain ) );
1741 if( is_domain_matching && !strcmp( cookie_name, current_cookie_name ) )
1743 /* Remove previous value for this cookie */
1744 free( current_cookie );
1745 vlc_array_remove( cookies, i );
1747 /* Clean */
1748 free( current_cookie_name );
1749 free( current_cookie_domain );
1750 break;
1752 free( current_cookie_name );
1753 free( current_cookie_domain );
1755 free( cookie_name );
1756 free( cookie_domain );
1757 vlc_array_append( cookies, cookie );
1761 /*****************************************************************************
1762 * HTTP authentication
1763 *****************************************************************************/
1765 static void AuthReply( access_t *p_access, const char *psz_prefix,
1766 vlc_url_t *p_url, http_auth_t *p_auth )
1768 access_sys_t *p_sys = p_access->p_sys;
1769 char *psz_value;
1771 psz_value =
1772 http_auth_FormatAuthorizationHeader( VLC_OBJECT(p_access), p_auth,
1773 "GET", p_url->psz_path,
1774 p_url->psz_username,
1775 p_url->psz_password );
1776 if ( psz_value == NULL )
1777 return;
1779 net_Printf( p_access, p_sys->fd, p_sys->p_vs,
1780 "%sAuthorization: %s\r\n", psz_prefix, psz_value );
1781 free( psz_value );
1784 static int AuthCheckReply( access_t *p_access, const char *psz_header,
1785 vlc_url_t *p_url, http_auth_t *p_auth )
1787 return
1788 http_auth_ParseAuthenticationInfoHeader( VLC_OBJECT(p_access), p_auth,
1789 psz_header, "",
1790 p_url->psz_path,
1791 p_url->psz_username,
1792 p_url->psz_password );