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