1 /*****************************************************************************
2 * http.c: HTTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: http.c,v 1.46 2003/09/10 21:03:56 fenrir 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 *****************************************************************************/
29 #include <vlc/input.h>
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
46 #if defined( UNDER_CE )
48 #elif defined( WIN32 )
49 # include <winsock2.h>
50 # include <ws2tcpip.h>
52 # define IN_MULTICAST(a) IN_CLASSD(a)
55 # include <sys/socket.h>
58 #include "vlc_playlist.h"
61 /*****************************************************************************
63 *****************************************************************************/
64 static int Open ( vlc_object_t * );
65 static void Close ( vlc_object_t * );
67 static void Seek ( input_thread_t *, off_t );
68 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
70 /*****************************************************************************
72 *****************************************************************************/
73 #define PROXY_TEXT N_("Specify an HTTP proxy")
74 #define PROXY_LONGTEXT N_( \
75 "Specify an HTTP proxy to use. It must be in the form " \
76 "http://myproxy.mydomain:myport. If none is specified, the HTTP_PROXY " \
77 "environment variable will be tried." )
79 #define CACHING_TEXT N_("Caching value in ms")
80 #define CACHING_LONGTEXT N_( \
81 "Allows you to modify the default caching value for http streams. This " \
82 "value should be set in miliseconds units." )
85 add_category_hint( N_("http"), NULL, VLC_FALSE );
86 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT, VLC_FALSE );
87 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
88 add_string( "http-user", NULL, NULL, "HTTP user name", "HTTP user name for Basic Authentification", VLC_FALSE );
89 add_string( "http-pwd", NULL , NULL, "HTTP password", "HTTP password for Basic Authentification", VLC_FALSE );
90 set_description( _("HTTP input") );
91 set_capability( "access", 0 );
92 add_shortcut( "http" );
93 add_shortcut( "http4" );
94 add_shortcut( "http6" );
95 set_callbacks( Open, Close );
98 /*****************************************************************************
99 * _input_socket_t: private access plug-in data, modified to add private
101 *****************************************************************************/
102 #define MAX_ANSWER_SIZE 1024
103 #define MAX_QUERY_SIZE 1024
105 typedef struct _input_socket_s
107 input_socket_t _socket;
110 network_socket_t socket_desc;
111 char psz_buffer[MAX_QUERY_SIZE];
112 char psz_auth_string[MAX_QUERY_SIZE];
116 /*****************************************************************************
117 * HTTPConnect: connect to the server and seek to i_tell
118 *****************************************************************************/
119 static int HTTPConnect( input_thread_t * p_input, off_t i_tell )
121 char psz_buffer[MAX_QUERY_SIZE];
122 _input_socket_t * p_access_data;
123 module_t * p_network;
124 char * psz_parser, * psz_value, * psz_answer;
126 int i_code, i_ret, i, i_size;
128 enum { HTTP_PROTOCOL, ICY_PROTOCOL } i_protocol;
130 /* Find an appropriate network module */
131 p_access_data = (_input_socket_t *)p_input->p_access_data;
132 p_input->p_private = (void*) &p_access_data->socket_desc;
133 p_network = module_Need( p_input, "network", p_access_data->psz_network );
134 if( p_network == NULL )
138 module_Unneed( p_input, p_network );
140 p_access_data->_socket.i_handle = p_access_data->socket_desc.i_handle;
142 # define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
143 # define HTTP_END "\r\n"
145 /* Build the query string */
146 if ( p_input->stream.b_seekable )
148 snprintf( psz_buffer, MAX_QUERY_SIZE,
150 "Range: bytes="I64Fd"-\r\n"
153 "Connection: Close\r\n"
155 p_access_data->psz_buffer, i_tell, p_access_data->psz_auth_string );
159 snprintf( psz_buffer, MAX_QUERY_SIZE,
163 "Connection: Close\r\n"
165 p_access_data->psz_buffer, p_access_data->psz_auth_string );
167 psz_buffer[MAX_QUERY_SIZE - 1] = '\0';
170 i_ret = send( p_access_data->_socket.i_handle,
171 psz_buffer, strlen( psz_buffer ), 0 );
175 msg_Err( p_input, "cannot send request (%s)", strerror(errno) );
177 msg_Err( p_input, "cannot send request" );
179 Close( VLC_OBJECT(p_input) );
183 /* Prepare the input thread for reading. */
184 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
186 /* FIXME: we shouldn't have to do that ! It's UGLY but mandatory because
187 * input_FillBuffer assumes p_input->pf_read exists */
188 p_input->pf_read = Read;
190 while( !input_FillBuffer( p_input ) )
192 if( p_input->b_die || p_input->b_error )
194 Close( VLC_OBJECT(p_input) );
199 /* Get the HTTP returncode */
200 i_size = input_Peek( p_input, &p_bytes, MAX_ANSWER_SIZE );
201 psz_parser = (char *)p_bytes;
205 msg_Err( p_input, "not enough data" );
206 Close( VLC_OBJECT(p_input) );
210 /* Guess the protocol */
211 if( ( ( (size_t)i_size >= strlen("HTTP/1.x") ) &&
212 !strncmp( psz_parser, "HTTP/1.", strlen("HTTP/1.") ) ) )
214 i_protocol = HTTP_PROTOCOL;
216 psz_parser += strlen("HTTP/1.x");
217 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 );
330 msg_Dbg( p_input, "stream size is "I64Fd, i_size );
332 vlc_mutex_lock( &p_input->stream.stream_lock );
333 p_input->stream.p_selected_area->i_size = i_size;
334 vlc_mutex_unlock( &p_input->stream.stream_lock );
336 /* Redirection support */
337 else if( ( i_code == 301 || i_code == 302 ||
338 i_code == 303 || i_code == 307 )
339 && !strcasecmp( psz_line, "location" ) )
341 playlist_t * p_playlist = (playlist_t *) vlc_object_find(
342 p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
345 msg_Err( p_input, "redirection failed: can't find playlist" );
349 msg_Dbg( p_input, "%i %s: redirected to %s",
350 i_code, psz_answer, psz_value );
351 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion
353 playlist_Add( p_playlist, psz_value, NULL, 0,
354 PLAYLIST_INSERT | PLAYLIST_GO,
355 p_playlist->i_index + 1 );
356 vlc_object_release( p_playlist );
359 /* TODO: parse other headers here */
362 /* Something went wrong */
365 msg_Err( p_input, "%i %s", i_code, psz_answer );
366 p_input->p_current_data = psz_parser + i_size;
373 /* Set final stream properties */
374 vlc_mutex_lock( &p_input->stream.stream_lock );
375 if( i_protocol == ICY_PROTOCOL )
377 p_input->stream.b_seekable = VLC_FALSE;
381 p_input->stream.b_seekable = VLC_TRUE;
384 if( p_input->stream.p_selected_area->i_size )
386 p_input->stream.p_selected_area->i_tell = i_tell;
390 p_input->stream.b_seekable = VLC_FALSE;
394 p_input->stream.b_seekable = VLC_FALSE;
396 p_input->stream.b_changed = VLC_TRUE;
397 vlc_mutex_unlock( &p_input->stream.stream_lock );
402 /*****************************************************************************
403 * Encode a string in base64
404 * Code borrowed from Rafael Steil
405 *****************************************************************************/
406 void encodeblock( unsigned char in[3], unsigned char out[4], int len )
408 static const char cb64[]
409 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
410 out[0] = cb64[ in[0] >> 2 ];
411 out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
412 out[2] = (unsigned char) (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=');
413 out[3] = (unsigned char) (len > 2 ? cb64[ in[2] & 0x3f ] : '=');
416 char *str_base64_encode(char *psz_str, input_thread_t *p_input )
418 unsigned char in[3], out[4];
419 unsigned int i, len, blocksout = 0, linesize = strlen(psz_str);
420 char *psz_tmp = psz_str;
421 char *psz_result = (char *)malloc( linesize / 3 * 4 + 5 );
425 msg_Err( p_input, "out of memory" );
433 for( i = 0; i < 3; i++ )
435 in[i] = (unsigned char)*psz_tmp;
447 encodeblock( in, out, len );
449 for( i = 0; i < 4; i++ )
451 psz_result[blocksout++] = out[i];
456 psz_result[blocksout] = '\0';
460 /*****************************************************************************
461 * Open: parse URL and open the remote file at the beginning
462 *****************************************************************************/
463 static int Open( vlc_object_t *p_this )
465 input_thread_t * p_input = (input_thread_t *)p_this;
466 _input_socket_t * p_access_data;
467 char * psz_name = strdup(p_input->psz_name);
468 char * psz_parser = psz_name, * psz_auth_parser;
469 char * psz_server_addr = "";
470 char * psz_server_port = "";
471 char * psz_path = "";
472 char * psz_proxy, *psz_proxy_orig;
473 char * psz_user = NULL, *psz_pwd = NULL;
474 int i_server_port = 0;
477 p_access_data = malloc( sizeof(_input_socket_t) );
478 p_input->p_access_data = (access_sys_t *)p_access_data;
479 if( p_access_data == NULL )
481 msg_Err( p_input, "out of memory" );
486 p_access_data->psz_name = psz_name;
487 p_access_data->psz_network = "";
488 memset(p_access_data->psz_auth_string, 0, MAX_QUERY_SIZE);
490 var_Create( p_input, "ipv4", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
491 var_Get( p_input, "ipv4", &val );
494 p_access_data->psz_network = "ipv4";
496 var_Create( p_input, "ipv6", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
497 var_Get( p_input, "ipv6", &val );
500 p_access_data->psz_network = "ipv6";
502 if( *p_input->psz_access )
504 /* Find out which shortcut was used */
505 if( !strncmp( p_input->psz_access, "http6", 6 ) )
507 p_access_data->psz_network = "ipv6";
509 else if( !strncmp( p_input->psz_access, "http4", 6 ) )
511 p_access_data->psz_network = "ipv4";
515 /* Parse psz_name syntax :
516 * //[user:password]@<hostname>[:<port>][/<path>] */
519 while( *psz_parser == '/' )
523 psz_auth_parser = psz_parser;
525 while ( *psz_auth_parser != '@' && *psz_auth_parser != '\0' )
529 if ( *psz_auth_parser == '@' )
531 psz_user = psz_parser;
532 while ( *psz_parser != ':' && psz_parser < psz_auth_parser )
536 if ( psz_parser != psz_auth_parser )
539 psz_pwd = psz_parser + 1;
545 *psz_auth_parser = '\0';
546 psz_parser = psz_auth_parser + 1;
549 psz_server_addr = psz_parser;
551 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
553 if( *psz_parser == '[' )
556 while( *psz_parser && *psz_parser != ']' )
564 if ( *psz_parser == ':' )
568 psz_server_port = psz_parser;
570 while( *psz_parser && *psz_parser != '/' )
576 if( *psz_parser == '/' )
580 psz_path = psz_parser;
583 /* Convert port format */
584 if( *psz_server_port )
586 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
589 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
590 free( p_input->p_access_data );
596 if( i_server_port == 0 )
601 if( !*psz_server_addr )
603 msg_Err( p_input, "no server given" );
604 free( p_input->p_access_data );
609 /* Handle autehtification */
613 var_Create( p_input, "http-user", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
614 var_Get( p_input, "http-user", &val );
615 psz_user = val.psz_string;
617 var_Create( p_input, "http-pwd", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
618 var_Get( p_input, "http-pwd", &val );
619 psz_pwd = val.psz_string;
624 char psz_user_pwd[MAX_QUERY_SIZE];
625 msg_Dbg( p_input, "authenticating, user=%s, password=%s",
627 snprintf( psz_user_pwd, MAX_QUERY_SIZE, "%s:%s", psz_user, psz_pwd );
628 snprintf( p_access_data->psz_auth_string, MAX_QUERY_SIZE,
629 "Authorization: Basic %s\r\n",
630 str_base64_encode( psz_user_pwd, p_input ) );
633 /* Check proxy config variable */
634 var_Create( p_input, "http-proxy", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
635 var_Get( p_input, "http-proxy", &val );
636 psz_proxy_orig = val.psz_string;
637 if( psz_proxy_orig == NULL )
639 /* Check proxy environment variable */
640 psz_proxy_orig = getenv( "http_proxy" );
641 if( psz_proxy_orig != NULL )
643 psz_proxy_orig = strdup( psz_proxy_orig );
647 psz_proxy = psz_proxy_orig;
648 if( psz_proxy != NULL && *psz_proxy )
650 /* http://myproxy.mydomain:myport/ */
651 int i_proxy_port = 0;
653 /* Skip the protocol name */
654 while( *psz_proxy && *psz_proxy != ':' )
659 /* Skip the "://" part */
660 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
665 /* Found a proxy name */
668 char *psz_port = psz_proxy;
670 /* Skip the hostname part */
671 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
676 /* Found a port name */
681 /* Replace ':' with '\0' */
686 while( *psz_junk && *psz_junk != '/' )
696 if( *psz_port != '\0' )
698 i_proxy_port = atoi( psz_port );
702 psz_proxy = strdup( psz_proxy );
704 msg_Dbg( p_input, "using HTTP proxy server=%s port=%d",
705 psz_proxy, i_proxy_port );
709 msg_Err( p_input, "HTTP proxy %s is invalid!", psz_proxy_orig );
710 free( p_input->p_access_data );
712 if( psz_proxy_orig ) free( psz_proxy_orig );
716 if( psz_proxy_orig ) free( psz_proxy_orig );
718 p_access_data->socket_desc.psz_server_addr = psz_proxy;
719 p_access_data->socket_desc.i_server_port = i_proxy_port;
720 p_access_data->socket_desc.i_type = NETWORK_TCP;
721 p_access_data->socket_desc.i_ttl = 0;
723 snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
724 "GET http://%s:%d/%s HTTP/1.0\r\n",
725 psz_server_addr, i_server_port, psz_path );
729 /* No proxy, direct connection. */
730 p_access_data->socket_desc.i_type = NETWORK_TCP;
731 p_access_data->socket_desc.psz_server_addr = psz_server_addr;
732 p_access_data->socket_desc.i_server_port = i_server_port;
733 p_access_data->socket_desc.i_ttl = 0;
735 snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
736 "GET /%s HTTP/1.1\r\nHost: %s\r\n",
737 psz_path, psz_server_addr );
739 p_access_data->psz_buffer[MAX_QUERY_SIZE - 1] = '\0';
741 msg_Dbg( p_input, "opening server=%s port=%d path=%s",
742 psz_server_addr, i_server_port, psz_path );
744 p_input->pf_read = Read;
745 p_input->pf_set_program = input_SetProgram;
746 p_input->pf_set_area = NULL;
747 p_input->pf_seek = Seek;
749 vlc_mutex_lock( &p_input->stream.stream_lock );
750 p_input->stream.b_pace_control = VLC_TRUE;
751 p_input->stream.b_seekable = VLC_TRUE;
752 p_input->stream.p_selected_area->i_tell = 0;
753 p_input->stream.p_selected_area->i_size = 0;
754 p_input->stream.i_method = INPUT_METHOD_NETWORK;
755 vlc_mutex_unlock( &p_input->stream.stream_lock );
758 if( HTTPConnect( p_input, 0 ) )
760 /* Request failed, try again with HTTP/1.0 */
761 char * psz_pos = strstr( p_access_data->psz_buffer, "HTTP/1.1" );
768 p_input->stream.b_seekable = VLC_FALSE;
770 if( HTTPConnect( p_input, 0 ) )
772 free( p_input->p_access_data );
778 /* Update default_pts to a suitable value for http access */
780 var_Create( p_input, "http-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
781 var_Get( p_input, "http-caching", &val );
782 p_input->i_pts_delay = val.i_int * 1000;
787 /*****************************************************************************
788 * Close: free unused data structures
789 *****************************************************************************/
790 static void Close( vlc_object_t *p_this )
792 input_thread_t * p_input = (input_thread_t *)p_this;
793 int i_handle = ((network_socket_t *)p_input->p_access_data)->i_handle;
794 _input_socket_t * p_access_data =
795 (_input_socket_t *)p_input->p_access_data;
797 free( p_access_data->psz_name );
799 msg_Info( p_input, "closing HTTP target `%s'", p_input->psz_source );
801 #if defined( WIN32 ) || defined( UNDER_CE )
802 closesocket( i_handle );
807 free( p_access_data );
810 /*****************************************************************************
811 * Seek: close and re-open a connection at the right place
812 *****************************************************************************/
813 static void Seek( input_thread_t * p_input, off_t i_pos )
815 _input_socket_t *p_access_data = (_input_socket_t*)p_input->p_access_data;
816 #if defined( WIN32 ) || defined( UNDER_CE )
817 closesocket( p_access_data->_socket.i_handle );
819 close( p_access_data->_socket.i_handle );
821 msg_Dbg( p_input, "seeking to position "I64Fd, i_pos );
822 HTTPConnect( p_input, i_pos );
825 /*****************************************************************************
826 * Read: Read up to i_len bytes from the http connection and place in
827 * p_buffer. Return the actual number of bytes read
828 *****************************************************************************/
829 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
831 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
832 struct timeval timeout;
837 /* Initialize file descriptor set */
839 FD_SET( p_access_data->i_handle, &fds );
841 /* We'll wait 0.5 second if nothing happens */
843 timeout.tv_usec = 500000;
845 /* Find if some data is available */
846 while( (i_ret = select( p_access_data->i_handle + 1, &fds,
847 NULL, NULL, &timeout )) == 0
849 || (i_ret < 0 && errno == EINTR)
854 FD_SET( p_access_data->i_handle, &fds );
856 timeout.tv_usec = 500000;
858 if( p_input->b_die || p_input->b_error )
866 msg_Err( p_input, "network select error" );
870 i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
875 msg_Err( p_input, "recv failed (%s)", strerror(errno) );
877 msg_Err( p_input, "recv failed" );