]> git.sesse.net Git - vlc/blob - modules/access/http.c
Removed useless stuff for icecast
[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.45 2003/09/10 15:50:25 zorglub 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     }
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 #ifdef HAVE_ATOLL
329             i_size = i_tell + atoll( psz_value );
330 #else
331             int sign = 1;
332
333             if( *psz_value == '-' ) sign = -1;
334             while( *psz_value >= '0' && *psz_value <= '9' )
335             {
336                 i_size = i_size * 10 + *psz_value++ - '0';
337             }
338             i_size = i_tell + ( i_size * sign );
339 #endif
340             msg_Dbg( p_input, "stream size is "I64Fd, i_size );
341
342             vlc_mutex_lock( &p_input->stream.stream_lock );
343             p_input->stream.p_selected_area->i_size = i_size;
344             vlc_mutex_unlock( &p_input->stream.stream_lock );
345         }
346         /* Redirection support */
347         else if( ( i_code == 301 || i_code == 302 ||
348                    i_code == 303 || i_code == 307 )
349                     && !strcasecmp( psz_line, "location" ) )
350         {
351             playlist_t * p_playlist = (playlist_t *) vlc_object_find(
352                                   p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
353             if( !p_playlist )
354             {
355                 msg_Err( p_input, "redirection failed: can't find playlist" );
356                 free( psz_answer );
357                 return VLC_EGENERIC;
358             }
359             msg_Dbg( p_input, "%i %s: redirected to %s",
360                               i_code, psz_answer, psz_value );
361             p_playlist->pp_items[p_playlist->i_index]->b_autodeletion
362                                                                   = VLC_TRUE;
363             playlist_Add( p_playlist, psz_value, NULL, 0,
364                           PLAYLIST_INSERT | PLAYLIST_GO,
365                           p_playlist->i_index + 1 );
366             vlc_object_release( p_playlist );
367         }
368
369         /* TODO: parse other headers here */
370     }
371
372     /* Something went wrong */
373     if ( i_code >= 400 )
374     {
375         msg_Err( p_input, "%i %s", i_code, psz_answer );
376         p_input->p_current_data = psz_parser + i_size;
377         free( psz_answer );
378         return VLC_EGENERIC;
379     }
380
381     free( psz_answer );
382
383     /* Set final stream properties */
384     vlc_mutex_lock( &p_input->stream.stream_lock );
385     if( i_protocol == ICY_PROTOCOL )
386     {
387         p_input->stream.b_seekable = VLC_FALSE;
388     }
389     else
390     {
391         p_input->stream.b_seekable = VLC_TRUE;
392     }
393
394     if( p_input->stream.p_selected_area->i_size )
395     {
396         p_input->stream.p_selected_area->i_tell = i_tell;
397     }
398     else
399     {
400         p_input->stream.b_seekable = VLC_FALSE;
401     }
402     if( i_code != 206 )
403     {
404         p_input->stream.b_seekable = VLC_FALSE;
405     }
406     p_input->stream.b_changed = VLC_TRUE;
407     vlc_mutex_unlock( &p_input->stream.stream_lock );
408
409     return VLC_SUCCESS;
410 }
411
412 /*****************************************************************************
413  * Encode a string in base64
414  * Code borrowed from Rafael Steil
415  *****************************************************************************/
416 void encodeblock( unsigned char in[3], unsigned char out[4], int len )
417 {
418     static const char cb64[]
419         = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
420     out[0] = cb64[ in[0] >> 2 ];
421     out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
422     out[2] = (unsigned char) (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=');
423     out[3] = (unsigned char) (len > 2 ? cb64[ in[2] & 0x3f ] : '=');
424 }
425
426 char *str_base64_encode(char *psz_str, input_thread_t *p_input )
427 {
428     unsigned char in[3], out[4];
429     unsigned int i, len, blocksout = 0, linesize = strlen(psz_str);
430     char *psz_tmp = psz_str;
431     char *psz_result = (char *)malloc( linesize / 3 * 4 + 5 );
432
433     if( !psz_result )
434     {
435         msg_Err( p_input, "out of memory" );
436         return NULL;
437     }
438
439     while( *psz_tmp )
440     {
441         len = 0;
442
443         for( i = 0; i < 3; i++ )
444         {
445             in[i] = (unsigned char)*psz_tmp;
446
447             if (*psz_tmp)
448                 len++;
449             else
450                 in[i] = 0;
451
452             psz_tmp++;
453         }
454
455         if( len )
456         {
457             encodeblock( in, out, len );
458
459             for( i = 0; i < 4; i++ )
460             {
461                 psz_result[blocksout++] = out[i];
462             }
463         }
464     }
465
466     psz_result[blocksout] = '\0';
467     return psz_result;
468 }
469
470 /*****************************************************************************
471  * Open: parse URL and open the remote file at the beginning
472  *****************************************************************************/
473 static int Open( vlc_object_t *p_this )
474 {
475     input_thread_t *    p_input = (input_thread_t *)p_this;
476     _input_socket_t *   p_access_data;
477     char *              psz_name = strdup(p_input->psz_name);
478     char *              psz_parser = psz_name, * psz_auth_parser;
479     char *              psz_server_addr = "";
480     char *              psz_server_port = "";
481     char *              psz_path = "";
482     char *              psz_proxy, *psz_proxy_orig;
483     char *              psz_user = NULL, *psz_pwd = NULL;
484     int                 i_server_port = 0;
485     vlc_value_t         val;
486
487     p_access_data = malloc( sizeof(_input_socket_t) );
488     p_input->p_access_data = (access_sys_t *)p_access_data;
489     if( p_access_data == NULL )
490     {
491         msg_Err( p_input, "out of memory" );
492         free(psz_name);
493         return VLC_ENOMEM;
494     }
495
496     p_access_data->psz_name = psz_name;
497     p_access_data->psz_network = "";
498     memset(p_access_data->psz_auth_string, 0, MAX_QUERY_SIZE);
499
500     var_Create( p_input, "ipv4", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
501     var_Get( p_input, "ipv4", &val );
502     if( val.i_int )
503     {
504         p_access_data->psz_network = "ipv4";
505     }
506     var_Create( p_input, "ipv6", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
507     var_Get( p_input, "ipv6", &val );
508     if( val.i_int )
509     {
510         p_access_data->psz_network = "ipv6";
511     }
512     if( *p_input->psz_access )
513     {
514         /* Find out which shortcut was used */
515         if( !strncmp( p_input->psz_access, "http6", 6 ) )
516         {
517             p_access_data->psz_network = "ipv6";
518         }
519         else if( !strncmp( p_input->psz_access, "http4", 6 ) )
520         {
521             p_access_data->psz_network = "ipv4";
522         }
523     }
524
525     /* Parse psz_name syntax :
526      * //[user:password]@<hostname>[:<port>][/<path>] */
527
528
529     while( *psz_parser == '/' )
530     {
531         psz_parser++;
532     }
533     psz_auth_parser = psz_parser;
534
535     while ( *psz_auth_parser != '@' && *psz_auth_parser != '\0' )
536     {
537         psz_auth_parser++;
538     }
539     if ( *psz_auth_parser == '@' )
540     {
541         psz_user = psz_parser;
542         while ( *psz_parser != ':' && psz_parser < psz_auth_parser )
543         {
544             psz_parser++;
545         }
546         if ( psz_parser != psz_auth_parser )
547         {
548             *psz_parser = '\0';
549             psz_pwd = psz_parser + 1;
550         }
551         else
552         {
553             psz_pwd = "";
554         }
555         *psz_auth_parser = '\0';
556         psz_parser = psz_auth_parser + 1;
557     }
558
559     psz_server_addr = psz_parser;
560
561     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
562     {
563         if( *psz_parser == '[' )
564         {
565             /* IPv6 address */
566             while( *psz_parser && *psz_parser != ']' )
567             {
568                 psz_parser++;
569             }
570         }
571         psz_parser++;
572     }
573
574     if ( *psz_parser == ':' )
575     {
576         *psz_parser = '\0';
577         psz_parser++;
578         psz_server_port = psz_parser;
579
580         while( *psz_parser && *psz_parser != '/' )
581         {
582             psz_parser++;
583         }
584     }
585
586     if( *psz_parser == '/' )
587     {
588         *psz_parser = '\0';
589         psz_parser++;
590         psz_path = psz_parser;
591     }
592
593     /* Convert port format */
594     if( *psz_server_port )
595     {
596         i_server_port = strtol( psz_server_port, &psz_parser, 10 );
597         if( *psz_parser )
598         {
599             msg_Err( p_input, "cannot parse server port near %s", psz_parser );
600             free( p_input->p_access_data );
601             free( psz_name );
602             return VLC_EGENERIC;
603         }
604     }
605
606     if( i_server_port == 0 )
607     {
608         i_server_port = 80;
609     }
610
611     if( !*psz_server_addr )
612     {
613         msg_Err( p_input, "no server given" );
614         free( p_input->p_access_data );
615         free( psz_name );
616         return VLC_EGENERIC;
617     }
618
619     /* Handle autehtification */
620
621    if ( !psz_user )
622     {
623         var_Create( p_input, "http-user", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
624         var_Get( p_input, "http-user", &val );
625         psz_user = val.psz_string;
626
627         var_Create( p_input, "http-pwd", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
628         var_Get( p_input, "http-pwd", &val );
629         psz_pwd = val.psz_string;
630     }
631
632     if ( *psz_user )
633     {
634         char psz_user_pwd[MAX_QUERY_SIZE];
635         msg_Dbg( p_input, "authenticating, user=%s, password=%s",
636                                            psz_user, psz_pwd );
637         snprintf( psz_user_pwd, MAX_QUERY_SIZE, "%s:%s", psz_user, psz_pwd );
638         snprintf( p_access_data->psz_auth_string, MAX_QUERY_SIZE,
639                   "Authorization: Basic %s\r\n",
640                   str_base64_encode( psz_user_pwd, p_input ) );
641     }
642
643     /* Check proxy config variable */
644     var_Create( p_input, "http-proxy", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
645     var_Get( p_input, "http-proxy", &val );
646     psz_proxy_orig = val.psz_string;
647     if( psz_proxy_orig == NULL )
648     {
649         /* Check proxy environment variable */
650         psz_proxy_orig = getenv( "http_proxy" );
651         if( psz_proxy_orig != NULL )
652         {
653             psz_proxy_orig = strdup( psz_proxy_orig );
654         }
655     }
656
657     psz_proxy = psz_proxy_orig;
658     if( psz_proxy != NULL && *psz_proxy )
659     {
660         /* http://myproxy.mydomain:myport/ */
661         int i_proxy_port = 0;
662
663         /* Skip the protocol name */
664         while( *psz_proxy && *psz_proxy != ':' )
665         {
666             psz_proxy++;
667         }
668
669         /* Skip the "://" part */
670         while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
671         {
672             psz_proxy++;
673         }
674
675         /* Found a proxy name */
676         if( *psz_proxy )
677         {
678             char *psz_port = psz_proxy;
679
680             /* Skip the hostname part */
681             while( *psz_port && *psz_port != ':' && *psz_port != '/' )
682             {
683                 psz_port++;
684             }
685
686             /* Found a port name */
687             if( *psz_port )
688             {
689                 char * psz_junk;
690
691                 /* Replace ':' with '\0' */
692                 *psz_port = '\0';
693                 psz_port++;
694
695                 psz_junk = psz_port;
696                 while( *psz_junk && *psz_junk != '/' )
697                 {
698                     psz_junk++;
699                 }
700
701                 if( *psz_junk )
702                 {
703                     *psz_junk = '\0';
704                 }
705
706                 if( *psz_port != '\0' )
707                 {
708                     i_proxy_port = atoi( psz_port );
709                 }
710             }
711
712             psz_proxy = strdup( psz_proxy );
713
714             msg_Dbg( p_input, "using HTTP proxy server=%s port=%d",
715                      psz_proxy, i_proxy_port );
716         }
717         else
718         {
719             msg_Err( p_input, "HTTP proxy %s is invalid!", psz_proxy_orig );
720             free( p_input->p_access_data );
721             free( psz_name );
722             if( psz_proxy_orig ) free( psz_proxy_orig );
723             return VLC_EGENERIC;
724         }
725
726         if( psz_proxy_orig ) free( psz_proxy_orig );
727
728         p_access_data->socket_desc.psz_server_addr = psz_proxy;
729         p_access_data->socket_desc.i_server_port = i_proxy_port;
730         p_access_data->socket_desc.i_type = NETWORK_TCP;
731         p_access_data->socket_desc.i_ttl           = 0;
732
733         snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
734                   "GET http://%s:%d/%s HTTP/1.0\r\n",
735                   psz_server_addr, i_server_port, psz_path );
736     }
737     else
738     {
739         /* No proxy, direct connection. */
740         p_access_data->socket_desc.i_type = NETWORK_TCP;
741         p_access_data->socket_desc.psz_server_addr = psz_server_addr;
742         p_access_data->socket_desc.i_server_port = i_server_port;
743         p_access_data->socket_desc.i_ttl           = 0;
744
745         snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
746                   "GET /%s HTTP/1.1\r\nHost: %s\r\n",
747                   psz_path, psz_server_addr );
748     }
749     p_access_data->psz_buffer[MAX_QUERY_SIZE - 1] = '\0';
750
751     msg_Dbg( p_input, "opening server=%s port=%d path=%s",
752                       psz_server_addr, i_server_port, psz_path );
753
754     p_input->pf_read = Read;
755     p_input->pf_set_program = input_SetProgram;
756     p_input->pf_set_area = NULL;
757     p_input->pf_seek = Seek;
758
759     vlc_mutex_lock( &p_input->stream.stream_lock );
760     p_input->stream.b_pace_control = VLC_TRUE;
761     p_input->stream.b_seekable = VLC_TRUE;
762     p_input->stream.p_selected_area->i_tell = 0;
763     p_input->stream.p_selected_area->i_size = 0;
764     p_input->stream.i_method = INPUT_METHOD_NETWORK;
765     vlc_mutex_unlock( &p_input->stream.stream_lock );
766     p_input->i_mtu = 0;
767
768     if( HTTPConnect( p_input, 0 ) )
769     {
770         /* Request failed, try again with HTTP/1.0 */
771         char * psz_pos = strstr( p_access_data->psz_buffer, "HTTP/1.1" );
772
773         if( !psz_pos )
774         {
775             return VLC_EGENERIC;
776         }
777
778         p_input->stream.b_seekable = VLC_FALSE;
779         psz_pos[7] = '0';
780         if( HTTPConnect( p_input, 0 ) )
781         {
782             free( p_input->p_access_data );
783             free( psz_name );
784             return VLC_EGENERIC;
785         }
786     }
787
788     /* Update default_pts to a suitable value for http access */
789
790     var_Create( p_input, "http-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
791     var_Get( p_input, "http-caching", &val );
792     p_input->i_pts_delay = val.i_int * 1000;
793
794     return VLC_SUCCESS;
795 }
796
797 /*****************************************************************************
798  * Close: free unused data structures
799  *****************************************************************************/
800 static void Close( vlc_object_t *p_this )
801 {
802     input_thread_t *  p_input = (input_thread_t *)p_this;
803     int i_handle = ((network_socket_t *)p_input->p_access_data)->i_handle;
804     _input_socket_t * p_access_data =
805         (_input_socket_t *)p_input->p_access_data;
806
807     free( p_access_data->psz_name );
808
809     msg_Info( p_input, "closing HTTP target `%s'", p_input->psz_source );
810
811 #if defined( WIN32 ) || defined( UNDER_CE )
812     closesocket( i_handle );
813 #else
814     close( i_handle );
815 #endif
816
817     free( p_access_data );
818 }
819
820 /*****************************************************************************
821  * Seek: close and re-open a connection at the right place
822  *****************************************************************************/
823 static void Seek( input_thread_t * p_input, off_t i_pos )
824 {
825     _input_socket_t *p_access_data = (_input_socket_t*)p_input->p_access_data;
826 #if defined( WIN32 ) || defined( UNDER_CE )
827     closesocket( p_access_data->_socket.i_handle );
828 #else
829     close( p_access_data->_socket.i_handle );
830 #endif
831     msg_Dbg( p_input, "seeking to position "I64Fd, i_pos );
832     HTTPConnect( p_input, i_pos );
833 }
834
835 /*****************************************************************************
836  * Read: Read up to i_len bytes from the http connection and place in
837  * p_buffer. Return the actual number of bytes read
838  *****************************************************************************/
839 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
840 {
841     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
842     struct timeval  timeout;
843     fd_set          fds;
844     ssize_t         i_recv;
845     int             i_ret;
846
847     /* Initialize file descriptor set */
848     FD_ZERO( &fds );
849     FD_SET( p_access_data->i_handle, &fds );
850
851     /* We'll wait 0.5 second if nothing happens */
852     timeout.tv_sec = 0;
853     timeout.tv_usec = 500000;
854
855     /* Find if some data is available */
856     while( (i_ret = select( p_access_data->i_handle + 1, &fds,
857                             NULL, NULL, &timeout )) == 0
858 #ifdef HAVE_ERRNO_H
859            || (i_ret < 0 && errno == EINTR)
860 #endif
861            )
862     {
863         FD_ZERO( &fds );
864         FD_SET( p_access_data->i_handle, &fds );
865         timeout.tv_sec = 0;
866         timeout.tv_usec = 500000;
867
868         if( p_input->b_die || p_input->b_error )
869         {
870             return 0;
871         }
872     }
873
874     if( i_ret < 0 )
875     {
876         msg_Err( p_input, "network select error" );
877         return -1;
878     }
879
880     i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
881
882     if( i_recv < 0 )
883     {
884 #ifdef HAVE_ERRNO_H
885         msg_Err( p_input, "recv failed (%s)", strerror(errno) );
886 #else
887         msg_Err( p_input, "recv failed" );
888 #endif
889     }
890
891     return i_recv;
892 }