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