]> git.sesse.net Git - vlc/blob - modules/access/http.c
* ALL: the build mechanism now uses automake. See HACKING for more details.
[vlc] / modules / access / http.c
1 /*****************************************************************************
2  * http.c: HTTP access plug-in
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: http.c,v 1.4 2002/09/30 11:05:34 sam Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <fcntl.h>
33
34 #include <vlc/vlc.h>
35 #include <vlc/input.h>
36
37 #ifdef HAVE_UNISTD_H
38 #   include <unistd.h>
39 #elif defined( _MSC_VER ) && defined( _WIN32 )
40 #   include <io.h>
41 #endif
42
43 #ifdef WIN32
44 #   include <winsock2.h>
45 #   include <ws2tcpip.h>
46 #   ifndef IN_MULTICAST
47 #       define IN_MULTICAST(a) IN_CLASSD(a)
48 #   endif
49 #else
50 #   include <sys/socket.h>
51 #endif
52
53 #include "network.h"
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 static int  Open       ( vlc_object_t * );
59 static void Close      ( vlc_object_t * );
60
61 static int  SetProgram ( input_thread_t *, pgrm_descriptor_t * );  
62 static void Seek       ( input_thread_t *, off_t );
63
64 /*****************************************************************************
65  * Module descriptor
66  *****************************************************************************/
67 vlc_module_begin();
68     set_description( _("HTTP access module") );
69     set_capability( "access", 0 );
70     add_shortcut( "http" );
71     add_shortcut( "http4" );
72     add_shortcut( "http6" );
73     set_callbacks( Open, Close );
74 vlc_module_end();
75
76 /*****************************************************************************
77  * _input_socket_t: private access plug-in data, modified to add private
78  *                  fields
79  *****************************************************************************/
80 typedef struct _input_socket_s
81 {
82     input_socket_t      _socket;
83
84     char *              psz_network;
85     network_socket_t    socket_desc;
86     char                psz_buffer[256];
87     char *              psz_name;
88 } _input_socket_t;
89
90 /*****************************************************************************
91  * HTTPConnect: connect to the server and seek to i_tell
92  *****************************************************************************/
93 static int HTTPConnect( input_thread_t * p_input, off_t i_tell )
94 {
95     _input_socket_t *   p_access_data;
96     module_t *          p_network;
97     char                psz_buffer[256];
98     byte_t *            psz_parser;
99     int                 i_returncode, i;
100     char *              psz_return_alpha;
101
102     /* Find an appropriate network module */
103     p_access_data = (_input_socket_t *)p_input->p_access_data;
104     p_input->p_private = (void*) &p_access_data->socket_desc;
105     p_network = module_Need( p_input, "network", p_access_data->psz_network );
106     if( p_network == NULL )
107     {
108         return( -1 );
109     }
110     module_Unneed( p_input, p_network );
111
112     p_access_data->_socket.i_handle = p_access_data->socket_desc.i_handle;
113
114 #   define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
115 #   define HTTP_END       "\r\n"
116  
117     if ( p_input->stream.b_seekable )
118     {
119          snprintf( psz_buffer, sizeof(psz_buffer),
120                    "%s"
121                    "Range: bytes=%d%d-\r\n"
122                    HTTP_USERAGENT HTTP_END,
123                    p_access_data->psz_buffer,
124                    (u32)(i_tell>>32), (u32)i_tell );
125     }
126     else
127     {
128          snprintf( psz_buffer, sizeof(psz_buffer),
129                    "%s"
130                    HTTP_USERAGENT HTTP_END,
131                    p_access_data->psz_buffer );
132     }
133     psz_buffer[sizeof(psz_buffer) - 1] = '\0';
134
135     /* Send GET ... */
136     if( send( p_access_data->_socket.i_handle, psz_buffer,
137                strlen( psz_buffer ), 0 ) == (-1) )
138     {
139         msg_Err( p_input, "cannot send request (%s)", strerror(errno) );
140         input_FDNetworkClose( p_input );
141         return( -1 );
142     }
143
144     /* Prepare the input thread for reading. */ 
145     p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
146
147     /* FIXME: we shouldn't have to do that ! It's UGLY but mandatory because
148      * input_FillBuffer assumes p_input->pf_read exists */
149     p_input->pf_read = input_FDNetworkRead;
150
151     while( !input_FillBuffer( p_input ) )
152     {
153         if( p_input->b_die || p_input->b_error )
154         {
155             input_FDNetworkClose( p_input );
156             return( -1 );
157         }
158     }
159
160     /* Parse HTTP header. */
161 #define MAX_LINE 1024
162
163     /* get the returncode */
164     if( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
165     {
166         msg_Err( p_input, "not enough data" );
167         input_FDNetworkClose( p_input );
168         return( -1 );
169     }
170
171     if( !strncmp( psz_parser, "HTTP/1.",
172                   strlen("HTTP/1.") ) )
173     {
174         psz_parser += strlen("HTTP 1.") + 2;
175         i_returncode = atoi( (char*)psz_parser );
176         msg_Dbg( p_input, "HTTP server replied: %i", i_returncode );
177         psz_parser += 4;
178         for ( i = 0; psz_parser[i] != '\r' || psz_parser[i+1] != '\n'; i++ )
179         {
180             ;
181         }
182         psz_return_alpha = malloc( i + 1 );
183         memcpy( psz_return_alpha, psz_parser, i );
184         psz_return_alpha[i] = '\0';
185     }
186     else
187     {
188         msg_Err( p_input, "invalid http reply" );
189         return -1;
190     }
191     
192     if ( i_returncode >= 400 ) /* something is wrong */
193     {
194         msg_Err( p_input, "%i %s", i_returncode,
195                  psz_return_alpha );
196         return -1;
197     }
198     
199     for( ; ; ) 
200     {
201         if( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
202         {
203             msg_Err( p_input, "not enough data" );
204             input_FDNetworkClose( p_input );
205             return( -1 );
206         }
207
208         if( psz_parser[0] == '\r' && psz_parser[1] == '\n' )
209         {
210             /* End of header. */
211             p_input->p_current_data += 2;
212             break;
213         }
214
215         if( !strncmp( psz_parser, "Content-Length: ",
216                       strlen("Content-Length: ") ) )
217         {
218             psz_parser += strlen("Content-Length: ");
219             vlc_mutex_lock( &p_input->stream.stream_lock );
220 #ifdef HAVE_ATOLL
221             p_input->stream.p_selected_area->i_size = atoll( (char*)psz_parser )
222                                                         + i_tell;
223 #else
224             /* FIXME : this won't work for 64-bit lengths */
225             p_input->stream.p_selected_area->i_size = atoi( (char*)psz_parser )
226                                                         + i_tell;
227 #endif
228             vlc_mutex_unlock( &p_input->stream.stream_lock );
229         }
230
231         while( *psz_parser != '\r' && psz_parser < p_input->p_last_data )
232         {
233             psz_parser++;
234         }
235         p_input->p_current_data = psz_parser + 2;
236     }
237
238     if( p_input->stream.p_selected_area->i_size )
239     {
240         vlc_mutex_lock( &p_input->stream.stream_lock );
241         p_input->stream.p_selected_area->i_tell = i_tell
242             + (p_input->p_last_data - p_input->p_current_data);
243         p_input->stream.b_seekable = 1;
244         p_input->stream.b_changed = 1;
245         vlc_mutex_unlock( &p_input->stream.stream_lock );
246     }
247
248     return( 0 );
249 }
250
251 /*****************************************************************************
252  * Open: parse URL and open the remote file at the beginning
253  *****************************************************************************/
254 static int Open( vlc_object_t *p_this )
255 {
256     input_thread_t *    p_input = (input_thread_t *)p_this;
257     _input_socket_t *   p_access_data;
258     char *              psz_name = strdup(p_input->psz_name);
259     char *              psz_parser = psz_name;
260     char *              psz_server_addr = "";
261     char *              psz_server_port = "";
262     char *              psz_path = "";
263     char *              psz_proxy;
264     int                 i_server_port = 0;
265
266     p_access_data = malloc( sizeof(_input_socket_t) );
267     p_input->p_access_data = (access_sys_t *)p_access_data;
268     if( p_access_data == NULL )
269     {
270         msg_Err( p_input, "out of memory" );
271         free(psz_name);
272         return( -1 );
273     }
274
275     p_access_data->psz_name = psz_name;
276     p_access_data->psz_network = "";
277     if( config_GetInt( p_input, "ipv4" ) )
278     {
279         p_access_data->psz_network = "ipv4";
280     }
281     if( config_GetInt( p_input, "ipv6" ) )
282     {
283         p_access_data->psz_network = "ipv6";
284     }
285     if( *p_input->psz_access )
286     {
287         /* Find out which shortcut was used */
288         if( !strncmp( p_input->psz_access, "http6", 6 ) )
289         {
290             p_access_data->psz_network = "ipv6";
291         }
292         else if( !strncmp( p_input->psz_access, "http4", 6 ) )
293         {
294             p_access_data->psz_network = "ipv4";
295         }
296     }
297
298     /* Parse psz_name syntax :
299      * //<hostname>[:<port>][/<path>] */
300     while( *psz_parser == '/' )
301     {
302         psz_parser++;
303     }
304     psz_server_addr = psz_parser;
305
306     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
307     {
308         psz_parser++;
309     }
310
311     if ( *psz_parser == ':' )
312     {
313         *psz_parser = '\0';
314         psz_parser++;
315         psz_server_port = psz_parser;
316
317         while( *psz_parser && *psz_parser != '/' )
318         {
319             psz_parser++;
320         }
321     }
322
323     if( *psz_parser == '/' )
324     {
325         *psz_parser = '\0';
326         psz_parser++;
327         psz_path = psz_parser;
328     }
329
330     /* Convert port format */
331     if( *psz_server_port )
332     {
333         i_server_port = strtol( psz_server_port, &psz_parser, 10 );
334         if( *psz_parser )
335         {
336             msg_Err( p_input, "cannot parse server port near %s", psz_parser );
337             free( p_input->p_access_data );
338             free( psz_name );
339             return( -1 );
340         }
341     }
342
343     if( i_server_port == 0 )
344     {
345         i_server_port = 80;
346     }
347
348     if( !*psz_server_addr )
349     {
350         msg_Err( p_input, "no server given" );
351         free( p_input->p_access_data );
352         free( psz_name );
353         return( -1 );
354     }
355
356     /* Check proxy */
357     if( (psz_proxy = getenv( "http_proxy" )) != NULL && *psz_proxy )
358     {
359         /* http://myproxy.mydomain:myport/ */
360         int                 i_proxy_port = 0;
361  
362         /* Skip the protocol name */
363         while( *psz_proxy && *psz_proxy != ':' )
364         {
365             psz_proxy++;
366         }
367  
368         /* Skip the "://" part */
369         while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
370         {
371             psz_proxy++;
372         }
373  
374         /* Found a proxy name */
375         if( *psz_proxy )
376         {
377             char *psz_port = psz_proxy;
378  
379             /* Skip the hostname part */
380             while( *psz_port && *psz_port != ':' && *psz_port != '/' )
381             {
382                 psz_port++;
383             }
384  
385             /* Found a port name */
386             if( *psz_port )
387             {
388                 char * psz_junk;
389  
390                 /* Replace ':' with '\0' */
391                 *psz_port = '\0';
392                 psz_port++;
393  
394                 psz_junk = psz_port;
395                 while( *psz_junk && *psz_junk != '/' )
396                 {
397                     psz_junk++;
398                 }
399  
400                 if( *psz_junk )
401                 {
402                     *psz_junk = '\0';
403                 }
404  
405                 if( *psz_port != '\0' )
406                 {
407                     i_proxy_port = atoi( psz_port );
408                 }
409             }
410         }
411         else
412         {
413             msg_Err( p_input, "http_proxy environment variable is invalid!" );
414             free( p_input->p_access_data );
415             free( psz_name );
416             return( -1 );
417         }
418
419         p_access_data->socket_desc.i_type = NETWORK_TCP;
420         p_access_data->socket_desc.psz_server_addr = psz_proxy;
421         p_access_data->socket_desc.i_server_port = i_proxy_port;
422
423         snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
424                   "GET http://%s:%d/%s\r\n HTTP/1.0\r\n",
425                   psz_server_addr, i_server_port, psz_path );
426     }
427     else
428     {
429         /* No proxy, direct connection. */
430         p_access_data->socket_desc.i_type = NETWORK_TCP;
431         p_access_data->socket_desc.psz_server_addr = psz_server_addr;
432         p_access_data->socket_desc.i_server_port = i_server_port;
433
434         snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
435                   "GET /%s HTTP/1.1\r\nHost: %s\r\n",
436                   psz_path, psz_server_addr );
437     }
438     p_access_data->psz_buffer[sizeof(p_access_data->psz_buffer) - 1] = '\0';
439
440     msg_Dbg( p_input, "opening server=%s port=%d path=%s",
441                       psz_server_addr, i_server_port, psz_path );
442
443     p_input->pf_read = input_FDNetworkRead;
444     p_input->pf_set_program = SetProgram;
445     p_input->pf_set_area = NULL;
446     p_input->pf_seek = Seek;
447
448     vlc_mutex_lock( &p_input->stream.stream_lock );
449     p_input->stream.b_pace_control = 1;
450     p_input->stream.b_seekable = 1;
451     p_input->stream.p_selected_area->i_tell = 0;
452     p_input->stream.p_selected_area->i_size = 0;
453     p_input->stream.i_method = INPUT_METHOD_NETWORK;
454     vlc_mutex_unlock( &p_input->stream.stream_lock );
455     p_input->i_mtu = 0;
456  
457     if( HTTPConnect( p_input, 0 ) )
458     {
459         char * psz_pos = strstr(p_access_data->psz_buffer, "HTTP/1.1");
460         p_input->stream.b_seekable = 0;
461         psz_pos[7] = '0';
462         if( HTTPConnect( p_input, 0 ) )
463         {
464             free( p_input->p_access_data );
465             free( psz_name );
466             return( -1 );
467         }
468     }
469     return 0;
470 }
471
472 /*****************************************************************************
473  * Close: free unused data structures
474  *****************************************************************************/
475 static void Close( vlc_object_t *p_this )
476 {
477     input_thread_t *  p_input = (input_thread_t *)p_this;
478     _input_socket_t * p_access_data = 
479         (_input_socket_t *)p_input->p_access_data;
480
481     free( p_access_data->psz_name );
482     input_FDNetworkClose( p_input );
483 }
484
485 /*****************************************************************************
486  * SetProgram: do nothing
487  *****************************************************************************/
488 static int SetProgram( input_thread_t * p_input,
489                        pgrm_descriptor_t * p_program )
490 {
491     return( 0 );
492 }
493
494 /*****************************************************************************
495  * Seek: close and re-open a connection at the right place
496  *****************************************************************************/
497 static void Seek( input_thread_t * p_input, off_t i_pos )
498 {
499     _input_socket_t *p_access_data = (_input_socket_t*)p_input->p_access_data;
500     close( p_access_data->_socket.i_handle );
501     msg_Dbg( p_input, "seeking to position %lld", i_pos );
502     HTTPConnect( p_input, i_pos );
503 }
504