]> git.sesse.net Git - vlc/blob - modules/access/http.c
4c63a4daa3b787b022b7c9eab1fe3fe3d54f5c10
[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.39 2003/07/31 18:25:12 bigben 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
405
406
407 void encodeblock( unsigned char in[3], unsigned char out[4], int len )
408 {
409     static const char cb64[]="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 *str, input_thread_t *p_input )
417 {
418     unsigned char in[3], out[4];
419     unsigned int i, len, blocksout = 0, linesize = strlen(str);
420         char *tmp = str;
421         char *result = (char *)malloc((linesize + 3 - linesize % 3) * 4 / 3 + 1);
422         
423         if (!result)
424         msg_Err( p_input, "out of memory" );            
425         while (*tmp) {
426         len = 0;
427                 
428         for( i = 0; i < 3; i++ ) {
429             in[i] = (unsigned char)(*tmp);
430                         
431             if (*tmp)
432                 len++;
433             else
434                 in[i] = 0;
435                         
436                         tmp++;
437         }
438                 
439         if( len ) {
440             encodeblock( in, out, len);
441                         
442             for( i = 0; i < 4; i++ ) 
443                 result[blocksout++] = out[i];
444         }               
445     }
446         
447         result[blocksout] = '\0';
448         return result;
449 }
450
451
452 /*****************************************************************************
453  * Open: parse URL and open the remote file at the beginning
454  *****************************************************************************/
455 static int Open( vlc_object_t *p_this )
456 {
457     input_thread_t *    p_input = (input_thread_t *)p_this;
458     _input_socket_t *   p_access_data;
459     char *              psz_name = strdup(p_input->psz_name);
460     char *              psz_parser = psz_name, * psz_auth_parser;
461     char *              psz_server_addr = "";
462     char *              psz_server_port = "";
463     char *              psz_path = "";
464     char *              psz_proxy, *psz_proxy_orig;
465     char *              psz_user = NULL, *psz_pwd = NULL;
466     int                 i_server_port = 0;
467
468     p_access_data = malloc( sizeof(_input_socket_t) );
469     p_input->p_access_data = (access_sys_t *)p_access_data;
470     if( p_access_data == NULL )
471     {
472         msg_Err( p_input, "out of memory" );
473         free(psz_name);
474         return VLC_ENOMEM;
475     }
476
477     p_access_data->psz_name = psz_name;
478     p_access_data->psz_network = "";
479     memset(p_access_data->psz_auth_string, 0, MAX_QUERY_SIZE);
480     if( config_GetInt( p_input, "ipv4" ) )
481     {
482         p_access_data->psz_network = "ipv4";
483     }
484     if( config_GetInt( p_input, "ipv6" ) )
485     {
486         p_access_data->psz_network = "ipv6";
487     }
488     if( *p_input->psz_access )
489     {
490         /* Find out which shortcut was used */
491         if( !strncmp( p_input->psz_access, "http6", 6 ) )
492         {
493             p_access_data->psz_network = "ipv6";
494         }
495         else if( !strncmp( p_input->psz_access, "http4", 6 ) )
496         {
497             p_access_data->psz_network = "ipv4";
498         }
499     }
500
501     /* Parse psz_name syntax :
502      * //[user:password]@<hostname>[:<port>][/<path>] */
503
504
505     while( *psz_parser == '/' )
506     {
507         psz_parser++;
508     }
509     psz_auth_parser = psz_parser;
510
511     while ( *psz_auth_parser != '@' && *psz_auth_parser != '\0' )
512     {
513         psz_auth_parser++;
514     }
515     if ( *psz_auth_parser == '@' )
516     {
517         psz_user = psz_parser;
518         while ( *psz_parser != ':' && psz_parser < psz_auth_parser )
519         {
520             psz_parser++;
521         }
522         if ( psz_parser != psz_auth_parser )
523         {
524             *psz_parser = '\0';
525             psz_pwd = psz_parser + 1;
526         }
527         else
528         {
529             psz_pwd = "";
530         }
531         *psz_auth_parser = '\0';
532         psz_parser = psz_auth_parser + 1;
533     }
534
535     psz_server_addr = psz_parser;
536
537     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
538     {
539         if( *psz_parser == '[' )
540         {
541             /* IPv6 address */
542             while( *psz_parser && *psz_parser != ']' )
543             {
544                 psz_parser++;
545             }
546         }
547         psz_parser++;
548     }
549
550     if ( *psz_parser == ':' )
551     {
552         *psz_parser = '\0';
553         psz_parser++;
554         psz_server_port = psz_parser;
555
556         while( *psz_parser && *psz_parser != '/' )
557         {
558             psz_parser++;
559         }
560     }
561
562     if( *psz_parser == '/' )
563     {
564         *psz_parser = '\0';
565         psz_parser++;
566         psz_path = psz_parser;
567     }
568
569     /* Convert port format */
570     if( *psz_server_port )
571     {
572         i_server_port = strtol( psz_server_port, &psz_parser, 10 );
573         if( *psz_parser )
574         {
575             msg_Err( p_input, "cannot parse server port near %s", psz_parser );
576             free( p_input->p_access_data );
577             free( psz_name );
578             return VLC_EGENERIC;
579         }
580     }
581
582     if( i_server_port == 0 )
583     {
584         i_server_port = 80;
585     }
586
587     if( !*psz_server_addr )
588     {
589         msg_Err( p_input, "no server given" );
590         free( p_input->p_access_data );
591         free( psz_name );
592         return VLC_EGENERIC;
593     }
594
595     /* Handle autehtification */
596
597     if ( psz_user == NULL )
598     {
599         psz_user = config_GetPsz( p_input, "http-user" );
600         psz_pwd = config_GetPsz( p_input, "http-pwd" );
601     } 
602
603     if (psz_user != NULL)
604     {
605         msg_Dbg( p_input, "Authentificating, user=%s, password=%s", 
606                                             psz_user, psz_pwd );
607         char psz_user_pwd[MAX_QUERY_SIZE];
608         snprintf( psz_user_pwd, MAX_QUERY_SIZE, "%s:%s", psz_user, psz_pwd );
609         snprintf( p_access_data->psz_auth_string, MAX_QUERY_SIZE, 
610                 "Authorization: Basic %s\r\n", 
611                 str_base64_encode( psz_user_pwd, p_input ) );
612     }
613
614     /* Check proxy config variable */
615     psz_proxy_orig = config_GetPsz( p_input, "http-proxy" );
616     if( psz_proxy_orig == NULL )
617     {
618         /* Check proxy environment variable */
619         psz_proxy_orig = getenv( "http_proxy" );
620         if( psz_proxy_orig != NULL )
621         {
622             psz_proxy_orig = strdup( psz_proxy_orig );
623         }
624     }
625
626     psz_proxy = psz_proxy_orig;
627     if( psz_proxy != NULL && *psz_proxy )
628     {
629         /* http://myproxy.mydomain:myport/ */
630         int i_proxy_port = 0;
631
632         /* Skip the protocol name */
633         while( *psz_proxy && *psz_proxy != ':' )
634         {
635             psz_proxy++;
636         }
637
638         /* Skip the "://" part */
639         while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
640         {
641             psz_proxy++;
642         }
643
644         /* Found a proxy name */
645         if( *psz_proxy )
646         {
647             char *psz_port = psz_proxy;
648
649             /* Skip the hostname part */
650             while( *psz_port && *psz_port != ':' && *psz_port != '/' )
651             {
652                 psz_port++;
653             }
654
655             /* Found a port name */
656             if( *psz_port )
657             {
658                 char * psz_junk;
659
660                 /* Replace ':' with '\0' */
661                 *psz_port = '\0';
662                 psz_port++;
663
664                 psz_junk = psz_port;
665                 while( *psz_junk && *psz_junk != '/' )
666                 {
667                     psz_junk++;
668                 }
669
670                 if( *psz_junk )
671                 {
672                     *psz_junk = '\0';
673                 }
674
675                 if( *psz_port != '\0' )
676                 {
677                     i_proxy_port = atoi( psz_port );
678                 }
679             }
680
681             psz_proxy = strdup( psz_proxy );
682
683             msg_Dbg( p_input, "using HTTP proxy server=%s port=%d",
684                      psz_proxy, i_proxy_port );
685         }
686         else
687         {
688             msg_Err( p_input, "HTTP proxy %s is invalid!", psz_proxy_orig );
689             free( p_input->p_access_data );
690             free( psz_name );
691             if( psz_proxy_orig ) free( psz_proxy_orig );
692             return VLC_EGENERIC;
693         }
694
695         if( psz_proxy_orig ) free( psz_proxy_orig );
696
697         p_access_data->socket_desc.psz_server_addr = psz_proxy;
698         p_access_data->socket_desc.i_server_port = i_proxy_port;
699         p_access_data->socket_desc.i_type = NETWORK_TCP;
700
701         snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
702                   "GET http://%s:%d/%s HTTP/1.0\r\n",
703                   psz_server_addr, i_server_port, psz_path );
704     }
705     else
706     {
707         /* No proxy, direct connection. */
708         p_access_data->socket_desc.i_type = NETWORK_TCP;
709         p_access_data->socket_desc.psz_server_addr = psz_server_addr;
710         p_access_data->socket_desc.i_server_port = i_server_port;
711
712         snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
713                   "GET /%s HTTP/1.1\r\nHost: %s\r\n",
714                   psz_path, psz_server_addr );
715     }
716     p_access_data->psz_buffer[MAX_QUERY_SIZE - 1] = '\0';
717
718     msg_Dbg( p_input, "opening server=%s port=%d path=%s",
719                       psz_server_addr, i_server_port, psz_path );
720
721     p_input->pf_read = Read;
722     p_input->pf_set_program = input_SetProgram;
723     p_input->pf_set_area = NULL;
724     p_input->pf_seek = Seek;
725
726     vlc_mutex_lock( &p_input->stream.stream_lock );
727     p_input->stream.b_pace_control = VLC_TRUE;
728     p_input->stream.b_seekable = VLC_TRUE;
729     p_input->stream.p_selected_area->i_tell = 0;
730     p_input->stream.p_selected_area->i_size = 0;
731     p_input->stream.i_method = INPUT_METHOD_NETWORK;
732     vlc_mutex_unlock( &p_input->stream.stream_lock );
733     p_input->i_mtu = 0;
734
735     if( HTTPConnect( p_input, 0 ) )
736     {
737         /* Request failed, try again with HTTP/1.0 */
738         char * psz_pos = strstr( p_access_data->psz_buffer, "HTTP/1.1" );
739
740         if( !psz_pos )
741         {
742             return VLC_EGENERIC;
743         }
744
745         p_input->stream.b_seekable = VLC_FALSE;
746         psz_pos[7] = '0';
747         if( HTTPConnect( p_input, 0 ) )
748         {
749             free( p_input->p_access_data );
750             free( psz_name );
751             return VLC_EGENERIC;
752         }
753     }
754
755     /* Update default_pts to a suitable value for http access */
756     p_input->i_pts_delay = config_GetInt( p_input, "http-caching" ) * 1000;
757
758     return VLC_SUCCESS;
759 }
760
761 /*****************************************************************************
762  * Close: free unused data structures
763  *****************************************************************************/
764 static void Close( vlc_object_t *p_this )
765 {
766     input_thread_t *  p_input = (input_thread_t *)p_this;
767     int i_handle = ((network_socket_t *)p_input->p_access_data)->i_handle;
768     _input_socket_t * p_access_data =
769         (_input_socket_t *)p_input->p_access_data;
770
771     free( p_access_data->psz_name );
772
773     msg_Info( p_input, "closing HTTP target `%s'", p_input->psz_source );
774
775 #if defined( WIN32 ) || defined( UNDER_CE )
776     closesocket( i_handle );
777 #else
778     close( i_handle );
779 #endif
780
781     free( p_access_data );
782 }
783
784 /*****************************************************************************
785  * Seek: close and re-open a connection at the right place
786  *****************************************************************************/
787 static void Seek( input_thread_t * p_input, off_t i_pos )
788 {
789     _input_socket_t *p_access_data = (_input_socket_t*)p_input->p_access_data;
790 #if defined( WIN32 ) || defined( UNDER_CE )
791     closesocket( p_access_data->_socket.i_handle );
792 #else
793     close( p_access_data->_socket.i_handle );
794 #endif
795     msg_Dbg( p_input, "seeking to position "I64Fd, i_pos );
796     HTTPConnect( p_input, i_pos );
797 }
798
799 /*****************************************************************************
800  * Read: Read up to i_len bytes from the http connection and place in
801  * p_buffer. Return the actual number of bytes read
802  *****************************************************************************/
803 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
804 {
805     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
806     struct timeval  timeout;
807     fd_set          fds;
808     ssize_t         i_recv;
809     int             i_ret;
810
811     /* Initialize file descriptor set */
812     FD_ZERO( &fds );
813     FD_SET( p_access_data->i_handle, &fds );
814
815     /* We'll wait 0.5 second if nothing happens */
816     timeout.tv_sec = 0;
817     timeout.tv_usec = 500000;
818
819     /* Find if some data is available */
820     while( (i_ret = select( p_access_data->i_handle + 1, &fds,
821                             NULL, NULL, &timeout )) == 0
822 #ifdef HAVE_ERRNO_H
823            || (i_ret < 0 && errno == EINTR)
824 #endif
825            )
826     {
827         FD_ZERO( &fds );
828         FD_SET( p_access_data->i_handle, &fds );
829         timeout.tv_sec = 0;
830         timeout.tv_usec = 500000;
831
832         if( p_input->b_die || p_input->b_error )
833         {
834             return 0;
835         }
836     }
837
838     if( i_ret < 0 )
839     {
840         msg_Err( p_input, "network select error" );
841         return -1;
842     }
843
844     i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
845
846     if( i_recv < 0 )
847     {
848 #ifdef HAVE_ERRNO_H
849         msg_Err( p_input, "recv failed (%s)", strerror(errno) );
850 #else
851         msg_Err( p_input, "recv failed" );
852 #endif
853     }
854
855     return i_recv;
856 }