1 /*****************************************************************************
2 * http.c: HTTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: http.c,v 1.33 2003/04/30 04:13:12 hartman 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 set_description( _("HTTP input") );
90 set_capability( "access", 0 );
91 add_shortcut( "http" );
92 add_shortcut( "http4" );
93 add_shortcut( "http6" );
94 set_callbacks( Open, Close );
97 /*****************************************************************************
98 * _input_socket_t: private access plug-in data, modified to add private
100 *****************************************************************************/
101 #define MAX_ANSWER_SIZE 1024
102 #define MAX_QUERY_SIZE 1024
104 typedef struct _input_socket_s
106 input_socket_t _socket;
109 network_socket_t socket_desc;
110 char psz_buffer[MAX_QUERY_SIZE];
114 /*****************************************************************************
115 * HTTPConnect: connect to the server and seek to i_tell
116 *****************************************************************************/
117 static int HTTPConnect( input_thread_t * p_input, off_t i_tell )
119 char psz_buffer[MAX_QUERY_SIZE];
120 _input_socket_t * p_access_data;
121 module_t * p_network;
122 char * psz_parser, * psz_value, * psz_answer;
123 int i_code, i_ret, i, i_size;
125 enum { HTTP_PROTOCOL, ICY_PROTOCOL } i_protocol;
127 /* Find an appropriate network module */
128 p_access_data = (_input_socket_t *)p_input->p_access_data;
129 p_input->p_private = (void*) &p_access_data->socket_desc;
130 p_network = module_Need( p_input, "network", p_access_data->psz_network );
131 if( p_network == NULL )
135 module_Unneed( p_input, p_network );
137 p_access_data->_socket.i_handle = p_access_data->socket_desc.i_handle;
139 # define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
140 # define HTTP_END "\r\n"
142 /* Build the query string */
143 if ( p_input->stream.b_seekable )
145 snprintf( psz_buffer, MAX_QUERY_SIZE,
147 "Range: bytes="I64Fd"-\r\n"
148 HTTP_USERAGENT HTTP_END,
149 p_access_data->psz_buffer, i_tell );
153 snprintf( psz_buffer, MAX_QUERY_SIZE,
155 HTTP_USERAGENT HTTP_END,
156 p_access_data->psz_buffer );
158 psz_buffer[MAX_QUERY_SIZE - 1] = '\0';
161 i_ret = send( p_access_data->_socket.i_handle,
162 psz_buffer, strlen( psz_buffer ), 0 );
166 msg_Err( p_input, "cannot send request (%s)", strerror(errno) );
168 msg_Err( p_input, "cannot send request" );
170 Close( VLC_OBJECT(p_input) );
174 /* Prepare the input thread for reading. */
175 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
177 /* FIXME: we shouldn't have to do that ! It's UGLY but mandatory because
178 * input_FillBuffer assumes p_input->pf_read exists */
179 p_input->pf_read = Read;
181 while( !input_FillBuffer( p_input ) )
183 if( p_input->b_die || p_input->b_error )
185 Close( VLC_OBJECT(p_input) );
190 /* Get the HTTP returncode */
191 i_size = input_Peek( p_input, (byte_t**)&psz_parser, MAX_ANSWER_SIZE );
195 msg_Err( p_input, "not enough data" );
196 Close( VLC_OBJECT(p_input) );
200 /* Guess the protocol */
201 if( ( ( (size_t)i_size >= strlen("HTTP/1.x") ) &&
202 !strncmp( psz_parser, "HTTP/1.", strlen("HTTP/1.") ) ) )
204 i_protocol = HTTP_PROTOCOL;
206 psz_parser += strlen("HTTP/1.x");
207 i_size -= strlen("HTTP/1.x");
209 else if( ( (size_t)i_size >= strlen("ICY") &&
210 !strncmp( psz_parser, "ICY", strlen("ICY") ) ) )
212 i_protocol = ICY_PROTOCOL;
213 if( !p_input->psz_demux || !*p_input->psz_demux )
215 msg_Info( p_input, "ICY server found, mp3 demuxer selected" );
216 p_input->psz_demux = "mp3"; // FIXME strdup ?
219 psz_parser += strlen("ICY");
220 i_size -= strlen("ICY");
224 msg_Err( p_input, "invalid HTTP reply '%s'", psz_parser );
228 /* Check the HTTP return code */
229 i_code = atoi( (char*)psz_parser );
230 msg_Dbg( p_input, "%s server replied: %i",
231 i_protocol == HTTP_PROTOCOL ? "HTTP" : "ICY", i_code );
235 /* Find the end of the line */
236 for ( i = 0; (i < i_size -1) && ((psz_parser[i] != '\r') ||
237 (psz_parser[i+1] != '\n')); i++ )
242 /* Check we actually parsed something */
243 if ( i+1 == i_size && psz_parser[i+1] != '\n' )
245 msg_Err( p_input, "stream not compliant with HTTP/1.x" );
249 /* Store the line we just parsed and skip it */
250 psz_answer = strndup( psz_parser, i );
256 p_input->p_current_data = psz_parser + i + 2;
258 /* Parse remaining headers */
261 char psz_line[MAX_ANSWER_SIZE];
263 i_size = input_Peek( p_input, (byte_t**)&psz_parser, MAX_ANSWER_SIZE );
267 msg_Err( p_input, "not enough data" );
268 Close( VLC_OBJECT(p_input) );
273 /* Copy one line to psz_line */
275 while( i_size && psz_parser[i] != '\r'
276 && psz_parser[i + 1] != '\n' )
278 psz_line[i] = psz_parser[i];
282 p_input->p_current_data = psz_parser + i + 2;
285 break; /* End of headers */
288 psz_parser = strchr( psz_line, ':' );
291 msg_Err( p_input, "malformed header line: %s", psz_line );
295 psz_parser[0] = '\0';
297 while ( *psz_parser == ' ' || *psz_parser == '\t' )
301 psz_value = psz_parser;
303 if( !strcasecmp( psz_line, "Content-Length" ) )
307 i_size = i_tell + atoll( psz_value );
310 psz_parser = psz_value;
312 while( *psz_value == ' ' || *psz_value == '\t' )
315 if( *psz_value == '-' )
317 while( *psz_value != '\0')
319 if( *psz_value >= '0' && *psz_value <= '9' )
320 i_size = i_size * 10 + *psz_value++ - '0';
324 i_size = i_tell + ( i_size * sign );
326 msg_Dbg( p_input, "stream size is "I64Fd, i_size );
328 vlc_mutex_lock( &p_input->stream.stream_lock );
329 p_input->stream.p_selected_area->i_size = i_size;
330 vlc_mutex_unlock( &p_input->stream.stream_lock );
332 /* Redirection support */
333 else if( ( i_code == 301 || i_code == 302 ||
334 i_code == 303 || i_code == 307 )
335 && !strcasecmp( psz_line, "location" ) )
337 playlist_t * p_playlist = (playlist_t *) vlc_object_find(
338 p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
341 msg_Err( p_input, "redirection failed: can't find playlist" );
345 msg_Dbg( p_input, "%i %s: redirected to %s",
346 i_code, psz_answer, psz_value );
347 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion
349 playlist_Add( p_playlist, psz_value,
350 PLAYLIST_INSERT | PLAYLIST_GO,
351 p_playlist->i_index + 1 );
352 vlc_object_release( p_playlist );
355 /* TODO: parse other headers here */
358 /* Something went wrong */
361 msg_Err( p_input, "%i %s", i_code, psz_answer );
362 p_input->p_current_data = psz_parser + i_size;
369 /* Set final stream properties */
370 vlc_mutex_lock( &p_input->stream.stream_lock );
371 if( i_protocol == ICY_PROTOCOL )
373 p_input->stream.b_seekable = VLC_FALSE;
377 p_input->stream.b_seekable = VLC_TRUE;
380 if( p_input->stream.p_selected_area->i_size )
382 p_input->stream.p_selected_area->i_tell = i_tell;
386 p_input->stream.b_seekable = VLC_FALSE;
388 p_input->stream.b_changed = VLC_TRUE;
389 vlc_mutex_unlock( &p_input->stream.stream_lock );
394 /*****************************************************************************
395 * Open: parse URL and open the remote file at the beginning
396 *****************************************************************************/
397 static int Open( vlc_object_t *p_this )
399 input_thread_t * p_input = (input_thread_t *)p_this;
400 _input_socket_t * p_access_data;
401 char * psz_name = strdup(p_input->psz_name);
402 char * psz_parser = psz_name;
403 char * psz_server_addr = "";
404 char * psz_server_port = "";
405 char * psz_path = "";
406 char * psz_proxy, *psz_proxy_orig;
407 int i_server_port = 0;
409 p_access_data = malloc( sizeof(_input_socket_t) );
410 p_input->p_access_data = (access_sys_t *)p_access_data;
411 if( p_access_data == NULL )
413 msg_Err( p_input, "out of memory" );
418 p_access_data->psz_name = psz_name;
419 p_access_data->psz_network = "";
420 if( config_GetInt( p_input, "ipv4" ) )
422 p_access_data->psz_network = "ipv4";
424 if( config_GetInt( p_input, "ipv6" ) )
426 p_access_data->psz_network = "ipv6";
428 if( *p_input->psz_access )
430 /* Find out which shortcut was used */
431 if( !strncmp( p_input->psz_access, "http6", 6 ) )
433 p_access_data->psz_network = "ipv6";
435 else if( !strncmp( p_input->psz_access, "http4", 6 ) )
437 p_access_data->psz_network = "ipv4";
441 /* Parse psz_name syntax :
442 * //<hostname>[:<port>][/<path>] */
443 while( *psz_parser == '/' )
447 psz_server_addr = psz_parser;
449 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
451 if( *psz_parser == '[' )
454 while( *psz_parser && *psz_parser != ']' )
462 if ( *psz_parser == ':' )
466 psz_server_port = psz_parser;
468 while( *psz_parser && *psz_parser != '/' )
474 if( *psz_parser == '/' )
478 psz_path = psz_parser;
481 /* Convert port format */
482 if( *psz_server_port )
484 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
487 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
488 free( p_input->p_access_data );
494 if( i_server_port == 0 )
499 if( !*psz_server_addr )
501 msg_Err( p_input, "no server given" );
502 free( p_input->p_access_data );
507 /* Check proxy config variable */
508 psz_proxy_orig = config_GetPsz( p_input, "http-proxy" );
509 if( psz_proxy_orig == NULL )
511 /* Check proxy environment variable */
512 psz_proxy_orig = getenv( "http_proxy" );
513 if( psz_proxy_orig != NULL )
515 psz_proxy_orig = strdup( psz_proxy_orig );
519 psz_proxy = psz_proxy_orig;
520 if( psz_proxy != NULL && *psz_proxy )
522 /* http://myproxy.mydomain:myport/ */
523 int i_proxy_port = 0;
525 /* Skip the protocol name */
526 while( *psz_proxy && *psz_proxy != ':' )
531 /* Skip the "://" part */
532 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
537 /* Found a proxy name */
540 char *psz_port = psz_proxy;
542 /* Skip the hostname part */
543 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
548 /* Found a port name */
553 /* Replace ':' with '\0' */
558 while( *psz_junk && *psz_junk != '/' )
568 if( *psz_port != '\0' )
570 i_proxy_port = atoi( psz_port );
574 psz_proxy = strdup( psz_proxy );
576 msg_Dbg( p_input, "using HTTP proxy server=%s port=%d",
577 psz_proxy, i_proxy_port );
581 msg_Err( p_input, "HTTP proxy %s is invalid!", psz_proxy_orig );
582 free( p_input->p_access_data );
584 if( psz_proxy_orig ) free( psz_proxy_orig );
588 if( psz_proxy_orig ) free( psz_proxy_orig );
590 p_access_data->socket_desc.psz_server_addr = psz_proxy;
591 p_access_data->socket_desc.i_server_port = i_proxy_port;
592 p_access_data->socket_desc.i_type = NETWORK_TCP;
594 snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
595 "GET http://%s:%d/%s HTTP/1.0\r\n",
596 psz_server_addr, i_server_port, psz_path );
600 /* No proxy, direct connection. */
601 p_access_data->socket_desc.i_type = NETWORK_TCP;
602 p_access_data->socket_desc.psz_server_addr = psz_server_addr;
603 p_access_data->socket_desc.i_server_port = i_server_port;
605 snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
606 "GET /%s HTTP/1.1\r\nHost: %s\r\n",
607 psz_path, psz_server_addr );
609 p_access_data->psz_buffer[MAX_QUERY_SIZE - 1] = '\0';
611 msg_Dbg( p_input, "opening server=%s port=%d path=%s",
612 psz_server_addr, i_server_port, psz_path );
614 p_input->pf_read = Read;
615 p_input->pf_set_program = input_SetProgram;
616 p_input->pf_set_area = NULL;
617 p_input->pf_seek = Seek;
619 vlc_mutex_lock( &p_input->stream.stream_lock );
620 p_input->stream.b_pace_control = VLC_TRUE;
621 p_input->stream.b_seekable = VLC_TRUE;
622 p_input->stream.p_selected_area->i_tell = 0;
623 p_input->stream.p_selected_area->i_size = 0;
624 p_input->stream.i_method = INPUT_METHOD_NETWORK;
625 vlc_mutex_unlock( &p_input->stream.stream_lock );
628 if( HTTPConnect( p_input, 0 ) )
630 /* Request failed, try again with HTTP/1.0 */
631 char * psz_pos = strstr( p_access_data->psz_buffer, "HTTP/1.1" );
638 p_input->stream.b_seekable = VLC_FALSE;
640 if( HTTPConnect( p_input, 0 ) )
642 free( p_input->p_access_data );
648 /* Update default_pts to a suitable value for http access */
649 p_input->i_pts_delay = config_GetInt( p_input, "http-caching" ) * 1000;
654 /*****************************************************************************
655 * Close: free unused data structures
656 *****************************************************************************/
657 static void Close( vlc_object_t *p_this )
659 input_thread_t * p_input = (input_thread_t *)p_this;
660 int i_handle = ((network_socket_t *)p_input->p_access_data)->i_handle;
661 _input_socket_t * p_access_data =
662 (_input_socket_t *)p_input->p_access_data;
664 free( p_access_data->psz_name );
666 msg_Info( p_input, "closing HTTP target `%s'", p_input->psz_source );
668 #if defined( WIN32 ) || defined( UNDER_CE )
669 closesocket( i_handle );
674 free( p_access_data );
677 /*****************************************************************************
678 * Seek: close and re-open a connection at the right place
679 *****************************************************************************/
680 static void Seek( input_thread_t * p_input, off_t i_pos )
682 _input_socket_t *p_access_data = (_input_socket_t*)p_input->p_access_data;
683 #if defined( WIN32 ) || defined( UNDER_CE )
684 closesocket( p_access_data->_socket.i_handle );
686 close( p_access_data->_socket.i_handle );
688 msg_Dbg( p_input, "seeking to position "I64Fd, i_pos );
689 HTTPConnect( p_input, i_pos );
692 /*****************************************************************************
693 * Read: Read up to i_len bytes from the http connection and place in
694 * p_buffer. Return the actual number of bytes read
695 *****************************************************************************/
696 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
698 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
699 struct timeval timeout;
704 /* Initialize file descriptor set */
706 FD_SET( p_access_data->i_handle, &fds );
708 /* We'll wait 0.5 second if nothing happens */
710 timeout.tv_usec = 500000;
712 /* Find if some data is available */
713 while( (i_ret = select( p_access_data->i_handle + 1, &fds,
714 NULL, NULL, &timeout )) == 0
716 || (i_ret < 0 && errno == EINTR)
721 FD_SET( p_access_data->i_handle, &fds );
723 timeout.tv_usec = 500000;
725 if( p_input->b_die || p_input->b_error )
733 msg_Err( p_input, "network select error" );
737 i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
742 msg_Err( p_input, "recv failed (%s)", strerror(errno) );
744 msg_Err( p_input, "recv failed" );