]> git.sesse.net Git - vlc/blob - modules/access/http.c
* ALL: using "%ll" in printf format strings is not portable (notably on win32) so
[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.7 2002/11/08 10:26:52 gbazin 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, i_size;
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="I64Fd"-\r\n"
122                    HTTP_USERAGENT HTTP_END,
123                    p_access_data->psz_buffer, i_tell );
124     }
125     else
126     {
127          snprintf( psz_buffer, sizeof(psz_buffer),
128                    "%s"
129                    HTTP_USERAGENT HTTP_END,
130                    p_access_data->psz_buffer );
131     }
132     psz_buffer[sizeof(psz_buffer) - 1] = '\0';
133
134     /* Send GET ... */
135     if( send( p_access_data->_socket.i_handle, psz_buffer,
136                strlen( psz_buffer ), 0 ) == (-1) )
137     {
138         msg_Err( p_input, "cannot send request (%s)", strerror(errno) );
139         input_FDNetworkClose( p_input );
140         return( -1 );
141     }
142
143     /* Prepare the input thread for reading. */ 
144     p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
145
146     /* FIXME: we shouldn't have to do that ! It's UGLY but mandatory because
147      * input_FillBuffer assumes p_input->pf_read exists */
148     p_input->pf_read = input_FDNetworkRead;
149
150     while( !input_FillBuffer( p_input ) )
151     {
152         if( p_input->b_die || p_input->b_error )
153         {
154             input_FDNetworkClose( p_input );
155             return( -1 );
156         }
157     }
158
159     /* Parse HTTP header. */
160 #define MAX_LINE 1024
161
162     /* get the returncode */
163     if( (i_size = input_Peek( p_input, &psz_parser, MAX_LINE )) <= 0 )
164     {
165         msg_Err( p_input, "not enough data" );
166         input_FDNetworkClose( p_input );
167         return( -1 );
168     }
169
170     if( (i_size >= sizeof("HTTP/1.") + 1 ) &&
171         !strncmp( psz_parser, "HTTP/1.", sizeof("HTTP/1.") - 1 ) )
172     {
173         psz_parser += sizeof("HTTP/1.") + 1;
174         i_returncode = atoi( (char*)psz_parser );
175         msg_Dbg( p_input, "HTTP server replied: %i", i_returncode );
176         psz_parser += 4;
177         i_size -= (sizeof("HTTP/1.") + 5);
178         for ( i = 0; (i < i_size -1) && ((psz_parser[i] != '\r') ||
179               (psz_parser[i+1] != '\n')); i++ )
180         {
181             ;
182         }
183          /* check we actually parsed something */
184         if ( (i == i_size - 1) && (psz_parser[i+1] != '\n') )
185         {
186             msg_Err( p_input, "stream not compliant with HTTP/1.x" );
187             return -1;
188         }
189
190         psz_return_alpha = malloc( i + 1 );
191         memcpy( psz_return_alpha, psz_parser, i );
192         psz_return_alpha[i] = '\0';
193     }
194     else
195     {
196         msg_Err( p_input, "invalid http reply" );
197         return -1;
198     }
199
200     if ( i_returncode >= 400 ) /* something is wrong */
201     {
202         msg_Err( p_input, "%i %s", i_returncode,
203                  psz_return_alpha );
204         return -1;
205     }
206     
207     for( ; ; ) 
208     {
209         if( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
210         {
211             msg_Err( p_input, "not enough data" );
212             input_FDNetworkClose( p_input );
213             return( -1 );
214         }
215
216         if( psz_parser[0] == '\r' && psz_parser[1] == '\n' )
217         {
218             /* End of header. */
219             p_input->p_current_data += 2;
220             break;
221         }
222
223         if( !strncmp( psz_parser, "Content-Length: ",
224                       strlen("Content-Length: ") ) )
225         {
226             psz_parser += strlen("Content-Length: ");
227             vlc_mutex_lock( &p_input->stream.stream_lock );
228 #ifdef HAVE_ATOLL
229             p_input->stream.p_selected_area->i_size = atoll( (char*)psz_parser )
230                                                         + i_tell;
231 #else
232             /* FIXME : this won't work for 64-bit lengths */
233             p_input->stream.p_selected_area->i_size = atoi( (char*)psz_parser )
234                                                         + i_tell;
235 #endif
236             vlc_mutex_unlock( &p_input->stream.stream_lock );
237         }
238
239         while( *psz_parser != '\r' && psz_parser < p_input->p_last_data )
240         {
241             psz_parser++;
242         }
243         p_input->p_current_data = psz_parser + 2;
244     }
245
246     if( p_input->stream.p_selected_area->i_size )
247     {
248         vlc_mutex_lock( &p_input->stream.stream_lock );
249         p_input->stream.p_selected_area->i_tell = i_tell
250             + (p_input->p_last_data - p_input->p_current_data);
251         p_input->stream.b_seekable = 1;
252         p_input->stream.b_changed = 1;
253         vlc_mutex_unlock( &p_input->stream.stream_lock );
254     }
255
256     return( 0 );
257 }
258
259 /*****************************************************************************
260  * Open: parse URL and open the remote file at the beginning
261  *****************************************************************************/
262 static int Open( vlc_object_t *p_this )
263 {
264     input_thread_t *    p_input = (input_thread_t *)p_this;
265     _input_socket_t *   p_access_data;
266     char *              psz_name = strdup(p_input->psz_name);
267     char *              psz_parser = psz_name;
268     char *              psz_server_addr = "";
269     char *              psz_server_port = "";
270     char *              psz_path = "";
271     char *              psz_proxy;
272     int                 i_server_port = 0;
273
274     p_access_data = malloc( sizeof(_input_socket_t) );
275     p_input->p_access_data = (access_sys_t *)p_access_data;
276     if( p_access_data == NULL )
277     {
278         msg_Err( p_input, "out of memory" );
279         free(psz_name);
280         return( -1 );
281     }
282
283     p_access_data->psz_name = psz_name;
284     p_access_data->psz_network = "";
285     if( config_GetInt( p_input, "ipv4" ) )
286     {
287         p_access_data->psz_network = "ipv4";
288     }
289     if( config_GetInt( p_input, "ipv6" ) )
290     {
291         p_access_data->psz_network = "ipv6";
292     }
293     if( *p_input->psz_access )
294     {
295         /* Find out which shortcut was used */
296         if( !strncmp( p_input->psz_access, "http6", 6 ) )
297         {
298             p_access_data->psz_network = "ipv6";
299         }
300         else if( !strncmp( p_input->psz_access, "http4", 6 ) )
301         {
302             p_access_data->psz_network = "ipv4";
303         }
304     }
305
306     /* Parse psz_name syntax :
307      * //<hostname>[:<port>][/<path>] */
308     while( *psz_parser == '/' )
309     {
310         psz_parser++;
311     }
312     psz_server_addr = psz_parser;
313
314     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
315     {
316         psz_parser++;
317     }
318
319     if ( *psz_parser == ':' )
320     {
321         *psz_parser = '\0';
322         psz_parser++;
323         psz_server_port = psz_parser;
324
325         while( *psz_parser && *psz_parser != '/' )
326         {
327             psz_parser++;
328         }
329     }
330
331     if( *psz_parser == '/' )
332     {
333         *psz_parser = '\0';
334         psz_parser++;
335         psz_path = psz_parser;
336     }
337
338     /* Convert port format */
339     if( *psz_server_port )
340     {
341         i_server_port = strtol( psz_server_port, &psz_parser, 10 );
342         if( *psz_parser )
343         {
344             msg_Err( p_input, "cannot parse server port near %s", psz_parser );
345             free( p_input->p_access_data );
346             free( psz_name );
347             return( -1 );
348         }
349     }
350
351     if( i_server_port == 0 )
352     {
353         i_server_port = 80;
354     }
355
356     if( !*psz_server_addr )
357     {
358         msg_Err( p_input, "no server given" );
359         free( p_input->p_access_data );
360         free( psz_name );
361         return( -1 );
362     }
363
364     /* Check proxy */
365     if( (psz_proxy = getenv( "http_proxy" )) != NULL && *psz_proxy )
366     {
367         /* http://myproxy.mydomain:myport/ */
368         int                 i_proxy_port = 0;
369  
370         /* Skip the protocol name */
371         while( *psz_proxy && *psz_proxy != ':' )
372         {
373             psz_proxy++;
374         }
375  
376         /* Skip the "://" part */
377         while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
378         {
379             psz_proxy++;
380         }
381  
382         /* Found a proxy name */
383         if( *psz_proxy )
384         {
385             char *psz_port = psz_proxy;
386  
387             /* Skip the hostname part */
388             while( *psz_port && *psz_port != ':' && *psz_port != '/' )
389             {
390                 psz_port++;
391             }
392  
393             /* Found a port name */
394             if( *psz_port )
395             {
396                 char * psz_junk;
397  
398                 /* Replace ':' with '\0' */
399                 *psz_port = '\0';
400                 psz_port++;
401  
402                 psz_junk = psz_port;
403                 while( *psz_junk && *psz_junk != '/' )
404                 {
405                     psz_junk++;
406                 }
407  
408                 if( *psz_junk )
409                 {
410                     *psz_junk = '\0';
411                 }
412  
413                 if( *psz_port != '\0' )
414                 {
415                     i_proxy_port = atoi( psz_port );
416                 }
417             }
418         }
419         else
420         {
421             msg_Err( p_input, "http_proxy environment variable is invalid!" );
422             free( p_input->p_access_data );
423             free( psz_name );
424             return( -1 );
425         }
426
427         p_access_data->socket_desc.i_type = NETWORK_TCP;
428         p_access_data->socket_desc.psz_server_addr = psz_proxy;
429         p_access_data->socket_desc.i_server_port = i_proxy_port;
430
431         snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
432                   "GET http://%s:%d/%s\r\n HTTP/1.0\r\n",
433                   psz_server_addr, i_server_port, psz_path );
434     }
435     else
436     {
437         /* No proxy, direct connection. */
438         p_access_data->socket_desc.i_type = NETWORK_TCP;
439         p_access_data->socket_desc.psz_server_addr = psz_server_addr;
440         p_access_data->socket_desc.i_server_port = i_server_port;
441
442         snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
443                   "GET /%s HTTP/1.1\r\nHost: %s\r\n",
444                   psz_path, psz_server_addr );
445     }
446     p_access_data->psz_buffer[sizeof(p_access_data->psz_buffer) - 1] = '\0';
447
448     msg_Dbg( p_input, "opening server=%s port=%d path=%s",
449                       psz_server_addr, i_server_port, psz_path );
450
451     p_input->pf_read = input_FDNetworkRead;
452     p_input->pf_set_program = SetProgram;
453     p_input->pf_set_area = NULL;
454     p_input->pf_seek = Seek;
455
456     vlc_mutex_lock( &p_input->stream.stream_lock );
457     p_input->stream.b_pace_control = 1;
458     p_input->stream.b_seekable = 1;
459     p_input->stream.p_selected_area->i_tell = 0;
460     p_input->stream.p_selected_area->i_size = 0;
461     p_input->stream.i_method = INPUT_METHOD_NETWORK;
462     vlc_mutex_unlock( &p_input->stream.stream_lock );
463     p_input->i_mtu = 0;
464  
465     if( HTTPConnect( p_input, 0 ) )
466     {
467         char * psz_pos = strstr(p_access_data->psz_buffer, "HTTP/1.1");
468         p_input->stream.b_seekable = 0;
469         psz_pos[7] = '0';
470         if( HTTPConnect( p_input, 0 ) )
471         {
472             free( p_input->p_access_data );
473             free( psz_name );
474             return( -1 );
475         }
476     }
477     return 0;
478 }
479
480 /*****************************************************************************
481  * Close: free unused data structures
482  *****************************************************************************/
483 static void Close( vlc_object_t *p_this )
484 {
485     input_thread_t *  p_input = (input_thread_t *)p_this;
486     _input_socket_t * p_access_data = 
487         (_input_socket_t *)p_input->p_access_data;
488
489     free( p_access_data->psz_name );
490     input_FDNetworkClose( p_input );
491 }
492
493 /*****************************************************************************
494  * SetProgram: do nothing
495  *****************************************************************************/
496 static int SetProgram( input_thread_t * p_input,
497                        pgrm_descriptor_t * p_program )
498 {
499     return( 0 );
500 }
501
502 /*****************************************************************************
503  * Seek: close and re-open a connection at the right place
504  *****************************************************************************/
505 static void Seek( input_thread_t * p_input, off_t i_pos )
506 {
507     _input_socket_t *p_access_data = (_input_socket_t*)p_input->p_access_data;
508     close( p_access_data->_socket.i_handle );
509     msg_Dbg( p_input, "seeking to position "I64Fd, i_pos );
510     HTTPConnect( p_input, i_pos );
511 }
512