1 /*****************************************************************************
2 * http.c: HTTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: http.c,v 1.1 2002/08/04 17:23:41 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 = p_input->p_access_data;
99 char * psz_return_alpha;
101 /* Find an appropriate network module */
102 p_input->p_private = (void*) &p_access_data->socket_desc;
103 p_network = module_Need( p_input, "network", p_access_data->psz_network );
104 if( p_network == NULL )
108 module_Unneed( p_input, p_network );
110 p_access_data->_socket.i_handle = p_access_data->socket_desc.i_handle;
112 # define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
113 # define HTTP_END "\r\n"
115 if ( p_input->stream.b_seekable )
117 snprintf( psz_buffer, sizeof(psz_buffer),
119 "Range: bytes=%lld-\r\n"
120 HTTP_USERAGENT HTTP_END,
121 p_access_data->psz_buffer, i_tell );
125 snprintf( psz_buffer, sizeof(psz_buffer),
127 HTTP_USERAGENT HTTP_END,
128 p_access_data->psz_buffer );
130 psz_buffer[sizeof(psz_buffer) - 1] = '\0';
133 if( send( p_access_data->_socket.i_handle, psz_buffer,
134 strlen( psz_buffer ), 0 ) == (-1) )
136 msg_Err( p_input, "cannot send request (%s)", strerror(errno) );
137 input_FDNetworkClose( p_input );
141 /* Prepare the input thread for reading. */
142 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
144 /* FIXME: we shouldn't have to do that ! It's UGLY but mandatory because
145 * input_FillBuffer assumes p_input->pf_read exists */
146 p_input->pf_read = input_FDNetworkRead;
148 while( !input_FillBuffer( p_input ) )
150 if( p_input->b_die || p_input->b_error )
152 input_FDNetworkClose( p_input );
157 /* Parse HTTP header. */
158 #define MAX_LINE 1024
160 /* get the returncode */
161 if( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
163 msg_Err( p_input, "not enough data" );
164 input_FDNetworkClose( p_input );
168 if( !strncmp( psz_parser, "HTTP/1.",
169 strlen("HTTP/1.") ) )
171 psz_parser += strlen("HTTP 1.") + 2;
172 i_returncode = atoi( psz_parser );
173 msg_Dbg( p_input, "HTTP server replied: %i", i_returncode );
175 for ( i = 0; psz_parser[i] != '\r' || psz_parser[i+1] != '\n'; i++ )
179 psz_return_alpha = malloc( i + 1 );
180 memcpy( psz_return_alpha, psz_parser, i );
181 psz_return_alpha[i] = '\0';
185 msg_Err( p_input, "invalid http reply" );
189 if ( i_returncode >= 400 ) /* something is wrong */
191 msg_Err( p_input, "%i %s", i_returncode,
198 if( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
200 msg_Err( p_input, "not enough data" );
201 input_FDNetworkClose( p_input );
205 if( psz_parser[0] == '\r' && psz_parser[1] == '\n' )
208 p_input->p_current_data += 2;
212 if( !strncmp( psz_parser, "Content-Length: ",
213 strlen("Content-Length: ") ) )
215 psz_parser += strlen("Content-Length: ");
216 vlc_mutex_lock( &p_input->stream.stream_lock );
218 p_input->stream.p_selected_area->i_size = atoll( psz_parser )
221 /* FIXME : this won't work for 64-bit lengths */
222 p_input->stream.p_selected_area->i_size = atoi( psz_parser )
225 vlc_mutex_unlock( &p_input->stream.stream_lock );
228 while( *psz_parser != '\r' && psz_parser < p_input->p_last_data )
232 p_input->p_current_data = psz_parser + 2;
235 if( p_input->stream.p_selected_area->i_size )
237 vlc_mutex_lock( &p_input->stream.stream_lock );
238 p_input->stream.p_selected_area->i_tell = i_tell
239 + (p_input->p_last_data - p_input->p_current_data);
240 p_input->stream.b_seekable = 1;
241 p_input->stream.b_changed = 1;
242 vlc_mutex_unlock( &p_input->stream.stream_lock );
248 /*****************************************************************************
249 * Open: parse URL and open the remote file at the beginning
250 *****************************************************************************/
251 static int Open( vlc_object_t *p_this )
253 input_thread_t * p_input = (input_thread_t *)p_this;
254 _input_socket_t * p_access_data;
255 char * psz_name = strdup(p_input->psz_name);
256 char * psz_parser = psz_name;
257 char * psz_server_addr = "";
258 char * psz_server_port = "";
259 char * psz_path = "";
261 int i_server_port = 0;
263 p_access_data = p_input->p_access_data = malloc( sizeof(_input_socket_t) );
264 if( p_access_data == NULL )
266 msg_Err( p_input, "out of memory" );
271 p_access_data->psz_name = psz_name;
272 p_access_data->psz_network = "";
273 if( config_GetInt( p_input, "ipv4" ) )
275 p_access_data->psz_network = "ipv4";
277 if( config_GetInt( p_input, "ipv6" ) )
279 p_access_data->psz_network = "ipv6";
281 if( *p_input->psz_access )
283 /* Find out which shortcut was used */
284 if( !strncmp( p_input->psz_access, "http6", 6 ) )
286 p_access_data->psz_network = "ipv6";
288 else if( !strncmp( p_input->psz_access, "http4", 6 ) )
290 p_access_data->psz_network = "ipv4";
294 /* Parse psz_name syntax :
295 * //<hostname>[:<port>][/<path>] */
296 while( *psz_parser == '/' )
300 psz_server_addr = psz_parser;
302 while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
307 if ( *psz_parser == ':' )
311 psz_server_port = psz_parser;
313 while( *psz_parser && *psz_parser != '/' )
319 if( *psz_parser == '/' )
323 psz_path = psz_parser;
326 /* Convert port format */
327 if( *psz_server_port )
329 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
332 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
333 free( p_input->p_access_data );
339 if( i_server_port == 0 )
344 if( !*psz_server_addr )
346 msg_Err( p_input, "no server given" );
347 free( p_input->p_access_data );
353 if( (psz_proxy = getenv( "http_proxy" )) != NULL && *psz_proxy )
355 /* http://myproxy.mydomain:myport/ */
356 int i_proxy_port = 0;
358 /* Skip the protocol name */
359 while( *psz_proxy && *psz_proxy != ':' )
364 /* Skip the "://" part */
365 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
370 /* Found a proxy name */
373 char *psz_port = psz_proxy;
375 /* Skip the hostname part */
376 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
381 /* Found a port name */
386 /* Replace ':' with '\0' */
391 while( *psz_junk && *psz_junk != '/' )
401 if( *psz_port != '\0' )
403 i_proxy_port = atoi( psz_port );
409 msg_Err( p_input, "http_proxy environment variable is invalid!" );
410 free( p_input->p_access_data );
415 p_access_data->socket_desc.i_type = NETWORK_TCP;
416 p_access_data->socket_desc.psz_server_addr = psz_proxy;
417 p_access_data->socket_desc.i_server_port = i_proxy_port;
419 snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
420 "GET http://%s:%d/%s\r\n HTTP/1.0\r\n",
421 psz_server_addr, i_server_port, psz_path );
425 /* No proxy, direct connection. */
426 p_access_data->socket_desc.i_type = NETWORK_TCP;
427 p_access_data->socket_desc.psz_server_addr = psz_server_addr;
428 p_access_data->socket_desc.i_server_port = i_server_port;
430 snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
431 "GET /%s HTTP/1.1\r\nHost: %s\r\n",
432 psz_path, psz_server_addr );
434 p_access_data->psz_buffer[sizeof(p_access_data->psz_buffer) - 1] = '\0';
436 msg_Dbg( p_input, "opening server=%s port=%d path=%s",
437 psz_server_addr, i_server_port, psz_path );
439 p_input->pf_read = input_FDNetworkRead;
440 p_input->pf_set_program = SetProgram;
441 p_input->pf_set_area = NULL;
442 p_input->pf_seek = Seek;
444 vlc_mutex_lock( &p_input->stream.stream_lock );
445 p_input->stream.b_pace_control = 1;
446 p_input->stream.b_seekable = 1;
447 p_input->stream.p_selected_area->i_tell = 0;
448 p_input->stream.p_selected_area->i_size = 0;
449 p_input->stream.i_method = INPUT_METHOD_NETWORK;
450 vlc_mutex_unlock( &p_input->stream.stream_lock );
453 if( HTTPConnect( p_input, 0 ) )
455 char * psz_pos = strstr(p_access_data->psz_buffer, "HTTP/1.1");
456 p_input->stream.b_seekable = 0;
458 if( HTTPConnect( p_input, 0 ) )
460 free( p_input->p_access_data );
468 /*****************************************************************************
469 * Close: free unused data structures
470 *****************************************************************************/
471 static void Close( vlc_object_t *p_this )
473 input_thread_t * p_input = (input_thread_t *)p_this;
474 _input_socket_t * p_access_data =
475 (_input_socket_t *)p_input->p_access_data;
477 free( p_access_data->psz_name );
478 input_FDNetworkClose( p_input );
481 /*****************************************************************************
482 * SetProgram: do nothing
483 *****************************************************************************/
484 static int SetProgram( input_thread_t * p_input,
485 pgrm_descriptor_t * p_program )
490 /*****************************************************************************
491 * Seek: close and re-open a connection at the right place
492 *****************************************************************************/
493 static void Seek( input_thread_t * p_input, off_t i_pos )
495 _input_socket_t * p_access_data = p_input->p_access_data;
496 close( p_access_data->_socket.i_handle );
497 msg_Dbg( p_input, "seeking to position %lld", i_pos );
498 HTTPConnect( p_input, i_pos );