1 /*****************************************************************************
2 * http.c: HTTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: http.c,v 1.24 2003/02/20 01:52:45 sigmunau 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 *****************************************************************************/
31 #include <vlc/input.h>
40 #ifdef HAVE_SYS_TIME_H
41 # include <sys/time.h>
46 #elif defined( _MSC_VER ) && defined( _WIN32 ) && !defined( UNDER_CE )
50 #if defined( UNDER_CE )
52 #elif defined( WIN32 )
53 # include <winsock2.h>
54 # include <ws2tcpip.h>
56 # define IN_MULTICAST(a) IN_CLASSD(a)
59 # include <sys/socket.h>
62 #include "vlc_playlist.h"
65 /*****************************************************************************
67 *****************************************************************************/
68 static int Open ( vlc_object_t * );
69 static void Close ( vlc_object_t * );
71 static int SetProgram ( input_thread_t *, pgrm_descriptor_t * );
72 static void Seek ( input_thread_t *, off_t );
73 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
75 /*****************************************************************************
77 *****************************************************************************/
78 #define PROXY_TEXT N_("specify an HTTP proxy")
79 #define PROXY_LONGTEXT N_( \
80 "Specify an HTTP proxy to use. It must be in the form " \
81 "http://myproxy.mydomain:myport. If none is specified, the HTTP_PROXY " \
82 "environment variable will be tried." )
84 #define CACHING_TEXT N_("caching value in ms")
85 #define CACHING_LONGTEXT N_( \
86 "Allows you to modify the default caching value for http streams. This " \
87 "value should be set in miliseconds units." )
90 add_category_hint( N_("http"), NULL, VLC_FALSE );
91 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT, VLC_FALSE );
92 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
93 set_description( _("HTTP access module") );
94 set_capability( "access", 0 );
95 add_shortcut( "http" );
96 add_shortcut( "http4" );
97 add_shortcut( "http6" );
98 set_callbacks( Open, Close );
101 /*****************************************************************************
102 * _input_socket_t: private access plug-in data, modified to add private
104 *****************************************************************************/
105 #define MAX_ANSWER_SIZE 1024
106 #define MAX_QUERY_SIZE 1024
108 typedef struct _input_socket_s
110 input_socket_t _socket;
113 network_socket_t socket_desc;
114 char psz_buffer[MAX_QUERY_SIZE];
118 /*****************************************************************************
119 * HTTPConnect: connect to the server and seek to i_tell
120 *****************************************************************************/
121 static int HTTPConnect( input_thread_t * p_input, off_t i_tell )
123 char psz_buffer[MAX_QUERY_SIZE];
124 _input_socket_t * p_access_data;
125 module_t * p_network;
126 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"
152 HTTP_USERAGENT HTTP_END,
153 p_access_data->psz_buffer, i_tell );
157 snprintf( psz_buffer, MAX_QUERY_SIZE,
159 HTTP_USERAGENT HTTP_END,
160 p_access_data->psz_buffer );
162 psz_buffer[MAX_QUERY_SIZE - 1] = '\0';
165 i_ret = send( p_access_data->_socket.i_handle,
166 psz_buffer, strlen( psz_buffer ), 0 );
170 msg_Err( p_input, "cannot send request (%s)", strerror(errno) );
172 msg_Err( p_input, "cannot send request" );
174 Close( VLC_OBJECT(p_input) );
178 /* Prepare the input thread for reading. */
179 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
181 /* FIXME: we shouldn't have to do that ! It's UGLY but mandatory because
182 * input_FillBuffer assumes p_input->pf_read exists */
183 p_input->pf_read = Read;
185 while( !input_FillBuffer( p_input ) )
187 if( p_input->b_die || p_input->b_error )
189 Close( VLC_OBJECT(p_input) );
194 /* Get the HTTP returncode */
195 i_size = input_Peek( p_input, (byte_t**)&psz_parser, MAX_ANSWER_SIZE );
199 msg_Err( p_input, "not enough data" );
200 Close( VLC_OBJECT(p_input) );
204 /* Guess the protocol */
205 if( ( ( (size_t)i_size >= strlen("HTTP/1.x") ) &&
206 !strncmp( psz_parser, "HTTP/1.", strlen("HTTP/1.") ) ) )
208 i_protocol = HTTP_PROTOCOL;
210 psz_parser += strlen("HTTP/1.x");
211 i_size -= strlen("HTTP/1.x");
213 else if( ( (size_t)i_size >= strlen("ICY") &&
214 !strncmp( psz_parser, "ICY", strlen("ICY") ) ) )
216 i_protocol = ICY_PROTOCOL;
217 if( !p_input->psz_demux || !*p_input->psz_demux )
219 msg_Info( p_input, "ICY server found, mp3 demuxer selected" );
220 p_input->psz_demux = "mp3"; // FIXME strdup ?
223 psz_parser += strlen("ICY");
224 i_size -= strlen("ICY");
228 msg_Err( p_input, "invalid HTTP reply '%s'", psz_parser );
232 /* Check the HTTP return code */
233 i_code = atoi( (char*)psz_parser );
234 msg_Dbg( p_input, "%s server replied: %i",
235 i_protocol == HTTP_PROTOCOL ? "HTTP" : "ICY", i_code );
239 /* Find the end of the line */
240 for ( i = 0; (i < i_size -1) && ((psz_parser[i] != '\r') ||
241 (psz_parser[i+1] != '\n')); i++ )
246 /* Check we actually parsed something */
247 if ( i+1 == i_size && psz_parser[i+1] != '\n' )
249 msg_Err( p_input, "stream not compliant with HTTP/1.x" );
253 /* Store the line we just parsed and skip it */
254 psz_answer = strndup( psz_parser, i );
260 p_input->p_current_data = psz_parser + i + 2;
262 /* Parse remaining headers */
265 char psz_line[MAX_ANSWER_SIZE];
267 i_size = input_Peek( p_input, (byte_t**)&psz_parser, MAX_ANSWER_SIZE );
271 msg_Err( p_input, "not enough data" );
272 Close( VLC_OBJECT(p_input) );
277 /* Copy one line to psz_line */
279 while( i_size && psz_parser[i] != '\r'
280 && psz_parser[i + 1] != '\n' )
282 psz_line[i] = psz_parser[i];
286 p_input->p_current_data = psz_parser + i + 2;
289 break; /* End of headers */
292 psz_parser = strchr( psz_line, ':' );
295 msg_Err( p_input, "malformed header line: %s", psz_line );
299 psz_parser[0] = '\0';
301 while ( *psz_parser == ' ' || *psz_parser == '\t' )
305 psz_value = psz_parser;
307 if( !strcasecmp( psz_line, "Content-Length" ) )
311 i_size = i_tell + atoll( psz_value );
313 psz_parser = psz_value;
314 while( psz_parser[0] >= '0' && psz_parser[0] <= '9' )
317 i_size += psz_parser[0] - '0';
321 msg_Dbg( p_input, "stream size is "I64Fd, i_size );
323 vlc_mutex_lock( &p_input->stream.stream_lock );
324 p_input->stream.p_selected_area->i_size = i_size;
325 vlc_mutex_unlock( &p_input->stream.stream_lock );
327 /* Redirection support */
328 else if( ( i_code == 301 || i_code == 302 ||
329 i_code == 303 || i_code == 307 )
330 && !strcasecmp( psz_line, "location" ) )
332 playlist_t * p_playlist = (playlist_t *) vlc_object_find(
333 p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
336 msg_Err( p_input, "redirection failed: can't find playlist" );
340 msg_Dbg( p_input, "%i %s: redirected to %s",
341 i_code, psz_answer, psz_value );
342 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion
344 playlist_Add( p_playlist, psz_value,
345 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
346 vlc_object_release( p_playlist );
349 /* TODO: parse other headers here */
352 /* Something went wrong */
355 msg_Err( p_input, "%i %s", i_code, psz_answer );
356 p_input->p_current_data = psz_parser + i_size;
363 /* Set final stream properties */
364 vlc_mutex_lock( &p_input->stream.stream_lock );
365 if( i_protocol == ICY_PROTOCOL )
367 p_input->stream.b_seekable = VLC_FALSE;
371 p_input->stream.b_seekable = VLC_TRUE;
374 if( p_input->stream.p_selected_area->i_size )
376 p_input->stream.p_selected_area->i_tell = i_tell;
380 p_input->stream.b_seekable = VLC_FALSE;
382 p_input->stream.b_changed = VLC_TRUE;
383 vlc_mutex_unlock( &p_input->stream.stream_lock );
388 /*****************************************************************************
389 * Open: parse URL and open the remote file at the beginning
390 *****************************************************************************/
391 static int Open( vlc_object_t *p_this )
393 input_thread_t * p_input = (input_thread_t *)p_this;
394 _input_socket_t * p_access_data;
395 char * psz_name = strdup(p_input->psz_name);
396 char * psz_parser = psz_name;
397 char * psz_server_addr = "";
398 char * psz_server_port = "";
399 char * psz_path = "";
400 char * psz_proxy, *psz_proxy_orig;
401 int i_server_port = 0;
403 p_access_data = malloc( sizeof(_input_socket_t) );
404 p_input->p_access_data = (access_sys_t *)p_access_data;
405 if( p_access_data == NULL )
407 msg_Err( p_input, "out of memory" );
412 p_access_data->psz_name = psz_name;
413 p_access_data->psz_network = "";
414 if( config_GetInt( p_input, "ipv4" ) )
416 p_access_data->psz_network = "ipv4";
418 if( config_GetInt( p_input, "ipv6" ) )
420 p_access_data->psz_network = "ipv6";
422 if( *p_input->psz_access )
424 /* Find out which shortcut was used */
425 if( !strncmp( p_input->psz_access, "http6", 6 ) )
427 p_access_data->psz_network = "ipv6";
429 else if( !strncmp( p_input->psz_access, "http4", 6 ) )
431 p_access_data->psz_network = "ipv4";
435 /* Parse psz_name syntax :
436 * //<hostname>[:<port>][/<path>] */
437 while( *psz_parser == '/' )
441 psz_server_addr = psz_parser;
443 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
445 if( *psz_parser == '[' )
448 while( *psz_parser && *psz_parser != ']' )
456 if ( *psz_parser == ':' )
460 psz_server_port = psz_parser;
462 while( *psz_parser && *psz_parser != '/' )
468 if( *psz_parser == '/' )
472 psz_path = psz_parser;
475 /* Convert port format */
476 if( *psz_server_port )
478 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
481 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
482 free( p_input->p_access_data );
488 if( i_server_port == 0 )
493 if( !*psz_server_addr )
495 msg_Err( p_input, "no server given" );
496 free( p_input->p_access_data );
501 /* Check proxy config variable */
502 psz_proxy_orig = config_GetPsz( p_input, "http-proxy" );
503 if( psz_proxy_orig == NULL )
505 /* Check proxy environment variable */
506 psz_proxy_orig = getenv( "http_proxy" );
507 if( psz_proxy_orig != NULL )
509 psz_proxy_orig = strdup( psz_proxy_orig );
513 psz_proxy = psz_proxy_orig;
514 if( psz_proxy != NULL && *psz_proxy )
516 /* http://myproxy.mydomain:myport/ */
517 int i_proxy_port = 0;
519 /* Skip the protocol name */
520 while( *psz_proxy && *psz_proxy != ':' )
525 /* Skip the "://" part */
526 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
531 /* Found a proxy name */
534 char *psz_port = psz_proxy;
536 /* Skip the hostname part */
537 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
542 /* Found a port name */
547 /* Replace ':' with '\0' */
552 while( *psz_junk && *psz_junk != '/' )
562 if( *psz_port != '\0' )
564 i_proxy_port = atoi( psz_port );
568 psz_proxy = strdup( psz_proxy );
570 msg_Dbg( p_input, "using HTTP proxy server=%s port=%d",
571 psz_proxy, i_proxy_port );
575 msg_Err( p_input, "HTTP proxy %s is invalid!", psz_proxy_orig );
576 free( p_input->p_access_data );
578 if( psz_proxy_orig ) free( psz_proxy_orig );
582 if( psz_proxy_orig ) free( psz_proxy_orig );
584 p_access_data->socket_desc.psz_server_addr = psz_proxy;
585 p_access_data->socket_desc.i_server_port = i_proxy_port;
586 p_access_data->socket_desc.i_type = NETWORK_TCP;
588 snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
589 "GET http://%s:%d/%s HTTP/1.0\r\n",
590 psz_server_addr, i_server_port, psz_path );
594 /* No proxy, direct connection. */
595 p_access_data->socket_desc.i_type = NETWORK_TCP;
596 p_access_data->socket_desc.psz_server_addr = psz_server_addr;
597 p_access_data->socket_desc.i_server_port = i_server_port;
599 snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
600 "GET /%s HTTP/1.1\r\nHost: %s\r\n",
601 psz_path, psz_server_addr );
603 p_access_data->psz_buffer[MAX_QUERY_SIZE - 1] = '\0';
605 msg_Dbg( p_input, "opening server=%s port=%d path=%s",
606 psz_server_addr, i_server_port, psz_path );
608 p_input->pf_read = Read;
609 p_input->pf_set_program = SetProgram;
610 p_input->pf_set_area = NULL;
611 p_input->pf_seek = Seek;
613 vlc_mutex_lock( &p_input->stream.stream_lock );
614 p_input->stream.b_pace_control = VLC_TRUE;
615 p_input->stream.b_seekable = VLC_TRUE;
616 p_input->stream.b_connected = VLC_TRUE;
617 p_input->stream.p_selected_area->i_tell = 0;
618 p_input->stream.p_selected_area->i_size = 0;
619 p_input->stream.i_method = INPUT_METHOD_NETWORK;
620 vlc_mutex_unlock( &p_input->stream.stream_lock );
623 if( HTTPConnect( p_input, 0 ) )
625 /* Request failed, try again with HTTP/1.0 */
626 char * psz_pos = strstr( p_access_data->psz_buffer, "HTTP/1.1" );
633 p_input->stream.b_seekable = VLC_FALSE;
635 if( HTTPConnect( p_input, 0 ) )
637 free( p_input->p_access_data );
643 /* Update default_pts to a suitable value for http access */
644 p_input->i_pts_delay = config_GetInt( p_input, "http-caching" ) * 1000;
649 /*****************************************************************************
650 * Close: free unused data structures
651 *****************************************************************************/
652 static void Close( vlc_object_t *p_this )
654 input_thread_t * p_input = (input_thread_t *)p_this;
655 int i_handle = ((network_socket_t *)p_input->p_access_data)->i_handle;
656 _input_socket_t * p_access_data =
657 (_input_socket_t *)p_input->p_access_data;
659 free( p_access_data->psz_name );
661 msg_Info( p_input, "closing HTTP target `%s'", p_input->psz_source );
663 #if defined( WIN32 ) || defined( UNDER_CE )
664 closesocket( i_handle );
669 free( p_access_data );
672 /*****************************************************************************
673 * SetProgram: do nothing
674 *****************************************************************************/
675 static int SetProgram( input_thread_t * p_input,
676 pgrm_descriptor_t * p_program )
681 /*****************************************************************************
682 * Seek: close and re-open a connection at the right place
683 *****************************************************************************/
684 static void Seek( input_thread_t * p_input, off_t i_pos )
686 _input_socket_t *p_access_data = (_input_socket_t*)p_input->p_access_data;
687 #if defined( WIN32 ) || defined( UNDER_CE )
688 closesocket( p_access_data->_socket.i_handle );
690 close( p_access_data->_socket.i_handle );
692 msg_Dbg( p_input, "seeking to position "I64Fd, i_pos );
693 HTTPConnect( p_input, i_pos );
696 /*****************************************************************************
697 * Read: read on a file descriptor, checking b_die periodically
698 *****************************************************************************/
699 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
701 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
702 struct timeval timeout;
706 /* Initialize file descriptor set */
708 FD_SET( p_access_data->i_handle, &fds );
710 /* We'll wait 0.5 second if nothing happens */
712 timeout.tv_usec = 500000;
714 /* Find if some data is available */
715 i_ret = select( p_access_data->i_handle + 1, &fds,
716 NULL, NULL, &timeout );
719 if( i_ret == -1 && errno != EINTR )
721 msg_Err( p_input, "network select error (%s)", strerror(errno) );
726 msg_Err( p_input, "network select error" );
731 ssize_t i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
736 msg_Err( p_input, "recv failed (%s)", strerror(errno) );
738 msg_Err( p_input, "recv failed" );