]> git.sesse.net Git - vlc/blob - plugins/access/http.c
* ./src/misc/modules.c: added the --plugin-path option to give vlc an
[vlc] / plugins / access / http.c
1 /*****************************************************************************
2  * http.c: HTTP access plug-in
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: http.c,v 1.13 2002/06/27 19:05:17 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 void input_getfunctions( function_list_t * );
59 static int  HTTPOpen       ( input_thread_t * );
60 static void HTTPClose      ( input_thread_t * );
61 static int  HTTPSetProgram ( input_thread_t *, pgrm_descriptor_t * );  
62 static void HTTPSeek       ( input_thread_t *, off_t );
63
64 /*****************************************************************************
65  * Build configuration tree.
66  *****************************************************************************/
67 MODULE_CONFIG_START
68 MODULE_CONFIG_STOP
69  
70 MODULE_INIT_START
71     SET_DESCRIPTION( _("HTTP access plug-in") )
72     ADD_CAPABILITY( ACCESS, 0 )
73     ADD_SHORTCUT( "http4" )
74     ADD_SHORTCUT( "http6" )
75 MODULE_INIT_STOP
76  
77 MODULE_ACTIVATE_START
78     input_getfunctions( &p_module->p_functions->access );
79 MODULE_ACTIVATE_STOP
80  
81 MODULE_DEACTIVATE_START
82 MODULE_DEACTIVATE_STOP
83
84 /*****************************************************************************
85  * Functions exported as capabilities. They are declared as static so that
86  * we don't pollute the namespace too much.
87  *****************************************************************************/
88 static void input_getfunctions( function_list_t * p_function_list )
89 {
90 #define input p_function_list->functions.access
91     input.pf_open             = HTTPOpen;
92     input.pf_read             = input_FDNetworkRead;
93     input.pf_close            = HTTPClose;
94     input.pf_set_program      = HTTPSetProgram;
95     input.pf_set_area         = NULL;
96     input.pf_seek             = HTTPSeek;
97 #undef input
98 }
99
100 /*****************************************************************************
101  * _input_socket_t: private access plug-in data, modified to add private
102  *                  fields
103  *****************************************************************************/
104 typedef struct _input_socket_s
105 {
106     input_socket_t      _socket;
107
108     char *              psz_network;
109     network_socket_t    socket_desc;
110     char                psz_buffer[256];
111     char *              psz_name;
112 } _input_socket_t;
113
114 /*****************************************************************************
115  * HTTPConnect: connect to the server and seek to i_tell
116  *****************************************************************************/
117 static int HTTPConnect( input_thread_t * p_input, off_t i_tell )
118 {
119     _input_socket_t *   p_access_data = p_input->p_access_data;
120     module_t *          p_network;
121     char                psz_buffer[256];
122     byte_t *            psz_parser;
123
124     /* Find an appropriate network module */
125     p_network = module_Need( p_input, MODULE_CAPABILITY_NETWORK,
126                              p_access_data->psz_network,
127                              &p_access_data->socket_desc );
128     if( p_network == NULL )
129     {
130         free( p_access_data );
131         return( -1 );
132     }
133     module_Unneed( p_network );
134
135     p_access_data->_socket.i_handle = p_access_data->socket_desc.i_handle;
136
137 #   define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
138 #   define HTTP_END       "\r\n"
139  
140     if ( p_input->stream.b_seekable )
141     {
142          snprintf( psz_buffer, sizeof(psz_buffer),
143                    "%s"
144                    "Range: bytes=%lld-\r\n"
145                    HTTP_USERAGENT HTTP_END,
146                    p_access_data->psz_buffer, i_tell );
147     }
148     else
149     {
150          snprintf( psz_buffer, sizeof(psz_buffer),
151                    "%s"
152                    HTTP_USERAGENT HTTP_END,
153                    p_access_data->psz_buffer );
154     }
155     psz_buffer[sizeof(psz_buffer) - 1] = '\0';
156
157     /* Send GET ... */
158     if( send( p_access_data->_socket.i_handle, psz_buffer,
159                strlen( psz_buffer ), 0 ) == (-1) )
160     {
161         msg_Err( p_input, "cannot send request (%s)", strerror(errno) );
162         input_FDNetworkClose( p_input );
163         return( -1 );
164     }
165
166     /* Prepare the input thread for reading. */ 
167     p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
168     /* FIXME: we shouldn't have to do that ! */
169     p_input->pf_read = input_FDNetworkRead;
170
171     while( !input_FillBuffer( p_input ) )
172     {
173         if( p_input->b_die || p_input->b_error )
174         {
175             input_FDNetworkClose( p_input );
176             return( -1 );
177         }
178     }
179
180     /* Parse HTTP header. */
181 #define MAX_LINE 1024
182     for( ; ; ) 
183     {
184         if( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
185         {
186             msg_Err( p_input, "not enough data" );
187             input_FDNetworkClose( p_input );
188             return( -1 );
189         }
190
191         if( psz_parser[0] == '\r' && psz_parser[1] == '\n' )
192         {
193             /* End of header. */
194             p_input->p_current_data += 2;
195             break;
196         }
197
198         if( !strncmp( psz_parser, "Content-Length: ",
199                       strlen("Content-Length: ") ) )
200         {
201             psz_parser += strlen("Content-Length: ");
202             vlc_mutex_lock( &p_input->stream.stream_lock );
203 #ifdef HAVE_ATOLL
204             p_input->stream.p_selected_area->i_size = atoll( psz_parser )
205                                                         + i_tell;
206 #else
207             /* FIXME : this won't work for 64-bit lengths */
208             p_input->stream.p_selected_area->i_size = atoi( psz_parser )
209                                                         + i_tell;
210 #endif
211             vlc_mutex_unlock( &p_input->stream.stream_lock );
212         }
213
214         while( *psz_parser != '\r' && psz_parser < p_input->p_last_data )
215         {
216             psz_parser++;
217         }
218         p_input->p_current_data = psz_parser + 2;
219     }
220
221     if( p_input->stream.p_selected_area->i_size )
222     {
223         vlc_mutex_lock( &p_input->stream.stream_lock );
224         p_input->stream.p_selected_area->i_tell = i_tell
225             + (p_input->p_last_data - p_input->p_current_data);
226         p_input->stream.b_seekable = 1;
227         p_input->stream.b_changed = 1;
228         vlc_mutex_unlock( &p_input->stream.stream_lock );
229     }
230
231     return( 0 );
232 }
233
234 /*****************************************************************************
235  * HTTPOpen: parse URL and open the remote file at the beginning
236  *****************************************************************************/
237 static int HTTPOpen( input_thread_t * p_input )
238 {
239     _input_socket_t *   p_access_data;
240     char *              psz_name = strdup(p_input->psz_name);
241     char *              psz_parser = psz_name;
242     char *              psz_server_addr = "";
243     char *              psz_server_port = "";
244     char *              psz_path = "";
245     char *              psz_proxy;
246     int                 i_server_port = 0;
247
248     p_access_data = p_input->p_access_data = malloc( sizeof(_input_socket_t) );
249     if( p_access_data == NULL )
250     {
251         msg_Err( p_input, "out of memory" );
252         free(psz_name);
253         return( -1 );
254     }
255
256     p_access_data->psz_name = psz_name;
257     p_access_data->psz_network = "";
258     if( config_GetInt( p_input, "ipv4" ) )
259     {
260         p_access_data->psz_network = "ipv4";
261     }
262     if( config_GetInt( p_input, "ipv6" ) )
263     {
264         p_access_data->psz_network = "ipv6";
265     }
266     if( *p_input->psz_access )
267     {
268         /* Find out which shortcut was used */
269         if( !strncmp( p_input->psz_access, "http6", 6 ) )
270         {
271             p_access_data->psz_network = "ipv6";
272         }
273         else if( !strncmp( p_input->psz_access, "http4", 6 ) )
274         {
275             p_access_data->psz_network = "ipv4";
276         }
277     }
278
279     /* Parse psz_name syntax :
280      * //<hostname>[:<port>][/<path>] */
281     while( *psz_parser == '/' )
282     {
283         psz_parser++;
284     }
285     psz_server_addr = psz_parser;
286
287     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
288     {
289         psz_parser++;
290     }
291
292     if ( *psz_parser == ':' )
293     {
294         *psz_parser = '\0';
295         psz_parser++;
296         psz_server_port = psz_parser;
297
298         while( *psz_parser && *psz_parser != '/' )
299         {
300             psz_parser++;
301         }
302     }
303
304     if( *psz_parser == '/' )
305     {
306         *psz_parser = '\0';
307         psz_parser++;
308         psz_path = psz_parser;
309     }
310
311     /* Convert port format */
312     if( *psz_server_port )
313     {
314         i_server_port = strtol( psz_server_port, &psz_parser, 10 );
315         if( *psz_parser )
316         {
317             msg_Err( p_input, "cannot parse server port near %s", psz_parser );
318             free( p_input->p_access_data );
319             free( psz_name );
320             return( -1 );
321         }
322     }
323
324     if( i_server_port == 0 )
325     {
326         i_server_port = 80;
327     }
328
329     if( !*psz_server_addr )
330     {
331         msg_Err( p_input, "no server given" );
332         free( p_input->p_access_data );
333         free( psz_name );
334         return( -1 );
335     }
336
337     /* Check proxy */
338     if( (psz_proxy = getenv( "http_proxy" )) != NULL && *psz_proxy )
339     {
340         /* http://myproxy.mydomain:myport/ */
341         int                 i_proxy_port = 0;
342  
343         /* Skip the protocol name */
344         while( *psz_proxy && *psz_proxy != ':' )
345         {
346             psz_proxy++;
347         }
348  
349         /* Skip the "://" part */
350         while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
351         {
352             psz_proxy++;
353         }
354  
355         /* Found a proxy name */
356         if( *psz_proxy )
357         {
358             char *psz_port = psz_proxy;
359  
360             /* Skip the hostname part */
361             while( *psz_port && *psz_port != ':' && *psz_port != '/' )
362             {
363                 psz_port++;
364             }
365  
366             /* Found a port name */
367             if( *psz_port )
368             {
369                 char * psz_junk;
370  
371                 /* Replace ':' with '\0' */
372                 *psz_port = '\0';
373                 psz_port++;
374  
375                 psz_junk = psz_port;
376                 while( *psz_junk && *psz_junk != '/' )
377                 {
378                     psz_junk++;
379                 }
380  
381                 if( *psz_junk )
382                 {
383                     *psz_junk = '\0';
384                 }
385  
386                 if( *psz_port != '\0' )
387                 {
388                     i_proxy_port = atoi( psz_port );
389                 }
390             }
391         }
392         else
393         {
394             msg_Err( p_input, "http_proxy environment variable is invalid!" );
395             free( p_input->p_access_data );
396             free( psz_name );
397             return( -1 );
398         }
399
400         p_access_data->socket_desc.i_type = NETWORK_TCP;
401         p_access_data->socket_desc.psz_server_addr = psz_proxy;
402         p_access_data->socket_desc.i_server_port = i_proxy_port;
403
404         snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
405                   "GET http://%s:%d/%s\r\n HTTP/1.0\r\n",
406                   psz_server_addr, i_server_port, psz_path );
407     }
408     else
409     {
410         /* No proxy, direct connection. */
411         p_access_data->socket_desc.i_type = NETWORK_TCP;
412         p_access_data->socket_desc.psz_server_addr = psz_server_addr;
413         p_access_data->socket_desc.i_server_port = i_server_port;
414
415         snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
416                   "GET /%s HTTP/1.1\r\nHost: %s\r\n",
417                   psz_path, psz_server_addr );
418     }
419     p_access_data->psz_buffer[sizeof(p_access_data->psz_buffer) - 1] = '\0';
420
421     msg_Dbg( p_input, "opening server=%s port=%d path=%s",
422                       psz_server_addr, i_server_port, psz_path );
423
424     vlc_mutex_lock( &p_input->stream.stream_lock );
425     p_input->stream.b_pace_control = 1;
426     p_input->stream.b_seekable = 1;
427     p_input->stream.p_selected_area->i_tell = 0;
428     p_input->stream.p_selected_area->i_size = 0;
429     p_input->stream.i_method = INPUT_METHOD_NETWORK;
430     vlc_mutex_unlock( &p_input->stream.stream_lock );
431     p_input->i_mtu = 0;
432  
433     if( HTTPConnect( p_input, 0 ) )
434     {
435         char * psz_pos = strstr(p_access_data->psz_buffer, "HTTP/1.1");
436         p_input->stream.b_seekable = 0;
437         psz_pos[7] = 0;
438         return( HTTPConnect( p_input, 0 ) );
439     }
440     return 0;
441 }
442
443 /*****************************************************************************
444  * HTTPClose: free unused data structures
445  *****************************************************************************/
446 static void HTTPClose( input_thread_t * p_input )
447 {
448     input_FDNetworkClose( p_input );
449 }
450
451 /*****************************************************************************
452  * HTTPSetProgram: do nothing
453  *****************************************************************************/
454 static int HTTPSetProgram( input_thread_t * p_input,
455                            pgrm_descriptor_t * p_program )
456 {
457     return( 0 );
458 }
459
460 /*****************************************************************************
461  * HTTPSeek: close and re-open a connection at the right place
462  *****************************************************************************/
463 static void HTTPSeek( input_thread_t * p_input, off_t i_pos )
464 {
465     _input_socket_t *   p_access_data = p_input->p_access_data;
466     close( p_access_data->_socket.i_handle );
467     msg_Dbg( p_input, "seeking to position %lld", i_pos );
468     HTTPConnect( p_input, i_pos );
469 }
470