]> git.sesse.net Git - vlc/blob - plugins/access/http.c
* ALL: the first libvlc commit.
[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.11 2002/06/01 12:31:58 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     snprintf( psz_buffer, sizeof(psz_buffer),
141               "%s"
142               "Range: bytes=%lld-\r\n"
143               HTTP_USERAGENT HTTP_END,
144               p_access_data->psz_buffer, i_tell );
145     psz_buffer[sizeof(psz_buffer) - 1] = '\0';
146
147     /* Send GET ... */
148     if( send( p_access_data->_socket.i_handle, psz_buffer,
149                strlen( psz_buffer ), 0 ) == (-1) )
150     {
151         msg_Err( p_input, "cannot send request (%s)", strerror(errno) );
152         input_FDNetworkClose( p_input );
153         return( -1 );
154     }
155
156     /* Prepare the input thread for reading. */ 
157     p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
158     /* FIXME: we shouldn't have to do that ! */
159     p_input->pf_read = input_FDNetworkRead;
160
161     while( !input_FillBuffer( p_input ) )
162     {
163         if( p_input->b_die || p_input->b_error )
164         {
165             input_FDNetworkClose( p_input );
166             return( -1 );
167         }
168     }
169
170     /* Parse HTTP header. */
171 #define MAX_LINE 1024
172     for( ; ; ) 
173     {
174         if( input_Peek( p_input, &psz_parser, MAX_LINE ) <= 0 )
175         {
176             msg_Err( p_input, "not enough data" );
177             input_FDNetworkClose( p_input );
178             return( -1 );
179         }
180
181         if( psz_parser[0] == '\r' && psz_parser[1] == '\n' )
182         {
183             /* End of header. */
184             p_input->p_current_data += 2;
185             break;
186         }
187
188         if( !strncmp( psz_parser, "Content-Length: ",
189                       strlen("Content-Length: ") ) )
190         {
191             psz_parser += strlen("Content-Length: ");
192             /* FIXME : this won't work for 64-bit lengths */
193             vlc_mutex_lock( &p_input->stream.stream_lock );
194             p_input->stream.p_selected_area->i_size = atoi( psz_parser )
195                                                         + i_tell;
196             vlc_mutex_unlock( &p_input->stream.stream_lock );
197         }
198
199         while( *psz_parser != '\r' && psz_parser < p_input->p_last_data )
200         {
201             psz_parser++;
202         }
203         p_input->p_current_data = psz_parser + 2;
204     }
205
206     if( p_input->stream.p_selected_area->i_size )
207     {
208         vlc_mutex_lock( &p_input->stream.stream_lock );
209         p_input->stream.p_selected_area->i_tell = i_tell
210             + (p_input->p_last_data - p_input->p_current_data);
211         p_input->stream.b_seekable = 1;
212         p_input->stream.b_changed = 1;
213         vlc_mutex_unlock( &p_input->stream.stream_lock );
214     }
215
216     return( 0 );
217 }
218
219 /*****************************************************************************
220  * HTTPOpen: parse URL and open the remote file at the beginning
221  *****************************************************************************/
222 static int HTTPOpen( input_thread_t * p_input )
223 {
224     _input_socket_t *   p_access_data;
225     char *              psz_name = strdup(p_input->psz_name);
226     char *              psz_parser = psz_name;
227     char *              psz_server_addr = "";
228     char *              psz_server_port = "";
229     char *              psz_path = "";
230     char *              psz_proxy;
231     int                 i_server_port = 0;
232
233     p_access_data = p_input->p_access_data = malloc( sizeof(_input_socket_t) );
234     if( p_access_data == NULL )
235     {
236         msg_Err( p_input, "out of memory" );
237         free(psz_name);
238         return( -1 );
239     }
240
241     p_access_data->psz_name = psz_name;
242     p_access_data->psz_network = "";
243     if( config_GetInt( p_input, "ipv4" ) )
244     {
245         p_access_data->psz_network = "ipv4";
246     }
247     if( config_GetInt( p_input, "ipv6" ) )
248     {
249         p_access_data->psz_network = "ipv6";
250     }
251     if( *p_input->psz_access )
252     {
253         /* Find out which shortcut was used */
254         if( !strncmp( p_input->psz_access, "http6", 6 ) )
255         {
256             p_access_data->psz_network = "ipv6";
257         }
258         else if( !strncmp( p_input->psz_access, "http4", 6 ) )
259         {
260             p_access_data->psz_network = "ipv4";
261         }
262     }
263
264     /* Parse psz_name syntax :
265      * //<hostname>[:<port>][/<path>] */
266     while( *psz_parser == '/' )
267     {
268         psz_parser++;
269     }
270     psz_server_addr = psz_parser;
271
272     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
273     {
274         psz_parser++;
275     }
276
277     if ( *psz_parser == ':' )
278     {
279         *psz_parser = '\0';
280         psz_parser++;
281         psz_server_port = psz_parser;
282
283         while( *psz_parser && *psz_parser != '/' )
284         {
285             psz_parser++;
286         }
287     }
288
289     if( *psz_parser == '/' )
290     {
291         *psz_parser = '\0';
292         psz_parser++;
293         psz_path = psz_parser;
294     }
295
296     /* Convert port format */
297     if( *psz_server_port )
298     {
299         i_server_port = strtol( psz_server_port, &psz_parser, 10 );
300         if( *psz_parser )
301         {
302             msg_Err( p_input, "cannot parse server port near %s", psz_parser );
303             free( p_input->p_access_data );
304             free( psz_name );
305             return( -1 );
306         }
307     }
308
309     if( i_server_port == 0 )
310     {
311         i_server_port = 80;
312     }
313
314     if( !*psz_server_addr )
315     {
316         msg_Err( p_input, "no server given" );
317         free( p_input->p_access_data );
318         free( psz_name );
319         return( -1 );
320     }
321
322     /* Check proxy */
323     if( (psz_proxy = getenv( "http_proxy" )) != NULL && *psz_proxy )
324     {
325         /* http://myproxy.mydomain:myport/ */
326         int                 i_proxy_port = 0;
327  
328         /* Skip the protocol name */
329         while( *psz_proxy && *psz_proxy != ':' )
330         {
331             psz_proxy++;
332         }
333  
334         /* Skip the "://" part */
335         while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
336         {
337             psz_proxy++;
338         }
339  
340         /* Found a proxy name */
341         if( *psz_proxy )
342         {
343             char *psz_port = psz_proxy;
344  
345             /* Skip the hostname part */
346             while( *psz_port && *psz_port != ':' && *psz_port != '/' )
347             {
348                 psz_port++;
349             }
350  
351             /* Found a port name */
352             if( *psz_port )
353             {
354                 char * psz_junk;
355  
356                 /* Replace ':' with '\0' */
357                 *psz_port = '\0';
358                 psz_port++;
359  
360                 psz_junk = psz_port;
361                 while( *psz_junk && *psz_junk != '/' )
362                 {
363                     psz_junk++;
364                 }
365  
366                 if( *psz_junk )
367                 {
368                     *psz_junk = '\0';
369                 }
370  
371                 if( *psz_port != '\0' )
372                 {
373                     i_proxy_port = atoi( psz_port );
374                 }
375             }
376         }
377         else
378         {
379             msg_Err( p_input, "http_proxy environment variable is invalid!" );
380             free( p_input->p_access_data );
381             free( psz_name );
382             return( -1 );
383         }
384
385         p_access_data->socket_desc.i_type = NETWORK_TCP;
386         p_access_data->socket_desc.psz_server_addr = psz_proxy;
387         p_access_data->socket_desc.i_server_port = i_proxy_port;
388
389         snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
390                   "GET http://%s:%d/%s HTTP/1.1\r\n",
391                   psz_server_addr, i_server_port, psz_path );
392     }
393     else
394     {
395         /* No proxy, direct connection. */
396         p_access_data->socket_desc.i_type = NETWORK_TCP;
397         p_access_data->socket_desc.psz_server_addr = psz_server_addr;
398         p_access_data->socket_desc.i_server_port = i_server_port;
399
400         snprintf( p_access_data->psz_buffer, sizeof(p_access_data->psz_buffer),
401                   "GET /%s HTTP/1.1\r\nHost: %s\r\n",
402                   psz_path, psz_server_addr );
403     }
404     p_access_data->psz_buffer[sizeof(p_access_data->psz_buffer) - 1] = '\0';
405
406     msg_Dbg( p_input, "opening server=%s port=%d path=%s",
407                       psz_server_addr, i_server_port, psz_path );
408
409     vlc_mutex_lock( &p_input->stream.stream_lock );
410     p_input->stream.b_pace_control = 1;
411     p_input->stream.b_seekable = 0;
412     p_input->stream.p_selected_area->i_tell = 0;
413     p_input->stream.p_selected_area->i_size = 0;
414     p_input->stream.i_method = INPUT_METHOD_NETWORK;
415     vlc_mutex_unlock( &p_input->stream.stream_lock );
416     p_input->i_mtu = 0;
417  
418     return( HTTPConnect( p_input, 0 ) );
419 }
420
421 /*****************************************************************************
422  * HTTPClose: free unused data structures
423  *****************************************************************************/
424 static void HTTPClose( input_thread_t * p_input )
425 {
426     input_FDNetworkClose( p_input );
427 }
428
429 /*****************************************************************************
430  * HTTPSetProgram: do nothing
431  *****************************************************************************/
432 static int HTTPSetProgram( input_thread_t * p_input,
433                            pgrm_descriptor_t * p_program )
434 {
435     return( 0 );
436 }
437
438 /*****************************************************************************
439  * HTTPSeek: close and re-open a connection at the right place
440  *****************************************************************************/
441 static void HTTPSeek( input_thread_t * p_input, off_t i_pos )
442 {
443     _input_socket_t *   p_access_data = p_input->p_access_data;
444     close( p_access_data->_socket.i_handle );
445     msg_Dbg( p_input, "seeking to position %lld", i_pos );
446     HTTPConnect( p_input, i_pos );
447 }
448