1 /*****************************************************************************
2 * http.c: HTTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2005 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Christophe Massiot <massiot@via.ecp.fr>
9 * RĂ©mi Denis-Courmont <rem # videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
32 #include <vlc_access.h>
34 #include <vlc_interface.h>
36 #include <vlc_network.h>
39 #include <vlc_strings.h>
40 #include <vlc_input.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close( vlc_object_t * );
48 #define PROXY_TEXT N_("HTTP proxy")
49 #define PROXY_LONGTEXT N_( \
50 "HTTP proxy to be used It must be of the form " \
51 "http://[user[:pass]@]myproxy.mydomain:myport/ ; " \
52 "if empty, the http_proxy environment variable will be tried." )
54 #define CACHING_TEXT N_("Caching value in ms")
55 #define CACHING_LONGTEXT N_( \
56 "Caching value for HTTP streams. This " \
57 "value should be set in milliseconds." )
59 #define AGENT_TEXT N_("HTTP user agent")
60 #define AGENT_LONGTEXT N_("User agent that will be " \
61 "used for the connection.")
63 #define RECONNECT_TEXT N_("Auto re-connect")
64 #define RECONNECT_LONGTEXT N_( \
65 "Automatically try to reconnect to the stream in case of a sudden " \
68 #define CONTINUOUS_TEXT N_("Continuous stream")
69 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
70 "being constantly updated (for example, a JPG file on a server). " \
71 "You should not globally enable this option as it will break all other " \
72 "types of HTTP streams." )
74 #define FORWARD_COOKIES_TEXT N_("Forward Cookies")
75 #define FORWARD_COOKIES_LONGTEXT N_("Forward Cookies Across http redirections ")
78 set_description( _("HTTP input") );
79 set_capability( "access2", 0 );
80 set_shortname( _( "HTTP(S)" ) );
81 set_category( CAT_INPUT );
82 set_subcategory( SUBCAT_INPUT_ACCESS );
84 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
86 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
87 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
88 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
89 AGENT_LONGTEXT, VLC_TRUE );
90 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
91 RECONNECT_LONGTEXT, VLC_TRUE );
92 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
93 CONTINUOUS_LONGTEXT, VLC_TRUE );
94 add_bool( "http-forward-cookies", 0, NULL, FORWARD_COOKIES_TEXT,
95 FORWARD_COOKIES_LONGTEXT, VLC_TRUE );
96 add_obsolete_string("http-user");
97 add_obsolete_string("http-pwd");
98 add_shortcut( "http" );
99 add_shortcut( "https" );
100 add_shortcut( "unsv" );
101 add_shortcut( "itpc" ); /* iTunes Podcast */
102 set_callbacks( Open, Close );
105 /*****************************************************************************
107 *****************************************************************************/
111 tls_session_t *p_tls;
116 char *psz_user_agent;
124 const char *psz_protocol;
131 vlc_bool_t b_icecast;
134 vlc_bool_t b_chunked;
144 vlc_bool_t b_seekable;
145 vlc_bool_t b_reconnect;
146 vlc_bool_t b_continuous;
147 vlc_bool_t b_pace_control;
149 vlc_array_t * cookies;
153 static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies );
156 static ssize_t Read( access_t *, uint8_t *, size_t );
157 static int Seek( access_t *, int64_t );
158 static int Control( access_t *, int, va_list );
161 static int Connect( access_t *, int64_t );
162 static int Request( access_t *p_access, int64_t i_tell );
163 static void Disconnect( access_t * );
165 /* Small Cookie utilities. Cookies support is partial. */
166 static char * cookie_get_content( const char * cookie );
167 static char * cookie_get_domain( const char * cookie );
168 static char * cookie_get_name( const char * cookie );
169 static void cookie_append( vlc_array_t * cookies, char * cookie );
171 /*****************************************************************************
173 *****************************************************************************/
174 static int Open( vlc_object_t *p_this )
176 return OpenWithCookies( p_this, NULL );
179 static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies )
181 access_t *p_access = (access_t*)p_this;
184 /* Only forward an store cookies if the corresponding option is activated */
185 vlc_bool_t b_forward_cookies = var_CreateGetBool( p_access, "http-forward-cookies" );
186 vlc_array_t * saved_cookies = b_forward_cookies ? (cookies ?: vlc_array_new()) : NULL;
188 /* Set up p_access */
189 STANDARD_READ_ACCESS_INIT;
191 p_sys->b_proxy = VLC_FALSE;
192 p_sys->i_version = 1;
193 p_sys->b_seekable = VLC_TRUE;
194 p_sys->psz_mime = NULL;
195 p_sys->psz_pragma = NULL;
196 p_sys->b_mms = VLC_FALSE;
197 p_sys->b_icecast = VLC_FALSE;
198 p_sys->psz_location = NULL;
199 p_sys->psz_user_agent = NULL;
200 p_sys->b_pace_control = VLC_TRUE;
201 p_sys->b_ssl = VLC_FALSE;
204 p_sys->i_icy_meta = 0;
205 p_sys->psz_icy_name = NULL;
206 p_sys->psz_icy_genre = NULL;
207 p_sys->psz_icy_title = NULL;
208 p_sys->i_remaining = 0;
210 p_sys->cookies = saved_cookies;
212 /* Parse URI - remove spaces */
213 p = psz = strdup( p_access->psz_path );
214 while( (p = strchr( p, ' ' )) != NULL )
216 vlc_UrlParse( &p_sys->url, psz, 0 );
219 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
221 msg_Warn( p_access, "invalid host" );
224 if( !strncmp( p_access->psz_access, "https", 5 ) )
227 p_sys->b_ssl = VLC_TRUE;
228 if( p_sys->url.i_port <= 0 )
229 p_sys->url.i_port = 443;
233 if( p_sys->url.i_port <= 0 )
234 p_sys->url.i_port = 80;
238 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
241 psz = var_CreateGetString( p_access, "http-proxy" );
244 p_sys->b_proxy = VLC_TRUE;
245 vlc_UrlParse( &p_sys->proxy, psz, 0 );
250 char *psz_proxy = getenv( "http_proxy" );
251 if( psz_proxy && *psz_proxy )
253 p_sys->b_proxy = VLC_TRUE;
254 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
262 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
264 msg_Warn( p_access, "invalid proxy host" );
267 if( p_sys->proxy.i_port <= 0 )
269 p_sys->proxy.i_port = 80;
273 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
274 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
277 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
278 p_sys->proxy.i_port );
280 if( p_sys->url.psz_username && *p_sys->url.psz_username )
282 msg_Dbg( p_access, " user='%s', pwd='%s'",
283 p_sys->url.psz_username, p_sys->url.psz_password );
286 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
287 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
291 switch( Connect( p_access, 0 ) )
297 /* Retry with http 1.0 */
298 msg_Dbg( p_access, "switching to HTTP version 1.0" );
299 p_sys->i_version = 0;
300 p_sys->b_seekable = VLC_FALSE;
302 if( p_access->b_die || Connect( p_access, 0 ) )
310 msg_Err( p_access, "You should not be here" );
315 if( p_sys->i_code == 401 )
317 char *psz_login = NULL; char *psz_password = NULL;
319 msg_Dbg( p_access, "authentication failed" );
320 i_ret = intf_UserLoginPassword( p_access, _("HTTP authentication"),
321 _("Please enter a valid login name and a password."),
322 &psz_login, &psz_password );
323 if( i_ret == DIALOG_OK_YES )
325 msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
326 psz_login, psz_password );
327 if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
328 if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
329 if( psz_login ) free( psz_login );
330 if( psz_password ) free( psz_password );
335 if( psz_login ) free( psz_login );
336 if( psz_password ) free( psz_password );
341 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
342 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
343 p_sys->psz_location && *p_sys->psz_location )
345 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
346 printf("redirection to %s", p_sys->psz_location );
349 /* Do not accept redirection outside of HTTP works */
350 if( strncmp( p_sys->psz_location, "http", 4 )
351 || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
352 && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
354 msg_Err( p_access, "insecure redirection ignored" );
357 free( p_access->psz_path );
358 p_access->psz_path = strdup( p_sys->psz_location );
359 /* Clean up current Open() run */
360 vlc_UrlClean( &p_sys->url );
361 vlc_UrlClean( &p_sys->proxy );
362 free( p_sys->psz_mime );
363 free( p_sys->psz_pragma );
364 free( p_sys->psz_location );
365 free( p_sys->psz_user_agent );
367 Disconnect( p_access );
368 cookies = p_sys->cookies;
371 /* Do new Open() run with new data */
372 return OpenWithCookies( p_this, cookies );
377 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
381 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
383 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
385 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
386 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
387 p_access->psz_demux = strdup( "nsv" );
388 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
389 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
390 p_access->psz_demux = strdup( "m4a" );
391 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
392 p_access->psz_demux = strdup( "mp3" );
394 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
395 p_access->psz_demux );
397 #if 0 /* Doesn't work really well because of the pre-buffering in
398 * shoutcast servers (the buffer content will be sent as fast as
400 p_sys->b_pace_control = VLC_FALSE;
403 else if( !p_sys->psz_mime )
406 p_access->psz_demux = strdup( "mp3" );
408 /* else probably Ogg Vorbis */
410 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
412 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
414 /* Grrrr! detect ultravox server and force NSV demuxer */
415 p_access->psz_demux = strdup( "nsv" );
417 else if( !strcmp( p_access->psz_access, "itpc" ) )
419 p_access->psz_demux = strdup( "podcast" );
421 else if( p_sys->psz_mime &&
422 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
423 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
424 p_access->psz_demux = strdup( "xspf-open" );
426 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
429 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
434 vlc_UrlClean( &p_sys->url );
435 vlc_UrlClean( &p_sys->proxy );
436 free( p_sys->psz_mime );
437 free( p_sys->psz_pragma );
438 free( p_sys->psz_location );
439 free( p_sys->psz_user_agent );
441 Disconnect( p_access );
446 /*****************************************************************************
448 *****************************************************************************/
449 static void Close( vlc_object_t *p_this )
451 access_t *p_access = (access_t*)p_this;
452 access_sys_t *p_sys = p_access->p_sys;
454 vlc_UrlClean( &p_sys->url );
455 vlc_UrlClean( &p_sys->proxy );
457 free( p_sys->psz_mime );
458 free( p_sys->psz_pragma );
459 free( p_sys->psz_location );
461 free( p_sys->psz_icy_name );
462 free( p_sys->psz_icy_genre );
463 free( p_sys->psz_icy_title );
465 free( p_sys->psz_user_agent );
467 Disconnect( p_access );
472 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
473 free(vlc_array_item_at_index( p_sys->cookies, i ));
474 vlc_array_destroy( p_sys->cookies );
480 /*****************************************************************************
481 * Read: Read up to i_len bytes from the http connection and place in
482 * p_buffer. Return the actual number of bytes read
483 *****************************************************************************/
484 static int ReadICYMeta( access_t *p_access );
485 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
487 access_sys_t *p_sys = p_access->p_sys;
492 p_access->info.b_eof = VLC_TRUE;
496 if( p_access->info.i_size > 0 &&
497 i_len + p_access->info.i_pos > p_access->info.i_size )
499 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
501 p_access->info.b_eof = VLC_TRUE;
506 if( p_sys->b_chunked )
508 if( p_sys->i_chunk < 0 )
510 p_access->info.b_eof = VLC_TRUE;
514 if( p_sys->i_chunk <= 0 )
516 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
517 /* read the chunk header */
520 /* fatal error - end of file */
521 msg_Dbg( p_access, "failed reading chunk-header line" );
524 p_sys->i_chunk = strtoll( psz, NULL, 16 );
527 if( p_sys->i_chunk <= 0 ) /* eof */
530 p_access->info.b_eof = VLC_TRUE;
535 if( i_len > p_sys->i_chunk )
537 i_len = p_sys->i_chunk;
541 if( p_sys->b_continuous && i_len > p_sys->i_remaining )
543 /* Only ask for the remaining length */
544 int i_new_len = p_sys->i_remaining;
547 Request( p_access, 0 );
548 i_read = Read( p_access, p_buffer, i_len );
554 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
556 int64_t i_next = p_sys->i_icy_meta -
557 p_access->info.i_pos % p_sys->i_icy_meta;
559 if( i_next == p_sys->i_icy_meta )
561 if( ReadICYMeta( p_access ) )
563 p_access->info.b_eof = VLC_TRUE;
571 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
575 p_access->info.i_pos += i_read;
577 if( p_sys->b_chunked )
579 p_sys->i_chunk -= i_read;
580 if( p_sys->i_chunk <= 0 )
582 /* read the empty line */
583 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
584 if( psz ) free( psz );
588 else if( i_read == 0 )
591 * I very much doubt that this will work.
592 * If i_read == 0, the connection *IS* dead, so the only
593 * sensible thing to do is Disconnect() and then retry.
594 * Otherwise, I got recv() completely wrong. -- Courmisch
596 if( p_sys->b_continuous )
598 Request( p_access, 0 );
599 p_sys->b_continuous = VLC_FALSE;
600 i_read = Read( p_access, p_buffer, i_len );
601 p_sys->b_continuous = VLC_TRUE;
603 Disconnect( p_access );
604 if( p_sys->b_reconnect )
606 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
607 if( Connect( p_access, p_access->info.i_pos ) )
609 msg_Dbg( p_access, "reconnection failed" );
613 p_sys->b_reconnect = VLC_FALSE;
614 i_read = Read( p_access, p_buffer, i_len );
615 p_sys->b_reconnect = VLC_TRUE;
619 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
622 if( p_sys->b_continuous )
624 p_sys->i_remaining -= i_read;
630 static int ReadICYMeta( access_t *p_access )
632 access_sys_t *p_sys = p_access->p_sys;
638 /* Read meta data length */
639 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
646 i_read = buffer << 4;
647 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
649 psz_meta = malloc( i_read + 1 );
650 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
651 (uint8_t *)psz_meta, i_read, VLC_TRUE ) != i_read )
654 psz_meta[i_read] = '\0'; /* Just in case */
656 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
658 /* Now parse the meta */
659 /* Look for StreamTitle= */
660 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
663 p += strlen( "StreamTitle=" );
664 if( *p == '\'' || *p == '"' )
666 char closing[] = { p[0], ';', '\0' };
667 char *psz = strstr( &p[1], closing );
669 psz = strchr( &p[1], ';' );
671 if( psz ) *psz = '\0';
675 char *psz = strchr( &p[1], ';' );
676 if( psz ) *psz = '\0';
679 if( !p_sys->psz_icy_title ||
680 strcmp( p_sys->psz_icy_title, &p[1] ) )
682 if( p_sys->psz_icy_title )
683 free( p_sys->psz_icy_title );
684 p_sys->psz_icy_title = strdup( &p[1] );
685 p_access->info.i_update |= INPUT_UPDATE_META;
687 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
695 /*****************************************************************************
696 * Seek: close and re-open a connection at the right place
697 *****************************************************************************/
698 static int Seek( access_t *p_access, int64_t i_pos )
700 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
702 Disconnect( p_access );
704 if( Connect( p_access, i_pos ) )
706 msg_Err( p_access, "seek failed" );
707 p_access->info.b_eof = VLC_TRUE;
713 /*****************************************************************************
715 *****************************************************************************/
716 static int Control( access_t *p_access, int i_query, va_list args )
718 access_sys_t *p_sys = p_access->p_sys;
727 case ACCESS_CAN_SEEK:
728 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
729 *pb_bool = p_sys->b_seekable;
731 case ACCESS_CAN_FASTSEEK:
732 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
733 *pb_bool = VLC_FALSE;
735 case ACCESS_CAN_PAUSE:
736 case ACCESS_CAN_CONTROL_PACE:
737 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
739 #if 0 /* Disable for now until we have a clock synchro algo
740 * which works with something else than MPEG over UDP */
741 *pb_bool = p_sys->b_pace_control;
748 pi_int = (int*)va_arg( args, int * );
752 case ACCESS_GET_PTS_DELAY:
753 pi_64 = (int64_t*)va_arg( args, int64_t * );
754 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
758 case ACCESS_SET_PAUSE_STATE:
761 case ACCESS_GET_META:
762 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
764 if( p_sys->psz_icy_name )
765 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
766 if( p_sys->psz_icy_genre )
767 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
768 if( p_sys->psz_icy_title )
769 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
772 case ACCESS_GET_CONTENT_TYPE:
773 *va_arg( args, char ** ) =
774 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
777 case ACCESS_GET_TITLE_INFO:
778 case ACCESS_SET_TITLE:
779 case ACCESS_SET_SEEKPOINT:
780 case ACCESS_SET_PRIVATE_ID_STATE:
784 msg_Warn( p_access, "unimplemented query in control" );
791 /*****************************************************************************
793 *****************************************************************************/
794 static int Connect( access_t *p_access, int64_t i_tell )
796 access_sys_t *p_sys = p_access->p_sys;
797 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
800 free( p_sys->psz_location );
801 free( p_sys->psz_mime );
802 free( p_sys->psz_pragma );
804 free( p_sys->psz_icy_genre );
805 free( p_sys->psz_icy_name );
806 free( p_sys->psz_icy_title );
809 p_sys->psz_location = NULL;
810 p_sys->psz_mime = NULL;
811 p_sys->psz_pragma = NULL;
812 p_sys->b_mms = VLC_FALSE;
813 p_sys->b_chunked = VLC_FALSE;
815 p_sys->i_icy_meta = 0;
816 p_sys->psz_icy_name = NULL;
817 p_sys->psz_icy_genre = NULL;
818 p_sys->psz_icy_title = NULL;
820 p_access->info.i_size = 0;
821 p_access->info.i_pos = i_tell;
822 p_access->info.b_eof = VLC_FALSE;
825 /* Open connection */
826 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
827 if( p_sys->fd == -1 )
829 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
833 /* Initialize TLS/SSL session */
834 if( p_sys->b_ssl == VLC_TRUE )
836 /* CONNECT to establish TLS tunnel through HTTP proxy */
840 unsigned i_status = 0;
842 if( p_sys->i_version == 0 )
844 /* CONNECT is not in HTTP/1.0 */
845 Disconnect( p_access );
849 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
850 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
851 p_sys->url.psz_host, p_sys->url.i_port,
853 p_sys->url.psz_host, p_sys->url.i_port);
855 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
858 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
859 Disconnect( p_access );
863 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
866 if( ( i_status / 100 ) != 2 )
868 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
869 Disconnect( p_access );
875 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
878 msg_Err( p_access, "HTTP proxy connection failed" );
879 Disconnect( p_access );
891 /* TLS/SSL handshake */
892 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
894 if( p_sys->p_tls == NULL )
896 msg_Err( p_access, "cannot establish HTTP/TLS session" );
897 Disconnect( p_access );
900 p_sys->p_vs = &p_sys->p_tls->sock;
903 return Request( p_access, i_tell ) ? -2 : 0;
907 static int Request( access_t *p_access, int64_t i_tell )
909 access_sys_t *p_sys = p_access->p_sys;
911 v_socket_t *pvs = p_sys->p_vs;
915 if( p_sys->url.psz_path )
917 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
918 "GET http://%s:%d%s HTTP/1.%d\r\n",
919 p_sys->url.psz_host, p_sys->url.i_port,
920 p_sys->url.psz_path, p_sys->i_version );
924 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
925 "GET http://%s:%d/ HTTP/1.%d\r\n",
926 p_sys->url.psz_host, p_sys->url.i_port,
932 const char *psz_path = p_sys->url.psz_path;
933 if( !psz_path || !*psz_path )
937 if( p_sys->url.i_port != 80)
939 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
940 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
941 psz_path, p_sys->i_version, p_sys->url.psz_host,
946 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
947 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
948 psz_path, p_sys->i_version, p_sys->url.psz_host );
952 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
953 p_sys->psz_user_agent );
955 if( p_sys->i_version == 1 )
957 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
958 "Range: bytes="I64Fd"-\r\n", i_tell );
965 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
967 const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
968 char * psz_cookie_content = cookie_get_content( cookie );
969 char * psz_cookie_domain = cookie_get_domain( cookie );
971 assert( psz_cookie_content );
973 /* FIXME: This is clearly not conforming to the rfc */
974 vlc_bool_t is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
976 if( is_in_right_domain )
978 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
979 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
980 msg_Err( p_access, "failed to send Cookie" );
982 free( psz_cookie_content );
983 free( psz_cookie_domain );
988 if( p_sys->url.psz_username || p_sys->url.psz_password )
990 char buf[strlen( p_sys->url.psz_username ?: "" )
991 + strlen( p_sys->url.psz_password ?: "" ) + 2];
994 snprintf( buf, sizeof( buf ), "%s:%s", p_sys->url.psz_username ?: "",
995 p_sys->url.psz_password ?: "" );
996 b64 = vlc_b64_encode( buf );
1000 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1001 "Authorization: Basic %s\r\n", b64 );
1006 /* Proxy Authentication */
1007 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1009 char buf[strlen( p_sys->proxy.psz_username ?: "" )
1010 + strlen( p_sys->proxy.psz_password ?: "" )];
1013 snprintf( buf, sizeof( buf ), "%s:%s", p_sys->proxy.psz_username ?: "",
1014 p_sys->proxy.psz_password ?: "" );
1015 b64 = vlc_b64_encode( buf );
1019 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1020 "Proxy-Authorization: Basic %s\r\n", b64 );
1025 /* ICY meta data request */
1026 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1029 if( p_sys->b_continuous )
1031 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
1032 "Connection: Keep-Alive\r\n" );
1034 else if( p_sys->i_version == 1 )
1036 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
1037 "Connection: Close\r\n");
1040 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
1042 msg_Err( p_access, "failed to send request" );
1043 Disconnect( p_access );
1044 return VLC_EGENERIC;
1048 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
1050 msg_Err( p_access, "failed to read answer" );
1053 if( !strncmp( psz, "HTTP/1.", 7 ) )
1055 p_sys->psz_protocol = "HTTP";
1056 p_sys->i_code = atoi( &psz[9] );
1058 else if( !strncmp( psz, "ICY", 3 ) )
1060 p_sys->psz_protocol = "ICY";
1061 p_sys->i_code = atoi( &psz[4] );
1062 p_sys->b_reconnect = VLC_TRUE;
1066 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1070 msg_Dbg( p_access, "protocol '%s' answer code %d",
1071 p_sys->psz_protocol, p_sys->i_code );
1072 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1074 p_sys->b_seekable = VLC_FALSE;
1076 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1078 p_sys->b_seekable = VLC_FALSE;
1080 /* Authentication error - We'll have to display the dialog */
1081 if( p_sys->i_code == 401 )
1085 /* Other fatal error */
1086 else if( p_sys->i_code >= 400 )
1088 msg_Err( p_access, "error: %s", psz );
1096 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1101 msg_Err( p_access, "failed to read answer" );
1105 /* msg_Dbg( p_input, "Line=%s", psz ); */
1113 if( ( p = strchr( psz, ':' ) ) == NULL )
1115 msg_Err( p_access, "malformed header line: %s", psz );
1120 while( *p == ' ' ) p++;
1122 if( !strcasecmp( psz, "Content-Length" ) )
1124 if( p_sys->b_continuous )
1126 p_access->info.i_size = -1;
1127 msg_Dbg( p_access, "this frame size=%lld", atoll(p ) );
1128 p_sys->i_remaining = atoll( p );
1132 p_access->info.i_size = i_tell + atoll( p );
1133 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1136 else if( !strcasecmp( psz, "Location" ) )
1140 /* This does not follow RFC 2068, but yet if the url is not absolute,
1141 * handle it as everyone does. */
1144 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1146 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1148 asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1149 p_sys->url.psz_host, p);
1153 asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1154 p_sys->url.psz_host, p_sys->url.i_port, p);
1159 psz_new_loc = strdup( p );
1162 if( p_sys->psz_location ) free( p_sys->psz_location );
1163 p_sys->psz_location = psz_new_loc;
1165 else if( !strcasecmp( psz, "Content-Type" ) )
1167 if( p_sys->psz_mime ) free( p_sys->psz_mime );
1168 p_sys->psz_mime = strdup( p );
1169 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1171 else if( !strcasecmp( psz, "Pragma" ) )
1173 if( !strcasecmp( psz, "Pragma: features" ) )
1174 p_sys->b_mms = VLC_TRUE;
1175 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1176 p_sys->psz_pragma = strdup( p );
1177 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1179 else if( !strcasecmp( psz, "Server" ) )
1181 msg_Dbg( p_access, "Server: %s", p );
1182 if( !strncasecmp( p, "Icecast", 7 ) ||
1183 !strncasecmp( p, "Nanocaster", 10 ) )
1185 /* Remember if this is Icecast
1186 * we need to force demux in this case without breaking
1189 /* Let live 365 streams (nanocaster) piggyback on the icecast
1190 * routine. They look very similar */
1192 p_sys->b_reconnect = VLC_TRUE;
1193 p_sys->b_pace_control = VLC_FALSE;
1194 p_sys->b_icecast = VLC_TRUE;
1197 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1199 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1200 if( !strncasecmp( p, "chunked", 7 ) )
1202 p_sys->b_chunked = VLC_TRUE;
1205 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1207 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1208 p_sys->i_icy_meta = atoi( p );
1209 if( p_sys->i_icy_meta < 0 )
1210 p_sys->i_icy_meta = 0;
1212 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1214 else if( !strcasecmp( psz, "Icy-Name" ) )
1216 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1217 p_sys->psz_icy_name = strdup( p );
1218 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1220 p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1221 p_sys->b_reconnect = VLC_TRUE;
1222 p_sys->b_pace_control = VLC_FALSE;
1224 else if( !strcasecmp( psz, "Icy-Genre" ) )
1226 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1227 p_sys->psz_icy_genre = strdup( p );
1228 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1230 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1232 msg_Dbg( p_access, "Icy-Notice: %s", p );
1234 else if( !strncasecmp( psz, "icy-", 4 ) ||
1235 !strncasecmp( psz, "ice-", 4 ) ||
1236 !strncasecmp( psz, "x-audiocast", 11 ) )
1238 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1239 } else if( !strcasecmp( psz, "Set-Cookie" ) )
1241 if( p_sys->cookies )
1243 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1244 cookie_append( p_sys->cookies, strdup(p) );
1247 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1255 Disconnect( p_access );
1256 return VLC_EGENERIC;
1259 /*****************************************************************************
1261 *****************************************************************************/
1262 static void Disconnect( access_t *p_access )
1264 access_sys_t *p_sys = p_access->p_sys;
1266 if( p_sys->p_tls != NULL)
1268 tls_ClientDelete( p_sys->p_tls );
1269 p_sys->p_tls = NULL;
1272 if( p_sys->fd != -1)
1274 net_Close(p_sys->fd);
1280 /*****************************************************************************
1281 * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1282 * them) (FIXME: only support the "domain=" param)
1283 *****************************************************************************/
1285 /* Get the NAME=VALUE part of the Cookie */
1286 static char * cookie_get_content( const char * cookie )
1288 char * ret = strdup( cookie );
1289 if( !ret ) return NULL;
1291 /* Look for a ';' */
1292 while( *str && *str != ';' ) str++;
1293 /* Replace it by a end-char */
1294 if( *str == ';' ) *str = 0;
1298 /* Get the domain where the cookie is stored */
1299 static char * cookie_get_domain( const char * cookie )
1301 const char * str = cookie;
1302 static const char domain[] = "domain=";
1305 /* Look for a ';' */
1308 if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1310 str += sizeof(domain) - 1 /* minus \0 */;
1311 char * ret = strdup( str );
1312 /* Now remove the next ';' if present */
1313 char * ret_iter = ret;
1314 while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1315 if( *ret_iter == ';' )
1319 /* Go to next ';' field */
1320 while( *str && *str != ';' ) str++;
1321 if( *str == ';' ) str++;
1323 while( *str && *str == ' ' ) str++;
1328 /* Get NAME in the NAME=VALUE field */
1329 static char * cookie_get_name( const char * cookie )
1331 char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1332 if( !ret ) return NULL;
1334 while( *str && *str != '=' ) str++;
1339 /* Add a cookie in cookies, checking to see how it should be added */
1340 static void cookie_append( vlc_array_t * cookies, char * cookie )
1347 char * cookie_name = cookie_get_name( cookie );
1349 /* Don't send invalid cookies */
1353 char * cookie_domain = cookie_get_domain( cookie );
1354 for( i = 0; i < vlc_array_count( cookies ); i++ )
1356 char * current_cookie = vlc_array_item_at_index( cookies, i );
1357 char * current_cookie_name = cookie_get_name( current_cookie );
1358 char * current_cookie_domain = cookie_get_domain( current_cookie );
1360 assert( current_cookie_name );
1362 vlc_bool_t is_domain_matching = ( cookie_domain && current_cookie_domain &&
1363 !strcmp( cookie_domain, current_cookie_domain ) );
1365 if( is_domain_matching && !strcmp( cookie_name, current_cookie_name ) )
1367 /* Remove previous value for this cookie */
1368 free( current_cookie );
1369 vlc_array_remove( cookies, i );
1372 free( current_cookie_name );
1373 free( current_cookie_domain );
1376 free( current_cookie_name );
1377 free( current_cookie_domain );
1379 free( cookie_name );
1380 free( cookie_domain );
1381 vlc_array_append( cookies, cookie );