1 /*****************************************************************************
2 * http.c: HTTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: http.c,v 1.42 2003/08/01 09:45:34 sam 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");
218 else if( ( (size_t)i_size >= strlen("ICY") &&
219 !strncmp( psz_parser, "ICY", strlen("ICY") ) ) )
221 i_protocol = ICY_PROTOCOL;
222 if( !p_input->psz_demux || !*p_input->psz_demux )
224 msg_Info( p_input, "ICY server found, mp3 demuxer selected" );
225 p_input->psz_demux = "mp3"; // FIXME strdup ?
228 psz_parser += strlen("ICY");
229 i_size -= strlen("ICY");
233 msg_Err( p_input, "invalid HTTP reply '%s'", psz_parser );
237 /* Check the HTTP return code */
238 i_code = atoi( (char*)psz_parser );
239 msg_Dbg( p_input, "%s server replied: %i",
240 i_protocol == HTTP_PROTOCOL ? "HTTP" : "ICY", i_code );
244 /* Find the end of the line */
245 for ( i = 0; (i < i_size -1) && ((psz_parser[i] != '\r') ||
246 (psz_parser[i+1] != '\n')); i++ )
251 /* Check we actually parsed something */
252 if ( i+1 == i_size && psz_parser[i+1] != '\n' )
254 msg_Err( p_input, "stream not compliant with HTTP/1.x" );
258 /* Store the line we just parsed and skip it */
259 psz_answer = strndup( psz_parser, i );
265 p_input->p_current_data = psz_parser + i + 2;
267 /* Parse remaining headers */
270 char psz_line[MAX_ANSWER_SIZE];
272 i_size = input_Peek( p_input, &p_bytes, MAX_ANSWER_SIZE );
273 psz_parser = (char *)p_bytes;
277 msg_Err( p_input, "not enough data" );
278 Close( VLC_OBJECT(p_input) );
283 /* Copy one line to psz_line */
285 while( i_size && psz_parser[i] != '\r'
286 && psz_parser[i + 1] != '\n' )
288 psz_line[i] = psz_parser[i];
292 p_input->p_current_data = psz_parser + i + 2;
295 break; /* End of headers */
298 psz_parser = strchr( psz_line, ':' );
301 msg_Err( p_input, "malformed header line: %s", psz_line );
305 psz_parser[0] = '\0';
307 while ( *psz_parser == ' ' || *psz_parser == '\t' )
311 psz_value = psz_parser;
313 if( !strcasecmp( psz_line, "Content-Length" ) )
317 i_size = i_tell + atoll( psz_value );
321 if( *psz_value == '-' ) sign = -1;
322 while( *psz_value >= '0' && *psz_value <= '9' )
324 i_size = i_size * 10 + *psz_value++ - '0';
326 i_size = i_tell + ( i_size * sign );
328 msg_Dbg( p_input, "stream size is "I64Fd, i_size );
330 vlc_mutex_lock( &p_input->stream.stream_lock );
331 p_input->stream.p_selected_area->i_size = i_size;
332 vlc_mutex_unlock( &p_input->stream.stream_lock );
334 /* Redirection support */
335 else if( ( i_code == 301 || i_code == 302 ||
336 i_code == 303 || i_code == 307 )
337 && !strcasecmp( psz_line, "location" ) )
339 playlist_t * p_playlist = (playlist_t *) vlc_object_find(
340 p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
343 msg_Err( p_input, "redirection failed: can't find playlist" );
347 msg_Dbg( p_input, "%i %s: redirected to %s",
348 i_code, psz_answer, psz_value );
349 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion
351 playlist_Add( p_playlist, psz_value, NULL, 0,
352 PLAYLIST_INSERT | PLAYLIST_GO,
353 p_playlist->i_index + 1 );
354 vlc_object_release( p_playlist );
357 /* TODO: parse other headers here */
360 /* Something went wrong */
363 msg_Err( p_input, "%i %s", i_code, psz_answer );
364 p_input->p_current_data = psz_parser + i_size;
371 /* Set final stream properties */
372 vlc_mutex_lock( &p_input->stream.stream_lock );
373 if( i_protocol == ICY_PROTOCOL )
375 p_input->stream.b_seekable = VLC_FALSE;
379 p_input->stream.b_seekable = VLC_TRUE;
382 if( p_input->stream.p_selected_area->i_size )
384 p_input->stream.p_selected_area->i_tell = i_tell;
388 p_input->stream.b_seekable = VLC_FALSE;
392 p_input->stream.b_seekable = VLC_FALSE;
394 p_input->stream.b_changed = VLC_TRUE;
395 vlc_mutex_unlock( &p_input->stream.stream_lock );
400 /*****************************************************************************
401 * Encode a string in base64
402 * Code borrowed from Rafael Steil
403 *****************************************************************************/
404 void encodeblock( unsigned char in[3], unsigned char out[4], int len )
406 static const char cb64[]
407 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
408 out[0] = cb64[ in[0] >> 2 ];
409 out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
410 out[2] = (unsigned char) (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=');
411 out[3] = (unsigned char) (len > 2 ? cb64[ in[2] & 0x3f ] : '=');
414 char *str_base64_encode(char *psz_str, input_thread_t *p_input )
416 unsigned char in[3], out[4];
417 unsigned int i, len, blocksout = 0, linesize = strlen(psz_str);
418 char *psz_tmp = psz_str;
419 char *psz_result = (char *)malloc( linesize / 3 * 4 + 5 );
423 msg_Err( p_input, "out of memory" );
431 for( i = 0; i < 3; i++ )
433 in[i] = (unsigned char)*psz_tmp;
445 encodeblock( in, out, len );
447 for( i = 0; i < 4; i++ )
449 psz_result[blocksout++] = out[i];
454 psz_result[blocksout] = '\0';
458 /*****************************************************************************
459 * Open: parse URL and open the remote file at the beginning
460 *****************************************************************************/
461 static int Open( vlc_object_t *p_this )
463 input_thread_t * p_input = (input_thread_t *)p_this;
464 _input_socket_t * p_access_data;
465 char * psz_name = strdup(p_input->psz_name);
466 char * psz_parser = psz_name, * psz_auth_parser;
467 char * psz_server_addr = "";
468 char * psz_server_port = "";
469 char * psz_path = "";
470 char * psz_proxy, *psz_proxy_orig;
471 char * psz_user = NULL, *psz_pwd = NULL;
472 int i_server_port = 0;
475 p_access_data = malloc( sizeof(_input_socket_t) );
476 p_input->p_access_data = (access_sys_t *)p_access_data;
477 if( p_access_data == NULL )
479 msg_Err( p_input, "out of memory" );
484 p_access_data->psz_name = psz_name;
485 p_access_data->psz_network = "";
486 memset(p_access_data->psz_auth_string, 0, MAX_QUERY_SIZE);
488 var_Create( p_input, "ipv4", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
489 var_Get( p_input, "ipv4", &val );
492 p_access_data->psz_network = "ipv4";
494 var_Create( p_input, "ipv6", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
495 var_Get( p_input, "ipv6", &val );
498 p_access_data->psz_network = "ipv6";
500 if( *p_input->psz_access )
502 /* Find out which shortcut was used */
503 if( !strncmp( p_input->psz_access, "http6", 6 ) )
505 p_access_data->psz_network = "ipv6";
507 else if( !strncmp( p_input->psz_access, "http4", 6 ) )
509 p_access_data->psz_network = "ipv4";
513 /* Parse psz_name syntax :
514 * //[user:password]@<hostname>[:<port>][/<path>] */
517 while( *psz_parser == '/' )
521 psz_auth_parser = psz_parser;
523 while ( *psz_auth_parser != '@' && *psz_auth_parser != '\0' )
527 if ( *psz_auth_parser == '@' )
529 psz_user = psz_parser;
530 while ( *psz_parser != ':' && psz_parser < psz_auth_parser )
534 if ( psz_parser != psz_auth_parser )
537 psz_pwd = psz_parser + 1;
543 *psz_auth_parser = '\0';
544 psz_parser = psz_auth_parser + 1;
547 psz_server_addr = psz_parser;
549 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
551 if( *psz_parser == '[' )
554 while( *psz_parser && *psz_parser != ']' )
562 if ( *psz_parser == ':' )
566 psz_server_port = psz_parser;
568 while( *psz_parser && *psz_parser != '/' )
574 if( *psz_parser == '/' )
578 psz_path = psz_parser;
581 /* Convert port format */
582 if( *psz_server_port )
584 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
587 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
588 free( p_input->p_access_data );
594 if( i_server_port == 0 )
599 if( !*psz_server_addr )
601 msg_Err( p_input, "no server given" );
602 free( p_input->p_access_data );
607 /* Handle autehtification */
609 if ( psz_user == NULL )
611 var_Create( p_input, "http-user", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
612 var_Get( p_input, "http-user", &val );
613 psz_user = val.psz_string;
615 var_Create( p_input, "http-pwd", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
616 var_Get( p_input, "http-pwd", &val );
617 psz_pwd = val.psz_string;
620 if (psz_user != NULL)
622 char psz_user_pwd[MAX_QUERY_SIZE];
623 msg_Dbg( p_input, "authenticating, user=%s, password=%s",
625 snprintf( psz_user_pwd, MAX_QUERY_SIZE, "%s:%s", psz_user, psz_pwd );
626 snprintf( p_access_data->psz_auth_string, MAX_QUERY_SIZE,
627 "Authorization: Basic %s\r\n",
628 str_base64_encode( psz_user_pwd, p_input ) );
631 /* Check proxy config variable */
632 var_Create( p_input, "http-proxy", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
633 var_Get( p_input, "http-proxy", &val );
634 psz_proxy_orig = val.psz_string;
635 if( psz_proxy_orig == NULL )
637 /* Check proxy environment variable */
638 psz_proxy_orig = getenv( "http_proxy" );
639 if( psz_proxy_orig != NULL )
641 psz_proxy_orig = strdup( psz_proxy_orig );
645 psz_proxy = psz_proxy_orig;
646 if( psz_proxy != NULL && *psz_proxy )
648 /* http://myproxy.mydomain:myport/ */
649 int i_proxy_port = 0;
651 /* Skip the protocol name */
652 while( *psz_proxy && *psz_proxy != ':' )
657 /* Skip the "://" part */
658 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
663 /* Found a proxy name */
666 char *psz_port = psz_proxy;
668 /* Skip the hostname part */
669 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
674 /* Found a port name */
679 /* Replace ':' with '\0' */
684 while( *psz_junk && *psz_junk != '/' )
694 if( *psz_port != '\0' )
696 i_proxy_port = atoi( psz_port );
700 psz_proxy = strdup( psz_proxy );
702 msg_Dbg( p_input, "using HTTP proxy server=%s port=%d",
703 psz_proxy, i_proxy_port );
707 msg_Err( p_input, "HTTP proxy %s is invalid!", psz_proxy_orig );
708 free( p_input->p_access_data );
710 if( psz_proxy_orig ) free( psz_proxy_orig );
714 if( psz_proxy_orig ) free( psz_proxy_orig );
716 p_access_data->socket_desc.psz_server_addr = psz_proxy;
717 p_access_data->socket_desc.i_server_port = i_proxy_port;
718 p_access_data->socket_desc.i_type = NETWORK_TCP;
719 p_access_data->socket_desc.i_ttl = 0;
721 snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
722 "GET http://%s:%d/%s HTTP/1.0\r\n",
723 psz_server_addr, i_server_port, psz_path );
727 /* No proxy, direct connection. */
728 p_access_data->socket_desc.i_type = NETWORK_TCP;
729 p_access_data->socket_desc.psz_server_addr = psz_server_addr;
730 p_access_data->socket_desc.i_server_port = i_server_port;
731 p_access_data->socket_desc.i_ttl = 0;
733 snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
734 "GET /%s HTTP/1.1\r\nHost: %s\r\n",
735 psz_path, psz_server_addr );
737 p_access_data->psz_buffer[MAX_QUERY_SIZE - 1] = '\0';
739 msg_Dbg( p_input, "opening server=%s port=%d path=%s",
740 psz_server_addr, i_server_port, psz_path );
742 p_input->pf_read = Read;
743 p_input->pf_set_program = input_SetProgram;
744 p_input->pf_set_area = NULL;
745 p_input->pf_seek = Seek;
747 vlc_mutex_lock( &p_input->stream.stream_lock );
748 p_input->stream.b_pace_control = VLC_TRUE;
749 p_input->stream.b_seekable = VLC_TRUE;
750 p_input->stream.p_selected_area->i_tell = 0;
751 p_input->stream.p_selected_area->i_size = 0;
752 p_input->stream.i_method = INPUT_METHOD_NETWORK;
753 vlc_mutex_unlock( &p_input->stream.stream_lock );
756 if( HTTPConnect( p_input, 0 ) )
758 /* Request failed, try again with HTTP/1.0 */
759 char * psz_pos = strstr( p_access_data->psz_buffer, "HTTP/1.1" );
766 p_input->stream.b_seekable = VLC_FALSE;
768 if( HTTPConnect( p_input, 0 ) )
770 free( p_input->p_access_data );
776 /* Update default_pts to a suitable value for http access */
778 var_Create( p_input, "http-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
779 var_Get( p_input, "http-caching", &val );
780 p_input->i_pts_delay = val.i_int * 1000;
785 /*****************************************************************************
786 * Close: free unused data structures
787 *****************************************************************************/
788 static void Close( vlc_object_t *p_this )
790 input_thread_t * p_input = (input_thread_t *)p_this;
791 int i_handle = ((network_socket_t *)p_input->p_access_data)->i_handle;
792 _input_socket_t * p_access_data =
793 (_input_socket_t *)p_input->p_access_data;
795 free( p_access_data->psz_name );
797 msg_Info( p_input, "closing HTTP target `%s'", p_input->psz_source );
799 #if defined( WIN32 ) || defined( UNDER_CE )
800 closesocket( i_handle );
805 free( p_access_data );
808 /*****************************************************************************
809 * Seek: close and re-open a connection at the right place
810 *****************************************************************************/
811 static void Seek( input_thread_t * p_input, off_t i_pos )
813 _input_socket_t *p_access_data = (_input_socket_t*)p_input->p_access_data;
814 #if defined( WIN32 ) || defined( UNDER_CE )
815 closesocket( p_access_data->_socket.i_handle );
817 close( p_access_data->_socket.i_handle );
819 msg_Dbg( p_input, "seeking to position "I64Fd, i_pos );
820 HTTPConnect( p_input, i_pos );
823 /*****************************************************************************
824 * Read: Read up to i_len bytes from the http connection and place in
825 * p_buffer. Return the actual number of bytes read
826 *****************************************************************************/
827 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
829 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
830 struct timeval timeout;
835 /* Initialize file descriptor set */
837 FD_SET( p_access_data->i_handle, &fds );
839 /* We'll wait 0.5 second if nothing happens */
841 timeout.tv_usec = 500000;
843 /* Find if some data is available */
844 while( (i_ret = select( p_access_data->i_handle + 1, &fds,
845 NULL, NULL, &timeout )) == 0
847 || (i_ret < 0 && errno == EINTR)
852 FD_SET( p_access_data->i_handle, &fds );
854 timeout.tv_usec = 500000;
856 if( p_input->b_die || p_input->b_error )
864 msg_Err( p_input, "network select error" );
868 i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
873 msg_Err( p_input, "recv failed (%s)", strerror(errno) );
875 msg_Err( p_input, "recv failed" );