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