1 /*****************************************************************************
2 * http.c: HTTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: http.c,v 1.11 2002/06/01 12:31:58 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 void input_getfunctions( function_list_t * );
59 static int HTTPOpen ( input_thread_t * );
60 static void HTTPClose ( input_thread_t * );
61 static int HTTPSetProgram ( input_thread_t *, pgrm_descriptor_t * );
62 static void HTTPSeek ( input_thread_t *, off_t );
64 /*****************************************************************************
65 * Build configuration tree.
66 *****************************************************************************/
71 SET_DESCRIPTION( _("HTTP access plug-in") )
72 ADD_CAPABILITY( ACCESS, 0 )
73 ADD_SHORTCUT( "http4" )
74 ADD_SHORTCUT( "http6" )
78 input_getfunctions( &p_module->p_functions->access );
81 MODULE_DEACTIVATE_START
82 MODULE_DEACTIVATE_STOP
84 /*****************************************************************************
85 * Functions exported as capabilities. They are declared as static so that
86 * we don't pollute the namespace too much.
87 *****************************************************************************/
88 static void input_getfunctions( function_list_t * p_function_list )
90 #define input p_function_list->functions.access
91 input.pf_open = HTTPOpen;
92 input.pf_read = input_FDNetworkRead;
93 input.pf_close = HTTPClose;
94 input.pf_set_program = HTTPSetProgram;
95 input.pf_set_area = NULL;
96 input.pf_seek = HTTPSeek;
100 /*****************************************************************************
101 * _input_socket_t: private access plug-in data, modified to add private
103 *****************************************************************************/
104 typedef struct _input_socket_s
106 input_socket_t _socket;
109 network_socket_t socket_desc;
110 char psz_buffer[256];
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 _input_socket_t * p_access_data = p_input->p_access_data;
120 module_t * p_network;
121 char psz_buffer[256];
124 /* Find an appropriate network module */
125 p_network = module_Need( p_input, MODULE_CAPABILITY_NETWORK,
126 p_access_data->psz_network,
127 &p_access_data->socket_desc );
128 if( p_network == NULL )
130 free( p_access_data );
133 module_Unneed( p_network );
135 p_access_data->_socket.i_handle = p_access_data->socket_desc.i_handle;
137 # define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
138 # define HTTP_END "\r\n"
140 snprintf( psz_buffer, sizeof(psz_buffer),
142 "Range: bytes=%lld-\r\n"
143 HTTP_USERAGENT HTTP_END,
144 p_access_data->psz_buffer, i_tell );
145 psz_buffer[sizeof(psz_buffer) - 1] = '\0';
148 if( send( p_access_data->_socket.i_handle, psz_buffer,
149 strlen( psz_buffer ), 0 ) == (-1) )
151 msg_Err( p_input, "cannot send request (%s)", strerror(errno) );
152 input_FDNetworkClose( p_input );
156 /* Prepare the input thread for reading. */
157 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
158 /* FIXME: we shouldn't have to do that ! */
159 p_input->pf_read = input_FDNetworkRead;
161 while( !input_FillBuffer( p_input ) )
163 if( p_input->b_die || p_input->b_error )
165 input_FDNetworkClose( p_input );
170 /* Parse HTTP header. */
171 #define MAX_LINE 1024
174 if( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
176 msg_Err( p_input, "not enough data" );
177 input_FDNetworkClose( p_input );
181 if( psz_parser[0] == '\r' && psz_parser[1] == '\n' )
184 p_input->p_current_data += 2;
188 if( !strncmp( psz_parser, "Content-Length: ",
189 strlen("Content-Length: ") ) )
191 psz_parser += strlen("Content-Length: ");
192 /* FIXME : this won't work for 64-bit lengths */
193 vlc_mutex_lock( &p_input->stream.stream_lock );
194 p_input->stream.p_selected_area->i_size = atoi( psz_parser )
196 vlc_mutex_unlock( &p_input->stream.stream_lock );
199 while( *psz_parser != '\r' && psz_parser < p_input->p_last_data )
203 p_input->p_current_data = psz_parser + 2;
206 if( p_input->stream.p_selected_area->i_size )
208 vlc_mutex_lock( &p_input->stream.stream_lock );
209 p_input->stream.p_selected_area->i_tell = i_tell
210 + (p_input->p_last_data - p_input->p_current_data);
211 p_input->stream.b_seekable = 1;
212 p_input->stream.b_changed = 1;
213 vlc_mutex_unlock( &p_input->stream.stream_lock );
219 /*****************************************************************************
220 * HTTPOpen: parse URL and open the remote file at the beginning
221 *****************************************************************************/
222 static int HTTPOpen( input_thread_t * p_input )
224 _input_socket_t * p_access_data;
225 char * psz_name = strdup(p_input->psz_name);
226 char * psz_parser = psz_name;
227 char * psz_server_addr = "";
228 char * psz_server_port = "";
229 char * psz_path = "";
231 int i_server_port = 0;
233 p_access_data = p_input->p_access_data = malloc( sizeof(_input_socket_t) );
234 if( p_access_data == NULL )
236 msg_Err( p_input, "out of memory" );
241 p_access_data->psz_name = psz_name;
242 p_access_data->psz_network = "";
243 if( config_GetInt( p_input, "ipv4" ) )
245 p_access_data->psz_network = "ipv4";
247 if( config_GetInt( p_input, "ipv6" ) )
249 p_access_data->psz_network = "ipv6";
251 if( *p_input->psz_access )
253 /* Find out which shortcut was used */
254 if( !strncmp( p_input->psz_access, "http6", 6 ) )
256 p_access_data->psz_network = "ipv6";
258 else if( !strncmp( p_input->psz_access, "http4", 6 ) )
260 p_access_data->psz_network = "ipv4";
264 /* Parse psz_name syntax :
265 * //<hostname>[:<port>][/<path>] */
266 while( *psz_parser == '/' )
270 psz_server_addr = psz_parser;
272 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
277 if ( *psz_parser == ':' )
281 psz_server_port = psz_parser;
283 while( *psz_parser && *psz_parser != '/' )
289 if( *psz_parser == '/' )
293 psz_path = psz_parser;
296 /* Convert port format */
297 if( *psz_server_port )
299 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
302 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
303 free( p_input->p_access_data );
309 if( i_server_port == 0 )
314 if( !*psz_server_addr )
316 msg_Err( p_input, "no server given" );
317 free( p_input->p_access_data );
323 if( (psz_proxy = getenv( "http_proxy" )) != NULL && *psz_proxy )
325 /* http://myproxy.mydomain:myport/ */
326 int i_proxy_port = 0;
328 /* Skip the protocol name */
329 while( *psz_proxy && *psz_proxy != ':' )
334 /* Skip the "://" part */
335 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
340 /* Found a proxy name */
343 char *psz_port = psz_proxy;
345 /* Skip the hostname part */
346 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
351 /* Found a port name */
356 /* Replace ':' with '\0' */
361 while( *psz_junk && *psz_junk != '/' )
371 if( *psz_port != '\0' )
373 i_proxy_port = atoi( psz_port );
379 msg_Err( p_input, "http_proxy environment variable is invalid!" );
380 free( p_input->p_access_data );
385 p_access_data->socket_desc.i_type = NETWORK_TCP;
386 p_access_data->socket_desc.psz_server_addr = psz_proxy;
387 p_access_data->socket_desc.i_server_port = i_proxy_port;
389 snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
390 "GET http://%s:%d/%s HTTP/1.1\r\n",
391 psz_server_addr, i_server_port, psz_path );
395 /* No proxy, direct connection. */
396 p_access_data->socket_desc.i_type = NETWORK_TCP;
397 p_access_data->socket_desc.psz_server_addr = psz_server_addr;
398 p_access_data->socket_desc.i_server_port = i_server_port;
400 snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
401 "GET /%s HTTP/1.1\r\nHost: %s\r\n",
402 psz_path, psz_server_addr );
404 p_access_data->psz_buffer[sizeof(p_access_data->psz_buffer) - 1] = '\0';
406 msg_Dbg( p_input, "opening server=%s port=%d path=%s",
407 psz_server_addr, i_server_port, psz_path );
409 vlc_mutex_lock( &p_input->stream.stream_lock );
410 p_input->stream.b_pace_control = 1;
411 p_input->stream.b_seekable = 0;
412 p_input->stream.p_selected_area->i_tell = 0;
413 p_input->stream.p_selected_area->i_size = 0;
414 p_input->stream.i_method = INPUT_METHOD_NETWORK;
415 vlc_mutex_unlock( &p_input->stream.stream_lock );
418 return( HTTPConnect( p_input, 0 ) );
421 /*****************************************************************************
422 * HTTPClose: free unused data structures
423 *****************************************************************************/
424 static void HTTPClose( input_thread_t * p_input )
426 input_FDNetworkClose( p_input );
429 /*****************************************************************************
430 * HTTPSetProgram: do nothing
431 *****************************************************************************/
432 static int HTTPSetProgram( input_thread_t * p_input,
433 pgrm_descriptor_t * p_program )
438 /*****************************************************************************
439 * HTTPSeek: close and re-open a connection at the right place
440 *****************************************************************************/
441 static void HTTPSeek( input_thread_t * p_input, off_t i_pos )
443 _input_socket_t * p_access_data = p_input->p_access_data;
444 close( p_access_data->_socket.i_handle );
445 msg_Dbg( p_input, "seeking to position %lld", i_pos );
446 HTTPConnect( p_input, i_pos );