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