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( "access", 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;
115 static void Seek( input_thread_t *, off_t );
116 static ssize_t Read( input_thread_t *, byte_t *, size_t );
118 static void ParseURL( access_sys_t *, char *psz_url );
119 static int Connect( input_thread_t *, vlc_bool_t *, off_t *, off_t );
121 static char *b64_encode( unsigned char *src );
123 /*****************************************************************************
125 *****************************************************************************/
126 static int Open ( vlc_object_t *p_this )
128 input_thread_t *p_input = (input_thread_t*)p_this;
132 /* Create private struct */
133 p_sys = p_input->p_access_data = malloc( sizeof( access_sys_t ) );
134 memset( p_sys, 0, sizeof( access_sys_t ) );
136 p_sys->b_proxy = VLC_FALSE;
137 p_sys->i_version = 1;
138 p_sys->psz_mime = NULL;
139 p_sys->psz_location = NULL;
140 p_sys->psz_user_agent = NULL;
142 /* First set ipv4/ipv6 */
143 var_Create( p_input, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
144 var_Create( p_input, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
146 if( *p_input->psz_access )
148 /* Find out which shortcut was used */
149 if( !strncmp( p_input->psz_access, "http4", 6 ) )
151 val.b_bool = VLC_TRUE;
152 var_Set( p_input, "ipv4", val );
154 val.b_bool = VLC_FALSE;
155 var_Set( p_input, "ipv6", val );
157 else if( !strncmp( p_input->psz_access, "http6", 6 ) )
159 val.b_bool = VLC_TRUE;
160 var_Set( p_input, "ipv6", val );
162 val.b_bool = VLC_FALSE;
163 var_Set( p_input, "ipv4", val );
168 ParseURL( p_sys, p_input->psz_name );
169 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
171 msg_Warn( p_input, "invalid host" );
174 if( p_sys->url.i_port <= 0 )
176 p_sys->url.i_port = 80;
178 if( !p_sys->psz_user || *p_sys->psz_user == '\0' )
180 var_Create( p_input, "http-user", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
181 var_Get( p_input, "http-user", &val );
182 p_sys->psz_user = val.psz_string;
184 var_Create( p_input, "http-pwd", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
185 var_Get( p_input, "http-pwd", &val );
186 p_sys->psz_passwd = val.psz_string;
190 var_Create( p_input, "http-user-agent", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
191 var_Get( p_input, "http-user-agent", &val );
192 p_sys->psz_user_agent = val.psz_string;
195 var_Create( p_input, "http-proxy", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
196 var_Get( p_input, "http-proxy", &val );
197 if( val.psz_string && *val.psz_string )
199 p_sys->b_proxy = VLC_TRUE;
200 vlc_UrlParse( &p_sys->proxy, val.psz_string, 0 );
204 char *psz_proxy = getenv( "http_proxy" );
205 if( psz_proxy && *psz_proxy )
207 p_sys->b_proxy = VLC_TRUE;
208 vlc_UrlParse( &p_sys->proxy, val.psz_string, 0 );
217 free( val.psz_string );
222 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
224 msg_Warn( p_input, "invalid proxy host" );
227 if( p_sys->proxy.i_port <= 0 )
229 p_sys->proxy.i_port = 80;
233 msg_Dbg( p_input, "http: server='%s' port=%d file='%s",
234 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
237 msg_Dbg( p_input, " proxy %s:%d", p_sys->proxy.psz_host,
238 p_sys->proxy.i_port );
240 if( p_sys->psz_user && *p_sys->psz_user )
242 msg_Dbg( p_input, " user='%s', pwd='%s'",
243 p_sys->psz_user, p_sys->psz_passwd );
247 if( Connect( p_input, &p_input->stream.b_seekable,
248 &p_input->stream.p_selected_area->i_size, 0 ) )
250 /* Retry with http 1.0 */
251 p_sys->i_version = 0;
253 if( p_input->b_die ||
254 Connect( p_input, &p_input->stream.b_seekable,
255 &p_input->stream.p_selected_area->i_size, 0 ) )
261 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
262 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
263 p_sys->psz_location && *p_sys->psz_location )
265 playlist_t * p_playlist;
267 msg_Dbg( p_input, "redirection to %s", p_sys->psz_location );
269 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
272 msg_Err( p_input, "redirection failed: can't find playlist" );
275 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
276 playlist_Add( p_playlist, p_sys->psz_location, p_sys->psz_location,
277 PLAYLIST_INSERT | PLAYLIST_GO,
278 p_playlist->i_index + 1 );
279 vlc_object_release( p_playlist );
281 p_sys->i_size = 0; /* Force to stop reading */
284 /* Finish to set up p_input */
285 p_input->pf_read = Read;
286 p_input->pf_set_program = input_SetProgram;
287 p_input->pf_set_area = NULL;
288 p_input->pf_seek = Seek;
290 vlc_mutex_lock( &p_input->stream.stream_lock );
291 p_input->stream.b_pace_control = VLC_TRUE;
292 p_input->stream.p_selected_area->i_tell = 0;
293 p_input->stream.i_method = INPUT_METHOD_NETWORK;
294 vlc_mutex_unlock( &p_input->stream.stream_lock );
296 if( !strcmp( p_sys->psz_protocol, "ICY" ) &&
297 ( !p_input->psz_demux || !*p_input->psz_demux ) )
299 if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "video/nsv" ) )
301 p_input->psz_demux = strdup( "nsv" );
305 p_input->psz_demux = strdup( "mp3" );
307 msg_Info( p_input, "ICY server found, %s demuxer selected",
308 p_input->psz_demux );
311 /* Update default_pts to a suitable value for http access */
312 var_Create( p_input, "http-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
313 var_Get( p_input, "http-caching", &val );
314 p_input->i_pts_delay = val.i_int * 1000;
319 vlc_UrlClean( &p_sys->url );
320 vlc_UrlClean( &p_sys->proxy );
321 if( p_sys->psz_mime ) free( p_sys->psz_mime );
322 if( p_sys->psz_location ) free( p_sys->psz_location );
323 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
324 if( p_sys->psz_user ) free( p_sys->psz_user );
325 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
329 net_Close( p_sys->fd );
335 /*****************************************************************************
337 *****************************************************************************/
338 static void Close( vlc_object_t *p_this )
340 input_thread_t *p_input = (input_thread_t*)p_this;
341 access_sys_t *p_sys = p_input->p_access_data;
343 vlc_UrlClean( &p_sys->url );
344 vlc_UrlClean( &p_sys->proxy );
346 if( p_sys->psz_user ) free( p_sys->psz_user );
347 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
349 if( p_sys->psz_mime ) free( p_sys->psz_mime );
350 if( p_sys->psz_location ) free( p_sys->psz_location );
352 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
356 net_Close( p_sys->fd );
361 /*****************************************************************************
362 * Seek: close and re-open a connection at the right place
363 *****************************************************************************/
364 static void Seek( input_thread_t * p_input, off_t i_pos )
366 access_sys_t *p_sys = p_input->p_access_data;
368 msg_Dbg( p_input, "trying to seek to "I64Fd, i_pos );
370 net_Close( p_sys->fd ); p_sys->fd = -1;
372 if( Connect( p_input, &p_input->stream.b_seekable,
373 &p_input->stream.p_selected_area->i_size, i_pos ) )
375 msg_Err( p_input, "seek failed" );
378 vlc_mutex_lock( &p_input->stream.stream_lock );
379 p_input->stream.p_selected_area->i_tell = i_pos;
380 vlc_mutex_unlock( &p_input->stream.stream_lock );
383 /*****************************************************************************
384 * Read: Read up to i_len bytes from the http connection and place in
385 * p_buffer. Return the actual number of bytes read
386 *****************************************************************************/
387 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
389 access_sys_t *p_sys = p_input->p_access_data;
396 if( p_sys->i_size > 0 && i_len + p_sys->i_tell > p_sys->i_size )
398 if( ( i_len = p_sys->i_size - p_sys->i_tell ) == 0 )
403 if( p_sys->b_chunked )
405 if( p_sys->i_chunk < 0 )
410 if( p_sys->i_chunk <= 0 )
412 char *psz = net_Gets( VLC_OBJECT(p_input), p_sys->fd );
413 /* read the chunk header */
416 msg_Dbg( p_input, "failed reading chunk-header line" );
419 p_sys->i_chunk = strtoll( psz, NULL, 16 );
422 if( p_sys->i_chunk <= 0 ) /* eof */
429 if( i_len > p_sys->i_chunk )
431 i_len = p_sys->i_chunk;
436 i_read = net_Read( p_input, p_sys->fd, p_buffer, i_len, VLC_FALSE );
439 p_sys->i_tell += i_read;
441 if( p_sys->b_chunked )
443 p_sys->i_chunk -= i_read;
444 if( p_sys->i_chunk <= 0 )
446 /* read the empty line */
447 char *psz = net_Gets( VLC_OBJECT(p_input), p_sys->fd );
458 /*****************************************************************************
459 * ParseURL: extract user:password
460 *****************************************************************************/
461 static void ParseURL( access_sys_t *p_sys, char *psz_url )
463 char *psz_dup = strdup( psz_url );
467 /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
475 if( ( p = strchr( psz, '@' ) ) )
480 comma = strchr( psz, ':' );
482 /* Retreive user:password */
487 p_sys->psz_user = strdup( psz );
488 p_sys->psz_passwd = strdup( comma );
492 p_sys->psz_user = strdup( psz );
501 vlc_UrlParse( &p_sys->url, p, 0 );
506 /*****************************************************************************
508 *****************************************************************************/
509 static int Connect( input_thread_t *p_input, vlc_bool_t *pb_seekable,
510 off_t *pi_size, off_t i_tell )
512 access_sys_t *p_sys = p_input->p_access_data;
513 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
517 if( p_sys->psz_location ) free( p_sys->psz_location );
518 if( p_sys->psz_mime ) free( p_sys->psz_mime );
520 p_sys->psz_location = NULL;
521 p_sys->psz_mime = NULL;
522 p_sys->b_chunked = VLC_FALSE;
525 p_sys->i_tell = i_tell;
528 /* Open connection */
529 p_sys->fd = net_OpenTCP( p_input, srv.psz_host, srv.i_port );
532 msg_Err( p_input, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
538 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
539 "GET http://%s:%d/%s HTTP/1.%d\r\n",
540 p_sys->url.psz_host, p_sys->url.i_port,
541 p_sys->url.psz_path, p_sys->i_version );
545 char *psz_path = p_sys->url.psz_path;
546 if( !psz_path || !*psz_path )
550 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
551 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
552 psz_path, p_sys->i_version, p_sys->url.psz_host );
555 net_Printf( VLC_OBJECT(p_input), p_sys->fd, "User-Agent: %s\r\n",
556 p_sys->psz_user_agent );
558 if( p_sys->i_version == 1 )
560 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
561 "Range: bytes="I64Fd"-\r\n", i_tell );
563 /* Authentification */
564 if( p_sys->psz_user && *p_sys->psz_user )
569 asprintf( &buf, "%s:%s", p_sys->psz_user,
570 p_sys->psz_passwd ? p_sys->psz_passwd : "" );
572 b64 = b64_encode( buf );
574 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
575 "Authorization: Basic %s\r\n", b64 );
578 net_Printf( VLC_OBJECT(p_input), p_sys->fd, "Connection: Close\r\n" );
580 if( net_Printf( VLC_OBJECT(p_input), p_sys->fd, "\r\n" ) < 0 )
582 msg_Err( p_input, "failed to send request" );
583 net_Close( p_sys->fd ); p_sys->fd = -1;
588 *pb_seekable = p_sys->i_version == 1 ? VLC_TRUE : VLC_FALSE;
592 if( ( psz = net_Gets( VLC_OBJECT(p_input), p_sys->fd ) ) == NULL )
594 msg_Err( p_input, "failed to read answer" );
597 if( !strncmp( psz, "HTTP/1.", 7 ) )
599 p_sys->psz_protocol = "HTTP";
600 p_sys->i_code = atoi( &psz[9] );
602 else if( !strncmp( psz, "ICY", 3 ) )
604 p_sys->psz_protocol = "ICY";
605 p_sys->i_code = atoi( &psz[4] );
609 msg_Err( p_input, "invalid HTTP reply '%s'", psz );
613 msg_Dbg( p_input, "protocol '%s' answer code %d",
614 p_sys->psz_protocol, p_sys->i_code );
615 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
617 *pb_seekable = VLC_FALSE;
619 if( p_sys->i_code != 206 )
621 *pb_seekable = VLC_FALSE;
623 if( p_sys->i_code >= 400 )
625 msg_Err( p_input, "error: %s", psz );
633 char *psz = net_Gets( VLC_OBJECT(p_input), p_sys->fd );
638 msg_Err( p_input, "failed to read answer" );
642 /* msg_Dbg( p_input, "Line=%s", psz ); */
650 if( ( p = strchr( psz, ':' ) ) == NULL )
652 msg_Err( p_input, "malformed header line: %s", psz );
657 while( *p == ' ' ) p++;
659 if( !strcasecmp( psz, "Content-Length" ) )
661 *pi_size = p_sys->i_size = i_tell + atoll( p );
662 msg_Dbg( p_input, "stream size="I64Fd, p_sys->i_size );
664 else if( !strcasecmp( psz, "Location" ) )
666 if( p_sys->psz_location ) free( p_sys->psz_location );
667 p_sys->psz_location = strdup( p );
669 else if( !strcasecmp( psz, "Content-Type" ) )
671 if( p_sys->psz_mime ) free( p_sys->psz_mime );
672 p_sys->psz_mime = strdup( p );
673 msg_Dbg( p_input, "Content-Type: %s", p_sys->psz_mime );
675 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
677 msg_Dbg( p_input, "Transfer-Encoding: %s", p );
678 if( !strncasecmp( p, "chunked", 7 ) )
680 p_sys->b_chunked = VLC_TRUE;
689 net_Close( p_sys->fd ); p_sys->fd = -1;
693 /*****************************************************************************
695 *****************************************************************************/
696 static char *b64_encode( unsigned char *src )
698 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
700 char *dst = malloc( strlen( src ) * 4 / 3 + 12 );
703 unsigned i_shift = 0;
709 i_bits = ( i_bits << 8 )|( *src++ );
712 else if( i_shift > 0 )
714 i_bits <<= 6 - i_shift;
723 while( i_shift >= 6 )
726 *dst++ = b64[(i_bits >> i_shift)&0x3f];