]> git.sesse.net Git - vlc/blob - modules/access/http.c
cefda1c5363592b28f7862918ebce2db8d158604
[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.42 2003/08/01 09:45:34 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 <string.h>
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #ifdef HAVE_ERRNO_H
33 #   include <errno.h>
34 #endif
35 #ifdef HAVE_FCNTL_H
36 #   include <fcntl.h>
37 #endif
38
39 #ifdef HAVE_SYS_TIME_H
40 #    include <sys/time.h>
41 #endif
42
43 #ifdef HAVE_UNISTD_H
44 #   include <unistd.h>
45 #endif
46
47 #if defined( UNDER_CE )
48 #   include <winsock.h>
49 #elif defined( WIN32 )
50 #   include <winsock2.h>
51 #   include <ws2tcpip.h>
52 #   ifndef IN_MULTICAST
53 #       define IN_MULTICAST(a) IN_CLASSD(a)
54 #   endif
55 #else
56 #   include <sys/socket.h>
57 #endif
58
59 #include "vlc_playlist.h"
60 #include "network.h"
61
62 /*****************************************************************************
63  * Local prototypes
64  *****************************************************************************/
65 static int  Open       ( vlc_object_t * );
66 static void Close      ( vlc_object_t * );
67
68 static void Seek       ( input_thread_t *, off_t );
69 static ssize_t Read    ( input_thread_t *, byte_t *, size_t );
70
71 /*****************************************************************************
72  * Module descriptor
73  *****************************************************************************/
74 #define PROXY_TEXT N_("Specify an HTTP proxy")
75 #define PROXY_LONGTEXT N_( \
76     "Specify an HTTP proxy to use. It must be in the form " \
77     "http://myproxy.mydomain:myport. If none is specified, the HTTP_PROXY " \
78     "environment variable will be tried." )
79
80 #define CACHING_TEXT N_("Caching value in ms")
81 #define CACHING_LONGTEXT N_( \
82     "Allows you to modify the default caching value for http streams. This " \
83     "value should be set in miliseconds units." )
84
85 vlc_module_begin();
86     add_category_hint( N_("http"), NULL, VLC_FALSE );
87     add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT, VLC_FALSE );
88     add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
89     add_string( "http-user", NULL, NULL, "HTTP user name", "HTTP user name for Basic Authentification", VLC_FALSE );
90     add_string( "http-pwd", NULL , NULL, "HTTP password", "HTTP password for Basic Authentification", VLC_FALSE );
91     set_description( _("HTTP input") );
92     set_capability( "access", 0 );
93     add_shortcut( "http" );
94     add_shortcut( "http4" );
95     add_shortcut( "http6" );
96     set_callbacks( Open, Close );
97 vlc_module_end();
98
99 /*****************************************************************************
100  * _input_socket_t: private access plug-in data, modified to add private
101  *                  fields
102  *****************************************************************************/
103 #define MAX_ANSWER_SIZE 1024
104 #define MAX_QUERY_SIZE 1024
105
106 typedef struct _input_socket_s
107 {
108     input_socket_t      _socket;
109
110     char *              psz_network;
111     network_socket_t    socket_desc;
112     char                psz_buffer[MAX_QUERY_SIZE];
113     char                psz_auth_string[MAX_QUERY_SIZE];
114     char *              psz_name;
115 } _input_socket_t;
116
117 /*****************************************************************************
118  * HTTPConnect: connect to the server and seek to i_tell
119  *****************************************************************************/
120 static int HTTPConnect( input_thread_t * p_input, off_t i_tell )
121 {
122     char psz_buffer[MAX_QUERY_SIZE];
123     _input_socket_t * p_access_data;
124     module_t * p_network;
125     char * psz_parser, * psz_value, * psz_answer;
126     byte_t * p_bytes;
127     int i_code, i_ret, i, i_size;
128
129     enum { HTTP_PROTOCOL, ICY_PROTOCOL } i_protocol;
130
131     /* Find an appropriate network module */
132     p_access_data = (_input_socket_t *)p_input->p_access_data;
133     p_input->p_private = (void*) &p_access_data->socket_desc;
134     p_network = module_Need( p_input, "network", p_access_data->psz_network );
135     if( p_network == NULL )
136     {
137         return VLC_ENOMOD;
138     }
139     module_Unneed( p_input, p_network );
140
141     p_access_data->_socket.i_handle = p_access_data->socket_desc.i_handle;
142
143 #   define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
144 #   define HTTP_END       "\r\n"
145
146     /* Build the query string */
147     if ( p_input->stream.b_seekable )
148     {
149          snprintf( psz_buffer, MAX_QUERY_SIZE,
150                    "%s"
151                    "Range: bytes="I64Fd"-\r\n"
152                    HTTP_USERAGENT
153                    "%s"
154                    HTTP_END,
155                    p_access_data->psz_buffer, i_tell, p_access_data->psz_auth_string );
156     }
157     else
158     {
159          snprintf( psz_buffer, MAX_QUERY_SIZE,
160                    "%s"
161                    HTTP_USERAGENT
162                    "%s"
163                    HTTP_END,
164                    p_access_data->psz_buffer, p_access_data->psz_auth_string );
165     }
166     psz_buffer[MAX_QUERY_SIZE - 1] = '\0';
167
168     /* Send GET query */
169     i_ret = send( p_access_data->_socket.i_handle,
170                   psz_buffer, strlen( psz_buffer ), 0 );
171     if( i_ret == -1 )
172     {
173 #ifdef HAVE_ERRNO_H
174         msg_Err( p_input, "cannot send request (%s)", strerror(errno) );
175 #else
176         msg_Err( p_input, "cannot send request" );
177 #endif
178         Close( VLC_OBJECT(p_input) );
179         return VLC_EGENERIC;
180     }
181
182     /* Prepare the input thread for reading. */
183     p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
184
185     /* FIXME: we shouldn't have to do that ! It's UGLY but mandatory because
186      * input_FillBuffer assumes p_input->pf_read exists */
187     p_input->pf_read = Read;
188
189     while( !input_FillBuffer( p_input ) )
190     {
191         if( p_input->b_die || p_input->b_error )
192         {
193             Close( VLC_OBJECT(p_input) );
194             return VLC_EGENERIC;
195         }
196     }
197
198     /* Get the HTTP returncode */
199     i_size = input_Peek( p_input, &p_bytes, MAX_ANSWER_SIZE );
200     psz_parser = (char *)p_bytes;
201
202     if( i_size <= 0 )
203     {
204         msg_Err( p_input, "not enough data" );
205         Close( VLC_OBJECT(p_input) );
206         return VLC_EGENERIC;
207     }
208
209     /* Guess the protocol */
210     if( ( ( (size_t)i_size >= strlen("HTTP/1.x") ) &&
211             !strncmp( psz_parser, "HTTP/1.", strlen("HTTP/1.") ) ) )
212     {
213         i_protocol = HTTP_PROTOCOL;
214
215         psz_parser += strlen("HTTP/1.x");
216         i_size -= strlen("HTTP/1.x");
217     }
218     else if( ( (size_t)i_size >= strlen("ICY") &&
219              !strncmp( psz_parser, "ICY", strlen("ICY") ) ) )
220     {
221         i_protocol = ICY_PROTOCOL;
222         if( !p_input->psz_demux || !*p_input->psz_demux  )
223         {
224             msg_Info( p_input, "ICY server found, mp3 demuxer selected" );
225             p_input->psz_demux = "mp3";    // FIXME strdup ?
226         }
227
228         psz_parser += strlen("ICY");
229         i_size -= strlen("ICY");
230     }
231     else
232     {
233         msg_Err( p_input, "invalid HTTP reply '%s'", psz_parser );
234         return VLC_EGENERIC;
235     }
236
237     /* Check the HTTP return code */
238     i_code = atoi( (char*)psz_parser );
239     msg_Dbg( p_input, "%s server replied: %i",
240              i_protocol == HTTP_PROTOCOL ? "HTTP" : "ICY", i_code );
241     psz_parser += 4;
242     i_size -= 4;
243
244     /* Find the end of the line */
245     for ( i = 0; (i < i_size -1) && ((psz_parser[i] != '\r') ||
246       (psz_parser[i+1] != '\n')); i++ )
247     {
248         ;
249     }
250
251     /* Check we actually parsed something */
252     if ( i+1 == i_size && psz_parser[i+1] != '\n' )
253     {
254         msg_Err( p_input, "stream not compliant with HTTP/1.x" );
255         return VLC_EGENERIC;
256     }
257
258     /* Store the line we just parsed and skip it */
259     psz_answer = strndup( psz_parser, i );
260     if( !psz_answer )
261     {
262         return VLC_ENOMEM;
263     }
264
265     p_input->p_current_data = psz_parser + i + 2;
266
267     /* Parse remaining headers */
268     for ( ; ; )
269     {
270         char psz_line[MAX_ANSWER_SIZE];
271
272         i_size = input_Peek( p_input, &p_bytes, MAX_ANSWER_SIZE );
273         psz_parser = (char *)p_bytes;
274
275         if( i_size <= 0 )
276         {
277             msg_Err( p_input, "not enough data" );
278             Close( VLC_OBJECT(p_input) );
279             free( psz_answer );
280             return VLC_EGENERIC;
281         }
282
283         /* Copy one line to psz_line */
284         i = 0;
285         while( i_size && psz_parser[i] != '\r'
286                       && psz_parser[i + 1] != '\n' )
287         {
288             psz_line[i] = psz_parser[i];
289             i++;
290             i_size--;
291         }
292         p_input->p_current_data = psz_parser + i + 2;
293         if( !i )
294         {
295             break; /* End of headers */
296         }
297         psz_line[i] = '\0';
298         psz_parser = strchr( psz_line, ':' );
299         if ( !psz_parser )
300         {
301             msg_Err( p_input, "malformed header line: %s", psz_line );
302             free( psz_answer );
303             return VLC_EGENERIC;
304         }
305         psz_parser[0] = '\0';
306         psz_parser++;
307         while ( *psz_parser == ' ' || *psz_parser == '\t' )
308         {
309             psz_parser++;
310         }
311         psz_value = psz_parser;
312
313         if( !strcasecmp( psz_line, "Content-Length" ) )
314         {
315             off_t i_size = 0;
316 #ifdef HAVE_ATOLL
317             i_size = i_tell + atoll( psz_value );
318 #else
319             int sign = 1;
320
321             if( *psz_value == '-' ) sign = -1;
322             while( *psz_value >= '0' && *psz_value <= '9' )
323             {
324                 i_size = i_size * 10 + *psz_value++ - '0';
325             }
326             i_size = i_tell + ( i_size * sign );
327 #endif
328             msg_Dbg( p_input, "stream size is "I64Fd, i_size );
329
330             vlc_mutex_lock( &p_input->stream.stream_lock );
331             p_input->stream.p_selected_area->i_size = i_size;
332             vlc_mutex_unlock( &p_input->stream.stream_lock );
333         }
334         /* Redirection support */
335         else if( ( i_code == 301 || i_code == 302 ||
336                    i_code == 303 || i_code == 307 )
337                     && !strcasecmp( psz_line, "location" ) )
338         {
339             playlist_t * p_playlist = (playlist_t *) vlc_object_find(
340                                   p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
341             if( !p_playlist )
342             {
343                 msg_Err( p_input, "redirection failed: can't find playlist" );
344                 free( psz_answer );
345                 return VLC_EGENERIC;
346             }
347             msg_Dbg( p_input, "%i %s: redirected to %s",
348                               i_code, psz_answer, psz_value );
349             p_playlist->pp_items[p_playlist->i_index]->b_autodeletion
350                                                                   = VLC_TRUE;
351             playlist_Add( p_playlist, psz_value, NULL, 0,
352                           PLAYLIST_INSERT | PLAYLIST_GO,
353                           p_playlist->i_index + 1 );
354             vlc_object_release( p_playlist );
355         }
356
357         /* TODO: parse other headers here */
358     }
359
360     /* Something went wrong */
361     if ( i_code >= 400 )
362     {
363         msg_Err( p_input, "%i %s", i_code, psz_answer );
364         p_input->p_current_data = psz_parser + i_size;
365         free( psz_answer );
366         return VLC_EGENERIC;
367     }
368
369     free( psz_answer );
370
371     /* Set final stream properties */
372     vlc_mutex_lock( &p_input->stream.stream_lock );
373     if( i_protocol == ICY_PROTOCOL )
374     {
375         p_input->stream.b_seekable = VLC_FALSE;
376     }
377     else
378     {
379         p_input->stream.b_seekable = VLC_TRUE;
380     }
381
382     if( p_input->stream.p_selected_area->i_size )
383     {
384         p_input->stream.p_selected_area->i_tell = i_tell;
385     }
386     else
387     {
388         p_input->stream.b_seekable = VLC_FALSE;
389     }
390     if( i_code != 206 )
391     {
392         p_input->stream.b_seekable = VLC_FALSE;
393     }
394     p_input->stream.b_changed = VLC_TRUE;
395     vlc_mutex_unlock( &p_input->stream.stream_lock );
396
397     return VLC_SUCCESS;
398 }
399
400 /*****************************************************************************
401  * Encode a string in base64
402  * Code borrowed from Rafael Steil
403  *****************************************************************************/
404 void encodeblock( unsigned char in[3], unsigned char out[4], int len )
405 {
406     static const char cb64[]
407         = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
408     out[0] = cb64[ in[0] >> 2 ];
409     out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
410     out[2] = (unsigned char) (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=');
411     out[3] = (unsigned char) (len > 2 ? cb64[ in[2] & 0x3f ] : '=');
412 }
413
414 char *str_base64_encode(char *psz_str, input_thread_t *p_input )
415 {
416     unsigned char in[3], out[4];
417     unsigned int i, len, blocksout = 0, linesize = strlen(psz_str);
418     char *psz_tmp = psz_str;
419     char *psz_result = (char *)malloc( linesize / 3 * 4 + 5 );
420
421     if( !psz_result )
422     {
423         msg_Err( p_input, "out of memory" );
424         return NULL;
425     }
426
427     while( *psz_tmp )
428     {
429         len = 0;
430
431         for( i = 0; i < 3; i++ )
432         {
433             in[i] = (unsigned char)*psz_tmp;
434
435             if (*psz_tmp)
436                 len++;
437             else
438                 in[i] = 0;
439
440             psz_tmp++;
441         }
442
443         if( len )
444         {
445             encodeblock( in, out, len );
446
447             for( i = 0; i < 4; i++ )
448             {
449                 psz_result[blocksout++] = out[i];
450             }
451         }
452     }
453
454     psz_result[blocksout] = '\0';
455     return psz_result;
456 }
457
458 /*****************************************************************************
459  * Open: parse URL and open the remote file at the beginning
460  *****************************************************************************/
461 static int Open( vlc_object_t *p_this )
462 {
463     input_thread_t *    p_input = (input_thread_t *)p_this;
464     _input_socket_t *   p_access_data;
465     char *              psz_name = strdup(p_input->psz_name);
466     char *              psz_parser = psz_name, * psz_auth_parser;
467     char *              psz_server_addr = "";
468     char *              psz_server_port = "";
469     char *              psz_path = "";
470     char *              psz_proxy, *psz_proxy_orig;
471     char *              psz_user = NULL, *psz_pwd = NULL;
472     int                 i_server_port = 0;
473     vlc_value_t         val;
474
475     p_access_data = malloc( sizeof(_input_socket_t) );
476     p_input->p_access_data = (access_sys_t *)p_access_data;
477     if( p_access_data == NULL )
478     {
479         msg_Err( p_input, "out of memory" );
480         free(psz_name);
481         return VLC_ENOMEM;
482     }
483
484     p_access_data->psz_name = psz_name;
485     p_access_data->psz_network = "";
486     memset(p_access_data->psz_auth_string, 0, MAX_QUERY_SIZE);
487
488     var_Create( p_input, "ipv4", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
489     var_Get( p_input, "ipv4", &val );
490     if( val.i_int )
491     {
492         p_access_data->psz_network = "ipv4";
493     }
494     var_Create( p_input, "ipv6", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
495     var_Get( p_input, "ipv6", &val );
496     if( val.i_int )
497     {
498         p_access_data->psz_network = "ipv6";
499     }
500     if( *p_input->psz_access )
501     {
502         /* Find out which shortcut was used */
503         if( !strncmp( p_input->psz_access, "http6", 6 ) )
504         {
505             p_access_data->psz_network = "ipv6";
506         }
507         else if( !strncmp( p_input->psz_access, "http4", 6 ) )
508         {
509             p_access_data->psz_network = "ipv4";
510         }
511     }
512
513     /* Parse psz_name syntax :
514      * //[user:password]@<hostname>[:<port>][/<path>] */
515
516
517     while( *psz_parser == '/' )
518     {
519         psz_parser++;
520     }
521     psz_auth_parser = psz_parser;
522
523     while ( *psz_auth_parser != '@' && *psz_auth_parser != '\0' )
524     {
525         psz_auth_parser++;
526     }
527     if ( *psz_auth_parser == '@' )
528     {
529         psz_user = psz_parser;
530         while ( *psz_parser != ':' && psz_parser < psz_auth_parser )
531         {
532             psz_parser++;
533         }
534         if ( psz_parser != psz_auth_parser )
535         {
536             *psz_parser = '\0';
537             psz_pwd = psz_parser + 1;
538         }
539         else
540         {
541             psz_pwd = "";
542         }
543         *psz_auth_parser = '\0';
544         psz_parser = psz_auth_parser + 1;
545     }
546
547     psz_server_addr = psz_parser;
548
549     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
550     {
551         if( *psz_parser == '[' )
552         {
553             /* IPv6 address */
554             while( *psz_parser && *psz_parser != ']' )
555             {
556                 psz_parser++;
557             }
558         }
559         psz_parser++;
560     }
561
562     if ( *psz_parser == ':' )
563     {
564         *psz_parser = '\0';
565         psz_parser++;
566         psz_server_port = psz_parser;
567
568         while( *psz_parser && *psz_parser != '/' )
569         {
570             psz_parser++;
571         }
572     }
573
574     if( *psz_parser == '/' )
575     {
576         *psz_parser = '\0';
577         psz_parser++;
578         psz_path = psz_parser;
579     }
580
581     /* Convert port format */
582     if( *psz_server_port )
583     {
584         i_server_port = strtol( psz_server_port, &psz_parser, 10 );
585         if( *psz_parser )
586         {
587             msg_Err( p_input, "cannot parse server port near %s", psz_parser );
588             free( p_input->p_access_data );
589             free( psz_name );
590             return VLC_EGENERIC;
591         }
592     }
593
594     if( i_server_port == 0 )
595     {
596         i_server_port = 80;
597     }
598
599     if( !*psz_server_addr )
600     {
601         msg_Err( p_input, "no server given" );
602         free( p_input->p_access_data );
603         free( psz_name );
604         return VLC_EGENERIC;
605     }
606
607     /* Handle autehtification */
608
609     if ( psz_user == NULL )
610     {
611         var_Create( p_input, "http-user", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
612         var_Get( p_input, "http-user", &val );
613         psz_user = val.psz_string;
614
615         var_Create( p_input, "http-pwd", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
616         var_Get( p_input, "http-pwd", &val );
617         psz_pwd = val.psz_string;
618     }
619
620     if (psz_user != NULL)
621     {
622         char psz_user_pwd[MAX_QUERY_SIZE];
623         msg_Dbg( p_input, "authenticating, user=%s, password=%s",
624                                            psz_user, psz_pwd );
625         snprintf( psz_user_pwd, MAX_QUERY_SIZE, "%s:%s", psz_user, psz_pwd );
626         snprintf( p_access_data->psz_auth_string, MAX_QUERY_SIZE,
627                   "Authorization: Basic %s\r\n",
628                   str_base64_encode( psz_user_pwd, p_input ) );
629     }
630
631     /* Check proxy config variable */
632     var_Create( p_input, "http-proxy", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
633     var_Get( p_input, "http-proxy", &val );
634     psz_proxy_orig = val.psz_string;
635     if( psz_proxy_orig == NULL )
636     {
637         /* Check proxy environment variable */
638         psz_proxy_orig = getenv( "http_proxy" );
639         if( psz_proxy_orig != NULL )
640         {
641             psz_proxy_orig = strdup( psz_proxy_orig );
642         }
643     }
644
645     psz_proxy = psz_proxy_orig;
646     if( psz_proxy != NULL && *psz_proxy )
647     {
648         /* http://myproxy.mydomain:myport/ */
649         int i_proxy_port = 0;
650
651         /* Skip the protocol name */
652         while( *psz_proxy && *psz_proxy != ':' )
653         {
654             psz_proxy++;
655         }
656
657         /* Skip the "://" part */
658         while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
659         {
660             psz_proxy++;
661         }
662
663         /* Found a proxy name */
664         if( *psz_proxy )
665         {
666             char *psz_port = psz_proxy;
667
668             /* Skip the hostname part */
669             while( *psz_port && *psz_port != ':' && *psz_port != '/' )
670             {
671                 psz_port++;
672             }
673
674             /* Found a port name */
675             if( *psz_port )
676             {
677                 char * psz_junk;
678
679                 /* Replace ':' with '\0' */
680                 *psz_port = '\0';
681                 psz_port++;
682
683                 psz_junk = psz_port;
684                 while( *psz_junk && *psz_junk != '/' )
685                 {
686                     psz_junk++;
687                 }
688
689                 if( *psz_junk )
690                 {
691                     *psz_junk = '\0';
692                 }
693
694                 if( *psz_port != '\0' )
695                 {
696                     i_proxy_port = atoi( psz_port );
697                 }
698             }
699
700             psz_proxy = strdup( psz_proxy );
701
702             msg_Dbg( p_input, "using HTTP proxy server=%s port=%d",
703                      psz_proxy, i_proxy_port );
704         }
705         else
706         {
707             msg_Err( p_input, "HTTP proxy %s is invalid!", psz_proxy_orig );
708             free( p_input->p_access_data );
709             free( psz_name );
710             if( psz_proxy_orig ) free( psz_proxy_orig );
711             return VLC_EGENERIC;
712         }
713
714         if( psz_proxy_orig ) free( psz_proxy_orig );
715
716         p_access_data->socket_desc.psz_server_addr = psz_proxy;
717         p_access_data->socket_desc.i_server_port = i_proxy_port;
718         p_access_data->socket_desc.i_type = NETWORK_TCP;
719         p_access_data->socket_desc.i_ttl           = 0;
720
721         snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
722                   "GET http://%s:%d/%s HTTP/1.0\r\n",
723                   psz_server_addr, i_server_port, psz_path );
724     }
725     else
726     {
727         /* No proxy, direct connection. */
728         p_access_data->socket_desc.i_type = NETWORK_TCP;
729         p_access_data->socket_desc.psz_server_addr = psz_server_addr;
730         p_access_data->socket_desc.i_server_port = i_server_port;
731         p_access_data->socket_desc.i_ttl           = 0;
732
733         snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
734                   "GET /%s HTTP/1.1\r\nHost: %s\r\n",
735                   psz_path, psz_server_addr );
736     }
737     p_access_data->psz_buffer[MAX_QUERY_SIZE - 1] = '\0';
738
739     msg_Dbg( p_input, "opening server=%s port=%d path=%s",
740                       psz_server_addr, i_server_port, psz_path );
741
742     p_input->pf_read = Read;
743     p_input->pf_set_program = input_SetProgram;
744     p_input->pf_set_area = NULL;
745     p_input->pf_seek = Seek;
746
747     vlc_mutex_lock( &p_input->stream.stream_lock );
748     p_input->stream.b_pace_control = VLC_TRUE;
749     p_input->stream.b_seekable = VLC_TRUE;
750     p_input->stream.p_selected_area->i_tell = 0;
751     p_input->stream.p_selected_area->i_size = 0;
752     p_input->stream.i_method = INPUT_METHOD_NETWORK;
753     vlc_mutex_unlock( &p_input->stream.stream_lock );
754     p_input->i_mtu = 0;
755
756     if( HTTPConnect( p_input, 0 ) )
757     {
758         /* Request failed, try again with HTTP/1.0 */
759         char * psz_pos = strstr( p_access_data->psz_buffer, "HTTP/1.1" );
760
761         if( !psz_pos )
762         {
763             return VLC_EGENERIC;
764         }
765
766         p_input->stream.b_seekable = VLC_FALSE;
767         psz_pos[7] = '0';
768         if( HTTPConnect( p_input, 0 ) )
769         {
770             free( p_input->p_access_data );
771             free( psz_name );
772             return VLC_EGENERIC;
773         }
774     }
775
776     /* Update default_pts to a suitable value for http access */
777
778     var_Create( p_input, "http-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
779     var_Get( p_input, "http-caching", &val );
780     p_input->i_pts_delay = val.i_int * 1000;
781
782     return VLC_SUCCESS;
783 }
784
785 /*****************************************************************************
786  * Close: free unused data structures
787  *****************************************************************************/
788 static void Close( vlc_object_t *p_this )
789 {
790     input_thread_t *  p_input = (input_thread_t *)p_this;
791     int i_handle = ((network_socket_t *)p_input->p_access_data)->i_handle;
792     _input_socket_t * p_access_data =
793         (_input_socket_t *)p_input->p_access_data;
794
795     free( p_access_data->psz_name );
796
797     msg_Info( p_input, "closing HTTP target `%s'", p_input->psz_source );
798
799 #if defined( WIN32 ) || defined( UNDER_CE )
800     closesocket( i_handle );
801 #else
802     close( i_handle );
803 #endif
804
805     free( p_access_data );
806 }
807
808 /*****************************************************************************
809  * Seek: close and re-open a connection at the right place
810  *****************************************************************************/
811 static void Seek( input_thread_t * p_input, off_t i_pos )
812 {
813     _input_socket_t *p_access_data = (_input_socket_t*)p_input->p_access_data;
814 #if defined( WIN32 ) || defined( UNDER_CE )
815     closesocket( p_access_data->_socket.i_handle );
816 #else
817     close( p_access_data->_socket.i_handle );
818 #endif
819     msg_Dbg( p_input, "seeking to position "I64Fd, i_pos );
820     HTTPConnect( p_input, i_pos );
821 }
822
823 /*****************************************************************************
824  * Read: Read up to i_len bytes from the http connection and place in
825  * p_buffer. Return the actual number of bytes read
826  *****************************************************************************/
827 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
828 {
829     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
830     struct timeval  timeout;
831     fd_set          fds;
832     ssize_t         i_recv;
833     int             i_ret;
834
835     /* Initialize file descriptor set */
836     FD_ZERO( &fds );
837     FD_SET( p_access_data->i_handle, &fds );
838
839     /* We'll wait 0.5 second if nothing happens */
840     timeout.tv_sec = 0;
841     timeout.tv_usec = 500000;
842
843     /* Find if some data is available */
844     while( (i_ret = select( p_access_data->i_handle + 1, &fds,
845                             NULL, NULL, &timeout )) == 0
846 #ifdef HAVE_ERRNO_H
847            || (i_ret < 0 && errno == EINTR)
848 #endif
849            )
850     {
851         FD_ZERO( &fds );
852         FD_SET( p_access_data->i_handle, &fds );
853         timeout.tv_sec = 0;
854         timeout.tv_usec = 500000;
855
856         if( p_input->b_die || p_input->b_error )
857         {
858             return 0;
859         }
860     }
861
862     if( i_ret < 0 )
863     {
864         msg_Err( p_input, "network select error" );
865         return -1;
866     }
867
868     i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
869
870     if( i_recv < 0 )
871     {
872 #ifdef HAVE_ERRNO_H
873         msg_Err( p_input, "recv failed (%s)", strerror(errno) );
874 #else
875         msg_Err( p_input, "recv failed" );
876 #endif
877     }
878
879     return i_recv;
880 }