]> git.sesse.net Git - vlc/blob - modules/access/http.c
* modules/gui/wxwindows/open.cpp: small fixes.
[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.35 2003/05/15 22:27:36 massiot 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
311             if( *psz_value == '-' ) sign = -1;
312             while( *psz_value >= '0' && *psz_value <= '9' )
313             {
314                 i_size = i_size * 10 + *psz_value++ - '0';
315             }
316             i_size = i_tell + ( i_size * sign );
317 #endif
318             msg_Dbg( p_input, "stream size is "I64Fd, i_size );
319
320             vlc_mutex_lock( &p_input->stream.stream_lock );
321             p_input->stream.p_selected_area->i_size = i_size;
322             vlc_mutex_unlock( &p_input->stream.stream_lock );
323         }
324         /* Redirection support */
325         else if( ( i_code == 301 || i_code == 302 ||
326                    i_code == 303 || i_code == 307 )
327                     && !strcasecmp( psz_line, "location" ) )
328         {
329             playlist_t * p_playlist = (playlist_t *) vlc_object_find(
330                                   p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
331             if( !p_playlist )
332             {
333                 msg_Err( p_input, "redirection failed: can't find playlist" );
334                 free( psz_answer );
335                 return VLC_EGENERIC;
336             }
337             msg_Dbg( p_input, "%i %s: redirected to %s",
338                               i_code, psz_answer, psz_value );
339             p_playlist->pp_items[p_playlist->i_index]->b_autodeletion
340                                                                   = VLC_TRUE;
341             playlist_Add( p_playlist, psz_value,
342                           PLAYLIST_INSERT | PLAYLIST_GO,
343                           p_playlist->i_index + 1 );
344             vlc_object_release( p_playlist );
345         }
346
347         /* TODO: parse other headers here */
348     }
349
350     /* Something went wrong */
351     if ( i_code >= 400 )
352     {
353         msg_Err( p_input, "%i %s", i_code, psz_answer );
354         p_input->p_current_data = psz_parser + i_size;
355         free( psz_answer );
356         return VLC_EGENERIC;
357     }
358
359     free( psz_answer );
360
361     /* Set final stream properties */
362     vlc_mutex_lock( &p_input->stream.stream_lock );
363     if( i_protocol == ICY_PROTOCOL )
364     {
365         p_input->stream.b_seekable = VLC_FALSE;
366     }
367     else
368     {
369         p_input->stream.b_seekable = VLC_TRUE;
370     }
371
372     if( p_input->stream.p_selected_area->i_size )
373     {
374         p_input->stream.p_selected_area->i_tell = i_tell;
375     }
376     else
377     {
378         p_input->stream.b_seekable = VLC_FALSE;
379     }
380     p_input->stream.b_changed = VLC_TRUE;
381     vlc_mutex_unlock( &p_input->stream.stream_lock );
382
383     return VLC_SUCCESS;
384 }
385
386 /*****************************************************************************
387  * Open: parse URL and open the remote file at the beginning
388  *****************************************************************************/
389 static int Open( vlc_object_t *p_this )
390 {
391     input_thread_t *    p_input = (input_thread_t *)p_this;
392     _input_socket_t *   p_access_data;
393     char *              psz_name = strdup(p_input->psz_name);
394     char *              psz_parser = psz_name;
395     char *              psz_server_addr = "";
396     char *              psz_server_port = "";
397     char *              psz_path = "";
398     char *              psz_proxy, *psz_proxy_orig;
399     int                 i_server_port = 0;
400
401     p_access_data = malloc( sizeof(_input_socket_t) );
402     p_input->p_access_data = (access_sys_t *)p_access_data;
403     if( p_access_data == NULL )
404     {
405         msg_Err( p_input, "out of memory" );
406         free(psz_name);
407         return VLC_ENOMEM;
408     }
409
410     p_access_data->psz_name = psz_name;
411     p_access_data->psz_network = "";
412     if( config_GetInt( p_input, "ipv4" ) )
413     {
414         p_access_data->psz_network = "ipv4";
415     }
416     if( config_GetInt( p_input, "ipv6" ) )
417     {
418         p_access_data->psz_network = "ipv6";
419     }
420     if( *p_input->psz_access )
421     {
422         /* Find out which shortcut was used */
423         if( !strncmp( p_input->psz_access, "http6", 6 ) )
424         {
425             p_access_data->psz_network = "ipv6";
426         }
427         else if( !strncmp( p_input->psz_access, "http4", 6 ) )
428         {
429             p_access_data->psz_network = "ipv4";
430         }
431     }
432
433     /* Parse psz_name syntax :
434      * //<hostname>[:<port>][/<path>] */
435     while( *psz_parser == '/' )
436     {
437         psz_parser++;
438     }
439     psz_server_addr = psz_parser;
440
441     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
442     {
443         if( *psz_parser == '[' )
444         {
445             /* IPv6 address */
446             while( *psz_parser && *psz_parser != ']' )
447             {
448                 psz_parser++;
449             }
450         }
451         psz_parser++;
452     }
453
454     if ( *psz_parser == ':' )
455     {
456         *psz_parser = '\0';
457         psz_parser++;
458         psz_server_port = psz_parser;
459
460         while( *psz_parser && *psz_parser != '/' )
461         {
462             psz_parser++;
463         }
464     }
465
466     if( *psz_parser == '/' )
467     {
468         *psz_parser = '\0';
469         psz_parser++;
470         psz_path = psz_parser;
471     }
472
473     /* Convert port format */
474     if( *psz_server_port )
475     {
476         i_server_port = strtol( psz_server_port, &psz_parser, 10 );
477         if( *psz_parser )
478         {
479             msg_Err( p_input, "cannot parse server port near %s", psz_parser );
480             free( p_input->p_access_data );
481             free( psz_name );
482             return VLC_EGENERIC;
483         }
484     }
485
486     if( i_server_port == 0 )
487     {
488         i_server_port = 80;
489     }
490
491     if( !*psz_server_addr )
492     {
493         msg_Err( p_input, "no server given" );
494         free( p_input->p_access_data );
495         free( psz_name );
496         return VLC_EGENERIC;
497     }
498
499     /* Check proxy config variable */
500     psz_proxy_orig = config_GetPsz( p_input, "http-proxy" );
501     if( psz_proxy_orig == NULL )
502     {
503         /* Check proxy environment variable */
504         psz_proxy_orig = getenv( "http_proxy" );
505         if( psz_proxy_orig != NULL )
506         {
507             psz_proxy_orig = strdup( psz_proxy_orig );
508         }
509     }
510
511     psz_proxy = psz_proxy_orig;
512     if( psz_proxy != NULL && *psz_proxy )
513     {
514         /* http://myproxy.mydomain:myport/ */
515         int i_proxy_port = 0;
516
517         /* Skip the protocol name */
518         while( *psz_proxy && *psz_proxy != ':' )
519         {
520             psz_proxy++;
521         }
522
523         /* Skip the "://" part */
524         while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
525         {
526             psz_proxy++;
527         }
528
529         /* Found a proxy name */
530         if( *psz_proxy )
531         {
532             char *psz_port = psz_proxy;
533
534             /* Skip the hostname part */
535             while( *psz_port && *psz_port != ':' && *psz_port != '/' )
536             {
537                 psz_port++;
538             }
539
540             /* Found a port name */
541             if( *psz_port )
542             {
543                 char * psz_junk;
544
545                 /* Replace ':' with '\0' */
546                 *psz_port = '\0';
547                 psz_port++;
548
549                 psz_junk = psz_port;
550                 while( *psz_junk && *psz_junk != '/' )
551                 {
552                     psz_junk++;
553                 }
554
555                 if( *psz_junk )
556                 {
557                     *psz_junk = '\0';
558                 }
559
560                 if( *psz_port != '\0' )
561                 {
562                     i_proxy_port = atoi( psz_port );
563                 }
564             }
565
566             psz_proxy = strdup( psz_proxy );
567
568             msg_Dbg( p_input, "using HTTP proxy server=%s port=%d",
569                      psz_proxy, i_proxy_port );
570         }
571         else
572         {
573             msg_Err( p_input, "HTTP proxy %s is invalid!", psz_proxy_orig );
574             free( p_input->p_access_data );
575             free( psz_name );
576             if( psz_proxy_orig ) free( psz_proxy_orig );
577             return VLC_EGENERIC;
578         }
579
580         if( psz_proxy_orig ) free( psz_proxy_orig );
581
582         p_access_data->socket_desc.psz_server_addr = psz_proxy;
583         p_access_data->socket_desc.i_server_port = i_proxy_port;
584         p_access_data->socket_desc.i_type = NETWORK_TCP;
585
586         snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
587                   "GET http://%s:%d/%s HTTP/1.0\r\n",
588                   psz_server_addr, i_server_port, psz_path );
589     }
590     else
591     {
592         /* No proxy, direct connection. */
593         p_access_data->socket_desc.i_type = NETWORK_TCP;
594         p_access_data->socket_desc.psz_server_addr = psz_server_addr;
595         p_access_data->socket_desc.i_server_port = i_server_port;
596
597         snprintf( p_access_data->psz_buffer, MAX_QUERY_SIZE,
598                   "GET /%s HTTP/1.1\r\nHost: %s\r\n",
599                   psz_path, psz_server_addr );
600     }
601     p_access_data->psz_buffer[MAX_QUERY_SIZE - 1] = '\0';
602
603     msg_Dbg( p_input, "opening server=%s port=%d path=%s",
604                       psz_server_addr, i_server_port, psz_path );
605
606     p_input->pf_read = Read;
607     p_input->pf_set_program = input_SetProgram;
608     p_input->pf_set_area = NULL;
609     p_input->pf_seek = Seek;
610
611     vlc_mutex_lock( &p_input->stream.stream_lock );
612     p_input->stream.b_pace_control = VLC_TRUE;
613     p_input->stream.b_seekable = VLC_TRUE;
614     p_input->stream.p_selected_area->i_tell = 0;
615     p_input->stream.p_selected_area->i_size = 0;
616     p_input->stream.i_method = INPUT_METHOD_NETWORK;
617     vlc_mutex_unlock( &p_input->stream.stream_lock );
618     p_input->i_mtu = 0;
619
620     if( HTTPConnect( p_input, 0 ) )
621     {
622         /* Request failed, try again with HTTP/1.0 */
623         char * psz_pos = strstr( p_access_data->psz_buffer, "HTTP/1.1" );
624
625         if( !psz_pos )
626         {
627             return VLC_EGENERIC;
628         }
629
630         p_input->stream.b_seekable = VLC_FALSE;
631         psz_pos[7] = '0';
632         if( HTTPConnect( p_input, 0 ) )
633         {
634             free( p_input->p_access_data );
635             free( psz_name );
636             return VLC_EGENERIC;
637         }
638     }
639
640     /* Update default_pts to a suitable value for http access */
641     p_input->i_pts_delay = config_GetInt( p_input, "http-caching" ) * 1000;
642
643     return VLC_SUCCESS;
644 }
645
646 /*****************************************************************************
647  * Close: free unused data structures
648  *****************************************************************************/
649 static void Close( vlc_object_t *p_this )
650 {
651     input_thread_t *  p_input = (input_thread_t *)p_this;
652     int i_handle = ((network_socket_t *)p_input->p_access_data)->i_handle;
653     _input_socket_t * p_access_data =
654         (_input_socket_t *)p_input->p_access_data;
655
656     free( p_access_data->psz_name );
657
658     msg_Info( p_input, "closing HTTP target `%s'", p_input->psz_source );
659
660 #if defined( WIN32 ) || defined( UNDER_CE )
661     closesocket( i_handle );
662 #else
663     close( i_handle );
664 #endif
665
666     free( p_access_data );
667 }
668
669 /*****************************************************************************
670  * Seek: close and re-open a connection at the right place
671  *****************************************************************************/
672 static void Seek( input_thread_t * p_input, off_t i_pos )
673 {
674     _input_socket_t *p_access_data = (_input_socket_t*)p_input->p_access_data;
675 #if defined( WIN32 ) || defined( UNDER_CE )
676     closesocket( p_access_data->_socket.i_handle );
677 #else
678     close( p_access_data->_socket.i_handle );
679 #endif
680     msg_Dbg( p_input, "seeking to position "I64Fd, i_pos );
681     HTTPConnect( p_input, i_pos );
682 }
683
684 /*****************************************************************************
685  * Read: Read up to i_len bytes from the http connection and place in
686  * p_buffer. Return the actual number of bytes read
687  *****************************************************************************/
688 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
689 {
690     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
691     struct timeval  timeout;
692     fd_set          fds;
693     ssize_t         i_recv;
694     int             i_ret;
695
696     /* Initialize file descriptor set */
697     FD_ZERO( &fds );
698     FD_SET( p_access_data->i_handle, &fds );
699
700     /* We'll wait 0.5 second if nothing happens */
701     timeout.tv_sec = 0;
702     timeout.tv_usec = 500000;
703
704     /* Find if some data is available */
705     while( (i_ret = select( p_access_data->i_handle + 1, &fds,
706                             NULL, NULL, &timeout )) == 0
707 #ifdef HAVE_ERRNO_H
708            || (i_ret < 0 && errno == EINTR)
709 #endif
710            )
711     {
712         FD_ZERO( &fds );
713         FD_SET( p_access_data->i_handle, &fds );
714         timeout.tv_sec = 0;
715         timeout.tv_usec = 500000;
716
717         if( p_input->b_die || p_input->b_error )
718         {
719             return 0;
720         }
721     }
722
723     if( i_ret < 0 )
724     {
725         msg_Err( p_input, "network select error" );
726         return -1;
727     }
728
729     i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
730
731     if( i_recv < 0 )
732     {
733 #ifdef HAVE_ERRNO_H
734         msg_Err( p_input, "recv failed (%s)", strerror(errno) );
735 #else
736         msg_Err( p_input, "recv failed" );
737 #endif
738     }
739
740     return i_recv;
741 }