1 /*****************************************************************************
2 * http.c: HTTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: http.c,v 1.2 2002/08/07 00:29:36 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=%lld-\r\n"
121 HTTP_USERAGENT HTTP_END,
122 p_access_data->psz_buffer, i_tell );
126 snprintf( psz_buffer, sizeof(psz_buffer),
128 HTTP_USERAGENT HTTP_END,
129 p_access_data->psz_buffer );
131 psz_buffer[sizeof(psz_buffer) - 1] = '\0';
134 if( send( p_access_data->_socket.i_handle, psz_buffer,
135 strlen( psz_buffer ), 0 ) == (-1) )
137 msg_Err( p_input, "cannot send request (%s)", strerror(errno) );
138 input_FDNetworkClose( p_input );
142 /* Prepare the input thread for reading. */
143 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
145 /* FIXME: we shouldn't have to do that ! It's UGLY but mandatory because
146 * input_FillBuffer assumes p_input->pf_read exists */
147 p_input->pf_read = input_FDNetworkRead;
149 while( !input_FillBuffer( p_input ) )
151 if( p_input->b_die || p_input->b_error )
153 input_FDNetworkClose( p_input );
158 /* Parse HTTP header. */
159 #define MAX_LINE 1024
161 /* get the returncode */
162 if( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
164 msg_Err( p_input, "not enough data" );
165 input_FDNetworkClose( p_input );
169 if( !strncmp( psz_parser, "HTTP/1.",
170 strlen("HTTP/1.") ) )
172 psz_parser += strlen("HTTP 1.") + 2;
173 i_returncode = atoi( psz_parser );
174 msg_Dbg( p_input, "HTTP server replied: %i", i_returncode );
176 for ( i = 0; psz_parser[i] != '\r' || psz_parser[i+1] != '\n'; i++ )
180 psz_return_alpha = malloc( i + 1 );
181 memcpy( psz_return_alpha, psz_parser, i );
182 psz_return_alpha[i] = '\0';
186 msg_Err( p_input, "invalid http reply" );
190 if ( i_returncode >= 400 ) /* something is wrong */
192 msg_Err( p_input, "%i %s", i_returncode,
199 if( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
201 msg_Err( p_input, "not enough data" );
202 input_FDNetworkClose( p_input );
206 if( psz_parser[0] == '\r' && psz_parser[1] == '\n' )
209 p_input->p_current_data += 2;
213 if( !strncmp( psz_parser, "Content-Length: ",
214 strlen("Content-Length: ") ) )
216 psz_parser += strlen("Content-Length: ");
217 vlc_mutex_lock( &p_input->stream.stream_lock );
219 p_input->stream.p_selected_area->i_size = atoll( psz_parser )
222 /* FIXME : this won't work for 64-bit lengths */
223 p_input->stream.p_selected_area->i_size = atoi( psz_parser )
226 vlc_mutex_unlock( &p_input->stream.stream_lock );
229 while( *psz_parser != '\r' && psz_parser < p_input->p_last_data )
233 p_input->p_current_data = psz_parser + 2;
236 if( p_input->stream.p_selected_area->i_size )
238 vlc_mutex_lock( &p_input->stream.stream_lock );
239 p_input->stream.p_selected_area->i_tell = i_tell
240 + (p_input->p_last_data - p_input->p_current_data);
241 p_input->stream.b_seekable = 1;
242 p_input->stream.b_changed = 1;
243 vlc_mutex_unlock( &p_input->stream.stream_lock );
249 /*****************************************************************************
250 * Open: parse URL and open the remote file at the beginning
251 *****************************************************************************/
252 static int Open( vlc_object_t *p_this )
254 input_thread_t * p_input = (input_thread_t *)p_this;
255 _input_socket_t * p_access_data;
256 char * psz_name = strdup(p_input->psz_name);
257 char * psz_parser = psz_name;
258 char * psz_server_addr = "";
259 char * psz_server_port = "";
260 char * psz_path = "";
262 int i_server_port = 0;
264 p_access_data = malloc( sizeof(_input_socket_t) );
265 p_input->p_access_data = (access_sys_t *)p_access_data;
266 if( p_access_data == NULL )
268 msg_Err( p_input, "out of memory" );
273 p_access_data->psz_name = psz_name;
274 p_access_data->psz_network = "";
275 if( config_GetInt( p_input, "ipv4" ) )
277 p_access_data->psz_network = "ipv4";
279 if( config_GetInt( p_input, "ipv6" ) )
281 p_access_data->psz_network = "ipv6";
283 if( *p_input->psz_access )
285 /* Find out which shortcut was used */
286 if( !strncmp( p_input->psz_access, "http6", 6 ) )
288 p_access_data->psz_network = "ipv6";
290 else if( !strncmp( p_input->psz_access, "http4", 6 ) )
292 p_access_data->psz_network = "ipv4";
296 /* Parse psz_name syntax :
297 * //<hostname>[:<port>][/<path>] */
298 while( *psz_parser == '/' )
302 psz_server_addr = psz_parser;
304 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
309 if ( *psz_parser == ':' )
313 psz_server_port = psz_parser;
315 while( *psz_parser && *psz_parser != '/' )
321 if( *psz_parser == '/' )
325 psz_path = psz_parser;
328 /* Convert port format */
329 if( *psz_server_port )
331 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
334 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
335 free( p_input->p_access_data );
341 if( i_server_port == 0 )
346 if( !*psz_server_addr )
348 msg_Err( p_input, "no server given" );
349 free( p_input->p_access_data );
355 if( (psz_proxy = getenv( "http_proxy" )) != NULL && *psz_proxy )
357 /* http://myproxy.mydomain:myport/ */
358 int i_proxy_port = 0;
360 /* Skip the protocol name */
361 while( *psz_proxy && *psz_proxy != ':' )
366 /* Skip the "://" part */
367 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
372 /* Found a proxy name */
375 char *psz_port = psz_proxy;
377 /* Skip the hostname part */
378 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
383 /* Found a port name */
388 /* Replace ':' with '\0' */
393 while( *psz_junk && *psz_junk != '/' )
403 if( *psz_port != '\0' )
405 i_proxy_port = atoi( psz_port );
411 msg_Err( p_input, "http_proxy environment variable is invalid!" );
412 free( p_input->p_access_data );
417 p_access_data->socket_desc.i_type = NETWORK_TCP;
418 p_access_data->socket_desc.psz_server_addr = psz_proxy;
419 p_access_data->socket_desc.i_server_port = i_proxy_port;
421 snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
422 "GET http://%s:%d/%s\r\n HTTP/1.0\r\n",
423 psz_server_addr, i_server_port, psz_path );
427 /* No proxy, direct connection. */
428 p_access_data->socket_desc.i_type = NETWORK_TCP;
429 p_access_data->socket_desc.psz_server_addr = psz_server_addr;
430 p_access_data->socket_desc.i_server_port = i_server_port;
432 snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
433 "GET /%s HTTP/1.1\r\nHost: %s\r\n",
434 psz_path, psz_server_addr );
436 p_access_data->psz_buffer[sizeof(p_access_data->psz_buffer) - 1] = '\0';
438 msg_Dbg( p_input, "opening server=%s port=%d path=%s",
439 psz_server_addr, i_server_port, psz_path );
441 p_input->pf_read = input_FDNetworkRead;
442 p_input->pf_set_program = SetProgram;
443 p_input->pf_set_area = NULL;
444 p_input->pf_seek = Seek;
446 vlc_mutex_lock( &p_input->stream.stream_lock );
447 p_input->stream.b_pace_control = 1;
448 p_input->stream.b_seekable = 1;
449 p_input->stream.p_selected_area->i_tell = 0;
450 p_input->stream.p_selected_area->i_size = 0;
451 p_input->stream.i_method = INPUT_METHOD_NETWORK;
452 vlc_mutex_unlock( &p_input->stream.stream_lock );
455 if( HTTPConnect( p_input, 0 ) )
457 char * psz_pos = strstr(p_access_data->psz_buffer, "HTTP/1.1");
458 p_input->stream.b_seekable = 0;
460 if( HTTPConnect( p_input, 0 ) )
462 free( p_input->p_access_data );
470 /*****************************************************************************
471 * Close: free unused data structures
472 *****************************************************************************/
473 static void Close( vlc_object_t *p_this )
475 input_thread_t * p_input = (input_thread_t *)p_this;
476 _input_socket_t * p_access_data =
477 (_input_socket_t *)p_input->p_access_data;
479 free( p_access_data->psz_name );
480 input_FDNetworkClose( p_input );
483 /*****************************************************************************
484 * SetProgram: do nothing
485 *****************************************************************************/
486 static int SetProgram( input_thread_t * p_input,
487 pgrm_descriptor_t * p_program )
492 /*****************************************************************************
493 * Seek: close and re-open a connection at the right place
494 *****************************************************************************/
495 static void Seek( input_thread_t * p_input, off_t i_pos )
497 _input_socket_t *p_access_data = (_input_socket_t*)p_input->p_access_data;
498 close( p_access_data->_socket.i_handle );
499 msg_Dbg( p_input, "seeking to position %lld", i_pos );
500 HTTPConnect( p_input, i_pos );