1 /*****************************************************************************
2 * http.c: HTTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: http.c,v 1.45 2003/09/10 15:50:25 zorglub Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.h>
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
47 #if defined( UNDER_CE )
49 #elif defined( WIN32 )
50 # include <winsock2.h>
51 # include <ws2tcpip.h>
53 # define IN_MULTICAST(a) IN_CLASSD(a)
56 # include <sys/socket.h>
59 #include "vlc_playlist.h"
62 /*****************************************************************************
64 *****************************************************************************/
65 static int Open ( vlc_object_t * );
66 static void Close ( vlc_object_t * );
68 static void Seek ( input_thread_t *, off_t );
69 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
71 /*****************************************************************************
73 *****************************************************************************/
74 #define PROXY_TEXT N_("Specify an HTTP proxy")
75 #define PROXY_LONGTEXT N_( \
76 "Specify an HTTP proxy to use. It must be in the form " \
77 "http://myproxy.mydomain:myport. If none is specified, the HTTP_PROXY " \
78 "environment variable will be tried." )
80 #define CACHING_TEXT N_("Caching value in ms")
81 #define CACHING_LONGTEXT N_( \
82 "Allows you to modify the default caching value for http streams. This " \
83 "value should be set in miliseconds units." )
86 add_category_hint( N_("http"), NULL, VLC_FALSE );
87 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT, VLC_FALSE );
88 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
89 add_string( "http-user", NULL, NULL, "HTTP user name", "HTTP user name for Basic Authentification", VLC_FALSE );
90 add_string( "http-pwd", NULL , NULL, "HTTP password", "HTTP password for Basic Authentification", VLC_FALSE );
91 set_description( _("HTTP input") );
92 set_capability( "access", 0 );
93 add_shortcut( "http" );
94 add_shortcut( "http4" );
95 add_shortcut( "http6" );
96 set_callbacks( Open, Close );
99 /*****************************************************************************
100 * _input_socket_t: private access plug-in data, modified to add private
102 *****************************************************************************/
103 #define MAX_ANSWER_SIZE 1024
104 #define MAX_QUERY_SIZE 1024
106 typedef struct _input_socket_s
108 input_socket_t _socket;
111 network_socket_t socket_desc;
112 char psz_buffer[MAX_QUERY_SIZE];
113 char psz_auth_string[MAX_QUERY_SIZE];
117 /*****************************************************************************
118 * HTTPConnect: connect to the server and seek to i_tell
119 *****************************************************************************/
120 static int HTTPConnect( input_thread_t * p_input, off_t i_tell )
122 char psz_buffer[MAX_QUERY_SIZE];
123 _input_socket_t * p_access_data;
124 module_t * p_network;
125 char * psz_parser, * psz_value, * psz_answer;
127 int i_code, i_ret, i, i_size;
129 enum { HTTP_PROTOCOL, ICY_PROTOCOL } i_protocol;
131 /* Find an appropriate network module */
132 p_access_data = (_input_socket_t *)p_input->p_access_data;
133 p_input->p_private = (void*) &p_access_data->socket_desc;
134 p_network = module_Need( p_input, "network", p_access_data->psz_network );
135 if( p_network == NULL )
139 module_Unneed( p_input, p_network );
141 p_access_data->_socket.i_handle = p_access_data->socket_desc.i_handle;
143 # define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
144 # define HTTP_END "\r\n"
146 /* Build the query string */
147 if ( p_input->stream.b_seekable )
149 snprintf( psz_buffer, MAX_QUERY_SIZE,
151 "Range: bytes="I64Fd"-\r\n"
155 p_access_data->psz_buffer, i_tell, p_access_data->psz_auth_string );
159 snprintf( psz_buffer, MAX_QUERY_SIZE,
164 p_access_data->psz_buffer, p_access_data->psz_auth_string );
166 psz_buffer[MAX_QUERY_SIZE - 1] = '\0';
169 i_ret = send( p_access_data->_socket.i_handle,
170 psz_buffer, strlen( psz_buffer ), 0 );
174 msg_Err( p_input, "cannot send request (%s)", strerror(errno) );
176 msg_Err( p_input, "cannot send request" );
178 Close( VLC_OBJECT(p_input) );
182 /* Prepare the input thread for reading. */
183 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
185 /* FIXME: we shouldn't have to do that ! It's UGLY but mandatory because
186 * input_FillBuffer assumes p_input->pf_read exists */
187 p_input->pf_read = Read;
189 while( !input_FillBuffer( p_input ) )
191 if( p_input->b_die || p_input->b_error )
193 Close( VLC_OBJECT(p_input) );
198 /* Get the HTTP returncode */
199 i_size = input_Peek( p_input, &p_bytes, MAX_ANSWER_SIZE );
200 psz_parser = (char *)p_bytes;
204 msg_Err( p_input, "not enough data" );
205 Close( VLC_OBJECT(p_input) );
209 /* Guess the protocol */
210 if( ( ( (size_t)i_size >= strlen("HTTP/1.x") ) &&
211 !strncmp( psz_parser, "HTTP/1.", strlen("HTTP/1.") ) ) )
213 i_protocol = HTTP_PROTOCOL;
215 psz_parser += strlen("HTTP/1.x");
216 i_size -= strlen("HTTP/1.x");
219 else if( ( (size_t)i_size >= strlen("ICY") &&
220 !strncmp( psz_parser, "ICY", strlen("ICY") ) ) )
222 i_protocol = ICY_PROTOCOL;
223 if( !p_input->psz_demux || !*p_input->psz_demux )
225 msg_Info( p_input, "ICY server found, mp3 demuxer selected" );
226 p_input->psz_demux = "mp3"; // FIXME strdup ?
229 psz_parser += strlen("ICY");
230 i_size -= strlen("ICY");
234 msg_Err( p_input, "invalid HTTP reply '%s'", psz_parser );
238 /* Check for buggy Icecast servers */
239 if( strstr( psz_parser , "x-audiocast") )
241 i_protocol = ICY_PROTOCOL;
242 if( !p_input->psz_demux || !*p_input->psz_demux )
244 msg_Info( p_input, "ICY server found, mp3 demuxer selected" );
245 p_input->psz_demux = "mp3"; // FIXME strdup ?
249 /* Check the HTTP return code */
250 i_code = atoi( (char*)psz_parser );
251 msg_Dbg( p_input, "%s server replied: %i",
252 i_protocol == HTTP_PROTOCOL ? "HTTP" : "ICY", i_code );
256 /* Find the end of the line */
257 for ( i = 0; (i < i_size -1) && ((psz_parser[i] != '\r') ||
258 (psz_parser[i+1] != '\n')); i++ )
263 /* Check we actually parsed something */
264 if ( i+1 == i_size && psz_parser[i+1] != '\n' )
266 msg_Err( p_input, "stream not compliant with HTTP/1.x" );
270 /* Store the line we just parsed and skip it */
271 psz_answer = strndup( psz_parser, i );
277 p_input->p_current_data = psz_parser + i + 2;
279 /* Parse remaining headers */
282 char psz_line[MAX_ANSWER_SIZE];
284 i_size = input_Peek( p_input, &p_bytes, MAX_ANSWER_SIZE );
285 psz_parser = (char *)p_bytes;
289 msg_Err( p_input, "not enough data" );
290 Close( VLC_OBJECT(p_input) );
295 /* Copy one line to psz_line */
297 while( i_size && psz_parser[i] != '\r'
298 && psz_parser[i + 1] != '\n' )
300 psz_line[i] = psz_parser[i];
304 p_input->p_current_data = psz_parser + i + 2;
307 break; /* End of headers */
310 psz_parser = strchr( psz_line, ':' );
313 msg_Err( p_input, "malformed header line: %s", psz_line );
317 psz_parser[0] = '\0';
319 while ( *psz_parser == ' ' || *psz_parser == '\t' )
323 psz_value = psz_parser;
325 if( !strcasecmp( psz_line, "Content-Length" ) )
329 i_size = i_tell + atoll( psz_value );
333 if( *psz_value == '-' ) sign = -1;
334 while( *psz_value >= '0' && *psz_value <= '9' )
336 i_size = i_size * 10 + *psz_value++ - '0';
338 i_size = i_tell + ( i_size * sign );
340 msg_Dbg( p_input, "stream size is "I64Fd, i_size );
342 vlc_mutex_lock( &p_input->stream.stream_lock );
343 p_input->stream.p_selected_area->i_size = i_size;
344 vlc_mutex_unlock( &p_input->stream.stream_lock );
346 /* Redirection support */
347 else if( ( i_code == 301 || i_code == 302 ||
348 i_code == 303 || i_code == 307 )
349 && !strcasecmp( psz_line, "location" ) )
351 playlist_t * p_playlist = (playlist_t *) vlc_object_find(
352 p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
355 msg_Err( p_input, "redirection failed: can't find playlist" );
359 msg_Dbg( p_input, "%i %s: redirected to %s",
360 i_code, psz_answer, psz_value );
361 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion
363 playlist_Add( p_playlist, psz_value, NULL, 0,
364 PLAYLIST_INSERT | PLAYLIST_GO,
365 p_playlist->i_index + 1 );
366 vlc_object_release( p_playlist );
369 /* TODO: parse other headers here */
372 /* Something went wrong */
375 msg_Err( p_input, "%i %s", i_code, psz_answer );
376 p_input->p_current_data = psz_parser + i_size;
383 /* Set final stream properties */
384 vlc_mutex_lock( &p_input->stream.stream_lock );
385 if( i_protocol == ICY_PROTOCOL )
387 p_input->stream.b_seekable = VLC_FALSE;
391 p_input->stream.b_seekable = VLC_TRUE;
394 if( p_input->stream.p_selected_area->i_size )
396 p_input->stream.p_selected_area->i_tell = i_tell;
400 p_input->stream.b_seekable = VLC_FALSE;
404 p_input->stream.b_seekable = VLC_FALSE;
406 p_input->stream.b_changed = VLC_TRUE;
407 vlc_mutex_unlock( &p_input->stream.stream_lock );
412 /*****************************************************************************
413 * Encode a string in base64
414 * Code borrowed from Rafael Steil
415 *****************************************************************************/
416 void encodeblock( unsigned char in[3], unsigned char out[4], int len )
418 static const char cb64[]
419 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
420 out[0] = cb64[ in[0] >> 2 ];
421 out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
422 out[2] = (unsigned char) (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=');
423 out[3] = (unsigned char) (len > 2 ? cb64[ in[2] & 0x3f ] : '=');
426 char *str_base64_encode(char *psz_str, input_thread_t *p_input )
428 unsigned char in[3], out[4];
429 unsigned int i, len, blocksout = 0, linesize = strlen(psz_str);
430 char *psz_tmp = psz_str;
431 char *psz_result = (char *)malloc( linesize / 3 * 4 + 5 );
435 msg_Err( p_input, "out of memory" );
443 for( i = 0; i < 3; i++ )
445 in[i] = (unsigned char)*psz_tmp;
457 encodeblock( in, out, len );
459 for( i = 0; i < 4; i++ )
461 psz_result[blocksout++] = out[i];
466 psz_result[blocksout] = '\0';
470 /*****************************************************************************
471 * Open: parse URL and open the remote file at the beginning
472 *****************************************************************************/
473 static int Open( vlc_object_t *p_this )
475 input_thread_t * p_input = (input_thread_t *)p_this;
476 _input_socket_t * p_access_data;
477 char * psz_name = strdup(p_input->psz_name);
478 char * psz_parser = psz_name, * psz_auth_parser;
479 char * psz_server_addr = "";
480 char * psz_server_port = "";
481 char * psz_path = "";
482 char * psz_proxy, *psz_proxy_orig;
483 char * psz_user = NULL, *psz_pwd = NULL;
484 int i_server_port = 0;
487 p_access_data = malloc( sizeof(_input_socket_t) );
488 p_input->p_access_data = (access_sys_t *)p_access_data;
489 if( p_access_data == NULL )
491 msg_Err( p_input, "out of memory" );
496 p_access_data->psz_name = psz_name;
497 p_access_data->psz_network = "";
498 memset(p_access_data->psz_auth_string, 0, MAX_QUERY_SIZE);
500 var_Create( p_input, "ipv4", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
501 var_Get( p_input, "ipv4", &val );
504 p_access_data->psz_network = "ipv4";
506 var_Create( p_input, "ipv6", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
507 var_Get( p_input, "ipv6", &val );
510 p_access_data->psz_network = "ipv6";
512 if( *p_input->psz_access )
514 /* Find out which shortcut was used */
515 if( !strncmp( p_input->psz_access, "http6", 6 ) )
517 p_access_data->psz_network = "ipv6";
519 else if( !strncmp( p_input->psz_access, "http4", 6 ) )
521 p_access_data->psz_network = "ipv4";
525 /* Parse psz_name syntax :
526 * //[user:password]@<hostname>[:<port>][/<path>] */
529 while( *psz_parser == '/' )
533 psz_auth_parser = psz_parser;
535 while ( *psz_auth_parser != '@' && *psz_auth_parser != '\0' )
539 if ( *psz_auth_parser == '@' )
541 psz_user = psz_parser;
542 while ( *psz_parser != ':' && psz_parser < psz_auth_parser )
546 if ( psz_parser != psz_auth_parser )
549 psz_pwd = psz_parser + 1;
555 *psz_auth_parser = '\0';
556 psz_parser = psz_auth_parser + 1;
559 psz_server_addr = psz_parser;
561 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
563 if( *psz_parser == '[' )
566 while( *psz_parser && *psz_parser != ']' )
574 if ( *psz_parser == ':' )
578 psz_server_port = psz_parser;
580 while( *psz_parser && *psz_parser != '/' )
586 if( *psz_parser == '/' )
590 psz_path = psz_parser;
593 /* Convert port format */
594 if( *psz_server_port )
596 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
599 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
600 free( p_input->p_access_data );
606 if( i_server_port == 0 )
611 if( !*psz_server_addr )
613 msg_Err( p_input, "no server given" );
614 free( p_input->p_access_data );
619 /* Handle autehtification */
623 var_Create( p_input, "http-user", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
624 var_Get( p_input, "http-user", &val );
625 psz_user = val.psz_string;
627 var_Create( p_input, "http-pwd", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
628 var_Get( p_input, "http-pwd", &val );
629 psz_pwd = val.psz_string;
634 char psz_user_pwd[MAX_QUERY_SIZE];
635 msg_Dbg( p_input, "authenticating, user=%s, password=%s",
637 snprintf( psz_user_pwd, MAX_QUERY_SIZE, "%s:%s", psz_user, psz_pwd );
638 snprintf( p_access_data->psz_auth_string, MAX_QUERY_SIZE,
639 "Authorization: Basic %s\r\n",
640 str_base64_encode( psz_user_pwd, p_input ) );
643 /* Check proxy config variable */
644 var_Create( p_input, "http-proxy", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
645 var_Get( p_input, "http-proxy", &val );
646 psz_proxy_orig = val.psz_string;
647 if( psz_proxy_orig == NULL )
649 /* Check proxy environment variable */
650 psz_proxy_orig = getenv( "http_proxy" );
651 if( psz_proxy_orig != NULL )
653 psz_proxy_orig = strdup( psz_proxy_orig );
657 psz_proxy = psz_proxy_orig;
658 if( psz_proxy != NULL && *psz_proxy )
660 /* http://myproxy.mydomain:myport/ */
661 int i_proxy_port = 0;
663 /* Skip the protocol name */
664 while( *psz_proxy && *psz_proxy != ':' )
669 /* Skip the "://" part */
670 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
675 /* Found a proxy name */
678 char *psz_port = psz_proxy;
680 /* Skip the hostname part */
681 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
686 /* Found a port name */
691 /* Replace ':' with '\0' */
696 while( *psz_junk && *psz_junk != '/' )
706 if( *psz_port != '\0' )
708 i_proxy_port = atoi( psz_port );
712 psz_proxy = strdup( psz_proxy );
714 msg_Dbg( p_input, "using HTTP proxy server=%s port=%d",
715 psz_proxy, i_proxy_port );
719 msg_Err( p_input, "HTTP proxy %s is invalid!", psz_proxy_orig );
720 free( p_input->p_access_data );
722 if( psz_proxy_orig ) free( psz_proxy_orig );
726 if( psz_proxy_orig ) free( psz_proxy_orig );
728 p_access_data->socket_desc.psz_server_addr = psz_proxy;
729 p_access_data->socket_desc.i_server_port = i_proxy_port;
730 p_access_data->socket_desc.i_type = NETWORK_TCP;
731 p_access_data->socket_desc.i_ttl = 0;
733 snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
734 "GET http://%s:%d/%s HTTP/1.0\r\n",
735 psz_server_addr, i_server_port, psz_path );
739 /* No proxy, direct connection. */
740 p_access_data->socket_desc.i_type = NETWORK_TCP;
741 p_access_data->socket_desc.psz_server_addr = psz_server_addr;
742 p_access_data->socket_desc.i_server_port = i_server_port;
743 p_access_data->socket_desc.i_ttl = 0;
745 snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
746 "GET /%s HTTP/1.1\r\nHost: %s\r\n",
747 psz_path, psz_server_addr );
749 p_access_data->psz_buffer[MAX_QUERY_SIZE - 1] = '\0';
751 msg_Dbg( p_input, "opening server=%s port=%d path=%s",
752 psz_server_addr, i_server_port, psz_path );
754 p_input->pf_read = Read;
755 p_input->pf_set_program = input_SetProgram;
756 p_input->pf_set_area = NULL;
757 p_input->pf_seek = Seek;
759 vlc_mutex_lock( &p_input->stream.stream_lock );
760 p_input->stream.b_pace_control = VLC_TRUE;
761 p_input->stream.b_seekable = VLC_TRUE;
762 p_input->stream.p_selected_area->i_tell = 0;
763 p_input->stream.p_selected_area->i_size = 0;
764 p_input->stream.i_method = INPUT_METHOD_NETWORK;
765 vlc_mutex_unlock( &p_input->stream.stream_lock );
768 if( HTTPConnect( p_input, 0 ) )
770 /* Request failed, try again with HTTP/1.0 */
771 char * psz_pos = strstr( p_access_data->psz_buffer, "HTTP/1.1" );
778 p_input->stream.b_seekable = VLC_FALSE;
780 if( HTTPConnect( p_input, 0 ) )
782 free( p_input->p_access_data );
788 /* Update default_pts to a suitable value for http access */
790 var_Create( p_input, "http-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
791 var_Get( p_input, "http-caching", &val );
792 p_input->i_pts_delay = val.i_int * 1000;
797 /*****************************************************************************
798 * Close: free unused data structures
799 *****************************************************************************/
800 static void Close( vlc_object_t *p_this )
802 input_thread_t * p_input = (input_thread_t *)p_this;
803 int i_handle = ((network_socket_t *)p_input->p_access_data)->i_handle;
804 _input_socket_t * p_access_data =
805 (_input_socket_t *)p_input->p_access_data;
807 free( p_access_data->psz_name );
809 msg_Info( p_input, "closing HTTP target `%s'", p_input->psz_source );
811 #if defined( WIN32 ) || defined( UNDER_CE )
812 closesocket( i_handle );
817 free( p_access_data );
820 /*****************************************************************************
821 * Seek: close and re-open a connection at the right place
822 *****************************************************************************/
823 static void Seek( input_thread_t * p_input, off_t i_pos )
825 _input_socket_t *p_access_data = (_input_socket_t*)p_input->p_access_data;
826 #if defined( WIN32 ) || defined( UNDER_CE )
827 closesocket( p_access_data->_socket.i_handle );
829 close( p_access_data->_socket.i_handle );
831 msg_Dbg( p_input, "seeking to position "I64Fd, i_pos );
832 HTTPConnect( p_input, i_pos );
835 /*****************************************************************************
836 * Read: Read up to i_len bytes from the http connection and place in
837 * p_buffer. Return the actual number of bytes read
838 *****************************************************************************/
839 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
841 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
842 struct timeval timeout;
847 /* Initialize file descriptor set */
849 FD_SET( p_access_data->i_handle, &fds );
851 /* We'll wait 0.5 second if nothing happens */
853 timeout.tv_usec = 500000;
855 /* Find if some data is available */
856 while( (i_ret = select( p_access_data->i_handle + 1, &fds,
857 NULL, NULL, &timeout )) == 0
859 || (i_ret < 0 && errno == EINTR)
864 FD_SET( p_access_data->i_handle, &fds );
866 timeout.tv_usec = 500000;
868 if( p_input->b_die || p_input->b_error )
876 msg_Err( p_input, "network select error" );
880 i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
885 msg_Err( p_input, "recv failed (%s)", strerror(errno) );
887 msg_Err( p_input, "recv failed" );