1 /*****************************************************************************
2 * http.c: HTTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: http.c,v 1.3 2002/08/08 00:35:10 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( "http4" );
71 add_shortcut( "http6" );
72 set_callbacks( Open, Close );
75 /*****************************************************************************
76 * _input_socket_t: private access plug-in data, modified to add private
78 *****************************************************************************/
79 typedef struct _input_socket_s
81 input_socket_t _socket;
84 network_socket_t socket_desc;
89 /*****************************************************************************
90 * HTTPConnect: connect to the server and seek to i_tell
91 *****************************************************************************/
92 static int HTTPConnect( input_thread_t * p_input, off_t i_tell )
94 _input_socket_t * p_access_data;
99 char * psz_return_alpha;
101 /* Find an appropriate network module */
102 p_access_data = (_input_socket_t *)p_input->p_access_data;
103 p_input->p_private = (void*) &p_access_data->socket_desc;
104 p_network = module_Need( p_input, "network", p_access_data->psz_network );
105 if( p_network == NULL )
109 module_Unneed( p_input, p_network );
111 p_access_data->_socket.i_handle = p_access_data->socket_desc.i_handle;
113 # define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
114 # define HTTP_END "\r\n"
116 if ( p_input->stream.b_seekable )
118 snprintf( psz_buffer, sizeof(psz_buffer),
120 "Range: bytes=%d%d-\r\n"
121 HTTP_USERAGENT HTTP_END,
122 p_access_data->psz_buffer,
123 (u32)(i_tell>>32), (u32)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( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
165 msg_Err( p_input, "not enough data" );
166 input_FDNetworkClose( p_input );
170 if( !strncmp( psz_parser, "HTTP/1.",
171 strlen("HTTP/1.") ) )
173 psz_parser += strlen("HTTP 1.") + 2;
174 i_returncode = atoi( (char*)psz_parser );
175 msg_Dbg( p_input, "HTTP server replied: %i", i_returncode );
177 for ( i = 0; psz_parser[i] != '\r' || psz_parser[i+1] != '\n'; i++ )
181 psz_return_alpha = malloc( i + 1 );
182 memcpy( psz_return_alpha, psz_parser, i );
183 psz_return_alpha[i] = '\0';
187 msg_Err( p_input, "invalid http reply" );
191 if ( i_returncode >= 400 ) /* something is wrong */
193 msg_Err( p_input, "%i %s", i_returncode,
200 if( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
202 msg_Err( p_input, "not enough data" );
203 input_FDNetworkClose( p_input );
207 if( psz_parser[0] == '\r' && psz_parser[1] == '\n' )
210 p_input->p_current_data += 2;
214 if( !strncmp( psz_parser, "Content-Length: ",
215 strlen("Content-Length: ") ) )
217 psz_parser += strlen("Content-Length: ");
218 vlc_mutex_lock( &p_input->stream.stream_lock );
220 p_input->stream.p_selected_area->i_size = atoll( (char*)psz_parser )
223 /* FIXME : this won't work for 64-bit lengths */
224 p_input->stream.p_selected_area->i_size = atoi( (char*)psz_parser )
227 vlc_mutex_unlock( &p_input->stream.stream_lock );
230 while( *psz_parser != '\r' && psz_parser < p_input->p_last_data )
234 p_input->p_current_data = psz_parser + 2;
237 if( p_input->stream.p_selected_area->i_size )
239 vlc_mutex_lock( &p_input->stream.stream_lock );
240 p_input->stream.p_selected_area->i_tell = i_tell
241 + (p_input->p_last_data - p_input->p_current_data);
242 p_input->stream.b_seekable = 1;
243 p_input->stream.b_changed = 1;
244 vlc_mutex_unlock( &p_input->stream.stream_lock );
250 /*****************************************************************************
251 * Open: parse URL and open the remote file at the beginning
252 *****************************************************************************/
253 static int Open( vlc_object_t *p_this )
255 input_thread_t * p_input = (input_thread_t *)p_this;
256 _input_socket_t * p_access_data;
257 char * psz_name = strdup(p_input->psz_name);
258 char * psz_parser = psz_name;
259 char * psz_server_addr = "";
260 char * psz_server_port = "";
261 char * psz_path = "";
263 int i_server_port = 0;
265 p_access_data = malloc( sizeof(_input_socket_t) );
266 p_input->p_access_data = (access_sys_t *)p_access_data;
267 if( p_access_data == NULL )
269 msg_Err( p_input, "out of memory" );
274 p_access_data->psz_name = psz_name;
275 p_access_data->psz_network = "";
276 if( config_GetInt( p_input, "ipv4" ) )
278 p_access_data->psz_network = "ipv4";
280 if( config_GetInt( p_input, "ipv6" ) )
282 p_access_data->psz_network = "ipv6";
284 if( *p_input->psz_access )
286 /* Find out which shortcut was used */
287 if( !strncmp( p_input->psz_access, "http6", 6 ) )
289 p_access_data->psz_network = "ipv6";
291 else if( !strncmp( p_input->psz_access, "http4", 6 ) )
293 p_access_data->psz_network = "ipv4";
297 /* Parse psz_name syntax :
298 * //<hostname>[:<port>][/<path>] */
299 while( *psz_parser == '/' )
303 psz_server_addr = psz_parser;
305 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
310 if ( *psz_parser == ':' )
314 psz_server_port = psz_parser;
316 while( *psz_parser && *psz_parser != '/' )
322 if( *psz_parser == '/' )
326 psz_path = psz_parser;
329 /* Convert port format */
330 if( *psz_server_port )
332 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
335 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
336 free( p_input->p_access_data );
342 if( i_server_port == 0 )
347 if( !*psz_server_addr )
349 msg_Err( p_input, "no server given" );
350 free( p_input->p_access_data );
356 if( (psz_proxy = getenv( "http_proxy" )) != NULL && *psz_proxy )
358 /* http://myproxy.mydomain:myport/ */
359 int i_proxy_port = 0;
361 /* Skip the protocol name */
362 while( *psz_proxy && *psz_proxy != ':' )
367 /* Skip the "://" part */
368 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
373 /* Found a proxy name */
376 char *psz_port = psz_proxy;
378 /* Skip the hostname part */
379 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
384 /* Found a port name */
389 /* Replace ':' with '\0' */
394 while( *psz_junk && *psz_junk != '/' )
404 if( *psz_port != '\0' )
406 i_proxy_port = atoi( psz_port );
412 msg_Err( p_input, "http_proxy environment variable is invalid!" );
413 free( p_input->p_access_data );
418 p_access_data->socket_desc.i_type = NETWORK_TCP;
419 p_access_data->socket_desc.psz_server_addr = psz_proxy;
420 p_access_data->socket_desc.i_server_port = i_proxy_port;
422 snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
423 "GET http://%s:%d/%s\r\n HTTP/1.0\r\n",
424 psz_server_addr, i_server_port, psz_path );
428 /* No proxy, direct connection. */
429 p_access_data->socket_desc.i_type = NETWORK_TCP;
430 p_access_data->socket_desc.psz_server_addr = psz_server_addr;
431 p_access_data->socket_desc.i_server_port = i_server_port;
433 snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
434 "GET /%s HTTP/1.1\r\nHost: %s\r\n",
435 psz_path, psz_server_addr );
437 p_access_data->psz_buffer[sizeof(p_access_data->psz_buffer) - 1] = '\0';
439 msg_Dbg( p_input, "opening server=%s port=%d path=%s",
440 psz_server_addr, i_server_port, psz_path );
442 p_input->pf_read = input_FDNetworkRead;
443 p_input->pf_set_program = SetProgram;
444 p_input->pf_set_area = NULL;
445 p_input->pf_seek = Seek;
447 vlc_mutex_lock( &p_input->stream.stream_lock );
448 p_input->stream.b_pace_control = 1;
449 p_input->stream.b_seekable = 1;
450 p_input->stream.p_selected_area->i_tell = 0;
451 p_input->stream.p_selected_area->i_size = 0;
452 p_input->stream.i_method = INPUT_METHOD_NETWORK;
453 vlc_mutex_unlock( &p_input->stream.stream_lock );
456 if( HTTPConnect( p_input, 0 ) )
458 char * psz_pos = strstr(p_access_data->psz_buffer, "HTTP/1.1");
459 p_input->stream.b_seekable = 0;
461 if( HTTPConnect( p_input, 0 ) )
463 free( p_input->p_access_data );
471 /*****************************************************************************
472 * Close: free unused data structures
473 *****************************************************************************/
474 static void Close( vlc_object_t *p_this )
476 input_thread_t * p_input = (input_thread_t *)p_this;
477 _input_socket_t * p_access_data =
478 (_input_socket_t *)p_input->p_access_data;
480 free( p_access_data->psz_name );
481 input_FDNetworkClose( p_input );
484 /*****************************************************************************
485 * SetProgram: do nothing
486 *****************************************************************************/
487 static int SetProgram( input_thread_t * p_input,
488 pgrm_descriptor_t * p_program )
493 /*****************************************************************************
494 * Seek: close and re-open a connection at the right place
495 *****************************************************************************/
496 static void Seek( input_thread_t * p_input, off_t i_pos )
498 _input_socket_t *p_access_data = (_input_socket_t*)p_input->p_access_data;
499 close( p_access_data->_socket.i_handle );
500 msg_Dbg( p_input, "seeking to position %lld", i_pos );
501 HTTPConnect( p_input, i_pos );