1 /*****************************************************************************
2 * http.c: HTTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: http.c,v 1.7 2002/11/08 10:26:52 gbazin 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 *****************************************************************************/
28 #include <sys/types.h>
35 #include <vlc/input.h>
39 #elif defined( _MSC_VER ) && defined( _WIN32 )
44 # include <winsock2.h>
45 # include <ws2tcpip.h>
47 # define IN_MULTICAST(a) IN_CLASSD(a)
50 # include <sys/socket.h>
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Open ( vlc_object_t * );
59 static void Close ( vlc_object_t * );
61 static int SetProgram ( input_thread_t *, pgrm_descriptor_t * );
62 static void Seek ( input_thread_t *, off_t );
64 /*****************************************************************************
66 *****************************************************************************/
68 set_description( _("HTTP access module") );
69 set_capability( "access", 0 );
70 add_shortcut( "http" );
71 add_shortcut( "http4" );
72 add_shortcut( "http6" );
73 set_callbacks( Open, Close );
76 /*****************************************************************************
77 * _input_socket_t: private access plug-in data, modified to add private
79 *****************************************************************************/
80 typedef struct _input_socket_s
82 input_socket_t _socket;
85 network_socket_t socket_desc;
90 /*****************************************************************************
91 * HTTPConnect: connect to the server and seek to i_tell
92 *****************************************************************************/
93 static int HTTPConnect( input_thread_t * p_input, off_t i_tell )
95 _input_socket_t * p_access_data;
99 int i_returncode, i, i_size;
100 char * psz_return_alpha;
102 /* Find an appropriate network module */
103 p_access_data = (_input_socket_t *)p_input->p_access_data;
104 p_input->p_private = (void*) &p_access_data->socket_desc;
105 p_network = module_Need( p_input, "network", p_access_data->psz_network );
106 if( p_network == NULL )
110 module_Unneed( p_input, p_network );
112 p_access_data->_socket.i_handle = p_access_data->socket_desc.i_handle;
114 # define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
115 # define HTTP_END "\r\n"
117 if ( p_input->stream.b_seekable )
119 snprintf( psz_buffer, sizeof(psz_buffer),
121 "Range: bytes="I64Fd"-\r\n"
122 HTTP_USERAGENT HTTP_END,
123 p_access_data->psz_buffer, i_tell );
127 snprintf( psz_buffer, sizeof(psz_buffer),
129 HTTP_USERAGENT HTTP_END,
130 p_access_data->psz_buffer );
132 psz_buffer[sizeof(psz_buffer) - 1] = '\0';
135 if( send( p_access_data->_socket.i_handle, psz_buffer,
136 strlen( psz_buffer ), 0 ) == (-1) )
138 msg_Err( p_input, "cannot send request (%s)", strerror(errno) );
139 input_FDNetworkClose( p_input );
143 /* Prepare the input thread for reading. */
144 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
146 /* FIXME: we shouldn't have to do that ! It's UGLY but mandatory because
147 * input_FillBuffer assumes p_input->pf_read exists */
148 p_input->pf_read = input_FDNetworkRead;
150 while( !input_FillBuffer( p_input ) )
152 if( p_input->b_die || p_input->b_error )
154 input_FDNetworkClose( p_input );
159 /* Parse HTTP header. */
160 #define MAX_LINE 1024
162 /* get the returncode */
163 if( (i_size = input_Peek( p_input, &psz_parser, MAX_LINE )) <= 0 )
165 msg_Err( p_input, "not enough data" );
166 input_FDNetworkClose( p_input );
170 if( (i_size >= sizeof("HTTP/1.") + 1 ) &&
171 !strncmp( psz_parser, "HTTP/1.", sizeof("HTTP/1.") - 1 ) )
173 psz_parser += sizeof("HTTP/1.") + 1;
174 i_returncode = atoi( (char*)psz_parser );
175 msg_Dbg( p_input, "HTTP server replied: %i", i_returncode );
177 i_size -= (sizeof("HTTP/1.") + 5);
178 for ( i = 0; (i < i_size -1) && ((psz_parser[i] != '\r') ||
179 (psz_parser[i+1] != '\n')); i++ )
183 /* check we actually parsed something */
184 if ( (i == i_size - 1) && (psz_parser[i+1] != '\n') )
186 msg_Err( p_input, "stream not compliant with HTTP/1.x" );
190 psz_return_alpha = malloc( i + 1 );
191 memcpy( psz_return_alpha, psz_parser, i );
192 psz_return_alpha[i] = '\0';
196 msg_Err( p_input, "invalid http reply" );
200 if ( i_returncode >= 400 ) /* something is wrong */
202 msg_Err( p_input, "%i %s", i_returncode,
209 if( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
211 msg_Err( p_input, "not enough data" );
212 input_FDNetworkClose( p_input );
216 if( psz_parser[0] == '\r' && psz_parser[1] == '\n' )
219 p_input->p_current_data += 2;
223 if( !strncmp( psz_parser, "Content-Length: ",
224 strlen("Content-Length: ") ) )
226 psz_parser += strlen("Content-Length: ");
227 vlc_mutex_lock( &p_input->stream.stream_lock );
229 p_input->stream.p_selected_area->i_size = atoll( (char*)psz_parser )
232 /* FIXME : this won't work for 64-bit lengths */
233 p_input->stream.p_selected_area->i_size = atoi( (char*)psz_parser )
236 vlc_mutex_unlock( &p_input->stream.stream_lock );
239 while( *psz_parser != '\r' && psz_parser < p_input->p_last_data )
243 p_input->p_current_data = psz_parser + 2;
246 if( p_input->stream.p_selected_area->i_size )
248 vlc_mutex_lock( &p_input->stream.stream_lock );
249 p_input->stream.p_selected_area->i_tell = i_tell
250 + (p_input->p_last_data - p_input->p_current_data);
251 p_input->stream.b_seekable = 1;
252 p_input->stream.b_changed = 1;
253 vlc_mutex_unlock( &p_input->stream.stream_lock );
259 /*****************************************************************************
260 * Open: parse URL and open the remote file at the beginning
261 *****************************************************************************/
262 static int Open( vlc_object_t *p_this )
264 input_thread_t * p_input = (input_thread_t *)p_this;
265 _input_socket_t * p_access_data;
266 char * psz_name = strdup(p_input->psz_name);
267 char * psz_parser = psz_name;
268 char * psz_server_addr = "";
269 char * psz_server_port = "";
270 char * psz_path = "";
272 int i_server_port = 0;
274 p_access_data = malloc( sizeof(_input_socket_t) );
275 p_input->p_access_data = (access_sys_t *)p_access_data;
276 if( p_access_data == NULL )
278 msg_Err( p_input, "out of memory" );
283 p_access_data->psz_name = psz_name;
284 p_access_data->psz_network = "";
285 if( config_GetInt( p_input, "ipv4" ) )
287 p_access_data->psz_network = "ipv4";
289 if( config_GetInt( p_input, "ipv6" ) )
291 p_access_data->psz_network = "ipv6";
293 if( *p_input->psz_access )
295 /* Find out which shortcut was used */
296 if( !strncmp( p_input->psz_access, "http6", 6 ) )
298 p_access_data->psz_network = "ipv6";
300 else if( !strncmp( p_input->psz_access, "http4", 6 ) )
302 p_access_data->psz_network = "ipv4";
306 /* Parse psz_name syntax :
307 * //<hostname>[:<port>][/<path>] */
308 while( *psz_parser == '/' )
312 psz_server_addr = psz_parser;
314 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
319 if ( *psz_parser == ':' )
323 psz_server_port = psz_parser;
325 while( *psz_parser && *psz_parser != '/' )
331 if( *psz_parser == '/' )
335 psz_path = psz_parser;
338 /* Convert port format */
339 if( *psz_server_port )
341 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
344 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
345 free( p_input->p_access_data );
351 if( i_server_port == 0 )
356 if( !*psz_server_addr )
358 msg_Err( p_input, "no server given" );
359 free( p_input->p_access_data );
365 if( (psz_proxy = getenv( "http_proxy" )) != NULL && *psz_proxy )
367 /* http://myproxy.mydomain:myport/ */
368 int i_proxy_port = 0;
370 /* Skip the protocol name */
371 while( *psz_proxy && *psz_proxy != ':' )
376 /* Skip the "://" part */
377 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
382 /* Found a proxy name */
385 char *psz_port = psz_proxy;
387 /* Skip the hostname part */
388 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
393 /* Found a port name */
398 /* Replace ':' with '\0' */
403 while( *psz_junk && *psz_junk != '/' )
413 if( *psz_port != '\0' )
415 i_proxy_port = atoi( psz_port );
421 msg_Err( p_input, "http_proxy environment variable is invalid!" );
422 free( p_input->p_access_data );
427 p_access_data->socket_desc.i_type = NETWORK_TCP;
428 p_access_data->socket_desc.psz_server_addr = psz_proxy;
429 p_access_data->socket_desc.i_server_port = i_proxy_port;
431 snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
432 "GET http://%s:%d/%s\r\n HTTP/1.0\r\n",
433 psz_server_addr, i_server_port, psz_path );
437 /* No proxy, direct connection. */
438 p_access_data->socket_desc.i_type = NETWORK_TCP;
439 p_access_data->socket_desc.psz_server_addr = psz_server_addr;
440 p_access_data->socket_desc.i_server_port = i_server_port;
442 snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
443 "GET /%s HTTP/1.1\r\nHost: %s\r\n",
444 psz_path, psz_server_addr );
446 p_access_data->psz_buffer[sizeof(p_access_data->psz_buffer) - 1] = '\0';
448 msg_Dbg( p_input, "opening server=%s port=%d path=%s",
449 psz_server_addr, i_server_port, psz_path );
451 p_input->pf_read = input_FDNetworkRead;
452 p_input->pf_set_program = SetProgram;
453 p_input->pf_set_area = NULL;
454 p_input->pf_seek = Seek;
456 vlc_mutex_lock( &p_input->stream.stream_lock );
457 p_input->stream.b_pace_control = 1;
458 p_input->stream.b_seekable = 1;
459 p_input->stream.p_selected_area->i_tell = 0;
460 p_input->stream.p_selected_area->i_size = 0;
461 p_input->stream.i_method = INPUT_METHOD_NETWORK;
462 vlc_mutex_unlock( &p_input->stream.stream_lock );
465 if( HTTPConnect( p_input, 0 ) )
467 char * psz_pos = strstr(p_access_data->psz_buffer, "HTTP/1.1");
468 p_input->stream.b_seekable = 0;
470 if( HTTPConnect( p_input, 0 ) )
472 free( p_input->p_access_data );
480 /*****************************************************************************
481 * Close: free unused data structures
482 *****************************************************************************/
483 static void Close( vlc_object_t *p_this )
485 input_thread_t * p_input = (input_thread_t *)p_this;
486 _input_socket_t * p_access_data =
487 (_input_socket_t *)p_input->p_access_data;
489 free( p_access_data->psz_name );
490 input_FDNetworkClose( p_input );
493 /*****************************************************************************
494 * SetProgram: do nothing
495 *****************************************************************************/
496 static int SetProgram( input_thread_t * p_input,
497 pgrm_descriptor_t * p_program )
502 /*****************************************************************************
503 * Seek: close and re-open a connection at the right place
504 *****************************************************************************/
505 static void Seek( input_thread_t * p_input, off_t i_pos )
507 _input_socket_t *p_access_data = (_input_socket_t*)p_input->p_access_data;
508 close( p_access_data->_socket.i_handle );
509 msg_Dbg( p_input, "seeking to position "I64Fd, i_pos );
510 HTTPConnect( p_input, i_pos );