1 /*****************************************************************************
2 * http.c: HTTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: http.c,v 1.4 2002/09/30 11:05: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 *****************************************************************************/
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;
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=%d%d-\r\n"
122 HTTP_USERAGENT HTTP_END,
123 p_access_data->psz_buffer,
124 (u32)(i_tell>>32), (u32)i_tell );
128 snprintf( psz_buffer, sizeof(psz_buffer),
130 HTTP_USERAGENT HTTP_END,
131 p_access_data->psz_buffer );
133 psz_buffer[sizeof(psz_buffer) - 1] = '\0';
136 if( send( p_access_data->_socket.i_handle, psz_buffer,
137 strlen( psz_buffer ), 0 ) == (-1) )
139 msg_Err( p_input, "cannot send request (%s)", strerror(errno) );
140 input_FDNetworkClose( p_input );
144 /* Prepare the input thread for reading. */
145 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
147 /* FIXME: we shouldn't have to do that ! It's UGLY but mandatory because
148 * input_FillBuffer assumes p_input->pf_read exists */
149 p_input->pf_read = input_FDNetworkRead;
151 while( !input_FillBuffer( p_input ) )
153 if( p_input->b_die || p_input->b_error )
155 input_FDNetworkClose( p_input );
160 /* Parse HTTP header. */
161 #define MAX_LINE 1024
163 /* get the returncode */
164 if( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
166 msg_Err( p_input, "not enough data" );
167 input_FDNetworkClose( p_input );
171 if( !strncmp( psz_parser, "HTTP/1.",
172 strlen("HTTP/1.") ) )
174 psz_parser += strlen("HTTP 1.") + 2;
175 i_returncode = atoi( (char*)psz_parser );
176 msg_Dbg( p_input, "HTTP server replied: %i", i_returncode );
178 for ( i = 0; psz_parser[i] != '\r' || psz_parser[i+1] != '\n'; i++ )
182 psz_return_alpha = malloc( i + 1 );
183 memcpy( psz_return_alpha, psz_parser, i );
184 psz_return_alpha[i] = '\0';
188 msg_Err( p_input, "invalid http reply" );
192 if ( i_returncode >= 400 ) /* something is wrong */
194 msg_Err( p_input, "%i %s", i_returncode,
201 if( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
203 msg_Err( p_input, "not enough data" );
204 input_FDNetworkClose( p_input );
208 if( psz_parser[0] == '\r' && psz_parser[1] == '\n' )
211 p_input->p_current_data += 2;
215 if( !strncmp( psz_parser, "Content-Length: ",
216 strlen("Content-Length: ") ) )
218 psz_parser += strlen("Content-Length: ");
219 vlc_mutex_lock( &p_input->stream.stream_lock );
221 p_input->stream.p_selected_area->i_size = atoll( (char*)psz_parser )
224 /* FIXME : this won't work for 64-bit lengths */
225 p_input->stream.p_selected_area->i_size = atoi( (char*)psz_parser )
228 vlc_mutex_unlock( &p_input->stream.stream_lock );
231 while( *psz_parser != '\r' && psz_parser < p_input->p_last_data )
235 p_input->p_current_data = psz_parser + 2;
238 if( p_input->stream.p_selected_area->i_size )
240 vlc_mutex_lock( &p_input->stream.stream_lock );
241 p_input->stream.p_selected_area->i_tell = i_tell
242 + (p_input->p_last_data - p_input->p_current_data);
243 p_input->stream.b_seekable = 1;
244 p_input->stream.b_changed = 1;
245 vlc_mutex_unlock( &p_input->stream.stream_lock );
251 /*****************************************************************************
252 * Open: parse URL and open the remote file at the beginning
253 *****************************************************************************/
254 static int Open( vlc_object_t *p_this )
256 input_thread_t * p_input = (input_thread_t *)p_this;
257 _input_socket_t * p_access_data;
258 char * psz_name = strdup(p_input->psz_name);
259 char * psz_parser = psz_name;
260 char * psz_server_addr = "";
261 char * psz_server_port = "";
262 char * psz_path = "";
264 int i_server_port = 0;
266 p_access_data = malloc( sizeof(_input_socket_t) );
267 p_input->p_access_data = (access_sys_t *)p_access_data;
268 if( p_access_data == NULL )
270 msg_Err( p_input, "out of memory" );
275 p_access_data->psz_name = psz_name;
276 p_access_data->psz_network = "";
277 if( config_GetInt( p_input, "ipv4" ) )
279 p_access_data->psz_network = "ipv4";
281 if( config_GetInt( p_input, "ipv6" ) )
283 p_access_data->psz_network = "ipv6";
285 if( *p_input->psz_access )
287 /* Find out which shortcut was used */
288 if( !strncmp( p_input->psz_access, "http6", 6 ) )
290 p_access_data->psz_network = "ipv6";
292 else if( !strncmp( p_input->psz_access, "http4", 6 ) )
294 p_access_data->psz_network = "ipv4";
298 /* Parse psz_name syntax :
299 * //<hostname>[:<port>][/<path>] */
300 while( *psz_parser == '/' )
304 psz_server_addr = psz_parser;
306 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
311 if ( *psz_parser == ':' )
315 psz_server_port = psz_parser;
317 while( *psz_parser && *psz_parser != '/' )
323 if( *psz_parser == '/' )
327 psz_path = psz_parser;
330 /* Convert port format */
331 if( *psz_server_port )
333 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
336 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
337 free( p_input->p_access_data );
343 if( i_server_port == 0 )
348 if( !*psz_server_addr )
350 msg_Err( p_input, "no server given" );
351 free( p_input->p_access_data );
357 if( (psz_proxy = getenv( "http_proxy" )) != NULL && *psz_proxy )
359 /* http://myproxy.mydomain:myport/ */
360 int i_proxy_port = 0;
362 /* Skip the protocol name */
363 while( *psz_proxy && *psz_proxy != ':' )
368 /* Skip the "://" part */
369 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
374 /* Found a proxy name */
377 char *psz_port = psz_proxy;
379 /* Skip the hostname part */
380 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
385 /* Found a port name */
390 /* Replace ':' with '\0' */
395 while( *psz_junk && *psz_junk != '/' )
405 if( *psz_port != '\0' )
407 i_proxy_port = atoi( psz_port );
413 msg_Err( p_input, "http_proxy environment variable is invalid!" );
414 free( p_input->p_access_data );
419 p_access_data->socket_desc.i_type = NETWORK_TCP;
420 p_access_data->socket_desc.psz_server_addr = psz_proxy;
421 p_access_data->socket_desc.i_server_port = i_proxy_port;
423 snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
424 "GET http://%s:%d/%s\r\n HTTP/1.0\r\n",
425 psz_server_addr, i_server_port, psz_path );
429 /* No proxy, direct connection. */
430 p_access_data->socket_desc.i_type = NETWORK_TCP;
431 p_access_data->socket_desc.psz_server_addr = psz_server_addr;
432 p_access_data->socket_desc.i_server_port = i_server_port;
434 snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
435 "GET /%s HTTP/1.1\r\nHost: %s\r\n",
436 psz_path, psz_server_addr );
438 p_access_data->psz_buffer[sizeof(p_access_data->psz_buffer) - 1] = '\0';
440 msg_Dbg( p_input, "opening server=%s port=%d path=%s",
441 psz_server_addr, i_server_port, psz_path );
443 p_input->pf_read = input_FDNetworkRead;
444 p_input->pf_set_program = SetProgram;
445 p_input->pf_set_area = NULL;
446 p_input->pf_seek = Seek;
448 vlc_mutex_lock( &p_input->stream.stream_lock );
449 p_input->stream.b_pace_control = 1;
450 p_input->stream.b_seekable = 1;
451 p_input->stream.p_selected_area->i_tell = 0;
452 p_input->stream.p_selected_area->i_size = 0;
453 p_input->stream.i_method = INPUT_METHOD_NETWORK;
454 vlc_mutex_unlock( &p_input->stream.stream_lock );
457 if( HTTPConnect( p_input, 0 ) )
459 char * psz_pos = strstr(p_access_data->psz_buffer, "HTTP/1.1");
460 p_input->stream.b_seekable = 0;
462 if( HTTPConnect( p_input, 0 ) )
464 free( p_input->p_access_data );
472 /*****************************************************************************
473 * Close: free unused data structures
474 *****************************************************************************/
475 static void Close( vlc_object_t *p_this )
477 input_thread_t * p_input = (input_thread_t *)p_this;
478 _input_socket_t * p_access_data =
479 (_input_socket_t *)p_input->p_access_data;
481 free( p_access_data->psz_name );
482 input_FDNetworkClose( p_input );
485 /*****************************************************************************
486 * SetProgram: do nothing
487 *****************************************************************************/
488 static int SetProgram( input_thread_t * p_input,
489 pgrm_descriptor_t * p_program )
494 /*****************************************************************************
495 * Seek: close and re-open a connection at the right place
496 *****************************************************************************/
497 static void Seek( input_thread_t * p_input, off_t i_pos )
499 _input_socket_t *p_access_data = (_input_socket_t*)p_input->p_access_data;
500 close( p_access_data->_socket.i_handle );
501 msg_Dbg( p_input, "seeking to position %lld", i_pos );
502 HTTPConnect( p_input, i_pos );