]> git.sesse.net Git - vlc/blob - modules/access/http.c
f4b198206038bf2339165fdd565d4923c777c1b8
[vlc] / modules / access / http.c
1 /*****************************************************************************
2  * http.c: HTTP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include "vlc_playlist.h"
34 #include "network.h"
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
41
42 #define PROXY_TEXT N_("HTTP proxy")
43 #define PROXY_LONGTEXT N_( \
44     "You can specify an HTTP proxy to use. It must be of the form " \
45     "http://myproxy.mydomain:myport/. If none is specified, the HTTP_PROXY " \
46     "environment variable will be tried." )
47
48 #define CACHING_TEXT N_("Caching value in ms")
49 #define CACHING_LONGTEXT N_( \
50     "Allows you to modify the default caching value for http streams. This " \
51     "value should be set in millisecond units." )
52
53 #define USER_TEXT N_("HTTP user name")
54 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
55     "be used for the connection (Basic authentication only).")
56
57 #define PASS_TEXT N_("HTTP password")
58 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
59     "used for the connection.")
60
61 #define AGENT_TEXT N_("HTTP user agent")
62 #define AGENT_LONGTEXT N_("Allows you to modify the user agent that will be " \
63     "used for the connection.")
64
65 vlc_module_begin();
66     set_description( _("HTTP input") );
67     set_capability( "access2", 0 );
68
69     add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
70                 VLC_FALSE );
71     add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
72                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
73     add_string( "http-user", NULL, NULL, USER_TEXT, USER_LONGTEXT, VLC_FALSE );
74     add_string( "http-pwd", NULL , NULL, PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
75     add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
76                 AGENT_LONGTEXT, VLC_FALSE );
77
78     add_shortcut( "http" );
79     add_shortcut( "http4" );
80     add_shortcut( "http6" );
81     set_callbacks( Open, Close );
82 vlc_module_end();
83
84 /*****************************************************************************
85  * Local prototypes
86  *****************************************************************************/
87 struct access_sys_t
88 {
89     int fd;
90
91     /* From uri */
92     vlc_url_t url;
93     char    *psz_user;
94     char    *psz_passwd;
95     char    *psz_user_agent;
96
97     /* Proxy */
98     vlc_bool_t b_proxy;
99     vlc_url_t  proxy;
100
101     /* */
102     int        i_code;
103     char      *psz_protocol;
104     int        i_version;
105
106     char       *psz_mime;
107     char       *psz_location;
108
109     vlc_bool_t b_chunked;
110     int64_t    i_chunk;
111
112     vlc_bool_t b_seekable;
113 };
114
115 /* */
116 static int Read( access_t *, uint8_t *, int );
117 static int Seek( access_t *, int64_t );
118 static int Control( access_t *, int, va_list );
119
120 /* */
121 static void ParseURL( access_sys_t *, char *psz_url );
122 static int  Connect( access_t *, int64_t );
123 static char *b64_encode( unsigned char *src );
124
125 /*****************************************************************************
126  * Open:
127  *****************************************************************************/
128 static int  Open ( vlc_object_t *p_this )
129 {
130     access_t     *p_access = (access_t*)p_this;
131     access_sys_t *p_sys;
132     vlc_value_t   val;
133
134     /* First set ipv4/ipv6 */
135     var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
136     var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
137
138     if( *p_access->psz_access )
139     {
140         /* Find out which shortcut was used */
141         if( !strncmp( p_access->psz_access, "http4", 6 ) )
142         {
143             val.b_bool = VLC_TRUE;
144             var_Set( p_access, "ipv4", val );
145
146             val.b_bool = VLC_FALSE;
147             var_Set( p_access, "ipv6", val );
148         }
149         else if( !strncmp( p_access->psz_access, "http6", 6 ) )
150         {
151             val.b_bool = VLC_TRUE;
152             var_Set( p_access, "ipv6", val );
153
154             val.b_bool = VLC_FALSE;
155             var_Set( p_access, "ipv4", val );
156         }
157     }
158
159     /* Set up p_access */
160     p_access->pf_read = Read;
161     p_access->pf_block = NULL;
162     p_access->pf_control = Control;
163     p_access->pf_seek = Seek;
164     p_access->info.i_update = 0;
165     p_access->info.i_size = 0;
166     p_access->info.i_pos = 0;
167     p_access->info.b_eof = VLC_FALSE;
168     p_access->info.i_title = 0;
169     p_access->info.i_seekpoint = 0;
170     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
171     memset( p_sys, 0, sizeof( access_sys_t ) );
172     p_sys->fd = -1;
173     p_sys->b_proxy = VLC_FALSE;
174     p_sys->i_version = 1;
175     p_sys->b_seekable = VLC_TRUE;
176     p_sys->psz_mime = NULL;
177     p_sys->psz_location = NULL;
178     p_sys->psz_user_agent = NULL;
179
180
181     /* Parse URI */
182     ParseURL( p_sys, p_access->psz_path );
183     if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
184     {
185         msg_Warn( p_access, "invalid host" );
186         goto error;
187     }
188     if( p_sys->url.i_port <= 0 )
189     {
190         p_sys->url.i_port = 80;
191     }
192     if( !p_sys->psz_user || *p_sys->psz_user == '\0' )
193     {
194         var_Create( p_access, "http-user", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
195         var_Get( p_access, "http-user", &val );
196         p_sys->psz_user = val.psz_string;
197
198         var_Create( p_access, "http-pwd", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
199         var_Get( p_access, "http-pwd", &val );
200         p_sys->psz_passwd = val.psz_string;
201     }
202
203     /* Do user agent */
204     var_Create( p_access, "http-user-agent", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
205     var_Get( p_access, "http-user-agent", &val );
206     p_sys->psz_user_agent = val.psz_string;
207
208     /* Check proxy */
209     var_Create( p_access, "http-proxy", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
210     var_Get( p_access, "http-proxy", &val );
211     if( val.psz_string && *val.psz_string )
212     {
213         p_sys->b_proxy = VLC_TRUE;
214         vlc_UrlParse( &p_sys->proxy, val.psz_string, 0 );
215     }
216     else
217     {
218         char *psz_proxy = getenv( "http_proxy" );
219         if( psz_proxy && *psz_proxy )
220         {
221             p_sys->b_proxy = VLC_TRUE;
222             vlc_UrlParse( &p_sys->proxy, val.psz_string, 0 );
223         }
224         if( psz_proxy )
225         {
226             free( psz_proxy );
227         }
228     }
229     if( val.psz_string )
230     {
231         free( val.psz_string );
232     }
233
234     if( p_sys->b_proxy )
235     {
236         if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
237         {
238             msg_Warn( p_access, "invalid proxy host" );
239             goto error;
240         }
241         if( p_sys->proxy.i_port <= 0 )
242         {
243             p_sys->proxy.i_port = 80;
244         }
245     }
246
247     msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
248              p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
249     if( p_sys->b_proxy )
250     {
251         msg_Dbg( p_access, "      proxy %s:%d", p_sys->proxy.psz_host,
252                  p_sys->proxy.i_port );
253     }
254     if( p_sys->psz_user && *p_sys->psz_user )
255     {
256         msg_Dbg( p_access, "      user='%s', pwd='%s'",
257                  p_sys->psz_user, p_sys->psz_passwd );
258     }
259
260     /* Connect */
261     if( Connect( p_access, 0 ) )
262     {
263         /* Retry with http 1.0 */
264         p_sys->i_version = 0;
265
266         if( p_access->b_die ||
267             Connect( p_access, 0 ) )
268         {
269             goto error;
270         }
271     }
272
273     if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
274           p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
275         p_sys->psz_location && *p_sys->psz_location )
276     {
277         playlist_t * p_playlist;
278
279         msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
280
281         p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
282         if( !p_playlist )
283         {
284             msg_Err( p_access, "redirection failed: can't find playlist" );
285             goto error;
286         }
287         p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
288         playlist_Add( p_playlist, p_sys->psz_location, p_sys->psz_location,
289                       PLAYLIST_INSERT | PLAYLIST_GO,
290                       p_playlist->i_index + 1 );
291         vlc_object_release( p_playlist );
292
293         p_access->info.i_size = 0;  /* Force to stop reading */
294     }
295
296     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
297     {
298         if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "video/nsv" ) )
299             p_access->psz_demux = strdup( "nsv" );
300         else
301             p_access->psz_demux = strdup( "mp3" );
302
303         msg_Info( p_access, "ICY server found, %s demuxer selected",
304                   p_access->psz_demux );
305     }
306
307     /* Pts delay */
308     var_Create( p_access, "http-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
309
310     return VLC_SUCCESS;
311
312 error:
313     vlc_UrlClean( &p_sys->url );
314     vlc_UrlClean( &p_sys->proxy );
315     if( p_sys->psz_mime ) free( p_sys->psz_mime );
316     if( p_sys->psz_location ) free( p_sys->psz_location );
317     if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
318     if( p_sys->psz_user ) free( p_sys->psz_user );
319     if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
320
321     if( p_sys->fd > 0 )
322     {
323         net_Close( p_sys->fd );
324     }
325     free( p_sys );
326     return VLC_EGENERIC;
327 }
328
329 /*****************************************************************************
330  * Close:
331  *****************************************************************************/
332 static void Close( vlc_object_t *p_this )
333 {
334     access_t     *p_access = (access_t*)p_this;
335     access_sys_t *p_sys = p_access->p_sys;
336
337     vlc_UrlClean( &p_sys->url );
338     vlc_UrlClean( &p_sys->proxy );
339
340     if( p_sys->psz_user ) free( p_sys->psz_user );
341     if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
342
343     if( p_sys->psz_mime ) free( p_sys->psz_mime );
344     if( p_sys->psz_location ) free( p_sys->psz_location );
345
346     if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
347
348     if( p_sys->fd > 0 )
349     {
350         net_Close( p_sys->fd );
351     }
352     free( p_sys );
353 }
354
355 /*****************************************************************************
356  * Read: Read up to i_len bytes from the http connection and place in
357  * p_buffer. Return the actual number of bytes read
358  *****************************************************************************/
359 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
360 {
361     access_sys_t *p_sys   = p_access->p_sys;
362     int          i_read;
363
364     if( p_sys->fd < 0 )
365     {
366         p_access->info.b_eof = VLC_TRUE;
367         return 0;
368     }
369
370     if( p_access->info.i_size > 0 &&
371         i_len + p_access->info.i_pos > p_access->info.i_size )
372     {
373         if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
374         {
375             p_access->info.b_eof = VLC_TRUE;
376             return 0;
377         }
378     }
379     if( p_sys->b_chunked )
380     {
381         if( p_sys->i_chunk < 0 )
382         {
383             p_access->info.b_eof = VLC_TRUE;
384             return 0;
385         }
386
387         if( p_sys->i_chunk <= 0 )
388         {
389             char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
390             /* read the chunk header */
391             if( psz == NULL )
392             {
393                 msg_Dbg( p_access, "failed reading chunk-header line" );
394                 return -1;
395             }
396             p_sys->i_chunk = strtoll( psz, NULL, 16 );
397             free( psz );
398
399             if( p_sys->i_chunk <= 0 )   /* eof */
400             {
401                 p_sys->i_chunk = -1;
402                 p_access->info.b_eof = VLC_TRUE;
403                 return 0;
404             }
405         }
406
407         if( i_len > p_sys->i_chunk )
408         {
409             i_len = p_sys->i_chunk;
410         }
411     }
412
413
414     i_read = net_Read( p_access, p_sys->fd, p_buffer, i_len, VLC_FALSE );
415     if( i_read > 0 )
416     {
417         p_access->info.i_pos += i_read;
418
419         if( p_sys->b_chunked )
420         {
421             p_sys->i_chunk -= i_read;
422             if( p_sys->i_chunk <= 0 )
423             {
424                 /* read the empty line */
425                 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
426                 if( psz )
427                 {
428                     free( psz );
429                 }
430             }
431         }
432     }
433     else if( i_read == 0 )
434     {
435         p_access->info.b_eof = VLC_TRUE;
436     }
437     return i_read;
438 }
439
440 /*****************************************************************************
441  * Seek: close and re-open a connection at the right place
442  *****************************************************************************/
443 static int Seek( access_t *p_access, int64_t i_pos )
444 {
445     access_sys_t *p_sys = p_access->p_sys;
446
447     msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
448
449     net_Close( p_sys->fd ); p_sys->fd = -1;
450
451     if( Connect( p_access, i_pos ) )
452     {
453         msg_Err( p_access, "seek failed" );
454         p_access->info.b_eof = VLC_TRUE;
455         return VLC_EGENERIC;
456     }
457     return VLC_SUCCESS;
458 }
459
460 /*****************************************************************************
461  * Control:
462  *****************************************************************************/
463 static int Control( access_t *p_access, int i_query, va_list args )
464 {
465     access_sys_t *p_sys = p_access->p_sys;
466     vlc_bool_t   *pb_bool;
467     int          *pi_int;
468     int64_t      *pi_64;
469     vlc_value_t  val;
470
471     switch( i_query )
472     {
473         /* */
474         case ACCESS_CAN_SEEK:
475             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
476             *pb_bool = p_sys->b_seekable;
477             break;
478         case ACCESS_CAN_FASTSEEK:
479             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
480             *pb_bool = VLC_FALSE;
481             break;
482         case ACCESS_CAN_PAUSE:
483             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
484             *pb_bool = VLC_TRUE;    /* FIXME */
485             break;
486         case ACCESS_CAN_CONTROL_PACE:
487             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
488             *pb_bool = VLC_TRUE;    /* FIXME */
489             break;
490
491         /* */
492         case ACCESS_GET_MTU:
493             pi_int = (int*)va_arg( args, int * );
494             *pi_int = 0;
495             break;
496
497         case ACCESS_GET_PTS_DELAY:
498             pi_64 = (int64_t*)va_arg( args, int64_t * );
499             var_Get( p_access, "http-caching", &val );
500             *pi_64 = val.i_int * 1000;
501             break;
502
503         /* */
504         case ACCESS_SET_PAUSE_STATE:
505             break;
506
507         case ACCESS_GET_TITLE_INFO:
508         case ACCESS_SET_TITLE:
509         case ACCESS_SET_SEEKPOINT:
510             return VLC_EGENERIC;
511
512         default:
513             msg_Err( p_access, "unimplemented query in control" );
514             return VLC_EGENERIC;
515
516     }
517     return VLC_SUCCESS;
518 }
519
520 /*****************************************************************************
521  * ParseURL: extract user:password
522  *****************************************************************************/
523 static void ParseURL( access_sys_t *p_sys, char *psz_url )
524 {
525     char *psz_dup = strdup( psz_url );
526     char *p = psz_dup;
527     char *psz;
528
529     /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
530     while( *p == '/' )
531     {
532         p++;
533     }
534     psz = p;
535
536     /* Parse auth */
537     if( ( p = strchr( psz, '@' ) ) )
538     {
539         char *comma;
540
541         *p++ = '\0';
542         comma = strchr( psz, ':' );
543
544         /* Retreive user:password */
545         if( comma )
546         {
547             *comma++ = '\0';
548
549             p_sys->psz_user = strdup( psz );
550             p_sys->psz_passwd = strdup( comma );
551         }
552         else
553         {
554             p_sys->psz_user = strdup( psz );
555         }
556     }
557     else
558     {
559         p = psz;
560     }
561
562     /* Parse uri */
563     vlc_UrlParse( &p_sys->url, p, 0 );
564
565     free( psz_dup );
566 }
567
568 /*****************************************************************************
569  * Connect:
570  *****************************************************************************/
571 static int Connect( access_t *p_access, int64_t i_tell )
572 {
573     access_sys_t   *p_sys = p_access->p_sys;
574     vlc_url_t      srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
575     char           *psz;
576
577     /* Clean info */
578     if( p_sys->psz_location ) free( p_sys->psz_location );
579     if( p_sys->psz_mime ) free( p_sys->psz_mime );
580
581     p_sys->psz_location = NULL;
582     p_sys->psz_mime = NULL;
583     p_sys->b_chunked = VLC_FALSE;
584     p_sys->i_chunk = 0;
585
586     p_access->info.i_size = 0;
587     p_access->info.i_pos  = i_tell;
588     p_access->info.b_eof  = VLC_FALSE;
589
590
591     /* Open connection */
592     p_sys->fd = net_OpenTCP( p_access, srv.psz_host, srv.i_port );
593     if( p_sys->fd < 0 )
594     {
595         msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
596         return VLC_EGENERIC;
597     }
598
599     if( p_sys->b_proxy )
600     {
601         net_Printf( VLC_OBJECT(p_access), p_sys->fd,
602                     "GET http://%s:%d/%s HTTP/1.%d\r\n",
603                     p_sys->url.psz_host, p_sys->url.i_port,
604                     p_sys->url.psz_path, p_sys->i_version );
605     }
606     else
607     {
608         char *psz_path = p_sys->url.psz_path;
609         if( !psz_path || !*psz_path )
610         {
611             psz_path = "/";
612         }
613         net_Printf( VLC_OBJECT(p_access), p_sys->fd,
614                     "GET %s HTTP/1.%d\r\nHost: %s\r\n",
615                     psz_path, p_sys->i_version, p_sys->url.psz_host );
616     }
617     /* User Agent */
618     net_Printf( VLC_OBJECT(p_access), p_sys->fd, "User-Agent: %s\r\n",
619                 p_sys->psz_user_agent );
620     /* Offset */
621     if( p_sys->i_version == 1 )
622     {
623         net_Printf( VLC_OBJECT(p_access), p_sys->fd,
624                     "Range: bytes="I64Fd"-\r\n", i_tell );
625     }
626     /* Authentification */
627     if( p_sys->psz_user && *p_sys->psz_user )
628     {
629         char *buf;
630         char *b64;
631
632         asprintf( &buf, "%s:%s", p_sys->psz_user,
633                    p_sys->psz_passwd ? p_sys->psz_passwd : "" );
634
635         b64 = b64_encode( buf );
636
637         net_Printf( VLC_OBJECT(p_access), p_sys->fd,
638                     "Authorization: Basic %s\r\n", b64 );
639         free( b64 );
640     }
641     net_Printf( VLC_OBJECT(p_access), p_sys->fd, "Connection: Close\r\n" );
642
643     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, "\r\n" ) < 0 )
644     {
645         msg_Err( p_access, "failed to send request" );
646         net_Close( p_sys->fd ); p_sys->fd = -1;
647         return VLC_EGENERIC;
648     }
649
650     /* Read Answer */
651     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd ) ) == NULL )
652     {
653         msg_Err( p_access, "failed to read answer" );
654         goto error;
655     }
656     if( !strncmp( psz, "HTTP/1.", 7 ) )
657     {
658         p_sys->psz_protocol = "HTTP";
659         p_sys->i_code = atoi( &psz[9] );
660     }
661     else if( !strncmp( psz, "ICY", 3 ) )
662     {
663         p_sys->psz_protocol = "ICY";
664         p_sys->i_code = atoi( &psz[4] );
665     }
666     else
667     {
668         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
669         free( psz );
670         goto error;
671     }
672     msg_Dbg( p_access, "protocol '%s' answer code %d",
673              p_sys->psz_protocol, p_sys->i_code );
674     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
675     {
676         p_sys->b_seekable = VLC_FALSE;
677     }
678     if( p_sys->i_code != 206 )
679     {
680         p_sys->b_seekable = VLC_FALSE;
681     }
682     if( p_sys->i_code >= 400 )
683     {
684         msg_Err( p_access, "error: %s", psz );
685         free( psz );
686         goto error;
687     }
688     free( psz );
689
690     for( ;; )
691     {
692         char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
693         char *p;
694
695         if( psz == NULL )
696         {
697             msg_Err( p_access, "failed to read answer" );
698             goto error;
699         }
700
701         /* msg_Dbg( p_input, "Line=%s", psz ); */
702         if( *psz == '\0' )
703         {
704             free( psz );
705             break;
706         }
707
708
709         if( ( p = strchr( psz, ':' ) ) == NULL )
710         {
711             msg_Err( p_access, "malformed header line: %s", psz );
712             free( psz );
713             goto error;
714         }
715         *p++ = '\0';
716         while( *p == ' ' ) p++;
717
718         if( !strcasecmp( psz, "Content-Length" ) )
719         {
720             p_access->info.i_size = i_tell + atoll( p );
721             msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
722         }
723         else if( !strcasecmp( psz, "Location" ) )
724         {
725             if( p_sys->psz_location ) free( p_sys->psz_location );
726             p_sys->psz_location = strdup( p );
727         }
728         else if( !strcasecmp( psz, "Content-Type" ) )
729         {
730             if( p_sys->psz_mime ) free( p_sys->psz_mime );
731             p_sys->psz_mime = strdup( p );
732             msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
733         }
734         else if( !strcasecmp( psz, "Transfer-Encoding" ) )
735         {
736             msg_Dbg( p_access, "Transfer-Encoding: %s", p );
737             if( !strncasecmp( p, "chunked", 7 ) )
738             {
739                 p_sys->b_chunked = VLC_TRUE;
740             }
741         }
742
743         free( psz );
744     }
745     return VLC_SUCCESS;
746
747 error:
748     net_Close( p_sys->fd ); p_sys->fd = -1;
749     return VLC_EGENERIC;
750 }
751
752 /*****************************************************************************
753  * b64_encode:
754  *****************************************************************************/
755 static char *b64_encode( unsigned char *src )
756 {
757     static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
758
759     char *dst = malloc( strlen( src ) * 4 / 3 + 12 );
760     char *ret = dst;
761     unsigned i_bits = 0;
762     unsigned i_shift = 0;
763
764     for( ;; )
765     {
766         if( *src )
767         {
768             i_bits = ( i_bits << 8 )|( *src++ );
769             i_shift += 8;
770         }
771         else if( i_shift > 0 )
772         {
773            i_bits <<= 6 - i_shift;
774            i_shift = 6;
775         }
776         else
777         {
778             *dst++ = '=';
779             break;
780         }
781
782         while( i_shift >= 6 )
783         {
784             i_shift -= 6;
785             *dst++ = b64[(i_bits >> i_shift)&0x3f];
786         }
787     }
788
789     *dst++ = '\0';
790
791     return ret;
792 }