1 /*****************************************************************************
2 * http.c: HTTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
5 * $Id: http.c,v 1.57 2004/01/25 17:31:22 gbazin Exp $
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 authentification 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 *****************************************************************************/
113 static void Seek( input_thread_t *, off_t );
114 static ssize_t Read( input_thread_t *, byte_t *, size_t );
116 static void ParseURL( access_sys_t *, char *psz_url );
117 static int Connect( input_thread_t *, vlc_bool_t *, off_t *, off_t );
119 static char *b64_encode( unsigned char *src );
121 /*****************************************************************************
123 *****************************************************************************/
124 static int Open ( vlc_object_t *p_this )
126 input_thread_t *p_input = (input_thread_t*)p_this;
130 /* Create private struct */
131 p_sys = p_input->p_access_data = malloc( sizeof( access_sys_t ) );
132 memset( p_sys, 0, sizeof( access_sys_t ) );
134 p_sys->b_proxy = VLC_FALSE;
135 p_sys->i_version = 1;
136 p_sys->psz_mime = NULL;
137 p_sys->psz_location = NULL;
138 p_sys->psz_user_agent = NULL;
140 /* First set ipv4/ipv6 */
141 var_Create( p_input, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
142 var_Create( p_input, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
144 if( *p_input->psz_access )
146 /* Find out which shortcut was used */
147 if( !strncmp( p_input->psz_access, "http4", 6 ) )
149 val.b_bool = VLC_TRUE;
150 var_Set( p_input, "ipv4", val );
152 val.b_bool = VLC_FALSE;
153 var_Set( p_input, "ipv6", val );
155 else if( !strncmp( p_input->psz_access, "http6", 6 ) )
157 val.b_bool = VLC_TRUE;
158 var_Set( p_input, "ipv6", val );
160 val.b_bool = VLC_FALSE;
161 var_Set( p_input, "ipv4", val );
166 ParseURL( p_sys, p_input->psz_name );
167 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
169 msg_Warn( p_input, "invalid host" );
172 if( p_sys->url.i_port <= 0 )
174 p_sys->url.i_port = 80;
176 if( !p_sys->psz_user || *p_sys->psz_user == '\0' )
178 var_Create( p_input, "http-user", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
179 var_Get( p_input, "http-user", &val );
180 p_sys->psz_user = val.psz_string;
182 var_Create( p_input, "http-pwd", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
183 var_Get( p_input, "http-pwd", &val );
184 p_sys->psz_passwd = val.psz_string;
188 var_Create( p_input, "http-user-agent", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
189 var_Get( p_input, "http-user-agent", &val );
190 p_sys->psz_user_agent = val.psz_string;
193 var_Create( p_input, "http-proxy", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
194 var_Get( p_input, "http-proxy", &val );
195 if( val.psz_string && *val.psz_string )
197 p_sys->b_proxy = VLC_TRUE;
198 vlc_UrlParse( &p_sys->proxy, val.psz_string, 0 );
202 char *psz_proxy = getenv( "http_proxy" );
203 if( psz_proxy && *psz_proxy )
205 p_sys->b_proxy = VLC_TRUE;
206 vlc_UrlParse( &p_sys->proxy, val.psz_string, 0 );
215 free( val.psz_string );
220 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
222 msg_Warn( p_input, "invalid proxy host" );
225 if( p_sys->proxy.i_port <= 0 )
227 p_sys->proxy.i_port = 80;
231 msg_Dbg( p_input, "http: server='%s' port=%d file='%s",
232 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
235 msg_Dbg( p_input, " proxy %s:%d", p_sys->proxy.psz_host,
236 p_sys->proxy.i_port );
238 if( p_sys->psz_user && *p_sys->psz_user )
240 msg_Dbg( p_input, " user='%s', pwd='%s'",
241 p_sys->psz_user, p_sys->psz_passwd );
245 if( Connect( p_input, &p_input->stream.b_seekable,
246 &p_input->stream.p_selected_area->i_size, 0 ) )
248 /* Retry with http 1.0 */
249 p_sys->i_version = 0;
251 if( p_input->b_die ||
252 Connect( p_input, &p_input->stream.b_seekable,
253 &p_input->stream.p_selected_area->i_size, 0 ) )
259 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
260 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
261 p_sys->psz_location && *p_sys->psz_location )
263 playlist_t * p_playlist;
265 msg_Dbg( p_input, "redirection to %s", p_sys->psz_location );
267 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
270 msg_Err( p_input, "redirection failed: can't find playlist" );
273 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
274 playlist_Add( p_playlist, p_sys->psz_location, p_sys->psz_location,
275 PLAYLIST_INSERT | PLAYLIST_GO,
276 p_playlist->i_index + 1 );
277 vlc_object_release( p_playlist );
279 p_sys->i_size = 0; /* Force to stop reading */
282 /* Finish to set up p_input */
283 p_input->pf_read = Read;
284 p_input->pf_set_program = input_SetProgram;
285 p_input->pf_set_area = NULL;
286 p_input->pf_seek = Seek;
288 vlc_mutex_lock( &p_input->stream.stream_lock );
289 p_input->stream.b_pace_control = VLC_TRUE;
290 p_input->stream.p_selected_area->i_tell = 0;
291 p_input->stream.i_method = INPUT_METHOD_NETWORK;
292 vlc_mutex_unlock( &p_input->stream.stream_lock );
294 if( !strcmp( p_sys->psz_protocol, "ICY" ) &&
295 ( !p_input->psz_demux || !*p_input->psz_demux ) )
297 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) )
299 p_input->psz_demux = strdup( "nsv" );
303 p_input->psz_demux = strdup( "mp3" );
305 msg_Info( p_input, "ICY server found, %s demuxer selected",
306 p_input->psz_demux );
309 /* Update default_pts to a suitable value for http access */
310 var_Create( p_input, "http-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
311 var_Get( p_input, "http-caching", &val );
312 p_input->i_pts_delay = val.i_int * 1000;
317 vlc_UrlClean( &p_sys->url );
318 vlc_UrlClean( &p_sys->proxy );
319 if( p_sys->psz_mime ) free( p_sys->psz_mime );
320 if( p_sys->psz_location ) free( p_sys->psz_location );
321 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
322 if( p_sys->psz_user ) free( p_sys->psz_user );
323 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
327 net_Close( p_sys->fd );
333 /*****************************************************************************
335 *****************************************************************************/
336 static void Close( vlc_object_t *p_this )
338 input_thread_t *p_input = (input_thread_t*)p_this;
339 access_sys_t *p_sys = p_input->p_access_data;
341 vlc_UrlClean( &p_sys->url );
342 vlc_UrlClean( &p_sys->proxy );
344 if( p_sys->psz_user ) free( p_sys->psz_user );
345 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
347 if( p_sys->psz_mime ) free( p_sys->psz_mime );
348 if( p_sys->psz_location ) free( p_sys->psz_location );
350 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
354 net_Close( p_sys->fd );
359 /*****************************************************************************
360 * Seek: close and re-open a connection at the right place
361 *****************************************************************************/
362 static void Seek( input_thread_t * p_input, off_t i_pos )
364 access_sys_t *p_sys = p_input->p_access_data;
366 msg_Dbg( p_input, "trying to seek to "I64Fd, i_pos );
368 net_Close( p_sys->fd ); p_sys->fd = -1;
370 if( Connect( p_input, &p_input->stream.b_seekable,
371 &p_input->stream.p_selected_area->i_size, i_pos ) )
373 msg_Err( p_input, "seek failed" );
376 vlc_mutex_lock( &p_input->stream.stream_lock );
377 p_input->stream.p_selected_area->i_tell = i_pos;
378 vlc_mutex_unlock( &p_input->stream.stream_lock );
381 /*****************************************************************************
382 * Read: Read up to i_len bytes from the http connection and place in
383 * p_buffer. Return the actual number of bytes read
384 *****************************************************************************/
385 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
387 access_sys_t *p_sys = p_input->p_access_data;
394 if( p_sys->i_size > 0 && i_len + p_sys->i_tell > p_sys->i_size )
396 if( ( i_len = p_sys->i_size - p_sys->i_tell ) == 0 )
402 i_read = net_Read( p_input, p_sys->fd, p_buffer, i_len, VLC_FALSE );
405 p_sys->i_tell += i_read;
410 /*****************************************************************************
411 * ParseURL: extract user:password
412 *****************************************************************************/
413 static void ParseURL( access_sys_t *p_sys, char *psz_url )
415 char *psz_dup = strdup( psz_url );
419 /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
427 if( ( p = strchr( psz, '@' ) ) )
432 comma = strchr( psz, ':' );
434 /* Retreive user:password */
439 p_sys->psz_user = strdup( psz );
440 p_sys->psz_passwd = strdup( comma );
444 p_sys->psz_user = strdup( psz );
453 vlc_UrlParse( &p_sys->url, p, 0 );
458 /*****************************************************************************
460 *****************************************************************************/
461 static int Connect( input_thread_t *p_input, vlc_bool_t *pb_seekable,
462 off_t *pi_size, off_t i_tell )
464 access_sys_t *p_sys = p_input->p_access_data;
465 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
469 if( p_sys->psz_location ) free( p_sys->psz_location );
470 if( p_sys->psz_mime ) free( p_sys->psz_mime );
472 p_sys->psz_location = NULL;
473 p_sys->psz_mime = NULL;
475 p_sys->i_tell = i_tell;
478 /* Open connection */
479 p_sys->fd = net_OpenTCP( p_input, srv.psz_host, srv.i_port );
482 msg_Err( p_input, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
488 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
489 "GET http://%s:%d/%s HTTP/1.%d\r\n",
490 p_sys->url.psz_host, p_sys->url.i_port,
491 p_sys->url.psz_path, p_sys->i_version );
495 char *psz_path = p_sys->url.psz_path;
496 if( !psz_path || !*psz_path )
500 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
501 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
502 psz_path, p_sys->i_version, p_sys->url.psz_host );
505 net_Printf( VLC_OBJECT(p_input), p_sys->fd, "User-Agent: %s\r\n",
506 p_sys->psz_user_agent );
508 if( p_sys->i_version == 1 )
510 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
511 "Range: bytes="I64Fd"-\r\n", i_tell );
513 /* Authentification */
514 if( p_sys->psz_user && *p_sys->psz_user )
519 asprintf( &buf, "%s:%s", p_sys->psz_user,
520 p_sys->psz_passwd ? p_sys->psz_passwd : "" );
522 b64 = b64_encode( buf );
524 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
525 "Authorization: Basic %s", b64 );
528 net_Printf( VLC_OBJECT(p_input), p_sys->fd, "Connection: Close\r\n" );
530 if( net_Printf( VLC_OBJECT(p_input), p_sys->fd, "\r\n" ) < 0 )
532 msg_Err( p_input, "failed to send request" );
533 net_Close( p_sys->fd ); p_sys->fd = -1;
538 *pb_seekable = p_sys->i_version == 1 ? VLC_TRUE : VLC_FALSE;
542 if( ( psz = net_Gets( VLC_OBJECT(p_input), p_sys->fd ) ) == NULL )
544 msg_Err( p_input, "failed to read answer" );
547 if( !strncmp( psz, "HTTP/1.", 7 ) )
549 p_sys->psz_protocol = "HTTP";
550 p_sys->i_code = atoi( &psz[9] );
552 else if( !strncmp( psz, "ICY", 3 ) )
554 p_sys->psz_protocol = "ICY";
555 p_sys->i_code = atoi( &psz[4] );
559 msg_Err( p_input, "invalid HTTP reply '%s'", psz );
563 msg_Dbg( p_input, "protocol '%s' answer code %d",
564 p_sys->psz_protocol, p_sys->i_code );
565 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
567 *pb_seekable = VLC_FALSE;
569 if( p_sys->i_code != 206 )
571 *pb_seekable = VLC_FALSE;
573 if( p_sys->i_code >= 400 )
575 msg_Err( p_input, "error: %s", psz );
583 char *psz = net_Gets( VLC_OBJECT(p_input), p_sys->fd );
588 msg_Err( p_input, "failed to read answer" );
592 /* msg_Dbg( p_input, "Line=%s", psz ); */
600 if( ( p = strchr( psz, ':' ) ) == NULL )
602 msg_Err( p_input, "malformed header line: %s", psz );
608 if( !strcasecmp( psz, "Content-Length" ) )
610 *pi_size = p_sys->i_size = i_tell + atoll( p );
611 msg_Dbg( p_input, "stream size="I64Fd, p_sys->i_size );
613 else if( !strcasecmp( psz, "Location" ) )
615 if( p_sys->psz_location ) free( p_sys->psz_location );
616 p_sys->psz_location = strdup( p );
618 else if( !strcasecmp( psz, "Content-Type" ) )
620 if( p_sys->psz_mime ) free( p_sys->psz_mime );
621 p_sys->psz_mime = strdup( p );
629 net_Close( p_sys->fd ); p_sys->fd = -1;
633 /*****************************************************************************
635 *****************************************************************************/
636 static char *b64_encode( unsigned char *src )
638 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
640 char *dst = malloc( strlen( src ) * 4 / 3 + 12 );
643 unsigned i_shift = 0;
649 i_bits = ( i_bits << 8 )|( *src++ );
652 else if( i_shift > 0 )
654 i_bits <<= 6 - i_shift;
663 while( i_shift >= 6 )
666 *dst++ = b64[(i_bits >> i_shift)&0x3f];