]> git.sesse.net Git - vlc/blob - modules/access/http.c
* ./modules/demux/mpeg/system.c: added a helper plugin for MPEG-related
[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.2 2002/08/07 00:29:36 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( "http4" );
71     add_shortcut( "http6" );
72     set_callbacks( Open, Close );
73 vlc_module_end();
74
75 /*****************************************************************************
76  * _input_socket_t: private access plug-in data, modified to add private
77  *                  fields
78  *****************************************************************************/
79 typedef struct _input_socket_s
80 {
81     input_socket_t      _socket;
82
83     char *              psz_network;
84     network_socket_t    socket_desc;
85     char                psz_buffer[256];
86     char *              psz_name;
87 } _input_socket_t;
88
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 )
93 {
94     _input_socket_t *   p_access_data;
95     module_t *          p_network;
96     char                psz_buffer[256];
97     byte_t *            psz_parser;
98     int                 i_returncode, i;
99     char *              psz_return_alpha;
100
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 )
106     {
107         return( -1 );
108     }
109     module_Unneed( p_input, p_network );
110
111     p_access_data->_socket.i_handle = p_access_data->socket_desc.i_handle;
112
113 #   define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
114 #   define HTTP_END       "\r\n"
115  
116     if ( p_input->stream.b_seekable )
117     {
118          snprintf( psz_buffer, sizeof(psz_buffer),
119                    "%s"
120                    "Range: bytes=%lld-\r\n"
121                    HTTP_USERAGENT HTTP_END,
122                    p_access_data->psz_buffer, i_tell );
123     }
124     else
125     {
126          snprintf( psz_buffer, sizeof(psz_buffer),
127                    "%s"
128                    HTTP_USERAGENT HTTP_END,
129                    p_access_data->psz_buffer );
130     }
131     psz_buffer[sizeof(psz_buffer) - 1] = '\0';
132
133     /* Send GET ... */
134     if( send( p_access_data->_socket.i_handle, psz_buffer,
135                strlen( psz_buffer ), 0 ) == (-1) )
136     {
137         msg_Err( p_input, "cannot send request (%s)", strerror(errno) );
138         input_FDNetworkClose( p_input );
139         return( -1 );
140     }
141
142     /* Prepare the input thread for reading. */ 
143     p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
144
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;
148
149     while( !input_FillBuffer( p_input ) )
150     {
151         if( p_input->b_die || p_input->b_error )
152         {
153             input_FDNetworkClose( p_input );
154             return( -1 );
155         }
156     }
157
158     /* Parse HTTP header. */
159 #define MAX_LINE 1024
160
161     /* get the returncode */
162     if( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
163     {
164         msg_Err( p_input, "not enough data" );
165         input_FDNetworkClose( p_input );
166         return( -1 );
167     }
168
169     if( !strncmp( psz_parser, "HTTP/1.",
170                   strlen("HTTP/1.") ) )
171     {
172         psz_parser += strlen("HTTP 1.") + 2;
173         i_returncode = atoi( psz_parser );
174         msg_Dbg( p_input, "HTTP server replied: %i", i_returncode );
175         psz_parser += 4;
176         for ( i = 0; psz_parser[i] != '\r' || psz_parser[i+1] != '\n'; i++ )
177         {
178             ;
179         }
180         psz_return_alpha = malloc( i + 1 );
181         memcpy( psz_return_alpha, psz_parser, i );
182         psz_return_alpha[i] = '\0';
183     }
184     else
185     {
186         msg_Err( p_input, "invalid http reply" );
187         return -1;
188     }
189     
190     if ( i_returncode >= 400 ) /* something is wrong */
191     {
192         msg_Err( p_input, "%i %s", i_returncode,
193                  psz_return_alpha );
194         return -1;
195     }
196     
197     for( ; ; ) 
198     {
199         if( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
200         {
201             msg_Err( p_input, "not enough data" );
202             input_FDNetworkClose( p_input );
203             return( -1 );
204         }
205
206         if( psz_parser[0] == '\r' && psz_parser[1] == '\n' )
207         {
208             /* End of header. */
209             p_input->p_current_data += 2;
210             break;
211         }
212
213         if( !strncmp( psz_parser, "Content-Length: ",
214                       strlen("Content-Length: ") ) )
215         {
216             psz_parser += strlen("Content-Length: ");
217             vlc_mutex_lock( &p_input->stream.stream_lock );
218 #ifdef HAVE_ATOLL
219             p_input->stream.p_selected_area->i_size = atoll( psz_parser )
220                                                         + i_tell;
221 #else
222             /* FIXME : this won't work for 64-bit lengths */
223             p_input->stream.p_selected_area->i_size = atoi( psz_parser )
224                                                         + i_tell;
225 #endif
226             vlc_mutex_unlock( &p_input->stream.stream_lock );
227         }
228
229         while( *psz_parser != '\r' && psz_parser < p_input->p_last_data )
230         {
231             psz_parser++;
232         }
233         p_input->p_current_data = psz_parser + 2;
234     }
235
236     if( p_input->stream.p_selected_area->i_size )
237     {
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 );
244     }
245
246     return( 0 );
247 }
248
249 /*****************************************************************************
250  * Open: parse URL and open the remote file at the beginning
251  *****************************************************************************/
252 static int Open( vlc_object_t *p_this )
253 {
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 = "";
261     char *              psz_proxy;
262     int                 i_server_port = 0;
263
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 )
267     {
268         msg_Err( p_input, "out of memory" );
269         free(psz_name);
270         return( -1 );
271     }
272
273     p_access_data->psz_name = psz_name;
274     p_access_data->psz_network = "";
275     if( config_GetInt( p_input, "ipv4" ) )
276     {
277         p_access_data->psz_network = "ipv4";
278     }
279     if( config_GetInt( p_input, "ipv6" ) )
280     {
281         p_access_data->psz_network = "ipv6";
282     }
283     if( *p_input->psz_access )
284     {
285         /* Find out which shortcut was used */
286         if( !strncmp( p_input->psz_access, "http6", 6 ) )
287         {
288             p_access_data->psz_network = "ipv6";
289         }
290         else if( !strncmp( p_input->psz_access, "http4", 6 ) )
291         {
292             p_access_data->psz_network = "ipv4";
293         }
294     }
295
296     /* Parse psz_name syntax :
297      * //<hostname>[:<port>][/<path>] */
298     while( *psz_parser == '/' )
299     {
300         psz_parser++;
301     }
302     psz_server_addr = psz_parser;
303
304     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
305     {
306         psz_parser++;
307     }
308
309     if ( *psz_parser == ':' )
310     {
311         *psz_parser = '\0';
312         psz_parser++;
313         psz_server_port = psz_parser;
314
315         while( *psz_parser && *psz_parser != '/' )
316         {
317             psz_parser++;
318         }
319     }
320
321     if( *psz_parser == '/' )
322     {
323         *psz_parser = '\0';
324         psz_parser++;
325         psz_path = psz_parser;
326     }
327
328     /* Convert port format */
329     if( *psz_server_port )
330     {
331         i_server_port = strtol( psz_server_port, &psz_parser, 10 );
332         if( *psz_parser )
333         {
334             msg_Err( p_input, "cannot parse server port near %s", psz_parser );
335             free( p_input->p_access_data );
336             free( psz_name );
337             return( -1 );
338         }
339     }
340
341     if( i_server_port == 0 )
342     {
343         i_server_port = 80;
344     }
345
346     if( !*psz_server_addr )
347     {
348         msg_Err( p_input, "no server given" );
349         free( p_input->p_access_data );
350         free( psz_name );
351         return( -1 );
352     }
353
354     /* Check proxy */
355     if( (psz_proxy = getenv( "http_proxy" )) != NULL && *psz_proxy )
356     {
357         /* http://myproxy.mydomain:myport/ */
358         int                 i_proxy_port = 0;
359  
360         /* Skip the protocol name */
361         while( *psz_proxy && *psz_proxy != ':' )
362         {
363             psz_proxy++;
364         }
365  
366         /* Skip the "://" part */
367         while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
368         {
369             psz_proxy++;
370         }
371  
372         /* Found a proxy name */
373         if( *psz_proxy )
374         {
375             char *psz_port = psz_proxy;
376  
377             /* Skip the hostname part */
378             while( *psz_port && *psz_port != ':' && *psz_port != '/' )
379             {
380                 psz_port++;
381             }
382  
383             /* Found a port name */
384             if( *psz_port )
385             {
386                 char * psz_junk;
387  
388                 /* Replace ':' with '\0' */
389                 *psz_port = '\0';
390                 psz_port++;
391  
392                 psz_junk = psz_port;
393                 while( *psz_junk && *psz_junk != '/' )
394                 {
395                     psz_junk++;
396                 }
397  
398                 if( *psz_junk )
399                 {
400                     *psz_junk = '\0';
401                 }
402  
403                 if( *psz_port != '\0' )
404                 {
405                     i_proxy_port = atoi( psz_port );
406                 }
407             }
408         }
409         else
410         {
411             msg_Err( p_input, "http_proxy environment variable is invalid!" );
412             free( p_input->p_access_data );
413             free( psz_name );
414             return( -1 );
415         }
416
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;
420
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 );
424     }
425     else
426     {
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;
431
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 );
435     }
436     p_access_data->psz_buffer[sizeof(p_access_data->psz_buffer) - 1] = '\0';
437
438     msg_Dbg( p_input, "opening server=%s port=%d path=%s",
439                       psz_server_addr, i_server_port, psz_path );
440
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;
445
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 );
453     p_input->i_mtu = 0;
454  
455     if( HTTPConnect( p_input, 0 ) )
456     {
457         char * psz_pos = strstr(p_access_data->psz_buffer, "HTTP/1.1");
458         p_input->stream.b_seekable = 0;
459         psz_pos[7] = '0';
460         if( HTTPConnect( p_input, 0 ) )
461         {
462             free( p_input->p_access_data );
463             free( psz_name );
464             return( -1 );
465         }
466     }
467     return 0;
468 }
469
470 /*****************************************************************************
471  * Close: free unused data structures
472  *****************************************************************************/
473 static void Close( vlc_object_t *p_this )
474 {
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;
478
479     free( p_access_data->psz_name );
480     input_FDNetworkClose( p_input );
481 }
482
483 /*****************************************************************************
484  * SetProgram: do nothing
485  *****************************************************************************/
486 static int SetProgram( input_thread_t * p_input,
487                        pgrm_descriptor_t * p_program )
488 {
489     return( 0 );
490 }
491
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 )
496 {
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 );
501 }
502