1 /*****************************************************************************
2 * http.c: HTTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Christophe Massiot <massiot@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #include <vlc/input.h>
33 #include "vlc_playlist.h"
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
42 #define PROXY_TEXT N_("HTTP proxy")
43 #define PROXY_LONGTEXT N_( \
44 "You can specify an HTTP proxy to use. It must be of the form " \
45 "http://myproxy.mydomain:myport/. If none is specified, the HTTP_PROXY " \
46 "environment variable will be tried." )
48 #define CACHING_TEXT N_("Caching value in ms")
49 #define CACHING_LONGTEXT N_( \
50 "Allows you to modify the default caching value for http streams. This " \
51 "value should be set in millisecond units." )
53 #define USER_TEXT N_("HTTP user name")
54 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
55 "be used for the connection (Basic authentication only).")
57 #define PASS_TEXT N_("HTTP password")
58 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
59 "used for the connection.")
61 #define AGENT_TEXT N_("HTTP user agent")
62 #define AGENT_LONGTEXT N_("Allows you to modify the user agent that will be " \
63 "used for the connection.")
65 #define RECONNECT_TEXT N_("Auto re-connect")
66 #define RECONNECT_LONGTEXT N_("Will automatically attempt a re-connection " \
67 "in case it was untimely closed.")
70 set_description( _("HTTP input") );
71 set_capability( "access2", 0 );
73 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
75 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
76 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
77 add_string( "http-user", NULL, NULL, USER_TEXT, USER_LONGTEXT, VLC_FALSE );
78 add_string( "http-pwd", NULL , NULL, PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
79 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
80 AGENT_LONGTEXT, VLC_FALSE );
81 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
82 RECONNECT_LONGTEXT, VLC_TRUE );
84 add_shortcut( "http" );
85 add_shortcut( "http4" );
86 add_shortcut( "http6" );
87 set_callbacks( Open, Close );
90 /*****************************************************************************
92 *****************************************************************************/
101 char *psz_user_agent;
116 vlc_bool_t b_icecast;
118 vlc_bool_t b_chunked;
121 vlc_bool_t b_seekable;
122 vlc_bool_t b_reconnect;
123 vlc_bool_t b_pace_control;
127 static int Read( access_t *, uint8_t *, int );
128 static int Seek( access_t *, int64_t );
129 static int Control( access_t *, int, va_list );
132 static void ParseURL( access_sys_t *, char *psz_url );
133 static int Connect( access_t *, int64_t );
135 /*****************************************************************************
137 *****************************************************************************/
138 static int Open( vlc_object_t *p_this )
140 access_t *p_access = (access_t*)p_this;
144 /* First set ipv4/ipv6 */
145 var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
146 var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
148 if( *p_access->psz_access )
151 /* Find out which shortcut was used */
152 if( !strncmp( p_access->psz_access, "http4", 6 ) )
154 val.b_bool = VLC_TRUE;
155 var_Set( p_access, "ipv4", val );
157 val.b_bool = VLC_FALSE;
158 var_Set( p_access, "ipv6", val );
160 else if( !strncmp( p_access->psz_access, "http6", 6 ) )
162 val.b_bool = VLC_TRUE;
163 var_Set( p_access, "ipv6", val );
165 val.b_bool = VLC_FALSE;
166 var_Set( p_access, "ipv4", val );
170 /* Set up p_access */
171 p_access->pf_read = Read;
172 p_access->pf_block = NULL;
173 p_access->pf_control = Control;
174 p_access->pf_seek = Seek;
175 p_access->info.i_update = 0;
176 p_access->info.i_size = 0;
177 p_access->info.i_pos = 0;
178 p_access->info.b_eof = VLC_FALSE;
179 p_access->info.i_title = 0;
180 p_access->info.i_seekpoint = 0;
181 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
182 memset( p_sys, 0, sizeof( access_sys_t ) );
184 p_sys->b_proxy = VLC_FALSE;
185 p_sys->i_version = 1;
186 p_sys->b_seekable = VLC_TRUE;
187 p_sys->psz_mime = NULL;
188 p_sys->psz_pragma = NULL;
189 p_sys->b_mms = VLC_FALSE;
190 p_sys->b_icecast = VLC_FALSE;
191 p_sys->psz_location = NULL;
192 p_sys->psz_user_agent = NULL;
193 p_sys->b_pace_control = VLC_TRUE;
196 ParseURL( p_sys, p_access->psz_path );
197 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
199 msg_Warn( p_access, "invalid host" );
202 if( p_sys->url.i_port <= 0 )
204 p_sys->url.i_port = 80;
206 if( !p_sys->psz_user || *p_sys->psz_user == '\0' )
208 p_sys->psz_user = var_CreateGetString( p_access, "http-user" );
209 p_sys->psz_passwd = var_CreateGetString( p_access, "http-pwd" );
213 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
216 psz = var_CreateGetString( p_access, "http-proxy" );
219 p_sys->b_proxy = VLC_TRUE;
220 vlc_UrlParse( &p_sys->proxy, psz, 0 );
224 char *psz_proxy = getenv( "http_proxy" );
225 if( psz_proxy && *psz_proxy )
227 p_sys->b_proxy = VLC_TRUE;
228 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
237 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
239 msg_Warn( p_access, "invalid proxy host" );
242 if( p_sys->proxy.i_port <= 0 )
244 p_sys->proxy.i_port = 80;
248 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
249 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
252 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
253 p_sys->proxy.i_port );
255 if( p_sys->psz_user && *p_sys->psz_user )
257 msg_Dbg( p_access, " user='%s', pwd='%s'",
258 p_sys->psz_user, p_sys->psz_passwd );
261 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
264 if( Connect( p_access, 0 ) )
266 /* Retry with http 1.0 */
267 p_sys->i_version = 0;
269 if( p_access->b_die ||
270 Connect( p_access, 0 ) )
276 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
277 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
278 p_sys->psz_location && *p_sys->psz_location )
280 playlist_t * p_playlist;
282 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
284 p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
288 msg_Err( p_access, "redirection failed: can't find playlist" );
291 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
292 playlist_Add( p_playlist, p_sys->psz_location, p_sys->psz_location,
294 p_playlist->i_index + 1 );
295 vlc_object_release( p_playlist );
297 p_access->info.i_size = 0; /* Force to stop reading */
302 msg_Dbg( p_access, "This is actually a live mms server, BAIL" );
306 if( p_sys->b_icecast )
308 if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
309 p_access->psz_demux = strdup( "mp3" );
312 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
314 if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "video/nsv" ) )
315 p_access->psz_demux = strdup( "nsv" );
316 else if( p_sys->psz_mime &&
317 ( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
318 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) ) )
319 p_access->psz_demux = strdup( "m4a" );
321 p_access->psz_demux = strdup( "mp3" );
323 msg_Info( p_access, "ICY server found, %s demuxer selected",
324 p_access->psz_demux );
326 #if 0 /* Doesn't work really well because of the pre-buffering in shoutcast
327 * servers (the buffer content will be sent as fast as possible). */
328 p_sys->b_pace_control = VLC_FALSE;
332 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
335 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
340 vlc_UrlClean( &p_sys->url );
341 vlc_UrlClean( &p_sys->proxy );
342 if( p_sys->psz_mime ) free( p_sys->psz_mime );
343 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
344 if( p_sys->psz_location ) free( p_sys->psz_location );
345 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
346 if( p_sys->psz_user ) free( p_sys->psz_user );
347 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
351 net_Close( p_sys->fd );
357 /*****************************************************************************
359 *****************************************************************************/
360 static void Close( vlc_object_t *p_this )
362 access_t *p_access = (access_t*)p_this;
363 access_sys_t *p_sys = p_access->p_sys;
365 vlc_UrlClean( &p_sys->url );
366 vlc_UrlClean( &p_sys->proxy );
368 if( p_sys->psz_user ) free( p_sys->psz_user );
369 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
371 if( p_sys->psz_mime ) free( p_sys->psz_mime );
372 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
373 if( p_sys->psz_location ) free( p_sys->psz_location );
375 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
379 net_Close( p_sys->fd );
384 /*****************************************************************************
385 * Read: Read up to i_len bytes from the http connection and place in
386 * p_buffer. Return the actual number of bytes read
387 *****************************************************************************/
388 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
390 access_sys_t *p_sys = p_access->p_sys;
395 p_access->info.b_eof = VLC_TRUE;
399 if( p_access->info.i_size > 0 &&
400 i_len + p_access->info.i_pos > p_access->info.i_size )
402 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
404 p_access->info.b_eof = VLC_TRUE;
408 if( p_sys->b_chunked )
410 if( p_sys->i_chunk < 0 )
412 p_access->info.b_eof = VLC_TRUE;
416 if( p_sys->i_chunk <= 0 )
418 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
419 /* read the chunk header */
422 msg_Dbg( p_access, "failed reading chunk-header line" );
425 p_sys->i_chunk = strtoll( psz, NULL, 16 );
428 if( p_sys->i_chunk <= 0 ) /* eof */
431 p_access->info.b_eof = VLC_TRUE;
436 if( i_len > p_sys->i_chunk )
438 i_len = p_sys->i_chunk;
443 i_read = net_Read( p_access, p_sys->fd, NULL, p_buffer, i_len,
447 p_access->info.i_pos += i_read;
449 if( p_sys->b_chunked )
451 p_sys->i_chunk -= i_read;
452 if( p_sys->i_chunk <= 0 )
454 /* read the empty line */
455 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
456 if( psz ) free( psz );
460 else if( i_read == 0 )
462 if( p_sys->b_reconnect )
464 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
465 net_Close( p_sys->fd ); p_sys->fd = -1;
466 if( Connect( p_access, p_access->info.i_pos ) )
468 msg_Dbg( p_access, "reconnection failed" );
472 p_sys->b_reconnect = VLC_FALSE;
473 i_read = Read( p_access, p_buffer, i_len );
474 p_sys->b_reconnect = VLC_TRUE;
478 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
484 /*****************************************************************************
485 * Seek: close and re-open a connection at the right place
486 *****************************************************************************/
487 static int Seek( access_t *p_access, int64_t i_pos )
489 access_sys_t *p_sys = p_access->p_sys;
491 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
493 net_Close( p_sys->fd ); p_sys->fd = -1;
495 if( Connect( p_access, i_pos ) )
497 msg_Err( p_access, "seek failed" );
498 p_access->info.b_eof = VLC_TRUE;
504 /*****************************************************************************
506 *****************************************************************************/
507 static int Control( access_t *p_access, int i_query, va_list args )
509 access_sys_t *p_sys = p_access->p_sys;
517 case ACCESS_CAN_SEEK:
518 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
519 *pb_bool = p_sys->b_seekable;
521 case ACCESS_CAN_FASTSEEK:
522 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
523 *pb_bool = VLC_FALSE;
525 case ACCESS_CAN_PAUSE:
526 case ACCESS_CAN_CONTROL_PACE:
527 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
528 *pb_bool = p_sys->b_pace_control;
533 pi_int = (int*)va_arg( args, int * );
537 case ACCESS_GET_PTS_DELAY:
538 pi_64 = (int64_t*)va_arg( args, int64_t * );
539 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
543 case ACCESS_SET_PAUSE_STATE:
546 case ACCESS_GET_TITLE_INFO:
547 case ACCESS_SET_TITLE:
548 case ACCESS_SET_SEEKPOINT:
549 case ACCESS_SET_PRIVATE_ID_STATE:
553 msg_Warn( p_access, "unimplemented query in control" );
560 /*****************************************************************************
561 * ParseURL: extract user:password
562 *****************************************************************************/
563 static void ParseURL( access_sys_t *p_sys, char *psz_url )
565 char *psz_dup = strdup( psz_url );
569 /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
577 if( ( p = strchr( psz, '@' ) ) )
582 comma = strchr( psz, ':' );
584 /* Retreive user:password */
589 p_sys->psz_user = strdup( psz );
590 p_sys->psz_passwd = strdup( comma );
594 p_sys->psz_user = strdup( psz );
603 vlc_UrlParse( &p_sys->url, p, 0 );
608 /*****************************************************************************
610 *****************************************************************************/
611 static int Connect( access_t *p_access, int64_t i_tell )
613 access_sys_t *p_sys = p_access->p_sys;
614 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
618 if( p_sys->psz_location ) free( p_sys->psz_location );
619 if( p_sys->psz_mime ) free( p_sys->psz_mime );
620 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
622 p_sys->psz_location = NULL;
623 p_sys->psz_mime = NULL;
624 p_sys->psz_pragma = NULL;
625 p_sys->b_mms = VLC_FALSE;
626 p_sys->b_chunked = VLC_FALSE;
629 p_access->info.i_size = 0;
630 p_access->info.i_pos = i_tell;
631 p_access->info.b_eof = VLC_FALSE;
634 /* Open connection */
635 p_sys->fd = net_OpenTCP( p_access, srv.psz_host, srv.i_port );
638 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
644 if( p_sys->url.psz_path )
646 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
647 "GET http://%s:%d%s HTTP/1.%d\r\n",
648 p_sys->url.psz_host, p_sys->url.i_port,
649 p_sys->url.psz_path, p_sys->i_version );
653 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
654 "GET http://%s:%d/ HTTP/1.%d\r\n",
655 p_sys->url.psz_host, p_sys->url.i_port,
661 char *psz_path = p_sys->url.psz_path;
662 if( !psz_path || !*psz_path )
666 if( p_sys->url.i_port != 80)
668 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
669 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
670 psz_path, p_sys->i_version, p_sys->url.psz_host,
675 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
676 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
677 psz_path, p_sys->i_version, p_sys->url.psz_host );
681 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "User-Agent: %s\r\n",
682 p_sys->psz_user_agent );
684 if( p_sys->i_version == 1 )
686 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
687 "Range: bytes="I64Fd"-\r\n", i_tell );
689 /* Authentification */
690 if( p_sys->psz_user && *p_sys->psz_user )
695 asprintf( &buf, "%s:%s", p_sys->psz_user,
696 p_sys->psz_passwd ? p_sys->psz_passwd : "" );
698 b64 = vlc_b64_encode( buf );
700 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
701 "Authorization: Basic %s\r\n", b64 );
704 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
705 "Connection: Close\r\n" );
707 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
709 msg_Err( p_access, "failed to send request" );
710 net_Close( p_sys->fd ); p_sys->fd = -1;
715 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL ) ) == NULL )
717 msg_Err( p_access, "failed to read answer" );
720 if( !strncmp( psz, "HTTP/1.", 7 ) )
722 p_sys->psz_protocol = "HTTP";
723 p_sys->i_code = atoi( &psz[9] );
725 else if( !strncmp( psz, "ICY", 3 ) )
727 p_sys->psz_protocol = "ICY";
728 p_sys->i_code = atoi( &psz[4] );
729 p_sys->b_reconnect = VLC_TRUE;
733 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
737 msg_Dbg( p_access, "protocol '%s' answer code %d",
738 p_sys->psz_protocol, p_sys->i_code );
739 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
741 p_sys->b_seekable = VLC_FALSE;
743 if( p_sys->i_code != 206 )
745 p_sys->b_seekable = VLC_FALSE;
747 if( p_sys->i_code >= 400 )
749 msg_Err( p_access, "error: %s", psz );
757 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
762 msg_Err( p_access, "failed to read answer" );
766 /* msg_Dbg( p_input, "Line=%s", psz ); */
774 if( ( p = strchr( psz, ':' ) ) == NULL )
776 msg_Err( p_access, "malformed header line: %s", psz );
781 while( *p == ' ' ) p++;
783 if( !strcasecmp( psz, "Content-Length" ) )
785 p_access->info.i_size = i_tell + atoll( p );
786 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
788 else if( !strcasecmp( psz, "Location" ) )
790 if( p_sys->psz_location ) free( p_sys->psz_location );
791 p_sys->psz_location = strdup( p );
793 else if( !strcasecmp( psz, "Content-Type" ) )
795 if( p_sys->psz_mime ) free( p_sys->psz_mime );
796 p_sys->psz_mime = strdup( p );
797 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
799 else if( !strcasecmp( psz, "Pragma" ) )
801 if( !strcasecmp( psz, "Pragma: features" ) )
802 p_sys->b_mms = VLC_TRUE;
803 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
804 p_sys->psz_pragma = strdup( p );
805 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
807 else if( !strcasecmp( psz, "Server" ) )
809 msg_Dbg( p_access, "Server: %s", p );
810 if( !strncasecmp( p, "Icecast", 7 ) ||
811 !strncasecmp( p, "Nanocaster", 10 ) )
813 /* Remember if this is Icecast
814 * we need to force mp3 in some cases without breaking autodetection */
816 /* Let live365 streams (nanocaster) piggyback on the icecast routine.
817 * They look very similar */
819 p_sys->b_reconnect = VLC_TRUE;
820 p_sys->b_pace_control = VLC_FALSE;
821 p_sys->b_icecast = VLC_TRUE;
824 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
826 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
827 if( !strncasecmp( p, "chunked", 7 ) )
829 p_sys->b_chunked = VLC_TRUE;
832 else if( !strncasecmp( psz, "icy-", 4 ) ||
833 !strncasecmp( psz, "ice-", 4 ) ||
834 !strncasecmp( psz, "x-audiocast", 11 ) )
836 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
844 net_Close( p_sys->fd ); p_sys->fd = -1;