1 /*****************************************************************************
2 * http.c: HTTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: http.c,v 1.13 2002/06/27 19:05:17 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 if ( p_input->stream.b_seekable )
142 snprintf( psz_buffer, sizeof(psz_buffer),
144 "Range: bytes=%lld-\r\n"
145 HTTP_USERAGENT HTTP_END,
146 p_access_data->psz_buffer, i_tell );
150 snprintf( psz_buffer, sizeof(psz_buffer),
152 HTTP_USERAGENT HTTP_END,
153 p_access_data->psz_buffer );
155 psz_buffer[sizeof(psz_buffer) - 1] = '\0';
158 if( send( p_access_data->_socket.i_handle, psz_buffer,
159 strlen( psz_buffer ), 0 ) == (-1) )
161 msg_Err( p_input, "cannot send request (%s)", strerror(errno) );
162 input_FDNetworkClose( p_input );
166 /* Prepare the input thread for reading. */
167 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
168 /* FIXME: we shouldn't have to do that ! */
169 p_input->pf_read = input_FDNetworkRead;
171 while( !input_FillBuffer( p_input ) )
173 if( p_input->b_die || p_input->b_error )
175 input_FDNetworkClose( p_input );
180 /* Parse HTTP header. */
181 #define MAX_LINE 1024
184 if( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
186 msg_Err( p_input, "not enough data" );
187 input_FDNetworkClose( p_input );
191 if( psz_parser[0] == '\r' && psz_parser[1] == '\n' )
194 p_input->p_current_data += 2;
198 if( !strncmp( psz_parser, "Content-Length: ",
199 strlen("Content-Length: ") ) )
201 psz_parser += strlen("Content-Length: ");
202 vlc_mutex_lock( &p_input->stream.stream_lock );
204 p_input->stream.p_selected_area->i_size = atoll( psz_parser )
207 /* FIXME : this won't work for 64-bit lengths */
208 p_input->stream.p_selected_area->i_size = atoi( psz_parser )
211 vlc_mutex_unlock( &p_input->stream.stream_lock );
214 while( *psz_parser != '\r' && psz_parser < p_input->p_last_data )
218 p_input->p_current_data = psz_parser + 2;
221 if( p_input->stream.p_selected_area->i_size )
223 vlc_mutex_lock( &p_input->stream.stream_lock );
224 p_input->stream.p_selected_area->i_tell = i_tell
225 + (p_input->p_last_data - p_input->p_current_data);
226 p_input->stream.b_seekable = 1;
227 p_input->stream.b_changed = 1;
228 vlc_mutex_unlock( &p_input->stream.stream_lock );
234 /*****************************************************************************
235 * HTTPOpen: parse URL and open the remote file at the beginning
236 *****************************************************************************/
237 static int HTTPOpen( input_thread_t * p_input )
239 _input_socket_t * p_access_data;
240 char * psz_name = strdup(p_input->psz_name);
241 char * psz_parser = psz_name;
242 char * psz_server_addr = "";
243 char * psz_server_port = "";
244 char * psz_path = "";
246 int i_server_port = 0;
248 p_access_data = p_input->p_access_data = malloc( sizeof(_input_socket_t) );
249 if( p_access_data == NULL )
251 msg_Err( p_input, "out of memory" );
256 p_access_data->psz_name = psz_name;
257 p_access_data->psz_network = "";
258 if( config_GetInt( p_input, "ipv4" ) )
260 p_access_data->psz_network = "ipv4";
262 if( config_GetInt( p_input, "ipv6" ) )
264 p_access_data->psz_network = "ipv6";
266 if( *p_input->psz_access )
268 /* Find out which shortcut was used */
269 if( !strncmp( p_input->psz_access, "http6", 6 ) )
271 p_access_data->psz_network = "ipv6";
273 else if( !strncmp( p_input->psz_access, "http4", 6 ) )
275 p_access_data->psz_network = "ipv4";
279 /* Parse psz_name syntax :
280 * //<hostname>[:<port>][/<path>] */
281 while( *psz_parser == '/' )
285 psz_server_addr = psz_parser;
287 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
292 if ( *psz_parser == ':' )
296 psz_server_port = psz_parser;
298 while( *psz_parser && *psz_parser != '/' )
304 if( *psz_parser == '/' )
308 psz_path = psz_parser;
311 /* Convert port format */
312 if( *psz_server_port )
314 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
317 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
318 free( p_input->p_access_data );
324 if( i_server_port == 0 )
329 if( !*psz_server_addr )
331 msg_Err( p_input, "no server given" );
332 free( p_input->p_access_data );
338 if( (psz_proxy = getenv( "http_proxy" )) != NULL && *psz_proxy )
340 /* http://myproxy.mydomain:myport/ */
341 int i_proxy_port = 0;
343 /* Skip the protocol name */
344 while( *psz_proxy && *psz_proxy != ':' )
349 /* Skip the "://" part */
350 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
355 /* Found a proxy name */
358 char *psz_port = psz_proxy;
360 /* Skip the hostname part */
361 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
366 /* Found a port name */
371 /* Replace ':' with '\0' */
376 while( *psz_junk && *psz_junk != '/' )
386 if( *psz_port != '\0' )
388 i_proxy_port = atoi( psz_port );
394 msg_Err( p_input, "http_proxy environment variable is invalid!" );
395 free( p_input->p_access_data );
400 p_access_data->socket_desc.i_type = NETWORK_TCP;
401 p_access_data->socket_desc.psz_server_addr = psz_proxy;
402 p_access_data->socket_desc.i_server_port = i_proxy_port;
404 snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
405 "GET http://%s:%d/%s\r\n HTTP/1.0\r\n",
406 psz_server_addr, i_server_port, psz_path );
410 /* No proxy, direct connection. */
411 p_access_data->socket_desc.i_type = NETWORK_TCP;
412 p_access_data->socket_desc.psz_server_addr = psz_server_addr;
413 p_access_data->socket_desc.i_server_port = i_server_port;
415 snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
416 "GET /%s HTTP/1.1\r\nHost: %s\r\n",
417 psz_path, psz_server_addr );
419 p_access_data->psz_buffer[sizeof(p_access_data->psz_buffer) - 1] = '\0';
421 msg_Dbg( p_input, "opening server=%s port=%d path=%s",
422 psz_server_addr, i_server_port, psz_path );
424 vlc_mutex_lock( &p_input->stream.stream_lock );
425 p_input->stream.b_pace_control = 1;
426 p_input->stream.b_seekable = 1;
427 p_input->stream.p_selected_area->i_tell = 0;
428 p_input->stream.p_selected_area->i_size = 0;
429 p_input->stream.i_method = INPUT_METHOD_NETWORK;
430 vlc_mutex_unlock( &p_input->stream.stream_lock );
433 if( HTTPConnect( p_input, 0 ) )
435 char * psz_pos = strstr(p_access_data->psz_buffer, "HTTP/1.1");
436 p_input->stream.b_seekable = 0;
438 return( HTTPConnect( p_input, 0 ) );
443 /*****************************************************************************
444 * HTTPClose: free unused data structures
445 *****************************************************************************/
446 static void HTTPClose( input_thread_t * p_input )
448 input_FDNetworkClose( p_input );
451 /*****************************************************************************
452 * HTTPSetProgram: do nothing
453 *****************************************************************************/
454 static int HTTPSetProgram( input_thread_t * p_input,
455 pgrm_descriptor_t * p_program )
460 /*****************************************************************************
461 * HTTPSeek: close and re-open a connection at the right place
462 *****************************************************************************/
463 static void HTTPSeek( input_thread_t * p_input, off_t i_pos )
465 _input_socket_t * p_access_data = p_input->p_access_data;
466 close( p_access_data->_socket.i_handle );
467 msg_Dbg( p_input, "seeking to position %lld", i_pos );
468 HTTPConnect( p_input, i_pos );