]> git.sesse.net Git - vlc/blob - modules/access/http.c
5871e28b178d2f1264c5d7b46e19e0e98dfdccc1
[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.33 2003/04/30 04:13:12 hartman 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     set_description( _("HTTP input") );
90     set_capability( "access", 0 );
91     add_shortcut( "http" );
92     add_shortcut( "http4" );
93     add_shortcut( "http6" );
94     set_callbacks( Open, Close );
95 vlc_module_end();
96
97 /*****************************************************************************
98  * _input_socket_t: private access plug-in data, modified to add private
99  *                  fields
100  *****************************************************************************/
101 #define MAX_ANSWER_SIZE 1024
102 #define MAX_QUERY_SIZE 1024
103
104 typedef struct _input_socket_s
105 {
106     input_socket_t      _socket;
107
108     char *              psz_network;
109     network_socket_t    socket_desc;
110     char                psz_buffer[MAX_QUERY_SIZE];
111     char *              psz_name;
112 } _input_socket_t;
113
114 /*****************************************************************************
115  * HTTPConnect: connect to the server and seek to i_tell
116  *****************************************************************************/
117 static int HTTPConnect( input_thread_t * p_input, off_t i_tell )
118 {
119     char psz_buffer[MAX_QUERY_SIZE];
120     _input_socket_t * p_access_data;
121     module_t * p_network;
122     char * psz_parser, * psz_value, * psz_answer;
123     int i_code, i_ret, i, i_size;
124
125     enum { HTTP_PROTOCOL, ICY_PROTOCOL } i_protocol;
126
127     /* Find an appropriate network module */
128     p_access_data = (_input_socket_t *)p_input->p_access_data;
129     p_input->p_private = (void*) &p_access_data->socket_desc;
130     p_network = module_Need( p_input, "network", p_access_data->psz_network );
131     if( p_network == NULL )
132     {
133         return VLC_ENOMOD;
134     }
135     module_Unneed( p_input, p_network );
136
137     p_access_data->_socket.i_handle = p_access_data->socket_desc.i_handle;
138
139 #   define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
140 #   define HTTP_END       "\r\n"
141
142     /* Build the query string */
143     if ( p_input->stream.b_seekable )
144     {
145          snprintf( psz_buffer, MAX_QUERY_SIZE,
146                    "%s"
147                    "Range: bytes="I64Fd"-\r\n"
148                    HTTP_USERAGENT HTTP_END,
149                    p_access_data->psz_buffer, i_tell );
150     }
151     else
152     {
153          snprintf( psz_buffer, MAX_QUERY_SIZE,
154                    "%s"
155                    HTTP_USERAGENT HTTP_END,
156                    p_access_data->psz_buffer );
157     }
158     psz_buffer[MAX_QUERY_SIZE - 1] = '\0';
159
160     /* Send GET query */
161     i_ret = send( p_access_data->_socket.i_handle,
162                   psz_buffer, strlen( psz_buffer ), 0 );
163     if( i_ret == -1 )
164     {
165 #ifdef HAVE_ERRNO_H
166         msg_Err( p_input, "cannot send request (%s)", strerror(errno) );
167 #else
168         msg_Err( p_input, "cannot send request" );
169 #endif
170         Close( VLC_OBJECT(p_input) );
171         return VLC_EGENERIC;
172     }
173
174     /* Prepare the input thread for reading. */
175     p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
176
177     /* FIXME: we shouldn't have to do that ! It's UGLY but mandatory because
178      * input_FillBuffer assumes p_input->pf_read exists */
179     p_input->pf_read = Read;
180
181     while( !input_FillBuffer( p_input ) )
182     {
183         if( p_input->b_die || p_input->b_error )
184         {
185             Close( VLC_OBJECT(p_input) );
186             return VLC_EGENERIC;
187         }
188     }
189
190     /* Get the HTTP returncode */
191     i_size = input_Peek( p_input, (byte_t**)&psz_parser, MAX_ANSWER_SIZE );
192
193     if( i_size <= 0 )
194     {
195         msg_Err( p_input, "not enough data" );
196         Close( VLC_OBJECT(p_input) );
197         return VLC_EGENERIC;
198     }
199
200     /* Guess the protocol */
201     if( ( ( (size_t)i_size >= strlen("HTTP/1.x") ) &&
202             !strncmp( psz_parser, "HTTP/1.", strlen("HTTP/1.") ) ) )
203     {
204         i_protocol = HTTP_PROTOCOL;
205
206         psz_parser += strlen("HTTP/1.x");
207         i_size -= strlen("HTTP/1.x");
208     }
209     else if( ( (size_t)i_size >= strlen("ICY") &&
210              !strncmp( psz_parser, "ICY", strlen("ICY") ) ) )
211     {
212         i_protocol = ICY_PROTOCOL;
213         if( !p_input->psz_demux || !*p_input->psz_demux  )
214         {
215             msg_Info( p_input, "ICY server found, mp3 demuxer selected" );
216             p_input->psz_demux = "mp3";    // FIXME strdup ?
217         }
218
219         psz_parser += strlen("ICY");
220         i_size -= strlen("ICY");
221     }
222     else
223     {
224         msg_Err( p_input, "invalid HTTP reply '%s'", psz_parser );
225         return VLC_EGENERIC;
226     }
227
228     /* Check the HTTP return code */
229     i_code = atoi( (char*)psz_parser );
230     msg_Dbg( p_input, "%s server replied: %i",
231              i_protocol == HTTP_PROTOCOL ? "HTTP" : "ICY", i_code );
232     psz_parser += 4;
233     i_size -= 4;
234
235     /* Find the end of the line */
236     for ( i = 0; (i < i_size -1) && ((psz_parser[i] != '\r') ||
237       (psz_parser[i+1] != '\n')); i++ )
238     {
239         ;
240     }
241
242     /* Check we actually parsed something */
243     if ( i+1 == i_size && psz_parser[i+1] != '\n' )
244     {
245         msg_Err( p_input, "stream not compliant with HTTP/1.x" );
246         return VLC_EGENERIC;
247     }
248
249     /* Store the line we just parsed and skip it */
250     psz_answer = strndup( psz_parser, i );
251     if( !psz_answer )
252     {
253         return VLC_ENOMEM;
254     }
255
256     p_input->p_current_data = psz_parser + i + 2;
257
258     /* Parse remaining headers */
259     for ( ; ; )
260     {
261         char psz_line[MAX_ANSWER_SIZE];
262
263         i_size = input_Peek( p_input, (byte_t**)&psz_parser, MAX_ANSWER_SIZE );
264
265         if( i_size <= 0 )
266         {
267             msg_Err( p_input, "not enough data" );
268             Close( VLC_OBJECT(p_input) );
269             free( psz_answer );
270             return VLC_EGENERIC;
271         }
272
273         /* Copy one line to psz_line */
274         i = 0;
275         while( i_size && psz_parser[i] != '\r'
276                       && psz_parser[i + 1] != '\n' )
277         {
278             psz_line[i] = psz_parser[i];
279             i++;
280             i_size--;
281         }
282         p_input->p_current_data = psz_parser + i + 2;
283         if( !i )
284         {
285             break; /* End of headers */
286         }
287         psz_line[i] = '\0';
288         psz_parser = strchr( psz_line, ':' );
289         if ( !psz_parser )
290         {
291             msg_Err( p_input, "malformed header line: %s", psz_line );
292             free( psz_answer );
293             return VLC_EGENERIC;
294         }
295         psz_parser[0] = '\0';
296         psz_parser++;
297         while ( *psz_parser == ' ' || *psz_parser == '\t' )
298         {
299             psz_parser++;
300         }
301         psz_value = psz_parser;
302
303         if( !strcasecmp( psz_line, "Content-Length" ) )
304         {
305             off_t i_size = 0;
306 #ifdef HAVE_ATOLL
307             i_size = i_tell + atoll( psz_value );
308 #else
309             int sign = 1;
310             psz_parser = psz_value;
311             
312             while( *psz_value == ' ' || *psz_value == '\t' )
313                 psz_value++;
314
315             if( *psz_value == '-' )
316                 sign = -1;
317             while( *psz_value != '\0')
318             {
319                 if( *psz_value >= '0' && *psz_value <= '9' )
320                     i_size = i_size * 10 + *psz_value++ - '0';
321                 else
322                     psz_value++;
323             }
324             i_size = i_tell + ( i_size * sign );
325 #endif
326             msg_Dbg( p_input, "stream size is "I64Fd, i_size );
327
328             vlc_mutex_lock( &p_input->stream.stream_lock );
329             p_input->stream.p_selected_area->i_size = i_size;
330             vlc_mutex_unlock( &p_input->stream.stream_lock );
331         }
332         /* Redirection support */
333         else if( ( i_code == 301 || i_code == 302 ||
334                    i_code == 303 || i_code == 307 )
335                     && !strcasecmp( psz_line, "location" ) )
336         {
337             playlist_t * p_playlist = (playlist_t *) vlc_object_find(
338                                   p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
339             if( !p_playlist )
340             {
341                 msg_Err( p_input, "redirection failed: can't find playlist" );
342                 free( psz_answer );
343                 return VLC_EGENERIC;
344             }
345             msg_Dbg( p_input, "%i %s: redirected to %s",
346                               i_code, psz_answer, psz_value );
347             p_playlist->pp_items[p_playlist->i_index]->b_autodeletion
348                                                                   = VLC_TRUE;
349             playlist_Add( p_playlist, psz_value,
350                           PLAYLIST_INSERT | PLAYLIST_GO,
351                           p_playlist->i_index + 1 );
352             vlc_object_release( p_playlist );
353         }
354
355         /* TODO: parse other headers here */
356     }
357
358     /* Something went wrong */
359     if ( i_code >= 400 )
360     {
361         msg_Err( p_input, "%i %s", i_code, psz_answer );
362         p_input->p_current_data = psz_parser + i_size;
363         free( psz_answer );
364         return VLC_EGENERIC;
365     }
366
367     free( psz_answer );
368
369     /* Set final stream properties */
370     vlc_mutex_lock( &p_input->stream.stream_lock );
371     if( i_protocol == ICY_PROTOCOL )
372     {
373         p_input->stream.b_seekable = VLC_FALSE;
374     }
375     else
376     {
377         p_input->stream.b_seekable = VLC_TRUE;
378     }
379
380     if( p_input->stream.p_selected_area->i_size )
381     {
382         p_input->stream.p_selected_area->i_tell = i_tell;
383     }
384     else
385     {
386         p_input->stream.b_seekable = VLC_FALSE;
387     }
388     p_input->stream.b_changed = VLC_TRUE;
389     vlc_mutex_unlock( &p_input->stream.stream_lock );
390
391     return VLC_SUCCESS;
392 }
393
394 /*****************************************************************************
395  * Open: parse URL and open the remote file at the beginning
396  *****************************************************************************/
397 static int Open( vlc_object_t *p_this )
398 {
399     input_thread_t *    p_input = (input_thread_t *)p_this;
400     _input_socket_t *   p_access_data;
401     char *              psz_name = strdup(p_input->psz_name);
402     char *              psz_parser = psz_name;
403     char *              psz_server_addr = "";
404     char *              psz_server_port = "";
405     char *              psz_path = "";
406     char *              psz_proxy, *psz_proxy_orig;
407     int                 i_server_port = 0;
408
409     p_access_data = malloc( sizeof(_input_socket_t) );
410     p_input->p_access_data = (access_sys_t *)p_access_data;
411     if( p_access_data == NULL )
412     {
413         msg_Err( p_input, "out of memory" );
414         free(psz_name);
415         return VLC_ENOMEM;
416     }
417
418     p_access_data->psz_name = psz_name;
419     p_access_data->psz_network = "";
420     if( config_GetInt( p_input, "ipv4" ) )
421     {
422         p_access_data->psz_network = "ipv4";
423     }
424     if( config_GetInt( p_input, "ipv6" ) )
425     {
426         p_access_data->psz_network = "ipv6";
427     }
428     if( *p_input->psz_access )
429     {
430         /* Find out which shortcut was used */
431         if( !strncmp( p_input->psz_access, "http6", 6 ) )
432         {
433             p_access_data->psz_network = "ipv6";
434         }
435         else if( !strncmp( p_input->psz_access, "http4", 6 ) )
436         {
437             p_access_data->psz_network = "ipv4";
438         }
439     }
440
441     /* Parse psz_name syntax :
442      * //<hostname>[:<port>][/<path>] */
443     while( *psz_parser == '/' )
444     {
445         psz_parser++;
446     }
447     psz_server_addr = psz_parser;
448
449     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
450     {
451         if( *psz_parser == '[' )
452         {
453             /* IPv6 address */
454             while( *psz_parser && *psz_parser != ']' )
455             {
456                 psz_parser++;
457             }
458         }
459         psz_parser++;
460     }
461
462     if ( *psz_parser == ':' )
463     {
464         *psz_parser = '\0';
465         psz_parser++;
466         psz_server_port = psz_parser;
467
468         while( *psz_parser && *psz_parser != '/' )
469         {
470             psz_parser++;
471         }
472     }
473
474     if( *psz_parser == '/' )
475     {
476         *psz_parser = '\0';
477         psz_parser++;
478         psz_path = psz_parser;
479     }
480
481     /* Convert port format */
482     if( *psz_server_port )
483     {
484         i_server_port = strtol( psz_server_port, &psz_parser, 10 );
485         if( *psz_parser )
486         {
487             msg_Err( p_input, "cannot parse server port near %s", psz_parser );
488             free( p_input->p_access_data );
489             free( psz_name );
490             return VLC_EGENERIC;
491         }
492     }
493
494     if( i_server_port == 0 )
495     {
496         i_server_port = 80;
497     }
498
499     if( !*psz_server_addr )
500     {
501         msg_Err( p_input, "no server given" );
502         free( p_input->p_access_data );
503         free( psz_name );
504         return VLC_EGENERIC;
505     }
506
507     /* Check proxy config variable */
508     psz_proxy_orig = config_GetPsz( p_input, "http-proxy" );
509     if( psz_proxy_orig == NULL )
510     {
511         /* Check proxy environment variable */
512         psz_proxy_orig = getenv( "http_proxy" );
513         if( psz_proxy_orig != NULL )
514         {
515             psz_proxy_orig = strdup( psz_proxy_orig );
516         }
517     }
518
519     psz_proxy = psz_proxy_orig;
520     if( psz_proxy != NULL && *psz_proxy )
521     {
522         /* http://myproxy.mydomain:myport/ */
523         int i_proxy_port = 0;
524
525         /* Skip the protocol name */
526         while( *psz_proxy && *psz_proxy != ':' )
527         {
528             psz_proxy++;
529         }
530
531         /* Skip the "://" part */
532         while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
533         {
534             psz_proxy++;
535         }
536
537         /* Found a proxy name */
538         if( *psz_proxy )
539         {
540             char *psz_port = psz_proxy;
541
542             /* Skip the hostname part */
543             while( *psz_port && *psz_port != ':' && *psz_port != '/' )
544             {
545                 psz_port++;
546             }
547
548             /* Found a port name */
549             if( *psz_port )
550             {
551                 char * psz_junk;
552
553                 /* Replace ':' with '\0' */
554                 *psz_port = '\0';
555                 psz_port++;
556
557                 psz_junk = psz_port;
558                 while( *psz_junk && *psz_junk != '/' )
559                 {
560                     psz_junk++;
561                 }
562
563                 if( *psz_junk )
564                 {
565                     *psz_junk = '\0';
566                 }
567
568                 if( *psz_port != '\0' )
569                 {
570                     i_proxy_port = atoi( psz_port );
571                 }
572             }
573
574             psz_proxy = strdup( psz_proxy );
575
576             msg_Dbg( p_input, "using HTTP proxy server=%s port=%d",
577                      psz_proxy, i_proxy_port );
578         }
579         else
580         {
581             msg_Err( p_input, "HTTP proxy %s is invalid!", psz_proxy_orig );
582             free( p_input->p_access_data );
583             free( psz_name );
584             if( psz_proxy_orig ) free( psz_proxy_orig );
585             return VLC_EGENERIC;
586         }
587
588         if( psz_proxy_orig ) free( psz_proxy_orig );
589
590         p_access_data->socket_desc.psz_server_addr = psz_proxy;
591         p_access_data->socket_desc.i_server_port = i_proxy_port;
592         p_access_data->socket_desc.i_type = NETWORK_TCP;
593
594         snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
595                   "GET http://%s:%d/%s HTTP/1.0\r\n",
596                   psz_server_addr, i_server_port, psz_path );
597     }
598     else
599     {
600         /* No proxy, direct connection. */
601         p_access_data->socket_desc.i_type = NETWORK_TCP;
602         p_access_data->socket_desc.psz_server_addr = psz_server_addr;
603         p_access_data->socket_desc.i_server_port = i_server_port;
604
605         snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
606                   "GET /%s HTTP/1.1\r\nHost: %s\r\n",
607                   psz_path, psz_server_addr );
608     }
609     p_access_data->psz_buffer[MAX_QUERY_SIZE - 1] = '\0';
610
611     msg_Dbg( p_input, "opening server=%s port=%d path=%s",
612                       psz_server_addr, i_server_port, psz_path );
613
614     p_input->pf_read = Read;
615     p_input->pf_set_program = input_SetProgram;
616     p_input->pf_set_area = NULL;
617     p_input->pf_seek = Seek;
618
619     vlc_mutex_lock( &p_input->stream.stream_lock );
620     p_input->stream.b_pace_control = VLC_TRUE;
621     p_input->stream.b_seekable = VLC_TRUE;
622     p_input->stream.p_selected_area->i_tell = 0;
623     p_input->stream.p_selected_area->i_size = 0;
624     p_input->stream.i_method = INPUT_METHOD_NETWORK;
625     vlc_mutex_unlock( &p_input->stream.stream_lock );
626     p_input->i_mtu = 0;
627
628     if( HTTPConnect( p_input, 0 ) )
629     {
630         /* Request failed, try again with HTTP/1.0 */
631         char * psz_pos = strstr( p_access_data->psz_buffer, "HTTP/1.1" );
632
633         if( !psz_pos )
634         {
635             return VLC_EGENERIC;
636         }
637
638         p_input->stream.b_seekable = VLC_FALSE;
639         psz_pos[7] = '0';
640         if( HTTPConnect( p_input, 0 ) )
641         {
642             free( p_input->p_access_data );
643             free( psz_name );
644             return VLC_EGENERIC;
645         }
646     }
647
648     /* Update default_pts to a suitable value for http access */
649     p_input->i_pts_delay = config_GetInt( p_input, "http-caching" ) * 1000;
650
651     return VLC_SUCCESS;
652 }
653
654 /*****************************************************************************
655  * Close: free unused data structures
656  *****************************************************************************/
657 static void Close( vlc_object_t *p_this )
658 {
659     input_thread_t *  p_input = (input_thread_t *)p_this;
660     int i_handle = ((network_socket_t *)p_input->p_access_data)->i_handle;
661     _input_socket_t * p_access_data =
662         (_input_socket_t *)p_input->p_access_data;
663
664     free( p_access_data->psz_name );
665
666     msg_Info( p_input, "closing HTTP target `%s'", p_input->psz_source );
667
668 #if defined( WIN32 ) || defined( UNDER_CE )
669     closesocket( i_handle );
670 #else
671     close( i_handle );
672 #endif
673
674     free( p_access_data );
675 }
676
677 /*****************************************************************************
678  * Seek: close and re-open a connection at the right place
679  *****************************************************************************/
680 static void Seek( input_thread_t * p_input, off_t i_pos )
681 {
682     _input_socket_t *p_access_data = (_input_socket_t*)p_input->p_access_data;
683 #if defined( WIN32 ) || defined( UNDER_CE )
684     closesocket( p_access_data->_socket.i_handle );
685 #else
686     close( p_access_data->_socket.i_handle );
687 #endif
688     msg_Dbg( p_input, "seeking to position "I64Fd, i_pos );
689     HTTPConnect( p_input, i_pos );
690 }
691
692 /*****************************************************************************
693  * Read: Read up to i_len bytes from the http connection and place in
694  * p_buffer. Return the actual number of bytes read
695  *****************************************************************************/
696 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
697 {
698     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
699     struct timeval  timeout;
700     fd_set          fds;
701     ssize_t         i_recv;
702     int             i_ret;
703
704     /* Initialize file descriptor set */
705     FD_ZERO( &fds );
706     FD_SET( p_access_data->i_handle, &fds );
707
708     /* We'll wait 0.5 second if nothing happens */
709     timeout.tv_sec = 0;
710     timeout.tv_usec = 500000;
711
712     /* Find if some data is available */
713     while( (i_ret = select( p_access_data->i_handle + 1, &fds,
714                             NULL, NULL, &timeout )) == 0
715 #ifdef HAVE_ERRNO_H
716            || (i_ret < 0 && errno == EINTR)
717 #endif
718            )
719     {
720         FD_ZERO( &fds );
721         FD_SET( p_access_data->i_handle, &fds );
722         timeout.tv_sec = 0;
723         timeout.tv_usec = 500000;
724
725         if( p_input->b_die || p_input->b_error )
726         {
727             return 0;
728         }
729     }
730
731     if( i_ret < 0 )
732     {
733         msg_Err( p_input, "network select error" );
734         return -1;
735     }
736
737     i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
738
739     if( i_recv < 0 )
740     {
741 #ifdef HAVE_ERRNO_H
742         msg_Err( p_input, "recv failed (%s)", strerror(errno) );
743 #else
744         msg_Err( p_input, "recv failed" );
745 #endif
746     }
747
748     return i_recv;
749 }