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.")
66 set_description( _("HTTP input") );
67 set_capability( "access2", 0 );
69 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
71 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
72 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
73 add_string( "http-user", NULL, NULL, USER_TEXT, USER_LONGTEXT, VLC_FALSE );
74 add_string( "http-pwd", NULL , NULL, PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
75 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
76 AGENT_LONGTEXT, VLC_FALSE );
78 add_shortcut( "http" );
79 add_shortcut( "http4" );
80 add_shortcut( "http6" );
81 set_callbacks( Open, Close );
84 /*****************************************************************************
86 *****************************************************************************/
109 vlc_bool_t b_chunked;
112 vlc_bool_t b_seekable;
116 static int Read( access_t *, uint8_t *, int );
117 static int Seek( access_t *, int64_t );
118 static int Control( access_t *, int, va_list );
121 static void ParseURL( access_sys_t *, char *psz_url );
122 static int Connect( access_t *, int64_t );
123 static char *b64_encode( unsigned char *src );
125 /*****************************************************************************
127 *****************************************************************************/
128 static int Open ( vlc_object_t *p_this )
130 access_t *p_access = (access_t*)p_this;
134 /* First set ipv4/ipv6 */
135 var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
136 var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
138 if( *p_access->psz_access )
140 /* Find out which shortcut was used */
141 if( !strncmp( p_access->psz_access, "http4", 6 ) )
143 val.b_bool = VLC_TRUE;
144 var_Set( p_access, "ipv4", val );
146 val.b_bool = VLC_FALSE;
147 var_Set( p_access, "ipv6", val );
149 else if( !strncmp( p_access->psz_access, "http6", 6 ) )
151 val.b_bool = VLC_TRUE;
152 var_Set( p_access, "ipv6", val );
154 val.b_bool = VLC_FALSE;
155 var_Set( p_access, "ipv4", val );
159 /* Set up p_access */
160 p_access->pf_read = Read;
161 p_access->pf_block = NULL;
162 p_access->pf_control = Control;
163 p_access->pf_seek = Seek;
164 p_access->info.i_update = 0;
165 p_access->info.i_size = 0;
166 p_access->info.i_pos = 0;
167 p_access->info.b_eof = VLC_FALSE;
168 p_access->info.i_title = 0;
169 p_access->info.i_seekpoint = 0;
170 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
171 memset( p_sys, 0, sizeof( access_sys_t ) );
173 p_sys->b_proxy = VLC_FALSE;
174 p_sys->i_version = 1;
175 p_sys->b_seekable = VLC_TRUE;
176 p_sys->psz_mime = NULL;
177 p_sys->psz_location = NULL;
178 p_sys->psz_user_agent = NULL;
182 ParseURL( p_sys, p_access->psz_path );
183 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
185 msg_Warn( p_access, "invalid host" );
188 if( p_sys->url.i_port <= 0 )
190 p_sys->url.i_port = 80;
192 if( !p_sys->psz_user || *p_sys->psz_user == '\0' )
194 var_Create( p_access, "http-user", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
195 var_Get( p_access, "http-user", &val );
196 p_sys->psz_user = val.psz_string;
198 var_Create( p_access, "http-pwd", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
199 var_Get( p_access, "http-pwd", &val );
200 p_sys->psz_passwd = val.psz_string;
204 var_Create( p_access, "http-user-agent", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
205 var_Get( p_access, "http-user-agent", &val );
206 p_sys->psz_user_agent = val.psz_string;
209 var_Create( p_access, "http-proxy", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
210 var_Get( p_access, "http-proxy", &val );
211 if( val.psz_string && *val.psz_string )
213 p_sys->b_proxy = VLC_TRUE;
214 vlc_UrlParse( &p_sys->proxy, val.psz_string, 0 );
218 char *psz_proxy = getenv( "http_proxy" );
219 if( psz_proxy && *psz_proxy )
221 p_sys->b_proxy = VLC_TRUE;
222 vlc_UrlParse( &p_sys->proxy, val.psz_string, 0 );
231 free( val.psz_string );
236 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
238 msg_Warn( p_access, "invalid proxy host" );
241 if( p_sys->proxy.i_port <= 0 )
243 p_sys->proxy.i_port = 80;
247 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
248 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
251 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
252 p_sys->proxy.i_port );
254 if( p_sys->psz_user && *p_sys->psz_user )
256 msg_Dbg( p_access, " user='%s', pwd='%s'",
257 p_sys->psz_user, p_sys->psz_passwd );
261 if( Connect( p_access, 0 ) )
263 /* Retry with http 1.0 */
264 p_sys->i_version = 0;
266 if( p_access->b_die ||
267 Connect( p_access, 0 ) )
273 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
274 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
275 p_sys->psz_location && *p_sys->psz_location )
277 playlist_t * p_playlist;
279 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
281 p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
284 msg_Err( p_access, "redirection failed: can't find playlist" );
287 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
288 playlist_Add( p_playlist, p_sys->psz_location, p_sys->psz_location,
289 PLAYLIST_INSERT | PLAYLIST_GO,
290 p_playlist->i_index + 1 );
291 vlc_object_release( p_playlist );
293 p_access->info.i_size = 0; /* Force to stop reading */
296 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
298 if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "video/nsv" ) )
299 p_access->psz_demux = strdup( "nsv" );
301 p_access->psz_demux = strdup( "mp3" );
303 msg_Info( p_access, "ICY server found, %s demuxer selected",
304 p_access->psz_demux );
308 var_Create( p_access, "http-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
313 vlc_UrlClean( &p_sys->url );
314 vlc_UrlClean( &p_sys->proxy );
315 if( p_sys->psz_mime ) free( p_sys->psz_mime );
316 if( p_sys->psz_location ) free( p_sys->psz_location );
317 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
318 if( p_sys->psz_user ) free( p_sys->psz_user );
319 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
323 net_Close( p_sys->fd );
329 /*****************************************************************************
331 *****************************************************************************/
332 static void Close( vlc_object_t *p_this )
334 access_t *p_access = (access_t*)p_this;
335 access_sys_t *p_sys = p_access->p_sys;
337 vlc_UrlClean( &p_sys->url );
338 vlc_UrlClean( &p_sys->proxy );
340 if( p_sys->psz_user ) free( p_sys->psz_user );
341 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
343 if( p_sys->psz_mime ) free( p_sys->psz_mime );
344 if( p_sys->psz_location ) free( p_sys->psz_location );
346 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
350 net_Close( p_sys->fd );
355 /*****************************************************************************
356 * Read: Read up to i_len bytes from the http connection and place in
357 * p_buffer. Return the actual number of bytes read
358 *****************************************************************************/
359 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
361 access_sys_t *p_sys = p_access->p_sys;
366 p_access->info.b_eof = VLC_TRUE;
370 if( p_access->info.i_size > 0 &&
371 i_len + p_access->info.i_pos > p_access->info.i_size )
373 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
375 p_access->info.b_eof = VLC_TRUE;
379 if( p_sys->b_chunked )
381 if( p_sys->i_chunk < 0 )
383 p_access->info.b_eof = VLC_TRUE;
387 if( p_sys->i_chunk <= 0 )
389 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
390 /* read the chunk header */
393 msg_Dbg( p_access, "failed reading chunk-header line" );
396 p_sys->i_chunk = strtoll( psz, NULL, 16 );
399 if( p_sys->i_chunk <= 0 ) /* eof */
402 p_access->info.b_eof = VLC_TRUE;
407 if( i_len > p_sys->i_chunk )
409 i_len = p_sys->i_chunk;
414 i_read = net_Read( p_access, p_sys->fd, p_buffer, i_len, VLC_FALSE );
417 p_access->info.i_pos += i_read;
419 if( p_sys->b_chunked )
421 p_sys->i_chunk -= i_read;
422 if( p_sys->i_chunk <= 0 )
424 /* read the empty line */
425 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
433 else if( i_read == 0 )
435 p_access->info.b_eof = VLC_TRUE;
440 /*****************************************************************************
441 * Seek: close and re-open a connection at the right place
442 *****************************************************************************/
443 static int Seek( access_t *p_access, int64_t i_pos )
445 access_sys_t *p_sys = p_access->p_sys;
447 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
449 net_Close( p_sys->fd ); p_sys->fd = -1;
451 if( Connect( p_access, i_pos ) )
453 msg_Err( p_access, "seek failed" );
454 p_access->info.b_eof = VLC_TRUE;
460 /*****************************************************************************
462 *****************************************************************************/
463 static int Control( access_t *p_access, int i_query, va_list args )
465 access_sys_t *p_sys = p_access->p_sys;
474 case ACCESS_CAN_SEEK:
475 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
476 *pb_bool = p_sys->b_seekable;
478 case ACCESS_CAN_FASTSEEK:
479 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
480 *pb_bool = VLC_FALSE;
482 case ACCESS_CAN_PAUSE:
483 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
484 *pb_bool = VLC_TRUE; /* FIXME */
486 case ACCESS_CAN_CONTROL_PACE:
487 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
488 *pb_bool = VLC_TRUE; /* FIXME */
493 pi_int = (int*)va_arg( args, int * );
497 case ACCESS_GET_PTS_DELAY:
498 pi_64 = (int64_t*)va_arg( args, int64_t * );
499 var_Get( p_access, "http-caching", &val );
500 *pi_64 = val.i_int * 1000;
504 case ACCESS_SET_PAUSE_STATE:
507 case ACCESS_GET_TITLE_INFO:
508 case ACCESS_SET_TITLE:
509 case ACCESS_SET_SEEKPOINT:
513 msg_Err( p_access, "unimplemented query in control" );
520 /*****************************************************************************
521 * ParseURL: extract user:password
522 *****************************************************************************/
523 static void ParseURL( access_sys_t *p_sys, char *psz_url )
525 char *psz_dup = strdup( psz_url );
529 /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
537 if( ( p = strchr( psz, '@' ) ) )
542 comma = strchr( psz, ':' );
544 /* Retreive user:password */
549 p_sys->psz_user = strdup( psz );
550 p_sys->psz_passwd = strdup( comma );
554 p_sys->psz_user = strdup( psz );
563 vlc_UrlParse( &p_sys->url, p, 0 );
568 /*****************************************************************************
570 *****************************************************************************/
571 static int Connect( access_t *p_access, int64_t i_tell )
573 access_sys_t *p_sys = p_access->p_sys;
574 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
578 if( p_sys->psz_location ) free( p_sys->psz_location );
579 if( p_sys->psz_mime ) free( p_sys->psz_mime );
581 p_sys->psz_location = NULL;
582 p_sys->psz_mime = NULL;
583 p_sys->b_chunked = VLC_FALSE;
586 p_access->info.i_size = 0;
587 p_access->info.i_pos = i_tell;
588 p_access->info.b_eof = VLC_FALSE;
591 /* Open connection */
592 p_sys->fd = net_OpenTCP( p_access, srv.psz_host, srv.i_port );
595 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
601 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
602 "GET http://%s:%d/%s HTTP/1.%d\r\n",
603 p_sys->url.psz_host, p_sys->url.i_port,
604 p_sys->url.psz_path, p_sys->i_version );
608 char *psz_path = p_sys->url.psz_path;
609 if( !psz_path || !*psz_path )
613 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
614 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
615 psz_path, p_sys->i_version, p_sys->url.psz_host );
618 net_Printf( VLC_OBJECT(p_access), p_sys->fd, "User-Agent: %s\r\n",
619 p_sys->psz_user_agent );
621 if( p_sys->i_version == 1 )
623 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
624 "Range: bytes="I64Fd"-\r\n", i_tell );
626 /* Authentification */
627 if( p_sys->psz_user && *p_sys->psz_user )
632 asprintf( &buf, "%s:%s", p_sys->psz_user,
633 p_sys->psz_passwd ? p_sys->psz_passwd : "" );
635 b64 = b64_encode( buf );
637 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
638 "Authorization: Basic %s\r\n", b64 );
641 net_Printf( VLC_OBJECT(p_access), p_sys->fd, "Connection: Close\r\n" );
643 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, "\r\n" ) < 0 )
645 msg_Err( p_access, "failed to send request" );
646 net_Close( p_sys->fd ); p_sys->fd = -1;
651 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd ) ) == NULL )
653 msg_Err( p_access, "failed to read answer" );
656 if( !strncmp( psz, "HTTP/1.", 7 ) )
658 p_sys->psz_protocol = "HTTP";
659 p_sys->i_code = atoi( &psz[9] );
661 else if( !strncmp( psz, "ICY", 3 ) )
663 p_sys->psz_protocol = "ICY";
664 p_sys->i_code = atoi( &psz[4] );
668 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
672 msg_Dbg( p_access, "protocol '%s' answer code %d",
673 p_sys->psz_protocol, p_sys->i_code );
674 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
676 p_sys->b_seekable = VLC_FALSE;
678 if( p_sys->i_code != 206 )
680 p_sys->b_seekable = VLC_FALSE;
682 if( p_sys->i_code >= 400 )
684 msg_Err( p_access, "error: %s", psz );
692 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
697 msg_Err( p_access, "failed to read answer" );
701 /* msg_Dbg( p_input, "Line=%s", psz ); */
709 if( ( p = strchr( psz, ':' ) ) == NULL )
711 msg_Err( p_access, "malformed header line: %s", psz );
716 while( *p == ' ' ) p++;
718 if( !strcasecmp( psz, "Content-Length" ) )
720 p_access->info.i_size = i_tell + atoll( p );
721 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
723 else if( !strcasecmp( psz, "Location" ) )
725 if( p_sys->psz_location ) free( p_sys->psz_location );
726 p_sys->psz_location = strdup( p );
728 else if( !strcasecmp( psz, "Content-Type" ) )
730 if( p_sys->psz_mime ) free( p_sys->psz_mime );
731 p_sys->psz_mime = strdup( p );
732 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
734 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
736 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
737 if( !strncasecmp( p, "chunked", 7 ) )
739 p_sys->b_chunked = VLC_TRUE;
748 net_Close( p_sys->fd ); p_sys->fd = -1;
752 /*****************************************************************************
754 *****************************************************************************/
755 static char *b64_encode( unsigned char *src )
757 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
759 char *dst = malloc( strlen( src ) * 4 / 3 + 12 );
762 unsigned i_shift = 0;
768 i_bits = ( i_bits << 8 )|( *src++ );
771 else if( i_shift > 0 )
773 i_bits <<= 6 - i_shift;
782 while( i_shift >= 6 )
785 *dst++ = b64[(i_bits >> i_shift)&0x3f];